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 **);
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" },
68 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
69 fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
74 ctl_usage(struct ctl_command *ctl)
76 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
77 ctl->name, ctl->usage);
82 parse(int argc, char **argv)
84 struct ctl_command *ctl = NULL;
85 struct parse_result res;
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]))
95 "ambiguous argument: %s\n", argv[0]);
98 ctl = &ctl_commands[i];
103 fprintf(stderr, "unknown argument: %s\n", argv[0]);
107 res.action = ctl->action;
110 if (!ctl->has_pledge) {
111 /* pledge(2) default if command doesn't have its own */
112 if (pledge("stdio", NULL) == -1)
116 status = ctl->main(&res, argc, argv);
123 enqueue_tracks(char **files)
128 for (; *files != NULL; ++files) {
129 memset(&res, 0, sizeof(res));
130 if (realpath(*files, res) == NULL) {
131 log_warn("realpath %s", *files);
135 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
144 jump_req(const char *arg)
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));
155 print_error_message(const char *prfx, struct imsg *imsg)
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);
172 show_add(struct imsg *imsg, int *ret, char ***files)
174 if (**files == NULL) {
175 log_warnx("received more replies than file sent");
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);
185 fatalx("got invalid message %d", imsg->hdr.type);
188 return (**files) == NULL;
192 show_complete(struct parse_result *res, struct imsg *imsg, int *ret)
194 struct player_status s;
197 if (imsg->hdr.type == IMSG_CTL_ERR) {
198 print_error_message("show failed", imsg);
203 datalen = IMSG_DATA_SIZE(*imsg);
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__);
214 printf("%c ", s.status == STATE_PLAYING ? '>' : ' ');
215 printf("%s\n", s.path);
220 show_status(struct imsg *imsg, int *ret)
222 struct player_status s;
225 if (imsg->hdr.type == IMSG_CTL_ERR) {
226 print_error_message("show failed", imsg);
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__);
256 printf("%s\n", s.path);
261 show_load(struct parse_result *res, struct imsg *imsg, int *ret)
271 if (imsg->hdr.type == IMSG_CTL_ERR) {
272 print_error_message("load failed", imsg);
277 if (imsg->hdr.type == IMSG_CTL_ADD)
280 if (imsg->hdr.type == IMSG_CTL_COMMIT)
283 if (imsg->hdr.type != IMSG_CTL_BEGIN)
284 fatalx("got unexpected message %d", imsg->hdr.type);
286 if (res->file == NULL)
288 else if ((f = fopen(res->file, "r")) == NULL) {
289 log_warn("can't open %s", res->file);
294 while ((linelen = getline(&line, &linesize, f)) != -1) {
297 line[linelen-1] = '\0';
299 if (file[0] == '>' && file[1] == ' ')
301 if (file[0] == ' ' && file[1] == ' ')
304 memset(&path, 0, sizeof(path));
305 if (realpath(file, path) == NULL) {
306 log_warn("realpath %s", file);
311 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
325 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
332 ctlaction(struct parse_result *res)
336 int ret = 0, done = 1;
339 switch (res->action) {
342 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
343 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
346 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 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);
354 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 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);
364 ret = enqueue_tracks(res->files);
367 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
371 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
375 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 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);
384 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
385 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
389 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
393 ret = jump_req(res->file);
396 /* action not expected */
397 fatalx("invalid action %u", res->action);
407 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
408 fatalx("imsg_read error");
410 fatalx("pipe closed");
413 if ((n = imsg_get(ibuf, &imsg)) == -1)
414 fatalx("imsg_get error");
418 switch (res->action) {
420 done = show_add(&imsg, &ret, &files);
423 done = show_complete(res, &imsg, &ret);
432 done = show_status(&imsg, &ret);
435 done = show_load(res, &imsg, &ret);
451 ctl_noarg(struct parse_result *res, int argc, char **argv)
455 return ctlaction(res);
459 ctl_add(struct parse_result *res, int argc, char **argv)
465 if (pledge("stdio rpath", NULL) == -1)
468 return ctlaction(res);
472 ctl_show(struct parse_result *res, int argc, char **argv)
476 while ((ch = getopt(argc, argv, "p")) != -1) {
486 return ctlaction(res);
490 ctl_load(struct parse_result *res, int argc, char **argv)
499 if (pledge("stdio rpath", NULL) == -1)
502 return ctlaction(res);
506 ctl_jump(struct parse_result *res, int argc, char **argv)
510 while ((ch = getopt(argc, argv, "")) != -1)
519 return ctlaction(res);
525 struct sockaddr_un sun;
526 int sock, saved_errno;
528 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
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) {
545 ctl(int argc, char **argv)
549 log_init(1, LOG_DAEMON);
550 log_setverbose(verbose);
553 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
555 if ((ctl_sock = sockconn()) != -1)
557 if (errno != ENOENT && errno != ECONNREFUSED)
558 fatal("connect %s", csock);
563 nanosleep(&ts, NULL);
567 fatalx("failed to connect to the daemon");
569 ibuf = xmalloc(sizeof(*ibuf));
570 imsg_init(ibuf, ctl_sock);
575 exit(parse(argc, argv));