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 FILE *f;
251 const char *file;
252 char *line = NULL;
253 char path[PATH_MAX];
254 size_t linesize = 0;
255 ssize_t linelen;
256 int any = 0;
258 if (imsg->hdr.type == IMSG_CTL_ERR) {
259 print_error_message("load failed", imsg);
260 *ret = 1;
261 return 1;
264 if (imsg->hdr.type == IMSG_CTL_ADD)
265 return 0;
267 if (imsg->hdr.type == IMSG_CTL_COMMIT)
268 return 1;
270 if (imsg->hdr.type != IMSG_CTL_BEGIN)
271 fatalx("got unexpected message %d", imsg->hdr.type);
273 if (res->file == NULL)
274 f = stdin;
275 else if ((f = fopen(res->file, "r")) == NULL) {
276 log_warn("can't open %s", res->file);
277 *ret = 1;
278 return 1;
281 while ((linelen = getline(&line, &linesize, f)) != -1) {
282 if (linelen == 0)
283 continue;
284 line[linelen-1] = '\0';
285 file = line;
286 if (file[0] == '>' && file[1] == ' ')
287 file += 2;
288 if (file[0] == ' ' && file[1] == ' ')
289 file += 2;
291 memset(&path, 0, sizeof(path));
292 if (realpath(file, path) == NULL) {
293 log_warn("realpath %s", file);
294 continue;
297 any++;
298 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
299 path, sizeof(path));
302 free(line);
303 if (ferror(f))
304 fatal("getline");
305 fclose(f);
307 if (!any) {
308 *ret = 1;
309 return 1;
312 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
313 NULL, 0);
314 imsg_flush(ibuf);
315 return 0;
318 static int
319 ctlaction(struct parse_result *res)
321 struct imsg imsg;
322 ssize_t n;
323 int ret = 0, done = 1;
324 char **files;
326 switch (res->action) {
327 case PLAY:
328 done = 0;
329 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
330 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
331 break;
332 case PAUSE:
333 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
334 break;
335 case TOGGLE:
336 done = 0;
337 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
338 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
339 break;
340 case STOP:
341 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
342 break;
343 case RESTART:
344 done = 0;
345 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
346 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
347 break;
348 case ADD:
349 done = 0;
350 files = res->files;
351 ret = enqueue_tracks(res->files);
352 break;
353 case FLUSH:
354 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
355 break;
356 case SHOW:
357 done = 0;
358 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
359 break;
360 case STATUS:
361 done = 0;
362 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
363 break;
364 case NEXT:
365 done = 0;
366 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
367 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
368 break;
369 case PREV:
370 done = 0;
371 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
372 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
373 break;
374 case LOAD:
375 done = 0;
376 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
377 break;
378 case NONE:
379 /* action not expected */
380 fatalx("invalid action %u", res->action);
381 break;
384 if (ret != 0)
385 goto end;
387 imsg_flush(ibuf);
389 while (!done) {
390 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
391 fatalx("imsg_read error");
392 if (n == 0)
393 fatalx("pipe closed");
395 while (!done) {
396 if ((n = imsg_get(ibuf, &imsg)) == -1)
397 fatalx("imsg_get error");
398 if (n == 0)
399 break;
401 switch (res->action) {
402 case ADD:
403 done = show_add(&imsg, &ret, &files);
404 break;
405 case SHOW:
406 done = show_complete(res, &imsg, &ret);
407 break;
408 case PLAY:
409 case TOGGLE:
410 case RESTART:
411 case STATUS:
412 case NEXT:
413 case PREV:
414 done = show_status(&imsg, &ret);
415 break;
416 case LOAD:
417 done = show_load(res, &imsg, &ret);
418 break;
419 default:
420 done = 1;
421 break;
424 imsg_free(&imsg);
428 end:
429 return ret;
432 int
433 ctl_noarg(struct parse_result *res, int argc, char **argv)
435 if (argc != 1)
436 ctl_usage(res->ctl);
437 return ctlaction(res);
440 int
441 ctl_add(struct parse_result *res, int argc, char **argv)
443 if (argc < 2)
444 ctl_usage(res->ctl);
445 res->files = argv+1;
447 if (pledge("stdio rpath", NULL) == -1)
448 fatal("pledge");
450 return ctlaction(res);
453 int
454 ctl_show(struct parse_result *res, int argc, char **argv)
456 int ch;
458 while ((ch = getopt(argc, argv, "p")) != -1) {
459 switch (ch) {
460 case 'p':
461 res->pretty = 1;
462 break;
463 default:
464 ctl_usage(res->ctl);
468 return ctlaction(res);
471 int
472 ctl_load(struct parse_result *res, int argc, char **argv)
474 if (argc < 2)
475 res->file = NULL;
476 else if (argc == 2)
477 res->file = argv[1];
478 else
479 ctl_usage(res->ctl);
481 if (pledge("stdio rpath", NULL) == -1)
482 fatal("pledge");
484 return ctlaction(res);
487 static int
488 sockconn(void)
490 struct sockaddr_un sun;
491 int sock, saved_errno;
493 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
494 fatal("socket");
496 memset(&sun, 0, sizeof(sun));
497 sun.sun_family = AF_UNIX;
498 strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
499 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
500 saved_errno = errno;
501 close(sock);
502 errno = saved_errno;
503 return -1;
506 return sock;
509 __dead void
510 ctl(int argc, char **argv)
512 int ctl_sock, i = 0;
514 log_init(1, LOG_DAEMON);
515 log_setverbose(verbose);
517 do {
518 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
520 if ((ctl_sock = sockconn()) != -1)
521 break;
522 if (errno != ENOENT && errno != ECONNREFUSED)
523 fatal("connect %s", csock);
525 if (i == 0)
526 spawn_daemon();
528 nanosleep(&ts, NULL);
529 } while (++i < 20);
531 if (ctl_sock == -1)
532 fatalx("failed to connect to the daemon");
534 ibuf = xmalloc(sizeof(*ibuf));
535 imsg_init(ibuf, ctl_sock);
537 optreset = 1;
538 optind = 1;
540 exit(parse(argc, argv));