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, "" },
55 { "next", NEXT, ctl_noarg, "" },
56 { "prev", PREV, ctl_noarg, "" },
63 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
64 fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
69 ctl_usage(struct ctl_command *ctl)
71 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
72 ctl->name, ctl->usage);
77 parse(int argc, char **argv)
79 struct ctl_command *ctl = NULL;
80 struct parse_result res;
83 memset(&res, 0, sizeof(res));
85 for (i = 0; ctl_commands[i].name != NULL; ++i) {
86 if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
90 "ambiguous argument: %s\n", argv[0]);
93 ctl = &ctl_commands[i];
98 fprintf(stderr, "unknown argument: %s\n", argv[0]);
102 res.action = ctl->action;
105 if (!ctl->has_pledge) {
106 /* pledge(2) default if command doesn't have its own */
107 if (pledge("stdio", NULL) == -1)
111 status = ctl->main(&res, argc, argv);
118 enqueue_tracks(char **files)
123 for (; *files != NULL; ++files) {
124 memset(&res, 0, sizeof(res));
125 if (realpath(*files, res) == NULL) {
126 log_warn("realpath %s", *files);
130 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
139 print_error_message(const char *prfx, struct imsg *imsg)
144 datalen = IMSG_DATA_SIZE(*imsg);
145 if ((msg = calloc(1, datalen)) == NULL)
146 fatal("calloc %zu", datalen);
147 memcpy(msg, imsg->data, datalen);
148 if (datalen == 0 || msg[datalen-1] != '\0')
149 fatalx("malformed error message");
151 log_warnx("%s: %s", prfx, msg);
156 show_add(struct imsg *imsg, int *ret, char ***files)
158 if (**files == NULL) {
159 log_warnx("received more replies than file sent");
164 if (imsg->hdr.type == IMSG_CTL_ERR)
165 print_error_message(**files, imsg);
166 else if (imsg->hdr.type == IMSG_CTL_ADD)
167 log_debug("enqueued %s", **files);
169 fatalx("got invalid message %d", imsg->hdr.type);
172 return (**files) == NULL;
176 show_complete(struct imsg *imsg, int *ret)
178 struct player_status s;
181 if (imsg->hdr.type == IMSG_CTL_ERR) {
182 print_error_message("show failed", imsg);
187 datalen = IMSG_DATA_SIZE(*imsg);
191 if (datalen != sizeof(s))
192 fatalx("%s: data size mismatch", __func__);
193 memcpy(&s, imsg->data, sizeof(s));
194 if (s.path[sizeof(s.path)-1] != '\0')
195 fatalx("%s: data corrupted?", __func__);
197 printf("%s %s\n", s.status == STATE_PLAYING ? ">" : " ", s.path);
202 show_status(struct imsg *imsg, int *ret)
204 struct player_status s;
207 if (imsg->hdr.type == IMSG_CTL_ERR) {
208 print_error_message("show failed", imsg);
213 if (imsg->hdr.type != IMSG_CTL_STATUS)
214 fatalx("%s: got wrong reply", __func__);
216 datalen = IMSG_DATA_SIZE(*imsg);
217 if (datalen != sizeof(s))
218 fatalx("%s: data size mismatch", __func__);
219 memcpy(&s, imsg->data, sizeof(s));
220 if (s.path[sizeof(s.path)-1] != '\0')
221 fatalx("%s: data corrupted?", __func__);
238 printf("%s\n", s.path);
243 ctlaction(struct parse_result *res)
247 int ret = 0, done = 1;
250 switch (res->action) {
252 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
255 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
258 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
261 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
264 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
269 ret = enqueue_tracks(res->files);
272 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
276 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
280 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
283 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
286 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
289 /* action not expected */
290 fatalx("invalid action %u", res->action);
300 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
301 fatalx("imsg_read error");
303 fatalx("pipe closed");
306 if ((n = imsg_get(ibuf, &imsg)) == -1)
307 fatalx("imsg_get error");
311 switch (res->action) {
313 done = show_add(&imsg, &ret, &files);
316 done = show_complete(&imsg, &ret);
319 done = show_status(&imsg, &ret);
335 ctl_noarg(struct parse_result *res, int argc, char **argv)
339 return ctlaction(res);
343 ctl_add(struct parse_result *res, int argc, char **argv)
348 return ctlaction(res);
354 struct sockaddr_un sun;
355 int sock, saved_errno;
357 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
360 memset(&sun, 0, sizeof(sun));
361 sun.sun_family = AF_UNIX;
362 strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
363 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
374 ctl(int argc, char **argv)
378 log_init(1, LOG_DAEMON);
379 log_setverbose(verbose);
382 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
384 if ((ctl_sock = sockconn()) != -1)
386 if (errno != ENOENT && errno != ECONNREFUSED)
387 fatal("connect %s", csock);
392 nanosleep(&ts, NULL);
396 fatalx("failed to connect to the daemon");
398 ibuf = xmalloc(sizeof(*ibuf));
399 imsg_init(ibuf, ctl_sock);
404 exit(parse(argc, argv));