chiark / gitweb /
lib/macros.c: Use `void (*)(void)' as the universal function-pointer.
[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 /** @brief Initialize @ref rtp_socket and @ref rtp_log if necessary */
38 static void rtp_init(void) {
39   if(!rtp_socket) {
40     const char *home = getenv("HOME");
41     char *dir, *base;
42     struct utsname uts;
43
44     byte_xasprintf(&dir, "%s/.disorder/", home);
45     mkdir(dir, 02700);
46     if(uname(&uts) < 0)
47       byte_xasprintf(&base, "%s/", dir);
48     else
49       byte_xasprintf(&base, "%s/%s-", dir, uts.nodename);
50     byte_xasprintf(&rtp_socket, "%srtp", base);
51     byte_xasprintf(&rtp_log, "%srtp.log", base);
52   }
53 }
54
55 /** @brief Return a connection to the RTP player's control socket */
56 static FILE *rtp_connect(void) {
57   struct sockaddr_un un;
58   int fd;
59   FILE *fp;
60
61   rtp_init();
62   memset(&un, 0, sizeof un);
63   un.sun_family = AF_UNIX;
64   strcpy(un.sun_path, rtp_socket);
65   fd = xsocket(PF_UNIX, SOCK_STREAM, 0);
66   if(connect(fd, (const struct sockaddr *)&un, sizeof un) < 0) {
67     /* Connection refused just means that the player quit without deleting its
68      * socket */
69     switch(errno) {
70     case ECONNREFUSED:
71     case ENOENT:
72       break;
73     default:
74       /* Anything else may be a problem */
75       fpopup_msg(GTK_MESSAGE_ERROR, "connecting to %s: %s",
76                  rtp_socket, strerror(errno));
77       break;
78     }
79     close(fd);
80     return 0;
81   }
82   if(!(fp = fdopen(fd, "r+"))) {
83     fpopup_msg(GTK_MESSAGE_ERROR, "error calling fdopen: %s", strerror(errno));
84     close(fd);
85     return 0;
86   }
87   return fp;
88 }
89
90 /** @brief Return non-0 iff the RTP player is running */
91 int rtp_running(void) {
92   FILE *fp = rtp_connect();
93
94   if(!fp)
95     return 0;                           /* no connection -> not running */
96   fclose(fp);                           /* connection -> running */
97   return 1;
98 }
99
100 int rtp_getvol(int *l, int *r) {
101   FILE *fp;
102   int rc = -1;
103
104   fp = rtp_connect(); if(!fp) goto end;
105   fprintf(fp, "getvol\n"); fflush(fp);
106   if(fscanf(fp, "%d %d\n", l, r) != 2) goto end;
107   rc = 0;
108 end:
109   if(fp) fclose(fp);
110   return rc;
111 }
112
113 int rtp_setvol(int *l, int *r) {
114   FILE *fp = rtp_connect(); if(!fp) return -1;
115   fprintf(fp, "setvol %d %d\n", *l, *r); fflush(fp);
116   if(fscanf(fp, "%d %d\n", l, r) != 2) { /* do nothing */ }
117   fclose(fp);
118   return 0;
119 }
120
121 /** @brief Activate the RTP player if it is not running */
122 void start_rtp(void) {
123   pid_t pid;
124   int w, fd;
125
126   if(rtp_running())
127     return;                             /* already running */
128   /* double-fork so we don't have to wait() later */
129   if(!(pid = xfork())) {
130     if(setsid() < 0)
131       disorder_fatal(errno, "error calling setsid");
132     if(!xfork()) {
133       /* grandchild */
134       exitfn = _exit;
135       /* log errors and output somewhere reasonably sane.  rtp_running()
136        * will have made sure the directory exists. */
137       if((fd = open(rtp_log, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0)
138         disorder_fatal(errno, "creating %s", rtp_log);
139       if(dup2(fd, 1) < 0
140          || dup2(fd, 2) < 0)
141         disorder_fatal(errno, "dup2");
142       if(close(fd) < 0)
143         disorder_fatal(errno, "close");
144       /* We don't want to hang onto whatever stdin was */
145       if((fd = open("/dev/null", O_RDONLY)) < 0)
146         disorder_fatal(errno, "opening /dev/null");
147       if(dup2(fd, 0) < 0)
148         disorder_fatal(errno, "dup2");
149       if(close(fd) < 0)
150         disorder_fatal(errno, "close");
151       /* execute the player */
152       execlp("disorder-playrtp",
153              "disorder-playrtp",
154              "--socket", rtp_socket,
155              "--api", rtp_api,
156              (char *)0);
157       disorder_fatal(errno, "disorder-playrtp");
158     } else {
159       /* child */
160       _exit(0);
161     }
162   } else {
163     /* parent */
164     while(waitpid(pid, &w, 0) < 0 && errno == EINTR)
165       ;
166   }
167 }
168
169 /** @brief Stop the RTP player if it is running */
170 void stop_rtp(void) {
171   FILE *fp = rtp_connect();
172
173   if(!fp)
174     return;                             /* already stopped */
175   fprintf(fp, "stop\n");
176   fclose(fp);
177 }
178
179 static char *rtp_config_file(void) {
180   static char *rtp_config;
181   const char *home = getenv("HOME");
182   if(!rtp_config)
183     byte_xasprintf(&rtp_config, "%s/.disorder/api", home);
184   return rtp_config;
185 }
186
187 const char *rtp_api;
188
189 void load_rtp_config(void) {
190   char *rtp_config = rtp_config_file();
191   FILE *fp;
192   if((fp = fopen(rtp_config, "r"))) {
193     char *line;
194     if(inputline(rtp_config, fp, &line, '\n') == 0) {
195       for(int n = 0; uaudio_apis[n]; ++n)
196         if(!strcmp(uaudio_apis[n]->name, line))
197           rtp_api = line;
198     }
199     fclose(fp);
200   }
201   if(!rtp_api)
202     rtp_api = uaudio_default(uaudio_apis, UAUDIO_API_CLIENT)->name;
203 }
204
205 void save_rtp_config(void) {
206   if(rtp_api) {
207     char *rtp_config = rtp_config_file();
208     char *tmp;
209     byte_xasprintf(&tmp, "%s.tmp", rtp_config);
210     FILE *fp;
211     if(!(fp = fopen(tmp, "w"))){
212       fpopup_msg(GTK_MESSAGE_ERROR, "error opening %s: %s",
213                  tmp, strerror(errno));
214       return;
215     }
216     if(fprintf(fp, "%s\n", rtp_api) < 0) {
217       fpopup_msg(GTK_MESSAGE_ERROR, "error writing to %s: %s",
218                  tmp, strerror(errno));
219       fclose(fp);
220       return;
221     }
222     if(fclose(fp) < 0) {
223       fpopup_msg(GTK_MESSAGE_ERROR, "error closing %s: %s",
224                  tmp, strerror(errno));
225       return;
226     }
227     if(rename(tmp, rtp_config) < 0) {
228       fpopup_msg(GTK_MESSAGE_ERROR, "error renaming %s: %s",
229                  tmp, strerror(errno));
230     }
231   }
232 }
233
234 void change_rtp_api(const char *api) {
235   if(rtp_api && !strcmp(api, rtp_api))
236     return;                             /* no change */
237   int running = rtp_running();
238   if(running)
239     stop_rtp();
240   rtp_api = api;
241   save_rtp_config();
242   // TODO this is racy and does not work; the player doesn't shut down quickly
243   // enough.
244   if(running)
245     start_rtp();
246 }
247
248 /*
249 Local Variables:
250 c-basic-offset:2
251 comment-column:40
252 fill-column:79
253 indent-tabs-mode:nil
254 End:
255 */