2 * Copyright (C) 2006 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.
22 #include <gdk/gdkkeysyms.h> /* for keyboard values */
37 #include "collect-io.h"
39 #include "filefilter.h"
40 #include "history_list.h"
41 #include "image-overlay.h"
43 #include "layout_image.h"
44 #include "layout_util.h"
47 #include "secure_save.h"
49 #include "ui_fileops.h"
50 #include "ui_utildlg.h"
51 #include "cache_maint.h"
56 #include "histogram.h"
57 #include "pixbuf_util.h"
61 #include <clutter-gtk/clutter-gtk.h>
65 gboolean thumb_format_changed = FALSE;
66 static RemoteConnection *remote_connection = NULL;
69 *-----------------------------------------------------------------------------
71 *-----------------------------------------------------------------------------
74 void keyboard_scroll_calc(gint *x, gint *y, GdkEventKey *event)
76 static gint delta = 0;
77 static guint32 time_old = 0;
78 static guint keyval_old = 0;
80 if (event->state & GDK_CONTROL_MASK)
82 if (*x < 0) *x = G_MININT / 2;
83 if (*x > 0) *x = G_MAXINT / 2;
84 if (*y < 0) *y = G_MININT / 2;
85 if (*y > 0) *y = G_MAXINT / 2;
90 if (options->progressive_key_scrolling)
94 time_diff = event->time - time_old;
96 /* key pressed within 125ms ? (1/8 second) */
97 if (time_diff > 125 || event->keyval != keyval_old) delta = 0;
99 time_old = event->time;
100 keyval_old = event->keyval;
109 *x = *x * delta * options->keyboard_scroll_step;
110 *y = *y * delta * options->keyboard_scroll_step;
116 *-----------------------------------------------------------------------------
117 * command line parser (private) hehe, who needs popt anyway?
118 *-----------------------------------------------------------------------------
121 static void parse_command_line_add_file(const gchar *file_path, gchar **path, gchar **file,
122 GList **list, GList **collection_list)
126 path_parsed = g_strdup(file_path);
127 parse_out_relatives(path_parsed);
129 if (file_extension_match(path_parsed, GQ_COLLECTION_EXT))
131 *collection_list = g_list_append(*collection_list, path_parsed);
135 if (!*path) *path = remove_level_from_path(path_parsed);
136 if (!*file) *file = g_strdup(path_parsed);
137 *list = g_list_prepend(*list, file_data_new_group(path_parsed));
141 static void parse_command_line_add_dir(const gchar *dir, gchar **path, gchar **file,
145 /* This is broken because file filter is not initialized yet.
151 path_parsed = g_strdup(dir);
152 parse_out_relatives(path_parsed);
153 dir_fd = file_data_new_dir(path_parsed);
156 if (filelist_read(dir_fd, &files, NULL))
160 files = filelist_filter(files, FALSE);
161 files = filelist_sort_path(files);
166 FileData *fd = work->data;
167 if (!*path) *path = remove_level_from_path(fd->path);
168 if (!*file) *file = g_strdup(fd->path);
169 *list = g_list_prepend(*list, fd);
178 file_data_unref(dir_fd);
180 DEBUG_1("multiple directories specified, ignoring: %s", dir);
184 static void parse_command_line_process_dir(const gchar *dir, gchar **path, gchar **file,
185 GList **list, gchar **first_dir)
188 if (!*list && !*first_dir)
190 *first_dir = g_strdup(dir);
196 parse_command_line_add_dir(*first_dir, path, file, list);
200 parse_command_line_add_dir(dir, path, file, list);
204 static void parse_command_line_process_file(const gchar *file_path, gchar **path, gchar **file,
205 GList **list, GList **collection_list, gchar **first_dir)
210 parse_command_line_add_dir(*first_dir, path, file, list);
214 parse_command_line_add_file(file_path, path, file, list, collection_list);
217 static void parse_command_line(gint argc, gchar *argv[])
220 GList *remote_list = NULL;
221 GList *remote_errors = NULL;
222 gboolean remote_do = FALSE;
223 gchar *first_dir = NULL;
226 command_line = g_new0(CommandLine, 1);
228 command_line->argc = argc;
229 command_line->argv = argv;
230 command_line->regexp = NULL;
235 gchar *base_dir = get_current_dir();
239 gchar *cmd_line = path_to_utf8(argv[i]);
240 gchar *cmd_all = g_build_filename(base_dir, cmd_line, NULL);
242 if (cmd_line[0] == G_DIR_SEPARATOR && isdir(cmd_line))
244 parse_command_line_process_dir(cmd_line, &command_line->path, &command_line->file, &list, &first_dir);
246 else if (isdir(cmd_all))
248 parse_command_line_process_dir(cmd_all, &command_line->path, &command_line->file, &list, &first_dir);
250 else if (cmd_line[0] == G_DIR_SEPARATOR && isfile(cmd_line))
252 parse_command_line_process_file(cmd_line, &command_line->path, &command_line->file,
253 &list, &command_line->collection_list, &first_dir);
255 else if (isfile(cmd_all))
257 parse_command_line_process_file(cmd_all, &command_line->path, &command_line->file,
258 &list, &command_line->collection_list, &first_dir);
260 else if (is_collection(cmd_line))
264 path = collection_path(cmd_line);
265 parse_command_line_process_file(path, &command_line->path, &command_line->file,
266 &list, &command_line->collection_list, &first_dir);
269 else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
271 /* do nothing but do not produce warnings */
273 else if (strcmp(cmd_line, "+t") == 0 ||
274 strcmp(cmd_line, "--with-tools") == 0)
276 command_line->tools_show = TRUE;
278 remote_list = g_list_append(remote_list, "+t");
280 else if (strcmp(cmd_line, "-t") == 0 ||
281 strcmp(cmd_line, "--without-tools") == 0)
283 command_line->tools_hide = TRUE;
285 remote_list = g_list_append(remote_list, "-t");
287 else if (strcmp(cmd_line, "-f") == 0 ||
288 strcmp(cmd_line, "--fullscreen") == 0)
290 command_line->startup_full_screen = TRUE;
292 else if (strcmp(cmd_line, "-s") == 0 ||
293 strcmp(cmd_line, "--slideshow") == 0)
295 command_line->startup_in_slideshow = TRUE;
297 else if (strcmp(cmd_line, "-l") == 0 ||
298 strcmp(cmd_line, "--list") == 0)
300 command_line->startup_command_line_collection = TRUE;
302 else if (strncmp(cmd_line, "--geometry=", 11) == 0)
304 if (!command_line->geometry) command_line->geometry = g_strdup(cmd_line + 11);
306 else if (strcmp(cmd_line, "-r") == 0 ||
307 strcmp(cmd_line, "--remote") == 0)
312 remote_list = remote_build_list(remote_list, argc - i, &argv[i], &remote_errors);
315 else if ((strcmp(cmd_line, "+w") == 0) ||
316 strcmp(cmd_line, "--show-log-window") == 0)
318 command_line->log_window_show = TRUE;
320 else if (strncmp(cmd_line, "-o:", 3) == 0)
322 command_line->log_file = g_strdup(cmd_line + 3);
324 else if (strncmp(cmd_line, "--log-file:", 11) == 0)
326 command_line->log_file = g_strdup(cmd_line + 11);
328 else if (strncmp(cmd_line, "-g:", 3) == 0)
330 set_regexp(g_strdup(cmd_line+3));
332 else if (strncmp(cmd_line, "-grep:", 6) == 0)
334 set_regexp(g_strdup(cmd_line+3));
336 else if (strcmp(cmd_line, "-rh") == 0 ||
337 strcmp(cmd_line, "--remote-help") == 0)
342 else if (strcmp(cmd_line, "--blank") == 0)
344 command_line->startup_blank = TRUE;
346 else if (strcmp(cmd_line, "-v") == 0 ||
347 strcmp(cmd_line, "--version") == 0)
349 printf_term(FALSE, "%s %s\n", GQ_APPNAME, VERSION);
352 else if (strcmp(cmd_line, "--alternate") == 0)
354 /* enable faster experimental algorithm */
355 log_printf("Alternate similarity algorithm enabled\n");
356 image_sim_alternate_set(TRUE);
358 else if (strcmp(cmd_line, "-h") == 0 ||
359 strcmp(cmd_line, "--help") == 0)
361 printf_term(FALSE, "%s %s\n", GQ_APPNAME, VERSION);
362 printf_term(FALSE, _("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
363 print_term(FALSE, _("valid options are:\n"));
364 print_term(FALSE, _(" +t, --with-tools force show of tools\n"));
365 print_term(FALSE, _(" -t, --without-tools force hide of tools\n"));
366 print_term(FALSE, _(" -f, --fullscreen start in full screen mode\n"));
367 print_term(FALSE, _(" -s, --slideshow start in slideshow mode\n"));
368 print_term(FALSE, _(" -l, --list [files] [collections] open collection window for command line\n"));
369 print_term(FALSE, _(" --blank start with blank file list\n"));
370 print_term(FALSE, _(" --geometry=XxY+XOFF+YOFF set main window location\n"));
371 print_term(FALSE, _(" -r, --remote send following commands to open window\n"));
372 print_term(FALSE, _(" -rh,--remote-help print remote command list\n"));
374 print_term(FALSE, _(" --debug[=level] turn on debug output\n"));
375 print_term(FALSE, _(" -g:<regexp>, --grep:<regexp> filter debug output\n"));
377 print_term(FALSE, _(" +w, --show-log-window show log window\n"));
378 print_term(FALSE, _(" -o:<file>, --log-file:<file> save log data to file\n"));
379 print_term(FALSE, _(" -v, --version print version info\n"));
380 print_term(FALSE, _(" -h, --help show this message\n\n"));
383 /* these options are not officially supported!
384 * only for testing new features, no need to translate them */
385 print_term(FALSE, " --alternate use alternate similarity algorithm\n");
393 printf_term(TRUE, _("invalid or ignored: %s\nUse --help for options\n"), cmd_line);
401 parse_out_relatives(command_line->path);
402 parse_out_relatives(command_line->file);
405 list = g_list_reverse(list);
407 if (!command_line->path && first_dir)
409 command_line->path = first_dir;
412 parse_out_relatives(command_line->path);
416 /* If Geeqie is already running, prevent a second instance
417 * from being started. Open a new window instead.
419 app_lock = g_build_filename(get_rc_dir(), ".command", NULL);
420 if (remote_server_exists(app_lock) && !remote_do)
423 remote_list = g_list_append(remote_list, "--new-window");
431 GList *work = remote_errors;
433 printf_term(TRUE,_("Invalid or ignored remote options: "));
436 gchar *opt = work->data;
438 printf_term(TRUE, "%s%s", (work == remote_errors) ? "" : ", ", opt);
442 printf_term(TRUE, _("\nUse --remote-help for valid remote options.\n"));
445 remote_control(argv[0], remote_list, command_line->path, list, command_line->collection_list);
447 g_list_free(remote_list);
449 if (list && list->next)
451 command_line->cmd_list = list;
456 command_line->cmd_list = NULL;
459 if (command_line->startup_blank)
461 g_free(command_line->path);
462 command_line->path = NULL;
463 g_free(command_line->file);
464 command_line->file = NULL;
465 filelist_free(command_line->cmd_list);
466 command_line->cmd_list = NULL;
467 string_list_free(command_line->collection_list);
468 command_line->collection_list = NULL;
472 static void parse_command_line_for_debug_option(gint argc, gchar *argv[])
475 const gchar *debug_option = "--debug";
476 gint len = strlen(debug_option);
482 for (i = 1; i < argc; i++)
484 const gchar *cmd_line = argv[i];
485 if (strncmp(cmd_line, debug_option, len) == 0)
487 gint cmd_line_len = strlen(cmd_line);
489 /* we now increment the debug state for verbosity */
490 if (cmd_line_len == len)
492 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
494 gint n = atoi(cmd_line + len + 1);
502 DEBUG_1("debugging output enabled (level %d)", get_debug_level());
507 *-----------------------------------------------------------------------------
508 * startup, init, and exit
509 *-----------------------------------------------------------------------------
512 #define RC_HISTORY_NAME "history"
514 static void setup_env_path(void)
516 const gchar *old_path = g_getenv("PATH");
517 gchar *path = g_strconcat(GQ_BIN_DIR, ":", old_path, NULL);
518 g_setenv("PATH", path, TRUE);
522 static void keys_load(void)
526 path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
527 history_list_load(path);
531 static void keys_save(void)
535 path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
536 history_list_save(path);
540 static void mkdir_if_not_exists(const gchar *path)
542 if (isdir(path)) return;
544 log_printf(_("Creating %s dir:%s\n"), GQ_APPNAME, path);
546 if (!recursive_mkdir_if_not_exists(path, 0755))
548 log_printf(_("Could not create dir:%s\n"), path);
553 /* We add to duplicate and modify gtk_accel_map_print() and gtk_accel_map_save()
554 * to improve the reliability in special cases (especially when disk is full)
555 * These functions are now using secure saving stuff.
557 static void gq_accel_map_print(
559 const gchar *accel_path,
561 GdkModifierType accel_mods,
564 GString *gstring = g_string_new(changed ? NULL : "; ");
565 SecureSaveInfo *ssi = data;
568 g_string_append(gstring, "(gtk_accel_path \"");
570 tmp = g_strescape(accel_path, NULL);
571 g_string_append(gstring, tmp);
574 g_string_append(gstring, "\" \"");
576 name = gtk_accelerator_name(accel_key, accel_mods);
577 tmp = g_strescape(name, NULL);
579 g_string_append(gstring, tmp);
582 g_string_append(gstring, "\")\n");
584 secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
586 g_string_free(gstring, TRUE);
589 static gboolean gq_accel_map_save(const gchar *path)
595 pathl = path_from_utf8(path);
596 ssi = secure_open(pathl);
600 log_printf(_("error saving file: %s\n"), path);
604 gstring = g_string_new("; ");
606 g_string_append(gstring, g_get_prgname());
607 g_string_append(gstring, " GtkAccelMap rc-file -*- scheme -*-\n");
608 g_string_append(gstring, "; this file is an automated accelerator map dump\n");
609 g_string_append(gstring, ";\n");
611 secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
613 g_string_free(gstring, TRUE);
615 gtk_accel_map_foreach((gpointer) ssi, gq_accel_map_print);
617 if (secure_close(ssi))
619 log_printf(_("error saving file: %s\nerror: %s\n"), path,
620 secsave_strerror(secsave_errno));
627 static gchar *accep_map_filename(void)
629 return g_build_filename(get_rc_dir(), "accels", NULL);
632 static void accel_map_save(void)
636 path = accep_map_filename();
637 gq_accel_map_save(path);
641 static void accel_map_load(void)
646 path = accep_map_filename();
647 pathl = path_from_utf8(path);
648 gtk_accel_map_load(pathl);
653 static void gtkrc_load(void)
658 /* If a gtkrc file exists in the rc directory, add it to the
659 * list of files to be parsed at the end of gtk_init() */
660 path = g_build_filename(get_rc_dir(), "gtkrc", NULL);
661 pathl = path_from_utf8(path);
662 if (access(pathl, R_OK) == 0)
663 gtk_rc_add_default_file(pathl);
668 static void exit_program_final(void)
670 LayoutWindow *lw = NULL;
672 /* make sure that external editors are loaded, we would save incomplete configuration otherwise */
673 layout_editors_reload_finish();
675 remote_close(remote_connection);
677 collect_manager_flush();
679 save_options(options);
683 if (layout_valid(&lw))
688 secure_close(command_line->ssi);
693 static GenericDialog *exit_dialog = NULL;
695 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer data)
698 generic_dialog_close(gd);
701 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer data)
704 generic_dialog_close(gd);
705 exit_program_final();
708 static gint exit_confirm_dlg(void)
716 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
720 if (!collection_window_modified_exists()) return FALSE;
724 if (layout_valid(&lw))
729 msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
730 exit_dialog = generic_dialog_new(msg,
731 "exit", parent, FALSE,
732 exit_confirm_cancel_cb, NULL);
734 msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
735 generic_dialog_add_message(exit_dialog, GTK_STOCK_DIALOG_QUESTION,
736 msg, _("Collections have been modified. Quit anyway?"), TRUE);
738 generic_dialog_add_button(exit_dialog, GTK_STOCK_QUIT, NULL, exit_confirm_exit_cb, TRUE);
740 gtk_widget_show(exit_dialog->dialog);
745 static void exit_program_write_metadata_cb(gint success, const gchar *dest_path, gpointer data)
747 if (success) exit_program();
750 void exit_program(void)
752 layout_image_full_screen_stop(NULL);
754 if (metadata_write_queue_confirm(FALSE, exit_program_write_metadata_cb, NULL)) return;
756 if (exit_confirm_dlg()) return;
758 exit_program_final();
761 /* This code is supposed to handle situation when a file mmaped by image_loader
762 * or by exif loader is truncated by some other process.
763 * This is probably not completely correct according to posix, because
764 * mmap is not in the list of calls that can be used safely in signal handler,
765 * but anyway, the handler is called in situation when the application would
767 * Ideas for improvement are welcome ;)
769 /* FIXME: this probably needs some better ifdefs. Please report any compilation problems */
771 #if defined(SIGBUS) && defined(SA_SIGINFO)
772 static void sigbus_handler_cb(int signum, siginfo_t *info, void *context)
774 unsigned long pagesize = sysconf(_SC_PAGE_SIZE);
775 DEBUG_1("SIGBUS %p", info->si_addr);
776 mmap((void *)(((unsigned long)info->si_addr / pagesize) * pagesize), pagesize, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
780 static void setup_sigbus_handler(void)
782 #if defined(SIGBUS) && defined(SA_SIGINFO)
783 struct sigaction sigbus_action;
784 sigfillset(&sigbus_action.sa_mask);
785 sigbus_action.sa_sigaction = sigbus_handler_cb;
786 sigbus_action.sa_flags = SA_SIGINFO;
788 sigaction(SIGBUS, &sigbus_action, NULL);
792 gint main(gint argc, gchar *argv[])
794 CollectionData *first_collection = NULL;
796 CollectionData *cd = NULL;
800 #if !GLIB_CHECK_VERSION(2,32,0)
808 /* init execution time counter (debug only) */
811 /* setup locale, i18n */
812 setlocale(LC_ALL, "");
815 bindtextdomain(PACKAGE, GQ_LOCALEDIR);
816 bind_textdomain_codeset(PACKAGE, "UTF-8");
826 /* setup random seed for random slideshow */
829 setup_sigbus_handler();
831 /* register global notify functions */
832 file_data_register_notify_func(cache_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
833 file_data_register_notify_func(thumb_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
834 file_data_register_notify_func(histogram_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
835 file_data_register_notify_func(collect_manager_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
836 file_data_register_notify_func(metadata_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
841 parse_command_line_for_debug_option(argc, argv);
842 DEBUG_1("%s main: gtk_init", get_exec_time());
844 if (gtk_clutter_init(&argc, &argv) != CLUTTER_INIT_SUCCESS)
846 log_printf("Can't initialize clutter-gtk.\n");
850 gtk_init(&argc, &argv);
853 if (gtk_major_version < GTK_MAJOR_VERSION ||
854 (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
856 log_printf("!!! This is a friendly warning.\n");
857 log_printf("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
858 log_printf("!!! compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
859 log_printf("!!! running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
860 log_printf("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
863 DEBUG_1("%s main: pixbuf_inline_register_stock_icons", get_exec_time());
864 pixbuf_inline_register_stock_icons();
866 DEBUG_1("%s main: setting default options before commandline handling", get_exec_time());
867 options = init_options(NULL);
868 setup_default_options(options);
870 DEBUG_1("%s main: parse_command_line", get_exec_time());
871 parse_command_line(argc, argv);
873 DEBUG_1("%s main: mkdir_if_not_exists", get_exec_time());
874 /* these functions don't depend on config file */
875 mkdir_if_not_exists(get_rc_dir());
876 mkdir_if_not_exists(get_collections_dir());
877 mkdir_if_not_exists(get_thumbnails_cache_dir());
878 mkdir_if_not_exists(get_metadata_cache_dir());
885 /* restore session from the config file */
888 DEBUG_1("%s main: load_options", get_exec_time());
889 if (!load_options(options))
891 /* load_options calls these functions after it parses global options, we have to call it here if it fails */
892 filter_add_defaults();
896 /* handle missing config file and commandline additions*/
897 if (!layout_window_list)
899 /* broken or no config file */
900 layout_new_from_config(NULL, NULL, TRUE);
903 layout_editors_reload_start();
905 if (command_line->collection_list && !command_line->startup_command_line_collection)
909 work = command_line->collection_list;
918 cw = collection_window_new(path);
919 if (!first_collection && cw) first_collection = cw->cd;
923 if (command_line->log_file)
926 gchar *path = g_strdup(command_line->log_file);
928 pathl = path_from_utf8(path);
929 command_line->ssi = secure_open(pathl);
932 if (command_line->cmd_list ||
933 (command_line->startup_command_line_collection && command_line->collection_list))
937 if (command_line->startup_command_line_collection)
941 cw = collection_window_new("");
946 cd = collection_new(""); /* if we pass NULL, untitled counter is falsely increm. */
952 cd->name = g_strdup(_("Command line"));
954 collection_path_changed(cd);
956 work = command_line->cmd_list;
959 collection_add(cd, (FileData *)work->data, FALSE);
963 work = command_line->collection_list;
966 collection_load(cd, (gchar *)work->data, COLLECTION_LOAD_APPEND);
970 if (cd->list) layout_image_set_collection(NULL, cd, cd->list->data);
972 /* mem leak, we never unref this collection when !startup_command_line_collection
973 * (the image view of the main window does not hold a ref to the collection)
974 * this is sort of unavoidable, for if it did hold a ref, next/back
975 * may not work as expected when closing collection windows.
977 * collection_unref(cd);
981 else if (first_collection)
983 layout_image_set_collection(NULL, first_collection,
984 collection_get_first(first_collection));
987 buf = g_build_filename(get_rc_dir(), ".command", NULL);
988 remote_connection = remote_server_init(buf, cd);
991 DEBUG_1("%s main: gtk_main", get_exec_time());
998 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */