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
26 #include <sys/types.h>
41 #include <sys/prctl.h>
44 #include <pulse/i18n.h>
45 #include <pulse/pulseaudio.h>
46 #include <pulsecore/macro.h>
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;
58 static void quit(int ret
) {
59 pa_assert(mainloop_api
);
60 mainloop_api
->quit(mainloop_api
, ret
);
64 static void context_drain_complete(pa_context
*c
, void *userdata
) {
65 pa_context_disconnect(c
);
68 static void drain(void) {
71 if (!(o
= pa_context_drain(context
, context_drain_complete
, NULL
)))
72 pa_context_disconnect(context
);
74 pa_operation_unref(o
);
77 static void start_child(void) {
79 if ((child_pid
= fork()) < 0) {
81 fprintf(stderr
, _("fork(): %s\n"), strerror(errno
));
84 } else if (child_pid
== 0) {
88 prctl(PR_SET_PDEATHSIG
, SIGTERM
, 0, 0, 0);
91 if (execvp(child_argv
[0], child_argv
) < 0)
92 fprintf(stderr
, _("execvp(): %s\n"), strerror(errno
));
103 static void suspend_complete(pa_context
*c
, int success
, void *userdata
) {
109 fprintf(stderr
, _("Failure to suspend: %s\n"), pa_strerror(pa_context_errno(c
)));
118 static void resume_complete(pa_context
*c
, int success
, void *userdata
) {
124 fprintf(stderr
, _("Failure to resume: %s\n"), pa_strerror(pa_context_errno(c
)));
130 drain(); /* drain and quit */
133 static void context_state_callback(pa_context
*c
, void *userdata
) {
136 switch (pa_context_get_state(c
)) {
137 case PA_CONTEXT_CONNECTING
:
138 case PA_CONTEXT_AUTHORIZING
:
139 case PA_CONTEXT_SETTING_NAME
:
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
));
147 fprintf(stderr
, _("WARNING: Sound server is not local, not suspending.\n"));
153 case PA_CONTEXT_TERMINATED
:
157 case PA_CONTEXT_FAILED
:
159 fprintf(stderr
, _("Connection failure: %s\n"), pa_strerror(pa_context_errno(c
)));
161 pa_context_unref(context
);
164 if (child_pid
== (pid_t
) -1)
165 /* not started yet, then we do it now */
168 /* already started, and dead, so let's quit */
175 static void sigint_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
176 fprintf(stderr
, _("Got SIGINT, exiting.\n"));
180 static void sigchld_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
184 p
= waitpid(-1, &status
, WNOHANG
);
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
));
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
));
206 /* Hmm, no context here, so let's terminate right away */
210 static void help(const char *argv0
) {
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"),
223 int main(int argc
, char *argv
[]) {
224 pa_mainloop
* m
= NULL
;
226 char *server
= NULL
, *bn
;
228 static const struct option long_options
[] = {
229 {"server", 1, NULL
, 's'},
230 {"version", 0, NULL
, ARG_VERSION
},
231 {"help", 0, NULL
, 'h'},
235 setlocale(LC_ALL
, "");
236 bindtextdomain(GETTEXT_PACKAGE
, PULSE_LOCALEDIR
);
238 bn
= pa_path_get_filename(argv
[0]);
240 while ((c
= getopt_long(argc
, argv
, "s:h", long_options
, NULL
)) != -1) {
248 printf(_("pasuspender %s\n"
249 "Compiled with libpulse %s\n"
250 "Linked with libpulse %s\n"),
252 pa_get_headers_version(),
253 pa_get_library_version());
259 server
= pa_xstrdup(optarg
);
267 child_argv
= argv
+ optind
;
268 child_argc
= argc
- optind
;
270 if (child_argc
<= 0) {
276 if (!(m
= pa_mainloop_new())) {
277 fprintf(stderr
, _("pa_mainloop_new() failed.\n"));
281 pa_assert_se(mainloop_api
= pa_mainloop_get_api(m
));
282 pa_assert_se(pa_signal_init(mainloop_api
) == 0);
283 pa_signal_new(SIGINT
, sigint_callback
, NULL
);
284 pa_signal_new(SIGCHLD
, sigchld_callback
, NULL
);
286 signal(SIGPIPE
, SIG_IGN
);
289 if (!(context
= pa_context_new(mainloop_api
, bn
))) {
290 fprintf(stderr
, _("pa_context_new() failed.\n"));
294 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
295 pa_context_connect(context
, server
, PA_CONTEXT_NOAUTOSPAWN
, NULL
);
297 if (pa_mainloop_run(m
, &ret
) < 0) {
298 fprintf(stderr
, _("pa_mainloop_run() failed.\n"));
304 pa_context_unref(context
);
314 kill(child_pid
, SIGTERM
);
316 return ret
== 0 ? child_ret
: ret
;