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"
26 #include <sys/socket.h>
32 #define SERVER_MAX_CLIENTS 8
34 #define REMOTE_SERVER_BACKLOG 4
38 #define UNIX_PATH_MAX 108
42 static RemoteConnection *remote_client_open(const gchar *path);
43 static gint remote_client_send(RemoteConnection *rc, const gchar *text);
46 typedef struct _RemoteClient RemoteClient;
47 struct _RemoteClient {
53 typedef struct _RemoteData RemoteData;
55 CollectionData *command_collection;
59 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
61 RemoteClient *client = data;
66 if (condition & G_IO_IN)
74 while (g_io_channel_read_line(source, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
78 buffer[termpos] = '\0';
80 if (strlen(buffer) > 0)
82 queue = g_list_append(queue, buffer);
95 log_printf("error reading socket: %s\n", error->message);
102 gchar *command = work->data;
105 if (rc->read_func) rc->read_func(rc, command, rc->read_data);
112 if (condition & G_IO_HUP)
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 static gint 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);
230 rc->path = g_strdup(path);
232 rc->read_func = NULL;
233 rc->read_data = NULL;
237 channel = g_io_channel_unix_new(rc->fd);
238 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
239 remote_server_read_cb, rc, NULL);
240 g_io_channel_unref(channel);
245 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
247 if (!rc || !rc->server) return;
249 rc->read_func = func;
250 rc->read_data = data;
254 static RemoteConnection *remote_client_open(const gchar *path)
256 RemoteConnection *rc;
258 struct sockaddr_un addr;
262 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
264 fd = socket(PF_UNIX, SOCK_STREAM, 0);
265 if (fd == -1) return NULL;
267 addr.sun_family = AF_UNIX;
268 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
269 strncpy(addr.sun_path, path, sun_path_len);
270 if (connect(fd, &addr, sizeof(addr)) == -1)
272 DEBUG_1("error connecting to socket: %s", strerror(errno));
277 rc = g_new0(RemoteConnection, 1);
280 rc->path = g_strdup(path);
282 /* this might fix the freezes on freebsd, solaris, etc. - completely untested */
283 remote_client_send(rc, "\n");
288 static sig_atomic_t sigpipe_occured = FALSE;
290 static void sighandler_sigpipe(gint sig)
292 sigpipe_occured = TRUE;
295 static gint remote_client_send(RemoteConnection *rc, const gchar *text)
297 struct sigaction new_action, old_action;
300 if (!rc || rc->server) return FALSE;
301 if (!text) return TRUE;
303 sigpipe_occured = 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 if (write(rc->fd, text, strlen(text)) == -1 ||
313 write(rc->fd, "\n", 1) == -1)
317 log_printf("SIGPIPE writing to socket: %s\n", rc->path);
321 log_printf("error writing to socket: %s\n", strerror(errno));
330 /* restore the original signal handler */
331 sigaction(SIGPIPE, &old_action, NULL);
336 void remote_close(RemoteConnection *rc)
342 remote_server_clients_close(rc);
344 g_source_remove(rc->channel_id);
349 g_free(rc->read_data);
358 *-----------------------------------------------------------------------------
360 *-----------------------------------------------------------------------------
363 static void gr_image_next(const gchar *text, gpointer data)
365 layout_image_next(NULL);
368 static void gr_image_prev(const gchar *text, gpointer data)
370 layout_image_prev(NULL);
373 static void gr_image_first(const gchar *text, gpointer data)
375 layout_image_first(NULL);
378 static void gr_image_last(const gchar *text, gpointer data)
380 layout_image_last(NULL);
383 static void gr_fullscreen_toggle(const gchar *text, gpointer data)
385 layout_image_full_screen_toggle(NULL);
388 static void gr_fullscreen_start(const gchar *text, gpointer data)
390 layout_image_full_screen_start(NULL);
393 static void gr_fullscreen_stop(const gchar *text, gpointer data)
395 layout_image_full_screen_stop(NULL);
398 static void gr_slideshow_start_rec(const gchar *text, gpointer data)
401 FileData *dir_fd = file_data_new_simple(text);
402 list = filelist_recursive(dir_fd);
403 file_data_unref(dir_fd);
405 //printf("length: %d\n", g_list_length(list));
406 layout_image_slideshow_stop(NULL);
407 layout_image_slideshow_start_from_list(NULL, list);
410 static void gr_slideshow_toggle(const gchar *text, gpointer data)
412 layout_image_slideshow_toggle(NULL);
415 static void gr_slideshow_start(const gchar *text, gpointer data)
417 layout_image_slideshow_start(NULL);
420 static void gr_slideshow_stop(const gchar *text, gpointer data)
422 layout_image_slideshow_stop(NULL);
425 static void gr_slideshow_delay(const gchar *text, gpointer data)
429 n = strtod(text, NULL);
430 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
432 printf_term("Remote slideshow delay out of range (%.1f to %.1f)\n",
433 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
436 options->slideshow.delay = (gint)(n * 10.0 + 0.01);
439 static void gr_tools_show(const gchar *text, gpointer data)
444 if (layout_tools_float_get(NULL, &popped, &hidden) && hidden)
446 layout_tools_float_set(NULL, popped, FALSE);
450 static void gr_tools_hide(const gchar *text, gpointer data)
455 if (layout_tools_float_get(NULL, &popped, &hidden) && !hidden)
457 layout_tools_float_set(NULL, popped, TRUE);
461 static gint gr_quit_idle_cb(gpointer data)
468 static void gr_quit(const gchar *text, gpointer data)
470 /* schedule exit when idle, if done from within a
471 * remote handler remote_close will crash
473 g_idle_add(gr_quit_idle_cb, NULL);
476 static void gr_file_load(const gchar *text, gpointer data)
478 gchar *filename = expand_tilde(text);
480 if (isfile(filename))
482 if (file_extension_match(filename, GQ_COLLECTION_EXT))
484 collection_window_new(filename);
488 layout_set_path(NULL, filename);
491 else if (isdir(filename))
493 layout_set_path(NULL, filename);
497 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
503 static void gr_file_view(const gchar *text, gpointer data)
505 gchar *filename = expand_tilde(text);
507 view_window_new(file_data_new_simple(filename));
511 static void gr_list_clear(const gchar *text, gpointer data)
513 RemoteData *remote_data = data;
515 if (remote_data->command_collection)
517 collection_unref(remote_data->command_collection);
518 remote_data->command_collection = NULL;
522 static void gr_list_add(const gchar *text, gpointer data)
524 RemoteData *remote_data = data;
527 if (!remote_data->command_collection)
531 cd = collection_new("");
536 cd->name = g_strdup(_("Command line"));
538 remote_data->command_collection = cd;
542 new = (!collection_get_first(remote_data->command_collection));
545 if (collection_add(remote_data->command_collection, file_data_new_simple(text), FALSE) && new)
547 layout_image_set_collection(NULL, remote_data->command_collection,
548 collection_get_first(remote_data->command_collection));
552 static void gr_raise(const gchar *text, gpointer data)
554 LayoutWindow *lw = NULL;
556 if (layout_valid(&lw))
558 gtk_window_present(GTK_WINDOW(lw->window));
562 typedef struct _RemoteCommandEntry RemoteCommandEntry;
563 struct _RemoteCommandEntry {
566 void (*func)(const gchar *text, gpointer data);
568 gint prefer_command_line;
572 static RemoteCommandEntry remote_commands[] = {
573 /* short, long callback, extra, prefer,description */
574 { "-n", "--next", gr_image_next, FALSE, FALSE, N_("next image") },
575 { "-b", "--back", gr_image_prev, FALSE, FALSE, N_("previous image") },
576 { NULL, "--first", gr_image_first, FALSE, FALSE, N_("first image") },
577 { NULL, "--last", gr_image_last, FALSE, FALSE, N_("last image") },
578 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, N_("toggle full screen") },
579 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, N_("start full screen") },
580 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, N_("stop full screen") },
581 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, N_("toggle slide show") },
582 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, N_("start slide show") },
583 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, N_("stop slide show") },
584 { "-sr","--slideshow-recurse", gr_slideshow_start_rec, TRUE, FALSE, N_("start recursive slide show") },
585 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("set slide show delay in seconds") },
586 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, N_("show tools") },
587 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, N_("hide tools") },
588 { "-q", "--quit", gr_quit, FALSE, FALSE, N_("quit") },
589 { NULL, "file:", gr_file_load, TRUE, FALSE, N_("open file") },
590 { NULL, "view:", gr_file_view, TRUE, FALSE, N_("open file in new window") },
591 { NULL, "--list-clear", gr_list_clear, FALSE, FALSE, NULL },
592 { NULL, "--list-add:", gr_list_add, TRUE, FALSE, NULL },
593 { NULL, "raise", gr_raise, FALSE, FALSE, NULL },
594 { NULL, NULL, NULL, FALSE, FALSE, NULL }
597 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
603 while (!match && remote_commands[i].func != NULL)
605 if (remote_commands[i].needs_extra)
607 if (remote_commands[i].opt_s &&
608 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
610 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
611 return &remote_commands[i];
613 else if (remote_commands[i].opt_l &&
614 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
616 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
617 return &remote_commands[i];
622 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
623 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
625 if (offset) *offset = text;
626 return &remote_commands[i];
636 static void remote_cb(RemoteConnection *rc, const gchar *text, gpointer data)
638 RemoteCommandEntry *entry;
641 entry = remote_command_find(text, &offset);
642 if (entry && entry->func)
644 entry->func(offset, data);
648 log_printf("unknown remote command:%s\n", text);
652 void remote_help(void)
656 print_term(_("Remote command list:\n"));
659 while (remote_commands[i].func != NULL)
661 if (remote_commands[i].description)
663 printf_term(" %-3s%s %-20s %s\n",
664 (remote_commands[i].opt_s) ? remote_commands[i].opt_s : "",
665 (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
666 (remote_commands[i].opt_l) ? remote_commands[i].opt_l : "",
667 _(remote_commands[i].description));
673 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
680 RemoteCommandEntry *entry;
682 entry = remote_command_find(argv[i], NULL);
685 list = g_list_append(list, argv[i]);
689 *errors = g_list_append(*errors, argv[i]);
697 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
698 GList *cmd_list, GList *collection_list)
700 RemoteConnection *rc;
701 gint started = FALSE;
704 buf = g_build_filename(get_rc_dir(), ".command", NULL);
705 rc = remote_client_open(buf);
710 gint retry_count = 12;
713 printf_term(_("Remote %s not running, starting..."), GQ_APPNAME);
715 command = g_string_new(arg_exec);
721 RemoteCommandEntry *entry;
726 entry = remote_command_find(text, NULL);
729 if (entry->prefer_command_line)
731 remote_list = g_list_remove(remote_list, text);
732 g_string_append(command, " ");
733 g_string_append(command, text);
735 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
742 if (blank || cmd_list || path) g_string_append(command, " --blank");
743 if (get_debug_level()) g_string_append(command, " --debug");
745 g_string_append(command, " &");
746 runcmd(command->str);
747 g_string_free(command, TRUE);
749 while (!rc && retry_count > 0)
751 usleep((retry_count > 10) ? 500000 : 1000000);
752 rc = remote_client_open(buf);
753 if (!rc) print_term(".");
767 gint use_path = TRUE;
774 RemoteCommandEntry *entry;
779 entry = remote_command_find(text, NULL);
782 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
784 remote_client_send(rc, text);
789 if (cmd_list && cmd_list->next)
791 prefix = "--list-add:";
792 remote_client_send(rc, "--list-clear");
808 text = g_strconcat(prefix, fd->path, NULL);
809 remote_client_send(rc, text);
815 if (path && !cmd_list && use_path)
819 text = g_strdup_printf("file:%s", path);
820 remote_client_send(rc, text);
826 work = collection_list;
835 text = g_strdup_printf("file:%s", name);
836 remote_client_send(rc, text);
842 if (!started && !sent)
844 remote_client_send(rc, "raise");
849 print_term(_("Remote not available\n"));
855 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
857 RemoteConnection *remote_connection = remote_server_open(path);
858 RemoteData *remote_data = g_new(RemoteData, 1);
860 remote_data->command_collection = command_collection;
862 remote_server_subscribe(remote_connection, remote_cb, remote_data);
863 return remote_connection;
865 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */