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 bb3f279f 2022-02-16 op #include <limits.h>
31 14be015f 2022-02-17 op #include <stdio.h>
32 3baa2617 2022-02-16 op #include <stdlib.h>
33 3baa2617 2022-02-16 op #include <string.h>
34 3baa2617 2022-02-16 op #include <unistd.h>
35 3baa2617 2022-02-16 op
36 3baa2617 2022-02-16 op #include "amused.h"
37 3baa2617 2022-02-16 op #include "log.h"
38 3baa2617 2022-02-16 op #include "control.h"
39 3baa2617 2022-02-16 op #include "playlist.h"
40 3baa2617 2022-02-16 op
41 3baa2617 2022-02-16 op #define CONTROL_BACKLOG 5
42 3baa2617 2022-02-16 op
43 3baa2617 2022-02-16 op struct {
44 3baa2617 2022-02-16 op struct event ev;
45 3baa2617 2022-02-16 op struct event evt;
46 3baa2617 2022-02-16 op int fd;
47 19d6b480 2022-02-17 op struct playlist play;
48 c6304ab9 2022-02-17 op int tx;
49 19d6b480 2022-02-17 op } control_state = {.fd = -1, .tx = -1};
50 3baa2617 2022-02-16 op
51 3baa2617 2022-02-16 op struct ctl_conn {
52 3baa2617 2022-02-16 op TAILQ_ENTRY(ctl_conn) entry;
53 87f575c3 2022-02-21 op int monitor; /* 1 if client is in monitor mode */
54 3baa2617 2022-02-16 op struct imsgev iev;
55 3baa2617 2022-02-16 op };
56 3baa2617 2022-02-16 op
57 3baa2617 2022-02-16 op TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
58 3baa2617 2022-02-16 op
59 3baa2617 2022-02-16 op struct ctl_conn *control_connbyfd(int);
60 3baa2617 2022-02-16 op struct ctl_conn *control_connbypid(pid_t);
61 3baa2617 2022-02-16 op void control_close(int);
62 3baa2617 2022-02-16 op
63 3baa2617 2022-02-16 op int
64 3baa2617 2022-02-16 op control_init(char *path)
65 3baa2617 2022-02-16 op {
66 3baa2617 2022-02-16 op struct sockaddr_un sun;
67 3baa2617 2022-02-16 op int fd;
68 3baa2617 2022-02-16 op mode_t old_umask;
69 3baa2617 2022-02-16 op
70 3baa2617 2022-02-16 op if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
71 3baa2617 2022-02-16 op 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 3baa2617 2022-02-16 op memset(&sun, 0, sizeof(sun));
77 3baa2617 2022-02-16 op sun.sun_family = AF_UNIX;
78 3baa2617 2022-02-16 op strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
79 3baa2617 2022-02-16 op
80 3baa2617 2022-02-16 op if (unlink(path) == -1)
81 3baa2617 2022-02-16 op if (errno != ENOENT) {
82 3baa2617 2022-02-16 op log_warn("%s: unlink %s", __func__, path);
83 3baa2617 2022-02-16 op close(fd);
84 3baa2617 2022-02-16 op return (-1);
85 3baa2617 2022-02-16 op }
86 3baa2617 2022-02-16 op
87 3baa2617 2022-02-16 op old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
88 3baa2617 2022-02-16 op if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
89 3baa2617 2022-02-16 op log_warn("%s: bind: %s", __func__, path);
90 3baa2617 2022-02-16 op close(fd);
91 3baa2617 2022-02-16 op umask(old_umask);
92 3baa2617 2022-02-16 op return (-1);
93 3baa2617 2022-02-16 op }
94 3baa2617 2022-02-16 op umask(old_umask);
95 3baa2617 2022-02-16 op
96 3baa2617 2022-02-16 op if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
97 3baa2617 2022-02-16 op log_warn("%s: chmod", __func__);
98 3baa2617 2022-02-16 op close(fd);
99 3baa2617 2022-02-16 op (void)unlink(path);
100 3baa2617 2022-02-16 op return (-1);
101 3baa2617 2022-02-16 op }
102 3baa2617 2022-02-16 op
103 3baa2617 2022-02-16 op return (fd);
104 3baa2617 2022-02-16 op }
105 3baa2617 2022-02-16 op
106 3baa2617 2022-02-16 op int
107 3baa2617 2022-02-16 op control_listen(int fd)
108 3baa2617 2022-02-16 op {
109 3baa2617 2022-02-16 op if (control_state.fd != -1)
110 3baa2617 2022-02-16 op fatalx("%s: received unexpected controlsock", __func__);
111 3baa2617 2022-02-16 op
112 3baa2617 2022-02-16 op control_state.fd = fd;
113 3baa2617 2022-02-16 op if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
114 3baa2617 2022-02-16 op log_warn("%s: listen", __func__);
115 3baa2617 2022-02-16 op return (-1);
116 3baa2617 2022-02-16 op }
117 3baa2617 2022-02-16 op
118 3baa2617 2022-02-16 op event_set(&control_state.ev, control_state.fd, EV_READ,
119 3baa2617 2022-02-16 op control_accept, NULL);
120 3baa2617 2022-02-16 op event_add(&control_state.ev, NULL);
121 3baa2617 2022-02-16 op evtimer_set(&control_state.evt, control_accept, NULL);
122 3baa2617 2022-02-16 op
123 3baa2617 2022-02-16 op return (0);
124 3baa2617 2022-02-16 op }
125 3baa2617 2022-02-16 op
126 3baa2617 2022-02-16 op void
127 3baa2617 2022-02-16 op control_accept(int listenfd, short event, void *bula)
128 3baa2617 2022-02-16 op {
129 3baa2617 2022-02-16 op int connfd;
130 3baa2617 2022-02-16 op socklen_t len;
131 3baa2617 2022-02-16 op struct sockaddr_un sun;
132 3baa2617 2022-02-16 op struct ctl_conn *c;
133 3baa2617 2022-02-16 op
134 3baa2617 2022-02-16 op event_add(&control_state.ev, NULL);
135 3baa2617 2022-02-16 op if ((event & EV_TIMEOUT))
136 3baa2617 2022-02-16 op return;
137 3baa2617 2022-02-16 op
138 3baa2617 2022-02-16 op len = sizeof(sun);
139 3baa2617 2022-02-16 op if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len,
140 3baa2617 2022-02-16 op SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) {
141 3baa2617 2022-02-16 op /*
142 3baa2617 2022-02-16 op * Pause accept if we are out of file descriptors, or
143 3baa2617 2022-02-16 op * libevent will haunt us here too.
144 3baa2617 2022-02-16 op */
145 3baa2617 2022-02-16 op if (errno == ENFILE || errno == EMFILE) {
146 3baa2617 2022-02-16 op struct timeval evtpause = { 1, 0 };
147 3baa2617 2022-02-16 op
148 3baa2617 2022-02-16 op event_del(&control_state.ev);
149 3baa2617 2022-02-16 op evtimer_add(&control_state.evt, &evtpause);
150 3baa2617 2022-02-16 op } else if (errno != EWOULDBLOCK && errno != EINTR &&
151 3baa2617 2022-02-16 op errno != ECONNABORTED)
152 3baa2617 2022-02-16 op log_warn("%s: accept4", __func__);
153 3baa2617 2022-02-16 op return;
154 3baa2617 2022-02-16 op }
155 3baa2617 2022-02-16 op
156 3baa2617 2022-02-16 op if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
157 3baa2617 2022-02-16 op log_warn("%s: calloc", __func__);
158 3baa2617 2022-02-16 op close(connfd);
159 3baa2617 2022-02-16 op return;
160 3baa2617 2022-02-16 op }
161 3baa2617 2022-02-16 op
162 3baa2617 2022-02-16 op imsg_init(&c->iev.ibuf, connfd);
163 3baa2617 2022-02-16 op c->iev.handler = control_dispatch_imsg;
164 3baa2617 2022-02-16 op c->iev.events = EV_READ;
165 3baa2617 2022-02-16 op event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
166 3baa2617 2022-02-16 op c->iev.handler, &c->iev);
167 3baa2617 2022-02-16 op event_add(&c->iev.ev, NULL);
168 3baa2617 2022-02-16 op
169 3baa2617 2022-02-16 op TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
170 3baa2617 2022-02-16 op }
171 3baa2617 2022-02-16 op
172 3baa2617 2022-02-16 op struct ctl_conn *
173 3baa2617 2022-02-16 op control_connbyfd(int fd)
174 3baa2617 2022-02-16 op {
175 3baa2617 2022-02-16 op struct ctl_conn *c;
176 3baa2617 2022-02-16 op
177 3baa2617 2022-02-16 op TAILQ_FOREACH(c, &ctl_conns, entry) {
178 3baa2617 2022-02-16 op if (c->iev.ibuf.fd == fd)
179 3baa2617 2022-02-16 op break;
180 3baa2617 2022-02-16 op }
181 3baa2617 2022-02-16 op
182 3baa2617 2022-02-16 op return (c);
183 3baa2617 2022-02-16 op }
184 3baa2617 2022-02-16 op
185 3baa2617 2022-02-16 op struct ctl_conn *
186 3baa2617 2022-02-16 op control_connbypid(pid_t pid)
187 3baa2617 2022-02-16 op {
188 3baa2617 2022-02-16 op struct ctl_conn *c;
189 3baa2617 2022-02-16 op
190 3baa2617 2022-02-16 op TAILQ_FOREACH(c, &ctl_conns, entry) {
191 3baa2617 2022-02-16 op if (c->iev.ibuf.pid == pid)
192 3baa2617 2022-02-16 op break;
193 3baa2617 2022-02-16 op }
194 3baa2617 2022-02-16 op
195 3baa2617 2022-02-16 op return (c);
196 3baa2617 2022-02-16 op }
197 3baa2617 2022-02-16 op
198 3baa2617 2022-02-16 op void
199 3baa2617 2022-02-16 op control_close(int fd)
200 3baa2617 2022-02-16 op {
201 3baa2617 2022-02-16 op struct ctl_conn *c;
202 3baa2617 2022-02-16 op
203 3baa2617 2022-02-16 op if ((c = control_connbyfd(fd)) == NULL) {
204 3baa2617 2022-02-16 op log_warnx("%s: fd %d: not found", __func__, fd);
205 3baa2617 2022-02-16 op return;
206 19d6b480 2022-02-17 op }
207 19d6b480 2022-02-17 op
208 19d6b480 2022-02-17 op /* abort the transaction if running by this user */
209 c6304ab9 2022-02-17 op if (control_state.tx != -1 && c->iev.ibuf.fd == control_state.tx) {
210 19d6b480 2022-02-17 op playlist_free(&control_state.play);
211 19d6b480 2022-02-17 op control_state.tx = -1;
212 3baa2617 2022-02-16 op }
213 3baa2617 2022-02-16 op
214 3baa2617 2022-02-16 op msgbuf_clear(&c->iev.ibuf.w);
215 3baa2617 2022-02-16 op TAILQ_REMOVE(&ctl_conns, c, entry);
216 3baa2617 2022-02-16 op
217 3baa2617 2022-02-16 op event_del(&c->iev.ev);
218 3baa2617 2022-02-16 op close(c->iev.ibuf.fd);
219 3baa2617 2022-02-16 op
220 3baa2617 2022-02-16 op /* Some file descriptors are available again. */
221 3baa2617 2022-02-16 op if (evtimer_pending(&control_state.evt, NULL)) {
222 3baa2617 2022-02-16 op evtimer_del(&control_state.evt);
223 3baa2617 2022-02-16 op event_add(&control_state.ev, NULL);
224 3baa2617 2022-02-16 op }
225 3baa2617 2022-02-16 op
226 3baa2617 2022-02-16 op free(c);
227 87f575c3 2022-02-21 op }
228 87f575c3 2022-02-21 op
229 87f575c3 2022-02-21 op void
230 949b5c2e 2022-07-12 op control_notify(int type)
231 87f575c3 2022-02-21 op {
232 87f575c3 2022-02-21 op struct ctl_conn *c;
233 3dd90731 2023-01-09 op struct player_event ev;
234 87f575c3 2022-02-21 op
235 3dd90731 2023-01-09 op memset(&ev, 0, sizeof(ev));
236 3dd90731 2023-01-09 op ev.event = type;
237 3dd90731 2023-01-09 op ev.position = current_position;
238 3dd90731 2023-01-09 op ev.duration = current_duration;
239 3dd90731 2023-01-09 op ev.mode.repeat_one = repeat_one;
240 3dd90731 2023-01-09 op ev.mode.repeat_all = repeat_all;
241 3dd90731 2023-01-09 op ev.mode.consume = consume;
242 3dd90731 2023-01-09 op
243 87f575c3 2022-02-21 op TAILQ_FOREACH(c, &ctl_conns, entry) {
244 949b5c2e 2022-07-12 op if (!c->monitor)
245 87f575c3 2022-02-21 op continue;
246 87f575c3 2022-02-21 op
247 87f575c3 2022-02-21 op imsg_compose_event(&c->iev, IMSG_CTL_MONITOR, 0, 0,
248 3dd90731 2023-01-09 op -1, &ev, sizeof(ev));
249 87f575c3 2022-02-21 op }
250 3baa2617 2022-02-16 op }
251 3baa2617 2022-02-16 op
252 9881c98c 2022-07-13 op static int
253 9881c98c 2022-07-13 op new_mode(int val, int newval)
254 9881c98c 2022-07-13 op {
255 9881c98c 2022-07-13 op if (newval == MODE_UNDEF)
256 9881c98c 2022-07-13 op return val;
257 9881c98c 2022-07-13 op if (newval == MODE_TOGGLE)
258 9881c98c 2022-07-13 op return !val;
259 9881c98c 2022-07-13 op return !!newval;
260 9881c98c 2022-07-13 op }
261 9881c98c 2022-07-13 op
262 3baa2617 2022-02-16 op void
263 3baa2617 2022-02-16 op control_dispatch_imsg(int fd, short event, void *bula)
264 3baa2617 2022-02-16 op {
265 310ef57c 2022-02-19 op struct ctl_conn *c;
266 310ef57c 2022-02-19 op struct imsg imsg;
267 9fb94242 2022-07-13 op struct player_mode mode;
268 15aecb89 2022-07-12 op struct player_seek seek;
269 3af93963 2022-03-02 op ssize_t n, off;
270 3baa2617 2022-02-16 op
271 3baa2617 2022-02-16 op if ((c = control_connbyfd(fd)) == NULL) {
272 3baa2617 2022-02-16 op log_warnx("%s: fd %d: not found", __func__, fd);
273 3baa2617 2022-02-16 op return;
274 3baa2617 2022-02-16 op }
275 3baa2617 2022-02-16 op
276 3baa2617 2022-02-16 op if (event & EV_READ) {
277 3baa2617 2022-02-16 op if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
278 3baa2617 2022-02-16 op n == 0) {
279 3baa2617 2022-02-16 op control_close(fd);
280 3baa2617 2022-02-16 op return;
281 3baa2617 2022-02-16 op }
282 3baa2617 2022-02-16 op }
283 3baa2617 2022-02-16 op if (event & EV_WRITE) {
284 3baa2617 2022-02-16 op if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
285 3baa2617 2022-02-16 op control_close(fd);
286 3baa2617 2022-02-16 op return;
287 3baa2617 2022-02-16 op }
288 3baa2617 2022-02-16 op }
289 3baa2617 2022-02-16 op
290 3baa2617 2022-02-16 op for (;;) {
291 3baa2617 2022-02-16 op if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
292 3baa2617 2022-02-16 op control_close(fd);
293 3baa2617 2022-02-16 op return;
294 3baa2617 2022-02-16 op }
295 3baa2617 2022-02-16 op if (n == 0)
296 3baa2617 2022-02-16 op break;
297 3baa2617 2022-02-16 op
298 3baa2617 2022-02-16 op switch (imsg.hdr.type) {
299 3baa2617 2022-02-16 op case IMSG_CTL_PLAY:
300 3baa2617 2022-02-16 op switch (play_state) {
301 3baa2617 2022-02-16 op case STATE_STOPPED:
302 2f589330 2022-02-17 op main_playlist_resume();
303 3baa2617 2022-02-16 op break;
304 3baa2617 2022-02-16 op case STATE_PLAYING:
305 3baa2617 2022-02-16 op /* do nothing */
306 3baa2617 2022-02-16 op break;
307 3baa2617 2022-02-16 op case STATE_PAUSED:
308 950ad05c 2022-02-16 op play_state = STATE_PLAYING;
309 791d3db3 2022-07-09 op main_send_player(IMSG_RESUME, -1, NULL ,0);
310 3baa2617 2022-02-16 op break;
311 3baa2617 2022-02-16 op }
312 949b5c2e 2022-07-12 op control_notify(imsg.hdr.type);
313 3baa2617 2022-02-16 op break;
314 3baa2617 2022-02-16 op case IMSG_CTL_TOGGLE_PLAY:
315 3baa2617 2022-02-16 op switch (play_state) {
316 3baa2617 2022-02-16 op case STATE_STOPPED:
317 15aecb89 2022-07-12 op control_notify(IMSG_CTL_PLAY);
318 2f589330 2022-02-17 op main_playlist_resume();
319 3baa2617 2022-02-16 op break;
320 3baa2617 2022-02-16 op case STATE_PLAYING:
321 15aecb89 2022-07-12 op control_notify(IMSG_CTL_PAUSE);
322 2ac7eafd 2022-02-16 op play_state = STATE_PAUSED;
323 791d3db3 2022-07-09 op main_send_player(IMSG_PAUSE, -1, NULL, 0);
324 3baa2617 2022-02-16 op break;
325 3baa2617 2022-02-16 op case STATE_PAUSED:
326 15aecb89 2022-07-12 op control_notify(IMSG_CTL_PLAY);
327 2ac7eafd 2022-02-16 op play_state = STATE_PLAYING;
328 791d3db3 2022-07-09 op main_send_player(IMSG_RESUME, -1, NULL, 0);
329 3baa2617 2022-02-16 op break;
330 3baa2617 2022-02-16 op }
331 3baa2617 2022-02-16 op break;
332 3baa2617 2022-02-16 op case IMSG_CTL_PAUSE:
333 3baa2617 2022-02-16 op if (play_state != STATE_PLAYING)
334 3baa2617 2022-02-16 op break;
335 950ad05c 2022-02-16 op play_state = STATE_PAUSED;
336 791d3db3 2022-07-09 op main_send_player(IMSG_PAUSE, -1, NULL, 0);
337 949b5c2e 2022-07-12 op control_notify(imsg.hdr.type);
338 3baa2617 2022-02-16 op break;
339 3baa2617 2022-02-16 op case IMSG_CTL_STOP:
340 3baa2617 2022-02-16 op if (play_state == STATE_STOPPED)
341 3baa2617 2022-02-16 op break;
342 bfaa7897 2022-02-17 op play_state = STATE_STOPPED;
343 791d3db3 2022-07-09 op main_send_player(IMSG_STOP, -1, NULL, 0);
344 949b5c2e 2022-07-12 op control_notify(imsg.hdr.type);
345 3baa2617 2022-02-16 op break;
346 3baa2617 2022-02-16 op case IMSG_CTL_FLUSH:
347 3baa2617 2022-02-16 op playlist_truncate();
348 15aecb89 2022-07-12 op control_notify(IMSG_CTL_COMMIT);
349 41f5e35a 2022-02-16 op break;
350 3baa2617 2022-02-16 op case IMSG_CTL_SHOW:
351 d980494c 2022-02-16 op main_send_playlist(&c->iev);
352 bb3f279f 2022-02-16 op break;
353 bb3f279f 2022-02-16 op case IMSG_CTL_STATUS:
354 bb3f279f 2022-02-16 op main_send_status(&c->iev);
355 af27e631 2022-02-17 op break;
356 af27e631 2022-02-17 op case IMSG_CTL_NEXT:
357 15aecb89 2022-07-12 op control_notify(imsg.hdr.type);
358 791d3db3 2022-07-09 op main_send_player(IMSG_STOP, -1, NULL, 0);
359 af27e631 2022-02-17 op main_playlist_advance();
360 af27e631 2022-02-17 op break;
361 af27e631 2022-02-17 op case IMSG_CTL_PREV:
362 15aecb89 2022-07-12 op control_notify(imsg.hdr.type);
363 791d3db3 2022-07-09 op main_send_player(IMSG_STOP, -1, NULL, 0);
364 af27e631 2022-02-17 op main_playlist_previous();
365 d980494c 2022-02-16 op break;
366 a913de21 2022-02-17 op case IMSG_CTL_JUMP:
367 a913de21 2022-02-17 op main_playlist_jump(&c->iev, &imsg);
368 310ef57c 2022-02-19 op break;
369 9fb94242 2022-07-13 op case IMSG_CTL_MODE:
370 9fb94242 2022-07-13 op if (IMSG_DATA_SIZE(imsg) != sizeof(mode)) {
371 310ef57c 2022-02-19 op log_warnx("%s: got wrong size", __func__);
372 310ef57c 2022-02-19 op break;
373 310ef57c 2022-02-19 op }
374 9fb94242 2022-07-13 op memcpy(&mode, imsg.data, sizeof(mode));
375 9881c98c 2022-07-13 op consume = new_mode(consume, mode.consume);
376 9881c98c 2022-07-13 op repeat_all = new_mode(repeat_all, mode.repeat_all);
377 9881c98c 2022-07-13 op repeat_one = new_mode(repeat_one, mode.repeat_one);
378 b72c0765 2022-07-13 op main_send_status(&c->iev);
379 949b5c2e 2022-07-12 op control_notify(imsg.hdr.type);
380 a913de21 2022-02-17 op break;
381 19d6b480 2022-02-17 op case IMSG_CTL_BEGIN:
382 19d6b480 2022-02-17 op if (control_state.tx != -1) {
383 19d6b480 2022-02-17 op main_senderr(&c->iev, "locked");
384 19d6b480 2022-02-17 op break;
385 19d6b480 2022-02-17 op }
386 c6304ab9 2022-02-17 op control_state.tx = c->iev.ibuf.fd;
387 19d6b480 2022-02-17 op imsg_compose_event(&c->iev, IMSG_CTL_BEGIN, 0, 0, -1,
388 19d6b480 2022-02-17 op NULL, 0);
389 19d6b480 2022-02-17 op break;
390 19d6b480 2022-02-17 op case IMSG_CTL_ADD:
391 19d6b480 2022-02-17 op if (control_state.tx != -1 &&
392 c6304ab9 2022-02-17 op control_state.tx != c->iev.ibuf.fd) {
393 19d6b480 2022-02-17 op main_senderr(&c->iev, "locked");
394 19d6b480 2022-02-17 op break;
395 19d6b480 2022-02-17 op }
396 19d6b480 2022-02-17 op main_enqueue(control_state.tx != -1,
397 19d6b480 2022-02-17 op &control_state.play,&c->iev, &imsg);
398 87f575c3 2022-02-21 op if (control_state.tx == -1)
399 949b5c2e 2022-07-12 op control_notify(imsg.hdr.type);
400 19d6b480 2022-02-17 op break;
401 19d6b480 2022-02-17 op case IMSG_CTL_COMMIT:
402 c6304ab9 2022-02-17 op if (control_state.tx != c->iev.ibuf.fd) {
403 19d6b480 2022-02-17 op main_senderr(&c->iev, "locked");
404 19d6b480 2022-02-17 op break;
405 19d6b480 2022-02-17 op }
406 3af93963 2022-03-02 op if (IMSG_DATA_SIZE(imsg) != sizeof(off)) {
407 3af93963 2022-03-02 op main_senderr(&c->iev, "wrong size");
408 3af93963 2022-03-02 op break;
409 3af93963 2022-03-02 op }
410 3af93963 2022-03-02 op memcpy(&off, imsg.data, sizeof(off));
411 3af93963 2022-03-02 op playlist_swap(&control_state.play, off);
412 19d6b480 2022-02-17 op memset(&control_state.play, 0,
413 19d6b480 2022-02-17 op sizeof(control_state.play));
414 19d6b480 2022-02-17 op control_state.tx = -1;
415 19d6b480 2022-02-17 op imsg_compose_event(&c->iev, IMSG_CTL_COMMIT, 0, 0, -1,
416 19d6b480 2022-02-17 op NULL, 0);
417 949b5c2e 2022-07-12 op control_notify(imsg.hdr.type);
418 19d6b480 2022-02-17 op break;
419 87f575c3 2022-02-21 op case IMSG_CTL_MONITOR:
420 87f575c3 2022-02-21 op c->monitor = 1;
421 87f575c3 2022-02-21 op break;
422 791d3db3 2022-07-09 op case IMSG_CTL_SEEK:
423 15aecb89 2022-07-12 op if (IMSG_DATA_SIZE(imsg) != sizeof(seek)) {
424 791d3db3 2022-07-09 op main_senderr(&c->iev, "wrong size");
425 3ad8bae8 2022-07-09 op break;
426 3ad8bae8 2022-07-09 op }
427 15aecb89 2022-07-12 op memcpy(&seek, imsg.data, sizeof(seek));
428 15aecb89 2022-07-12 op main_seek(&seek);
429 791d3db3 2022-07-09 op break;
430 3baa2617 2022-02-16 op default:
431 3baa2617 2022-02-16 op log_debug("%s: error handling imsg %d", __func__,
432 3baa2617 2022-02-16 op imsg.hdr.type);
433 3baa2617 2022-02-16 op break;
434 3baa2617 2022-02-16 op }
435 3baa2617 2022-02-16 op imsg_free(&imsg);
436 3baa2617 2022-02-16 op }
437 3baa2617 2022-02-16 op
438 3baa2617 2022-02-16 op imsg_event_add(&c->iev);
439 3baa2617 2022-02-16 op }
440 3baa2617 2022-02-16 op
441 3baa2617 2022-02-16 op int
442 3baa2617 2022-02-16 op control_imsg_relay(struct imsg *imsg)
443 3baa2617 2022-02-16 op {
444 3baa2617 2022-02-16 op struct ctl_conn *c;
445 3baa2617 2022-02-16 op
446 3baa2617 2022-02-16 op if ((c = control_connbypid(imsg->hdr.pid)) == NULL)
447 3baa2617 2022-02-16 op return (0);
448 3baa2617 2022-02-16 op
449 3baa2617 2022-02-16 op return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
450 3baa2617 2022-02-16 op -1, imsg->data, IMSG_DATA_SIZE(*imsg)));
451 3baa2617 2022-02-16 op }