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.
29 #include "layout_image.h"
31 #include "slideshow.h"
32 #include "ui_fileops.h"
35 #include <sys/socket.h>
41 #define SERVER_MAX_CLIENTS 8
43 #define REMOTE_SERVER_BACKLOG 4
47 #define UNIX_PATH_MAX 108
51 static RemoteConnection *remote_client_open(const gchar *path);
52 static gint remote_client_send(RemoteConnection *rc, const gchar *text);
55 typedef struct _RemoteClient RemoteClient;
56 struct _RemoteClient {
58 guint channel_id; /* event source id */
62 typedef struct _RemoteData RemoteData;
64 CollectionData *command_collection;
68 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
70 RemoteClient *client = data;
72 GIOStatus status = G_IO_STATUS_NORMAL;
76 if (condition & G_IO_IN)
82 while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
86 buffer[termpos] = '\0';
88 if (strlen(buffer) > 0)
90 if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
91 g_io_channel_write_chars(source, "\n", -1, NULL, NULL); /* empty line finishes the command */
92 g_io_channel_flush(source, NULL);
102 log_printf("error reading socket: %s\n", error->message);
107 if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
109 rc->clients = g_list_remove(rc->clients, client);
111 DEBUG_1("HUP detected, closing client.");
112 DEBUG_1("client count %d", g_list_length(rc->clients));
114 g_source_remove(client->channel_id);
122 static void remote_server_client_add(RemoteConnection *rc, gint fd)
124 RemoteClient *client;
127 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
129 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
134 client = g_new0(RemoteClient, 1);
138 channel = g_io_channel_unix_new(fd);
139 client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP,
140 remote_server_client_cb, client, NULL);
141 g_io_channel_unref(channel);
143 rc->clients = g_list_append(rc->clients, client);
144 DEBUG_1("client count %d", g_list_length(rc->clients));
147 static void remote_server_clients_close(RemoteConnection *rc)
151 RemoteClient *client = rc->clients->data;
153 rc->clients = g_list_remove(rc->clients, client);
155 g_source_remove(client->channel_id);
161 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
163 RemoteConnection *rc = data;
167 fd = accept(rc->fd, NULL, &alen);
170 log_printf("error accepting socket: %s\n", strerror(errno));
174 remote_server_client_add(rc, fd);
179 static gboolean remote_server_exists(const gchar *path)
181 RemoteConnection *rc;
183 /* verify server up */
184 rc = remote_client_open(path);
189 /* unable to connect, remove socket file to free up address */
194 static RemoteConnection *remote_server_open(const gchar *path)
196 RemoteConnection *rc;
197 struct sockaddr_un addr;
202 if (remote_server_exists(path))
204 log_printf("Address already in use: %s\n", path);
208 fd = socket(PF_UNIX, SOCK_STREAM, 0);
209 if (fd == -1) return NULL;
211 addr.sun_family = AF_UNIX;
212 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
213 strncpy(addr.sun_path, path, sun_path_len);
214 if (bind(fd, &addr, sizeof(addr)) == -1 ||
215 listen(fd, REMOTE_SERVER_BACKLOG) == -1)
217 log_printf("error subscribing to socket: %s\n", strerror(errno));
222 rc = g_new0(RemoteConnection, 1);
226 rc->path = g_strdup(path);
228 channel = g_io_channel_unix_new(rc->fd);
229 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
231 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
232 remote_server_read_cb, rc, NULL);
233 g_io_channel_unref(channel);
238 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
240 if (!rc || !rc->server) return;
242 rc->read_func = func;
243 rc->read_data = data;
247 static RemoteConnection *remote_client_open(const gchar *path)
249 RemoteConnection *rc;
251 struct sockaddr_un addr;
255 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
257 fd = socket(PF_UNIX, SOCK_STREAM, 0);
258 if (fd == -1) return NULL;
260 addr.sun_family = AF_UNIX;
261 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
262 strncpy(addr.sun_path, path, sun_path_len);
263 if (connect(fd, &addr, sizeof(addr)) == -1)
265 DEBUG_1("error connecting to socket: %s", strerror(errno));
270 rc = g_new0(RemoteConnection, 1);
273 rc->path = g_strdup(path);
278 static sig_atomic_t sigpipe_occured = FALSE;
280 static void sighandler_sigpipe(gint sig)
282 sigpipe_occured = TRUE;
285 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
287 struct sigaction new_action, old_action;
288 gboolean ret = FALSE;
289 GError *error = NULL;
292 if (!rc || rc->server) return FALSE;
293 if (!text) return TRUE;
295 sigpipe_occured = FALSE;
297 new_action.sa_handler = sighandler_sigpipe;
298 sigemptyset(&new_action.sa_mask);
299 new_action.sa_flags = 0;
301 /* setup our signal handler */
302 sigaction(SIGPIPE, &new_action, &old_action);
304 channel = g_io_channel_unix_new(rc->fd);
306 g_io_channel_write_chars(channel, text, -1, NULL, &error);
307 g_io_channel_write_chars(channel, "\n", -1, NULL, &error);
308 g_io_channel_flush(channel, &error);
312 log_printf("error reading socket: %s\n", error->message);
323 gchar *buffer = NULL;
325 while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
329 if (buffer[0] == '\n') /* empty line finishes the command */
335 buffer[termpos] = '\0';
336 printf("%s\n", buffer);
344 log_printf("error reading socket: %s\n", error->message);
351 /* restore the original signal handler */
352 sigaction(SIGPIPE, &old_action, NULL);
353 g_io_channel_unref(channel);
357 void remote_close(RemoteConnection *rc)
363 remote_server_clients_close(rc);
365 g_source_remove(rc->channel_id);
370 g_free(rc->read_data);
379 *-----------------------------------------------------------------------------
381 *-----------------------------------------------------------------------------
384 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data)
386 layout_image_next(NULL);
389 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data)
391 layout_image_prev(NULL);
394 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data)
396 layout_image_first(NULL);
399 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data)
401 layout_image_last(NULL);
404 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data)
406 layout_image_full_screen_toggle(NULL);
409 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data)
411 layout_image_full_screen_start(NULL);
414 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data)
416 layout_image_full_screen_stop(NULL);
419 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *channel, gpointer data)
422 FileData *dir_fd = file_data_new_dir(text);
423 list = filelist_recursive(dir_fd);
424 file_data_unref(dir_fd);
426 //printf("length: %d\n", g_list_length(list));
427 layout_image_slideshow_stop(NULL);
428 layout_image_slideshow_start_from_list(NULL, list);
431 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data)
433 layout_image_slideshow_toggle(NULL);
436 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data)
438 layout_image_slideshow_start(NULL);
441 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data)
443 layout_image_slideshow_stop(NULL);
446 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data)
450 n = g_ascii_strtod(text, NULL);
451 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
453 printf_term("Remote slideshow delay out of range (%.1f to %.1f)\n",
454 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
457 options->slideshow.delay = (gint)(n * 10.0 + 0.01);
460 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data)
465 if (layout_tools_float_get(NULL, &popped, &hidden) && hidden)
467 layout_tools_float_set(NULL, popped, FALSE);
471 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data)
476 if (layout_tools_float_get(NULL, &popped, &hidden) && !hidden)
478 layout_tools_float_set(NULL, popped, TRUE);
482 static gboolean gr_quit_idle_cb(gpointer data)
489 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data)
491 /* schedule exit when idle, if done from within a
492 * remote handler remote_close will crash
494 g_idle_add(gr_quit_idle_cb, NULL);
497 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
499 gchar *filename = expand_tilde(text);
501 if (isfile(filename))
503 if (file_extension_match(filename, GQ_COLLECTION_EXT))
505 collection_window_new(filename);
509 layout_set_path(NULL, filename);
512 else if (isdir(filename))
514 layout_set_path(NULL, filename);
518 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
524 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
526 gchar *filename = expand_tilde(text);
528 if (isfile(filename))
530 load_config_from_file(filename, FALSE);
534 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
540 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer data)
542 gchar *filename = expand_tilde(text);
543 FileData *fd = file_data_new_group(filename);
546 if (fd->parent) fd = fd->parent;
548 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
549 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
551 work = fd->sidecar_files;
557 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
558 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
563 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer data)
565 gchar *filename = expand_tilde(text);
566 FileData *fd = file_data_new_group(filename);
568 if (fd->change && fd->change->dest)
570 g_io_channel_write_chars(channel, fd->change->dest, -1, NULL, NULL);
571 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
576 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
578 gchar *filename = expand_tilde(text);
580 view_window_new(file_data_new_group(filename));
584 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
586 RemoteData *remote_data = data;
588 if (remote_data->command_collection)
590 collection_unref(remote_data->command_collection);
591 remote_data->command_collection = NULL;
595 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
597 RemoteData *remote_data = data;
600 if (!remote_data->command_collection)
604 cd = collection_new("");
609 cd->name = g_strdup(_("Command line"));
611 remote_data->command_collection = cd;
615 new = (!collection_get_first(remote_data->command_collection));
618 if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && new)
620 layout_image_set_collection(NULL, remote_data->command_collection,
621 collection_get_first(remote_data->command_collection));
625 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
627 LayoutWindow *lw = NULL;
629 if (layout_valid(&lw))
631 gtk_window_present(GTK_WINDOW(lw->window));
635 typedef struct _RemoteCommandEntry RemoteCommandEntry;
636 struct _RemoteCommandEntry {
639 void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
640 gboolean needs_extra;
641 gboolean prefer_command_line;
646 static RemoteCommandEntry remote_commands[] = {
647 /* short, long callback, extra, prefer, parameter, description */
648 { "-n", "--next", gr_image_next, FALSE, FALSE, NULL, N_("next image") },
649 { "-b", "--back", gr_image_prev, FALSE, FALSE, NULL, N_("previous image") },
650 { NULL, "--first", gr_image_first, FALSE, FALSE, NULL, N_("first image") },
651 { NULL, "--last", gr_image_last, FALSE, FALSE, NULL, N_("last image") },
652 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, NULL, N_("toggle full screen") },
653 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, NULL, N_("start full screen") },
654 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, NULL, N_("stop full screen") },
655 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, NULL, N_("toggle slide show") },
656 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, NULL, N_("start slide show") },
657 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, NULL, N_("stop slide show") },
658 { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE, FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
659 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("<[N][.M]>"), N_("set slide show delay to N.M seconds") },
660 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, NULL, N_("show tools") },
661 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, NULL, N_("hide tools") },
662 { "-q", "--quit", gr_quit, FALSE, FALSE, NULL, N_("quit") },
663 { NULL, "--config-load:", gr_config_load, TRUE, FALSE, N_("<FILE>"), N_("load configuration from FILE") },
664 { NULL, "--get-sidecars:", gr_get_sidecars, TRUE, FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
665 { NULL, "--get-destination:", gr_get_destination, TRUE, FALSE, N_("<FILE>"), N_("get destination path of FILE") },
666 { NULL, "file:", gr_file_load, TRUE, FALSE, N_("<FILE>"), N_("open FILE") },
667 { NULL, "view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
668 { NULL, "--list-clear", gr_list_clear, FALSE, FALSE, NULL, N_("clear command line collection list") },
669 { NULL, "--list-add:", gr_list_add, TRUE, FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
670 { NULL, "raise", gr_raise, FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
671 { NULL, NULL, NULL, FALSE, FALSE, NULL }
674 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
676 gboolean match = FALSE;
680 while (!match && remote_commands[i].func != NULL)
682 if (remote_commands[i].needs_extra)
684 if (remote_commands[i].opt_s &&
685 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
687 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
688 return &remote_commands[i];
690 else if (remote_commands[i].opt_l &&
691 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
693 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
694 return &remote_commands[i];
699 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
700 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
702 if (offset) *offset = text;
703 return &remote_commands[i];
713 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
715 RemoteCommandEntry *entry;
718 entry = remote_command_find(text, &offset);
719 if (entry && entry->func)
721 entry->func(offset, channel, data);
725 log_printf("unknown remote command:%s\n", text);
729 void remote_help(void)
735 print_term(_("Remote command list:\n"));
738 while (remote_commands[i].func != NULL)
740 if (remote_commands[i].description)
742 s_opt_param = g_strconcat(remote_commands[i].opt_s, remote_commands[i].parameter, NULL);
743 l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
744 printf_term(" %-11s%-1s %-30s%-s\n",
745 (remote_commands[i].opt_s) ? s_opt_param : "",
746 (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
747 (remote_commands[i].opt_l) ? l_opt_param : "",
748 _(remote_commands[i].description));
754 printf_term(N_("\n All other command line parameters are used as plain files if they exists.\n"));
757 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
764 RemoteCommandEntry *entry;
766 entry = remote_command_find(argv[i], NULL);
769 list = g_list_append(list, argv[i]);
771 else if (errors && !isfile(argv[i]))
773 *errors = g_list_append(*errors, argv[i]);
782 * \param arg_exec Binary (argv0)
783 * \param remote_list Evaluated and recognized remote commands
784 * \param path The current path
785 * \param cmd_list List of all non collections in Path
786 * \param collection_list List of all collections in argv
788 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
789 GList *cmd_list, GList *collection_list)
791 RemoteConnection *rc;
792 gboolean started = FALSE;
795 buf = g_build_filename(get_rc_dir(), ".command", NULL);
796 rc = remote_client_open(buf);
801 gint retry_count = 12;
802 gboolean blank = FALSE;
804 printf_term(_("Remote %s not running, starting..."), GQ_APPNAME);
806 command = g_string_new(arg_exec);
812 RemoteCommandEntry *entry;
817 entry = remote_command_find(text, NULL);
820 if (entry->prefer_command_line)
822 remote_list = g_list_remove(remote_list, text);
823 g_string_append(command, " ");
824 g_string_append(command, text);
826 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
833 if (blank || cmd_list || path) g_string_append(command, " --blank");
834 if (get_debug_level()) g_string_append(command, " --debug");
836 g_string_append(command, " &");
837 runcmd(command->str);
838 g_string_free(command, TRUE);
840 while (!rc && retry_count > 0)
842 usleep((retry_count > 10) ? 500000 : 1000000);
843 rc = remote_client_open(buf);
844 if (!rc) print_term(".");
858 gboolean use_path = TRUE;
859 gboolean sent = FALSE;
865 RemoteCommandEntry *entry;
870 entry = remote_command_find(text, NULL);
873 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
875 remote_client_send(rc, text);
880 if (cmd_list && cmd_list->next)
882 prefix = "--list-add:";
883 remote_client_send(rc, "--list-clear");
899 text = g_strconcat(prefix, fd->path, NULL);
900 remote_client_send(rc, text);
906 if (path && !cmd_list && use_path)
910 text = g_strdup_printf("file:%s", path);
911 remote_client_send(rc, text);
917 work = collection_list;
926 text = g_strdup_printf("file:%s", name);
927 remote_client_send(rc, text);
933 if (!started && !sent)
935 remote_client_send(rc, "raise");
940 print_term(_("Remote not available\n"));
946 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
948 RemoteConnection *remote_connection = remote_server_open(path);
949 RemoteData *remote_data = g_new(RemoteData, 1);
951 remote_data->command_collection = command_collection;
953 remote_server_subscribe(remote_connection, remote_cb, remote_data);
954 return remote_connection;
956 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */