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" },
70 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
75 ctl_usage(struct ctl_command *ctl)
77 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
78 ctl->name, ctl->usage);
83 parse(int argc, char **argv)
85 struct ctl_command *ctl = NULL;
86 struct parse_result res;
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]))
96 "ambiguous argument: %s\n", argv[0]);
99 ctl = &ctl_commands[i];
104 fprintf(stderr, "unknown argument: %s\n", argv[0]);
108 res.action = ctl->action;
111 if (!ctl->has_pledge) {
112 /* pledge(2) default if command doesn't have its own */
113 if (pledge("stdio", NULL) == -1)
117 status = ctl->main(&res, argc, argv);
124 enqueue_tracks(char **files)
129 for (; *files != NULL; ++files) {
130 memset(&res, 0, sizeof(res));
131 if (realpath(*files, res) == NULL) {
132 log_warn("realpath %s", *files);
136 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
145 jump_req(const char *arg)
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));
156 print_error_message(const char *prfx, struct imsg *imsg)
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);
173 show_add(struct imsg *imsg, int *ret, char ***files)
175 if (**files == NULL) {
176 log_warnx("received more replies than file sent");
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);
186 fatalx("got invalid message %d", imsg->hdr.type);
189 return (**files) == NULL;
193 show_complete(struct parse_result *res, struct imsg *imsg, int *ret)
195 struct player_status s;
198 if (imsg->hdr.type == IMSG_CTL_ERR) {
199 print_error_message("show failed", imsg);
204 datalen = IMSG_DATA_SIZE(*imsg);
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__);
215 printf("%c ", s.status == STATE_PLAYING ? '>' : ' ');
216 printf("%s\n", s.path);
221 show_status(struct imsg *imsg, int *ret)
223 struct player_status s;
226 if (imsg->hdr.type == IMSG_CTL_ERR) {
227 print_error_message("show failed", imsg);
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__);
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");
265 show_load(struct parse_result *res, struct imsg *imsg, int *ret)
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] == ' ')
305 if (file[0] == ' ' && file[1] == ' ')
308 memset(&path, 0, sizeof(path));
309 if (realpath(file, path) == NULL) {
310 log_warn("realpath %s", file);
315 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
329 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
336 ctlaction(struct parse_result *res)
340 int ret = 0, done = 1;
343 switch (res->action) {
345 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
347 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
353 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
356 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
358 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
364 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
367 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
369 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
377 ret = enqueue_tracks(res->files);
380 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
384 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
388 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
391 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
393 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
399 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
401 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
408 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
412 ret = jump_req(res->file);
415 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
416 &res->rep, sizeof(res->rep));
419 /* action not expected */
420 fatalx("invalid action %u", res->action);
430 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
431 fatalx("imsg_read error");
433 fatalx("pipe closed");
436 if ((n = imsg_get(ibuf, &imsg)) == -1)
437 fatalx("imsg_get error");
441 switch (res->action) {
443 done = show_add(&imsg, &ret, &files);
446 done = show_complete(res, &imsg, &ret);
455 done = show_status(&imsg, &ret);
458 done = show_load(res, &imsg, &ret);
474 ctl_noarg(struct parse_result *res, int argc, char **argv)
478 return ctlaction(res);
482 ctl_add(struct parse_result *res, int argc, char **argv)
488 if (pledge("stdio rpath", NULL) == -1)
491 return ctlaction(res);
495 ctl_show(struct parse_result *res, int argc, char **argv)
499 while ((ch = getopt(argc, argv, "p")) != -1) {
509 return ctlaction(res);
513 ctl_load(struct parse_result *res, int argc, char **argv)
522 if (pledge("stdio rpath", NULL) == -1)
525 return ctlaction(res);
529 ctl_jump(struct parse_result *res, int argc, char **argv)
533 while ((ch = getopt(argc, argv, "")) != -1)
542 return ctlaction(res);
546 ctl_repeat(struct parse_result *res, int argc, char **argv)
550 while ((ch = getopt(argc, argv, "")) != -1)
558 if (!strcmp(argv[1], "on"))
560 else if (!strcmp(argv[1], "off"))
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;
574 return ctlaction(res);
580 struct sockaddr_un sun;
581 int sock, saved_errno;
583 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
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) {
600 ctl(int argc, char **argv)
604 log_init(1, LOG_DAEMON);
605 log_setverbose(verbose);
608 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
610 if ((ctl_sock = sockconn()) != -1)
612 if (errno != ENOENT && errno != ECONNREFUSED)
613 fatal("connect %s", csock);
618 nanosleep(&ts, NULL);
622 fatalx("failed to connect to the daemon");
624 ibuf = xmalloc(sizeof(*ibuf));
625 imsg_init(ibuf, ctl_sock);
630 exit(parse(argc, argv));