Mercurial > audlegacy
annotate audacious/controlsocket.c @ 2103:5d961ebb435d trunk
[svn] - run update-po
- change cy.po's indices (invalid string fix in r3209)
author | nenolod |
---|---|
date | Mon, 11 Dec 2006 19:32:18 -0800 |
parents | b05046902327 |
children | f18a5b617c34 |
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" | |
1653 | 47 #include "equalizer.h" |
48 #include "mainwin.h" | |
0 | 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" |
1653 | 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 { | |
1761
70caa62ead63
[svn] - fix for "bmp_rcfile_write_string: assertion `value != NULL' failed"
yaz
parents:
1653
diff
changeset
|
90 if (strcmp(cfg.session_uri_base, "")) |
70caa62ead63
[svn] - fix for "bmp_rcfile_write_string: assertion `value != NULL' failed"
yaz
parents:
1653
diff
changeset
|
91 audacious_set_session_uri(cfg.session_uri_base); |
70caa62ead63
[svn] - fix for "bmp_rcfile_write_string: assertion `value != NULL' failed"
yaz
parents:
1653
diff
changeset
|
92 else |
70caa62ead63
[svn] - fix for "bmp_rcfile_write_string: assertion `value != NULL' failed"
yaz
parents:
1653
diff
changeset
|
93 return ctrlsocket_setup_unix(); |
1453 | 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 | |
2042
0985452d1962
[svn] - clean up some internal audacious controlsocket functions
nenolod
parents:
1766
diff
changeset
|
108 audacious_set_session_type(AUDACIOUS_TYPE_UNIX); |
1456
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 |
2042
0985452d1962
[svn] - clean up some internal audacious controlsocket functions
nenolod
parents:
1766
diff
changeset
|
166 audacious_set_session_type(AUDACIOUS_TYPE_TCP); |
1456
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 | |
1766
6cbb9360e8e2
[svn] - always zero-out stack variables, as stack memory may be dirty on x86 and friends
nenolod
parents:
1761
diff
changeset
|
258 memset(&pkthdr, '\0', sizeof(ServerPktHeader)); |
6cbb9360e8e2
[svn] - always zero-out stack variables, as stack memory may be dirty on x86 and friends
nenolod
parents:
1761
diff
changeset
|
259 |
0 | 260 pkthdr.version = XMMS_PROTOCOL_VERSION; |
261 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
|
262 if ((size_t)write(fd, &pkthdr, sizeof(ServerPktHeader)) < sizeof(pkthdr)) |
0 | 263 return; |
264 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
|
265 write(fd, data, length); |
0 | 266 } |
267 | |
268 static void | |
269 ctrl_write_gint(gint fd, gint val) | |
270 { | |
271 ctrl_write_packet(fd, &val, sizeof(gint)); | |
272 } | |
273 | |
274 static void | |
275 ctrl_write_gfloat(gint fd, gfloat val) | |
276 { | |
277 ctrl_write_packet(fd, &val, sizeof(gfloat)); | |
278 } | |
279 | |
280 static void | |
281 ctrl_write_gboolean(gint fd, gboolean bool) | |
282 { | |
283 ctrl_write_packet(fd, &bool, sizeof(gboolean)); | |
284 } | |
285 | |
286 static void | |
287 ctrl_write_string(gint fd, gchar * string) | |
288 { | |
289 ctrl_write_packet(fd, string, string ? strlen(string) + 1 : 0); | |
290 } | |
291 | |
292 static void | |
293 ctrl_ack_packet(PacketNode * pkt) | |
294 { | |
295 ctrl_write_packet(pkt->fd, NULL, 0); | |
296 close(pkt->fd); | |
297 if (pkt->data) | |
298 g_free(pkt->data); | |
299 g_free(pkt); | |
300 } | |
301 | |
302 static gboolean | |
303 ctrlsocket_is_going(void) | |
304 { | |
305 gboolean result; | |
306 | |
307 g_mutex_lock(status_mutex); | |
308 result = going; | |
309 g_mutex_unlock(status_mutex); | |
310 | |
311 return result; | |
312 } | |
313 | |
314 static gpointer | |
315 ctrlsocket_func(gpointer arg) | |
316 { | |
317 fd_set set; | |
318 struct timeval tv; | |
319 struct sockaddr_un saddr; | |
320 gint fd, b, i; | |
377 | 321 gint info[3]; |
0 | 322 gint32 v[2]; |
323 PacketNode *pkt; | |
324 socklen_t len; | |
325 gfloat fval[11]; | |
326 | |
327 g_mutex_lock(status_mutex); | |
328 while (!started && going) | |
329 g_cond_wait(start_cond, status_mutex); | |
330 g_mutex_unlock(status_mutex); | |
331 | |
332 while (ctrlsocket_is_going()) { | |
333 FD_ZERO(&set); | |
334 FD_SET(ctrl_fd, &set); | |
335 tv.tv_sec = 0; | |
336 tv.tv_usec = CTRLSOCKET_TIMEOUT; | |
337 len = sizeof(saddr); | |
338 if (select(ctrl_fd + 1, &set, NULL, NULL, &tv) <= 0) | |
339 continue; | |
340 if ((fd = accept(ctrl_fd, (struct sockaddr *) &saddr, &len)) == -1) | |
341 continue; | |
342 | |
343 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
|
344 if ((size_t)read(fd, &pkt->hdr, sizeof(ClientPktHeader)) |
0 | 345 < sizeof(ClientPktHeader)) { |
346 g_free(pkt); | |
347 continue; | |
348 } | |
349 | |
350 if (pkt->hdr.data_length) { | |
351 size_t data_length = pkt->hdr.data_length; | |
352 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
|
353 if ((size_t)read(fd, pkt->data, data_length) < data_length) { |
0 | 354 g_free(pkt->data); |
355 g_free(pkt); | |
356 g_warning("ctrlsocket_func(): Incomplete data packet dropped"); | |
357 continue; | |
358 } | |
359 } | |
360 | |
361 pkt->fd = fd; | |
362 switch (pkt->hdr.command) { | |
363 case CMD_GET_VERSION: | |
364 ctrl_write_gint(pkt->fd, 0x09a3); | |
365 ctrl_ack_packet(pkt); | |
366 break; | |
367 case CMD_IS_PLAYING: | |
368 ctrl_write_gboolean(pkt->fd, bmp_playback_get_playing()); | |
369 ctrl_ack_packet(pkt); | |
370 break; | |
371 case CMD_IS_PAUSED: | |
372 ctrl_write_gboolean(pkt->fd, bmp_playback_get_paused()); | |
373 ctrl_ack_packet(pkt); | |
374 break; | |
375 case CMD_GET_PLAYLIST_POS: | |
2090
b89947411061
[svn] Make controlsocket.c happier. Still a ton of warnings, but it compiles now.
kiyoshi
parents:
2042
diff
changeset
|
376 ctrl_write_gint(pkt->fd, playlist_get_position(playlist_get_active())); |
0 | 377 ctrl_ack_packet(pkt); |
378 break; | |
379 case CMD_GET_PLAYLIST_LENGTH: | |
2090
b89947411061
[svn] Make controlsocket.c happier. Still a ton of warnings, but it compiles now.
kiyoshi
parents:
2042
diff
changeset
|
380 ctrl_write_gint(pkt->fd, playlist_get_length(playlist_get_active())); |
0 | 381 ctrl_ack_packet(pkt); |
382 break; | |
383 case CMD_GET_PLAYQUEUE_LENGTH: | |
2090
b89947411061
[svn] Make controlsocket.c happier. Still a ton of warnings, but it compiles now.
kiyoshi
parents:
2042
diff
changeset
|
384 ctrl_write_gint(pkt->fd, playlist_queue_get_length(playlist_get_active())); |
0 | 385 ctrl_ack_packet(pkt); |
386 break; | |
984 | 387 case CMD_PLAYQUEUE_IS_QUEUED: |
388 ctrl_write_gboolean(pkt->fd, | |
2093
b05046902327
[svn] Oops. controlsocket.c is *much* happier now that the arguments are in the right order. >.>
kiyoshi
parents:
2090
diff
changeset
|
389 playlist_is_position_queued(playlist_get_active(), *((guint32 *) pkt->data))); |
984 | 390 ctrl_ack_packet(pkt); |
391 break; | |
392 case CMD_PLAYQUEUE_GET_POS: | |
393 if (pkt->data) | |
394 ctrl_write_gint(pkt->fd, | |
2093
b05046902327
[svn] Oops. controlsocket.c is *much* happier now that the arguments are in the right order. >.>
kiyoshi
parents:
2090
diff
changeset
|
395 playlist_get_queue_position_number(playlist_get_active(), |
b05046902327
[svn] Oops. controlsocket.c is *much* happier now that the arguments are in the right order. >.>
kiyoshi
parents:
2090
diff
changeset
|
396 *((guint32 *) pkt->data))); |
984 | 397 else |
398 ctrl_write_gint(pkt->fd, 0); | |
399 | |
400 ctrl_ack_packet(pkt); | |
401 break; | |
402 case CMD_PLAYQUEUE_GET_QPOS: | |
403 if (pkt->data) | |
404 ctrl_write_gint(pkt->fd, | |
2093
b05046902327
[svn] Oops. controlsocket.c is *much* happier now that the arguments are in the right order. >.>
kiyoshi
parents:
2090
diff
changeset
|
405 playlist_get_queue_position_number(playlist_get_active(), |
b05046902327
[svn] Oops. controlsocket.c is *much* happier now that the arguments are in the right order. >.>
kiyoshi
parents:
2090
diff
changeset
|
406 *((guint32 *) pkt->data))); |
984 | 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; | |
1653 | 437 case CMD_GET_SKIN: |
438 ctrl_write_string(pkt->fd, bmp_active_skin->path); | |
439 ctrl_ack_packet(pkt); | |
440 break; | |
0 | 441 case CMD_GET_PLAYLIST_FILE: |
442 if (pkt->data) { | |
443 gchar *filename; | |
2093
b05046902327
[svn] Oops. controlsocket.c is *much* happier now that the arguments are in the right order. >.>
kiyoshi
parents:
2090
diff
changeset
|
444 filename = playlist_get_filename(playlist_get_active(), *((guint32 *) pkt->data)); |
0 | 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; | |
2093
b05046902327
[svn] Oops. controlsocket.c is *much* happier now that the arguments are in the right order. >.>
kiyoshi
parents:
2090
diff
changeset
|
455 title = playlist_get_songtitle(playlist_get_active(), *((guint32 *) pkt->data)); |
0 | 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, | |
2093
b05046902327
[svn] Oops. controlsocket.c is *much* happier now that the arguments are in the right order. >.>
kiyoshi
parents:
2090
diff
changeset
|
466 playlist_get_songtime(playlist_get_active(), |
b05046902327
[svn] Oops. controlsocket.c is *much* happier now that the arguments are in the right order. >.>
kiyoshi
parents:
2090
diff
changeset
|
467 *((guint32 *) pkt->data))); |
0 | 468 else |
469 ctrl_write_gint(pkt->fd, -1); | |
470 | |
471 ctrl_ack_packet(pkt); | |
472 break; | |
473 case CMD_GET_INFO: | |
474 playback_get_sample_params(&info[0], &info[1], &info[2]); | |
475 ctrl_write_packet(pkt->fd, info, 3 * sizeof(gint)); | |
476 ctrl_ack_packet(pkt); | |
477 break; | |
478 case CMD_GET_EQ_DATA: | |
479 case CMD_SET_EQ_DATA: | |
480 /* obsolete */ | |
481 ctrl_ack_packet(pkt); | |
482 break; | |
483 case CMD_PING: | |
484 ctrl_ack_packet(pkt); | |
485 break; | |
486 case CMD_PLAYLIST_ADD: | |
487 if (pkt->data) { | |
488 guint32 *dataptr = pkt->data; | |
489 while ((len = *dataptr) > 0) { | |
490 gchar *filename; | |
491 | |
492 dataptr++; | |
493 filename = g_malloc0(len); | |
494 memcpy(filename, dataptr, len); | |
495 | |
496 GDK_THREADS_ENTER(); | |
2093
b05046902327
[svn] Oops. controlsocket.c is *much* happier now that the arguments are in the right order. >.>
kiyoshi
parents:
2090
diff
changeset
|
497 playlist_add_url(playlist_get_active(), filename); |
0 | 498 GDK_THREADS_LEAVE(); |
499 | |
500 g_free(filename); | |
501 dataptr += (len + 3) / 4; | |
502 } | |
503 } | |
504 ctrl_ack_packet(pkt); | |
505 break; | |
506 case CMD_PLAYLIST_ADD_URL_STRING: | |
507 GDK_THREADS_ENTER(); | |
2093
b05046902327
[svn] Oops. controlsocket.c is *much* happier now that the arguments are in the right order. >.>
kiyoshi
parents:
2090
diff
changeset
|
508 playlist_add_url(playlist_get_active(), pkt->data); |
0 | 509 GDK_THREADS_LEAVE(); |
510 | |
511 ctrl_ack_packet(pkt); | |
512 break; | |
513 case CMD_PLAYLIST_INS_URL_STRING: | |
514 if (pkt->data) { | |
515 gint pos = *(gint *) pkt->data; | |
516 gchar *ptr = pkt->data; | |
517 ptr += sizeof(gint); | |
2093
b05046902327
[svn] Oops. controlsocket.c is *much* happier now that the arguments are in the right order. >.>
kiyoshi
parents:
2090
diff
changeset
|
518 playlist_ins_url(playlist_get_active(), ptr, pos); |
0 | 519 } |
520 ctrl_ack_packet(pkt); | |
521 break; | |
522 case CMD_PLAYLIST_DELETE: | |
523 GDK_THREADS_ENTER(); | |
2093
b05046902327
[svn] Oops. controlsocket.c is *much* happier now that the arguments are in the right order. >.>
kiyoshi
parents:
2090
diff
changeset
|
524 playlist_delete_index(playlist_get_active(), *((guint32 *) pkt->data)); |
0 | 525 GDK_THREADS_LEAVE(); |
526 ctrl_ack_packet(pkt); | |
527 break; | |
528 case CMD_PLAYLIST_CLEAR: | |
529 GDK_THREADS_ENTER(); | |
2090
b89947411061
[svn] Make controlsocket.c happier. Still a ton of warnings, but it compiles now.
kiyoshi
parents:
2042
diff
changeset
|
530 playlist_clear(playlist_get_active()); |
0 | 531 mainwin_clear_song_info(); |
532 mainwin_set_info_text(); | |
533 GDK_THREADS_LEAVE(); | |
534 ctrl_ack_packet(pkt); | |
535 break; | |
536 case CMD_IS_MAIN_WIN: | |
537 ctrl_write_gboolean(pkt->fd, cfg.player_visible); | |
538 ctrl_ack_packet(pkt); | |
539 break; | |
540 case CMD_IS_PL_WIN: | |
541 ctrl_write_gboolean(pkt->fd, cfg.playlist_visible); | |
542 ctrl_ack_packet(pkt); | |
543 break; | |
544 case CMD_IS_EQ_WIN: | |
545 ctrl_write_gboolean(pkt->fd, cfg.equalizer_visible); | |
546 ctrl_ack_packet(pkt); | |
547 break; | |
548 case CMD_IS_REPEAT: | |
549 ctrl_write_gboolean(pkt->fd, cfg.repeat); | |
550 ctrl_ack_packet(pkt); | |
551 break; | |
552 case CMD_IS_SHUFFLE: | |
553 ctrl_write_gboolean(pkt->fd, cfg.shuffle); | |
554 ctrl_ack_packet(pkt); | |
555 break; | |
556 case CMD_IS_ADVANCE: | |
557 ctrl_write_gboolean(pkt->fd, !cfg.no_playlist_advance); | |
558 ctrl_ack_packet(pkt); | |
559 break; | |
560 case CMD_GET_EQ: | |
561 fval[0] = equalizerwin_get_preamp(); | |
562 for (i = 0; i < 10; i++) | |
563 fval[i + 1] = equalizerwin_get_band(i); | |
564 ctrl_write_packet(pkt->fd, fval, 11 * sizeof(gfloat)); | |
565 ctrl_ack_packet(pkt); | |
566 break; | |
567 case CMD_GET_EQ_PREAMP: | |
568 ctrl_write_gfloat(pkt->fd, equalizerwin_get_preamp()); | |
569 ctrl_ack_packet(pkt); | |
570 break; | |
571 case CMD_GET_EQ_BAND: | |
572 i = *((guint32 *) pkt->data); | |
573 ctrl_write_gfloat(pkt->fd, equalizerwin_get_band(i)); | |
574 ctrl_ack_packet(pkt); | |
575 break; | |
576 default: | |
577 g_mutex_lock(packet_list_mutex); | |
578 packet_list = g_list_append(packet_list, pkt); | |
579 ctrl_write_packet(pkt->fd, NULL, 0); | |
580 close(pkt->fd); | |
581 g_mutex_unlock(packet_list_mutex); | |
582 break; | |
583 } | |
584 } | |
585 g_thread_exit(NULL); | |
586 | |
587 /* Used to suppress GCC warnings. Sometimes you'd wish C has | |
588 native threading support :p */ | |
589 return NULL; | |
590 } | |
591 | |
592 void | |
593 ctrlsocket_check(void) | |
594 { | |
595 GList *pkt_list, *next; | |
596 PacketNode *pkt; | |
597 gpointer data; | |
598 guint32 v[2], i, num; | |
599 gboolean tbool; | |
600 gfloat *fval, f; | |
601 | |
602 g_mutex_lock(packet_list_mutex); | |
603 for (pkt_list = packet_list; pkt_list; pkt_list = next) { | |
604 pkt = pkt_list->data; | |
605 data = pkt->data; | |
606 | |
607 switch (pkt->hdr.command) { | |
608 case CMD_PLAY: | |
609 if (bmp_playback_get_paused()) | |
610 bmp_playback_pause(); | |
2090
b89947411061
[svn] Make controlsocket.c happier. Still a ton of warnings, but it compiles now.
kiyoshi
parents:
2042
diff
changeset
|
611 else if (playlist_get_length(playlist_get_active())) |
0 | 612 bmp_playback_initiate(); |
613 else | |
614 mainwin_eject_pushed(); | |
615 break; | |
616 case CMD_PAUSE: | |
617 bmp_playback_pause(); | |
618 break; | |
619 case CMD_STOP: | |
906 | 620 ip_data.stop = TRUE; |
0 | 621 bmp_playback_stop(); |
906 | 622 ip_data.stop = FALSE; |
0 | 623 mainwin_clear_song_info(); |
624 break; | |
625 case CMD_PLAY_PAUSE: | |
626 if (bmp_playback_get_playing()) | |
627 bmp_playback_pause(); | |
628 else | |
629 bmp_playback_initiate(); | |
630 break; | |
631 case CMD_PLAYQUEUE_ADD: | |
632 num = *((guint32 *) data); | |
2090
b89947411061
[svn] Make controlsocket.c happier. Still a ton of warnings, but it compiles now.
kiyoshi
parents:
2042
diff
changeset
|
633 if (num < (guint)playlist_get_length(playlist_get_active())) |
2093
b05046902327
[svn] Oops. controlsocket.c is *much* happier now that the arguments are in the right order. >.>
kiyoshi
parents:
2090
diff
changeset
|
634 playlist_queue_position(playlist_get_active(), num); |
0 | 635 break; |
636 case CMD_PLAYQUEUE_REMOVE: | |
637 num = *((guint32 *) data); | |
2090
b89947411061
[svn] Make controlsocket.c happier. Still a ton of warnings, but it compiles now.
kiyoshi
parents:
2042
diff
changeset
|
638 if (num < (guint)playlist_get_length(playlist_get_active())) |
2093
b05046902327
[svn] Oops. controlsocket.c is *much* happier now that the arguments are in the right order. >.>
kiyoshi
parents:
2090
diff
changeset
|
639 playlist_queue_remove(playlist_get_active(), num); |
0 | 640 break; |
984 | 641 case CMD_PLAYQUEUE_CLEAR: |
2090
b89947411061
[svn] Make controlsocket.c happier. Still a ton of warnings, but it compiles now.
kiyoshi
parents:
2042
diff
changeset
|
642 playlist_clear_queue(playlist_get_active()); |
984 | 643 break; |
0 | 644 case CMD_SET_PLAYLIST_POS: |
645 num = *((guint32 *) data); | |
2090
b89947411061
[svn] Make controlsocket.c happier. Still a ton of warnings, but it compiles now.
kiyoshi
parents:
2042
diff
changeset
|
646 if (num < (guint)playlist_get_length(playlist_get_active())) |
2093
b05046902327
[svn] Oops. controlsocket.c is *much* happier now that the arguments are in the right order. >.>
kiyoshi
parents:
2090
diff
changeset
|
647 playlist_set_position(playlist_get_active(), num); |
0 | 648 break; |
649 case CMD_JUMP_TO_TIME: | |
650 num = *((guint32 *) data); | |
2090
b89947411061
[svn] Make controlsocket.c happier. Still a ton of warnings, but it compiles now.
kiyoshi
parents:
2042
diff
changeset
|
651 if (playlist_get_current_length(playlist_get_active()) > 0 && |
b89947411061
[svn] Make controlsocket.c happier. Still a ton of warnings, but it compiles now.
kiyoshi
parents:
2042
diff
changeset
|
652 num < (guint)playlist_get_current_length(playlist_get_active())) |
0 | 653 bmp_playback_seek(num / 1000); |
654 break; | |
655 case CMD_SET_VOLUME: | |
656 v[0] = ((guint32 *) data)[0]; | |
657 v[1] = ((guint32 *) data)[1]; | |
658 for (i = 0; i < 2; i++) { | |
659 if (v[i] > 100) | |
660 v[i] = 100; | |
661 } | |
662 input_set_volume(v[0], v[1]); | |
663 break; | |
664 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
|
665 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
|
666 bmp_active_skin_load(data); |
0 | 667 break; |
668 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
|
669 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
|
670 break; |
0 | 671 tbool = *((gboolean *) data); |
672 if (tbool) | |
673 playlistwin_show(); | |
674 else | |
675 playlistwin_hide(); | |
676 break; | |
677 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
|
678 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
|
679 break; |
0 | 680 tbool = *((gboolean *) data); |
681 equalizerwin_show(!!tbool); | |
682 break; | |
683 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
|
684 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
|
685 break; |
0 | 686 show_prefs_window(); |
687 break; | |
984 | 688 case CMD_SHOW_JTF_BOX: |
689 if (has_x11_connection != TRUE) | |
690 break; | |
691 mainwin_jump_to_file(); | |
692 break; | |
0 | 693 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
|
694 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
|
695 break; |
0 | 696 tbool = *((gboolean *) data); |
697 mainwin_set_always_on_top(tbool); | |
698 break; | |
699 case CMD_SHOW_ABOUT_BOX: | |
700 break; | |
701 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
|
702 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
|
703 break; |
0 | 704 mainwin_eject_pushed(); |
705 break; | |
706 case CMD_PLAYLIST_PREV: | |
2090
b89947411061
[svn] Make controlsocket.c happier. Still a ton of warnings, but it compiles now.
kiyoshi
parents:
2042
diff
changeset
|
707 playlist_prev(playlist_get_active()); |
0 | 708 break; |
709 case CMD_PLAYLIST_NEXT: | |
2090
b89947411061
[svn] Make controlsocket.c happier. Still a ton of warnings, but it compiles now.
kiyoshi
parents:
2042
diff
changeset
|
710 playlist_next(playlist_get_active()); |
0 | 711 break; |
712 case CMD_TOGGLE_REPEAT: | |
713 mainwin_repeat_pushed(!cfg.repeat); | |
714 break; | |
715 case CMD_TOGGLE_SHUFFLE: | |
716 mainwin_shuffle_pushed(!cfg.shuffle); | |
717 break; | |
718 case CMD_TOGGLE_ADVANCE: | |
719 /* FIXME: to be implemented */ | |
720 break; | |
721 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
|
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 mainwin_show(!!tbool); | |
726 break; | |
727 case CMD_SET_EQ: | |
728 if (pkt->hdr.data_length >= 11 * sizeof(gfloat)) { | |
729 fval = (gfloat *) data; | |
730 equalizerwin_set_preamp(fval[0]); | |
731 for (i = 0; i < 10; i++) | |
732 equalizerwin_set_band(i, fval[i + 1]); | |
733 } | |
734 break; | |
735 case CMD_SET_EQ_PREAMP: | |
736 f = *((gfloat *) data); | |
737 equalizerwin_set_preamp(f); | |
738 break; | |
739 case CMD_SET_EQ_BAND: | |
740 if (pkt->hdr.data_length >= sizeof(gint) + sizeof(gfloat)) { | |
741 i = *((gint *) data); | |
742 f = *((gfloat *) ((gchar *) data + sizeof(gint))); | |
743 equalizerwin_set_band(i, f); | |
744 } | |
745 break; | |
746 case CMD_QUIT: | |
747 /* | |
748 * We unlock the packet_list_mutex to | |
749 * avoid that cleanup_ctrlsocket() can | |
750 * deadlock, mainwin_quit_cb() will | |
751 * never return anyway, so this will | |
752 * work ok. | |
753 */ | |
754 g_mutex_unlock(packet_list_mutex); | |
755 mainwin_quit_cb(); | |
756 break; | |
757 case CMD_ACTIVATE: | |
1653 | 758 gtk_window_present(GTK_WINDOW(mainwin)); |
0 | 759 break; |
760 default: | |
761 g_message("Unknown socket command received"); | |
762 break; | |
763 } | |
764 next = g_list_next(pkt_list); | |
765 packet_list = g_list_remove_link(packet_list, pkt_list); | |
766 g_list_free_1(pkt_list); | |
767 if (pkt->data) | |
768 g_free(pkt->data); | |
769 g_free(pkt); | |
770 } | |
771 g_mutex_unlock(packet_list_mutex); | |
772 } |