Commit | Line | Data |
---|---|---|
a99c4e9a RK |
1 | /* |
2 | * This file is part of Disobedience | |
9efa107e | 3 | * Copyright (C) 2007-2010 Richard Kettlewell |
a99c4e9a | 4 | * |
e7eb3a27 | 5 | * This program is free software: you can redistribute it and/or modify |
a99c4e9a | 6 | * it under the terms of the GNU General Public License as published by |
e7eb3a27 | 7 | * the Free Software Foundation, either version 3 of the License, or |
a99c4e9a RK |
8 | * (at your option) any later version. |
9 | * | |
e7eb3a27 RK |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
a99c4e9a | 15 | * You should have received a copy of the GNU General Public License |
e7eb3a27 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
a99c4e9a RK |
17 | */ |
18 | /** @file disobedience/rtp.c | |
19 | * @brief RTP player support for Disobedience | |
20 | */ | |
21 | ||
22 | #include "disobedience.h" | |
9fbe0996 | 23 | #include <sys/types.h> |
a99c4e9a RK |
24 | #include <sys/stat.h> |
25 | #include <unistd.h> | |
26 | #include <fcntl.h> | |
27 | #include <sys/un.h> | |
c764e004 | 28 | #include <sys/utsname.h> |
9fbe0996 | 29 | #include <sys/wait.h> |
a99c4e9a RK |
30 | |
31 | /** @brief Path to RTP player's control socket */ | |
32 | static char *rtp_socket; | |
33 | ||
34 | /** @brief Path to RTP player's logfile */ | |
35 | static char *rtp_log; | |
36 | ||
53a4a6fd MW |
37 | static char *substitute_instance_name(void) { |
38 | const char *p = config->rtp_instance_name; | |
39 | struct utsname uts; | |
40 | size_t n; | |
41 | int ok; | |
42 | struct dynstr z; | |
43 | ||
44 | ok = (uname(&uts) >= 0); | |
45 | if(!p) p = "%h-rtp"; | |
46 | dynstr_init(&z); | |
47 | for(;;) { | |
48 | n = strcspn(p, "%"); | |
49 | if(n) { dynstr_append_bytes(&z, p, n); p += n; } | |
50 | if(!*p) break; | |
51 | p++; | |
52 | switch(*p) { | |
53 | case '%': dynstr_append_bytes(&z, "%", 1); p++; break; | |
54 | case 'h': | |
55 | if(ok) { dynstr_append_string(&z, uts.nodename); p++; } | |
56 | else { p++; if(*p) p++; } | |
57 | case 0: break; | |
58 | default: dynstr_append_bytes(&z, p - 1, 2); p++; break; | |
59 | } | |
60 | } | |
61 | dynstr_terminate(&z); | |
62 | return z.vec; | |
63 | } | |
64 | ||
a99c4e9a RK |
65 | /** @brief Initialize @ref rtp_socket and @ref rtp_log if necessary */ |
66 | static void rtp_init(void) { | |
67 | if(!rtp_socket) { | |
e1e3ef08 MW |
68 | const char *dir, *instance; |
69 | if(!(dir = profile_directory())) | |
70 | disorder_fatal(0, "failed to find profile directory"); | |
a99c4e9a | 71 | mkdir(dir, 02700); |
53a4a6fd | 72 | instance = substitute_instance_name(); |
e1e3ef08 MW |
73 | byte_xasprintf(&rtp_socket, "%s/%s", dir, instance); |
74 | byte_xasprintf(&rtp_log, "%s/%s.log", dir, instance); | |
a99c4e9a RK |
75 | } |
76 | } | |
77 | ||
78 | /** @brief Return a connection to the RTP player's control socket */ | |
79 | static FILE *rtp_connect(void) { | |
80 | struct sockaddr_un un; | |
81 | int fd; | |
82 | FILE *fp; | |
83 | ||
84 | rtp_init(); | |
85 | memset(&un, 0, sizeof un); | |
86 | un.sun_family = AF_UNIX; | |
87 | strcpy(un.sun_path, rtp_socket); | |
88 | fd = xsocket(PF_UNIX, SOCK_STREAM, 0); | |
89 | if(connect(fd, (const struct sockaddr *)&un, sizeof un) < 0) { | |
90 | /* Connection refused just means that the player quit without deleting its | |
91 | * socket */ | |
92 | switch(errno) { | |
93 | case ECONNREFUSED: | |
94 | case ENOENT: | |
95 | break; | |
96 | default: | |
97 | /* Anything else may be a problem */ | |
98 | fpopup_msg(GTK_MESSAGE_ERROR, "connecting to %s: %s", | |
99 | rtp_socket, strerror(errno)); | |
100 | break; | |
101 | } | |
102 | close(fd); | |
103 | return 0; | |
104 | } | |
105 | if(!(fp = fdopen(fd, "r+"))) { | |
106 | fpopup_msg(GTK_MESSAGE_ERROR, "error calling fdopen: %s", strerror(errno)); | |
107 | close(fd); | |
108 | return 0; | |
109 | } | |
110 | return fp; | |
111 | } | |
112 | ||
113 | /** @brief Return non-0 iff the RTP player is running */ | |
114 | int rtp_running(void) { | |
115 | FILE *fp = rtp_connect(); | |
116 | ||
117 | if(!fp) | |
118 | return 0; /* no connection -> not running */ | |
119 | fclose(fp); /* connection -> running */ | |
120 | return 1; | |
121 | } | |
122 | ||
af21fb6b MW |
123 | int rtp_getvol(int *l, int *r) { |
124 | FILE *fp; | |
125 | int rc = -1; | |
126 | ||
127 | fp = rtp_connect(); if(!fp) goto end; | |
128 | fprintf(fp, "getvol\n"); fflush(fp); | |
129 | if(fscanf(fp, "%d %d\n", l, r) != 2) goto end; | |
130 | rc = 0; | |
131 | end: | |
132 | if(fp) fclose(fp); | |
133 | return rc; | |
134 | } | |
135 | ||
136 | int rtp_setvol(int *l, int *r) { | |
137 | FILE *fp = rtp_connect(); if(!fp) return -1; | |
138 | fprintf(fp, "setvol %d %d\n", *l, *r); fflush(fp); | |
139 | if(fscanf(fp, "%d %d\n", l, r) != 2) { /* do nothing */ } | |
140 | fclose(fp); | |
141 | return 0; | |
142 | } | |
143 | ||
a99c4e9a RK |
144 | /** @brief Activate the RTP player if it is not running */ |
145 | void start_rtp(void) { | |
146 | pid_t pid; | |
147 | int w, fd; | |
148 | ||
149 | if(rtp_running()) | |
150 | return; /* already running */ | |
151 | /* double-fork so we don't have to wait() later */ | |
152 | if(!(pid = xfork())) { | |
153 | if(setsid() < 0) | |
2e9ba080 | 154 | disorder_fatal(errno, "error calling setsid"); |
9efa107e | 155 | if(!xfork()) { |
a99c4e9a RK |
156 | /* grandchild */ |
157 | exitfn = _exit; | |
158 | /* log errors and output somewhere reasonably sane. rtp_running() | |
159 | * will have made sure the directory exists. */ | |
160 | if((fd = open(rtp_log, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) | |
2e9ba080 | 161 | disorder_fatal(errno, "creating %s", rtp_log); |
a99c4e9a RK |
162 | if(dup2(fd, 1) < 0 |
163 | || dup2(fd, 2) < 0) | |
2e9ba080 | 164 | disorder_fatal(errno, "dup2"); |
a99c4e9a | 165 | if(close(fd) < 0) |
2e9ba080 | 166 | disorder_fatal(errno, "close"); |
a99c4e9a RK |
167 | /* We don't want to hang onto whatever stdin was */ |
168 | if((fd = open("/dev/null", O_RDONLY)) < 0) | |
2e9ba080 | 169 | disorder_fatal(errno, "opening /dev/null"); |
a99c4e9a | 170 | if(dup2(fd, 0) < 0) |
2e9ba080 | 171 | disorder_fatal(errno, "dup2"); |
a99c4e9a | 172 | if(close(fd) < 0) |
2e9ba080 | 173 | disorder_fatal(errno, "close"); |
a99c4e9a RK |
174 | /* execute the player */ |
175 | execlp("disorder-playrtp", | |
a2364abb RK |
176 | "disorder-playrtp", |
177 | "--socket", rtp_socket, | |
178 | "--api", rtp_api, | |
179 | (char *)0); | |
2e9ba080 | 180 | disorder_fatal(errno, "disorder-playrtp"); |
a99c4e9a RK |
181 | } else { |
182 | /* child */ | |
183 | _exit(0); | |
184 | } | |
185 | } else { | |
186 | /* parent */ | |
187 | while(waitpid(pid, &w, 0) < 0 && errno == EINTR) | |
188 | ; | |
189 | } | |
190 | } | |
191 | ||
192 | /** @brief Stop the RTP player if it is running */ | |
193 | void stop_rtp(void) { | |
194 | FILE *fp = rtp_connect(); | |
195 | ||
196 | if(!fp) | |
197 | return; /* already stopped */ | |
198 | fprintf(fp, "stop\n"); | |
199 | fclose(fp); | |
200 | } | |
201 | ||
a2364abb RK |
202 | static char *rtp_config_file(void) { |
203 | static char *rtp_config; | |
a2364abb | 204 | if(!rtp_config) |
e1e3ef08 | 205 | rtp_config = profile_filename("api"); |
a2364abb RK |
206 | return rtp_config; |
207 | } | |
208 | ||
209 | const char *rtp_api; | |
210 | ||
211 | void load_rtp_config(void) { | |
212 | char *rtp_config = rtp_config_file(); | |
213 | FILE *fp; | |
214 | if((fp = fopen(rtp_config, "r"))) { | |
215 | char *line; | |
216 | if(inputline(rtp_config, fp, &line, '\n') == 0) { | |
217 | for(int n = 0; uaudio_apis[n]; ++n) | |
218 | if(!strcmp(uaudio_apis[n]->name, line)) | |
219 | rtp_api = line; | |
220 | } | |
221 | fclose(fp); | |
222 | } | |
223 | if(!rtp_api) | |
224 | rtp_api = uaudio_default(uaudio_apis, UAUDIO_API_CLIENT)->name; | |
225 | } | |
226 | ||
227 | void save_rtp_config(void) { | |
228 | if(rtp_api) { | |
229 | char *rtp_config = rtp_config_file(); | |
230 | char *tmp; | |
231 | byte_xasprintf(&tmp, "%s.tmp", rtp_config); | |
232 | FILE *fp; | |
233 | if(!(fp = fopen(tmp, "w"))){ | |
234 | fpopup_msg(GTK_MESSAGE_ERROR, "error opening %s: %s", | |
235 | tmp, strerror(errno)); | |
236 | return; | |
237 | } | |
238 | if(fprintf(fp, "%s\n", rtp_api) < 0) { | |
239 | fpopup_msg(GTK_MESSAGE_ERROR, "error writing to %s: %s", | |
240 | tmp, strerror(errno)); | |
241 | fclose(fp); | |
242 | return; | |
243 | } | |
244 | if(fclose(fp) < 0) { | |
245 | fpopup_msg(GTK_MESSAGE_ERROR, "error closing %s: %s", | |
246 | tmp, strerror(errno)); | |
247 | return; | |
248 | } | |
249 | if(rename(tmp, rtp_config) < 0) { | |
250 | fpopup_msg(GTK_MESSAGE_ERROR, "error renaming %s: %s", | |
251 | tmp, strerror(errno)); | |
252 | } | |
253 | } | |
254 | } | |
255 | ||
256 | void change_rtp_api(const char *api) { | |
257 | if(rtp_api && !strcmp(api, rtp_api)) | |
258 | return; /* no change */ | |
259 | int running = rtp_running(); | |
260 | if(running) | |
261 | stop_rtp(); | |
262 | rtp_api = api; | |
263 | save_rtp_config(); | |
264 | // TODO this is racy and does not work; the player doesn't shut down quickly | |
265 | // enough. | |
266 | if(running) | |
267 | start_rtp(); | |
268 | } | |
269 | ||
a99c4e9a RK |
270 | /* |
271 | Local Variables: | |
272 | c-basic-offset:2 | |
273 | comment-column:40 | |
274 | fill-column:79 | |
275 | indent-tabs-mode:nil | |
276 | End: | |
277 | */ |