2 * Copyright (C) 2006 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <gdk/gdkkeysyms.h> /* for keyboard values */
37 #include "collect-io.h"
39 #include "filefilter.h"
40 #include "history_list.h"
42 #include "image-overlay.h"
45 #include "layout_image.h"
46 #include "layout_util.h"
51 #include "secure_save.h"
53 #include "ui_fileops.h"
54 #include "ui_utildlg.h"
55 #include "cache_maint.h"
60 #include "histogram.h"
61 #include "pixbuf_util.h"
66 #include <clutter-gtk/clutter-gtk.h>
70 /** @FIXME see below */
74 gboolean thumb_format_changed = FALSE;
75 static RemoteConnection *remote_connection = NULL;
83 gchar *gq_executable_path;
84 gchar *desktop_file_template;
85 gchar *instance_identifier;
88 *-----------------------------------------------------------------------------
90 *-----------------------------------------------------------------------------
93 void keyboard_scroll_calc(gint *x, gint *y, GdkEventKey *event)
95 static gint delta = 0;
96 static guint32 time_old = 0;
97 static guint keyval_old = 0;
99 if (event->state & GDK_CONTROL_MASK)
101 if (*x < 0) *x = G_MININT / 2;
102 if (*x > 0) *x = G_MAXINT / 2;
103 if (*y < 0) *y = G_MININT / 2;
104 if (*y > 0) *y = G_MAXINT / 2;
109 if (options->progressive_key_scrolling)
113 time_diff = event->time - time_old;
115 /* key pressed within 125ms ? (1/8 second) */
116 if (time_diff > 125 || event->keyval != keyval_old) delta = 0;
118 time_old = event->time;
119 keyval_old = event->keyval;
128 *x = *x * delta * options->keyboard_scroll_step;
129 *y = *y * delta * options->keyboard_scroll_step;
135 *-----------------------------------------------------------------------------
136 * command line parser (private) hehe, who needs popt anyway?
137 *-----------------------------------------------------------------------------
140 static void parse_command_line_add_file(const gchar *file_path, gchar **path, gchar **file,
141 GList **list, GList **collection_list)
145 path_parsed = g_strdup(file_path);
146 parse_out_relatives(path_parsed);
148 if (file_extension_match(path_parsed, GQ_COLLECTION_EXT))
150 *collection_list = g_list_append(*collection_list, path_parsed);
154 if (!*path) *path = remove_level_from_path(path_parsed);
155 if (!*file) *file = g_strdup(path_parsed);
156 *list = g_list_prepend(*list, path_parsed);
160 static void parse_command_line_add_dir(const gchar *dir, gchar **path, gchar **file,
164 /* This is broken because file filter is not initialized yet.
170 path_parsed = g_strdup(dir);
171 parse_out_relatives(path_parsed);
172 dir_fd = file_data_new_dir(path_parsed);
175 if (filelist_read(dir_fd, &files, NULL))
179 files = filelist_filter(files, FALSE);
180 files = filelist_sort_path(files);
185 FileData *fd = work->data;
186 if (!*path) *path = remove_level_from_path(fd->path);
187 if (!*file) *file = g_strdup(fd->path);
188 *list = g_list_prepend(*list, fd);
197 file_data_unref(dir_fd);
199 DEBUG_1("multiple directories specified, ignoring: %s", dir);
203 static void parse_command_line_process_dir(const gchar *dir, gchar **path, gchar **file,
204 GList **list, gchar **first_dir)
207 if (!*list && !*first_dir)
209 *first_dir = g_strdup(dir);
215 parse_command_line_add_dir(*first_dir, path, file, list);
219 parse_command_line_add_dir(dir, path, file, list);
223 static void parse_command_line_process_file(const gchar *file_path, gchar **path, gchar **file,
224 GList **list, GList **collection_list, gchar **first_dir)
229 parse_command_line_add_dir(*first_dir, path, file, list);
233 parse_command_line_add_file(file_path, path, file, list, collection_list);
236 static void parse_command_line(gint argc, gchar *argv[])
239 GList *remote_list = NULL;
240 GList *remote_errors = NULL;
241 gboolean remote_do = FALSE;
242 gchar *first_dir = NULL;
246 gchar *geometry = NULL;
247 GtkWidget *dialog_warning;
248 GString *command_line_errors = g_string_new(NULL);
250 command_line = g_new0(CommandLine, 1);
252 command_line->argc = argc;
253 command_line->argv = argv;
254 command_line->regexp = NULL;
259 gchar *base_dir = get_current_dir();
263 gchar *cmd_line = path_to_utf8(argv[i]);
264 gchar *cmd_all = g_build_filename(base_dir, cmd_line, NULL);
266 if (cmd_line[0] == G_DIR_SEPARATOR && isdir(cmd_line))
268 parse_command_line_process_dir(cmd_line, &command_line->path, &command_line->file, &list, &first_dir);
270 else if (isdir(cmd_all))
272 parse_command_line_process_dir(cmd_all, &command_line->path, &command_line->file, &list, &first_dir);
274 else if (cmd_line[0] == G_DIR_SEPARATOR && isfile(cmd_line))
276 parse_command_line_process_file(cmd_line, &command_line->path, &command_line->file,
277 &list, &command_line->collection_list, &first_dir);
279 else if (isfile(cmd_all))
281 parse_command_line_process_file(cmd_all, &command_line->path, &command_line->file,
282 &list, &command_line->collection_list, &first_dir);
284 else if (download_web_file(cmd_line, FALSE, NULL))
287 else if (is_collection(cmd_line))
291 path = collection_path(cmd_line);
292 parse_command_line_process_file(path, &command_line->path, &command_line->file,
293 &list, &command_line->collection_list, &first_dir);
296 else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
298 /* do nothing but do not produce warnings */
300 else if (strncmp(cmd_line, "--disable-clutter", 17) == 0 && (cmd_line[17] == '\0'))
302 /* do nothing but do not produce warnings */
304 else if (strcmp(cmd_line, "+t") == 0 ||
305 strcmp(cmd_line, "--with-tools") == 0)
307 command_line->tools_show = TRUE;
309 remote_list = g_list_append(remote_list, "+t");
311 else if (strcmp(cmd_line, "-t") == 0 ||
312 strcmp(cmd_line, "--without-tools") == 0)
314 command_line->tools_hide = TRUE;
316 remote_list = g_list_append(remote_list, "-t");
318 else if (strcmp(cmd_line, "-f") == 0 ||
319 strcmp(cmd_line, "--fullscreen") == 0)
321 command_line->startup_full_screen = TRUE;
323 else if (strcmp(cmd_line, "-s") == 0 ||
324 strcmp(cmd_line, "--slideshow") == 0)
326 command_line->startup_in_slideshow = TRUE;
328 else if (strcmp(cmd_line, "-l") == 0 ||
329 strcmp(cmd_line, "--list") == 0)
331 command_line->startup_command_line_collection = TRUE;
333 else if (strncmp(cmd_line, "--geometry=", 11) == 0)
335 if (!command_line->geometry) command_line->geometry = g_strdup(cmd_line + 11);
337 else if (strcmp(cmd_line, "-r") == 0 ||
338 strcmp(cmd_line, "--remote") == 0)
343 remote_list = remote_build_list(remote_list, argc - i, &argv[i], &remote_errors);
346 else if ((strcmp(cmd_line, "+w") == 0) ||
347 strcmp(cmd_line, "--show-log-window") == 0)
349 command_line->log_window_show = TRUE;
351 else if (strncmp(cmd_line, "-o:", 3) == 0)
353 command_line->log_file = g_strdup(cmd_line + 3);
355 else if (strncmp(cmd_line, "--log-file:", 11) == 0)
357 command_line->log_file = g_strdup(cmd_line + 11);
359 else if (strncmp(cmd_line, "-g:", 3) == 0)
361 set_regexp(g_strdup(cmd_line+3));
363 else if (strncmp(cmd_line, "-grep:", 6) == 0)
365 set_regexp(g_strdup(cmd_line+3));
367 else if (strncmp(cmd_line, "-n", 2) == 0)
369 command_line->new_instance = TRUE;
371 else if (strncmp(cmd_line, "--new-instance", 14) == 0)
373 command_line->new_instance = TRUE;
375 else if (strcmp(cmd_line, "-rh") == 0 ||
376 strcmp(cmd_line, "--remote-help") == 0)
381 else if (strcmp(cmd_line, "--blank") == 0)
383 command_line->startup_blank = TRUE;
385 else if (strcmp(cmd_line, "-v") == 0 ||
386 strcmp(cmd_line, "--version") == 0)
388 printf_term(FALSE, "%s %s GTK%d\n", GQ_APPNAME, VERSION, gtk_major_version);
391 else if (strcmp(cmd_line, "--alternate") == 0)
393 /* enable faster experimental algorithm */
394 log_printf("Alternate similarity algorithm enabled\n");
395 image_sim_alternate_set(TRUE);
397 else if (strcmp(cmd_line, "-h") == 0 ||
398 strcmp(cmd_line, "--help") == 0)
400 printf_term(FALSE, "%s %s\n", GQ_APPNAME, VERSION);
401 printf_term(FALSE, _("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
402 print_term(FALSE, _("Valid options:\n"));
403 print_term(FALSE, _(" --blank start with blank file list\n"));
404 print_term(FALSE, _(" --cache-maintenance <path> run cache maintenance in non-GUI mode\n"));
405 print_term(FALSE, _(" --disable-clutter disable use of Clutter library (i.e. GPU accel.)\n"));
406 print_term(FALSE, _(" -f, --fullscreen start in full screen mode\n"));
407 print_term(FALSE, _(" --geometry=WxH+XOFF+YOFF set main window location\n"));
408 print_term(FALSE, _(" -h, --help show this message\n"));
409 print_term(FALSE, _(" -l, --list [files] [collections] open collection window for command line\n"));
410 print_term(FALSE, _(" -n, --new-instance open a new instance of Geeqie\n"));
411 print_term(FALSE, _(" -o:, --log-file:<file> save log data to file\n"));
412 print_term(FALSE, _(" -r, --remote send following commands to open window\n"));
413 print_term(FALSE, _(" -rh, --remote-help print remote command list\n"));
414 print_term(FALSE, _(" -s, --slideshow start in slideshow mode\n"));
415 print_term(FALSE, _(" +t, --with-tools force show of tools\n"));
416 print_term(FALSE, _(" -t, --without-tools force hide of tools\n"));
417 print_term(FALSE, _(" -v, --version print version info\n"));
418 print_term(FALSE, _(" +w, --show-log-window show log window\n"));
420 print_term(FALSE, _(" --debug[=level] turn on debug output\n"));
421 print_term(FALSE, _(" -g:, --grep:<regexp> filter debug output\n"));
425 /* these options are not officially supported!
426 * only for testing new features, no need to translate them */
427 print_term(FALSE, " --alternate use alternate similarity algorithm\n");
429 print_term(FALSE, "\n");
438 command_line_errors = g_string_append(command_line_errors, cmd_line);
439 command_line_errors = g_string_append(command_line_errors, "\n");
447 if (command_line_errors->len > 0)
449 dialog_warning = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", "Invalid parameter(s):");
450 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_warning), "%s", command_line_errors->str);
451 gtk_window_set_title(GTK_WINDOW(dialog_warning), GQ_APPNAME);
452 gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
453 gtk_dialog_run(GTK_DIALOG(dialog_warning));
454 gtk_widget_destroy(dialog_warning);
455 g_string_free(command_line_errors, TRUE);
461 parse_out_relatives(command_line->path);
462 parse_out_relatives(command_line->file);
465 list = g_list_reverse(list);
467 if (!command_line->path && first_dir)
469 command_line->path = first_dir;
472 parse_out_relatives(command_line->path);
476 if (!command_line->new_instance)
478 /* If Geeqie is already running, prevent a second instance
479 * from being started. Open a new window instead.
481 app_lock = g_build_filename(get_rc_dir(), ".command", NULL);
482 if (remote_server_exists(app_lock) && !remote_do)
485 if (command_line->geometry)
487 geometry = g_strdup_printf("--geometry=%s", command_line->geometry);
488 remote_list = g_list_prepend(remote_list, geometry);
490 remote_list = g_list_prepend(remote_list, "--new-window");
499 GList *work = remote_errors;
503 gchar *opt = work->data;
505 command_line_errors = g_string_append(command_line_errors, opt);
506 command_line_errors = g_string_append(command_line_errors, "\n");
510 dialog_warning = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", "Invalid parameter(s):");
511 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_warning), "%s", command_line_errors->str);
512 gtk_window_set_title(GTK_WINDOW(dialog_warning), GQ_APPNAME);
513 gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
514 gtk_dialog_run(GTK_DIALOG(dialog_warning));
515 gtk_widget_destroy(dialog_warning);
516 g_string_free(command_line_errors, TRUE);
521 /* prepend the current dir the remote command was made from,
522 * for use by any remote command that needs it
524 current_dir = g_get_current_dir();
525 pwd = g_strconcat("--PWD:", current_dir, NULL);
526 remote_list = g_list_prepend(remote_list, pwd);
528 remote_control(argv[0], remote_list, command_line->path, list, command_line->collection_list);
529 /* There is no return to this point
535 g_list_free(remote_list);
537 if (list && list->next)
539 command_line->cmd_list = list;
543 string_list_free(list);
544 command_line->cmd_list = NULL;
547 if (command_line->startup_blank)
549 g_free(command_line->path);
550 command_line->path = NULL;
551 g_free(command_line->file);
552 command_line->file = NULL;
553 filelist_free(command_line->cmd_list);
554 command_line->cmd_list = NULL;
555 string_list_free(command_line->collection_list);
556 command_line->collection_list = NULL;
560 static void parse_command_line_for_debug_option(gint argc, gchar *argv[])
563 const gchar *debug_option = "--debug";
564 gint len = strlen(debug_option);
570 for (i = 1; i < argc; i++)
572 const gchar *cmd_line = argv[i];
573 if (strncmp(cmd_line, debug_option, len) == 0)
575 gint cmd_line_len = strlen(cmd_line);
577 /* we now increment the debug state for verbosity */
578 if (cmd_line_len == len)
580 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
582 gint n = atoi(cmd_line + len + 1);
590 DEBUG_1("debugging output enabled (level %d)", get_debug_level());
595 static gboolean parse_command_line_for_clutter_option(gint argc, gchar *argv[])
597 const gchar *clutter_option = "--disable-clutter";
598 gint len = strlen(clutter_option);
599 gboolean ret = FALSE;
605 for (i = 1; i < argc; i++)
607 const gchar *cmd_line = argv[i];
608 if (strncmp(cmd_line, clutter_option, len) == 0)
619 static gboolean parse_command_line_for_cache_maintenance_option(gint argc, gchar *argv[])
621 const gchar *cache_maintenance_option = "--cache-maintenance";
622 gint len = strlen(cache_maintenance_option);
623 gboolean ret = FALSE;
627 const gchar *cmd_line = argv[1];
628 if (strncmp(cmd_line, cache_maintenance_option, len) == 0)
637 static void process_command_line_for_cache_maintenance_option(gint argc, gchar *argv[])
640 gchar *folder_path = NULL;
643 gchar *buf_config_file;
648 folder_path = expand_tilde(argv[2]);
650 if (isdir(folder_path))
652 rc_path = g_build_filename(get_rc_dir(), RC_FILE_NAME, NULL);
656 if (g_file_get_contents(rc_path, &buf_config_file, &size, NULL))
660 diff_count = strncmp("</global>", &buf_config_file[i], 9);
667 /* Load only the <global> section */
668 load_config_from_buf(buf_config_file, i + 9, FALSE);
670 if (options->thumbnails.enable_caching)
672 cache_maintenance(folder_path);
676 print_term(TRUE, "Caching not enabled\n");
679 g_free(buf_config_file);
683 print_term(TRUE, g_strconcat(_("Cannot load "), rc_path, "\n", NULL));
689 print_term(TRUE, g_strconcat(_("Configuration file path "), rc_path, _(" is not a file\n"), NULL));
696 print_term(TRUE, g_strconcat(argv[2], _(" is not a folder\n"), NULL));
703 print_term(TRUE, _("No path parameter given\n"));
709 *-----------------------------------------------------------------------------
710 * startup, init, and exit
711 *-----------------------------------------------------------------------------
714 #define RC_HISTORY_NAME "history"
715 #define RC_MARKS_NAME "marks"
717 static void setup_env_path(void)
719 const gchar *old_path = g_getenv("PATH");
720 gchar *path = g_strconcat(gq_bin_dir, ":", old_path, NULL);
721 g_setenv("PATH", path, TRUE);
725 static void keys_load(void)
729 path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
730 history_list_load(path);
734 static void keys_save(void)
738 path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
739 history_list_save(path);
743 static void marks_load(void)
747 path = g_build_filename(get_rc_dir(), RC_MARKS_NAME, NULL);
748 marks_list_load(path);
752 static void marks_save(gboolean save)
756 path = g_build_filename(get_rc_dir(), RC_MARKS_NAME, NULL);
757 marks_list_save(path, save);
761 static void mkdir_if_not_exists(const gchar *path)
763 if (isdir(path)) return;
765 log_printf(_("Creating %s dir:%s\n"), GQ_APPNAME, path);
767 if (!recursive_mkdir_if_not_exists(path, 0755))
769 log_printf(_("Could not create dir:%s\n"), path);
774 /* We add to duplicate and modify gtk_accel_map_print() and gtk_accel_map_save()
775 * to improve the reliability in special cases (especially when disk is full)
776 * These functions are now using secure saving stuff.
778 static void gq_accel_map_print(
780 const gchar *accel_path,
782 GdkModifierType accel_mods,
785 GString *gstring = g_string_new(changed ? NULL : "; ");
786 SecureSaveInfo *ssi = data;
789 g_string_append(gstring, "(gtk_accel_path \"");
791 tmp = g_strescape(accel_path, NULL);
792 g_string_append(gstring, tmp);
795 g_string_append(gstring, "\" \"");
797 name = gtk_accelerator_name(accel_key, accel_mods);
798 tmp = g_strescape(name, NULL);
800 g_string_append(gstring, tmp);
803 g_string_append(gstring, "\")\n");
805 secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
807 g_string_free(gstring, TRUE);
810 static gboolean gq_accel_map_save(const gchar *path)
816 pathl = path_from_utf8(path);
817 ssi = secure_open(pathl);
821 log_printf(_("error saving file: %s\n"), path);
825 gstring = g_string_new("; ");
827 g_string_append(gstring, g_get_prgname());
828 g_string_append(gstring, " GtkAccelMap rc-file -*- scheme -*-\n");
829 g_string_append(gstring, "; this file is an automated accelerator map dump\n");
830 g_string_append(gstring, ";\n");
832 secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
834 g_string_free(gstring, TRUE);
836 gtk_accel_map_foreach((gpointer) ssi, gq_accel_map_print);
838 if (secure_close(ssi))
840 log_printf(_("error saving file: %s\nerror: %s\n"), path,
841 secsave_strerror(secsave_errno));
848 static gchar *accep_map_filename(void)
850 return g_build_filename(get_rc_dir(), "accels", NULL);
853 static void accel_map_save(void)
857 path = accep_map_filename();
858 gq_accel_map_save(path);
862 static void accel_map_load(void)
867 path = accep_map_filename();
868 pathl = path_from_utf8(path);
869 gtk_accel_map_load(pathl);
874 static void gtkrc_load(void)
879 /* If a gtkrc file exists in the rc directory, add it to the
880 * list of files to be parsed at the end of gtk_init() */
881 path = g_build_filename(get_rc_dir(), "gtkrc", NULL);
882 pathl = path_from_utf8(path);
883 if (access(pathl, R_OK) == 0)
884 gtk_rc_add_default_file(pathl);
889 static void exit_program_final(void)
891 LayoutWindow *lw = NULL;
893 LayoutWindow *tmp_lw;
897 /* make sure that external editors are loaded, we would save incomplete configuration otherwise */
898 layout_editors_reload_finish();
900 remote_close(remote_connection);
902 collect_manager_flush();
904 /* Save the named windows */
905 if (layout_window_list && layout_window_list->next)
907 list = layout_window_list;
911 if (!g_str_has_prefix(tmp_lw->options.id, "lw"))
913 save_layout(list->data);
919 save_options(options);
923 if (layout_valid(&lw))
928 /* Delete any files/folders in /tmp that have been created by the open archive function */
929 archive_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, NULL);
930 if (isdir(archive_dir))
932 archive_file = g_file_new_for_path(archive_dir);
933 rmdir_recursive(archive_file, NULL, NULL);
935 g_object_unref(archive_file);
938 /* If there are still sub-dirs created by another instance, this will fail
939 * but that does not matter */
940 archive_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, NULL);
941 if (isdir(archive_dir))
943 archive_file = g_file_new_for_path(archive_dir);
944 g_file_delete(archive_file, NULL, NULL);
946 g_object_unref(archive_file);
949 secure_close(command_line->ssi);
954 static GenericDialog *exit_dialog = NULL;
956 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer data)
959 generic_dialog_close(gd);
962 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer data)
965 generic_dialog_close(gd);
966 exit_program_final();
969 static gint exit_confirm_dlg(void)
977 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
981 if (!collection_window_modified_exists()) return FALSE;
985 if (layout_valid(&lw))
990 msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
991 exit_dialog = generic_dialog_new(msg,
992 "exit", parent, FALSE,
993 exit_confirm_cancel_cb, NULL);
995 msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
996 generic_dialog_add_message(exit_dialog, GTK_STOCK_DIALOG_QUESTION,
997 msg, _("Collections have been modified. Quit anyway?"), TRUE);
999 generic_dialog_add_button(exit_dialog, GTK_STOCK_QUIT, NULL, exit_confirm_exit_cb, TRUE);
1001 gtk_widget_show(exit_dialog->dialog);
1006 static void exit_program_write_metadata_cb(gint success, const gchar *dest_path, gpointer data)
1008 if (success) exit_program();
1011 void exit_program(void)
1013 layout_image_full_screen_stop(NULL);
1015 if (metadata_write_queue_confirm(FALSE, exit_program_write_metadata_cb, NULL)) return;
1017 options->marks_save ? marks_save(TRUE) : marks_save(FALSE);
1019 if (exit_confirm_dlg()) return;
1021 exit_program_final();
1024 /* This code is supposed to handle situation when a file mmaped by image_loader
1025 * or by exif loader is truncated by some other process.
1026 * This is probably not completely correct according to posix, because
1027 * mmap is not in the list of calls that can be used safely in signal handler,
1028 * but anyway, the handler is called in situation when the application would
1030 * Ideas for improvement are welcome ;)
1032 /** @FIXME this probably needs some better ifdefs. Please report any compilation problems */
1034 #if defined(SIGBUS) && defined(SA_SIGINFO)
1035 static void sigbus_handler_cb(int signum, siginfo_t *info, void *context)
1037 unsigned long pagesize = sysconf(_SC_PAGE_SIZE);
1038 DEBUG_1("SIGBUS %p", info->si_addr);
1039 mmap((void *)(((unsigned long)info->si_addr / pagesize) * pagesize), pagesize, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1043 static void setup_sigbus_handler(void)
1045 #if defined(SIGBUS) && defined(SA_SIGINFO)
1046 struct sigaction sigbus_action;
1047 sigfillset(&sigbus_action.sa_mask);
1048 sigbus_action.sa_sigaction = sigbus_handler_cb;
1049 sigbus_action.sa_flags = SA_SIGINFO;
1051 sigaction(SIGBUS, &sigbus_action, NULL);
1055 static void set_theme_bg_color()
1057 #if GTK_CHECK_VERSION(3,0,0)
1059 GdkColor theme_color;
1060 GtkStyleContext *style_context;
1064 if (!options->image.use_custom_border_color)
1066 work = layout_window_list;
1069 style_context = gtk_widget_get_style_context(lw->window);
1070 gtk_style_context_get_background_color(style_context, GTK_STATE_FLAG_NORMAL, &bg_color);
1072 theme_color.red = bg_color.red * 65535;
1073 theme_color.green = bg_color.green * 65535;
1074 theme_color.blue = bg_color.blue * 65535;
1079 image_background_set_color(lw->image, &theme_color);
1084 view_window_colors_update();
1088 static gboolean theme_change_cb(GObject *gobject, GParamSpec *pspec, gpointer data)
1090 set_theme_bg_color();
1096 * @brief Set up the application paths
1098 * This function is required for use of AppImages. AppImages are
1099 * relocatable, and therefore cannot use fixed paths to various components.
1100 * These paths were originally #defines created during compilation.
1101 * They are now variables, all defined relative to one level above the
1102 * directory that the executable is run from.
1104 static void create_application_paths(gchar *argv[])
1111 length = wai_getExecutablePath(NULL, 0, NULL);
1112 path = (gchar *)malloc(length + 1);
1113 wai_getExecutablePath(path, length, NULL);
1114 path[length] = '\0';
1116 gq_executable_path = g_strdup(path);
1117 dirname = g_path_get_dirname(gq_executable_path); // default is /usr/bin/
1118 gq_prefix = g_path_get_dirname(dirname);
1120 gq_localedir = g_build_filename(gq_prefix, "share", "locale", NULL);
1121 tmp = g_build_filename(gq_prefix, "share", "doc", NULL);
1122 gq_helpdir = g_strconcat(tmp, G_DIR_SEPARATOR_S, "geeqie-", VERSION, NULL);
1123 gq_htmldir = g_build_filename(gq_helpdir, "html", NULL);
1124 gq_app_dir = g_build_filename(gq_prefix, "share", "geeqie", NULL);
1125 gq_bin_dir = g_build_filename(gq_prefix, "lib", "geeqie", NULL);
1126 desktop_file_template = g_build_filename(gq_app_dir, "template.desktop", NULL);
1133 gboolean stderr_channel_cb(GIOChannel *source, GIOCondition condition, gpointer data)
1135 static GString *message_str = NULL;
1136 gchar buf[10] = {0};
1141 message_str = g_string_new(NULL);
1144 g_io_channel_read_chars(source, buf, 1, &count, NULL);
1150 log_printf("%s", message_str->str);
1151 g_string_free(message_str, TRUE);
1156 message_str = g_string_append_c(message_str, buf[0]);
1166 gint main(gint argc, gchar *argv[])
1168 CollectionData *first_collection = NULL;
1170 CollectionData *cd = NULL;
1171 gboolean disable_clutter = FALSE;
1172 gboolean single_dir = TRUE;
1174 GtkSettings *default_settings;
1176 GIOChannel *stderr_channel;
1179 #if !GLIB_CHECK_VERSION(2,32,0)
1180 g_thread_init(NULL);
1183 gdk_threads_enter();
1187 /* init execution time counter (debug only) */
1190 create_application_paths(argv);
1192 /* setup locale, i18n */
1193 setlocale(LC_ALL, "");
1196 bindtextdomain(PACKAGE, gq_localedir);
1197 bind_textdomain_codeset(PACKAGE, "UTF-8");
1198 textdomain(PACKAGE);
1201 /* Tee stderr to log window */
1202 if (pipe(fd_stderr) == 0)
1204 if (dup2(fd_stderr[1], fileno(stderr)) != -1)
1206 close(fd_stderr[1]);
1207 stderr_channel = g_io_channel_unix_new(fd_stderr[0]);
1208 g_io_add_watch(stderr_channel, G_IO_IN, (GIOFunc)stderr_channel_cb, NULL);
1218 /* setup random seed for random slideshow */
1221 setup_sigbus_handler();
1223 /* register global notify functions */
1224 file_data_register_notify_func(cache_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
1225 file_data_register_notify_func(thumb_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
1226 file_data_register_notify_func(histogram_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
1227 file_data_register_notify_func(collect_manager_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
1228 file_data_register_notify_func(metadata_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
1233 parse_command_line_for_debug_option(argc, argv);
1234 DEBUG_1("%s main: gtk_init", get_exec_time());
1236 if (parse_command_line_for_clutter_option(argc, argv))
1238 disable_clutter = TRUE;
1239 gtk_init(&argc, &argv);
1243 if (gtk_clutter_init(&argc, &argv) != CLUTTER_INIT_SUCCESS)
1245 log_printf("Can't initialize clutter-gtk.\nStart Geeqie with the option \"geeqie --disable-clutter\"");
1246 runcmd("zenity --error --title=\"Geeqie\" --text \"Can't initialize clutter-gtk.\n\nStart Geeqie with the option:\n geeqie --disable-clutter\" --width=300");
1251 gtk_init(&argc, &argv);
1254 if (gtk_major_version < GTK_MAJOR_VERSION ||
1255 (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
1257 log_printf("!!! This is a friendly warning.\n");
1258 log_printf("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
1259 log_printf("!!! compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
1260 log_printf("!!! running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
1261 log_printf("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
1264 DEBUG_1("%s main: pixbuf_inline_register_stock_icons", get_exec_time());
1265 pixbuf_inline_register_stock_icons();
1267 DEBUG_1("%s main: setting default options before commandline handling", get_exec_time());
1268 options = init_options(NULL);
1269 setup_default_options(options);
1270 if (disable_clutter)
1272 options->disable_gpu = TRUE;
1275 /* Generate a unique identifier used by the open archive function */
1276 instance_identifier = g_strdup_printf("%x", g_random_int());
1278 DEBUG_1("%s main: mkdir_if_not_exists", get_exec_time());
1279 /* these functions don't depend on config file */
1280 mkdir_if_not_exists(get_rc_dir());
1281 mkdir_if_not_exists(get_collections_dir());
1282 mkdir_if_not_exists(get_thumbnails_cache_dir());
1283 mkdir_if_not_exists(get_metadata_cache_dir());
1284 mkdir_if_not_exists(get_window_layouts_dir());
1288 if (parse_command_line_for_cache_maintenance_option(argc, argv))
1290 process_command_line_for_cache_maintenance_option(argc, argv);
1294 DEBUG_1("%s main: parse_command_line", get_exec_time());
1295 parse_command_line(argc, argv);
1300 /* restore session from the config file */
1303 DEBUG_1("%s main: load_options", get_exec_time());
1304 if (!load_options(options))
1306 /* load_options calls these functions after it parses global options, we have to call it here if it fails */
1307 filter_add_defaults();
1312 /** @FIXME For the background of this see:
1313 * https://github.com/BestImageViewer/geeqie/issues/397
1314 * The feature CLUTTER_FEATURE_SWAP_EVENTS indictates if the
1315 * system is liable to exhibit this problem.
1316 * The user is provided with an override in Preferences/Behavior
1318 if (!options->override_disable_gpu && !options->disable_gpu)
1320 DEBUG_1("CLUTTER_FEATURE_SWAP_EVENTS %d",clutter_feature_available(CLUTTER_FEATURE_SWAP_EVENTS));
1321 if (clutter_feature_available(CLUTTER_FEATURE_SWAP_EVENTS) != 0)
1323 options->disable_gpu = TRUE;
1328 /* handle missing config file and commandline additions*/
1329 if (!layout_window_list)
1331 /* broken or no config file or no <layout> section */
1332 layout_new_from_default();
1335 layout_editors_reload_start();
1337 /* If no --list option, open a separate collection window for each
1338 * .gqv file on the command line
1340 if (command_line->collection_list && !command_line->startup_command_line_collection)
1344 work = command_line->collection_list;
1353 cw = collection_window_new(path);
1354 if (!first_collection && cw) first_collection = cw->cd;
1358 if (command_line->log_file)
1361 gchar *path = g_strdup(command_line->log_file);
1363 pathl = path_from_utf8(path);
1364 command_line->ssi = secure_open(pathl);
1367 /* If there is a files list on the command line and no --list option,
1368 * check if they are all in the same folder
1370 if (command_line->cmd_list && !(command_line->startup_command_line_collection))
1375 work = command_line->cmd_list;
1377 while (work && single_dir)
1381 dirname = g_path_get_dirname(work->data);
1384 path = g_strdup(dirname);
1388 if (g_strcmp0(path, dirname) != 0)
1399 /* Files from multiple folders, or --list option given
1400 * then open an unnamed collection and insert all files
1402 if ((command_line->cmd_list && !single_dir) || (command_line->startup_command_line_collection && command_line->cmd_list))
1407 cw = collection_window_new(NULL);
1410 collection_path_changed(cd);
1412 work = command_line->cmd_list;
1417 fd = file_data_new_simple(work->data);
1418 collection_add(cd, fd, FALSE);
1419 file_data_unref(fd);
1423 work = command_line->collection_list;
1426 collection_load(cd, (gchar *)work->data, COLLECTION_LOAD_APPEND);
1430 if (cd->list) layout_image_set_collection(NULL, cd, cd->list->data);
1432 /* mem leak, we never unref this collection when !startup_command_line_collection
1433 * (the image view of the main window does not hold a ref to the collection)
1434 * this is sort of unavoidable, for if it did hold a ref, next/back
1435 * may not work as expected when closing collection windows.
1437 * collection_unref(cd);
1441 else if (first_collection)
1443 layout_image_set_collection(NULL, first_collection,
1444 collection_get_first(first_collection));
1447 /* If the files on the command line are from one folder, select those files
1448 * unless it is a command line collection - then leave focus on collection window
1453 if (single_dir && command_line->cmd_list && !command_line->startup_command_line_collection)
1460 work = command_line->cmd_list;
1463 fd = file_data_new_simple((gchar *)work->data);
1464 selected = g_list_append(selected, fd);
1465 file_data_unref(fd);
1468 layout_select_list(lw, selected);
1471 buf = g_build_filename(get_rc_dir(), ".command", NULL);
1472 remote_connection = remote_server_init(buf, cd);
1477 default_settings = gtk_settings_get_default();
1478 g_signal_connect(default_settings, "notify::gtk-theme-name", G_CALLBACK(theme_change_cb), NULL);
1479 set_theme_bg_color();
1482 DEBUG_1("%s main: gtk_main", get_exec_time());
1485 gdk_threads_leave();
1489 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */