]>
code.delx.au - pulseaudio/blob - src/utils/pasuspender.c
4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <sys/types.h>
44 #include <sys/prctl.h>
47 #include <pulse/pulseaudio.h>
48 #include <pulsecore/macro.h>
50 #if PA_API_VERSION < 10
51 #error Invalid PulseAudio API version
56 static pa_context
*context
= NULL
;
57 static pa_mainloop_api
*mainloop_api
= NULL
;
58 static char **child_argv
= NULL
;
59 static int child_argc
= 0;
60 static pid_t child_pid
= (pid_t
) -1;
61 static int child_ret
= 0;
64 static void quit(int ret
) {
65 pa_assert(mainloop_api
);
66 mainloop_api
->quit(mainloop_api
, ret
);
70 static void context_drain_complete(pa_context
*c
, void *userdata
) {
71 pa_context_disconnect(c
);
74 static void drain(void) {
77 if (!(o
= pa_context_drain(context
, context_drain_complete
, NULL
)))
78 pa_context_disconnect(context
);
80 pa_operation_unref(o
);
83 static void start_child(void) {
85 if ((child_pid
= fork()) < 0) {
87 fprintf(stderr
, "fork(): %s\n", strerror(errno
));
90 } else if (child_pid
== 0) {
94 prctl(PR_SET_PDEATHSIG
, SIGTERM
, 0, 0, 0);
97 if (execvp(child_argv
[0], child_argv
) < 0)
98 fprintf(stderr
, "execvp(): %s\n", strerror(errno
));
109 static void suspend_complete(pa_context
*c
, int success
, void *userdata
) {
115 fprintf(stderr
, "Failure to suspend: %s\n", pa_strerror(pa_context_errno(c
)));
124 static void resume_complete(pa_context
*c
, int success
, void *userdata
) {
130 fprintf(stderr
, "Failure to resume: %s\n", pa_strerror(pa_context_errno(c
)));
136 drain(); /* drain and quit */
139 static void context_state_callback(pa_context
*c
, void *userdata
) {
142 switch (pa_context_get_state(c
)) {
143 case PA_CONTEXT_CONNECTING
:
144 case PA_CONTEXT_AUTHORIZING
:
145 case PA_CONTEXT_SETTING_NAME
:
148 case PA_CONTEXT_READY
:
149 if (pa_context_is_local(c
)) {
150 pa_operation_unref(pa_context_suspend_sink_by_index(c
, PA_INVALID_INDEX
, 1, suspend_complete
, NULL
));
151 pa_operation_unref(pa_context_suspend_source_by_index(c
, PA_INVALID_INDEX
, 1, suspend_complete
, NULL
));
157 case PA_CONTEXT_TERMINATED
:
161 case PA_CONTEXT_FAILED
:
163 fprintf(stderr
, "Connection failure: %s\n", pa_strerror(pa_context_errno(c
)));
165 pa_context_unref(context
);
168 if (child_pid
== (pid_t
) -1)
169 /* not started yet, then we do it now */
172 /* already started, and dead, so let's quit */
179 static void sigint_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
180 fprintf(stderr
, "Got SIGINT, exiting.\n");
184 static void sigchld_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
188 p
= waitpid(-1, &status
, WNOHANG
);
195 if (WIFEXITED(status
))
196 child_ret
= WEXITSTATUS(status
);
197 else if (WIFSIGNALED(status
)) {
198 fprintf(stderr
, "WARNING: Child process terminated by signal %u\n", WTERMSIG(status
));
203 if (pa_context_is_local(context
)) {
204 /* A context is around, so let's resume */
205 pa_operation_unref(pa_context_suspend_sink_by_index(context
, PA_INVALID_INDEX
, 0, resume_complete
, NULL
));
206 pa_operation_unref(pa_context_suspend_source_by_index(context
, PA_INVALID_INDEX
, 0, resume_complete
, NULL
));
210 /* Hmm, no context here, so let's terminate right away */
214 static void help(const char *argv0
) {
216 printf("%s [options] ... \n\n"
217 " -h, --help Show this help\n"
218 " --version Show version\n"
219 " -s, --server=SERVER The name of the server to connect to\n\n",
227 int main(int argc
, char *argv
[]) {
228 pa_mainloop
* m
= NULL
;
230 char *server
= NULL
, *bn
;
232 static const struct option long_options
[] = {
233 {"server", 1, NULL
, 's'},
234 {"version", 0, NULL
, ARG_VERSION
},
235 {"help", 0, NULL
, 'h'},
239 if (!(bn
= strrchr(argv
[0], '/')))
244 while ((c
= getopt_long(argc
, argv
, "s:h", long_options
, NULL
)) != -1) {
252 printf("pasuspender "PACKAGE_VERSION
"\nCompiled with libpulse %s\nLinked with libpulse %s\n", pa_get_headers_version(), pa_get_library_version());
258 server
= pa_xstrdup(optarg
);
266 child_argv
= argv
+ optind
;
267 child_argc
= argc
- optind
;
269 if (child_argc
<= 0) {
275 if (!(m
= pa_mainloop_new())) {
276 fprintf(stderr
, "pa_mainloop_new() failed.\n");
280 pa_assert_se(mainloop_api
= pa_mainloop_get_api(m
));
281 pa_assert_se(pa_signal_init(mainloop_api
) == 0);
282 pa_signal_new(SIGINT
, sigint_callback
, NULL
);
283 pa_signal_new(SIGCHLD
, sigchld_callback
, NULL
);
285 signal(SIGPIPE
, SIG_IGN
);
288 if (!(context
= pa_context_new(mainloop_api
, bn
))) {
289 fprintf(stderr
, "pa_context_new() failed.\n");
293 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
294 pa_context_connect(context
, server
, PA_CONTEXT_NOAUTOSPAWN
, NULL
);
296 if (pa_mainloop_run(m
, &ret
) < 0) {
297 fprintf(stderr
, "pa_mainloop_run() failed.\n");
303 pa_context_unref(context
);
313 kill(child_pid
, SIGTERM
);
315 return ret
== 0 ? child_ret
: ret
;