]> code.delx.au - pulseaudio/blob - src/utils/pasuspender.c
add new tool pasuspender which temporarily suspends all sinks and resumes them later...
[pulseaudio] / src / utils / pasuspender.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
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.
12
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.
17
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
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <sys/prctl.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31
32 #include <signal.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <assert.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <limits.h>
40 #include <getopt.h>
41
42 #include <sndfile.h>
43
44 #include <pulse/pulseaudio.h>
45 #include <pulsecore/macro.h>
46
47 #if PA_API_VERSION < 10
48 #error Invalid PulseAudio API version
49 #endif
50
51 #define BUFSIZE 1024
52
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;
59 static int dead = 1;
60
61 static void quit(int ret) {
62 pa_assert(mainloop_api);
63 mainloop_api->quit(mainloop_api, ret);
64 }
65
66
67 static void context_drain_complete(pa_context *c, void *userdata) {
68 pa_context_disconnect(c);
69 }
70
71 static void drain(void) {
72 pa_operation *o;
73
74 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
75 pa_context_disconnect(context);
76 else
77 pa_operation_unref(o);
78 }
79
80 static void suspend_complete(pa_context *c, int success, void *userdata) {
81 static int n = 0;
82
83 n++;
84
85 if (!success) {
86 fprintf(stderr, "Failure to suspend: %s\n", pa_strerror(pa_context_errno(c)));
87 quit(1);
88 }
89
90 if (n >= 2) {
91
92 if ((child_pid = fork()) < 0) {
93
94 fprintf(stderr, "fork(): %s\n", strerror(errno));
95 quit(1);
96
97 } else if (child_pid == 0) {
98 /* Child */
99
100 #ifdef __linux__
101 prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0);
102 #endif
103
104 if (execvp(child_argv[0], child_argv) < 0)
105 fprintf(stderr, "execvp(): %s\n", strerror(errno));
106
107 _exit(1);
108
109 } else {
110
111 /* parent */
112 dead = 0;
113 }
114 }
115 }
116
117 static void resume_complete(pa_context *c, int success, void *userdata) {
118 static int n = 0;
119
120 n++;
121
122 if (!success) {
123 fprintf(stderr, "Failure to resume: %s\n", pa_strerror(pa_context_errno(c)));
124 quit(1);
125 return;
126 }
127
128 if (n >= 2)
129 drain(); /* drain and quit */
130 }
131
132 static void context_state_callback(pa_context *c, void *userdata) {
133 pa_assert(c);
134
135 switch (pa_context_get_state(c)) {
136 case PA_CONTEXT_CONNECTING:
137 case PA_CONTEXT_AUTHORIZING:
138 case PA_CONTEXT_SETTING_NAME:
139 break;
140
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));
144 break;
145
146 case PA_CONTEXT_TERMINATED:
147 quit(0);
148 break;
149
150 case PA_CONTEXT_FAILED:
151 default:
152 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
153 quit(1);
154 }
155 }
156
157 static void sigint_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
158 fprintf(stderr, "Got SIGINT, exiting.\n");
159 quit(0);
160 }
161
162 static void sigchld_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
163 int status = 0;
164 pid_t p;
165
166 p = waitpid(-1, &status, WNOHANG);
167
168 if (p != child_pid)
169 return;
170
171 dead = 1;
172
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));
177 child_ret = 1;
178 }
179
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));
182 }
183
184 static void help(const char *argv0) {
185
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",
190 argv0);
191 }
192
193 enum {
194 ARG_VERSION = 256
195 };
196
197 int main(int argc, char *argv[]) {
198 pa_mainloop* m = NULL;
199 int c, ret = 1;
200 char *server = NULL, *bn;
201
202 static const struct option long_options[] = {
203 {"server", 1, NULL, 's'},
204 {"version", 0, NULL, ARG_VERSION},
205 {"help", 0, NULL, 'h'},
206 {NULL, 0, NULL, 0}
207 };
208
209 if (!(bn = strrchr(argv[0], '/')))
210 bn = argv[0];
211 else
212 bn++;
213
214 while ((c = getopt_long(argc, argv, "s:h", long_options, NULL)) != -1) {
215 switch (c) {
216 case 'h' :
217 help(bn);
218 ret = 0;
219 goto quit;
220
221 case ARG_VERSION:
222 printf("pasuspender "PACKAGE_VERSION"\nCompiled with libpulse %s\nLinked with libpulse %s\n", pa_get_headers_version(), pa_get_library_version());
223 ret = 0;
224 goto quit;
225
226 case 's':
227 pa_xfree(server);
228 server = pa_xstrdup(optarg);
229 break;
230
231 default:
232 goto quit;
233 }
234 }
235
236 child_argv = argv + optind;
237 child_argc = argc - optind;
238
239 if (child_argc <= 0) {
240 help(bn);
241 ret = 0;
242 goto quit;
243 }
244
245 if (!(m = pa_mainloop_new())) {
246 fprintf(stderr, "pa_mainloop_new() failed.\n");
247 goto quit;
248 }
249
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);
254 #ifdef SIGPIPE
255 signal(SIGPIPE, SIG_IGN);
256 #endif
257
258 if (!(context = pa_context_new(mainloop_api, bn))) {
259 fprintf(stderr, "pa_context_new() failed.\n");
260 goto quit;
261 }
262
263 pa_context_set_state_callback(context, context_state_callback, NULL);
264 pa_context_connect(context, server, 0, NULL);
265
266 if (pa_mainloop_run(m, &ret) < 0) {
267 fprintf(stderr, "pa_mainloop_run() failed.\n");
268 goto quit;
269 }
270
271 quit:
272 if (context)
273 pa_context_unref(context);
274
275 if (m) {
276 pa_signal_done();
277 pa_mainloop_free(m);
278 }
279
280 pa_xfree(server);
281
282 if (!dead)
283 kill(child_pid, SIGTERM);
284
285 return ret == 0 ? child_ret : ret;
286 }