4 * Copyright (C) 2008 - 2009 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
21 #include "layout_image.h"
23 #include "slideshow.h"
24 #include "ui_fileops.h"
26 #include <sys/socket.h>
32 #define SERVER_MAX_CLIENTS 8
34 #define REMOTE_SERVER_BACKLOG 4
38 #define UNIX_PATH_MAX 108
42 static RemoteConnection *remote_client_open(const gchar *path);
43 static gint remote_client_send(RemoteConnection *rc, const gchar *text);
46 typedef struct _RemoteClient RemoteClient;
47 struct _RemoteClient {
53 typedef struct _RemoteData RemoteData;
55 CollectionData *command_collection;
59 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
61 RemoteClient *client = data;
63 GIOStatus status = G_IO_STATUS_NORMAL;
67 if (condition & G_IO_IN)
75 while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
79 buffer[termpos] = '\0';
81 if (strlen(buffer) > 0)
83 queue = g_list_append(queue, buffer);
96 log_printf("error reading socket: %s\n", error->message);
103 gchar *command = work->data;
106 if (rc->read_func) rc->read_func(rc, command, rc->read_data);
113 if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
115 rc->clients = g_list_remove(rc->clients, client);
117 DEBUG_1("HUP detected, closing client.");
118 DEBUG_1("client count %d", g_list_length(rc->clients));
120 g_source_remove(client->channel_id);
128 static void remote_server_client_add(RemoteConnection *rc, gint fd)
130 RemoteClient *client;
133 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
135 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
140 client = g_new0(RemoteClient, 1);
144 channel = g_io_channel_unix_new(fd);
145 client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP,
146 remote_server_client_cb, client, NULL);
147 g_io_channel_unref(channel);
149 rc->clients = g_list_append(rc->clients, client);
150 DEBUG_1("client count %d", g_list_length(rc->clients));
153 static void remote_server_clients_close(RemoteConnection *rc)
157 RemoteClient *client = rc->clients->data;
159 rc->clients = g_list_remove(rc->clients, client);
161 g_source_remove(client->channel_id);
167 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
169 RemoteConnection *rc = data;
173 fd = accept(rc->fd, NULL, &alen);
176 log_printf("error accepting socket: %s\n", strerror(errno));
180 remote_server_client_add(rc, fd);
185 static gint remote_server_exists(const gchar *path)
187 RemoteConnection *rc;
189 /* verify server up */
190 rc = remote_client_open(path);
195 /* unable to connect, remove socket file to free up address */
200 static RemoteConnection *remote_server_open(const gchar *path)
202 RemoteConnection *rc;
203 struct sockaddr_un addr;
208 if (remote_server_exists(path))
210 log_printf("Address already in use: %s\n", path);
214 fd = socket(PF_UNIX, SOCK_STREAM, 0);
215 if (fd == -1) return NULL;
217 addr.sun_family = AF_UNIX;
218 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
219 strncpy(addr.sun_path, path, sun_path_len);
220 if (bind(fd, &addr, sizeof(addr)) == -1 ||
221 listen(fd, REMOTE_SERVER_BACKLOG) == -1)
223 log_printf("error subscribing to socket: %s\n", strerror(errno));
228 rc = g_new0(RemoteConnection, 1);
232 rc->path = g_strdup(path);
234 channel = g_io_channel_unix_new(rc->fd);
235 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
237 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
238 remote_server_read_cb, rc, NULL);
239 g_io_channel_unref(channel);
244 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
246 if (!rc || !rc->server) return;
248 rc->read_func = func;
249 rc->read_data = data;
253 static RemoteConnection *remote_client_open(const gchar *path)
255 RemoteConnection *rc;
257 struct sockaddr_un addr;
261 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
263 fd = socket(PF_UNIX, SOCK_STREAM, 0);
264 if (fd == -1) return NULL;
266 addr.sun_family = AF_UNIX;
267 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
268 strncpy(addr.sun_path, path, sun_path_len);
269 if (connect(fd, &addr, sizeof(addr)) == -1)
271 DEBUG_1("error connecting to socket: %s", strerror(errno));
276 rc = g_new0(RemoteConnection, 1);
279 rc->path = g_strdup(path);
281 /* this might fix the freezes on freebsd, solaris, etc. - completely untested */
282 remote_client_send(rc, "\n");
287 static sig_atomic_t sigpipe_occured = FALSE;
289 static void sighandler_sigpipe(gint sig)
291 sigpipe_occured = TRUE;
294 static gint remote_client_send(RemoteConnection *rc, const gchar *text)
296 struct sigaction new_action, old_action;
299 if (!rc || rc->server) return FALSE;
300 if (!text) return TRUE;
302 sigpipe_occured = FALSE;
304 new_action.sa_handler = sighandler_sigpipe;
305 sigemptyset(&new_action.sa_mask);
306 new_action.sa_flags = 0;
308 /* setup our signal handler */
309 sigaction(SIGPIPE, &new_action, &old_action);
311 if (write(rc->fd, text, strlen(text)) == -1 ||
312 write(rc->fd, "\n", 1) == -1)
316 log_printf("SIGPIPE writing to socket: %s\n", rc->path);
320 log_printf("error writing to socket: %s\n", strerror(errno));
329 /* restore the original signal handler */
330 sigaction(SIGPIPE, &old_action, NULL);
335 void remote_close(RemoteConnection *rc)
341 remote_server_clients_close(rc);
343 g_source_remove(rc->channel_id);
348 g_free(rc->read_data);
357 *-----------------------------------------------------------------------------
359 *-----------------------------------------------------------------------------
362 static void gr_image_next(const gchar *text, gpointer data)
364 layout_image_next(NULL);
367 static void gr_image_prev(const gchar *text, gpointer data)
369 layout_image_prev(NULL);
372 static void gr_image_first(const gchar *text, gpointer data)
374 layout_image_first(NULL);
377 static void gr_image_last(const gchar *text, gpointer data)
379 layout_image_last(NULL);
382 static void gr_fullscreen_toggle(const gchar *text, gpointer data)
384 layout_image_full_screen_toggle(NULL);
387 static void gr_fullscreen_start(const gchar *text, gpointer data)
389 layout_image_full_screen_start(NULL);
392 static void gr_fullscreen_stop(const gchar *text, gpointer data)
394 layout_image_full_screen_stop(NULL);
397 static void gr_slideshow_start_rec(const gchar *text, gpointer data)
400 FileData *dir_fd = file_data_new_simple(text);
401 list = filelist_recursive(dir_fd);
402 file_data_unref(dir_fd);
404 //printf("length: %d\n", g_list_length(list));
405 layout_image_slideshow_stop(NULL);
406 layout_image_slideshow_start_from_list(NULL, list);
409 static void gr_slideshow_toggle(const gchar *text, gpointer data)
411 layout_image_slideshow_toggle(NULL);
414 static void gr_slideshow_start(const gchar *text, gpointer data)
416 layout_image_slideshow_start(NULL);
419 static void gr_slideshow_stop(const gchar *text, gpointer data)
421 layout_image_slideshow_stop(NULL);
424 static void gr_slideshow_delay(const gchar *text, gpointer data)
428 n = g_ascii_strtod(text, NULL);
429 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
431 printf_term("Remote slideshow delay out of range (%.1f to %.1f)\n",
432 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
435 options->slideshow.delay = (gint)(n * 10.0 + 0.01);
438 static void gr_tools_show(const gchar *text, gpointer data)
443 if (layout_tools_float_get(NULL, &popped, &hidden) && hidden)
445 layout_tools_float_set(NULL, popped, FALSE);
449 static void gr_tools_hide(const gchar *text, gpointer data)
454 if (layout_tools_float_get(NULL, &popped, &hidden) && !hidden)
456 layout_tools_float_set(NULL, popped, TRUE);
460 static gint gr_quit_idle_cb(gpointer data)
467 static void gr_quit(const gchar *text, gpointer data)
469 /* schedule exit when idle, if done from within a
470 * remote handler remote_close will crash
472 g_idle_add(gr_quit_idle_cb, NULL);
475 static void gr_file_load(const gchar *text, gpointer data)
477 gchar *filename = expand_tilde(text);
479 if (isfile(filename))
481 if (file_extension_match(filename, GQ_COLLECTION_EXT))
483 collection_window_new(filename);
487 layout_set_path(NULL, filename);
490 else if (isdir(filename))
492 layout_set_path(NULL, filename);
496 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
502 static void gr_file_view(const gchar *text, gpointer data)
504 gchar *filename = expand_tilde(text);
506 view_window_new(file_data_new_simple(filename));
510 static void gr_list_clear(const gchar *text, gpointer data)
512 RemoteData *remote_data = data;
514 if (remote_data->command_collection)
516 collection_unref(remote_data->command_collection);
517 remote_data->command_collection = NULL;
521 static void gr_list_add(const gchar *text, gpointer data)
523 RemoteData *remote_data = data;
526 if (!remote_data->command_collection)
530 cd = collection_new("");
535 cd->name = g_strdup(_("Command line"));
537 remote_data->command_collection = cd;
541 new = (!collection_get_first(remote_data->command_collection));
544 if (collection_add(remote_data->command_collection, file_data_new_simple(text), FALSE) && new)
546 layout_image_set_collection(NULL, remote_data->command_collection,
547 collection_get_first(remote_data->command_collection));
551 static void gr_raise(const gchar *text, gpointer data)
553 LayoutWindow *lw = NULL;
555 if (layout_valid(&lw))
557 gtk_window_present(GTK_WINDOW(lw->window));
561 typedef struct _RemoteCommandEntry RemoteCommandEntry;
562 struct _RemoteCommandEntry {
565 void (*func)(const gchar *text, gpointer data);
567 gint prefer_command_line;
571 static RemoteCommandEntry remote_commands[] = {
572 /* short, long callback, extra, prefer,description */
573 { "-n", "--next", gr_image_next, FALSE, FALSE, N_("next image") },
574 { "-b", "--back", gr_image_prev, FALSE, FALSE, N_("previous image") },
575 { NULL, "--first", gr_image_first, FALSE, FALSE, N_("first image") },
576 { NULL, "--last", gr_image_last, FALSE, FALSE, N_("last image") },
577 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, N_("toggle full screen") },
578 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, N_("start full screen") },
579 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, N_("stop full screen") },
580 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, N_("toggle slide show") },
581 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, N_("start slide show") },
582 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, N_("stop slide show") },
583 { "-sr","--slideshow-recurse", gr_slideshow_start_rec, TRUE, FALSE, N_("start recursive slide show") },
584 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("set slide show delay in seconds") },
585 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, N_("show tools") },
586 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, N_("hide tools") },
587 { "-q", "--quit", gr_quit, FALSE, FALSE, N_("quit") },
588 { NULL, "file:", gr_file_load, TRUE, FALSE, N_("open file") },
589 { NULL, "view:", gr_file_view, TRUE, FALSE, N_("open file in new window") },
590 { NULL, "--list-clear", gr_list_clear, FALSE, FALSE, NULL },
591 { NULL, "--list-add:", gr_list_add, TRUE, FALSE, NULL },
592 { NULL, "raise", gr_raise, FALSE, FALSE, NULL },
593 { NULL, NULL, NULL, FALSE, FALSE, NULL }
596 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
602 while (!match && remote_commands[i].func != NULL)
604 if (remote_commands[i].needs_extra)
606 if (remote_commands[i].opt_s &&
607 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
609 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
610 return &remote_commands[i];
612 else if (remote_commands[i].opt_l &&
613 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
615 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
616 return &remote_commands[i];
621 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
622 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
624 if (offset) *offset = text;
625 return &remote_commands[i];
635 static void remote_cb(RemoteConnection *rc, const gchar *text, gpointer data)
637 RemoteCommandEntry *entry;
640 entry = remote_command_find(text, &offset);
641 if (entry && entry->func)
643 entry->func(offset, data);
647 log_printf("unknown remote command:%s\n", text);
651 void remote_help(void)
655 print_term(_("Remote command list:\n"));
658 while (remote_commands[i].func != NULL)
660 if (remote_commands[i].description)
662 printf_term(" %-3s%s %-20s %s\n",
663 (remote_commands[i].opt_s) ? remote_commands[i].opt_s : "",
664 (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
665 (remote_commands[i].opt_l) ? remote_commands[i].opt_l : "",
666 _(remote_commands[i].description));
672 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
679 RemoteCommandEntry *entry;
681 entry = remote_command_find(argv[i], NULL);
684 list = g_list_append(list, argv[i]);
688 *errors = g_list_append(*errors, argv[i]);
696 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
697 GList *cmd_list, GList *collection_list)
699 RemoteConnection *rc;
700 gint started = FALSE;
703 buf = g_build_filename(get_rc_dir(), ".command", NULL);
704 rc = remote_client_open(buf);
709 gint retry_count = 12;
712 printf_term(_("Remote %s not running, starting..."), GQ_APPNAME);
714 command = g_string_new(arg_exec);
720 RemoteCommandEntry *entry;
725 entry = remote_command_find(text, NULL);
728 if (entry->prefer_command_line)
730 remote_list = g_list_remove(remote_list, text);
731 g_string_append(command, " ");
732 g_string_append(command, text);
734 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
741 if (blank || cmd_list || path) g_string_append(command, " --blank");
742 if (get_debug_level()) g_string_append(command, " --debug");
744 g_string_append(command, " &");
745 runcmd(command->str);
746 g_string_free(command, TRUE);
748 while (!rc && retry_count > 0)
750 usleep((retry_count > 10) ? 500000 : 1000000);
751 rc = remote_client_open(buf);
752 if (!rc) print_term(".");
766 gint use_path = TRUE;
773 RemoteCommandEntry *entry;
778 entry = remote_command_find(text, NULL);
781 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
783 remote_client_send(rc, text);
788 if (cmd_list && cmd_list->next)
790 prefix = "--list-add:";
791 remote_client_send(rc, "--list-clear");
807 text = g_strconcat(prefix, fd->path, NULL);
808 remote_client_send(rc, text);
814 if (path && !cmd_list && use_path)
818 text = g_strdup_printf("file:%s", path);
819 remote_client_send(rc, text);
825 work = collection_list;
834 text = g_strdup_printf("file:%s", name);
835 remote_client_send(rc, text);
841 if (!started && !sent)
843 remote_client_send(rc, "raise");
848 print_term(_("Remote not available\n"));
854 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
856 RemoteConnection *remote_connection = remote_server_open(path);
857 RemoteData *remote_data = g_new(RemoteData, 1);
859 remote_data->command_collection = command_collection;
861 remote_server_subscribe(remote_connection, remote_cb, remote_data);
862 return remote_connection;
864 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */