]>
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>
35 #include <pulse/util.h>
36 #include <pulse/xmalloc.h>
38 #include <pulsecore/i18n.h>
39 #include <pulsecore/poll.h>
40 #include <pulsecore/macro.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/pid.h>
45 int main(int argc
, char*argv
[]) {
49 struct sockaddr_un sa
;
50 char ibuf
[PIPE_BUF
], obuf
[PIPE_BUF
];
51 size_t ibuf_index
, ibuf_length
, obuf_index
, obuf_length
;
53 pa_bool_t ibuf_eof
, obuf_eof
, ibuf_closed
, obuf_closed
;
54 struct pollfd pollfd
[3];
55 struct pollfd
*watch_socket
, *watch_stdin
, *watch_stdout
;
57 int stdin_type
= 0, stdout_type
= 0, fd_type
= 0;
59 setlocale(LC_ALL
, "");
60 bindtextdomain(GETTEXT_PACKAGE
, PULSE_LOCALEDIR
);
62 if (pa_pid_file_check_running(&pid
, "pulseaudio") < 0) {
63 pa_log(_("No PulseAudio daemon running, or not running as session daemon."));
67 if ((fd
= pa_socket_cloexec(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
68 pa_log(_("socket(PF_UNIX, SOCK_STREAM, 0): %s"), strerror(errno
));
73 sa
.sun_family
= AF_UNIX
;
75 if (!(cli
= pa_runtime_path("cli")))
78 pa_strlcpy(sa
.sun_path
, cli
, sizeof(sa
.sun_path
));
81 for (i
= 0; i
< 5; i
++) {
84 if ((r
= connect(fd
, (struct sockaddr
*) &sa
, sizeof(sa
))) < 0 && (errno
!= ECONNREFUSED
&& errno
!= ENOENT
)) {
85 pa_log(_("connect(): %s"), strerror(errno
));
92 if (pa_pid_file_kill(SIGUSR2
, NULL
, "pulseaudio") < 0) {
93 pa_log(_("Failed to kill PulseAudio daemon."));
101 pa_log(_("Daemon not responding."));
105 ibuf_index
= ibuf_length
= obuf_index
= obuf_length
= 0;
106 ibuf_eof
= obuf_eof
= ibuf_closed
= obuf_closed
= FALSE
;
109 for (i
= 1; i
< argc
; i
++) {
112 k
= PA_MIN(sizeof(ibuf
) - ibuf_length
, strlen(argv
[i
]));
113 memcpy(ibuf
+ ibuf_length
, argv
[i
], k
);
116 if (ibuf_length
< sizeof(ibuf
)) {
117 ibuf
[ibuf_length
] = i
< argc
-1 ? ' ' : '\n';
134 if (ibuf_length
<= 0 && ibuf_eof
&& !ibuf_closed
) {
135 shutdown(fd
, SHUT_WR
);
139 if (obuf_length
<= 0 && obuf_eof
&& !obuf_closed
) {
140 shutdown(fd
, SHUT_RD
);
148 if (ibuf_length
> 0 || (!obuf_eof
&& obuf_length
<= 0)) {
150 watch_socket
->fd
= fd
;
151 watch_socket
->events
=
152 (ibuf_length
> 0 ? POLLOUT
: 0) |
153 (!obuf_eof
&& obuf_length
<= 0 ? POLLIN
: 0);
157 if (!ibuf_eof
&& ibuf_length
<= 0) {
159 watch_stdin
->fd
= STDIN_FILENO
;
160 watch_stdin
->events
= POLLIN
;
164 if (obuf_length
> 0) {
166 watch_stdout
->fd
= STDOUT_FILENO
;
167 watch_stdout
->events
= POLLOUT
;
171 if (pa_poll(pollfd
, p
-pollfd
, -1) < 0) {
176 pa_log(_("poll(): %s"), strerror(errno
));
181 if (watch_stdin
->revents
& POLLIN
) {
183 pa_assert(ibuf_length
<= 0);
185 if ((r
= pa_read(STDIN_FILENO
, ibuf
, sizeof(ibuf
), &stdin_type
)) <= 0) {
187 pa_log(_("read(): %s"), strerror(errno
));
193 ibuf_length
= (size_t) r
;
196 } else if (watch_stdin
->revents
& POLLHUP
)
201 if (watch_socket
->revents
& POLLIN
) {
203 pa_assert(obuf_length
<= 0);
205 if ((r
= pa_read(fd
, obuf
, sizeof(obuf
), &fd_type
)) <= 0) {
207 pa_log(_("read(): %s"), strerror(errno
));
213 obuf_length
= (size_t) r
;
216 } else if (watch_socket
->revents
& POLLHUP
)
221 if (watch_stdout
->revents
& POLLHUP
) {
224 } else if (watch_stdout
->revents
& POLLOUT
) {
226 pa_assert(obuf_length
> 0);
228 if ((r
= pa_write(STDOUT_FILENO
, obuf
+ obuf_index
, obuf_length
, &stdout_type
)) < 0) {
229 pa_log(_("write(): %s"), strerror(errno
));
233 obuf_length
-= (size_t) r
;
234 obuf_index
+= obuf_index
;
239 if (watch_socket
->revents
& POLLHUP
) {
242 } if (watch_socket
->revents
& POLLOUT
) {
244 pa_assert(ibuf_length
> 0);
246 if ((r
= pa_write(fd
, ibuf
+ ibuf_index
, ibuf_length
, &fd_type
)) < 0) {
247 pa_log(_("write(): %s"), strerror(errno
));
251 ibuf_length
-= (size_t) r
;
252 ibuf_index
+= obuf_index
;