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