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