Blame


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