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.
25 #include "cache-maint.h"
27 #include "collect-io.h"
30 #include "filefilter.h"
33 #include "layout-image.h"
34 #include "layout-util.h"
36 #include "pixbuf-renderer.h"
37 #include "slideshow.h"
38 #include "ui-fileops.h"
41 #include "view-file.h"
44 #include <sys/socket.h>
49 #define SERVER_MAX_CLIENTS 8
51 #define REMOTE_SERVER_BACKLOG 4
55 #define UNIX_PATH_MAX 108
59 static RemoteConnection *remote_client_open(const gchar *path);
60 static gint remote_client_send(RemoteConnection *rc, const gchar *text);
61 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data);
63 static LayoutWindow *lw_id = nullptr; /* points to the window set by the --id option */
67 guint channel_id; /* event source id */
72 CollectionData *command_collection;
77 /* To enable file names containing newlines to be processed correctly,
78 * the --print0 remote option sets returned data to be terminated with a null
79 * character rather a newline
81 static gboolean print0 = FALSE;
83 /* Remote commands from main.cc are prepended with the current dir the remote
84 * command was made from. Some remote commands require this. The
85 * value is stored here
87 static gchar *pwd = nullptr;
90 * @brief Ensures file path is absolute.
91 * @param[in] filename Filepath, absolute or relative to calling directory
92 * @returns absolute path
94 * If first character of input filepath is not the directory
95 * separator, assume it as a relative path and prepend
96 * the directory the remote command was initiated from
98 * Return value must be freed with g_free()
100 static gchar *set_pwd(gchar *filename)
104 if (strncmp(filename, G_DIR_SEPARATOR_S, 1) != 0)
106 temp = g_build_filename(pwd, filename, NULL);
110 temp = g_strdup(filename);
116 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
118 auto client = static_cast<RemoteClient *>(data);
119 RemoteConnection *rc;
120 GIOStatus status = G_IO_STATUS_NORMAL;
125 if (condition & G_IO_IN)
127 gchar *buffer = nullptr;
128 GError *error = nullptr;
130 /** @FIXME it should be possible to terminate the command with a null character */
131 g_io_channel_set_line_term(source, "<gq_end_of_command>", -1);
132 while ((status = g_io_channel_read_line(source, &buffer, nullptr, &termpos, &error)) == G_IO_STATUS_NORMAL)
136 buffer[termpos] = '\0';
138 if (strlen(buffer) > 0)
140 if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
141 g_io_channel_write_chars(source, "<gq_end_of_command>", -1, nullptr, nullptr); /* empty line finishes the command */
142 g_io_channel_flush(source, nullptr);
152 log_printf("error reading socket: %s\n", error->message);
157 if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
159 rc->clients = g_list_remove(rc->clients, client);
161 DEBUG_1("HUP detected, closing client.");
162 DEBUG_1("client count %d", g_list_length(rc->clients));
164 g_source_remove(client->channel_id);
172 static void remote_server_client_add(RemoteConnection *rc, gint fd)
174 RemoteClient *client;
177 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
179 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
184 client = g_new0(RemoteClient, 1);
188 channel = g_io_channel_unix_new(fd);
189 client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, static_cast<GIOCondition>(G_IO_IN | G_IO_HUP),
190 remote_server_client_cb, client, nullptr);
191 g_io_channel_unref(channel);
193 rc->clients = g_list_append(rc->clients, client);
194 DEBUG_1("client count %d", g_list_length(rc->clients));
197 static void remote_server_clients_close(RemoteConnection *rc)
201 auto client = static_cast<RemoteClient *>(rc->clients->data);
203 rc->clients = g_list_remove(rc->clients, client);
205 g_source_remove(client->channel_id);
211 static gboolean remote_server_read_cb(GIOChannel *, GIOCondition, gpointer data)
213 auto rc = static_cast<RemoteConnection *>(data);
217 fd = accept(rc->fd, nullptr, &alen);
220 log_printf("error accepting socket: %s\n", strerror(errno));
224 remote_server_client_add(rc, fd);
229 gboolean remote_server_exists(const gchar *path)
231 RemoteConnection *rc;
233 /* verify server up */
234 rc = remote_client_open(path);
239 /* unable to connect, remove socket file to free up address */
244 static RemoteConnection *remote_server_open(const gchar *path)
246 RemoteConnection *rc;
247 struct sockaddr_un addr;
252 if (remote_server_exists(path))
254 log_printf("Address already in use: %s\n", path);
258 fd = socket(PF_UNIX, SOCK_STREAM, 0);
259 if (fd == -1) return nullptr;
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 (bind(fd, reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)) == -1 ||
265 listen(fd, REMOTE_SERVER_BACKLOG) == -1)
267 log_printf("error subscribing to socket: %s\n", strerror(errno));
272 rc = g_new0(RemoteConnection, 1);
276 rc->path = g_strdup(path);
278 channel = g_io_channel_unix_new(rc->fd);
279 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, nullptr);
281 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
282 remote_server_read_cb, rc, nullptr);
283 g_io_channel_unref(channel);
288 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
290 if (!rc || !rc->server) return;
292 rc->read_func = func;
293 rc->read_data = data;
297 static RemoteConnection *remote_client_open(const gchar *path)
299 RemoteConnection *rc;
301 struct sockaddr_un addr;
305 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return nullptr;
307 fd = socket(PF_UNIX, SOCK_STREAM, 0);
308 if (fd == -1) return nullptr;
310 addr.sun_family = AF_UNIX;
311 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
312 strncpy(addr.sun_path, path, sun_path_len);
313 if (connect(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) == -1)
315 DEBUG_1("error connecting to socket: %s", strerror(errno));
320 rc = g_new0(RemoteConnection, 1);
323 rc->path = g_strdup(path);
328 static sig_atomic_t sigpipe_occurred = FALSE;
330 static void sighandler_sigpipe(gint)
332 sigpipe_occurred = TRUE;
335 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
337 struct sigaction new_action, old_action;
338 gboolean ret = FALSE;
339 GError *error = nullptr;
342 if (!rc || rc->server) return FALSE;
343 if (!text) return TRUE;
345 sigpipe_occurred = FALSE;
347 new_action.sa_handler = sighandler_sigpipe;
348 sigemptyset(&new_action.sa_mask);
349 new_action.sa_flags = 0;
351 /* setup our signal handler */
352 sigaction(SIGPIPE, &new_action, &old_action);
354 channel = g_io_channel_unix_new(rc->fd);
356 g_io_channel_write_chars(channel, text, -1, nullptr, &error);
357 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, &error);
358 g_io_channel_flush(channel, &error);
362 log_printf("error reading socket: %s\n", error->message);
373 gchar *buffer = nullptr;
375 g_io_channel_set_line_term(channel, "<gq_end_of_command>", -1);
376 while (g_io_channel_read_line(channel, &buffer, nullptr, &termpos, &error) == G_IO_STATUS_NORMAL)
380 if (g_strstr_len(buffer, -1, "<gq_end_of_command>") == buffer) /* empty line finishes the command */
386 buffer[termpos] = '\0';
387 if (g_strstr_len(buffer, -1, "print0") != nullptr)
395 printf("%s%c", buffer, 0);
399 printf("%s\n", buffer);
409 log_printf("error reading socket: %s\n", error->message);
416 /* restore the original signal handler */
417 sigaction(SIGPIPE, &old_action, nullptr);
418 g_io_channel_unref(channel);
422 void remote_close(RemoteConnection *rc)
428 remote_server_clients_close(rc);
430 g_source_remove(rc->channel_id);
435 g_free(rc->read_data);
444 *-----------------------------------------------------------------------------
446 *-----------------------------------------------------------------------------
449 static void gr_image_next(const gchar *, GIOChannel *, gpointer)
451 layout_image_next(lw_id);
454 static void gr_new_window(const gchar *, GIOChannel *, gpointer)
456 LayoutWindow *lw = nullptr;
458 if (!layout_valid(&lw)) return;
460 lw_id = layout_new_from_default();
462 layout_set_path(lw_id, pwd);
465 static gboolean gr_close_window_cb(gpointer)
467 if (!layout_valid(&lw_id)) return FALSE;
469 layout_menu_close_cb(nullptr, lw_id);
471 return G_SOURCE_REMOVE;
474 static void gr_close_window(const gchar *, GIOChannel *, gpointer)
476 g_idle_add((gr_close_window_cb), nullptr);
479 static void gr_image_prev(const gchar *, GIOChannel *, gpointer)
481 layout_image_prev(lw_id);
484 static void gr_image_first(const gchar *, GIOChannel *, gpointer)
486 layout_image_first(lw_id);
489 static void gr_image_last(const gchar *, GIOChannel *, gpointer)
491 layout_image_last(lw_id);
494 static void gr_fullscreen_toggle(const gchar *, GIOChannel *, gpointer)
496 layout_image_full_screen_toggle(lw_id);
499 static void gr_fullscreen_start(const gchar *, GIOChannel *, gpointer)
501 layout_image_full_screen_start(lw_id);
504 static void gr_fullscreen_stop(const gchar *, GIOChannel *, gpointer)
506 layout_image_full_screen_stop(lw_id);
509 static void gr_lw_id(const gchar *text, GIOChannel *, gpointer)
511 lw_id = layout_find_by_layout_id(text);
514 log_printf("remote sent window ID that does not exist:\"%s\"\n",text);
516 layout_valid(&lw_id);
519 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *, gpointer)
522 gchar *tilde_filename;
524 tilde_filename = expand_tilde(text);
526 FileData *dir_fd = file_data_new_dir(tilde_filename);
527 g_free(tilde_filename);
529 layout_valid(&lw_id);
530 list = filelist_recursive_full(dir_fd, lw_id->options.file_view_list_sort.method, lw_id->options.file_view_list_sort.ascend, lw_id->options.file_view_list_sort.case_sensitive);
531 file_data_unref(dir_fd);
534 layout_image_slideshow_stop(lw_id);
535 layout_image_slideshow_start_from_list(lw_id, list);
538 static void gr_cache_thumb(const gchar *text, GIOChannel *, gpointer)
540 if (!g_strcmp0(text, "clear"))
542 cache_maintain_home_remote(FALSE, TRUE, nullptr);
544 else if (!g_strcmp0(text, "clean"))
546 cache_maintain_home_remote(FALSE, FALSE, nullptr);
550 static void gr_cache_shared(const gchar *text, GIOChannel *, gpointer)
552 if (!g_strcmp0(text, "clear"))
553 cache_manager_standard_process_remote(TRUE);
554 else if (!g_strcmp0(text, "clean"))
555 cache_manager_standard_process_remote(FALSE);
558 static void gr_cache_metadata(const gchar *, GIOChannel *, gpointer)
560 cache_maintain_home_remote(TRUE, FALSE, nullptr);
563 static void gr_cache_render(const gchar *text, GIOChannel *, gpointer)
565 cache_manager_render_remote(text, FALSE, FALSE, nullptr);
568 static void gr_cache_render_recurse(const gchar *text, GIOChannel *, gpointer)
570 cache_manager_render_remote(text, TRUE, FALSE, nullptr);
573 static void gr_cache_render_standard(const gchar *text, GIOChannel *, gpointer)
575 if(options->thumbnails.spec_standard)
577 cache_manager_render_remote(text, FALSE, TRUE, nullptr);
581 static void gr_cache_render_standard_recurse(const gchar *text, GIOChannel *, gpointer)
583 if(options->thumbnails.spec_standard)
585 cache_manager_render_remote(text, TRUE, TRUE, nullptr);
589 static void gr_slideshow_toggle(const gchar *, GIOChannel *, gpointer)
591 layout_image_slideshow_toggle(lw_id);
594 static void gr_slideshow_start(const gchar *, GIOChannel *, gpointer)
596 layout_image_slideshow_start(lw_id);
599 static void gr_slideshow_stop(const gchar *, GIOChannel *, gpointer)
601 layout_image_slideshow_stop(lw_id);
604 static void gr_slideshow_delay(const gchar *text, GIOChannel *, gpointer)
606 gdouble t1, t2, t3, n;
609 res = sscanf(text, "%lf:%lf:%lf", &t1, &t2, &t3);
612 n = (t1 * 3600) + (t2 * 60) + t3;
613 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
614 t1 >= 24 || t2 >= 60 || t3 >= 60)
616 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
617 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
624 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
625 t1 >= 60 || t2 >= 60)
627 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
628 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
635 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
637 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
638 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
647 options->slideshow.delay = static_cast<gint>(n * 10.0 + 0.01);
650 static void gr_tools_show(const gchar *, GIOChannel *, gpointer)
655 if (layout_tools_float_get(lw_id, &popped, &hidden) && hidden)
657 layout_tools_float_set(lw_id, popped, FALSE);
661 static void gr_tools_hide(const gchar *, GIOChannel *, gpointer)
666 if (layout_tools_float_get(lw_id, &popped, &hidden) && !hidden)
668 layout_tools_float_set(lw_id, popped, TRUE);
672 static gboolean gr_quit_idle_cb(gpointer)
676 return G_SOURCE_REMOVE;
679 static void gr_quit(const gchar *, GIOChannel *, gpointer)
681 /* schedule exit when idle, if done from within a
682 * remote handler remote_close will crash
684 g_idle_add(gr_quit_idle_cb, nullptr);
687 static void gr_file_load_no_raise(const gchar *text, GIOChannel *, gpointer)
690 gchar *tilde_filename;
692 if (!download_web_file(text, TRUE, nullptr))
694 tilde_filename = expand_tilde(text);
695 filename = set_pwd(tilde_filename);
697 if (isfile(filename))
699 if (file_extension_match(filename, GQ_COLLECTION_EXT))
701 collection_window_new(filename);
705 layout_set_path(lw_id, filename);
708 else if (isdir(filename))
710 layout_set_path(lw_id, filename);
714 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
715 layout_set_path(lw_id, homedir());
719 g_free(tilde_filename);
723 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
725 gr_file_load_no_raise(text, channel, data);
727 gr_raise(text, channel, data);
730 static void gr_pixel_info(const gchar *, GIOChannel *channel, gpointer)
733 gint x_pixel, y_pixel;
735 gint r_mouse, g_mouse, b_mouse;
738 if (!layout_valid(&lw_id)) return;
740 pr = reinterpret_cast<PixbufRenderer*>(lw_id->image->pr);
744 pixbuf_renderer_get_image_size(pr, &width, &height);
745 if (width < 1 || height < 1) return;
747 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
749 if (x_pixel >= 0 && y_pixel >= 0)
751 pixbuf_renderer_get_pixel_colors(pr, x_pixel, y_pixel,
752 &r_mouse, &g_mouse, &b_mouse);
754 pixel_info = g_strdup_printf(_("[%d,%d]: RGB(%3d,%3d,%3d)"),
756 r_mouse, g_mouse, b_mouse);
758 g_io_channel_write_chars(channel, pixel_info, -1, nullptr, nullptr);
759 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
774 static void gr_rectangle(const gchar *, GIOChannel *channel, gpointer)
776 gchar *rectangle_info;
780 if (!options->draw_rectangle) return;
781 if (!layout_valid(&lw_id)) return;
783 pr = reinterpret_cast<PixbufRenderer*>(lw_id->image->pr);
787 image_get_rectangle(&x1, &y1, &x2, &y2);
788 rectangle_info = g_strdup_printf(_("%dx%d+%d+%d"),
789 (x2 > x1) ? x2 - x1 : x1 - x2,
790 (y2 > y1) ? y2 - y1 : y1 - y2,
792 (y2 > y1) ? y1 : y2);
794 g_io_channel_write_chars(channel, rectangle_info, -1, nullptr, nullptr);
795 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
797 g_free(rectangle_info);
801 static void gr_render_intent(const gchar *, GIOChannel *channel, gpointer)
803 gchar *render_intent;
805 switch (options->color_profile.render_intent)
808 render_intent = g_strdup("Perceptual");
811 render_intent = g_strdup("Relative Colorimetric");
814 render_intent = g_strdup("Saturation");
817 render_intent = g_strdup("Absolute Colorimetric");
820 render_intent = g_strdup("none");
824 g_io_channel_write_chars(channel, render_intent, -1, nullptr, nullptr);
825 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
827 g_free(render_intent);
830 static void get_filelist(const gchar *text, GIOChannel *channel, gboolean recurse)
832 GList *list = nullptr;
833 FileFormatClass format_class;
837 gchar *tilde_filename;
839 if (strcmp(text, "") == 0)
841 if (layout_valid(&lw_id))
843 dir_fd = file_data_new_dir(lw_id->dir_fd->path);
852 tilde_filename = expand_tilde(text);
853 if (isdir(tilde_filename))
855 dir_fd = file_data_new_dir(tilde_filename);
859 g_free(tilde_filename);
862 g_free(tilde_filename);
867 list = filelist_recursive(dir_fd);
871 filelist_read(dir_fd, &list, nullptr);
874 GString *out_string = g_string_new(nullptr);
878 fd = static_cast<FileData *>(work->data);
879 g_string_append_printf(out_string, "%s", fd->path);
880 format_class = filter_file_get_class(fd->path);
882 switch (format_class)
884 case FORMAT_CLASS_IMAGE:
885 out_string = g_string_append(out_string, " Class: Image");
887 case FORMAT_CLASS_RAWIMAGE:
888 out_string = g_string_append(out_string, " Class: RAW image");
890 case FORMAT_CLASS_META:
891 out_string = g_string_append(out_string, " Class: Metadata");
893 case FORMAT_CLASS_VIDEO:
894 out_string = g_string_append(out_string, " Class: Video");
896 case FORMAT_CLASS_COLLECTION:
897 out_string = g_string_append(out_string, " Class: Collection");
899 case FORMAT_CLASS_DOCUMENT:
900 out_string = g_string_append(out_string, " Class: Document");
902 case FORMAT_CLASS_ARCHIVE:
903 out_string = g_string_append(out_string, " Class: Archive");
905 case FORMAT_CLASS_UNKNOWN:
906 out_string = g_string_append(out_string, " Class: Unknown");
909 out_string = g_string_append(out_string, " Class: Unknown");
912 out_string = g_string_append(out_string, "\n");
916 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
917 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
919 g_string_free(out_string, TRUE);
921 file_data_unref(dir_fd);
924 static void gr_get_selection(const gchar *, GIOChannel *channel, gpointer)
926 if (!layout_valid(&lw_id)) return;
928 GList *selected = layout_selection_list(lw_id); // Keep copy to free.
929 GString *out_string = g_string_new(nullptr);
931 GList *work = selected;
934 auto fd = static_cast<FileData *>(work->data);
935 g_assert(fd->magick == FD_MAGICK);
937 g_string_append_printf(out_string, "%s %s\n",
939 format_class_list[filter_file_get_class(fd->path)]);
944 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
945 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
947 filelist_free(selected);
948 g_string_free(out_string, TRUE);
951 static void gr_selection_add(const gchar *text, GIOChannel *, gpointer)
953 if (!layout_valid(&lw_id)) return;
955 FileData *fd_to_select = nullptr;
956 if (strcmp(text, "") == 0)
958 // No file specified, use current fd.
959 fd_to_select = layout_image_get_fd(lw_id);
963 // Search through the current file list for a file matching the specified path.
964 // "Match" is either a basename match or a file path match.
965 gchar *path = expand_tilde(text);
966 gchar *filename = g_path_get_basename(path);
967 gchar *slash_plus_filename = g_strdup_printf("%s%s", G_DIR_SEPARATOR_S, filename);
969 GList *file_list = layout_list(lw_id);
970 for (GList *work = file_list; work && !fd_to_select; work = work->next)
972 auto fd = static_cast<FileData *>(work->data);
973 if (!strcmp(path, fd->path) || g_str_has_suffix(fd->path, slash_plus_filename))
975 fd_to_select = file_data_ref(fd);
976 continue; // will exit loop.
979 for (GList *sidecar = fd->sidecar_files; sidecar && !fd_to_select; sidecar = sidecar->next)
981 auto side_fd = static_cast<FileData *>(sidecar->data);
982 if (!strcmp(path, side_fd->path)
983 || g_str_has_suffix(side_fd->path, slash_plus_filename))
985 fd_to_select = file_data_ref(side_fd);
986 continue; // will exit both nested loops.
993 log_printf("remote sent --selection-add filename that could not be found: \"%s\"\n",
997 filelist_free(file_list);
998 g_free(slash_plus_filename);
1005 GList *to_select = g_list_append(nullptr, fd_to_select);
1006 // Using the "_list" variant doesn't clear the existing selection.
1007 layout_select_list(lw_id, to_select);
1008 filelist_free(to_select);
1012 static void gr_selection_clear(const gchar *, GIOChannel *, gpointer)
1014 layout_select_none(lw_id); // Checks lw_id validity internally.
1017 static void gr_selection_remove(const gchar *text, GIOChannel *, gpointer)
1019 if (!layout_valid(&lw_id)) return;
1021 GList *selected = layout_selection_list(lw_id); // Keep copy to free.
1024 log_printf("remote sent --selection-remove with empty selection.");
1028 FileData *fd_to_deselect = nullptr;
1029 gchar *path = nullptr;
1030 gchar *filename = nullptr;
1031 gchar *slash_plus_filename = nullptr;
1032 if (strcmp(text, "") == 0)
1034 // No file specified, use current fd.
1035 fd_to_deselect = layout_image_get_fd(lw_id);
1036 if (!fd_to_deselect)
1038 log_printf("remote sent \"--selection-remove:\" with no current image");
1039 filelist_free(selected);
1045 // Search through the selection list for a file matching the specified path.
1046 // "Match" is either a basename match or a file path match.
1047 path = expand_tilde(text);
1048 filename = g_path_get_basename(path);
1049 slash_plus_filename = g_strdup_printf("%s%s", G_DIR_SEPARATOR_S, filename);
1052 GList *prior_link = nullptr; // Stash base for link removal to avoid a second traversal.
1053 GList *link_to_remove = nullptr;
1054 for (GList *work = selected; work; prior_link = work, work = work->next)
1056 auto fd = static_cast<FileData *>(work->data);
1059 if (fd == fd_to_deselect)
1061 link_to_remove = work;
1067 // path, filename, and slash_plus_filename should be defined.
1069 if (!strcmp(path, fd->path) || g_str_has_suffix(fd->path, slash_plus_filename))
1071 link_to_remove = work;
1077 if (!link_to_remove)
1081 log_printf("remote sent \"--selection-remove:\" but current image is not selected");
1085 log_printf("remote sent \"--selection-remove:%s\" but that filename is not selected",
1091 if (link_to_remove == selected)
1093 // Remove first link.
1094 selected = g_list_remove_link(selected, link_to_remove);
1095 filelist_free(link_to_remove);
1096 link_to_remove = nullptr;
1100 // Remove a subsequent link.
1101 prior_link = g_list_remove_link(prior_link, link_to_remove);
1102 filelist_free(link_to_remove);
1103 link_to_remove = nullptr;
1106 // Re-select all but the deselected item.
1107 layout_select_none(lw_id);
1108 layout_select_list(lw_id, selected);
1111 filelist_free(selected);
1112 file_data_unref(fd_to_deselect);
1113 g_free(slash_plus_filename);
1118 static void gr_collection(const gchar *text, GIOChannel *channel, gpointer)
1120 GString *contents = g_string_new(nullptr);
1122 if (is_collection(text))
1124 collection_contents(text, &contents);
1131 g_io_channel_write_chars(channel, contents->str, -1, nullptr, nullptr);
1132 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1134 g_string_free(contents, TRUE);
1137 static void gr_collection_list(const gchar *, GIOChannel *channel, gpointer)
1140 GList *collection_list = nullptr;
1142 GString *out_string = g_string_new(nullptr);
1144 collect_manager_list(&collection_list, nullptr, nullptr);
1146 work = collection_list;
1149 auto collection_name = static_cast<const gchar *>(work->data);
1150 out_string = g_string_append(out_string, g_strdup(collection_name));
1151 out_string = g_string_append(out_string, "\n");
1156 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
1157 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1159 g_list_free_full(collection_list, g_free);
1160 g_string_free(out_string, TRUE);
1163 static gboolean wait_cb(gpointer data)
1165 gint position = GPOINTER_TO_INT(data);
1166 gint x = position >> 16;
1167 gint y = position - (x << 16);
1169 gtk_window_move(GTK_WINDOW(lw_id->window), x, y);
1171 return G_SOURCE_REMOVE;
1174 static void gr_geometry(const gchar *text, GIOChannel *, gpointer)
1178 if (!layout_valid(&lw_id) || !text)
1185 geometry = g_strsplit_set(text, "+", 3);
1186 if (geometry[1] != nullptr && geometry[2] != nullptr )
1188 gtk_window_move(GTK_WINDOW(lw_id->window), atoi(geometry[1]), atoi(geometry[2]));
1193 geometry = g_strsplit_set(text, "+x", 4);
1194 if (geometry[0] != nullptr && geometry[1] != nullptr)
1196 gtk_window_resize(GTK_WINDOW(lw_id->window), atoi(geometry[0]), atoi(geometry[1]));
1198 if (geometry[2] != nullptr && geometry[3] != nullptr)
1200 /* There is an occasional problem with a window_move immediately after a window_resize */
1201 g_idle_add(wait_cb, GINT_TO_POINTER((atoi(geometry[2]) << 16) + atoi(geometry[3])));
1204 g_strfreev(geometry);
1207 static void gr_filelist(const gchar *text, GIOChannel *channel, gpointer)
1209 get_filelist(text, channel, FALSE);
1212 static void gr_filelist_recurse(const gchar *text, GIOChannel *channel, gpointer)
1214 get_filelist(text, channel, TRUE);
1217 static void gr_file_tell(const gchar *, GIOChannel *channel, gpointer)
1220 gchar *collection_name = nullptr;
1222 if (!layout_valid(&lw_id)) return;
1224 if (image_get_path(lw_id->image))
1226 if (lw_id->image->collection && lw_id->image->collection->name)
1228 collection_name = remove_extension_from_path(lw_id->image->collection->name);
1229 out_string = g_strconcat(image_get_path(lw_id->image), " Collection: ", collection_name, NULL);
1233 out_string = g_strconcat(image_get_path(lw_id->image), NULL);
1238 out_string = g_strconcat(lw_id->dir_fd->path, G_DIR_SEPARATOR_S, NULL);
1241 g_io_channel_write_chars(channel, out_string, -1, nullptr, nullptr);
1242 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1244 g_free(collection_name);
1248 static void gr_file_info(const gchar *, GIOChannel *channel, gpointer)
1252 gchar *country_name;
1253 gchar *country_code;
1256 GString *out_string;
1257 FileFormatClass format_class;
1259 if (!layout_valid(&lw_id)) return;
1261 if (image_get_path(lw_id->image))
1263 filename = g_strdup(image_get_path(lw_id->image));
1264 fd = file_data_new_group(filename);
1265 out_string = g_string_new(nullptr);
1269 format_class = filter_file_get_class(image_get_path(lw_id->image));
1273 format_class = FORMAT_CLASS_UNKNOWN;
1276 g_string_append_printf(out_string, _("Class: %s\n"), format_class_list[format_class]);
1278 if (fd->page_total > 1)
1280 g_string_append_printf(out_string, _("Page no: %d/%d\n"), fd->page_num + 1, fd->page_total);
1285 country_name = exif_get_data_as_text(fd->exif, "formatted.countryname");
1288 g_string_append_printf(out_string, _("Country name: %s\n"), country_name);
1289 g_free(country_name);
1292 country_code = exif_get_data_as_text(fd->exif, "formatted.countrycode");
1295 g_string_append_printf(out_string, _("Country code: %s\n"), country_code);
1296 g_free(country_code);
1299 timezone = exif_get_data_as_text(fd->exif, "formatted.timezone");
1302 g_string_append_printf(out_string, _("Timezone: %s\n"), timezone);
1306 local_time = exif_get_data_as_text(fd->exif, "formatted.localtime");
1309 g_string_append_printf(out_string, ("Local time: %s\n"), local_time);
1314 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
1315 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1317 g_string_free(out_string, TRUE);
1318 file_data_unref(fd);
1323 static gchar *config_file_path(const gchar *param)
1325 gchar *path = nullptr;
1326 gchar *full_name = nullptr;
1328 if (file_extension_match(param, ".xml"))
1330 path = g_build_filename(get_window_layouts_dir(), param, NULL);
1332 else if (file_extension_match(param, nullptr))
1334 full_name = g_strconcat(param, ".xml", NULL);
1335 path = g_build_filename(get_window_layouts_dir(), full_name, NULL);
1348 static gboolean is_config_file(const gchar *param)
1350 gchar *name = nullptr;
1352 name = config_file_path(param);
1361 static void gr_config_load(const gchar *text, GIOChannel *, gpointer)
1363 gchar *filename = expand_tilde(text);
1365 if (!g_strstr_len(filename, -1, G_DIR_SEPARATOR_S))
1367 if (is_config_file(filename))
1369 gchar *tmp = config_file_path(filename);
1375 if (isfile(filename))
1377 load_config_from_file(filename, FALSE);
1381 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
1382 layout_set_path(nullptr, homedir());
1388 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer)
1390 gchar *filename = expand_tilde(text);
1391 FileData *fd = file_data_new_group(filename);
1394 if (fd->parent) fd = fd->parent;
1396 g_io_channel_write_chars(channel, fd->path, -1, nullptr, nullptr);
1397 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1399 work = fd->sidecar_files;
1403 fd = static_cast<FileData *>(work->data);
1405 g_io_channel_write_chars(channel, fd->path, -1, nullptr, nullptr);
1406 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1411 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer)
1413 gchar *filename = expand_tilde(text);
1414 FileData *fd = file_data_new_group(filename);
1416 if (fd->change && fd->change->dest)
1418 g_io_channel_write_chars(channel, fd->change->dest, -1, nullptr, nullptr);
1419 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1424 static void gr_file_view(const gchar *text, GIOChannel *, gpointer)
1427 gchar *tilde_filename = expand_tilde(text);
1429 filename = set_pwd(tilde_filename);
1431 view_window_new(file_data_new_group(filename));
1433 g_free(tilde_filename);
1436 static void gr_list_clear(const gchar *, GIOChannel *, gpointer data)
1438 auto remote_data = static_cast<RemoteData *>(data);
1440 remote_data->command_collection = nullptr;
1441 remote_data->file_list = nullptr;
1442 remote_data->single_dir = TRUE;
1445 static void gr_list_add(const gchar *text, GIOChannel *, gpointer data)
1447 auto remote_data = static_cast<RemoteData *>(data);
1448 gboolean is_new = TRUE;
1449 gchar *path = nullptr;
1453 /** @FIXME Should check if file is in current dir, has tilde or is relative */
1456 log_printf("Warning: File does not exist --remote --list-add:%s", text);
1461 /* If there is a files list on the command line
1462 * check if they are all in the same folder
1464 if (remote_data->single_dir)
1467 work = remote_data->file_list;
1468 while (work && remote_data->single_dir)
1471 dirname = g_path_get_dirname((static_cast<FileData *>(work->data))->path);
1474 path = g_strdup(dirname);
1478 if (g_strcmp0(path, dirname) != 0)
1480 remote_data->single_dir = FALSE;
1489 gchar *pathname = g_path_get_dirname(text);
1490 layout_set_path(lw_id, pathname);
1493 fd = file_data_new_simple(text);
1494 remote_data->file_list = g_list_append(remote_data->file_list, fd);
1495 file_data_unref(fd);
1497 vf_select_none(lw_id->vf);
1498 remote_data->file_list = g_list_reverse(remote_data->file_list);
1500 layout_select_list(lw_id, remote_data->file_list);
1501 layout_refresh(lw_id);
1502 first = static_cast<FileData *>(g_list_first(vf_selection_get_list(lw_id->vf))->data);
1503 layout_set_fd(lw_id, first);
1508 if (!remote_data->command_collection && !remote_data->single_dir)
1510 cw = collection_window_new(nullptr);
1513 collection_path_changed(cd);
1515 remote_data->command_collection = cd;
1517 else if (!remote_data->single_dir)
1519 is_new = (!collection_get_first(remote_data->command_collection));
1522 if (!remote_data->single_dir)
1524 layout_image_set_collection(lw_id, remote_data->command_collection, collection_get_first(remote_data->command_collection));
1525 if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && is_new)
1527 layout_image_set_collection(lw_id, remote_data->command_collection, collection_get_first(remote_data->command_collection));
1532 static void gr_action(const gchar *text, GIOChannel *, gpointer)
1536 if (!layout_valid(&lw_id))
1541 if (g_strstr_len(text, -1, ".desktop") != nullptr)
1543 file_util_start_editor_from_filelist(text, layout_selection_list(lw_id), layout_get_path(lw_id), lw_id->window);
1547 action = gtk_action_group_get_action(lw_id->action_group, text);
1550 gtk_action_activate(action);
1554 log_printf("Action %s unknown", text);
1559 static gint simple_sort(gconstpointer a, gconstpointer b)
1561 return g_strcmp0((gchar *)a, (gchar *)b);
1564 static void gr_action_list(const gchar *, GIOChannel *channel, gpointer)
1566 const gchar *accel_path;
1569 gchar *comment_list;
1572 gint max_length = 0;
1575 GList *list_final = nullptr;
1576 GList *list = nullptr;
1578 GString *out_string = g_string_new(nullptr);
1581 if (!layout_valid(&lw_id))
1586 groups = gtk_ui_manager_get_action_groups(lw_id->ui_manager);
1589 actions = gtk_action_group_list_actions(GTK_ACTION_GROUP(groups->data));
1592 action = GTK_ACTION(actions->data);
1593 accel_path = gtk_action_get_accel_path(action);
1595 if (accel_path && gtk_accel_map_lookup_entry(accel_path, nullptr))
1597 g_object_get(action, "tooltip", &tooltip, "label", &label, NULL);
1599 action_name = g_path_get_basename(accel_path);
1601 /* Used for output column padding */
1602 if (g_utf8_strlen(action_name, -1) > max_length)
1604 max_length = g_utf8_strlen(action_name, -1);
1607 /* Tooltips with newlines affect output format */
1608 if (tooltip && (g_strstr_len(tooltip, -1, "\n") == nullptr) )
1610 list = g_list_prepend(list, g_strdup(tooltip));
1614 list = g_list_prepend(list, g_strdup(label));
1617 list = g_list_prepend(list, g_strdup(action_name));
1619 g_free(action_name);
1624 actions = actions->next;
1627 groups = groups->next;
1630 /* Pad the action names to the same column for readable output */
1634 /* Menu actions are irrelevant */
1635 if (g_strstr_len(static_cast<gchar *>(work->data), -1, "Menu") == nullptr)
1637 action_list = g_strdup_printf("%-*s", max_length + 4, static_cast<gchar *>(work->data));
1641 comment_list = static_cast<gchar *>(work->data);
1642 list_final = g_list_prepend(list_final, g_strconcat(action_list, comment_list, nullptr));
1644 g_free(action_list);
1653 string_list_free(list);
1655 list_final = g_list_sort(list_final, simple_sort);
1660 out_string = g_string_append(out_string, static_cast<gchar *>(work->data) );
1661 out_string = g_string_append(out_string, "\n");
1665 string_list_free(list_final);
1667 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
1668 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1670 g_string_free(out_string, TRUE);
1673 static void gr_raise(const gchar *, GIOChannel *, gpointer)
1675 if (layout_valid(&lw_id))
1677 gtk_window_present(GTK_WINDOW(lw_id->window));
1681 static void gr_pwd(const gchar *text, GIOChannel *, gpointer)
1683 LayoutWindow *lw = nullptr;
1688 pwd = g_strdup(text);
1692 static void gr_print0(const gchar *, GIOChannel *channel, gpointer)
1694 g_io_channel_write_chars(channel, "print0", -1, nullptr, nullptr);
1695 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1699 static void gr_lua(const gchar *text, GIOChannel *channel, gpointer)
1701 gchar *result = nullptr;
1702 gchar **lua_command;
1704 lua_command = g_strsplit(text, ",", 2);
1706 if (lua_command[0] && lua_command[1])
1708 FileData *fd = file_data_new_group(lua_command[0]);
1709 result = g_strdup(lua_callvalue(fd, lua_command[1], nullptr));
1712 g_io_channel_write_chars(channel, result, -1, nullptr, nullptr);
1716 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, nullptr, nullptr);
1721 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, nullptr, nullptr);
1724 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1726 g_strfreev(lua_command);
1731 struct RemoteCommandEntry {
1734 void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
1735 gboolean needs_extra;
1736 gboolean prefer_command_line;
1737 const gchar *parameter;
1738 const gchar *description;
1741 static RemoteCommandEntry remote_commands[] = {
1742 /* short, long callback, extra, prefer, parameter, description */
1743 { nullptr, "--action:", gr_action, TRUE, FALSE, N_("<ACTION>"), N_("execute keyboard action (See Help/Reference/Remote Keyboard Actions)") },
1744 { nullptr, "--action-list", gr_action_list, FALSE, FALSE, nullptr, N_("list available keyboard actions (some are redundant)") },
1745 { "-b", "--back", gr_image_prev, FALSE, FALSE, nullptr, N_("previous image") },
1746 { nullptr, "--close-window", gr_close_window, FALSE, FALSE, nullptr, N_("close window") },
1747 { nullptr, "--config-load:", gr_config_load, TRUE, FALSE, N_("<FILE>|layout ID"), N_("load configuration from FILE") },
1748 { "-cm","--cache-metadata", gr_cache_metadata, FALSE, FALSE, nullptr, N_("clean the metadata cache") },
1749 { "-cr:", "--cache-render:", gr_cache_render, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails") },
1750 { "-crr:", "--cache-render-recurse:", gr_cache_render_recurse, TRUE, FALSE, N_("<folder> "), N_("render thumbnails recursively") },
1751 { "-crs:", "--cache-render-shared:", gr_cache_render_standard, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails (see Help)") },
1752 { "-crsr:", "--cache-render-shared-recurse:", gr_cache_render_standard_recurse, TRUE, FALSE, N_("<folder>"), N_(" render thumbnails recursively (see Help)") },
1753 { "-cs:", "--cache-shared:", gr_cache_shared, TRUE, FALSE, N_("clear|clean"), N_("clear or clean shared thumbnail cache") },
1754 { "-ct:", "--cache-thumbs:", gr_cache_thumb, TRUE, FALSE, N_("clear|clean"), N_("clear or clean thumbnail cache") },
1755 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("<[H:][M:][N][.M]>"), N_("set slide show delay to Hrs Mins N.M seconds") },
1756 { nullptr, "--first", gr_image_first, FALSE, FALSE, nullptr, N_("first image") },
1757 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, nullptr, N_("toggle full screen") },
1758 { nullptr, "--file:", gr_file_load, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, bring Geeqie window to the top") },
1759 { nullptr, "file:", gr_file_load, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, bring Geeqie window to the top") },
1760 { nullptr, "--File:", gr_file_load_no_raise, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, do not bring Geeqie window to the top") },
1761 { nullptr, "File:", gr_file_load_no_raise, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, do not bring Geeqie window to the top") },
1762 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, nullptr, N_("start full screen") },
1763 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, nullptr, N_("stop full screen") },
1764 { nullptr, "--geometry=", gr_geometry, TRUE, FALSE, N_("<GEOMETRY>"), N_("set window geometry") },
1765 { nullptr, "--get-collection:", gr_collection, TRUE, FALSE, N_("<COLLECTION>"), N_("get collection content") },
1766 { nullptr, "--get-collection-list", gr_collection_list, FALSE, FALSE, nullptr, N_("get collection list") },
1767 { nullptr, "--get-destination:", gr_get_destination, TRUE, FALSE, N_("<FILE>"), N_("get destination path of FILE (See Plugins Configuration)") },
1768 { nullptr, "--get-file-info", gr_file_info, FALSE, FALSE, nullptr, N_("get file info") },
1769 { nullptr, "--get-filelist:", gr_filelist, TRUE, FALSE, N_("[<FOLDER>]"), N_("get list of files and class") },
1770 { nullptr, "--get-filelist-recurse:", gr_filelist_recurse, TRUE, FALSE, N_("[<FOLDER>]"), N_("get list of files and class recursive") },
1771 { nullptr, "--get-rectangle", gr_rectangle, FALSE, FALSE, nullptr, N_("get rectangle co-ordinates") },
1772 { nullptr, "--get-render-intent", gr_render_intent, FALSE, FALSE, nullptr, N_("get render intent") },
1773 { nullptr, "--get-selection", gr_get_selection, FALSE, FALSE, nullptr, N_("get list of selected files") },
1774 { nullptr, "--get-sidecars:", gr_get_sidecars, TRUE, FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
1775 { nullptr, "--id:", gr_lw_id, TRUE, FALSE, N_("<ID>"), N_("window id for following commands") },
1776 { nullptr, "--last", gr_image_last, FALSE, FALSE, nullptr, N_("last image") },
1777 { nullptr, "--list-add:", gr_list_add, TRUE, FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
1778 { nullptr, "--list-clear", gr_list_clear, FALSE, FALSE, nullptr, N_("clear command line collection list") },
1780 { nullptr, "--lua:", gr_lua, TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
1782 { nullptr, "--new-window", gr_new_window, FALSE, FALSE, nullptr, N_("new window") },
1783 { "-n", "--next", gr_image_next, FALSE, FALSE, nullptr, N_("next image") },
1784 { nullptr, "--pixel-info", gr_pixel_info, FALSE, FALSE, nullptr, N_("print pixel info of mouse pointer on current image") },
1785 { nullptr, "--print0", gr_print0, TRUE, FALSE, nullptr, N_("terminate returned data with null character instead of newline") },
1786 { nullptr, "--PWD:", gr_pwd, TRUE, FALSE, N_("<PWD>"), N_("use PWD as working directory for following commands") },
1787 { "-q", "--quit", gr_quit, FALSE, FALSE, nullptr, N_("quit") },
1788 { nullptr, "--raise", gr_raise, FALSE, FALSE, nullptr, N_("bring the Geeqie window to the top") },
1789 { nullptr, "raise", gr_raise, FALSE, FALSE, nullptr, N_("bring the Geeqie window to the top") },
1790 { nullptr, "--selection-add:", gr_selection_add, TRUE, FALSE, N_("[<FILE>]"), N_("adds the current file (or the specified file) to the current selection") },
1791 { nullptr, "--selection-clear", gr_selection_clear, FALSE, FALSE, nullptr, N_("clears the current selection") },
1792 { nullptr, "--selection-remove:", gr_selection_remove, TRUE, FALSE, N_("[<FILE>]"), N_("removes the current file (or the specified file) from the current selection") },
1793 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, nullptr, N_("toggle slide show") },
1794 { nullptr, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE, FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
1795 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, nullptr, N_("start slide show") },
1796 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, nullptr, N_("stop slide show") },
1797 { nullptr, "--tell", gr_file_tell, FALSE, FALSE, nullptr, N_("print filename [and Collection] of current image") },
1798 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, nullptr, N_("show tools") },
1799 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, nullptr, N_("hide tools") },
1800 { nullptr, "--view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
1801 { nullptr, "view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
1802 { nullptr, nullptr, nullptr, FALSE, FALSE, nullptr, nullptr }
1805 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
1807 gboolean match = FALSE;
1811 while (!match && remote_commands[i].func != nullptr)
1813 if (remote_commands[i].needs_extra)
1815 if (remote_commands[i].opt_s &&
1816 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
1818 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
1819 return &remote_commands[i];
1821 else if (remote_commands[i].opt_l &&
1822 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
1824 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
1825 return &remote_commands[i];
1830 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
1831 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
1833 if (offset) *offset = text;
1834 return &remote_commands[i];
1844 static void remote_cb(RemoteConnection *, const gchar *text, GIOChannel *channel, gpointer data)
1846 RemoteCommandEntry *entry;
1847 const gchar *offset;
1849 entry = remote_command_find(text, &offset);
1850 if (entry && entry->func)
1852 entry->func(offset, channel, data);
1856 log_printf("unknown remote command:%s\n", text);
1866 print_term(FALSE, _("Remote command list:\n"));
1869 while (remote_commands[i].func != nullptr)
1871 if (remote_commands[i].description)
1873 s_opt_param = g_strdup(remote_commands[i].opt_s ? remote_commands[i].opt_s : "" );
1874 l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
1875 printf_term(FALSE, " %-4s %-40s%-s\n",
1878 _(remote_commands[i].description));
1879 g_free(s_opt_param);
1880 g_free(l_opt_param);
1884 printf_term(FALSE, _("\n\n All other command line parameters are used as plain files if they exist.\n\n The name of a collection, with or without either path or extension (.gqv) may be used.\n"));
1887 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
1894 RemoteCommandEntry *entry;
1896 entry = remote_command_find(argv[i], nullptr);
1899 list = g_list_append(list, argv[i]);
1901 else if (errors && !isname(argv[i]))
1903 *errors = g_list_append(*errors, argv[i]);
1912 * @param arg_exec Binary (argv0)
1913 * @param remote_list Evaluated and recognized remote commands
1914 * @param path The current path
1915 * @param cmd_list List of all non collections in Path (gchar *path)
1916 * @param collection_list List of all collections in argv
1918 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
1919 GList *cmd_list, GList *collection_list)
1921 RemoteConnection *rc;
1922 gboolean started = FALSE;
1925 buf = g_build_filename(get_rc_dir(), ".command", NULL);
1926 rc = remote_client_open(buf);
1931 gint retry_count = 12;
1932 gboolean blank = FALSE;
1934 printf_term(FALSE, _("Remote %s not running, starting..."), GQ_APPNAME);
1936 command = g_string_new(arg_exec);
1942 RemoteCommandEntry *entry;
1944 text = static_cast<gchar *>(work->data);
1947 entry = remote_command_find(text, nullptr);
1950 /* If Geeqie is not running, stop the --new-window command opening a second window */
1951 if (g_strcmp0(text, "--new-window") == 0)
1953 remote_list = g_list_remove(remote_list, text);
1955 if (entry->prefer_command_line)
1957 remote_list = g_list_remove(remote_list, text);
1958 g_string_append(command, " ");
1959 g_string_append(command, text);
1961 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
1968 if (blank || cmd_list || path) g_string_append(command, " --blank");
1969 if (get_debug_level()) g_string_append(command, " --debug");
1971 g_string_append(command, " &");
1972 runcmd(command->str);
1973 g_string_free(command, TRUE);
1975 while (!rc && retry_count > 0)
1977 usleep((retry_count > 10) ? 500000 : 1000000);
1978 rc = remote_client_open(buf);
1979 if (!rc) print_term(FALSE, ".");
1983 print_term(FALSE, "\n");
1992 const gchar *prefix;
1993 gboolean use_path = TRUE;
1994 gboolean sent = FALSE;
2000 RemoteCommandEntry *entry;
2002 text = static_cast<gchar *>(work->data);
2005 entry = remote_command_find(text, nullptr);
2008 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
2010 remote_client_send(rc, text);
2015 if (cmd_list && cmd_list->next)
2017 prefix = "--list-add:";
2018 remote_client_send(rc, "--list-clear");
2030 text = g_strconcat(prefix, work->data, NULL);
2031 remote_client_send(rc, text);
2038 if (path && !cmd_list && use_path)
2042 text = g_strdup_printf("file:%s", path);
2043 remote_client_send(rc, text);
2049 work = collection_list;
2055 name = static_cast<const gchar *>(work->data);
2058 text = g_strdup_printf("file:%s", name);
2059 remote_client_send(rc, text);
2065 if (!started && !sent)
2067 remote_client_send(rc, "raise");
2072 print_term(TRUE, _("Remote not available\n"));
2078 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
2080 RemoteConnection *remote_connection = remote_server_open(path);
2081 auto remote_data = g_new(RemoteData, 1);
2083 remote_data->command_collection = command_collection;
2085 remote_server_subscribe(remote_connection, remote_cb, remote_data);
2086 return remote_connection;
2088 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */