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>
39 static struct imsgbuf *ibuf;
41 int ctl_noarg(struct parse_result *, int, char **);
42 int ctl_add(struct parse_result *, int, char **);
44 struct ctl_command ctl_commands[] = {
45 { "play", PLAY, ctl_noarg, "" },
46 { "pause", PAUSE, ctl_noarg, "" },
47 { "toggle", TOGGLE, ctl_noarg, "" },
48 { "stop", STOP, ctl_noarg, "" },
49 { "restart", RESTART, ctl_noarg, "" },
50 { "add", ADD, ctl_add, "files...", 1 },
51 { "flush", FLUSH, ctl_noarg, "" },
58 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
59 fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
64 ctl_usage(struct ctl_command *ctl)
66 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
67 ctl->name, ctl->usage);
72 parse(int argc, char **argv)
74 struct ctl_command *ctl = NULL;
75 struct parse_result res;
78 memset(&res, 0, sizeof(res));
80 for (i = 0; ctl_commands[i].name != NULL; ++i) {
81 if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
85 "ambiguous argument: %s\n", argv[0]);
88 ctl = &ctl_commands[i];
93 fprintf(stderr, "unknown argument: %s\n", argv[0]);
97 res.action = ctl->action;
100 if (!ctl->has_pledge) {
101 /* pledge(2) default if command doesn't have its own */
102 if (pledge("stdio", NULL) == -1)
106 status = ctl->main(&res, argc, argv);
113 enqueue_tracks(char **files)
118 for (; *files != NULL; ++files) {
119 memset(&res, 0, sizeof(res));
120 if (realpath(*files, res) == NULL) {
121 log_warn("realpath %s", *files);
125 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
134 print_error_message(const char *prfx, struct imsg *imsg)
139 datalen = IMSG_DATA_SIZE(*imsg);
140 if ((msg = calloc(1, datalen)) == NULL)
141 fatal("calloc %zu", datalen);
142 memcpy(msg, imsg->data, datalen);
143 if (datalen == 0 || msg[datalen-1] != '\0')
144 fatalx("malformed error message");
146 log_warnx("%s: %s", prfx, msg);
151 show_add(struct imsg *imsg, int *ret, char ***files)
153 if (**files == NULL) {
154 log_warnx("received more replies than file sent");
159 if (imsg->hdr.type == IMSG_CTL_ERR)
160 print_error_message(**files, imsg);
161 else if (imsg->hdr.type == IMSG_CTL_ADD)
162 log_debug("enqueued %s", **files);
164 fatalx("got invalid message %d", imsg->hdr.type);
167 return (**files) == NULL;
171 show_complete(struct imsg *imsg, int *ret)
176 if (imsg->hdr.type == IMSG_CTL_ERR) {
177 print_error_message("show failed", imsg);
182 datalen = IMSG_DATA_SIZE(*imsg);
186 if (datalen != sizeof(path))
187 fatalx("%s: data size mismatch", __func__);
188 memcpy(path, imsg->data, sizeof(path));
189 if (path[datalen-1] != '\0')
190 fatalx("%s: data corrupted?", __func__);
192 printf("%s\n", path);
197 ctlaction(struct parse_result *res)
201 int ret = 0, done = 1;
204 switch (res->action) {
206 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
209 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
212 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
215 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
218 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
223 ret = enqueue_tracks(res->files);
226 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
230 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
233 /* action not expected */
234 fatalx("invalid action %u", res->action);
244 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
245 fatalx("imsg_read error");
247 fatalx("pipe closed");
250 if ((n = imsg_get(ibuf, &imsg)) == -1)
251 fatalx("imsg_get error");
255 switch (res->action) {
257 done = show_add(&imsg, &ret, &files);
260 done = show_complete(&imsg, &ret);
276 ctl_noarg(struct parse_result *res, int argc, char **argv)
280 return ctlaction(res);
284 ctl_add(struct parse_result *res, int argc, char **argv)
289 return ctlaction(res);
293 ctl(int argc, char **argv)
295 struct sockaddr_un sun;
298 log_init(1, LOG_DAEMON);
299 log_setverbose(verbose);
301 if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
304 memset(&sun, 0, sizeof(sun));
305 sun.sun_family = AF_UNIX;
306 strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
308 if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
309 fatal("connect %s", csock);
311 ibuf = xmalloc(sizeof(*ibuf));
312 imsg_init(ibuf, ctl_sock);
317 exit(parse(argc, argv));