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.
35 # include <clutter-gtk/clutter-gtk.h>
36 # include <clutter/clutter.h>
45 #include <glib-object.h>
53 #include "third-party/backward.h"
56 #include "cache-maint.h"
58 #include "collect-io.h"
64 #include "filefilter.h"
66 #include "histogram.h"
67 #include "history-list.h"
71 #include "layout-image.h"
72 #include "layout-util.h"
74 #include "main-defines.h"
78 #include "pixbuf-util.h"
81 #include "secure-save.h"
82 #include "third-party/whereami.h"
84 #include "ui-fileops.h"
85 #include "ui-utildlg.h"
87 gboolean thumb_format_changed = FALSE;
88 static RemoteConnection *remote_connection = nullptr;
96 gchar *gq_executable_path;
97 gchar *desktop_file_template;
98 gchar *instance_identifier;
100 #if defined(SA_SIGINFO)
101 void sig_handler_cb(int signo, siginfo_t *info, void *)
104 const gchar *signal_name = nullptr;
116 const gchar *sig_name;
118 struct signals signals_list[7];
120 signals_list[0].sig_no = SIGABRT;
121 signals_list[0].sig_name = "Abort";
122 signals_list[1].sig_no = SIGBUS;
123 signals_list[1].sig_name = "Bus error";
124 signals_list[2].sig_no = SIGFPE;
125 signals_list[2].sig_name = "Floating-point exception";
126 signals_list[3].sig_no = SIGILL;
127 signals_list[3].sig_name = "Illegal instruction";
128 signals_list[4].sig_no = SIGIOT;
129 signals_list[4].sig_name = "IOT trap";
130 signals_list[5].sig_no = SIGSEGV;
131 signals_list[5].sig_name = "Invalid memory reference";
132 signals_list[6].sig_no = -1;
133 signals_list[6].sig_name = "END";
152 signal_name = "Unknown signal";
153 while (signals_list[i].sig_no != -1)
155 if (signo == signals_list[i].sig_no)
157 signal_name = signals_list[i].sig_name;
163 len = write(STDERR_FILENO, "Geeqie fatal error\n", 19);
164 len = write(STDERR_FILENO, "Signal: ", 8);
165 len = write(STDERR_FILENO, signal_name, strlen(signal_name));
166 len = write(STDERR_FILENO, "\n", 1);
168 len = write(STDERR_FILENO, "Code: ", 6);
169 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"));
170 len = write(STDERR_FILENO, "\n", 1);
172 len = write(STDERR_FILENO, "Address: ", 9);
174 if (info->si_addr == nullptr)
176 len = write(STDERR_FILENO, "0x0\n", 4);
180 /* Assume the address is 64-bit */
181 len = write(STDERR_FILENO, "0x", 2);
182 addr = reinterpret_cast<guint64>(info->si_addr);
184 for (i = 0; i < 16; i++)
186 char_index = addr & 0xf000000000000000;
187 char_index = char_index >> 60;
190 len = write(STDERR_FILENO, &hex_char[char_index], 1);
192 len = write(STDERR_FILENO, "\n", 1);
196 bt_size = backtrace(bt, 1024);
197 backtrace_symbols_fd(bt, bt_size, STDERR_FILENO);
200 /* Avoid "not used" warning */
205 #else /* defined(SA_SIGINFO) */
206 void sig_handler_cb(int)
213 write(STDERR_FILENO, "Geeqie fatal error\n", 19);
214 write(STDERR_FILENO, "Signal: Segmentation fault\n", 27);
217 bt_size = backtrace(bt, 1024);
218 backtrace_symbols_fd(bt, bt_size, STDERR_FILENO);
223 #endif /* defined(SA_SIGINFO) */
226 *-----------------------------------------------------------------------------
227 * command line parser (private) hehe, who needs popt anyway?
228 *-----------------------------------------------------------------------------
231 static void parse_command_line_add_file(const gchar *file_path, gchar **path, gchar **file,
232 GList **list, GList **collection_list)
236 path_parsed = g_strdup(file_path);
237 parse_out_relatives(path_parsed);
239 if (file_extension_match(path_parsed, GQ_COLLECTION_EXT))
241 *collection_list = g_list_append(*collection_list, path_parsed);
245 if (!*path) *path = remove_level_from_path(path_parsed);
246 if (!*file) *file = g_strdup(path_parsed);
247 *list = g_list_prepend(*list, path_parsed);
251 static void parse_command_line_add_dir(const gchar *dir, gchar **, gchar **, GList **)
254 /* This is broken because file filter is not initialized yet.
260 path_parsed = g_strdup(dir);
261 parse_out_relatives(path_parsed);
262 dir_fd = file_data_new_dir(path_parsed);
265 if (filelist_read(dir_fd, &files, NULL))
269 files = filelist_filter(files, FALSE);
270 files = filelist_sort_path(files);
275 FileData *fd = static_cast<FileData *>(work->data);
276 if (!*path) *path = remove_level_from_path(fd->path);
277 if (!*file) *file = g_strdup(fd->path);
278 *list = g_list_prepend(*list, fd);
287 file_data_unref(dir_fd);
289 DEBUG_1("multiple directories specified, ignoring: %s", dir);
293 static void parse_command_line_process_dir(const gchar *dir, gchar **path, gchar **file,
294 GList **list, gchar **first_dir)
297 if (!*list && !*first_dir)
299 *first_dir = g_strdup(dir);
305 parse_command_line_add_dir(*first_dir, path, file, list);
307 *first_dir = nullptr;
309 parse_command_line_add_dir(dir, path, file, list);
313 static void parse_command_line_process_file(const gchar *file_path, gchar **path, gchar **file,
314 GList **list, GList **collection_list, gchar **first_dir)
319 parse_command_line_add_dir(*first_dir, path, file, list);
321 *first_dir = nullptr;
323 parse_command_line_add_file(file_path, path, file, list, collection_list);
326 static void parse_command_line(gint argc, gchar *argv[])
328 GList *list = nullptr;
329 GList *remote_list = nullptr;
330 GList *remote_errors = nullptr;
331 gboolean remote_do = FALSE;
332 gchar *first_dir = nullptr;
336 gchar *geometry = nullptr;
337 GtkWidget *dialog_warning;
338 GString *command_line_errors = g_string_new(nullptr);
340 command_line = g_new0(CommandLine, 1);
342 command_line->argc = argc;
343 command_line->argv = argv;
344 command_line->regexp = nullptr;
349 gchar *base_dir = get_current_dir();
353 gchar *cmd_line = path_to_utf8(argv[i]);
354 gchar *cmd_all = g_build_filename(base_dir, cmd_line, NULL);
356 if (cmd_line[0] == G_DIR_SEPARATOR && isdir(cmd_line))
358 parse_command_line_process_dir(cmd_line, &command_line->path, &command_line->file, &list, &first_dir);
360 else if (isdir(cmd_all))
362 parse_command_line_process_dir(cmd_all, &command_line->path, &command_line->file, &list, &first_dir);
364 else if (cmd_line[0] == G_DIR_SEPARATOR && isfile(cmd_line))
366 parse_command_line_process_file(cmd_line, &command_line->path, &command_line->file,
367 &list, &command_line->collection_list, &first_dir);
369 else if (isfile(cmd_all))
371 parse_command_line_process_file(cmd_all, &command_line->path, &command_line->file,
372 &list, &command_line->collection_list, &first_dir);
374 else if (download_web_file(cmd_line, FALSE, nullptr))
377 else if (is_collection(cmd_line))
379 gchar *path = nullptr;
381 path = collection_path(cmd_line);
382 parse_command_line_process_file(path, &command_line->path, &command_line->file,
383 &list, &command_line->collection_list, &first_dir);
386 else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
388 /* do nothing but do not produce warnings */
390 else if (strncmp(cmd_line, "--disable-clutter", 17) == 0 && (cmd_line[17] == '\0'))
392 /* do nothing but do not produce warnings */
394 else if (strcmp(cmd_line, "-T") == 0 ||
395 strcmp(cmd_line, "--with-tools") == 0)
397 command_line->tools_show = TRUE;
399 remote_list = g_list_append(remote_list, g_strdup("-T"));
401 else if (strcmp(cmd_line, "-t") == 0 ||
402 strcmp(cmd_line, "--without-tools") == 0)
404 command_line->tools_hide = TRUE;
406 remote_list = g_list_append(remote_list, g_strdup("-t"));
408 else if (strcmp(cmd_line, "-f") == 0 ||
409 strcmp(cmd_line, "--fullscreen") == 0)
411 command_line->startup_full_screen = TRUE;
413 else if (strcmp(cmd_line, "-s") == 0 ||
414 strcmp(cmd_line, "--slideshow") == 0)
416 command_line->startup_in_slideshow = TRUE;
418 else if (strcmp(cmd_line, "-l") == 0 ||
419 strcmp(cmd_line, "--list") == 0)
421 command_line->startup_command_line_collection = TRUE;
423 else if (strncmp(cmd_line, "--geometry=", 11) == 0)
425 if (!command_line->geometry) command_line->geometry = g_strdup(cmd_line + 11);
427 else if (strcmp(cmd_line, "-r") == 0 ||
428 strcmp(cmd_line, "--remote") == 0)
433 remote_list = remote_build_list(remote_list, argc - i, &argv[i], &remote_errors);
436 else if ((strcmp(cmd_line, "-w") == 0) ||
437 strcmp(cmd_line, "--show-log-window") == 0)
439 command_line->log_window_show = TRUE;
441 else if (strncmp(cmd_line, "-o:", 3) == 0)
443 command_line->log_file = g_strdup(cmd_line + 3);
445 else if (strncmp(cmd_line, "--log-file:", 11) == 0)
447 command_line->log_file = g_strdup(cmd_line + 11);
449 else if (strncmp(cmd_line, "-g:", 3) == 0)
451 set_regexp(g_strdup(cmd_line+3));
453 else if (strncmp(cmd_line, "-grep:", 6) == 0)
455 set_regexp(g_strdup(cmd_line+3));
457 else if (strncmp(cmd_line, "-n", 2) == 0)
459 command_line->new_instance = TRUE;
461 else if (strncmp(cmd_line, "--new-instance", 14) == 0)
463 command_line->new_instance = TRUE;
465 else if (strcmp(cmd_line, "-rh") == 0 ||
466 strcmp(cmd_line, "--remote-help") == 0)
471 else if (strcmp(cmd_line, "--blank") == 0)
473 command_line->startup_blank = TRUE;
475 else if (strcmp(cmd_line, "-v") == 0 ||
476 strcmp(cmd_line, "--version") == 0)
478 printf_term(FALSE, "%s %s GTK%d\n", GQ_APPNAME, VERSION, gtk_major_version);
481 else if (strcmp(cmd_line, "-h") == 0 ||
482 strcmp(cmd_line, "--help") == 0)
484 printf_term(FALSE, "%s %s\n", GQ_APPNAME, VERSION);
485 printf_term(FALSE, _("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
486 print_term(FALSE, _("Valid options:\n"));
487 print_term(FALSE, _(" --blank start with blank file list\n"));
488 print_term(FALSE, _(" --cache-maintenance <path> run cache maintenance in non-GUI mode\n"));
489 print_term(FALSE, _(" --disable-clutter disable use of Clutter library (i.e. GPU accel.)\n"));
490 print_term(FALSE, _(" -f, --fullscreen start in full screen mode\n"));
491 print_term(FALSE, _(" --geometry=WxH+XOFF+YOFF set main window location\n"));
492 print_term(FALSE, _(" -h, --help show this message\n"));
493 print_term(FALSE, _(" -l, --list [files] [collections] open collection window for command line\n"));
494 print_term(FALSE, _(" -n, --new-instance open a new instance of Geeqie *\n"));
495 print_term(FALSE, _(" -o:, --log-file:<file> save log data to file\n"));
496 print_term(FALSE, _(" -r, --remote send following commands to open window\n"));
497 print_term(FALSE, _(" -rh, --remote-help print remote command list\n"));
498 print_term(FALSE, _(" -s, --slideshow start in slideshow mode\n"));
499 print_term(FALSE, _(" -T, --with-tools force show of tools\n"));
500 print_term(FALSE, _(" -t, --without-tools force hide of tools\n"));
501 print_term(FALSE, _(" -v, --version print version info\n"));
502 print_term(FALSE, _(" -w, --show-log-window show log window\n"));
504 print_term(FALSE, _(" --debug[=level] turn on debug output\n"));
505 print_term(FALSE, _(" -g:, --grep:<regexp> filter debug output\n"));
508 print_term(FALSE, "\n");
509 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");
518 g_string_append_printf(command_line_errors, is_remote_command(cmd_line) ? _("%s\n\nThis is a --remote command option\n") : _("%s\n\nThis option is unknown\n"), cmd_line);
526 if (command_line_errors->len > 0)
528 dialog_warning = gtk_message_dialog_new(nullptr, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", "Invalid parameter(s):");
529 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_warning), "%s", command_line_errors->str);
530 gtk_window_set_title(GTK_WINDOW(dialog_warning), GQ_APPNAME);
531 gq_gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
532 gtk_dialog_run(GTK_DIALOG(dialog_warning));
533 g_object_unref(dialog_warning);
534 g_string_free(command_line_errors, TRUE);
540 parse_out_relatives(command_line->path);
541 parse_out_relatives(command_line->file);
544 list = g_list_reverse(list);
546 if (!command_line->path && first_dir)
548 command_line->path = first_dir;
551 parse_out_relatives(command_line->path);
555 if (!command_line->new_instance)
557 /* If Geeqie is already running, prevent a second instance
558 * from being started. Open a new window instead.
560 app_lock = g_build_filename(get_rc_dir(), ".command", NULL);
561 if (remote_server_exists(app_lock) && !remote_do)
564 if (command_line->geometry)
566 geometry = g_strdup_printf("--geometry=%s", command_line->geometry);
567 remote_list = g_list_prepend(remote_list, geometry);
569 remote_list = g_list_prepend(remote_list, g_strdup("--new-window"));
578 GList *work = remote_errors;
582 auto opt = static_cast<gchar *>(work->data);
584 command_line_errors = g_string_append(command_line_errors, opt);
585 command_line_errors = g_string_append(command_line_errors, "\n");
589 dialog_warning = gtk_message_dialog_new(nullptr, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", "Invalid parameter(s):");
590 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_warning), "%s", command_line_errors->str);
591 gtk_window_set_title(GTK_WINDOW(dialog_warning), GQ_APPNAME);
592 gq_gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
593 gtk_dialog_run(GTK_DIALOG(dialog_warning));
594 g_object_unref(dialog_warning);
595 g_string_free(command_line_errors, TRUE);
600 /* prepend the current dir the remote command was made from,
601 * for use by any remote command that needs it
603 current_dir = g_get_current_dir();
604 pwd = g_strconcat("--PWD:", current_dir, NULL);
605 remote_list = g_list_prepend(remote_list, pwd);
607 remote_control(argv[0], remote_list, command_line->path, list, command_line->collection_list);
608 /* There is no return to this point
614 g_list_free(remote_list);
616 if (list && list->next)
618 command_line->cmd_list = list;
622 g_list_free_full(list, g_free);
623 command_line->cmd_list = nullptr;
626 if (command_line->startup_blank)
628 g_free(command_line->path);
629 command_line->path = nullptr;
630 g_free(command_line->file);
631 command_line->file = nullptr;
632 filelist_free(command_line->cmd_list);
633 command_line->cmd_list = nullptr;
634 g_list_free_full(command_line->collection_list, g_free);
635 command_line->collection_list = nullptr;
639 static void parse_command_line_for_debug_option(gint argc, gchar *argv[])
642 const gchar *debug_option = "--debug";
643 gint len = strlen(debug_option);
649 for (i = 1; i < argc; i++)
651 const gchar *cmd_line = argv[i];
652 if (strncmp(cmd_line, debug_option, len) == 0)
654 gint cmd_line_len = strlen(cmd_line);
656 /* we now increment the debug state for verbosity */
657 if (cmd_line_len == len)
659 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
661 gint n = atoi(cmd_line + len + 1);
669 DEBUG_1("debugging output enabled (level %d)", get_debug_level());
674 static gboolean parse_command_line_for_clutter_option(gint argc, gchar *argv[])
676 const gchar *clutter_option = "--disable-clutter";
677 gint len = strlen(clutter_option);
678 gboolean ret = FALSE;
684 for (i = 1; i < argc; i++)
686 const gchar *cmd_line = argv[i];
687 if (strncmp(cmd_line, clutter_option, len) == 0)
698 static gboolean parse_command_line_for_cache_maintenance_option(gint argc, gchar *argv[])
700 const gchar *cache_maintenance_option = "--cache-maintenance";
701 gint len = strlen(cache_maintenance_option);
702 gboolean ret = FALSE;
706 const gchar *cmd_line = argv[1];
707 if (strncmp(cmd_line, cache_maintenance_option, len) == 0)
716 static void process_command_line_for_cache_maintenance_option(gint argc, gchar *argv[])
719 gchar *folder_path = nullptr;
722 gchar *buf_config_file;
727 folder_path = expand_tilde(argv[2]);
729 if (isdir(folder_path))
731 rc_path = g_build_filename(get_rc_dir(), RC_FILE_NAME, NULL);
735 if (g_file_get_contents(rc_path, &buf_config_file, &size, nullptr))
739 diff_count = strncmp("</global>", &buf_config_file[i], 9);
746 /* Load only the <global> section */
747 load_config_from_buf(buf_config_file, i + 9, FALSE);
749 if (options->thumbnails.enable_caching)
751 cache_maintenance(folder_path);
755 print_term(TRUE, "Caching not enabled\n");
758 g_free(buf_config_file);
762 print_term(TRUE, g_strconcat(_("Cannot load "), rc_path, "\n", NULL));
768 print_term(TRUE, g_strconcat(_("Configuration file path "), rc_path, _(" is not a file\n"), NULL));
775 print_term(TRUE, g_strconcat(argv[2], _(" is not a folder\n"), NULL));
782 print_term(TRUE, _("No path parameter given\n"));
788 *-----------------------------------------------------------------------------
789 * startup, init, and exit
790 *-----------------------------------------------------------------------------
793 #define RC_HISTORY_NAME "history"
794 #define RC_MARKS_NAME "marks"
796 static void setup_env_path()
798 const gchar *old_path = g_getenv("PATH");
799 gchar *path = g_strconcat(gq_bindir, ":", old_path, NULL);
800 g_setenv("PATH", path, TRUE);
804 static void keys_load()
808 path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
809 history_list_load(path);
813 static void keys_save()
817 path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
818 history_list_save(path);
822 static void marks_load()
826 path = g_build_filename(get_rc_dir(), RC_MARKS_NAME, NULL);
827 marks_list_load(path);
831 static void marks_save(gboolean save)
835 path = g_build_filename(get_rc_dir(), RC_MARKS_NAME, NULL);
836 marks_list_save(path, save);
840 static void mkdir_if_not_exists(const gchar *path)
842 if (isdir(path)) return;
844 log_printf(_("Creating %s dir:%s\n"), GQ_APPNAME, path);
846 if (!recursive_mkdir_if_not_exists(path, 0755))
848 log_printf(_("Could not create dir:%s\n"), path);
853 /* We add to duplicate and modify gtk_accel_map_print() and gtk_accel_map_save()
854 * to improve the reliability in special cases (especially when disk is full)
855 * These functions are now using secure saving stuff.
857 static void gq_accel_map_print(
859 const gchar *accel_path,
861 GdkModifierType accel_mods,
864 GString *gstring = g_string_new(changed ? nullptr : "; ");
865 auto ssi = static_cast<SecureSaveInfo *>(data);
869 g_string_append(gstring, "(gtk_accel_path \"");
871 tmp = g_strescape(accel_path, nullptr);
872 g_string_append(gstring, tmp);
875 g_string_append(gstring, "\" \"");
877 name = gtk_accelerator_name(accel_key, accel_mods);
878 tmp = g_strescape(name, nullptr);
880 g_string_append(gstring, tmp);
883 g_string_append(gstring, "\")\n");
885 secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
887 g_string_free(gstring, TRUE);
890 static gboolean gq_accel_map_save(const gchar *path)
896 pathl = path_from_utf8(path);
897 ssi = secure_open(pathl);
901 log_printf(_("error saving file: %s\n"), path);
905 gstring = g_string_new("; ");
907 g_string_append(gstring, g_get_prgname());
908 g_string_append(gstring, " GtkAccelMap rc-file -*- scheme -*-\n");
909 g_string_append(gstring, "; this file is an automated accelerator map dump\n");
910 g_string_append(gstring, ";\n");
912 secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
914 g_string_free(gstring, TRUE);
916 gtk_accel_map_foreach(ssi, gq_accel_map_print);
918 if (secure_close(ssi))
920 log_printf(_("error saving file: %s\nerror: %s\n"), path,
921 secsave_strerror(secsave_errno));
928 static gchar *accep_map_filename()
930 return g_build_filename(get_rc_dir(), "accels", NULL);
933 static void accel_map_save()
937 path = accep_map_filename();
938 gq_accel_map_save(path);
942 static void accel_map_load()
947 path = accep_map_filename();
948 pathl = path_from_utf8(path);
949 gtk_accel_map_load(pathl);
954 static void gtkrc_load()
959 /* If a gtkrc file exists in the rc directory, add it to the
960 * list of files to be parsed at the end of gtk_init() */
961 path = g_build_filename(get_rc_dir(), "gtkrc", NULL);
962 pathl = path_from_utf8(path);
963 if (access(pathl, R_OK) == 0)
964 gtk_rc_add_default_file(pathl);
969 static void exit_program_final()
971 LayoutWindow *lw = nullptr;
973 LayoutWindow *tmp_lw;
977 /* make sure that external editors are loaded, we would save incomplete configuration otherwise */
978 layout_editors_reload_finish();
980 remote_close(remote_connection);
982 collect_manager_flush();
984 /* Save the named windows */
985 if (layout_window_list && layout_window_list->next)
987 list = layout_window_list;
990 tmp_lw = static_cast<LayoutWindow *>(list->data);
991 if (!g_str_has_prefix(tmp_lw->options.id, "lw"))
993 save_layout(static_cast<LayoutWindow *>(list->data));
999 save_options(options);
1003 if (layout_valid(&lw))
1008 /* Delete any files/folders in /tmp that have been created by the open archive function */
1009 archive_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, NULL);
1010 if (isdir(archive_dir))
1012 archive_file = g_file_new_for_path(archive_dir);
1013 rmdir_recursive(archive_file, nullptr, nullptr);
1014 g_free(archive_dir);
1015 g_object_unref(archive_file);
1018 /* If there are still sub-dirs created by another instance, this will fail
1019 * but that does not matter */
1020 archive_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, NULL);
1021 if (isdir(archive_dir))
1023 archive_file = g_file_new_for_path(archive_dir);
1024 g_file_delete(archive_file, nullptr, nullptr);
1025 g_free(archive_dir);
1026 g_object_unref(archive_file);
1029 secure_close(command_line->ssi);
1034 static GenericDialog *exit_dialog = nullptr;
1036 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer)
1038 exit_dialog = nullptr;
1039 generic_dialog_close(gd);
1042 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer)
1044 exit_dialog = nullptr;
1045 generic_dialog_close(gd);
1046 exit_program_final();
1049 static gint exit_confirm_dlg()
1058 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
1062 if (!collection_window_modified_exists() && (layout_window_count() == 1)) return FALSE;
1066 if (layout_valid(&lw))
1068 parent = lw->window;
1071 msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
1072 exit_dialog = generic_dialog_new(msg,
1073 "exit", parent, FALSE,
1074 exit_confirm_cancel_cb, nullptr);
1076 msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
1078 message = g_string_new(nullptr);
1080 if (collection_window_modified_exists())
1082 message = g_string_append(message, _("Collections have been modified.\n"));
1085 if (layout_window_count() > 1)
1087 g_string_append_printf(message, _("%d windows are open.\n\n"), layout_window_count());
1090 message = g_string_append(message, _("Quit anyway?"));
1092 generic_dialog_add_message(exit_dialog, GQ_ICON_DIALOG_QUESTION, msg, message->str, TRUE);
1094 generic_dialog_add_button(exit_dialog, GQ_ICON_QUIT, _("Quit"), exit_confirm_exit_cb, TRUE);
1096 gtk_widget_show(exit_dialog->dialog);
1098 g_string_free(message, TRUE);
1103 static void exit_program_write_metadata_cb(gint success, const gchar *, gpointer)
1105 if (success) exit_program();
1110 layout_image_full_screen_stop(nullptr);
1112 if (metadata_write_queue_confirm(FALSE, exit_program_write_metadata_cb, nullptr)) return;
1114 options->marks_save ? marks_save(TRUE) : marks_save(FALSE);
1116 if (exit_confirm_dlg()) return;
1118 exit_program_final();
1121 /* This code attempts to handle situation when a file mmaped by image_loader
1122 * or by exif loader is truncated by some other process.
1123 * This code is incorrect according to POSIX, because:
1125 * mmap is not async-signal-safe and thus may not be called from a signal handler
1127 * mmap must be called with a valid file descriptor. POSIX requires that
1128 * a fildes argument of -1 must cause mmap to return EBADF.
1130 * See https://github.com/BestImageViewer/geeqie/issues/1052 for discussion of
1131 * an alternative approach.
1133 /** @FIXME this probably needs some better ifdefs. Please report any compilation problems */
1134 /** @FIXME This section needs revising */
1136 #pragma GCC diagnostic push
1137 #pragma GCC diagnostic ignored "-Wunused-function"
1138 #if defined(SIGBUS) && defined(SA_SIGINFO)
1139 static void sigbus_handler_cb_unused(int, siginfo_t *info, void *)
1142 * @FIXME Design and implement a POSIX-acceptable approach,
1143 * after first documenting the sitations where SIGBUS occurs.
1144 * See https://github.com/BestImageViewer/geeqie/issues/1052 for discussion
1147 DEBUG_1("SIGBUS %p NOT HANDLED", info->si_addr);
1152 static void setup_sigbus_handler_unused()
1154 #if defined(SIGBUS) && defined(SA_SIGINFO)
1155 struct sigaction sigbus_action;
1156 sigfillset(&sigbus_action.sa_mask);
1157 sigbus_action.sa_sigaction = sigbus_handler_cb_unused;
1158 sigbus_action.sa_flags = SA_SIGINFO;
1160 sigaction(SIGBUS, &sigbus_action, nullptr);
1163 #pragma GCC diagnostic pop
1166 static void setup_sig_handler()
1168 struct sigaction sigsegv_action;
1169 sigfillset(&sigsegv_action.sa_mask);
1170 sigsegv_action.sa_sigaction = sig_handler_cb;
1171 sigsegv_action.sa_flags = SA_SIGINFO;
1173 sigaction(SIGABRT, &sigsegv_action, nullptr);
1174 sigaction(SIGBUS, &sigsegv_action, nullptr);
1175 sigaction(SIGFPE, &sigsegv_action, nullptr);
1176 sigaction(SIGILL, &sigsegv_action, nullptr);
1177 sigaction(SIGIOT, &sigsegv_action, nullptr);
1178 sigaction(SIGSEGV, &sigsegv_action, nullptr);
1182 static void set_theme_bg_color()
1185 GdkRGBA theme_color;
1186 GtkStyleContext *style_context;
1190 if (!options->image.use_custom_border_color)
1192 work = layout_window_list;
1193 lw = static_cast<LayoutWindow *>(work->data);
1195 style_context = gtk_widget_get_style_context(lw->window);
1196 gtk_style_context_get_background_color(style_context, GTK_STATE_FLAG_NORMAL, &bg_color);
1198 theme_color.red = bg_color.red ;
1199 theme_color.green = bg_color.green ;
1200 theme_color.blue = bg_color.blue ;
1204 lw = static_cast<LayoutWindow *>(work->data);
1205 image_background_set_color(lw->image, &theme_color);
1210 view_window_colors_update();
1213 static gboolean theme_change_cb(GObject *, GParamSpec *, gpointer)
1215 set_theme_bg_color();
1221 * @brief Set up the application paths
1223 * This function is required for use of AppImages. AppImages are
1224 * relocatable, and therefore cannot use fixed paths to various components.
1225 * These paths were originally #defines created during compilation.
1226 * They are now variables, all defined relative to one level above the
1227 * directory that the executable is run from.
1229 static void create_application_paths()
1235 length = wai_getExecutablePath(nullptr, 0, nullptr);
1236 path = static_cast<gchar *>(malloc(length + 1));
1237 wai_getExecutablePath(path, length, nullptr);
1238 path[length] = '\0';
1240 gq_executable_path = g_strdup(path);
1241 dirname = g_path_get_dirname(gq_executable_path);
1242 gq_prefix = g_path_get_dirname(dirname);
1244 gq_localedir = g_build_filename(gq_prefix, GQ_LOCALEDIR, NULL);
1245 gq_helpdir = g_build_filename(gq_prefix, GQ_HELPDIR, NULL);
1246 gq_htmldir = g_build_filename(gq_prefix, GQ_HTMLDIR, NULL);
1247 gq_appdir = g_build_filename(gq_prefix, GQ_APPDIR, NULL);
1248 gq_bindir = g_build_filename(gq_prefix, GQ_BINDIR, NULL);
1249 desktop_file_template = g_build_filename(gq_appdir, "org.geeqie.template.desktop", NULL);
1255 gint main(gint argc, gchar *argv[])
1257 CollectionData *cd = nullptr;
1258 CollectionData *first_collection = nullptr;
1259 gboolean disable_clutter = FALSE;
1260 gboolean single_dir = TRUE;
1263 GtkCssProvider *provider;
1264 GtkSettings *default_settings;
1267 gdk_set_allowed_backends("x11,*");
1270 gdk_threads_enter();
1272 /* seg. fault handler */
1274 backward::SignalHandling sh{};
1276 setup_sig_handler();
1279 /* init execution time counter (debug only) */
1282 create_application_paths();
1284 /* setup locale, i18n */
1285 setlocale(LC_ALL, "");
1288 bindtextdomain(PACKAGE, gq_localedir);
1289 bind_textdomain_codeset(PACKAGE, "UTF-8");
1290 textdomain(PACKAGE);
1299 /* setup random seed for random slideshow */
1300 srand(time(nullptr));
1303 /* See later comment; this handler leads to UB. */
1304 setup_sigbus_handler();
1307 /* register global notify functions */
1308 file_data_register_notify_func(cache_notify_cb, nullptr, NOTIFY_PRIORITY_HIGH);
1309 file_data_register_notify_func(thumb_notify_cb, nullptr, NOTIFY_PRIORITY_HIGH);
1310 file_data_register_notify_func(histogram_notify_cb, nullptr, NOTIFY_PRIORITY_HIGH);
1311 file_data_register_notify_func(collect_manager_notify_cb, nullptr, NOTIFY_PRIORITY_LOW);
1312 file_data_register_notify_func(metadata_notify_cb, nullptr, NOTIFY_PRIORITY_LOW);
1317 parse_command_line_for_debug_option(argc, argv);
1318 DEBUG_1("%s main: gtk_init", get_exec_time());
1320 if (parse_command_line_for_clutter_option(argc, argv))
1322 disable_clutter = TRUE;
1323 gtk_init(&argc, &argv);
1327 if (gtk_clutter_init(&argc, &argv) != CLUTTER_INIT_SUCCESS)
1329 log_printf("Can't initialize clutter-gtk.\nStart Geeqie with the option \"geeqie --disable-clutter\"");
1330 runcmd("zenity --error --title=\"Geeqie\" --text \"Can't initialize clutter-gtk.\n\nStart Geeqie with the option:\n geeqie --disable-clutter\" --width=300");
1335 gtk_init(&argc, &argv);
1338 if (gtk_major_version < GTK_MAJOR_VERSION ||
1339 (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
1341 log_printf("!!! This is a friendly warning.\n");
1342 log_printf("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
1343 log_printf("!!! compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
1344 log_printf("!!! running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
1345 log_printf("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
1348 DEBUG_1("%s main: pixbuf_inline_register_stock_icons", get_exec_time());
1349 gtk_icon_theme_add_resource_path(gtk_icon_theme_get_default(), GQ_RESOURCE_PATH_ICONS);
1350 pixbuf_inline_register_stock_icons();
1352 DEBUG_1("%s main: setting default options before commandline handling", get_exec_time());
1353 options = init_options(nullptr);
1354 setup_default_options(options);
1355 if (disable_clutter)
1357 options->disable_gpu = TRUE;
1360 /* Generate a unique identifier used by the open archive function */
1361 instance_identifier = g_strdup_printf("%x", g_random_int());
1363 DEBUG_1("%s main: mkdir_if_not_exists", get_exec_time());
1364 /* these functions don't depend on config file */
1365 mkdir_if_not_exists(get_rc_dir());
1366 mkdir_if_not_exists(get_collections_dir());
1367 mkdir_if_not_exists(get_thumbnails_cache_dir());
1368 mkdir_if_not_exists(get_metadata_cache_dir());
1369 mkdir_if_not_exists(get_window_layouts_dir());
1373 screen = gdk_screen_get_default();
1374 provider = gtk_css_provider_new();
1375 gtk_css_provider_load_from_resource(provider, GQ_RESOURCE_PATH_UI "/custom.css");
1376 gtk_style_context_add_provider_for_screen(screen, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
1378 if (parse_command_line_for_cache_maintenance_option(argc, argv))
1380 process_command_line_for_cache_maintenance_option(argc, argv);
1384 DEBUG_1("%s main: parse_command_line", get_exec_time());
1385 parse_command_line(argc, argv);
1390 /* restore session from the config file */
1393 DEBUG_1("%s main: load_options", get_exec_time());
1394 if (!load_options(options))
1396 /* load_options calls these functions after it parses global options, we have to call it here if it fails */
1397 filter_add_defaults();
1402 /** @FIXME For the background of this see:
1403 * https://github.com/BestImageViewer/geeqie/issues/397
1404 * The feature CLUTTER_FEATURE_SWAP_EVENTS indictates if the
1405 * system is liable to exhibit this problem.
1406 * The user is provided with an override in Preferences/Behavior
1408 if (!options->override_disable_gpu && !options->disable_gpu)
1410 DEBUG_1("CLUTTER_FEATURE_SWAP_EVENTS %d",clutter_feature_available(CLUTTER_FEATURE_SWAP_EVENTS));
1411 if (clutter_feature_available(CLUTTER_FEATURE_SWAP_EVENTS) != 0)
1413 options->disable_gpu = TRUE;
1418 /* handle missing config file and commandline additions*/
1419 if (!layout_window_list)
1421 /* broken or no config file or no <layout> section */
1422 layout_new_from_default();
1425 layout_editors_reload_start();
1427 /* If no --list option, open a separate collection window for each
1428 * .gqv file on the command line
1430 if (command_line->collection_list && !command_line->startup_command_line_collection)
1434 work = command_line->collection_list;
1440 path = static_cast<const gchar *>(work->data);
1443 cw = collection_window_new(path);
1444 if (!first_collection && cw) first_collection = cw->cd;
1448 if (command_line->log_file)
1451 gchar *path = g_strdup(command_line->log_file);
1453 pathl = path_from_utf8(path);
1454 command_line->ssi = secure_open(pathl);
1457 /* If there is a files list on the command line and no --list option,
1458 * check if they are all in the same folder
1460 if (command_line->cmd_list && !(command_line->startup_command_line_collection))
1463 gchar *path = nullptr;
1465 work = command_line->cmd_list;
1467 while (work && single_dir)
1471 dirname = g_path_get_dirname(static_cast<const gchar *>(work->data));
1474 path = g_strdup(dirname);
1478 if (g_strcmp0(path, dirname) != 0)
1489 /* Files from multiple folders, or --list option given
1490 * then open an unnamed collection and insert all files
1492 if ((command_line->cmd_list && !single_dir) || (command_line->startup_command_line_collection && command_line->cmd_list))
1497 cw = collection_window_new(nullptr);
1500 collection_path_changed(cd);
1502 work = command_line->cmd_list;
1507 fd = file_data_new_simple(static_cast<const gchar *>(work->data));
1508 collection_add(cd, fd, FALSE);
1509 file_data_unref(fd);
1513 work = command_line->collection_list;
1516 collection_load(cd, static_cast<gchar *>(work->data), COLLECTION_LOAD_APPEND);
1520 if (cd->list) layout_image_set_collection(nullptr, cd, static_cast<CollectInfo *>(cd->list->data));
1522 /* mem leak, we never unref this collection when !startup_command_line_collection
1523 * (the image view of the main window does not hold a ref to the collection)
1524 * this is sort of unavoidable, for if it did hold a ref, next/back
1525 * may not work as expected when closing collection windows.
1527 * collection_unref(cd);
1531 else if (first_collection)
1533 layout_image_set_collection(nullptr, first_collection,
1534 collection_get_first(first_collection));
1537 /* If the files on the command line are from one folder, select those files
1538 * unless it is a command line collection - then leave focus on collection window
1543 if (single_dir && command_line->cmd_list && !command_line->startup_command_line_collection)
1550 work = command_line->cmd_list;
1553 fd = file_data_new_simple(static_cast<gchar *>(work->data));
1554 selected = g_list_append(selected, fd);
1555 file_data_unref(fd);
1558 layout_select_list(lw, selected);
1561 buf = g_build_filename(get_rc_dir(), ".command", NULL);
1562 remote_connection = remote_server_init(buf, cd);
1567 default_settings = gtk_settings_get_default();
1568 g_signal_connect(default_settings, "notify::gtk-theme-name", G_CALLBACK(theme_change_cb), NULL);
1569 set_theme_bg_color();
1572 /* Show a fade-out notification window if the server has a newer AppImage version */
1573 if (options->appimage_notifications)
1575 if (g_getenv("APPDIR") && strstr(g_getenv("APPDIR"), "/tmp/.mount_Geeqie"))
1577 appimage_notification();
1579 else if (g_strstr_len(gq_executable_path, -1, "AppRun"))
1581 /* Probably running an extracted AppImage */
1582 appimage_notification();
1586 DEBUG_1("%s main: gtk_main", get_exec_time());
1589 gdk_threads_leave();
1592 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */