2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
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.
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.
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/queue.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, "" },
71 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
76 ctl_usage(struct ctl_command *ctl)
78 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
79 ctl->name, ctl->usage);
84 parse(int argc, char **argv)
86 struct ctl_command *ctl = NULL;
87 struct parse_result res;
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]))
97 "ambiguous argument: %s\n", argv[0]);
100 ctl = &ctl_commands[i];
105 fprintf(stderr, "unknown argument: %s\n", argv[0]);
109 res.action = ctl->action;
112 if (!ctl->has_pledge) {
113 /* pledge(2) default if command doesn't have its own */
114 if (pledge("stdio", NULL) == -1)
118 status = ctl->main(&res, argc, argv);
125 enqueue_tracks(char **files)
130 for (; *files != NULL; ++files) {
131 memset(&res, 0, sizeof(res));
132 if (realpath(*files, res) == NULL) {
133 log_warn("realpath %s", *files);
137 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
146 jump_req(const char *arg)
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));
157 print_error_message(const char *prfx, struct imsg *imsg)
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);
174 show_add(struct imsg *imsg, int *ret, char ***files)
176 if (**files == NULL) {
177 log_warnx("received more replies than file sent");
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);
187 fatalx("got invalid message %d", imsg->hdr.type);
190 return (**files) == NULL;
194 show_complete(struct parse_result *res, struct imsg *imsg, int *ret)
196 struct player_status s;
199 if (imsg->hdr.type == IMSG_CTL_ERR) {
200 print_error_message("show failed", imsg);
205 datalen = IMSG_DATA_SIZE(*imsg);
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__);
216 printf("%c ", s.status == STATE_PLAYING ? '>' : ' ');
217 printf("%s\n", s.path);
222 show_status(struct imsg *imsg, int *ret)
224 struct player_status s;
227 if (imsg->hdr.type == IMSG_CTL_ERR) {
228 print_error_message("show failed", imsg);
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__);
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");
266 show_load(struct parse_result *res, struct imsg *imsg, int *ret)
272 size_t linesize = 0, i = 0;
273 ssize_t linelen, curr = -1;
275 if (imsg->hdr.type == IMSG_CTL_ERR) {
276 print_error_message("load failed", imsg);
281 if (imsg->hdr.type == IMSG_CTL_ADD)
284 if (imsg->hdr.type == IMSG_CTL_COMMIT)
287 if (imsg->hdr.type != IMSG_CTL_BEGIN)
288 fatalx("got unexpected message %d", imsg->hdr.type);
290 if (res->file == NULL)
292 else if ((f = fopen(res->file, "r")) == NULL) {
293 log_warn("can't open %s", res->file);
298 while ((linelen = getline(&line, &linesize, f)) != -1) {
301 line[linelen-1] = '\0';
303 if (file[0] == '>' && file[1] == ' ') {
307 if (file[0] == ' ' && file[1] == ' ')
310 memset(&path, 0, sizeof(path));
311 if (realpath(file, path) == NULL) {
312 log_warn("realpath %s", file);
317 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
331 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
332 &curr, sizeof(curr));
338 show_monitor(struct imsg *imsg, int *ret)
342 if (imsg->hdr.type != IMSG_CTL_MONITOR) {
343 log_warnx("wrong message type received: %d",
349 if (IMSG_DATA_SIZE(*imsg) != sizeof(type)) {
350 log_warnx("size mismatch");
355 memcpy(&type, imsg->data, sizeof(type));
360 case IMSG_CTL_TOGGLE_PLAY:
369 case IMSG_CTL_RESTART:
384 case IMSG_CTL_REPEAT:
390 case IMSG_CTL_COMMIT:
403 ctlaction(struct parse_result *res)
407 int ret = 0, done = 1;
410 switch (res->action) {
412 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
414 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
420 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
423 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
425 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
431 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
434 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
436 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
444 ret = enqueue_tracks(res->files);
447 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
451 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
455 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
458 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
460 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
466 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
468 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
475 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
479 ret = jump_req(res->file);
482 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
483 &res->rep, sizeof(res->rep));
487 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
491 /* action not expected */
492 fatalx("invalid action %u", res->action);
502 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
503 fatalx("imsg_read error");
505 fatalx("pipe closed");
508 if ((n = imsg_get(ibuf, &imsg)) == -1)
509 fatalx("imsg_get error");
513 switch (res->action) {
515 done = show_add(&imsg, &ret, &files);
518 done = show_complete(res, &imsg, &ret);
527 done = show_status(&imsg, &ret);
530 done = show_load(res, &imsg, &ret);
533 done = show_monitor(&imsg, &ret);
549 ctl_noarg(struct parse_result *res, int argc, char **argv)
553 return ctlaction(res);
557 ctl_add(struct parse_result *res, int argc, char **argv)
563 if (pledge("stdio rpath", NULL) == -1)
566 return ctlaction(res);
570 ctl_show(struct parse_result *res, int argc, char **argv)
574 while ((ch = getopt(argc, argv, "p")) != -1) {
584 return ctlaction(res);
588 ctl_load(struct parse_result *res, int argc, char **argv)
597 if (pledge("stdio rpath", NULL) == -1)
600 return ctlaction(res);
604 ctl_jump(struct parse_result *res, int argc, char **argv)
608 while ((ch = getopt(argc, argv, "")) != -1)
617 return ctlaction(res);
621 ctl_repeat(struct parse_result *res, int argc, char **argv)
625 while ((ch = getopt(argc, argv, "")) != -1)
633 if (!strcmp(argv[1], "on"))
635 else if (!strcmp(argv[1], "off"))
640 res->rep.repeat_one = -1;
641 res->rep.repeat_all = -1;
642 if (!strcmp(argv[0], "one"))
643 res->rep.repeat_one = b;
644 else if (!strcmp(argv[0], "all"))
645 res->rep.repeat_all = b;
649 return ctlaction(res);
655 struct sockaddr_un sun;
656 int sock, saved_errno;
658 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
661 memset(&sun, 0, sizeof(sun));
662 sun.sun_family = AF_UNIX;
663 strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
664 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
675 ctl(int argc, char **argv)
679 log_init(1, LOG_DAEMON);
680 log_setverbose(verbose);
683 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
685 if ((ctl_sock = sockconn()) != -1)
687 if (errno != ENOENT && errno != ECONNREFUSED)
688 fatal("connect %s", csock);
693 nanosleep(&ts, NULL);
697 fatalx("failed to connect to the daemon");
699 ibuf = xmalloc(sizeof(*ibuf));
700 imsg_init(ibuf, ctl_sock);
705 exit(parse(argc, argv));