Mercurial > audlegacy
annotate src/audacious/controlsocket.c @ 2361:f24ae4f40e29 trunk
[svn] - security and warning fixes from ssommer@suse
author | nenolod |
---|---|
date | Thu, 18 Jan 2007 03:02:55 -0800 |
parents | 3149d4b1a9a9 |
children | 847213941224 |
rev | line source |
---|---|
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)) | |
2361
f24ae4f40e29
[svn] - security and warning fixes from ssommer@suse
nenolod
parents:
2313
diff
changeset
|
271 { |
f24ae4f40e29
[svn] - security and warning fixes from ssommer@suse
nenolod
parents:
2313
diff
changeset
|
272 g_warning("ctrl_write_packet: failed to write packet header"); |
2313 | 273 return; |
2361
f24ae4f40e29
[svn] - security and warning fixes from ssommer@suse
nenolod
parents:
2313
diff
changeset
|
274 } |
2313 | 275 if (data && length > 0) |
2361
f24ae4f40e29
[svn] - security and warning fixes from ssommer@suse
nenolod
parents:
2313
diff
changeset
|
276 if(length != write(fd, data, length)) |
f24ae4f40e29
[svn] - security and warning fixes from ssommer@suse
nenolod
parents:
2313
diff
changeset
|
277 { |
f24ae4f40e29
[svn] - security and warning fixes from ssommer@suse
nenolod
parents:
2313
diff
changeset
|
278 g_warning("ctrl_write_packet: failed to write packet"); |
f24ae4f40e29
[svn] - security and warning fixes from ssommer@suse
nenolod
parents:
2313
diff
changeset
|
279 return; |
f24ae4f40e29
[svn] - security and warning fixes from ssommer@suse
nenolod
parents:
2313
diff
changeset
|
280 } |
2313 | 281 } |
282 | |
283 static void | |
284 ctrl_write_gint(gint fd, gint val) | |
285 { | |
286 ctrl_write_packet(fd, &val, sizeof(gint)); | |
287 } | |
288 | |
289 static void | |
290 ctrl_write_gfloat(gint fd, gfloat val) | |
291 { | |
292 ctrl_write_packet(fd, &val, sizeof(gfloat)); | |
293 } | |
294 | |
295 static void | |
296 ctrl_write_gboolean(gint fd, gboolean bool) | |
297 { | |
298 ctrl_write_packet(fd, &bool, sizeof(gboolean)); | |
299 } | |
300 | |
301 static void | |
302 ctrl_write_string(gint fd, gchar * string) | |
303 { | |
304 ctrl_write_packet(fd, string, string ? strlen(string) + 1 : 0); | |
305 } | |
306 | |
307 static void | |
308 ctrl_ack_packet(PacketNode * pkt) | |
309 { | |
310 ctrl_write_packet(pkt->fd, NULL, 0); | |
311 close(pkt->fd); | |
312 if (pkt->data) | |
313 memory_pool_release(cs_pool, pkt->data); | |
314 memory_pool_release(cs_pool, pkt); | |
315 } | |
316 | |
317 static gboolean | |
318 ctrlsocket_is_going(void) | |
319 { | |
320 gboolean result; | |
321 | |
322 g_mutex_lock(status_mutex); | |
323 result = going; | |
324 g_mutex_unlock(status_mutex); | |
325 | |
326 return result; | |
327 } | |
328 | |
329 static gpointer | |
330 ctrlsocket_func(gpointer arg) | |
331 { | |
332 fd_set set; | |
333 struct timeval tv; | |
334 struct sockaddr_un saddr; | |
335 gint fd, b, i; | |
336 gint info[3]; | |
337 gint32 v[2]; | |
338 PacketNode *pkt; | |
339 socklen_t len; | |
340 gfloat fval[11]; | |
341 | |
342 g_mutex_lock(status_mutex); | |
343 while (!started && going) | |
344 g_cond_wait(start_cond, status_mutex); | |
345 g_mutex_unlock(status_mutex); | |
346 | |
347 while (ctrlsocket_is_going()) { | |
348 FD_ZERO(&set); | |
349 FD_SET(ctrl_fd, &set); | |
350 tv.tv_sec = 0; | |
351 tv.tv_usec = CTRLSOCKET_TIMEOUT; | |
352 len = sizeof(saddr); | |
353 if (select(ctrl_fd + 1, &set, NULL, NULL, &tv) <= 0) | |
354 continue; | |
355 if ((fd = accept(ctrl_fd, (struct sockaddr *) &saddr, &len)) == -1) | |
356 continue; | |
357 | |
358 pkt = memory_pool_alloc_object(cs_pool, PacketNode); | |
359 if ((size_t)read(fd, &pkt->hdr, sizeof(ClientPktHeader)) | |
360 < sizeof(ClientPktHeader)) { | |
361 memory_pool_release(cs_pool, pkt); | |
362 continue; | |
363 } | |
364 | |
365 if (pkt->hdr.data_length) { | |
366 size_t data_length = pkt->hdr.data_length; | |
367 pkt->data = memory_pool_allocate(cs_pool, data_length); | |
368 if ((size_t)read(fd, pkt->data, data_length) < data_length) { | |
369 memory_pool_release(cs_pool, pkt->data); | |
370 memory_pool_release(cs_pool, pkt); | |
371 g_warning("ctrlsocket_func(): Incomplete data packet dropped"); | |
372 continue; | |
373 } | |
374 } | |
375 | |
376 pkt->fd = fd; | |
377 switch (pkt->hdr.command) { | |
378 case CMD_GET_VERSION: | |
379 ctrl_write_gint(pkt->fd, 0x09a3); | |
380 ctrl_ack_packet(pkt); | |
381 break; | |
382 case CMD_IS_PLAYING: | |
383 ctrl_write_gboolean(pkt->fd, playback_get_playing()); | |
384 ctrl_ack_packet(pkt); | |
385 break; | |
386 case CMD_IS_PAUSED: | |
387 ctrl_write_gboolean(pkt->fd, playback_get_paused()); | |
388 ctrl_ack_packet(pkt); | |
389 break; | |
390 case CMD_GET_PLAYLIST_POS: | |
391 ctrl_write_gint(pkt->fd, playlist_get_position(playlist_get_active())); | |
392 ctrl_ack_packet(pkt); | |
393 break; | |
394 case CMD_GET_PLAYLIST_LENGTH: | |
395 ctrl_write_gint(pkt->fd, playlist_get_length(playlist_get_active())); | |
396 ctrl_ack_packet(pkt); | |
397 break; | |
398 case CMD_GET_PLAYQUEUE_LENGTH: | |
399 ctrl_write_gint(pkt->fd, playlist_queue_get_length(playlist_get_active())); | |
400 ctrl_ack_packet(pkt); | |
401 break; | |
402 case CMD_PLAYQUEUE_IS_QUEUED: | |
403 ctrl_write_gboolean(pkt->fd, | |
404 playlist_is_position_queued(playlist_get_active(), *((guint32 *) pkt->data))); | |
405 ctrl_ack_packet(pkt); | |
406 break; | |
407 case CMD_PLAYQUEUE_GET_POS: | |
408 if (pkt->data) | |
409 ctrl_write_gint(pkt->fd, | |
410 playlist_get_queue_position_number(playlist_get_active(), | |
411 *((guint32 *) pkt->data))); | |
412 else | |
413 ctrl_write_gint(pkt->fd, 0); | |
414 | |
415 ctrl_ack_packet(pkt); | |
416 break; | |
417 case CMD_PLAYQUEUE_GET_QPOS: | |
418 if (pkt->data) | |
419 ctrl_write_gint(pkt->fd, | |
420 playlist_get_queue_position_number(playlist_get_active(), | |
421 *((guint32 *) pkt->data))); | |
422 else | |
423 ctrl_write_gint(pkt->fd, 0); | |
424 | |
425 ctrl_ack_packet(pkt); | |
426 break; | |
427 case CMD_GET_OUTPUT_TIME: | |
428 if (playback_get_playing()) | |
429 ctrl_write_gint(pkt->fd, playback_get_time()); | |
430 else | |
431 ctrl_write_gint(pkt->fd, 0); | |
432 ctrl_ack_packet(pkt); | |
433 break; | |
434 case CMD_GET_VOLUME: | |
435 input_get_volume(&v[0], &v[1]); | |
436 ctrl_write_packet(pkt->fd, v, sizeof(v)); | |
437 ctrl_ack_packet(pkt); | |
438 break; | |
439 case CMD_GET_BALANCE: | |
440 input_get_volume(&v[0], &v[1]); | |
441 if (v[0] < 0 || v[1] < 0) | |
442 b = 0; | |
443 else if (v[0] > v[1]) | |
444 b = -100 + ((v[1] * 100) / v[0]); | |
445 else if (v[1] > v[0]) | |
446 b = 100 - ((v[0] * 100) / v[1]); | |
447 else | |
448 b = 0; | |
449 ctrl_write_gint(pkt->fd, b); | |
450 ctrl_ack_packet(pkt); | |
451 break; | |
452 case CMD_GET_SKIN: | |
453 ctrl_write_string(pkt->fd, bmp_active_skin->path); | |
454 ctrl_ack_packet(pkt); | |
455 break; | |
456 case CMD_GET_PLAYLIST_FILE: | |
457 if (pkt->data) { | |
458 gchar *filename; | |
459 filename = playlist_get_filename(playlist_get_active(), *((guint32 *) pkt->data)); | |
460 ctrl_write_string(pkt->fd, filename); | |
461 g_free(filename); | |
462 } | |
463 else | |
464 ctrl_write_string(pkt->fd, NULL); | |
465 ctrl_ack_packet(pkt); | |
466 break; | |
467 case CMD_GET_PLAYLIST_TITLE: | |
468 if (pkt->data) { | |
469 gchar *title; | |
470 title = playlist_get_songtitle(playlist_get_active(), *((guint32 *) pkt->data)); | |
471 ctrl_write_string(pkt->fd, title); | |
472 g_free(title); | |
473 } | |
474 else | |
475 ctrl_write_string(pkt->fd, NULL); | |
476 ctrl_ack_packet(pkt); | |
477 break; | |
478 case CMD_GET_PLAYLIST_TIME: | |
479 if (pkt->data) | |
480 ctrl_write_gint(pkt->fd, | |
481 playlist_get_songtime(playlist_get_active(), | |
482 *((guint32 *) pkt->data))); | |
483 else | |
484 ctrl_write_gint(pkt->fd, -1); | |
485 | |
486 ctrl_ack_packet(pkt); | |
487 break; | |
488 case CMD_GET_INFO: | |
489 playback_get_sample_params(&info[0], &info[1], &info[2]); | |
490 ctrl_write_packet(pkt->fd, info, 3 * sizeof(gint)); | |
491 ctrl_ack_packet(pkt); | |
492 break; | |
493 case CMD_GET_EQ_DATA: | |
494 case CMD_SET_EQ_DATA: | |
495 /* obsolete */ | |
496 ctrl_ack_packet(pkt); | |
497 break; | |
498 case CMD_PING: | |
499 ctrl_ack_packet(pkt); | |
500 break; | |
501 case CMD_PLAYLIST_ADD: | |
502 if (pkt->data) { | |
503 guint32 *dataptr = pkt->data; | |
504 while ((len = *dataptr) > 0) { | |
505 gchar *filename; | |
506 | |
507 dataptr++; | |
508 filename = memory_pool_allocate(cs_pool, len); | |
509 memcpy(filename, dataptr, len); | |
510 | |
511 GDK_THREADS_ENTER(); | |
512 playlist_add_url(playlist_get_active(), filename); | |
513 GDK_THREADS_LEAVE(); | |
514 | |
515 memory_pool_release(cs_pool, filename); | |
516 dataptr += (len + 3) / 4; | |
517 } | |
518 } | |
519 ctrl_ack_packet(pkt); | |
520 break; | |
521 case CMD_PLAYLIST_ENQUEUE_TO_TEMP: | |
522 { | |
523 Playlist *new_pl = playlist_new(); | |
524 | |
525 GDK_THREADS_ENTER(); | |
526 playlist_select_playlist(new_pl); | |
527 playlist_add_url(new_pl, pkt->data); | |
528 GDK_THREADS_LEAVE(); | |
529 | |
530 ctrl_ack_packet(pkt); | |
531 } | |
532 break; | |
533 case CMD_PLAYLIST_ADD_URL_STRING: | |
534 GDK_THREADS_ENTER(); | |
535 playlist_add_url(playlist_get_active(), pkt->data); | |
536 GDK_THREADS_LEAVE(); | |
537 | |
538 ctrl_ack_packet(pkt); | |
539 break; | |
540 case CMD_PLAYLIST_INS_URL_STRING: | |
541 if (pkt->data) { | |
542 gint pos = *(gint *) pkt->data; | |
543 gchar *ptr = pkt->data; | |
544 ptr += sizeof(gint); | |
545 playlist_ins_url(playlist_get_active(), ptr, pos); | |
546 } | |
547 ctrl_ack_packet(pkt); | |
548 break; | |
549 case CMD_PLAYLIST_DELETE: | |
550 GDK_THREADS_ENTER(); | |
551 playlist_delete_index(playlist_get_active(), *((guint32 *) pkt->data)); | |
552 GDK_THREADS_LEAVE(); | |
553 ctrl_ack_packet(pkt); | |
554 break; | |
555 case CMD_PLAYLIST_CLEAR: | |
556 GDK_THREADS_ENTER(); | |
557 playlist_clear(playlist_get_active()); | |
558 mainwin_clear_song_info(); | |
559 mainwin_set_info_text(); | |
560 GDK_THREADS_LEAVE(); | |
561 ctrl_ack_packet(pkt); | |
562 break; | |
563 case CMD_IS_MAIN_WIN: | |
564 ctrl_write_gboolean(pkt->fd, cfg.player_visible); | |
565 ctrl_ack_packet(pkt); | |
566 break; | |
567 case CMD_IS_PL_WIN: | |
568 ctrl_write_gboolean(pkt->fd, cfg.playlist_visible); | |
569 ctrl_ack_packet(pkt); | |
570 break; | |
571 case CMD_IS_EQ_WIN: | |
572 ctrl_write_gboolean(pkt->fd, cfg.equalizer_visible); | |
573 ctrl_ack_packet(pkt); | |
574 break; | |
575 case CMD_IS_REPEAT: | |
576 ctrl_write_gboolean(pkt->fd, cfg.repeat); | |
577 ctrl_ack_packet(pkt); | |
578 break; | |
579 case CMD_IS_SHUFFLE: | |
580 ctrl_write_gboolean(pkt->fd, cfg.shuffle); | |
581 ctrl_ack_packet(pkt); | |
582 break; | |
583 case CMD_IS_ADVANCE: | |
584 ctrl_write_gboolean(pkt->fd, !cfg.no_playlist_advance); | |
585 ctrl_ack_packet(pkt); | |
586 break; | |
587 case CMD_GET_EQ: | |
588 fval[0] = equalizerwin_get_preamp(); | |
589 for (i = 0; i < 10; i++) | |
590 fval[i + 1] = equalizerwin_get_band(i); | |
591 ctrl_write_packet(pkt->fd, fval, 11 * sizeof(gfloat)); | |
592 ctrl_ack_packet(pkt); | |
593 break; | |
594 case CMD_GET_EQ_PREAMP: | |
595 ctrl_write_gfloat(pkt->fd, equalizerwin_get_preamp()); | |
596 ctrl_ack_packet(pkt); | |
597 break; | |
598 case CMD_GET_EQ_BAND: | |
599 i = *((guint32 *) pkt->data); | |
600 ctrl_write_gfloat(pkt->fd, equalizerwin_get_band(i)); | |
601 ctrl_ack_packet(pkt); | |
602 break; | |
603 default: | |
604 g_mutex_lock(packet_list_mutex); | |
605 packet_list = g_list_append(packet_list, pkt); | |
606 ctrl_write_packet(pkt->fd, NULL, 0); | |
607 close(pkt->fd); | |
608 g_mutex_unlock(packet_list_mutex); | |
609 break; | |
610 } | |
611 } | |
612 g_thread_exit(NULL); | |
613 | |
614 /* Used to suppress GCC warnings. Sometimes you'd wish C has | |
615 native threading support :p */ | |
616 return NULL; | |
617 } | |
618 | |
619 void | |
620 ctrlsocket_check(void) | |
621 { | |
622 GList *pkt_list, *next; | |
623 PacketNode *pkt; | |
624 gpointer data; | |
625 guint32 v[2], i, num; | |
626 gboolean tbool; | |
627 gfloat *fval, f; | |
628 | |
629 g_mutex_lock(packet_list_mutex); | |
630 for (pkt_list = packet_list; pkt_list; pkt_list = next) { | |
631 pkt = pkt_list->data; | |
632 data = pkt->data; | |
633 | |
634 switch (pkt->hdr.command) { | |
635 case CMD_PLAY: | |
636 if (playback_get_paused()) | |
637 playback_pause(); | |
638 else if (playlist_get_length(playlist_get_active())) | |
639 playback_initiate(); | |
640 else | |
641 mainwin_eject_pushed(); | |
642 break; | |
643 case CMD_PAUSE: | |
644 playback_pause(); | |
645 break; | |
646 case CMD_STOP: | |
647 ip_data.stop = TRUE; | |
648 playback_stop(); | |
649 ip_data.stop = FALSE; | |
650 mainwin_clear_song_info(); | |
651 break; | |
652 case CMD_PLAY_PAUSE: | |
653 if (playback_get_playing()) | |
654 playback_pause(); | |
655 else | |
656 playback_initiate(); | |
657 break; | |
658 case CMD_PLAYQUEUE_ADD: | |
659 num = *((guint32 *) data); | |
660 if (num < (guint)playlist_get_length(playlist_get_active())) | |
661 playlist_queue_position(playlist_get_active(), num); | |
662 break; | |
663 case CMD_PLAYQUEUE_REMOVE: | |
664 num = *((guint32 *) data); | |
665 if (num < (guint)playlist_get_length(playlist_get_active())) | |
666 playlist_queue_remove(playlist_get_active(), num); | |
667 break; | |
668 case CMD_PLAYQUEUE_CLEAR: | |
669 playlist_clear_queue(playlist_get_active()); | |
670 break; | |
671 case CMD_SET_PLAYLIST_POS: | |
672 num = *((guint32 *) data); | |
673 if (num < (guint)playlist_get_length(playlist_get_active())) | |
674 playlist_set_position(playlist_get_active(), num); | |
675 break; | |
676 case CMD_JUMP_TO_TIME: | |
677 num = *((guint32 *) data); | |
678 if (playlist_get_current_length(playlist_get_active()) > 0 && | |
679 num < (guint)playlist_get_current_length(playlist_get_active())) | |
680 playback_seek(num / 1000); | |
681 break; | |
682 case CMD_SET_VOLUME: | |
683 v[0] = ((guint32 *) data)[0]; | |
684 v[1] = ((guint32 *) data)[1]; | |
685 for (i = 0; i < 2; i++) { | |
686 if (v[i] > 100) | |
687 v[i] = 100; | |
688 } | |
689 input_set_volume(v[0], v[1]); | |
690 break; | |
691 case CMD_SET_SKIN: | |
692 if (has_x11_connection == TRUE) | |
693 bmp_active_skin_load(data); | |
694 break; | |
695 case CMD_PL_WIN_TOGGLE: | |
696 if (has_x11_connection != TRUE) | |
697 break; | |
698 tbool = *((gboolean *) data); | |
699 if (tbool) | |
700 playlistwin_show(); | |
701 else | |
702 playlistwin_hide(); | |
703 break; | |
704 case CMD_EQ_WIN_TOGGLE: | |
705 if (has_x11_connection != TRUE) | |
706 break; | |
707 tbool = *((gboolean *) data); | |
708 equalizerwin_show(!!tbool); | |
709 break; | |
710 case CMD_SHOW_PREFS_BOX: | |
711 if (has_x11_connection != TRUE) | |
712 break; | |
713 show_prefs_window(); | |
714 break; | |
715 case CMD_SHOW_JTF_BOX: | |
716 if (has_x11_connection != TRUE) | |
717 break; | |
718 mainwin_jump_to_file(); | |
719 break; | |
720 case CMD_TOGGLE_AOT: | |
721 if (has_x11_connection != TRUE) | |
722 break; | |
723 tbool = *((gboolean *) data); | |
724 mainwin_set_always_on_top(tbool); | |
725 break; | |
726 case CMD_SHOW_ABOUT_BOX: | |
727 break; | |
728 case CMD_EJECT: | |
729 if (has_x11_connection != TRUE) | |
730 break; | |
731 mainwin_eject_pushed(); | |
732 break; | |
733 case CMD_PLAYLIST_PREV: | |
734 playlist_prev(playlist_get_active()); | |
735 break; | |
736 case CMD_PLAYLIST_NEXT: | |
737 playlist_next(playlist_get_active()); | |
738 break; | |
739 case CMD_TOGGLE_REPEAT: | |
740 mainwin_repeat_pushed(!cfg.repeat); | |
741 break; | |
742 case CMD_TOGGLE_SHUFFLE: | |
743 mainwin_shuffle_pushed(!cfg.shuffle); | |
744 break; | |
745 case CMD_TOGGLE_ADVANCE: | |
746 /* FIXME: to be implemented */ | |
747 break; | |
748 case CMD_MAIN_WIN_TOGGLE: | |
749 if (has_x11_connection != TRUE) | |
750 break; | |
751 tbool = *((gboolean *) data); | |
752 mainwin_show(!!tbool); | |
753 break; | |
754 case CMD_SET_EQ: | |
755 if (pkt->hdr.data_length >= 11 * sizeof(gfloat)) { | |
756 fval = (gfloat *) data; | |
757 equalizerwin_set_preamp(fval[0]); | |
758 for (i = 0; i < 10; i++) | |
759 equalizerwin_set_band(i, fval[i + 1]); | |
760 } | |
761 break; | |
762 case CMD_SET_EQ_PREAMP: | |
763 f = *((gfloat *) data); | |
764 equalizerwin_set_preamp(f); | |
765 break; | |
766 case CMD_SET_EQ_BAND: | |
767 if (pkt->hdr.data_length >= sizeof(gint) + sizeof(gfloat)) { | |
768 i = *((gint *) data); | |
769 f = *((gfloat *) ((gchar *) data + sizeof(gint))); | |
770 equalizerwin_set_band(i, f); | |
771 } | |
772 break; | |
773 case CMD_QUIT: | |
774 /* | |
775 * We unlock the packet_list_mutex to | |
776 * avoid that cleanup_ctrlsocket() can | |
777 * deadlock, mainwin_quit_cb() will | |
778 * never return anyway, so this will | |
779 * work ok. | |
780 */ | |
781 g_mutex_unlock(packet_list_mutex); | |
782 mainwin_quit_cb(); | |
783 break; | |
784 case CMD_ACTIVATE: | |
785 gtk_window_present(GTK_WINDOW(mainwin)); | |
786 break; | |
787 default: | |
788 g_message("Unknown socket command received"); | |
789 break; | |
790 } | |
791 next = g_list_next(pkt_list); | |
792 packet_list = g_list_remove_link(packet_list, pkt_list); | |
793 g_list_free_1(pkt_list); | |
794 if (pkt->data) | |
795 memory_pool_release(cs_pool, pkt->data); | |
796 memory_pool_release(cs_pool, pkt); | |
797 } | |
798 g_mutex_unlock(packet_list_mutex); | |
799 } |