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 **);
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 },
66 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
67 fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
72 ctl_usage(struct ctl_command *ctl)
74 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
75 ctl->name, ctl->usage);
80 parse(int argc, char **argv)
82 struct ctl_command *ctl = NULL;
83 struct parse_result res;
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]))
93 "ambiguous argument: %s\n", argv[0]);
96 ctl = &ctl_commands[i];
101 fprintf(stderr, "unknown argument: %s\n", argv[0]);
105 res.action = ctl->action;
108 if (!ctl->has_pledge) {
109 /* pledge(2) default if command doesn't have its own */
110 if (pledge("stdio", NULL) == -1)
114 status = ctl->main(&res, argc, argv);
121 enqueue_tracks(char **files)
126 for (; *files != NULL; ++files) {
127 memset(&res, 0, sizeof(res));
128 if (realpath(*files, res) == NULL) {
129 log_warn("realpath %s", *files);
133 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
142 print_error_message(const char *prfx, struct imsg *imsg)
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);
159 show_add(struct imsg *imsg, int *ret, char ***files)
161 if (**files == NULL) {
162 log_warnx("received more replies than file sent");
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);
172 fatalx("got invalid message %d", imsg->hdr.type);
175 return (**files) == NULL;
179 show_complete(struct parse_result *res, struct imsg *imsg, int *ret)
181 struct player_status s;
184 if (imsg->hdr.type == IMSG_CTL_ERR) {
185 print_error_message("show failed", imsg);
190 datalen = IMSG_DATA_SIZE(*imsg);
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__);
201 printf("%c ", s.status == STATE_PLAYING ? '>' : ' ');
202 printf("%s\n", s.path);
207 show_status(struct imsg *imsg, int *ret)
209 struct player_status s;
212 if (imsg->hdr.type == IMSG_CTL_ERR) {
213 print_error_message("show failed", imsg);
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__);
243 printf("%s\n", s.path);
248 show_load(struct parse_result *res, struct imsg *imsg, int *ret)
257 if (imsg->hdr.type == IMSG_CTL_ERR) {
258 print_error_message("load failed", imsg);
263 if (imsg->hdr.type == IMSG_CTL_ADD)
266 if (imsg->hdr.type == IMSG_CTL_COMMIT)
269 if (imsg->hdr.type != IMSG_CTL_BEGIN)
270 fatalx("got unexpected message %d", imsg->hdr.type);
272 while ((linelen = getline(&line, &linesize, res->file)) != -1) {
275 line[linelen-1] = '\0';
277 if (file[0] == '>' && file[1] == ' ')
279 if (file[0] == ' ' && file[1] == ' ')
282 memset(&path, 0, sizeof(path));
283 if (realpath(file, path) == NULL) {
284 log_warn("realpath %s", file);
289 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
294 if (ferror(res->file))
304 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
311 ctlaction(struct parse_result *res)
315 int ret = 0, done = 1;
318 switch (res->action) {
321 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
322 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
325 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
329 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
330 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
333 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
337 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
338 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
343 ret = enqueue_tracks(res->files);
346 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
350 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
354 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
358 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
359 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
363 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
364 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
368 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
371 /* action not expected */
372 fatalx("invalid action %u", res->action);
382 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
383 fatalx("imsg_read error");
385 fatalx("pipe closed");
388 if ((n = imsg_get(ibuf, &imsg)) == -1)
389 fatalx("imsg_get error");
393 switch (res->action) {
395 done = show_add(&imsg, &ret, &files);
398 done = show_complete(res, &imsg, &ret);
406 done = show_status(&imsg, &ret);
409 done = show_load(res, &imsg, &ret);
425 ctl_noarg(struct parse_result *res, int argc, char **argv)
429 return ctlaction(res);
433 ctl_add(struct parse_result *res, int argc, char **argv)
439 if (pledge("stdio rpath", NULL) == -1)
442 return ctlaction(res);
446 ctl_show(struct parse_result *res, int argc, char **argv)
450 while ((ch = getopt(argc, argv, "p")) != -1) {
460 return ctlaction(res);
464 ctl_load(struct parse_result *res, int argc, char **argv)
468 else if (argc == 2) {
469 if ((res->file = fopen(argv[1], "r")) == NULL)
470 fatal("open %s", argv[1]);
474 if (pledge("stdio rpath", NULL) == -1)
477 return ctlaction(res);
483 struct sockaddr_un sun;
484 int sock, saved_errno;
486 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
489 memset(&sun, 0, sizeof(sun));
490 sun.sun_family = AF_UNIX;
491 strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
492 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
503 ctl(int argc, char **argv)
507 log_init(1, LOG_DAEMON);
508 log_setverbose(verbose);
511 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
513 if ((ctl_sock = sockconn()) != -1)
515 if (errno != ENOENT && errno != ECONNREFUSED)
516 fatal("connect %s", csock);
521 nanosleep(&ts, NULL);
525 fatalx("failed to connect to the daemon");
527 ibuf = xmalloc(sizeof(*ibuf));
528 imsg_init(ibuf, ctl_sock);
533 exit(parse(argc, argv));