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