Blame


1 3baa2617 2022-02-16 op /*
2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 3baa2617 2022-02-16 op *
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.
7 3baa2617 2022-02-16 op *
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.
15 3baa2617 2022-02-16 op */
16 3baa2617 2022-02-16 op
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>
22 3baa2617 2022-02-16 op
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>
34 3baa2617 2022-02-16 op
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"
39 3baa2617 2022-02-16 op
40 3baa2617 2022-02-16 op static struct imsgbuf *ibuf;
41 3baa2617 2022-02-16 op
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 **);
44 8e916714 2022-02-17 op int ctl_show(struct parse_result *, int, char **);
45 14be015f 2022-02-17 op int ctl_load(struct parse_result *, int, char **);
46 a913de21 2022-02-17 op int ctl_jump(struct parse_result *, int, char **);
47 310ef57c 2022-02-19 op int ctl_repeat(struct parse_result *, int, char **);
48 3baa2617 2022-02-16 op
49 3baa2617 2022-02-16 op struct ctl_command ctl_commands[] = {
50 3baa2617 2022-02-16 op { "play", PLAY, ctl_noarg, "" },
51 3baa2617 2022-02-16 op { "pause", PAUSE, ctl_noarg, "" },
52 3baa2617 2022-02-16 op { "toggle", TOGGLE, ctl_noarg, "" },
53 3baa2617 2022-02-16 op { "stop", STOP, ctl_noarg, "" },
54 3baa2617 2022-02-16 op { "restart", RESTART, ctl_noarg, "" },
55 3baa2617 2022-02-16 op { "add", ADD, ctl_add, "files...", 1 },
56 3baa2617 2022-02-16 op { "flush", FLUSH, ctl_noarg, "" },
57 714abf92 2022-02-19 op { "show", SHOW, ctl_show, "[-p]" },
58 bb3f279f 2022-02-16 op { "status", STATUS, ctl_noarg, "" },
59 af27e631 2022-02-17 op { "next", NEXT, ctl_noarg, "" },
60 af27e631 2022-02-17 op { "prev", PREV, ctl_noarg, "" },
61 14be015f 2022-02-17 op { "load", LOAD, ctl_load, "[file]", 1 },
62 a913de21 2022-02-17 op { "jump", JUMP, ctl_jump, "pattern" },
63 310ef57c 2022-02-19 op { "repeat", REPEAT, ctl_repeat, "one|all on|off" },
64 6b47a39f 2022-02-21 op { "monitor", MONITOR, ctl_noarg, "" },
65 3baa2617 2022-02-16 op { NULL },
66 3baa2617 2022-02-16 op };
67 3baa2617 2022-02-16 op
68 3baa2617 2022-02-16 op __dead void
69 3baa2617 2022-02-16 op usage(void)
70 3baa2617 2022-02-16 op {
71 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
72 3baa2617 2022-02-16 op exit(1);
73 3baa2617 2022-02-16 op }
74 3baa2617 2022-02-16 op
75 3baa2617 2022-02-16 op static __dead void
76 3baa2617 2022-02-16 op ctl_usage(struct ctl_command *ctl)
77 3baa2617 2022-02-16 op {
78 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
79 3baa2617 2022-02-16 op ctl->name, ctl->usage);
80 3baa2617 2022-02-16 op exit(1);
81 3baa2617 2022-02-16 op }
82 3baa2617 2022-02-16 op
83 3baa2617 2022-02-16 op static int
84 3baa2617 2022-02-16 op parse(int argc, char **argv)
85 3baa2617 2022-02-16 op {
86 3baa2617 2022-02-16 op struct ctl_command *ctl = NULL;
87 3baa2617 2022-02-16 op struct parse_result res;
88 3baa2617 2022-02-16 op int i, status;
89 3baa2617 2022-02-16 op
90 3baa2617 2022-02-16 op memset(&res, 0, sizeof(res));
91 3baa2617 2022-02-16 op
92 3baa2617 2022-02-16 op for (i = 0; ctl_commands[i].name != NULL; ++i) {
93 3baa2617 2022-02-16 op if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
94 3baa2617 2022-02-16 op == 0) {
95 3baa2617 2022-02-16 op if (ctl != NULL) {
96 3baa2617 2022-02-16 op fprintf(stderr,
97 3baa2617 2022-02-16 op "ambiguous argument: %s\n", argv[0]);
98 3baa2617 2022-02-16 op usage();
99 3baa2617 2022-02-16 op }
100 3baa2617 2022-02-16 op ctl = &ctl_commands[i];
101 3baa2617 2022-02-16 op }
102 3baa2617 2022-02-16 op }
103 3baa2617 2022-02-16 op
104 3baa2617 2022-02-16 op if (ctl == NULL) {
105 3baa2617 2022-02-16 op fprintf(stderr, "unknown argument: %s\n", argv[0]);
106 3baa2617 2022-02-16 op usage();
107 3baa2617 2022-02-16 op }
108 3baa2617 2022-02-16 op
109 3baa2617 2022-02-16 op res.action = ctl->action;
110 3baa2617 2022-02-16 op res.ctl = ctl;
111 3baa2617 2022-02-16 op
112 3baa2617 2022-02-16 op if (!ctl->has_pledge) {
113 3baa2617 2022-02-16 op /* pledge(2) default if command doesn't have its own */
114 3baa2617 2022-02-16 op if (pledge("stdio", NULL) == -1)
115 3baa2617 2022-02-16 op fatal("pledge");
116 3baa2617 2022-02-16 op }
117 3baa2617 2022-02-16 op
118 3baa2617 2022-02-16 op status = ctl->main(&res, argc, argv);
119 3baa2617 2022-02-16 op close(ibuf->fd);
120 3baa2617 2022-02-16 op free(ibuf);
121 3baa2617 2022-02-16 op return status;
122 3baa2617 2022-02-16 op }
123 3baa2617 2022-02-16 op
124 3baa2617 2022-02-16 op static int
125 3baa2617 2022-02-16 op enqueue_tracks(char **files)
126 3baa2617 2022-02-16 op {
127 3baa2617 2022-02-16 op char res[PATH_MAX];
128 3baa2617 2022-02-16 op int enq = 0;
129 3baa2617 2022-02-16 op
130 3baa2617 2022-02-16 op for (; *files != NULL; ++files) {
131 3baa2617 2022-02-16 op memset(&res, 0, sizeof(res));
132 3baa2617 2022-02-16 op if (realpath(*files, res) == NULL) {
133 3baa2617 2022-02-16 op log_warn("realpath %s", *files);
134 3baa2617 2022-02-16 op continue;
135 3baa2617 2022-02-16 op }
136 3baa2617 2022-02-16 op
137 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
138 3baa2617 2022-02-16 op res, sizeof(res));
139 3baa2617 2022-02-16 op enq++;
140 3baa2617 2022-02-16 op }
141 3baa2617 2022-02-16 op
142 3baa2617 2022-02-16 op return enq == 0;
143 a913de21 2022-02-17 op }
144 a913de21 2022-02-17 op
145 a913de21 2022-02-17 op static int
146 a913de21 2022-02-17 op jump_req(const char *arg)
147 a913de21 2022-02-17 op {
148 a913de21 2022-02-17 op char path[PATH_MAX];
149 a913de21 2022-02-17 op
150 a913de21 2022-02-17 op memset(path, 0, sizeof(path));
151 a913de21 2022-02-17 op strlcpy(path, arg, sizeof(path));
152 a913de21 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1, path, sizeof(path));
153 a913de21 2022-02-17 op return 0;
154 3baa2617 2022-02-16 op }
155 3baa2617 2022-02-16 op
156 8ff9231f 2022-02-16 op static void
157 8ff9231f 2022-02-16 op print_error_message(const char *prfx, struct imsg *imsg)
158 8ff9231f 2022-02-16 op {
159 8ff9231f 2022-02-16 op size_t datalen;
160 8ff9231f 2022-02-16 op char *msg;
161 8ff9231f 2022-02-16 op
162 8ff9231f 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
163 8ff9231f 2022-02-16 op if ((msg = calloc(1, datalen)) == NULL)
164 8ff9231f 2022-02-16 op fatal("calloc %zu", datalen);
165 8ff9231f 2022-02-16 op memcpy(msg, imsg->data, datalen);
166 8ff9231f 2022-02-16 op if (datalen == 0 || msg[datalen-1] != '\0')
167 8ff9231f 2022-02-16 op fatalx("malformed error message");
168 8ff9231f 2022-02-16 op
169 8ff9231f 2022-02-16 op log_warnx("%s: %s", prfx, msg);
170 8ff9231f 2022-02-16 op free(msg);
171 8ff9231f 2022-02-16 op }
172 8ff9231f 2022-02-16 op
173 3baa2617 2022-02-16 op static int
174 8ff9231f 2022-02-16 op show_add(struct imsg *imsg, int *ret, char ***files)
175 8ff9231f 2022-02-16 op {
176 8ff9231f 2022-02-16 op if (**files == NULL) {
177 8ff9231f 2022-02-16 op log_warnx("received more replies than file sent");
178 8ff9231f 2022-02-16 op *ret = 1;
179 8ff9231f 2022-02-16 op return 1;
180 8ff9231f 2022-02-16 op }
181 8ff9231f 2022-02-16 op
182 8ff9231f 2022-02-16 op if (imsg->hdr.type == IMSG_CTL_ERR)
183 8ff9231f 2022-02-16 op print_error_message(**files, imsg);
184 8ff9231f 2022-02-16 op else if (imsg->hdr.type == IMSG_CTL_ADD)
185 8ff9231f 2022-02-16 op log_debug("enqueued %s", **files);
186 8ff9231f 2022-02-16 op else
187 8ff9231f 2022-02-16 op fatalx("got invalid message %d", imsg->hdr.type);
188 8ff9231f 2022-02-16 op
189 8ff9231f 2022-02-16 op (*files)++;
190 8ff9231f 2022-02-16 op return (**files) == NULL;
191 8ff9231f 2022-02-16 op }
192 8ff9231f 2022-02-16 op
193 8ff9231f 2022-02-16 op static int
194 8e916714 2022-02-17 op show_complete(struct parse_result *res, struct imsg *imsg, int *ret)
195 3baa2617 2022-02-16 op {
196 63dd8e70 2022-02-17 op struct player_status s;
197 3baa2617 2022-02-16 op size_t datalen;
198 3baa2617 2022-02-16 op
199 8ff9231f 2022-02-16 op if (imsg->hdr.type == IMSG_CTL_ERR) {
200 8ff9231f 2022-02-16 op print_error_message("show failed", imsg);
201 8ff9231f 2022-02-16 op *ret = 1;
202 8ff9231f 2022-02-16 op return 1;
203 8ff9231f 2022-02-16 op }
204 8ff9231f 2022-02-16 op
205 3baa2617 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
206 3baa2617 2022-02-16 op if (datalen == 0)
207 3baa2617 2022-02-16 op return 1;
208 3baa2617 2022-02-16 op
209 63dd8e70 2022-02-17 op if (datalen != sizeof(s))
210 3baa2617 2022-02-16 op fatalx("%s: data size mismatch", __func__);
211 63dd8e70 2022-02-17 op memcpy(&s, imsg->data, sizeof(s));
212 63dd8e70 2022-02-17 op if (s.path[sizeof(s.path)-1] != '\0')
213 3baa2617 2022-02-16 op fatalx("%s: data corrupted?", __func__);
214 3baa2617 2022-02-16 op
215 8e916714 2022-02-17 op if (res->pretty)
216 8e916714 2022-02-17 op printf("%c ", s.status == STATE_PLAYING ? '>' : ' ');
217 8e916714 2022-02-17 op printf("%s\n", s.path);
218 3baa2617 2022-02-16 op return 0;
219 bb3f279f 2022-02-16 op }
220 bb3f279f 2022-02-16 op
221 bb3f279f 2022-02-16 op static int
222 bb3f279f 2022-02-16 op show_status(struct imsg *imsg, int *ret)
223 bb3f279f 2022-02-16 op {
224 bb3f279f 2022-02-16 op struct player_status s;
225 bb3f279f 2022-02-16 op size_t datalen;
226 bb3f279f 2022-02-16 op
227 bb3f279f 2022-02-16 op if (imsg->hdr.type == IMSG_CTL_ERR) {
228 bb3f279f 2022-02-16 op print_error_message("show failed", imsg);
229 bb3f279f 2022-02-16 op *ret = 1;
230 bb3f279f 2022-02-16 op return 1;
231 bb3f279f 2022-02-16 op }
232 bb3f279f 2022-02-16 op
233 bb3f279f 2022-02-16 op if (imsg->hdr.type != IMSG_CTL_STATUS)
234 bb3f279f 2022-02-16 op fatalx("%s: got wrong reply", __func__);
235 bb3f279f 2022-02-16 op
236 bb3f279f 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
237 bb3f279f 2022-02-16 op if (datalen != sizeof(s))
238 bb3f279f 2022-02-16 op fatalx("%s: data size mismatch", __func__);
239 bb3f279f 2022-02-16 op memcpy(&s, imsg->data, sizeof(s));
240 bb3f279f 2022-02-16 op if (s.path[sizeof(s.path)-1] != '\0')
241 bb3f279f 2022-02-16 op fatalx("%s: data corrupted?", __func__);
242 bb3f279f 2022-02-16 op
243 bb3f279f 2022-02-16 op switch (s.status) {
244 bb3f279f 2022-02-16 op case STATE_STOPPED:
245 bb3f279f 2022-02-16 op printf("stopped ");
246 bb3f279f 2022-02-16 op break;
247 bb3f279f 2022-02-16 op case STATE_PLAYING:
248 bb3f279f 2022-02-16 op printf("playing ");
249 bb3f279f 2022-02-16 op break;
250 bb3f279f 2022-02-16 op case STATE_PAUSED:
251 bb3f279f 2022-02-16 op printf("paused ");
252 bb3f279f 2022-02-16 op break;
253 bb3f279f 2022-02-16 op default:
254 bb3f279f 2022-02-16 op printf("unknown ");
255 bb3f279f 2022-02-16 op break;
256 bb3f279f 2022-02-16 op }
257 bb3f279f 2022-02-16 op
258 bb3f279f 2022-02-16 op printf("%s\n", s.path);
259 44398779 2022-02-19 op printf("repeat one %s -- repeat all %s\n",
260 44398779 2022-02-19 op s.rp.repeat_one ? "on" : "off",
261 44398779 2022-02-19 op s.rp.repeat_all ? "on" : "off");
262 bb3f279f 2022-02-16 op return 1;
263 7a427ecd 2022-02-17 op }
264 7a427ecd 2022-02-17 op
265 7a427ecd 2022-02-17 op static int
266 7a427ecd 2022-02-17 op show_load(struct parse_result *res, struct imsg *imsg, int *ret)
267 7a427ecd 2022-02-17 op {
268 ec1fb0c7 2022-02-17 op FILE *f;
269 7a427ecd 2022-02-17 op const char *file;
270 7a427ecd 2022-02-17 op char *line = NULL;
271 7a427ecd 2022-02-17 op char path[PATH_MAX];
272 7a427ecd 2022-02-17 op size_t linesize = 0;
273 7a427ecd 2022-02-17 op ssize_t linelen;
274 7a427ecd 2022-02-17 op int any = 0;
275 7a427ecd 2022-02-17 op
276 7a427ecd 2022-02-17 op if (imsg->hdr.type == IMSG_CTL_ERR) {
277 7a427ecd 2022-02-17 op print_error_message("load failed", imsg);
278 7a427ecd 2022-02-17 op *ret = 1;
279 7a427ecd 2022-02-17 op return 1;
280 7a427ecd 2022-02-17 op }
281 7a427ecd 2022-02-17 op
282 7a427ecd 2022-02-17 op if (imsg->hdr.type == IMSG_CTL_ADD)
283 7a427ecd 2022-02-17 op return 0;
284 7a427ecd 2022-02-17 op
285 7a427ecd 2022-02-17 op if (imsg->hdr.type == IMSG_CTL_COMMIT)
286 7a427ecd 2022-02-17 op return 1;
287 7a427ecd 2022-02-17 op
288 7a427ecd 2022-02-17 op if (imsg->hdr.type != IMSG_CTL_BEGIN)
289 7a427ecd 2022-02-17 op fatalx("got unexpected message %d", imsg->hdr.type);
290 7a427ecd 2022-02-17 op
291 ec1fb0c7 2022-02-17 op if (res->file == NULL)
292 ec1fb0c7 2022-02-17 op f = stdin;
293 ec1fb0c7 2022-02-17 op else if ((f = fopen(res->file, "r")) == NULL) {
294 ec1fb0c7 2022-02-17 op log_warn("can't open %s", res->file);
295 ec1fb0c7 2022-02-17 op *ret = 1;
296 ec1fb0c7 2022-02-17 op return 1;
297 ec1fb0c7 2022-02-17 op }
298 ec1fb0c7 2022-02-17 op
299 ec1fb0c7 2022-02-17 op while ((linelen = getline(&line, &linesize, f)) != -1) {
300 7a427ecd 2022-02-17 op if (linelen == 0)
301 7a427ecd 2022-02-17 op continue;
302 7a427ecd 2022-02-17 op line[linelen-1] = '\0';
303 7a427ecd 2022-02-17 op file = line;
304 7a427ecd 2022-02-17 op if (file[0] == '>' && file[1] == ' ')
305 7a427ecd 2022-02-17 op file += 2;
306 7a427ecd 2022-02-17 op if (file[0] == ' ' && file[1] == ' ')
307 7a427ecd 2022-02-17 op file += 2;
308 7a427ecd 2022-02-17 op
309 7a427ecd 2022-02-17 op memset(&path, 0, sizeof(path));
310 7a427ecd 2022-02-17 op if (realpath(file, path) == NULL) {
311 7a427ecd 2022-02-17 op log_warn("realpath %s", file);
312 7a427ecd 2022-02-17 op continue;
313 7a427ecd 2022-02-17 op }
314 7a427ecd 2022-02-17 op
315 7a427ecd 2022-02-17 op any++;
316 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
317 7a427ecd 2022-02-17 op path, sizeof(path));
318 7a427ecd 2022-02-17 op }
319 7a427ecd 2022-02-17 op
320 7a427ecd 2022-02-17 op free(line);
321 ec1fb0c7 2022-02-17 op if (ferror(f))
322 7a427ecd 2022-02-17 op fatal("getline");
323 ec1fb0c7 2022-02-17 op fclose(f);
324 7a427ecd 2022-02-17 op
325 7a427ecd 2022-02-17 op if (!any) {
326 7a427ecd 2022-02-17 op *ret = 1;
327 7a427ecd 2022-02-17 op return 1;
328 7a427ecd 2022-02-17 op }
329 7a427ecd 2022-02-17 op
330 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
331 7a427ecd 2022-02-17 op NULL, 0);
332 7a427ecd 2022-02-17 op imsg_flush(ibuf);
333 7a427ecd 2022-02-17 op return 0;
334 3baa2617 2022-02-16 op }
335 3baa2617 2022-02-16 op
336 3baa2617 2022-02-16 op static int
337 6b47a39f 2022-02-21 op show_monitor(struct imsg *imsg, int *ret)
338 6b47a39f 2022-02-21 op {
339 6b47a39f 2022-02-21 op int type;
340 6b47a39f 2022-02-21 op
341 6b47a39f 2022-02-21 op if (imsg->hdr.type != IMSG_CTL_MONITOR) {
342 6b47a39f 2022-02-21 op log_warnx("wrong message type received: %d",
343 6b47a39f 2022-02-21 op imsg->hdr.type);
344 6b47a39f 2022-02-21 op *ret = 1;
345 6b47a39f 2022-02-21 op return 1;
346 6b47a39f 2022-02-21 op }
347 6b47a39f 2022-02-21 op
348 6b47a39f 2022-02-21 op if (IMSG_DATA_SIZE(*imsg) != sizeof(type)) {
349 6b47a39f 2022-02-21 op log_warnx("size mismatch");
350 6b47a39f 2022-02-21 op *ret = 1;
351 6b47a39f 2022-02-21 op return 1;
352 6b47a39f 2022-02-21 op }
353 6b47a39f 2022-02-21 op
354 6b47a39f 2022-02-21 op memcpy(&type, imsg->data, sizeof(type));
355 6b47a39f 2022-02-21 op switch (type) {
356 6b47a39f 2022-02-21 op case IMSG_CTL_PLAY:
357 6b47a39f 2022-02-21 op puts("play");
358 6b47a39f 2022-02-21 op break;
359 6b47a39f 2022-02-21 op case IMSG_CTL_TOGGLE_PLAY:
360 6b47a39f 2022-02-21 op puts("toggle");
361 6b47a39f 2022-02-21 op break;
362 6b47a39f 2022-02-21 op case IMSG_CTL_PAUSE:
363 6b47a39f 2022-02-21 op puts("pause");
364 6b47a39f 2022-02-21 op break;
365 6b47a39f 2022-02-21 op case IMSG_CTL_STOP:
366 6b47a39f 2022-02-21 op puts("stop");
367 6b47a39f 2022-02-21 op break;
368 6b47a39f 2022-02-21 op case IMSG_CTL_RESTART:
369 6b47a39f 2022-02-21 op puts("restart");
370 6b47a39f 2022-02-21 op break;
371 6b47a39f 2022-02-21 op case IMSG_CTL_FLUSH:
372 6b47a39f 2022-02-21 op puts("flush");
373 6b47a39f 2022-02-21 op break;
374 6b47a39f 2022-02-21 op case IMSG_CTL_NEXT:
375 6b47a39f 2022-02-21 op puts("next");
376 6b47a39f 2022-02-21 op break;
377 6b47a39f 2022-02-21 op case IMSG_CTL_PREV:
378 6b47a39f 2022-02-21 op puts("prev");
379 6b47a39f 2022-02-21 op break;
380 6b47a39f 2022-02-21 op case IMSG_CTL_JUMP:
381 6b47a39f 2022-02-21 op puts("jump");
382 6b47a39f 2022-02-21 op break;
383 6b47a39f 2022-02-21 op case IMSG_CTL_REPEAT:
384 6b47a39f 2022-02-21 op puts("repeat");
385 6b47a39f 2022-02-21 op break;
386 6b47a39f 2022-02-21 op case IMSG_CTL_ADD:
387 6b47a39f 2022-02-21 op puts("add");
388 6b47a39f 2022-02-21 op break;
389 6b47a39f 2022-02-21 op case IMSG_CTL_COMMIT:
390 6b47a39f 2022-02-21 op puts("load");
391 6b47a39f 2022-02-21 op break;
392 6b47a39f 2022-02-21 op default:
393 6b47a39f 2022-02-21 op puts("unknown");
394 6b47a39f 2022-02-21 op break;
395 6b47a39f 2022-02-21 op }
396 6b47a39f 2022-02-21 op
397 8b51ccee 2022-02-22 op fflush(stdout);
398 6b47a39f 2022-02-21 op return 0;
399 6b47a39f 2022-02-21 op }
400 6b47a39f 2022-02-21 op
401 6b47a39f 2022-02-21 op static int
402 3baa2617 2022-02-16 op ctlaction(struct parse_result *res)
403 3baa2617 2022-02-16 op {
404 3baa2617 2022-02-16 op struct imsg imsg;
405 3baa2617 2022-02-16 op ssize_t n;
406 3baa2617 2022-02-16 op int ret = 0, done = 1;
407 8ff9231f 2022-02-16 op char **files;
408 3baa2617 2022-02-16 op
409 3baa2617 2022-02-16 op switch (res->action) {
410 3baa2617 2022-02-16 op case PLAY:
411 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
412 00f500ff 2022-02-19 op if (verbose) {
413 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
414 00f500ff 2022-02-19 op NULL, 0);
415 00f500ff 2022-02-19 op done = 0;
416 00f500ff 2022-02-19 op }
417 3baa2617 2022-02-16 op break;
418 3baa2617 2022-02-16 op case PAUSE:
419 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
420 3baa2617 2022-02-16 op break;
421 3baa2617 2022-02-16 op case TOGGLE:
422 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
423 00f500ff 2022-02-19 op if (verbose) {
424 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
425 00f500ff 2022-02-19 op NULL, 0);
426 00f500ff 2022-02-19 op done = 0;
427 00f500ff 2022-02-19 op }
428 3baa2617 2022-02-16 op break;
429 3baa2617 2022-02-16 op case STOP:
430 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
431 3baa2617 2022-02-16 op break;
432 3baa2617 2022-02-16 op case RESTART:
433 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
434 00f500ff 2022-02-19 op if (verbose) {
435 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
436 00f500ff 2022-02-19 op NULL, 0);
437 00f500ff 2022-02-19 op done = 0;
438 00f500ff 2022-02-19 op }
439 3baa2617 2022-02-16 op break;
440 3baa2617 2022-02-16 op case ADD:
441 8ff9231f 2022-02-16 op done = 0;
442 8ff9231f 2022-02-16 op files = res->files;
443 3baa2617 2022-02-16 op ret = enqueue_tracks(res->files);
444 3baa2617 2022-02-16 op break;
445 3baa2617 2022-02-16 op case FLUSH:
446 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
447 3baa2617 2022-02-16 op break;
448 3baa2617 2022-02-16 op case SHOW:
449 3baa2617 2022-02-16 op done = 0;
450 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
451 3baa2617 2022-02-16 op break;
452 bb3f279f 2022-02-16 op case STATUS:
453 bb3f279f 2022-02-16 op done = 0;
454 bb3f279f 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
455 af27e631 2022-02-17 op break;
456 af27e631 2022-02-17 op case NEXT:
457 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
458 00f500ff 2022-02-19 op if (verbose) {
459 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
460 00f500ff 2022-02-19 op NULL, 0);
461 00f500ff 2022-02-19 op done = 0;
462 00f500ff 2022-02-19 op }
463 bb3f279f 2022-02-16 op break;
464 af27e631 2022-02-17 op case PREV:
465 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
466 00f500ff 2022-02-19 op if (verbose) {
467 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
468 00f500ff 2022-02-19 op NULL, 0);
469 00f500ff 2022-02-19 op done = 0;
470 00f500ff 2022-02-19 op }
471 af27e631 2022-02-17 op break;
472 14be015f 2022-02-17 op case LOAD:
473 7a427ecd 2022-02-17 op done = 0;
474 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
475 a913de21 2022-02-17 op break;
476 a913de21 2022-02-17 op case JUMP:
477 a913de21 2022-02-17 op done = 0;
478 a913de21 2022-02-17 op ret = jump_req(res->file);
479 7a427ecd 2022-02-17 op break;
480 310ef57c 2022-02-19 op case REPEAT:
481 310ef57c 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
482 310ef57c 2022-02-19 op &res->rep, sizeof(res->rep));
483 310ef57c 2022-02-19 op break;
484 6b47a39f 2022-02-21 op case MONITOR:
485 6b47a39f 2022-02-21 op done = 0;
486 6b47a39f 2022-02-21 op imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
487 6b47a39f 2022-02-21 op NULL, 0);
488 6b47a39f 2022-02-21 op break;
489 3baa2617 2022-02-16 op case NONE:
490 3baa2617 2022-02-16 op /* action not expected */
491 3baa2617 2022-02-16 op fatalx("invalid action %u", res->action);
492 3baa2617 2022-02-16 op break;
493 3baa2617 2022-02-16 op }
494 3baa2617 2022-02-16 op
495 3baa2617 2022-02-16 op if (ret != 0)
496 3baa2617 2022-02-16 op goto end;
497 3baa2617 2022-02-16 op
498 3baa2617 2022-02-16 op imsg_flush(ibuf);
499 3baa2617 2022-02-16 op
500 3baa2617 2022-02-16 op while (!done) {
501 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
502 3baa2617 2022-02-16 op fatalx("imsg_read error");
503 3baa2617 2022-02-16 op if (n == 0)
504 3baa2617 2022-02-16 op fatalx("pipe closed");
505 3baa2617 2022-02-16 op
506 3baa2617 2022-02-16 op while (!done) {
507 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
508 3baa2617 2022-02-16 op fatalx("imsg_get error");
509 3baa2617 2022-02-16 op if (n == 0)
510 3baa2617 2022-02-16 op break;
511 3baa2617 2022-02-16 op
512 3baa2617 2022-02-16 op switch (res->action) {
513 8ff9231f 2022-02-16 op case ADD:
514 8ff9231f 2022-02-16 op done = show_add(&imsg, &ret, &files);
515 8ff9231f 2022-02-16 op break;
516 3baa2617 2022-02-16 op case SHOW:
517 8e916714 2022-02-17 op done = show_complete(res, &imsg, &ret);
518 3baa2617 2022-02-16 op break;
519 146307bd 2022-02-17 op case PLAY:
520 146307bd 2022-02-17 op case TOGGLE:
521 146307bd 2022-02-17 op case RESTART:
522 bb3f279f 2022-02-16 op case STATUS:
523 146307bd 2022-02-17 op case NEXT:
524 146307bd 2022-02-17 op case PREV:
525 a913de21 2022-02-17 op case JUMP:
526 bb3f279f 2022-02-16 op done = show_status(&imsg, &ret);
527 bb3f279f 2022-02-16 op break;
528 7a427ecd 2022-02-17 op case LOAD:
529 7a427ecd 2022-02-17 op done = show_load(res, &imsg, &ret);
530 7a427ecd 2022-02-17 op break;
531 6b47a39f 2022-02-21 op case MONITOR:
532 6b47a39f 2022-02-21 op done = show_monitor(&imsg, &ret);
533 6b47a39f 2022-02-21 op break;
534 3baa2617 2022-02-16 op default:
535 3baa2617 2022-02-16 op done = 1;
536 3baa2617 2022-02-16 op break;
537 3baa2617 2022-02-16 op }
538 3baa2617 2022-02-16 op
539 3baa2617 2022-02-16 op imsg_free(&imsg);
540 3baa2617 2022-02-16 op }
541 3baa2617 2022-02-16 op }
542 3baa2617 2022-02-16 op
543 3baa2617 2022-02-16 op end:
544 3baa2617 2022-02-16 op return ret;
545 3baa2617 2022-02-16 op }
546 3baa2617 2022-02-16 op
547 3baa2617 2022-02-16 op int
548 3baa2617 2022-02-16 op ctl_noarg(struct parse_result *res, int argc, char **argv)
549 3baa2617 2022-02-16 op {
550 3baa2617 2022-02-16 op if (argc != 1)
551 3baa2617 2022-02-16 op ctl_usage(res->ctl);
552 3baa2617 2022-02-16 op return ctlaction(res);
553 3baa2617 2022-02-16 op }
554 3baa2617 2022-02-16 op
555 3baa2617 2022-02-16 op int
556 3baa2617 2022-02-16 op ctl_add(struct parse_result *res, int argc, char **argv)
557 3baa2617 2022-02-16 op {
558 3baa2617 2022-02-16 op if (argc < 2)
559 3baa2617 2022-02-16 op ctl_usage(res->ctl);
560 3baa2617 2022-02-16 op res->files = argv+1;
561 14be015f 2022-02-17 op
562 14be015f 2022-02-17 op if (pledge("stdio rpath", NULL) == -1)
563 14be015f 2022-02-17 op fatal("pledge");
564 14be015f 2022-02-17 op
565 3baa2617 2022-02-16 op return ctlaction(res);
566 3baa2617 2022-02-16 op }
567 14be015f 2022-02-17 op
568 14be015f 2022-02-17 op int
569 8e916714 2022-02-17 op ctl_show(struct parse_result *res, int argc, char **argv)
570 8e916714 2022-02-17 op {
571 8e916714 2022-02-17 op int ch;
572 8e916714 2022-02-17 op
573 8e916714 2022-02-17 op while ((ch = getopt(argc, argv, "p")) != -1) {
574 8e916714 2022-02-17 op switch (ch) {
575 8e916714 2022-02-17 op case 'p':
576 8e916714 2022-02-17 op res->pretty = 1;
577 8e916714 2022-02-17 op break;
578 8e916714 2022-02-17 op default:
579 8e916714 2022-02-17 op ctl_usage(res->ctl);
580 8e916714 2022-02-17 op }
581 8e916714 2022-02-17 op }
582 8e916714 2022-02-17 op
583 8e916714 2022-02-17 op return ctlaction(res);
584 8e916714 2022-02-17 op }
585 8e916714 2022-02-17 op
586 8e916714 2022-02-17 op int
587 14be015f 2022-02-17 op ctl_load(struct parse_result *res, int argc, char **argv)
588 14be015f 2022-02-17 op {
589 14be015f 2022-02-17 op if (argc < 2)
590 ec1fb0c7 2022-02-17 op res->file = NULL;
591 ec1fb0c7 2022-02-17 op else if (argc == 2)
592 ec1fb0c7 2022-02-17 op res->file = argv[1];
593 ec1fb0c7 2022-02-17 op else
594 14be015f 2022-02-17 op ctl_usage(res->ctl);
595 14be015f 2022-02-17 op
596 14be015f 2022-02-17 op if (pledge("stdio rpath", NULL) == -1)
597 14be015f 2022-02-17 op fatal("pledge");
598 14be015f 2022-02-17 op
599 7a427ecd 2022-02-17 op return ctlaction(res);
600 14be015f 2022-02-17 op }
601 14be015f 2022-02-17 op
602 a913de21 2022-02-17 op int
603 a913de21 2022-02-17 op ctl_jump(struct parse_result *res, int argc, char **argv)
604 a913de21 2022-02-17 op {
605 a913de21 2022-02-17 op int ch;
606 a913de21 2022-02-17 op
607 a913de21 2022-02-17 op while ((ch = getopt(argc, argv, "")) != -1)
608 a913de21 2022-02-17 op ctl_usage(res->ctl);
609 a913de21 2022-02-17 op argc -= optind;
610 a913de21 2022-02-17 op argv += optind;
611 a913de21 2022-02-17 op
612 a913de21 2022-02-17 op if (argc != 1)
613 a913de21 2022-02-17 op ctl_usage(res->ctl);
614 a913de21 2022-02-17 op
615 a913de21 2022-02-17 op res->file = argv[0];
616 310ef57c 2022-02-19 op return ctlaction(res);
617 310ef57c 2022-02-19 op }
618 310ef57c 2022-02-19 op
619 310ef57c 2022-02-19 op int
620 310ef57c 2022-02-19 op ctl_repeat(struct parse_result *res, int argc, char **argv)
621 310ef57c 2022-02-19 op {
622 310ef57c 2022-02-19 op int ch, b;
623 310ef57c 2022-02-19 op
624 310ef57c 2022-02-19 op while ((ch = getopt(argc, argv, "")) != -1)
625 310ef57c 2022-02-19 op ctl_usage(res->ctl);
626 310ef57c 2022-02-19 op argc -= optind;
627 310ef57c 2022-02-19 op argv += optind;
628 310ef57c 2022-02-19 op
629 310ef57c 2022-02-19 op if (argc != 2)
630 310ef57c 2022-02-19 op ctl_usage(res->ctl);
631 310ef57c 2022-02-19 op
632 310ef57c 2022-02-19 op if (!strcmp(argv[1], "on"))
633 310ef57c 2022-02-19 op b = 1;
634 310ef57c 2022-02-19 op else if (!strcmp(argv[1], "off"))
635 310ef57c 2022-02-19 op b = 0;
636 310ef57c 2022-02-19 op else
637 310ef57c 2022-02-19 op ctl_usage(res->ctl);
638 310ef57c 2022-02-19 op
639 310ef57c 2022-02-19 op res->rep.repeat_one = -1;
640 310ef57c 2022-02-19 op res->rep.repeat_all = -1;
641 310ef57c 2022-02-19 op if (!strcmp(argv[0], "one"))
642 310ef57c 2022-02-19 op res->rep.repeat_one = b;
643 310ef57c 2022-02-19 op else if (!strcmp(argv[0], "all"))
644 310ef57c 2022-02-19 op res->rep.repeat_all = b;
645 310ef57c 2022-02-19 op else
646 310ef57c 2022-02-19 op ctl_usage(res->ctl);
647 310ef57c 2022-02-19 op
648 a913de21 2022-02-17 op return ctlaction(res);
649 a913de21 2022-02-17 op }
650 a913de21 2022-02-17 op
651 fea541a8 2022-02-16 op static int
652 fea541a8 2022-02-16 op sockconn(void)
653 3baa2617 2022-02-16 op {
654 fea541a8 2022-02-16 op struct sockaddr_un sun;
655 fea541a8 2022-02-16 op int sock, saved_errno;
656 3baa2617 2022-02-16 op
657 fea541a8 2022-02-16 op if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
658 3baa2617 2022-02-16 op fatal("socket");
659 3baa2617 2022-02-16 op
660 3baa2617 2022-02-16 op memset(&sun, 0, sizeof(sun));
661 3baa2617 2022-02-16 op sun.sun_family = AF_UNIX;
662 3baa2617 2022-02-16 op strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
663 fea541a8 2022-02-16 op if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
664 fea541a8 2022-02-16 op saved_errno = errno;
665 fea541a8 2022-02-16 op close(sock);
666 fea541a8 2022-02-16 op errno = saved_errno;
667 fea541a8 2022-02-16 op return -1;
668 fea541a8 2022-02-16 op }
669 3baa2617 2022-02-16 op
670 fea541a8 2022-02-16 op return sock;
671 fea541a8 2022-02-16 op }
672 fea541a8 2022-02-16 op
673 fea541a8 2022-02-16 op __dead void
674 fea541a8 2022-02-16 op ctl(int argc, char **argv)
675 fea541a8 2022-02-16 op {
676 fea541a8 2022-02-16 op int ctl_sock, i = 0;
677 3baa2617 2022-02-16 op
678 fea541a8 2022-02-16 op log_init(1, LOG_DAEMON);
679 fea541a8 2022-02-16 op log_setverbose(verbose);
680 fea541a8 2022-02-16 op
681 fea541a8 2022-02-16 op do {
682 fea541a8 2022-02-16 op struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
683 fea541a8 2022-02-16 op
684 fea541a8 2022-02-16 op if ((ctl_sock = sockconn()) != -1)
685 fea541a8 2022-02-16 op break;
686 fea541a8 2022-02-16 op if (errno != ENOENT && errno != ECONNREFUSED)
687 fea541a8 2022-02-16 op fatal("connect %s", csock);
688 fea541a8 2022-02-16 op
689 fea541a8 2022-02-16 op if (i == 0)
690 fea541a8 2022-02-16 op spawn_daemon();
691 fea541a8 2022-02-16 op
692 fea541a8 2022-02-16 op nanosleep(&ts, NULL);
693 fea541a8 2022-02-16 op } while (++i < 20);
694 fea541a8 2022-02-16 op
695 fea541a8 2022-02-16 op if (ctl_sock == -1)
696 fea541a8 2022-02-16 op fatalx("failed to connect to the daemon");
697 fea541a8 2022-02-16 op
698 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
699 3baa2617 2022-02-16 op imsg_init(ibuf, ctl_sock);
700 3baa2617 2022-02-16 op
701 3baa2617 2022-02-16 op optreset = 1;
702 3baa2617 2022-02-16 op optind = 1;
703 3baa2617 2022-02-16 op
704 3baa2617 2022-02-16 op exit(parse(argc, argv));
705 3baa2617 2022-02-16 op }