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"
29 #include "filefilter.h"
33 #include "layout_image.h"
34 #include "layout_util.h"
36 #include "pixbuf-renderer.h"
37 #include "slideshow.h"
38 #include "ui_fileops.h"
41 #include <sys/socket.h>
48 #define SERVER_MAX_CLIENTS 8
50 #define REMOTE_SERVER_BACKLOG 4
54 #define UNIX_PATH_MAX 108
58 static RemoteConnection *remote_client_open(const gchar *path);
59 static gint remote_client_send(RemoteConnection *rc, const gchar *text);
60 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data);
62 static LayoutWindow *lw_id = NULL; /* points to the window set by the --id option */
64 typedef struct _RemoteClient RemoteClient;
65 struct _RemoteClient {
67 guint channel_id; /* event source id */
71 typedef struct _RemoteData RemoteData;
73 CollectionData *command_collection;
76 /* Remote commands from main.c are prepended with the current dir the remote
77 * command was made from. Some remote commands require this. The
78 * value is stored here
80 static gchar *pwd = NULL;
83 * @brief Ensures file path is absolute.
84 * @param[in] filename Filepath, absolute or relative to calling directory
85 * @returns absolute path
87 * If first character of input filepath is not the directory
88 * separator, assume it as a relative path and prepend
89 * the directory the remote command was initiated from
91 * Return value must be freed with g_free()
93 static gchar *set_pwd(gchar *filename)
97 if (strncmp(filename, G_DIR_SEPARATOR_S, 1) != 0)
99 temp = g_build_filename(pwd, filename, NULL);
103 temp = g_strdup(filename);
109 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
111 RemoteClient *client = data;
112 RemoteConnection *rc;
113 GIOStatus status = G_IO_STATUS_NORMAL;
118 if (condition & G_IO_IN)
120 gchar *buffer = NULL;
121 GError *error = NULL;
124 while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
128 buffer[termpos] = '\0';
130 if (strlen(buffer) > 0)
132 if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
133 g_io_channel_write_chars(source, "\n", -1, NULL, NULL); /* empty line finishes the command */
134 g_io_channel_flush(source, NULL);
144 log_printf("error reading socket: %s\n", error->message);
149 if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
151 rc->clients = g_list_remove(rc->clients, client);
153 DEBUG_1("HUP detected, closing client.");
154 DEBUG_1("client count %d", g_list_length(rc->clients));
156 g_source_remove(client->channel_id);
164 static void remote_server_client_add(RemoteConnection *rc, gint fd)
166 RemoteClient *client;
169 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
171 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
176 client = g_new0(RemoteClient, 1);
180 channel = g_io_channel_unix_new(fd);
181 client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP,
182 remote_server_client_cb, client, NULL);
183 g_io_channel_unref(channel);
185 rc->clients = g_list_append(rc->clients, client);
186 DEBUG_1("client count %d", g_list_length(rc->clients));
189 static void remote_server_clients_close(RemoteConnection *rc)
193 RemoteClient *client = rc->clients->data;
195 rc->clients = g_list_remove(rc->clients, client);
197 g_source_remove(client->channel_id);
203 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
205 RemoteConnection *rc = data;
209 fd = accept(rc->fd, NULL, &alen);
212 log_printf("error accepting socket: %s\n", strerror(errno));
216 remote_server_client_add(rc, fd);
221 gboolean remote_server_exists(const gchar *path)
223 RemoteConnection *rc;
225 /* verify server up */
226 rc = remote_client_open(path);
231 /* unable to connect, remove socket file to free up address */
236 static RemoteConnection *remote_server_open(const gchar *path)
238 RemoteConnection *rc;
239 struct sockaddr_un addr;
244 if (remote_server_exists(path))
246 log_printf("Address already in use: %s\n", path);
250 fd = socket(PF_UNIX, SOCK_STREAM, 0);
251 if (fd == -1) return NULL;
253 addr.sun_family = AF_UNIX;
254 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
255 strncpy(addr.sun_path, path, sun_path_len);
256 if (bind(fd, &addr, sizeof(addr)) == -1 ||
257 listen(fd, REMOTE_SERVER_BACKLOG) == -1)
259 log_printf("error subscribing to socket: %s\n", strerror(errno));
264 rc = g_new0(RemoteConnection, 1);
268 rc->path = g_strdup(path);
270 channel = g_io_channel_unix_new(rc->fd);
271 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
273 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
274 remote_server_read_cb, rc, NULL);
275 g_io_channel_unref(channel);
280 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
282 if (!rc || !rc->server) return;
284 rc->read_func = func;
285 rc->read_data = data;
289 static RemoteConnection *remote_client_open(const gchar *path)
291 RemoteConnection *rc;
293 struct sockaddr_un addr;
297 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
299 fd = socket(PF_UNIX, SOCK_STREAM, 0);
300 if (fd == -1) return NULL;
302 addr.sun_family = AF_UNIX;
303 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
304 strncpy(addr.sun_path, path, sun_path_len);
305 if (connect(fd, &addr, sizeof(addr)) == -1)
307 DEBUG_1("error connecting to socket: %s", strerror(errno));
312 rc = g_new0(RemoteConnection, 1);
315 rc->path = g_strdup(path);
320 static sig_atomic_t sigpipe_occurred = FALSE;
322 static void sighandler_sigpipe(gint sig)
324 sigpipe_occurred = TRUE;
327 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
329 struct sigaction new_action, old_action;
330 gboolean ret = FALSE;
331 GError *error = NULL;
334 if (!rc || rc->server) return FALSE;
335 if (!text) return TRUE;
337 sigpipe_occurred = FALSE;
339 new_action.sa_handler = sighandler_sigpipe;
340 sigemptyset(&new_action.sa_mask);
341 new_action.sa_flags = 0;
343 /* setup our signal handler */
344 sigaction(SIGPIPE, &new_action, &old_action);
346 channel = g_io_channel_unix_new(rc->fd);
348 g_io_channel_write_chars(channel, text, -1, NULL, &error);
349 g_io_channel_write_chars(channel, "\n", -1, NULL, &error);
350 g_io_channel_flush(channel, &error);
354 log_printf("error reading socket: %s\n", error->message);
365 gchar *buffer = NULL;
367 while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
371 if (buffer[0] == '\n') /* empty line finishes the command */
377 buffer[termpos] = '\0';
378 printf("%s\n", buffer);
386 log_printf("error reading socket: %s\n", error->message);
393 /* restore the original signal handler */
394 sigaction(SIGPIPE, &old_action, NULL);
395 g_io_channel_unref(channel);
399 void remote_close(RemoteConnection *rc)
405 remote_server_clients_close(rc);
407 g_source_remove(rc->channel_id);
412 g_free(rc->read_data);
421 *-----------------------------------------------------------------------------
423 *-----------------------------------------------------------------------------
426 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data)
428 layout_image_next(lw_id);
431 static void gr_new_window(const gchar *text, GIOChannel *channel, gpointer data)
433 LayoutWindow *lw = NULL;
435 if (!layout_valid(&lw)) return;
437 lw_id = layout_menu_new_window(NULL, lw);
440 static gboolean gr_close_window_cb()
442 if (!layout_valid(&lw_id)) return FALSE;
444 layout_menu_close_cb(NULL, lw_id);
449 static void gr_close_window(const gchar *text, GIOChannel *channel, gpointer data)
451 g_idle_add(gr_close_window_cb, NULL);
454 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data)
456 layout_image_prev(lw_id);
459 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data)
461 layout_image_first(lw_id);
464 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data)
466 layout_image_last(lw_id);
469 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data)
471 layout_image_full_screen_toggle(lw_id);
474 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data)
476 layout_image_full_screen_start(lw_id);
479 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data)
481 layout_image_full_screen_stop(lw_id);
484 static void gr_lw_id(const gchar *text, GIOChannel *channel, gpointer data)
486 lw_id = layout_find_by_layout_id(text);
489 log_printf("remote sent window ID that does not exist:\"%s\"\n",text);
491 layout_valid(&lw_id);
494 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *channel, gpointer data)
497 FileData *dir_fd = file_data_new_dir(text);
499 layout_valid(&lw_id);
500 list = filelist_recursive_full(dir_fd, lw_id->sort_method, lw_id->sort_ascend);
501 file_data_unref(dir_fd);
503 //printf("length: %d\n", g_list_length(list));
504 layout_image_slideshow_stop(lw_id);
505 layout_image_slideshow_start_from_list(lw_id, list);
508 static void gr_cache_thumb(const gchar *text, GIOChannel *channel, gpointer data)
510 if (!g_strcmp0(text, "clear"))
511 cache_maintain_home_remote(FALSE, TRUE);
512 else if (!g_strcmp0(text, "clean"))
513 cache_maintain_home_remote(FALSE, FALSE);
516 static void gr_cache_shared(const gchar *text, GIOChannel *channel, gpointer data)
518 if (!g_strcmp0(text, "clear"))
519 cache_manager_standard_process_remote(TRUE);
520 else if (!g_strcmp0(text, "clean"))
521 cache_manager_standard_process_remote(FALSE);
524 static void gr_cache_metadata(const gchar *text, GIOChannel *channel, gpointer data)
526 cache_maintain_home_remote(TRUE, FALSE);
529 static void gr_cache_render(const gchar *text, GIOChannel *channel, gpointer data)
531 cache_manager_render_remote(text, FALSE, FALSE);
534 static void gr_cache_render_recurse(const gchar *text, GIOChannel *channel, gpointer data)
536 cache_manager_render_remote(text, TRUE, FALSE);
539 static void gr_cache_render_standard(const gchar *text, GIOChannel *channel, gpointer data)
541 if(options->thumbnails.spec_standard)
542 cache_manager_render_remote(text, FALSE, TRUE);
545 static void gr_cache_render_standard_recurse(const gchar *text, GIOChannel *channel, gpointer data)
547 if(options->thumbnails.spec_standard)
548 cache_manager_render_remote(text, TRUE, TRUE);
551 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data)
553 layout_image_slideshow_toggle(lw_id);
556 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data)
558 layout_image_slideshow_start(lw_id);
561 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data)
563 layout_image_slideshow_stop(lw_id);
566 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data)
568 gdouble t1, t2, t3, n;
571 res = sscanf(text, "%lf:%lf:%lf", &t1, &t2, &t3);
574 n = (t1 * 3600) + (t2 * 60) + t3;
575 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
576 t1 >= 24 || t2 >= 60 || t3 >= 60)
578 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
579 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
586 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
587 t1 >= 60 || t2 >= 60)
589 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
590 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
597 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
599 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
600 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
609 options->slideshow.delay = (gint)(n * 10.0 + 0.01);
612 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data)
617 if (layout_tools_float_get(lw_id, &popped, &hidden) && hidden)
619 layout_tools_float_set(lw_id, popped, FALSE);
623 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data)
628 if (layout_tools_float_get(lw_id, &popped, &hidden) && !hidden)
630 layout_tools_float_set(lw_id, popped, TRUE);
634 static gboolean gr_quit_idle_cb(gpointer data)
641 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data)
643 /* schedule exit when idle, if done from within a
644 * remote handler remote_close will crash
646 g_idle_add(gr_quit_idle_cb, NULL);
649 static void gr_file_load_no_raise(const gchar *text, GIOChannel *channel, gpointer data)
652 gchar *tilde_filename = expand_tilde(text);
654 filename = set_pwd(tilde_filename);
656 if (isfile(filename))
658 if (file_extension_match(filename, GQ_COLLECTION_EXT))
660 collection_window_new(filename);
664 layout_set_path(lw_id, filename);
667 else if (isdir(filename))
669 layout_set_path(lw_id, filename);
673 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
674 layout_set_path(lw_id, homedir());
678 g_free(tilde_filename);
681 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
683 gr_file_load_no_raise(text, channel, data);
685 gr_raise(text, channel, data);
688 static void gr_pixel_info(const gchar *text, GIOChannel *channel, gpointer data)
691 gint x_pixel, y_pixel;
693 gint r_mouse, g_mouse, b_mouse;
695 LayoutWindow *lw = NULL;
697 if (!layout_valid(&lw_id)) return;
699 pr = (PixbufRenderer*)lw_id->image->pr;
703 pixbuf_renderer_get_image_size(pr, &width, &height);
704 if (width < 1 || height < 1) return;
706 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
708 if (x_pixel >= 0 && y_pixel >= 0)
710 pixbuf_renderer_get_pixel_colors(pr, x_pixel, y_pixel,
711 &r_mouse, &g_mouse, &b_mouse);
713 pixel_info = g_strdup_printf(_("[%d,%d]: RGB(%3d,%3d,%3d)"),
715 r_mouse, g_mouse, b_mouse);
717 g_io_channel_write_chars(channel, pixel_info, -1, NULL, NULL);
718 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
733 static void gr_rectangle(const gchar *text, GIOChannel *channel, gpointer data)
735 gchar *rectangle_info;
737 LayoutWindow *lw = NULL;
740 if (!options->draw_rectangle) return;
741 if (!layout_valid(&lw_id)) return;
743 pr = (PixbufRenderer*)lw_id->image->pr;
747 image_get_rectangle(&x1, &y1, &x2, &y2);
748 rectangle_info = g_strdup_printf(_("%dx%d+%d+%d"),
749 (x2 > x1) ? x2 - x1 : x1 - x2,
750 (y2 > y1) ? y2 - y1 : y1 - y2,
752 (y2 > y1) ? y1 : y2);
754 g_io_channel_write_chars(channel, rectangle_info, -1, NULL, NULL);
755 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
757 g_free(rectangle_info);
761 static void gr_render_intent(const gchar *text, GIOChannel *channel, gpointer data)
763 gchar *render_intent;
765 switch (options->color_profile.render_intent)
768 render_intent = g_strdup("Perceptual");
771 render_intent = g_strdup("Relative Colorimetric");
774 render_intent = g_strdup("Saturation");
777 render_intent = g_strdup("Absolute Colorimetric");
780 render_intent = g_strdup("none");
784 g_io_channel_write_chars(channel, render_intent, -1, NULL, NULL);
785 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
787 g_free(render_intent);
790 static void get_filelist(const gchar *text, GIOChannel *channel, gboolean recurse)
793 FileFormatClass class;
796 GString *out_string = g_string_new(NULL);
799 if (strcmp(text, "") == 0)
801 if (layout_valid(&lw_id))
803 dir_fd = file_data_new_dir(lw_id->dir_fd->path);
814 dir_fd = file_data_new_dir(text);
824 list = filelist_recursive(dir_fd);
828 filelist_read(dir_fd, &list, NULL);
835 g_string_append_printf(out_string, "%s", fd->path);
836 class = filter_file_get_class(fd->path);
840 case FORMAT_CLASS_IMAGE:
841 out_string = g_string_append(out_string, " Class: Image");
843 case FORMAT_CLASS_RAWIMAGE:
844 out_string = g_string_append(out_string, " Class: RAW image");
846 case FORMAT_CLASS_META:
847 out_string = g_string_append(out_string, " Class: Metadata");
849 case FORMAT_CLASS_VIDEO:
850 out_string = g_string_append(out_string, " Class: Video");
852 case FORMAT_CLASS_COLLECTION:
853 out_string = g_string_append(out_string, " Class: Collection");
855 case FORMAT_CLASS_PDF:
856 out_string = g_string_append(out_string, " Class: PDF");
858 case FORMAT_CLASS_HEIF:
859 out_string = g_string_append(out_string, " Class: HEIF");
861 case FORMAT_CLASS_UNKNOWN:
862 out_string = g_string_append(out_string, " Class: Unknown");
865 out_string = g_string_append(out_string, " Class: Unknown");
868 out_string = g_string_append(out_string, "\n");
872 g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
873 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
875 g_string_free(out_string, TRUE);
877 file_data_unref(dir_fd);
880 static void gr_collection(const gchar *text, GIOChannel *channel, gpointer data)
882 GString *contents = g_string_new(NULL);
884 if (is_collection(text))
886 collection_contents(text, &contents);
893 g_io_channel_write_chars(channel, contents->str, -1, NULL, NULL);
894 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
896 g_string_free(contents, TRUE);
899 static void gr_collection_list(const gchar *text, GIOChannel *channel, gpointer data)
902 GList *collection_list = NULL;
904 GString *out_string = g_string_new(NULL);
906 collect_manager_list(&collection_list, NULL, NULL);
908 work = collection_list;
911 const gchar *collection_name = work->data;
912 out_string = g_string_append(out_string, g_strdup(collection_name));
913 out_string = g_string_append(out_string, "\n");
918 g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
919 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
921 string_list_free(collection_list);
922 g_string_free(out_string, TRUE);
926 static void gr_filelist(const gchar *text, GIOChannel *channel, gpointer data)
928 get_filelist(text, channel, FALSE);
931 static void gr_filelist_recurse(const gchar *text, GIOChannel *channel, gpointer data)
933 get_filelist(text, channel, TRUE);
936 static void gr_file_tell(const gchar *text, GIOChannel *channel, gpointer data)
939 gchar *collection_name = NULL;
941 if (!layout_valid(&lw_id)) return;
943 if (image_get_path(lw_id->image))
945 if (lw_id->image->collection && lw_id->image->collection->name)
947 collection_name = remove_extension_from_path(lw_id->image->collection->name);
948 out_string = g_strconcat(image_get_path(lw_id->image), " Collection: ", collection_name, NULL);
952 out_string = g_strconcat(image_get_path(lw_id->image), NULL);
955 g_io_channel_write_chars(channel, out_string, -1, NULL, NULL);
956 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
958 g_free(collection_name);
963 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
965 gchar *filename = expand_tilde(text);
967 if (isfile(filename))
969 load_config_from_file(filename, FALSE);
973 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
974 layout_set_path(NULL, homedir());
980 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer data)
982 gchar *filename = expand_tilde(text);
983 FileData *fd = file_data_new_group(filename);
986 if (fd->parent) fd = fd->parent;
988 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
989 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
991 work = fd->sidecar_files;
997 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
998 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
1003 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer data)
1005 gchar *filename = expand_tilde(text);
1006 FileData *fd = file_data_new_group(filename);
1008 if (fd->change && fd->change->dest)
1010 g_io_channel_write_chars(channel, fd->change->dest, -1, NULL, NULL);
1011 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
1016 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
1019 gchar *tilde_filename = expand_tilde(text);
1021 filename = set_pwd(tilde_filename);
1023 view_window_new(file_data_new_group(filename));
1025 g_free(tilde_filename);
1028 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
1030 RemoteData *remote_data = data;
1032 if (remote_data->command_collection)
1034 collection_unref(remote_data->command_collection);
1035 remote_data->command_collection = NULL;
1039 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
1041 RemoteData *remote_data = data;
1042 gboolean new = TRUE;
1044 if (!remote_data->command_collection)
1048 cd = collection_new("");
1053 cd->name = g_strdup(_("Command line"));
1055 remote_data->command_collection = cd;
1059 new = (!collection_get_first(remote_data->command_collection));
1062 if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && new)
1064 layout_image_set_collection(NULL, remote_data->command_collection,
1065 collection_get_first(remote_data->command_collection));
1069 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
1071 LayoutWindow *lw = NULL;
1073 if (layout_valid(&lw_id))
1075 gtk_window_present(GTK_WINDOW(lw_id->window));
1079 static void gr_pwd(const gchar *text, GIOChannel *channel, gpointer data)
1081 LayoutWindow *lw = NULL;
1084 pwd = g_strdup(text);
1088 static void gr_lua(const gchar *text, GIOChannel *channel, gpointer data)
1090 gchar *result = NULL;
1091 gchar **lua_command;
1093 lua_command = g_strsplit(text, ",", 2);
1095 if (lua_command[0] && lua_command[1])
1097 FileData *fd = file_data_new_group(lua_command[0]);
1098 result = g_strdup(lua_callvalue(fd, lua_command[1], NULL));
1101 g_io_channel_write_chars(channel, result, -1, NULL, NULL);
1105 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
1110 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
1113 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
1115 g_strfreev(lua_command);
1120 typedef struct _RemoteCommandEntry RemoteCommandEntry;
1121 struct _RemoteCommandEntry {
1124 void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
1125 gboolean needs_extra;
1126 gboolean prefer_command_line;
1131 static RemoteCommandEntry remote_commands[] = {
1132 /* short, long callback, extra, prefer, parameter, description */
1133 { "-n", "--next", gr_image_next, FALSE, FALSE, NULL, N_("next image") },
1134 { "-b", "--back", gr_image_prev, FALSE, FALSE, NULL, N_("previous image") },
1135 { NULL, "--first", gr_image_first, FALSE, FALSE, NULL, N_("first image") },
1136 { NULL, "--last", gr_image_last, FALSE, FALSE, NULL, N_("last image") },
1137 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, NULL, N_("toggle full screen") },
1138 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, NULL, N_("start full screen") },
1139 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, NULL, N_("stop full screen") },
1140 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, NULL, N_("toggle slide show") },
1141 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, NULL, N_("start slide show") },
1142 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, NULL, N_("stop slide show") },
1143 { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE, FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
1144 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("<[H:][M:][N][.M]>"), N_("set slide show delay to Hrs Mins N.M seconds") },
1145 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, NULL, N_("show tools") },
1146 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, NULL, N_("hide tools") },
1147 { "-q", "--quit", gr_quit, FALSE, FALSE, NULL, N_("quit") },
1148 { NULL, "--config-load:", gr_config_load, TRUE, FALSE, N_("<FILE>"), N_("load configuration from FILE") },
1149 { NULL, "--get-sidecars:", gr_get_sidecars, TRUE, FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
1150 { NULL, "--get-destination:", gr_get_destination, TRUE, FALSE, N_("<FILE>"), N_("get destination path of FILE") },
1151 { NULL, "file:", gr_file_load, TRUE, FALSE, N_("<FILE>"), N_("open FILE, bring Geeqie window to the top") },
1152 { NULL, "--file:", gr_file_load, TRUE, FALSE, N_("<FILE>"), N_("open FILE, bring Geeqie window to the top") },
1153 { NULL, "File:", gr_file_load_no_raise, TRUE, FALSE, N_("<FILE>"), N_("open FILE, do not bring Geeqie window to the top") },
1154 { NULL, "--File:", gr_file_load_no_raise, TRUE, FALSE, N_("<FILE>"), N_("open FILE, do not bring Geeqie window to the top") },
1155 { NULL, "--tell", gr_file_tell, FALSE, FALSE, NULL, N_("print filename [and Collection] of current image") },
1156 { NULL, "--pixel-info", gr_pixel_info, FALSE, FALSE, NULL, N_("print pixel info of mouse pointer on current image") },
1157 { NULL, "--get-rectangle", gr_rectangle, FALSE, FALSE, NULL, N_("get rectangle co-ordinates") },
1158 { NULL, "--get-render-intent", gr_render_intent, FALSE, FALSE, NULL, N_("get render intent") },
1159 { NULL, "--get-filelist:", gr_filelist, TRUE, FALSE, N_("[<FOLDER>]"), N_("get list of files and class") },
1160 { NULL, "--get-filelist-recurse:", gr_filelist_recurse, TRUE, FALSE, N_("[<FOLDER>]"), N_("get list of files and class recursive") },
1161 { NULL, "--get-collection:", gr_collection, TRUE, FALSE, N_("<COLLECTION>"), N_("get collection content") },
1162 { NULL, "--get-collection-list", gr_collection_list, FALSE, FALSE, NULL, N_("get collection list") },
1163 { NULL, "view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
1164 { NULL, "--view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
1165 { NULL, "--list-clear", gr_list_clear, FALSE, FALSE, NULL, N_("clear command line collection list") },
1166 { NULL, "--list-add:", gr_list_add, TRUE, FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
1167 { NULL, "raise", gr_raise, FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
1168 { NULL, "--raise", gr_raise, FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
1169 { NULL, "--id:", gr_lw_id, TRUE, FALSE, N_("<ID>"), N_("window id for following commands") },
1170 { NULL, "--new-window", gr_new_window, FALSE, FALSE, NULL, N_("new window") },
1171 { NULL, "--close-window", gr_close_window, FALSE, FALSE, NULL, N_("close window") },
1172 { "-ct:", "--cache-thumbs:", gr_cache_thumb, TRUE, FALSE, N_("clear|clean"), N_("clear or clean thumbnail cache") },
1173 { "-cs:", "--cache-shared:", gr_cache_shared, TRUE, FALSE, N_("clear|clean"), N_("clear or clean shared thumbnail cache") },
1174 { "-cm","--cache-metadata", gr_cache_metadata, FALSE, FALSE, NULL, N_(" clean the metadata cache") },
1175 { "-cr:", "--cache-render:", gr_cache_render, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails") },
1176 { "-crr:", "--cache-render-recurse:", gr_cache_render_recurse, TRUE, FALSE, N_("<folder> "), N_("render thumbnails recursively") },
1177 { "-crs:", "--cache-render-shared:", gr_cache_render_standard, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails (see Help)") },
1178 { "-crsr:", "--cache-render-shared-recurse:", gr_cache_render_standard_recurse, TRUE, FALSE, N_("<folder>"), N_(" render thumbnails recursively (see Help)") },
1180 { NULL, "--lua:", gr_lua, TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
1182 { NULL, "--PWD:", gr_pwd, TRUE, FALSE, N_("<PWD>"), N_("use PWD as working directory for following commands") },
1183 { NULL, NULL, NULL, FALSE, FALSE, NULL, NULL }
1186 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
1188 gboolean match = FALSE;
1192 while (!match && remote_commands[i].func != NULL)
1194 if (remote_commands[i].needs_extra)
1196 if (remote_commands[i].opt_s &&
1197 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
1199 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
1200 return &remote_commands[i];
1202 else if (remote_commands[i].opt_l &&
1203 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
1205 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
1206 return &remote_commands[i];
1211 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
1212 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
1214 if (offset) *offset = text;
1215 return &remote_commands[i];
1225 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
1227 RemoteCommandEntry *entry;
1228 const gchar *offset;
1230 entry = remote_command_find(text, &offset);
1231 if (entry && entry->func)
1233 entry->func(offset, channel, data);
1237 log_printf("unknown remote command:%s\n", text);
1241 void remote_help(void)
1247 print_term(FALSE, _("Remote command list:\n"));
1250 while (remote_commands[i].func != NULL)
1252 if (remote_commands[i].description)
1254 s_opt_param = g_strconcat(remote_commands[i].opt_s, remote_commands[i].parameter, NULL);
1255 l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
1256 printf_term(FALSE, " %-11s%-1s %-30s%-s\n",
1257 (remote_commands[i].opt_s) ? s_opt_param : "",
1258 (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
1259 (remote_commands[i].opt_l) ? l_opt_param : "",
1260 _(remote_commands[i].description));
1261 g_free(s_opt_param);
1262 g_free(l_opt_param);
1266 printf_term(FALSE, N_("\n All other command line parameters are used as plain files if they exists.\n"));
1269 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
1276 RemoteCommandEntry *entry;
1278 entry = remote_command_find(argv[i], NULL);
1281 list = g_list_append(list, argv[i]);
1283 else if (errors && !isfile(argv[i]))
1285 *errors = g_list_append(*errors, argv[i]);
1294 * \param arg_exec Binary (argv0)
1295 * \param remote_list Evaluated and recognized remote commands
1296 * \param path The current path
1297 * \param cmd_list List of all non collections in Path
1298 * \param collection_list List of all collections in argv
1300 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
1301 GList *cmd_list, GList *collection_list)
1303 RemoteConnection *rc;
1304 gboolean started = FALSE;
1307 buf = g_build_filename(get_rc_dir(), ".command", NULL);
1308 rc = remote_client_open(buf);
1313 gint retry_count = 12;
1314 gboolean blank = FALSE;
1316 printf_term(FALSE, _("Remote %s not running, starting..."), GQ_APPNAME);
1318 command = g_string_new(arg_exec);
1324 RemoteCommandEntry *entry;
1329 entry = remote_command_find(text, NULL);
1332 if (entry->prefer_command_line)
1334 remote_list = g_list_remove(remote_list, text);
1335 g_string_append(command, " ");
1336 g_string_append(command, text);
1338 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
1345 if (blank || cmd_list || path) g_string_append(command, " --blank");
1346 if (get_debug_level()) g_string_append(command, " --debug");
1348 g_string_append(command, " &");
1349 runcmd(command->str);
1350 g_string_free(command, TRUE);
1352 while (!rc && retry_count > 0)
1354 usleep((retry_count > 10) ? 500000 : 1000000);
1355 rc = remote_client_open(buf);
1356 if (!rc) print_term(FALSE, ".");
1360 print_term(FALSE, "\n");
1369 const gchar *prefix;
1370 gboolean use_path = TRUE;
1371 gboolean sent = FALSE;
1377 RemoteCommandEntry *entry;
1382 entry = remote_command_find(text, NULL);
1385 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
1387 remote_client_send(rc, text);
1392 if (cmd_list && cmd_list->next)
1394 prefix = "--list-add:";
1395 remote_client_send(rc, "--list-clear");
1411 text = g_strconcat(prefix, fd->path, NULL);
1412 remote_client_send(rc, text);
1418 if (path && !cmd_list && use_path)
1422 text = g_strdup_printf("file:%s", path);
1423 remote_client_send(rc, text);
1429 work = collection_list;
1438 text = g_strdup_printf("file:%s", name);
1439 remote_client_send(rc, text);
1445 if (!started && !sent)
1447 remote_client_send(rc, "raise");
1452 print_term(TRUE, _("Remote not available\n"));
1458 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
1460 RemoteConnection *remote_connection = remote_server_open(path);
1461 RemoteData *remote_data = g_new(RemoteData, 1);
1463 remote_data->command_collection = command_collection;
1465 remote_server_subscribe(remote_connection, remote_cb, remote_data);
1466 return remote_connection;
1468 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */