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, "" },
52 { "show", SHOW, ctl_noarg, "" },
59 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
60 fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
65 ctl_usage(struct ctl_command *ctl)
67 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
68 ctl->name, ctl->usage);
73 parse(int argc, char **argv)
75 struct ctl_command *ctl = NULL;
76 struct parse_result res;
79 memset(&res, 0, sizeof(res));
81 for (i = 0; ctl_commands[i].name != NULL; ++i) {
82 if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
86 "ambiguous argument: %s\n", argv[0]);
89 ctl = &ctl_commands[i];
94 fprintf(stderr, "unknown argument: %s\n", argv[0]);
98 res.action = ctl->action;
101 if (!ctl->has_pledge) {
102 /* pledge(2) default if command doesn't have its own */
103 if (pledge("stdio", NULL) == -1)
107 status = ctl->main(&res, argc, argv);
114 enqueue_tracks(char **files)
119 for (; *files != NULL; ++files) {
120 memset(&res, 0, sizeof(res));
121 if (realpath(*files, res) == NULL) {
122 log_warn("realpath %s", *files);
126 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
135 print_error_message(const char *prfx, struct imsg *imsg)
140 datalen = IMSG_DATA_SIZE(*imsg);
141 if ((msg = calloc(1, datalen)) == NULL)
142 fatal("calloc %zu", datalen);
143 memcpy(msg, imsg->data, datalen);
144 if (datalen == 0 || msg[datalen-1] != '\0')
145 fatalx("malformed error message");
147 log_warnx("%s: %s", prfx, msg);
152 show_add(struct imsg *imsg, int *ret, char ***files)
154 if (**files == NULL) {
155 log_warnx("received more replies than file sent");
160 if (imsg->hdr.type == IMSG_CTL_ERR)
161 print_error_message(**files, imsg);
162 else if (imsg->hdr.type == IMSG_CTL_ADD)
163 log_debug("enqueued %s", **files);
165 fatalx("got invalid message %d", imsg->hdr.type);
168 return (**files) == NULL;
172 show_complete(struct imsg *imsg, int *ret)
177 if (imsg->hdr.type == IMSG_CTL_ERR) {
178 print_error_message("show failed", imsg);
183 datalen = IMSG_DATA_SIZE(*imsg);
187 if (datalen != sizeof(path))
188 fatalx("%s: data size mismatch", __func__);
189 memcpy(path, imsg->data, sizeof(path));
190 if (path[datalen-1] != '\0')
191 fatalx("%s: data corrupted?", __func__);
193 printf("%s\n", path);
198 ctlaction(struct parse_result *res)
202 int ret = 0, done = 1;
205 switch (res->action) {
207 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
210 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
213 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
216 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
219 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
224 ret = enqueue_tracks(res->files);
227 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
231 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
234 /* action not expected */
235 fatalx("invalid action %u", res->action);
245 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
246 fatalx("imsg_read error");
248 fatalx("pipe closed");
251 if ((n = imsg_get(ibuf, &imsg)) == -1)
252 fatalx("imsg_get error");
256 switch (res->action) {
258 done = show_add(&imsg, &ret, &files);
261 done = show_complete(&imsg, &ret);
277 ctl_noarg(struct parse_result *res, int argc, char **argv)
281 return ctlaction(res);
285 ctl_add(struct parse_result *res, int argc, char **argv)
290 return ctlaction(res);
296 struct sockaddr_un sun;
297 int sock, saved_errno;
299 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
302 memset(&sun, 0, sizeof(sun));
303 sun.sun_family = AF_UNIX;
304 strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
305 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
316 ctl(int argc, char **argv)
320 log_init(1, LOG_DAEMON);
321 log_setverbose(verbose);
324 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
326 if ((ctl_sock = sockconn()) != -1)
328 if (errno != ENOENT && errno != ECONNREFUSED)
329 fatal("connect %s", csock);
334 nanosleep(&ts, NULL);
338 fatalx("failed to connect to the daemon");
340 ibuf = xmalloc(sizeof(*ibuf));
341 imsg_init(ibuf, ctl_sock);
346 exit(parse(argc, argv));