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"
40 #include "view-file.h"
42 #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;
78 /* To enable file names containing newlines to be processed correctly,
79 * the --print0 remote option sets returned data to be terminated with a null
80 * character rather a newline
82 static gboolean print0 = FALSE;
84 /* Remote commands from main.cc are prepended with the current dir the remote
85 * command was made from. Some remote commands require this. The
86 * value is stored here
88 static gchar *pwd = NULL;
91 * @brief Ensures file path is absolute.
92 * @param[in] filename Filepath, absolute or relative to calling directory
93 * @returns absolute path
95 * If first character of input filepath is not the directory
96 * separator, assume it as a relative path and prepend
97 * the directory the remote command was initiated from
99 * Return value must be freed with g_free()
101 static gchar *set_pwd(gchar *filename)
105 if (strncmp(filename, G_DIR_SEPARATOR_S, 1) != 0)
107 temp = g_build_filename(pwd, filename, NULL);
111 temp = g_strdup(filename);
117 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
119 RemoteClient *client = data;
120 RemoteConnection *rc;
121 GIOStatus status = G_IO_STATUS_NORMAL;
126 if (condition & G_IO_IN)
128 gchar *buffer = NULL;
129 GError *error = NULL;
131 /** @FIXME it should be possible to terminate the command with a null character */
132 g_io_channel_set_line_term(source, "<gq_end_of_command>", -1);
133 while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
137 buffer[termpos] = '\0';
139 if (strlen(buffer) > 0)
141 if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
142 g_io_channel_write_chars(source, "<gq_end_of_command>", -1, NULL, NULL); /* empty line finishes the command */
143 g_io_channel_flush(source, NULL);
153 log_printf("error reading socket: %s\n", error->message);
158 if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
160 rc->clients = g_list_remove(rc->clients, client);
162 DEBUG_1("HUP detected, closing client.");
163 DEBUG_1("client count %d", g_list_length(rc->clients));
165 g_source_remove(client->channel_id);
173 static void remote_server_client_add(RemoteConnection *rc, gint fd)
175 RemoteClient *client;
178 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
180 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
185 client = g_new0(RemoteClient, 1);
189 channel = g_io_channel_unix_new(fd);
190 client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP,
191 remote_server_client_cb, client, NULL);
192 g_io_channel_unref(channel);
194 rc->clients = g_list_append(rc->clients, client);
195 DEBUG_1("client count %d", g_list_length(rc->clients));
198 static void remote_server_clients_close(RemoteConnection *rc)
202 RemoteClient *client = rc->clients->data;
204 rc->clients = g_list_remove(rc->clients, client);
206 g_source_remove(client->channel_id);
212 static gboolean remote_server_read_cb(GIOChannel *UNUSED(source), GIOCondition UNUSED(condition), gpointer data)
214 RemoteConnection *rc = data;
218 fd = accept(rc->fd, NULL, &alen);
221 log_printf("error accepting socket: %s\n", strerror(errno));
225 remote_server_client_add(rc, fd);
230 gboolean remote_server_exists(const gchar *path)
232 RemoteConnection *rc;
234 /* verify server up */
235 rc = remote_client_open(path);
240 /* unable to connect, remove socket file to free up address */
245 static RemoteConnection *remote_server_open(const gchar *path)
247 RemoteConnection *rc;
248 struct sockaddr_un addr;
253 if (remote_server_exists(path))
255 log_printf("Address already in use: %s\n", path);
259 fd = socket(PF_UNIX, SOCK_STREAM, 0);
260 if (fd == -1) return NULL;
262 addr.sun_family = AF_UNIX;
263 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
264 strncpy(addr.sun_path, path, sun_path_len);
265 if (bind(fd, (const struct sockaddr*)&addr, sizeof(addr)) == -1 ||
266 listen(fd, REMOTE_SERVER_BACKLOG) == -1)
268 log_printf("error subscribing to socket: %s\n", strerror(errno));
273 rc = g_new0(RemoteConnection, 1);
277 rc->path = g_strdup(path);
279 channel = g_io_channel_unix_new(rc->fd);
280 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
282 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
283 remote_server_read_cb, rc, NULL);
284 g_io_channel_unref(channel);
289 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
291 if (!rc || !rc->server) return;
293 rc->read_func = func;
294 rc->read_data = data;
298 static RemoteConnection *remote_client_open(const gchar *path)
300 RemoteConnection *rc;
302 struct sockaddr_un addr;
306 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
308 fd = socket(PF_UNIX, SOCK_STREAM, 0);
309 if (fd == -1) return NULL;
311 addr.sun_family = AF_UNIX;
312 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
313 strncpy(addr.sun_path, path, sun_path_len);
314 if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1)
316 DEBUG_1("error connecting to socket: %s", strerror(errno));
321 rc = g_new0(RemoteConnection, 1);
324 rc->path = g_strdup(path);
329 static sig_atomic_t sigpipe_occurred = FALSE;
331 static void sighandler_sigpipe(gint UNUSED(sig))
333 sigpipe_occurred = TRUE;
336 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
338 struct sigaction new_action, old_action;
339 gboolean ret = FALSE;
340 GError *error = NULL;
343 if (!rc || rc->server) return FALSE;
344 if (!text) return TRUE;
346 sigpipe_occurred = FALSE;
348 new_action.sa_handler = sighandler_sigpipe;
349 sigemptyset(&new_action.sa_mask);
350 new_action.sa_flags = 0;
352 /* setup our signal handler */
353 sigaction(SIGPIPE, &new_action, &old_action);
355 channel = g_io_channel_unix_new(rc->fd);
357 g_io_channel_write_chars(channel, text, -1, NULL, &error);
358 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, &error);
359 g_io_channel_flush(channel, &error);
363 log_printf("error reading socket: %s\n", error->message);
374 gchar *buffer = NULL;
376 g_io_channel_set_line_term(channel, "<gq_end_of_command>", -1);
377 while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
381 if (g_strstr_len(buffer, -1, "<gq_end_of_command>") == buffer) /* empty line finishes the command */
387 buffer[termpos] = '\0';
388 if (g_strstr_len(buffer, -1, "print0") != 0)
396 printf("%s%c", buffer, 0);
400 printf("%s\n", buffer);
410 log_printf("error reading socket: %s\n", error->message);
417 /* restore the original signal handler */
418 sigaction(SIGPIPE, &old_action, NULL);
419 g_io_channel_unref(channel);
423 void remote_close(RemoteConnection *rc)
429 remote_server_clients_close(rc);
431 g_source_remove(rc->channel_id);
436 g_free(rc->read_data);
445 *-----------------------------------------------------------------------------
447 *-----------------------------------------------------------------------------
450 static void gr_image_next(const gchar *UNUSED(text), GIOChannel *UNUSED(channel), gpointer UNUSED(data))
452 layout_image_next(lw_id);
455 static void gr_new_window(const gchar *UNUSED(text), GIOChannel *UNUSED(channel), gpointer UNUSED(data))
457 LayoutWindow *lw = NULL;
459 if (!layout_valid(&lw)) return;
461 lw_id = layout_new_from_default();
463 layout_set_path(lw_id, pwd);
466 static gboolean gr_close_window_cb()
468 if (!layout_valid(&lw_id)) return FALSE;
470 layout_menu_close_cb(NULL, lw_id);
475 static void gr_close_window(const gchar *UNUSED(text), GIOChannel *UNUSED(channel), gpointer UNUSED(data))
477 g_idle_add(gr_close_window_cb, NULL);
480 static void gr_image_prev(const gchar *UNUSED(text), GIOChannel *UNUSED(channel), gpointer UNUSED(data))
482 layout_image_prev(lw_id);
485 static void gr_image_first(const gchar *UNUSED(text), GIOChannel *UNUSED(channel), gpointer UNUSED(data))
487 layout_image_first(lw_id);
490 static void gr_image_last(const gchar *UNUSED(text), GIOChannel *UNUSED(channel), gpointer UNUSED(data))
492 layout_image_last(lw_id);
495 static void gr_fullscreen_toggle(const gchar *UNUSED(text), GIOChannel *UNUSED(channel), gpointer UNUSED(data))
497 layout_image_full_screen_toggle(lw_id);
500 static void gr_fullscreen_start(const gchar *UNUSED(text), GIOChannel *UNUSED(channel), gpointer UNUSED(data))
502 layout_image_full_screen_start(lw_id);
505 static void gr_fullscreen_stop(const gchar *UNUSED(text), GIOChannel *UNUSED(channel), gpointer UNUSED(data))
507 layout_image_full_screen_stop(lw_id);
510 static void gr_lw_id(const gchar *text, GIOChannel *UNUSED(channel), gpointer UNUSED(data))
512 lw_id = layout_find_by_layout_id(text);
515 log_printf("remote sent window ID that does not exist:\"%s\"\n",text);
517 layout_valid(&lw_id);
520 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *UNUSED(channel), gpointer UNUSED(data))
523 gchar *tilde_filename;
525 tilde_filename = expand_tilde(text);
527 FileData *dir_fd = file_data_new_dir(tilde_filename);
528 g_free(tilde_filename);
530 layout_valid(&lw_id);
531 list = filelist_recursive_full(dir_fd, lw_id->sort_method, lw_id->sort_ascend);
532 file_data_unref(dir_fd);
534 //printf("length: %d\n", g_list_length(list));
535 layout_image_slideshow_stop(lw_id);
536 layout_image_slideshow_start_from_list(lw_id, list);
539 static void gr_cache_thumb(const gchar *text, GIOChannel *UNUSED(channel), gpointer UNUSED(data))
541 if (!g_strcmp0(text, "clear"))
543 cache_maintain_home_remote(FALSE, TRUE, NULL);
545 else if (!g_strcmp0(text, "clean"))
547 cache_maintain_home_remote(FALSE, FALSE, NULL);
551 static void gr_cache_shared(const gchar *text, GIOChannel *UNUSED(channel), gpointer UNUSED(data))
553 if (!g_strcmp0(text, "clear"))
554 cache_manager_standard_process_remote(TRUE);
555 else if (!g_strcmp0(text, "clean"))
556 cache_manager_standard_process_remote(FALSE);
559 static void gr_cache_metadata(const gchar *UNUSED(text), GIOChannel *UNUSED(channel), gpointer UNUSED(data))
561 cache_maintain_home_remote(TRUE, FALSE, NULL);
564 static void gr_cache_render(const gchar *text, GIOChannel *UNUSED(channel), gpointer UNUSED(data))
566 cache_manager_render_remote(text, FALSE, FALSE, NULL);
569 static void gr_cache_render_recurse(const gchar *text, GIOChannel *UNUSED(channel), gpointer UNUSED(data))
571 cache_manager_render_remote(text, TRUE, FALSE, NULL);
574 static void gr_cache_render_standard(const gchar *text, GIOChannel *UNUSED(channel), gpointer UNUSED(data))
576 if(options->thumbnails.spec_standard)
578 cache_manager_render_remote(text, FALSE, TRUE, NULL);
582 static void gr_cache_render_standard_recurse(const gchar *text, GIOChannel *UNUSED(channel), gpointer UNUSED(data))
584 if(options->thumbnails.spec_standard)
586 cache_manager_render_remote(text, TRUE, TRUE, NULL);
590 static void gr_slideshow_toggle(const gchar *UNUSED(text), GIOChannel *UNUSED(channel), gpointer UNUSED(data))
592 layout_image_slideshow_toggle(lw_id);
595 static void gr_slideshow_start(const gchar *UNUSED(text), GIOChannel *UNUSED(channel), gpointer UNUSED(data))
597 layout_image_slideshow_start(lw_id);
600 static void gr_slideshow_stop(const gchar *UNUSED(text), GIOChannel *UNUSED(channel), gpointer UNUSED(data))
602 layout_image_slideshow_stop(lw_id);
605 static void gr_slideshow_delay(const gchar *text, GIOChannel *UNUSED(channel), gpointer UNUSED(data))
607 gdouble t1, t2, t3, n;
610 res = sscanf(text, "%lf:%lf:%lf", &t1, &t2, &t3);
613 n = (t1 * 3600) + (t2 * 60) + t3;
614 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
615 t1 >= 24 || t2 >= 60 || t3 >= 60)
617 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
618 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
625 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
626 t1 >= 60 || t2 >= 60)
628 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
629 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
636 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
638 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
639 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
648 options->slideshow.delay = (gint)(n * 10.0 + 0.01);
651 static void gr_tools_show(const gchar *UNUSED(text), GIOChannel *UNUSED(channel), gpointer UNUSED(data))
656 if (layout_tools_float_get(lw_id, &popped, &hidden) && hidden)
658 layout_tools_float_set(lw_id, popped, FALSE);
662 static void gr_tools_hide(const gchar *UNUSED(text), GIOChannel *UNUSED(channel), gpointer UNUSED(data))
667 if (layout_tools_float_get(lw_id, &popped, &hidden) && !hidden)
669 layout_tools_float_set(lw_id, popped, TRUE);
673 static gboolean gr_quit_idle_cb(gpointer UNUSED(data))
680 static void gr_quit(const gchar *UNUSED(text), GIOChannel *UNUSED(channel), gpointer UNUSED(data))
682 /* schedule exit when idle, if done from within a
683 * remote handler remote_close will crash
685 g_idle_add(gr_quit_idle_cb, NULL);
688 static void gr_file_load_no_raise(const gchar *text, GIOChannel *UNUSED(channel), gpointer UNUSED(data))
691 gchar *tilde_filename;
693 if (!download_web_file(text, TRUE, NULL))
695 tilde_filename = expand_tilde(text);
696 filename = set_pwd(tilde_filename);
698 if (isfile(filename))
700 if (file_extension_match(filename, GQ_COLLECTION_EXT))
702 collection_window_new(filename);
706 layout_set_path(lw_id, filename);
709 else if (isdir(filename))
711 layout_set_path(lw_id, filename);
715 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
716 layout_set_path(lw_id, homedir());
720 g_free(tilde_filename);
724 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
726 gr_file_load_no_raise(text, channel, data);
728 gr_raise(text, channel, data);
731 static void gr_pixel_info(const gchar *UNUSED(text), GIOChannel *channel, gpointer UNUSED(data))
734 gint x_pixel, y_pixel;
736 gint r_mouse, g_mouse, b_mouse;
739 if (!layout_valid(&lw_id)) return;
741 pr = (PixbufRenderer*)lw_id->image->pr;
745 pixbuf_renderer_get_image_size(pr, &width, &height);
746 if (width < 1 || height < 1) return;
748 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
750 if (x_pixel >= 0 && y_pixel >= 0)
752 pixbuf_renderer_get_pixel_colors(pr, x_pixel, y_pixel,
753 &r_mouse, &g_mouse, &b_mouse);
755 pixel_info = g_strdup_printf(_("[%d,%d]: RGB(%3d,%3d,%3d)"),
757 r_mouse, g_mouse, b_mouse);
759 g_io_channel_write_chars(channel, pixel_info, -1, NULL, NULL);
760 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
775 static void gr_rectangle(const gchar *UNUSED(text), GIOChannel *channel, gpointer UNUSED(data))
777 gchar *rectangle_info;
781 if (!options->draw_rectangle) return;
782 if (!layout_valid(&lw_id)) return;
784 pr = (PixbufRenderer*)lw_id->image->pr;
788 image_get_rectangle(&x1, &y1, &x2, &y2);
789 rectangle_info = g_strdup_printf(_("%dx%d+%d+%d"),
790 (x2 > x1) ? x2 - x1 : x1 - x2,
791 (y2 > y1) ? y2 - y1 : y1 - y2,
793 (y2 > y1) ? y1 : y2);
795 g_io_channel_write_chars(channel, rectangle_info, -1, NULL, NULL);
796 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
798 g_free(rectangle_info);
802 static void gr_render_intent(const gchar *UNUSED(text), GIOChannel *channel, gpointer UNUSED(data))
804 gchar *render_intent;
806 switch (options->color_profile.render_intent)
809 render_intent = g_strdup("Perceptual");
812 render_intent = g_strdup("Relative Colorimetric");
815 render_intent = g_strdup("Saturation");
818 render_intent = g_strdup("Absolute Colorimetric");
821 render_intent = g_strdup("none");
825 g_io_channel_write_chars(channel, render_intent, -1, NULL, NULL);
826 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
828 g_free(render_intent);
831 static void get_filelist(const gchar *text, GIOChannel *channel, gboolean recurse)
834 FileFormatClass format_class;
837 GString *out_string = g_string_new(NULL);
839 gchar *tilde_filename;
841 if (strcmp(text, "") == 0)
843 if (layout_valid(&lw_id))
845 dir_fd = file_data_new_dir(lw_id->dir_fd->path);
854 tilde_filename = expand_tilde(text);
855 if (isdir(tilde_filename))
857 dir_fd = file_data_new_dir(tilde_filename);
861 g_free(tilde_filename);
864 g_free(tilde_filename);
869 list = filelist_recursive(dir_fd);
873 filelist_read(dir_fd, &list, NULL);
880 g_string_append_printf(out_string, "%s", fd->path);
881 format_class = filter_file_get_class(fd->path);
883 switch (format_class)
885 case FORMAT_CLASS_IMAGE:
886 out_string = g_string_append(out_string, " Class: Image");
888 case FORMAT_CLASS_RAWIMAGE:
889 out_string = g_string_append(out_string, " Class: RAW image");
891 case FORMAT_CLASS_META:
892 out_string = g_string_append(out_string, " Class: Metadata");
894 case FORMAT_CLASS_VIDEO:
895 out_string = g_string_append(out_string, " Class: Video");
897 case FORMAT_CLASS_COLLECTION:
898 out_string = g_string_append(out_string, " Class: Collection");
900 case FORMAT_CLASS_DOCUMENT:
901 out_string = g_string_append(out_string, " Class: Document");
903 case FORMAT_CLASS_ARCHIVE:
904 out_string = g_string_append(out_string, " Class: Archive");
906 case FORMAT_CLASS_UNKNOWN:
907 out_string = g_string_append(out_string, " Class: Unknown");
910 out_string = g_string_append(out_string, " Class: Unknown");
913 out_string = g_string_append(out_string, "\n");
917 g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
918 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
920 g_string_free(out_string, TRUE);
922 file_data_unref(dir_fd);
925 static void gr_get_selection(const gchar *UNUSED(text), GIOChannel *channel, gboolean UNUSED(data))
927 if (!layout_valid(&lw_id)) return;
929 GList *selected = layout_selection_list(lw_id); // Keep copy to free.
930 GString *out_string = g_string_new(NULL);
932 GList *work = selected;
935 FileData *fd = work->data;
936 g_assert(fd->magick == FD_MAGICK);
938 g_string_append_printf(out_string, "%s %s\n",
940 format_class_list[filter_file_get_class(fd->path)]);
945 g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
946 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
948 filelist_free(selected);
949 g_string_free(out_string, TRUE);
952 static void gr_collection(const gchar *text, GIOChannel *channel, gpointer UNUSED(data))
954 GString *contents = g_string_new(NULL);
956 if (is_collection(text))
958 collection_contents(text, &contents);
965 g_io_channel_write_chars(channel, contents->str, -1, NULL, NULL);
966 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
968 g_string_free(contents, TRUE);
971 static void gr_collection_list(const gchar *UNUSED(text), GIOChannel *channel, gpointer UNUSED(data))
974 GList *collection_list = NULL;
976 GString *out_string = g_string_new(NULL);
978 collect_manager_list(&collection_list, NULL, NULL);
980 work = collection_list;
983 const gchar *collection_name = work->data;
984 out_string = g_string_append(out_string, g_strdup(collection_name));
985 out_string = g_string_append(out_string, "\n");
990 g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
991 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
993 string_list_free(collection_list);
994 g_string_free(out_string, TRUE);
997 static gboolean wait_cb(const gpointer data)
999 gint position = GPOINTER_TO_INT(data);
1000 gint x = position >> 16;
1001 gint y = position - (x << 16);
1003 gtk_window_move(GTK_WINDOW(lw_id->window), x, y);
1008 static void gr_geometry(const gchar *text, GIOChannel *UNUSED(channel), gpointer UNUSED(data))
1012 if (!layout_valid(&lw_id) || !text)
1019 geometry = g_strsplit_set(text, "+", 3);
1020 if (geometry[1] != NULL && geometry[2] != NULL )
1022 gtk_window_move(GTK_WINDOW(lw_id->window), atoi(geometry[1]), atoi(geometry[2]));
1027 geometry = g_strsplit_set(text, "+x", 4);
1028 if (geometry[0] != NULL && geometry[1] != NULL)
1030 gtk_window_resize(GTK_WINDOW(lw_id->window), atoi(geometry[0]), atoi(geometry[1]));
1032 if (geometry[2] != NULL && geometry[3] != NULL)
1034 /* There is an occasional problem with a window_move immediately after a window_resize */
1035 g_idle_add(wait_cb, GINT_TO_POINTER((atoi(geometry[2]) << 16) + atoi(geometry[3])));
1038 g_strfreev(geometry);
1041 static void gr_filelist(const gchar *text, GIOChannel *channel, gpointer UNUSED(data))
1043 get_filelist(text, channel, FALSE);
1046 static void gr_filelist_recurse(const gchar *text, GIOChannel *channel, gpointer UNUSED(data))
1048 get_filelist(text, channel, TRUE);
1051 static void gr_file_tell(const gchar *UNUSED(text), GIOChannel *channel, gpointer UNUSED(data))
1054 gchar *collection_name = NULL;
1056 if (!layout_valid(&lw_id)) return;
1058 if (image_get_path(lw_id->image))
1060 if (lw_id->image->collection && lw_id->image->collection->name)
1062 collection_name = remove_extension_from_path(lw_id->image->collection->name);
1063 out_string = g_strconcat(image_get_path(lw_id->image), " Collection: ", collection_name, NULL);
1067 out_string = g_strconcat(image_get_path(lw_id->image), NULL);
1072 out_string = g_strconcat(lw_id->dir_fd->path, G_DIR_SEPARATOR_S, NULL);
1075 g_io_channel_write_chars(channel, out_string, -1, NULL, NULL);
1076 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1078 g_free(collection_name);
1082 static void gr_file_info(const gchar *UNUSED(text), GIOChannel *channel, gpointer UNUSED(data))
1086 gchar *country_name;
1087 gchar *country_code;
1090 GString *out_string;
1091 FileFormatClass format_class;
1093 if (!layout_valid(&lw_id)) return;
1095 if (image_get_path(lw_id->image))
1097 filename = g_strdup(image_get_path(lw_id->image));
1098 fd = file_data_new_group(filename);
1099 out_string = g_string_new(NULL);
1101 format_class = filter_file_get_class(image_get_path(lw_id->image));
1104 g_string_append_printf(out_string, _("Class: %s\n"), format_class_list[format_class]);
1107 if (fd->page_total > 1)
1109 g_string_append_printf(out_string, _("Page no: %d/%d\n"), fd->page_num + 1, fd->page_total);
1114 country_name = exif_get_data_as_text(fd->exif, "formatted.countryname");
1117 g_string_append_printf(out_string, _("Country name: %s\n"), country_name);
1118 g_free(country_name);
1121 country_code = exif_get_data_as_text(fd->exif, "formatted.countrycode");
1124 g_string_append_printf(out_string, _("Country code: %s\n"), country_code);
1125 g_free(country_code);
1128 timezone = exif_get_data_as_text(fd->exif, "formatted.timezone");
1131 g_string_append_printf(out_string, _("Timezone: %s\n"), timezone);
1135 local_time = exif_get_data_as_text(fd->exif, "formatted.localtime");
1138 g_string_append_printf(out_string, ("Local time: %s\n"), local_time);
1143 g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
1144 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1146 g_string_free(out_string, TRUE);
1147 file_data_unref(fd);
1152 static gchar *config_file_path(const gchar *param)
1155 gchar *full_name = NULL;
1157 if (file_extension_match(param, ".xml"))
1159 path = g_build_filename(get_window_layouts_dir(), param, NULL);
1161 else if (file_extension_match(param, NULL))
1163 full_name = g_strconcat(param, ".xml", NULL);
1164 path = g_build_filename(get_window_layouts_dir(), full_name, NULL);
1177 static gboolean is_config_file(const gchar *param)
1181 name = config_file_path(param);
1190 static void gr_config_load(const gchar *text, GIOChannel *UNUSED(channel), gpointer UNUSED(data))
1192 gchar *filename = expand_tilde(text);
1194 if (!g_strstr_len(filename, -1, G_DIR_SEPARATOR_S))
1196 if (is_config_file(filename))
1198 gchar *tmp = config_file_path(filename);
1204 if (isfile(filename))
1206 load_config_from_file(filename, FALSE);
1210 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
1211 layout_set_path(NULL, homedir());
1217 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer UNUSED(data))
1219 gchar *filename = expand_tilde(text);
1220 FileData *fd = file_data_new_group(filename);
1223 if (fd->parent) fd = fd->parent;
1225 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
1226 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1228 work = fd->sidecar_files;
1234 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
1235 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1240 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer UNUSED(data))
1242 gchar *filename = expand_tilde(text);
1243 FileData *fd = file_data_new_group(filename);
1245 if (fd->change && fd->change->dest)
1247 g_io_channel_write_chars(channel, fd->change->dest, -1, NULL, NULL);
1248 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1253 static void gr_file_view(const gchar *text, GIOChannel *UNUSED(channel), gpointer UNUSED(data))
1256 gchar *tilde_filename = expand_tilde(text);
1258 filename = set_pwd(tilde_filename);
1260 view_window_new(file_data_new_group(filename));
1262 g_free(tilde_filename);
1265 static void gr_list_clear(const gchar *UNUSED(text), GIOChannel *UNUSED(channel), gpointer data)
1267 RemoteData *remote_data = data;
1269 remote_data->command_collection = NULL;
1270 remote_data->file_list = NULL;
1271 remote_data->single_dir = TRUE;
1274 static void gr_list_add(const gchar *text, GIOChannel *UNUSED(channel), gpointer data)
1276 RemoteData *remote_data = data;
1277 gboolean is_new = TRUE;
1282 /** @FIXME Should check if file is in current dir, has tilde or is relative */
1285 log_printf("Warning: File does not exist --remote --list-add:%s", text);
1290 /* If there is a files list on the command line
1291 * check if they are all in the same folder
1293 if (remote_data->single_dir)
1296 work = remote_data->file_list;
1297 while (work && remote_data->single_dir)
1300 dirname = g_path_get_dirname(((FileData *)work->data)->path);
1303 path = g_strdup(dirname);
1307 if (g_strcmp0(path, dirname) != 0)
1309 remote_data->single_dir = FALSE;
1318 gchar *pathname = g_path_get_dirname(text);
1319 layout_set_path(lw_id, pathname);
1322 fd = file_data_new_simple(text);
1323 remote_data->file_list = g_list_append(remote_data->file_list, fd);
1324 file_data_unref(fd);
1326 vf_select_none(lw_id->vf);
1327 remote_data->file_list = g_list_reverse(remote_data->file_list);
1329 layout_select_list(lw_id, remote_data->file_list);
1330 layout_refresh(lw_id);
1331 first = (FileData *)(g_list_first(vf_selection_get_list(lw_id->vf))->data);
1332 layout_set_fd(lw_id, first);
1337 if (!remote_data->command_collection && !remote_data->single_dir)
1339 cw = collection_window_new(NULL);
1342 collection_path_changed(cd);
1344 remote_data->command_collection = cd;
1346 else if (!remote_data->single_dir)
1348 is_new = (!collection_get_first(remote_data->command_collection));
1351 if (!remote_data->single_dir)
1353 layout_image_set_collection(lw_id, remote_data->command_collection, collection_get_first(remote_data->command_collection));
1354 if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && is_new)
1356 layout_image_set_collection(lw_id, remote_data->command_collection, collection_get_first(remote_data->command_collection));
1361 static void gr_raise(const gchar *UNUSED(text), GIOChannel *UNUSED(channel), gpointer UNUSED(data))
1363 if (layout_valid(&lw_id))
1365 gtk_window_present(GTK_WINDOW(lw_id->window));
1369 static void gr_pwd(const gchar *text, GIOChannel *UNUSED(channel), gpointer UNUSED(data))
1371 LayoutWindow *lw = NULL;
1376 pwd = g_strdup(text);
1380 static void gr_print0(const gchar *UNUSED(text), GIOChannel *channel, gpointer UNUSED(data))
1382 g_io_channel_write_chars(channel, "print0", -1, NULL, NULL);
1383 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1387 static void gr_lua(const gchar *text, GIOChannel *channel, gpointer UNUSED(data))
1389 gchar *result = NULL;
1390 gchar **lua_command;
1392 lua_command = g_strsplit(text, ",", 2);
1394 if (lua_command[0] && lua_command[1])
1396 FileData *fd = file_data_new_group(lua_command[0]);
1397 result = g_strdup(lua_callvalue(fd, lua_command[1], NULL));
1400 g_io_channel_write_chars(channel, result, -1, NULL, NULL);
1404 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
1409 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
1412 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1414 g_strfreev(lua_command);
1419 typedef struct _RemoteCommandEntry RemoteCommandEntry;
1420 struct _RemoteCommandEntry {
1423 void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
1424 gboolean needs_extra;
1425 gboolean prefer_command_line;
1430 static RemoteCommandEntry remote_commands[] = {
1431 /* short, long callback, extra, prefer, parameter, description */
1432 { "-b", "--back", gr_image_prev, FALSE, FALSE, NULL, N_("previous image") },
1433 { NULL, "--close-window", gr_close_window, FALSE, FALSE, NULL, N_("close window") },
1434 { NULL, "--config-load:", gr_config_load, TRUE, FALSE, N_("<FILE>|layout ID"), N_("load configuration from FILE") },
1435 { "-cm","--cache-metadata", gr_cache_metadata, FALSE, FALSE, NULL, N_("clean the metadata cache") },
1436 { "-cr:", "--cache-render:", gr_cache_render, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails") },
1437 { "-crr:", "--cache-render-recurse:", gr_cache_render_recurse, TRUE, FALSE, N_("<folder> "), N_("render thumbnails recursively") },
1438 { "-crs:", "--cache-render-shared:", gr_cache_render_standard, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails (see Help)") },
1439 { "-crsr:", "--cache-render-shared-recurse:", gr_cache_render_standard_recurse, TRUE, FALSE, N_("<folder>"), N_(" render thumbnails recursively (see Help)") },
1440 { "-cs:", "--cache-shared:", gr_cache_shared, TRUE, FALSE, N_("clear|clean"), N_("clear or clean shared thumbnail cache") },
1441 { "-ct:", "--cache-thumbs:", gr_cache_thumb, TRUE, FALSE, N_("clear|clean"), N_("clear or clean thumbnail cache") },
1442 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("<[H:][M:][N][.M]>"), N_("set slide show delay to Hrs Mins N.M seconds") },
1443 { NULL, "--first", gr_image_first, FALSE, FALSE, NULL, N_("first image") },
1444 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, NULL, N_("toggle full screen") },
1445 { NULL, "--file:", gr_file_load, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, bring Geeqie window to the top") },
1446 { NULL, "file:", gr_file_load, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, bring Geeqie window to the top") },
1447 { NULL, "--File:", gr_file_load_no_raise, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, do not bring Geeqie window to the top") },
1448 { NULL, "File:", gr_file_load_no_raise, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, do not bring Geeqie window to the top") },
1449 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, NULL, N_("start full screen") },
1450 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, NULL, N_("stop full screen") },
1451 { NULL, "--geometry=", gr_geometry, TRUE, FALSE, N_("<GEOMETRY>"), N_("set window geometry") },
1452 { NULL, "--get-collection:", gr_collection, TRUE, FALSE, N_("<COLLECTION>"), N_("get collection content") },
1453 { NULL, "--get-collection-list", gr_collection_list, FALSE, FALSE, NULL, N_("get collection list") },
1454 { NULL, "--get-destination:", gr_get_destination, TRUE, FALSE, N_("<FILE>"), N_("get destination path of FILE (See Plugins Configuration)") },
1455 { NULL, "--get-file-info", gr_file_info, FALSE, FALSE, NULL, N_("get file info") },
1456 { NULL, "--get-filelist:", gr_filelist, TRUE, FALSE, N_("[<FOLDER>]"), N_("get list of files and class") },
1457 { NULL, "--get-filelist-recurse:", gr_filelist_recurse, TRUE, FALSE, N_("[<FOLDER>]"), N_("get list of files and class recursive") },
1458 { NULL, "--get-rectangle", gr_rectangle, FALSE, FALSE, NULL, N_("get rectangle co-ordinates") },
1459 { NULL, "--get-render-intent", gr_render_intent, FALSE, FALSE, NULL, N_("get render intent") },
1460 { NULL, "--get-selection", gr_get_selection, FALSE, FALSE, NULL, N_("get list of selected files") },
1461 { NULL, "--get-sidecars:", gr_get_sidecars, TRUE, FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
1462 { NULL, "--id:", gr_lw_id, TRUE, FALSE, N_("<ID>"), N_("window id for following commands") },
1463 { NULL, "--last", gr_image_last, FALSE, FALSE, NULL, N_("last image") },
1464 { NULL, "--list-add:", gr_list_add, TRUE, FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
1465 { NULL, "--list-clear", gr_list_clear, FALSE, FALSE, NULL, N_("clear command line collection list") },
1467 { NULL, "--lua:", gr_lua, TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
1469 { NULL, "--new-window", gr_new_window, FALSE, FALSE, NULL, N_("new window") },
1470 { "-n", "--next", gr_image_next, FALSE, FALSE, NULL, N_("next image") },
1471 { NULL, "--pixel-info", gr_pixel_info, FALSE, FALSE, NULL, N_("print pixel info of mouse pointer on current image") },
1472 { NULL, "--print0", gr_print0, TRUE, FALSE, NULL, N_("terminate returned data with null character instead of newline") },
1473 { NULL, "--PWD:", gr_pwd, TRUE, FALSE, N_("<PWD>"), N_("use PWD as working directory for following commands") },
1474 { "-q", "--quit", gr_quit, FALSE, FALSE, NULL, N_("quit") },
1475 { NULL, "--raise", gr_raise, FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
1476 { NULL, "raise", gr_raise, FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
1477 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, NULL, N_("toggle slide show") },
1478 { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE, FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
1479 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, NULL, N_("start slide show") },
1480 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, NULL, N_("stop slide show") },
1481 { NULL, "--tell", gr_file_tell, FALSE, FALSE, NULL, N_("print filename [and Collection] of current image") },
1482 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, NULL, N_("show tools") },
1483 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, NULL, N_("hide tools") },
1484 { NULL, "--view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
1485 { NULL, "view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
1486 { NULL, NULL, NULL, FALSE, FALSE, NULL, NULL }
1489 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
1491 gboolean match = FALSE;
1495 while (!match && remote_commands[i].func != NULL)
1497 if (remote_commands[i].needs_extra)
1499 if (remote_commands[i].opt_s &&
1500 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
1502 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
1503 return &remote_commands[i];
1505 else if (remote_commands[i].opt_l &&
1506 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
1508 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
1509 return &remote_commands[i];
1514 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
1515 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
1517 if (offset) *offset = text;
1518 return &remote_commands[i];
1528 static void remote_cb(RemoteConnection *UNUSED(rc), const gchar *text, GIOChannel *channel, gpointer data)
1530 RemoteCommandEntry *entry;
1531 const gchar *offset;
1533 entry = remote_command_find(text, &offset);
1534 if (entry && entry->func)
1536 entry->func(offset, channel, data);
1540 log_printf("unknown remote command:%s\n", text);
1544 void remote_help(void)
1550 print_term(FALSE, _("Remote command list:\n"));
1553 while (remote_commands[i].func != NULL)
1555 if (remote_commands[i].description)
1557 s_opt_param = g_strdup(remote_commands[i].opt_s ? remote_commands[i].opt_s : "" );
1558 l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
1559 printf_term(FALSE, " %-4s %-40s%-s\n",
1562 _(remote_commands[i].description));
1563 g_free(s_opt_param);
1564 g_free(l_opt_param);
1568 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"));
1571 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
1578 RemoteCommandEntry *entry;
1580 entry = remote_command_find(argv[i], NULL);
1583 list = g_list_append(list, argv[i]);
1585 else if (errors && !isname(argv[i]))
1587 *errors = g_list_append(*errors, argv[i]);
1596 * @param arg_exec Binary (argv0)
1597 * @param remote_list Evaluated and recognized remote commands
1598 * @param path The current path
1599 * @param cmd_list List of all non collections in Path (gchar *path)
1600 * @param collection_list List of all collections in argv
1602 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
1603 GList *cmd_list, GList *collection_list)
1605 RemoteConnection *rc;
1606 gboolean started = FALSE;
1609 buf = g_build_filename(get_rc_dir(), ".command", NULL);
1610 rc = remote_client_open(buf);
1615 gint retry_count = 12;
1616 gboolean blank = FALSE;
1618 printf_term(FALSE, _("Remote %s not running, starting..."), GQ_APPNAME);
1620 command = g_string_new(arg_exec);
1626 RemoteCommandEntry *entry;
1631 entry = remote_command_find(text, NULL);
1634 /* If Geeqie is not running, stop the --new-window command opening a second window */
1635 if (g_strcmp0(text, "--new-window") == 0)
1637 remote_list = g_list_remove(remote_list, text);
1639 if (entry->prefer_command_line)
1641 remote_list = g_list_remove(remote_list, text);
1642 g_string_append(command, " ");
1643 g_string_append(command, text);
1645 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
1652 if (blank || cmd_list || path) g_string_append(command, " --blank");
1653 if (get_debug_level()) g_string_append(command, " --debug");
1655 g_string_append(command, " &");
1656 runcmd(command->str);
1657 g_string_free(command, TRUE);
1659 while (!rc && retry_count > 0)
1661 usleep((retry_count > 10) ? 500000 : 1000000);
1662 rc = remote_client_open(buf);
1663 if (!rc) print_term(FALSE, ".");
1667 print_term(FALSE, "\n");
1676 const gchar *prefix;
1677 gboolean use_path = TRUE;
1678 gboolean sent = FALSE;
1684 RemoteCommandEntry *entry;
1689 entry = remote_command_find(text, NULL);
1692 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
1694 remote_client_send(rc, text);
1699 if (cmd_list && cmd_list->next)
1701 prefix = "--list-add:";
1702 remote_client_send(rc, "--list-clear");
1714 text = g_strconcat(prefix, work->data, NULL);
1715 remote_client_send(rc, text);
1722 if (path && !cmd_list && use_path)
1726 text = g_strdup_printf("file:%s", path);
1727 remote_client_send(rc, text);
1733 work = collection_list;
1742 text = g_strdup_printf("file:%s", name);
1743 remote_client_send(rc, text);
1749 if (!started && !sent)
1751 remote_client_send(rc, "raise");
1756 print_term(TRUE, _("Remote not available\n"));
1762 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
1764 RemoteConnection *remote_connection = remote_server_open(path);
1765 RemoteData *remote_data = g_new(RemoteData, 1);
1767 remote_data->command_collection = command_collection;
1769 remote_server_subscribe(remote_connection, remote_cb, remote_data);
1770 return remote_connection;
1772 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */