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 "xmalloc.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 { NULL },
53 };
55 __dead void
56 usage(void)
57 {
58 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
59 fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
60 exit(1);
61 }
63 static __dead void
64 ctl_usage(struct ctl_command *ctl)
65 {
66 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
67 ctl->name, ctl->usage);
68 exit(1);
69 }
71 static int
72 parse(int argc, char **argv)
73 {
74 struct ctl_command *ctl = NULL;
75 struct parse_result res;
76 int i, status;
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]))
82 == 0) {
83 if (ctl != NULL) {
84 fprintf(stderr,
85 "ambiguous argument: %s\n", argv[0]);
86 usage();
87 }
88 ctl = &ctl_commands[i];
89 }
90 }
92 if (ctl == NULL) {
93 fprintf(stderr, "unknown argument: %s\n", argv[0]);
94 usage();
95 }
97 res.action = ctl->action;
98 res.ctl = ctl;
100 if (!ctl->has_pledge) {
101 /* pledge(2) default if command doesn't have its own */
102 if (pledge("stdio", NULL) == -1)
103 fatal("pledge");
106 status = ctl->main(&res, argc, argv);
107 close(ibuf->fd);
108 free(ibuf);
109 return status;
112 static int
113 enqueue_tracks(char **files)
115 char res[PATH_MAX];
116 int enq = 0;
118 for (; *files != NULL; ++files) {
119 memset(&res, 0, sizeof(res));
120 if (realpath(*files, res) == NULL) {
121 log_warn("realpath %s", *files);
122 continue;
125 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
126 res, sizeof(res));
127 enq++;
130 return enq == 0;
133 static int
134 show_complete(struct imsg *imsg, int *ret)
136 size_t datalen;
137 char path[PATH_MAX];
139 datalen = IMSG_DATA_SIZE(*imsg);
140 if (datalen == 0)
141 return 1;
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);
150 return 0;
153 static int
154 ctlaction(struct parse_result *res)
156 struct imsg imsg;
157 ssize_t n;
158 int ret = 0, done = 1;
160 switch (res->action) {
161 case PLAY:
162 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
163 break;
164 case PAUSE:
165 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
166 break;
167 case TOGGLE:
168 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
169 break;
170 case STOP:
171 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
172 break;
173 case RESTART:
174 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
175 break;
176 case ADD:
177 ret = enqueue_tracks(res->files);
178 break;
179 case FLUSH:
180 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
181 break;
182 case SHOW:
183 done = 0;
184 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
185 break;
186 case NONE:
187 /* action not expected */
188 fatalx("invalid action %u", res->action);
189 break;
192 if (ret != 0)
193 goto end;
195 imsg_flush(ibuf);
197 while (!done) {
198 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
199 fatalx("imsg_read error");
200 if (n == 0)
201 fatalx("pipe closed");
203 while (!done) {
204 if ((n = imsg_get(ibuf, &imsg)) == -1)
205 fatalx("imsg_get error");
206 if (n == 0)
207 break;
209 switch (res->action) {
210 case SHOW:
211 done = show_complete(&imsg, &ret);
212 break;
213 default:
214 done = 1;
215 break;
218 imsg_free(&imsg);
222 end:
223 return ret;
226 int
227 ctl_noarg(struct parse_result *res, int argc, char **argv)
229 if (argc != 1)
230 ctl_usage(res->ctl);
231 return ctlaction(res);
234 int
235 ctl_add(struct parse_result *res, int argc, char **argv)
237 if (argc < 2)
238 ctl_usage(res->ctl);
239 res->files = argv+1;
240 return ctlaction(res);
243 __dead void
244 ctl(int argc, char **argv)
246 struct sockaddr_un sun;
247 int ctl_sock;
249 log_init(1, LOG_DAEMON);
250 log_setverbose(verbose);
252 if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
253 fatal("socket");
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);
265 optreset = 1;
266 optind = 1;
268 exit(parse(argc, argv));