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"
42 #include "view-file.h"
45 #include <sys/socket.h>
51 SERVER_MAX_CLIENTS = 8
55 REMOTE_SERVER_BACKLOG = 4
60 #define UNIX_PATH_MAX 108
64 static RemoteConnection *remote_client_open(const gchar *path);
65 static gint remote_client_send(RemoteConnection *rc, const gchar *text);
66 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data);
68 static LayoutWindow *lw_id = nullptr; /* points to the window set by the --id option */
72 guint channel_id; /* event source id */
77 CollectionData *command_collection;
82 /* To enable file names containing newlines to be processed correctly,
83 * the --print0 remote option sets returned data to be terminated with a null
84 * character rather a newline
86 static gboolean print0 = FALSE;
88 /* Remote commands from main.cc are prepended with the current dir the remote
89 * command was made from. Some remote commands require this. The
90 * value is stored here
92 static gchar *pwd = nullptr;
95 * @brief Ensures file path is absolute.
96 * @param[in] filename Filepath, absolute or relative to calling directory
97 * @returns absolute path
99 * If first character of input filepath is not the directory
100 * separator, assume it as a relative path and prepend
101 * the directory the remote command was initiated from
103 * Return value must be freed with g_free()
105 static gchar *set_pwd(gchar *filename)
109 if (strncmp(filename, G_DIR_SEPARATOR_S, 1) != 0)
111 temp = g_build_filename(pwd, filename, NULL);
115 temp = g_strdup(filename);
121 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
123 auto client = static_cast<RemoteClient *>(data);
124 RemoteConnection *rc;
125 GIOStatus status = G_IO_STATUS_NORMAL;
130 if (condition & G_IO_IN)
132 gchar *buffer = nullptr;
133 GError *error = nullptr;
135 /** @FIXME it should be possible to terminate the command with a null character */
136 g_io_channel_set_line_term(source, "<gq_end_of_command>", -1);
137 while ((status = g_io_channel_read_line(source, &buffer, nullptr, &termpos, &error)) == G_IO_STATUS_NORMAL)
141 buffer[termpos] = '\0';
143 if (strlen(buffer) > 0)
145 if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
146 g_io_channel_write_chars(source, "<gq_end_of_command>", -1, nullptr, nullptr); /* empty line finishes the command */
147 g_io_channel_flush(source, nullptr);
157 log_printf("error reading socket: %s\n", error->message);
162 if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
164 rc->clients = g_list_remove(rc->clients, client);
166 DEBUG_1("HUP detected, closing client.");
167 DEBUG_1("client count %d", g_list_length(rc->clients));
169 g_source_remove(client->channel_id);
177 static void remote_server_client_add(RemoteConnection *rc, gint fd)
179 RemoteClient *client;
182 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
184 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
189 client = g_new0(RemoteClient, 1);
193 channel = g_io_channel_unix_new(fd);
194 client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, static_cast<GIOCondition>(G_IO_IN | G_IO_HUP),
195 remote_server_client_cb, client, nullptr);
196 g_io_channel_unref(channel);
198 rc->clients = g_list_append(rc->clients, client);
199 DEBUG_1("client count %d", g_list_length(rc->clients));
202 static void remote_server_clients_close(RemoteConnection *rc)
206 auto client = static_cast<RemoteClient *>(rc->clients->data);
208 rc->clients = g_list_remove(rc->clients, client);
210 g_source_remove(client->channel_id);
216 static gboolean remote_server_read_cb(GIOChannel *, GIOCondition, gpointer data)
218 auto rc = static_cast<RemoteConnection *>(data);
222 fd = accept(rc->fd, nullptr, &alen);
225 log_printf("error accepting socket: %s\n", strerror(errno));
229 remote_server_client_add(rc, fd);
234 gboolean remote_server_exists(const gchar *path)
236 RemoteConnection *rc;
238 /* verify server up */
239 rc = remote_client_open(path);
244 /* unable to connect, remove socket file to free up address */
249 static RemoteConnection *remote_server_open(const gchar *path)
251 RemoteConnection *rc;
252 struct sockaddr_un addr;
256 if (strlen(path) >= sizeof(addr.sun_path))
258 log_printf("Address is too long: %s\n", path);
262 if (remote_server_exists(path))
264 log_printf("Address already in use: %s\n", path);
268 fd = socket(PF_UNIX, SOCK_STREAM, 0);
269 if (fd == -1) return nullptr;
271 addr.sun_family = AF_UNIX;
272 #pragma GCC diagnostic push
273 #pragma GCC diagnostic ignored "-Wstringop-truncation"
274 strncpy(addr.sun_path, path, sizeof(addr.sun_path));
275 #pragma GCC diagnostic pop
276 if (bind(fd, reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)) == -1 ||
277 listen(fd, REMOTE_SERVER_BACKLOG) == -1)
279 log_printf("error subscribing to socket: %s\n", strerror(errno));
284 rc = g_new0(RemoteConnection, 1);
288 rc->path = g_strdup(path);
290 channel = g_io_channel_unix_new(rc->fd);
291 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, nullptr);
293 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
294 remote_server_read_cb, rc, nullptr);
295 g_io_channel_unref(channel);
300 static void remote_server_subscribe(RemoteConnection *rc, RemoteConnection::ReadFunc *func, gpointer data)
302 if (!rc || !rc->server) return;
304 rc->read_func = func;
305 rc->read_data = data;
309 static RemoteConnection *remote_client_open(const gchar *path)
311 RemoteConnection *rc;
313 struct sockaddr_un addr;
316 if (strlen(path) >= sizeof(addr.sun_path)) return nullptr;
318 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return nullptr;
320 fd = socket(PF_UNIX, SOCK_STREAM, 0);
321 if (fd == -1) return nullptr;
323 addr.sun_family = AF_UNIX;
324 #pragma GCC diagnostic push
325 #pragma GCC diagnostic ignored "-Wstringop-truncation"
326 strncpy(addr.sun_path, path, sizeof(addr.sun_path));
327 #pragma GCC diagnostic pop
328 if (connect(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) == -1)
330 DEBUG_1("error connecting to socket: %s", strerror(errno));
335 rc = g_new0(RemoteConnection, 1);
338 rc->path = g_strdup(path);
343 static sig_atomic_t sigpipe_occurred = FALSE;
345 static void sighandler_sigpipe(gint)
347 sigpipe_occurred = TRUE;
350 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
352 struct sigaction new_action;
353 struct sigaction old_action;
354 gboolean ret = FALSE;
355 GError *error = nullptr;
358 if (!rc || rc->server) return FALSE;
359 if (!text) return TRUE;
361 sigpipe_occurred = FALSE;
363 new_action.sa_handler = sighandler_sigpipe;
364 sigemptyset(&new_action.sa_mask);
365 new_action.sa_flags = 0;
367 /* setup our signal handler */
368 sigaction(SIGPIPE, &new_action, &old_action);
370 channel = g_io_channel_unix_new(rc->fd);
372 g_io_channel_write_chars(channel, text, -1, nullptr, &error);
373 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, &error);
374 g_io_channel_flush(channel, &error);
378 log_printf("error reading socket: %s\n", error->message);
389 gchar *buffer = nullptr;
391 g_io_channel_set_line_term(channel, "<gq_end_of_command>", -1);
392 while (g_io_channel_read_line(channel, &buffer, nullptr, &termpos, &error) == G_IO_STATUS_NORMAL)
396 if (g_strstr_len(buffer, -1, "<gq_end_of_command>") == buffer) /* empty line finishes the command */
402 buffer[termpos] = '\0';
403 if (g_strstr_len(buffer, -1, "print0") != nullptr)
411 printf("%s%c", buffer, 0);
415 printf("%s\n", buffer);
425 log_printf("error reading socket: %s\n", error->message);
432 /* restore the original signal handler */
433 sigaction(SIGPIPE, &old_action, nullptr);
434 g_io_channel_unref(channel);
438 void remote_close(RemoteConnection *rc)
444 remote_server_clients_close(rc);
446 g_source_remove(rc->channel_id);
451 g_free(rc->read_data);
460 *-----------------------------------------------------------------------------
462 *-----------------------------------------------------------------------------
465 static void gr_image_next(const gchar *, GIOChannel *, gpointer)
467 layout_image_next(lw_id);
470 static void gr_new_window(const gchar *, GIOChannel *, gpointer)
472 LayoutWindow *lw = nullptr;
474 if (!layout_valid(&lw)) return;
476 lw_id = layout_new_from_default();
478 layout_set_path(lw_id, pwd);
481 static gboolean gr_close_window_cb(gpointer)
483 if (!layout_valid(&lw_id)) return FALSE;
485 layout_menu_close_cb(nullptr, lw_id);
487 return G_SOURCE_REMOVE;
490 static void gr_close_window(const gchar *, GIOChannel *, gpointer)
492 g_idle_add((gr_close_window_cb), nullptr);
495 static void gr_image_prev(const gchar *, GIOChannel *, gpointer)
497 layout_image_prev(lw_id);
500 static void gr_image_first(const gchar *, GIOChannel *, gpointer)
502 layout_image_first(lw_id);
505 static void gr_image_last(const gchar *, GIOChannel *, gpointer)
507 layout_image_last(lw_id);
510 static void gr_fullscreen_toggle(const gchar *, GIOChannel *, gpointer)
512 layout_image_full_screen_toggle(lw_id);
515 static void gr_fullscreen_start(const gchar *, GIOChannel *, gpointer)
517 layout_image_full_screen_start(lw_id);
520 static void gr_fullscreen_stop(const gchar *, GIOChannel *, gpointer)
522 layout_image_full_screen_stop(lw_id);
525 static void gr_lw_id(const gchar *text, GIOChannel *, gpointer)
527 lw_id = layout_find_by_layout_id(text);
530 log_printf("remote sent window ID that does not exist:\"%s\"\n",text);
532 layout_valid(&lw_id);
535 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *, gpointer)
538 gchar *tilde_filename;
540 tilde_filename = expand_tilde(text);
542 FileData *dir_fd = file_data_new_dir(tilde_filename);
543 g_free(tilde_filename);
545 layout_valid(&lw_id);
546 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);
547 file_data_unref(dir_fd);
550 layout_image_slideshow_stop(lw_id);
551 layout_image_slideshow_start_from_list(lw_id, list);
554 static void gr_cache_thumb(const gchar *text, GIOChannel *, gpointer)
556 if (!g_strcmp0(text, "clear"))
558 cache_maintain_home_remote(FALSE, TRUE, nullptr);
560 else if (!g_strcmp0(text, "clean"))
562 cache_maintain_home_remote(FALSE, FALSE, nullptr);
566 static void gr_cache_shared(const gchar *text, GIOChannel *, gpointer)
568 if (!g_strcmp0(text, "clear"))
569 cache_manager_standard_process_remote(TRUE);
570 else if (!g_strcmp0(text, "clean"))
571 cache_manager_standard_process_remote(FALSE);
574 static void gr_cache_metadata(const gchar *, GIOChannel *, gpointer)
576 cache_maintain_home_remote(TRUE, FALSE, nullptr);
579 static void gr_cache_render(const gchar *text, GIOChannel *, gpointer)
581 cache_manager_render_remote(text, FALSE, FALSE, nullptr);
584 static void gr_cache_render_recurse(const gchar *text, GIOChannel *, gpointer)
586 cache_manager_render_remote(text, TRUE, FALSE, nullptr);
589 static void gr_cache_render_standard(const gchar *text, GIOChannel *, gpointer)
591 if(options->thumbnails.spec_standard)
593 cache_manager_render_remote(text, FALSE, TRUE, nullptr);
597 static void gr_cache_render_standard_recurse(const gchar *text, GIOChannel *, gpointer)
599 if(options->thumbnails.spec_standard)
601 cache_manager_render_remote(text, TRUE, TRUE, nullptr);
605 static void gr_slideshow_toggle(const gchar *, GIOChannel *, gpointer)
607 layout_image_slideshow_toggle(lw_id);
610 static void gr_slideshow_start(const gchar *, GIOChannel *, gpointer)
612 layout_image_slideshow_start(lw_id);
615 static void gr_slideshow_stop(const gchar *, GIOChannel *, gpointer)
617 layout_image_slideshow_stop(lw_id);
620 static void gr_slideshow_delay(const gchar *text, GIOChannel *, gpointer)
628 res = sscanf(text, "%lf:%lf:%lf", &t1, &t2, &t3);
631 n = (t1 * 3600) + (t2 * 60) + t3;
632 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
633 t1 >= 24 || t2 >= 60 || t3 >= 60)
635 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
636 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
643 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
644 t1 >= 60 || t2 >= 60)
646 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
647 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
654 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
656 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
657 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
666 options->slideshow.delay = static_cast<gint>(n * 10.0 + 0.01);
669 static void gr_tools_show(const gchar *, GIOChannel *, gpointer)
674 if (layout_tools_float_get(lw_id, &popped, &hidden) && hidden)
676 layout_tools_float_set(lw_id, popped, FALSE);
680 static void gr_tools_hide(const gchar *, GIOChannel *, gpointer)
685 if (layout_tools_float_get(lw_id, &popped, &hidden) && !hidden)
687 layout_tools_float_set(lw_id, popped, TRUE);
691 static gboolean gr_quit_idle_cb(gpointer)
695 return G_SOURCE_REMOVE;
698 static void gr_quit(const gchar *, GIOChannel *, gpointer)
700 /* schedule exit when idle, if done from within a
701 * remote handler remote_close will crash
703 g_idle_add(gr_quit_idle_cb, nullptr);
706 static void gr_file_load_no_raise(const gchar *text, GIOChannel *, gpointer)
709 gchar *tilde_filename;
711 if (!download_web_file(text, TRUE, nullptr))
713 tilde_filename = expand_tilde(text);
714 filename = set_pwd(tilde_filename);
716 if (isfile(filename))
718 if (file_extension_match(filename, GQ_COLLECTION_EXT))
720 collection_window_new(filename);
724 layout_set_path(lw_id, filename);
727 else if (isdir(filename))
729 layout_set_path(lw_id, filename);
733 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
734 layout_set_path(lw_id, homedir());
738 g_free(tilde_filename);
742 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
744 gr_file_load_no_raise(text, channel, data);
746 gr_raise(text, channel, data);
749 static void gr_pixel_info(const gchar *, GIOChannel *channel, gpointer)
761 if (!layout_valid(&lw_id)) return;
763 pr = reinterpret_cast<PixbufRenderer*>(lw_id->image->pr);
767 pixbuf_renderer_get_image_size(pr, &width, &height);
768 if (width < 1 || height < 1) return;
770 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
772 if (x_pixel >= 0 && y_pixel >= 0)
774 pixbuf_renderer_get_pixel_colors(pr, x_pixel, y_pixel,
775 &r_mouse, &g_mouse, &b_mouse);
777 pixel_info = g_strdup_printf(_("[%d,%d]: RGB(%3d,%3d,%3d)"),
779 r_mouse, g_mouse, b_mouse);
781 g_io_channel_write_chars(channel, pixel_info, -1, nullptr, nullptr);
782 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
797 static void gr_rectangle(const gchar *, GIOChannel *channel, gpointer)
799 gchar *rectangle_info;
806 if (!options->draw_rectangle) return;
807 if (!layout_valid(&lw_id)) return;
809 pr = reinterpret_cast<PixbufRenderer*>(lw_id->image->pr);
813 image_get_rectangle(&x1, &y1, &x2, &y2);
814 rectangle_info = g_strdup_printf(_("%dx%d+%d+%d"),
815 (x2 > x1) ? x2 - x1 : x1 - x2,
816 (y2 > y1) ? y2 - y1 : y1 - y2,
818 (y2 > y1) ? y1 : y2);
820 g_io_channel_write_chars(channel, rectangle_info, -1, nullptr, nullptr);
821 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
823 g_free(rectangle_info);
827 static void gr_render_intent(const gchar *, GIOChannel *channel, gpointer)
829 gchar *render_intent;
831 switch (options->color_profile.render_intent)
834 render_intent = g_strdup("Perceptual");
837 render_intent = g_strdup("Relative Colorimetric");
840 render_intent = g_strdup("Saturation");
843 render_intent = g_strdup("Absolute Colorimetric");
846 render_intent = g_strdup("none");
850 g_io_channel_write_chars(channel, render_intent, -1, nullptr, nullptr);
851 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
853 g_free(render_intent);
856 static void get_filelist(const gchar *text, GIOChannel *channel, gboolean recurse)
858 GList *list = nullptr;
859 FileFormatClass format_class;
863 gchar *tilde_filename;
865 if (strcmp(text, "") == 0)
867 if (layout_valid(&lw_id))
869 dir_fd = file_data_new_dir(lw_id->dir_fd->path);
878 tilde_filename = expand_tilde(text);
879 if (isdir(tilde_filename))
881 dir_fd = file_data_new_dir(tilde_filename);
885 g_free(tilde_filename);
888 g_free(tilde_filename);
893 list = filelist_recursive(dir_fd);
897 filelist_read(dir_fd, &list, nullptr);
900 GString *out_string = g_string_new(nullptr);
904 fd = static_cast<FileData *>(work->data);
905 g_string_append(out_string, fd->path);
906 format_class = filter_file_get_class(fd->path);
908 switch (format_class)
910 case FORMAT_CLASS_IMAGE:
911 out_string = g_string_append(out_string, " Class: Image");
913 case FORMAT_CLASS_RAWIMAGE:
914 out_string = g_string_append(out_string, " Class: RAW image");
916 case FORMAT_CLASS_META:
917 out_string = g_string_append(out_string, " Class: Metadata");
919 case FORMAT_CLASS_VIDEO:
920 out_string = g_string_append(out_string, " Class: Video");
922 case FORMAT_CLASS_COLLECTION:
923 out_string = g_string_append(out_string, " Class: Collection");
925 case FORMAT_CLASS_DOCUMENT:
926 out_string = g_string_append(out_string, " Class: Document");
928 case FORMAT_CLASS_ARCHIVE:
929 out_string = g_string_append(out_string, " Class: Archive");
931 case FORMAT_CLASS_UNKNOWN:
932 out_string = g_string_append(out_string, " Class: Unknown");
935 out_string = g_string_append(out_string, " Class: Unknown");
938 out_string = g_string_append(out_string, "\n");
942 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
943 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
945 g_string_free(out_string, TRUE);
947 file_data_unref(dir_fd);
950 static void gr_get_selection(const gchar *, GIOChannel *channel, gpointer)
952 if (!layout_valid(&lw_id)) return;
954 GList *selected = layout_selection_list(lw_id); // Keep copy to free.
955 GString *out_string = g_string_new(nullptr);
957 GList *work = selected;
960 auto fd = static_cast<FileData *>(work->data);
961 g_assert(fd->magick == FD_MAGICK);
963 g_string_append_printf(out_string, "%s %s\n",
965 format_class_list[filter_file_get_class(fd->path)]);
970 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
971 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
973 filelist_free(selected);
974 g_string_free(out_string, TRUE);
977 static void gr_selection_add(const gchar *text, GIOChannel *, gpointer)
979 if (!layout_valid(&lw_id)) return;
981 FileData *fd_to_select = nullptr;
982 if (strcmp(text, "") == 0)
984 // No file specified, use current fd.
985 fd_to_select = layout_image_get_fd(lw_id);
989 // Search through the current file list for a file matching the specified path.
990 // "Match" is either a basename match or a file path match.
991 gchar *path = expand_tilde(text);
992 gchar *filename = g_path_get_basename(path);
993 gchar *slash_plus_filename = g_strdup_printf("%s%s", G_DIR_SEPARATOR_S, filename);
995 GList *file_list = layout_list(lw_id);
996 for (GList *work = file_list; work && !fd_to_select; work = work->next)
998 auto fd = static_cast<FileData *>(work->data);
999 if (!strcmp(path, fd->path) || g_str_has_suffix(fd->path, slash_plus_filename))
1001 fd_to_select = file_data_ref(fd);
1002 continue; // will exit loop.
1005 for (GList *sidecar = fd->sidecar_files; sidecar && !fd_to_select; sidecar = sidecar->next)
1007 auto side_fd = static_cast<FileData *>(sidecar->data);
1008 if (!strcmp(path, side_fd->path)
1009 || g_str_has_suffix(side_fd->path, slash_plus_filename))
1011 fd_to_select = file_data_ref(side_fd);
1012 continue; // will exit both nested loops.
1019 log_printf("remote sent --selection-add filename that could not be found: \"%s\"\n",
1023 filelist_free(file_list);
1024 g_free(slash_plus_filename);
1031 GList *to_select = g_list_append(nullptr, fd_to_select);
1032 // Using the "_list" variant doesn't clear the existing selection.
1033 layout_select_list(lw_id, to_select);
1034 filelist_free(to_select);
1038 static void gr_selection_clear(const gchar *, GIOChannel *, gpointer)
1040 layout_select_none(lw_id); // Checks lw_id validity internally.
1043 static void gr_selection_remove(const gchar *text, GIOChannel *, gpointer)
1045 if (!layout_valid(&lw_id)) return;
1047 GList *selected = layout_selection_list(lw_id); // Keep copy to free.
1050 log_printf("remote sent --selection-remove with empty selection.");
1054 FileData *fd_to_deselect = nullptr;
1055 gchar *path = nullptr;
1056 gchar *filename = nullptr;
1057 gchar *slash_plus_filename = nullptr;
1058 if (strcmp(text, "") == 0)
1060 // No file specified, use current fd.
1061 fd_to_deselect = layout_image_get_fd(lw_id);
1062 if (!fd_to_deselect)
1064 log_printf("remote sent \"--selection-remove:\" with no current image");
1065 filelist_free(selected);
1071 // Search through the selection list for a file matching the specified path.
1072 // "Match" is either a basename match or a file path match.
1073 path = expand_tilde(text);
1074 filename = g_path_get_basename(path);
1075 slash_plus_filename = g_strdup_printf("%s%s", G_DIR_SEPARATOR_S, filename);
1078 GList *prior_link = nullptr; // Stash base for link removal to avoid a second traversal.
1079 GList *link_to_remove = nullptr;
1080 for (GList *work = selected; work; prior_link = work, work = work->next)
1082 auto fd = static_cast<FileData *>(work->data);
1085 if (fd == fd_to_deselect)
1087 link_to_remove = work;
1093 // path, filename, and slash_plus_filename should be defined.
1095 if (!strcmp(path, fd->path) || g_str_has_suffix(fd->path, slash_plus_filename))
1097 link_to_remove = work;
1103 if (!link_to_remove)
1107 log_printf("remote sent \"--selection-remove:\" but current image is not selected");
1111 log_printf("remote sent \"--selection-remove:%s\" but that filename is not selected",
1117 if (link_to_remove == selected)
1119 // Remove first link.
1120 selected = g_list_remove_link(selected, link_to_remove);
1121 filelist_free(link_to_remove);
1122 link_to_remove = nullptr;
1126 // Remove a subsequent link.
1127 prior_link = g_list_remove_link(prior_link, link_to_remove);
1128 filelist_free(link_to_remove);
1129 link_to_remove = nullptr;
1132 // Re-select all but the deselected item.
1133 layout_select_none(lw_id);
1134 layout_select_list(lw_id, selected);
1137 filelist_free(selected);
1138 file_data_unref(fd_to_deselect);
1139 g_free(slash_plus_filename);
1144 static void gr_collection(const gchar *text, GIOChannel *channel, gpointer)
1146 GString *contents = g_string_new(nullptr);
1148 if (is_collection(text))
1150 collection_contents(text, &contents);
1157 g_io_channel_write_chars(channel, contents->str, -1, nullptr, nullptr);
1158 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1160 g_string_free(contents, TRUE);
1163 static void gr_collection_list(const gchar *, GIOChannel *channel, gpointer)
1166 GList *collection_list = nullptr;
1168 GString *out_string = g_string_new(nullptr);
1170 collect_manager_list(&collection_list, nullptr, nullptr);
1172 work = collection_list;
1175 auto collection_name = static_cast<const gchar *>(work->data);
1176 out_string = g_string_append(out_string, collection_name);
1177 out_string = g_string_append(out_string, "\n");
1182 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
1183 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1185 g_list_free_full(collection_list, g_free);
1186 g_string_free(out_string, TRUE);
1189 static gboolean wait_cb(gpointer data)
1191 gint position = GPOINTER_TO_INT(data);
1192 gint x = position >> 16;
1193 gint y = position - (x << 16);
1195 gq_gtk_window_move(GTK_WINDOW(lw_id->window), x, y);
1197 return G_SOURCE_REMOVE;
1200 static void gr_geometry(const gchar *text, GIOChannel *, gpointer)
1204 if (!layout_valid(&lw_id) || !text)
1211 geometry = g_strsplit_set(text, "+", 3);
1212 if (geometry[1] != nullptr && geometry[2] != nullptr )
1214 gq_gtk_window_move(GTK_WINDOW(lw_id->window), atoi(geometry[1]), atoi(geometry[2]));
1219 geometry = g_strsplit_set(text, "+x", 4);
1220 if (geometry[0] != nullptr && geometry[1] != nullptr)
1222 gtk_window_resize(GTK_WINDOW(lw_id->window), atoi(geometry[0]), atoi(geometry[1]));
1224 if (geometry[2] != nullptr && geometry[3] != nullptr)
1226 /* There is an occasional problem with a window_move immediately after a window_resize */
1227 g_idle_add(wait_cb, GINT_TO_POINTER((atoi(geometry[2]) << 16) + atoi(geometry[3])));
1230 g_strfreev(geometry);
1233 static void gr_filelist(const gchar *text, GIOChannel *channel, gpointer)
1235 get_filelist(text, channel, FALSE);
1238 static void gr_filelist_recurse(const gchar *text, GIOChannel *channel, gpointer)
1240 get_filelist(text, channel, TRUE);
1243 static void gr_file_tell(const gchar *, GIOChannel *channel, gpointer)
1246 gchar *collection_name = nullptr;
1248 if (!layout_valid(&lw_id)) return;
1250 if (image_get_path(lw_id->image))
1252 if (lw_id->image->collection && lw_id->image->collection->name)
1254 collection_name = remove_extension_from_path(lw_id->image->collection->name);
1255 out_string = g_strconcat(image_get_path(lw_id->image), " Collection: ", collection_name, NULL);
1259 out_string = g_strconcat(image_get_path(lw_id->image), NULL);
1264 out_string = g_strconcat(lw_id->dir_fd->path, G_DIR_SEPARATOR_S, NULL);
1267 g_io_channel_write_chars(channel, out_string, -1, nullptr, nullptr);
1268 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1270 g_free(collection_name);
1274 static void gr_file_info(const gchar *, GIOChannel *channel, gpointer)
1278 gchar *country_name;
1279 gchar *country_code;
1282 GString *out_string;
1283 FileFormatClass format_class;
1285 if (!layout_valid(&lw_id)) return;
1287 if (image_get_path(lw_id->image))
1289 filename = g_strdup(image_get_path(lw_id->image));
1290 fd = file_data_new_group(filename);
1291 out_string = g_string_new(nullptr);
1295 format_class = filter_file_get_class(image_get_path(lw_id->image));
1299 format_class = FORMAT_CLASS_UNKNOWN;
1302 g_string_append_printf(out_string, _("Class: %s\n"), format_class_list[format_class]);
1304 if (fd->page_total > 1)
1306 g_string_append_printf(out_string, _("Page no: %d/%d\n"), fd->page_num + 1, fd->page_total);
1311 country_name = exif_get_data_as_text(fd->exif, "formatted.countryname");
1314 g_string_append_printf(out_string, _("Country name: %s\n"), country_name);
1315 g_free(country_name);
1318 country_code = exif_get_data_as_text(fd->exif, "formatted.countrycode");
1321 g_string_append_printf(out_string, _("Country code: %s\n"), country_code);
1322 g_free(country_code);
1325 timezone = exif_get_data_as_text(fd->exif, "formatted.timezone");
1328 g_string_append_printf(out_string, _("Timezone: %s\n"), timezone);
1332 local_time = exif_get_data_as_text(fd->exif, "formatted.localtime");
1335 g_string_append_printf(out_string, ("Local time: %s\n"), local_time);
1340 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
1341 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1343 g_string_free(out_string, TRUE);
1344 file_data_unref(fd);
1349 static gchar *config_file_path(const gchar *param)
1351 gchar *path = nullptr;
1352 gchar *full_name = nullptr;
1354 if (file_extension_match(param, ".xml"))
1356 path = g_build_filename(get_window_layouts_dir(), param, NULL);
1358 else if (file_extension_match(param, nullptr))
1360 full_name = g_strconcat(param, ".xml", NULL);
1361 path = g_build_filename(get_window_layouts_dir(), full_name, NULL);
1374 static gboolean is_config_file(const gchar *param)
1376 gchar *name = nullptr;
1378 name = config_file_path(param);
1387 static void gr_config_load(const gchar *text, GIOChannel *, gpointer)
1389 gchar *filename = expand_tilde(text);
1391 if (!g_strstr_len(filename, -1, G_DIR_SEPARATOR_S))
1393 if (is_config_file(filename))
1395 gchar *tmp = config_file_path(filename);
1401 if (isfile(filename))
1403 load_config_from_file(filename, FALSE);
1407 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
1408 layout_set_path(nullptr, homedir());
1414 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer)
1416 gchar *filename = expand_tilde(text);
1417 FileData *fd = file_data_new_group(filename);
1420 if (fd->parent) fd = fd->parent;
1422 g_io_channel_write_chars(channel, fd->path, -1, nullptr, nullptr);
1423 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1425 work = fd->sidecar_files;
1429 fd = static_cast<FileData *>(work->data);
1431 g_io_channel_write_chars(channel, fd->path, -1, nullptr, nullptr);
1432 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1437 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer)
1439 gchar *filename = expand_tilde(text);
1440 FileData *fd = file_data_new_group(filename);
1442 if (fd->change && fd->change->dest)
1444 g_io_channel_write_chars(channel, fd->change->dest, -1, nullptr, nullptr);
1445 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1450 static void gr_file_view(const gchar *text, GIOChannel *, gpointer)
1453 gchar *tilde_filename = expand_tilde(text);
1455 filename = set_pwd(tilde_filename);
1457 view_window_new(file_data_new_group(filename));
1459 g_free(tilde_filename);
1462 static void gr_list_clear(const gchar *, GIOChannel *, gpointer data)
1464 auto remote_data = static_cast<RemoteData *>(data);
1466 remote_data->command_collection = nullptr;
1467 remote_data->file_list = nullptr;
1468 remote_data->single_dir = TRUE;
1471 static void gr_list_add(const gchar *text, GIOChannel *, gpointer data)
1473 auto remote_data = static_cast<RemoteData *>(data);
1474 gboolean is_new = TRUE;
1475 gchar *path = nullptr;
1479 /** @FIXME Should check if file is in current dir, has tilde or is relative */
1482 log_printf("Warning: File does not exist --remote --list-add:%s", text);
1487 /* If there is a files list on the command line
1488 * check if they are all in the same folder
1490 if (remote_data->single_dir)
1493 work = remote_data->file_list;
1494 while (work && remote_data->single_dir)
1497 dirname = g_path_get_dirname((static_cast<FileData *>(work->data))->path);
1500 path = g_strdup(dirname);
1504 if (g_strcmp0(path, dirname) != 0)
1506 remote_data->single_dir = FALSE;
1515 gchar *pathname = g_path_get_dirname(text);
1516 layout_set_path(lw_id, pathname);
1519 fd = file_data_new_simple(text);
1520 remote_data->file_list = g_list_append(remote_data->file_list, fd);
1521 file_data_unref(fd);
1523 vf_select_none(lw_id->vf);
1524 remote_data->file_list = g_list_reverse(remote_data->file_list);
1526 layout_select_list(lw_id, remote_data->file_list);
1527 layout_refresh(lw_id);
1528 first = static_cast<FileData *>(g_list_first(vf_selection_get_list(lw_id->vf))->data);
1529 layout_set_fd(lw_id, first);
1534 if (!remote_data->command_collection && !remote_data->single_dir)
1536 cw = collection_window_new(nullptr);
1539 collection_path_changed(cd);
1541 remote_data->command_collection = cd;
1543 else if (!remote_data->single_dir)
1545 is_new = (!collection_get_first(remote_data->command_collection));
1548 if (!remote_data->single_dir)
1550 layout_image_set_collection(lw_id, remote_data->command_collection, collection_get_first(remote_data->command_collection));
1551 if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && is_new)
1553 layout_image_set_collection(lw_id, remote_data->command_collection, collection_get_first(remote_data->command_collection));
1558 static void gr_action(const gchar *text, GIOChannel *, gpointer)
1562 if (!layout_valid(&lw_id))
1567 if (g_strstr_len(text, -1, ".desktop") != nullptr)
1569 file_util_start_editor_from_filelist(text, layout_selection_list(lw_id), layout_get_path(lw_id), lw_id->window);
1573 action = gtk_action_group_get_action(lw_id->action_group, text);
1576 gtk_action_activate(action);
1580 log_printf("Action %s unknown", text);
1585 static void gr_action_list(const gchar *, GIOChannel *channel, gpointer)
1587 ActionItem *action_item;
1588 gint max_length = 0;
1589 GList *list = nullptr;
1590 GString *out_string = g_string_new(nullptr);
1592 if (!layout_valid(&lw_id))
1597 list = get_action_items();
1599 /* Get the length required for padding */
1600 for (GList *work = list; work; work = work->next)
1602 action_item = static_cast<ActionItem *>(work->data);
1603 const auto length = g_utf8_strlen(action_item->name, -1);
1604 max_length = MAX(length, max_length);
1607 /* Pad the action names to the same column for readable output */
1608 for (GList *work = list; work; work = work->next)
1610 action_item = static_cast<ActionItem *>(work->data);
1612 g_string_append_printf(out_string, "%-*s", max_length + 4, action_item->name);
1613 out_string = g_string_append(out_string, action_item->label);
1614 out_string = g_string_append(out_string, "\n");
1617 action_items_free(list);
1619 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
1620 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1622 g_string_free(out_string, TRUE);
1625 static void gr_raise(const gchar *, GIOChannel *, gpointer)
1627 if (layout_valid(&lw_id))
1629 gtk_window_present(GTK_WINDOW(lw_id->window));
1633 static void gr_pwd(const gchar *text, GIOChannel *, gpointer)
1635 LayoutWindow *lw = nullptr;
1640 pwd = g_strdup(text);
1644 static void gr_print0(const gchar *, GIOChannel *channel, gpointer)
1646 g_io_channel_write_chars(channel, "print0", -1, nullptr, nullptr);
1647 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1651 static void gr_lua(const gchar *text, GIOChannel *channel, gpointer)
1653 gchar *result = nullptr;
1654 gchar **lua_command;
1656 lua_command = g_strsplit(text, ",", 2);
1658 if (lua_command[0] && lua_command[1])
1660 FileData *fd = file_data_new_group(lua_command[0]);
1661 result = g_strdup(lua_callvalue(fd, lua_command[1], nullptr));
1664 g_io_channel_write_chars(channel, result, -1, nullptr, nullptr);
1668 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, nullptr, nullptr);
1673 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, nullptr, nullptr);
1676 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1678 g_strfreev(lua_command);
1683 struct RemoteCommandEntry {
1686 void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
1687 gboolean needs_extra;
1688 gboolean prefer_command_line;
1689 const gchar *parameter;
1690 const gchar *description;
1693 static RemoteCommandEntry remote_commands[] = {
1694 /* short, long callback, extra, prefer, parameter, description */
1695 { nullptr, "--action:", gr_action, TRUE, FALSE, N_("<ACTION>"), N_("execute keyboard action (See Help/Reference/Remote Keyboard Actions)") },
1696 { nullptr, "--action-list", gr_action_list, FALSE, FALSE, nullptr, N_("list available keyboard actions (some are redundant)") },
1697 { "-b", "--back", gr_image_prev, FALSE, FALSE, nullptr, N_("previous image") },
1698 { nullptr, "--close-window", gr_close_window, FALSE, FALSE, nullptr, N_("close window") },
1699 { nullptr, "--config-load:", gr_config_load, TRUE, FALSE, N_("<FILE>|layout ID"), N_("load configuration from FILE") },
1700 { "-cm","--cache-metadata", gr_cache_metadata, FALSE, FALSE, nullptr, N_("clean the metadata cache") },
1701 { "-cr:", "--cache-render:", gr_cache_render, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails") },
1702 { "-crr:", "--cache-render-recurse:", gr_cache_render_recurse, TRUE, FALSE, N_("<folder> "), N_("render thumbnails recursively") },
1703 { "-crs:", "--cache-render-shared:", gr_cache_render_standard, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails (see Help)") },
1704 { "-crsr:", "--cache-render-shared-recurse:", gr_cache_render_standard_recurse, TRUE, FALSE, N_("<folder>"), N_(" render thumbnails recursively (see Help)") },
1705 { "-cs:", "--cache-shared:", gr_cache_shared, TRUE, FALSE, N_("clear|clean"), N_("clear or clean shared thumbnail cache") },
1706 { "-ct:", "--cache-thumbs:", gr_cache_thumb, TRUE, FALSE, N_("clear|clean"), N_("clear or clean thumbnail cache") },
1707 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("<[H:][M:][N][.M]>"), N_("set slide show delay to Hrs Mins N.M seconds") },
1708 { nullptr, "--first", gr_image_first, FALSE, FALSE, nullptr, N_("first image") },
1709 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, nullptr, N_("toggle full screen") },
1710 { nullptr, "--file:", gr_file_load, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, bring Geeqie window to the top") },
1711 { nullptr, "file:", gr_file_load, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, bring Geeqie window to the top") },
1712 { 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") },
1713 { 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") },
1714 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, nullptr, N_("start full screen") },
1715 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, nullptr, N_("stop full screen") },
1716 { nullptr, "--geometry=", gr_geometry, TRUE, FALSE, N_("<GEOMETRY>"), N_("set window geometry") },
1717 { nullptr, "--get-collection:", gr_collection, TRUE, FALSE, N_("<COLLECTION>"), N_("get collection content") },
1718 { nullptr, "--get-collection-list", gr_collection_list, FALSE, FALSE, nullptr, N_("get collection list") },
1719 { nullptr, "--get-destination:", gr_get_destination, TRUE, FALSE, N_("<FILE>"), N_("get destination path of FILE (See Plugins Configuration)") },
1720 { nullptr, "--get-file-info", gr_file_info, FALSE, FALSE, nullptr, N_("get file info") },
1721 { nullptr, "--get-filelist:", gr_filelist, TRUE, FALSE, N_("[<FOLDER>]"), N_("get list of files and class") },
1722 { nullptr, "--get-filelist-recurse:", gr_filelist_recurse, TRUE, FALSE, N_("[<FOLDER>]"), N_("get list of files and class recursive") },
1723 { nullptr, "--get-rectangle", gr_rectangle, FALSE, FALSE, nullptr, N_("get rectangle co-ordinates") },
1724 { nullptr, "--get-render-intent", gr_render_intent, FALSE, FALSE, nullptr, N_("get render intent") },
1725 { nullptr, "--get-selection", gr_get_selection, FALSE, FALSE, nullptr, N_("get list of selected files") },
1726 { nullptr, "--get-sidecars:", gr_get_sidecars, TRUE, FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
1727 { nullptr, "--id:", gr_lw_id, TRUE, FALSE, N_("<ID>"), N_("window id for following commands") },
1728 { nullptr, "--last", gr_image_last, FALSE, FALSE, nullptr, N_("last image") },
1729 { nullptr, "--list-add:", gr_list_add, TRUE, FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
1730 { nullptr, "--list-clear", gr_list_clear, FALSE, FALSE, nullptr, N_("clear command line collection list") },
1732 { nullptr, "--lua:", gr_lua, TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
1734 { nullptr, "--new-window", gr_new_window, FALSE, FALSE, nullptr, N_("new window") },
1735 { "-n", "--next", gr_image_next, FALSE, FALSE, nullptr, N_("next image") },
1736 { nullptr, "--pixel-info", gr_pixel_info, FALSE, FALSE, nullptr, N_("print pixel info of mouse pointer on current image") },
1737 { nullptr, "--print0", gr_print0, TRUE, FALSE, nullptr, N_("terminate returned data with null character instead of newline") },
1738 { nullptr, "--PWD:", gr_pwd, TRUE, FALSE, N_("<PWD>"), N_("use PWD as working directory for following commands") },
1739 { "-q", "--quit", gr_quit, FALSE, FALSE, nullptr, N_("quit") },
1740 { nullptr, "--raise", gr_raise, FALSE, FALSE, nullptr, N_("bring the Geeqie window to the top") },
1741 { nullptr, "raise", gr_raise, FALSE, FALSE, nullptr, N_("bring the Geeqie window to the top") },
1742 { nullptr, "--selection-add:", gr_selection_add, TRUE, FALSE, N_("[<FILE>]"), N_("adds the current file (or the specified file) to the current selection") },
1743 { nullptr, "--selection-clear", gr_selection_clear, FALSE, FALSE, nullptr, N_("clears the current selection") },
1744 { nullptr, "--selection-remove:", gr_selection_remove, TRUE, FALSE, N_("[<FILE>]"), N_("removes the current file (or the specified file) from the current selection") },
1745 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, nullptr, N_("toggle slide show") },
1746 { nullptr, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE, FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
1747 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, nullptr, N_("start slide show") },
1748 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, nullptr, N_("stop slide show") },
1749 { nullptr, "--tell", gr_file_tell, FALSE, FALSE, nullptr, N_("print filename [and Collection] of current image") },
1750 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, nullptr, N_("show tools") },
1751 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, nullptr, N_("hide tools") },
1752 { nullptr, "--view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
1753 { nullptr, "view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
1754 { nullptr, nullptr, nullptr, FALSE, FALSE, nullptr, nullptr }
1757 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
1759 gboolean match = FALSE;
1763 while (!match && remote_commands[i].func != nullptr)
1765 if (remote_commands[i].needs_extra)
1767 if (remote_commands[i].opt_s &&
1768 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
1770 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
1771 return &remote_commands[i];
1774 if (remote_commands[i].opt_l &&
1775 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
1777 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
1778 return &remote_commands[i];
1783 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
1784 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
1786 if (offset) *offset = text;
1787 return &remote_commands[i];
1797 static void remote_cb(RemoteConnection *, const gchar *text, GIOChannel *channel, gpointer data)
1799 RemoteCommandEntry *entry;
1800 const gchar *offset;
1802 entry = remote_command_find(text, &offset);
1803 if (entry && entry->func)
1805 entry->func(offset, channel, data);
1809 log_printf("unknown remote command:%s\n", text);
1819 print_term(FALSE, _("Remote command list:\n"));
1822 while (remote_commands[i].func != nullptr)
1824 if (remote_commands[i].description)
1826 s_opt_param = g_strdup(remote_commands[i].opt_s ? remote_commands[i].opt_s : "" );
1827 l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
1829 if (g_str_has_prefix(l_opt_param, "--"))
1831 printf_term(FALSE, " %-4s %-40s%-s\n", s_opt_param, l_opt_param, _(remote_commands[i].description));
1833 g_free(s_opt_param);
1834 g_free(l_opt_param);
1838 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"));
1841 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
1848 RemoteCommandEntry *entry;
1850 entry = remote_command_find(argv[i], nullptr);
1853 list = g_list_append(list, argv[i]);
1855 else if (errors && !isname(argv[i]))
1857 *errors = g_list_append(*errors, argv[i]);
1866 * @param arg_exec Binary (argv0)
1867 * @param remote_list Evaluated and recognized remote commands
1868 * @param path The current path
1869 * @param cmd_list List of all non collections in Path (gchar *path)
1870 * @param collection_list List of all collections in argv
1872 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
1873 GList *cmd_list, GList *collection_list)
1875 RemoteConnection *rc;
1876 gboolean started = FALSE;
1879 buf = g_build_filename(get_rc_dir(), ".command", NULL);
1880 rc = remote_client_open(buf);
1885 gint retry_count = 12;
1886 gboolean blank = FALSE;
1888 printf_term(FALSE, _("Remote %s not running, starting..."), GQ_APPNAME);
1890 command = g_string_new(arg_exec);
1896 RemoteCommandEntry *entry;
1898 text = static_cast<gchar *>(work->data);
1901 entry = remote_command_find(text, nullptr);
1904 /* If Geeqie is not running, stop the --new-window command opening a second window */
1905 if (g_strcmp0(text, "--new-window") == 0)
1907 remote_list = g_list_remove(remote_list, text);
1909 if (entry->prefer_command_line)
1911 remote_list = g_list_remove(remote_list, text);
1912 g_string_append(command, " ");
1913 g_string_append(command, text);
1915 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
1922 if (blank || cmd_list || path) g_string_append(command, " --blank");
1923 if (get_debug_level()) g_string_append(command, " --debug");
1925 g_string_append(command, " &");
1926 runcmd(command->str);
1927 g_string_free(command, TRUE);
1929 while (!rc && retry_count > 0)
1931 usleep((retry_count > 10) ? 500000 : 1000000);
1932 rc = remote_client_open(buf);
1933 if (!rc) print_term(FALSE, ".");
1937 print_term(FALSE, "\n");
1946 const gchar *prefix;
1947 gboolean use_path = TRUE;
1948 gboolean sent = FALSE;
1954 RemoteCommandEntry *entry;
1956 text = static_cast<gchar *>(work->data);
1959 entry = remote_command_find(text, nullptr);
1962 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
1964 remote_client_send(rc, text);
1969 if (cmd_list && cmd_list->next)
1971 prefix = "--list-add:";
1972 remote_client_send(rc, "--list-clear");
1984 text = g_strconcat(prefix, work->data, NULL);
1985 remote_client_send(rc, text);
1992 if (path && !cmd_list && use_path)
1996 text = g_strdup_printf("file:%s", path);
1997 remote_client_send(rc, text);
2003 work = collection_list;
2009 name = static_cast<const gchar *>(work->data);
2012 text = g_strdup_printf("file:%s", name);
2013 remote_client_send(rc, text);
2019 if (!started && !sent)
2021 remote_client_send(rc, "raise");
2026 print_term(TRUE, _("Remote not available\n"));
2032 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
2034 RemoteConnection *remote_connection = remote_server_open(path);
2035 auto remote_data = g_new(RemoteData, 1);
2037 remote_data->command_collection = command_collection;
2039 remote_server_subscribe(remote_connection, remote_cb, remote_data);
2040 return remote_connection;
2042 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */