]> code.delx.au - pulseaudio/blob - src/utils/pasuspender.c
i18n: Update Greek translation
[pulseaudio] / 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 static int fork_failed = 0;
56
57 static void quit(int ret) {
58 pa_assert(mainloop_api);
59 mainloop_api->quit(mainloop_api, ret);
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 (context) {
70 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
71 pa_context_disconnect(context);
72 else
73 pa_operation_unref(o);
74 } else
75 quit(0);
76 }
77
78 static int start_child(void) {
79
80 if ((child_pid = fork()) < 0) {
81 fprintf(stderr, _("fork(): %s\n"), strerror(errno));
82 fork_failed = 1;
83
84 return -1;
85
86 } else if (child_pid == 0) {
87 /* Child */
88
89 #ifdef __linux__
90 prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0);
91 #endif
92
93 if (execvp(child_argv[0], child_argv) < 0)
94 fprintf(stderr, _("execvp(): %s\n"), strerror(errno));
95
96 _exit(1);
97
98 } else {
99
100 /* parent */
101 dead = 0;
102 }
103
104 return 0;
105 }
106
107 static void resume_complete(pa_context *c, int success, void *userdata) {
108 static int n = 0;
109
110 n++;
111
112 if (!success) {
113 fprintf(stderr, _("Failure to resume: %s\n"), pa_strerror(pa_context_errno(c)));
114 quit(1);
115 return;
116 }
117
118 if (n >= 2)
119 drain(); /* drain and quit */
120 }
121
122 static void resume(void) {
123 static int n = 0;
124
125 n++;
126
127 if (n > 1)
128 return;
129
130 if (context) {
131 if (pa_context_is_local(context)) {
132 pa_operation_unref(pa_context_suspend_sink_by_index(context, PA_INVALID_INDEX, 0, resume_complete, NULL));
133 pa_operation_unref(pa_context_suspend_source_by_index(context, PA_INVALID_INDEX, 0, resume_complete, NULL));
134 } else
135 drain();
136 } else {
137 quit(0);
138 }
139 }
140
141 static void suspend_complete(pa_context *c, int success, void *userdata) {
142 static int n = 0;
143
144 n++;
145
146 if (!success) {
147 fprintf(stderr, _("Failure to suspend: %s\n"), pa_strerror(pa_context_errno(c)));
148 quit(1);
149 return;
150 }
151
152 if (n >= 2) {
153 if (start_child() < 0)
154 resume();
155 }
156 }
157
158 static void context_state_callback(pa_context *c, void *userdata) {
159 pa_assert(c);
160
161 switch (pa_context_get_state(c)) {
162 case PA_CONTEXT_CONNECTING:
163 case PA_CONTEXT_AUTHORIZING:
164 case PA_CONTEXT_SETTING_NAME:
165 break;
166
167 case PA_CONTEXT_READY:
168 if (pa_context_is_local(c)) {
169 pa_operation_unref(pa_context_suspend_sink_by_index(c, PA_INVALID_INDEX, 1, suspend_complete, NULL));
170 pa_operation_unref(pa_context_suspend_source_by_index(c, PA_INVALID_INDEX, 1, suspend_complete, NULL));
171 } else {
172 fprintf(stderr, _("WARNING: Sound server is not local, not suspending.\n"));
173 if (start_child() < 0)
174 drain();
175 }
176
177 break;
178
179 case PA_CONTEXT_TERMINATED:
180 quit(0);
181 break;
182
183 case PA_CONTEXT_FAILED:
184 default:
185 fprintf(stderr, _("Connection failure: %s\n"), pa_strerror(pa_context_errno(c)));
186
187 pa_context_unref(context);
188 context = NULL;
189
190 if (child_pid == (pid_t) -1) {
191 /* not started yet, then we do it now */
192 if (start_child() < 0)
193 quit(1);
194 } else if (dead)
195 /* already started, and dead, so let's quit */
196 quit(1);
197
198 break;
199 }
200 }
201
202 static void sigint_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
203 fprintf(stderr, _("Got SIGINT, exiting.\n"));
204 resume();
205 }
206
207 static void sigchld_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
208 int status = 0;
209 pid_t p;
210
211 p = waitpid(-1, &status, WNOHANG);
212
213 if (p != child_pid)
214 return;
215
216 dead = 1;
217
218 if (WIFEXITED(status))
219 child_ret = WEXITSTATUS(status);
220 else if (WIFSIGNALED(status)) {
221 fprintf(stderr, _("WARNING: Child process terminated by signal %u\n"), WTERMSIG(status));
222 child_ret = 1;
223 }
224
225 resume();
226 }
227
228 static void help(const char *argv0) {
229
230 printf(_("%s [options] ... \n\n"
231 " -h, --help Show this help\n"
232 " --version Show version\n"
233 " -s, --server=SERVER The name of the server to connect to\n\n"),
234 argv0);
235 }
236
237 enum {
238 ARG_VERSION = 256
239 };
240
241 int main(int argc, char *argv[]) {
242 pa_mainloop* m = NULL;
243 int c, ret = 1;
244 char *server = NULL, *bn;
245
246 static const struct option long_options[] = {
247 {"server", 1, NULL, 's'},
248 {"version", 0, NULL, ARG_VERSION},
249 {"help", 0, NULL, 'h'},
250 {NULL, 0, NULL, 0}
251 };
252
253 setlocale(LC_ALL, "");
254 #ifdef ENABLE_NLS
255 bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
256 #endif
257
258 bn = pa_path_get_filename(argv[0]);
259
260 while ((c = getopt_long(argc, argv, "s:h", long_options, NULL)) != -1) {
261 switch (c) {
262 case 'h' :
263 help(bn);
264 ret = 0;
265 goto quit;
266
267 case ARG_VERSION:
268 printf(_("pasuspender %s\n"
269 "Compiled with libpulse %s\n"
270 "Linked with libpulse %s\n"),
271 PACKAGE_VERSION,
272 pa_get_headers_version(),
273 pa_get_library_version());
274 ret = 0;
275 goto quit;
276
277 case 's':
278 pa_xfree(server);
279 server = pa_xstrdup(optarg);
280 break;
281
282 default:
283 goto quit;
284 }
285 }
286
287 child_argv = argv + optind;
288 child_argc = argc - optind;
289
290 if (child_argc <= 0) {
291 help(bn);
292 ret = 0;
293 goto quit;
294 }
295
296 if (!(m = pa_mainloop_new())) {
297 fprintf(stderr, _("pa_mainloop_new() failed.\n"));
298 goto quit;
299 }
300
301 pa_assert_se(mainloop_api = pa_mainloop_get_api(m));
302 pa_assert_se(pa_signal_init(mainloop_api) == 0);
303 pa_signal_new(SIGINT, sigint_callback, NULL);
304 pa_signal_new(SIGCHLD, sigchld_callback, NULL);
305 #ifdef SIGPIPE
306 signal(SIGPIPE, SIG_IGN);
307 #endif
308
309 if (!(context = pa_context_new(mainloop_api, bn))) {
310 fprintf(stderr, _("pa_context_new() failed.\n"));
311 goto quit;
312 }
313
314 pa_context_set_state_callback(context, context_state_callback, NULL);
315
316 if (pa_context_connect(context, server, PA_CONTEXT_NOAUTOSPAWN, NULL) < 0) {
317 fprintf(stderr, "pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(context)));
318 goto quit;
319 }
320
321 if (pa_mainloop_run(m, &ret) < 0) {
322 fprintf(stderr, _("pa_mainloop_run() failed.\n"));
323 goto quit;
324 }
325
326 if (ret == 0 && fork_failed)
327 ret = 1;
328
329 quit:
330 if (context)
331 pa_context_unref(context);
332
333 if (m) {
334 pa_signal_done();
335 pa_mainloop_free(m);
336 }
337
338 pa_xfree(server);
339
340 if (!dead)
341 kill(child_pid, SIGTERM);
342
343 return ret == 0 ? child_ret : ret;
344 }