4 * Copyright (C) 2008 - 2009 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 */
326 buffer[termpos] = '\0';
327 printf("%s\n", buffer);
335 log_printf("error reading socket: %s\n", error->message);
342 /* restore the original signal handler */
343 sigaction(SIGPIPE, &old_action, NULL);
344 g_io_channel_unref(channel);
348 void remote_close(RemoteConnection *rc)
354 remote_server_clients_close(rc);
356 g_source_remove(rc->channel_id);
361 g_free(rc->read_data);
370 *-----------------------------------------------------------------------------
372 *-----------------------------------------------------------------------------
375 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data)
377 layout_image_next(NULL);
380 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data)
382 layout_image_prev(NULL);
385 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data)
387 layout_image_first(NULL);
390 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data)
392 layout_image_last(NULL);
395 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data)
397 layout_image_full_screen_toggle(NULL);
400 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data)
402 layout_image_full_screen_start(NULL);
405 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data)
407 layout_image_full_screen_stop(NULL);
410 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *channel, gpointer data)
413 FileData *dir_fd = file_data_new_simple(text);
414 list = filelist_recursive(dir_fd);
415 file_data_unref(dir_fd);
417 //printf("length: %d\n", g_list_length(list));
418 layout_image_slideshow_stop(NULL);
419 layout_image_slideshow_start_from_list(NULL, list);
422 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data)
424 layout_image_slideshow_toggle(NULL);
427 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data)
429 layout_image_slideshow_start(NULL);
432 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data)
434 layout_image_slideshow_stop(NULL);
437 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data)
441 n = g_ascii_strtod(text, NULL);
442 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
444 printf_term("Remote slideshow delay out of range (%.1f to %.1f)\n",
445 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
448 options->slideshow.delay = (gint)(n * 10.0 + 0.01);
451 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data)
456 if (layout_tools_float_get(NULL, &popped, &hidden) && hidden)
458 layout_tools_float_set(NULL, popped, FALSE);
462 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data)
467 if (layout_tools_float_get(NULL, &popped, &hidden) && !hidden)
469 layout_tools_float_set(NULL, popped, TRUE);
473 static gboolean gr_quit_idle_cb(gpointer data)
480 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data)
482 /* schedule exit when idle, if done from within a
483 * remote handler remote_close will crash
485 g_idle_add(gr_quit_idle_cb, NULL);
488 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
490 gchar *filename = expand_tilde(text);
492 if (isfile(filename))
494 if (file_extension_match(filename, GQ_COLLECTION_EXT))
496 collection_window_new(filename);
500 layout_set_path(NULL, filename);
503 else if (isdir(filename))
505 layout_set_path(NULL, filename);
509 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
515 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
517 gchar *filename = expand_tilde(text);
519 if (isfile(filename))
521 load_config_from_file(filename, FALSE);
525 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
531 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
533 gchar *filename = expand_tilde(text);
535 view_window_new(file_data_new_simple(filename));
539 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
541 RemoteData *remote_data = data;
543 if (remote_data->command_collection)
545 collection_unref(remote_data->command_collection);
546 remote_data->command_collection = NULL;
550 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
552 RemoteData *remote_data = data;
555 if (!remote_data->command_collection)
559 cd = collection_new("");
564 cd->name = g_strdup(_("Command line"));
566 remote_data->command_collection = cd;
570 new = (!collection_get_first(remote_data->command_collection));
573 if (collection_add(remote_data->command_collection, file_data_new_simple(text), FALSE) && new)
575 layout_image_set_collection(NULL, remote_data->command_collection,
576 collection_get_first(remote_data->command_collection));
580 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
582 LayoutWindow *lw = NULL;
584 if (layout_valid(&lw))
586 gtk_window_present(GTK_WINDOW(lw->window));
590 typedef struct _RemoteCommandEntry RemoteCommandEntry;
591 struct _RemoteCommandEntry {
594 void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
595 gboolean needs_extra;
596 gboolean prefer_command_line;
600 static RemoteCommandEntry remote_commands[] = {
601 /* short, long callback, extra, prefer,description */
602 { "-n", "--next", gr_image_next, FALSE, FALSE, N_("next image") },
603 { "-b", "--back", gr_image_prev, FALSE, FALSE, N_("previous image") },
604 { NULL, "--first", gr_image_first, FALSE, FALSE, N_("first image") },
605 { NULL, "--last", gr_image_last, FALSE, FALSE, N_("last image") },
606 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, N_("toggle full screen") },
607 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, N_("start full screen") },
608 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, N_("stop full screen") },
609 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, N_("toggle slide show") },
610 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, N_("start slide show") },
611 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, N_("stop slide show") },
612 { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE, FALSE, N_("start recursive slide show") },
613 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("set slide show delay in seconds") },
614 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, N_("show tools") },
615 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, N_("hide tools") },
616 { "-q", "--quit", gr_quit, FALSE, FALSE, N_("quit") },
617 { NULL, "--config-load:", gr_config_load, TRUE, FALSE, N_("load config file") },
618 { NULL, "file:", gr_file_load, TRUE, FALSE, N_("open file") },
619 { NULL, "view:", gr_file_view, TRUE, FALSE, N_("open file in new window") },
620 { NULL, "--list-clear", gr_list_clear, FALSE, FALSE, NULL },
621 { NULL, "--list-add:", gr_list_add, TRUE, FALSE, NULL },
622 { NULL, "raise", gr_raise, FALSE, FALSE, NULL },
623 { NULL, NULL, NULL, FALSE, FALSE, NULL }
626 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
628 gboolean match = FALSE;
632 while (!match && remote_commands[i].func != NULL)
634 if (remote_commands[i].needs_extra)
636 if (remote_commands[i].opt_s &&
637 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
639 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
640 return &remote_commands[i];
642 else if (remote_commands[i].opt_l &&
643 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
645 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
646 return &remote_commands[i];
651 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
652 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
654 if (offset) *offset = text;
655 return &remote_commands[i];
665 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
667 RemoteCommandEntry *entry;
670 entry = remote_command_find(text, &offset);
671 if (entry && entry->func)
673 entry->func(offset, channel, data);
677 log_printf("unknown remote command:%s\n", text);
681 void remote_help(void)
685 print_term(_("Remote command list:\n"));
688 while (remote_commands[i].func != NULL)
690 if (remote_commands[i].description)
692 printf_term(" %-3s%s %-20s %s\n",
693 (remote_commands[i].opt_s) ? remote_commands[i].opt_s : "",
694 (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
695 (remote_commands[i].opt_l) ? remote_commands[i].opt_l : "",
696 _(remote_commands[i].description));
702 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
709 RemoteCommandEntry *entry;
711 entry = remote_command_find(argv[i], NULL);
714 list = g_list_append(list, argv[i]);
718 *errors = g_list_append(*errors, argv[i]);
726 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
727 GList *cmd_list, GList *collection_list)
729 RemoteConnection *rc;
730 gboolean started = FALSE;
733 buf = g_build_filename(get_rc_dir(), ".command", NULL);
734 rc = remote_client_open(buf);
739 gint retry_count = 12;
740 gboolean blank = FALSE;
742 printf_term(_("Remote %s not running, starting..."), GQ_APPNAME);
744 command = g_string_new(arg_exec);
750 RemoteCommandEntry *entry;
755 entry = remote_command_find(text, NULL);
758 if (entry->prefer_command_line)
760 remote_list = g_list_remove(remote_list, text);
761 g_string_append(command, " ");
762 g_string_append(command, text);
764 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
771 if (blank || cmd_list || path) g_string_append(command, " --blank");
772 if (get_debug_level()) g_string_append(command, " --debug");
774 g_string_append(command, " &");
775 runcmd(command->str);
776 g_string_free(command, TRUE);
778 while (!rc && retry_count > 0)
780 usleep((retry_count > 10) ? 500000 : 1000000);
781 rc = remote_client_open(buf);
782 if (!rc) print_term(".");
796 gboolean use_path = TRUE;
797 gboolean sent = FALSE;
803 RemoteCommandEntry *entry;
808 entry = remote_command_find(text, NULL);
811 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
813 remote_client_send(rc, text);
818 if (cmd_list && cmd_list->next)
820 prefix = "--list-add:";
821 remote_client_send(rc, "--list-clear");
837 text = g_strconcat(prefix, fd->path, NULL);
838 remote_client_send(rc, text);
844 if (path && !cmd_list && use_path)
848 text = g_strdup_printf("file:%s", path);
849 remote_client_send(rc, text);
855 work = collection_list;
864 text = g_strdup_printf("file:%s", name);
865 remote_client_send(rc, text);
871 if (!started && !sent)
873 remote_client_send(rc, "raise");
878 print_term(_("Remote not available\n"));
884 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
886 RemoteConnection *remote_connection = remote_server_open(path);
887 RemoteData *remote_data = g_new(RemoteData, 1);
889 remote_data->command_collection = command_collection;
891 remote_server_subscribe(remote_connection, remote_cb, remote_data);
892 return remote_connection;
894 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */