]>
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
29 #include <sys/socket.h>
36 #include <pulse/error.h>
37 #include <pulse/util.h>
38 #include <pulse/xmalloc.h>
39 #include <pulse/i18n.h>
41 #include <pulsecore/macro.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/pid.h>
46 int main(int argc
, char*argv
[]) {
58 struct sockaddr_un sa
;
59 char ibuf
[PIPE_BUF
], obuf
[PIPE_BUF
];
60 size_t ibuf_index
, ibuf_length
, obuf_index
, obuf_length
;
62 pa_bool_t ibuf_eof
, obuf_eof
, ibuf_closed
, obuf_closed
;
63 struct pollfd pollfd
[N_WATCH
];
65 setlocale(LC_ALL
, "");
66 bindtextdomain(GETTEXT_PACKAGE
, PULSE_LOCALEDIR
);
68 if (pa_pid_file_check_running(&pid
, "pulseaudio") < 0) {
69 pa_log(_("No PulseAudio daemon running, or not running as session daemon."));
73 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
74 pa_log(_("socket(PF_UNIX, SOCK_STREAM, 0): %s"), strerror(errno
));
79 sa
.sun_family
= AF_UNIX
;
81 if (!(cli
= pa_runtime_path("cli")))
84 pa_strlcpy(sa
.sun_path
, cli
, sizeof(sa
.sun_path
));
87 for (i
= 0; i
< 5; i
++) {
90 if ((r
= connect(fd
, (struct sockaddr
*) &sa
, sizeof(sa
))) < 0 && (errno
!= ECONNREFUSED
&& errno
!= ENOENT
)) {
91 pa_log(_("connect(): %s"), strerror(errno
));
98 if (pa_pid_file_kill(SIGUSR2
, NULL
, "pulseaudio") < 0) {
99 pa_log(_("Failed to kill PulseAudio daemon."));
107 pa_log(_("Daemon not responding."));
111 ibuf_index
= ibuf_length
= obuf_index
= obuf_length
= 0;
112 ibuf_eof
= obuf_eof
= ibuf_closed
= obuf_closed
= FALSE
;
115 for (i
= 1; i
< argc
; i
++) {
118 k
= PA_MIN(sizeof(ibuf
) - ibuf_length
, strlen(argv
[i
]));
119 memcpy(ibuf
+ ibuf_length
, argv
[i
], k
);
122 if (ibuf_length
< sizeof(ibuf
)) {
123 ibuf
[ibuf_length
] = i
< argc
-1 ? ' ' : '\n';
133 pollfd
[WATCH_STDIN
].fd
= STDIN_FILENO
;
134 pollfd
[WATCH_STDOUT
].fd
= STDOUT_FILENO
;
135 pollfd
[WATCH_SOCKET
].fd
= fd
;
144 pollfd
[WATCH_STDIN
].events
= pollfd
[WATCH_STDOUT
].events
= pollfd
[WATCH_SOCKET
].events
= 0;
147 pollfd
[WATCH_STDOUT
].events
|= POLLOUT
;
149 pollfd
[WATCH_SOCKET
].events
|= POLLIN
;
152 pollfd
[WATCH_SOCKET
].events
|= POLLOUT
;
154 pollfd
[WATCH_STDIN
].events
|= POLLIN
;
156 if (poll(pollfd
, N_WATCH
, -1) < 0) {
161 pa_log(_("poll(): %s"), strerror(errno
));
165 if (pollfd
[WATCH_STDIN
].revents
& POLLIN
) {
167 pa_assert(!ibuf_length
);
169 if ((r
= pa_read(STDIN_FILENO
, ibuf
, sizeof(ibuf
), NULL
)) <= 0) {
171 pa_log(_("read(): %s"), strerror(errno
));
177 ibuf_length
= (size_t) r
;
182 if (pollfd
[WATCH_SOCKET
].revents
& POLLIN
) {
184 pa_assert(!obuf_length
);
186 if ((r
= pa_read(fd
, obuf
, sizeof(obuf
), NULL
)) <= 0) {
188 pa_log(_("read(): %s"), strerror(errno
));
194 obuf_length
= (size_t) r
;
199 if (pollfd
[WATCH_STDOUT
].revents
& POLLHUP
) {
202 } else if (pollfd
[WATCH_STDOUT
].revents
& POLLOUT
) {
204 pa_assert(obuf_length
);
206 if ((r
= pa_write(STDOUT_FILENO
, obuf
+ obuf_index
, obuf_length
, NULL
)) < 0) {
207 pa_log(_("write(): %s"), strerror(errno
));
211 obuf_length
-= (size_t) r
;
212 obuf_index
+= obuf_index
;
215 if (pollfd
[WATCH_SOCKET
].revents
& POLLHUP
) {
218 } if (pollfd
[WATCH_SOCKET
].revents
& POLLOUT
) {
220 pa_assert(ibuf_length
);
222 if ((r
= pa_write(fd
, ibuf
+ ibuf_index
, ibuf_length
, NULL
)) < 0) {
223 pa_log(_("write(): %s"), strerror(errno
));
227 ibuf_length
-= (size_t) r
;
228 ibuf_index
+= obuf_index
;
231 if (ibuf_length
<= 0 && ibuf_eof
&& !ibuf_closed
) {
232 pa_close(STDIN_FILENO
);
233 shutdown(fd
, SHUT_WR
);
237 if (obuf_length
<= 0 && obuf_eof
&& !obuf_closed
) {
238 shutdown(fd
, SHUT_RD
);
239 pa_close(STDOUT_FILENO
);