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 show_complete(struct imsg *imsg, int *ret)
139 datalen = IMSG_DATA_SIZE(*imsg);
143 if (datalen != sizeof(path))
144 fatalx("%s: data size mismatch", __func__);
145 memcpy(path, imsg->data, sizeof(path));
146 if (path[datalen-1] != '\0')
147 fatalx("%s: data corrupted?", __func__);
149 printf("%s\n", path);
154 ctlaction(struct parse_result *res)
158 int ret = 0, done = 1;
160 switch (res->action) {
162 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
165 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
168 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
171 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
174 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
177 ret = enqueue_tracks(res->files);
180 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
184 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
187 /* action not expected */
188 fatalx("invalid action %u", res->action);
198 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
199 fatalx("imsg_read error");
201 fatalx("pipe closed");
204 if ((n = imsg_get(ibuf, &imsg)) == -1)
205 fatalx("imsg_get error");
209 switch (res->action) {
211 done = show_complete(&imsg, &ret);
227 ctl_noarg(struct parse_result *res, int argc, char **argv)
231 return ctlaction(res);
235 ctl_add(struct parse_result *res, int argc, char **argv)
240 return ctlaction(res);
244 ctl(int argc, char **argv)
246 struct sockaddr_un sun;
249 log_init(1, LOG_DAEMON);
250 log_setverbose(verbose);
252 if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
255 memset(&sun, 0, sizeof(sun));
256 sun.sun_family = AF_UNIX;
257 strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
259 if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
260 fatal("connect %s", csock);
262 ibuf = xmalloc(sizeof(*ibuf));
263 imsg_init(ibuf, ctl_sock);
268 exit(parse(argc, argv));