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 { NULL },
65 };
67 __dead void
68 usage(void)
69 {
70 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
71 exit(1);
72 }
74 static __dead void
75 ctl_usage(struct ctl_command *ctl)
76 {
77 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
78 ctl->name, ctl->usage);
79 exit(1);
80 }
82 static int
83 parse(int argc, char **argv)
84 {
85 struct ctl_command *ctl = NULL;
86 struct parse_result res;
87 int i, status;
89 memset(&res, 0, sizeof(res));
91 for (i = 0; ctl_commands[i].name != NULL; ++i) {
92 if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
93 == 0) {
94 if (ctl != NULL) {
95 fprintf(stderr,
96 "ambiguous argument: %s\n", argv[0]);
97 usage();
98 }
99 ctl = &ctl_commands[i];
103 if (ctl == NULL) {
104 fprintf(stderr, "unknown argument: %s\n", argv[0]);
105 usage();
108 res.action = ctl->action;
109 res.ctl = ctl;
111 if (!ctl->has_pledge) {
112 /* pledge(2) default if command doesn't have its own */
113 if (pledge("stdio", NULL) == -1)
114 fatal("pledge");
117 status = ctl->main(&res, argc, argv);
118 close(ibuf->fd);
119 free(ibuf);
120 return status;
123 static int
124 enqueue_tracks(char **files)
126 char res[PATH_MAX];
127 int enq = 0;
129 for (; *files != NULL; ++files) {
130 memset(&res, 0, sizeof(res));
131 if (realpath(*files, res) == NULL) {
132 log_warn("realpath %s", *files);
133 continue;
136 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
137 res, sizeof(res));
138 enq++;
141 return enq == 0;
144 static int
145 jump_req(const char *arg)
147 char path[PATH_MAX];
149 memset(path, 0, sizeof(path));
150 strlcpy(path, arg, sizeof(path));
151 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1, path, sizeof(path));
152 return 0;
155 static void
156 print_error_message(const char *prfx, struct imsg *imsg)
158 size_t datalen;
159 char *msg;
161 datalen = IMSG_DATA_SIZE(*imsg);
162 if ((msg = calloc(1, datalen)) == NULL)
163 fatal("calloc %zu", datalen);
164 memcpy(msg, imsg->data, datalen);
165 if (datalen == 0 || msg[datalen-1] != '\0')
166 fatalx("malformed error message");
168 log_warnx("%s: %s", prfx, msg);
169 free(msg);
172 static int
173 show_add(struct imsg *imsg, int *ret, char ***files)
175 if (**files == NULL) {
176 log_warnx("received more replies than file sent");
177 *ret = 1;
178 return 1;
181 if (imsg->hdr.type == IMSG_CTL_ERR)
182 print_error_message(**files, imsg);
183 else if (imsg->hdr.type == IMSG_CTL_ADD)
184 log_debug("enqueued %s", **files);
185 else
186 fatalx("got invalid message %d", imsg->hdr.type);
188 (*files)++;
189 return (**files) == NULL;
192 static int
193 show_complete(struct parse_result *res, struct imsg *imsg, int *ret)
195 struct player_status s;
196 size_t datalen;
198 if (imsg->hdr.type == IMSG_CTL_ERR) {
199 print_error_message("show failed", imsg);
200 *ret = 1;
201 return 1;
204 datalen = IMSG_DATA_SIZE(*imsg);
205 if (datalen == 0)
206 return 1;
208 if (datalen != sizeof(s))
209 fatalx("%s: data size mismatch", __func__);
210 memcpy(&s, imsg->data, sizeof(s));
211 if (s.path[sizeof(s.path)-1] != '\0')
212 fatalx("%s: data corrupted?", __func__);
214 if (res->pretty)
215 printf("%c ", s.status == STATE_PLAYING ? '>' : ' ');
216 printf("%s\n", s.path);
217 return 0;
220 static int
221 show_status(struct imsg *imsg, int *ret)
223 struct player_status s;
224 size_t datalen;
226 if (imsg->hdr.type == IMSG_CTL_ERR) {
227 print_error_message("show failed", imsg);
228 *ret = 1;
229 return 1;
232 if (imsg->hdr.type != IMSG_CTL_STATUS)
233 fatalx("%s: got wrong reply", __func__);
235 datalen = IMSG_DATA_SIZE(*imsg);
236 if (datalen != sizeof(s))
237 fatalx("%s: data size mismatch", __func__);
238 memcpy(&s, imsg->data, sizeof(s));
239 if (s.path[sizeof(s.path)-1] != '\0')
240 fatalx("%s: data corrupted?", __func__);
242 switch (s.status) {
243 case STATE_STOPPED:
244 printf("stopped ");
245 break;
246 case STATE_PLAYING:
247 printf("playing ");
248 break;
249 case STATE_PAUSED:
250 printf("paused ");
251 break;
252 default:
253 printf("unknown ");
254 break;
257 printf("%s\n", s.path);
258 printf("repeat one %s -- repeat all %s\n",
259 s.rp.repeat_one ? "on" : "off",
260 s.rp.repeat_all ? "on" : "off");
261 return 1;
264 static int
265 show_load(struct parse_result *res, struct imsg *imsg, int *ret)
267 FILE *f;
268 const char *file;
269 char *line = NULL;
270 char path[PATH_MAX];
271 size_t linesize = 0;
272 ssize_t linelen;
273 int any = 0;
275 if (imsg->hdr.type == IMSG_CTL_ERR) {
276 print_error_message("load failed", imsg);
277 *ret = 1;
278 return 1;
281 if (imsg->hdr.type == IMSG_CTL_ADD)
282 return 0;
284 if (imsg->hdr.type == IMSG_CTL_COMMIT)
285 return 1;
287 if (imsg->hdr.type != IMSG_CTL_BEGIN)
288 fatalx("got unexpected message %d", imsg->hdr.type);
290 if (res->file == NULL)
291 f = stdin;
292 else if ((f = fopen(res->file, "r")) == NULL) {
293 log_warn("can't open %s", res->file);
294 *ret = 1;
295 return 1;
298 while ((linelen = getline(&line, &linesize, f)) != -1) {
299 if (linelen == 0)
300 continue;
301 line[linelen-1] = '\0';
302 file = line;
303 if (file[0] == '>' && file[1] == ' ')
304 file += 2;
305 if (file[0] == ' ' && file[1] == ' ')
306 file += 2;
308 memset(&path, 0, sizeof(path));
309 if (realpath(file, path) == NULL) {
310 log_warn("realpath %s", file);
311 continue;
314 any++;
315 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
316 path, sizeof(path));
319 free(line);
320 if (ferror(f))
321 fatal("getline");
322 fclose(f);
324 if (!any) {
325 *ret = 1;
326 return 1;
329 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
330 NULL, 0);
331 imsg_flush(ibuf);
332 return 0;
335 static int
336 ctlaction(struct parse_result *res)
338 struct imsg imsg;
339 ssize_t n;
340 int ret = 0, done = 1;
341 char **files;
343 switch (res->action) {
344 case PLAY:
345 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
346 if (verbose) {
347 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
348 NULL, 0);
349 done = 0;
351 break;
352 case PAUSE:
353 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
354 break;
355 case TOGGLE:
356 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
357 if (verbose) {
358 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
359 NULL, 0);
360 done = 0;
362 break;
363 case STOP:
364 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
365 break;
366 case RESTART:
367 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
368 if (verbose) {
369 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
370 NULL, 0);
371 done = 0;
373 break;
374 case ADD:
375 done = 0;
376 files = res->files;
377 ret = enqueue_tracks(res->files);
378 break;
379 case FLUSH:
380 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
381 break;
382 case SHOW:
383 done = 0;
384 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
385 break;
386 case STATUS:
387 done = 0;
388 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
389 break;
390 case NEXT:
391 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
392 if (verbose) {
393 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
394 NULL, 0);
395 done = 0;
397 break;
398 case PREV:
399 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
400 if (verbose) {
401 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
402 NULL, 0);
403 done = 0;
405 break;
406 case LOAD:
407 done = 0;
408 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
409 break;
410 case JUMP:
411 done = 0;
412 ret = jump_req(res->file);
413 break;
414 case REPEAT:
415 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
416 &res->rep, sizeof(res->rep));
417 break;
418 case NONE:
419 /* action not expected */
420 fatalx("invalid action %u", res->action);
421 break;
424 if (ret != 0)
425 goto end;
427 imsg_flush(ibuf);
429 while (!done) {
430 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
431 fatalx("imsg_read error");
432 if (n == 0)
433 fatalx("pipe closed");
435 while (!done) {
436 if ((n = imsg_get(ibuf, &imsg)) == -1)
437 fatalx("imsg_get error");
438 if (n == 0)
439 break;
441 switch (res->action) {
442 case ADD:
443 done = show_add(&imsg, &ret, &files);
444 break;
445 case SHOW:
446 done = show_complete(res, &imsg, &ret);
447 break;
448 case PLAY:
449 case TOGGLE:
450 case RESTART:
451 case STATUS:
452 case NEXT:
453 case PREV:
454 case JUMP:
455 done = show_status(&imsg, &ret);
456 break;
457 case LOAD:
458 done = show_load(res, &imsg, &ret);
459 break;
460 default:
461 done = 1;
462 break;
465 imsg_free(&imsg);
469 end:
470 return ret;
473 int
474 ctl_noarg(struct parse_result *res, int argc, char **argv)
476 if (argc != 1)
477 ctl_usage(res->ctl);
478 return ctlaction(res);
481 int
482 ctl_add(struct parse_result *res, int argc, char **argv)
484 if (argc < 2)
485 ctl_usage(res->ctl);
486 res->files = argv+1;
488 if (pledge("stdio rpath", NULL) == -1)
489 fatal("pledge");
491 return ctlaction(res);
494 int
495 ctl_show(struct parse_result *res, int argc, char **argv)
497 int ch;
499 while ((ch = getopt(argc, argv, "p")) != -1) {
500 switch (ch) {
501 case 'p':
502 res->pretty = 1;
503 break;
504 default:
505 ctl_usage(res->ctl);
509 return ctlaction(res);
512 int
513 ctl_load(struct parse_result *res, int argc, char **argv)
515 if (argc < 2)
516 res->file = NULL;
517 else if (argc == 2)
518 res->file = argv[1];
519 else
520 ctl_usage(res->ctl);
522 if (pledge("stdio rpath", NULL) == -1)
523 fatal("pledge");
525 return ctlaction(res);
528 int
529 ctl_jump(struct parse_result *res, int argc, char **argv)
531 int ch;
533 while ((ch = getopt(argc, argv, "")) != -1)
534 ctl_usage(res->ctl);
535 argc -= optind;
536 argv += optind;
538 if (argc != 1)
539 ctl_usage(res->ctl);
541 res->file = argv[0];
542 return ctlaction(res);
545 int
546 ctl_repeat(struct parse_result *res, int argc, char **argv)
548 int ch, b;
550 while ((ch = getopt(argc, argv, "")) != -1)
551 ctl_usage(res->ctl);
552 argc -= optind;
553 argv += optind;
555 if (argc != 2)
556 ctl_usage(res->ctl);
558 if (!strcmp(argv[1], "on"))
559 b = 1;
560 else if (!strcmp(argv[1], "off"))
561 b = 0;
562 else
563 ctl_usage(res->ctl);
565 res->rep.repeat_one = -1;
566 res->rep.repeat_all = -1;
567 if (!strcmp(argv[0], "one"))
568 res->rep.repeat_one = b;
569 else if (!strcmp(argv[0], "all"))
570 res->rep.repeat_all = b;
571 else
572 ctl_usage(res->ctl);
574 return ctlaction(res);
577 static int
578 sockconn(void)
580 struct sockaddr_un sun;
581 int sock, saved_errno;
583 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
584 fatal("socket");
586 memset(&sun, 0, sizeof(sun));
587 sun.sun_family = AF_UNIX;
588 strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
589 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
590 saved_errno = errno;
591 close(sock);
592 errno = saved_errno;
593 return -1;
596 return sock;
599 __dead void
600 ctl(int argc, char **argv)
602 int ctl_sock, i = 0;
604 log_init(1, LOG_DAEMON);
605 log_setverbose(verbose);
607 do {
608 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
610 if ((ctl_sock = sockconn()) != -1)
611 break;
612 if (errno != ENOENT && errno != ECONNREFUSED)
613 fatal("connect %s", csock);
615 if (i == 0)
616 spawn_daemon();
618 nanosleep(&ts, NULL);
619 } while (++i < 20);
621 if (ctl_sock == -1)
622 fatalx("failed to connect to the daemon");
624 ibuf = xmalloc(sizeof(*ibuf));
625 imsg_init(ibuf, ctl_sock);
627 optreset = 1;
628 optind = 1;
630 exit(parse(argc, argv));