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 fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
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 return 1;
262 static int
263 show_load(struct parse_result *res, struct imsg *imsg, int *ret)
265 FILE *f;
266 const char *file;
267 char *line = NULL;
268 char path[PATH_MAX];
269 size_t linesize = 0;
270 ssize_t linelen;
271 int any = 0;
273 if (imsg->hdr.type == IMSG_CTL_ERR) {
274 print_error_message("load failed", imsg);
275 *ret = 1;
276 return 1;
279 if (imsg->hdr.type == IMSG_CTL_ADD)
280 return 0;
282 if (imsg->hdr.type == IMSG_CTL_COMMIT)
283 return 1;
285 if (imsg->hdr.type != IMSG_CTL_BEGIN)
286 fatalx("got unexpected message %d", imsg->hdr.type);
288 if (res->file == NULL)
289 f = stdin;
290 else if ((f = fopen(res->file, "r")) == NULL) {
291 log_warn("can't open %s", res->file);
292 *ret = 1;
293 return 1;
296 while ((linelen = getline(&line, &linesize, f)) != -1) {
297 if (linelen == 0)
298 continue;
299 line[linelen-1] = '\0';
300 file = line;
301 if (file[0] == '>' && file[1] == ' ')
302 file += 2;
303 if (file[0] == ' ' && file[1] == ' ')
304 file += 2;
306 memset(&path, 0, sizeof(path));
307 if (realpath(file, path) == NULL) {
308 log_warn("realpath %s", file);
309 continue;
312 any++;
313 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
314 path, sizeof(path));
317 free(line);
318 if (ferror(f))
319 fatal("getline");
320 fclose(f);
322 if (!any) {
323 *ret = 1;
324 return 1;
327 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
328 NULL, 0);
329 imsg_flush(ibuf);
330 return 0;
333 static int
334 ctlaction(struct parse_result *res)
336 struct imsg imsg;
337 ssize_t n;
338 int ret = 0, done = 1;
339 char **files;
341 switch (res->action) {
342 case PLAY:
343 done = 0;
344 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
345 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
346 break;
347 case PAUSE:
348 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
349 break;
350 case TOGGLE:
351 done = 0;
352 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
353 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
354 break;
355 case STOP:
356 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
357 break;
358 case RESTART:
359 done = 0;
360 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
361 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
362 break;
363 case ADD:
364 done = 0;
365 files = res->files;
366 ret = enqueue_tracks(res->files);
367 break;
368 case FLUSH:
369 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
370 break;
371 case SHOW:
372 done = 0;
373 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
374 break;
375 case STATUS:
376 done = 0;
377 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
378 break;
379 case NEXT:
380 done = 0;
381 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
382 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
383 break;
384 case PREV:
385 done = 0;
386 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
387 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
388 break;
389 case LOAD:
390 done = 0;
391 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
392 break;
393 case JUMP:
394 done = 0;
395 ret = jump_req(res->file);
396 break;
397 case REPEAT:
398 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
399 &res->rep, sizeof(res->rep));
400 break;
401 case NONE:
402 /* action not expected */
403 fatalx("invalid action %u", res->action);
404 break;
407 if (ret != 0)
408 goto end;
410 imsg_flush(ibuf);
412 while (!done) {
413 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
414 fatalx("imsg_read error");
415 if (n == 0)
416 fatalx("pipe closed");
418 while (!done) {
419 if ((n = imsg_get(ibuf, &imsg)) == -1)
420 fatalx("imsg_get error");
421 if (n == 0)
422 break;
424 switch (res->action) {
425 case ADD:
426 done = show_add(&imsg, &ret, &files);
427 break;
428 case SHOW:
429 done = show_complete(res, &imsg, &ret);
430 break;
431 case PLAY:
432 case TOGGLE:
433 case RESTART:
434 case STATUS:
435 case NEXT:
436 case PREV:
437 case JUMP:
438 done = show_status(&imsg, &ret);
439 break;
440 case LOAD:
441 done = show_load(res, &imsg, &ret);
442 break;
443 default:
444 done = 1;
445 break;
448 imsg_free(&imsg);
452 end:
453 return ret;
456 int
457 ctl_noarg(struct parse_result *res, int argc, char **argv)
459 if (argc != 1)
460 ctl_usage(res->ctl);
461 return ctlaction(res);
464 int
465 ctl_add(struct parse_result *res, int argc, char **argv)
467 if (argc < 2)
468 ctl_usage(res->ctl);
469 res->files = argv+1;
471 if (pledge("stdio rpath", NULL) == -1)
472 fatal("pledge");
474 return ctlaction(res);
477 int
478 ctl_show(struct parse_result *res, int argc, char **argv)
480 int ch;
482 while ((ch = getopt(argc, argv, "p")) != -1) {
483 switch (ch) {
484 case 'p':
485 res->pretty = 1;
486 break;
487 default:
488 ctl_usage(res->ctl);
492 return ctlaction(res);
495 int
496 ctl_load(struct parse_result *res, int argc, char **argv)
498 if (argc < 2)
499 res->file = NULL;
500 else if (argc == 2)
501 res->file = argv[1];
502 else
503 ctl_usage(res->ctl);
505 if (pledge("stdio rpath", NULL) == -1)
506 fatal("pledge");
508 return ctlaction(res);
511 int
512 ctl_jump(struct parse_result *res, int argc, char **argv)
514 int ch;
516 while ((ch = getopt(argc, argv, "")) != -1)
517 ctl_usage(res->ctl);
518 argc -= optind;
519 argv += optind;
521 if (argc != 1)
522 ctl_usage(res->ctl);
524 res->file = argv[0];
525 return ctlaction(res);
528 int
529 ctl_repeat(struct parse_result *res, int argc, char **argv)
531 int ch, b;
533 while ((ch = getopt(argc, argv, "")) != -1)
534 ctl_usage(res->ctl);
535 argc -= optind;
536 argv += optind;
538 if (argc != 2)
539 ctl_usage(res->ctl);
541 if (!strcmp(argv[1], "on"))
542 b = 1;
543 else if (!strcmp(argv[1], "off"))
544 b = 0;
545 else
546 ctl_usage(res->ctl);
548 res->rep.repeat_one = -1;
549 res->rep.repeat_all = -1;
550 if (!strcmp(argv[0], "one"))
551 res->rep.repeat_one = b;
552 else if (!strcmp(argv[0], "all"))
553 res->rep.repeat_all = b;
554 else
555 ctl_usage(res->ctl);
557 return ctlaction(res);
560 static int
561 sockconn(void)
563 struct sockaddr_un sun;
564 int sock, saved_errno;
566 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
567 fatal("socket");
569 memset(&sun, 0, sizeof(sun));
570 sun.sun_family = AF_UNIX;
571 strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
572 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
573 saved_errno = errno;
574 close(sock);
575 errno = saved_errno;
576 return -1;
579 return sock;
582 __dead void
583 ctl(int argc, char **argv)
585 int ctl_sock, i = 0;
587 log_init(1, LOG_DAEMON);
588 log_setverbose(verbose);
590 do {
591 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
593 if ((ctl_sock = sockconn()) != -1)
594 break;
595 if (errno != ENOENT && errno != ECONNREFUSED)
596 fatal("connect %s", csock);
598 if (i == 0)
599 spawn_daemon();
601 nanosleep(&ts, NULL);
602 } while (++i < 20);
604 if (ctl_sock == -1)
605 fatalx("failed to connect to the daemon");
607 ibuf = xmalloc(sizeof(*ibuf));
608 imsg_init(ibuf, ctl_sock);
610 optreset = 1;
611 optind = 1;
613 exit(parse(argc, argv));