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 **);
46 int ctl_jump(struct parse_result *, int, char **);
47 int ctl_repeat(struct parse_result *, int, char **);
49 struct ctl_command ctl_commands[] = {
50 { "play", PLAY, ctl_noarg, "" },
51 { "pause", PAUSE, ctl_noarg, "" },
52 { "toggle", TOGGLE, ctl_noarg, "" },
53 { "stop", STOP, ctl_noarg, "" },
54 { "restart", RESTART, ctl_noarg, "" },
55 { "add", ADD, ctl_add, "files...", 1 },
56 { "flush", FLUSH, ctl_noarg, "" },
57 { "show", SHOW, ctl_show, "[-p]" },
58 { "status", STATUS, ctl_noarg, "" },
59 { "next", NEXT, ctl_noarg, "" },
60 { "prev", PREV, ctl_noarg, "" },
61 { "load", LOAD, ctl_load, "[file]", 1 },
62 { "jump", JUMP, ctl_jump, "pattern" },
63 { "repeat", REPEAT, ctl_repeat, "one|all on|off" },
64 { "monitor", MONITOR, ctl_noarg, "" },
65 { NULL },
66 };
68 __dead void
69 usage(void)
70 {
71 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
72 exit(1);
73 }
75 static __dead void
76 ctl_usage(struct ctl_command *ctl)
77 {
78 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
79 ctl->name, ctl->usage);
80 exit(1);
81 }
83 static int
84 parse(int argc, char **argv)
85 {
86 struct ctl_command *ctl = NULL;
87 struct parse_result res;
88 int i, status;
90 memset(&res, 0, sizeof(res));
92 for (i = 0; ctl_commands[i].name != NULL; ++i) {
93 if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
94 == 0) {
95 if (ctl != NULL) {
96 fprintf(stderr,
97 "ambiguous argument: %s\n", argv[0]);
98 usage();
99 }
100 ctl = &ctl_commands[i];
104 if (ctl == NULL) {
105 fprintf(stderr, "unknown argument: %s\n", argv[0]);
106 usage();
109 res.action = ctl->action;
110 res.ctl = ctl;
112 if (!ctl->has_pledge) {
113 /* pledge(2) default if command doesn't have its own */
114 if (pledge("stdio", NULL) == -1)
115 fatal("pledge");
118 status = ctl->main(&res, argc, argv);
119 close(ibuf->fd);
120 free(ibuf);
121 return status;
124 static int
125 enqueue_tracks(char **files)
127 char res[PATH_MAX];
128 int enq = 0;
130 for (; *files != NULL; ++files) {
131 memset(&res, 0, sizeof(res));
132 if (realpath(*files, res) == NULL) {
133 log_warn("realpath %s", *files);
134 continue;
137 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
138 res, sizeof(res));
139 enq++;
142 return enq == 0;
145 static int
146 jump_req(const char *arg)
148 char path[PATH_MAX];
150 memset(path, 0, sizeof(path));
151 strlcpy(path, arg, sizeof(path));
152 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1, path, sizeof(path));
153 return 0;
156 static void
157 print_error_message(const char *prfx, struct imsg *imsg)
159 size_t datalen;
160 char *msg;
162 datalen = IMSG_DATA_SIZE(*imsg);
163 if ((msg = calloc(1, datalen)) == NULL)
164 fatal("calloc %zu", datalen);
165 memcpy(msg, imsg->data, datalen);
166 if (datalen == 0 || msg[datalen-1] != '\0')
167 fatalx("malformed error message");
169 log_warnx("%s: %s", prfx, msg);
170 free(msg);
173 static int
174 show_add(struct imsg *imsg, int *ret, char ***files)
176 if (**files == NULL) {
177 log_warnx("received more replies than file sent");
178 *ret = 1;
179 return 1;
182 if (imsg->hdr.type == IMSG_CTL_ERR)
183 print_error_message(**files, imsg);
184 else if (imsg->hdr.type == IMSG_CTL_ADD)
185 log_debug("enqueued %s", **files);
186 else
187 fatalx("got invalid message %d", imsg->hdr.type);
189 (*files)++;
190 return (**files) == NULL;
193 static int
194 show_complete(struct parse_result *res, struct imsg *imsg, int *ret)
196 struct player_status s;
197 size_t datalen;
199 if (imsg->hdr.type == IMSG_CTL_ERR) {
200 print_error_message("show failed", imsg);
201 *ret = 1;
202 return 1;
205 datalen = IMSG_DATA_SIZE(*imsg);
206 if (datalen == 0)
207 return 1;
209 if (datalen != sizeof(s))
210 fatalx("%s: data size mismatch", __func__);
211 memcpy(&s, imsg->data, sizeof(s));
212 if (s.path[sizeof(s.path)-1] != '\0')
213 fatalx("%s: data corrupted?", __func__);
215 if (res->pretty)
216 printf("%c ", s.status == STATE_PLAYING ? '>' : ' ');
217 printf("%s\n", s.path);
218 return 0;
221 static int
222 show_status(struct imsg *imsg, int *ret)
224 struct player_status s;
225 size_t datalen;
227 if (imsg->hdr.type == IMSG_CTL_ERR) {
228 print_error_message("show failed", imsg);
229 *ret = 1;
230 return 1;
233 if (imsg->hdr.type != IMSG_CTL_STATUS)
234 fatalx("%s: got wrong reply", __func__);
236 datalen = IMSG_DATA_SIZE(*imsg);
237 if (datalen != sizeof(s))
238 fatalx("%s: data size mismatch", __func__);
239 memcpy(&s, imsg->data, sizeof(s));
240 if (s.path[sizeof(s.path)-1] != '\0')
241 fatalx("%s: data corrupted?", __func__);
243 switch (s.status) {
244 case STATE_STOPPED:
245 printf("stopped ");
246 break;
247 case STATE_PLAYING:
248 printf("playing ");
249 break;
250 case STATE_PAUSED:
251 printf("paused ");
252 break;
253 default:
254 printf("unknown ");
255 break;
258 printf("%s\n", s.path);
259 printf("repeat one %s -- repeat all %s\n",
260 s.rp.repeat_one ? "on" : "off",
261 s.rp.repeat_all ? "on" : "off");
262 return 1;
265 static int
266 show_load(struct parse_result *res, struct imsg *imsg, int *ret)
268 FILE *f;
269 const char *file;
270 char *line = NULL;
271 char path[PATH_MAX];
272 size_t linesize = 0;
273 ssize_t linelen;
274 int any = 0;
276 if (imsg->hdr.type == IMSG_CTL_ERR) {
277 print_error_message("load failed", imsg);
278 *ret = 1;
279 return 1;
282 if (imsg->hdr.type == IMSG_CTL_ADD)
283 return 0;
285 if (imsg->hdr.type == IMSG_CTL_COMMIT)
286 return 1;
288 if (imsg->hdr.type != IMSG_CTL_BEGIN)
289 fatalx("got unexpected message %d", imsg->hdr.type);
291 if (res->file == NULL)
292 f = stdin;
293 else if ((f = fopen(res->file, "r")) == NULL) {
294 log_warn("can't open %s", res->file);
295 *ret = 1;
296 return 1;
299 while ((linelen = getline(&line, &linesize, f)) != -1) {
300 if (linelen == 0)
301 continue;
302 line[linelen-1] = '\0';
303 file = line;
304 if (file[0] == '>' && file[1] == ' ')
305 file += 2;
306 if (file[0] == ' ' && file[1] == ' ')
307 file += 2;
309 memset(&path, 0, sizeof(path));
310 if (realpath(file, path) == NULL) {
311 log_warn("realpath %s", file);
312 continue;
315 any++;
316 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
317 path, sizeof(path));
320 free(line);
321 if (ferror(f))
322 fatal("getline");
323 fclose(f);
325 if (!any) {
326 *ret = 1;
327 return 1;
330 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
331 NULL, 0);
332 imsg_flush(ibuf);
333 return 0;
336 static int
337 show_monitor(struct imsg *imsg, int *ret)
339 int type;
341 if (imsg->hdr.type != IMSG_CTL_MONITOR) {
342 log_warnx("wrong message type received: %d",
343 imsg->hdr.type);
344 *ret = 1;
345 return 1;
348 if (IMSG_DATA_SIZE(*imsg) != sizeof(type)) {
349 log_warnx("size mismatch");
350 *ret = 1;
351 return 1;
354 memcpy(&type, imsg->data, sizeof(type));
355 switch (type) {
356 case IMSG_CTL_PLAY:
357 puts("play");
358 break;
359 case IMSG_CTL_TOGGLE_PLAY:
360 puts("toggle");
361 break;
362 case IMSG_CTL_PAUSE:
363 puts("pause");
364 break;
365 case IMSG_CTL_STOP:
366 puts("stop");
367 break;
368 case IMSG_CTL_RESTART:
369 puts("restart");
370 break;
371 case IMSG_CTL_FLUSH:
372 puts("flush");
373 break;
374 case IMSG_CTL_NEXT:
375 puts("next");
376 break;
377 case IMSG_CTL_PREV:
378 puts("prev");
379 break;
380 case IMSG_CTL_JUMP:
381 puts("jump");
382 break;
383 case IMSG_CTL_REPEAT:
384 puts("repeat");
385 break;
386 case IMSG_CTL_ADD:
387 puts("add");
388 break;
389 case IMSG_CTL_COMMIT:
390 puts("load");
391 break;
392 default:
393 puts("unknown");
394 break;
397 return 0;
400 static int
401 ctlaction(struct parse_result *res)
403 struct imsg imsg;
404 ssize_t n;
405 int ret = 0, done = 1;
406 char **files;
408 switch (res->action) {
409 case PLAY:
410 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
411 if (verbose) {
412 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
413 NULL, 0);
414 done = 0;
416 break;
417 case PAUSE:
418 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
419 break;
420 case TOGGLE:
421 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
422 if (verbose) {
423 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
424 NULL, 0);
425 done = 0;
427 break;
428 case STOP:
429 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
430 break;
431 case RESTART:
432 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
433 if (verbose) {
434 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
435 NULL, 0);
436 done = 0;
438 break;
439 case ADD:
440 done = 0;
441 files = res->files;
442 ret = enqueue_tracks(res->files);
443 break;
444 case FLUSH:
445 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
446 break;
447 case SHOW:
448 done = 0;
449 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
450 break;
451 case STATUS:
452 done = 0;
453 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
454 break;
455 case NEXT:
456 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
457 if (verbose) {
458 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
459 NULL, 0);
460 done = 0;
462 break;
463 case PREV:
464 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
465 if (verbose) {
466 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
467 NULL, 0);
468 done = 0;
470 break;
471 case LOAD:
472 done = 0;
473 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
474 break;
475 case JUMP:
476 done = 0;
477 ret = jump_req(res->file);
478 break;
479 case REPEAT:
480 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
481 &res->rep, sizeof(res->rep));
482 break;
483 case MONITOR:
484 done = 0;
485 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
486 NULL, 0);
487 break;
488 case NONE:
489 /* action not expected */
490 fatalx("invalid action %u", res->action);
491 break;
494 if (ret != 0)
495 goto end;
497 imsg_flush(ibuf);
499 while (!done) {
500 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
501 fatalx("imsg_read error");
502 if (n == 0)
503 fatalx("pipe closed");
505 while (!done) {
506 if ((n = imsg_get(ibuf, &imsg)) == -1)
507 fatalx("imsg_get error");
508 if (n == 0)
509 break;
511 switch (res->action) {
512 case ADD:
513 done = show_add(&imsg, &ret, &files);
514 break;
515 case SHOW:
516 done = show_complete(res, &imsg, &ret);
517 break;
518 case PLAY:
519 case TOGGLE:
520 case RESTART:
521 case STATUS:
522 case NEXT:
523 case PREV:
524 case JUMP:
525 done = show_status(&imsg, &ret);
526 break;
527 case LOAD:
528 done = show_load(res, &imsg, &ret);
529 break;
530 case MONITOR:
531 done = show_monitor(&imsg, &ret);
532 break;
533 default:
534 done = 1;
535 break;
538 imsg_free(&imsg);
542 end:
543 return ret;
546 int
547 ctl_noarg(struct parse_result *res, int argc, char **argv)
549 if (argc != 1)
550 ctl_usage(res->ctl);
551 return ctlaction(res);
554 int
555 ctl_add(struct parse_result *res, int argc, char **argv)
557 if (argc < 2)
558 ctl_usage(res->ctl);
559 res->files = argv+1;
561 if (pledge("stdio rpath", NULL) == -1)
562 fatal("pledge");
564 return ctlaction(res);
567 int
568 ctl_show(struct parse_result *res, int argc, char **argv)
570 int ch;
572 while ((ch = getopt(argc, argv, "p")) != -1) {
573 switch (ch) {
574 case 'p':
575 res->pretty = 1;
576 break;
577 default:
578 ctl_usage(res->ctl);
582 return ctlaction(res);
585 int
586 ctl_load(struct parse_result *res, int argc, char **argv)
588 if (argc < 2)
589 res->file = NULL;
590 else if (argc == 2)
591 res->file = argv[1];
592 else
593 ctl_usage(res->ctl);
595 if (pledge("stdio rpath", NULL) == -1)
596 fatal("pledge");
598 return ctlaction(res);
601 int
602 ctl_jump(struct parse_result *res, int argc, char **argv)
604 int ch;
606 while ((ch = getopt(argc, argv, "")) != -1)
607 ctl_usage(res->ctl);
608 argc -= optind;
609 argv += optind;
611 if (argc != 1)
612 ctl_usage(res->ctl);
614 res->file = argv[0];
615 return ctlaction(res);
618 int
619 ctl_repeat(struct parse_result *res, int argc, char **argv)
621 int ch, b;
623 while ((ch = getopt(argc, argv, "")) != -1)
624 ctl_usage(res->ctl);
625 argc -= optind;
626 argv += optind;
628 if (argc != 2)
629 ctl_usage(res->ctl);
631 if (!strcmp(argv[1], "on"))
632 b = 1;
633 else if (!strcmp(argv[1], "off"))
634 b = 0;
635 else
636 ctl_usage(res->ctl);
638 res->rep.repeat_one = -1;
639 res->rep.repeat_all = -1;
640 if (!strcmp(argv[0], "one"))
641 res->rep.repeat_one = b;
642 else if (!strcmp(argv[0], "all"))
643 res->rep.repeat_all = b;
644 else
645 ctl_usage(res->ctl);
647 return ctlaction(res);
650 static int
651 sockconn(void)
653 struct sockaddr_un sun;
654 int sock, saved_errno;
656 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
657 fatal("socket");
659 memset(&sun, 0, sizeof(sun));
660 sun.sun_family = AF_UNIX;
661 strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
662 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
663 saved_errno = errno;
664 close(sock);
665 errno = saved_errno;
666 return -1;
669 return sock;
672 __dead void
673 ctl(int argc, char **argv)
675 int ctl_sock, i = 0;
677 log_init(1, LOG_DAEMON);
678 log_setverbose(verbose);
680 do {
681 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
683 if ((ctl_sock = sockconn()) != -1)
684 break;
685 if (errno != ENOENT && errno != ECONNREFUSED)
686 fatal("connect %s", csock);
688 if (i == 0)
689 spawn_daemon();
691 nanosleep(&ts, NULL);
692 } while (++i < 20);
694 if (ctl_sock == -1)
695 fatalx("failed to connect to the daemon");
697 ibuf = xmalloc(sizeof(*ibuf));
698 imsg_init(ibuf, ctl_sock);
700 optreset = 1;
701 optind = 1;
703 exit(parse(argc, argv));