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);
56 typedef struct _RemoteClient RemoteClient;
57 struct _RemoteClient {
59 guint channel_id; /* event source id */
63 typedef struct _RemoteData RemoteData;
65 CollectionData *command_collection;
69 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
71 RemoteClient *client = data;
73 GIOStatus status = G_IO_STATUS_NORMAL;
77 if (condition & G_IO_IN)
83 while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
87 buffer[termpos] = '\0';
89 if (strlen(buffer) > 0)
91 if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
92 g_io_channel_write_chars(source, "\n", -1, NULL, NULL); /* empty line finishes the command */
93 g_io_channel_flush(source, NULL);
103 log_printf("error reading socket: %s\n", error->message);
108 if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
110 rc->clients = g_list_remove(rc->clients, client);
112 DEBUG_1("HUP detected, closing client.");
113 DEBUG_1("client count %d", g_list_length(rc->clients));
115 g_source_remove(client->channel_id);
123 static void remote_server_client_add(RemoteConnection *rc, gint fd)
125 RemoteClient *client;
128 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
130 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
135 client = g_new0(RemoteClient, 1);
139 channel = g_io_channel_unix_new(fd);
140 client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP,
141 remote_server_client_cb, client, NULL);
142 g_io_channel_unref(channel);
144 rc->clients = g_list_append(rc->clients, client);
145 DEBUG_1("client count %d", g_list_length(rc->clients));
148 static void remote_server_clients_close(RemoteConnection *rc)
152 RemoteClient *client = rc->clients->data;
154 rc->clients = g_list_remove(rc->clients, client);
156 g_source_remove(client->channel_id);
162 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
164 RemoteConnection *rc = data;
168 fd = accept(rc->fd, NULL, &alen);
171 log_printf("error accepting socket: %s\n", strerror(errno));
175 remote_server_client_add(rc, fd);
180 static gboolean remote_server_exists(const gchar *path)
182 RemoteConnection *rc;
184 /* verify server up */
185 rc = remote_client_open(path);
190 /* unable to connect, remove socket file to free up address */
195 static RemoteConnection *remote_server_open(const gchar *path)
197 RemoteConnection *rc;
198 struct sockaddr_un addr;
203 if (remote_server_exists(path))
205 log_printf("Address already in use: %s\n", path);
209 fd = socket(PF_UNIX, SOCK_STREAM, 0);
210 if (fd == -1) return NULL;
212 addr.sun_family = AF_UNIX;
213 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
214 strncpy(addr.sun_path, path, sun_path_len);
215 if (bind(fd, &addr, sizeof(addr)) == -1 ||
216 listen(fd, REMOTE_SERVER_BACKLOG) == -1)
218 log_printf("error subscribing to socket: %s\n", strerror(errno));
223 rc = g_new0(RemoteConnection, 1);
227 rc->path = g_strdup(path);
229 channel = g_io_channel_unix_new(rc->fd);
230 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
232 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
233 remote_server_read_cb, rc, NULL);
234 g_io_channel_unref(channel);
239 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
241 if (!rc || !rc->server) return;
243 rc->read_func = func;
244 rc->read_data = data;
248 static RemoteConnection *remote_client_open(const gchar *path)
250 RemoteConnection *rc;
252 struct sockaddr_un addr;
256 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
258 fd = socket(PF_UNIX, SOCK_STREAM, 0);
259 if (fd == -1) return NULL;
261 addr.sun_family = AF_UNIX;
262 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
263 strncpy(addr.sun_path, path, sun_path_len);
264 if (connect(fd, &addr, sizeof(addr)) == -1)
266 DEBUG_1("error connecting to socket: %s", strerror(errno));
271 rc = g_new0(RemoteConnection, 1);
274 rc->path = g_strdup(path);
279 static sig_atomic_t sigpipe_occured = FALSE;
281 static void sighandler_sigpipe(gint sig)
283 sigpipe_occured = TRUE;
286 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
288 struct sigaction new_action, old_action;
289 gboolean ret = FALSE;
290 GError *error = NULL;
293 if (!rc || rc->server) return FALSE;
294 if (!text) return TRUE;
296 sigpipe_occured = FALSE;
298 new_action.sa_handler = sighandler_sigpipe;
299 sigemptyset(&new_action.sa_mask);
300 new_action.sa_flags = 0;
302 /* setup our signal handler */
303 sigaction(SIGPIPE, &new_action, &old_action);
305 channel = g_io_channel_unix_new(rc->fd);
307 g_io_channel_write_chars(channel, text, -1, NULL, &error);
308 g_io_channel_write_chars(channel, "\n", -1, NULL, &error);
309 g_io_channel_flush(channel, &error);
313 log_printf("error reading socket: %s\n", error->message);
324 gchar *buffer = NULL;
326 while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
330 if (buffer[0] == '\n') /* empty line finishes the command */
336 buffer[termpos] = '\0';
337 printf("%s\n", buffer);
345 log_printf("error reading socket: %s\n", error->message);
352 /* restore the original signal handler */
353 sigaction(SIGPIPE, &old_action, NULL);
354 g_io_channel_unref(channel);
358 void remote_close(RemoteConnection *rc)
364 remote_server_clients_close(rc);
366 g_source_remove(rc->channel_id);
371 g_free(rc->read_data);
380 *-----------------------------------------------------------------------------
382 *-----------------------------------------------------------------------------
385 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data)
387 layout_image_next(NULL);
390 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data)
392 layout_image_prev(NULL);
395 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data)
397 layout_image_first(NULL);
400 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data)
402 layout_image_last(NULL);
405 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data)
407 layout_image_full_screen_toggle(NULL);
410 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data)
412 layout_image_full_screen_start(NULL);
415 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data)
417 layout_image_full_screen_stop(NULL);
420 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *channel, gpointer data)
423 FileData *dir_fd = file_data_new_dir(text);
424 list = filelist_recursive(dir_fd);
425 file_data_unref(dir_fd);
427 //printf("length: %d\n", g_list_length(list));
428 layout_image_slideshow_stop(NULL);
429 layout_image_slideshow_start_from_list(NULL, list);
432 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data)
434 layout_image_slideshow_toggle(NULL);
437 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data)
439 layout_image_slideshow_start(NULL);
442 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data)
444 layout_image_slideshow_stop(NULL);
447 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data)
451 n = g_ascii_strtod(text, NULL);
452 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
454 printf_term("Remote slideshow delay out of range (%.1f to %.1f)\n",
455 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
458 options->slideshow.delay = (gint)(n * 10.0 + 0.01);
461 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data)
466 if (layout_tools_float_get(NULL, &popped, &hidden) && hidden)
468 layout_tools_float_set(NULL, popped, FALSE);
472 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data)
477 if (layout_tools_float_get(NULL, &popped, &hidden) && !hidden)
479 layout_tools_float_set(NULL, popped, TRUE);
483 static gboolean gr_quit_idle_cb(gpointer data)
490 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data)
492 /* schedule exit when idle, if done from within a
493 * remote handler remote_close will crash
495 g_idle_add(gr_quit_idle_cb, NULL);
498 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
500 gchar *filename = expand_tilde(text);
502 if (isfile(filename))
504 if (file_extension_match(filename, GQ_COLLECTION_EXT))
506 collection_window_new(filename);
510 layout_set_path(NULL, filename);
513 else if (isdir(filename))
515 layout_set_path(NULL, filename);
519 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
520 layout_set_path(NULL, homedir());
526 static void gr_file_tell(const gchar *text, GIOChannel *channel, gpointer data)
528 LayoutWindow *lw = NULL; /* NULL to force layout_valid() to do some magic */
529 if (!layout_valid(&lw)) return;
530 if (image_get_path(lw->image))
531 printf_term("%s %s\n", GQ_APPNAME, image_get_path(lw->image));
534 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
536 gchar *filename = expand_tilde(text);
538 if (isfile(filename))
540 load_config_from_file(filename, FALSE);
544 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
545 layout_set_path(NULL, homedir());
551 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer data)
553 gchar *filename = expand_tilde(text);
554 FileData *fd = file_data_new_group(filename);
557 if (fd->parent) fd = fd->parent;
559 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
560 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
562 work = fd->sidecar_files;
568 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
569 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
574 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer data)
576 gchar *filename = expand_tilde(text);
577 FileData *fd = file_data_new_group(filename);
579 if (fd->change && fd->change->dest)
581 g_io_channel_write_chars(channel, fd->change->dest, -1, NULL, NULL);
582 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
587 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
589 gchar *filename = expand_tilde(text);
591 view_window_new(file_data_new_group(filename));
595 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
597 RemoteData *remote_data = data;
599 if (remote_data->command_collection)
601 collection_unref(remote_data->command_collection);
602 remote_data->command_collection = NULL;
606 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
608 RemoteData *remote_data = data;
611 if (!remote_data->command_collection)
615 cd = collection_new("");
620 cd->name = g_strdup(_("Command line"));
622 remote_data->command_collection = cd;
626 new = (!collection_get_first(remote_data->command_collection));
629 if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && new)
631 layout_image_set_collection(NULL, remote_data->command_collection,
632 collection_get_first(remote_data->command_collection));
636 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
638 LayoutWindow *lw = NULL;
640 if (layout_valid(&lw))
642 gtk_window_present(GTK_WINDOW(lw->window));
646 typedef struct _RemoteCommandEntry RemoteCommandEntry;
647 struct _RemoteCommandEntry {
650 void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
651 gboolean needs_extra;
652 gboolean prefer_command_line;
657 static RemoteCommandEntry remote_commands[] = {
658 /* short, long callback, extra, prefer, parameter, description */
659 { "-n", "--next", gr_image_next, FALSE, FALSE, NULL, N_("next image") },
660 { "-b", "--back", gr_image_prev, FALSE, FALSE, NULL, N_("previous image") },
661 { NULL, "--first", gr_image_first, FALSE, FALSE, NULL, N_("first image") },
662 { NULL, "--last", gr_image_last, FALSE, FALSE, NULL, N_("last image") },
663 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, NULL, N_("toggle full screen") },
664 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, NULL, N_("start full screen") },
665 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, NULL, N_("stop full screen") },
666 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, NULL, N_("toggle slide show") },
667 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, NULL, N_("start slide show") },
668 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, NULL, N_("stop slide show") },
669 { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE, FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
670 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("<[N][.M]>"), N_("set slide show delay to N.M seconds") },
671 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, NULL, N_("show tools") },
672 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, NULL, N_("hide tools") },
673 { "-q", "--quit", gr_quit, FALSE, FALSE, NULL, N_("quit") },
674 { NULL, "--config-load:", gr_config_load, TRUE, FALSE, N_("<FILE>"), N_("load configuration from FILE") },
675 { NULL, "--get-sidecars:", gr_get_sidecars, TRUE, FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
676 { NULL, "--get-destination:", gr_get_destination, TRUE, FALSE, N_("<FILE>"), N_("get destination path of FILE") },
677 { NULL, "file:", gr_file_load, TRUE, FALSE, N_("<FILE>"), N_("open FILE") },
678 { NULL, "--tell", gr_file_tell, FALSE, FALSE, NULL, N_("print filename of current image") },
679 { NULL, "view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
680 { NULL, "--list-clear", gr_list_clear, FALSE, FALSE, NULL, N_("clear command line collection list") },
681 { NULL, "--list-add:", gr_list_add, TRUE, FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
682 { NULL, "raise", gr_raise, FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
683 { NULL, NULL, NULL, FALSE, FALSE, NULL }
686 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
688 gboolean match = FALSE;
692 while (!match && remote_commands[i].func != NULL)
694 if (remote_commands[i].needs_extra)
696 if (remote_commands[i].opt_s &&
697 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
699 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
700 return &remote_commands[i];
702 else if (remote_commands[i].opt_l &&
703 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
705 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
706 return &remote_commands[i];
711 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
712 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
714 if (offset) *offset = text;
715 return &remote_commands[i];
725 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
727 RemoteCommandEntry *entry;
730 entry = remote_command_find(text, &offset);
731 if (entry && entry->func)
733 entry->func(offset, channel, data);
737 log_printf("unknown remote command:%s\n", text);
741 void remote_help(void)
747 print_term(_("Remote command list:\n"));
750 while (remote_commands[i].func != NULL)
752 if (remote_commands[i].description)
754 s_opt_param = g_strconcat(remote_commands[i].opt_s, remote_commands[i].parameter, NULL);
755 l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
756 printf_term(" %-11s%-1s %-30s%-s\n",
757 (remote_commands[i].opt_s) ? s_opt_param : "",
758 (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
759 (remote_commands[i].opt_l) ? l_opt_param : "",
760 _(remote_commands[i].description));
766 printf_term(N_("\n All other command line parameters are used as plain files if they exists.\n"));
769 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
776 RemoteCommandEntry *entry;
778 entry = remote_command_find(argv[i], NULL);
781 list = g_list_append(list, argv[i]);
783 else if (errors && !isfile(argv[i]))
785 *errors = g_list_append(*errors, argv[i]);
794 * \param arg_exec Binary (argv0)
795 * \param remote_list Evaluated and recognized remote commands
796 * \param path The current path
797 * \param cmd_list List of all non collections in Path
798 * \param collection_list List of all collections in argv
800 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
801 GList *cmd_list, GList *collection_list)
803 RemoteConnection *rc;
804 gboolean started = FALSE;
807 buf = g_build_filename(get_rc_dir(), ".command", NULL);
808 rc = remote_client_open(buf);
813 gint retry_count = 12;
814 gboolean blank = FALSE;
816 printf_term(_("Remote %s not running, starting..."), GQ_APPNAME);
818 command = g_string_new(arg_exec);
824 RemoteCommandEntry *entry;
829 entry = remote_command_find(text, NULL);
832 if (entry->prefer_command_line)
834 remote_list = g_list_remove(remote_list, text);
835 g_string_append(command, " ");
836 g_string_append(command, text);
838 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
845 if (blank || cmd_list || path) g_string_append(command, " --blank");
846 if (get_debug_level()) g_string_append(command, " --debug");
848 g_string_append(command, " &");
849 runcmd(command->str);
850 g_string_free(command, TRUE);
852 while (!rc && retry_count > 0)
854 usleep((retry_count > 10) ? 500000 : 1000000);
855 rc = remote_client_open(buf);
856 if (!rc) print_term(".");
870 gboolean use_path = TRUE;
871 gboolean sent = FALSE;
877 RemoteCommandEntry *entry;
882 entry = remote_command_find(text, NULL);
885 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
887 remote_client_send(rc, text);
892 if (cmd_list && cmd_list->next)
894 prefix = "--list-add:";
895 remote_client_send(rc, "--list-clear");
911 text = g_strconcat(prefix, fd->path, NULL);
912 remote_client_send(rc, text);
918 if (path && !cmd_list && use_path)
922 text = g_strdup_printf("file:%s", path);
923 remote_client_send(rc, text);
929 work = collection_list;
938 text = g_strdup_printf("file:%s", name);
939 remote_client_send(rc, text);
945 if (!started && !sent)
947 remote_client_send(rc, "raise");
952 print_term(_("Remote not available\n"));
958 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
960 RemoteConnection *remote_connection = remote_server_open(path);
961 RemoteData *remote_data = g_new(RemoteData, 1);
963 remote_data->command_collection = command_collection;
965 remote_server_subscribe(remote_connection, remote_cb, remote_data);
966 return remote_connection;
968 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */