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