Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/un.h>
23 #include <errno.h>
24 #include <event.h>
25 #include <limits.h>
26 #include <sndio.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <string.h>
31 #include <syslog.h>
32 #include <unistd.h>
33 #include <imsg.h>
35 #include "amused.h"
36 #include "log.h"
37 #include "playlist.h"
38 #include "xmalloc.h"
40 static struct imsgbuf *ibuf;
42 int ctl_noarg(struct parse_result *, int, char **);
43 int ctl_add(struct parse_result *, int, char **);
44 int ctl_show(struct parse_result *, int, char **);
45 int ctl_load(struct parse_result *, int, char **);
47 struct ctl_command ctl_commands[] = {
48 { "play", PLAY, ctl_noarg, "" },
49 { "pause", PAUSE, ctl_noarg, "" },
50 { "toggle", TOGGLE, ctl_noarg, "" },
51 { "stop", STOP, ctl_noarg, "" },
52 { "restart", RESTART, ctl_noarg, "" },
53 { "add", ADD, ctl_add, "files...", 1 },
54 { "flush", FLUSH, ctl_noarg, "" },
55 { "show", SHOW, ctl_show, "" },
56 { "status", STATUS, ctl_noarg, "" },
57 { "next", NEXT, ctl_noarg, "" },
58 { "prev", PREV, ctl_noarg, "" },
59 { "load", LOAD, ctl_load, "[file]", 1 },
60 { NULL },
61 };
63 __dead void
64 usage(void)
65 {
66 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
67 fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
68 exit(1);
69 }
71 static __dead void
72 ctl_usage(struct ctl_command *ctl)
73 {
74 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
75 ctl->name, ctl->usage);
76 exit(1);
77 }
79 static int
80 parse(int argc, char **argv)
81 {
82 struct ctl_command *ctl = NULL;
83 struct parse_result res;
84 int i, status;
86 memset(&res, 0, sizeof(res));
88 for (i = 0; ctl_commands[i].name != NULL; ++i) {
89 if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
90 == 0) {
91 if (ctl != NULL) {
92 fprintf(stderr,
93 "ambiguous argument: %s\n", argv[0]);
94 usage();
95 }
96 ctl = &ctl_commands[i];
97 }
98 }
100 if (ctl == NULL) {
101 fprintf(stderr, "unknown argument: %s\n", argv[0]);
102 usage();
105 res.action = ctl->action;
106 res.ctl = ctl;
108 if (!ctl->has_pledge) {
109 /* pledge(2) default if command doesn't have its own */
110 if (pledge("stdio", NULL) == -1)
111 fatal("pledge");
114 status = ctl->main(&res, argc, argv);
115 close(ibuf->fd);
116 free(ibuf);
117 return status;
120 static int
121 enqueue_tracks(char **files)
123 char res[PATH_MAX];
124 int enq = 0;
126 for (; *files != NULL; ++files) {
127 memset(&res, 0, sizeof(res));
128 if (realpath(*files, res) == NULL) {
129 log_warn("realpath %s", *files);
130 continue;
133 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
134 res, sizeof(res));
135 enq++;
138 return enq == 0;
141 static void
142 print_error_message(const char *prfx, struct imsg *imsg)
144 size_t datalen;
145 char *msg;
147 datalen = IMSG_DATA_SIZE(*imsg);
148 if ((msg = calloc(1, datalen)) == NULL)
149 fatal("calloc %zu", datalen);
150 memcpy(msg, imsg->data, datalen);
151 if (datalen == 0 || msg[datalen-1] != '\0')
152 fatalx("malformed error message");
154 log_warnx("%s: %s", prfx, msg);
155 free(msg);
158 static int
159 show_add(struct imsg *imsg, int *ret, char ***files)
161 if (**files == NULL) {
162 log_warnx("received more replies than file sent");
163 *ret = 1;
164 return 1;
167 if (imsg->hdr.type == IMSG_CTL_ERR)
168 print_error_message(**files, imsg);
169 else if (imsg->hdr.type == IMSG_CTL_ADD)
170 log_debug("enqueued %s", **files);
171 else
172 fatalx("got invalid message %d", imsg->hdr.type);
174 (*files)++;
175 return (**files) == NULL;
178 static int
179 show_complete(struct parse_result *res, struct imsg *imsg, int *ret)
181 struct player_status s;
182 size_t datalen;
184 if (imsg->hdr.type == IMSG_CTL_ERR) {
185 print_error_message("show failed", imsg);
186 *ret = 1;
187 return 1;
190 datalen = IMSG_DATA_SIZE(*imsg);
191 if (datalen == 0)
192 return 1;
194 if (datalen != sizeof(s))
195 fatalx("%s: data size mismatch", __func__);
196 memcpy(&s, imsg->data, sizeof(s));
197 if (s.path[sizeof(s.path)-1] != '\0')
198 fatalx("%s: data corrupted?", __func__);
200 if (res->pretty)
201 printf("%c ", s.status == STATE_PLAYING ? '>' : ' ');
202 printf("%s\n", s.path);
203 return 0;
206 static int
207 show_status(struct imsg *imsg, int *ret)
209 struct player_status s;
210 size_t datalen;
212 if (imsg->hdr.type == IMSG_CTL_ERR) {
213 print_error_message("show failed", imsg);
214 *ret = 1;
215 return 1;
218 if (imsg->hdr.type != IMSG_CTL_STATUS)
219 fatalx("%s: got wrong reply", __func__);
221 datalen = IMSG_DATA_SIZE(*imsg);
222 if (datalen != sizeof(s))
223 fatalx("%s: data size mismatch", __func__);
224 memcpy(&s, imsg->data, sizeof(s));
225 if (s.path[sizeof(s.path)-1] != '\0')
226 fatalx("%s: data corrupted?", __func__);
228 switch (s.status) {
229 case STATE_STOPPED:
230 printf("stopped ");
231 break;
232 case STATE_PLAYING:
233 printf("playing ");
234 break;
235 case STATE_PAUSED:
236 printf("paused ");
237 break;
238 default:
239 printf("unknown ");
240 break;
243 printf("%s\n", s.path);
244 return 1;
247 static int
248 show_load(struct parse_result *res, struct imsg *imsg, int *ret)
250 const char *file;
251 char *line = NULL;
252 char path[PATH_MAX];
253 size_t linesize = 0;
254 ssize_t linelen;
255 int any = 0;
257 if (imsg->hdr.type == IMSG_CTL_ERR) {
258 print_error_message("load failed", imsg);
259 *ret = 1;
260 return 1;
263 if (imsg->hdr.type == IMSG_CTL_ADD)
264 return 0;
266 if (imsg->hdr.type == IMSG_CTL_COMMIT)
267 return 1;
269 if (imsg->hdr.type != IMSG_CTL_BEGIN)
270 fatalx("got unexpected message %d", imsg->hdr.type);
272 while ((linelen = getline(&line, &linesize, res->file)) != -1) {
273 if (linelen == 0)
274 continue;
275 line[linelen-1] = '\0';
276 file = line;
277 if (file[0] == '>' && file[1] == ' ')
278 file += 2;
279 if (file[0] == ' ' && file[1] == ' ')
280 file += 2;
282 memset(&path, 0, sizeof(path));
283 if (realpath(file, path) == NULL) {
284 log_warn("realpath %s", file);
285 continue;
288 any++;
289 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
290 path, sizeof(path));
293 free(line);
294 if (ferror(res->file))
295 fatal("getline");
296 fclose(res->file);
297 res->file = NULL;
299 if (!any) {
300 *ret = 1;
301 return 1;
304 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
305 NULL, 0);
306 imsg_flush(ibuf);
307 return 0;
310 static int
311 ctlaction(struct parse_result *res)
313 struct imsg imsg;
314 ssize_t n;
315 int ret = 0, done = 1;
316 char **files;
318 switch (res->action) {
319 case PLAY:
320 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
321 break;
322 case PAUSE:
323 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
324 break;
325 case TOGGLE:
326 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
327 break;
328 case STOP:
329 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
330 break;
331 case RESTART:
332 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
333 break;
334 case ADD:
335 done = 0;
336 files = res->files;
337 ret = enqueue_tracks(res->files);
338 break;
339 case FLUSH:
340 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
341 break;
342 case SHOW:
343 done = 0;
344 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
345 break;
346 case STATUS:
347 done = 0;
348 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
349 break;
350 case NEXT:
351 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
352 break;
353 case PREV:
354 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
355 break;
356 case LOAD:
357 done = 0;
358 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
359 break;
360 case NONE:
361 /* action not expected */
362 fatalx("invalid action %u", res->action);
363 break;
366 if (ret != 0)
367 goto end;
369 imsg_flush(ibuf);
371 while (!done) {
372 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
373 fatalx("imsg_read error");
374 if (n == 0)
375 fatalx("pipe closed");
377 while (!done) {
378 if ((n = imsg_get(ibuf, &imsg)) == -1)
379 fatalx("imsg_get error");
380 if (n == 0)
381 break;
383 switch (res->action) {
384 case ADD:
385 done = show_add(&imsg, &ret, &files);
386 break;
387 case SHOW:
388 done = show_complete(res, &imsg, &ret);
389 break;
390 case STATUS:
391 done = show_status(&imsg, &ret);
392 break;
393 case LOAD:
394 done = show_load(res, &imsg, &ret);
395 break;
396 default:
397 done = 1;
398 break;
401 imsg_free(&imsg);
405 end:
406 return ret;
409 int
410 ctl_noarg(struct parse_result *res, int argc, char **argv)
412 if (argc != 1)
413 ctl_usage(res->ctl);
414 return ctlaction(res);
417 int
418 ctl_add(struct parse_result *res, int argc, char **argv)
420 if (argc < 2)
421 ctl_usage(res->ctl);
422 res->files = argv+1;
424 if (pledge("stdio rpath", NULL) == -1)
425 fatal("pledge");
427 return ctlaction(res);
430 int
431 ctl_show(struct parse_result *res, int argc, char **argv)
433 int ch;
435 while ((ch = getopt(argc, argv, "p")) != -1) {
436 switch (ch) {
437 case 'p':
438 res->pretty = 1;
439 break;
440 default:
441 ctl_usage(res->ctl);
445 return ctlaction(res);
448 int
449 ctl_load(struct parse_result *res, int argc, char **argv)
451 if (argc < 2)
452 res->file = stdin;
453 else if (argc == 2) {
454 if ((res->file = fopen(argv[1], "r")) == NULL)
455 fatal("open %s", argv[1]);
456 } else
457 ctl_usage(res->ctl);
459 if (pledge("stdio rpath", NULL) == -1)
460 fatal("pledge");
462 return ctlaction(res);
465 static int
466 sockconn(void)
468 struct sockaddr_un sun;
469 int sock, saved_errno;
471 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
472 fatal("socket");
474 memset(&sun, 0, sizeof(sun));
475 sun.sun_family = AF_UNIX;
476 strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
477 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
478 saved_errno = errno;
479 close(sock);
480 errno = saved_errno;
481 return -1;
484 return sock;
487 __dead void
488 ctl(int argc, char **argv)
490 int ctl_sock, i = 0;
492 log_init(1, LOG_DAEMON);
493 log_setverbose(verbose);
495 do {
496 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
498 if ((ctl_sock = sockconn()) != -1)
499 break;
500 if (errno != ENOENT && errno != ECONNREFUSED)
501 fatal("connect %s", csock);
503 if (i == 0)
504 spawn_daemon();
506 nanosleep(&ts, NULL);
507 } while (++i < 20);
509 if (ctl_sock == -1)
510 fatalx("failed to connect to the daemon");
512 ibuf = xmalloc(sizeof(*ibuf));
513 imsg_init(ibuf, ctl_sock);
515 optreset = 1;
516 optind = 1;
518 exit(parse(argc, argv));