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