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"
31 #include "layout_image.h"
32 #include "layout_util.h"
34 #include "pixbuf-renderer.h"
35 #include "slideshow.h"
36 #include "ui_fileops.h"
39 #include <sys/socket.h>
46 #define SERVER_MAX_CLIENTS 8
48 #define REMOTE_SERVER_BACKLOG 4
52 #define UNIX_PATH_MAX 108
56 static RemoteConnection *remote_client_open(const gchar *path);
57 static gint remote_client_send(RemoteConnection *rc, const gchar *text);
58 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data);
60 static LayoutWindow *lw_id = NULL; /* points to the window set by the --id option */
62 typedef struct _RemoteClient RemoteClient;
63 struct _RemoteClient {
65 guint channel_id; /* event source id */
69 typedef struct _RemoteData RemoteData;
71 CollectionData *command_collection;
75 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
77 RemoteClient *client = data;
79 GIOStatus status = G_IO_STATUS_NORMAL;
84 if (condition & G_IO_IN)
90 while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
94 buffer[termpos] = '\0';
96 if (strlen(buffer) > 0)
98 if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
99 g_io_channel_write_chars(source, "\n", -1, NULL, NULL); /* empty line finishes the command */
100 g_io_channel_flush(source, NULL);
110 log_printf("error reading socket: %s\n", error->message);
115 if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
117 rc->clients = g_list_remove(rc->clients, client);
119 DEBUG_1("HUP detected, closing client.");
120 DEBUG_1("client count %d", g_list_length(rc->clients));
122 g_source_remove(client->channel_id);
130 static void remote_server_client_add(RemoteConnection *rc, gint fd)
132 RemoteClient *client;
135 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
137 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
142 client = g_new0(RemoteClient, 1);
146 channel = g_io_channel_unix_new(fd);
147 client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP,
148 remote_server_client_cb, client, NULL);
149 g_io_channel_unref(channel);
151 rc->clients = g_list_append(rc->clients, client);
152 DEBUG_1("client count %d", g_list_length(rc->clients));
155 static void remote_server_clients_close(RemoteConnection *rc)
159 RemoteClient *client = rc->clients->data;
161 rc->clients = g_list_remove(rc->clients, client);
163 g_source_remove(client->channel_id);
169 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
171 RemoteConnection *rc = data;
175 fd = accept(rc->fd, NULL, &alen);
178 log_printf("error accepting socket: %s\n", strerror(errno));
182 remote_server_client_add(rc, fd);
187 gboolean remote_server_exists(const gchar *path)
189 RemoteConnection *rc;
191 /* verify server up */
192 rc = remote_client_open(path);
197 /* unable to connect, remove socket file to free up address */
202 static RemoteConnection *remote_server_open(const gchar *path)
204 RemoteConnection *rc;
205 struct sockaddr_un addr;
210 if (remote_server_exists(path))
212 log_printf("Address already in use: %s\n", path);
216 fd = socket(PF_UNIX, SOCK_STREAM, 0);
217 if (fd == -1) return NULL;
219 addr.sun_family = AF_UNIX;
220 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
221 strncpy(addr.sun_path, path, sun_path_len);
222 if (bind(fd, &addr, sizeof(addr)) == -1 ||
223 listen(fd, REMOTE_SERVER_BACKLOG) == -1)
225 log_printf("error subscribing to socket: %s\n", strerror(errno));
230 rc = g_new0(RemoteConnection, 1);
234 rc->path = g_strdup(path);
236 channel = g_io_channel_unix_new(rc->fd);
237 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
239 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
240 remote_server_read_cb, rc, NULL);
241 g_io_channel_unref(channel);
246 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
248 if (!rc || !rc->server) return;
250 rc->read_func = func;
251 rc->read_data = data;
255 static RemoteConnection *remote_client_open(const gchar *path)
257 RemoteConnection *rc;
259 struct sockaddr_un addr;
263 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
265 fd = socket(PF_UNIX, SOCK_STREAM, 0);
266 if (fd == -1) return NULL;
268 addr.sun_family = AF_UNIX;
269 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
270 strncpy(addr.sun_path, path, sun_path_len);
271 if (connect(fd, &addr, sizeof(addr)) == -1)
273 DEBUG_1("error connecting to socket: %s", strerror(errno));
278 rc = g_new0(RemoteConnection, 1);
281 rc->path = g_strdup(path);
286 static sig_atomic_t sigpipe_occurred = FALSE;
288 static void sighandler_sigpipe(gint sig)
290 sigpipe_occurred = TRUE;
293 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
295 struct sigaction new_action, old_action;
296 gboolean ret = FALSE;
297 GError *error = NULL;
300 if (!rc || rc->server) return FALSE;
301 if (!text) return TRUE;
303 sigpipe_occurred = FALSE;
305 new_action.sa_handler = sighandler_sigpipe;
306 sigemptyset(&new_action.sa_mask);
307 new_action.sa_flags = 0;
309 /* setup our signal handler */
310 sigaction(SIGPIPE, &new_action, &old_action);
312 channel = g_io_channel_unix_new(rc->fd);
314 g_io_channel_write_chars(channel, text, -1, NULL, &error);
315 g_io_channel_write_chars(channel, "\n", -1, NULL, &error);
316 g_io_channel_flush(channel, &error);
320 log_printf("error reading socket: %s\n", error->message);
331 gchar *buffer = NULL;
333 while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
337 if (buffer[0] == '\n') /* empty line finishes the command */
343 buffer[termpos] = '\0';
344 printf("%s\n", buffer);
352 log_printf("error reading socket: %s\n", error->message);
359 /* restore the original signal handler */
360 sigaction(SIGPIPE, &old_action, NULL);
361 g_io_channel_unref(channel);
365 void remote_close(RemoteConnection *rc)
371 remote_server_clients_close(rc);
373 g_source_remove(rc->channel_id);
378 g_free(rc->read_data);
387 *-----------------------------------------------------------------------------
389 *-----------------------------------------------------------------------------
392 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data)
394 layout_image_next(lw_id);
397 static void gr_new_window(const gchar *text, GIOChannel *channel, gpointer data)
399 LayoutWindow *lw = NULL;
401 if (!layout_valid(&lw)) return;
403 lw_id = layout_menu_new_window(NULL, lw);
406 static gboolean gr_close_window_cb()
408 if (!layout_valid(&lw_id)) return FALSE;
410 layout_menu_close_cb(NULL, lw_id);
415 static void gr_close_window(const gchar *text, GIOChannel *channel, gpointer data)
417 g_idle_add(gr_close_window_cb, NULL);
420 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data)
422 layout_image_prev(lw_id);
425 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data)
427 layout_image_first(lw_id);
430 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data)
432 layout_image_last(lw_id);
435 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data)
437 layout_image_full_screen_toggle(lw_id);
440 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data)
442 layout_image_full_screen_start(lw_id);
445 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data)
447 layout_image_full_screen_stop(lw_id);
450 static void gr_lw_id(const gchar *text, GIOChannel *channel, gpointer data)
452 lw_id = layout_find_by_layout_id(text);
455 log_printf("remote sent window ID that does not exist:\"%s\"\n",text);
457 layout_valid(&lw_id);
460 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *channel, gpointer data)
463 FileData *dir_fd = file_data_new_dir(text);
464 list = filelist_recursive(dir_fd);
465 file_data_unref(dir_fd);
467 //printf("length: %d\n", g_list_length(list));
468 layout_image_slideshow_stop(lw_id);
469 layout_image_slideshow_start_from_list(lw_id, list);
472 static void gr_cache_thumb(const gchar *text, GIOChannel *channel, gpointer data)
474 if (!g_strcmp0(text, "clear"))
475 cache_maintain_home_remote(FALSE, TRUE);
476 else if (!g_strcmp0(text, "clean"))
477 cache_maintain_home_remote(FALSE, FALSE);
480 static void gr_cache_shared(const gchar *text, GIOChannel *channel, gpointer data)
482 if (!g_strcmp0(text, "clear"))
483 cache_manager_standard_process_remote(TRUE);
484 else if (!g_strcmp0(text, "clean"))
485 cache_manager_standard_process_remote(FALSE);
488 static void gr_cache_metadata(const gchar *text, GIOChannel *channel, gpointer data)
490 cache_maintain_home_remote(TRUE, FALSE);
493 static void gr_cache_render(const gchar *text, GIOChannel *channel, gpointer data)
495 cache_manager_render_remote(text, FALSE, FALSE);
498 static void gr_cache_render_recurse(const gchar *text, GIOChannel *channel, gpointer data)
500 cache_manager_render_remote(text, TRUE, FALSE);
503 static void gr_cache_render_standard(const gchar *text, GIOChannel *channel, gpointer data)
505 if(options->thumbnails.spec_standard)
506 cache_manager_render_remote(text, FALSE, TRUE);
509 static void gr_cache_render_standard_recurse(const gchar *text, GIOChannel *channel, gpointer data)
511 if(options->thumbnails.spec_standard)
512 cache_manager_render_remote(text, TRUE, TRUE);
515 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data)
517 layout_image_slideshow_toggle(lw_id);
520 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data)
522 layout_image_slideshow_start(lw_id);
525 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data)
527 layout_image_slideshow_stop(lw_id);
530 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data)
532 gdouble t1, t2, t3, n;
535 res = sscanf(text, "%lf:%lf:%lf", &t1, &t2, &t3);
538 n = (t1 * 3600) + (t2 * 60) + t3;
539 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
540 t1 >= 24 || t2 >= 60 || t3 >= 60)
542 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
543 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
550 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
551 t1 >= 60 || t2 >= 60)
553 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
554 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
561 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
563 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
564 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
573 options->slideshow.delay = (gint)(n * 10.0 + 0.01);
576 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data)
581 if (layout_tools_float_get(lw_id, &popped, &hidden) && hidden)
583 layout_tools_float_set(lw_id, popped, FALSE);
587 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data)
592 if (layout_tools_float_get(lw_id, &popped, &hidden) && !hidden)
594 layout_tools_float_set(lw_id, popped, TRUE);
598 static gboolean gr_quit_idle_cb(gpointer data)
605 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data)
607 /* schedule exit when idle, if done from within a
608 * remote handler remote_close will crash
610 g_idle_add(gr_quit_idle_cb, NULL);
613 static void gr_file_load_no_raise(const gchar *text, GIOChannel *channel, gpointer data)
615 gchar *filename = expand_tilde(text);
617 if (isfile(filename))
619 if (file_extension_match(filename, GQ_COLLECTION_EXT))
621 collection_window_new(filename);
625 layout_set_path(lw_id, filename);
628 else if (isdir(filename))
630 layout_set_path(lw_id, filename);
634 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
635 layout_set_path(lw_id, homedir());
641 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
643 gr_file_load_no_raise(text, channel, data);
645 gr_raise(text, channel, data);
648 static void gr_pixel_info(const gchar *text, GIOChannel *channel, gpointer data)
651 gint x_pixel, y_pixel;
653 gint r_mouse, g_mouse, b_mouse;
655 LayoutWindow *lw = NULL;
657 if (!layout_valid(&lw_id)) return;
659 pr = (PixbufRenderer*)lw_id->image->pr;
663 pixbuf_renderer_get_image_size(pr, &width, &height);
664 if (width < 1 || height < 1) return;
666 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
668 if (x_pixel >= 0 && y_pixel >= 0)
670 pixbuf_renderer_get_pixel_colors(pr, x_pixel, y_pixel,
671 &r_mouse, &g_mouse, &b_mouse);
673 pixel_info = g_strdup_printf(_("[%d,%d]: RGB(%3d,%3d,%3d)"),
675 r_mouse, g_mouse, b_mouse);
677 g_io_channel_write_chars(channel, pixel_info, -1, NULL, NULL);
678 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
693 static void gr_rectangle(const gchar *text, GIOChannel *channel, gpointer data)
695 gchar *rectangle_info;
697 LayoutWindow *lw = NULL;
700 if (!options->draw_rectangle) return;
701 if (!layout_valid(&lw_id)) return;
703 pr = (PixbufRenderer*)lw_id->image->pr;
707 image_get_rectangle(&x1, &y1, &x2, &y2);
708 rectangle_info = g_strdup_printf(_("%dx%d+%d+%d"),
709 (x2 > x1) ? x2 - x1 : x1 - x2,
710 (y2 > y1) ? y2 - y1 : y1 - y2,
712 (y2 > y1) ? y1 : y2);
714 g_io_channel_write_chars(channel, rectangle_info, -1, NULL, NULL);
715 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
717 g_free(rectangle_info);
721 static void gr_render_intent(const gchar *text, GIOChannel *channel, gpointer data)
723 gchar *render_intent;
725 switch (options->color_profile.render_intent)
728 render_intent = g_strdup("Perceptual");
731 render_intent = g_strdup("Relative Colorimetric");
734 render_intent = g_strdup("Saturation");
737 render_intent = g_strdup("Absolute Colorimetric");
740 render_intent = g_strdup("none");
744 g_io_channel_write_chars(channel, render_intent, -1, NULL, NULL);
745 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
747 g_free(render_intent);
750 static void gr_file_tell(const gchar *text, GIOChannel *channel, gpointer data)
752 if (!layout_valid(&lw_id)) return;
754 if (image_get_path(lw_id->image))
756 g_io_channel_write_chars(channel, image_get_path(lw_id->image), -1, NULL, NULL);
757 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
761 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
763 gchar *filename = expand_tilde(text);
765 if (isfile(filename))
767 load_config_from_file(filename, FALSE);
771 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
772 layout_set_path(NULL, homedir());
778 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer data)
780 gchar *filename = expand_tilde(text);
781 FileData *fd = file_data_new_group(filename);
784 if (fd->parent) fd = fd->parent;
786 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
787 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
789 work = fd->sidecar_files;
795 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
796 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
801 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer data)
803 gchar *filename = expand_tilde(text);
804 FileData *fd = file_data_new_group(filename);
806 if (fd->change && fd->change->dest)
808 g_io_channel_write_chars(channel, fd->change->dest, -1, NULL, NULL);
809 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
814 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
816 gchar *filename = expand_tilde(text);
818 view_window_new(file_data_new_group(filename));
822 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
824 RemoteData *remote_data = data;
826 if (remote_data->command_collection)
828 collection_unref(remote_data->command_collection);
829 remote_data->command_collection = NULL;
833 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
835 RemoteData *remote_data = data;
838 if (!remote_data->command_collection)
842 cd = collection_new("");
847 cd->name = g_strdup(_("Command line"));
849 remote_data->command_collection = cd;
853 new = (!collection_get_first(remote_data->command_collection));
856 if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && new)
858 layout_image_set_collection(NULL, remote_data->command_collection,
859 collection_get_first(remote_data->command_collection));
863 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
865 LayoutWindow *lw = NULL;
867 if (layout_valid(&lw_id))
869 gtk_window_present(GTK_WINDOW(lw_id->window));
874 static void gr_lua(const gchar *text, GIOChannel *channel, gpointer data)
876 gchar *result = NULL;
879 lua_command = g_strsplit(text, ",", 2);
881 if (lua_command[0] && lua_command[1])
883 FileData *fd = file_data_new_group(lua_command[0]);
884 result = g_strdup(lua_callvalue(fd, lua_command[1], NULL));
887 g_io_channel_write_chars(channel, result, -1, NULL, NULL);
891 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
896 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
899 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
901 g_strfreev(lua_command);
906 typedef struct _RemoteCommandEntry RemoteCommandEntry;
907 struct _RemoteCommandEntry {
910 void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
911 gboolean needs_extra;
912 gboolean prefer_command_line;
917 static RemoteCommandEntry remote_commands[] = {
918 /* short, long callback, extra, prefer, parameter, description */
919 { "-n", "--next", gr_image_next, FALSE, FALSE, NULL, N_("next image") },
920 { "-b", "--back", gr_image_prev, FALSE, FALSE, NULL, N_("previous image") },
921 { NULL, "--first", gr_image_first, FALSE, FALSE, NULL, N_("first image") },
922 { NULL, "--last", gr_image_last, FALSE, FALSE, NULL, N_("last image") },
923 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, NULL, N_("toggle full screen") },
924 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, NULL, N_("start full screen") },
925 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, NULL, N_("stop full screen") },
926 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, NULL, N_("toggle slide show") },
927 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, NULL, N_("start slide show") },
928 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, NULL, N_("stop slide show") },
929 { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE, FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
930 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("<[H:][M:][N][.M]>"), N_("set slide show delay to Hrs Mins N.M seconds") },
931 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, NULL, N_("show tools") },
932 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, NULL, N_("hide tools") },
933 { "-q", "--quit", gr_quit, FALSE, FALSE, NULL, N_("quit") },
934 { NULL, "--config-load:", gr_config_load, TRUE, FALSE, N_("<FILE>"), N_("load configuration from FILE") },
935 { NULL, "--get-sidecars:", gr_get_sidecars, TRUE, FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
936 { NULL, "--get-destination:", gr_get_destination, TRUE, FALSE, N_("<FILE>"), N_("get destination path of FILE") },
937 { NULL, "file:", gr_file_load, TRUE, FALSE, N_("<FILE>"), N_("open FILE, bring Geeqie window to the top") },
938 { NULL, "File:", gr_file_load_no_raise, TRUE, FALSE, N_("<FILE>"), N_("open FILE, do not bring Geeqie window to the top") },
939 { NULL, "--tell", gr_file_tell, FALSE, FALSE, NULL, N_("print filename of current image") },
940 { NULL, "--pixel-info", gr_pixel_info, FALSE, FALSE, NULL, N_("print pixel info of mouse pointer on current image") },
941 { NULL, "--get-rectangle", gr_rectangle, FALSE, FALSE, NULL, N_("get rectangle co-ordinates") },
942 { NULL, "--get-render-intent", gr_render_intent, FALSE, FALSE, NULL, N_("get render intent") },
943 { NULL, "view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
944 { NULL, "--list-clear", gr_list_clear, FALSE, FALSE, NULL, N_("clear command line collection list") },
945 { NULL, "--list-add:", gr_list_add, TRUE, FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
946 { NULL, "raise", gr_raise, FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
947 { NULL, "--id:", gr_lw_id, TRUE, FALSE, N_("<ID>"), N_("window id for following commands") },
948 { NULL, "--new-window", gr_new_window, FALSE, FALSE, NULL, N_("new window") },
949 { NULL, "--close-window", gr_close_window, FALSE, FALSE, NULL, N_("close window") },
950 { "-ct:", "--cache-thumbs:", gr_cache_thumb, TRUE, FALSE, N_("clear|clean"), N_("clear or clean thumbnail cache") },
951 { "-cs:", "--cache-shared:", gr_cache_shared, TRUE, FALSE, N_("clear|clean"), N_("clear or clean shared thumbnail cache") },
952 { "-cm","--cache-metadata", gr_cache_metadata, FALSE, FALSE, NULL, N_(" clean the metadata cache") },
953 { "-cr:", "--cache-render:", gr_cache_render, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails") },
954 { "-crr:", "--cache-render-recurse:", gr_cache_render_recurse, TRUE, FALSE, N_("<folder> "), N_("render thumbnails recursively") },
955 { "-crs:", "--cache-render-shared:", gr_cache_render_standard, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails (see Help)") },
956 { "-crsr:", "--cache-render-shared-recurse:", gr_cache_render_standard_recurse, TRUE, FALSE, N_("<folder>"), N_(" render thumbnails recursively (see Help)") },
958 { NULL, "--lua:", gr_lua, TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
960 { NULL, NULL, NULL, FALSE, FALSE, NULL, NULL }
963 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
965 gboolean match = FALSE;
969 while (!match && remote_commands[i].func != NULL)
971 if (remote_commands[i].needs_extra)
973 if (remote_commands[i].opt_s &&
974 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
976 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
977 return &remote_commands[i];
979 else if (remote_commands[i].opt_l &&
980 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
982 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
983 return &remote_commands[i];
988 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
989 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
991 if (offset) *offset = text;
992 return &remote_commands[i];
1002 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
1004 RemoteCommandEntry *entry;
1005 const gchar *offset;
1007 entry = remote_command_find(text, &offset);
1008 if (entry && entry->func)
1010 entry->func(offset, channel, data);
1014 log_printf("unknown remote command:%s\n", text);
1018 void remote_help(void)
1024 print_term(FALSE, _("Remote command list:\n"));
1027 while (remote_commands[i].func != NULL)
1029 if (remote_commands[i].description)
1031 s_opt_param = g_strconcat(remote_commands[i].opt_s, remote_commands[i].parameter, NULL);
1032 l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
1033 printf_term(FALSE, " %-11s%-1s %-30s%-s\n",
1034 (remote_commands[i].opt_s) ? s_opt_param : "",
1035 (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
1036 (remote_commands[i].opt_l) ? l_opt_param : "",
1037 _(remote_commands[i].description));
1038 g_free(s_opt_param);
1039 g_free(l_opt_param);
1043 printf_term(FALSE, N_("\n All other command line parameters are used as plain files if they exists.\n"));
1046 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
1053 RemoteCommandEntry *entry;
1055 entry = remote_command_find(argv[i], NULL);
1058 list = g_list_append(list, argv[i]);
1060 else if (errors && !isfile(argv[i]))
1062 *errors = g_list_append(*errors, argv[i]);
1071 * \param arg_exec Binary (argv0)
1072 * \param remote_list Evaluated and recognized remote commands
1073 * \param path The current path
1074 * \param cmd_list List of all non collections in Path
1075 * \param collection_list List of all collections in argv
1077 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
1078 GList *cmd_list, GList *collection_list)
1080 RemoteConnection *rc;
1081 gboolean started = FALSE;
1084 buf = g_build_filename(get_rc_dir(), ".command", NULL);
1085 rc = remote_client_open(buf);
1090 gint retry_count = 12;
1091 gboolean blank = FALSE;
1093 printf_term(FALSE, _("Remote %s not running, starting..."), GQ_APPNAME);
1095 command = g_string_new(arg_exec);
1101 RemoteCommandEntry *entry;
1106 entry = remote_command_find(text, NULL);
1109 if (entry->prefer_command_line)
1111 remote_list = g_list_remove(remote_list, text);
1112 g_string_append(command, " ");
1113 g_string_append(command, text);
1115 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
1122 if (blank || cmd_list || path) g_string_append(command, " --blank");
1123 if (get_debug_level()) g_string_append(command, " --debug");
1125 g_string_append(command, " &");
1126 runcmd(command->str);
1127 g_string_free(command, TRUE);
1129 while (!rc && retry_count > 0)
1131 usleep((retry_count > 10) ? 500000 : 1000000);
1132 rc = remote_client_open(buf);
1133 if (!rc) print_term(FALSE, ".");
1137 print_term(FALSE, "\n");
1146 const gchar *prefix;
1147 gboolean use_path = TRUE;
1148 gboolean sent = FALSE;
1154 RemoteCommandEntry *entry;
1159 entry = remote_command_find(text, NULL);
1162 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
1164 remote_client_send(rc, text);
1169 if (cmd_list && cmd_list->next)
1171 prefix = "--list-add:";
1172 remote_client_send(rc, "--list-clear");
1188 text = g_strconcat(prefix, fd->path, NULL);
1189 remote_client_send(rc, text);
1195 if (path && !cmd_list && use_path)
1199 text = g_strdup_printf("file:%s", path);
1200 remote_client_send(rc, text);
1206 work = collection_list;
1215 text = g_strdup_printf("file:%s", name);
1216 remote_client_send(rc, text);
1222 if (!started && !sent)
1224 remote_client_send(rc, "raise");
1229 print_term(TRUE, _("Remote not available\n"));
1235 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
1237 RemoteConnection *remote_connection = remote_server_open(path);
1238 RemoteData *remote_data = g_new(RemoteData, 1);
1240 remote_data->command_collection = command_collection;
1242 remote_server_subscribe(remote_connection, remote_cb, remote_data);
1243 return remote_connection;
1245 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */