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"
49 #include "secure_save.h"
51 #include "ui_fileops.h"
52 #include "ui_utildlg.h"
53 #include "cache_maint.h"
58 #include "histogram.h"
59 #include "pixbuf_util.h"
63 #include <clutter-gtk/clutter-gtk.h>
67 /** @FIXME see below */
72 * @page diagrams Diagrams
73 * @section options_overview Options Overview
75 * #_ConfOptions #_LayoutOptions
82 * options.h : ConfOptions
84 * options.h : Options applicable to **all** Layout Windows
85 * options.h : These are in the <global> section of geeqierc.xml
86 * options.h : Available to all modules via the global variable **options**
87 * typedefs.h : LayoutOptions
89 * typedefs.h : Options applicable to **each** Layout Window
90 * typedefs.h : These are in the <layout> section of geeqierc.xml
91 * typedefs.h : There is one <layout> section for each Layout Window displayed
92 * typedefs.h : Available via **<layout_window>->options**
98 * @page diagrams Diagrams
99 * @section options_diagrams_main Options - New Window From Main
102 * #layout_new_from_default
103 * #load_config_from_file
105 * #setup_default_options
111 * : **init_options()**
113 * Set **options** = ConfOptions from hard-coded init values;
117 * : **setup_default_options()**
119 * set hard-coded ConfOptions:
127 * OSD template string
129 * shell path and options
131 * help search engine;
136 * --new-instance) then (yes)
138 * : **load_options()**
142 * : GQ_SYSTEM_WIDE_DIR
149 * /.geeqie/geeqierc.xml;
153 * : **load_config_from_file()**
155 * set **options** from file
156 * and all <layout window>->options in file;
161 * if (broken config. file
163 * or no layout section loaded
164 * (i.e. session not saved)) then (yes)
166 * : **layout_new_from_default()**;
167 * if (default.xml exists) then (yes)
169 * layout_window default options
170 * from default.xml file;
173 * layout_window default options;
179 * : Send --new-window request to remote
180 * No return to this point
181 * This instance terminates;
185 * : Enter gtk main loop;
192 * @page diagrams Diagrams
193 * @section options_diagrams_remote Options - New Window From Remote
194 * #layout_new_from_default
200 * : **layout_new_from_default()**;
201 * if (default.xml exists) then (yes)
203 * layout_window default options
204 * from default.xml file;
207 * layout_window default options;
210 * : set path from PWD;
215 * @page diagrams Diagrams
216 * @section options_diagrams_menu Options - New Window From Menu
217 * #layout_menu_new_window_cb
218 * #layout_menu_window_from_current_cb
219 * #layout_new_from_default
222 * group layout_util.c
228 * : **layout_new_from_default()**;
229 * if (default.xml exists) then (yes)
231 * layout_window default options
232 * from default.xml file;
235 * layout_window default options;
242 * **layout_menu_window_from_current_cb()**
243 * copy layout_window options
244 * from current window;
249 * **layout_menu_new_window_cb()**
250 * load layout_window options
251 * from saved xml file list;
259 * @ref options_overview Options Overview
263 gboolean thumb_format_changed = FALSE;
264 static RemoteConnection *remote_connection = NULL;
272 gchar *desktop_file_template;
275 *-----------------------------------------------------------------------------
277 *-----------------------------------------------------------------------------
280 void keyboard_scroll_calc(gint *x, gint *y, GdkEventKey *event)
282 static gint delta = 0;
283 static guint32 time_old = 0;
284 static guint keyval_old = 0;
286 if (event->state & GDK_CONTROL_MASK)
288 if (*x < 0) *x = G_MININT / 2;
289 if (*x > 0) *x = G_MAXINT / 2;
290 if (*y < 0) *y = G_MININT / 2;
291 if (*y > 0) *y = G_MAXINT / 2;
296 if (options->progressive_key_scrolling)
300 time_diff = event->time - time_old;
302 /* key pressed within 125ms ? (1/8 second) */
303 if (time_diff > 125 || event->keyval != keyval_old) delta = 0;
305 time_old = event->time;
306 keyval_old = event->keyval;
315 *x = *x * delta * options->keyboard_scroll_step;
316 *y = *y * delta * options->keyboard_scroll_step;
322 *-----------------------------------------------------------------------------
323 * command line parser (private) hehe, who needs popt anyway?
324 *-----------------------------------------------------------------------------
327 static void parse_command_line_add_file(const gchar *file_path, gchar **path, gchar **file,
328 GList **list, GList **collection_list)
332 path_parsed = g_strdup(file_path);
333 parse_out_relatives(path_parsed);
335 if (file_extension_match(path_parsed, GQ_COLLECTION_EXT))
337 *collection_list = g_list_append(*collection_list, path_parsed);
341 if (!*path) *path = remove_level_from_path(path_parsed);
342 if (!*file) *file = g_strdup(path_parsed);
343 *list = g_list_prepend(*list, path_parsed);
347 static void parse_command_line_add_dir(const gchar *dir, gchar **path, gchar **file,
351 /* This is broken because file filter is not initialized yet.
357 path_parsed = g_strdup(dir);
358 parse_out_relatives(path_parsed);
359 dir_fd = file_data_new_dir(path_parsed);
362 if (filelist_read(dir_fd, &files, NULL))
366 files = filelist_filter(files, FALSE);
367 files = filelist_sort_path(files);
372 FileData *fd = work->data;
373 if (!*path) *path = remove_level_from_path(fd->path);
374 if (!*file) *file = g_strdup(fd->path);
375 *list = g_list_prepend(*list, fd);
384 file_data_unref(dir_fd);
386 DEBUG_1("multiple directories specified, ignoring: %s", dir);
390 static void parse_command_line_process_dir(const gchar *dir, gchar **path, gchar **file,
391 GList **list, gchar **first_dir)
394 if (!*list && !*first_dir)
396 *first_dir = g_strdup(dir);
402 parse_command_line_add_dir(*first_dir, path, file, list);
406 parse_command_line_add_dir(dir, path, file, list);
410 static void parse_command_line_process_file(const gchar *file_path, gchar **path, gchar **file,
411 GList **list, GList **collection_list, gchar **first_dir)
416 parse_command_line_add_dir(*first_dir, path, file, list);
420 parse_command_line_add_file(file_path, path, file, list, collection_list);
423 static void parse_command_line(gint argc, gchar *argv[])
426 GList *remote_list = NULL;
427 GList *remote_errors = NULL;
428 gboolean remote_do = FALSE;
429 gchar *first_dir = NULL;
433 gchar *geometry = NULL;
434 GtkWidget *dialog_warning;
435 GString *command_line_errors = g_string_new(NULL);
437 command_line = g_new0(CommandLine, 1);
439 command_line->argc = argc;
440 command_line->argv = argv;
441 command_line->regexp = NULL;
446 gchar *base_dir = get_current_dir();
450 gchar *cmd_line = path_to_utf8(argv[i]);
451 gchar *cmd_all = g_build_filename(base_dir, cmd_line, NULL);
453 if (cmd_line[0] == G_DIR_SEPARATOR && isdir(cmd_line))
455 parse_command_line_process_dir(cmd_line, &command_line->path, &command_line->file, &list, &first_dir);
457 else if (isdir(cmd_all))
459 parse_command_line_process_dir(cmd_all, &command_line->path, &command_line->file, &list, &first_dir);
461 else if (cmd_line[0] == G_DIR_SEPARATOR && isfile(cmd_line))
463 parse_command_line_process_file(cmd_line, &command_line->path, &command_line->file,
464 &list, &command_line->collection_list, &first_dir);
466 else if (isfile(cmd_all))
468 parse_command_line_process_file(cmd_all, &command_line->path, &command_line->file,
469 &list, &command_line->collection_list, &first_dir);
471 else if (download_web_file(cmd_line, FALSE, NULL))
474 else if (is_collection(cmd_line))
478 path = collection_path(cmd_line);
479 parse_command_line_process_file(path, &command_line->path, &command_line->file,
480 &list, &command_line->collection_list, &first_dir);
483 else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
485 /* do nothing but do not produce warnings */
487 else if (strncmp(cmd_line, "--disable-clutter", 17) == 0 && (cmd_line[17] == '\0'))
489 /* do nothing but do not produce warnings */
491 else if (strcmp(cmd_line, "+t") == 0 ||
492 strcmp(cmd_line, "--with-tools") == 0)
494 command_line->tools_show = TRUE;
496 remote_list = g_list_append(remote_list, "+t");
498 else if (strcmp(cmd_line, "-t") == 0 ||
499 strcmp(cmd_line, "--without-tools") == 0)
501 command_line->tools_hide = TRUE;
503 remote_list = g_list_append(remote_list, "-t");
505 else if (strcmp(cmd_line, "-f") == 0 ||
506 strcmp(cmd_line, "--fullscreen") == 0)
508 command_line->startup_full_screen = TRUE;
510 else if (strcmp(cmd_line, "-s") == 0 ||
511 strcmp(cmd_line, "--slideshow") == 0)
513 command_line->startup_in_slideshow = TRUE;
515 else if (strcmp(cmd_line, "-l") == 0 ||
516 strcmp(cmd_line, "--list") == 0)
518 command_line->startup_command_line_collection = TRUE;
520 else if (strncmp(cmd_line, "--geometry=", 11) == 0)
522 if (!command_line->geometry) command_line->geometry = g_strdup(cmd_line + 11);
524 else if (strcmp(cmd_line, "-r") == 0 ||
525 strcmp(cmd_line, "--remote") == 0)
530 remote_list = remote_build_list(remote_list, argc - i, &argv[i], &remote_errors);
533 else if ((strcmp(cmd_line, "+w") == 0) ||
534 strcmp(cmd_line, "--show-log-window") == 0)
536 command_line->log_window_show = TRUE;
538 else if (strncmp(cmd_line, "-o:", 3) == 0)
540 command_line->log_file = g_strdup(cmd_line + 3);
542 else if (strncmp(cmd_line, "--log-file:", 11) == 0)
544 command_line->log_file = g_strdup(cmd_line + 11);
546 else if (strncmp(cmd_line, "-g:", 3) == 0)
548 set_regexp(g_strdup(cmd_line+3));
550 else if (strncmp(cmd_line, "-grep:", 6) == 0)
552 set_regexp(g_strdup(cmd_line+3));
554 else if (strncmp(cmd_line, "-n", 2) == 0)
556 command_line->new_instance = TRUE;
558 else if (strncmp(cmd_line, "--new-instance", 14) == 0)
560 command_line->new_instance = TRUE;
562 else if (strcmp(cmd_line, "-rh") == 0 ||
563 strcmp(cmd_line, "--remote-help") == 0)
568 else if (strcmp(cmd_line, "--blank") == 0)
570 command_line->startup_blank = TRUE;
572 else if (strcmp(cmd_line, "-v") == 0 ||
573 strcmp(cmd_line, "--version") == 0)
575 printf_term(FALSE, "%s %s GTK%d\n", GQ_APPNAME, VERSION, gtk_major_version);
578 else if (strcmp(cmd_line, "--alternate") == 0)
580 /* enable faster experimental algorithm */
581 log_printf("Alternate similarity algorithm enabled\n");
582 image_sim_alternate_set(TRUE);
584 else if (strcmp(cmd_line, "-h") == 0 ||
585 strcmp(cmd_line, "--help") == 0)
587 printf_term(FALSE, "%s %s\n", GQ_APPNAME, VERSION);
588 printf_term(FALSE, _("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
589 print_term(FALSE, _("valid options are:\n"));
590 print_term(FALSE, _(" +t, --with-tools force show of tools\n"));
591 print_term(FALSE, _(" -t, --without-tools force hide of tools\n"));
592 print_term(FALSE, _(" -f, --fullscreen start in full screen mode\n"));
593 print_term(FALSE, _(" -s, --slideshow start in slideshow mode\n"));
594 print_term(FALSE, _(" -l, --list [files] [collections] open collection window for command line\n"));
595 print_term(FALSE, _(" --blank start with blank file list\n"));
596 print_term(FALSE, _(" --geometry=XxY+XOFF+YOFF set main window location\n"));
597 print_term(FALSE, _(" -n, --new-instance open a new instance of Geeqie\n"));
598 print_term(FALSE, _(" -r, --remote send following commands to open window\n"));
599 print_term(FALSE, _(" -rh,--remote-help print remote command list\n"));
601 print_term(FALSE, _(" --debug[=level] turn on debug output\n"));
602 print_term(FALSE, _(" -g:<regexp>, --grep:<regexp> filter debug output\n"));
604 print_term(FALSE, _(" +w, --show-log-window show log window\n"));
605 print_term(FALSE, _(" -o:<file>, --log-file:<file> save log data to file\n"));
606 print_term(FALSE, _(" -v, --version print version info\n"));
607 print_term(FALSE, _(" -h, --help show this message\n"));
608 print_term(FALSE, _(" --disable-clutter disable use of Clutter library (i.e. GPU accel.)\n"));
609 print_term(FALSE, _(" --cache-maintenance <path> run cache maintenance in non-GUI mode\n\n"));
612 /* these options are not officially supported!
613 * only for testing new features, no need to translate them */
614 print_term(FALSE, " --alternate use alternate similarity algorithm\n");
622 command_line_errors = g_string_append(command_line_errors, cmd_line);
623 command_line_errors = g_string_append(command_line_errors, "\n");
631 if (command_line_errors->len > 0)
633 dialog_warning = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", "Invalid parameter(s):");
634 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_warning), "%s", command_line_errors->str);
635 gtk_window_set_title(GTK_WINDOW(dialog_warning), GQ_APPNAME);
636 gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
637 gtk_dialog_run(GTK_DIALOG(dialog_warning));
638 gtk_widget_destroy(dialog_warning);
639 g_string_free(command_line_errors, TRUE);
645 parse_out_relatives(command_line->path);
646 parse_out_relatives(command_line->file);
649 list = g_list_reverse(list);
651 if (!command_line->path && first_dir)
653 command_line->path = first_dir;
656 parse_out_relatives(command_line->path);
660 if (!command_line->new_instance)
662 /* If Geeqie is already running, prevent a second instance
663 * from being started. Open a new window instead.
665 app_lock = g_build_filename(get_rc_dir(), ".command", NULL);
666 if (remote_server_exists(app_lock) && !remote_do)
669 if (command_line->geometry)
671 geometry = g_strdup_printf("--geometry=%s", command_line->geometry);
672 remote_list = g_list_prepend(remote_list, geometry);
674 remote_list = g_list_prepend(remote_list, "--new-window");
683 GList *work = remote_errors;
687 gchar *opt = work->data;
689 command_line_errors = g_string_append(command_line_errors, opt);
690 command_line_errors = g_string_append(command_line_errors, "\n");
694 dialog_warning = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", "Invalid parameter(s):");
695 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_warning), "%s", command_line_errors->str);
696 gtk_window_set_title(GTK_WINDOW(dialog_warning), GQ_APPNAME);
697 gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
698 gtk_dialog_run(GTK_DIALOG(dialog_warning));
699 gtk_widget_destroy(dialog_warning);
700 g_string_free(command_line_errors, TRUE);
705 /* prepend the current dir the remote command was made from,
706 * for use by any remote command that needs it
708 current_dir = g_get_current_dir();
709 pwd = g_strconcat("--PWD:", current_dir, NULL);
710 remote_list = g_list_prepend(remote_list, pwd);
712 remote_control(argv[0], remote_list, command_line->path, list, command_line->collection_list);
713 /* There is no return to this point
719 g_list_free(remote_list);
721 if (list && list->next)
723 command_line->cmd_list = list;
727 string_list_free(list);
728 command_line->cmd_list = NULL;
731 if (command_line->startup_blank)
733 g_free(command_line->path);
734 command_line->path = NULL;
735 g_free(command_line->file);
736 command_line->file = NULL;
737 filelist_free(command_line->cmd_list);
738 command_line->cmd_list = NULL;
739 string_list_free(command_line->collection_list);
740 command_line->collection_list = NULL;
744 static void parse_command_line_for_debug_option(gint argc, gchar *argv[])
747 const gchar *debug_option = "--debug";
748 gint len = strlen(debug_option);
754 for (i = 1; i < argc; i++)
756 const gchar *cmd_line = argv[i];
757 if (strncmp(cmd_line, debug_option, len) == 0)
759 gint cmd_line_len = strlen(cmd_line);
761 /* we now increment the debug state for verbosity */
762 if (cmd_line_len == len)
764 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
766 gint n = atoi(cmd_line + len + 1);
774 DEBUG_1("debugging output enabled (level %d)", get_debug_level());
779 static gboolean parse_command_line_for_clutter_option(gint argc, gchar *argv[])
781 const gchar *clutter_option = "--disable-clutter";
782 gint len = strlen(clutter_option);
783 gboolean ret = FALSE;
789 for (i = 1; i < argc; i++)
791 const gchar *cmd_line = argv[i];
792 if (strncmp(cmd_line, clutter_option, len) == 0)
803 static gboolean parse_command_line_for_cache_maintenance_option(gint argc, gchar *argv[])
805 const gchar *cache_maintenance_option = "--cache-maintenance";
806 gint len = strlen(cache_maintenance_option);
807 gboolean ret = FALSE;
811 const gchar *cmd_line = argv[1];
812 if (strncmp(cmd_line, cache_maintenance_option, len) == 0)
821 static void process_command_line_for_cache_maintenance_option(gint argc, gchar *argv[])
824 gchar *folder_path = NULL;
827 gchar *buf_config_file;
832 folder_path = expand_tilde(argv[2]);
834 if (isdir(folder_path))
836 rc_path = g_build_filename(get_rc_dir(), RC_FILE_NAME, NULL);
840 if (g_file_get_contents(rc_path, &buf_config_file, &size, NULL))
844 diff_count = strncmp("</global>", &buf_config_file[i], 9);
851 /* Load only the <global> section */
852 load_config_from_buf(buf_config_file, i + 9, FALSE);
854 if (options->thumbnails.enable_caching)
856 cache_maintenance(folder_path);
860 print_term(TRUE, "Caching not enabled\n");
863 g_free(buf_config_file);
867 print_term(TRUE, g_strconcat(_("Cannot load "), rc_path, "\n", NULL));
873 print_term(TRUE, g_strconcat(_("Configuration file path "), rc_path, _(" is not a file\n"), NULL));
880 print_term(TRUE, g_strconcat(argv[2], _(" is not a folder\n"), NULL));
887 print_term(TRUE, _("No path parameter given\n"));
893 *-----------------------------------------------------------------------------
894 * startup, init, and exit
895 *-----------------------------------------------------------------------------
898 #define RC_HISTORY_NAME "history"
899 #define RC_MARKS_NAME "marks"
901 static void setup_env_path(void)
903 const gchar *old_path = g_getenv("PATH");
904 gchar *path = g_strconcat(gq_bin_dir, ":", old_path, NULL);
905 g_setenv("PATH", path, TRUE);
909 static void keys_load(void)
913 path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
914 history_list_load(path);
918 static void keys_save(void)
922 path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
923 history_list_save(path);
927 static void marks_load(void)
931 path = g_build_filename(get_rc_dir(), RC_MARKS_NAME, NULL);
932 marks_list_load(path);
936 static void marks_save(gboolean save)
940 path = g_build_filename(get_rc_dir(), RC_MARKS_NAME, NULL);
941 marks_list_save(path, save);
945 static void mkdir_if_not_exists(const gchar *path)
947 if (isdir(path)) return;
949 log_printf(_("Creating %s dir:%s\n"), GQ_APPNAME, path);
951 if (!recursive_mkdir_if_not_exists(path, 0755))
953 log_printf(_("Could not create dir:%s\n"), path);
958 /* We add to duplicate and modify gtk_accel_map_print() and gtk_accel_map_save()
959 * to improve the reliability in special cases (especially when disk is full)
960 * These functions are now using secure saving stuff.
962 static void gq_accel_map_print(
964 const gchar *accel_path,
966 GdkModifierType accel_mods,
969 GString *gstring = g_string_new(changed ? NULL : "; ");
970 SecureSaveInfo *ssi = data;
973 g_string_append(gstring, "(gtk_accel_path \"");
975 tmp = g_strescape(accel_path, NULL);
976 g_string_append(gstring, tmp);
979 g_string_append(gstring, "\" \"");
981 name = gtk_accelerator_name(accel_key, accel_mods);
982 tmp = g_strescape(name, NULL);
984 g_string_append(gstring, tmp);
987 g_string_append(gstring, "\")\n");
989 secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
991 g_string_free(gstring, TRUE);
994 static gboolean gq_accel_map_save(const gchar *path)
1000 pathl = path_from_utf8(path);
1001 ssi = secure_open(pathl);
1005 log_printf(_("error saving file: %s\n"), path);
1009 gstring = g_string_new("; ");
1010 if (g_get_prgname())
1011 g_string_append(gstring, g_get_prgname());
1012 g_string_append(gstring, " GtkAccelMap rc-file -*- scheme -*-\n");
1013 g_string_append(gstring, "; this file is an automated accelerator map dump\n");
1014 g_string_append(gstring, ";\n");
1016 secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
1018 g_string_free(gstring, TRUE);
1020 gtk_accel_map_foreach((gpointer) ssi, gq_accel_map_print);
1022 if (secure_close(ssi))
1024 log_printf(_("error saving file: %s\nerror: %s\n"), path,
1025 secsave_strerror(secsave_errno));
1032 static gchar *accep_map_filename(void)
1034 return g_build_filename(get_rc_dir(), "accels", NULL);
1037 static void accel_map_save(void)
1041 path = accep_map_filename();
1042 gq_accel_map_save(path);
1046 static void accel_map_load(void)
1051 path = accep_map_filename();
1052 pathl = path_from_utf8(path);
1053 gtk_accel_map_load(pathl);
1058 static void gtkrc_load(void)
1063 /* If a gtkrc file exists in the rc directory, add it to the
1064 * list of files to be parsed at the end of gtk_init() */
1065 path = g_build_filename(get_rc_dir(), "gtkrc", NULL);
1066 pathl = path_from_utf8(path);
1067 if (access(pathl, R_OK) == 0)
1068 gtk_rc_add_default_file(pathl);
1073 static void exit_program_final(void)
1075 LayoutWindow *lw = NULL;
1077 LayoutWindow *tmp_lw;
1079 /* make sure that external editors are loaded, we would save incomplete configuration otherwise */
1080 layout_editors_reload_finish();
1082 remote_close(remote_connection);
1084 collect_manager_flush();
1086 /* Save the named windows */
1087 if (layout_window_list && layout_window_list->next)
1089 list = layout_window_list;
1092 tmp_lw = list->data;
1093 if (!g_str_has_prefix(tmp_lw->options.id, "lw"))
1095 save_layout(list->data);
1101 save_options(options);
1105 if (layout_valid(&lw))
1110 secure_close(command_line->ssi);
1115 static GenericDialog *exit_dialog = NULL;
1117 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer data)
1120 generic_dialog_close(gd);
1123 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer data)
1126 generic_dialog_close(gd);
1127 exit_program_final();
1130 static gint exit_confirm_dlg(void)
1138 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
1142 if (!collection_window_modified_exists()) return FALSE;
1146 if (layout_valid(&lw))
1148 parent = lw->window;
1151 msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
1152 exit_dialog = generic_dialog_new(msg,
1153 "exit", parent, FALSE,
1154 exit_confirm_cancel_cb, NULL);
1156 msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
1157 generic_dialog_add_message(exit_dialog, GTK_STOCK_DIALOG_QUESTION,
1158 msg, _("Collections have been modified. Quit anyway?"), TRUE);
1160 generic_dialog_add_button(exit_dialog, GTK_STOCK_QUIT, NULL, exit_confirm_exit_cb, TRUE);
1162 gtk_widget_show(exit_dialog->dialog);
1167 static void exit_program_write_metadata_cb(gint success, const gchar *dest_path, gpointer data)
1169 if (success) exit_program();
1172 void exit_program(void)
1174 layout_image_full_screen_stop(NULL);
1176 if (metadata_write_queue_confirm(FALSE, exit_program_write_metadata_cb, NULL)) return;
1178 options->marks_save ? marks_save(TRUE) : marks_save(FALSE);
1180 if (exit_confirm_dlg()) return;
1182 exit_program_final();
1185 /* This code is supposed to handle situation when a file mmaped by image_loader
1186 * or by exif loader is truncated by some other process.
1187 * This is probably not completely correct according to posix, because
1188 * mmap is not in the list of calls that can be used safely in signal handler,
1189 * but anyway, the handler is called in situation when the application would
1191 * Ideas for improvement are welcome ;)
1193 /** @FIXME this probably needs some better ifdefs. Please report any compilation problems */
1195 #if defined(SIGBUS) && defined(SA_SIGINFO)
1196 static void sigbus_handler_cb(int signum, siginfo_t *info, void *context)
1198 unsigned long pagesize = sysconf(_SC_PAGE_SIZE);
1199 DEBUG_1("SIGBUS %p", info->si_addr);
1200 mmap((void *)(((unsigned long)info->si_addr / pagesize) * pagesize), pagesize, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1204 static void setup_sigbus_handler(void)
1206 #if defined(SIGBUS) && defined(SA_SIGINFO)
1207 struct sigaction sigbus_action;
1208 sigfillset(&sigbus_action.sa_mask);
1209 sigbus_action.sa_sigaction = sigbus_handler_cb;
1210 sigbus_action.sa_flags = SA_SIGINFO;
1212 sigaction(SIGBUS, &sigbus_action, NULL);
1217 * @brief Set up the application paths
1219 * This function is required for use of AppImages. AppImages are
1220 * relocatable, and therefore cannot use fixed paths to various components.
1221 * These paths were originally #defines created during compilation.
1222 * They are now variables, all defined relative to one level above the
1223 * directory that the executable is run from.
1225 static void create_application_paths()
1231 memset(buf, 0, sizeof(buf));
1232 if (readlink("/proc/self/exe", buf, sizeof(buf) - 1) < 0)
1234 /* There was an error. Perhaps the path does not exist
1235 * or the buffer is not big enough. */
1236 log_printf("Can't get path from /proc/self/exe");
1240 dirname = g_path_get_dirname(buf); // default is /usr/bin/
1241 gq_prefix = g_path_get_dirname(dirname);
1243 gq_localedir = g_build_filename(gq_prefix, "share", "locale", NULL);
1244 tmp = g_build_filename(gq_prefix, "share", "doc", NULL);
1245 gq_helpdir = g_strconcat(tmp, G_DIR_SEPARATOR_S, "geeqie-", VERSION, NULL);
1246 gq_htmldir = g_build_filename(gq_helpdir, "html", NULL);
1247 gq_app_dir = g_build_filename(gq_prefix, "share", "geeqie", NULL);
1248 gq_bin_dir = g_build_filename(gq_prefix, "lib", "geeqie", NULL);
1249 desktop_file_template = g_build_filename(gq_app_dir, "template.desktop", NULL);
1255 gint main(gint argc, gchar *argv[])
1257 CollectionData *first_collection = NULL;
1259 CollectionData *cd = NULL;
1260 gboolean disable_clutter = FALSE;
1261 gboolean single_dir = TRUE;
1265 #if !GLIB_CHECK_VERSION(2,32,0)
1266 g_thread_init(NULL);
1269 gdk_threads_enter();
1273 /* init execution time counter (debug only) */
1276 create_application_paths();
1278 /* setup locale, i18n */
1279 setlocale(LC_ALL, "");
1282 bindtextdomain(PACKAGE, gq_localedir);
1283 bind_textdomain_codeset(PACKAGE, "UTF-8");
1284 textdomain(PACKAGE);
1293 /* setup random seed for random slideshow */
1296 setup_sigbus_handler();
1298 /* register global notify functions */
1299 file_data_register_notify_func(cache_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
1300 file_data_register_notify_func(thumb_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
1301 file_data_register_notify_func(histogram_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
1302 file_data_register_notify_func(collect_manager_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
1303 file_data_register_notify_func(metadata_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
1308 parse_command_line_for_debug_option(argc, argv);
1309 DEBUG_1("%s main: gtk_init", get_exec_time());
1311 if (parse_command_line_for_clutter_option(argc, argv))
1313 disable_clutter = TRUE;
1314 gtk_init(&argc, &argv);
1318 if (gtk_clutter_init(&argc, &argv) != CLUTTER_INIT_SUCCESS)
1320 log_printf("Can't initialize clutter-gtk.\nStart Geeqie with the option \"geeqie --disable-clutter\"");
1321 runcmd("zenity --error --title=\"Geeqie\" --text \"Can't initialize clutter-gtk.\n\nStart Geeqie with the option:\n geeqie --disable-clutter\" --width=300");
1326 gtk_init(&argc, &argv);
1329 if (gtk_major_version < GTK_MAJOR_VERSION ||
1330 (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
1332 log_printf("!!! This is a friendly warning.\n");
1333 log_printf("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
1334 log_printf("!!! compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
1335 log_printf("!!! running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
1336 log_printf("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
1339 DEBUG_1("%s main: pixbuf_inline_register_stock_icons", get_exec_time());
1340 pixbuf_inline_register_stock_icons();
1342 DEBUG_1("%s main: setting default options before commandline handling", get_exec_time());
1343 options = init_options(NULL);
1344 setup_default_options(options);
1345 if (disable_clutter)
1347 options->disable_gpu = TRUE;
1350 DEBUG_1("%s main: mkdir_if_not_exists", get_exec_time());
1351 /* these functions don't depend on config file */
1352 mkdir_if_not_exists(get_rc_dir());
1353 mkdir_if_not_exists(get_collections_dir());
1354 mkdir_if_not_exists(get_thumbnails_cache_dir());
1355 mkdir_if_not_exists(get_metadata_cache_dir());
1356 mkdir_if_not_exists(get_window_layouts_dir());
1360 if (parse_command_line_for_cache_maintenance_option(argc, argv))
1362 process_command_line_for_cache_maintenance_option(argc, argv);
1366 DEBUG_1("%s main: parse_command_line", get_exec_time());
1367 parse_command_line(argc, argv);
1372 /* restore session from the config file */
1375 DEBUG_1("%s main: load_options", get_exec_time());
1376 if (!load_options(options))
1378 /* load_options calls these functions after it parses global options, we have to call it here if it fails */
1379 filter_add_defaults();
1384 /** @FIXME For the background of this see:
1385 * https://github.com/BestImageViewer/geeqie/issues/397
1386 * The feature CLUTTER_FEATURE_SWAP_EVENTS indictates if the
1387 * system is liable to exhibit this problem.
1388 * The user is provided with an override in Preferences/Behavior
1390 if (!options->override_disable_gpu && !options->disable_gpu)
1392 DEBUG_1("CLUTTER_FEATURE_SWAP_EVENTS %d",clutter_feature_available(CLUTTER_FEATURE_SWAP_EVENTS));
1393 if (clutter_feature_available(CLUTTER_FEATURE_SWAP_EVENTS) != 0)
1395 options->disable_gpu = TRUE;
1400 /* handle missing config file and commandline additions*/
1401 if (!layout_window_list)
1403 /* broken or no config file or no <layout> section */
1404 layout_new_from_default();
1407 layout_editors_reload_start();
1409 /* If no --list option, open a separate collection window for each
1410 * .gqv file on the command line
1412 if (command_line->collection_list && !command_line->startup_command_line_collection)
1416 work = command_line->collection_list;
1425 cw = collection_window_new(path);
1426 if (!first_collection && cw) first_collection = cw->cd;
1430 if (command_line->log_file)
1433 gchar *path = g_strdup(command_line->log_file);
1435 pathl = path_from_utf8(path);
1436 command_line->ssi = secure_open(pathl);
1439 /* If there is a files list on the command line and no --list option,
1440 * check if they are all in the same folder
1442 if (command_line->cmd_list && !(command_line->startup_command_line_collection))
1447 work = command_line->cmd_list;
1449 while (work && single_dir)
1453 dirname = g_path_get_dirname(work->data);
1456 path = g_strdup(dirname);
1460 if (g_strcmp0(path, dirname) != 0)
1471 /* Files from multiple folders, or --list option given
1472 * then open an unnamed collection and insert all files
1474 if ((command_line->cmd_list && !single_dir) || (command_line->startup_command_line_collection && command_line->cmd_list))
1479 cw = collection_window_new(NULL);
1482 collection_path_changed(cd);
1484 work = command_line->cmd_list;
1489 fd = file_data_new_simple(work->data);
1490 collection_add(cd, fd, FALSE);
1491 file_data_unref(fd);
1495 work = command_line->collection_list;
1498 collection_load(cd, (gchar *)work->data, COLLECTION_LOAD_APPEND);
1502 if (cd->list) layout_image_set_collection(NULL, cd, cd->list->data);
1504 /* mem leak, we never unref this collection when !startup_command_line_collection
1505 * (the image view of the main window does not hold a ref to the collection)
1506 * this is sort of unavoidable, for if it did hold a ref, next/back
1507 * may not work as expected when closing collection windows.
1509 * collection_unref(cd);
1513 else if (first_collection)
1515 layout_image_set_collection(NULL, first_collection,
1516 collection_get_first(first_collection));
1519 /* If the files on the command line are from one folder, select those files
1520 * unless it is a command line collection - then leave focus on collection window
1525 if (single_dir && command_line->cmd_list && !command_line->startup_command_line_collection)
1532 work = command_line->cmd_list;
1535 fd = file_data_new_simple((gchar *)work->data);
1536 selected = g_list_append(selected, fd);
1537 file_data_unref(fd);
1540 layout_select_list(lw, selected);
1543 buf = g_build_filename(get_rc_dir(), ".command", NULL);
1544 remote_connection = remote_server_init(buf, cd);
1550 DEBUG_1("%s main: gtk_main", get_exec_time());
1553 gdk_threads_leave();
1557 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */