bluetooth: Refactor property parsing code
[profile/ivi/pulseaudio-panda.git] / 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", _("#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-log-level", _("NUMERIC LEVEL"));
77     printf("%s %s %s\n", argv0, "set-log-meta", _("1|0"));
78     printf("%s %s %s\n", argv0, "set-log-time", _("1|0"));
79     printf("%s %s %s\n", argv0, "set-log-backtrace", _("FRAMES"));
80
81     printf(_("\n"
82          "  -h, --help                            Show this help\n"
83          "      --version                         Show version\n"
84          "When no command is given pacdm starts in the interactive mode\n" ));
85 }
86
87 enum {
88     ARG_VERSION = 256
89 };
90
91 int main(int argc, char*argv[]) {
92     pid_t pid;
93     int fd = -1;
94     int ret = 1, i;
95     struct sockaddr_un sa;
96     char *ibuf = NULL;
97     char *obuf = NULL;
98     size_t buf_size, ibuf_size, ibuf_index, ibuf_length, obuf_size, obuf_index, obuf_length;
99     char *cli;
100     pa_bool_t ibuf_eof, obuf_eof, ibuf_closed, obuf_closed;
101     struct pollfd pollfd[3];
102     struct pollfd *watch_socket, *watch_stdin, *watch_stdout;
103
104     int stdin_type = 0, stdout_type = 0, fd_type = 0;
105
106     char *bn = NULL;
107     int c;
108
109     static const struct option long_options[] = {
110         {"version",     0, NULL, ARG_VERSION},
111         {"help",        0, NULL, 'h'},
112         {NULL,          0, NULL, 0}
113     };
114
115     setlocale(LC_ALL, "");
116 #ifdef ENABLE_NLS
117     bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
118 #endif
119
120     bn = pa_path_get_filename(argv[0]);
121
122     while ((c = getopt_long(argc, argv, "h", long_options, NULL)) != -1) {
123         switch (c) {
124             case 'h' :
125                 help(bn);
126                 ret = 0;
127                 goto quit;
128             case ARG_VERSION:
129                 printf(_("pacmd %s\n"
130                          "Compiled with libpulse %s\n"
131                          "Linked with libpulse %s\n"),
132                        PACKAGE_VERSION,
133                        pa_get_headers_version(),
134                        pa_get_library_version());
135                 ret = 0;
136                 goto quit;
137             default:
138                 goto quit;
139         }
140     }
141
142     if (pa_pid_file_check_running(&pid, "pulseaudio") < 0) {
143         pa_log(_("No PulseAudio daemon running, or not running as session daemon."));
144         goto quit;
145     }
146
147     if ((fd = pa_socket_cloexec(PF_UNIX, SOCK_STREAM, 0)) < 0) {
148         pa_log(_("socket(PF_UNIX, SOCK_STREAM, 0): %s"), strerror(errno));
149         goto quit;
150     }
151
152     pa_zero(sa);
153     sa.sun_family = AF_UNIX;
154
155     if (!(cli = pa_runtime_path("cli")))
156         goto quit;
157
158     pa_strlcpy(sa.sun_path, cli, sizeof(sa.sun_path));
159     pa_xfree(cli);
160
161     for (i = 0; i < 5; i++) {
162         int r;
163
164         if ((r = connect(fd, (struct sockaddr*) &sa, sizeof(sa))) < 0 && (errno != ECONNREFUSED && errno != ENOENT)) {
165             pa_log(_("connect(): %s"), strerror(errno));
166             goto quit;
167         }
168
169
170         if (r >= 0)
171             break;
172
173         if (pa_pid_file_kill(SIGUSR2, NULL, "pulseaudio") < 0) {
174             pa_log(_("Failed to kill PulseAudio daemon."));
175             goto quit;
176         }
177
178         pa_msleep(300);
179     }
180
181     if (i >= 5) {
182         pa_log(_("Daemon not responding."));
183         goto quit;
184     }
185
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;
193
194     if (argc > 1) {
195         for (i = 1; i < argc; i++) {
196             size_t k;
197
198             k = PA_MIN(ibuf_size - ibuf_length, strlen(argv[i]));
199             memcpy(ibuf + ibuf_length, argv[i], k);
200             ibuf_length += k;
201
202             if (ibuf_length < ibuf_size) {
203                 ibuf[ibuf_length] = i < argc-1 ? ' ' : '\n';
204                 ibuf_length++;
205             }
206         }
207
208         ibuf_eof = TRUE;
209     }
210
211     for (;;) {
212         struct pollfd *p;
213
214         if (ibuf_eof &&
215             obuf_eof &&
216             ibuf_length <= 0 &&
217             obuf_length <= 0)
218             break;
219
220         if (ibuf_length <= 0 && ibuf_eof && !ibuf_closed) {
221             shutdown(fd, SHUT_WR);
222             ibuf_closed = TRUE;
223         }
224
225         if (obuf_length <= 0 && obuf_eof && !obuf_closed) {
226             shutdown(fd, SHUT_RD);
227             obuf_closed = TRUE;
228         }
229
230         pa_zero(pollfd);
231
232         p = pollfd;
233
234         if (ibuf_length > 0 || (!obuf_eof && obuf_length <= 0)) {
235             watch_socket = p++;
236             watch_socket->fd = fd;
237             watch_socket->events =
238                 (ibuf_length > 0 ? POLLOUT : 0) |
239                 (!obuf_eof && obuf_length <= 0 ? POLLIN : 0);
240         } else
241             watch_socket = NULL;
242
243         if (!ibuf_eof && ibuf_length <= 0) {
244             watch_stdin = p++;
245             watch_stdin->fd = STDIN_FILENO;
246             watch_stdin->events = POLLIN;
247         } else
248             watch_stdin = NULL;
249
250         if (obuf_length > 0) {
251             watch_stdout = p++;
252             watch_stdout->fd = STDOUT_FILENO;
253             watch_stdout->events = POLLOUT;
254         } else
255             watch_stdout = NULL;
256
257         if (pa_poll(pollfd, p-pollfd, -1) < 0) {
258
259             if (errno == EINTR)
260                 continue;
261
262             pa_log(_("poll(): %s"), strerror(errno));
263             goto quit;
264         }
265
266         if (watch_stdin) {
267             if (watch_stdin->revents & POLLIN) {
268                 ssize_t r;
269                 pa_assert(ibuf_length <= 0);
270
271                 if ((r = pa_read(STDIN_FILENO, ibuf, ibuf_size, &stdin_type)) <= 0) {
272                     if (r < 0) {
273                         pa_log(_("read(): %s"), strerror(errno));
274                         goto quit;
275                     }
276
277                     ibuf_eof = TRUE;
278                 } else {
279                     ibuf_length = (size_t) r;
280                     ibuf_index = 0;
281                 }
282             } else if (watch_stdin->revents & POLLHUP)
283                 ibuf_eof = TRUE;
284         }
285
286         if (watch_socket) {
287             if (watch_socket->revents & POLLIN) {
288                 ssize_t r;
289                 pa_assert(obuf_length <= 0);
290
291                 if ((r = pa_read(fd, obuf, obuf_size, &fd_type)) <= 0) {
292                     if (r < 0) {
293                         pa_log(_("read(): %s"), strerror(errno));
294                         goto quit;
295                     }
296
297                     obuf_eof = TRUE;
298                 } else {
299                     obuf_length = (size_t) r;
300                     obuf_index = 0;
301                 }
302             } else if (watch_socket->revents & POLLHUP)
303                 obuf_eof = TRUE;
304         }
305
306         if (watch_stdout) {
307             if (watch_stdout->revents & POLLHUP) {
308                 obuf_eof = TRUE;
309                 obuf_length = 0;
310             } else if (watch_stdout->revents & POLLOUT) {
311                 ssize_t r;
312                 pa_assert(obuf_length > 0);
313
314                 if ((r = pa_write(STDOUT_FILENO, obuf + obuf_index, obuf_length, &stdout_type)) < 0) {
315                     pa_log(_("write(): %s"), strerror(errno));
316                     goto quit;
317                 }
318
319                 obuf_length -= (size_t) r;
320                 obuf_index += obuf_index;
321             }
322         }
323
324         if (watch_socket) {
325             if (watch_socket->revents & POLLHUP) {
326                 ibuf_eof = TRUE;
327                 ibuf_length = 0;
328             } if (watch_socket->revents & POLLOUT) {
329                 ssize_t r;
330                 pa_assert(ibuf_length > 0);
331
332                 if ((r = pa_write(fd, ibuf + ibuf_index, ibuf_length, &fd_type)) < 0) {
333                     pa_log(_("write(): %s"), strerror(errno));
334                     goto quit;
335                 }
336
337                 ibuf_length -= (size_t) r;
338                 ibuf_index += obuf_index;
339             }
340         }
341     }
342
343     ret = 0;
344
345 quit:
346     if (fd >= 0)
347         pa_close(fd);
348
349     pa_xfree(obuf);
350     pa_xfree(ibuf);
351
352     return ret;
353 }