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 *)
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)
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 **, gchar **, GList **)
279 /* This is broken because file filter is not initialized yet.
285 path_parsed = g_strdup(dir);
286 parse_out_relatives(path_parsed);
287 dir_fd = file_data_new_dir(path_parsed);
290 if (filelist_read(dir_fd, &files, NULL))
294 files = filelist_filter(files, FALSE);
295 files = filelist_sort_path(files);
300 FileData *fd = static_cast<FileData *>(work->data);
301 if (!*path) *path = remove_level_from_path(fd->path);
302 if (!*file) *file = g_strdup(fd->path);
303 *list = g_list_prepend(*list, fd);
312 file_data_unref(dir_fd);
314 DEBUG_1("multiple directories specified, ignoring: %s", dir);
318 static void parse_command_line_process_dir(const gchar *dir, gchar **path, gchar **file,
319 GList **list, gchar **first_dir)
322 if (!*list && !*first_dir)
324 *first_dir = g_strdup(dir);
330 parse_command_line_add_dir(*first_dir, path, file, list);
332 *first_dir = nullptr;
334 parse_command_line_add_dir(dir, path, file, list);
338 static void parse_command_line_process_file(const gchar *file_path, gchar **path, gchar **file,
339 GList **list, GList **collection_list, gchar **first_dir)
344 parse_command_line_add_dir(*first_dir, path, file, list);
346 *first_dir = nullptr;
348 parse_command_line_add_file(file_path, path, file, list, collection_list);
351 static void parse_command_line(gint argc, gchar *argv[])
353 GList *list = nullptr;
354 GList *remote_list = nullptr;
355 GList *remote_errors = nullptr;
356 gboolean remote_do = FALSE;
357 gchar *first_dir = nullptr;
361 gchar *geometry = nullptr;
362 GtkWidget *dialog_warning;
363 GString *command_line_errors = g_string_new(nullptr);
365 command_line = g_new0(CommandLine, 1);
367 command_line->argc = argc;
368 command_line->argv = argv;
369 command_line->regexp = nullptr;
374 gchar *base_dir = get_current_dir();
378 gchar *cmd_line = path_to_utf8(argv[i]);
379 gchar *cmd_all = g_build_filename(base_dir, cmd_line, NULL);
381 if (cmd_line[0] == G_DIR_SEPARATOR && isdir(cmd_line))
383 parse_command_line_process_dir(cmd_line, &command_line->path, &command_line->file, &list, &first_dir);
385 else if (isdir(cmd_all))
387 parse_command_line_process_dir(cmd_all, &command_line->path, &command_line->file, &list, &first_dir);
389 else if (cmd_line[0] == G_DIR_SEPARATOR && isfile(cmd_line))
391 parse_command_line_process_file(cmd_line, &command_line->path, &command_line->file,
392 &list, &command_line->collection_list, &first_dir);
394 else if (isfile(cmd_all))
396 parse_command_line_process_file(cmd_all, &command_line->path, &command_line->file,
397 &list, &command_line->collection_list, &first_dir);
399 else if (download_web_file(cmd_line, FALSE, nullptr))
402 else if (is_collection(cmd_line))
404 gchar *path = nullptr;
406 path = collection_path(cmd_line);
407 parse_command_line_process_file(path, &command_line->path, &command_line->file,
408 &list, &command_line->collection_list, &first_dir);
411 else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
413 /* do nothing but do not produce warnings */
415 else if (strncmp(cmd_line, "--disable-clutter", 17) == 0 && (cmd_line[17] == '\0'))
417 /* do nothing but do not produce warnings */
419 else if (strcmp(cmd_line, "+t") == 0 ||
420 strcmp(cmd_line, "--with-tools") == 0)
422 command_line->tools_show = TRUE;
424 remote_list = g_list_append(remote_list, g_strdup("+t"));
426 else if (strcmp(cmd_line, "-t") == 0 ||
427 strcmp(cmd_line, "--without-tools") == 0)
429 command_line->tools_hide = TRUE;
431 remote_list = g_list_append(remote_list, g_strdup("-t"));
433 else if (strcmp(cmd_line, "-f") == 0 ||
434 strcmp(cmd_line, "--fullscreen") == 0)
436 command_line->startup_full_screen = TRUE;
438 else if (strcmp(cmd_line, "-s") == 0 ||
439 strcmp(cmd_line, "--slideshow") == 0)
441 command_line->startup_in_slideshow = TRUE;
443 else if (strcmp(cmd_line, "-l") == 0 ||
444 strcmp(cmd_line, "--list") == 0)
446 command_line->startup_command_line_collection = TRUE;
448 else if (strncmp(cmd_line, "--geometry=", 11) == 0)
450 if (!command_line->geometry) command_line->geometry = g_strdup(cmd_line + 11);
452 else if (strcmp(cmd_line, "-r") == 0 ||
453 strcmp(cmd_line, "--remote") == 0)
458 remote_list = remote_build_list(remote_list, argc - i, &argv[i], &remote_errors);
461 else if ((strcmp(cmd_line, "+w") == 0) ||
462 strcmp(cmd_line, "--show-log-window") == 0)
464 command_line->log_window_show = TRUE;
466 else if (strncmp(cmd_line, "-o:", 3) == 0)
468 command_line->log_file = g_strdup(cmd_line + 3);
470 else if (strncmp(cmd_line, "--log-file:", 11) == 0)
472 command_line->log_file = g_strdup(cmd_line + 11);
474 else if (strncmp(cmd_line, "-g:", 3) == 0)
476 set_regexp(g_strdup(cmd_line+3));
478 else if (strncmp(cmd_line, "-grep:", 6) == 0)
480 set_regexp(g_strdup(cmd_line+3));
482 else if (strncmp(cmd_line, "-n", 2) == 0)
484 command_line->new_instance = TRUE;
486 else if (strncmp(cmd_line, "--new-instance", 14) == 0)
488 command_line->new_instance = TRUE;
490 else if (strcmp(cmd_line, "-rh") == 0 ||
491 strcmp(cmd_line, "--remote-help") == 0)
496 else if (strcmp(cmd_line, "--blank") == 0)
498 command_line->startup_blank = TRUE;
500 else if (strcmp(cmd_line, "-v") == 0 ||
501 strcmp(cmd_line, "--version") == 0)
503 printf_term(FALSE, "%s %s GTK%d\n", GQ_APPNAME, VERSION, gtk_major_version);
506 else if (strcmp(cmd_line, "-h") == 0 ||
507 strcmp(cmd_line, "--help") == 0)
509 printf_term(FALSE, "%s %s\n", GQ_APPNAME, VERSION);
510 printf_term(FALSE, _("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
511 print_term(FALSE, _("Valid options:\n"));
512 print_term(FALSE, _(" --blank start with blank file list\n"));
513 print_term(FALSE, _(" --cache-maintenance <path> run cache maintenance in non-GUI mode\n"));
514 print_term(FALSE, _(" --disable-clutter disable use of Clutter library (i.e. GPU accel.)\n"));
515 print_term(FALSE, _(" -f, --fullscreen start in full screen mode\n"));
516 print_term(FALSE, _(" --geometry=WxH+XOFF+YOFF set main window location\n"));
517 print_term(FALSE, _(" -h, --help show this message\n"));
518 print_term(FALSE, _(" -l, --list [files] [collections] open collection window for command line\n"));
519 print_term(FALSE, _(" -n, --new-instance open a new instance of Geeqie *\n"));
520 print_term(FALSE, _(" -o:, --log-file:<file> save log data to file\n"));
521 print_term(FALSE, _(" -r, --remote send following commands to open window\n"));
522 print_term(FALSE, _(" -rh, --remote-help print remote command list\n"));
523 print_term(FALSE, _(" -s, --slideshow start in slideshow mode\n"));
524 print_term(FALSE, _(" +t, --with-tools force show of tools\n"));
525 print_term(FALSE, _(" -t, --without-tools force hide of tools\n"));
526 print_term(FALSE, _(" -v, --version print version info\n"));
527 print_term(FALSE, _(" +w, --show-log-window show log window\n"));
529 print_term(FALSE, _(" --debug[=level] turn on debug output\n"));
530 print_term(FALSE, _(" -g:, --grep:<regexp> filter debug output\n"));
533 print_term(FALSE, "\n");
534 print_term(FALSE, "* Normally a single set of configuration files is used for all instances.\nHowever, the environment variables XDG_CONFIG_HOME, XDG_CACHE_HOME, XDG_DATA_HOME\ncan be used to modify this behavior on an individual basis e.g.\n\nXDG_CONFIG_HOME=/tmp/a XDG_CACHE_HOME=/tmp/b geeqie\n\n");
543 command_line_errors = g_string_append(command_line_errors, cmd_line);
544 command_line_errors = g_string_append(command_line_errors, "\n");
552 if (command_line_errors->len > 0)
554 dialog_warning = gtk_message_dialog_new(nullptr, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", "Invalid parameter(s):");
555 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_warning), "%s", command_line_errors->str);
556 gtk_window_set_title(GTK_WINDOW(dialog_warning), GQ_APPNAME);
557 gq_gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
558 gtk_dialog_run(GTK_DIALOG(dialog_warning));
559 g_object_unref(dialog_warning);
560 g_string_free(command_line_errors, TRUE);
566 parse_out_relatives(command_line->path);
567 parse_out_relatives(command_line->file);
570 list = g_list_reverse(list);
572 if (!command_line->path && first_dir)
574 command_line->path = first_dir;
577 parse_out_relatives(command_line->path);
581 if (!command_line->new_instance)
583 /* If Geeqie is already running, prevent a second instance
584 * from being started. Open a new window instead.
586 app_lock = g_build_filename(get_rc_dir(), ".command", NULL);
587 if (remote_server_exists(app_lock) && !remote_do)
590 if (command_line->geometry)
592 geometry = g_strdup_printf("--geometry=%s", command_line->geometry);
593 remote_list = g_list_prepend(remote_list, geometry);
595 remote_list = g_list_prepend(remote_list, g_strdup("--new-window"));
604 GList *work = remote_errors;
608 auto opt = static_cast<gchar *>(work->data);
610 command_line_errors = g_string_append(command_line_errors, opt);
611 command_line_errors = g_string_append(command_line_errors, "\n");
615 dialog_warning = gtk_message_dialog_new(nullptr, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", "Invalid parameter(s):");
616 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_warning), "%s", command_line_errors->str);
617 gtk_window_set_title(GTK_WINDOW(dialog_warning), GQ_APPNAME);
618 gq_gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
619 gtk_dialog_run(GTK_DIALOG(dialog_warning));
620 g_object_unref(dialog_warning);
621 g_string_free(command_line_errors, TRUE);
626 /* prepend the current dir the remote command was made from,
627 * for use by any remote command that needs it
629 current_dir = g_get_current_dir();
630 pwd = g_strconcat("--PWD:", current_dir, NULL);
631 remote_list = g_list_prepend(remote_list, pwd);
633 remote_control(argv[0], remote_list, command_line->path, list, command_line->collection_list);
634 /* There is no return to this point
640 g_list_free(remote_list);
642 if (list && list->next)
644 command_line->cmd_list = list;
648 g_list_free_full(list, g_free);
649 command_line->cmd_list = nullptr;
652 if (command_line->startup_blank)
654 g_free(command_line->path);
655 command_line->path = nullptr;
656 g_free(command_line->file);
657 command_line->file = nullptr;
658 filelist_free(command_line->cmd_list);
659 command_line->cmd_list = nullptr;
660 g_list_free_full(command_line->collection_list, g_free);
661 command_line->collection_list = nullptr;
665 static void parse_command_line_for_debug_option(gint argc, gchar *argv[])
668 const gchar *debug_option = "--debug";
669 gint len = strlen(debug_option);
675 for (i = 1; i < argc; i++)
677 const gchar *cmd_line = argv[i];
678 if (strncmp(cmd_line, debug_option, len) == 0)
680 gint cmd_line_len = strlen(cmd_line);
682 /* we now increment the debug state for verbosity */
683 if (cmd_line_len == len)
685 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
687 gint n = atoi(cmd_line + len + 1);
695 DEBUG_1("debugging output enabled (level %d)", get_debug_level());
700 static gboolean parse_command_line_for_clutter_option(gint argc, gchar *argv[])
702 const gchar *clutter_option = "--disable-clutter";
703 gint len = strlen(clutter_option);
704 gboolean ret = FALSE;
710 for (i = 1; i < argc; i++)
712 const gchar *cmd_line = argv[i];
713 if (strncmp(cmd_line, clutter_option, len) == 0)
724 static gboolean parse_command_line_for_cache_maintenance_option(gint argc, gchar *argv[])
726 const gchar *cache_maintenance_option = "--cache-maintenance";
727 gint len = strlen(cache_maintenance_option);
728 gboolean ret = FALSE;
732 const gchar *cmd_line = argv[1];
733 if (strncmp(cmd_line, cache_maintenance_option, len) == 0)
742 static void process_command_line_for_cache_maintenance_option(gint argc, gchar *argv[])
745 gchar *folder_path = nullptr;
748 gchar *buf_config_file;
753 folder_path = expand_tilde(argv[2]);
755 if (isdir(folder_path))
757 rc_path = g_build_filename(get_rc_dir(), RC_FILE_NAME, NULL);
761 if (g_file_get_contents(rc_path, &buf_config_file, &size, nullptr))
765 diff_count = strncmp("</global>", &buf_config_file[i], 9);
772 /* Load only the <global> section */
773 load_config_from_buf(buf_config_file, i + 9, FALSE);
775 if (options->thumbnails.enable_caching)
777 cache_maintenance(folder_path);
781 print_term(TRUE, "Caching not enabled\n");
784 g_free(buf_config_file);
788 print_term(TRUE, g_strconcat(_("Cannot load "), rc_path, "\n", NULL));
794 print_term(TRUE, g_strconcat(_("Configuration file path "), rc_path, _(" is not a file\n"), NULL));
801 print_term(TRUE, g_strconcat(argv[2], _(" is not a folder\n"), NULL));
808 print_term(TRUE, _("No path parameter given\n"));
814 *-----------------------------------------------------------------------------
815 * startup, init, and exit
816 *-----------------------------------------------------------------------------
819 #define RC_HISTORY_NAME "history"
820 #define RC_MARKS_NAME "marks"
822 static void setup_env_path()
824 const gchar *old_path = g_getenv("PATH");
825 gchar *path = g_strconcat(gq_bindir, ":", old_path, NULL);
826 g_setenv("PATH", path, TRUE);
830 static void keys_load()
834 path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
835 history_list_load(path);
839 static void keys_save()
843 path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
844 history_list_save(path);
848 static void marks_load()
852 path = g_build_filename(get_rc_dir(), RC_MARKS_NAME, NULL);
853 marks_list_load(path);
857 static void marks_save(gboolean save)
861 path = g_build_filename(get_rc_dir(), RC_MARKS_NAME, NULL);
862 marks_list_save(path, save);
866 static void mkdir_if_not_exists(const gchar *path)
868 if (isdir(path)) return;
870 log_printf(_("Creating %s dir:%s\n"), GQ_APPNAME, path);
872 if (!recursive_mkdir_if_not_exists(path, 0755))
874 log_printf(_("Could not create dir:%s\n"), path);
879 /* We add to duplicate and modify gtk_accel_map_print() and gtk_accel_map_save()
880 * to improve the reliability in special cases (especially when disk is full)
881 * These functions are now using secure saving stuff.
883 static void gq_accel_map_print(
885 const gchar *accel_path,
887 GdkModifierType accel_mods,
890 GString *gstring = g_string_new(changed ? nullptr : "; ");
891 auto ssi = static_cast<SecureSaveInfo *>(data);
894 g_string_append(gstring, "(gtk_accel_path \"");
896 tmp = g_strescape(accel_path, nullptr);
897 g_string_append(gstring, tmp);
900 g_string_append(gstring, "\" \"");
902 name = gtk_accelerator_name(accel_key, accel_mods);
903 tmp = g_strescape(name, nullptr);
905 g_string_append(gstring, tmp);
908 g_string_append(gstring, "\")\n");
910 secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
912 g_string_free(gstring, TRUE);
915 static gboolean gq_accel_map_save(const gchar *path)
921 pathl = path_from_utf8(path);
922 ssi = secure_open(pathl);
926 log_printf(_("error saving file: %s\n"), path);
930 gstring = g_string_new("; ");
932 g_string_append(gstring, g_get_prgname());
933 g_string_append(gstring, " GtkAccelMap rc-file -*- scheme -*-\n");
934 g_string_append(gstring, "; this file is an automated accelerator map dump\n");
935 g_string_append(gstring, ";\n");
937 secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
939 g_string_free(gstring, TRUE);
941 gtk_accel_map_foreach(ssi, gq_accel_map_print);
943 if (secure_close(ssi))
945 log_printf(_("error saving file: %s\nerror: %s\n"), path,
946 secsave_strerror(secsave_errno));
953 static gchar *accep_map_filename()
955 return g_build_filename(get_rc_dir(), "accels", NULL);
958 static void accel_map_save()
962 path = accep_map_filename();
963 gq_accel_map_save(path);
967 static void accel_map_load()
972 path = accep_map_filename();
973 pathl = path_from_utf8(path);
974 gtk_accel_map_load(pathl);
979 static void gtkrc_load()
984 /* If a gtkrc file exists in the rc directory, add it to the
985 * list of files to be parsed at the end of gtk_init() */
986 path = g_build_filename(get_rc_dir(), "gtkrc", NULL);
987 pathl = path_from_utf8(path);
988 if (access(pathl, R_OK) == 0)
989 gtk_rc_add_default_file(pathl);
994 static void exit_program_final()
996 LayoutWindow *lw = nullptr;
998 LayoutWindow *tmp_lw;
1000 GFile *archive_file;
1002 /* make sure that external editors are loaded, we would save incomplete configuration otherwise */
1003 layout_editors_reload_finish();
1005 remote_close(remote_connection);
1007 collect_manager_flush();
1009 /* Save the named windows */
1010 if (layout_window_list && layout_window_list->next)
1012 list = layout_window_list;
1015 tmp_lw = static_cast<LayoutWindow *>(list->data);
1016 if (!g_str_has_prefix(tmp_lw->options.id, "lw"))
1018 save_layout(static_cast<LayoutWindow *>(list->data));
1024 save_options(options);
1028 if (layout_valid(&lw))
1033 /* Delete any files/folders in /tmp that have been created by the open archive function */
1034 archive_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, NULL);
1035 if (isdir(archive_dir))
1037 archive_file = g_file_new_for_path(archive_dir);
1038 rmdir_recursive(archive_file, nullptr, nullptr);
1039 g_free(archive_dir);
1040 g_object_unref(archive_file);
1043 /* If there are still sub-dirs created by another instance, this will fail
1044 * but that does not matter */
1045 archive_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, NULL);
1046 if (isdir(archive_dir))
1048 archive_file = g_file_new_for_path(archive_dir);
1049 g_file_delete(archive_file, nullptr, nullptr);
1050 g_free(archive_dir);
1051 g_object_unref(archive_file);
1054 secure_close(command_line->ssi);
1059 static GenericDialog *exit_dialog = nullptr;
1061 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer)
1063 exit_dialog = nullptr;
1064 generic_dialog_close(gd);
1067 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer)
1069 exit_dialog = nullptr;
1070 generic_dialog_close(gd);
1071 exit_program_final();
1074 static gint exit_confirm_dlg()
1083 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
1087 if (!collection_window_modified_exists() && (layout_window_count() == 1)) return FALSE;
1091 if (layout_valid(&lw))
1093 parent = lw->window;
1096 msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
1097 exit_dialog = generic_dialog_new(msg,
1098 "exit", parent, FALSE,
1099 exit_confirm_cancel_cb, nullptr);
1101 msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
1103 message = g_string_new(NULL);
1105 if (collection_window_modified_exists())
1107 message = g_string_append(message, _("Collections have been modified.\n"));
1110 if (layout_window_count() > 1)
1112 g_string_append_printf(message, _("%d windows are open.\n\n"), layout_window_count());
1115 message = g_string_append(message, _("Quit anyway?"));
1117 generic_dialog_add_message(exit_dialog, GQ_ICON_DIALOG_QUESTION, msg, message->str, TRUE);
1119 generic_dialog_add_button(exit_dialog, GQ_ICON_QUIT, _("Quit"), exit_confirm_exit_cb, TRUE);
1121 gtk_widget_show(exit_dialog->dialog);
1123 g_string_free(message, TRUE);
1128 static void exit_program_write_metadata_cb(gint success, const gchar *, gpointer)
1130 if (success) exit_program();
1135 layout_image_full_screen_stop(nullptr);
1137 if (metadata_write_queue_confirm(FALSE, exit_program_write_metadata_cb, nullptr)) return;
1139 options->marks_save ? marks_save(TRUE) : marks_save(FALSE);
1141 if (exit_confirm_dlg()) return;
1143 exit_program_final();
1146 /* This code attempts to handle situation when a file mmaped by image_loader
1147 * or by exif loader is truncated by some other process.
1148 * This code is incorrect according to POSIX, because:
1150 * mmap is not async-signal-safe and thus may not be called from a signal handler
1152 * mmap must be called with a valid file descriptor. POSIX requires that
1153 * a fildes argument of -1 must cause mmap to return EBADF.
1155 * See https://github.com/BestImageViewer/geeqie/issues/1052 for discussion of
1156 * an alternative approach.
1158 /** @FIXME this probably needs some better ifdefs. Please report any compilation problems */
1159 /** @FIXME This section needs revising */
1161 #pragma GCC diagnostic push
1162 #pragma GCC diagnostic ignored "-Wunused-function"
1163 #if defined(SIGBUS) && defined(SA_SIGINFO)
1164 static void sigbus_handler_cb_unused(int, siginfo_t *info, void *)
1167 * @FIXME Design and implement a POSIX-acceptable approach,
1168 * after first documenting the sitations where SIGBUS occurs.
1169 * See https://github.com/BestImageViewer/geeqie/issues/1052 for discussion
1172 DEBUG_1("SIGBUS %p NOT HANDLED", info->si_addr);
1177 static void setup_sigbus_handler_unused(void)
1179 #if defined(SIGBUS) && defined(SA_SIGINFO)
1180 struct sigaction sigbus_action;
1181 sigfillset(&sigbus_action.sa_mask);
1182 sigbus_action.sa_sigaction = sigbus_handler_cb_unused;
1183 sigbus_action.sa_flags = SA_SIGINFO;
1185 sigaction(SIGBUS, &sigbus_action, NULL);
1188 #pragma GCC diagnostic pop
1190 #ifndef HAVE_DEVELOPER
1191 static void setup_sig_handler()
1193 struct sigaction sigsegv_action;
1194 sigfillset(&sigsegv_action.sa_mask);
1195 sigsegv_action.sa_sigaction = sig_handler_cb;
1196 sigsegv_action.sa_flags = SA_SIGINFO;
1198 sigaction(SIGABRT, &sigsegv_action, nullptr);
1199 sigaction(SIGBUS, &sigsegv_action, nullptr);
1200 sigaction(SIGFPE, &sigsegv_action, nullptr);
1201 sigaction(SIGILL, &sigsegv_action, nullptr);
1202 sigaction(SIGIOT, &sigsegv_action, nullptr);
1203 sigaction(SIGSEGV, &sigsegv_action, nullptr);
1207 static void set_theme_bg_color()
1210 GdkRGBA theme_color;
1211 GtkStyleContext *style_context;
1215 if (!options->image.use_custom_border_color)
1217 work = layout_window_list;
1218 lw = static_cast<LayoutWindow *>(work->data);
1220 style_context = gtk_widget_get_style_context(lw->window);
1221 gtk_style_context_get_background_color(style_context, GTK_STATE_FLAG_NORMAL, &bg_color);
1223 theme_color.red = bg_color.red ;
1224 theme_color.green = bg_color.green ;
1225 theme_color.blue = bg_color.blue ;
1229 lw = static_cast<LayoutWindow *>(work->data);
1230 image_background_set_color(lw->image, &theme_color);
1235 view_window_colors_update();
1238 static gboolean theme_change_cb(GObject *, GParamSpec *, gpointer)
1240 set_theme_bg_color();
1246 * @brief Set up the application paths
1248 * This function is required for use of AppImages. AppImages are
1249 * relocatable, and therefore cannot use fixed paths to various components.
1250 * These paths were originally #defines created during compilation.
1251 * They are now variables, all defined relative to one level above the
1252 * directory that the executable is run from.
1254 static void create_application_paths()
1260 length = wai_getExecutablePath(nullptr, 0, nullptr);
1261 path = static_cast<gchar *>(malloc(length + 1));
1262 wai_getExecutablePath(path, length, nullptr);
1263 path[length] = '\0';
1265 gq_executable_path = g_strdup(path);
1266 dirname = g_path_get_dirname(gq_executable_path);
1267 gq_prefix = g_path_get_dirname(dirname);
1269 gq_localedir = g_build_filename(gq_prefix, GQ_LOCALEDIR, NULL);
1270 gq_helpdir = g_build_filename(gq_prefix, GQ_HELPDIR, NULL);
1271 gq_htmldir = g_build_filename(gq_prefix, GQ_HTMLDIR, NULL);
1272 gq_appdir = g_build_filename(gq_prefix, GQ_APPDIR, NULL);
1273 gq_bindir = g_build_filename(gq_prefix, GQ_BINDIR, NULL);
1274 desktop_file_template = g_build_filename(gq_appdir, "org.geeqie.template.desktop", NULL);
1280 gint main(gint argc, gchar *argv[])
1282 CollectionData *cd = nullptr;
1283 CollectionData *first_collection = nullptr;
1284 gboolean disable_clutter = FALSE;
1285 gboolean single_dir = TRUE;
1288 GtkCssProvider *provider;
1289 GtkSettings *default_settings;
1292 gdk_set_allowed_backends("x11,*");
1295 gdk_threads_enter();
1297 /* seg. fault handler */
1298 #ifdef HAVE_DEVELOPER
1299 backward::SignalHandling sh{};
1301 setup_sig_handler();
1304 /* init execution time counter (debug only) */
1307 create_application_paths();
1309 /* setup locale, i18n */
1310 setlocale(LC_ALL, "");
1313 bindtextdomain(PACKAGE, gq_localedir);
1314 bind_textdomain_codeset(PACKAGE, "UTF-8");
1315 textdomain(PACKAGE);
1324 /* setup random seed for random slideshow */
1325 srand(time(nullptr));
1328 /* See later comment; this handler leads to UB. */
1329 setup_sigbus_handler();
1332 /* register global notify functions */
1333 file_data_register_notify_func(cache_notify_cb, nullptr, NOTIFY_PRIORITY_HIGH);
1334 file_data_register_notify_func(thumb_notify_cb, nullptr, NOTIFY_PRIORITY_HIGH);
1335 file_data_register_notify_func(histogram_notify_cb, nullptr, NOTIFY_PRIORITY_HIGH);
1336 file_data_register_notify_func(collect_manager_notify_cb, nullptr, NOTIFY_PRIORITY_LOW);
1337 file_data_register_notify_func(metadata_notify_cb, nullptr, NOTIFY_PRIORITY_LOW);
1342 parse_command_line_for_debug_option(argc, argv);
1343 DEBUG_1("%s main: gtk_init", get_exec_time());
1345 if (parse_command_line_for_clutter_option(argc, argv))
1347 disable_clutter = TRUE;
1348 gtk_init(&argc, &argv);
1352 if (gtk_clutter_init(&argc, &argv) != CLUTTER_INIT_SUCCESS)
1354 log_printf("Can't initialize clutter-gtk.\nStart Geeqie with the option \"geeqie --disable-clutter\"");
1355 runcmd("zenity --error --title=\"Geeqie\" --text \"Can't initialize clutter-gtk.\n\nStart Geeqie with the option:\n geeqie --disable-clutter\" --width=300");
1360 gtk_init(&argc, &argv);
1363 if (gtk_major_version < GTK_MAJOR_VERSION ||
1364 (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
1366 log_printf("!!! This is a friendly warning.\n");
1367 log_printf("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
1368 log_printf("!!! compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
1369 log_printf("!!! running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
1370 log_printf("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
1373 DEBUG_1("%s main: pixbuf_inline_register_stock_icons", get_exec_time());
1374 gtk_icon_theme_add_resource_path(gtk_icon_theme_get_default(), GQ_RESOURCE_PATH_ICONS);
1375 pixbuf_inline_register_stock_icons();
1377 DEBUG_1("%s main: setting default options before commandline handling", get_exec_time());
1378 options = init_options(nullptr);
1379 setup_default_options(options);
1380 if (disable_clutter)
1382 options->disable_gpu = TRUE;
1385 /* Generate a unique identifier used by the open archive function */
1386 instance_identifier = g_strdup_printf("%x", g_random_int());
1388 DEBUG_1("%s main: mkdir_if_not_exists", get_exec_time());
1389 /* these functions don't depend on config file */
1390 mkdir_if_not_exists(get_rc_dir());
1391 mkdir_if_not_exists(get_collections_dir());
1392 mkdir_if_not_exists(get_thumbnails_cache_dir());
1393 mkdir_if_not_exists(get_metadata_cache_dir());
1394 mkdir_if_not_exists(get_window_layouts_dir());
1398 screen = gdk_screen_get_default();
1399 provider = gtk_css_provider_new();
1400 gtk_css_provider_load_from_resource(provider, GQ_RESOURCE_PATH_UI "/custom.css");
1401 gtk_style_context_add_provider_for_screen(screen, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
1403 if (parse_command_line_for_cache_maintenance_option(argc, argv))
1405 process_command_line_for_cache_maintenance_option(argc, argv);
1409 DEBUG_1("%s main: parse_command_line", get_exec_time());
1410 parse_command_line(argc, argv);
1415 /* restore session from the config file */
1418 DEBUG_1("%s main: load_options", get_exec_time());
1419 if (!load_options(options))
1421 /* load_options calls these functions after it parses global options, we have to call it here if it fails */
1422 filter_add_defaults();
1427 /** @FIXME For the background of this see:
1428 * https://github.com/BestImageViewer/geeqie/issues/397
1429 * The feature CLUTTER_FEATURE_SWAP_EVENTS indictates if the
1430 * system is liable to exhibit this problem.
1431 * The user is provided with an override in Preferences/Behavior
1433 if (!options->override_disable_gpu && !options->disable_gpu)
1435 DEBUG_1("CLUTTER_FEATURE_SWAP_EVENTS %d",clutter_feature_available(CLUTTER_FEATURE_SWAP_EVENTS));
1436 if (clutter_feature_available(CLUTTER_FEATURE_SWAP_EVENTS) != 0)
1438 options->disable_gpu = TRUE;
1443 /* handle missing config file and commandline additions*/
1444 if (!layout_window_list)
1446 /* broken or no config file or no <layout> section */
1447 layout_new_from_default();
1450 layout_editors_reload_start();
1452 /* If no --list option, open a separate collection window for each
1453 * .gqv file on the command line
1455 if (command_line->collection_list && !command_line->startup_command_line_collection)
1459 work = command_line->collection_list;
1465 path = static_cast<const gchar *>(work->data);
1468 cw = collection_window_new(path);
1469 if (!first_collection && cw) first_collection = cw->cd;
1473 if (command_line->log_file)
1476 gchar *path = g_strdup(command_line->log_file);
1478 pathl = path_from_utf8(path);
1479 command_line->ssi = secure_open(pathl);
1482 /* If there is a files list on the command line and no --list option,
1483 * check if they are all in the same folder
1485 if (command_line->cmd_list && !(command_line->startup_command_line_collection))
1488 gchar *path = nullptr;
1490 work = command_line->cmd_list;
1492 while (work && single_dir)
1496 dirname = g_path_get_dirname(static_cast<const gchar *>(work->data));
1499 path = g_strdup(dirname);
1503 if (g_strcmp0(path, dirname) != 0)
1514 /* Files from multiple folders, or --list option given
1515 * then open an unnamed collection and insert all files
1517 if ((command_line->cmd_list && !single_dir) || (command_line->startup_command_line_collection && command_line->cmd_list))
1522 cw = collection_window_new(nullptr);
1525 collection_path_changed(cd);
1527 work = command_line->cmd_list;
1532 fd = file_data_new_simple(static_cast<const gchar *>(work->data));
1533 collection_add(cd, fd, FALSE);
1534 file_data_unref(fd);
1538 work = command_line->collection_list;
1541 collection_load(cd, static_cast<gchar *>(work->data), COLLECTION_LOAD_APPEND);
1545 if (cd->list) layout_image_set_collection(nullptr, cd, static_cast<CollectInfo *>(cd->list->data));
1547 /* mem leak, we never unref this collection when !startup_command_line_collection
1548 * (the image view of the main window does not hold a ref to the collection)
1549 * this is sort of unavoidable, for if it did hold a ref, next/back
1550 * may not work as expected when closing collection windows.
1552 * collection_unref(cd);
1556 else if (first_collection)
1558 layout_image_set_collection(nullptr, first_collection,
1559 collection_get_first(first_collection));
1562 /* If the files on the command line are from one folder, select those files
1563 * unless it is a command line collection - then leave focus on collection window
1568 if (single_dir && command_line->cmd_list && !command_line->startup_command_line_collection)
1575 work = command_line->cmd_list;
1578 fd = file_data_new_simple(static_cast<gchar *>(work->data));
1579 selected = g_list_append(selected, fd);
1580 file_data_unref(fd);
1583 layout_select_list(lw, selected);
1586 buf = g_build_filename(get_rc_dir(), ".command", NULL);
1587 remote_connection = remote_server_init(buf, cd);
1592 default_settings = gtk_settings_get_default();
1593 g_signal_connect(default_settings, "notify::gtk-theme-name", G_CALLBACK(theme_change_cb), NULL);
1594 set_theme_bg_color();
1597 /* Show a fade-out notification window if the server has a newer AppImage version */
1598 if (options->appimage_notifications)
1600 if (g_getenv("APPDIR") && strstr(g_getenv("APPDIR"), "/tmp/.mount_Geeqie"))
1602 appimage_notification();
1604 else if (g_strstr_len(gq_executable_path, -1, "AppRun"))
1606 /* Probably running an extracted AppImage */
1607 appimage_notification();
1611 DEBUG_1("%s main: gtk_main", get_exec_time());
1614 gdk_threads_leave();
1617 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */