]>
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/select.h>
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/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
;
55 setlocale(LC_ALL
, "");
56 bindtextdomain(GETTEXT_PACKAGE
, PULSE_LOCALEDIR
);
58 if (pa_pid_file_check_running(&pid
, "pulseaudio") < 0) {
59 pa_log("No PulseAudio daemon running, or not running as session daemon.");
63 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
64 pa_log(_("socket(PF_UNIX, SOCK_STREAM, 0): %s"), strerror(errno
));
68 memset(&sa
, 0, sizeof(sa
));
69 sa
.sun_family
= AF_UNIX
;
71 if (!(cli
= pa_runtime_path("cli")))
74 pa_strlcpy(sa
.sun_path
, cli
, sizeof(sa
.sun_path
));
77 for (i
= 0; i
< 5; i
++) {
80 if ((r
= connect(fd
, (struct sockaddr
*) &sa
, sizeof(sa
))) < 0 && (errno
!= ECONNREFUSED
&& errno
!= ENOENT
)) {
81 pa_log(_("connect(): %s"), strerror(errno
));
88 if (pa_pid_file_kill(SIGUSR2
, NULL
, "pulseaudio") < 0) {
89 pa_log(_("Failed to kill PulseAudio daemon."));
97 pa_log(_("Daemon not responding."));
101 ibuf_index
= ibuf_length
= obuf_index
= obuf_length
= 0;
102 ibuf_eof
= obuf_eof
= ibuf_closed
= obuf_closed
= FALSE
;
105 for (i
= 1; i
< argc
; i
++) {
108 k
= PA_MIN(sizeof(ibuf
) - ibuf_length
, strlen(argv
[i
]));
109 memcpy(ibuf
+ ibuf_length
, argv
[1], k
);
112 if (ibuf_length
< sizeof(ibuf
)) {
113 ibuf
[ibuf_length
] = i
< argc
-1 ? ' ' : '\n';
143 if (select(FD_SETSIZE
, &ifds
, &ofds
, NULL
, NULL
) < 0) {
144 pa_log(_("select(): %s"), strerror(errno
));
148 if (FD_ISSET(0, &ifds
)) {
150 assert(!ibuf_length
);
152 if ((r
= read(0, ibuf
, sizeof(ibuf
))) <= 0) {
154 pa_log(_("read(): %s"), strerror(errno
));
160 ibuf_length
= (size_t) r
;
165 if (FD_ISSET(fd
, &ifds
)) {
167 assert(!obuf_length
);
169 if ((r
= read(fd
, obuf
, sizeof(obuf
))) <= 0) {
171 pa_log(_("read(): %s"), strerror(errno
));
177 obuf_length
= (size_t) r
;
182 if (FD_ISSET(1, &ofds
)) {
186 if ((r
= write(1, obuf
+ obuf_index
, obuf_length
)) < 0) {
187 pa_log(_("write(): %s"), strerror(errno
));
191 obuf_length
-= (size_t) r
;
192 obuf_index
+= obuf_index
;
196 if (FD_ISSET(fd
, &ofds
)) {
200 if ((r
= write(fd
, ibuf
+ ibuf_index
, ibuf_length
)) < 0) {
201 pa_log(_("write(): %s"), strerror(errno
));
205 ibuf_length
-= (size_t) r
;
206 ibuf_index
+= obuf_index
;
209 if (ibuf_length
<= 0 && ibuf_eof
&& !ibuf_closed
) {
211 shutdown(fd
, SHUT_WR
);
215 if (obuf_length
<= 0 && obuf_eof
&& !obuf_closed
) {
216 shutdown(fd
, SHUT_RD
);