2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
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.
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.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include "layout_image.h"
32 #include "slideshow.h"
33 #include "ui_fileops.h"
36 #include <sys/socket.h>
42 #define SERVER_MAX_CLIENTS 8
44 #define REMOTE_SERVER_BACKLOG 4
48 #define UNIX_PATH_MAX 108
52 static RemoteConnection *remote_client_open(const gchar *path);
53 static gint remote_client_send(RemoteConnection *rc, const gchar *text);
54 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data);
57 typedef struct _RemoteClient RemoteClient;
58 struct _RemoteClient {
60 guint channel_id; /* event source id */
64 typedef struct _RemoteData RemoteData;
66 CollectionData *command_collection;
70 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
72 RemoteClient *client = data;
74 GIOStatus status = G_IO_STATUS_NORMAL;
78 if (condition & G_IO_IN)
84 while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
88 buffer[termpos] = '\0';
90 if (strlen(buffer) > 0)
92 if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
93 g_io_channel_write_chars(source, "\n", -1, NULL, NULL); /* empty line finishes the command */
94 g_io_channel_flush(source, NULL);
104 log_printf("error reading socket: %s\n", error->message);
109 if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
111 rc->clients = g_list_remove(rc->clients, client);
113 DEBUG_1("HUP detected, closing client.");
114 DEBUG_1("client count %d", g_list_length(rc->clients));
116 g_source_remove(client->channel_id);
124 static void remote_server_client_add(RemoteConnection *rc, gint fd)
126 RemoteClient *client;
129 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
131 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
136 client = g_new0(RemoteClient, 1);
140 channel = g_io_channel_unix_new(fd);
141 client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP,
142 remote_server_client_cb, client, NULL);
143 g_io_channel_unref(channel);
145 rc->clients = g_list_append(rc->clients, client);
146 DEBUG_1("client count %d", g_list_length(rc->clients));
149 static void remote_server_clients_close(RemoteConnection *rc)
153 RemoteClient *client = rc->clients->data;
155 rc->clients = g_list_remove(rc->clients, client);
157 g_source_remove(client->channel_id);
163 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
165 RemoteConnection *rc = data;
169 fd = accept(rc->fd, NULL, &alen);
172 log_printf("error accepting socket: %s\n", strerror(errno));
176 remote_server_client_add(rc, fd);
181 static gboolean remote_server_exists(const gchar *path)
183 RemoteConnection *rc;
185 /* verify server up */
186 rc = remote_client_open(path);
191 /* unable to connect, remove socket file to free up address */
196 static RemoteConnection *remote_server_open(const gchar *path)
198 RemoteConnection *rc;
199 struct sockaddr_un addr;
204 if (remote_server_exists(path))
206 log_printf("Address already in use: %s\n", path);
210 fd = socket(PF_UNIX, SOCK_STREAM, 0);
211 if (fd == -1) return NULL;
213 addr.sun_family = AF_UNIX;
214 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
215 strncpy(addr.sun_path, path, sun_path_len);
216 if (bind(fd, &addr, sizeof(addr)) == -1 ||
217 listen(fd, REMOTE_SERVER_BACKLOG) == -1)
219 log_printf("error subscribing to socket: %s\n", strerror(errno));
224 rc = g_new0(RemoteConnection, 1);
228 rc->path = g_strdup(path);
230 channel = g_io_channel_unix_new(rc->fd);
231 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
233 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
234 remote_server_read_cb, rc, NULL);
235 g_io_channel_unref(channel);
240 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
242 if (!rc || !rc->server) return;
244 rc->read_func = func;
245 rc->read_data = data;
249 static RemoteConnection *remote_client_open(const gchar *path)
251 RemoteConnection *rc;
253 struct sockaddr_un addr;
257 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
259 fd = socket(PF_UNIX, SOCK_STREAM, 0);
260 if (fd == -1) return NULL;
262 addr.sun_family = AF_UNIX;
263 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
264 strncpy(addr.sun_path, path, sun_path_len);
265 if (connect(fd, &addr, sizeof(addr)) == -1)
267 DEBUG_1("error connecting to socket: %s", strerror(errno));
272 rc = g_new0(RemoteConnection, 1);
275 rc->path = g_strdup(path);
280 static sig_atomic_t sigpipe_occured = FALSE;
282 static void sighandler_sigpipe(gint sig)
284 sigpipe_occured = TRUE;
287 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
289 struct sigaction new_action, old_action;
290 gboolean ret = FALSE;
291 GError *error = NULL;
294 if (!rc || rc->server) return FALSE;
295 if (!text) return TRUE;
297 sigpipe_occured = FALSE;
299 new_action.sa_handler = sighandler_sigpipe;
300 sigemptyset(&new_action.sa_mask);
301 new_action.sa_flags = 0;
303 /* setup our signal handler */
304 sigaction(SIGPIPE, &new_action, &old_action);
306 channel = g_io_channel_unix_new(rc->fd);
308 g_io_channel_write_chars(channel, text, -1, NULL, &error);
309 g_io_channel_write_chars(channel, "\n", -1, NULL, &error);
310 g_io_channel_flush(channel, &error);
314 log_printf("error reading socket: %s\n", error->message);
325 gchar *buffer = NULL;
327 while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
331 if (buffer[0] == '\n') /* empty line finishes the command */
337 buffer[termpos] = '\0';
338 printf("%s\n", buffer);
346 log_printf("error reading socket: %s\n", error->message);
353 /* restore the original signal handler */
354 sigaction(SIGPIPE, &old_action, NULL);
355 g_io_channel_unref(channel);
359 void remote_close(RemoteConnection *rc)
365 remote_server_clients_close(rc);
367 g_source_remove(rc->channel_id);
372 g_free(rc->read_data);
381 *-----------------------------------------------------------------------------
383 *-----------------------------------------------------------------------------
386 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data)
388 layout_image_next(NULL);
391 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data)
393 layout_image_prev(NULL);
396 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data)
398 layout_image_first(NULL);
401 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data)
403 layout_image_last(NULL);
406 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data)
408 layout_image_full_screen_toggle(NULL);
411 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data)
413 layout_image_full_screen_start(NULL);
416 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data)
418 layout_image_full_screen_stop(NULL);
421 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *channel, gpointer data)
424 FileData *dir_fd = file_data_new_dir(text);
425 list = filelist_recursive(dir_fd);
426 file_data_unref(dir_fd);
428 //printf("length: %d\n", g_list_length(list));
429 layout_image_slideshow_stop(NULL);
430 layout_image_slideshow_start_from_list(NULL, list);
433 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data)
435 layout_image_slideshow_toggle(NULL);
438 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data)
440 layout_image_slideshow_start(NULL);
443 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data)
445 layout_image_slideshow_stop(NULL);
448 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data)
452 n = g_ascii_strtod(text, NULL);
453 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
455 printf_term("Remote slideshow delay out of range (%.1f to %.1f)\n",
456 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
459 options->slideshow.delay = (gint)(n * 10.0 + 0.01);
462 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data)
467 if (layout_tools_float_get(NULL, &popped, &hidden) && hidden)
469 layout_tools_float_set(NULL, popped, FALSE);
473 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data)
478 if (layout_tools_float_get(NULL, &popped, &hidden) && !hidden)
480 layout_tools_float_set(NULL, popped, TRUE);
484 static gboolean gr_quit_idle_cb(gpointer data)
491 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data)
493 /* schedule exit when idle, if done from within a
494 * remote handler remote_close will crash
496 g_idle_add(gr_quit_idle_cb, NULL);
499 static void gr_file_load_no_raise(const gchar *text, GIOChannel *channel, gpointer data)
501 gchar *filename = expand_tilde(text);
503 if (isfile(filename))
505 if (file_extension_match(filename, GQ_COLLECTION_EXT))
507 collection_window_new(filename);
511 layout_set_path(NULL, filename);
514 else if (isdir(filename))
516 layout_set_path(NULL, filename);
520 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
521 layout_set_path(NULL, homedir());
527 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
529 gr_file_load_no_raise(text, channel, data);
531 gr_raise(text, channel, data);
534 static void gr_file_tell(const gchar *text, GIOChannel *channel, gpointer data)
536 LayoutWindow *lw = NULL; /* NULL to force layout_valid() to do some magic */
537 if (!layout_valid(&lw)) return;
538 if (image_get_path(lw->image))
540 g_io_channel_write_chars(channel, image_get_path(lw->image), -1, NULL, NULL);
541 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
545 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
547 gchar *filename = expand_tilde(text);
549 if (isfile(filename))
551 load_config_from_file(filename, FALSE);
555 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
556 layout_set_path(NULL, homedir());
562 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer data)
564 gchar *filename = expand_tilde(text);
565 FileData *fd = file_data_new_group(filename);
568 if (fd->parent) fd = fd->parent;
570 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
571 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
573 work = fd->sidecar_files;
579 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
580 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
585 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer data)
587 gchar *filename = expand_tilde(text);
588 FileData *fd = file_data_new_group(filename);
590 if (fd->change && fd->change->dest)
592 g_io_channel_write_chars(channel, fd->change->dest, -1, NULL, NULL);
593 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
598 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
600 gchar *filename = expand_tilde(text);
602 view_window_new(file_data_new_group(filename));
606 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
608 RemoteData *remote_data = data;
610 if (remote_data->command_collection)
612 collection_unref(remote_data->command_collection);
613 remote_data->command_collection = NULL;
617 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
619 RemoteData *remote_data = data;
622 if (!remote_data->command_collection)
626 cd = collection_new("");
631 cd->name = g_strdup(_("Command line"));
633 remote_data->command_collection = cd;
637 new = (!collection_get_first(remote_data->command_collection));
640 if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && new)
642 layout_image_set_collection(NULL, remote_data->command_collection,
643 collection_get_first(remote_data->command_collection));
647 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
649 LayoutWindow *lw = NULL;
651 if (layout_valid(&lw))
653 gtk_window_present(GTK_WINDOW(lw->window));
657 typedef struct _RemoteCommandEntry RemoteCommandEntry;
658 struct _RemoteCommandEntry {
661 void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
662 gboolean needs_extra;
663 gboolean prefer_command_line;
668 static RemoteCommandEntry remote_commands[] = {
669 /* short, long callback, extra, prefer, parameter, description */
670 { "-n", "--next", gr_image_next, FALSE, FALSE, NULL, N_("next image") },
671 { "-b", "--back", gr_image_prev, FALSE, FALSE, NULL, N_("previous image") },
672 { NULL, "--first", gr_image_first, FALSE, FALSE, NULL, N_("first image") },
673 { NULL, "--last", gr_image_last, FALSE, FALSE, NULL, N_("last image") },
674 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, NULL, N_("toggle full screen") },
675 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, NULL, N_("start full screen") },
676 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, NULL, N_("stop full screen") },
677 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, NULL, N_("toggle slide show") },
678 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, NULL, N_("start slide show") },
679 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, NULL, N_("stop slide show") },
680 { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE, FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
681 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("<[N][.M]>"), N_("set slide show delay to N.M seconds") },
682 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, NULL, N_("show tools") },
683 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, NULL, N_("hide tools") },
684 { "-q", "--quit", gr_quit, FALSE, FALSE, NULL, N_("quit") },
685 { NULL, "--config-load:", gr_config_load, TRUE, FALSE, N_("<FILE>"), N_("load configuration from FILE") },
686 { NULL, "--get-sidecars:", gr_get_sidecars, TRUE, FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
687 { NULL, "--get-destination:", gr_get_destination, TRUE, FALSE, N_("<FILE>"), N_("get destination path of FILE") },
688 { NULL, "file:", gr_file_load, TRUE, FALSE, N_("<FILE>"), N_("open FILE, bring Geeqie window to the top") },
689 { NULL, "File:", gr_file_load_no_raise, TRUE, FALSE, N_("<FILE>"), N_("open FILE, do not bring Geeqie window to the top") },
690 { NULL, "--tell", gr_file_tell, FALSE, FALSE, NULL, N_("print filename of current image") },
691 { NULL, "view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
692 { NULL, "--list-clear", gr_list_clear, FALSE, FALSE, NULL, N_("clear command line collection list") },
693 { NULL, "--list-add:", gr_list_add, TRUE, FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
694 { NULL, "raise", gr_raise, FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
695 { NULL, NULL, NULL, FALSE, FALSE, NULL }
698 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
700 gboolean match = FALSE;
704 while (!match && remote_commands[i].func != NULL)
706 if (remote_commands[i].needs_extra)
708 if (remote_commands[i].opt_s &&
709 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
711 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
712 return &remote_commands[i];
714 else if (remote_commands[i].opt_l &&
715 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
717 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
718 return &remote_commands[i];
723 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
724 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
726 if (offset) *offset = text;
727 return &remote_commands[i];
737 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
739 RemoteCommandEntry *entry;
742 entry = remote_command_find(text, &offset);
743 if (entry && entry->func)
745 entry->func(offset, channel, data);
749 log_printf("unknown remote command:%s\n", text);
753 void remote_help(void)
759 print_term(_("Remote command list:\n"));
762 while (remote_commands[i].func != NULL)
764 if (remote_commands[i].description)
766 s_opt_param = g_strconcat(remote_commands[i].opt_s, remote_commands[i].parameter, NULL);
767 l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
768 printf_term(" %-11s%-1s %-30s%-s\n",
769 (remote_commands[i].opt_s) ? s_opt_param : "",
770 (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
771 (remote_commands[i].opt_l) ? l_opt_param : "",
772 _(remote_commands[i].description));
778 printf_term(N_("\n All other command line parameters are used as plain files if they exists.\n"));
781 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
788 RemoteCommandEntry *entry;
790 entry = remote_command_find(argv[i], NULL);
793 list = g_list_append(list, argv[i]);
795 else if (errors && !isfile(argv[i]))
797 *errors = g_list_append(*errors, argv[i]);
806 * \param arg_exec Binary (argv0)
807 * \param remote_list Evaluated and recognized remote commands
808 * \param path The current path
809 * \param cmd_list List of all non collections in Path
810 * \param collection_list List of all collections in argv
812 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
813 GList *cmd_list, GList *collection_list)
815 RemoteConnection *rc;
816 gboolean started = FALSE;
819 buf = g_build_filename(get_rc_dir(), ".command", NULL);
820 rc = remote_client_open(buf);
825 gint retry_count = 12;
826 gboolean blank = FALSE;
828 printf_term(_("Remote %s not running, starting..."), GQ_APPNAME);
830 command = g_string_new(arg_exec);
836 RemoteCommandEntry *entry;
841 entry = remote_command_find(text, NULL);
844 if (entry->prefer_command_line)
846 remote_list = g_list_remove(remote_list, text);
847 g_string_append(command, " ");
848 g_string_append(command, text);
850 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
857 if (blank || cmd_list || path) g_string_append(command, " --blank");
858 if (get_debug_level()) g_string_append(command, " --debug");
860 g_string_append(command, " &");
861 runcmd(command->str);
862 g_string_free(command, TRUE);
864 while (!rc && retry_count > 0)
866 usleep((retry_count > 10) ? 500000 : 1000000);
867 rc = remote_client_open(buf);
868 if (!rc) print_term(".");
882 gboolean use_path = TRUE;
883 gboolean sent = FALSE;
889 RemoteCommandEntry *entry;
894 entry = remote_command_find(text, NULL);
897 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
899 remote_client_send(rc, text);
904 if (cmd_list && cmd_list->next)
906 prefix = "--list-add:";
907 remote_client_send(rc, "--list-clear");
923 text = g_strconcat(prefix, fd->path, NULL);
924 remote_client_send(rc, text);
930 if (path && !cmd_list && use_path)
934 text = g_strdup_printf("file:%s", path);
935 remote_client_send(rc, text);
941 work = collection_list;
950 text = g_strdup_printf("file:%s", name);
951 remote_client_send(rc, text);
957 if (!started && !sent)
959 remote_client_send(rc, "raise");
964 print_term(_("Remote not available\n"));
970 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
972 RemoteConnection *remote_connection = remote_server_open(path);
973 RemoteData *remote_data = g_new(RemoteData, 1);
975 remote_data->command_collection = command_collection;
977 remote_server_subscribe(remote_connection, remote_cb, remote_data);
978 return remote_connection;
980 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */