bluetooth: Refactor property parsing code
[profile/ivi/pulseaudio-panda.git] / src / utils / pasuspender.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2004-2006 Lennart Poettering
5
6   PulseAudio is free software; you can redistribute it and/or modify
7   it under the terms of the GNU Lesser General Public License as published
8   by the Free Software Foundation; either version 2.1 of the License,
9   or (at your option) any later version.
10
11   PulseAudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with PulseAudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/types.h>
27 #include <sys/wait.h>
28
29 #include <signal.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <assert.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <getopt.h>
37 #include <locale.h>
38
39 #ifdef __linux__
40 #include <sys/prctl.h>
41 #endif
42
43 #include <pulse/pulseaudio.h>
44
45 #include <pulsecore/i18n.h>
46 #include <pulsecore/macro.h>
47
48 static pa_context *context = NULL;
49 static pa_mainloop_api *mainloop_api = NULL;
50 static char **child_argv = NULL;
51 static int child_argc = 0;
52 static pid_t child_pid = (pid_t) -1;
53 static int child_ret = 0;
54 static int dead = 1;
55
56 static void quit(int ret) {
57     pa_assert(mainloop_api);
58     mainloop_api->quit(mainloop_api, ret);
59 }
60
61
62 static void context_drain_complete(pa_context *c, void *userdata) {
63     pa_context_disconnect(c);
64 }
65
66 static void drain(void) {
67     pa_operation *o;
68
69     if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
70         pa_context_disconnect(context);
71     else
72         pa_operation_unref(o);
73 }
74
75 static void start_child(void) {
76
77     if ((child_pid = fork()) < 0) {
78
79         fprintf(stderr, _("fork(): %s\n"), strerror(errno));
80         quit(1);
81
82     } else if (child_pid == 0) {
83         /* Child */
84
85 #ifdef __linux__
86         prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0);
87 #endif
88
89         if (execvp(child_argv[0], child_argv) < 0)
90             fprintf(stderr, _("execvp(): %s\n"), strerror(errno));
91
92         _exit(1);
93
94     } else {
95
96         /* parent */
97         dead = 0;
98     }
99 }
100
101 static void suspend_complete(pa_context *c, int success, void *userdata) {
102     static int n = 0;
103
104     n++;
105
106     if (!success) {
107         fprintf(stderr, _("Failure to suspend: %s\n"), pa_strerror(pa_context_errno(c)));
108         quit(1);
109         return;
110     }
111
112     if (n >= 2)
113         start_child();
114 }
115
116 static void resume_complete(pa_context *c, int success, void *userdata) {
117     static int n = 0;
118
119     n++;
120
121     if (!success) {
122         fprintf(stderr, _("Failure to resume: %s\n"), pa_strerror(pa_context_errno(c)));
123         quit(1);
124         return;
125     }
126
127     if (n >= 2)
128         drain(); /* drain and quit */
129 }
130
131 static void context_state_callback(pa_context *c, void *userdata) {
132     pa_assert(c);
133
134     switch (pa_context_get_state(c)) {
135         case PA_CONTEXT_CONNECTING:
136         case PA_CONTEXT_AUTHORIZING:
137         case PA_CONTEXT_SETTING_NAME:
138             break;
139
140         case PA_CONTEXT_READY:
141             if (pa_context_is_local(c)) {
142                 pa_operation_unref(pa_context_suspend_sink_by_index(c, PA_INVALID_INDEX, 1, suspend_complete, NULL));
143                 pa_operation_unref(pa_context_suspend_source_by_index(c, PA_INVALID_INDEX, 1, suspend_complete, NULL));
144             } else {
145                 fprintf(stderr, _("WARNING: Sound server is not local, not suspending.\n"));
146                 start_child();
147             }
148
149             break;
150
151         case PA_CONTEXT_TERMINATED:
152             quit(0);
153             break;
154
155         case PA_CONTEXT_FAILED:
156         default:
157             fprintf(stderr, _("Connection failure: %s\n"), pa_strerror(pa_context_errno(c)));
158
159             pa_context_unref(context);
160             context = NULL;
161
162             if (child_pid == (pid_t) -1)
163                 /* not started yet, then we do it now */
164                 start_child();
165             else if (dead)
166                 /* already started, and dead, so let's quit */
167                 quit(1);
168
169             break;
170     }
171 }
172
173 static void sigint_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
174     fprintf(stderr, _("Got SIGINT, exiting.\n"));
175     quit(0);
176 }
177
178 static void sigchld_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
179     int status = 0;
180     pid_t p;
181
182     p = waitpid(-1, &status, WNOHANG);
183
184     if (p != child_pid)
185         return;
186
187     dead = 1;
188
189     if (WIFEXITED(status))
190         child_ret = WEXITSTATUS(status);
191     else if (WIFSIGNALED(status)) {
192         fprintf(stderr, _("WARNING: Child process terminated by signal %u\n"), WTERMSIG(status));
193         child_ret = 1;
194     }
195
196     if (context) {
197         if (pa_context_is_local(context)) {
198             /* A context is around, so let's resume */
199             pa_operation_unref(pa_context_suspend_sink_by_index(context, PA_INVALID_INDEX, 0, resume_complete, NULL));
200             pa_operation_unref(pa_context_suspend_source_by_index(context, PA_INVALID_INDEX, 0, resume_complete, NULL));
201         } else
202             drain();
203     } else
204         /* Hmm, no context here, so let's terminate right away */
205         quit(0);
206 }
207
208 static void help(const char *argv0) {
209
210     printf(_("%s [options] ... \n\n"
211            "  -h, --help                            Show this help\n"
212            "      --version                         Show version\n"
213            "  -s, --server=SERVER                   The name of the server to connect to\n\n"),
214            argv0);
215 }
216
217 enum {
218     ARG_VERSION = 256
219 };
220
221 int main(int argc, char *argv[]) {
222     pa_mainloop* m = NULL;
223     int c, ret = 1;
224     char *server = NULL, *bn;
225
226     static const struct option long_options[] = {
227         {"server",      1, NULL, 's'},
228         {"version",     0, NULL, ARG_VERSION},
229         {"help",        0, NULL, 'h'},
230         {NULL,          0, NULL, 0}
231     };
232
233     setlocale(LC_ALL, "");
234 #ifdef ENABLE_NLS
235     bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
236 #endif
237
238     bn = pa_path_get_filename(argv[0]);
239
240     while ((c = getopt_long(argc, argv, "s:h", long_options, NULL)) != -1) {
241         switch (c) {
242             case 'h' :
243                 help(bn);
244                 ret = 0;
245                 goto quit;
246
247             case ARG_VERSION:
248                 printf(_("pasuspender %s\n"
249                          "Compiled with libpulse %s\n"
250                          "Linked with libpulse %s\n"),
251                        PACKAGE_VERSION,
252                        pa_get_headers_version(),
253                        pa_get_library_version());
254                 ret = 0;
255                 goto quit;
256
257             case 's':
258                 pa_xfree(server);
259                 server = pa_xstrdup(optarg);
260                 break;
261
262             default:
263                 goto quit;
264         }
265     }
266
267     child_argv = argv + optind;
268     child_argc = argc - optind;
269
270     if (child_argc <= 0) {
271         help(bn);
272         ret = 0;
273         goto quit;
274     }
275
276     if (!(m = pa_mainloop_new())) {
277         fprintf(stderr, _("pa_mainloop_new() failed.\n"));
278         goto quit;
279     }
280
281     pa_assert_se(mainloop_api = pa_mainloop_get_api(m));
282     pa_assert_se(pa_signal_init(mainloop_api) == 0);
283     pa_signal_new(SIGINT, sigint_callback, NULL);
284     pa_signal_new(SIGCHLD, sigchld_callback, NULL);
285 #ifdef SIGPIPE
286     signal(SIGPIPE, SIG_IGN);
287 #endif
288
289     if (!(context = pa_context_new(mainloop_api, bn))) {
290         fprintf(stderr, _("pa_context_new() failed.\n"));
291         goto quit;
292     }
293
294     pa_context_set_state_callback(context, context_state_callback, NULL);
295     pa_context_connect(context, server, PA_CONTEXT_NOAUTOSPAWN, NULL);
296
297     if (pa_mainloop_run(m, &ret) < 0) {
298         fprintf(stderr, _("pa_mainloop_run() failed.\n"));
299         goto quit;
300     }
301
302 quit:
303     if (context)
304         pa_context_unref(context);
305
306     if (m) {
307         pa_signal_done();
308         pa_mainloop_free(m);
309     }
310
311     pa_xfree(server);
312
313     if (!dead)
314         kill(child_pid, SIGTERM);
315
316     return ret == 0 ? child_ret : ret;
317 }