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 **);
45 struct ctl_command ctl_commands[] = {
46 { "play", PLAY, ctl_noarg, "" },
47 { "pause", PAUSE, ctl_noarg, "" },
48 { "toggle", TOGGLE, ctl_noarg, "" },
49 { "stop", STOP, ctl_noarg, "" },
50 { "restart", RESTART, ctl_noarg, "" },
51 { "add", ADD, ctl_add, "files...", 1 },
52 { "flush", FLUSH, ctl_noarg, "" },
53 { "show", SHOW, ctl_noarg, "" },
54 { "status", STATUS, ctl_noarg, "" },
61 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
62 fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
67 ctl_usage(struct ctl_command *ctl)
69 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
70 ctl->name, ctl->usage);
75 parse(int argc, char **argv)
77 struct ctl_command *ctl = NULL;
78 struct parse_result res;
81 memset(&res, 0, sizeof(res));
83 for (i = 0; ctl_commands[i].name != NULL; ++i) {
84 if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
88 "ambiguous argument: %s\n", argv[0]);
91 ctl = &ctl_commands[i];
96 fprintf(stderr, "unknown argument: %s\n", argv[0]);
100 res.action = ctl->action;
103 if (!ctl->has_pledge) {
104 /* pledge(2) default if command doesn't have its own */
105 if (pledge("stdio", NULL) == -1)
109 status = ctl->main(&res, argc, argv);
116 enqueue_tracks(char **files)
121 for (; *files != NULL; ++files) {
122 memset(&res, 0, sizeof(res));
123 if (realpath(*files, res) == NULL) {
124 log_warn("realpath %s", *files);
128 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
137 print_error_message(const char *prfx, struct imsg *imsg)
142 datalen = IMSG_DATA_SIZE(*imsg);
143 if ((msg = calloc(1, datalen)) == NULL)
144 fatal("calloc %zu", datalen);
145 memcpy(msg, imsg->data, datalen);
146 if (datalen == 0 || msg[datalen-1] != '\0')
147 fatalx("malformed error message");
149 log_warnx("%s: %s", prfx, msg);
154 show_add(struct imsg *imsg, int *ret, char ***files)
156 if (**files == NULL) {
157 log_warnx("received more replies than file sent");
162 if (imsg->hdr.type == IMSG_CTL_ERR)
163 print_error_message(**files, imsg);
164 else if (imsg->hdr.type == IMSG_CTL_ADD)
165 log_debug("enqueued %s", **files);
167 fatalx("got invalid message %d", imsg->hdr.type);
170 return (**files) == NULL;
174 show_complete(struct imsg *imsg, int *ret)
179 if (imsg->hdr.type == IMSG_CTL_ERR) {
180 print_error_message("show failed", imsg);
185 datalen = IMSG_DATA_SIZE(*imsg);
189 if (datalen != sizeof(path))
190 fatalx("%s: data size mismatch", __func__);
191 memcpy(path, imsg->data, sizeof(path));
192 if (path[datalen-1] != '\0')
193 fatalx("%s: data corrupted?", __func__);
195 printf("%s\n", path);
200 show_status(struct imsg *imsg, int *ret)
202 struct player_status s;
205 if (imsg->hdr.type == IMSG_CTL_ERR) {
206 print_error_message("show failed", imsg);
211 if (imsg->hdr.type != IMSG_CTL_STATUS)
212 fatalx("%s: got wrong reply", __func__);
214 datalen = IMSG_DATA_SIZE(*imsg);
215 if (datalen != sizeof(s))
216 fatalx("%s: data size mismatch", __func__);
217 memcpy(&s, imsg->data, sizeof(s));
218 if (s.path[sizeof(s.path)-1] != '\0')
219 fatalx("%s: data corrupted?", __func__);
236 printf("%s\n", s.path);
241 ctlaction(struct parse_result *res)
245 int ret = 0, done = 1;
248 switch (res->action) {
250 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
253 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
256 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
259 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
262 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
267 ret = enqueue_tracks(res->files);
270 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
274 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
278 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
281 /* action not expected */
282 fatalx("invalid action %u", res->action);
292 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
293 fatalx("imsg_read error");
295 fatalx("pipe closed");
298 if ((n = imsg_get(ibuf, &imsg)) == -1)
299 fatalx("imsg_get error");
303 switch (res->action) {
305 done = show_add(&imsg, &ret, &files);
308 done = show_complete(&imsg, &ret);
311 done = show_status(&imsg, &ret);
327 ctl_noarg(struct parse_result *res, int argc, char **argv)
331 return ctlaction(res);
335 ctl_add(struct parse_result *res, int argc, char **argv)
340 return ctlaction(res);
346 struct sockaddr_un sun;
347 int sock, saved_errno;
349 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
352 memset(&sun, 0, sizeof(sun));
353 sun.sun_family = AF_UNIX;
354 strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
355 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
366 ctl(int argc, char **argv)
370 log_init(1, LOG_DAEMON);
371 log_setverbose(verbose);
374 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
376 if ((ctl_sock = sockconn()) != -1)
378 if (errno != ENOENT && errno != ECONNREFUSED)
379 fatal("connect %s", csock);
384 nanosleep(&ts, NULL);
388 fatalx("failed to connect to the daemon");
390 ibuf = xmalloc(sizeof(*ibuf));
391 imsg_init(ibuf, ctl_sock);
396 exit(parse(argc, argv));