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