Blame


1 3baa2617 2022-02-16 op /* $OpenBSD: control.c,v 1.8 2021/03/02 04:10:07 jsg Exp $ */
2 3baa2617 2022-02-16 op
3 3baa2617 2022-02-16 op /*
4 578f8d0c 2023-05-02 op * Copyright (c) 2022, 2023 Omar Polo <op@omarpolo.com>
5 3baa2617 2022-02-16 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 3baa2617 2022-02-16 op *
7 3baa2617 2022-02-16 op * Permission to use, copy, modify, and distribute this software for any
8 3baa2617 2022-02-16 op * purpose with or without fee is hereby granted, provided that the above
9 3baa2617 2022-02-16 op * copyright notice and this permission notice appear in all copies.
10 3baa2617 2022-02-16 op *
11 3baa2617 2022-02-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 3baa2617 2022-02-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 3baa2617 2022-02-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 3baa2617 2022-02-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 3baa2617 2022-02-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 3baa2617 2022-02-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 3baa2617 2022-02-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 3baa2617 2022-02-16 op */
19 f36fd90a 2022-07-09 op
20 f36fd90a 2022-07-09 op #include "config.h"
21 f36fd90a 2022-07-09 op
22 3baa2617 2022-02-16 op #include <sys/stat.h>
23 3baa2617 2022-02-16 op #include <sys/socket.h>
24 3baa2617 2022-02-16 op #include <sys/un.h>
25 3baa2617 2022-02-16 op
26 3baa2617 2022-02-16 op #include <netinet/in.h>
27 3baa2617 2022-02-16 op #include <net/if.h>
28 3baa2617 2022-02-16 op
29 3baa2617 2022-02-16 op #include <errno.h>
30 1738062c 2023-10-07 op #include <fcntl.h>
31 bb3f279f 2022-02-16 op #include <limits.h>
32 14be015f 2022-02-17 op #include <stdio.h>
33 3baa2617 2022-02-16 op #include <stdlib.h>
34 3baa2617 2022-02-16 op #include <string.h>
35 3baa2617 2022-02-16 op #include <unistd.h>
36 3baa2617 2022-02-16 op
37 3baa2617 2022-02-16 op #include "amused.h"
38 0a53bd62 2023-08-16 op #include "ev.h"
39 3baa2617 2022-02-16 op #include "log.h"
40 3baa2617 2022-02-16 op #include "control.h"
41 3baa2617 2022-02-16 op #include "playlist.h"
42 3baa2617 2022-02-16 op
43 3baa2617 2022-02-16 op #define CONTROL_BACKLOG 5
44 3baa2617 2022-02-16 op
45 3baa2617 2022-02-16 op struct {
46 3baa2617 2022-02-16 op int fd;
47 6be81433 2024-04-14 op unsigned int tout;
48 19d6b480 2022-02-17 op struct playlist play;
49 c6304ab9 2022-02-17 op int tx;
50 19d6b480 2022-02-17 op } control_state = {.fd = -1, .tx = -1};
51 3baa2617 2022-02-16 op
52 3baa2617 2022-02-16 op struct ctl_conn {
53 3baa2617 2022-02-16 op TAILQ_ENTRY(ctl_conn) entry;
54 87f575c3 2022-02-21 op int monitor; /* 1 if client is in monitor mode */
55 3baa2617 2022-02-16 op struct imsgev iev;
56 3baa2617 2022-02-16 op };
57 3baa2617 2022-02-16 op
58 3baa2617 2022-02-16 op TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
59 3baa2617 2022-02-16 op
60 3baa2617 2022-02-16 op struct ctl_conn *control_connbyfd(int);
61 3baa2617 2022-02-16 op struct ctl_conn *control_connbypid(pid_t);
62 3baa2617 2022-02-16 op void control_close(int);
63 3baa2617 2022-02-16 op
64 3baa2617 2022-02-16 op int
65 3baa2617 2022-02-16 op control_init(char *path)
66 3baa2617 2022-02-16 op {
67 3baa2617 2022-02-16 op struct sockaddr_un sun;
68 1738062c 2023-10-07 op int fd, flags;
69 3baa2617 2022-02-16 op mode_t old_umask;
70 3baa2617 2022-02-16 op
71 1738062c 2023-10-07 op if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
72 3baa2617 2022-02-16 op log_warn("%s: socket", __func__);
73 3baa2617 2022-02-16 op return (-1);
74 3baa2617 2022-02-16 op }
75 3baa2617 2022-02-16 op
76 1738062c 2023-10-07 op if ((flags = fcntl(fd, F_GETFL)) == -1 ||
77 1738062c 2023-10-07 op fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
78 1738062c 2023-10-07 op fatal("fcntl(O_NONBLOCK)");
79 1738062c 2023-10-07 op
80 1738062c 2023-10-07 op if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
81 1738062c 2023-10-07 op fatal("fcntl(CLOEXEC)");
82 1738062c 2023-10-07 op
83 3baa2617 2022-02-16 op memset(&sun, 0, sizeof(sun));
84 3baa2617 2022-02-16 op sun.sun_family = AF_UNIX;
85 3baa2617 2022-02-16 op strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
86 3baa2617 2022-02-16 op
87 3baa2617 2022-02-16 op if (unlink(path) == -1)
88 3baa2617 2022-02-16 op if (errno != ENOENT) {
89 3baa2617 2022-02-16 op log_warn("%s: unlink %s", __func__, path);
90 3baa2617 2022-02-16 op close(fd);
91 3baa2617 2022-02-16 op return (-1);
92 3baa2617 2022-02-16 op }
93 3baa2617 2022-02-16 op
94 3baa2617 2022-02-16 op old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
95 3baa2617 2022-02-16 op if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
96 3baa2617 2022-02-16 op log_warn("%s: bind: %s", __func__, path);
97 3baa2617 2022-02-16 op close(fd);
98 3baa2617 2022-02-16 op umask(old_umask);
99 3baa2617 2022-02-16 op return (-1);
100 3baa2617 2022-02-16 op }
101 3baa2617 2022-02-16 op umask(old_umask);
102 3baa2617 2022-02-16 op
103 3baa2617 2022-02-16 op if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
104 3baa2617 2022-02-16 op log_warn("%s: chmod", __func__);
105 3baa2617 2022-02-16 op close(fd);
106 3baa2617 2022-02-16 op (void)unlink(path);
107 3baa2617 2022-02-16 op return (-1);
108 3baa2617 2022-02-16 op }
109 3baa2617 2022-02-16 op
110 3baa2617 2022-02-16 op return (fd);
111 3c6e1aae 2023-08-16 op }
112 3c6e1aae 2023-08-16 op
113 3c6e1aae 2023-08-16 op static void
114 3c6e1aae 2023-08-16 op enable_accept(int fd, int ev, void *bula)
115 3c6e1aae 2023-08-16 op {
116 6be81433 2024-04-14 op control_state.tout = 0;
117 6be81433 2024-04-14 op ev_add(control_state.fd, EV_READ, control_accept, NULL);
118 3baa2617 2022-02-16 op }
119 3baa2617 2022-02-16 op
120 3baa2617 2022-02-16 op int
121 3baa2617 2022-02-16 op control_listen(int fd)
122 3baa2617 2022-02-16 op {
123 3baa2617 2022-02-16 op if (control_state.fd != -1)
124 3baa2617 2022-02-16 op fatalx("%s: received unexpected controlsock", __func__);
125 3baa2617 2022-02-16 op
126 3baa2617 2022-02-16 op control_state.fd = fd;
127 3baa2617 2022-02-16 op if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
128 3baa2617 2022-02-16 op log_warn("%s: listen", __func__);
129 3baa2617 2022-02-16 op return (-1);
130 3baa2617 2022-02-16 op }
131 3baa2617 2022-02-16 op
132 3c6e1aae 2023-08-16 op enable_accept(-1, 0, NULL);
133 3baa2617 2022-02-16 op return (0);
134 3baa2617 2022-02-16 op }
135 3baa2617 2022-02-16 op
136 3baa2617 2022-02-16 op void
137 0a53bd62 2023-08-16 op control_accept(int listenfd, int event, void *bula)
138 3baa2617 2022-02-16 op {
139 23a99671 2023-10-08 op int connfd, flags;
140 3baa2617 2022-02-16 op socklen_t len;
141 3baa2617 2022-02-16 op struct sockaddr_un sun;
142 3baa2617 2022-02-16 op struct ctl_conn *c;
143 3baa2617 2022-02-16 op
144 3baa2617 2022-02-16 op len = sizeof(sun);
145 23a99671 2023-10-08 op if ((connfd = accept(listenfd, (struct sockaddr *)&sun, &len)) == -1) {
146 3baa2617 2022-02-16 op /*
147 3baa2617 2022-02-16 op * Pause accept if we are out of file descriptors, or
148 3baa2617 2022-02-16 op * libevent will haunt us here too.
149 3baa2617 2022-02-16 op */
150 3baa2617 2022-02-16 op if (errno == ENFILE || errno == EMFILE) {
151 3baa2617 2022-02-16 op struct timeval evtpause = { 1, 0 };
152 3baa2617 2022-02-16 op
153 0a53bd62 2023-08-16 op ev_del(control_state.fd);
154 6be81433 2024-04-14 op control_state.tout = ev_timer(&evtpause, enable_accept, NULL);
155 6be81433 2024-04-14 op if (control_state.tout == 0)
156 6be81433 2024-04-14 op fatal("ev_timer failed");
157 3baa2617 2022-02-16 op } else if (errno != EWOULDBLOCK && errno != EINTR &&
158 3baa2617 2022-02-16 op errno != ECONNABORTED)
159 3baa2617 2022-02-16 op log_warn("%s: accept4", __func__);
160 3baa2617 2022-02-16 op return;
161 3baa2617 2022-02-16 op }
162 3baa2617 2022-02-16 op
163 23a99671 2023-10-08 op if ((flags = fcntl(connfd, F_GETFL)) == -1 ||
164 23a99671 2023-10-08 op fcntl(connfd, F_SETFL, flags | O_NONBLOCK) == -1)
165 23a99671 2023-10-08 op fatal("fcntl(O_NONBLOCK)");
166 23a99671 2023-10-08 op if (fcntl(connfd, F_SETFD, FD_CLOEXEC) == -1)
167 23a99671 2023-10-08 op fatal("fcntl(CLOEXEC)");
168 23a99671 2023-10-08 op
169 3baa2617 2022-02-16 op if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
170 3baa2617 2022-02-16 op log_warn("%s: calloc", __func__);
171 3baa2617 2022-02-16 op close(connfd);
172 3baa2617 2022-02-16 op return;
173 3baa2617 2022-02-16 op }
174 3baa2617 2022-02-16 op
175 50e0da0e 2024-01-21 op imsg_init(&c->iev.imsgbuf, connfd);
176 3baa2617 2022-02-16 op c->iev.handler = control_dispatch_imsg;
177 6be81433 2024-04-14 op c->iev.events = EV_READ;
178 50e0da0e 2024-01-21 op ev_add(c->iev.imsgbuf.fd, c->iev.events, c->iev.handler, &c->iev);
179 3baa2617 2022-02-16 op
180 3baa2617 2022-02-16 op TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
181 3baa2617 2022-02-16 op }
182 3baa2617 2022-02-16 op
183 3baa2617 2022-02-16 op struct ctl_conn *
184 3baa2617 2022-02-16 op control_connbyfd(int fd)
185 3baa2617 2022-02-16 op {
186 3baa2617 2022-02-16 op struct ctl_conn *c;
187 3baa2617 2022-02-16 op
188 3baa2617 2022-02-16 op TAILQ_FOREACH(c, &ctl_conns, entry) {
189 50e0da0e 2024-01-21 op if (c->iev.imsgbuf.fd == fd)
190 3baa2617 2022-02-16 op break;
191 3baa2617 2022-02-16 op }
192 3baa2617 2022-02-16 op
193 3baa2617 2022-02-16 op return (c);
194 3baa2617 2022-02-16 op }
195 3baa2617 2022-02-16 op
196 3baa2617 2022-02-16 op struct ctl_conn *
197 3baa2617 2022-02-16 op control_connbypid(pid_t pid)
198 3baa2617 2022-02-16 op {
199 3baa2617 2022-02-16 op struct ctl_conn *c;
200 3baa2617 2022-02-16 op
201 3baa2617 2022-02-16 op TAILQ_FOREACH(c, &ctl_conns, entry) {
202 50e0da0e 2024-01-21 op if (c->iev.imsgbuf.pid == pid)
203 3baa2617 2022-02-16 op break;
204 3baa2617 2022-02-16 op }
205 3baa2617 2022-02-16 op
206 3baa2617 2022-02-16 op return (c);
207 3baa2617 2022-02-16 op }
208 3baa2617 2022-02-16 op
209 3baa2617 2022-02-16 op void
210 3baa2617 2022-02-16 op control_close(int fd)
211 3baa2617 2022-02-16 op {
212 3baa2617 2022-02-16 op struct ctl_conn *c;
213 3baa2617 2022-02-16 op
214 3baa2617 2022-02-16 op if ((c = control_connbyfd(fd)) == NULL) {
215 3baa2617 2022-02-16 op log_warnx("%s: fd %d: not found", __func__, fd);
216 3baa2617 2022-02-16 op return;
217 19d6b480 2022-02-17 op }
218 19d6b480 2022-02-17 op
219 19d6b480 2022-02-17 op /* abort the transaction if running by this user */
220 50e0da0e 2024-01-21 op if (control_state.tx != -1 && c->iev.imsgbuf.fd == control_state.tx) {
221 19d6b480 2022-02-17 op playlist_free(&control_state.play);
222 19d6b480 2022-02-17 op control_state.tx = -1;
223 3baa2617 2022-02-16 op }
224 3baa2617 2022-02-16 op
225 50e0da0e 2024-01-21 op msgbuf_clear(&c->iev.imsgbuf.w);
226 3baa2617 2022-02-16 op TAILQ_REMOVE(&ctl_conns, c, entry);
227 3baa2617 2022-02-16 op
228 50e0da0e 2024-01-21 op ev_del(c->iev.imsgbuf.fd);
229 50e0da0e 2024-01-21 op close(c->iev.imsgbuf.fd);
230 3baa2617 2022-02-16 op
231 3baa2617 2022-02-16 op /* Some file descriptors are available again. */
232 6be81433 2024-04-14 op if (ev_timer_pending(control_state.tout)) {
233 0a53bd62 2023-08-16 op ev_timer(NULL, NULL, NULL);
234 6be81433 2024-04-14 op ev_add(control_state.fd, EV_READ, control_accept, NULL);
235 3baa2617 2022-02-16 op }
236 3baa2617 2022-02-16 op
237 3baa2617 2022-02-16 op free(c);
238 87f575c3 2022-02-21 op }
239 87f575c3 2022-02-21 op
240 87f575c3 2022-02-21 op void
241 949b5c2e 2022-07-12 op control_notify(int type)
242 87f575c3 2022-02-21 op {
243 87f575c3 2022-02-21 op struct ctl_conn *c;
244 3dd90731 2023-01-09 op struct player_event ev;
245 87f575c3 2022-02-21 op
246 3dd90731 2023-01-09 op memset(&ev, 0, sizeof(ev));
247 3dd90731 2023-01-09 op ev.event = type;
248 3dd90731 2023-01-09 op ev.position = current_position;
249 3dd90731 2023-01-09 op ev.duration = current_duration;
250 3dd90731 2023-01-09 op ev.mode.repeat_one = repeat_one;
251 3dd90731 2023-01-09 op ev.mode.repeat_all = repeat_all;
252 3dd90731 2023-01-09 op ev.mode.consume = consume;
253 3dd90731 2023-01-09 op
254 87f575c3 2022-02-21 op TAILQ_FOREACH(c, &ctl_conns, entry) {
255 949b5c2e 2022-07-12 op if (!c->monitor)
256 87f575c3 2022-02-21 op continue;
257 87f575c3 2022-02-21 op
258 87f575c3 2022-02-21 op imsg_compose_event(&c->iev, IMSG_CTL_MONITOR, 0, 0,
259 3dd90731 2023-01-09 op -1, &ev, sizeof(ev));
260 87f575c3 2022-02-21 op }
261 3baa2617 2022-02-16 op }
262 3baa2617 2022-02-16 op
263 9881c98c 2022-07-13 op static int
264 9881c98c 2022-07-13 op new_mode(int val, int newval)
265 9881c98c 2022-07-13 op {
266 9881c98c 2022-07-13 op if (newval == MODE_UNDEF)
267 9881c98c 2022-07-13 op return val;
268 9881c98c 2022-07-13 op if (newval == MODE_TOGGLE)
269 9881c98c 2022-07-13 op return !val;
270 9881c98c 2022-07-13 op return !!newval;
271 9881c98c 2022-07-13 op }
272 9881c98c 2022-07-13 op
273 3baa2617 2022-02-16 op void
274 0a53bd62 2023-08-16 op control_dispatch_imsg(int fd, int event, void *bula)
275 3baa2617 2022-02-16 op {
276 310ef57c 2022-02-19 op struct ctl_conn *c;
277 50e0da0e 2024-01-21 op struct imsgbuf *imsgbuf;
278 310ef57c 2022-02-19 op struct imsg imsg;
279 9fb94242 2022-07-13 op struct player_mode mode;
280 15aecb89 2022-07-12 op struct player_seek seek;
281 3af93963 2022-03-02 op ssize_t n, off;
282 ddaffed6 2024-01-21 op int type;
283 3baa2617 2022-02-16 op
284 3baa2617 2022-02-16 op if ((c = control_connbyfd(fd)) == NULL) {
285 3baa2617 2022-02-16 op log_warnx("%s: fd %d: not found", __func__, fd);
286 3baa2617 2022-02-16 op return;
287 3baa2617 2022-02-16 op }
288 3baa2617 2022-02-16 op
289 50e0da0e 2024-01-21 op imsgbuf = &c->iev.imsgbuf;
290 50e0da0e 2024-01-21 op
291 6be81433 2024-04-14 op if (event & EV_READ) {
292 50e0da0e 2024-01-21 op if (((n = imsg_read(imsgbuf)) == -1 && errno != EAGAIN) ||
293 3baa2617 2022-02-16 op n == 0) {
294 3baa2617 2022-02-16 op control_close(fd);
295 3baa2617 2022-02-16 op return;
296 3baa2617 2022-02-16 op }
297 3baa2617 2022-02-16 op }
298 6be81433 2024-04-14 op if (event & EV_WRITE) {
299 50e0da0e 2024-01-21 op if (msgbuf_write(&imsgbuf->w) <= 0 && errno != EAGAIN) {
300 3baa2617 2022-02-16 op control_close(fd);
301 3baa2617 2022-02-16 op return;
302 3baa2617 2022-02-16 op }
303 3baa2617 2022-02-16 op }
304 3baa2617 2022-02-16 op
305 3baa2617 2022-02-16 op for (;;) {
306 50e0da0e 2024-01-21 op if ((n = imsg_get(imsgbuf, &imsg)) == -1) {
307 3baa2617 2022-02-16 op control_close(fd);
308 3baa2617 2022-02-16 op return;
309 3baa2617 2022-02-16 op }
310 3baa2617 2022-02-16 op if (n == 0)
311 3baa2617 2022-02-16 op break;
312 3baa2617 2022-02-16 op
313 ddaffed6 2024-01-21 op switch (type = imsg_get_type(&imsg)) {
314 3baa2617 2022-02-16 op case IMSG_CTL_PLAY:
315 3baa2617 2022-02-16 op switch (play_state) {
316 3baa2617 2022-02-16 op case STATE_STOPPED:
317 2f589330 2022-02-17 op main_playlist_resume();
318 3baa2617 2022-02-16 op break;
319 3baa2617 2022-02-16 op case STATE_PLAYING:
320 3baa2617 2022-02-16 op /* do nothing */
321 3baa2617 2022-02-16 op break;
322 3baa2617 2022-02-16 op case STATE_PAUSED:
323 950ad05c 2022-02-16 op play_state = STATE_PLAYING;
324 791d3db3 2022-07-09 op main_send_player(IMSG_RESUME, -1, NULL ,0);
325 3baa2617 2022-02-16 op break;
326 3baa2617 2022-02-16 op }
327 ddaffed6 2024-01-21 op control_notify(type);
328 3baa2617 2022-02-16 op break;
329 3baa2617 2022-02-16 op case IMSG_CTL_TOGGLE_PLAY:
330 3baa2617 2022-02-16 op switch (play_state) {
331 3baa2617 2022-02-16 op case STATE_STOPPED:
332 15aecb89 2022-07-12 op control_notify(IMSG_CTL_PLAY);
333 2f589330 2022-02-17 op main_playlist_resume();
334 3baa2617 2022-02-16 op break;
335 3baa2617 2022-02-16 op case STATE_PLAYING:
336 15aecb89 2022-07-12 op control_notify(IMSG_CTL_PAUSE);
337 2ac7eafd 2022-02-16 op play_state = STATE_PAUSED;
338 791d3db3 2022-07-09 op main_send_player(IMSG_PAUSE, -1, NULL, 0);
339 3baa2617 2022-02-16 op break;
340 3baa2617 2022-02-16 op case STATE_PAUSED:
341 15aecb89 2022-07-12 op control_notify(IMSG_CTL_PLAY);
342 2ac7eafd 2022-02-16 op play_state = STATE_PLAYING;
343 791d3db3 2022-07-09 op main_send_player(IMSG_RESUME, -1, NULL, 0);
344 3baa2617 2022-02-16 op break;
345 3baa2617 2022-02-16 op }
346 3baa2617 2022-02-16 op break;
347 3baa2617 2022-02-16 op case IMSG_CTL_PAUSE:
348 3baa2617 2022-02-16 op if (play_state != STATE_PLAYING)
349 3baa2617 2022-02-16 op break;
350 950ad05c 2022-02-16 op play_state = STATE_PAUSED;
351 791d3db3 2022-07-09 op main_send_player(IMSG_PAUSE, -1, NULL, 0);
352 ddaffed6 2024-01-21 op control_notify(type);
353 3baa2617 2022-02-16 op break;
354 3baa2617 2022-02-16 op case IMSG_CTL_STOP:
355 3baa2617 2022-02-16 op if (play_state == STATE_STOPPED)
356 3baa2617 2022-02-16 op break;
357 bfaa7897 2022-02-17 op play_state = STATE_STOPPED;
358 791d3db3 2022-07-09 op main_send_player(IMSG_STOP, -1, NULL, 0);
359 ddaffed6 2024-01-21 op control_notify(type);
360 3baa2617 2022-02-16 op break;
361 3baa2617 2022-02-16 op case IMSG_CTL_FLUSH:
362 3baa2617 2022-02-16 op playlist_truncate();
363 15aecb89 2022-07-12 op control_notify(IMSG_CTL_COMMIT);
364 41f5e35a 2022-02-16 op break;
365 3baa2617 2022-02-16 op case IMSG_CTL_SHOW:
366 d980494c 2022-02-16 op main_send_playlist(&c->iev);
367 bb3f279f 2022-02-16 op break;
368 bb3f279f 2022-02-16 op case IMSG_CTL_STATUS:
369 bb3f279f 2022-02-16 op main_send_status(&c->iev);
370 af27e631 2022-02-17 op break;
371 af27e631 2022-02-17 op case IMSG_CTL_NEXT:
372 ddaffed6 2024-01-21 op control_notify(type);
373 791d3db3 2022-07-09 op main_send_player(IMSG_STOP, -1, NULL, 0);
374 af27e631 2022-02-17 op main_playlist_advance();
375 af27e631 2022-02-17 op break;
376 af27e631 2022-02-17 op case IMSG_CTL_PREV:
377 ddaffed6 2024-01-21 op control_notify(type);
378 791d3db3 2022-07-09 op main_send_player(IMSG_STOP, -1, NULL, 0);
379 af27e631 2022-02-17 op main_playlist_previous();
380 d980494c 2022-02-16 op break;
381 a913de21 2022-02-17 op case IMSG_CTL_JUMP:
382 a913de21 2022-02-17 op main_playlist_jump(&c->iev, &imsg);
383 310ef57c 2022-02-19 op break;
384 9fb94242 2022-07-13 op case IMSG_CTL_MODE:
385 9307af9e 2024-01-21 op if (imsg_get_data(&imsg, &mode, sizeof(mode)) == -1) {
386 310ef57c 2022-02-19 op log_warnx("%s: got wrong size", __func__);
387 310ef57c 2022-02-19 op break;
388 310ef57c 2022-02-19 op }
389 9881c98c 2022-07-13 op consume = new_mode(consume, mode.consume);
390 9881c98c 2022-07-13 op repeat_all = new_mode(repeat_all, mode.repeat_all);
391 9881c98c 2022-07-13 op repeat_one = new_mode(repeat_one, mode.repeat_one);
392 ddaffed6 2024-01-21 op control_notify(type);
393 a913de21 2022-02-17 op break;
394 19d6b480 2022-02-17 op case IMSG_CTL_BEGIN:
395 19d6b480 2022-02-17 op if (control_state.tx != -1) {
396 19d6b480 2022-02-17 op main_senderr(&c->iev, "locked");
397 19d6b480 2022-02-17 op break;
398 19d6b480 2022-02-17 op }
399 50e0da0e 2024-01-21 op control_state.tx = imsgbuf->fd;
400 19d6b480 2022-02-17 op imsg_compose_event(&c->iev, IMSG_CTL_BEGIN, 0, 0, -1,
401 19d6b480 2022-02-17 op NULL, 0);
402 19d6b480 2022-02-17 op break;
403 19d6b480 2022-02-17 op case IMSG_CTL_ADD:
404 19d6b480 2022-02-17 op if (control_state.tx != -1 &&
405 50e0da0e 2024-01-21 op control_state.tx != imsgbuf->fd) {
406 19d6b480 2022-02-17 op main_senderr(&c->iev, "locked");
407 19d6b480 2022-02-17 op break;
408 19d6b480 2022-02-17 op }
409 19d6b480 2022-02-17 op main_enqueue(control_state.tx != -1,
410 19d6b480 2022-02-17 op &control_state.play,&c->iev, &imsg);
411 87f575c3 2022-02-21 op if (control_state.tx == -1)
412 ddaffed6 2024-01-21 op control_notify(type);
413 19d6b480 2022-02-17 op break;
414 19d6b480 2022-02-17 op case IMSG_CTL_COMMIT:
415 50e0da0e 2024-01-21 op if (control_state.tx != imsgbuf->fd) {
416 19d6b480 2022-02-17 op main_senderr(&c->iev, "locked");
417 19d6b480 2022-02-17 op break;
418 19d6b480 2022-02-17 op }
419 9307af9e 2024-01-21 op if (imsg_get_data(&imsg, &off, sizeof(off)) == -1) {
420 3af93963 2022-03-02 op main_senderr(&c->iev, "wrong size");
421 3af93963 2022-03-02 op break;
422 3af93963 2022-03-02 op }
423 3af93963 2022-03-02 op playlist_swap(&control_state.play, off);
424 19d6b480 2022-02-17 op memset(&control_state.play, 0,
425 19d6b480 2022-02-17 op sizeof(control_state.play));
426 19d6b480 2022-02-17 op control_state.tx = -1;
427 19d6b480 2022-02-17 op imsg_compose_event(&c->iev, IMSG_CTL_COMMIT, 0, 0, -1,
428 19d6b480 2022-02-17 op NULL, 0);
429 ddaffed6 2024-01-21 op control_notify(type);
430 19d6b480 2022-02-17 op break;
431 87f575c3 2022-02-21 op case IMSG_CTL_MONITOR:
432 87f575c3 2022-02-21 op c->monitor = 1;
433 87f575c3 2022-02-21 op break;
434 791d3db3 2022-07-09 op case IMSG_CTL_SEEK:
435 9307af9e 2024-01-21 op if (imsg_get_data(&imsg, &seek, sizeof(seek)) == -1) {
436 791d3db3 2022-07-09 op main_senderr(&c->iev, "wrong size");
437 3ad8bae8 2022-07-09 op break;
438 3ad8bae8 2022-07-09 op }
439 15aecb89 2022-07-12 op main_seek(&seek);
440 791d3db3 2022-07-09 op break;
441 3baa2617 2022-02-16 op default:
442 3baa2617 2022-02-16 op log_debug("%s: error handling imsg %d", __func__,
443 ddaffed6 2024-01-21 op type);
444 3baa2617 2022-02-16 op break;
445 3baa2617 2022-02-16 op }
446 3baa2617 2022-02-16 op imsg_free(&imsg);
447 3baa2617 2022-02-16 op }
448 3baa2617 2022-02-16 op
449 3baa2617 2022-02-16 op imsg_event_add(&c->iev);
450 3baa2617 2022-02-16 op }