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.
34 #include "collect-io.h"
36 #include "filefilter.h"
37 #include "history-list.h"
40 #include "layout-image.h"
41 #include "layout-util.h"
45 #include "secure-save.h"
46 #include "ui-fileops.h"
47 #include "ui-utildlg.h"
48 #include "cache-maint.h"
52 #include "histogram.h"
53 #include "pixbuf-util.h"
58 #include <clutter-gtk/clutter-gtk.h>
61 #ifdef HAVE_EXECINFO_H
65 gboolean thumb_format_changed = FALSE;
66 static RemoteConnection *remote_connection = nullptr;
74 gchar *gq_executable_path;
75 gchar *desktop_file_template;
76 gchar *instance_identifier;
78 #if defined(SA_SIGINFO)
79 void sig_handler_cb(int signo, siginfo_t *info, void *UNUSED(context))
82 const gchar *signal_name = nullptr;
87 #ifdef HAVE_EXECINFO_H
94 const gchar *sig_name;
96 struct signals signals_list[7];
98 signals_list[0].sig_no = SIGABRT;
99 signals_list[0].sig_name = "Abort";
100 signals_list[1].sig_no = SIGBUS;
101 signals_list[1].sig_name = "Bus error";
102 signals_list[2].sig_no = SIGFPE;
103 signals_list[2].sig_name = "Floating-point exception";
104 signals_list[3].sig_no = SIGILL;
105 signals_list[3].sig_name = "Illegal instruction";
106 signals_list[4].sig_no = SIGIOT;
107 signals_list[4].sig_name = "IOT trap";
108 signals_list[5].sig_no = SIGSEGV;
109 signals_list[5].sig_name = "Invalid memory reference";
110 signals_list[6].sig_no = -1;
111 signals_list[6].sig_name = "END";
130 signal_name = "Unknown signal";
131 while (signals_list[i].sig_no != -1)
133 if (signo == signals_list[i].sig_no)
135 signal_name = signals_list[i].sig_name;
141 len = write(STDERR_FILENO, "Geeqie fatal error\n", 19);
142 len = write(STDERR_FILENO, "Signal: ", 8);
143 len = write(STDERR_FILENO, signal_name, strlen(signal_name));
144 len = write(STDERR_FILENO, "\n", 1);
146 len = write(STDERR_FILENO, "Code: ", 6);
147 len = write(STDERR_FILENO, (info->si_code == SEGV_MAPERR) ? "Address not mapped" : "Invalid permissions", strlen((info->si_code == SEGV_MAPERR) ? "Address not mapped" : "Invalid permissions"));
148 len = write(STDERR_FILENO, "\n", 1);
150 len = write(STDERR_FILENO, "Address: ", 9);
152 if (info->si_addr == nullptr)
154 len = write(STDERR_FILENO, "0x0\n", 4);
158 /* Assume the address is 64-bit */
159 len = write(STDERR_FILENO, "0x", 2);
160 addr = reinterpret_cast<guint64>(info->si_addr);
162 for (i = 0; i < 16; i++)
164 char_index = addr & 0xf000000000000000;
165 char_index = char_index >> 60;
168 len = write(STDERR_FILENO, &hex_char[char_index], 1);
170 len = write(STDERR_FILENO, "\n", 1);
173 #ifdef HAVE_EXECINFO_H
174 bt_size = backtrace(bt, 1024);
175 backtrace_symbols_fd(bt, bt_size, STDERR_FILENO);
178 /* Avoid "not used" warning */
183 #else /* defined(SA_SIGINFO) */
184 void sig_handler_cb(int UNUSED(signo))
186 #ifdef HAVE_EXECINFO_H
191 write(STDERR_FILENO, "Geeqie fatal error\n", 19);
192 write(STDERR_FILENO, "Signal: Segmentation fault\n", 27);
194 #ifdef HAVE_EXECINFO_H
195 bt_size = backtrace(bt, 1024);
196 backtrace_symbols_fd(bt, bt_size, STDERR_FILENO);
201 #endif /* defined(SA_SIGINFO) */
204 *-----------------------------------------------------------------------------
206 *-----------------------------------------------------------------------------
209 void keyboard_scroll_calc(gint *x, gint *y, GdkEventKey *event)
211 static gint delta = 0;
212 static guint32 time_old = 0;
213 static guint keyval_old = 0;
215 if (event->state & GDK_CONTROL_MASK)
217 if (*x < 0) *x = G_MININT / 2;
218 if (*x > 0) *x = G_MAXINT / 2;
219 if (*y < 0) *y = G_MININT / 2;
220 if (*y > 0) *y = G_MAXINT / 2;
225 if (options->progressive_key_scrolling)
229 time_diff = event->time - time_old;
231 /* key pressed within 125ms ? (1/8 second) */
232 if (time_diff > 125 || event->keyval != keyval_old) delta = 0;
234 time_old = event->time;
235 keyval_old = event->keyval;
244 *x = *x * delta * options->keyboard_scroll_step;
245 *y = *y * delta * options->keyboard_scroll_step;
251 *-----------------------------------------------------------------------------
252 * command line parser (private) hehe, who needs popt anyway?
253 *-----------------------------------------------------------------------------
256 static void parse_command_line_add_file(const gchar *file_path, gchar **path, gchar **file,
257 GList **list, GList **collection_list)
261 path_parsed = g_strdup(file_path);
262 parse_out_relatives(path_parsed);
264 if (file_extension_match(path_parsed, GQ_COLLECTION_EXT))
266 *collection_list = g_list_append(*collection_list, path_parsed);
270 if (!*path) *path = remove_level_from_path(path_parsed);
271 if (!*file) *file = g_strdup(path_parsed);
272 *list = g_list_prepend(*list, path_parsed);
276 static void parse_command_line_add_dir(const gchar *dir, gchar **UNUSED(path), gchar **UNUSED(file),
277 GList **UNUSED(list))
280 /* This is broken because file filter is not initialized yet.
286 path_parsed = g_strdup(dir);
287 parse_out_relatives(path_parsed);
288 dir_fd = file_data_new_dir(path_parsed);
291 if (filelist_read(dir_fd, &files, NULL))
295 files = filelist_filter(files, FALSE);
296 files = filelist_sort_path(files);
301 FileData *fd = static_cast<FileData *>(work->data);
302 if (!*path) *path = remove_level_from_path(fd->path);
303 if (!*file) *file = g_strdup(fd->path);
304 *list = g_list_prepend(*list, fd);
313 file_data_unref(dir_fd);
315 DEBUG_1("multiple directories specified, ignoring: %s", dir);
319 static void parse_command_line_process_dir(const gchar *dir, gchar **path, gchar **file,
320 GList **list, gchar **first_dir)
323 if (!*list && !*first_dir)
325 *first_dir = g_strdup(dir);
331 parse_command_line_add_dir(*first_dir, path, file, list);
333 *first_dir = nullptr;
335 parse_command_line_add_dir(dir, path, file, list);
339 static void parse_command_line_process_file(const gchar *file_path, gchar **path, gchar **file,
340 GList **list, GList **collection_list, gchar **first_dir)
345 parse_command_line_add_dir(*first_dir, path, file, list);
347 *first_dir = nullptr;
349 parse_command_line_add_file(file_path, path, file, list, collection_list);
352 static void parse_command_line(gint argc, gchar *argv[])
354 GList *list = nullptr;
355 GList *remote_list = nullptr;
356 GList *remote_errors = nullptr;
357 gboolean remote_do = FALSE;
358 gchar *first_dir = nullptr;
362 gchar *geometry = nullptr;
363 GtkWidget *dialog_warning;
364 GString *command_line_errors = g_string_new(nullptr);
366 command_line = g_new0(CommandLine, 1);
368 command_line->argc = argc;
369 command_line->argv = argv;
370 command_line->regexp = nullptr;
375 gchar *base_dir = get_current_dir();
379 gchar *cmd_line = path_to_utf8(argv[i]);
380 gchar *cmd_all = g_build_filename(base_dir, cmd_line, NULL);
382 if (cmd_line[0] == G_DIR_SEPARATOR && isdir(cmd_line))
384 parse_command_line_process_dir(cmd_line, &command_line->path, &command_line->file, &list, &first_dir);
386 else if (isdir(cmd_all))
388 parse_command_line_process_dir(cmd_all, &command_line->path, &command_line->file, &list, &first_dir);
390 else if (cmd_line[0] == G_DIR_SEPARATOR && isfile(cmd_line))
392 parse_command_line_process_file(cmd_line, &command_line->path, &command_line->file,
393 &list, &command_line->collection_list, &first_dir);
395 else if (isfile(cmd_all))
397 parse_command_line_process_file(cmd_all, &command_line->path, &command_line->file,
398 &list, &command_line->collection_list, &first_dir);
400 else if (download_web_file(cmd_line, FALSE, nullptr))
403 else if (is_collection(cmd_line))
405 gchar *path = nullptr;
407 path = collection_path(cmd_line);
408 parse_command_line_process_file(path, &command_line->path, &command_line->file,
409 &list, &command_line->collection_list, &first_dir);
412 else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
414 /* do nothing but do not produce warnings */
416 else if (strncmp(cmd_line, "--disable-clutter", 17) == 0 && (cmd_line[17] == '\0'))
418 /* do nothing but do not produce warnings */
420 else if (strcmp(cmd_line, "+t") == 0 ||
421 strcmp(cmd_line, "--with-tools") == 0)
423 command_line->tools_show = TRUE;
425 remote_list = g_list_append(remote_list, g_strdup("+t"));
427 else if (strcmp(cmd_line, "-t") == 0 ||
428 strcmp(cmd_line, "--without-tools") == 0)
430 command_line->tools_hide = TRUE;
432 remote_list = g_list_append(remote_list, g_strdup("-t"));
434 else if (strcmp(cmd_line, "-f") == 0 ||
435 strcmp(cmd_line, "--fullscreen") == 0)
437 command_line->startup_full_screen = TRUE;
439 else if (strcmp(cmd_line, "-s") == 0 ||
440 strcmp(cmd_line, "--slideshow") == 0)
442 command_line->startup_in_slideshow = TRUE;
444 else if (strcmp(cmd_line, "-l") == 0 ||
445 strcmp(cmd_line, "--list") == 0)
447 command_line->startup_command_line_collection = TRUE;
449 else if (strncmp(cmd_line, "--geometry=", 11) == 0)
451 if (!command_line->geometry) command_line->geometry = g_strdup(cmd_line + 11);
453 else if (strcmp(cmd_line, "-r") == 0 ||
454 strcmp(cmd_line, "--remote") == 0)
459 remote_list = remote_build_list(remote_list, argc - i, &argv[i], &remote_errors);
462 else if ((strcmp(cmd_line, "+w") == 0) ||
463 strcmp(cmd_line, "--show-log-window") == 0)
465 command_line->log_window_show = TRUE;
467 else if (strncmp(cmd_line, "-o:", 3) == 0)
469 command_line->log_file = g_strdup(cmd_line + 3);
471 else if (strncmp(cmd_line, "--log-file:", 11) == 0)
473 command_line->log_file = g_strdup(cmd_line + 11);
475 else if (strncmp(cmd_line, "-g:", 3) == 0)
477 set_regexp(g_strdup(cmd_line+3));
479 else if (strncmp(cmd_line, "-grep:", 6) == 0)
481 set_regexp(g_strdup(cmd_line+3));
483 else if (strncmp(cmd_line, "-n", 2) == 0)
485 command_line->new_instance = TRUE;
487 else if (strncmp(cmd_line, "--new-instance", 14) == 0)
489 command_line->new_instance = TRUE;
491 else if (strcmp(cmd_line, "-rh") == 0 ||
492 strcmp(cmd_line, "--remote-help") == 0)
497 else if (strcmp(cmd_line, "--blank") == 0)
499 command_line->startup_blank = TRUE;
501 else if (strcmp(cmd_line, "-v") == 0 ||
502 strcmp(cmd_line, "--version") == 0)
504 printf_term(FALSE, "%s %s GTK%d\n", GQ_APPNAME, VERSION, gtk_major_version);
507 else if (strcmp(cmd_line, "--alternate") == 0)
509 /* enable faster experimental algorithm */
510 log_printf("Alternate similarity algorithm enabled\n");
511 image_sim_alternate_set(TRUE);
513 else if (strcmp(cmd_line, "-h") == 0 ||
514 strcmp(cmd_line, "--help") == 0)
516 printf_term(FALSE, "%s %s\n", GQ_APPNAME, VERSION);
517 printf_term(FALSE, _("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
518 print_term(FALSE, _("Valid options:\n"));
519 print_term(FALSE, _(" --blank start with blank file list\n"));
520 print_term(FALSE, _(" --cache-maintenance <path> run cache maintenance in non-GUI mode\n"));
521 print_term(FALSE, _(" --disable-clutter disable use of Clutter library (i.e. GPU accel.)\n"));
522 print_term(FALSE, _(" -f, --fullscreen start in full screen mode\n"));
523 print_term(FALSE, _(" --geometry=WxH+XOFF+YOFF set main window location\n"));
524 print_term(FALSE, _(" -h, --help show this message\n"));
525 print_term(FALSE, _(" -l, --list [files] [collections] open collection window for command line\n"));
526 print_term(FALSE, _(" -n, --new-instance open a new instance of Geeqie\n"));
527 print_term(FALSE, _(" -o:, --log-file:<file> save log data to file\n"));
528 print_term(FALSE, _(" -r, --remote send following commands to open window\n"));
529 print_term(FALSE, _(" -rh, --remote-help print remote command list\n"));
530 print_term(FALSE, _(" -s, --slideshow start in slideshow mode\n"));
531 print_term(FALSE, _(" +t, --with-tools force show of tools\n"));
532 print_term(FALSE, _(" -t, --without-tools force hide of tools\n"));
533 print_term(FALSE, _(" -v, --version print version info\n"));
534 print_term(FALSE, _(" +w, --show-log-window show log window\n"));
536 print_term(FALSE, _(" --debug[=level] turn on debug output\n"));
537 print_term(FALSE, _(" -g:, --grep:<regexp> filter debug output\n"));
541 /* these options are not officially supported!
542 * only for testing new features, no need to translate them */
543 print_term(FALSE, " --alternate use alternate similarity algorithm\n");
545 print_term(FALSE, "\n");
554 command_line_errors = g_string_append(command_line_errors, cmd_line);
555 command_line_errors = g_string_append(command_line_errors, "\n");
563 if (command_line_errors->len > 0)
565 dialog_warning = gtk_message_dialog_new(nullptr, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", "Invalid parameter(s):");
566 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_warning), "%s", command_line_errors->str);
567 gtk_window_set_title(GTK_WINDOW(dialog_warning), GQ_APPNAME);
568 gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
569 gtk_dialog_run(GTK_DIALOG(dialog_warning));
570 gtk_widget_destroy(dialog_warning);
571 g_string_free(command_line_errors, TRUE);
577 parse_out_relatives(command_line->path);
578 parse_out_relatives(command_line->file);
581 list = g_list_reverse(list);
583 if (!command_line->path && first_dir)
585 command_line->path = first_dir;
588 parse_out_relatives(command_line->path);
592 if (!command_line->new_instance)
594 /* If Geeqie is already running, prevent a second instance
595 * from being started. Open a new window instead.
597 app_lock = g_build_filename(get_rc_dir(), ".command", NULL);
598 if (remote_server_exists(app_lock) && !remote_do)
601 if (command_line->geometry)
603 geometry = g_strdup_printf("--geometry=%s", command_line->geometry);
604 remote_list = g_list_prepend(remote_list, geometry);
606 remote_list = g_list_prepend(remote_list, g_strdup("--new-window"));
615 GList *work = remote_errors;
619 auto opt = static_cast<gchar *>(work->data);
621 command_line_errors = g_string_append(command_line_errors, opt);
622 command_line_errors = g_string_append(command_line_errors, "\n");
626 dialog_warning = gtk_message_dialog_new(nullptr, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", "Invalid parameter(s):");
627 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_warning), "%s", command_line_errors->str);
628 gtk_window_set_title(GTK_WINDOW(dialog_warning), GQ_APPNAME);
629 gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
630 gtk_dialog_run(GTK_DIALOG(dialog_warning));
631 gtk_widget_destroy(dialog_warning);
632 g_string_free(command_line_errors, TRUE);
637 /* prepend the current dir the remote command was made from,
638 * for use by any remote command that needs it
640 current_dir = g_get_current_dir();
641 pwd = g_strconcat("--PWD:", current_dir, NULL);
642 remote_list = g_list_prepend(remote_list, pwd);
644 remote_control(argv[0], remote_list, command_line->path, list, command_line->collection_list);
645 /* There is no return to this point
651 g_list_free(remote_list);
653 if (list && list->next)
655 command_line->cmd_list = list;
659 g_list_free_full(list, g_free);
660 command_line->cmd_list = nullptr;
663 if (command_line->startup_blank)
665 g_free(command_line->path);
666 command_line->path = nullptr;
667 g_free(command_line->file);
668 command_line->file = nullptr;
669 filelist_free(command_line->cmd_list);
670 command_line->cmd_list = nullptr;
671 g_list_free_full(command_line->collection_list, g_free);
672 command_line->collection_list = nullptr;
676 static void parse_command_line_for_debug_option(gint argc, gchar *argv[])
679 const gchar *debug_option = "--debug";
680 gint len = strlen(debug_option);
686 for (i = 1; i < argc; i++)
688 const gchar *cmd_line = argv[i];
689 if (strncmp(cmd_line, debug_option, len) == 0)
691 gint cmd_line_len = strlen(cmd_line);
693 /* we now increment the debug state for verbosity */
694 if (cmd_line_len == len)
696 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
698 gint n = atoi(cmd_line + len + 1);
706 DEBUG_1("debugging output enabled (level %d)", get_debug_level());
711 static gboolean parse_command_line_for_clutter_option(gint argc, gchar *argv[])
713 const gchar *clutter_option = "--disable-clutter";
714 gint len = strlen(clutter_option);
715 gboolean ret = FALSE;
721 for (i = 1; i < argc; i++)
723 const gchar *cmd_line = argv[i];
724 if (strncmp(cmd_line, clutter_option, len) == 0)
735 static gboolean parse_command_line_for_cache_maintenance_option(gint argc, gchar *argv[])
737 const gchar *cache_maintenance_option = "--cache-maintenance";
738 gint len = strlen(cache_maintenance_option);
739 gboolean ret = FALSE;
743 const gchar *cmd_line = argv[1];
744 if (strncmp(cmd_line, cache_maintenance_option, len) == 0)
753 static void process_command_line_for_cache_maintenance_option(gint argc, gchar *argv[])
756 gchar *folder_path = nullptr;
759 gchar *buf_config_file;
764 folder_path = expand_tilde(argv[2]);
766 if (isdir(folder_path))
768 rc_path = g_build_filename(get_rc_dir(), RC_FILE_NAME, NULL);
772 if (g_file_get_contents(rc_path, &buf_config_file, &size, nullptr))
776 diff_count = strncmp("</global>", &buf_config_file[i], 9);
783 /* Load only the <global> section */
784 load_config_from_buf(buf_config_file, i + 9, FALSE);
786 if (options->thumbnails.enable_caching)
788 cache_maintenance(folder_path);
792 print_term(TRUE, "Caching not enabled\n");
795 g_free(buf_config_file);
799 print_term(TRUE, g_strconcat(_("Cannot load "), rc_path, "\n", NULL));
805 print_term(TRUE, g_strconcat(_("Configuration file path "), rc_path, _(" is not a file\n"), NULL));
812 print_term(TRUE, g_strconcat(argv[2], _(" is not a folder\n"), NULL));
819 print_term(TRUE, _("No path parameter given\n"));
825 *-----------------------------------------------------------------------------
826 * startup, init, and exit
827 *-----------------------------------------------------------------------------
830 #define RC_HISTORY_NAME "history"
831 #define RC_MARKS_NAME "marks"
833 static void setup_env_path()
835 const gchar *old_path = g_getenv("PATH");
836 gchar *path = g_strconcat(gq_bindir, ":", old_path, NULL);
837 g_setenv("PATH", path, TRUE);
841 static void keys_load()
845 path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
846 history_list_load(path);
850 static void keys_save()
854 path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
855 history_list_save(path);
859 static void marks_load()
863 path = g_build_filename(get_rc_dir(), RC_MARKS_NAME, NULL);
864 marks_list_load(path);
868 static void marks_save(gboolean save)
872 path = g_build_filename(get_rc_dir(), RC_MARKS_NAME, NULL);
873 marks_list_save(path, save);
877 static void mkdir_if_not_exists(const gchar *path)
879 if (isdir(path)) return;
881 log_printf(_("Creating %s dir:%s\n"), GQ_APPNAME, path);
883 if (!recursive_mkdir_if_not_exists(path, 0755))
885 log_printf(_("Could not create dir:%s\n"), path);
890 /* We add to duplicate and modify gtk_accel_map_print() and gtk_accel_map_save()
891 * to improve the reliability in special cases (especially when disk is full)
892 * These functions are now using secure saving stuff.
894 static void gq_accel_map_print(
896 const gchar *accel_path,
898 GdkModifierType accel_mods,
901 GString *gstring = g_string_new(changed ? nullptr : "; ");
902 auto ssi = static_cast<SecureSaveInfo *>(data);
905 g_string_append(gstring, "(gtk_accel_path \"");
907 tmp = g_strescape(accel_path, nullptr);
908 g_string_append(gstring, tmp);
911 g_string_append(gstring, "\" \"");
913 name = gtk_accelerator_name(accel_key, accel_mods);
914 tmp = g_strescape(name, nullptr);
916 g_string_append(gstring, tmp);
919 g_string_append(gstring, "\")\n");
921 secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
923 g_string_free(gstring, TRUE);
926 static gboolean gq_accel_map_save(const gchar *path)
932 pathl = path_from_utf8(path);
933 ssi = secure_open(pathl);
937 log_printf(_("error saving file: %s\n"), path);
941 gstring = g_string_new("; ");
943 g_string_append(gstring, g_get_prgname());
944 g_string_append(gstring, " GtkAccelMap rc-file -*- scheme -*-\n");
945 g_string_append(gstring, "; this file is an automated accelerator map dump\n");
946 g_string_append(gstring, ";\n");
948 secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
950 g_string_free(gstring, TRUE);
952 gtk_accel_map_foreach(ssi, gq_accel_map_print);
954 if (secure_close(ssi))
956 log_printf(_("error saving file: %s\nerror: %s\n"), path,
957 secsave_strerror(secsave_errno));
964 static gchar *accep_map_filename()
966 return g_build_filename(get_rc_dir(), "accels", NULL);
969 static void accel_map_save()
973 path = accep_map_filename();
974 gq_accel_map_save(path);
978 static void accel_map_load()
983 path = accep_map_filename();
984 pathl = path_from_utf8(path);
985 gtk_accel_map_load(pathl);
990 static void gtkrc_load()
995 /* If a gtkrc file exists in the rc directory, add it to the
996 * list of files to be parsed at the end of gtk_init() */
997 path = g_build_filename(get_rc_dir(), "gtkrc", NULL);
998 pathl = path_from_utf8(path);
999 if (access(pathl, R_OK) == 0)
1000 gtk_rc_add_default_file(pathl);
1005 static void exit_program_final()
1007 LayoutWindow *lw = nullptr;
1009 LayoutWindow *tmp_lw;
1011 GFile *archive_file;
1013 /* make sure that external editors are loaded, we would save incomplete configuration otherwise */
1014 layout_editors_reload_finish();
1016 remote_close(remote_connection);
1018 collect_manager_flush();
1020 /* Save the named windows */
1021 if (layout_window_list && layout_window_list->next)
1023 list = layout_window_list;
1026 tmp_lw = static_cast<LayoutWindow *>(list->data);
1027 if (!g_str_has_prefix(tmp_lw->options.id, "lw"))
1029 save_layout(static_cast<LayoutWindow *>(list->data));
1035 save_options(options);
1039 if (layout_valid(&lw))
1044 /* Delete any files/folders in /tmp that have been created by the open archive function */
1045 archive_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, NULL);
1046 if (isdir(archive_dir))
1048 archive_file = g_file_new_for_path(archive_dir);
1049 rmdir_recursive(archive_file, nullptr, nullptr);
1050 g_free(archive_dir);
1051 g_object_unref(archive_file);
1054 /* If there are still sub-dirs created by another instance, this will fail
1055 * but that does not matter */
1056 archive_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, NULL);
1057 if (isdir(archive_dir))
1059 archive_file = g_file_new_for_path(archive_dir);
1060 g_file_delete(archive_file, nullptr, nullptr);
1061 g_free(archive_dir);
1062 g_object_unref(archive_file);
1065 secure_close(command_line->ssi);
1070 static GenericDialog *exit_dialog = nullptr;
1072 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer UNUSED(data))
1074 exit_dialog = nullptr;
1075 generic_dialog_close(gd);
1078 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer UNUSED(data))
1080 exit_dialog = nullptr;
1081 generic_dialog_close(gd);
1082 exit_program_final();
1085 static gint exit_confirm_dlg()
1093 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
1097 if (!collection_window_modified_exists()) return FALSE;
1101 if (layout_valid(&lw))
1103 parent = lw->window;
1106 msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
1107 exit_dialog = generic_dialog_new(msg,
1108 "exit", parent, FALSE,
1109 exit_confirm_cancel_cb, nullptr);
1111 msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
1112 generic_dialog_add_message(exit_dialog, GTK_STOCK_DIALOG_QUESTION,
1113 msg, _("Collections have been modified. Quit anyway?"), TRUE);
1115 generic_dialog_add_button(exit_dialog, GTK_STOCK_QUIT, nullptr, exit_confirm_exit_cb, TRUE);
1117 gtk_widget_show(exit_dialog->dialog);
1122 static void exit_program_write_metadata_cb(gint success, const gchar *UNUSED(dest_path), gpointer UNUSED(data))
1124 if (success) exit_program();
1129 layout_image_full_screen_stop(nullptr);
1131 if (metadata_write_queue_confirm(FALSE, exit_program_write_metadata_cb, nullptr)) return;
1133 options->marks_save ? marks_save(TRUE) : marks_save(FALSE);
1135 if (exit_confirm_dlg()) return;
1137 exit_program_final();
1140 /* This code attempts to handle situation when a file mmaped by image_loader
1141 * or by exif loader is truncated by some other process.
1142 * This code is incorrect according to POSIX, because:
1144 * mmap is not async-signal-safe and thus may not be called from a signal handler
1146 * mmap must be called with a valid file descriptor. POSIX requires that
1147 * a fildes argument of -1 must cause mmap to return EBADF.
1149 * See https://github.com/BestImageViewer/geeqie/issues/1052 for discussion of
1150 * an alternative approach.
1152 /** @FIXME this probably needs some better ifdefs. Please report any compilation problems */
1153 /** @FIXME This section needs revising */
1155 #pragma GCC diagnostic push
1156 #pragma GCC diagnostic ignored "-Wunused-function"
1157 #if defined(SIGBUS) && defined(SA_SIGINFO)
1158 static void sigbus_handler_cb_unused(int UNUSED(signum), siginfo_t *info, void *UNUSED(context))
1161 * @FIXME Design and implement a POSIX-acceptable approach,
1162 * after first documenting the sitations where SIGBUS occurs.
1163 * See https://github.com/BestImageViewer/geeqie/issues/1052 for discussion
1166 DEBUG_1("SIGBUS %p NOT HANDLED", info->si_addr);
1171 static void setup_sigbus_handler_unused(void)
1173 #if defined(SIGBUS) && defined(SA_SIGINFO)
1174 struct sigaction sigbus_action;
1175 sigfillset(&sigbus_action.sa_mask);
1176 sigbus_action.sa_sigaction = sigbus_handler_cb_unused;
1177 sigbus_action.sa_flags = SA_SIGINFO;
1179 sigaction(SIGBUS, &sigbus_action, NULL);
1182 #pragma GCC diagnostic pop
1184 #ifndef HAVE_DEVELOPER
1185 static void setup_sig_handler()
1187 struct sigaction sigsegv_action;
1188 sigfillset(&sigsegv_action.sa_mask);
1189 sigsegv_action.sa_sigaction = sig_handler_cb;
1190 sigsegv_action.sa_flags = SA_SIGINFO;
1192 sigaction(SIGABRT, &sigsegv_action, nullptr);
1193 sigaction(SIGBUS, &sigsegv_action, nullptr);
1194 sigaction(SIGFPE, &sigsegv_action, nullptr);
1195 sigaction(SIGILL, &sigsegv_action, nullptr);
1196 sigaction(SIGIOT, &sigsegv_action, nullptr);
1197 sigaction(SIGSEGV, &sigsegv_action, nullptr);
1201 static void set_theme_bg_color()
1204 GdkColor theme_color;
1205 GtkStyleContext *style_context;
1209 if (!options->image.use_custom_border_color)
1211 work = layout_window_list;
1212 lw = static_cast<LayoutWindow *>(work->data);
1214 style_context = gtk_widget_get_style_context(lw->window);
1215 gtk_style_context_get_background_color(style_context, GTK_STATE_FLAG_NORMAL, &bg_color);
1217 theme_color.red = bg_color.red * 65535;
1218 theme_color.green = bg_color.green * 65535;
1219 theme_color.blue = bg_color.blue * 65535;
1223 lw = static_cast<LayoutWindow *>(work->data);
1224 image_background_set_color(lw->image, &theme_color);
1229 view_window_colors_update();
1232 static gboolean theme_change_cb(GObject *UNUSED(gobject), GParamSpec *UNUSED(pspec), gpointer UNUSED(data))
1234 set_theme_bg_color();
1240 * @brief Set up the application paths
1242 * This function is required for use of AppImages. AppImages are
1243 * relocatable, and therefore cannot use fixed paths to various components.
1244 * These paths were originally #defines created during compilation.
1245 * They are now variables, all defined relative to one level above the
1246 * directory that the executable is run from.
1248 static void create_application_paths()
1254 length = wai_getExecutablePath(nullptr, 0, nullptr);
1255 path = static_cast<gchar *>(malloc(length + 1));
1256 wai_getExecutablePath(path, length, nullptr);
1257 path[length] = '\0';
1259 gq_executable_path = g_strdup(path);
1260 dirname = g_path_get_dirname(gq_executable_path);
1261 gq_prefix = g_path_get_dirname(dirname);
1263 gq_localedir = g_build_filename(gq_prefix, GQ_LOCALEDIR, NULL);
1264 gq_helpdir = g_build_filename(gq_prefix, GQ_HELPDIR, NULL);
1265 gq_htmldir = g_build_filename(gq_prefix, GQ_HTMLDIR, NULL);
1266 gq_appdir = g_build_filename(gq_prefix, GQ_APPDIR, NULL);
1267 gq_bindir = g_build_filename(gq_prefix, GQ_BINDIR, NULL);
1268 desktop_file_template = g_build_filename(gq_appdir, "template.desktop", NULL);
1274 gint main(gint argc, gchar *argv[])
1276 CollectionData *first_collection = nullptr;
1278 CollectionData *cd = nullptr;
1279 gboolean disable_clutter = FALSE;
1280 gboolean single_dir = TRUE;
1282 GtkSettings *default_settings;
1284 gdk_set_allowed_backends("x11,*");
1287 gdk_threads_enter();
1289 /* seg. fault handler */
1290 #ifdef HAVE_DEVELOPER
1291 backward::SignalHandling sh{};
1293 setup_sig_handler();
1296 /* init execution time counter (debug only) */
1299 create_application_paths();
1301 /* setup locale, i18n */
1302 setlocale(LC_ALL, "");
1305 bindtextdomain(PACKAGE, gq_localedir);
1306 bind_textdomain_codeset(PACKAGE, "UTF-8");
1307 textdomain(PACKAGE);
1316 /* setup random seed for random slideshow */
1317 srand(time(nullptr));
1320 /* See later comment; this handler leads to UB. */
1321 setup_sigbus_handler();
1324 /* register global notify functions */
1325 file_data_register_notify_func(cache_notify_cb, nullptr, NOTIFY_PRIORITY_HIGH);
1326 file_data_register_notify_func(thumb_notify_cb, nullptr, NOTIFY_PRIORITY_HIGH);
1327 file_data_register_notify_func(histogram_notify_cb, nullptr, NOTIFY_PRIORITY_HIGH);
1328 file_data_register_notify_func(collect_manager_notify_cb, nullptr, NOTIFY_PRIORITY_LOW);
1329 file_data_register_notify_func(metadata_notify_cb, nullptr, NOTIFY_PRIORITY_LOW);
1334 parse_command_line_for_debug_option(argc, argv);
1335 DEBUG_1("%s main: gtk_init", get_exec_time());
1337 if (parse_command_line_for_clutter_option(argc, argv))
1339 disable_clutter = TRUE;
1340 gtk_init(&argc, &argv);
1344 if (gtk_clutter_init(&argc, &argv) != CLUTTER_INIT_SUCCESS)
1346 log_printf("Can't initialize clutter-gtk.\nStart Geeqie with the option \"geeqie --disable-clutter\"");
1347 runcmd("zenity --error --title=\"Geeqie\" --text \"Can't initialize clutter-gtk.\n\nStart Geeqie with the option:\n geeqie --disable-clutter\" --width=300");
1352 gtk_init(&argc, &argv);
1355 if (gtk_major_version < GTK_MAJOR_VERSION ||
1356 (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
1358 log_printf("!!! This is a friendly warning.\n");
1359 log_printf("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
1360 log_printf("!!! compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
1361 log_printf("!!! running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
1362 log_printf("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
1365 DEBUG_1("%s main: pixbuf_inline_register_stock_icons", get_exec_time());
1366 gtk_icon_theme_add_resource_path(gtk_icon_theme_get_default(), GQ_RESOURCE_PATH_ICONS);
1367 pixbuf_inline_register_stock_icons();
1369 DEBUG_1("%s main: setting default options before commandline handling", get_exec_time());
1370 options = init_options(nullptr);
1371 setup_default_options(options);
1372 if (disable_clutter)
1374 options->disable_gpu = TRUE;
1377 /* Generate a unique identifier used by the open archive function */
1378 instance_identifier = g_strdup_printf("%x", g_random_int());
1380 DEBUG_1("%s main: mkdir_if_not_exists", get_exec_time());
1381 /* these functions don't depend on config file */
1382 mkdir_if_not_exists(get_rc_dir());
1383 mkdir_if_not_exists(get_collections_dir());
1384 mkdir_if_not_exists(get_thumbnails_cache_dir());
1385 mkdir_if_not_exists(get_metadata_cache_dir());
1386 mkdir_if_not_exists(get_window_layouts_dir());
1390 if (parse_command_line_for_cache_maintenance_option(argc, argv))
1392 process_command_line_for_cache_maintenance_option(argc, argv);
1396 DEBUG_1("%s main: parse_command_line", get_exec_time());
1397 parse_command_line(argc, argv);
1402 /* restore session from the config file */
1405 DEBUG_1("%s main: load_options", get_exec_time());
1406 if (!load_options(options))
1408 /* load_options calls these functions after it parses global options, we have to call it here if it fails */
1409 filter_add_defaults();
1414 /** @FIXME For the background of this see:
1415 * https://github.com/BestImageViewer/geeqie/issues/397
1416 * The feature CLUTTER_FEATURE_SWAP_EVENTS indictates if the
1417 * system is liable to exhibit this problem.
1418 * The user is provided with an override in Preferences/Behavior
1420 if (!options->override_disable_gpu && !options->disable_gpu)
1422 DEBUG_1("CLUTTER_FEATURE_SWAP_EVENTS %d",clutter_feature_available(CLUTTER_FEATURE_SWAP_EVENTS));
1423 if (clutter_feature_available(CLUTTER_FEATURE_SWAP_EVENTS) != 0)
1425 options->disable_gpu = TRUE;
1430 /* handle missing config file and commandline additions*/
1431 if (!layout_window_list)
1433 /* broken or no config file or no <layout> section */
1434 layout_new_from_default();
1437 layout_editors_reload_start();
1439 /* If no --list option, open a separate collection window for each
1440 * .gqv file on the command line
1442 if (command_line->collection_list && !command_line->startup_command_line_collection)
1446 work = command_line->collection_list;
1452 path = static_cast<const gchar *>(work->data);
1455 cw = collection_window_new(path);
1456 if (!first_collection && cw) first_collection = cw->cd;
1460 if (command_line->log_file)
1463 gchar *path = g_strdup(command_line->log_file);
1465 pathl = path_from_utf8(path);
1466 command_line->ssi = secure_open(pathl);
1469 /* If there is a files list on the command line and no --list option,
1470 * check if they are all in the same folder
1472 if (command_line->cmd_list && !(command_line->startup_command_line_collection))
1475 gchar *path = nullptr;
1477 work = command_line->cmd_list;
1479 while (work && single_dir)
1483 dirname = g_path_get_dirname(static_cast<const gchar *>(work->data));
1486 path = g_strdup(dirname);
1490 if (g_strcmp0(path, dirname) != 0)
1501 /* Files from multiple folders, or --list option given
1502 * then open an unnamed collection and insert all files
1504 if ((command_line->cmd_list && !single_dir) || (command_line->startup_command_line_collection && command_line->cmd_list))
1509 cw = collection_window_new(nullptr);
1512 collection_path_changed(cd);
1514 work = command_line->cmd_list;
1519 fd = file_data_new_simple(static_cast<const gchar *>(work->data));
1520 collection_add(cd, fd, FALSE);
1521 file_data_unref(fd);
1525 work = command_line->collection_list;
1528 collection_load(cd, static_cast<gchar *>(work->data), COLLECTION_LOAD_APPEND);
1532 if (cd->list) layout_image_set_collection(nullptr, cd, static_cast<CollectInfo *>(cd->list->data));
1534 /* mem leak, we never unref this collection when !startup_command_line_collection
1535 * (the image view of the main window does not hold a ref to the collection)
1536 * this is sort of unavoidable, for if it did hold a ref, next/back
1537 * may not work as expected when closing collection windows.
1539 * collection_unref(cd);
1543 else if (first_collection)
1545 layout_image_set_collection(nullptr, first_collection,
1546 collection_get_first(first_collection));
1549 /* If the files on the command line are from one folder, select those files
1550 * unless it is a command line collection - then leave focus on collection window
1555 if (single_dir && command_line->cmd_list && !command_line->startup_command_line_collection)
1562 work = command_line->cmd_list;
1565 fd = file_data_new_simple(static_cast<gchar *>(work->data));
1566 selected = g_list_append(selected, fd);
1567 file_data_unref(fd);
1570 layout_select_list(lw, selected);
1573 buf = g_build_filename(get_rc_dir(), ".command", NULL);
1574 remote_connection = remote_server_init(buf, cd);
1579 default_settings = gtk_settings_get_default();
1580 g_signal_connect(default_settings, "notify::gtk-theme-name", G_CALLBACK(theme_change_cb), NULL);
1581 set_theme_bg_color();
1584 /* Show a fade-out notification window if the server has a newer AppImage version */
1585 if (options->appimage_notifications && g_getenv("APPDIR") && strstr(g_getenv("APPDIR"), "/tmp/.mount_Geeqie"))
1587 appimage_notification();
1590 DEBUG_1("%s main: gtk_main", get_exec_time());
1593 gdk_threads_leave();
1596 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */