2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 3baa2617 2022-02-16 op * Permission to use, copy, modify, and distribute this software for any
5 3baa2617 2022-02-16 op * purpose with or without fee is hereby granted, provided that the above
6 3baa2617 2022-02-16 op * copyright notice and this permission notice appear in all copies.
8 3baa2617 2022-02-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3baa2617 2022-02-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3baa2617 2022-02-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3baa2617 2022-02-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3baa2617 2022-02-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3baa2617 2022-02-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3baa2617 2022-02-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 3baa2617 2022-02-16 op #include <sys/types.h>
18 3baa2617 2022-02-16 op #include <sys/socket.h>
19 3baa2617 2022-02-16 op #include <sys/queue.h>
20 3baa2617 2022-02-16 op #include <sys/uio.h>
21 3baa2617 2022-02-16 op #include <sys/un.h>
23 3baa2617 2022-02-16 op #include <errno.h>
24 3baa2617 2022-02-16 op #include <event.h>
25 3baa2617 2022-02-16 op #include <limits.h>
26 3baa2617 2022-02-16 op #include <sndio.h>
27 3baa2617 2022-02-16 op #include <stdio.h>
28 3baa2617 2022-02-16 op #include <stdlib.h>
29 3baa2617 2022-02-16 op #include <stdint.h>
30 3baa2617 2022-02-16 op #include <string.h>
31 3baa2617 2022-02-16 op #include <syslog.h>
32 3baa2617 2022-02-16 op #include <unistd.h>
33 3baa2617 2022-02-16 op #include <imsg.h>
35 3baa2617 2022-02-16 op #include "amused.h"
36 3baa2617 2022-02-16 op #include "log.h"
37 bb3f279f 2022-02-16 op #include "playlist.h"
38 3baa2617 2022-02-16 op #include "xmalloc.h"
40 3baa2617 2022-02-16 op static struct imsgbuf *ibuf;
42 3baa2617 2022-02-16 op int ctl_noarg(struct parse_result *, int, char **);
43 3baa2617 2022-02-16 op int ctl_add(struct parse_result *, int, char **);
45 3baa2617 2022-02-16 op struct ctl_command ctl_commands[] = {
46 3baa2617 2022-02-16 op { "play", PLAY, ctl_noarg, "" },
47 3baa2617 2022-02-16 op { "pause", PAUSE, ctl_noarg, "" },
48 3baa2617 2022-02-16 op { "toggle", TOGGLE, ctl_noarg, "" },
49 3baa2617 2022-02-16 op { "stop", STOP, ctl_noarg, "" },
50 3baa2617 2022-02-16 op { "restart", RESTART, ctl_noarg, "" },
51 3baa2617 2022-02-16 op { "add", ADD, ctl_add, "files...", 1 },
52 3baa2617 2022-02-16 op { "flush", FLUSH, ctl_noarg, "" },
53 d980494c 2022-02-16 op { "show", SHOW, ctl_noarg, "" },
54 bb3f279f 2022-02-16 op { "status", STATUS, ctl_noarg, "" },
55 af27e631 2022-02-17 op { "next", NEXT, ctl_noarg, "" },
56 af27e631 2022-02-17 op { "prev", PREV, ctl_noarg, "" },
63 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
64 3baa2617 2022-02-16 op fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
68 3baa2617 2022-02-16 op static __dead void
69 3baa2617 2022-02-16 op ctl_usage(struct ctl_command *ctl)
71 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
72 3baa2617 2022-02-16 op ctl->name, ctl->usage);
77 3baa2617 2022-02-16 op parse(int argc, char **argv)
79 3baa2617 2022-02-16 op struct ctl_command *ctl = NULL;
80 3baa2617 2022-02-16 op struct parse_result res;
81 3baa2617 2022-02-16 op int i, status;
83 3baa2617 2022-02-16 op memset(&res, 0, sizeof(res));
85 3baa2617 2022-02-16 op for (i = 0; ctl_commands[i].name != NULL; ++i) {
86 3baa2617 2022-02-16 op if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
88 3baa2617 2022-02-16 op if (ctl != NULL) {
89 3baa2617 2022-02-16 op fprintf(stderr,
90 3baa2617 2022-02-16 op "ambiguous argument: %s\n", argv[0]);
93 3baa2617 2022-02-16 op ctl = &ctl_commands[i];
97 3baa2617 2022-02-16 op if (ctl == NULL) {
98 3baa2617 2022-02-16 op fprintf(stderr, "unknown argument: %s\n", argv[0]);
102 3baa2617 2022-02-16 op res.action = ctl->action;
103 3baa2617 2022-02-16 op res.ctl = ctl;
105 3baa2617 2022-02-16 op if (!ctl->has_pledge) {
106 3baa2617 2022-02-16 op /* pledge(2) default if command doesn't have its own */
107 3baa2617 2022-02-16 op if (pledge("stdio", NULL) == -1)
108 3baa2617 2022-02-16 op fatal("pledge");
111 3baa2617 2022-02-16 op status = ctl->main(&res, argc, argv);
112 3baa2617 2022-02-16 op close(ibuf->fd);
114 3baa2617 2022-02-16 op return status;
118 3baa2617 2022-02-16 op enqueue_tracks(char **files)
120 3baa2617 2022-02-16 op char res[PATH_MAX];
123 3baa2617 2022-02-16 op for (; *files != NULL; ++files) {
124 3baa2617 2022-02-16 op memset(&res, 0, sizeof(res));
125 3baa2617 2022-02-16 op if (realpath(*files, res) == NULL) {
126 3baa2617 2022-02-16 op log_warn("realpath %s", *files);
130 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
131 3baa2617 2022-02-16 op res, sizeof(res));
135 3baa2617 2022-02-16 op return enq == 0;
139 8ff9231f 2022-02-16 op print_error_message(const char *prfx, struct imsg *imsg)
141 8ff9231f 2022-02-16 op size_t datalen;
144 8ff9231f 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
145 8ff9231f 2022-02-16 op if ((msg = calloc(1, datalen)) == NULL)
146 8ff9231f 2022-02-16 op fatal("calloc %zu", datalen);
147 8ff9231f 2022-02-16 op memcpy(msg, imsg->data, datalen);
148 8ff9231f 2022-02-16 op if (datalen == 0 || msg[datalen-1] != '\0')
149 8ff9231f 2022-02-16 op fatalx("malformed error message");
151 8ff9231f 2022-02-16 op log_warnx("%s: %s", prfx, msg);
156 8ff9231f 2022-02-16 op show_add(struct imsg *imsg, int *ret, char ***files)
158 8ff9231f 2022-02-16 op if (**files == NULL) {
159 8ff9231f 2022-02-16 op log_warnx("received more replies than file sent");
164 8ff9231f 2022-02-16 op if (imsg->hdr.type == IMSG_CTL_ERR)
165 8ff9231f 2022-02-16 op print_error_message(**files, imsg);
166 8ff9231f 2022-02-16 op else if (imsg->hdr.type == IMSG_CTL_ADD)
167 8ff9231f 2022-02-16 op log_debug("enqueued %s", **files);
169 8ff9231f 2022-02-16 op fatalx("got invalid message %d", imsg->hdr.type);
172 8ff9231f 2022-02-16 op return (**files) == NULL;
176 3baa2617 2022-02-16 op show_complete(struct imsg *imsg, int *ret)
178 63dd8e70 2022-02-17 op struct player_status s;
179 3baa2617 2022-02-16 op size_t datalen;
181 8ff9231f 2022-02-16 op if (imsg->hdr.type == IMSG_CTL_ERR) {
182 8ff9231f 2022-02-16 op print_error_message("show failed", imsg);
187 3baa2617 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
188 3baa2617 2022-02-16 op if (datalen == 0)
191 63dd8e70 2022-02-17 op if (datalen != sizeof(s))
192 3baa2617 2022-02-16 op fatalx("%s: data size mismatch", __func__);
193 63dd8e70 2022-02-17 op memcpy(&s, imsg->data, sizeof(s));
194 63dd8e70 2022-02-17 op if (s.path[sizeof(s.path)-1] != '\0')
195 3baa2617 2022-02-16 op fatalx("%s: data corrupted?", __func__);
197 63dd8e70 2022-02-17 op printf("%s %s\n", s.status == STATE_PLAYING ? ">" : " ", s.path);
202 bb3f279f 2022-02-16 op show_status(struct imsg *imsg, int *ret)
204 bb3f279f 2022-02-16 op struct player_status s;
205 bb3f279f 2022-02-16 op size_t datalen;
207 bb3f279f 2022-02-16 op if (imsg->hdr.type == IMSG_CTL_ERR) {
208 bb3f279f 2022-02-16 op print_error_message("show failed", imsg);
213 bb3f279f 2022-02-16 op if (imsg->hdr.type != IMSG_CTL_STATUS)
214 bb3f279f 2022-02-16 op fatalx("%s: got wrong reply", __func__);
216 bb3f279f 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
217 bb3f279f 2022-02-16 op if (datalen != sizeof(s))
218 bb3f279f 2022-02-16 op fatalx("%s: data size mismatch", __func__);
219 bb3f279f 2022-02-16 op memcpy(&s, imsg->data, sizeof(s));
220 bb3f279f 2022-02-16 op if (s.path[sizeof(s.path)-1] != '\0')
221 bb3f279f 2022-02-16 op fatalx("%s: data corrupted?", __func__);
223 bb3f279f 2022-02-16 op switch (s.status) {
224 bb3f279f 2022-02-16 op case STATE_STOPPED:
225 bb3f279f 2022-02-16 op printf("stopped ");
227 bb3f279f 2022-02-16 op case STATE_PLAYING:
228 bb3f279f 2022-02-16 op printf("playing ");
230 bb3f279f 2022-02-16 op case STATE_PAUSED:
231 bb3f279f 2022-02-16 op printf("paused ");
234 bb3f279f 2022-02-16 op printf("unknown ");
238 bb3f279f 2022-02-16 op printf("%s\n", s.path);
243 3baa2617 2022-02-16 op ctlaction(struct parse_result *res)
245 3baa2617 2022-02-16 op struct imsg imsg;
247 3baa2617 2022-02-16 op int ret = 0, done = 1;
248 8ff9231f 2022-02-16 op char **files;
250 3baa2617 2022-02-16 op switch (res->action) {
252 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
255 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
258 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
261 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
263 3baa2617 2022-02-16 op case RESTART:
264 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
268 8ff9231f 2022-02-16 op files = res->files;
269 3baa2617 2022-02-16 op ret = enqueue_tracks(res->files);
272 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
276 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
280 bb3f279f 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
283 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
286 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
289 3baa2617 2022-02-16 op /* action not expected */
290 3baa2617 2022-02-16 op fatalx("invalid action %u", res->action);
294 3baa2617 2022-02-16 op if (ret != 0)
297 3baa2617 2022-02-16 op imsg_flush(ibuf);
299 3baa2617 2022-02-16 op while (!done) {
300 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
301 3baa2617 2022-02-16 op fatalx("imsg_read error");
303 3baa2617 2022-02-16 op fatalx("pipe closed");
305 3baa2617 2022-02-16 op while (!done) {
306 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
307 3baa2617 2022-02-16 op fatalx("imsg_get error");
311 3baa2617 2022-02-16 op switch (res->action) {
313 8ff9231f 2022-02-16 op done = show_add(&imsg, &ret, &files);
316 3baa2617 2022-02-16 op done = show_complete(&imsg, &ret);
319 bb3f279f 2022-02-16 op done = show_status(&imsg, &ret);
326 3baa2617 2022-02-16 op imsg_free(&imsg);
335 3baa2617 2022-02-16 op ctl_noarg(struct parse_result *res, int argc, char **argv)
337 3baa2617 2022-02-16 op if (argc != 1)
338 3baa2617 2022-02-16 op ctl_usage(res->ctl);
339 3baa2617 2022-02-16 op return ctlaction(res);
343 3baa2617 2022-02-16 op ctl_add(struct parse_result *res, int argc, char **argv)
345 3baa2617 2022-02-16 op if (argc < 2)
346 3baa2617 2022-02-16 op ctl_usage(res->ctl);
347 3baa2617 2022-02-16 op res->files = argv+1;
348 3baa2617 2022-02-16 op return ctlaction(res);
352 fea541a8 2022-02-16 op sockconn(void)
354 fea541a8 2022-02-16 op struct sockaddr_un sun;
355 fea541a8 2022-02-16 op int sock, saved_errno;
357 fea541a8 2022-02-16 op if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
358 3baa2617 2022-02-16 op fatal("socket");
360 3baa2617 2022-02-16 op memset(&sun, 0, sizeof(sun));
361 3baa2617 2022-02-16 op sun.sun_family = AF_UNIX;
362 3baa2617 2022-02-16 op strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
363 fea541a8 2022-02-16 op if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
364 fea541a8 2022-02-16 op saved_errno = errno;
366 fea541a8 2022-02-16 op errno = saved_errno;
374 fea541a8 2022-02-16 op ctl(int argc, char **argv)
376 fea541a8 2022-02-16 op int ctl_sock, i = 0;
378 fea541a8 2022-02-16 op log_init(1, LOG_DAEMON);
379 fea541a8 2022-02-16 op log_setverbose(verbose);
382 fea541a8 2022-02-16 op struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
384 fea541a8 2022-02-16 op if ((ctl_sock = sockconn()) != -1)
386 fea541a8 2022-02-16 op if (errno != ENOENT && errno != ECONNREFUSED)
387 fea541a8 2022-02-16 op fatal("connect %s", csock);
390 fea541a8 2022-02-16 op spawn_daemon();
392 fea541a8 2022-02-16 op nanosleep(&ts, NULL);
393 fea541a8 2022-02-16 op } while (++i < 20);
395 fea541a8 2022-02-16 op if (ctl_sock == -1)
396 fea541a8 2022-02-16 op fatalx("failed to connect to the daemon");
398 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
399 3baa2617 2022-02-16 op imsg_init(ibuf, ctl_sock);
401 3baa2617 2022-02-16 op optreset = 1;
404 3baa2617 2022-02-16 op exit(parse(argc, argv));