build-sys: Use ax_check_flag macros from autoconf archive
[pulseaudio-mirror.git] / src / utils / pasuspender.c
blobe1ee2512187c0ee46c238ff7b2fc7aa4d51e8ecc
1 /***
2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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.
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.
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 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <sys/types.h>
27 #include <sys/wait.h>
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>
39 #ifdef __linux__
40 #include <sys/prctl.h>
41 #endif
43 #include <pulse/i18n.h>
44 #include <pulse/pulseaudio.h>
45 #include <pulsecore/macro.h>
47 static pa_context *context = NULL;
48 static pa_mainloop_api *mainloop_api = NULL;
49 static char **child_argv = NULL;
50 static int child_argc = 0;
51 static pid_t child_pid = (pid_t) -1;
52 static int child_ret = 0;
53 static int dead = 1;
55 static void quit(int ret) {
56 pa_assert(mainloop_api);
57 mainloop_api->quit(mainloop_api, ret);
61 static void context_drain_complete(pa_context *c, void *userdata) {
62 pa_context_disconnect(c);
65 static void drain(void) {
66 pa_operation *o;
68 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
69 pa_context_disconnect(context);
70 else
71 pa_operation_unref(o);
74 static void start_child(void) {
76 if ((child_pid = fork()) < 0) {
78 fprintf(stderr, _("fork(): %s\n"), strerror(errno));
79 quit(1);
81 } else if (child_pid == 0) {
82 /* Child */
84 #ifdef __linux__
85 prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0);
86 #endif
88 if (execvp(child_argv[0], child_argv) < 0)
89 fprintf(stderr, _("execvp(): %s\n"), strerror(errno));
91 _exit(1);
93 } else {
95 /* parent */
96 dead = 0;
100 static void suspend_complete(pa_context *c, int success, void *userdata) {
101 static int n = 0;
103 n++;
105 if (!success) {
106 fprintf(stderr, _("Failure to suspend: %s\n"), pa_strerror(pa_context_errno(c)));
107 quit(1);
108 return;
111 if (n >= 2)
112 start_child();
115 static void resume_complete(pa_context *c, int success, void *userdata) {
116 static int n = 0;
118 n++;
120 if (!success) {
121 fprintf(stderr, _("Failure to resume: %s\n"), pa_strerror(pa_context_errno(c)));
122 quit(1);
123 return;
126 if (n >= 2)
127 drain(); /* drain and quit */
130 static void context_state_callback(pa_context *c, void *userdata) {
131 pa_assert(c);
133 switch (pa_context_get_state(c)) {
134 case PA_CONTEXT_CONNECTING:
135 case PA_CONTEXT_AUTHORIZING:
136 case PA_CONTEXT_SETTING_NAME:
137 break;
139 case PA_CONTEXT_READY:
140 if (pa_context_is_local(c)) {
141 pa_operation_unref(pa_context_suspend_sink_by_index(c, PA_INVALID_INDEX, 1, suspend_complete, NULL));
142 pa_operation_unref(pa_context_suspend_source_by_index(c, PA_INVALID_INDEX, 1, suspend_complete, NULL));
143 } else {
144 fprintf(stderr, _("WARNING: Sound server is not local, not suspending.\n"));
145 start_child();
148 break;
150 case PA_CONTEXT_TERMINATED:
151 quit(0);
152 break;
154 case PA_CONTEXT_FAILED:
155 default:
156 fprintf(stderr, _("Connection failure: %s\n"), pa_strerror(pa_context_errno(c)));
158 pa_context_unref(context);
159 context = NULL;
161 if (child_pid == (pid_t) -1)
162 /* not started yet, then we do it now */
163 start_child();
164 else if (dead)
165 /* already started, and dead, so let's quit */
166 quit(1);
168 break;
172 static void sigint_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
173 fprintf(stderr, _("Got SIGINT, exiting.\n"));
174 quit(0);
177 static void sigchld_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
178 int status = 0;
179 pid_t p;
181 p = waitpid(-1, &status, WNOHANG);
183 if (p != child_pid)
184 return;
186 dead = 1;
188 if (WIFEXITED(status))
189 child_ret = WEXITSTATUS(status);
190 else if (WIFSIGNALED(status)) {
191 fprintf(stderr, _("WARNING: Child process terminated by signal %u\n"), WTERMSIG(status));
192 child_ret = 1;
195 if (context) {
196 if (pa_context_is_local(context)) {
197 /* A context is around, so let's resume */
198 pa_operation_unref(pa_context_suspend_sink_by_index(context, PA_INVALID_INDEX, 0, resume_complete, NULL));
199 pa_operation_unref(pa_context_suspend_source_by_index(context, PA_INVALID_INDEX, 0, resume_complete, NULL));
200 } else
201 drain();
202 } else
203 /* Hmm, no context here, so let's terminate right away */
204 quit(0);
207 static void help(const char *argv0) {
209 printf(_("%s [options] ... \n\n"
210 " -h, --help Show this help\n"
211 " --version Show version\n"
212 " -s, --server=SERVER The name of the server to connect to\n\n"),
213 argv0);
216 enum {
217 ARG_VERSION = 256
220 int main(int argc, char *argv[]) {
221 pa_mainloop* m = NULL;
222 int c, ret = 1;
223 char *server = NULL, *bn;
225 static const struct option long_options[] = {
226 {"server", 1, NULL, 's'},
227 {"version", 0, NULL, ARG_VERSION},
228 {"help", 0, NULL, 'h'},
229 {NULL, 0, NULL, 0}
232 setlocale(LC_ALL, "");
233 bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
235 bn = pa_path_get_filename(argv[0]);
237 while ((c = getopt_long(argc, argv, "s:h", long_options, NULL)) != -1) {
238 switch (c) {
239 case 'h' :
240 help(bn);
241 ret = 0;
242 goto quit;
244 case ARG_VERSION:
245 printf(_("pasuspender %s\n"
246 "Compiled with libpulse %s\n"
247 "Linked with libpulse %s\n"),
248 PACKAGE_VERSION,
249 pa_get_headers_version(),
250 pa_get_library_version());
251 ret = 0;
252 goto quit;
254 case 's':
255 pa_xfree(server);
256 server = pa_xstrdup(optarg);
257 break;
259 default:
260 goto quit;
264 child_argv = argv + optind;
265 child_argc = argc - optind;
267 if (child_argc <= 0) {
268 help(bn);
269 ret = 0;
270 goto quit;
273 if (!(m = pa_mainloop_new())) {
274 fprintf(stderr, _("pa_mainloop_new() failed.\n"));
275 goto quit;
278 pa_assert_se(mainloop_api = pa_mainloop_get_api(m));
279 pa_assert_se(pa_signal_init(mainloop_api) == 0);
280 pa_signal_new(SIGINT, sigint_callback, NULL);
281 pa_signal_new(SIGCHLD, sigchld_callback, NULL);
282 #ifdef SIGPIPE
283 signal(SIGPIPE, SIG_IGN);
284 #endif
286 if (!(context = pa_context_new(mainloop_api, bn))) {
287 fprintf(stderr, _("pa_context_new() failed.\n"));
288 goto quit;
291 pa_context_set_state_callback(context, context_state_callback, NULL);
292 pa_context_connect(context, server, PA_CONTEXT_NOAUTOSPAWN, NULL);
294 if (pa_mainloop_run(m, &ret) < 0) {
295 fprintf(stderr, _("pa_mainloop_run() failed.\n"));
296 goto quit;
299 quit:
300 if (context)
301 pa_context_unref(context);
303 if (m) {
304 pa_signal_done();
305 pa_mainloop_free(m);
308 pa_xfree(server);
310 if (!dead)
311 kill(child_pid, SIGTERM);
313 return ret == 0 ? child_ret : ret;