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"
33 #include "pixbuf-renderer.h"
34 #include "slideshow.h"
35 #include "ui_fileops.h"
38 #include <sys/socket.h>
45 #define SERVER_MAX_CLIENTS 8
47 #define REMOTE_SERVER_BACKLOG 4
51 #define UNIX_PATH_MAX 108
55 static RemoteConnection *remote_client_open(const gchar *path);
56 static gint remote_client_send(RemoteConnection *rc, const gchar *text);
57 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data);
60 typedef struct _RemoteClient RemoteClient;
61 struct _RemoteClient {
63 guint channel_id; /* event source id */
67 typedef struct _RemoteData RemoteData;
69 CollectionData *command_collection;
73 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
75 RemoteClient *client = data;
77 GIOStatus status = G_IO_STATUS_NORMAL;
81 if (condition & G_IO_IN)
87 while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
91 buffer[termpos] = '\0';
93 if (strlen(buffer) > 0)
95 if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
96 g_io_channel_write_chars(source, "\n", -1, NULL, NULL); /* empty line finishes the command */
97 g_io_channel_flush(source, NULL);
107 log_printf("error reading socket: %s\n", error->message);
112 if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
114 rc->clients = g_list_remove(rc->clients, client);
116 DEBUG_1("HUP detected, closing client.");
117 DEBUG_1("client count %d", g_list_length(rc->clients));
119 g_source_remove(client->channel_id);
127 static void remote_server_client_add(RemoteConnection *rc, gint fd)
129 RemoteClient *client;
132 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
134 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
139 client = g_new0(RemoteClient, 1);
143 channel = g_io_channel_unix_new(fd);
144 client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP,
145 remote_server_client_cb, client, NULL);
146 g_io_channel_unref(channel);
148 rc->clients = g_list_append(rc->clients, client);
149 DEBUG_1("client count %d", g_list_length(rc->clients));
152 static void remote_server_clients_close(RemoteConnection *rc)
156 RemoteClient *client = rc->clients->data;
158 rc->clients = g_list_remove(rc->clients, client);
160 g_source_remove(client->channel_id);
166 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
168 RemoteConnection *rc = data;
172 fd = accept(rc->fd, NULL, &alen);
175 log_printf("error accepting socket: %s\n", strerror(errno));
179 remote_server_client_add(rc, fd);
184 gboolean remote_server_exists(const gchar *path)
186 RemoteConnection *rc;
188 /* verify server up */
189 rc = remote_client_open(path);
194 /* unable to connect, remove socket file to free up address */
199 static RemoteConnection *remote_server_open(const gchar *path)
201 RemoteConnection *rc;
202 struct sockaddr_un addr;
207 if (remote_server_exists(path))
209 log_printf("Address already in use: %s\n", path);
213 fd = socket(PF_UNIX, SOCK_STREAM, 0);
214 if (fd == -1) return NULL;
216 addr.sun_family = AF_UNIX;
217 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
218 strncpy(addr.sun_path, path, sun_path_len);
219 if (bind(fd, &addr, sizeof(addr)) == -1 ||
220 listen(fd, REMOTE_SERVER_BACKLOG) == -1)
222 log_printf("error subscribing to socket: %s\n", strerror(errno));
227 rc = g_new0(RemoteConnection, 1);
231 rc->path = g_strdup(path);
233 channel = g_io_channel_unix_new(rc->fd);
234 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
236 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
237 remote_server_read_cb, rc, NULL);
238 g_io_channel_unref(channel);
243 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
245 if (!rc || !rc->server) return;
247 rc->read_func = func;
248 rc->read_data = data;
252 static RemoteConnection *remote_client_open(const gchar *path)
254 RemoteConnection *rc;
256 struct sockaddr_un addr;
260 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
262 fd = socket(PF_UNIX, SOCK_STREAM, 0);
263 if (fd == -1) return NULL;
265 addr.sun_family = AF_UNIX;
266 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
267 strncpy(addr.sun_path, path, sun_path_len);
268 if (connect(fd, &addr, sizeof(addr)) == -1)
270 DEBUG_1("error connecting to socket: %s", strerror(errno));
275 rc = g_new0(RemoteConnection, 1);
278 rc->path = g_strdup(path);
283 static sig_atomic_t sigpipe_occurred = FALSE;
285 static void sighandler_sigpipe(gint sig)
287 sigpipe_occurred = TRUE;
290 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
292 struct sigaction new_action, old_action;
293 gboolean ret = FALSE;
294 GError *error = NULL;
297 if (!rc || rc->server) return FALSE;
298 if (!text) return TRUE;
300 sigpipe_occurred = FALSE;
302 new_action.sa_handler = sighandler_sigpipe;
303 sigemptyset(&new_action.sa_mask);
304 new_action.sa_flags = 0;
306 /* setup our signal handler */
307 sigaction(SIGPIPE, &new_action, &old_action);
309 channel = g_io_channel_unix_new(rc->fd);
311 g_io_channel_write_chars(channel, text, -1, NULL, &error);
312 g_io_channel_write_chars(channel, "\n", -1, NULL, &error);
313 g_io_channel_flush(channel, &error);
317 log_printf("error reading socket: %s\n", error->message);
328 gchar *buffer = NULL;
330 while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
334 if (buffer[0] == '\n') /* empty line finishes the command */
340 buffer[termpos] = '\0';
341 printf("%s\n", buffer);
349 log_printf("error reading socket: %s\n", error->message);
356 /* restore the original signal handler */
357 sigaction(SIGPIPE, &old_action, NULL);
358 g_io_channel_unref(channel);
362 void remote_close(RemoteConnection *rc)
368 remote_server_clients_close(rc);
370 g_source_remove(rc->channel_id);
375 g_free(rc->read_data);
384 *-----------------------------------------------------------------------------
386 *-----------------------------------------------------------------------------
389 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data)
391 layout_image_next(NULL);
394 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data)
396 layout_image_prev(NULL);
399 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data)
401 layout_image_first(NULL);
404 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data)
406 layout_image_last(NULL);
409 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data)
411 layout_image_full_screen_toggle(NULL);
414 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data)
416 layout_image_full_screen_start(NULL);
419 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data)
421 layout_image_full_screen_stop(NULL);
424 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *channel, gpointer data)
427 FileData *dir_fd = file_data_new_dir(text);
428 list = filelist_recursive(dir_fd);
429 file_data_unref(dir_fd);
431 //printf("length: %d\n", g_list_length(list));
432 layout_image_slideshow_stop(NULL);
433 layout_image_slideshow_start_from_list(NULL, list);
436 static void gr_cache_thumb(const gchar *text, GIOChannel *channel, gpointer data)
438 if (!g_strcmp0(text, "clear"))
439 cache_maintain_home_remote(FALSE, TRUE);
440 else if (!g_strcmp0(text, "clean"))
441 cache_maintain_home_remote(FALSE, FALSE);
444 static void gr_cache_shared(const gchar *text, GIOChannel *channel, gpointer data)
446 if (!g_strcmp0(text, "clear"))
447 cache_manager_standard_process_remote(TRUE);
448 else if (!g_strcmp0(text, "clean"))
449 cache_manager_standard_process_remote(FALSE);
452 static void gr_cache_metadata(const gchar *text, GIOChannel *channel, gpointer data)
454 cache_maintain_home_remote(TRUE, FALSE);
457 static void gr_cache_render(const gchar *text, GIOChannel *channel, gpointer data)
459 cache_manager_render_remote(text, FALSE, FALSE);
462 static void gr_cache_render_recurse(const gchar *text, GIOChannel *channel, gpointer data)
464 cache_manager_render_remote(text, TRUE, FALSE);
467 static void gr_cache_render_standard(const gchar *text, GIOChannel *channel, gpointer data)
469 if(options->thumbnails.spec_standard)
470 cache_manager_render_remote(text, FALSE, TRUE);
473 static void gr_cache_render_standard_recurse(const gchar *text, GIOChannel *channel, gpointer data)
475 if(options->thumbnails.spec_standard)
476 cache_manager_render_remote(text, TRUE, TRUE);
479 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data)
481 layout_image_slideshow_toggle(NULL);
484 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data)
486 layout_image_slideshow_start(NULL);
489 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data)
491 layout_image_slideshow_stop(NULL);
494 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data)
496 gdouble t1, t2, t3, n;
499 res = sscanf(text, "%lf:%lf:%lf", &t1, &t2, &t3);
502 n = (t1 * 3600) + (t2 * 60) + t3;
503 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
504 t1 >= 24 || t2 >= 60 || t3 >= 60)
506 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
507 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
514 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
515 t1 >= 60 || t2 >= 60)
517 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
518 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
525 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
527 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
528 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
537 options->slideshow.delay = (gint)(n * 10.0 + 0.01);
540 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data)
545 if (layout_tools_float_get(NULL, &popped, &hidden) && hidden)
547 layout_tools_float_set(NULL, popped, FALSE);
551 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data)
556 if (layout_tools_float_get(NULL, &popped, &hidden) && !hidden)
558 layout_tools_float_set(NULL, popped, TRUE);
562 static gboolean gr_quit_idle_cb(gpointer data)
569 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data)
571 /* schedule exit when idle, if done from within a
572 * remote handler remote_close will crash
574 g_idle_add(gr_quit_idle_cb, NULL);
577 static void gr_file_load_no_raise(const gchar *text, GIOChannel *channel, gpointer data)
579 gchar *filename = expand_tilde(text);
581 if (isfile(filename))
583 if (file_extension_match(filename, GQ_COLLECTION_EXT))
585 collection_window_new(filename);
589 layout_set_path(NULL, filename);
592 else if (isdir(filename))
594 layout_set_path(NULL, filename);
598 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
599 layout_set_path(NULL, homedir());
605 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
607 gr_file_load_no_raise(text, channel, data);
609 gr_raise(text, channel, data);
612 static void gr_pixel_info(const gchar *text, GIOChannel *channel, gpointer data)
615 gint x_pixel, y_pixel;
617 gint r_mouse, g_mouse, b_mouse;
619 LayoutWindow *lw = NULL;
621 if (!layout_valid(&lw)) return;
623 pr = (PixbufRenderer*)lw->image->pr;
627 pixbuf_renderer_get_image_size(pr, &width, &height);
628 if (width < 1 || height < 1) return;
630 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
632 if (x_pixel >= 0 && y_pixel >= 0)
634 pixbuf_renderer_get_pixel_colors(pr, x_pixel, y_pixel,
635 &r_mouse, &g_mouse, &b_mouse);
637 pixel_info = g_strdup_printf(_("[%d,%d]: RGB(%3d,%3d,%3d)"),
639 r_mouse, g_mouse, b_mouse);
641 g_io_channel_write_chars(channel, pixel_info, -1, NULL, NULL);
642 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
657 static void gr_file_tell(const gchar *text, GIOChannel *channel, gpointer data)
659 LayoutWindow *lw = NULL; /* NULL to force layout_valid() to do some magic */
660 if (!layout_valid(&lw)) return;
661 if (image_get_path(lw->image))
663 g_io_channel_write_chars(channel, image_get_path(lw->image), -1, NULL, NULL);
664 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
668 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
670 gchar *filename = expand_tilde(text);
672 if (isfile(filename))
674 load_config_from_file(filename, FALSE);
678 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
679 layout_set_path(NULL, homedir());
685 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer data)
687 gchar *filename = expand_tilde(text);
688 FileData *fd = file_data_new_group(filename);
691 if (fd->parent) fd = fd->parent;
693 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
694 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
696 work = fd->sidecar_files;
702 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
703 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
708 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer data)
710 gchar *filename = expand_tilde(text);
711 FileData *fd = file_data_new_group(filename);
713 if (fd->change && fd->change->dest)
715 g_io_channel_write_chars(channel, fd->change->dest, -1, NULL, NULL);
716 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
721 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
723 gchar *filename = expand_tilde(text);
725 view_window_new(file_data_new_group(filename));
729 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
731 RemoteData *remote_data = data;
733 if (remote_data->command_collection)
735 collection_unref(remote_data->command_collection);
736 remote_data->command_collection = NULL;
740 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
742 RemoteData *remote_data = data;
745 if (!remote_data->command_collection)
749 cd = collection_new("");
754 cd->name = g_strdup(_("Command line"));
756 remote_data->command_collection = cd;
760 new = (!collection_get_first(remote_data->command_collection));
763 if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && new)
765 layout_image_set_collection(NULL, remote_data->command_collection,
766 collection_get_first(remote_data->command_collection));
770 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
772 LayoutWindow *lw = NULL;
774 if (layout_valid(&lw))
776 gtk_window_present(GTK_WINDOW(lw->window));
781 static void gr_lua(const gchar *text, GIOChannel *channel, gpointer data)
783 gchar *result = NULL;
786 lua_command = g_strsplit(text, ",", 2);
788 if (lua_command[0] && lua_command[1])
790 FileData *fd = file_data_new_group(lua_command[0]);
791 result = g_strdup(lua_callvalue(fd, lua_command[1], NULL));
794 g_io_channel_write_chars(channel, result, -1, NULL, NULL);
798 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
803 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
806 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
808 g_strfreev(lua_command);
813 typedef struct _RemoteCommandEntry RemoteCommandEntry;
814 struct _RemoteCommandEntry {
817 void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
818 gboolean needs_extra;
819 gboolean prefer_command_line;
824 static RemoteCommandEntry remote_commands[] = {
825 /* short, long callback, extra, prefer, parameter, description */
826 { "-n", "--next", gr_image_next, FALSE, FALSE, NULL, N_("next image") },
827 { "-b", "--back", gr_image_prev, FALSE, FALSE, NULL, N_("previous image") },
828 { NULL, "--first", gr_image_first, FALSE, FALSE, NULL, N_("first image") },
829 { NULL, "--last", gr_image_last, FALSE, FALSE, NULL, N_("last image") },
830 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, NULL, N_("toggle full screen") },
831 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, NULL, N_("start full screen") },
832 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, NULL, N_("stop full screen") },
833 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, NULL, N_("toggle slide show") },
834 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, NULL, N_("start slide show") },
835 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, NULL, N_("stop slide show") },
836 { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE, FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
837 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("<[H:][M:][N][.M]>"), N_("set slide show delay to Hrs Mins N.M seconds") },
838 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, NULL, N_("show tools") },
839 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, NULL, N_("hide tools") },
840 { "-q", "--quit", gr_quit, FALSE, FALSE, NULL, N_("quit") },
841 { NULL, "--config-load:", gr_config_load, TRUE, FALSE, N_("<FILE>"), N_("load configuration from FILE") },
842 { NULL, "--get-sidecars:", gr_get_sidecars, TRUE, FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
843 { NULL, "--get-destination:", gr_get_destination, TRUE, FALSE, N_("<FILE>"), N_("get destination path of FILE") },
844 { NULL, "file:", gr_file_load, TRUE, FALSE, N_("<FILE>"), N_("open FILE, bring Geeqie window to the top") },
845 { NULL, "File:", gr_file_load_no_raise, TRUE, FALSE, N_("<FILE>"), N_("open FILE, do not bring Geeqie window to the top") },
846 { NULL, "--tell", gr_file_tell, FALSE, FALSE, NULL, N_("print filename of current image") },
847 { NULL, "--pixel-info", gr_pixel_info, FALSE, FALSE, NULL, N_("print pixel info of mouse pointer on current image") },
848 { NULL, "view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
849 { NULL, "--list-clear", gr_list_clear, FALSE, FALSE, NULL, N_("clear command line collection list") },
850 { NULL, "--list-add:", gr_list_add, TRUE, FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
851 { NULL, "raise", gr_raise, FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
852 { "-ct:", "--cache-thumbs:", gr_cache_thumb, TRUE, FALSE, N_("clear|clean"), N_("clear or clean thumbnail cache") },
853 { "-cs:", "--cache-shared:", gr_cache_shared, TRUE, FALSE, N_("clear|clean"), N_("clear or clean shared thumbnail cache") },
854 { "-cm","--cache-metadata", gr_cache_metadata, FALSE, FALSE, NULL, N_(" clean the metadata cache") },
855 { "-cr:", "--cache-render:", gr_cache_render, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails") },
856 { "-crr:", "--cache-render-recurse:", gr_cache_render_recurse, TRUE, FALSE, N_("<folder> "), N_("render thumbnails recursively") },
857 { "-crs:", "--cache-render-shared:", gr_cache_render_standard, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails (see Help)") },
858 { "-crsr:", "--cache-render-shared-recurse:", gr_cache_render_standard_recurse, TRUE, FALSE, N_("<folder>"), N_(" render thumbnails recursively (see Help)") },
860 { NULL, "--lua:", gr_lua, TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
862 { NULL, NULL, NULL, FALSE, FALSE, NULL, NULL }
865 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
867 gboolean match = FALSE;
871 while (!match && remote_commands[i].func != NULL)
873 if (remote_commands[i].needs_extra)
875 if (remote_commands[i].opt_s &&
876 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
878 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
879 return &remote_commands[i];
881 else if (remote_commands[i].opt_l &&
882 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
884 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
885 return &remote_commands[i];
890 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
891 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
893 if (offset) *offset = text;
894 return &remote_commands[i];
904 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
906 RemoteCommandEntry *entry;
909 entry = remote_command_find(text, &offset);
910 if (entry && entry->func)
912 entry->func(offset, channel, data);
916 log_printf("unknown remote command:%s\n", text);
920 void remote_help(void)
926 print_term(FALSE, _("Remote command list:\n"));
929 while (remote_commands[i].func != NULL)
931 if (remote_commands[i].description)
933 s_opt_param = g_strconcat(remote_commands[i].opt_s, remote_commands[i].parameter, NULL);
934 l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
935 printf_term(FALSE, " %-11s%-1s %-30s%-s\n",
936 (remote_commands[i].opt_s) ? s_opt_param : "",
937 (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
938 (remote_commands[i].opt_l) ? l_opt_param : "",
939 _(remote_commands[i].description));
945 printf_term(FALSE, N_("\n All other command line parameters are used as plain files if they exists.\n"));
948 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
955 RemoteCommandEntry *entry;
957 entry = remote_command_find(argv[i], NULL);
960 list = g_list_append(list, argv[i]);
962 else if (errors && !isfile(argv[i]))
964 *errors = g_list_append(*errors, argv[i]);
973 * \param arg_exec Binary (argv0)
974 * \param remote_list Evaluated and recognized remote commands
975 * \param path The current path
976 * \param cmd_list List of all non collections in Path
977 * \param collection_list List of all collections in argv
979 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
980 GList *cmd_list, GList *collection_list)
982 RemoteConnection *rc;
983 gboolean started = FALSE;
986 buf = g_build_filename(get_rc_dir(), ".command", NULL);
987 rc = remote_client_open(buf);
992 gint retry_count = 12;
993 gboolean blank = FALSE;
995 printf_term(FALSE, _("Remote %s not running, starting..."), GQ_APPNAME);
997 command = g_string_new(arg_exec);
1003 RemoteCommandEntry *entry;
1008 entry = remote_command_find(text, NULL);
1011 if (entry->prefer_command_line)
1013 remote_list = g_list_remove(remote_list, text);
1014 g_string_append(command, " ");
1015 g_string_append(command, text);
1017 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
1024 if (blank || cmd_list || path) g_string_append(command, " --blank");
1025 if (get_debug_level()) g_string_append(command, " --debug");
1027 g_string_append(command, " &");
1028 runcmd(command->str);
1029 g_string_free(command, TRUE);
1031 while (!rc && retry_count > 0)
1033 usleep((retry_count > 10) ? 500000 : 1000000);
1034 rc = remote_client_open(buf);
1035 if (!rc) print_term(FALSE, ".");
1039 print_term(FALSE, "\n");
1048 const gchar *prefix;
1049 gboolean use_path = TRUE;
1050 gboolean sent = FALSE;
1056 RemoteCommandEntry *entry;
1061 entry = remote_command_find(text, NULL);
1064 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
1066 remote_client_send(rc, text);
1071 if (cmd_list && cmd_list->next)
1073 prefix = "--list-add:";
1074 remote_client_send(rc, "--list-clear");
1090 text = g_strconcat(prefix, fd->path, NULL);
1091 remote_client_send(rc, text);
1097 if (path && !cmd_list && use_path)
1101 text = g_strdup_printf("file:%s", path);
1102 remote_client_send(rc, text);
1108 work = collection_list;
1117 text = g_strdup_printf("file:%s", name);
1118 remote_client_send(rc, text);
1124 if (!started && !sent)
1126 remote_client_send(rc, "raise");
1131 print_term(TRUE, _("Remote not available\n"));
1137 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
1139 RemoteConnection *remote_connection = remote_server_open(path);
1140 RemoteData *remote_data = g_new(RemoteData, 1);
1142 remote_data->command_collection = command_collection;
1144 remote_server_subscribe(remote_connection, remote_cb, remote_data);
1145 return remote_connection;
1147 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */