2313
|
1 /* Audacious
|
|
2 * Copyright (C) 2005-2007 Audacious development team.
|
|
3 *
|
|
4 * Based on BMP:
|
|
5 * Copyright (C) 2003-2004 BMP development team.
|
|
6 *
|
|
7 * Based on XMMS:
|
|
8 * Copyright (C) 1998-2003 XMMS development team.
|
|
9 *
|
|
10 * This program is free software; you can redistribute it and/or modify
|
|
11 * it under the terms of the GNU General Public License as published by
|
|
12 * the Free Software Foundation; under version 2 of the License.
|
|
13 *
|
|
14 * This program is distributed in the hope that it will be useful,
|
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 * GNU General Public License for more details.
|
|
18 *
|
|
19 * You should have received a copy of the GNU General Public License
|
|
20 * along with this program; if not, write to the Free Software
|
|
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
22 */
|
|
23
|
|
24 #include "controlsocket.h"
|
|
25
|
|
26 #include <glib.h>
|
|
27 #include <stdlib.h>
|
|
28 #include <string.h>
|
|
29
|
|
30 #include <unistd.h>
|
|
31 #include <errno.h>
|
|
32 #include <sys/time.h>
|
|
33 #include <sys/types.h>
|
|
34 #include <sys/socket.h>
|
|
35 #include <sys/un.h>
|
|
36 #include <arpa/inet.h>
|
|
37 #include <netdb.h>
|
|
38 #include <netinet/in.h>
|
|
39 #include <unistd.h>
|
|
40 #include <grp.h>
|
|
41 #include <sys/time.h>
|
|
42 #include <sys/wait.h>
|
|
43 #include <sys/resource.h>
|
|
44 #include <sys/socket.h>
|
|
45 #include <fcntl.h>
|
|
46 #include <arpa/inet.h>
|
|
47
|
|
48 #include "main.h"
|
|
49 #include "ui_equalizer.h"
|
|
50 #include "ui_main.h"
|
|
51 #include "input.h"
|
|
52 #include "playback.h"
|
|
53 #include "playlist.h"
|
|
54 #include "ui_playlist.h"
|
|
55 #include "ui_preferences.h"
|
|
56 #include "libaudacious/beepctrl.h"
|
|
57 #include "memorypool.h"
|
|
58
|
|
59 #define CTRLSOCKET_BACKLOG 100
|
|
60 #define CTRLSOCKET_TIMEOUT 100000
|
|
61
|
|
62
|
|
63 static gint session_id = 0;
|
|
64
|
|
65 static gint ctrl_fd = 0;
|
|
66 static gchar *socket_name = NULL;
|
|
67
|
|
68 static gpointer ctrlsocket_func(gpointer);
|
|
69 static GThread *ctrlsocket_thread;
|
|
70
|
|
71 static GList *packet_list = NULL;
|
|
72 static GMutex *packet_list_mutex = NULL;
|
|
73
|
|
74 static gboolean started = FALSE;
|
|
75 static gboolean going = TRUE;
|
|
76 static GCond *start_cond = NULL;
|
|
77 static GMutex *status_mutex = NULL;
|
|
78 static MemoryPool *cs_pool = NULL;
|
|
79
|
|
80 static void
|
|
81 ctrlsocket_start_thread(void)
|
|
82 {
|
|
83 start_cond = g_cond_new();
|
|
84 status_mutex = g_mutex_new();
|
|
85 packet_list_mutex = g_mutex_new();
|
|
86
|
|
87 ctrlsocket_thread = g_thread_create(ctrlsocket_func, NULL, TRUE, NULL);
|
|
88 }
|
|
89
|
|
90 gboolean
|
|
91 ctrlsocket_setup(void)
|
|
92 {
|
|
93 cs_pool = memory_pool_new();
|
|
94
|
|
95 if (strcmp(cfg.session_uri_base, ""))
|
|
96 audacious_set_session_uri(cfg.session_uri_base);
|
|
97 else
|
|
98 return ctrlsocket_setup_unix();
|
|
99
|
|
100 if (!g_strncasecmp(cfg.session_uri_base, "tcp://", 6))
|
|
101 return ctrlsocket_setup_tcp();
|
|
102
|
|
103 return ctrlsocket_setup_unix();
|
|
104 }
|
|
105
|
|
106 gboolean
|
|
107 ctrlsocket_setup_unix(void)
|
|
108 {
|
|
109 struct sockaddr_un saddr;
|
|
110 gint i;
|
|
111 gint fd;
|
|
112
|
|
113 audacious_set_session_type(AUDACIOUS_TYPE_UNIX);
|
|
114
|
|
115 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
|
116 g_critical("ctrlsocket_setup(): Failed to open socket: %s",
|
|
117 strerror(errno));
|
|
118 return FALSE;
|
|
119 }
|
|
120
|
|
121 for (i = 0;; i++) {
|
|
122 saddr.sun_family = AF_UNIX;
|
|
123 g_snprintf(saddr.sun_path, sizeof(saddr.sun_path),
|
|
124 "%s/%s_%s.%d", g_get_tmp_dir(),
|
|
125 CTRLSOCKET_NAME, g_get_user_name(), i);
|
|
126
|
|
127 if (xmms_remote_is_running(i)) {
|
|
128 if (cfg.allow_multiple_instances)
|
|
129 continue;
|
|
130 break;
|
|
131 }
|
|
132
|
|
133 if ((unlink(saddr.sun_path) == -1) && errno != ENOENT) {
|
|
134 g_critical
|
|
135 ("ctrlsocket_setup(): Failed to unlink %s (Error: %s)",
|
|
136 saddr.sun_path, strerror(errno));
|
|
137 break;
|
|
138 }
|
|
139
|
|
140 if (bind(fd, (struct sockaddr *) &saddr, sizeof(saddr)) == -1) {
|
|
141 g_critical
|
|
142 ("ctrlsocket_setup(): Failed to assign %s to a socket (Error: %s)",
|
|
143 saddr.sun_path, strerror(errno));
|
|
144 break;
|
|
145 }
|
|
146
|
|
147 listen(fd, CTRLSOCKET_BACKLOG);
|
|
148
|
|
149 socket_name = memory_pool_strdup(cs_pool, saddr.sun_path);
|
|
150 ctrl_fd = fd;
|
|
151 session_id = i;
|
|
152 going = TRUE;
|
|
153
|
|
154 ctrlsocket_start_thread();
|
|
155
|
|
156 return TRUE;
|
|
157 }
|
|
158
|
|
159 close(fd);
|
|
160
|
|
161 return FALSE;
|
|
162 }
|
|
163
|
|
164 gboolean
|
|
165 ctrlsocket_setup_tcp(void)
|
|
166 {
|
|
167 struct sockaddr_in saddr;
|
|
168 gint i;
|
|
169 gint fd;
|
|
170
|
|
171 audacious_set_session_type(AUDACIOUS_TYPE_TCP);
|
|
172
|
|
173 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
|
|
174 g_critical("ctrlsocket_setup(): Failed to open socket: %s",
|
|
175 strerror(errno));
|
|
176 return FALSE;
|
|
177 }
|
|
178
|
|
179 for (i = 0;; i++) {
|
|
180 memset(&saddr, '\0', sizeof(saddr));
|
|
181 saddr.sin_family = AF_INET;
|
|
182 saddr.sin_port = htons(37370 + i);
|
|
183
|
|
184 if (xmms_remote_is_running(i)) {
|
|
185 if (cfg.allow_multiple_instances)
|
|
186 continue;
|
|
187 break;
|
|
188 }
|
|
189
|
|
190 if (bind(fd, (struct sockaddr *) &saddr, sizeof(saddr)) == -1) {
|
|
191 g_critical
|
|
192 ("ctrlsocket_setup(): Failed to bind the socket (Error: %s)",
|
|
193 strerror(errno));
|
|
194 break;
|
|
195 }
|
|
196
|
|
197 listen(fd, CTRLSOCKET_BACKLOG);
|
|
198
|
|
199 ctrl_fd = fd;
|
|
200 session_id = i;
|
|
201 going = TRUE;
|
|
202
|
|
203 ctrlsocket_start_thread();
|
|
204
|
|
205 return TRUE;
|
|
206 }
|
|
207
|
|
208 close(fd);
|
|
209
|
|
210 return FALSE;
|
|
211 }
|
|
212
|
|
213 gint
|
|
214 ctrlsocket_get_session_id(void)
|
|
215 {
|
|
216 return session_id;
|
|
217 }
|
|
218
|
|
219 void
|
|
220 ctrlsocket_cleanup(void)
|
|
221 {
|
|
222 if (ctrl_fd)
|
|
223 {
|
|
224 g_mutex_lock(status_mutex);
|
|
225 going = FALSE;
|
|
226 g_cond_signal(start_cond);
|
|
227 g_mutex_unlock(status_mutex);
|
|
228
|
|
229 /* wait for ctrlsocket_thread to terminate */
|
|
230 g_thread_join(ctrlsocket_thread);
|
|
231
|
|
232 /* close and remove socket */
|
|
233 close(ctrl_fd);
|
|
234 ctrl_fd = 0;
|
|
235
|
|
236 if (socket_name != NULL)
|
|
237 {
|
|
238 unlink(socket_name);
|
|
239 memory_pool_release(cs_pool, socket_name);
|
|
240 }
|
|
241
|
|
242 g_cond_free(start_cond);
|
|
243 g_mutex_free(status_mutex);
|
|
244 g_mutex_free(packet_list_mutex);
|
|
245 }
|
|
246
|
|
247 memory_pool_destroy(cs_pool);
|
|
248 }
|
|
249
|
|
250 void
|
|
251 ctrlsocket_start(void)
|
|
252 {
|
|
253 /* tell control socket thread to go 'live' i.e. start handling
|
|
254 * packets */
|
|
255 g_mutex_lock(status_mutex);
|
|
256 started = TRUE;
|
|
257 g_cond_signal(start_cond);
|
|
258 g_mutex_unlock(status_mutex);
|
|
259 }
|
|
260
|
|
261 static void
|
|
262 ctrl_write_packet(gint fd, gpointer data, gint length)
|
|
263 {
|
|
264 ServerPktHeader pkthdr;
|
|
265
|
|
266 memset(&pkthdr, '\0', sizeof(ServerPktHeader));
|
|
267
|
|
268 pkthdr.version = XMMS_PROTOCOL_VERSION;
|
|
269 pkthdr.data_length = length;
|
|
270 if ((size_t)write(fd, &pkthdr, sizeof(ServerPktHeader)) < sizeof(pkthdr))
|
|
271 return;
|
|
272 if (data && length > 0)
|
|
273 write(fd, data, length);
|
|
274 }
|
|
275
|
|
276 static void
|
|
277 ctrl_write_gint(gint fd, gint val)
|
|
278 {
|
|
279 ctrl_write_packet(fd, &val, sizeof(gint));
|
|
280 }
|
|
281
|
|
282 static void
|
|
283 ctrl_write_gfloat(gint fd, gfloat val)
|
|
284 {
|
|
285 ctrl_write_packet(fd, &val, sizeof(gfloat));
|
|
286 }
|
|
287
|
|
288 static void
|
|
289 ctrl_write_gboolean(gint fd, gboolean bool)
|
|
290 {
|
|
291 ctrl_write_packet(fd, &bool, sizeof(gboolean));
|
|
292 }
|
|
293
|
|
294 static void
|
|
295 ctrl_write_string(gint fd, gchar * string)
|
|
296 {
|
|
297 ctrl_write_packet(fd, string, string ? strlen(string) + 1 : 0);
|
|
298 }
|
|
299
|
|
300 static void
|
|
301 ctrl_ack_packet(PacketNode * pkt)
|
|
302 {
|
|
303 ctrl_write_packet(pkt->fd, NULL, 0);
|
|
304 close(pkt->fd);
|
|
305 if (pkt->data)
|
|
306 memory_pool_release(cs_pool, pkt->data);
|
|
307 memory_pool_release(cs_pool, pkt);
|
|
308 }
|
|
309
|
|
310 static gboolean
|
|
311 ctrlsocket_is_going(void)
|
|
312 {
|
|
313 gboolean result;
|
|
314
|
|
315 g_mutex_lock(status_mutex);
|
|
316 result = going;
|
|
317 g_mutex_unlock(status_mutex);
|
|
318
|
|
319 return result;
|
|
320 }
|
|
321
|
|
322 static gpointer
|
|
323 ctrlsocket_func(gpointer arg)
|
|
324 {
|
|
325 fd_set set;
|
|
326 struct timeval tv;
|
|
327 struct sockaddr_un saddr;
|
|
328 gint fd, b, i;
|
|
329 gint info[3];
|
|
330 gint32 v[2];
|
|
331 PacketNode *pkt;
|
|
332 socklen_t len;
|
|
333 gfloat fval[11];
|
|
334
|
|
335 g_mutex_lock(status_mutex);
|
|
336 while (!started && going)
|
|
337 g_cond_wait(start_cond, status_mutex);
|
|
338 g_mutex_unlock(status_mutex);
|
|
339
|
|
340 while (ctrlsocket_is_going()) {
|
|
341 FD_ZERO(&set);
|
|
342 FD_SET(ctrl_fd, &set);
|
|
343 tv.tv_sec = 0;
|
|
344 tv.tv_usec = CTRLSOCKET_TIMEOUT;
|
|
345 len = sizeof(saddr);
|
|
346 if (select(ctrl_fd + 1, &set, NULL, NULL, &tv) <= 0)
|
|
347 continue;
|
|
348 if ((fd = accept(ctrl_fd, (struct sockaddr *) &saddr, &len)) == -1)
|
|
349 continue;
|
|
350
|
|
351 pkt = memory_pool_alloc_object(cs_pool, PacketNode);
|
|
352 if ((size_t)read(fd, &pkt->hdr, sizeof(ClientPktHeader))
|
|
353 < sizeof(ClientPktHeader)) {
|
|
354 memory_pool_release(cs_pool, pkt);
|
|
355 continue;
|
|
356 }
|
|
357
|
|
358 if (pkt->hdr.data_length) {
|
|
359 size_t data_length = pkt->hdr.data_length;
|
|
360 pkt->data = memory_pool_allocate(cs_pool, data_length);
|
|
361 if ((size_t)read(fd, pkt->data, data_length) < data_length) {
|
|
362 memory_pool_release(cs_pool, pkt->data);
|
|
363 memory_pool_release(cs_pool, pkt);
|
|
364 g_warning("ctrlsocket_func(): Incomplete data packet dropped");
|
|
365 continue;
|
|
366 }
|
|
367 }
|
|
368
|
|
369 pkt->fd = fd;
|
|
370 switch (pkt->hdr.command) {
|
|
371 case CMD_GET_VERSION:
|
|
372 ctrl_write_gint(pkt->fd, 0x09a3);
|
|
373 ctrl_ack_packet(pkt);
|
|
374 break;
|
|
375 case CMD_IS_PLAYING:
|
|
376 ctrl_write_gboolean(pkt->fd, playback_get_playing());
|
|
377 ctrl_ack_packet(pkt);
|
|
378 break;
|
|
379 case CMD_IS_PAUSED:
|
|
380 ctrl_write_gboolean(pkt->fd, playback_get_paused());
|
|
381 ctrl_ack_packet(pkt);
|
|
382 break;
|
|
383 case CMD_GET_PLAYLIST_POS:
|
|
384 ctrl_write_gint(pkt->fd, playlist_get_position(playlist_get_active()));
|
|
385 ctrl_ack_packet(pkt);
|
|
386 break;
|
|
387 case CMD_GET_PLAYLIST_LENGTH:
|
|
388 ctrl_write_gint(pkt->fd, playlist_get_length(playlist_get_active()));
|
|
389 ctrl_ack_packet(pkt);
|
|
390 break;
|
|
391 case CMD_GET_PLAYQUEUE_LENGTH:
|
|
392 ctrl_write_gint(pkt->fd, playlist_queue_get_length(playlist_get_active()));
|
|
393 ctrl_ack_packet(pkt);
|
|
394 break;
|
|
395 case CMD_PLAYQUEUE_IS_QUEUED:
|
|
396 ctrl_write_gboolean(pkt->fd,
|
|
397 playlist_is_position_queued(playlist_get_active(), *((guint32 *) pkt->data)));
|
|
398 ctrl_ack_packet(pkt);
|
|
399 break;
|
|
400 case CMD_PLAYQUEUE_GET_POS:
|
|
401 if (pkt->data)
|
|
402 ctrl_write_gint(pkt->fd,
|
|
403 playlist_get_queue_position_number(playlist_get_active(),
|
|
404 *((guint32 *) pkt->data)));
|
|
405 else
|
|
406 ctrl_write_gint(pkt->fd, 0);
|
|
407
|
|
408 ctrl_ack_packet(pkt);
|
|
409 break;
|
|
410 case CMD_PLAYQUEUE_GET_QPOS:
|
|
411 if (pkt->data)
|
|
412 ctrl_write_gint(pkt->fd,
|
|
413 playlist_get_queue_position_number(playlist_get_active(),
|
|
414 *((guint32 *) pkt->data)));
|
|
415 else
|
|
416 ctrl_write_gint(pkt->fd, 0);
|
|
417
|
|
418 ctrl_ack_packet(pkt);
|
|
419 break;
|
|
420 case CMD_GET_OUTPUT_TIME:
|
|
421 if (playback_get_playing())
|
|
422 ctrl_write_gint(pkt->fd, playback_get_time());
|
|
423 else
|
|
424 ctrl_write_gint(pkt->fd, 0);
|
|
425 ctrl_ack_packet(pkt);
|
|
426 break;
|
|
427 case CMD_GET_VOLUME:
|
|
428 input_get_volume(&v[0], &v[1]);
|
|
429 ctrl_write_packet(pkt->fd, v, sizeof(v));
|
|
430 ctrl_ack_packet(pkt);
|
|
431 break;
|
|
432 case CMD_GET_BALANCE:
|
|
433 input_get_volume(&v[0], &v[1]);
|
|
434 if (v[0] < 0 || v[1] < 0)
|
|
435 b = 0;
|
|
436 else if (v[0] > v[1])
|
|
437 b = -100 + ((v[1] * 100) / v[0]);
|
|
438 else if (v[1] > v[0])
|
|
439 b = 100 - ((v[0] * 100) / v[1]);
|
|
440 else
|
|
441 b = 0;
|
|
442 ctrl_write_gint(pkt->fd, b);
|
|
443 ctrl_ack_packet(pkt);
|
|
444 break;
|
|
445 case CMD_GET_SKIN:
|
|
446 ctrl_write_string(pkt->fd, bmp_active_skin->path);
|
|
447 ctrl_ack_packet(pkt);
|
|
448 break;
|
|
449 case CMD_GET_PLAYLIST_FILE:
|
|
450 if (pkt->data) {
|
|
451 gchar *filename;
|
|
452 filename = playlist_get_filename(playlist_get_active(), *((guint32 *) pkt->data));
|
|
453 ctrl_write_string(pkt->fd, filename);
|
|
454 g_free(filename);
|
|
455 }
|
|
456 else
|
|
457 ctrl_write_string(pkt->fd, NULL);
|
|
458 ctrl_ack_packet(pkt);
|
|
459 break;
|
|
460 case CMD_GET_PLAYLIST_TITLE:
|
|
461 if (pkt->data) {
|
|
462 gchar *title;
|
|
463 title = playlist_get_songtitle(playlist_get_active(), *((guint32 *) pkt->data));
|
|
464 ctrl_write_string(pkt->fd, title);
|
|
465 g_free(title);
|
|
466 }
|
|
467 else
|
|
468 ctrl_write_string(pkt->fd, NULL);
|
|
469 ctrl_ack_packet(pkt);
|
|
470 break;
|
|
471 case CMD_GET_PLAYLIST_TIME:
|
|
472 if (pkt->data)
|
|
473 ctrl_write_gint(pkt->fd,
|
|
474 playlist_get_songtime(playlist_get_active(),
|
|
475 *((guint32 *) pkt->data)));
|
|
476 else
|
|
477 ctrl_write_gint(pkt->fd, -1);
|
|
478
|
|
479 ctrl_ack_packet(pkt);
|
|
480 break;
|
|
481 case CMD_GET_INFO:
|
|
482 playback_get_sample_params(&info[0], &info[1], &info[2]);
|
|
483 ctrl_write_packet(pkt->fd, info, 3 * sizeof(gint));
|
|
484 ctrl_ack_packet(pkt);
|
|
485 break;
|
|
486 case CMD_GET_EQ_DATA:
|
|
487 case CMD_SET_EQ_DATA:
|
|
488 /* obsolete */
|
|
489 ctrl_ack_packet(pkt);
|
|
490 break;
|
|
491 case CMD_PING:
|
|
492 ctrl_ack_packet(pkt);
|
|
493 break;
|
|
494 case CMD_PLAYLIST_ADD:
|
|
495 if (pkt->data) {
|
|
496 guint32 *dataptr = pkt->data;
|
|
497 while ((len = *dataptr) > 0) {
|
|
498 gchar *filename;
|
|
499
|
|
500 dataptr++;
|
|
501 filename = memory_pool_allocate(cs_pool, len);
|
|
502 memcpy(filename, dataptr, len);
|
|
503
|
|
504 GDK_THREADS_ENTER();
|
|
505 playlist_add_url(playlist_get_active(), filename);
|
|
506 GDK_THREADS_LEAVE();
|
|
507
|
|
508 memory_pool_release(cs_pool, filename);
|
|
509 dataptr += (len + 3) / 4;
|
|
510 }
|
|
511 }
|
|
512 ctrl_ack_packet(pkt);
|
|
513 break;
|
|
514 case CMD_PLAYLIST_ENQUEUE_TO_TEMP:
|
|
515 {
|
|
516 Playlist *new_pl = playlist_new();
|
|
517
|
|
518 GDK_THREADS_ENTER();
|
|
519 playlist_select_playlist(new_pl);
|
|
520 playlist_add_url(new_pl, pkt->data);
|
|
521 GDK_THREADS_LEAVE();
|
|
522
|
|
523 ctrl_ack_packet(pkt);
|
|
524 }
|
|
525 break;
|
|
526 case CMD_PLAYLIST_ADD_URL_STRING:
|
|
527 GDK_THREADS_ENTER();
|
|
528 playlist_add_url(playlist_get_active(), pkt->data);
|
|
529 GDK_THREADS_LEAVE();
|
|
530
|
|
531 ctrl_ack_packet(pkt);
|
|
532 break;
|
|
533 case CMD_PLAYLIST_INS_URL_STRING:
|
|
534 if (pkt->data) {
|
|
535 gint pos = *(gint *) pkt->data;
|
|
536 gchar *ptr = pkt->data;
|
|
537 ptr += sizeof(gint);
|
|
538 playlist_ins_url(playlist_get_active(), ptr, pos);
|
|
539 }
|
|
540 ctrl_ack_packet(pkt);
|
|
541 break;
|
|
542 case CMD_PLAYLIST_DELETE:
|
|
543 GDK_THREADS_ENTER();
|
|
544 playlist_delete_index(playlist_get_active(), *((guint32 *) pkt->data));
|
|
545 GDK_THREADS_LEAVE();
|
|
546 ctrl_ack_packet(pkt);
|
|
547 break;
|
|
548 case CMD_PLAYLIST_CLEAR:
|
|
549 GDK_THREADS_ENTER();
|
|
550 playlist_clear(playlist_get_active());
|
|
551 mainwin_clear_song_info();
|
|
552 mainwin_set_info_text();
|
|
553 GDK_THREADS_LEAVE();
|
|
554 ctrl_ack_packet(pkt);
|
|
555 break;
|
|
556 case CMD_IS_MAIN_WIN:
|
|
557 ctrl_write_gboolean(pkt->fd, cfg.player_visible);
|
|
558 ctrl_ack_packet(pkt);
|
|
559 break;
|
|
560 case CMD_IS_PL_WIN:
|
|
561 ctrl_write_gboolean(pkt->fd, cfg.playlist_visible);
|
|
562 ctrl_ack_packet(pkt);
|
|
563 break;
|
|
564 case CMD_IS_EQ_WIN:
|
|
565 ctrl_write_gboolean(pkt->fd, cfg.equalizer_visible);
|
|
566 ctrl_ack_packet(pkt);
|
|
567 break;
|
|
568 case CMD_IS_REPEAT:
|
|
569 ctrl_write_gboolean(pkt->fd, cfg.repeat);
|
|
570 ctrl_ack_packet(pkt);
|
|
571 break;
|
|
572 case CMD_IS_SHUFFLE:
|
|
573 ctrl_write_gboolean(pkt->fd, cfg.shuffle);
|
|
574 ctrl_ack_packet(pkt);
|
|
575 break;
|
|
576 case CMD_IS_ADVANCE:
|
|
577 ctrl_write_gboolean(pkt->fd, !cfg.no_playlist_advance);
|
|
578 ctrl_ack_packet(pkt);
|
|
579 break;
|
|
580 case CMD_GET_EQ:
|
|
581 fval[0] = equalizerwin_get_preamp();
|
|
582 for (i = 0; i < 10; i++)
|
|
583 fval[i + 1] = equalizerwin_get_band(i);
|
|
584 ctrl_write_packet(pkt->fd, fval, 11 * sizeof(gfloat));
|
|
585 ctrl_ack_packet(pkt);
|
|
586 break;
|
|
587 case CMD_GET_EQ_PREAMP:
|
|
588 ctrl_write_gfloat(pkt->fd, equalizerwin_get_preamp());
|
|
589 ctrl_ack_packet(pkt);
|
|
590 break;
|
|
591 case CMD_GET_EQ_BAND:
|
|
592 i = *((guint32 *) pkt->data);
|
|
593 ctrl_write_gfloat(pkt->fd, equalizerwin_get_band(i));
|
|
594 ctrl_ack_packet(pkt);
|
|
595 break;
|
|
596 default:
|
|
597 g_mutex_lock(packet_list_mutex);
|
|
598 packet_list = g_list_append(packet_list, pkt);
|
|
599 ctrl_write_packet(pkt->fd, NULL, 0);
|
|
600 close(pkt->fd);
|
|
601 g_mutex_unlock(packet_list_mutex);
|
|
602 break;
|
|
603 }
|
|
604 }
|
|
605 g_thread_exit(NULL);
|
|
606
|
|
607 /* Used to suppress GCC warnings. Sometimes you'd wish C has
|
|
608 native threading support :p */
|
|
609 return NULL;
|
|
610 }
|
|
611
|
|
612 void
|
|
613 ctrlsocket_check(void)
|
|
614 {
|
|
615 GList *pkt_list, *next;
|
|
616 PacketNode *pkt;
|
|
617 gpointer data;
|
|
618 guint32 v[2], i, num;
|
|
619 gboolean tbool;
|
|
620 gfloat *fval, f;
|
|
621
|
|
622 g_mutex_lock(packet_list_mutex);
|
|
623 for (pkt_list = packet_list; pkt_list; pkt_list = next) {
|
|
624 pkt = pkt_list->data;
|
|
625 data = pkt->data;
|
|
626
|
|
627 switch (pkt->hdr.command) {
|
|
628 case CMD_PLAY:
|
|
629 if (playback_get_paused())
|
|
630 playback_pause();
|
|
631 else if (playlist_get_length(playlist_get_active()))
|
|
632 playback_initiate();
|
|
633 else
|
|
634 mainwin_eject_pushed();
|
|
635 break;
|
|
636 case CMD_PAUSE:
|
|
637 playback_pause();
|
|
638 break;
|
|
639 case CMD_STOP:
|
|
640 ip_data.stop = TRUE;
|
|
641 playback_stop();
|
|
642 ip_data.stop = FALSE;
|
|
643 mainwin_clear_song_info();
|
|
644 break;
|
|
645 case CMD_PLAY_PAUSE:
|
|
646 if (playback_get_playing())
|
|
647 playback_pause();
|
|
648 else
|
|
649 playback_initiate();
|
|
650 break;
|
|
651 case CMD_PLAYQUEUE_ADD:
|
|
652 num = *((guint32 *) data);
|
|
653 if (num < (guint)playlist_get_length(playlist_get_active()))
|
|
654 playlist_queue_position(playlist_get_active(), num);
|
|
655 break;
|
|
656 case CMD_PLAYQUEUE_REMOVE:
|
|
657 num = *((guint32 *) data);
|
|
658 if (num < (guint)playlist_get_length(playlist_get_active()))
|
|
659 playlist_queue_remove(playlist_get_active(), num);
|
|
660 break;
|
|
661 case CMD_PLAYQUEUE_CLEAR:
|
|
662 playlist_clear_queue(playlist_get_active());
|
|
663 break;
|
|
664 case CMD_SET_PLAYLIST_POS:
|
|
665 num = *((guint32 *) data);
|
|
666 if (num < (guint)playlist_get_length(playlist_get_active()))
|
|
667 playlist_set_position(playlist_get_active(), num);
|
|
668 break;
|
|
669 case CMD_JUMP_TO_TIME:
|
|
670 num = *((guint32 *) data);
|
|
671 if (playlist_get_current_length(playlist_get_active()) > 0 &&
|
|
672 num < (guint)playlist_get_current_length(playlist_get_active()))
|
|
673 playback_seek(num / 1000);
|
|
674 break;
|
|
675 case CMD_SET_VOLUME:
|
|
676 v[0] = ((guint32 *) data)[0];
|
|
677 v[1] = ((guint32 *) data)[1];
|
|
678 for (i = 0; i < 2; i++) {
|
|
679 if (v[i] > 100)
|
|
680 v[i] = 100;
|
|
681 }
|
|
682 input_set_volume(v[0], v[1]);
|
|
683 break;
|
|
684 case CMD_SET_SKIN:
|
|
685 if (has_x11_connection == TRUE)
|
|
686 bmp_active_skin_load(data);
|
|
687 break;
|
|
688 case CMD_PL_WIN_TOGGLE:
|
|
689 if (has_x11_connection != TRUE)
|
|
690 break;
|
|
691 tbool = *((gboolean *) data);
|
|
692 if (tbool)
|
|
693 playlistwin_show();
|
|
694 else
|
|
695 playlistwin_hide();
|
|
696 break;
|
|
697 case CMD_EQ_WIN_TOGGLE:
|
|
698 if (has_x11_connection != TRUE)
|
|
699 break;
|
|
700 tbool = *((gboolean *) data);
|
|
701 equalizerwin_show(!!tbool);
|
|
702 break;
|
|
703 case CMD_SHOW_PREFS_BOX:
|
|
704 if (has_x11_connection != TRUE)
|
|
705 break;
|
|
706 show_prefs_window();
|
|
707 break;
|
|
708 case CMD_SHOW_JTF_BOX:
|
|
709 if (has_x11_connection != TRUE)
|
|
710 break;
|
|
711 mainwin_jump_to_file();
|
|
712 break;
|
|
713 case CMD_TOGGLE_AOT:
|
|
714 if (has_x11_connection != TRUE)
|
|
715 break;
|
|
716 tbool = *((gboolean *) data);
|
|
717 mainwin_set_always_on_top(tbool);
|
|
718 break;
|
|
719 case CMD_SHOW_ABOUT_BOX:
|
|
720 break;
|
|
721 case CMD_EJECT:
|
|
722 if (has_x11_connection != TRUE)
|
|
723 break;
|
|
724 mainwin_eject_pushed();
|
|
725 break;
|
|
726 case CMD_PLAYLIST_PREV:
|
|
727 playlist_prev(playlist_get_active());
|
|
728 break;
|
|
729 case CMD_PLAYLIST_NEXT:
|
|
730 playlist_next(playlist_get_active());
|
|
731 break;
|
|
732 case CMD_TOGGLE_REPEAT:
|
|
733 mainwin_repeat_pushed(!cfg.repeat);
|
|
734 break;
|
|
735 case CMD_TOGGLE_SHUFFLE:
|
|
736 mainwin_shuffle_pushed(!cfg.shuffle);
|
|
737 break;
|
|
738 case CMD_TOGGLE_ADVANCE:
|
|
739 /* FIXME: to be implemented */
|
|
740 break;
|
|
741 case CMD_MAIN_WIN_TOGGLE:
|
|
742 if (has_x11_connection != TRUE)
|
|
743 break;
|
|
744 tbool = *((gboolean *) data);
|
|
745 mainwin_show(!!tbool);
|
|
746 break;
|
|
747 case CMD_SET_EQ:
|
|
748 if (pkt->hdr.data_length >= 11 * sizeof(gfloat)) {
|
|
749 fval = (gfloat *) data;
|
|
750 equalizerwin_set_preamp(fval[0]);
|
|
751 for (i = 0; i < 10; i++)
|
|
752 equalizerwin_set_band(i, fval[i + 1]);
|
|
753 }
|
|
754 break;
|
|
755 case CMD_SET_EQ_PREAMP:
|
|
756 f = *((gfloat *) data);
|
|
757 equalizerwin_set_preamp(f);
|
|
758 break;
|
|
759 case CMD_SET_EQ_BAND:
|
|
760 if (pkt->hdr.data_length >= sizeof(gint) + sizeof(gfloat)) {
|
|
761 i = *((gint *) data);
|
|
762 f = *((gfloat *) ((gchar *) data + sizeof(gint)));
|
|
763 equalizerwin_set_band(i, f);
|
|
764 }
|
|
765 break;
|
|
766 case CMD_QUIT:
|
|
767 /*
|
|
768 * We unlock the packet_list_mutex to
|
|
769 * avoid that cleanup_ctrlsocket() can
|
|
770 * deadlock, mainwin_quit_cb() will
|
|
771 * never return anyway, so this will
|
|
772 * work ok.
|
|
773 */
|
|
774 g_mutex_unlock(packet_list_mutex);
|
|
775 mainwin_quit_cb();
|
|
776 break;
|
|
777 case CMD_ACTIVATE:
|
|
778 gtk_window_present(GTK_WINDOW(mainwin));
|
|
779 break;
|
|
780 default:
|
|
781 g_message("Unknown socket command received");
|
|
782 break;
|
|
783 }
|
|
784 next = g_list_next(pkt_list);
|
|
785 packet_list = g_list_remove_link(packet_list, pkt_list);
|
|
786 g_list_free_1(pkt_list);
|
|
787 if (pkt->data)
|
|
788 memory_pool_release(cs_pool, pkt->data);
|
|
789 memory_pool_release(cs_pool, pkt);
|
|
790 }
|
|
791 g_mutex_unlock(packet_list_mutex);
|
|
792 }
|