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 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
240 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
241 remote_server_read_cb, rc, NULL);
242 g_io_channel_unref(channel);
247 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
249 if (!rc || !rc->server) return;
251 rc->read_func = func;
252 rc->read_data = data;
256 static RemoteConnection *remote_client_open(const gchar *path)
258 RemoteConnection *rc;
260 struct sockaddr_un addr;
264 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
266 fd = socket(PF_UNIX, SOCK_STREAM, 0);
267 if (fd == -1) return NULL;
269 addr.sun_family = AF_UNIX;
270 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
271 strncpy(addr.sun_path, path, sun_path_len);
272 if (connect(fd, &addr, sizeof(addr)) == -1)
274 DEBUG_1("error connecting to socket: %s", strerror(errno));
279 rc = g_new0(RemoteConnection, 1);
282 rc->path = g_strdup(path);
284 /* this might fix the freezes on freebsd, solaris, etc. - completely untested */
285 remote_client_send(rc, "\n");
290 static sig_atomic_t sigpipe_occured = FALSE;
292 static void sighandler_sigpipe(gint sig)
294 sigpipe_occured = TRUE;
297 static gint remote_client_send(RemoteConnection *rc, const gchar *text)
299 struct sigaction new_action, old_action;
302 if (!rc || rc->server) return FALSE;
303 if (!text) return TRUE;
305 sigpipe_occured = FALSE;
307 new_action.sa_handler = sighandler_sigpipe;
308 sigemptyset(&new_action.sa_mask);
309 new_action.sa_flags = 0;
311 /* setup our signal handler */
312 sigaction(SIGPIPE, &new_action, &old_action);
314 if (write(rc->fd, text, strlen(text)) == -1 ||
315 write(rc->fd, "\n", 1) == -1)
319 log_printf("SIGPIPE writing to socket: %s\n", rc->path);
323 log_printf("error writing to socket: %s\n", strerror(errno));
332 /* restore the original signal handler */
333 sigaction(SIGPIPE, &old_action, NULL);
338 void remote_close(RemoteConnection *rc)
344 remote_server_clients_close(rc);
346 g_source_remove(rc->channel_id);
351 g_free(rc->read_data);
360 *-----------------------------------------------------------------------------
362 *-----------------------------------------------------------------------------
365 static void gr_image_next(const gchar *text, gpointer data)
367 layout_image_next(NULL);
370 static void gr_image_prev(const gchar *text, gpointer data)
372 layout_image_prev(NULL);
375 static void gr_image_first(const gchar *text, gpointer data)
377 layout_image_first(NULL);
380 static void gr_image_last(const gchar *text, gpointer data)
382 layout_image_last(NULL);
385 static void gr_fullscreen_toggle(const gchar *text, gpointer data)
387 layout_image_full_screen_toggle(NULL);
390 static void gr_fullscreen_start(const gchar *text, gpointer data)
392 layout_image_full_screen_start(NULL);
395 static void gr_fullscreen_stop(const gchar *text, gpointer data)
397 layout_image_full_screen_stop(NULL);
400 static void gr_slideshow_start_rec(const gchar *text, gpointer data)
403 FileData *dir_fd = file_data_new_simple(text);
404 list = filelist_recursive(dir_fd);
405 file_data_unref(dir_fd);
407 //printf("length: %d\n", g_list_length(list));
408 layout_image_slideshow_stop(NULL);
409 layout_image_slideshow_start_from_list(NULL, list);
412 static void gr_slideshow_toggle(const gchar *text, gpointer data)
414 layout_image_slideshow_toggle(NULL);
417 static void gr_slideshow_start(const gchar *text, gpointer data)
419 layout_image_slideshow_start(NULL);
422 static void gr_slideshow_stop(const gchar *text, gpointer data)
424 layout_image_slideshow_stop(NULL);
427 static void gr_slideshow_delay(const gchar *text, gpointer data)
431 n = strtod(text, NULL);
432 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
434 printf_term("Remote slideshow delay out of range (%.1f to %.1f)\n",
435 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
438 options->slideshow.delay = (gint)(n * 10.0 + 0.01);
441 static void gr_tools_show(const gchar *text, gpointer data)
446 if (layout_tools_float_get(NULL, &popped, &hidden) && hidden)
448 layout_tools_float_set(NULL, popped, FALSE);
452 static void gr_tools_hide(const gchar *text, gpointer data)
457 if (layout_tools_float_get(NULL, &popped, &hidden) && !hidden)
459 layout_tools_float_set(NULL, popped, TRUE);
463 static gint gr_quit_idle_cb(gpointer data)
470 static void gr_quit(const gchar *text, gpointer data)
472 /* schedule exit when idle, if done from within a
473 * remote handler remote_close will crash
475 g_idle_add(gr_quit_idle_cb, NULL);
478 static void gr_file_load(const gchar *text, gpointer data)
480 gchar *filename = expand_tilde(text);
482 if (isfile(filename))
484 if (file_extension_match(filename, GQ_COLLECTION_EXT))
486 collection_window_new(filename);
490 layout_set_path(NULL, filename);
493 else if (isdir(filename))
495 layout_set_path(NULL, filename);
499 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
505 static void gr_file_view(const gchar *text, gpointer data)
507 gchar *filename = expand_tilde(text);
509 view_window_new(file_data_new_simple(filename));
513 static void gr_list_clear(const gchar *text, gpointer data)
515 RemoteData *remote_data = data;
517 if (remote_data->command_collection)
519 collection_unref(remote_data->command_collection);
520 remote_data->command_collection = NULL;
524 static void gr_list_add(const gchar *text, gpointer data)
526 RemoteData *remote_data = data;
529 if (!remote_data->command_collection)
533 cd = collection_new("");
538 cd->name = g_strdup(_("Command line"));
540 remote_data->command_collection = cd;
544 new = (!collection_get_first(remote_data->command_collection));
547 if (collection_add(remote_data->command_collection, file_data_new_simple(text), FALSE) && new)
549 layout_image_set_collection(NULL, remote_data->command_collection,
550 collection_get_first(remote_data->command_collection));
554 static void gr_raise(const gchar *text, gpointer data)
556 LayoutWindow *lw = NULL;
558 if (layout_valid(&lw))
560 gtk_window_present(GTK_WINDOW(lw->window));
564 typedef struct _RemoteCommandEntry RemoteCommandEntry;
565 struct _RemoteCommandEntry {
568 void (*func)(const gchar *text, gpointer data);
570 gint prefer_command_line;
574 static RemoteCommandEntry remote_commands[] = {
575 /* short, long callback, extra, prefer,description */
576 { "-n", "--next", gr_image_next, FALSE, FALSE, N_("next image") },
577 { "-b", "--back", gr_image_prev, FALSE, FALSE, N_("previous image") },
578 { NULL, "--first", gr_image_first, FALSE, FALSE, N_("first image") },
579 { NULL, "--last", gr_image_last, FALSE, FALSE, N_("last image") },
580 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, N_("toggle full screen") },
581 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, N_("start full screen") },
582 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, N_("stop full screen") },
583 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, N_("toggle slide show") },
584 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, N_("start slide show") },
585 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, N_("stop slide show") },
586 { "-sr","--slideshow-recurse", gr_slideshow_start_rec, TRUE, FALSE, N_("start recursive slide show") },
587 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("set slide show delay in seconds") },
588 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, N_("show tools") },
589 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, N_("hide tools") },
590 { "-q", "--quit", gr_quit, FALSE, FALSE, N_("quit") },
591 { NULL, "file:", gr_file_load, TRUE, FALSE, N_("open file") },
592 { NULL, "view:", gr_file_view, TRUE, FALSE, N_("open file in new window") },
593 { NULL, "--list-clear", gr_list_clear, FALSE, FALSE, NULL },
594 { NULL, "--list-add:", gr_list_add, TRUE, FALSE, NULL },
595 { NULL, "raise", gr_raise, FALSE, FALSE, NULL },
596 { NULL, NULL, NULL, FALSE, FALSE, NULL }
599 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
605 while (!match && remote_commands[i].func != NULL)
607 if (remote_commands[i].needs_extra)
609 if (remote_commands[i].opt_s &&
610 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
612 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
613 return &remote_commands[i];
615 else if (remote_commands[i].opt_l &&
616 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
618 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
619 return &remote_commands[i];
624 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
625 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
627 if (offset) *offset = text;
628 return &remote_commands[i];
638 static void remote_cb(RemoteConnection *rc, const gchar *text, gpointer data)
640 RemoteCommandEntry *entry;
643 entry = remote_command_find(text, &offset);
644 if (entry && entry->func)
646 entry->func(offset, data);
650 log_printf("unknown remote command:%s\n", text);
654 void remote_help(void)
658 print_term(_("Remote command list:\n"));
661 while (remote_commands[i].func != NULL)
663 if (remote_commands[i].description)
665 printf_term(" %-3s%s %-20s %s\n",
666 (remote_commands[i].opt_s) ? remote_commands[i].opt_s : "",
667 (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
668 (remote_commands[i].opt_l) ? remote_commands[i].opt_l : "",
669 _(remote_commands[i].description));
675 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
682 RemoteCommandEntry *entry;
684 entry = remote_command_find(argv[i], NULL);
687 list = g_list_append(list, argv[i]);
691 *errors = g_list_append(*errors, argv[i]);
699 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
700 GList *cmd_list, GList *collection_list)
702 RemoteConnection *rc;
703 gint started = FALSE;
706 buf = g_build_filename(get_rc_dir(), ".command", NULL);
707 rc = remote_client_open(buf);
712 gint retry_count = 12;
715 printf_term(_("Remote %s not running, starting..."), GQ_APPNAME);
717 command = g_string_new(arg_exec);
723 RemoteCommandEntry *entry;
728 entry = remote_command_find(text, NULL);
731 if (entry->prefer_command_line)
733 remote_list = g_list_remove(remote_list, text);
734 g_string_append(command, " ");
735 g_string_append(command, text);
737 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
744 if (blank || cmd_list || path) g_string_append(command, " --blank");
745 if (get_debug_level()) g_string_append(command, " --debug");
747 g_string_append(command, " &");
748 runcmd(command->str);
749 g_string_free(command, TRUE);
751 while (!rc && retry_count > 0)
753 usleep((retry_count > 10) ? 500000 : 1000000);
754 rc = remote_client_open(buf);
755 if (!rc) print_term(".");
769 gint use_path = TRUE;
776 RemoteCommandEntry *entry;
781 entry = remote_command_find(text, NULL);
784 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
786 remote_client_send(rc, text);
791 if (cmd_list && cmd_list->next)
793 prefix = "--list-add:";
794 remote_client_send(rc, "--list-clear");
810 text = g_strconcat(prefix, fd->path, NULL);
811 remote_client_send(rc, text);
817 if (path && !cmd_list && use_path)
821 text = g_strdup_printf("file:%s", path);
822 remote_client_send(rc, text);
828 work = collection_list;
837 text = g_strdup_printf("file:%s", name);
838 remote_client_send(rc, text);
844 if (!started && !sent)
846 remote_client_send(rc, "raise");
851 print_term(_("Remote not available\n"));
857 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
859 RemoteConnection *remote_connection = remote_server_open(path);
860 RemoteData *remote_data = g_new(RemoteData, 1);
862 remote_data->command_collection = command_collection;
864 remote_server_subscribe(remote_connection, remote_cb, remote_data);
865 return remote_connection;
867 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */