]>
code.delx.au - pulseaudio/blob - src/utils/pacmd.c
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
28 #include <sys/socket.h>
36 #include <pulse/util.h>
37 #include <pulse/xmalloc.h>
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>
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"));
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" ));
93 int main(int argc
, char*argv
[]) {
97 struct sockaddr_un sa
;
100 size_t buf_size
, ibuf_size
, ibuf_index
, ibuf_length
, obuf_size
, obuf_index
, obuf_length
;
102 bool ibuf_eof
, obuf_eof
, ibuf_closed
, obuf_closed
;
103 struct pollfd pollfd
[3];
104 struct pollfd
*watch_socket
, *watch_stdin
, *watch_stdout
;
105 int stdin_type
= 0, stdout_type
= 0, fd_type
= 0;
110 static const struct option long_options
[] = {
111 {"version", 0, NULL
, ARG_VERSION
},
112 {"help", 0, NULL
, 'h'},
116 setlocale(LC_ALL
, "");
118 bindtextdomain(GETTEXT_PACKAGE
, PULSE_LOCALEDIR
);
121 bn
= pa_path_get_filename(argv
[0]);
123 while ((c
= getopt_long(argc
, argv
, "h", long_options
, NULL
)) != -1) {
130 printf(_("pacmd %s\n"
131 "Compiled with libpulse %s\n"
132 "Linked with libpulse %s\n"),
134 pa_get_headers_version(),
135 pa_get_library_version());
143 if (pa_pid_file_check_running(&pid
, "pulseaudio") < 0) {
144 pa_log(_("No PulseAudio daemon running, or not running as session daemon."));
148 if ((fd
= pa_socket_cloexec(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
149 pa_log(_("socket(PF_UNIX, SOCK_STREAM, 0): %s"), strerror(errno
));
154 sa
.sun_family
= AF_UNIX
;
156 if (!(cli
= pa_runtime_path("cli")))
159 pa_strlcpy(sa
.sun_path
, cli
, sizeof(sa
.sun_path
));
162 for (i
= 0; i
< 5; i
++) {
165 if ((r
= connect(fd
, (struct sockaddr
*) &sa
, sizeof(sa
))) < 0 && (errno
!= ECONNREFUSED
&& errno
!= ENOENT
)) {
166 pa_log(_("connect(): %s"), strerror(errno
));
173 if (pa_pid_file_kill(SIGUSR2
, NULL
, "pulseaudio") < 0) {
174 pa_log(_("Failed to kill PulseAudio daemon."));
182 pa_log(_("Daemon not responding."));
186 buf_size
= pa_pipe_buf(fd
);
187 ibuf_size
= PA_MIN(buf_size
, pa_pipe_buf(STDIN_FILENO
));
188 ibuf
= pa_xmalloc(ibuf_size
);
189 obuf_size
= PA_MIN(buf_size
, pa_pipe_buf(STDOUT_FILENO
));
190 obuf
= pa_xmalloc(obuf_size
);
191 ibuf_index
= ibuf_length
= obuf_index
= obuf_length
= 0;
192 ibuf_eof
= obuf_eof
= ibuf_closed
= obuf_closed
= false;
195 for (i
= 1; i
< argc
; i
++) {
198 k
= PA_MIN(ibuf_size
- ibuf_length
, strlen(argv
[i
]));
199 memcpy(ibuf
+ ibuf_length
, argv
[i
], k
);
202 if (ibuf_length
< ibuf_size
) {
203 ibuf
[ibuf_length
] = i
< argc
-1 ? ' ' : '\n';
211 if (!ibuf_eof
&& isatty(STDIN_FILENO
)) {
212 /* send hello to enable interactive mode (welcome message, prompt) */
213 if (pa_write(fd
, "hello\n", 6, &fd_type
) < 0) {
214 pa_log(_("write(): %s"), strerror(errno
));
228 if (ibuf_length
<= 0 && ibuf_eof
&& !ibuf_closed
) {
229 shutdown(fd
, SHUT_WR
);
233 if (obuf_length
<= 0 && obuf_eof
&& !obuf_closed
) {
234 shutdown(fd
, SHUT_RD
);
242 if (ibuf_length
> 0 || (!obuf_eof
&& obuf_length
<= 0)) {
244 watch_socket
->fd
= fd
;
245 watch_socket
->events
=
246 (ibuf_length
> 0 ? POLLOUT
: 0) |
247 (!obuf_eof
&& obuf_length
<= 0 ? POLLIN
: 0);
251 if (!ibuf_eof
&& ibuf_length
<= 0) {
253 watch_stdin
->fd
= STDIN_FILENO
;
254 watch_stdin
->events
= POLLIN
;
258 if (obuf_length
> 0) {
260 watch_stdout
->fd
= STDOUT_FILENO
;
261 watch_stdout
->events
= POLLOUT
;
265 if (pa_poll(pollfd
, p
-pollfd
, -1) < 0) {
270 pa_log(_("poll(): %s"), strerror(errno
));
275 if (watch_stdin
->revents
& POLLIN
) {
277 pa_assert(ibuf_length
<= 0);
279 if ((r
= pa_read(STDIN_FILENO
, ibuf
, ibuf_size
, &stdin_type
)) <= 0) {
281 pa_log(_("read(): %s"), strerror(errno
));
287 ibuf_length
= (size_t) r
;
290 } else if (watch_stdin
->revents
& POLLHUP
)
295 if (watch_socket
->revents
& POLLIN
) {
297 pa_assert(obuf_length
<= 0);
299 if ((r
= pa_read(fd
, obuf
, obuf_size
, &fd_type
)) <= 0) {
301 pa_log(_("read(): %s"), strerror(errno
));
307 obuf_length
= (size_t) r
;
310 } else if (watch_socket
->revents
& POLLHUP
)
315 if (watch_stdout
->revents
& POLLHUP
) {
318 } else if (watch_stdout
->revents
& POLLOUT
) {
320 pa_assert(obuf_length
> 0);
322 if ((r
= pa_write(STDOUT_FILENO
, obuf
+ obuf_index
, obuf_length
, &stdout_type
)) < 0) {
323 pa_log(_("write(): %s"), strerror(errno
));
327 obuf_length
-= (size_t) r
;
328 obuf_index
+= obuf_index
;
333 if (watch_socket
->revents
& POLLHUP
) {
336 } if (watch_socket
->revents
& POLLOUT
) {
338 pa_assert(ibuf_length
> 0);
340 if ((r
= pa_write(fd
, ibuf
+ ibuf_index
, ibuf_length
, &fd_type
)) < 0) {
341 pa_log(_("write(): %s"), strerror(errno
));
345 ibuf_length
-= (size_t) r
;
346 ibuf_index
+= obuf_index
;