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, old_action;
353 gboolean ret = FALSE;
354 GError *error = nullptr;
357 if (!rc || rc->server) return FALSE;
358 if (!text) return TRUE;
360 sigpipe_occurred = FALSE;
362 new_action.sa_handler = sighandler_sigpipe;
363 sigemptyset(&new_action.sa_mask);
364 new_action.sa_flags = 0;
366 /* setup our signal handler */
367 sigaction(SIGPIPE, &new_action, &old_action);
369 channel = g_io_channel_unix_new(rc->fd);
371 g_io_channel_write_chars(channel, text, -1, nullptr, &error);
372 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, &error);
373 g_io_channel_flush(channel, &error);
377 log_printf("error reading socket: %s\n", error->message);
388 gchar *buffer = nullptr;
390 g_io_channel_set_line_term(channel, "<gq_end_of_command>", -1);
391 while (g_io_channel_read_line(channel, &buffer, nullptr, &termpos, &error) == G_IO_STATUS_NORMAL)
395 if (g_strstr_len(buffer, -1, "<gq_end_of_command>") == buffer) /* empty line finishes the command */
401 buffer[termpos] = '\0';
402 if (g_strstr_len(buffer, -1, "print0") != nullptr)
410 printf("%s%c", buffer, 0);
414 printf("%s\n", buffer);
424 log_printf("error reading socket: %s\n", error->message);
431 /* restore the original signal handler */
432 sigaction(SIGPIPE, &old_action, nullptr);
433 g_io_channel_unref(channel);
437 void remote_close(RemoteConnection *rc)
443 remote_server_clients_close(rc);
445 g_source_remove(rc->channel_id);
450 g_free(rc->read_data);
459 *-----------------------------------------------------------------------------
461 *-----------------------------------------------------------------------------
464 static void gr_image_next(const gchar *, GIOChannel *, gpointer)
466 layout_image_next(lw_id);
469 static void gr_new_window(const gchar *, GIOChannel *, gpointer)
471 LayoutWindow *lw = nullptr;
473 if (!layout_valid(&lw)) return;
475 lw_id = layout_new_from_default();
477 layout_set_path(lw_id, pwd);
480 static gboolean gr_close_window_cb(gpointer)
482 if (!layout_valid(&lw_id)) return FALSE;
484 layout_menu_close_cb(nullptr, lw_id);
486 return G_SOURCE_REMOVE;
489 static void gr_close_window(const gchar *, GIOChannel *, gpointer)
491 g_idle_add((gr_close_window_cb), nullptr);
494 static void gr_image_prev(const gchar *, GIOChannel *, gpointer)
496 layout_image_prev(lw_id);
499 static void gr_image_first(const gchar *, GIOChannel *, gpointer)
501 layout_image_first(lw_id);
504 static void gr_image_last(const gchar *, GIOChannel *, gpointer)
506 layout_image_last(lw_id);
509 static void gr_fullscreen_toggle(const gchar *, GIOChannel *, gpointer)
511 layout_image_full_screen_toggle(lw_id);
514 static void gr_fullscreen_start(const gchar *, GIOChannel *, gpointer)
516 layout_image_full_screen_start(lw_id);
519 static void gr_fullscreen_stop(const gchar *, GIOChannel *, gpointer)
521 layout_image_full_screen_stop(lw_id);
524 static void gr_lw_id(const gchar *text, GIOChannel *, gpointer)
526 lw_id = layout_find_by_layout_id(text);
529 log_printf("remote sent window ID that does not exist:\"%s\"\n",text);
531 layout_valid(&lw_id);
534 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *, gpointer)
537 gchar *tilde_filename;
539 tilde_filename = expand_tilde(text);
541 FileData *dir_fd = file_data_new_dir(tilde_filename);
542 g_free(tilde_filename);
544 layout_valid(&lw_id);
545 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);
546 file_data_unref(dir_fd);
549 layout_image_slideshow_stop(lw_id);
550 layout_image_slideshow_start_from_list(lw_id, list);
553 static void gr_cache_thumb(const gchar *text, GIOChannel *, gpointer)
555 if (!g_strcmp0(text, "clear"))
557 cache_maintain_home_remote(FALSE, TRUE, nullptr);
559 else if (!g_strcmp0(text, "clean"))
561 cache_maintain_home_remote(FALSE, FALSE, nullptr);
565 static void gr_cache_shared(const gchar *text, GIOChannel *, gpointer)
567 if (!g_strcmp0(text, "clear"))
568 cache_manager_standard_process_remote(TRUE);
569 else if (!g_strcmp0(text, "clean"))
570 cache_manager_standard_process_remote(FALSE);
573 static void gr_cache_metadata(const gchar *, GIOChannel *, gpointer)
575 cache_maintain_home_remote(TRUE, FALSE, nullptr);
578 static void gr_cache_render(const gchar *text, GIOChannel *, gpointer)
580 cache_manager_render_remote(text, FALSE, FALSE, nullptr);
583 static void gr_cache_render_recurse(const gchar *text, GIOChannel *, gpointer)
585 cache_manager_render_remote(text, TRUE, FALSE, nullptr);
588 static void gr_cache_render_standard(const gchar *text, GIOChannel *, gpointer)
590 if(options->thumbnails.spec_standard)
592 cache_manager_render_remote(text, FALSE, TRUE, nullptr);
596 static void gr_cache_render_standard_recurse(const gchar *text, GIOChannel *, gpointer)
598 if(options->thumbnails.spec_standard)
600 cache_manager_render_remote(text, TRUE, TRUE, nullptr);
604 static void gr_slideshow_toggle(const gchar *, GIOChannel *, gpointer)
606 layout_image_slideshow_toggle(lw_id);
609 static void gr_slideshow_start(const gchar *, GIOChannel *, gpointer)
611 layout_image_slideshow_start(lw_id);
614 static void gr_slideshow_stop(const gchar *, GIOChannel *, gpointer)
616 layout_image_slideshow_stop(lw_id);
619 static void gr_slideshow_delay(const gchar *text, GIOChannel *, gpointer)
621 gdouble t1, t2, t3, n;
624 res = sscanf(text, "%lf:%lf:%lf", &t1, &t2, &t3);
627 n = (t1 * 3600) + (t2 * 60) + t3;
628 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
629 t1 >= 24 || t2 >= 60 || t3 >= 60)
631 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
632 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
639 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
640 t1 >= 60 || t2 >= 60)
642 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
643 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
650 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
652 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
653 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
662 options->slideshow.delay = static_cast<gint>(n * 10.0 + 0.01);
665 static void gr_tools_show(const gchar *, GIOChannel *, gpointer)
670 if (layout_tools_float_get(lw_id, &popped, &hidden) && hidden)
672 layout_tools_float_set(lw_id, popped, FALSE);
676 static void gr_tools_hide(const gchar *, GIOChannel *, gpointer)
681 if (layout_tools_float_get(lw_id, &popped, &hidden) && !hidden)
683 layout_tools_float_set(lw_id, popped, TRUE);
687 static gboolean gr_quit_idle_cb(gpointer)
691 return G_SOURCE_REMOVE;
694 static void gr_quit(const gchar *, GIOChannel *, gpointer)
696 /* schedule exit when idle, if done from within a
697 * remote handler remote_close will crash
699 g_idle_add(gr_quit_idle_cb, nullptr);
702 static void gr_file_load_no_raise(const gchar *text, GIOChannel *, gpointer)
705 gchar *tilde_filename;
707 if (!download_web_file(text, TRUE, nullptr))
709 tilde_filename = expand_tilde(text);
710 filename = set_pwd(tilde_filename);
712 if (isfile(filename))
714 if (file_extension_match(filename, GQ_COLLECTION_EXT))
716 collection_window_new(filename);
720 layout_set_path(lw_id, filename);
723 else if (isdir(filename))
725 layout_set_path(lw_id, filename);
729 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
730 layout_set_path(lw_id, homedir());
734 g_free(tilde_filename);
738 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
740 gr_file_load_no_raise(text, channel, data);
742 gr_raise(text, channel, data);
745 static void gr_pixel_info(const gchar *, GIOChannel *channel, gpointer)
748 gint x_pixel, y_pixel;
750 gint r_mouse, g_mouse, b_mouse;
753 if (!layout_valid(&lw_id)) return;
755 pr = reinterpret_cast<PixbufRenderer*>(lw_id->image->pr);
759 pixbuf_renderer_get_image_size(pr, &width, &height);
760 if (width < 1 || height < 1) return;
762 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
764 if (x_pixel >= 0 && y_pixel >= 0)
766 pixbuf_renderer_get_pixel_colors(pr, x_pixel, y_pixel,
767 &r_mouse, &g_mouse, &b_mouse);
769 pixel_info = g_strdup_printf(_("[%d,%d]: RGB(%3d,%3d,%3d)"),
771 r_mouse, g_mouse, b_mouse);
773 g_io_channel_write_chars(channel, pixel_info, -1, nullptr, nullptr);
774 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
789 static void gr_rectangle(const gchar *, GIOChannel *channel, gpointer)
791 gchar *rectangle_info;
795 if (!options->draw_rectangle) return;
796 if (!layout_valid(&lw_id)) return;
798 pr = reinterpret_cast<PixbufRenderer*>(lw_id->image->pr);
802 image_get_rectangle(&x1, &y1, &x2, &y2);
803 rectangle_info = g_strdup_printf(_("%dx%d+%d+%d"),
804 (x2 > x1) ? x2 - x1 : x1 - x2,
805 (y2 > y1) ? y2 - y1 : y1 - y2,
807 (y2 > y1) ? y1 : y2);
809 g_io_channel_write_chars(channel, rectangle_info, -1, nullptr, nullptr);
810 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
812 g_free(rectangle_info);
816 static void gr_render_intent(const gchar *, GIOChannel *channel, gpointer)
818 gchar *render_intent;
820 switch (options->color_profile.render_intent)
823 render_intent = g_strdup("Perceptual");
826 render_intent = g_strdup("Relative Colorimetric");
829 render_intent = g_strdup("Saturation");
832 render_intent = g_strdup("Absolute Colorimetric");
835 render_intent = g_strdup("none");
839 g_io_channel_write_chars(channel, render_intent, -1, nullptr, nullptr);
840 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
842 g_free(render_intent);
845 static void get_filelist(const gchar *text, GIOChannel *channel, gboolean recurse)
847 GList *list = nullptr;
848 FileFormatClass format_class;
852 gchar *tilde_filename;
854 if (strcmp(text, "") == 0)
856 if (layout_valid(&lw_id))
858 dir_fd = file_data_new_dir(lw_id->dir_fd->path);
867 tilde_filename = expand_tilde(text);
868 if (isdir(tilde_filename))
870 dir_fd = file_data_new_dir(tilde_filename);
874 g_free(tilde_filename);
877 g_free(tilde_filename);
882 list = filelist_recursive(dir_fd);
886 filelist_read(dir_fd, &list, nullptr);
889 GString *out_string = g_string_new(nullptr);
893 fd = static_cast<FileData *>(work->data);
894 g_string_append(out_string, fd->path);
895 format_class = filter_file_get_class(fd->path);
897 switch (format_class)
899 case FORMAT_CLASS_IMAGE:
900 out_string = g_string_append(out_string, " Class: Image");
902 case FORMAT_CLASS_RAWIMAGE:
903 out_string = g_string_append(out_string, " Class: RAW image");
905 case FORMAT_CLASS_META:
906 out_string = g_string_append(out_string, " Class: Metadata");
908 case FORMAT_CLASS_VIDEO:
909 out_string = g_string_append(out_string, " Class: Video");
911 case FORMAT_CLASS_COLLECTION:
912 out_string = g_string_append(out_string, " Class: Collection");
914 case FORMAT_CLASS_DOCUMENT:
915 out_string = g_string_append(out_string, " Class: Document");
917 case FORMAT_CLASS_ARCHIVE:
918 out_string = g_string_append(out_string, " Class: Archive");
920 case FORMAT_CLASS_UNKNOWN:
921 out_string = g_string_append(out_string, " Class: Unknown");
924 out_string = g_string_append(out_string, " Class: Unknown");
927 out_string = g_string_append(out_string, "\n");
931 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
932 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
934 g_string_free(out_string, TRUE);
936 file_data_unref(dir_fd);
939 static void gr_get_selection(const gchar *, GIOChannel *channel, gpointer)
941 if (!layout_valid(&lw_id)) return;
943 GList *selected = layout_selection_list(lw_id); // Keep copy to free.
944 GString *out_string = g_string_new(nullptr);
946 GList *work = selected;
949 auto fd = static_cast<FileData *>(work->data);
950 g_assert(fd->magick == FD_MAGICK);
952 g_string_append_printf(out_string, "%s %s\n",
954 format_class_list[filter_file_get_class(fd->path)]);
959 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
960 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
962 filelist_free(selected);
963 g_string_free(out_string, TRUE);
966 static void gr_selection_add(const gchar *text, GIOChannel *, gpointer)
968 if (!layout_valid(&lw_id)) return;
970 FileData *fd_to_select = nullptr;
971 if (strcmp(text, "") == 0)
973 // No file specified, use current fd.
974 fd_to_select = layout_image_get_fd(lw_id);
978 // Search through the current file list for a file matching the specified path.
979 // "Match" is either a basename match or a file path match.
980 gchar *path = expand_tilde(text);
981 gchar *filename = g_path_get_basename(path);
982 gchar *slash_plus_filename = g_strdup_printf("%s%s", G_DIR_SEPARATOR_S, filename);
984 GList *file_list = layout_list(lw_id);
985 for (GList *work = file_list; work && !fd_to_select; work = work->next)
987 auto fd = static_cast<FileData *>(work->data);
988 if (!strcmp(path, fd->path) || g_str_has_suffix(fd->path, slash_plus_filename))
990 fd_to_select = file_data_ref(fd);
991 continue; // will exit loop.
994 for (GList *sidecar = fd->sidecar_files; sidecar && !fd_to_select; sidecar = sidecar->next)
996 auto side_fd = static_cast<FileData *>(sidecar->data);
997 if (!strcmp(path, side_fd->path)
998 || g_str_has_suffix(side_fd->path, slash_plus_filename))
1000 fd_to_select = file_data_ref(side_fd);
1001 continue; // will exit both nested loops.
1008 log_printf("remote sent --selection-add filename that could not be found: \"%s\"\n",
1012 filelist_free(file_list);
1013 g_free(slash_plus_filename);
1020 GList *to_select = g_list_append(nullptr, fd_to_select);
1021 // Using the "_list" variant doesn't clear the existing selection.
1022 layout_select_list(lw_id, to_select);
1023 filelist_free(to_select);
1027 static void gr_selection_clear(const gchar *, GIOChannel *, gpointer)
1029 layout_select_none(lw_id); // Checks lw_id validity internally.
1032 static void gr_selection_remove(const gchar *text, GIOChannel *, gpointer)
1034 if (!layout_valid(&lw_id)) return;
1036 GList *selected = layout_selection_list(lw_id); // Keep copy to free.
1039 log_printf("remote sent --selection-remove with empty selection.");
1043 FileData *fd_to_deselect = nullptr;
1044 gchar *path = nullptr;
1045 gchar *filename = nullptr;
1046 gchar *slash_plus_filename = nullptr;
1047 if (strcmp(text, "") == 0)
1049 // No file specified, use current fd.
1050 fd_to_deselect = layout_image_get_fd(lw_id);
1051 if (!fd_to_deselect)
1053 log_printf("remote sent \"--selection-remove:\" with no current image");
1054 filelist_free(selected);
1060 // Search through the selection list for a file matching the specified path.
1061 // "Match" is either a basename match or a file path match.
1062 path = expand_tilde(text);
1063 filename = g_path_get_basename(path);
1064 slash_plus_filename = g_strdup_printf("%s%s", G_DIR_SEPARATOR_S, filename);
1067 GList *prior_link = nullptr; // Stash base for link removal to avoid a second traversal.
1068 GList *link_to_remove = nullptr;
1069 for (GList *work = selected; work; prior_link = work, work = work->next)
1071 auto fd = static_cast<FileData *>(work->data);
1074 if (fd == fd_to_deselect)
1076 link_to_remove = work;
1082 // path, filename, and slash_plus_filename should be defined.
1084 if (!strcmp(path, fd->path) || g_str_has_suffix(fd->path, slash_plus_filename))
1086 link_to_remove = work;
1092 if (!link_to_remove)
1096 log_printf("remote sent \"--selection-remove:\" but current image is not selected");
1100 log_printf("remote sent \"--selection-remove:%s\" but that filename is not selected",
1106 if (link_to_remove == selected)
1108 // Remove first link.
1109 selected = g_list_remove_link(selected, link_to_remove);
1110 filelist_free(link_to_remove);
1111 link_to_remove = nullptr;
1115 // Remove a subsequent link.
1116 prior_link = g_list_remove_link(prior_link, link_to_remove);
1117 filelist_free(link_to_remove);
1118 link_to_remove = nullptr;
1121 // Re-select all but the deselected item.
1122 layout_select_none(lw_id);
1123 layout_select_list(lw_id, selected);
1126 filelist_free(selected);
1127 file_data_unref(fd_to_deselect);
1128 g_free(slash_plus_filename);
1133 static void gr_collection(const gchar *text, GIOChannel *channel, gpointer)
1135 GString *contents = g_string_new(nullptr);
1137 if (is_collection(text))
1139 collection_contents(text, &contents);
1146 g_io_channel_write_chars(channel, contents->str, -1, nullptr, nullptr);
1147 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1149 g_string_free(contents, TRUE);
1152 static void gr_collection_list(const gchar *, GIOChannel *channel, gpointer)
1155 GList *collection_list = nullptr;
1157 GString *out_string = g_string_new(nullptr);
1159 collect_manager_list(&collection_list, nullptr, nullptr);
1161 work = collection_list;
1164 auto collection_name = static_cast<const gchar *>(work->data);
1165 out_string = g_string_append(out_string, collection_name);
1166 out_string = g_string_append(out_string, "\n");
1171 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
1172 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1174 g_list_free_full(collection_list, g_free);
1175 g_string_free(out_string, TRUE);
1178 static gboolean wait_cb(gpointer data)
1180 gint position = GPOINTER_TO_INT(data);
1181 gint x = position >> 16;
1182 gint y = position - (x << 16);
1184 gq_gtk_window_move(GTK_WINDOW(lw_id->window), x, y);
1186 return G_SOURCE_REMOVE;
1189 static void gr_geometry(const gchar *text, GIOChannel *, gpointer)
1193 if (!layout_valid(&lw_id) || !text)
1200 geometry = g_strsplit_set(text, "+", 3);
1201 if (geometry[1] != nullptr && geometry[2] != nullptr )
1203 gq_gtk_window_move(GTK_WINDOW(lw_id->window), atoi(geometry[1]), atoi(geometry[2]));
1208 geometry = g_strsplit_set(text, "+x", 4);
1209 if (geometry[0] != nullptr && geometry[1] != nullptr)
1211 gtk_window_resize(GTK_WINDOW(lw_id->window), atoi(geometry[0]), atoi(geometry[1]));
1213 if (geometry[2] != nullptr && geometry[3] != nullptr)
1215 /* There is an occasional problem with a window_move immediately after a window_resize */
1216 g_idle_add(wait_cb, GINT_TO_POINTER((atoi(geometry[2]) << 16) + atoi(geometry[3])));
1219 g_strfreev(geometry);
1222 static void gr_filelist(const gchar *text, GIOChannel *channel, gpointer)
1224 get_filelist(text, channel, FALSE);
1227 static void gr_filelist_recurse(const gchar *text, GIOChannel *channel, gpointer)
1229 get_filelist(text, channel, TRUE);
1232 static void gr_file_tell(const gchar *, GIOChannel *channel, gpointer)
1235 gchar *collection_name = nullptr;
1237 if (!layout_valid(&lw_id)) return;
1239 if (image_get_path(lw_id->image))
1241 if (lw_id->image->collection && lw_id->image->collection->name)
1243 collection_name = remove_extension_from_path(lw_id->image->collection->name);
1244 out_string = g_strconcat(image_get_path(lw_id->image), " Collection: ", collection_name, NULL);
1248 out_string = g_strconcat(image_get_path(lw_id->image), NULL);
1253 out_string = g_strconcat(lw_id->dir_fd->path, G_DIR_SEPARATOR_S, NULL);
1256 g_io_channel_write_chars(channel, out_string, -1, nullptr, nullptr);
1257 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1259 g_free(collection_name);
1263 static void gr_file_info(const gchar *, GIOChannel *channel, gpointer)
1267 gchar *country_name;
1268 gchar *country_code;
1271 GString *out_string;
1272 FileFormatClass format_class;
1274 if (!layout_valid(&lw_id)) return;
1276 if (image_get_path(lw_id->image))
1278 filename = g_strdup(image_get_path(lw_id->image));
1279 fd = file_data_new_group(filename);
1280 out_string = g_string_new(nullptr);
1284 format_class = filter_file_get_class(image_get_path(lw_id->image));
1288 format_class = FORMAT_CLASS_UNKNOWN;
1291 g_string_append_printf(out_string, _("Class: %s\n"), format_class_list[format_class]);
1293 if (fd->page_total > 1)
1295 g_string_append_printf(out_string, _("Page no: %d/%d\n"), fd->page_num + 1, fd->page_total);
1300 country_name = exif_get_data_as_text(fd->exif, "formatted.countryname");
1303 g_string_append_printf(out_string, _("Country name: %s\n"), country_name);
1304 g_free(country_name);
1307 country_code = exif_get_data_as_text(fd->exif, "formatted.countrycode");
1310 g_string_append_printf(out_string, _("Country code: %s\n"), country_code);
1311 g_free(country_code);
1314 timezone = exif_get_data_as_text(fd->exif, "formatted.timezone");
1317 g_string_append_printf(out_string, _("Timezone: %s\n"), timezone);
1321 local_time = exif_get_data_as_text(fd->exif, "formatted.localtime");
1324 g_string_append_printf(out_string, ("Local time: %s\n"), local_time);
1329 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
1330 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1332 g_string_free(out_string, TRUE);
1333 file_data_unref(fd);
1338 static gchar *config_file_path(const gchar *param)
1340 gchar *path = nullptr;
1341 gchar *full_name = nullptr;
1343 if (file_extension_match(param, ".xml"))
1345 path = g_build_filename(get_window_layouts_dir(), param, NULL);
1347 else if (file_extension_match(param, nullptr))
1349 full_name = g_strconcat(param, ".xml", NULL);
1350 path = g_build_filename(get_window_layouts_dir(), full_name, NULL);
1363 static gboolean is_config_file(const gchar *param)
1365 gchar *name = nullptr;
1367 name = config_file_path(param);
1376 static void gr_config_load(const gchar *text, GIOChannel *, gpointer)
1378 gchar *filename = expand_tilde(text);
1380 if (!g_strstr_len(filename, -1, G_DIR_SEPARATOR_S))
1382 if (is_config_file(filename))
1384 gchar *tmp = config_file_path(filename);
1390 if (isfile(filename))
1392 load_config_from_file(filename, FALSE);
1396 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
1397 layout_set_path(nullptr, homedir());
1403 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer)
1405 gchar *filename = expand_tilde(text);
1406 FileData *fd = file_data_new_group(filename);
1409 if (fd->parent) fd = fd->parent;
1411 g_io_channel_write_chars(channel, fd->path, -1, nullptr, nullptr);
1412 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1414 work = fd->sidecar_files;
1418 fd = static_cast<FileData *>(work->data);
1420 g_io_channel_write_chars(channel, fd->path, -1, nullptr, nullptr);
1421 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1426 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer)
1428 gchar *filename = expand_tilde(text);
1429 FileData *fd = file_data_new_group(filename);
1431 if (fd->change && fd->change->dest)
1433 g_io_channel_write_chars(channel, fd->change->dest, -1, nullptr, nullptr);
1434 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1439 static void gr_file_view(const gchar *text, GIOChannel *, gpointer)
1442 gchar *tilde_filename = expand_tilde(text);
1444 filename = set_pwd(tilde_filename);
1446 view_window_new(file_data_new_group(filename));
1448 g_free(tilde_filename);
1451 static void gr_list_clear(const gchar *, GIOChannel *, gpointer data)
1453 auto remote_data = static_cast<RemoteData *>(data);
1455 remote_data->command_collection = nullptr;
1456 remote_data->file_list = nullptr;
1457 remote_data->single_dir = TRUE;
1460 static void gr_list_add(const gchar *text, GIOChannel *, gpointer data)
1462 auto remote_data = static_cast<RemoteData *>(data);
1463 gboolean is_new = TRUE;
1464 gchar *path = nullptr;
1468 /** @FIXME Should check if file is in current dir, has tilde or is relative */
1471 log_printf("Warning: File does not exist --remote --list-add:%s", text);
1476 /* If there is a files list on the command line
1477 * check if they are all in the same folder
1479 if (remote_data->single_dir)
1482 work = remote_data->file_list;
1483 while (work && remote_data->single_dir)
1486 dirname = g_path_get_dirname((static_cast<FileData *>(work->data))->path);
1489 path = g_strdup(dirname);
1493 if (g_strcmp0(path, dirname) != 0)
1495 remote_data->single_dir = FALSE;
1504 gchar *pathname = g_path_get_dirname(text);
1505 layout_set_path(lw_id, pathname);
1508 fd = file_data_new_simple(text);
1509 remote_data->file_list = g_list_append(remote_data->file_list, fd);
1510 file_data_unref(fd);
1512 vf_select_none(lw_id->vf);
1513 remote_data->file_list = g_list_reverse(remote_data->file_list);
1515 layout_select_list(lw_id, remote_data->file_list);
1516 layout_refresh(lw_id);
1517 first = static_cast<FileData *>(g_list_first(vf_selection_get_list(lw_id->vf))->data);
1518 layout_set_fd(lw_id, first);
1523 if (!remote_data->command_collection && !remote_data->single_dir)
1525 cw = collection_window_new(nullptr);
1528 collection_path_changed(cd);
1530 remote_data->command_collection = cd;
1532 else if (!remote_data->single_dir)
1534 is_new = (!collection_get_first(remote_data->command_collection));
1537 if (!remote_data->single_dir)
1539 layout_image_set_collection(lw_id, remote_data->command_collection, collection_get_first(remote_data->command_collection));
1540 if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && is_new)
1542 layout_image_set_collection(lw_id, remote_data->command_collection, collection_get_first(remote_data->command_collection));
1547 static void gr_action(const gchar *text, GIOChannel *, gpointer)
1551 if (!layout_valid(&lw_id))
1556 if (g_strstr_len(text, -1, ".desktop") != nullptr)
1558 file_util_start_editor_from_filelist(text, layout_selection_list(lw_id), layout_get_path(lw_id), lw_id->window);
1562 action = gtk_action_group_get_action(lw_id->action_group, text);
1565 gtk_action_activate(action);
1569 log_printf("Action %s unknown", text);
1574 static void gr_action_list(const gchar *, GIOChannel *channel, gpointer)
1576 ActionItem *action_item;
1577 gint max_length = 0;
1578 GList *list = nullptr;
1579 GString *out_string = g_string_new(nullptr);
1581 if (!layout_valid(&lw_id))
1586 list = get_action_items();
1588 /* Get the length required for padding */
1589 for (GList *work = list; work; work = work->next)
1591 action_item = static_cast<ActionItem *>(work->data);
1592 const auto length = g_utf8_strlen(action_item->name, -1);
1593 max_length = MAX(length, max_length);
1596 /* Pad the action names to the same column for readable output */
1597 for (GList *work = list; work; work = work->next)
1599 action_item = static_cast<ActionItem *>(work->data);
1601 g_string_append_printf(out_string, "%-*s", max_length + 4, action_item->name);
1602 out_string = g_string_append(out_string, action_item->label);
1603 out_string = g_string_append(out_string, "\n");
1606 action_items_free(list);
1608 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
1609 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1611 g_string_free(out_string, TRUE);
1614 static void gr_raise(const gchar *, GIOChannel *, gpointer)
1616 if (layout_valid(&lw_id))
1618 gtk_window_present(GTK_WINDOW(lw_id->window));
1622 static void gr_pwd(const gchar *text, GIOChannel *, gpointer)
1624 LayoutWindow *lw = nullptr;
1629 pwd = g_strdup(text);
1633 static void gr_print0(const gchar *, GIOChannel *channel, gpointer)
1635 g_io_channel_write_chars(channel, "print0", -1, nullptr, nullptr);
1636 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1640 static void gr_lua(const gchar *text, GIOChannel *channel, gpointer)
1642 gchar *result = nullptr;
1643 gchar **lua_command;
1645 lua_command = g_strsplit(text, ",", 2);
1647 if (lua_command[0] && lua_command[1])
1649 FileData *fd = file_data_new_group(lua_command[0]);
1650 result = g_strdup(lua_callvalue(fd, lua_command[1], nullptr));
1653 g_io_channel_write_chars(channel, result, -1, nullptr, nullptr);
1657 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, nullptr, nullptr);
1662 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, nullptr, nullptr);
1665 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1667 g_strfreev(lua_command);
1672 struct RemoteCommandEntry {
1675 void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
1676 gboolean needs_extra;
1677 gboolean prefer_command_line;
1678 const gchar *parameter;
1679 const gchar *description;
1682 static RemoteCommandEntry remote_commands[] = {
1683 /* short, long callback, extra, prefer, parameter, description */
1684 { nullptr, "--action:", gr_action, TRUE, FALSE, N_("<ACTION>"), N_("execute keyboard action (See Help/Reference/Remote Keyboard Actions)") },
1685 { nullptr, "--action-list", gr_action_list, FALSE, FALSE, nullptr, N_("list available keyboard actions (some are redundant)") },
1686 { "-b", "--back", gr_image_prev, FALSE, FALSE, nullptr, N_("previous image") },
1687 { nullptr, "--close-window", gr_close_window, FALSE, FALSE, nullptr, N_("close window") },
1688 { nullptr, "--config-load:", gr_config_load, TRUE, FALSE, N_("<FILE>|layout ID"), N_("load configuration from FILE") },
1689 { "-cm","--cache-metadata", gr_cache_metadata, FALSE, FALSE, nullptr, N_("clean the metadata cache") },
1690 { "-cr:", "--cache-render:", gr_cache_render, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails") },
1691 { "-crr:", "--cache-render-recurse:", gr_cache_render_recurse, TRUE, FALSE, N_("<folder> "), N_("render thumbnails recursively") },
1692 { "-crs:", "--cache-render-shared:", gr_cache_render_standard, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails (see Help)") },
1693 { "-crsr:", "--cache-render-shared-recurse:", gr_cache_render_standard_recurse, TRUE, FALSE, N_("<folder>"), N_(" render thumbnails recursively (see Help)") },
1694 { "-cs:", "--cache-shared:", gr_cache_shared, TRUE, FALSE, N_("clear|clean"), N_("clear or clean shared thumbnail cache") },
1695 { "-ct:", "--cache-thumbs:", gr_cache_thumb, TRUE, FALSE, N_("clear|clean"), N_("clear or clean thumbnail cache") },
1696 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("<[H:][M:][N][.M]>"), N_("set slide show delay to Hrs Mins N.M seconds") },
1697 { nullptr, "--first", gr_image_first, FALSE, FALSE, nullptr, N_("first image") },
1698 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, nullptr, N_("toggle full screen") },
1699 { nullptr, "--file:", gr_file_load, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, bring Geeqie window to the top") },
1700 { nullptr, "file:", gr_file_load, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, bring Geeqie window to the top") },
1701 { 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") },
1702 { 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") },
1703 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, nullptr, N_("start full screen") },
1704 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, nullptr, N_("stop full screen") },
1705 { nullptr, "--geometry=", gr_geometry, TRUE, FALSE, N_("<GEOMETRY>"), N_("set window geometry") },
1706 { nullptr, "--get-collection:", gr_collection, TRUE, FALSE, N_("<COLLECTION>"), N_("get collection content") },
1707 { nullptr, "--get-collection-list", gr_collection_list, FALSE, FALSE, nullptr, N_("get collection list") },
1708 { nullptr, "--get-destination:", gr_get_destination, TRUE, FALSE, N_("<FILE>"), N_("get destination path of FILE (See Plugins Configuration)") },
1709 { nullptr, "--get-file-info", gr_file_info, FALSE, FALSE, nullptr, N_("get file info") },
1710 { nullptr, "--get-filelist:", gr_filelist, TRUE, FALSE, N_("[<FOLDER>]"), N_("get list of files and class") },
1711 { nullptr, "--get-filelist-recurse:", gr_filelist_recurse, TRUE, FALSE, N_("[<FOLDER>]"), N_("get list of files and class recursive") },
1712 { nullptr, "--get-rectangle", gr_rectangle, FALSE, FALSE, nullptr, N_("get rectangle co-ordinates") },
1713 { nullptr, "--get-render-intent", gr_render_intent, FALSE, FALSE, nullptr, N_("get render intent") },
1714 { nullptr, "--get-selection", gr_get_selection, FALSE, FALSE, nullptr, N_("get list of selected files") },
1715 { nullptr, "--get-sidecars:", gr_get_sidecars, TRUE, FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
1716 { nullptr, "--id:", gr_lw_id, TRUE, FALSE, N_("<ID>"), N_("window id for following commands") },
1717 { nullptr, "--last", gr_image_last, FALSE, FALSE, nullptr, N_("last image") },
1718 { nullptr, "--list-add:", gr_list_add, TRUE, FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
1719 { nullptr, "--list-clear", gr_list_clear, FALSE, FALSE, nullptr, N_("clear command line collection list") },
1721 { nullptr, "--lua:", gr_lua, TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
1723 { nullptr, "--new-window", gr_new_window, FALSE, FALSE, nullptr, N_("new window") },
1724 { "-n", "--next", gr_image_next, FALSE, FALSE, nullptr, N_("next image") },
1725 { nullptr, "--pixel-info", gr_pixel_info, FALSE, FALSE, nullptr, N_("print pixel info of mouse pointer on current image") },
1726 { nullptr, "--print0", gr_print0, TRUE, FALSE, nullptr, N_("terminate returned data with null character instead of newline") },
1727 { nullptr, "--PWD:", gr_pwd, TRUE, FALSE, N_("<PWD>"), N_("use PWD as working directory for following commands") },
1728 { "-q", "--quit", gr_quit, FALSE, FALSE, nullptr, N_("quit") },
1729 { nullptr, "--raise", gr_raise, FALSE, FALSE, nullptr, N_("bring the Geeqie window to the top") },
1730 { nullptr, "raise", gr_raise, FALSE, FALSE, nullptr, N_("bring the Geeqie window to the top") },
1731 { nullptr, "--selection-add:", gr_selection_add, TRUE, FALSE, N_("[<FILE>]"), N_("adds the current file (or the specified file) to the current selection") },
1732 { nullptr, "--selection-clear", gr_selection_clear, FALSE, FALSE, nullptr, N_("clears the current selection") },
1733 { nullptr, "--selection-remove:", gr_selection_remove, TRUE, FALSE, N_("[<FILE>]"), N_("removes the current file (or the specified file) from the current selection") },
1734 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, nullptr, N_("toggle slide show") },
1735 { nullptr, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE, FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
1736 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, nullptr, N_("start slide show") },
1737 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, nullptr, N_("stop slide show") },
1738 { nullptr, "--tell", gr_file_tell, FALSE, FALSE, nullptr, N_("print filename [and Collection] of current image") },
1739 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, nullptr, N_("show tools") },
1740 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, nullptr, N_("hide tools") },
1741 { nullptr, "--view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
1742 { nullptr, "view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
1743 { nullptr, nullptr, nullptr, FALSE, FALSE, nullptr, nullptr }
1746 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
1748 gboolean match = FALSE;
1752 while (!match && remote_commands[i].func != nullptr)
1754 if (remote_commands[i].needs_extra)
1756 if (remote_commands[i].opt_s &&
1757 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
1759 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
1760 return &remote_commands[i];
1763 if (remote_commands[i].opt_l &&
1764 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
1766 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
1767 return &remote_commands[i];
1772 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
1773 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
1775 if (offset) *offset = text;
1776 return &remote_commands[i];
1786 static void remote_cb(RemoteConnection *, const gchar *text, GIOChannel *channel, gpointer data)
1788 RemoteCommandEntry *entry;
1789 const gchar *offset;
1791 entry = remote_command_find(text, &offset);
1792 if (entry && entry->func)
1794 entry->func(offset, channel, data);
1798 log_printf("unknown remote command:%s\n", text);
1808 print_term(FALSE, _("Remote command list:\n"));
1811 while (remote_commands[i].func != nullptr)
1813 if (remote_commands[i].description)
1815 s_opt_param = g_strdup(remote_commands[i].opt_s ? remote_commands[i].opt_s : "" );
1816 l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
1818 if (g_str_has_prefix(l_opt_param, "--"))
1820 printf_term(FALSE, " %-4s %-40s%-s\n", s_opt_param, l_opt_param, _(remote_commands[i].description));
1822 g_free(s_opt_param);
1823 g_free(l_opt_param);
1827 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"));
1830 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
1837 RemoteCommandEntry *entry;
1839 entry = remote_command_find(argv[i], nullptr);
1842 list = g_list_append(list, argv[i]);
1844 else if (errors && !isname(argv[i]))
1846 *errors = g_list_append(*errors, argv[i]);
1855 * @param arg_exec Binary (argv0)
1856 * @param remote_list Evaluated and recognized remote commands
1857 * @param path The current path
1858 * @param cmd_list List of all non collections in Path (gchar *path)
1859 * @param collection_list List of all collections in argv
1861 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
1862 GList *cmd_list, GList *collection_list)
1864 RemoteConnection *rc;
1865 gboolean started = FALSE;
1868 buf = g_build_filename(get_rc_dir(), ".command", NULL);
1869 rc = remote_client_open(buf);
1874 gint retry_count = 12;
1875 gboolean blank = FALSE;
1877 printf_term(FALSE, _("Remote %s not running, starting..."), GQ_APPNAME);
1879 command = g_string_new(arg_exec);
1885 RemoteCommandEntry *entry;
1887 text = static_cast<gchar *>(work->data);
1890 entry = remote_command_find(text, nullptr);
1893 /* If Geeqie is not running, stop the --new-window command opening a second window */
1894 if (g_strcmp0(text, "--new-window") == 0)
1896 remote_list = g_list_remove(remote_list, text);
1898 if (entry->prefer_command_line)
1900 remote_list = g_list_remove(remote_list, text);
1901 g_string_append(command, " ");
1902 g_string_append(command, text);
1904 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
1911 if (blank || cmd_list || path) g_string_append(command, " --blank");
1912 if (get_debug_level()) g_string_append(command, " --debug");
1914 g_string_append(command, " &");
1915 runcmd(command->str);
1916 g_string_free(command, TRUE);
1918 while (!rc && retry_count > 0)
1920 usleep((retry_count > 10) ? 500000 : 1000000);
1921 rc = remote_client_open(buf);
1922 if (!rc) print_term(FALSE, ".");
1926 print_term(FALSE, "\n");
1935 const gchar *prefix;
1936 gboolean use_path = TRUE;
1937 gboolean sent = FALSE;
1943 RemoteCommandEntry *entry;
1945 text = static_cast<gchar *>(work->data);
1948 entry = remote_command_find(text, nullptr);
1951 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
1953 remote_client_send(rc, text);
1958 if (cmd_list && cmd_list->next)
1960 prefix = "--list-add:";
1961 remote_client_send(rc, "--list-clear");
1973 text = g_strconcat(prefix, work->data, NULL);
1974 remote_client_send(rc, text);
1981 if (path && !cmd_list && use_path)
1985 text = g_strdup_printf("file:%s", path);
1986 remote_client_send(rc, text);
1992 work = collection_list;
1998 name = static_cast<const gchar *>(work->data);
2001 text = g_strdup_printf("file:%s", name);
2002 remote_client_send(rc, text);
2008 if (!started && !sent)
2010 remote_client_send(rc, "raise");
2015 print_term(TRUE, _("Remote not available\n"));
2021 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
2023 RemoteConnection *remote_connection = remote_server_open(path);
2024 auto remote_data = g_new(RemoteData, 1);
2026 remote_data->command_collection = command_collection;
2028 remote_server_subscribe(remote_connection, remote_cb, remote_data);
2029 return remote_connection;
2031 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */