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 **);
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, "" },
57 { NULL },
58 };
60 __dead void
61 usage(void)
62 {
63 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
64 fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
65 exit(1);
66 }
68 static __dead void
69 ctl_usage(struct ctl_command *ctl)
70 {
71 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
72 ctl->name, ctl->usage);
73 exit(1);
74 }
76 static int
77 parse(int argc, char **argv)
78 {
79 struct ctl_command *ctl = NULL;
80 struct parse_result res;
81 int i, status;
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]))
87 == 0) {
88 if (ctl != NULL) {
89 fprintf(stderr,
90 "ambiguous argument: %s\n", argv[0]);
91 usage();
92 }
93 ctl = &ctl_commands[i];
94 }
95 }
97 if (ctl == NULL) {
98 fprintf(stderr, "unknown argument: %s\n", argv[0]);
99 usage();
102 res.action = ctl->action;
103 res.ctl = ctl;
105 if (!ctl->has_pledge) {
106 /* pledge(2) default if command doesn't have its own */
107 if (pledge("stdio", NULL) == -1)
108 fatal("pledge");
111 status = ctl->main(&res, argc, argv);
112 close(ibuf->fd);
113 free(ibuf);
114 return status;
117 static int
118 enqueue_tracks(char **files)
120 char res[PATH_MAX];
121 int enq = 0;
123 for (; *files != NULL; ++files) {
124 memset(&res, 0, sizeof(res));
125 if (realpath(*files, res) == NULL) {
126 log_warn("realpath %s", *files);
127 continue;
130 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
131 res, sizeof(res));
132 enq++;
135 return enq == 0;
138 static void
139 print_error_message(const char *prfx, struct imsg *imsg)
141 size_t datalen;
142 char *msg;
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);
152 free(msg);
155 static int
156 show_add(struct imsg *imsg, int *ret, char ***files)
158 if (**files == NULL) {
159 log_warnx("received more replies than file sent");
160 *ret = 1;
161 return 1;
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);
168 else
169 fatalx("got invalid message %d", imsg->hdr.type);
171 (*files)++;
172 return (**files) == NULL;
175 static int
176 show_complete(struct imsg *imsg, int *ret)
178 struct player_status s;
179 size_t datalen;
181 if (imsg->hdr.type == IMSG_CTL_ERR) {
182 print_error_message("show failed", imsg);
183 *ret = 1;
184 return 1;
187 datalen = IMSG_DATA_SIZE(*imsg);
188 if (datalen == 0)
189 return 1;
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);
198 return 0;
201 static int
202 show_status(struct imsg *imsg, int *ret)
204 struct player_status s;
205 size_t datalen;
207 if (imsg->hdr.type == IMSG_CTL_ERR) {
208 print_error_message("show failed", imsg);
209 *ret = 1;
210 return 1;
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__);
223 switch (s.status) {
224 case STATE_STOPPED:
225 printf("stopped ");
226 break;
227 case STATE_PLAYING:
228 printf("playing ");
229 break;
230 case STATE_PAUSED:
231 printf("paused ");
232 break;
233 default:
234 printf("unknown ");
235 break;
238 printf("%s\n", s.path);
239 return 1;
242 static int
243 ctlaction(struct parse_result *res)
245 struct imsg imsg;
246 ssize_t n;
247 int ret = 0, done = 1;
248 char **files;
250 switch (res->action) {
251 case PLAY:
252 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
253 break;
254 case PAUSE:
255 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
256 break;
257 case TOGGLE:
258 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
259 break;
260 case STOP:
261 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
262 break;
263 case RESTART:
264 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
265 break;
266 case ADD:
267 done = 0;
268 files = res->files;
269 ret = enqueue_tracks(res->files);
270 break;
271 case FLUSH:
272 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
273 break;
274 case SHOW:
275 done = 0;
276 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
277 break;
278 case STATUS:
279 done = 0;
280 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
281 break;
282 case NEXT:
283 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
284 break;
285 case PREV:
286 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
287 break;
288 case NONE:
289 /* action not expected */
290 fatalx("invalid action %u", res->action);
291 break;
294 if (ret != 0)
295 goto end;
297 imsg_flush(ibuf);
299 while (!done) {
300 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
301 fatalx("imsg_read error");
302 if (n == 0)
303 fatalx("pipe closed");
305 while (!done) {
306 if ((n = imsg_get(ibuf, &imsg)) == -1)
307 fatalx("imsg_get error");
308 if (n == 0)
309 break;
311 switch (res->action) {
312 case ADD:
313 done = show_add(&imsg, &ret, &files);
314 break;
315 case SHOW:
316 done = show_complete(&imsg, &ret);
317 break;
318 case STATUS:
319 done = show_status(&imsg, &ret);
320 break;
321 default:
322 done = 1;
323 break;
326 imsg_free(&imsg);
330 end:
331 return ret;
334 int
335 ctl_noarg(struct parse_result *res, int argc, char **argv)
337 if (argc != 1)
338 ctl_usage(res->ctl);
339 return ctlaction(res);
342 int
343 ctl_add(struct parse_result *res, int argc, char **argv)
345 if (argc < 2)
346 ctl_usage(res->ctl);
347 res->files = argv+1;
348 return ctlaction(res);
351 static int
352 sockconn(void)
354 struct sockaddr_un sun;
355 int sock, saved_errno;
357 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
358 fatal("socket");
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) {
364 saved_errno = errno;
365 close(sock);
366 errno = saved_errno;
367 return -1;
370 return sock;
373 __dead void
374 ctl(int argc, char **argv)
376 int ctl_sock, i = 0;
378 log_init(1, LOG_DAEMON);
379 log_setverbose(verbose);
381 do {
382 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
384 if ((ctl_sock = sockconn()) != -1)
385 break;
386 if (errno != ENOENT && errno != ECONNREFUSED)
387 fatal("connect %s", csock);
389 if (i == 0)
390 spawn_daemon();
392 nanosleep(&ts, NULL);
393 } while (++i < 20);
395 if (ctl_sock == -1)
396 fatalx("failed to connect to the daemon");
398 ibuf = xmalloc(sizeof(*ibuf));
399 imsg_init(ibuf, ctl_sock);
401 optreset = 1;
402 optind = 1;
404 exit(parse(argc, argv));