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