Blame


1 3baa2617 2022-02-16 op /*
2 578f8d0c 2023-05-02 op * Copyright (c) 2022, 2023 Omar Polo <op@omarpolo.com>
3 96233476 2022-06-09 op * Copyright (c) 2015 Theo de Raadt <deraadt@openbsd.org>
4 3baa2617 2022-02-16 op *
5 3baa2617 2022-02-16 op * Permission to use, copy, modify, and distribute this software for any
6 3baa2617 2022-02-16 op * purpose with or without fee is hereby granted, provided that the above
7 3baa2617 2022-02-16 op * copyright notice and this permission notice appear in all copies.
8 3baa2617 2022-02-16 op *
9 3baa2617 2022-02-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 3baa2617 2022-02-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 3baa2617 2022-02-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 3baa2617 2022-02-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 3baa2617 2022-02-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 3baa2617 2022-02-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 3baa2617 2022-02-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 3baa2617 2022-02-16 op */
17 3baa2617 2022-02-16 op
18 f36fd90a 2022-07-09 op #include "config.h"
19 f36fd90a 2022-07-09 op
20 3baa2617 2022-02-16 op #include <sys/socket.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 1d673950 2022-03-03 op #include <fcntl.h>
25 3baa2617 2022-02-16 op #include <limits.h>
26 3baa2617 2022-02-16 op #include <stdio.h>
27 3baa2617 2022-02-16 op #include <stdlib.h>
28 3baa2617 2022-02-16 op #include <stdint.h>
29 3baa2617 2022-02-16 op #include <string.h>
30 3baa2617 2022-02-16 op #include <syslog.h>
31 0a53bd62 2023-08-16 op #include <time.h>
32 3baa2617 2022-02-16 op #include <unistd.h>
33 3baa2617 2022-02-16 op
34 3baa2617 2022-02-16 op #include "amused.h"
35 3baa2617 2022-02-16 op #include "log.h"
36 bb3f279f 2022-02-16 op #include "playlist.h"
37 3baa2617 2022-02-16 op #include "xmalloc.h"
38 3baa2617 2022-02-16 op
39 50e0da0e 2024-01-21 op static struct imsgbuf *imsgbuf;
40 96233476 2022-06-09 op char cwd[PATH_MAX];
41 3baa2617 2022-02-16 op
42 2ddcfa40 2022-07-08 op static int ctl_noarg(struct parse_result *, int, char **);
43 2ddcfa40 2022-07-08 op static int ctl_add(struct parse_result *, int, char **);
44 2ddcfa40 2022-07-08 op static int ctl_show(struct parse_result *, int, char **);
45 2ddcfa40 2022-07-08 op static int ctl_load(struct parse_result *, int, char **);
46 2ddcfa40 2022-07-08 op static int ctl_jump(struct parse_result *, int, char **);
47 2ddcfa40 2022-07-08 op static int ctl_repeat(struct parse_result *, int, char **);
48 9fb94242 2022-07-13 op static int ctl_consume(struct parse_result *, int, char **);
49 2ddcfa40 2022-07-08 op static int ctl_monitor(struct parse_result *, int, char **);
50 e5d4e9f7 2022-07-09 op static int ctl_seek(struct parse_result *, int, char **);
51 68e4f29e 2022-07-10 op static int ctl_status(struct parse_result *, int, char **);
52 3baa2617 2022-02-16 op
53 3baa2617 2022-02-16 op struct ctl_command ctl_commands[] = {
54 4b42d3a3 2022-07-13 op { "add", ADD, ctl_add, "files..."},
55 9fb94242 2022-07-13 op { "consume", MODE, ctl_consume, "one|all"},
56 4b42d3a3 2022-07-13 op { "flush", FLUSH, ctl_noarg, ""},
57 4b42d3a3 2022-07-13 op { "jump", JUMP, ctl_jump, "pattern"},
58 4b42d3a3 2022-07-13 op { "load", LOAD, ctl_load, "[file]"},
59 4b42d3a3 2022-07-13 op { "monitor", MONITOR, ctl_monitor, "[events]"},
60 4b42d3a3 2022-07-13 op { "next", NEXT, ctl_noarg, ""},
61 4b42d3a3 2022-07-13 op { "pause", PAUSE, ctl_noarg, ""},
62 4b42d3a3 2022-07-13 op { "play", PLAY, ctl_noarg, ""},
63 4b42d3a3 2022-07-13 op { "prev", PREV, ctl_noarg, ""},
64 9fb94242 2022-07-13 op { "repeat", MODE, ctl_repeat, "one|all on|off"},
65 4b42d3a3 2022-07-13 op { "restart", RESTART, ctl_noarg, ""},
66 4b42d3a3 2022-07-13 op { "seek", SEEK, ctl_seek, "[+-]time[%]"},
67 4b42d3a3 2022-07-13 op { "show", SHOW, ctl_show, "[-p]"},
68 4b42d3a3 2022-07-13 op { "status", STATUS, ctl_status, "[-f fmt]"},
69 4b42d3a3 2022-07-13 op { "stop", STOP, ctl_noarg, ""},
70 4b42d3a3 2022-07-13 op { "toggle", TOGGLE, ctl_noarg, ""},
71 4b42d3a3 2022-07-13 op { NULL, 0, NULL, NULL},
72 3baa2617 2022-02-16 op };
73 3baa2617 2022-02-16 op
74 3baa2617 2022-02-16 op __dead void
75 3baa2617 2022-02-16 op usage(void)
76 3baa2617 2022-02-16 op {
77 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
78 3baa2617 2022-02-16 op exit(1);
79 3baa2617 2022-02-16 op }
80 3baa2617 2022-02-16 op
81 3baa2617 2022-02-16 op static __dead void
82 3baa2617 2022-02-16 op ctl_usage(struct ctl_command *ctl)
83 3baa2617 2022-02-16 op {
84 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
85 3baa2617 2022-02-16 op ctl->name, ctl->usage);
86 3baa2617 2022-02-16 op exit(1);
87 3baa2617 2022-02-16 op }
88 3baa2617 2022-02-16 op
89 96233476 2022-06-09 op /* based on canonpath from kern_pledge.c */
90 3baa2617 2022-02-16 op static int
91 96233476 2022-06-09 op canonpath(const char *input, char *buf, size_t bufsize)
92 96233476 2022-06-09 op {
93 96233476 2022-06-09 op const char *p;
94 96233476 2022-06-09 op char *q, path[PATH_MAX];
95 15decef2 2023-02-08 op int r;
96 96233476 2022-06-09 op
97 96233476 2022-06-09 op if (input[0] != '/') {
98 15decef2 2023-02-08 op r = snprintf(path, sizeof(path), "%s/%s", cwd, input);
99 15decef2 2023-02-08 op if (r < 0 || (size_t)r >= sizeof(path)) {
100 96233476 2022-06-09 op errno = ENAMETOOLONG;
101 96233476 2022-06-09 op return -1;
102 96233476 2022-06-09 op }
103 96233476 2022-06-09 op input = path;
104 96233476 2022-06-09 op }
105 96233476 2022-06-09 op
106 96233476 2022-06-09 op p = input;
107 96233476 2022-06-09 op q = buf;
108 96233476 2022-06-09 op while (*p && (q - buf < bufsize)) {
109 96233476 2022-06-09 op if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
110 96233476 2022-06-09 op p += 1;
111 96233476 2022-06-09 op
112 96233476 2022-06-09 op } else if (p[0] == '/' && p[1] == '.' &&
113 96233476 2022-06-09 op (p[2] == '/' || p[2] == '\0')) {
114 96233476 2022-06-09 op p += 2;
115 96233476 2022-06-09 op
116 96233476 2022-06-09 op } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
117 96233476 2022-06-09 op (p[3] == '/' || p[3] == '\0')) {
118 96233476 2022-06-09 op p += 3;
119 96233476 2022-06-09 op if (q != buf) /* "/../" at start of buf */
120 96233476 2022-06-09 op while (*--q != '/')
121 96233476 2022-06-09 op continue;
122 96233476 2022-06-09 op
123 96233476 2022-06-09 op } else {
124 96233476 2022-06-09 op *q++ = *p++;
125 96233476 2022-06-09 op }
126 96233476 2022-06-09 op }
127 96233476 2022-06-09 op if ((*p == '\0') && (q - buf < bufsize)) {
128 96233476 2022-06-09 op *q = 0;
129 96233476 2022-06-09 op return 0;
130 96233476 2022-06-09 op } else {
131 96233476 2022-06-09 op errno = ENAMETOOLONG;
132 96233476 2022-06-09 op return -1;
133 96233476 2022-06-09 op }
134 96233476 2022-06-09 op }
135 96233476 2022-06-09 op
136 96233476 2022-06-09 op static int
137 6576093a 2022-07-11 op parse(struct parse_result *res, int argc, char **argv)
138 3baa2617 2022-02-16 op {
139 3baa2617 2022-02-16 op struct ctl_command *ctl = NULL;
140 1d673950 2022-03-03 op const char *argv0;
141 3baa2617 2022-02-16 op int i, status;
142 3baa2617 2022-02-16 op
143 1d673950 2022-03-03 op if ((argv0 = argv[0]) == NULL)
144 1d673950 2022-03-03 op argv0 = "status";
145 1d673950 2022-03-03 op
146 3baa2617 2022-02-16 op for (i = 0; ctl_commands[i].name != NULL; ++i) {
147 1d673950 2022-03-03 op if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
148 3baa2617 2022-02-16 op == 0) {
149 3baa2617 2022-02-16 op if (ctl != NULL) {
150 3baa2617 2022-02-16 op fprintf(stderr,
151 1d673950 2022-03-03 op "ambiguous argument: %s\n", argv0);
152 3baa2617 2022-02-16 op usage();
153 3baa2617 2022-02-16 op }
154 3baa2617 2022-02-16 op ctl = &ctl_commands[i];
155 3baa2617 2022-02-16 op }
156 3baa2617 2022-02-16 op }
157 3baa2617 2022-02-16 op
158 3baa2617 2022-02-16 op if (ctl == NULL) {
159 3baa2617 2022-02-16 op fprintf(stderr, "unknown argument: %s\n", argv[0]);
160 3baa2617 2022-02-16 op usage();
161 3baa2617 2022-02-16 op }
162 3baa2617 2022-02-16 op
163 6576093a 2022-07-11 op res->action = ctl->action;
164 6576093a 2022-07-11 op res->ctl = ctl;
165 3baa2617 2022-02-16 op
166 6576093a 2022-07-11 op status = ctl->main(res, argc, argv);
167 50e0da0e 2024-01-21 op close(imsgbuf->fd);
168 50e0da0e 2024-01-21 op free(imsgbuf);
169 3baa2617 2022-02-16 op return status;
170 3baa2617 2022-02-16 op }
171 3baa2617 2022-02-16 op
172 3baa2617 2022-02-16 op static int
173 fad0fb69 2022-05-28 op load_files(struct parse_result *res, int *ret)
174 7a427ecd 2022-02-17 op {
175 7a427ecd 2022-02-17 op const char *file;
176 7a427ecd 2022-02-17 op char *line = NULL;
177 96233476 2022-06-09 op char path[PATH_MAX];
178 96233476 2022-06-09 op size_t linesize = 0, i = 0;
179 3af93963 2022-03-02 op ssize_t linelen, curr = -1;
180 7a427ecd 2022-02-17 op
181 4b42d3a3 2022-07-13 op while ((linelen = getline(&line, &linesize, res->fp)) != -1) {
182 7a427ecd 2022-02-17 op if (linelen == 0)
183 7a427ecd 2022-02-17 op continue;
184 7a427ecd 2022-02-17 op line[linelen-1] = '\0';
185 7a427ecd 2022-02-17 op file = line;
186 1dd3b054 2022-03-25 op if (!strncmp(file, "> ", 2)) {
187 7a427ecd 2022-02-17 op file += 2;
188 3af93963 2022-03-02 op curr = i;
189 1dd3b054 2022-03-25 op } else if (!strncmp(file, " ", 2))
190 7a427ecd 2022-02-17 op file += 2;
191 1dd3b054 2022-03-25 op
192 18223c28 2022-06-13 op memset(path, 0, sizeof(path));
193 96233476 2022-06-09 op if (canonpath(file, path, sizeof(path)) == -1) {
194 f1beb69a 2022-06-13 op log_warn("canonpath %s", file);
195 7a427ecd 2022-02-17 op continue;
196 7a427ecd 2022-02-17 op }
197 7a427ecd 2022-02-17 op
198 3af93963 2022-03-02 op i++;
199 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_ADD, 0, 0, -1,
200 7a427ecd 2022-02-17 op path, sizeof(path));
201 7a427ecd 2022-02-17 op }
202 7a427ecd 2022-02-17 op
203 7a427ecd 2022-02-17 op free(line);
204 4b42d3a3 2022-07-13 op if (ferror(res->fp))
205 7a427ecd 2022-02-17 op fatal("getline");
206 4b42d3a3 2022-07-13 op fclose(res->fp);
207 4b42d3a3 2022-07-13 op res->fp = NULL;
208 7a427ecd 2022-02-17 op
209 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_COMMIT, 0, 0, -1,
210 3af93963 2022-03-02 op &curr, sizeof(curr));
211 50e0da0e 2024-01-21 op imsg_flush(imsgbuf);
212 7a427ecd 2022-02-17 op return 0;
213 3baa2617 2022-02-16 op }
214 3baa2617 2022-02-16 op
215 fad0fb69 2022-05-28 op static const char *
216 fad0fb69 2022-05-28 op imsg_strerror(struct imsg *imsg)
217 6b47a39f 2022-02-21 op {
218 9307af9e 2024-01-21 op struct ibuf ibuf;
219 fad0fb69 2022-05-28 op size_t datalen;
220 fad0fb69 2022-05-28 op const char *msg;
221 6b47a39f 2022-02-21 op
222 9307af9e 2024-01-21 op if (imsg_get_ibuf(imsg, &ibuf) == -1 ||
223 9307af9e 2024-01-21 op (datalen = ibuf_size(&ibuf)) == 0 ||
224 9307af9e 2024-01-21 op (msg = ibuf_data(&ibuf)) == NULL ||
225 9307af9e 2024-01-21 op msg[datalen - 1] != '\0')
226 fad0fb69 2022-05-28 op fatalx("malformed error message");
227 6b47a39f 2022-02-21 op
228 fad0fb69 2022-05-28 op return msg;
229 fad0fb69 2022-05-28 op }
230 90122a37 2022-05-14 op
231 fad0fb69 2022-05-28 op static const char *
232 232e682b 2022-07-12 op event_name(int type)
233 fad0fb69 2022-05-28 op {
234 6b47a39f 2022-02-21 op switch (type) {
235 6b47a39f 2022-02-21 op case IMSG_CTL_PLAY:
236 fad0fb69 2022-05-28 op return "play";
237 6b47a39f 2022-02-21 op case IMSG_CTL_PAUSE:
238 fad0fb69 2022-05-28 op return "pause";
239 6b47a39f 2022-02-21 op case IMSG_CTL_STOP:
240 fad0fb69 2022-05-28 op return "stop";
241 6b47a39f 2022-02-21 op case IMSG_CTL_NEXT:
242 fad0fb69 2022-05-28 op return "next";
243 6b47a39f 2022-02-21 op case IMSG_CTL_PREV:
244 fad0fb69 2022-05-28 op return "prev";
245 6b47a39f 2022-02-21 op case IMSG_CTL_JUMP:
246 fad0fb69 2022-05-28 op return "jump";
247 6b47a39f 2022-02-21 op case IMSG_CTL_ADD:
248 fad0fb69 2022-05-28 op return "add";
249 6b47a39f 2022-02-21 op case IMSG_CTL_COMMIT:
250 fad0fb69 2022-05-28 op return "load";
251 9fb94242 2022-07-13 op case IMSG_CTL_MODE:
252 9fb94242 2022-07-13 op return "mode";
253 15aecb89 2022-07-12 op case IMSG_CTL_SEEK:
254 15aecb89 2022-07-12 op return "seek";
255 6b47a39f 2022-02-21 op default:
256 fad0fb69 2022-05-28 op return "unknown";
257 6b47a39f 2022-02-21 op }
258 bdc8c4e0 2022-07-08 op }
259 bdc8c4e0 2022-07-08 op
260 bdc8c4e0 2022-07-08 op static void
261 68e4f29e 2022-07-10 op print_time(const char *label, int64_t seconds, const char *suffx)
262 bdc8c4e0 2022-07-08 op {
263 bdc8c4e0 2022-07-08 op int hours, minutes;
264 bdc8c4e0 2022-07-08 op
265 bdc8c4e0 2022-07-08 op if (seconds < 0)
266 bdc8c4e0 2022-07-08 op seconds = 0;
267 bdc8c4e0 2022-07-08 op
268 bdc8c4e0 2022-07-08 op hours = seconds / 3600;
269 bdc8c4e0 2022-07-08 op seconds -= hours * 3600;
270 bdc8c4e0 2022-07-08 op
271 bdc8c4e0 2022-07-08 op minutes = seconds / 60;
272 bdc8c4e0 2022-07-08 op seconds -= minutes * 60;
273 bdc8c4e0 2022-07-08 op
274 68e4f29e 2022-07-10 op printf("%s", label);
275 bdc8c4e0 2022-07-08 op if (hours != 0)
276 bdc8c4e0 2022-07-08 op printf("%02d:", hours);
277 68e4f29e 2022-07-10 op printf("%02d:%02lld%s", minutes, (long long)seconds, suffx);
278 68e4f29e 2022-07-10 op }
279 68e4f29e 2022-07-10 op
280 68e4f29e 2022-07-10 op static void
281 68e4f29e 2022-07-10 op print_status(struct player_status *ps, const char *spec)
282 68e4f29e 2022-07-10 op {
283 68e4f29e 2022-07-10 op const char *status;
284 68e4f29e 2022-07-10 op double percent;
285 68e4f29e 2022-07-10 op char *dup, *tmp, *tok;
286 68e4f29e 2022-07-10 op
287 68e4f29e 2022-07-10 op if (ps->status == STATE_STOPPED)
288 68e4f29e 2022-07-10 op status = "stopped";
289 68e4f29e 2022-07-10 op else if (ps->status == STATE_PLAYING)
290 68e4f29e 2022-07-10 op status = "playing";
291 68e4f29e 2022-07-10 op else if (ps->status == STATE_PAUSED)
292 68e4f29e 2022-07-10 op status = "paused";
293 68e4f29e 2022-07-10 op else
294 68e4f29e 2022-07-10 op status = "unknown";
295 68e4f29e 2022-07-10 op
296 68e4f29e 2022-07-10 op percent = 100.0 * (double)ps->position / (double)ps->duration;
297 68e4f29e 2022-07-10 op
298 68e4f29e 2022-07-10 op tmp = dup = xstrdup(spec);
299 68e4f29e 2022-07-10 op while ((tok = strsep(&tmp, ",")) != NULL) {
300 68e4f29e 2022-07-10 op if (*tok == '\0')
301 68e4f29e 2022-07-10 op continue;
302 68e4f29e 2022-07-10 op
303 68e4f29e 2022-07-10 op if (!strcmp(tok, "path")) {
304 68e4f29e 2022-07-10 op puts(ps->path);
305 9fb94242 2022-07-13 op } else if (!strcmp(tok, "mode:oneline")) {
306 68e4f29e 2022-07-10 op printf("repeat one:%s ",
307 9fb94242 2022-07-13 op ps->mode.repeat_one ? "on" : "off");
308 9fb94242 2022-07-13 op printf("all:%s ", ps->mode.repeat_all ? "on" : "off");
309 9fb94242 2022-07-13 op printf("consume:%s\n", ps->mode.consume ? "on" : "off");
310 e08b784c 2022-07-13 op } else if (!strcmp(tok, "mode")) {
311 68e4f29e 2022-07-10 op printf("repeat all %s\n",
312 9fb94242 2022-07-13 op ps->mode.repeat_all ? "on" : "off");
313 32f74855 2022-07-13 op printf("repeat one %s\n",
314 32f74855 2022-07-13 op ps->mode.repeat_one ? "on" : "off");
315 9fb94242 2022-07-13 op printf("consume %s\n",
316 9fb94242 2022-07-13 op ps->mode.consume ? "on" : "off");
317 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "status")) {
318 68e4f29e 2022-07-10 op printf("%s %s\n", status, ps->path);
319 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "time:oneline")) {
320 68e4f29e 2022-07-10 op print_time("time ", ps->position, " / ");
321 68e4f29e 2022-07-10 op print_time("", ps->duration, "\n");
322 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "time:percentage")) {
323 68e4f29e 2022-07-10 op printf("position %.2f%%\n", percent);
324 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "time:raw")) {
325 68e4f29e 2022-07-10 op printf("position %lld\n", (long long)ps->position);
326 68e4f29e 2022-07-10 op printf("duration %lld\n", (long long)ps->duration);
327 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "time")) {
328 68e4f29e 2022-07-10 op print_time("position ", ps->position, "\n");
329 68e4f29e 2022-07-10 op print_time("duration ", ps->duration, "\n");
330 68e4f29e 2022-07-10 op }
331 68e4f29e 2022-07-10 op }
332 68e4f29e 2022-07-10 op
333 68e4f29e 2022-07-10 op free(dup);
334 6b47a39f 2022-02-21 op }
335 6b47a39f 2022-02-21 op
336 3dd90731 2023-01-09 op static void
337 3dd90731 2023-01-09 op print_monitor_event(struct player_event *ev)
338 3dd90731 2023-01-09 op {
339 3dd90731 2023-01-09 op switch (ev->event) {
340 3dd90731 2023-01-09 op case IMSG_CTL_MODE:
341 3dd90731 2023-01-09 op printf("%s repeat one:%s all:%s consume:%s\n",
342 3dd90731 2023-01-09 op event_name(ev->event),
343 3dd90731 2023-01-09 op ev->mode.repeat_one ? "on" : "off",
344 3dd90731 2023-01-09 op ev->mode.repeat_all ? "on" : "off",
345 3dd90731 2023-01-09 op ev->mode.consume ? "on" : "off");
346 3dd90731 2023-01-09 op break;
347 3dd90731 2023-01-09 op case IMSG_CTL_SEEK:
348 3dd90731 2023-01-09 op printf("%s %lld %lld\n", event_name(ev->event),
349 3dd90731 2023-01-09 op (long long)ev->position, (long long)ev->duration);
350 3dd90731 2023-01-09 op break;
351 3dd90731 2023-01-09 op default:
352 3dd90731 2023-01-09 op puts(event_name(ev->event));
353 3dd90731 2023-01-09 op break;
354 3dd90731 2023-01-09 op }
355 3dd90731 2023-01-09 op
356 3dd90731 2023-01-09 op fflush(stdout);
357 3dd90731 2023-01-09 op }
358 3dd90731 2023-01-09 op
359 6b47a39f 2022-02-21 op static int
360 3baa2617 2022-02-16 op ctlaction(struct parse_result *res)
361 3baa2617 2022-02-16 op {
362 fad0fb69 2022-05-28 op char path[PATH_MAX];
363 3baa2617 2022-02-16 op struct imsg imsg;
364 fad0fb69 2022-05-28 op struct player_status ps;
365 3dd90731 2023-01-09 op struct player_event ev;
366 3baa2617 2022-02-16 op ssize_t n;
367 3dd90731 2023-01-09 op int i, ret = 0, done = 1;
368 3baa2617 2022-02-16 op
369 4b42d3a3 2022-07-13 op if (pledge("stdio", NULL) == -1)
370 4b42d3a3 2022-07-13 op fatal("pledge");
371 4b42d3a3 2022-07-13 op
372 3baa2617 2022-02-16 op switch (res->action) {
373 3baa2617 2022-02-16 op case PLAY:
374 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
375 00f500ff 2022-02-19 op if (verbose) {
376 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_STATUS, 0, 0, -1,
377 00f500ff 2022-02-19 op NULL, 0);
378 00f500ff 2022-02-19 op done = 0;
379 00f500ff 2022-02-19 op }
380 3baa2617 2022-02-16 op break;
381 3baa2617 2022-02-16 op case PAUSE:
382 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
383 3baa2617 2022-02-16 op break;
384 3baa2617 2022-02-16 op case TOGGLE:
385 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
386 00f500ff 2022-02-19 op if (verbose) {
387 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_STATUS, 0, 0, -1,
388 00f500ff 2022-02-19 op NULL, 0);
389 00f500ff 2022-02-19 op done = 0;
390 00f500ff 2022-02-19 op }
391 3baa2617 2022-02-16 op break;
392 3baa2617 2022-02-16 op case STOP:
393 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
394 3baa2617 2022-02-16 op break;
395 3baa2617 2022-02-16 op case ADD:
396 8ff9231f 2022-02-16 op done = 0;
397 d903ec9a 2022-05-29 op for (i = 0; res->files[i] != NULL; ++i) {
398 18223c28 2022-06-13 op memset(path, 0, sizeof(path));
399 ef593b43 2022-06-09 op if (canonpath(res->files[i], path, sizeof(path))
400 ef593b43 2022-06-09 op == -1) {
401 ef593b43 2022-06-09 op log_warn("canonpath %s", res->files[i]);
402 fad0fb69 2022-05-28 op continue;
403 fad0fb69 2022-05-28 op }
404 fad0fb69 2022-05-28 op
405 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_ADD, 0, 0, -1,
406 fad0fb69 2022-05-28 op path, sizeof(path));
407 fad0fb69 2022-05-28 op }
408 fad0fb69 2022-05-28 op ret = i == 0;
409 3baa2617 2022-02-16 op break;
410 3baa2617 2022-02-16 op case FLUSH:
411 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
412 3baa2617 2022-02-16 op break;
413 3baa2617 2022-02-16 op case SHOW:
414 3baa2617 2022-02-16 op done = 0;
415 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
416 3baa2617 2022-02-16 op break;
417 bb3f279f 2022-02-16 op case STATUS:
418 bb3f279f 2022-02-16 op done = 0;
419 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
420 af27e631 2022-02-17 op break;
421 af27e631 2022-02-17 op case NEXT:
422 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
423 00f500ff 2022-02-19 op if (verbose) {
424 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, 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 bb3f279f 2022-02-16 op break;
429 af27e631 2022-02-17 op case PREV:
430 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
431 00f500ff 2022-02-19 op if (verbose) {
432 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_STATUS, 0, 0, -1,
433 00f500ff 2022-02-19 op NULL, 0);
434 00f500ff 2022-02-19 op done = 0;
435 00f500ff 2022-02-19 op }
436 af27e631 2022-02-17 op break;
437 14be015f 2022-02-17 op case LOAD:
438 7a427ecd 2022-02-17 op done = 0;
439 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
440 a913de21 2022-02-17 op break;
441 a913de21 2022-02-17 op case JUMP:
442 a913de21 2022-02-17 op done = 0;
443 fad0fb69 2022-05-28 op memset(path, 0, sizeof(path));
444 4b42d3a3 2022-07-13 op strlcpy(path, res->files[0], sizeof(path));
445 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_JUMP, 0, 0, -1,
446 fad0fb69 2022-05-28 op path, sizeof(path));
447 7a427ecd 2022-02-17 op break;
448 9fb94242 2022-07-13 op case MODE:
449 b72c0765 2022-07-13 op done = 0;
450 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_MODE, 0, 0, -1,
451 ff37a270 2023-08-31 op &res->mode, sizeof(res->mode));
452 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_STATUS, 0, 0, -1,
453 9fb94242 2022-07-13 op &res->mode, sizeof(res->mode));
454 b72c0765 2022-07-13 op res->status_format = "mode:oneline";
455 b72c0765 2022-07-13 op if (verbose)
456 b72c0765 2022-07-13 op res->status_format = "mode";
457 310ef57c 2022-02-19 op break;
458 6b47a39f 2022-02-21 op case MONITOR:
459 6b47a39f 2022-02-21 op done = 0;
460 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_MONITOR, 0, 0, -1,
461 6b47a39f 2022-02-21 op NULL, 0);
462 6b47a39f 2022-02-21 op break;
463 8d087670 2022-07-09 op case RESTART:
464 8d087670 2022-07-09 op memset(&res->seek, 0, sizeof(res->seek));
465 8d087670 2022-07-09 op /* fallthrough */
466 e5d4e9f7 2022-07-09 op case SEEK:
467 50e0da0e 2024-01-21 op imsg_compose(imsgbuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
468 e5d4e9f7 2022-07-09 op sizeof(res->seek));
469 e5d4e9f7 2022-07-09 op break;
470 3baa2617 2022-02-16 op case NONE:
471 3baa2617 2022-02-16 op /* action not expected */
472 3baa2617 2022-02-16 op fatalx("invalid action %u", res->action);
473 3baa2617 2022-02-16 op break;
474 3baa2617 2022-02-16 op }
475 3baa2617 2022-02-16 op
476 3baa2617 2022-02-16 op if (ret != 0)
477 3baa2617 2022-02-16 op goto end;
478 3baa2617 2022-02-16 op
479 50e0da0e 2024-01-21 op imsg_flush(imsgbuf);
480 3baa2617 2022-02-16 op
481 fad0fb69 2022-05-28 op i = 0;
482 3baa2617 2022-02-16 op while (!done) {
483 50e0da0e 2024-01-21 op if ((n = imsg_read(imsgbuf)) == -1 && errno != EAGAIN)
484 3baa2617 2022-02-16 op fatalx("imsg_read error");
485 3baa2617 2022-02-16 op if (n == 0)
486 3baa2617 2022-02-16 op fatalx("pipe closed");
487 3baa2617 2022-02-16 op
488 3baa2617 2022-02-16 op while (!done) {
489 50e0da0e 2024-01-21 op if ((n = imsg_get(imsgbuf, &imsg)) == -1)
490 3baa2617 2022-02-16 op fatalx("imsg_get error");
491 3baa2617 2022-02-16 op if (n == 0)
492 3baa2617 2022-02-16 op break;
493 3baa2617 2022-02-16 op
494 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ERR) {
495 fad0fb69 2022-05-28 op log_warnx("%s: %s", res->ctl->name,
496 fad0fb69 2022-05-28 op imsg_strerror(&imsg));
497 fad0fb69 2022-05-28 op ret = 1;
498 fad0fb69 2022-05-28 op done = 1;
499 fad0fb69 2022-05-28 op break;
500 fad0fb69 2022-05-28 op }
501 fad0fb69 2022-05-28 op
502 3baa2617 2022-02-16 op switch (res->action) {
503 8ff9231f 2022-02-16 op case ADD:
504 d903ec9a 2022-05-29 op if (res->files[i] == NULL)
505 fad0fb69 2022-05-28 op fatalx("received more replies than "
506 fad0fb69 2022-05-28 op "files enqueued.");
507 fad0fb69 2022-05-28 op
508 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
509 d903ec9a 2022-05-29 op log_debug("enqueued %s", res->files[i]);
510 fad0fb69 2022-05-28 op else
511 fad0fb69 2022-05-28 op fatalx("invalid message %d",
512 fad0fb69 2022-05-28 op imsg.hdr.type);
513 fad0fb69 2022-05-28 op i++;
514 d903ec9a 2022-05-29 op done = res->files[i] == NULL;
515 8ff9231f 2022-02-16 op break;
516 3baa2617 2022-02-16 op case SHOW:
517 9307af9e 2024-01-21 op if (imsg_get_len(&imsg) == 0) {
518 fad0fb69 2022-05-28 op done = 1;
519 fad0fb69 2022-05-28 op break;
520 fad0fb69 2022-05-28 op }
521 9307af9e 2024-01-21 op if (imsg_get_data(&imsg, &ps, sizeof(ps))
522 9307af9e 2024-01-21 op == -1)
523 fad0fb69 2022-05-28 op fatalx("data size mismatch");
524 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
525 fad0fb69 2022-05-28 op fatalx("received corrupted data");
526 fad0fb69 2022-05-28 op if (res->pretty) {
527 fad0fb69 2022-05-28 op char c = ' ';
528 fad0fb69 2022-05-28 op if (ps.status == STATE_PLAYING)
529 fad0fb69 2022-05-28 op c = '>';
530 fad0fb69 2022-05-28 op printf("%c ", c);
531 fad0fb69 2022-05-28 op }
532 fad0fb69 2022-05-28 op puts(ps.path);
533 3baa2617 2022-02-16 op break;
534 146307bd 2022-02-17 op case PLAY:
535 146307bd 2022-02-17 op case TOGGLE:
536 bb3f279f 2022-02-16 op case STATUS:
537 146307bd 2022-02-17 op case NEXT:
538 146307bd 2022-02-17 op case PREV:
539 a913de21 2022-02-17 op case JUMP:
540 b72c0765 2022-07-13 op case MODE:
541 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_STATUS)
542 fad0fb69 2022-05-28 op fatalx("invalid message %d",
543 fad0fb69 2022-05-28 op imsg.hdr.type);
544 fad0fb69 2022-05-28 op
545 9307af9e 2024-01-21 op if (imsg_get_data(&imsg, &ps, sizeof(ps))
546 9307af9e 2024-01-21 op == -1)
547 fad0fb69 2022-05-28 op fatalx("data size mismatch");
548 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
549 fad0fb69 2022-05-28 op fatalx("received corrupted data");
550 fad0fb69 2022-05-28 op
551 68e4f29e 2022-07-10 op print_status(&ps, res->status_format);
552 fad0fb69 2022-05-28 op done = 1;
553 bb3f279f 2022-02-16 op break;
554 7a427ecd 2022-02-17 op case LOAD:
555 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
556 fad0fb69 2022-05-28 op break;
557 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_COMMIT) {
558 fad0fb69 2022-05-28 op done = 1;
559 fad0fb69 2022-05-28 op break;
560 fad0fb69 2022-05-28 op }
561 fad0fb69 2022-05-28 op
562 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_BEGIN)
563 fad0fb69 2022-05-28 op fatalx("invalid message %d",
564 fad0fb69 2022-05-28 op imsg.hdr.type);
565 fad0fb69 2022-05-28 op
566 fad0fb69 2022-05-28 op load_files(res, &ret);
567 7a427ecd 2022-02-17 op break;
568 6b47a39f 2022-02-21 op case MONITOR:
569 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_MONITOR)
570 fad0fb69 2022-05-28 op fatalx("invalid message %d",
571 fad0fb69 2022-05-28 op imsg.hdr.type);
572 fad0fb69 2022-05-28 op
573 9307af9e 2024-01-21 op if (imsg_get_data(&imsg, &ev, sizeof(ev))
574 9307af9e 2024-01-21 op == -1)
575 fad0fb69 2022-05-28 op fatalx("data size mismatch");
576 fad0fb69 2022-05-28 op
577 3dd90731 2023-01-09 op if (ev.event < 0 || ev.event > IMSG__LAST)
578 fad0fb69 2022-05-28 op fatalx("received corrupted data");
579 fad0fb69 2022-05-28 op
580 3dd90731 2023-01-09 op if (!res->monitor[ev.event])
581 fad0fb69 2022-05-28 op break;
582 fad0fb69 2022-05-28 op
583 3dd90731 2023-01-09 op print_monitor_event(&ev);
584 6b47a39f 2022-02-21 op break;
585 3baa2617 2022-02-16 op default:
586 3baa2617 2022-02-16 op done = 1;
587 3baa2617 2022-02-16 op break;
588 3baa2617 2022-02-16 op }
589 3baa2617 2022-02-16 op
590 3baa2617 2022-02-16 op imsg_free(&imsg);
591 3baa2617 2022-02-16 op }
592 3baa2617 2022-02-16 op }
593 3baa2617 2022-02-16 op
594 3baa2617 2022-02-16 op end:
595 3baa2617 2022-02-16 op return ret;
596 3baa2617 2022-02-16 op }
597 3baa2617 2022-02-16 op
598 2ddcfa40 2022-07-08 op static int
599 3baa2617 2022-02-16 op ctl_noarg(struct parse_result *res, int argc, char **argv)
600 3baa2617 2022-02-16 op {
601 7e29fc4a 2022-03-03 op int ch;
602 7e29fc4a 2022-03-03 op
603 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
604 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
605 7e29fc4a 2022-03-03 op argc -= optind;
606 7e29fc4a 2022-03-03 op argv += optind;
607 7e29fc4a 2022-03-03 op
608 acaf7eb2 2022-03-05 op if (argc > 0)
609 3baa2617 2022-02-16 op ctl_usage(res->ctl);
610 7e29fc4a 2022-03-03 op
611 3baa2617 2022-02-16 op return ctlaction(res);
612 3baa2617 2022-02-16 op }
613 3baa2617 2022-02-16 op
614 2ddcfa40 2022-07-08 op static int
615 3baa2617 2022-02-16 op ctl_add(struct parse_result *res, int argc, char **argv)
616 3baa2617 2022-02-16 op {
617 7e29fc4a 2022-03-03 op int ch;
618 7e29fc4a 2022-03-03 op
619 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
620 3baa2617 2022-02-16 op ctl_usage(res->ctl);
621 7e29fc4a 2022-03-03 op argc -= optind;
622 7e29fc4a 2022-03-03 op argv += optind;
623 7e29fc4a 2022-03-03 op
624 7e29fc4a 2022-03-03 op if (argc == 0)
625 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
626 7e29fc4a 2022-03-03 op res->files = argv;
627 14be015f 2022-02-17 op
628 3baa2617 2022-02-16 op return ctlaction(res);
629 3baa2617 2022-02-16 op }
630 14be015f 2022-02-17 op
631 2ddcfa40 2022-07-08 op static int
632 8e916714 2022-02-17 op ctl_show(struct parse_result *res, int argc, char **argv)
633 8e916714 2022-02-17 op {
634 8e916714 2022-02-17 op int ch;
635 8e916714 2022-02-17 op
636 8e916714 2022-02-17 op while ((ch = getopt(argc, argv, "p")) != -1) {
637 8e916714 2022-02-17 op switch (ch) {
638 8e916714 2022-02-17 op case 'p':
639 8e916714 2022-02-17 op res->pretty = 1;
640 8e916714 2022-02-17 op break;
641 8e916714 2022-02-17 op default:
642 8e916714 2022-02-17 op ctl_usage(res->ctl);
643 8e916714 2022-02-17 op }
644 8e916714 2022-02-17 op }
645 8e916714 2022-02-17 op
646 8e916714 2022-02-17 op return ctlaction(res);
647 8e916714 2022-02-17 op }
648 8e916714 2022-02-17 op
649 2ddcfa40 2022-07-08 op static int
650 14be015f 2022-02-17 op ctl_load(struct parse_result *res, int argc, char **argv)
651 14be015f 2022-02-17 op {
652 7e29fc4a 2022-03-03 op int ch;
653 7e29fc4a 2022-03-03 op
654 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
655 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
656 7e29fc4a 2022-03-03 op argc -= optind;
657 7e29fc4a 2022-03-03 op argv += optind;
658 7e29fc4a 2022-03-03 op
659 4b42d3a3 2022-07-13 op if (argc > 1)
660 14be015f 2022-02-17 op ctl_usage(res->ctl);
661 14be015f 2022-02-17 op
662 4b42d3a3 2022-07-13 op res->fp = stdin;
663 4b42d3a3 2022-07-13 op if (argc == 1) {
664 4b42d3a3 2022-07-13 op if ((res->fp = fopen(argv[0], "r")) == NULL)
665 4b42d3a3 2022-07-13 op fatal("can't open %s", argv[0]);
666 4b42d3a3 2022-07-13 op }
667 14be015f 2022-02-17 op
668 7a427ecd 2022-02-17 op return ctlaction(res);
669 14be015f 2022-02-17 op }
670 14be015f 2022-02-17 op
671 2ddcfa40 2022-07-08 op static int
672 a913de21 2022-02-17 op ctl_jump(struct parse_result *res, int argc, char **argv)
673 a913de21 2022-02-17 op {
674 a913de21 2022-02-17 op int ch;
675 a913de21 2022-02-17 op
676 a913de21 2022-02-17 op while ((ch = getopt(argc, argv, "")) != -1)
677 a913de21 2022-02-17 op ctl_usage(res->ctl);
678 a913de21 2022-02-17 op argc -= optind;
679 a913de21 2022-02-17 op argv += optind;
680 a913de21 2022-02-17 op
681 a913de21 2022-02-17 op if (argc != 1)
682 a913de21 2022-02-17 op ctl_usage(res->ctl);
683 a913de21 2022-02-17 op
684 4b42d3a3 2022-07-13 op res->files = argv;
685 310ef57c 2022-02-19 op return ctlaction(res);
686 1eae758b 2022-07-13 op }
687 1eae758b 2022-07-13 op
688 1eae758b 2022-07-13 op static int
689 1eae758b 2022-07-13 op parse_mode(struct parse_result *res, const char *v)
690 1eae758b 2022-07-13 op {
691 1eae758b 2022-07-13 op if (v == NULL)
692 1eae758b 2022-07-13 op return MODE_TOGGLE;
693 1eae758b 2022-07-13 op if (!strcmp(v, "on"))
694 1eae758b 2022-07-13 op return MODE_ON;
695 1eae758b 2022-07-13 op if (!strcmp(v, "off"))
696 1eae758b 2022-07-13 op return MODE_OFF;
697 1eae758b 2022-07-13 op ctl_usage(res->ctl);
698 310ef57c 2022-02-19 op }
699 310ef57c 2022-02-19 op
700 2ddcfa40 2022-07-08 op static int
701 310ef57c 2022-02-19 op ctl_repeat(struct parse_result *res, int argc, char **argv)
702 310ef57c 2022-02-19 op {
703 1eae758b 2022-07-13 op int ch;
704 310ef57c 2022-02-19 op
705 310ef57c 2022-02-19 op while ((ch = getopt(argc, argv, "")) != -1)
706 310ef57c 2022-02-19 op ctl_usage(res->ctl);
707 310ef57c 2022-02-19 op argc -= optind;
708 310ef57c 2022-02-19 op argv += optind;
709 310ef57c 2022-02-19 op
710 1eae758b 2022-07-13 op if (argc != 1 && argc != 2)
711 310ef57c 2022-02-19 op ctl_usage(res->ctl);
712 310ef57c 2022-02-19 op
713 310ef57c 2022-02-19 op if (!strcmp(argv[0], "one"))
714 1eae758b 2022-07-13 op res->mode.repeat_one = parse_mode(res, argv[1]);
715 310ef57c 2022-02-19 op else if (!strcmp(argv[0], "all"))
716 1eae758b 2022-07-13 op res->mode.repeat_all = parse_mode(res, argv[1]);
717 310ef57c 2022-02-19 op else
718 9fb94242 2022-07-13 op ctl_usage(res->ctl);
719 9fb94242 2022-07-13 op
720 9fb94242 2022-07-13 op return ctlaction(res);
721 9fb94242 2022-07-13 op }
722 9fb94242 2022-07-13 op
723 9fb94242 2022-07-13 op static int
724 9fb94242 2022-07-13 op ctl_consume(struct parse_result *res, int argc, char **argv)
725 9fb94242 2022-07-13 op {
726 9fb94242 2022-07-13 op int ch;
727 9fb94242 2022-07-13 op
728 9fb94242 2022-07-13 op while ((ch = getopt(argc, argv, "")) != -1)
729 9fb94242 2022-07-13 op ctl_usage(res->ctl);
730 9fb94242 2022-07-13 op argc -= optind;
731 9fb94242 2022-07-13 op argv += optind;
732 9fb94242 2022-07-13 op
733 1eae758b 2022-07-13 op if (argc > 1)
734 310ef57c 2022-02-19 op ctl_usage(res->ctl);
735 90122a37 2022-05-14 op
736 1eae758b 2022-07-13 op res->mode.consume = parse_mode(res, argv[0]);
737 90122a37 2022-05-14 op return ctlaction(res);
738 90122a37 2022-05-14 op }
739 90122a37 2022-05-14 op
740 2ddcfa40 2022-07-08 op static int
741 90122a37 2022-05-14 op ctl_monitor(struct parse_result *res, int argc, char **argv)
742 90122a37 2022-05-14 op {
743 15aecb89 2022-07-12 op int ch, n = 0;
744 90122a37 2022-05-14 op const char *events;
745 90122a37 2022-05-14 op char *dup, *tmp, *tok;
746 90122a37 2022-05-14 op
747 90122a37 2022-05-14 op while ((ch = getopt(argc, argv, "")) != -1)
748 90122a37 2022-05-14 op ctl_usage(res->ctl);
749 90122a37 2022-05-14 op argc -= optind;
750 90122a37 2022-05-14 op argv += optind;
751 90122a37 2022-05-14 op
752 90122a37 2022-05-14 op if (argc > 1)
753 90122a37 2022-05-14 op ctl_usage(res->ctl);
754 90122a37 2022-05-14 op
755 9fb94242 2022-07-13 op events = "play,pause,stop,next,prev,jump,mode,add,load,seek";
756 90122a37 2022-05-14 op if (argc == 1)
757 90122a37 2022-05-14 op events = *argv;
758 310ef57c 2022-02-19 op
759 90122a37 2022-05-14 op tmp = dup = xstrdup(events);
760 90122a37 2022-05-14 op while ((tok = strsep(&tmp, ",")) != NULL) {
761 90122a37 2022-05-14 op if (*tok == '\0')
762 90122a37 2022-05-14 op continue;
763 90122a37 2022-05-14 op
764 15aecb89 2022-07-12 op n++;
765 90122a37 2022-05-14 op if (!strcmp(tok, "play"))
766 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PLAY] = 1;
767 90122a37 2022-05-14 op else if (!strcmp(tok, "pause"))
768 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PAUSE] = 1;
769 90122a37 2022-05-14 op else if (!strcmp(tok, "stop"))
770 90122a37 2022-05-14 op res->monitor[IMSG_CTL_STOP] = 1;
771 90122a37 2022-05-14 op else if (!strcmp(tok, "next"))
772 90122a37 2022-05-14 op res->monitor[IMSG_CTL_NEXT] = 1;
773 90122a37 2022-05-14 op else if (!strcmp(tok, "prev"))
774 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PREV] = 1;
775 90122a37 2022-05-14 op else if (!strcmp(tok, "jump"))
776 90122a37 2022-05-14 op res->monitor[IMSG_CTL_JUMP] = 1;
777 9fb94242 2022-07-13 op else if (!strcmp(tok, "mode"))
778 9fb94242 2022-07-13 op res->monitor[IMSG_CTL_MODE] = 1;
779 90122a37 2022-05-14 op else if (!strcmp(tok, "add"))
780 90122a37 2022-05-14 op res->monitor[IMSG_CTL_ADD] = 1;
781 90122a37 2022-05-14 op else if (!strcmp(tok, "load"))
782 90122a37 2022-05-14 op res->monitor[IMSG_CTL_COMMIT] = 1;
783 88519c7b 2022-07-09 op else if (!strcmp(tok, "seek"))
784 88519c7b 2022-07-09 op res->monitor[IMSG_CTL_SEEK] = 1;
785 15aecb89 2022-07-12 op else {
786 15aecb89 2022-07-12 op log_warnx("unknown event \"%s\"", tok);
787 15aecb89 2022-07-12 op n--;
788 15aecb89 2022-07-12 op }
789 90122a37 2022-05-14 op }
790 90122a37 2022-05-14 op
791 90122a37 2022-05-14 op free(dup);
792 15aecb89 2022-07-12 op if (n == 0)
793 15aecb89 2022-07-12 op ctl_usage(res->ctl);
794 e5d4e9f7 2022-07-09 op return ctlaction(res);
795 e5d4e9f7 2022-07-09 op }
796 e5d4e9f7 2022-07-09 op
797 e5d4e9f7 2022-07-09 op static int
798 e5d4e9f7 2022-07-09 op ctl_seek(struct parse_result *res, int argc, char **argv)
799 e5d4e9f7 2022-07-09 op {
800 071167ed 2022-07-09 op const char *n;
801 071167ed 2022-07-09 op char *ep;
802 071167ed 2022-07-09 op int hours = 0, minutes = 0, seconds = 0;
803 071167ed 2022-07-09 op int sign = 1;
804 e5d4e9f7 2022-07-09 op
805 e5d4e9f7 2022-07-09 op if (argc > 0) {
806 e5d4e9f7 2022-07-09 op /* skip the command name */
807 e5d4e9f7 2022-07-09 op argc--;
808 e5d4e9f7 2022-07-09 op argv++;
809 e5d4e9f7 2022-07-09 op }
810 e5d4e9f7 2022-07-09 op
811 e5d4e9f7 2022-07-09 op if (argc > 0 && !strcmp(*argv, "--")) {
812 e5d4e9f7 2022-07-09 op argc--;
813 e5d4e9f7 2022-07-09 op argv++;
814 e5d4e9f7 2022-07-09 op }
815 e5d4e9f7 2022-07-09 op
816 e5d4e9f7 2022-07-09 op if (argc != 1)
817 e5d4e9f7 2022-07-09 op ctl_usage(res->ctl);
818 e5d4e9f7 2022-07-09 op
819 e5d4e9f7 2022-07-09 op n = *argv;
820 e5d4e9f7 2022-07-09 op if (*n == '-' || *n == '+')
821 e5d4e9f7 2022-07-09 op res->seek.relative = 1;
822 071167ed 2022-07-09 op if (*n == '-') {
823 071167ed 2022-07-09 op n++;
824 071167ed 2022-07-09 op sign = -1;
825 071167ed 2022-07-09 op }
826 e5d4e9f7 2022-07-09 op
827 071167ed 2022-07-09 op seconds = strtol(n, &ep, 10);
828 071167ed 2022-07-09 op if (n[0] == '\0' ||
829 071167ed 2022-07-09 op (*ep != '\0' && *ep != ':' && *ep != '%') ||
830 071167ed 2022-07-09 op (*ep == '%' && ep[1] != '\0'))
831 071167ed 2022-07-09 op fatalx("invalid offset: %s", argv[0]);
832 071167ed 2022-07-09 op if (*ep == '\0' || *ep == '%') {
833 071167ed 2022-07-09 op res->seek.percent = *ep == '%';
834 071167ed 2022-07-09 op goto done;
835 071167ed 2022-07-09 op }
836 e5d4e9f7 2022-07-09 op
837 071167ed 2022-07-09 op n = ++ep;
838 071167ed 2022-07-09 op minutes = seconds;
839 071167ed 2022-07-09 op seconds = strtol(n, &ep, 10);
840 071167ed 2022-07-09 op if (n[0] == '\0' || (*ep != '\0' && *ep != ':'))
841 071167ed 2022-07-09 op fatalx("invalid offset: %s", argv[0]);
842 071167ed 2022-07-09 op if (*ep == '\0')
843 071167ed 2022-07-09 op goto done;
844 071167ed 2022-07-09 op
845 071167ed 2022-07-09 op n = ++ep;
846 071167ed 2022-07-09 op hours = minutes;
847 071167ed 2022-07-09 op minutes = seconds;
848 071167ed 2022-07-09 op seconds = strtol(n, &ep, 10);
849 071167ed 2022-07-09 op if (n[0] == '\0' || *ep != '\0')
850 071167ed 2022-07-09 op fatalx("invalid offset: %s", argv[0]);
851 071167ed 2022-07-09 op
852 071167ed 2022-07-09 op done:
853 071167ed 2022-07-09 op res->seek.offset = sign * (hours * 3600 + minutes * 60 + seconds);
854 a913de21 2022-02-17 op return ctlaction(res);
855 a913de21 2022-02-17 op }
856 a913de21 2022-02-17 op
857 fea541a8 2022-02-16 op static int
858 68e4f29e 2022-07-10 op ctl_status(struct parse_result *res, int argc, char **argv)
859 68e4f29e 2022-07-10 op {
860 68e4f29e 2022-07-10 op int ch;
861 68e4f29e 2022-07-10 op
862 68e4f29e 2022-07-10 op while ((ch = getopt(argc, argv, "f:")) != -1) {
863 68e4f29e 2022-07-10 op switch (ch) {
864 68e4f29e 2022-07-10 op case 'f':
865 6576093a 2022-07-11 op res->status_format = optarg;
866 68e4f29e 2022-07-10 op break;
867 68e4f29e 2022-07-10 op default:
868 68e4f29e 2022-07-10 op ctl_usage(res->ctl);
869 68e4f29e 2022-07-10 op }
870 68e4f29e 2022-07-10 op }
871 68e4f29e 2022-07-10 op argc -= optind;
872 68e4f29e 2022-07-10 op argv += optind;
873 68e4f29e 2022-07-10 op
874 68e4f29e 2022-07-10 op if (argc > 0)
875 68e4f29e 2022-07-10 op ctl_usage(res->ctl);
876 68e4f29e 2022-07-10 op
877 68e4f29e 2022-07-10 op return ctlaction(res);
878 68e4f29e 2022-07-10 op }
879 68e4f29e 2022-07-10 op
880 68e4f29e 2022-07-10 op static int
881 1d673950 2022-03-03 op ctl_get_lock(const char *lockfile)
882 3baa2617 2022-02-16 op {
883 1d673950 2022-03-03 op int lockfd;
884 3baa2617 2022-02-16 op
885 1d673950 2022-03-03 op if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
886 1d673950 2022-03-03 op log_debug("open failed: %s", strerror(errno));
887 1d673950 2022-03-03 op return -1;
888 1d673950 2022-03-03 op }
889 3baa2617 2022-02-16 op
890 1d673950 2022-03-03 op if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
891 1d673950 2022-03-03 op log_debug("flock failed: %s", strerror(errno));
892 1d673950 2022-03-03 op if (errno != EAGAIN)
893 1d673950 2022-03-03 op return -1;
894 1d673950 2022-03-03 op while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
895 1d673950 2022-03-03 op /* nop */;
896 1d673950 2022-03-03 op close(lockfd);
897 1d673950 2022-03-03 op return -2;
898 1d673950 2022-03-03 op }
899 1d673950 2022-03-03 op log_debug("flock succeeded");
900 1d673950 2022-03-03 op
901 1d673950 2022-03-03 op return lockfd;
902 1d673950 2022-03-03 op }
903 1d673950 2022-03-03 op
904 1d673950 2022-03-03 op static int
905 1d673950 2022-03-03 op ctl_connect(void)
906 1d673950 2022-03-03 op {
907 1d673950 2022-03-03 op struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
908 1d673950 2022-03-03 op struct sockaddr_un sa;
909 1d673950 2022-03-03 op size_t size;
910 1d673950 2022-03-03 op int fd, lockfd = -1, locked = 0, spawned = 0;
911 1b77ad48 2022-07-08 op int attempt = 0;
912 1d673950 2022-03-03 op char *lockfile = NULL;
913 1d673950 2022-03-03 op
914 1d673950 2022-03-03 op memset(&sa, 0, sizeof(sa));
915 1d673950 2022-03-03 op sa.sun_family = AF_UNIX;
916 1d673950 2022-03-03 op size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
917 1d673950 2022-03-03 op if (size >= sizeof(sa.sun_path)) {
918 1d673950 2022-03-03 op errno = ENAMETOOLONG;
919 fea541a8 2022-02-16 op return -1;
920 fea541a8 2022-02-16 op }
921 3baa2617 2022-02-16 op
922 1d673950 2022-03-03 op retry:
923 1d673950 2022-03-03 op if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
924 1d673950 2022-03-03 op return -1;
925 1d673950 2022-03-03 op
926 1d673950 2022-03-03 op if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
927 1d673950 2022-03-03 op log_debug("connection failed: %s", strerror(errno));
928 1d673950 2022-03-03 op if (errno != ECONNREFUSED && errno != ENOENT)
929 1d673950 2022-03-03 op goto failed;
930 1b77ad48 2022-07-08 op if (attempt++ == 100)
931 1b77ad48 2022-07-08 op goto failed;
932 1d673950 2022-03-03 op close(fd);
933 1d673950 2022-03-03 op
934 1d673950 2022-03-03 op if (!locked) {
935 1d673950 2022-03-03 op xasprintf(&lockfile, "%s.lock", csock);
936 1d673950 2022-03-03 op if ((lockfd = ctl_get_lock(lockfile)) < 0) {
937 1d673950 2022-03-03 op log_debug("didn't get the lock (%d)", lockfd);
938 1d673950 2022-03-03 op
939 1d673950 2022-03-03 op free(lockfile);
940 1d673950 2022-03-03 op lockfile = NULL;
941 1d673950 2022-03-03 op
942 1d673950 2022-03-03 op if (lockfd == -1)
943 1d673950 2022-03-03 op goto retry;
944 1d673950 2022-03-03 op }
945 1d673950 2022-03-03 op
946 1d673950 2022-03-03 op /*
947 1d673950 2022-03-03 op * Always retry at least once, even if we got
948 1d673950 2022-03-03 op * the lock, because another client could have
949 1d673950 2022-03-03 op * taken the lock, started the server and released
950 1d673950 2022-03-03 op * the lock between our connect() and flock()
951 1d673950 2022-03-03 op */
952 1d673950 2022-03-03 op locked = 1;
953 1d673950 2022-03-03 op goto retry;
954 1d673950 2022-03-03 op }
955 1d673950 2022-03-03 op
956 1d673950 2022-03-03 op if (!spawned) {
957 1d673950 2022-03-03 op log_debug("spawning the daemon");
958 1d673950 2022-03-03 op spawn_daemon();
959 1d673950 2022-03-03 op spawned = 1;
960 1d673950 2022-03-03 op }
961 1d673950 2022-03-03 op
962 1d673950 2022-03-03 op nanosleep(&ts, NULL);
963 1d673950 2022-03-03 op goto retry;
964 1d673950 2022-03-03 op }
965 1d673950 2022-03-03 op
966 1d673950 2022-03-03 op if (locked && lockfd >= 0) {
967 1d673950 2022-03-03 op unlink(lockfile);
968 1d673950 2022-03-03 op free(lockfile);
969 1d673950 2022-03-03 op close(lockfd);
970 1d673950 2022-03-03 op }
971 1d673950 2022-03-03 op return fd;
972 1d673950 2022-03-03 op
973 1d673950 2022-03-03 op failed:
974 1d673950 2022-03-03 op if (locked) {
975 1d673950 2022-03-03 op free(lockfile);
976 1d673950 2022-03-03 op close(lockfd);
977 1d673950 2022-03-03 op }
978 1d673950 2022-03-03 op close(fd);
979 1d673950 2022-03-03 op return -1;
980 fea541a8 2022-02-16 op }
981 fea541a8 2022-02-16 op
982 fea541a8 2022-02-16 op __dead void
983 fea541a8 2022-02-16 op ctl(int argc, char **argv)
984 fea541a8 2022-02-16 op {
985 6576093a 2022-07-11 op struct parse_result res;
986 6576093a 2022-07-11 op const char *fmt;
987 1d673950 2022-03-03 op int ctl_sock;
988 3baa2617 2022-02-16 op
989 6576093a 2022-07-11 op memset(&res, 0, sizeof(res));
990 6576093a 2022-07-11 op if ((fmt = getenv("AMUSED_STATUS_FORMAT")) == NULL)
991 99bf1536 2022-07-13 op fmt = "status,time:oneline,mode:oneline";
992 6576093a 2022-07-11 op res.status_format = fmt;
993 6576093a 2022-07-11 op
994 1eae758b 2022-07-13 op res.mode.consume = MODE_UNDEF;
995 1eae758b 2022-07-13 op res.mode.repeat_all = MODE_UNDEF;
996 1eae758b 2022-07-13 op res.mode.repeat_one = MODE_UNDEF;
997 1eae758b 2022-07-13 op
998 fea541a8 2022-02-16 op log_init(1, LOG_DAEMON);
999 fea541a8 2022-02-16 op log_setverbose(verbose);
1000 fea541a8 2022-02-16 op
1001 96233476 2022-06-09 op if (getcwd(cwd, sizeof(cwd)) == NULL)
1002 96233476 2022-06-09 op fatal("getcwd");
1003 96233476 2022-06-09 op
1004 1d673950 2022-03-03 op if ((ctl_sock = ctl_connect()) == -1)
1005 1d673950 2022-03-03 op fatal("can't connect");
1006 fea541a8 2022-02-16 op
1007 50e0da0e 2024-01-21 op imsgbuf = xmalloc(sizeof(*imsgbuf));
1008 50e0da0e 2024-01-21 op imsg_init(imsgbuf, ctl_sock);
1009 3baa2617 2022-02-16 op
1010 3baa2617 2022-02-16 op optreset = 1;
1011 3baa2617 2022-02-16 op optind = 1;
1012 3baa2617 2022-02-16 op
1013 4b42d3a3 2022-07-13 op /* we'll drop rpath too later in ctlaction */
1014 4b42d3a3 2022-07-13 op if (pledge("stdio rpath", NULL) == -1)
1015 4b42d3a3 2022-07-13 op fatal("pledge");
1016 4b42d3a3 2022-07-13 op
1017 6576093a 2022-07-11 op exit(parse(&res, argc, argv));
1018 3baa2617 2022-02-16 op }