Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/un.h>
23 #include <errno.h>
24 #include <event.h>
25 #include <limits.h>
26 #include <sndio.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <string.h>
31 #include <syslog.h>
32 #include <unistd.h>
33 #include <imsg.h>
35 #include "amused.h"
36 #include "log.h"
37 #include "playlist.h"
38 #include "xmalloc.h"
40 static struct imsgbuf *ibuf;
42 int ctl_noarg(struct parse_result *, int, char **);
43 int ctl_add(struct parse_result *, int, char **);
44 int ctl_load(struct parse_result *, int, char **);
46 struct ctl_command ctl_commands[] = {
47 { "play", PLAY, ctl_noarg, "" },
48 { "pause", PAUSE, ctl_noarg, "" },
49 { "toggle", TOGGLE, ctl_noarg, "" },
50 { "stop", STOP, ctl_noarg, "" },
51 { "restart", RESTART, ctl_noarg, "" },
52 { "add", ADD, ctl_add, "files...", 1 },
53 { "flush", FLUSH, ctl_noarg, "" },
54 { "show", SHOW, ctl_noarg, "" },
55 { "status", STATUS, ctl_noarg, "" },
56 { "next", NEXT, ctl_noarg, "" },
57 { "prev", PREV, ctl_noarg, "" },
58 { "load", LOAD, ctl_load, "[file]", 1 },
59 { NULL },
60 };
62 __dead void
63 usage(void)
64 {
65 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
66 fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
67 exit(1);
68 }
70 static __dead void
71 ctl_usage(struct ctl_command *ctl)
72 {
73 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
74 ctl->name, ctl->usage);
75 exit(1);
76 }
78 static int
79 parse(int argc, char **argv)
80 {
81 struct ctl_command *ctl = NULL;
82 struct parse_result res;
83 int i, status;
85 memset(&res, 0, sizeof(res));
87 for (i = 0; ctl_commands[i].name != NULL; ++i) {
88 if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
89 == 0) {
90 if (ctl != NULL) {
91 fprintf(stderr,
92 "ambiguous argument: %s\n", argv[0]);
93 usage();
94 }
95 ctl = &ctl_commands[i];
96 }
97 }
99 if (ctl == NULL) {
100 fprintf(stderr, "unknown argument: %s\n", argv[0]);
101 usage();
104 res.action = ctl->action;
105 res.ctl = ctl;
107 if (!ctl->has_pledge) {
108 /* pledge(2) default if command doesn't have its own */
109 if (pledge("stdio", NULL) == -1)
110 fatal("pledge");
113 status = ctl->main(&res, argc, argv);
114 close(ibuf->fd);
115 free(ibuf);
116 return status;
119 static int
120 enqueue_tracks(char **files)
122 char res[PATH_MAX];
123 int enq = 0;
125 for (; *files != NULL; ++files) {
126 memset(&res, 0, sizeof(res));
127 if (realpath(*files, res) == NULL) {
128 log_warn("realpath %s", *files);
129 continue;
132 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
133 res, sizeof(res));
134 enq++;
137 return enq == 0;
140 static void
141 print_error_message(const char *prfx, struct imsg *imsg)
143 size_t datalen;
144 char *msg;
146 datalen = IMSG_DATA_SIZE(*imsg);
147 if ((msg = calloc(1, datalen)) == NULL)
148 fatal("calloc %zu", datalen);
149 memcpy(msg, imsg->data, datalen);
150 if (datalen == 0 || msg[datalen-1] != '\0')
151 fatalx("malformed error message");
153 log_warnx("%s: %s", prfx, msg);
154 free(msg);
157 static int
158 show_add(struct imsg *imsg, int *ret, char ***files)
160 if (**files == NULL) {
161 log_warnx("received more replies than file sent");
162 *ret = 1;
163 return 1;
166 if (imsg->hdr.type == IMSG_CTL_ERR)
167 print_error_message(**files, imsg);
168 else if (imsg->hdr.type == IMSG_CTL_ADD)
169 log_debug("enqueued %s", **files);
170 else
171 fatalx("got invalid message %d", imsg->hdr.type);
173 (*files)++;
174 return (**files) == NULL;
177 static int
178 show_complete(struct imsg *imsg, int *ret)
180 struct player_status s;
181 size_t datalen;
183 if (imsg->hdr.type == IMSG_CTL_ERR) {
184 print_error_message("show failed", imsg);
185 *ret = 1;
186 return 1;
189 datalen = IMSG_DATA_SIZE(*imsg);
190 if (datalen == 0)
191 return 1;
193 if (datalen != sizeof(s))
194 fatalx("%s: data size mismatch", __func__);
195 memcpy(&s, imsg->data, sizeof(s));
196 if (s.path[sizeof(s.path)-1] != '\0')
197 fatalx("%s: data corrupted?", __func__);
199 printf("%s %s\n", s.status == STATE_PLAYING ? ">" : " ", s.path);
200 return 0;
203 static int
204 show_status(struct imsg *imsg, int *ret)
206 struct player_status s;
207 size_t datalen;
209 if (imsg->hdr.type == IMSG_CTL_ERR) {
210 print_error_message("show failed", imsg);
211 *ret = 1;
212 return 1;
215 if (imsg->hdr.type != IMSG_CTL_STATUS)
216 fatalx("%s: got wrong reply", __func__);
218 datalen = IMSG_DATA_SIZE(*imsg);
219 if (datalen != sizeof(s))
220 fatalx("%s: data size mismatch", __func__);
221 memcpy(&s, imsg->data, sizeof(s));
222 if (s.path[sizeof(s.path)-1] != '\0')
223 fatalx("%s: data corrupted?", __func__);
225 switch (s.status) {
226 case STATE_STOPPED:
227 printf("stopped ");
228 break;
229 case STATE_PLAYING:
230 printf("playing ");
231 break;
232 case STATE_PAUSED:
233 printf("paused ");
234 break;
235 default:
236 printf("unknown ");
237 break;
240 printf("%s\n", s.path);
241 return 1;
244 static int
245 ctlaction(struct parse_result *res)
247 struct imsg imsg;
248 ssize_t n;
249 int ret = 0, done = 1;
250 char **files;
252 switch (res->action) {
253 case PLAY:
254 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
255 break;
256 case PAUSE:
257 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
258 break;
259 case TOGGLE:
260 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
261 break;
262 case STOP:
263 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
264 break;
265 case RESTART:
266 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
267 break;
268 case ADD:
269 done = 0;
270 files = res->files;
271 ret = enqueue_tracks(res->files);
272 break;
273 case FLUSH:
274 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
275 break;
276 case SHOW:
277 done = 0;
278 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
279 break;
280 case STATUS:
281 done = 0;
282 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
283 break;
284 case NEXT:
285 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
286 break;
287 case PREV:
288 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
289 break;
290 case LOAD:
291 case NONE:
292 /* action not expected */
293 fatalx("invalid action %u", res->action);
294 break;
297 if (ret != 0)
298 goto end;
300 imsg_flush(ibuf);
302 while (!done) {
303 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
304 fatalx("imsg_read error");
305 if (n == 0)
306 fatalx("pipe closed");
308 while (!done) {
309 if ((n = imsg_get(ibuf, &imsg)) == -1)
310 fatalx("imsg_get error");
311 if (n == 0)
312 break;
314 switch (res->action) {
315 case ADD:
316 done = show_add(&imsg, &ret, &files);
317 break;
318 case SHOW:
319 done = show_complete(&imsg, &ret);
320 break;
321 case STATUS:
322 done = show_status(&imsg, &ret);
323 break;
324 default:
325 done = 1;
326 break;
329 imsg_free(&imsg);
333 end:
334 return ret;
337 int
338 ctl_noarg(struct parse_result *res, int argc, char **argv)
340 if (argc != 1)
341 ctl_usage(res->ctl);
342 return ctlaction(res);
345 int
346 ctl_add(struct parse_result *res, int argc, char **argv)
348 if (argc < 2)
349 ctl_usage(res->ctl);
350 res->files = argv+1;
352 if (pledge("stdio rpath", NULL) == -1)
353 fatal("pledge");
355 return ctlaction(res);
358 int
359 ctl_load(struct parse_result *res, int argc, char **argv)
361 const char *file;
362 char *line = NULL;
363 char path[PATH_MAX];
364 size_t linesize = 0;
365 ssize_t linelen;
367 if (argc < 2)
368 res->file = stdin;
369 else if (argc == 2) {
370 if ((res->file = fopen(argv[1], "r")) == NULL)
371 fatal("open %s", argv[1]);
372 } else
373 ctl_usage(res->ctl);
375 if (pledge("stdio rpath", NULL) == -1)
376 fatal("pledge");
378 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
380 while ((linelen = getline(&line, &linesize, res->file)) != -1) {
381 if (linelen == 0)
382 continue;
383 line[linelen-1] = '\0';
384 file = line;
385 if (file[0] == '>' && file[1] == ' ')
386 file += 2;
387 if (file[0] == ' ' && file[1] == ' ')
388 file += 2;
390 memset(&path, 0, sizeof(path));
391 if (realpath(file, path) == NULL) {
392 log_warn("realpath %s", file);
393 continue;
396 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
397 path, sizeof(path));
400 imsg_flush(ibuf);
401 free(line);
402 if (ferror(res->file))
403 fatal("getline");
404 fclose(res->file);
405 return 0;
408 static int
409 sockconn(void)
411 struct sockaddr_un sun;
412 int sock, saved_errno;
414 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
415 fatal("socket");
417 memset(&sun, 0, sizeof(sun));
418 sun.sun_family = AF_UNIX;
419 strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
420 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
421 saved_errno = errno;
422 close(sock);
423 errno = saved_errno;
424 return -1;
427 return sock;
430 __dead void
431 ctl(int argc, char **argv)
433 int ctl_sock, i = 0;
435 log_init(1, LOG_DAEMON);
436 log_setverbose(verbose);
438 do {
439 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
441 if ((ctl_sock = sockconn()) != -1)
442 break;
443 if (errno != ENOENT && errno != ECONNREFUSED)
444 fatal("connect %s", csock);
446 if (i == 0)
447 spawn_daemon();
449 nanosleep(&ts, NULL);
450 } while (++i < 20);
452 if (ctl_sock == -1)
453 fatalx("failed to connect to the daemon");
455 ibuf = xmalloc(sizeof(*ibuf));
456 imsg_init(ibuf, ctl_sock);
458 optreset = 1;
459 optind = 1;
461 exit(parse(argc, argv));