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;
77 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
79 RemoteClient *client = data;
81 GIOStatus status = G_IO_STATUS_NORMAL;
86 if (condition & G_IO_IN)
92 while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
96 buffer[termpos] = '\0';
98 if (strlen(buffer) > 0)
100 if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
101 g_io_channel_write_chars(source, "\n", -1, NULL, NULL); /* empty line finishes the command */
102 g_io_channel_flush(source, NULL);
112 log_printf("error reading socket: %s\n", error->message);
117 if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
119 rc->clients = g_list_remove(rc->clients, client);
121 DEBUG_1("HUP detected, closing client.");
122 DEBUG_1("client count %d", g_list_length(rc->clients));
124 g_source_remove(client->channel_id);
132 static void remote_server_client_add(RemoteConnection *rc, gint fd)
134 RemoteClient *client;
137 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
139 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
144 client = g_new0(RemoteClient, 1);
148 channel = g_io_channel_unix_new(fd);
149 client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP,
150 remote_server_client_cb, client, NULL);
151 g_io_channel_unref(channel);
153 rc->clients = g_list_append(rc->clients, client);
154 DEBUG_1("client count %d", g_list_length(rc->clients));
157 static void remote_server_clients_close(RemoteConnection *rc)
161 RemoteClient *client = rc->clients->data;
163 rc->clients = g_list_remove(rc->clients, client);
165 g_source_remove(client->channel_id);
171 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
173 RemoteConnection *rc = data;
177 fd = accept(rc->fd, NULL, &alen);
180 log_printf("error accepting socket: %s\n", strerror(errno));
184 remote_server_client_add(rc, fd);
189 gboolean remote_server_exists(const gchar *path)
191 RemoteConnection *rc;
193 /* verify server up */
194 rc = remote_client_open(path);
199 /* unable to connect, remove socket file to free up address */
204 static RemoteConnection *remote_server_open(const gchar *path)
206 RemoteConnection *rc;
207 struct sockaddr_un addr;
212 if (remote_server_exists(path))
214 log_printf("Address already in use: %s\n", path);
218 fd = socket(PF_UNIX, SOCK_STREAM, 0);
219 if (fd == -1) return NULL;
221 addr.sun_family = AF_UNIX;
222 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
223 strncpy(addr.sun_path, path, sun_path_len);
224 if (bind(fd, &addr, sizeof(addr)) == -1 ||
225 listen(fd, REMOTE_SERVER_BACKLOG) == -1)
227 log_printf("error subscribing to socket: %s\n", strerror(errno));
232 rc = g_new0(RemoteConnection, 1);
236 rc->path = g_strdup(path);
238 channel = g_io_channel_unix_new(rc->fd);
239 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
241 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
242 remote_server_read_cb, rc, NULL);
243 g_io_channel_unref(channel);
248 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
250 if (!rc || !rc->server) return;
252 rc->read_func = func;
253 rc->read_data = data;
257 static RemoteConnection *remote_client_open(const gchar *path)
259 RemoteConnection *rc;
261 struct sockaddr_un addr;
265 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
267 fd = socket(PF_UNIX, SOCK_STREAM, 0);
268 if (fd == -1) return NULL;
270 addr.sun_family = AF_UNIX;
271 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
272 strncpy(addr.sun_path, path, sun_path_len);
273 if (connect(fd, &addr, sizeof(addr)) == -1)
275 DEBUG_1("error connecting to socket: %s", strerror(errno));
280 rc = g_new0(RemoteConnection, 1);
283 rc->path = g_strdup(path);
288 static sig_atomic_t sigpipe_occurred = FALSE;
290 static void sighandler_sigpipe(gint sig)
292 sigpipe_occurred = TRUE;
295 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
297 struct sigaction new_action, old_action;
298 gboolean ret = FALSE;
299 GError *error = NULL;
302 if (!rc || rc->server) return FALSE;
303 if (!text) return TRUE;
305 sigpipe_occurred = FALSE;
307 new_action.sa_handler = sighandler_sigpipe;
308 sigemptyset(&new_action.sa_mask);
309 new_action.sa_flags = 0;
311 /* setup our signal handler */
312 sigaction(SIGPIPE, &new_action, &old_action);
314 channel = g_io_channel_unix_new(rc->fd);
316 g_io_channel_write_chars(channel, text, -1, NULL, &error);
317 g_io_channel_write_chars(channel, "\n", -1, NULL, &error);
318 g_io_channel_flush(channel, &error);
322 log_printf("error reading socket: %s\n", error->message);
333 gchar *buffer = NULL;
335 while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
339 if (buffer[0] == '\n') /* empty line finishes the command */
345 buffer[termpos] = '\0';
346 printf("%s\n", buffer);
354 log_printf("error reading socket: %s\n", error->message);
361 /* restore the original signal handler */
362 sigaction(SIGPIPE, &old_action, NULL);
363 g_io_channel_unref(channel);
367 void remote_close(RemoteConnection *rc)
373 remote_server_clients_close(rc);
375 g_source_remove(rc->channel_id);
380 g_free(rc->read_data);
389 *-----------------------------------------------------------------------------
391 *-----------------------------------------------------------------------------
394 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data)
396 layout_image_next(lw_id);
399 static void gr_new_window(const gchar *text, GIOChannel *channel, gpointer data)
401 LayoutWindow *lw = NULL;
403 if (!layout_valid(&lw)) return;
405 lw_id = layout_menu_new_window(NULL, lw);
408 static gboolean gr_close_window_cb()
410 if (!layout_valid(&lw_id)) return FALSE;
412 layout_menu_close_cb(NULL, lw_id);
417 static void gr_close_window(const gchar *text, GIOChannel *channel, gpointer data)
419 g_idle_add(gr_close_window_cb, NULL);
422 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data)
424 layout_image_prev(lw_id);
427 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data)
429 layout_image_first(lw_id);
432 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data)
434 layout_image_last(lw_id);
437 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data)
439 layout_image_full_screen_toggle(lw_id);
442 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data)
444 layout_image_full_screen_start(lw_id);
447 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data)
449 layout_image_full_screen_stop(lw_id);
452 static void gr_lw_id(const gchar *text, GIOChannel *channel, gpointer data)
454 lw_id = layout_find_by_layout_id(text);
457 log_printf("remote sent window ID that does not exist:\"%s\"\n",text);
459 layout_valid(&lw_id);
462 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *channel, gpointer data)
465 FileData *dir_fd = file_data_new_dir(text);
466 list = filelist_recursive(dir_fd);
467 file_data_unref(dir_fd);
469 //printf("length: %d\n", g_list_length(list));
470 layout_image_slideshow_stop(lw_id);
471 layout_image_slideshow_start_from_list(lw_id, list);
474 static void gr_cache_thumb(const gchar *text, GIOChannel *channel, gpointer data)
476 if (!g_strcmp0(text, "clear"))
477 cache_maintain_home_remote(FALSE, TRUE);
478 else if (!g_strcmp0(text, "clean"))
479 cache_maintain_home_remote(FALSE, FALSE);
482 static void gr_cache_shared(const gchar *text, GIOChannel *channel, gpointer data)
484 if (!g_strcmp0(text, "clear"))
485 cache_manager_standard_process_remote(TRUE);
486 else if (!g_strcmp0(text, "clean"))
487 cache_manager_standard_process_remote(FALSE);
490 static void gr_cache_metadata(const gchar *text, GIOChannel *channel, gpointer data)
492 cache_maintain_home_remote(TRUE, FALSE);
495 static void gr_cache_render(const gchar *text, GIOChannel *channel, gpointer data)
497 cache_manager_render_remote(text, FALSE, FALSE);
500 static void gr_cache_render_recurse(const gchar *text, GIOChannel *channel, gpointer data)
502 cache_manager_render_remote(text, TRUE, FALSE);
505 static void gr_cache_render_standard(const gchar *text, GIOChannel *channel, gpointer data)
507 if(options->thumbnails.spec_standard)
508 cache_manager_render_remote(text, FALSE, TRUE);
511 static void gr_cache_render_standard_recurse(const gchar *text, GIOChannel *channel, gpointer data)
513 if(options->thumbnails.spec_standard)
514 cache_manager_render_remote(text, TRUE, TRUE);
517 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data)
519 layout_image_slideshow_toggle(lw_id);
522 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data)
524 layout_image_slideshow_start(lw_id);
527 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data)
529 layout_image_slideshow_stop(lw_id);
532 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data)
534 gdouble t1, t2, t3, n;
537 res = sscanf(text, "%lf:%lf:%lf", &t1, &t2, &t3);
540 n = (t1 * 3600) + (t2 * 60) + t3;
541 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
542 t1 >= 24 || t2 >= 60 || t3 >= 60)
544 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
545 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
552 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
553 t1 >= 60 || t2 >= 60)
555 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
556 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
563 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
565 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
566 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
575 options->slideshow.delay = (gint)(n * 10.0 + 0.01);
578 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data)
583 if (layout_tools_float_get(lw_id, &popped, &hidden) && hidden)
585 layout_tools_float_set(lw_id, popped, FALSE);
589 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data)
594 if (layout_tools_float_get(lw_id, &popped, &hidden) && !hidden)
596 layout_tools_float_set(lw_id, popped, TRUE);
600 static gboolean gr_quit_idle_cb(gpointer data)
607 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data)
609 /* schedule exit when idle, if done from within a
610 * remote handler remote_close will crash
612 g_idle_add(gr_quit_idle_cb, NULL);
615 static void gr_file_load_no_raise(const gchar *text, GIOChannel *channel, gpointer data)
617 gchar *filename = expand_tilde(text);
619 if (isfile(filename))
621 if (file_extension_match(filename, GQ_COLLECTION_EXT))
623 collection_window_new(filename);
627 layout_set_path(lw_id, filename);
630 else if (isdir(filename))
632 layout_set_path(lw_id, filename);
636 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
637 layout_set_path(lw_id, homedir());
643 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
645 gr_file_load_no_raise(text, channel, data);
647 gr_raise(text, channel, data);
650 static void gr_pixel_info(const gchar *text, GIOChannel *channel, gpointer data)
653 gint x_pixel, y_pixel;
655 gint r_mouse, g_mouse, b_mouse;
657 LayoutWindow *lw = NULL;
659 if (!layout_valid(&lw_id)) return;
661 pr = (PixbufRenderer*)lw_id->image->pr;
665 pixbuf_renderer_get_image_size(pr, &width, &height);
666 if (width < 1 || height < 1) return;
668 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
670 if (x_pixel >= 0 && y_pixel >= 0)
672 pixbuf_renderer_get_pixel_colors(pr, x_pixel, y_pixel,
673 &r_mouse, &g_mouse, &b_mouse);
675 pixel_info = g_strdup_printf(_("[%d,%d]: RGB(%3d,%3d,%3d)"),
677 r_mouse, g_mouse, b_mouse);
679 g_io_channel_write_chars(channel, pixel_info, -1, NULL, NULL);
680 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
695 static void gr_rectangle(const gchar *text, GIOChannel *channel, gpointer data)
697 gchar *rectangle_info;
699 LayoutWindow *lw = NULL;
702 if (!options->draw_rectangle) return;
703 if (!layout_valid(&lw_id)) return;
705 pr = (PixbufRenderer*)lw_id->image->pr;
709 image_get_rectangle(&x1, &y1, &x2, &y2);
710 rectangle_info = g_strdup_printf(_("%dx%d+%d+%d"),
711 (x2 > x1) ? x2 - x1 : x1 - x2,
712 (y2 > y1) ? y2 - y1 : y1 - y2,
714 (y2 > y1) ? y1 : y2);
716 g_io_channel_write_chars(channel, rectangle_info, -1, NULL, NULL);
717 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
719 g_free(rectangle_info);
723 static void gr_render_intent(const gchar *text, GIOChannel *channel, gpointer data)
725 gchar *render_intent;
727 switch (options->color_profile.render_intent)
730 render_intent = g_strdup("Perceptual");
733 render_intent = g_strdup("Relative Colorimetric");
736 render_intent = g_strdup("Saturation");
739 render_intent = g_strdup("Absolute Colorimetric");
742 render_intent = g_strdup("none");
746 g_io_channel_write_chars(channel, render_intent, -1, NULL, NULL);
747 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
749 g_free(render_intent);
752 static void get_filelist(const gchar *text, GIOChannel *channel, gboolean recurse)
755 FileFormatClass class;
758 GString *out_string = g_string_new(NULL);
761 if (strcmp(text, "") == 0)
763 if (layout_valid(&lw_id))
765 dir_fd = file_data_new_dir(lw_id->dir_fd->path);
776 dir_fd = file_data_new_dir(text);
786 list = filelist_recursive(dir_fd);
790 filelist_read(dir_fd, &list, NULL);
797 g_string_append_printf(out_string, "%s", fd->path);
798 class = filter_file_get_class(fd->path);
802 case FORMAT_CLASS_IMAGE:
803 out_string = g_string_append(out_string, " Class: Image");
805 case FORMAT_CLASS_RAWIMAGE:
806 out_string = g_string_append(out_string, " Class: RAW image");
808 case FORMAT_CLASS_META:
809 out_string = g_string_append(out_string, " Class: Metadata");
811 case FORMAT_CLASS_VIDEO:
812 out_string = g_string_append(out_string, " Class: Video");
814 case FORMAT_CLASS_COLLECTION:
815 out_string = g_string_append(out_string, " Class: Collection");
817 case FORMAT_CLASS_PDF:
818 out_string = g_string_append(out_string, " Class: PDF");
820 case FORMAT_CLASS_UNKNOWN:
821 out_string = g_string_append(out_string, " Class: Unknown");
824 out_string = g_string_append(out_string, " Class: Unknown");
827 out_string = g_string_append(out_string, "\n");
831 g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
832 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
834 g_string_free(out_string, TRUE);
836 file_data_unref(dir_fd);
839 static void gr_collection(const gchar *text, GIOChannel *channel, gpointer data)
841 GString *contents = g_string_new(NULL);
843 if (is_collection(text))
845 collection_contents(text, &contents);
852 g_io_channel_write_chars(channel, contents->str, -1, NULL, NULL);
853 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
855 g_string_free(contents, TRUE);
858 static void gr_collection_list(const gchar *text, GIOChannel *channel, gpointer data)
861 GList *collection_list = NULL;
863 GString *out_string = g_string_new(NULL);
865 collect_manager_list(&collection_list, NULL, NULL);
867 work = collection_list;
870 const gchar *collection_name = work->data;
871 out_string = g_string_append(out_string, g_strdup(collection_name));
872 out_string = g_string_append(out_string, "\n");
877 g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
878 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
880 string_list_free(collection_list);
881 g_string_free(out_string, TRUE);
885 static void gr_filelist(const gchar *text, GIOChannel *channel, gpointer data)
887 get_filelist(text, channel, FALSE);
890 static void gr_filelist_recurse(const gchar *text, GIOChannel *channel, gpointer data)
892 get_filelist(text, channel, TRUE);
895 static void gr_file_tell(const gchar *text, GIOChannel *channel, gpointer data)
898 gchar *collection_name = NULL;
900 if (!layout_valid(&lw_id)) return;
902 if (image_get_path(lw_id->image))
904 if (lw_id->image->collection && lw_id->image->collection->name)
906 collection_name = remove_extension_from_path(lw_id->image->collection->name);
907 out_string = g_strconcat(image_get_path(lw_id->image), " Collection: ", collection_name, NULL);
911 out_string = g_strconcat(image_get_path(lw_id->image), NULL);
914 g_io_channel_write_chars(channel, out_string, -1, NULL, NULL);
915 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
917 g_free(collection_name);
922 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
924 gchar *filename = expand_tilde(text);
926 if (isfile(filename))
928 load_config_from_file(filename, FALSE);
932 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
933 layout_set_path(NULL, homedir());
939 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer data)
941 gchar *filename = expand_tilde(text);
942 FileData *fd = file_data_new_group(filename);
945 if (fd->parent) fd = fd->parent;
947 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
948 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
950 work = fd->sidecar_files;
956 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
957 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
962 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer data)
964 gchar *filename = expand_tilde(text);
965 FileData *fd = file_data_new_group(filename);
967 if (fd->change && fd->change->dest)
969 g_io_channel_write_chars(channel, fd->change->dest, -1, NULL, NULL);
970 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
975 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
977 gchar *filename = expand_tilde(text);
979 view_window_new(file_data_new_group(filename));
983 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
985 RemoteData *remote_data = data;
987 if (remote_data->command_collection)
989 collection_unref(remote_data->command_collection);
990 remote_data->command_collection = NULL;
994 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
996 RemoteData *remote_data = data;
999 if (!remote_data->command_collection)
1003 cd = collection_new("");
1008 cd->name = g_strdup(_("Command line"));
1010 remote_data->command_collection = cd;
1014 new = (!collection_get_first(remote_data->command_collection));
1017 if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && new)
1019 layout_image_set_collection(NULL, remote_data->command_collection,
1020 collection_get_first(remote_data->command_collection));
1024 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
1026 LayoutWindow *lw = NULL;
1028 if (layout_valid(&lw_id))
1030 gtk_window_present(GTK_WINDOW(lw_id->window));
1035 static void gr_lua(const gchar *text, GIOChannel *channel, gpointer data)
1037 gchar *result = NULL;
1038 gchar **lua_command;
1040 lua_command = g_strsplit(text, ",", 2);
1042 if (lua_command[0] && lua_command[1])
1044 FileData *fd = file_data_new_group(lua_command[0]);
1045 result = g_strdup(lua_callvalue(fd, lua_command[1], NULL));
1048 g_io_channel_write_chars(channel, result, -1, NULL, NULL);
1052 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
1057 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
1060 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
1062 g_strfreev(lua_command);
1067 typedef struct _RemoteCommandEntry RemoteCommandEntry;
1068 struct _RemoteCommandEntry {
1071 void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
1072 gboolean needs_extra;
1073 gboolean prefer_command_line;
1078 static RemoteCommandEntry remote_commands[] = {
1079 /* short, long callback, extra, prefer, parameter, description */
1080 { "-n", "--next", gr_image_next, FALSE, FALSE, NULL, N_("next image") },
1081 { "-b", "--back", gr_image_prev, FALSE, FALSE, NULL, N_("previous image") },
1082 { NULL, "--first", gr_image_first, FALSE, FALSE, NULL, N_("first image") },
1083 { NULL, "--last", gr_image_last, FALSE, FALSE, NULL, N_("last image") },
1084 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, NULL, N_("toggle full screen") },
1085 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, NULL, N_("start full screen") },
1086 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, NULL, N_("stop full screen") },
1087 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, NULL, N_("toggle slide show") },
1088 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, NULL, N_("start slide show") },
1089 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, NULL, N_("stop slide show") },
1090 { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE, FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
1091 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("<[H:][M:][N][.M]>"), N_("set slide show delay to Hrs Mins N.M seconds") },
1092 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, NULL, N_("show tools") },
1093 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, NULL, N_("hide tools") },
1094 { "-q", "--quit", gr_quit, FALSE, FALSE, NULL, N_("quit") },
1095 { NULL, "--config-load:", gr_config_load, TRUE, FALSE, N_("<FILE>"), N_("load configuration from FILE") },
1096 { NULL, "--get-sidecars:", gr_get_sidecars, TRUE, FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
1097 { NULL, "--get-destination:", gr_get_destination, TRUE, FALSE, N_("<FILE>"), N_("get destination path of FILE") },
1098 { NULL, "file:", gr_file_load, TRUE, FALSE, N_("<FILE>"), N_("open FILE, bring Geeqie window to the top") },
1099 { NULL, "File:", gr_file_load_no_raise, TRUE, FALSE, N_("<FILE>"), N_("open FILE, do not bring Geeqie window to the top") },
1100 { NULL, "--tell", gr_file_tell, FALSE, FALSE, NULL, N_("print filename [and Collection] of current image") },
1101 { NULL, "--pixel-info", gr_pixel_info, FALSE, FALSE, NULL, N_("print pixel info of mouse pointer on current image") },
1102 { NULL, "--get-rectangle", gr_rectangle, FALSE, FALSE, NULL, N_("get rectangle co-ordinates") },
1103 { NULL, "--get-render-intent", gr_render_intent, FALSE, FALSE, NULL, N_("get render intent") },
1104 { NULL, "--get-filelist:", gr_filelist, TRUE, FALSE, N_("[<FOLDER>]"), N_("get list of files and class") },
1105 { NULL, "--get-filelist-recurse:", gr_filelist_recurse, TRUE, FALSE, N_("[<FOLDER>]"), N_("get list of files and class recursive") },
1106 { NULL, "--get-collection:", gr_collection, TRUE, FALSE, N_("<COLLECTION>"), N_("get collection content") },
1107 { NULL, "--get-collection-list", gr_collection_list, FALSE, FALSE, NULL, N_("get collection list") },
1108 { NULL, "view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
1109 { NULL, "--list-clear", gr_list_clear, FALSE, FALSE, NULL, N_("clear command line collection list") },
1110 { NULL, "--list-add:", gr_list_add, TRUE, FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
1111 { NULL, "raise", gr_raise, FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
1112 { NULL, "--id:", gr_lw_id, TRUE, FALSE, N_("<ID>"), N_("window id for following commands") },
1113 { NULL, "--new-window", gr_new_window, FALSE, FALSE, NULL, N_("new window") },
1114 { NULL, "--close-window", gr_close_window, FALSE, FALSE, NULL, N_("close window") },
1115 { "-ct:", "--cache-thumbs:", gr_cache_thumb, TRUE, FALSE, N_("clear|clean"), N_("clear or clean thumbnail cache") },
1116 { "-cs:", "--cache-shared:", gr_cache_shared, TRUE, FALSE, N_("clear|clean"), N_("clear or clean shared thumbnail cache") },
1117 { "-cm","--cache-metadata", gr_cache_metadata, FALSE, FALSE, NULL, N_(" clean the metadata cache") },
1118 { "-cr:", "--cache-render:", gr_cache_render, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails") },
1119 { "-crr:", "--cache-render-recurse:", gr_cache_render_recurse, TRUE, FALSE, N_("<folder> "), N_("render thumbnails recursively") },
1120 { "-crs:", "--cache-render-shared:", gr_cache_render_standard, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails (see Help)") },
1121 { "-crsr:", "--cache-render-shared-recurse:", gr_cache_render_standard_recurse, TRUE, FALSE, N_("<folder>"), N_(" render thumbnails recursively (see Help)") },
1123 { NULL, "--lua:", gr_lua, TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
1125 { NULL, NULL, NULL, FALSE, FALSE, NULL, NULL }
1128 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
1130 gboolean match = FALSE;
1134 while (!match && remote_commands[i].func != NULL)
1136 if (remote_commands[i].needs_extra)
1138 if (remote_commands[i].opt_s &&
1139 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
1141 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
1142 return &remote_commands[i];
1144 else if (remote_commands[i].opt_l &&
1145 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
1147 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
1148 return &remote_commands[i];
1153 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
1154 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
1156 if (offset) *offset = text;
1157 return &remote_commands[i];
1167 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
1169 RemoteCommandEntry *entry;
1170 const gchar *offset;
1172 entry = remote_command_find(text, &offset);
1173 if (entry && entry->func)
1175 entry->func(offset, channel, data);
1179 log_printf("unknown remote command:%s\n", text);
1183 void remote_help(void)
1189 print_term(FALSE, _("Remote command list:\n"));
1192 while (remote_commands[i].func != NULL)
1194 if (remote_commands[i].description)
1196 s_opt_param = g_strconcat(remote_commands[i].opt_s, remote_commands[i].parameter, NULL);
1197 l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
1198 printf_term(FALSE, " %-11s%-1s %-30s%-s\n",
1199 (remote_commands[i].opt_s) ? s_opt_param : "",
1200 (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
1201 (remote_commands[i].opt_l) ? l_opt_param : "",
1202 _(remote_commands[i].description));
1203 g_free(s_opt_param);
1204 g_free(l_opt_param);
1208 printf_term(FALSE, N_("\n All other command line parameters are used as plain files if they exists.\n"));
1211 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
1218 RemoteCommandEntry *entry;
1220 entry = remote_command_find(argv[i], NULL);
1223 list = g_list_append(list, argv[i]);
1225 else if (errors && !isfile(argv[i]))
1227 *errors = g_list_append(*errors, argv[i]);
1236 * \param arg_exec Binary (argv0)
1237 * \param remote_list Evaluated and recognized remote commands
1238 * \param path The current path
1239 * \param cmd_list List of all non collections in Path
1240 * \param collection_list List of all collections in argv
1242 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
1243 GList *cmd_list, GList *collection_list)
1245 RemoteConnection *rc;
1246 gboolean started = FALSE;
1249 buf = g_build_filename(get_rc_dir(), ".command", NULL);
1250 rc = remote_client_open(buf);
1255 gint retry_count = 12;
1256 gboolean blank = FALSE;
1258 printf_term(FALSE, _("Remote %s not running, starting..."), GQ_APPNAME);
1260 command = g_string_new(arg_exec);
1266 RemoteCommandEntry *entry;
1271 entry = remote_command_find(text, NULL);
1274 if (entry->prefer_command_line)
1276 remote_list = g_list_remove(remote_list, text);
1277 g_string_append(command, " ");
1278 g_string_append(command, text);
1280 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
1287 if (blank || cmd_list || path) g_string_append(command, " --blank");
1288 if (get_debug_level()) g_string_append(command, " --debug");
1290 g_string_append(command, " &");
1291 runcmd(command->str);
1292 g_string_free(command, TRUE);
1294 while (!rc && retry_count > 0)
1296 usleep((retry_count > 10) ? 500000 : 1000000);
1297 rc = remote_client_open(buf);
1298 if (!rc) print_term(FALSE, ".");
1302 print_term(FALSE, "\n");
1311 const gchar *prefix;
1312 gboolean use_path = TRUE;
1313 gboolean sent = FALSE;
1319 RemoteCommandEntry *entry;
1324 entry = remote_command_find(text, NULL);
1327 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
1329 remote_client_send(rc, text);
1334 if (cmd_list && cmd_list->next)
1336 prefix = "--list-add:";
1337 remote_client_send(rc, "--list-clear");
1353 text = g_strconcat(prefix, fd->path, NULL);
1354 remote_client_send(rc, text);
1360 if (path && !cmd_list && use_path)
1364 text = g_strdup_printf("file:%s", path);
1365 remote_client_send(rc, text);
1371 work = collection_list;
1380 text = g_strdup_printf("file:%s", name);
1381 remote_client_send(rc, text);
1387 if (!started && !sent)
1389 remote_client_send(rc, "raise");
1394 print_term(TRUE, _("Remote not available\n"));
1400 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
1402 RemoteConnection *remote_connection = remote_server_open(path);
1403 RemoteData *remote_data = g_new(RemoteData, 1);
1405 remote_data->command_collection = command_collection;
1407 remote_server_subscribe(remote_connection, remote_cb, remote_data);
1408 return remote_connection;
1410 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */