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 **);
48 struct ctl_command ctl_commands[] = {
49 { "play", PLAY, ctl_noarg, "" },
50 { "pause", PAUSE, ctl_noarg, "" },
51 { "toggle", TOGGLE, ctl_noarg, "" },
52 { "stop", STOP, ctl_noarg, "" },
53 { "restart", RESTART, ctl_noarg, "" },
54 { "add", ADD, ctl_add, "files...", 1 },
55 { "flush", FLUSH, ctl_noarg, "" },
56 { "show", SHOW, ctl_show, "" },
57 { "status", STATUS, ctl_noarg, "" },
58 { "next", NEXT, ctl_noarg, "" },
59 { "prev", PREV, ctl_noarg, "" },
60 { "load", LOAD, ctl_load, "[file]", 1 },
61 { "jump", JUMP, ctl_jump, "pattern" },
62 { NULL },
63 };
65 __dead void
66 usage(void)
67 {
68 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
69 fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
70 exit(1);
71 }
73 static __dead void
74 ctl_usage(struct ctl_command *ctl)
75 {
76 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
77 ctl->name, ctl->usage);
78 exit(1);
79 }
81 static int
82 parse(int argc, char **argv)
83 {
84 struct ctl_command *ctl = NULL;
85 struct parse_result res;
86 int i, status;
88 memset(&res, 0, sizeof(res));
90 for (i = 0; ctl_commands[i].name != NULL; ++i) {
91 if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
92 == 0) {
93 if (ctl != NULL) {
94 fprintf(stderr,
95 "ambiguous argument: %s\n", argv[0]);
96 usage();
97 }
98 ctl = &ctl_commands[i];
99 }
102 if (ctl == NULL) {
103 fprintf(stderr, "unknown argument: %s\n", argv[0]);
104 usage();
107 res.action = ctl->action;
108 res.ctl = ctl;
110 if (!ctl->has_pledge) {
111 /* pledge(2) default if command doesn't have its own */
112 if (pledge("stdio", NULL) == -1)
113 fatal("pledge");
116 status = ctl->main(&res, argc, argv);
117 close(ibuf->fd);
118 free(ibuf);
119 return status;
122 static int
123 enqueue_tracks(char **files)
125 char res[PATH_MAX];
126 int enq = 0;
128 for (; *files != NULL; ++files) {
129 memset(&res, 0, sizeof(res));
130 if (realpath(*files, res) == NULL) {
131 log_warn("realpath %s", *files);
132 continue;
135 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
136 res, sizeof(res));
137 enq++;
140 return enq == 0;
143 static int
144 jump_req(const char *arg)
146 char path[PATH_MAX];
148 memset(path, 0, sizeof(path));
149 strlcpy(path, arg, sizeof(path));
150 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1, path, sizeof(path));
151 return 0;
154 static void
155 print_error_message(const char *prfx, struct imsg *imsg)
157 size_t datalen;
158 char *msg;
160 datalen = IMSG_DATA_SIZE(*imsg);
161 if ((msg = calloc(1, datalen)) == NULL)
162 fatal("calloc %zu", datalen);
163 memcpy(msg, imsg->data, datalen);
164 if (datalen == 0 || msg[datalen-1] != '\0')
165 fatalx("malformed error message");
167 log_warnx("%s: %s", prfx, msg);
168 free(msg);
171 static int
172 show_add(struct imsg *imsg, int *ret, char ***files)
174 if (**files == NULL) {
175 log_warnx("received more replies than file sent");
176 *ret = 1;
177 return 1;
180 if (imsg->hdr.type == IMSG_CTL_ERR)
181 print_error_message(**files, imsg);
182 else if (imsg->hdr.type == IMSG_CTL_ADD)
183 log_debug("enqueued %s", **files);
184 else
185 fatalx("got invalid message %d", imsg->hdr.type);
187 (*files)++;
188 return (**files) == NULL;
191 static int
192 show_complete(struct parse_result *res, struct imsg *imsg, int *ret)
194 struct player_status s;
195 size_t datalen;
197 if (imsg->hdr.type == IMSG_CTL_ERR) {
198 print_error_message("show failed", imsg);
199 *ret = 1;
200 return 1;
203 datalen = IMSG_DATA_SIZE(*imsg);
204 if (datalen == 0)
205 return 1;
207 if (datalen != sizeof(s))
208 fatalx("%s: data size mismatch", __func__);
209 memcpy(&s, imsg->data, sizeof(s));
210 if (s.path[sizeof(s.path)-1] != '\0')
211 fatalx("%s: data corrupted?", __func__);
213 if (res->pretty)
214 printf("%c ", s.status == STATE_PLAYING ? '>' : ' ');
215 printf("%s\n", s.path);
216 return 0;
219 static int
220 show_status(struct imsg *imsg, int *ret)
222 struct player_status s;
223 size_t datalen;
225 if (imsg->hdr.type == IMSG_CTL_ERR) {
226 print_error_message("show failed", imsg);
227 *ret = 1;
228 return 1;
231 if (imsg->hdr.type != IMSG_CTL_STATUS)
232 fatalx("%s: got wrong reply", __func__);
234 datalen = IMSG_DATA_SIZE(*imsg);
235 if (datalen != sizeof(s))
236 fatalx("%s: data size mismatch", __func__);
237 memcpy(&s, imsg->data, sizeof(s));
238 if (s.path[sizeof(s.path)-1] != '\0')
239 fatalx("%s: data corrupted?", __func__);
241 switch (s.status) {
242 case STATE_STOPPED:
243 printf("stopped ");
244 break;
245 case STATE_PLAYING:
246 printf("playing ");
247 break;
248 case STATE_PAUSED:
249 printf("paused ");
250 break;
251 default:
252 printf("unknown ");
253 break;
256 printf("%s\n", s.path);
257 return 1;
260 static int
261 show_load(struct parse_result *res, struct imsg *imsg, int *ret)
263 FILE *f;
264 const char *file;
265 char *line = NULL;
266 char path[PATH_MAX];
267 size_t linesize = 0;
268 ssize_t linelen;
269 int any = 0;
271 if (imsg->hdr.type == IMSG_CTL_ERR) {
272 print_error_message("load failed", imsg);
273 *ret = 1;
274 return 1;
277 if (imsg->hdr.type == IMSG_CTL_ADD)
278 return 0;
280 if (imsg->hdr.type == IMSG_CTL_COMMIT)
281 return 1;
283 if (imsg->hdr.type != IMSG_CTL_BEGIN)
284 fatalx("got unexpected message %d", imsg->hdr.type);
286 if (res->file == NULL)
287 f = stdin;
288 else if ((f = fopen(res->file, "r")) == NULL) {
289 log_warn("can't open %s", res->file);
290 *ret = 1;
291 return 1;
294 while ((linelen = getline(&line, &linesize, f)) != -1) {
295 if (linelen == 0)
296 continue;
297 line[linelen-1] = '\0';
298 file = line;
299 if (file[0] == '>' && file[1] == ' ')
300 file += 2;
301 if (file[0] == ' ' && file[1] == ' ')
302 file += 2;
304 memset(&path, 0, sizeof(path));
305 if (realpath(file, path) == NULL) {
306 log_warn("realpath %s", file);
307 continue;
310 any++;
311 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
312 path, sizeof(path));
315 free(line);
316 if (ferror(f))
317 fatal("getline");
318 fclose(f);
320 if (!any) {
321 *ret = 1;
322 return 1;
325 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
326 NULL, 0);
327 imsg_flush(ibuf);
328 return 0;
331 static int
332 ctlaction(struct parse_result *res)
334 struct imsg imsg;
335 ssize_t n;
336 int ret = 0, done = 1;
337 char **files;
339 switch (res->action) {
340 case PLAY:
341 done = 0;
342 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
343 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
344 break;
345 case PAUSE:
346 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
347 break;
348 case TOGGLE:
349 done = 0;
350 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
351 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
352 break;
353 case STOP:
354 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
355 break;
356 case RESTART:
357 done = 0;
358 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
359 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
360 break;
361 case ADD:
362 done = 0;
363 files = res->files;
364 ret = enqueue_tracks(res->files);
365 break;
366 case FLUSH:
367 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
368 break;
369 case SHOW:
370 done = 0;
371 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
372 break;
373 case STATUS:
374 done = 0;
375 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
376 break;
377 case NEXT:
378 done = 0;
379 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
380 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
381 break;
382 case PREV:
383 done = 0;
384 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
385 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
386 break;
387 case LOAD:
388 done = 0;
389 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
390 break;
391 case JUMP:
392 done = 0;
393 ret = jump_req(res->file);
394 break;
395 case NONE:
396 /* action not expected */
397 fatalx("invalid action %u", res->action);
398 break;
401 if (ret != 0)
402 goto end;
404 imsg_flush(ibuf);
406 while (!done) {
407 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
408 fatalx("imsg_read error");
409 if (n == 0)
410 fatalx("pipe closed");
412 while (!done) {
413 if ((n = imsg_get(ibuf, &imsg)) == -1)
414 fatalx("imsg_get error");
415 if (n == 0)
416 break;
418 switch (res->action) {
419 case ADD:
420 done = show_add(&imsg, &ret, &files);
421 break;
422 case SHOW:
423 done = show_complete(res, &imsg, &ret);
424 break;
425 case PLAY:
426 case TOGGLE:
427 case RESTART:
428 case STATUS:
429 case NEXT:
430 case PREV:
431 case JUMP:
432 done = show_status(&imsg, &ret);
433 break;
434 case LOAD:
435 done = show_load(res, &imsg, &ret);
436 break;
437 default:
438 done = 1;
439 break;
442 imsg_free(&imsg);
446 end:
447 return ret;
450 int
451 ctl_noarg(struct parse_result *res, int argc, char **argv)
453 if (argc != 1)
454 ctl_usage(res->ctl);
455 return ctlaction(res);
458 int
459 ctl_add(struct parse_result *res, int argc, char **argv)
461 if (argc < 2)
462 ctl_usage(res->ctl);
463 res->files = argv+1;
465 if (pledge("stdio rpath", NULL) == -1)
466 fatal("pledge");
468 return ctlaction(res);
471 int
472 ctl_show(struct parse_result *res, int argc, char **argv)
474 int ch;
476 while ((ch = getopt(argc, argv, "p")) != -1) {
477 switch (ch) {
478 case 'p':
479 res->pretty = 1;
480 break;
481 default:
482 ctl_usage(res->ctl);
486 return ctlaction(res);
489 int
490 ctl_load(struct parse_result *res, int argc, char **argv)
492 if (argc < 2)
493 res->file = NULL;
494 else if (argc == 2)
495 res->file = argv[1];
496 else
497 ctl_usage(res->ctl);
499 if (pledge("stdio rpath", NULL) == -1)
500 fatal("pledge");
502 return ctlaction(res);
505 int
506 ctl_jump(struct parse_result *res, int argc, char **argv)
508 int ch;
510 while ((ch = getopt(argc, argv, "")) != -1)
511 ctl_usage(res->ctl);
512 argc -= optind;
513 argv += optind;
515 if (argc != 1)
516 ctl_usage(res->ctl);
518 res->file = argv[0];
519 return ctlaction(res);
522 static int
523 sockconn(void)
525 struct sockaddr_un sun;
526 int sock, saved_errno;
528 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
529 fatal("socket");
531 memset(&sun, 0, sizeof(sun));
532 sun.sun_family = AF_UNIX;
533 strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
534 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
535 saved_errno = errno;
536 close(sock);
537 errno = saved_errno;
538 return -1;
541 return sock;
544 __dead void
545 ctl(int argc, char **argv)
547 int ctl_sock, i = 0;
549 log_init(1, LOG_DAEMON);
550 log_setverbose(verbose);
552 do {
553 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
555 if ((ctl_sock = sockconn()) != -1)
556 break;
557 if (errno != ENOENT && errno != ECONNREFUSED)
558 fatal("connect %s", csock);
560 if (i == 0)
561 spawn_daemon();
563 nanosleep(&ts, NULL);
564 } while (++i < 20);
566 if (ctl_sock == -1)
567 fatalx("failed to connect to the daemon");
569 ibuf = xmalloc(sizeof(*ibuf));
570 imsg_init(ibuf, ctl_sock);
572 optreset = 1;
573 optind = 1;
575 exit(parse(argc, argv));