4 * Copyright (C) 2008 - 2012 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"
27 #include <sys/socket.h>
33 #define SERVER_MAX_CLIENTS 8
35 #define REMOTE_SERVER_BACKLOG 4
39 #define UNIX_PATH_MAX 108
43 static RemoteConnection *remote_client_open(const gchar *path);
44 static gint remote_client_send(RemoteConnection *rc, const gchar *text);
47 typedef struct _RemoteClient RemoteClient;
48 struct _RemoteClient {
50 guint channel_id; /* event source id */
54 typedef struct _RemoteData RemoteData;
56 CollectionData *command_collection;
60 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
62 RemoteClient *client = data;
64 GIOStatus status = G_IO_STATUS_NORMAL;
68 if (condition & G_IO_IN)
74 while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
78 buffer[termpos] = '\0';
80 if (strlen(buffer) > 0)
82 if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
83 g_io_channel_write_chars(source, "\n", -1, NULL, NULL); /* empty line finishes the command */
84 g_io_channel_flush(source, NULL);
94 log_printf("error reading socket: %s\n", error->message);
99 if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
101 rc->clients = g_list_remove(rc->clients, client);
103 DEBUG_1("HUP detected, closing client.");
104 DEBUG_1("client count %d", g_list_length(rc->clients));
106 g_source_remove(client->channel_id);
114 static void remote_server_client_add(RemoteConnection *rc, gint fd)
116 RemoteClient *client;
119 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
121 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
126 client = g_new0(RemoteClient, 1);
130 channel = g_io_channel_unix_new(fd);
131 client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP,
132 remote_server_client_cb, client, NULL);
133 g_io_channel_unref(channel);
135 rc->clients = g_list_append(rc->clients, client);
136 DEBUG_1("client count %d", g_list_length(rc->clients));
139 static void remote_server_clients_close(RemoteConnection *rc)
143 RemoteClient *client = rc->clients->data;
145 rc->clients = g_list_remove(rc->clients, client);
147 g_source_remove(client->channel_id);
153 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
155 RemoteConnection *rc = data;
159 fd = accept(rc->fd, NULL, &alen);
162 log_printf("error accepting socket: %s\n", strerror(errno));
166 remote_server_client_add(rc, fd);
171 static gboolean remote_server_exists(const gchar *path)
173 RemoteConnection *rc;
175 /* verify server up */
176 rc = remote_client_open(path);
181 /* unable to connect, remove socket file to free up address */
186 static RemoteConnection *remote_server_open(const gchar *path)
188 RemoteConnection *rc;
189 struct sockaddr_un addr;
194 if (remote_server_exists(path))
196 log_printf("Address already in use: %s\n", path);
200 fd = socket(PF_UNIX, SOCK_STREAM, 0);
201 if (fd == -1) return NULL;
203 addr.sun_family = AF_UNIX;
204 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
205 strncpy(addr.sun_path, path, sun_path_len);
206 if (bind(fd, &addr, sizeof(addr)) == -1 ||
207 listen(fd, REMOTE_SERVER_BACKLOG) == -1)
209 log_printf("error subscribing to socket: %s\n", strerror(errno));
214 rc = g_new0(RemoteConnection, 1);
218 rc->path = g_strdup(path);
220 channel = g_io_channel_unix_new(rc->fd);
221 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
223 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
224 remote_server_read_cb, rc, NULL);
225 g_io_channel_unref(channel);
230 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
232 if (!rc || !rc->server) return;
234 rc->read_func = func;
235 rc->read_data = data;
239 static RemoteConnection *remote_client_open(const gchar *path)
241 RemoteConnection *rc;
243 struct sockaddr_un addr;
247 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
249 fd = socket(PF_UNIX, SOCK_STREAM, 0);
250 if (fd == -1) return NULL;
252 addr.sun_family = AF_UNIX;
253 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
254 strncpy(addr.sun_path, path, sun_path_len);
255 if (connect(fd, &addr, sizeof(addr)) == -1)
257 DEBUG_1("error connecting to socket: %s", strerror(errno));
262 rc = g_new0(RemoteConnection, 1);
265 rc->path = g_strdup(path);
270 static sig_atomic_t sigpipe_occured = FALSE;
272 static void sighandler_sigpipe(gint sig)
274 sigpipe_occured = TRUE;
277 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
279 struct sigaction new_action, old_action;
280 gboolean ret = FALSE;
281 GError *error = NULL;
284 if (!rc || rc->server) return FALSE;
285 if (!text) return TRUE;
287 sigpipe_occured = FALSE;
289 new_action.sa_handler = sighandler_sigpipe;
290 sigemptyset(&new_action.sa_mask);
291 new_action.sa_flags = 0;
293 /* setup our signal handler */
294 sigaction(SIGPIPE, &new_action, &old_action);
296 channel = g_io_channel_unix_new(rc->fd);
298 g_io_channel_write_chars(channel, text, -1, NULL, &error);
299 g_io_channel_write_chars(channel, "\n", -1, NULL, &error);
300 g_io_channel_flush(channel, &error);
304 log_printf("error reading socket: %s\n", error->message);
315 gchar *buffer = NULL;
317 while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
321 if (buffer[0] == '\n') /* empty line finishes the command */
327 buffer[termpos] = '\0';
328 printf("%s\n", buffer);
336 log_printf("error reading socket: %s\n", error->message);
343 /* restore the original signal handler */
344 sigaction(SIGPIPE, &old_action, NULL);
345 g_io_channel_unref(channel);
349 void remote_close(RemoteConnection *rc)
355 remote_server_clients_close(rc);
357 g_source_remove(rc->channel_id);
362 g_free(rc->read_data);
371 *-----------------------------------------------------------------------------
373 *-----------------------------------------------------------------------------
376 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data)
378 layout_image_next(NULL);
381 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data)
383 layout_image_prev(NULL);
386 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data)
388 layout_image_first(NULL);
391 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data)
393 layout_image_last(NULL);
396 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data)
398 layout_image_full_screen_toggle(NULL);
401 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data)
403 layout_image_full_screen_start(NULL);
406 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data)
408 layout_image_full_screen_stop(NULL);
411 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *channel, gpointer data)
414 FileData *dir_fd = file_data_new_dir(text);
415 list = filelist_recursive(dir_fd);
416 file_data_unref(dir_fd);
418 //printf("length: %d\n", g_list_length(list));
419 layout_image_slideshow_stop(NULL);
420 layout_image_slideshow_start_from_list(NULL, list);
423 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data)
425 layout_image_slideshow_toggle(NULL);
428 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data)
430 layout_image_slideshow_start(NULL);
433 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data)
435 layout_image_slideshow_stop(NULL);
438 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data)
442 n = g_ascii_strtod(text, NULL);
443 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
445 printf_term("Remote slideshow delay out of range (%.1f to %.1f)\n",
446 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
449 options->slideshow.delay = (gint)(n * 10.0 + 0.01);
452 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data)
457 if (layout_tools_float_get(NULL, &popped, &hidden) && hidden)
459 layout_tools_float_set(NULL, popped, FALSE);
463 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data)
468 if (layout_tools_float_get(NULL, &popped, &hidden) && !hidden)
470 layout_tools_float_set(NULL, popped, TRUE);
474 static gboolean gr_quit_idle_cb(gpointer data)
481 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data)
483 /* schedule exit when idle, if done from within a
484 * remote handler remote_close will crash
486 g_idle_add(gr_quit_idle_cb, NULL);
489 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
491 gchar *filename = expand_tilde(text);
493 if (isfile(filename))
495 if (file_extension_match(filename, GQ_COLLECTION_EXT))
497 collection_window_new(filename);
501 layout_set_path(NULL, filename);
504 else if (isdir(filename))
506 layout_set_path(NULL, filename);
510 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
516 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
518 gchar *filename = expand_tilde(text);
520 if (isfile(filename))
522 load_config_from_file(filename, FALSE);
526 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
532 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer data)
534 gchar *filename = expand_tilde(text);
535 FileData *fd = file_data_new_group(filename);
538 if (fd->parent) fd = fd->parent;
540 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
541 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
543 work = fd->sidecar_files;
549 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
550 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
555 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer data)
557 gchar *filename = expand_tilde(text);
558 FileData *fd = file_data_new_group(filename);
560 if (fd->change && fd->change->dest)
562 g_io_channel_write_chars(channel, fd->change->dest, -1, NULL, NULL);
563 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
568 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
570 gchar *filename = expand_tilde(text);
572 view_window_new(file_data_new_group(filename));
576 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
578 RemoteData *remote_data = data;
580 if (remote_data->command_collection)
582 collection_unref(remote_data->command_collection);
583 remote_data->command_collection = NULL;
587 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
589 RemoteData *remote_data = data;
592 if (!remote_data->command_collection)
596 cd = collection_new("");
601 cd->name = g_strdup(_("Command line"));
603 remote_data->command_collection = cd;
607 new = (!collection_get_first(remote_data->command_collection));
610 if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && new)
612 layout_image_set_collection(NULL, remote_data->command_collection,
613 collection_get_first(remote_data->command_collection));
617 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
619 LayoutWindow *lw = NULL;
621 if (layout_valid(&lw))
623 gtk_window_present(GTK_WINDOW(lw->window));
627 typedef struct _RemoteCommandEntry RemoteCommandEntry;
628 struct _RemoteCommandEntry {
631 void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
632 gboolean needs_extra;
633 gboolean prefer_command_line;
638 static RemoteCommandEntry remote_commands[] = {
639 /* short, long callback, extra, prefer, parameter, description */
640 { "-n", "--next", gr_image_next, FALSE, FALSE, NULL, N_("next image") },
641 { "-b", "--back", gr_image_prev, FALSE, FALSE, NULL, N_("previous image") },
642 { NULL, "--first", gr_image_first, FALSE, FALSE, NULL, N_("first image") },
643 { NULL, "--last", gr_image_last, FALSE, FALSE, NULL, N_("last image") },
644 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, NULL, N_("toggle full screen") },
645 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, NULL, N_("start full screen") },
646 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, NULL, N_("stop full screen") },
647 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, NULL, N_("toggle slide show") },
648 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, NULL, N_("start slide show") },
649 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, NULL, N_("stop slide show") },
650 { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE, FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
651 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("<[N][.M]>"), N_("set slide show delay to N.M seconds") },
652 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, NULL, N_("show tools") },
653 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, NULL, N_("hide tools") },
654 { "-q", "--quit", gr_quit, FALSE, FALSE, NULL, N_("quit") },
655 { NULL, "--config-load:", gr_config_load, TRUE, FALSE, N_("<FILE>"), N_("load configuration from FILE") },
656 { NULL, "--get-sidecars:", gr_get_sidecars, TRUE, FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
657 { NULL, "--get-destination:", gr_get_destination, TRUE, FALSE, N_("<FILE>"), N_("get destination path of FILE") },
658 { NULL, "file:", gr_file_load, TRUE, FALSE, N_("<FILE>"), N_("open FILE") },
659 { NULL, "view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
660 { NULL, "--list-clear", gr_list_clear, FALSE, FALSE, NULL, N_("clear command line collection list") },
661 { NULL, "--list-add:", gr_list_add, TRUE, FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
662 { NULL, "raise", gr_raise, FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
663 { NULL, NULL, NULL, FALSE, FALSE, NULL }
666 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
668 gboolean match = FALSE;
672 while (!match && remote_commands[i].func != NULL)
674 if (remote_commands[i].needs_extra)
676 if (remote_commands[i].opt_s &&
677 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
679 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
680 return &remote_commands[i];
682 else if (remote_commands[i].opt_l &&
683 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
685 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
686 return &remote_commands[i];
691 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
692 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
694 if (offset) *offset = text;
695 return &remote_commands[i];
705 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
707 RemoteCommandEntry *entry;
710 entry = remote_command_find(text, &offset);
711 if (entry && entry->func)
713 entry->func(offset, channel, data);
717 log_printf("unknown remote command:%s\n", text);
721 void remote_help(void)
727 print_term(_("Remote command list:\n"));
730 while (remote_commands[i].func != NULL)
732 if (remote_commands[i].description)
734 s_opt_param = g_strconcat(remote_commands[i].opt_s, remote_commands[i].parameter, NULL);
735 l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
736 printf_term(" %-11s%-1s %-30s%-s\n",
737 (remote_commands[i].opt_s) ? s_opt_param : "",
738 (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
739 (remote_commands[i].opt_l) ? l_opt_param : "",
740 _(remote_commands[i].description));
746 printf_term(N_("\n All other command line parameters are used as plain files if they exists.\n"));
749 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
756 RemoteCommandEntry *entry;
758 entry = remote_command_find(argv[i], NULL);
761 list = g_list_append(list, argv[i]);
763 else if (errors && !isfile(argv[i]))
765 *errors = g_list_append(*errors, argv[i]);
774 * \param arg_exec Binary (argv0)
775 * \param remote_list Evaluated and recognized remote commands
776 * \param path The current path
777 * \param cmd_list List of all non collections in Path
778 * \param collection_list List of all collections in argv
780 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
781 GList *cmd_list, GList *collection_list)
783 RemoteConnection *rc;
784 gboolean started = FALSE;
787 buf = g_build_filename(get_rc_dir(), ".command", NULL);
788 rc = remote_client_open(buf);
793 gint retry_count = 12;
794 gboolean blank = FALSE;
796 printf_term(_("Remote %s not running, starting..."), GQ_APPNAME);
798 command = g_string_new(arg_exec);
804 RemoteCommandEntry *entry;
809 entry = remote_command_find(text, NULL);
812 if (entry->prefer_command_line)
814 remote_list = g_list_remove(remote_list, text);
815 g_string_append(command, " ");
816 g_string_append(command, text);
818 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
825 if (blank || cmd_list || path) g_string_append(command, " --blank");
826 if (get_debug_level()) g_string_append(command, " --debug");
828 g_string_append(command, " &");
829 runcmd(command->str);
830 g_string_free(command, TRUE);
832 while (!rc && retry_count > 0)
834 usleep((retry_count > 10) ? 500000 : 1000000);
835 rc = remote_client_open(buf);
836 if (!rc) print_term(".");
850 gboolean use_path = TRUE;
851 gboolean sent = FALSE;
857 RemoteCommandEntry *entry;
862 entry = remote_command_find(text, NULL);
865 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
867 remote_client_send(rc, text);
872 if (cmd_list && cmd_list->next)
874 prefix = "--list-add:";
875 remote_client_send(rc, "--list-clear");
891 text = g_strconcat(prefix, fd->path, NULL);
892 remote_client_send(rc, text);
898 if (path && !cmd_list && use_path)
902 text = g_strdup_printf("file:%s", path);
903 remote_client_send(rc, text);
909 work = collection_list;
918 text = g_strdup_printf("file:%s", name);
919 remote_client_send(rc, text);
925 if (!started && !sent)
927 remote_client_send(rc, "raise");
932 print_term(_("Remote not available\n"));
938 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
940 RemoteConnection *remote_connection = remote_server_open(path);
941 RemoteData *remote_data = g_new(RemoteData, 1);
943 remote_data->command_collection = command_collection;
945 remote_server_subscribe(remote_connection, remote_cb, remote_data);
946 return remote_connection;
948 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */