]>
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/prctl.h>
29 #include <sys/types.h>
44 #include <pulse/pulseaudio.h>
45 #include <pulsecore/macro.h>
47 #if PA_API_VERSION < 10
48 #error Invalid PulseAudio API version
53 static pa_context
*context
= NULL
;
54 static pa_mainloop_api
*mainloop_api
= NULL
;
55 static char **child_argv
= NULL
;
56 static int child_argc
= 0;
57 static pid_t child_pid
= (pid_t
) -1;
58 static int child_ret
= 0;
61 static void quit(int ret
) {
62 pa_assert(mainloop_api
);
63 mainloop_api
->quit(mainloop_api
, ret
);
67 static void context_drain_complete(pa_context
*c
, void *userdata
) {
68 pa_context_disconnect(c
);
71 static void drain(void) {
74 if (!(o
= pa_context_drain(context
, context_drain_complete
, NULL
)))
75 pa_context_disconnect(context
);
77 pa_operation_unref(o
);
80 static void suspend_complete(pa_context
*c
, int success
, void *userdata
) {
86 fprintf(stderr
, "Failure to suspend: %s\n", pa_strerror(pa_context_errno(c
)));
92 if ((child_pid
= fork()) < 0) {
94 fprintf(stderr
, "fork(): %s\n", strerror(errno
));
97 } else if (child_pid
== 0) {
101 prctl(PR_SET_PDEATHSIG
, SIGTERM
, 0, 0, 0);
104 if (execvp(child_argv
[0], child_argv
) < 0)
105 fprintf(stderr
, "execvp(): %s\n", strerror(errno
));
117 static void resume_complete(pa_context
*c
, int success
, void *userdata
) {
123 fprintf(stderr
, "Failure to resume: %s\n", pa_strerror(pa_context_errno(c
)));
129 drain(); /* drain and quit */
132 static void context_state_callback(pa_context
*c
, void *userdata
) {
135 switch (pa_context_get_state(c
)) {
136 case PA_CONTEXT_CONNECTING
:
137 case PA_CONTEXT_AUTHORIZING
:
138 case PA_CONTEXT_SETTING_NAME
:
141 case PA_CONTEXT_READY
:
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
));
146 case PA_CONTEXT_TERMINATED
:
150 case PA_CONTEXT_FAILED
:
152 fprintf(stderr
, "Connection failure: %s\n", pa_strerror(pa_context_errno(c
)));
157 static void sigint_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
158 fprintf(stderr
, "Got SIGINT, exiting.\n");
162 static void sigchld_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
166 p
= waitpid(-1, &status
, WNOHANG
);
173 if (WIFEXITED(status
))
174 child_ret
= WEXITSTATUS(status
);
175 else if (WIFSIGNALED(status
)) {
176 fprintf(stderr
, "WARNING: Child process terminated by signal %u\n", WTERMSIG(status
));
180 pa_operation_unref(pa_context_suspend_sink_by_index(context
, PA_INVALID_INDEX
, 0, resume_complete
, NULL
));
181 pa_operation_unref(pa_context_suspend_source_by_index(context
, PA_INVALID_INDEX
, 0, resume_complete
, NULL
));
184 static void help(const char *argv0
) {
186 printf("%s [options] ... \n\n"
187 " -h, --help Show this help\n"
188 " --version Show version\n"
189 " -s, --server=SERVER The name of the server to connect to\n\n",
197 int main(int argc
, char *argv
[]) {
198 pa_mainloop
* m
= NULL
;
200 char *server
= NULL
, *bn
;
202 static const struct option long_options
[] = {
203 {"server", 1, NULL
, 's'},
204 {"version", 0, NULL
, ARG_VERSION
},
205 {"help", 0, NULL
, 'h'},
209 if (!(bn
= strrchr(argv
[0], '/')))
214 while ((c
= getopt_long(argc
, argv
, "s:h", long_options
, NULL
)) != -1) {
222 printf("pasuspender "PACKAGE_VERSION
"\nCompiled with libpulse %s\nLinked with libpulse %s\n", pa_get_headers_version(), pa_get_library_version());
228 server
= pa_xstrdup(optarg
);
236 child_argv
= argv
+ optind
;
237 child_argc
= argc
- optind
;
239 if (child_argc
<= 0) {
245 if (!(m
= pa_mainloop_new())) {
246 fprintf(stderr
, "pa_mainloop_new() failed.\n");
250 pa_assert_se(mainloop_api
= pa_mainloop_get_api(m
));
251 pa_assert_se(pa_signal_init(mainloop_api
) == 0);
252 pa_signal_new(SIGINT
, sigint_callback
, NULL
);
253 pa_signal_new(SIGCHLD
, sigchld_callback
, NULL
);
255 signal(SIGPIPE
, SIG_IGN
);
258 if (!(context
= pa_context_new(mainloop_api
, bn
))) {
259 fprintf(stderr
, "pa_context_new() failed.\n");
263 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
264 pa_context_connect(context
, server
, 0, NULL
);
266 if (pa_mainloop_run(m
, &ret
) < 0) {
267 fprintf(stderr
, "pa_mainloop_run() failed.\n");
273 pa_context_unref(context
);
283 kill(child_pid
, SIGTERM
);
285 return ret
== 0 ? child_ret
: ret
;