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