]> code.delx.au - pulseaudio/blob - src/utils/pacmd.c
dc477aae81d1231ab7c3d9c2d44938f559319264
[pulseaudio] / src / utils / pacmd.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
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.
10
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.
15
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 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <signal.h>
28 #include <sys/socket.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <sys/un.h>
33 #include <getopt.h>
34 #include <locale.h>
35
36 #include <pulse/util.h>
37 #include <pulse/xmalloc.h>
38
39 #include <pulsecore/i18n.h>
40 #include <pulsecore/poll.h>
41 #include <pulsecore/macro.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/pid.h>
45
46 static void help(const char *argv0) {
47 printf("%s %s\n", argv0, "exit");
48 printf("%s %s\n", argv0, "help");
49 printf("%s %s\n", argv0, "list-(modules|sinks|sources|clients|cards|samples)");
50 printf("%s %s\n", argv0, "list-(sink-inputs|source-outputs)");
51 printf("%s %s\n", argv0, "stat");
52 printf("%s %s\n", argv0, "info");
53 printf("%s %s %s\n", argv0, "load-module", _("NAME [ARGS ...]"));
54 printf("%s %s %s\n", argv0, "unload-module", _("NAME|#N"));
55 printf("%s %s %s\n", argv0, "describe-module", _("NAME"));
56 printf("%s %s %s\n", argv0, "set-(sink|source)-volume", _("NAME|#N VOLUME"));
57 printf("%s %s %s\n", argv0, "set-(sink-input|source-output)-volume", _("#N VOLUME"));
58 printf("%s %s %s\n", argv0, "set-(sink|source)-mute", _("NAME|#N 1|0"));
59 printf("%s %s %s\n", argv0, "set-(sink-input|source-output)-mute", _("#N 1|0"));
60 printf("%s %s %s\n", argv0, "update-(sink|source)-proplist", _("NAME|#N KEY=VALUE"));
61 printf("%s %s %s\n", argv0, "update-(sink-input|source-output)-proplist", _("#N KEY=VALUE"));
62 printf("%s %s %s\n", argv0, "set-default(sink|source)", _("NAME|#N"));
63 printf("%s %s %s\n", argv0, "kill-(client|sink-input|source-output)", _("#N"));
64 printf("%s %s %s\n", argv0, "play-sample", _("NAME SINK|#N"));
65 printf("%s %s %s\n", argv0, "remove-sample", _("NAME"));
66 printf("%s %s %s\n", argv0, "load-sample", _("NAME FILENAME"));
67 printf("%s %s %s\n", argv0, "load-sample-lazy", _("NAME FILENAME"));
68 printf("%s %s %s\n", argv0, "load-sample-dir-lazy", _("PATHNAME"));
69 printf("%s %s %s\n", argv0, "play-file", _("FILENAME SINK|#N"));
70 printf("%s %s\n", argv0, "dump");
71 printf("%s %s %s\n", argv0, "move-(sink-input|source-output)", _("#N SINK|SOURCE"));
72 printf("%s %s %s\n", argv0, "suspend-(sink|source)", _("NAME|#N 1|0"));
73 printf("%s %s %s\n", argv0, "suspend", _("1|0"));
74 printf("%s %s %s\n", argv0, "set-card-profile", _("CARD PROFILE"));
75 printf("%s %s %s\n", argv0, "set-(sink|source)-port", _("NAME|#N PORT"));
76 printf("%s %s %s\n", argv0, "set-port-latency-offset", _("CARD-NAME|CARD-#N PORT OFFSET"));
77 printf("%s %s %s\n", argv0, "set-log-target", _("TARGET"));
78 printf("%s %s %s\n", argv0, "set-log-level", _("NUMERIC LEVEL"));
79 printf("%s %s %s\n", argv0, "set-log-meta", _("1|0"));
80 printf("%s %s %s\n", argv0, "set-log-time", _("1|0"));
81 printf("%s %s %s\n", argv0, "set-log-backtrace", _("FRAMES"));
82
83 printf(_("\n"
84 " -h, --help Show this help\n"
85 " --version Show version\n"
86 "When no command is given pacmd starts in the interactive mode\n" ));
87 }
88
89 enum {
90 ARG_VERSION = 256
91 };
92
93 int main(int argc, char*argv[]) {
94 pid_t pid;
95 int fd = -1;
96 int ret = 1, i;
97 struct sockaddr_un sa;
98 char *ibuf = NULL;
99 char *obuf = NULL;
100 size_t buf_size, ibuf_size, ibuf_index, ibuf_length, obuf_size, obuf_index, obuf_length;
101 char *cli;
102 pa_bool_t ibuf_eof, obuf_eof, ibuf_closed, obuf_closed;
103 struct pollfd pollfd[3];
104 struct pollfd *watch_socket, *watch_stdin, *watch_stdout;
105
106 int stdin_type = 0, stdout_type = 0, fd_type = 0;
107
108 char *bn = NULL;
109 int c;
110
111 static const struct option long_options[] = {
112 {"version", 0, NULL, ARG_VERSION},
113 {"help", 0, NULL, 'h'},
114 {NULL, 0, NULL, 0}
115 };
116
117 setlocale(LC_ALL, "");
118 #ifdef ENABLE_NLS
119 bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
120 #endif
121
122 bn = pa_path_get_filename(argv[0]);
123
124 while ((c = getopt_long(argc, argv, "h", long_options, NULL)) != -1) {
125 switch (c) {
126 case 'h' :
127 help(bn);
128 ret = 0;
129 goto quit;
130 case ARG_VERSION:
131 printf(_("pacmd %s\n"
132 "Compiled with libpulse %s\n"
133 "Linked with libpulse %s\n"),
134 PACKAGE_VERSION,
135 pa_get_headers_version(),
136 pa_get_library_version());
137 ret = 0;
138 goto quit;
139 default:
140 goto quit;
141 }
142 }
143
144 if (pa_pid_file_check_running(&pid, "pulseaudio") < 0) {
145 pa_log(_("No PulseAudio daemon running, or not running as session daemon."));
146 goto quit;
147 }
148
149 if ((fd = pa_socket_cloexec(PF_UNIX, SOCK_STREAM, 0)) < 0) {
150 pa_log(_("socket(PF_UNIX, SOCK_STREAM, 0): %s"), strerror(errno));
151 goto quit;
152 }
153
154 pa_zero(sa);
155 sa.sun_family = AF_UNIX;
156
157 if (!(cli = pa_runtime_path("cli")))
158 goto quit;
159
160 pa_strlcpy(sa.sun_path, cli, sizeof(sa.sun_path));
161 pa_xfree(cli);
162
163 for (i = 0; i < 5; i++) {
164 int r;
165
166 if ((r = connect(fd, (struct sockaddr*) &sa, sizeof(sa))) < 0 && (errno != ECONNREFUSED && errno != ENOENT)) {
167 pa_log(_("connect(): %s"), strerror(errno));
168 goto quit;
169 }
170
171
172 if (r >= 0)
173 break;
174
175 if (pa_pid_file_kill(SIGUSR2, NULL, "pulseaudio") < 0) {
176 pa_log(_("Failed to kill PulseAudio daemon."));
177 goto quit;
178 }
179
180 pa_msleep(300);
181 }
182
183 if (i >= 5) {
184 pa_log(_("Daemon not responding."));
185 goto quit;
186 }
187
188 buf_size = pa_pipe_buf(fd);
189 ibuf_size = PA_MIN(buf_size, pa_pipe_buf(STDIN_FILENO));
190 ibuf = pa_xmalloc(ibuf_size);
191 obuf_size = PA_MIN(buf_size, pa_pipe_buf(STDOUT_FILENO));
192 obuf = pa_xmalloc(obuf_size);
193 ibuf_index = ibuf_length = obuf_index = obuf_length = 0;
194 ibuf_eof = obuf_eof = ibuf_closed = obuf_closed = FALSE;
195
196 if (argc > 1) {
197 for (i = 1; i < argc; i++) {
198 size_t k;
199
200 k = PA_MIN(ibuf_size - ibuf_length, strlen(argv[i]));
201 memcpy(ibuf + ibuf_length, argv[i], k);
202 ibuf_length += k;
203
204 if (ibuf_length < ibuf_size) {
205 ibuf[ibuf_length] = i < argc-1 ? ' ' : '\n';
206 ibuf_length++;
207 }
208 }
209
210 ibuf_eof = TRUE;
211 }
212
213 for (;;) {
214 struct pollfd *p;
215
216 if (ibuf_eof &&
217 obuf_eof &&
218 ibuf_length <= 0 &&
219 obuf_length <= 0)
220 break;
221
222 if (ibuf_length <= 0 && ibuf_eof && !ibuf_closed) {
223 shutdown(fd, SHUT_WR);
224 ibuf_closed = TRUE;
225 }
226
227 if (obuf_length <= 0 && obuf_eof && !obuf_closed) {
228 shutdown(fd, SHUT_RD);
229 obuf_closed = TRUE;
230 }
231
232 pa_zero(pollfd);
233
234 p = pollfd;
235
236 if (ibuf_length > 0 || (!obuf_eof && obuf_length <= 0)) {
237 watch_socket = p++;
238 watch_socket->fd = fd;
239 watch_socket->events =
240 (ibuf_length > 0 ? POLLOUT : 0) |
241 (!obuf_eof && obuf_length <= 0 ? POLLIN : 0);
242 } else
243 watch_socket = NULL;
244
245 if (!ibuf_eof && ibuf_length <= 0) {
246 watch_stdin = p++;
247 watch_stdin->fd = STDIN_FILENO;
248 watch_stdin->events = POLLIN;
249 } else
250 watch_stdin = NULL;
251
252 if (obuf_length > 0) {
253 watch_stdout = p++;
254 watch_stdout->fd = STDOUT_FILENO;
255 watch_stdout->events = POLLOUT;
256 } else
257 watch_stdout = NULL;
258
259 if (pa_poll(pollfd, p-pollfd, -1) < 0) {
260
261 if (errno == EINTR)
262 continue;
263
264 pa_log(_("poll(): %s"), strerror(errno));
265 goto quit;
266 }
267
268 if (watch_stdin) {
269 if (watch_stdin->revents & POLLIN) {
270 ssize_t r;
271 pa_assert(ibuf_length <= 0);
272
273 if ((r = pa_read(STDIN_FILENO, ibuf, ibuf_size, &stdin_type)) <= 0) {
274 if (r < 0) {
275 pa_log(_("read(): %s"), strerror(errno));
276 goto quit;
277 }
278
279 ibuf_eof = TRUE;
280 } else {
281 ibuf_length = (size_t) r;
282 ibuf_index = 0;
283 }
284 } else if (watch_stdin->revents & POLLHUP)
285 ibuf_eof = TRUE;
286 }
287
288 if (watch_socket) {
289 if (watch_socket->revents & POLLIN) {
290 ssize_t r;
291 pa_assert(obuf_length <= 0);
292
293 if ((r = pa_read(fd, obuf, obuf_size, &fd_type)) <= 0) {
294 if (r < 0) {
295 pa_log(_("read(): %s"), strerror(errno));
296 goto quit;
297 }
298
299 obuf_eof = TRUE;
300 } else {
301 obuf_length = (size_t) r;
302 obuf_index = 0;
303 }
304 } else if (watch_socket->revents & POLLHUP)
305 obuf_eof = TRUE;
306 }
307
308 if (watch_stdout) {
309 if (watch_stdout->revents & POLLHUP) {
310 obuf_eof = TRUE;
311 obuf_length = 0;
312 } else if (watch_stdout->revents & POLLOUT) {
313 ssize_t r;
314 pa_assert(obuf_length > 0);
315
316 if ((r = pa_write(STDOUT_FILENO, obuf + obuf_index, obuf_length, &stdout_type)) < 0) {
317 pa_log(_("write(): %s"), strerror(errno));
318 goto quit;
319 }
320
321 obuf_length -= (size_t) r;
322 obuf_index += obuf_index;
323 }
324 }
325
326 if (watch_socket) {
327 if (watch_socket->revents & POLLHUP) {
328 ibuf_eof = TRUE;
329 ibuf_length = 0;
330 } if (watch_socket->revents & POLLOUT) {
331 ssize_t r;
332 pa_assert(ibuf_length > 0);
333
334 if ((r = pa_write(fd, ibuf + ibuf_index, ibuf_length, &fd_type)) < 0) {
335 pa_log(_("write(): %s"), strerror(errno));
336 goto quit;
337 }
338
339 ibuf_length -= (size_t) r;
340 ibuf_index += obuf_index;
341 }
342 }
343 }
344
345 ret = 0;
346
347 quit:
348 if (fd >= 0)
349 pa_close(fd);
350
351 pa_xfree(obuf);
352 pa_xfree(ibuf);
353
354 return ret;
355 }