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_load(struct parse_result *, int, char **);
46 struct ctl_command ctl_commands[] = {
47 { "play", PLAY, ctl_noarg, "" },
48 { "pause", PAUSE, ctl_noarg, "" },
49 { "toggle", TOGGLE, ctl_noarg, "" },
50 { "stop", STOP, ctl_noarg, "" },
51 { "restart", RESTART, ctl_noarg, "" },
52 { "add", ADD, ctl_add, "files...", 1 },
53 { "flush", FLUSH, ctl_noarg, "" },
54 { "show", SHOW, ctl_noarg, "" },
55 { "status", STATUS, ctl_noarg, "" },
56 { "next", NEXT, ctl_noarg, "" },
57 { "prev", PREV, ctl_noarg, "" },
58 { "load", LOAD, ctl_load, "[file]", 1 },
65 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
66 fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
71 ctl_usage(struct ctl_command *ctl)
73 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
74 ctl->name, ctl->usage);
79 parse(int argc, char **argv)
81 struct ctl_command *ctl = NULL;
82 struct parse_result res;
85 memset(&res, 0, sizeof(res));
87 for (i = 0; ctl_commands[i].name != NULL; ++i) {
88 if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
92 "ambiguous argument: %s\n", argv[0]);
95 ctl = &ctl_commands[i];
100 fprintf(stderr, "unknown argument: %s\n", argv[0]);
104 res.action = ctl->action;
107 if (!ctl->has_pledge) {
108 /* pledge(2) default if command doesn't have its own */
109 if (pledge("stdio", NULL) == -1)
113 status = ctl->main(&res, argc, argv);
120 enqueue_tracks(char **files)
125 for (; *files != NULL; ++files) {
126 memset(&res, 0, sizeof(res));
127 if (realpath(*files, res) == NULL) {
128 log_warn("realpath %s", *files);
132 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
141 print_error_message(const char *prfx, struct imsg *imsg)
146 datalen = IMSG_DATA_SIZE(*imsg);
147 if ((msg = calloc(1, datalen)) == NULL)
148 fatal("calloc %zu", datalen);
149 memcpy(msg, imsg->data, datalen);
150 if (datalen == 0 || msg[datalen-1] != '\0')
151 fatalx("malformed error message");
153 log_warnx("%s: %s", prfx, msg);
158 show_add(struct imsg *imsg, int *ret, char ***files)
160 if (**files == NULL) {
161 log_warnx("received more replies than file sent");
166 if (imsg->hdr.type == IMSG_CTL_ERR)
167 print_error_message(**files, imsg);
168 else if (imsg->hdr.type == IMSG_CTL_ADD)
169 log_debug("enqueued %s", **files);
171 fatalx("got invalid message %d", imsg->hdr.type);
174 return (**files) == NULL;
178 show_complete(struct imsg *imsg, int *ret)
180 struct player_status s;
183 if (imsg->hdr.type == IMSG_CTL_ERR) {
184 print_error_message("show failed", imsg);
189 datalen = IMSG_DATA_SIZE(*imsg);
193 if (datalen != sizeof(s))
194 fatalx("%s: data size mismatch", __func__);
195 memcpy(&s, imsg->data, sizeof(s));
196 if (s.path[sizeof(s.path)-1] != '\0')
197 fatalx("%s: data corrupted?", __func__);
199 printf("%s %s\n", s.status == STATE_PLAYING ? ">" : " ", s.path);
204 show_status(struct imsg *imsg, int *ret)
206 struct player_status s;
209 if (imsg->hdr.type == IMSG_CTL_ERR) {
210 print_error_message("show failed", imsg);
215 if (imsg->hdr.type != IMSG_CTL_STATUS)
216 fatalx("%s: got wrong reply", __func__);
218 datalen = IMSG_DATA_SIZE(*imsg);
219 if (datalen != sizeof(s))
220 fatalx("%s: data size mismatch", __func__);
221 memcpy(&s, imsg->data, sizeof(s));
222 if (s.path[sizeof(s.path)-1] != '\0')
223 fatalx("%s: data corrupted?", __func__);
240 printf("%s\n", s.path);
245 ctlaction(struct parse_result *res)
249 int ret = 0, done = 1;
252 switch (res->action) {
254 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
257 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
260 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
263 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
266 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
271 ret = enqueue_tracks(res->files);
274 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
278 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
282 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
285 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
288 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
292 /* action not expected */
293 fatalx("invalid action %u", res->action);
303 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
304 fatalx("imsg_read error");
306 fatalx("pipe closed");
309 if ((n = imsg_get(ibuf, &imsg)) == -1)
310 fatalx("imsg_get error");
314 switch (res->action) {
316 done = show_add(&imsg, &ret, &files);
319 done = show_complete(&imsg, &ret);
322 done = show_status(&imsg, &ret);
338 ctl_noarg(struct parse_result *res, int argc, char **argv)
342 return ctlaction(res);
346 ctl_add(struct parse_result *res, int argc, char **argv)
352 if (pledge("stdio rpath", NULL) == -1)
355 return ctlaction(res);
359 ctl_load(struct parse_result *res, int argc, char **argv)
369 else if (argc == 2) {
370 if ((res->file = fopen(argv[1], "r")) == NULL)
371 fatal("open %s", argv[1]);
375 if (pledge("stdio rpath", NULL) == -1)
378 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
380 while ((linelen = getline(&line, &linesize, res->file)) != -1) {
383 line[linelen-1] = '\0';
385 if (file[0] == '>' && file[1] == ' ')
387 if (file[0] == ' ' && file[1] == ' ')
390 memset(&path, 0, sizeof(path));
391 if (realpath(file, path) == NULL) {
392 log_warn("realpath %s", file);
396 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
402 if (ferror(res->file))
411 struct sockaddr_un sun;
412 int sock, saved_errno;
414 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
417 memset(&sun, 0, sizeof(sun));
418 sun.sun_family = AF_UNIX;
419 strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
420 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
431 ctl(int argc, char **argv)
435 log_init(1, LOG_DAEMON);
436 log_setverbose(verbose);
439 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
441 if ((ctl_sock = sockconn()) != -1)
443 if (errno != ENOENT && errno != ECONNREFUSED)
444 fatal("connect %s", csock);
449 nanosleep(&ts, NULL);
453 fatalx("failed to connect to the daemon");
455 ibuf = xmalloc(sizeof(*ibuf));
456 imsg_init(ibuf, ctl_sock);
461 exit(parse(argc, argv));