4 * Copyright (C) 2008 - 2012 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
21 #include "layout_image.h"
23 #include "slideshow.h"
24 #include "ui_fileops.h"
27 #include <sys/socket.h>
33 #define SERVER_MAX_CLIENTS 8
35 #define REMOTE_SERVER_BACKLOG 4
39 #define UNIX_PATH_MAX 108
43 static RemoteConnection *remote_client_open(const gchar *path);
44 static gint remote_client_send(RemoteConnection *rc, const gchar *text);
47 typedef struct _RemoteClient RemoteClient;
48 struct _RemoteClient {
50 guint channel_id; /* event source id */
54 typedef struct _RemoteData RemoteData;
56 CollectionData *command_collection;
60 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
62 RemoteClient *client = data;
64 GIOStatus status = G_IO_STATUS_NORMAL;
68 if (condition & G_IO_IN)
74 while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
78 buffer[termpos] = '\0';
80 if (strlen(buffer) > 0)
82 if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
83 g_io_channel_write_chars(source, "\n", -1, NULL, NULL); /* empty line finishes the command */
84 g_io_channel_flush(source, NULL);
94 log_printf("error reading socket: %s\n", error->message);
99 if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
101 rc->clients = g_list_remove(rc->clients, client);
103 DEBUG_1("HUP detected, closing client.");
104 DEBUG_1("client count %d", g_list_length(rc->clients));
106 g_source_remove(client->channel_id);
114 static void remote_server_client_add(RemoteConnection *rc, gint fd)
116 RemoteClient *client;
119 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
121 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
126 client = g_new0(RemoteClient, 1);
130 channel = g_io_channel_unix_new(fd);
131 client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP,
132 remote_server_client_cb, client, NULL);
133 g_io_channel_unref(channel);
135 rc->clients = g_list_append(rc->clients, client);
136 DEBUG_1("client count %d", g_list_length(rc->clients));
139 static void remote_server_clients_close(RemoteConnection *rc)
143 RemoteClient *client = rc->clients->data;
145 rc->clients = g_list_remove(rc->clients, client);
147 g_source_remove(client->channel_id);
153 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
155 RemoteConnection *rc = data;
159 fd = accept(rc->fd, NULL, &alen);
162 log_printf("error accepting socket: %s\n", strerror(errno));
166 remote_server_client_add(rc, fd);
171 static gboolean remote_server_exists(const gchar *path)
173 RemoteConnection *rc;
175 /* verify server up */
176 rc = remote_client_open(path);
181 /* unable to connect, remove socket file to free up address */
186 static RemoteConnection *remote_server_open(const gchar *path)
188 RemoteConnection *rc;
189 struct sockaddr_un addr;
194 if (remote_server_exists(path))
196 log_printf("Address already in use: %s\n", path);
200 fd = socket(PF_UNIX, SOCK_STREAM, 0);
201 if (fd == -1) return NULL;
203 addr.sun_family = AF_UNIX;
204 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
205 strncpy(addr.sun_path, path, sun_path_len);
206 if (bind(fd, &addr, sizeof(addr)) == -1 ||
207 listen(fd, REMOTE_SERVER_BACKLOG) == -1)
209 log_printf("error subscribing to socket: %s\n", strerror(errno));
214 rc = g_new0(RemoteConnection, 1);
218 rc->path = g_strdup(path);
220 channel = g_io_channel_unix_new(rc->fd);
221 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
223 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
224 remote_server_read_cb, rc, NULL);
225 g_io_channel_unref(channel);
230 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
232 if (!rc || !rc->server) return;
234 rc->read_func = func;
235 rc->read_data = data;
239 static RemoteConnection *remote_client_open(const gchar *path)
241 RemoteConnection *rc;
243 struct sockaddr_un addr;
247 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
249 fd = socket(PF_UNIX, SOCK_STREAM, 0);
250 if (fd == -1) return NULL;
252 addr.sun_family = AF_UNIX;
253 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
254 strncpy(addr.sun_path, path, sun_path_len);
255 if (connect(fd, &addr, sizeof(addr)) == -1)
257 DEBUG_1("error connecting to socket: %s", strerror(errno));
262 rc = g_new0(RemoteConnection, 1);
265 rc->path = g_strdup(path);
270 static sig_atomic_t sigpipe_occured = FALSE;
272 static void sighandler_sigpipe(gint sig)
274 sigpipe_occured = TRUE;
277 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
279 struct sigaction new_action, old_action;
280 gboolean ret = FALSE;
281 GError *error = NULL;
284 if (!rc || rc->server) return FALSE;
285 if (!text) return TRUE;
287 sigpipe_occured = FALSE;
289 new_action.sa_handler = sighandler_sigpipe;
290 sigemptyset(&new_action.sa_mask);
291 new_action.sa_flags = 0;
293 /* setup our signal handler */
294 sigaction(SIGPIPE, &new_action, &old_action);
296 channel = g_io_channel_unix_new(rc->fd);
298 g_io_channel_write_chars(channel, text, -1, NULL, &error);
299 g_io_channel_write_chars(channel, "\n", -1, NULL, &error);
300 g_io_channel_flush(channel, &error);
304 log_printf("error reading socket: %s\n", error->message);
315 gchar *buffer = NULL;
317 while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
321 if (buffer[0] == '\n') /* empty line finishes the command */
327 buffer[termpos] = '\0';
328 printf("%s\n", buffer);
336 log_printf("error reading socket: %s\n", error->message);
343 /* restore the original signal handler */
344 sigaction(SIGPIPE, &old_action, NULL);
345 g_io_channel_unref(channel);
349 void remote_close(RemoteConnection *rc)
355 remote_server_clients_close(rc);
357 g_source_remove(rc->channel_id);
362 g_free(rc->read_data);
371 *-----------------------------------------------------------------------------
373 *-----------------------------------------------------------------------------
376 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data)
378 layout_image_next(NULL);
381 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data)
383 layout_image_prev(NULL);
386 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data)
388 layout_image_first(NULL);
391 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data)
393 layout_image_last(NULL);
396 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data)
398 layout_image_full_screen_toggle(NULL);
401 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data)
403 layout_image_full_screen_start(NULL);
406 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data)
408 layout_image_full_screen_stop(NULL);
411 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *channel, gpointer data)
414 FileData *dir_fd = file_data_new_dir(text);
415 list = filelist_recursive(dir_fd);
416 file_data_unref(dir_fd);
418 //printf("length: %d\n", g_list_length(list));
419 layout_image_slideshow_stop(NULL);
420 layout_image_slideshow_start_from_list(NULL, list);
423 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data)
425 layout_image_slideshow_toggle(NULL);
428 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data)
430 layout_image_slideshow_start(NULL);
433 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data)
435 layout_image_slideshow_stop(NULL);
438 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data)
442 n = g_ascii_strtod(text, NULL);
443 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
445 printf_term("Remote slideshow delay out of range (%.1f to %.1f)\n",
446 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
449 options->slideshow.delay = (gint)(n * 10.0 + 0.01);
452 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data)
457 if (layout_tools_float_get(NULL, &popped, &hidden) && hidden)
459 layout_tools_float_set(NULL, popped, FALSE);
463 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data)
468 if (layout_tools_float_get(NULL, &popped, &hidden) && !hidden)
470 layout_tools_float_set(NULL, popped, TRUE);
474 static gboolean gr_quit_idle_cb(gpointer data)
481 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data)
483 /* schedule exit when idle, if done from within a
484 * remote handler remote_close will crash
486 g_idle_add(gr_quit_idle_cb, NULL);
489 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
491 gchar *filename = expand_tilde(text);
493 if (isfile(filename))
495 if (file_extension_match(filename, GQ_COLLECTION_EXT))
497 collection_window_new(filename);
501 layout_set_path(NULL, filename);
504 else if (isdir(filename))
506 layout_set_path(NULL, filename);
510 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
516 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
518 gchar *filename = expand_tilde(text);
520 if (isfile(filename))
522 load_config_from_file(filename, FALSE);
526 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
532 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer data)
534 gchar *filename = expand_tilde(text);
535 FileData *fd = file_data_new_group(filename);
538 if (fd->parent) fd = fd->parent;
540 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
541 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
543 work = fd->sidecar_files;
549 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
550 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
555 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer data)
557 gchar *filename = expand_tilde(text);
558 FileData *fd = file_data_new_group(filename);
560 if (fd->change && fd->change->dest)
562 g_io_channel_write_chars(channel, fd->change->dest, -1, NULL, NULL);
563 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
568 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
570 gchar *filename = expand_tilde(text);
572 view_window_new(file_data_new_group(filename));
576 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
578 RemoteData *remote_data = data;
580 if (remote_data->command_collection)
582 collection_unref(remote_data->command_collection);
583 remote_data->command_collection = NULL;
587 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
589 RemoteData *remote_data = data;
592 if (!remote_data->command_collection)
596 cd = collection_new("");
601 cd->name = g_strdup(_("Command line"));
603 remote_data->command_collection = cd;
607 new = (!collection_get_first(remote_data->command_collection));
610 if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && new)
612 layout_image_set_collection(NULL, remote_data->command_collection,
613 collection_get_first(remote_data->command_collection));
617 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
619 LayoutWindow *lw = NULL;
621 if (layout_valid(&lw))
623 gtk_window_present(GTK_WINDOW(lw->window));
627 typedef struct _RemoteCommandEntry RemoteCommandEntry;
628 struct _RemoteCommandEntry {
631 void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
632 gboolean needs_extra;
633 gboolean prefer_command_line;
637 static RemoteCommandEntry remote_commands[] = {
638 /* short, long callback, extra, prefer,description */
639 { "-n", "--next", gr_image_next, FALSE, FALSE, N_("next image") },
640 { "-b", "--back", gr_image_prev, FALSE, FALSE, N_("previous image") },
641 { NULL, "--first", gr_image_first, FALSE, FALSE, N_("first image") },
642 { NULL, "--last", gr_image_last, FALSE, FALSE, N_("last image") },
643 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, N_("toggle full screen") },
644 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, N_("start full screen") },
645 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, N_("stop full screen") },
646 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, N_("toggle slide show") },
647 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, N_("start slide show") },
648 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, N_("stop slide show") },
649 { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE, FALSE, N_("start recursive slide show") },
650 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("set slide show delay in seconds") },
651 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, N_("show tools") },
652 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, N_("hide tools") },
653 { "-q", "--quit", gr_quit, FALSE, FALSE, N_("quit") },
654 { NULL, "--config-load:", gr_config_load, TRUE, FALSE, N_("load config file") },
655 { NULL, "--get-sidecars:", gr_get_sidecars, TRUE, FALSE, N_("get list of sidecars of the given file") },
656 { NULL, "--get-destination:", gr_get_destination, TRUE, FALSE, N_("get destination path for the given file") },
657 { NULL, "file:", gr_file_load, TRUE, FALSE, N_("open file") },
658 { NULL, "view:", gr_file_view, TRUE, FALSE, N_("open file in new window") },
659 { NULL, "--list-clear", gr_list_clear, FALSE, FALSE, NULL },
660 { NULL, "--list-add:", gr_list_add, TRUE, FALSE, NULL },
661 { NULL, "raise", gr_raise, FALSE, FALSE, NULL },
662 { NULL, NULL, NULL, FALSE, FALSE, NULL }
665 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
667 gboolean match = FALSE;
671 while (!match && remote_commands[i].func != NULL)
673 if (remote_commands[i].needs_extra)
675 if (remote_commands[i].opt_s &&
676 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
678 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
679 return &remote_commands[i];
681 else if (remote_commands[i].opt_l &&
682 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
684 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
685 return &remote_commands[i];
690 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
691 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
693 if (offset) *offset = text;
694 return &remote_commands[i];
704 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
706 RemoteCommandEntry *entry;
709 entry = remote_command_find(text, &offset);
710 if (entry && entry->func)
712 entry->func(offset, channel, data);
716 log_printf("unknown remote command:%s\n", text);
720 void remote_help(void)
724 print_term(_("Remote command list:\n"));
727 while (remote_commands[i].func != NULL)
729 if (remote_commands[i].description)
731 printf_term(" %-3s%s %-20s %s\n",
732 (remote_commands[i].opt_s) ? remote_commands[i].opt_s : "",
733 (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
734 (remote_commands[i].opt_l) ? remote_commands[i].opt_l : "",
735 _(remote_commands[i].description));
739 printf_term(N_("\n All other command line parameters are used as plain files if they exists.\n"));
742 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
749 RemoteCommandEntry *entry;
751 entry = remote_command_find(argv[i], NULL);
754 list = g_list_append(list, argv[i]);
756 else if (errors && !isfile(argv[i]))
758 *errors = g_list_append(*errors, argv[i]);
767 * \param arg_exec Binary (argv0)
768 * \param remote_list Evaluated and recognized remote commands
769 * \param path The current path
770 * \param cmd_list List of all non collections in Path
771 * \param collection_list List of all collections in argv
773 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
774 GList *cmd_list, GList *collection_list)
776 RemoteConnection *rc;
777 gboolean started = FALSE;
780 buf = g_build_filename(get_rc_dir(), ".command", NULL);
781 rc = remote_client_open(buf);
786 gint retry_count = 12;
787 gboolean blank = FALSE;
789 printf_term(_("Remote %s not running, starting..."), GQ_APPNAME);
791 command = g_string_new(arg_exec);
797 RemoteCommandEntry *entry;
802 entry = remote_command_find(text, NULL);
805 if (entry->prefer_command_line)
807 remote_list = g_list_remove(remote_list, text);
808 g_string_append(command, " ");
809 g_string_append(command, text);
811 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
818 if (blank || cmd_list || path) g_string_append(command, " --blank");
819 if (get_debug_level()) g_string_append(command, " --debug");
821 g_string_append(command, " &");
822 runcmd(command->str);
823 g_string_free(command, TRUE);
825 while (!rc && retry_count > 0)
827 usleep((retry_count > 10) ? 500000 : 1000000);
828 rc = remote_client_open(buf);
829 if (!rc) print_term(".");
843 gboolean use_path = TRUE;
844 gboolean sent = FALSE;
850 RemoteCommandEntry *entry;
855 entry = remote_command_find(text, NULL);
858 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
860 remote_client_send(rc, text);
865 if (cmd_list && cmd_list->next)
867 prefix = "--list-add:";
868 remote_client_send(rc, "--list-clear");
884 text = g_strconcat(prefix, fd->path, NULL);
885 remote_client_send(rc, text);
891 if (path && !cmd_list && use_path)
895 text = g_strdup_printf("file:%s", path);
896 remote_client_send(rc, text);
902 work = collection_list;
911 text = g_strdup_printf("file:%s", name);
912 remote_client_send(rc, text);
918 if (!started && !sent)
920 remote_client_send(rc, "raise");
925 print_term(_("Remote not available\n"));
931 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
933 RemoteConnection *remote_connection = remote_server_open(path);
934 RemoteData *remote_data = g_new(RemoteData, 1);
936 remote_data->command_collection = command_collection;
938 remote_server_subscribe(remote_connection, remote_cb, remote_data);
939 return remote_connection;
941 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */