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>
69 gboolean thumb_format_changed = FALSE;
70 static RemoteConnection *remote_connection = NULL;
78 gchar *gq_executable_path;
79 gchar *desktop_file_template;
80 gchar *instance_identifier;
83 *-----------------------------------------------------------------------------
85 *-----------------------------------------------------------------------------
88 void keyboard_scroll_calc(gint *x, gint *y, GdkEventKey *event)
90 static gint delta = 0;
91 static guint32 time_old = 0;
92 static guint keyval_old = 0;
94 if (event->state & GDK_CONTROL_MASK)
96 if (*x < 0) *x = G_MININT / 2;
97 if (*x > 0) *x = G_MAXINT / 2;
98 if (*y < 0) *y = G_MININT / 2;
99 if (*y > 0) *y = G_MAXINT / 2;
104 if (options->progressive_key_scrolling)
108 time_diff = event->time - time_old;
110 /* key pressed within 125ms ? (1/8 second) */
111 if (time_diff > 125 || event->keyval != keyval_old) delta = 0;
113 time_old = event->time;
114 keyval_old = event->keyval;
123 *x = *x * delta * options->keyboard_scroll_step;
124 *y = *y * delta * options->keyboard_scroll_step;
130 *-----------------------------------------------------------------------------
131 * command line parser (private) hehe, who needs popt anyway?
132 *-----------------------------------------------------------------------------
135 static void parse_command_line_add_file(const gchar *file_path, gchar **path, gchar **file,
136 GList **list, GList **collection_list)
140 path_parsed = g_strdup(file_path);
141 parse_out_relatives(path_parsed);
143 if (file_extension_match(path_parsed, GQ_COLLECTION_EXT))
145 *collection_list = g_list_append(*collection_list, path_parsed);
149 if (!*path) *path = remove_level_from_path(path_parsed);
150 if (!*file) *file = g_strdup(path_parsed);
151 *list = g_list_prepend(*list, path_parsed);
155 static void parse_command_line_add_dir(const gchar *dir, gchar **UNUSED(path), gchar **UNUSED(file),
156 GList **UNUSED(list))
159 /* This is broken because file filter is not initialized yet.
165 path_parsed = g_strdup(dir);
166 parse_out_relatives(path_parsed);
167 dir_fd = file_data_new_dir(path_parsed);
170 if (filelist_read(dir_fd, &files, NULL))
174 files = filelist_filter(files, FALSE);
175 files = filelist_sort_path(files);
180 FileData *fd = work->data;
181 if (!*path) *path = remove_level_from_path(fd->path);
182 if (!*file) *file = g_strdup(fd->path);
183 *list = g_list_prepend(*list, fd);
192 file_data_unref(dir_fd);
194 DEBUG_1("multiple directories specified, ignoring: %s", dir);
198 static void parse_command_line_process_dir(const gchar *dir, gchar **path, gchar **file,
199 GList **list, gchar **first_dir)
202 if (!*list && !*first_dir)
204 *first_dir = g_strdup(dir);
210 parse_command_line_add_dir(*first_dir, path, file, list);
214 parse_command_line_add_dir(dir, path, file, list);
218 static void parse_command_line_process_file(const gchar *file_path, gchar **path, gchar **file,
219 GList **list, GList **collection_list, gchar **first_dir)
224 parse_command_line_add_dir(*first_dir, path, file, list);
228 parse_command_line_add_file(file_path, path, file, list, collection_list);
231 static void parse_command_line(gint argc, gchar *argv[])
234 GList *remote_list = NULL;
235 GList *remote_errors = NULL;
236 gboolean remote_do = FALSE;
237 gchar *first_dir = NULL;
241 gchar *geometry = NULL;
242 GtkWidget *dialog_warning;
243 GString *command_line_errors = g_string_new(NULL);
245 command_line = g_new0(CommandLine, 1);
247 command_line->argc = argc;
248 command_line->argv = argv;
249 command_line->regexp = NULL;
254 gchar *base_dir = get_current_dir();
258 gchar *cmd_line = path_to_utf8(argv[i]);
259 gchar *cmd_all = g_build_filename(base_dir, cmd_line, NULL);
261 if (cmd_line[0] == G_DIR_SEPARATOR && isdir(cmd_line))
263 parse_command_line_process_dir(cmd_line, &command_line->path, &command_line->file, &list, &first_dir);
265 else if (isdir(cmd_all))
267 parse_command_line_process_dir(cmd_all, &command_line->path, &command_line->file, &list, &first_dir);
269 else if (cmd_line[0] == G_DIR_SEPARATOR && isfile(cmd_line))
271 parse_command_line_process_file(cmd_line, &command_line->path, &command_line->file,
272 &list, &command_line->collection_list, &first_dir);
274 else if (isfile(cmd_all))
276 parse_command_line_process_file(cmd_all, &command_line->path, &command_line->file,
277 &list, &command_line->collection_list, &first_dir);
279 else if (download_web_file(cmd_line, FALSE, NULL))
282 else if (is_collection(cmd_line))
286 path = collection_path(cmd_line);
287 parse_command_line_process_file(path, &command_line->path, &command_line->file,
288 &list, &command_line->collection_list, &first_dir);
291 else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
293 /* do nothing but do not produce warnings */
295 else if (strncmp(cmd_line, "--disable-clutter", 17) == 0 && (cmd_line[17] == '\0'))
297 /* do nothing but do not produce warnings */
299 else if (strcmp(cmd_line, "+t") == 0 ||
300 strcmp(cmd_line, "--with-tools") == 0)
302 command_line->tools_show = TRUE;
304 remote_list = g_list_append(remote_list, "+t");
306 else if (strcmp(cmd_line, "-t") == 0 ||
307 strcmp(cmd_line, "--without-tools") == 0)
309 command_line->tools_hide = TRUE;
311 remote_list = g_list_append(remote_list, "-t");
313 else if (strcmp(cmd_line, "-f") == 0 ||
314 strcmp(cmd_line, "--fullscreen") == 0)
316 command_line->startup_full_screen = TRUE;
318 else if (strcmp(cmd_line, "-s") == 0 ||
319 strcmp(cmd_line, "--slideshow") == 0)
321 command_line->startup_in_slideshow = TRUE;
323 else if (strcmp(cmd_line, "-l") == 0 ||
324 strcmp(cmd_line, "--list") == 0)
326 command_line->startup_command_line_collection = TRUE;
328 else if (strncmp(cmd_line, "--geometry=", 11) == 0)
330 if (!command_line->geometry) command_line->geometry = g_strdup(cmd_line + 11);
332 else if (strcmp(cmd_line, "-r") == 0 ||
333 strcmp(cmd_line, "--remote") == 0)
338 remote_list = remote_build_list(remote_list, argc - i, &argv[i], &remote_errors);
341 else if ((strcmp(cmd_line, "+w") == 0) ||
342 strcmp(cmd_line, "--show-log-window") == 0)
344 command_line->log_window_show = TRUE;
346 else if (strncmp(cmd_line, "-o:", 3) == 0)
348 command_line->log_file = g_strdup(cmd_line + 3);
350 else if (strncmp(cmd_line, "--log-file:", 11) == 0)
352 command_line->log_file = g_strdup(cmd_line + 11);
354 else if (strncmp(cmd_line, "-g:", 3) == 0)
356 set_regexp(g_strdup(cmd_line+3));
358 else if (strncmp(cmd_line, "-grep:", 6) == 0)
360 set_regexp(g_strdup(cmd_line+3));
362 else if (strncmp(cmd_line, "-n", 2) == 0)
364 command_line->new_instance = TRUE;
366 else if (strncmp(cmd_line, "--new-instance", 14) == 0)
368 command_line->new_instance = TRUE;
370 else if (strcmp(cmd_line, "-rh") == 0 ||
371 strcmp(cmd_line, "--remote-help") == 0)
376 else if (strcmp(cmd_line, "--blank") == 0)
378 command_line->startup_blank = TRUE;
380 else if (strcmp(cmd_line, "-v") == 0 ||
381 strcmp(cmd_line, "--version") == 0)
383 printf_term(FALSE, "%s %s GTK%d\n", GQ_APPNAME, VERSION, gtk_major_version);
386 else if (strcmp(cmd_line, "--alternate") == 0)
388 /* enable faster experimental algorithm */
389 log_printf("Alternate similarity algorithm enabled\n");
390 image_sim_alternate_set(TRUE);
392 else if (strcmp(cmd_line, "-h") == 0 ||
393 strcmp(cmd_line, "--help") == 0)
395 printf_term(FALSE, "%s %s\n", GQ_APPNAME, VERSION);
396 printf_term(FALSE, _("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
397 print_term(FALSE, _("Valid options:\n"));
398 print_term(FALSE, _(" --blank start with blank file list\n"));
399 print_term(FALSE, _(" --cache-maintenance <path> run cache maintenance in non-GUI mode\n"));
400 print_term(FALSE, _(" --disable-clutter disable use of Clutter library (i.e. GPU accel.)\n"));
401 print_term(FALSE, _(" -f, --fullscreen start in full screen mode\n"));
402 print_term(FALSE, _(" --geometry=WxH+XOFF+YOFF set main window location\n"));
403 print_term(FALSE, _(" -h, --help show this message\n"));
404 print_term(FALSE, _(" -l, --list [files] [collections] open collection window for command line\n"));
405 print_term(FALSE, _(" -n, --new-instance open a new instance of Geeqie\n"));
406 print_term(FALSE, _(" -o:, --log-file:<file> save log data to file\n"));
407 print_term(FALSE, _(" -r, --remote send following commands to open window\n"));
408 print_term(FALSE, _(" -rh, --remote-help print remote command list\n"));
409 print_term(FALSE, _(" -s, --slideshow start in slideshow mode\n"));
410 print_term(FALSE, _(" +t, --with-tools force show of tools\n"));
411 print_term(FALSE, _(" -t, --without-tools force hide of tools\n"));
412 print_term(FALSE, _(" -v, --version print version info\n"));
413 print_term(FALSE, _(" +w, --show-log-window show log window\n"));
415 print_term(FALSE, _(" --debug[=level] turn on debug output\n"));
416 print_term(FALSE, _(" -g:, --grep:<regexp> filter debug output\n"));
420 /* these options are not officially supported!
421 * only for testing new features, no need to translate them */
422 print_term(FALSE, " --alternate use alternate similarity algorithm\n");
424 print_term(FALSE, "\n");
433 command_line_errors = g_string_append(command_line_errors, cmd_line);
434 command_line_errors = g_string_append(command_line_errors, "\n");
442 if (command_line_errors->len > 0)
444 dialog_warning = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", "Invalid parameter(s):");
445 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_warning), "%s", command_line_errors->str);
446 gtk_window_set_title(GTK_WINDOW(dialog_warning), GQ_APPNAME);
447 gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
448 gtk_dialog_run(GTK_DIALOG(dialog_warning));
449 gtk_widget_destroy(dialog_warning);
450 g_string_free(command_line_errors, TRUE);
456 parse_out_relatives(command_line->path);
457 parse_out_relatives(command_line->file);
460 list = g_list_reverse(list);
462 if (!command_line->path && first_dir)
464 command_line->path = first_dir;
467 parse_out_relatives(command_line->path);
471 if (!command_line->new_instance)
473 /* If Geeqie is already running, prevent a second instance
474 * from being started. Open a new window instead.
476 app_lock = g_build_filename(get_rc_dir(), ".command", NULL);
477 if (remote_server_exists(app_lock) && !remote_do)
480 if (command_line->geometry)
482 geometry = g_strdup_printf("--geometry=%s", command_line->geometry);
483 remote_list = g_list_prepend(remote_list, geometry);
485 remote_list = g_list_prepend(remote_list, "--new-window");
494 GList *work = remote_errors;
498 gchar *opt = work->data;
500 command_line_errors = g_string_append(command_line_errors, opt);
501 command_line_errors = g_string_append(command_line_errors, "\n");
505 dialog_warning = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", "Invalid parameter(s):");
506 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_warning), "%s", command_line_errors->str);
507 gtk_window_set_title(GTK_WINDOW(dialog_warning), GQ_APPNAME);
508 gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
509 gtk_dialog_run(GTK_DIALOG(dialog_warning));
510 gtk_widget_destroy(dialog_warning);
511 g_string_free(command_line_errors, TRUE);
516 /* prepend the current dir the remote command was made from,
517 * for use by any remote command that needs it
519 current_dir = g_get_current_dir();
520 pwd = g_strconcat("--PWD:", current_dir, NULL);
521 remote_list = g_list_prepend(remote_list, pwd);
523 remote_control(argv[0], remote_list, command_line->path, list, command_line->collection_list);
524 /* There is no return to this point
530 g_list_free(remote_list);
532 if (list && list->next)
534 command_line->cmd_list = list;
538 string_list_free(list);
539 command_line->cmd_list = NULL;
542 if (command_line->startup_blank)
544 g_free(command_line->path);
545 command_line->path = NULL;
546 g_free(command_line->file);
547 command_line->file = NULL;
548 filelist_free(command_line->cmd_list);
549 command_line->cmd_list = NULL;
550 string_list_free(command_line->collection_list);
551 command_line->collection_list = NULL;
555 static void parse_command_line_for_debug_option(gint argc, gchar *argv[])
558 const gchar *debug_option = "--debug";
559 gint len = strlen(debug_option);
565 for (i = 1; i < argc; i++)
567 const gchar *cmd_line = argv[i];
568 if (strncmp(cmd_line, debug_option, len) == 0)
570 gint cmd_line_len = strlen(cmd_line);
572 /* we now increment the debug state for verbosity */
573 if (cmd_line_len == len)
575 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
577 gint n = atoi(cmd_line + len + 1);
585 DEBUG_1("debugging output enabled (level %d)", get_debug_level());
590 static gboolean parse_command_line_for_clutter_option(gint argc, gchar *argv[])
592 const gchar *clutter_option = "--disable-clutter";
593 gint len = strlen(clutter_option);
594 gboolean ret = FALSE;
600 for (i = 1; i < argc; i++)
602 const gchar *cmd_line = argv[i];
603 if (strncmp(cmd_line, clutter_option, len) == 0)
614 static gboolean parse_command_line_for_cache_maintenance_option(gint argc, gchar *argv[])
616 const gchar *cache_maintenance_option = "--cache-maintenance";
617 gint len = strlen(cache_maintenance_option);
618 gboolean ret = FALSE;
622 const gchar *cmd_line = argv[1];
623 if (strncmp(cmd_line, cache_maintenance_option, len) == 0)
632 static void process_command_line_for_cache_maintenance_option(gint argc, gchar *argv[])
635 gchar *folder_path = NULL;
638 gchar *buf_config_file;
643 folder_path = expand_tilde(argv[2]);
645 if (isdir(folder_path))
647 rc_path = g_build_filename(get_rc_dir(), RC_FILE_NAME, NULL);
651 if (g_file_get_contents(rc_path, &buf_config_file, &size, NULL))
655 diff_count = strncmp("</global>", &buf_config_file[i], 9);
662 /* Load only the <global> section */
663 load_config_from_buf(buf_config_file, i + 9, FALSE);
665 if (options->thumbnails.enable_caching)
667 cache_maintenance(folder_path);
671 print_term(TRUE, "Caching not enabled\n");
674 g_free(buf_config_file);
678 print_term(TRUE, g_strconcat(_("Cannot load "), rc_path, "\n", NULL));
684 print_term(TRUE, g_strconcat(_("Configuration file path "), rc_path, _(" is not a file\n"), NULL));
691 print_term(TRUE, g_strconcat(argv[2], _(" is not a folder\n"), NULL));
698 print_term(TRUE, _("No path parameter given\n"));
704 *-----------------------------------------------------------------------------
705 * startup, init, and exit
706 *-----------------------------------------------------------------------------
709 #define RC_HISTORY_NAME "history"
710 #define RC_MARKS_NAME "marks"
712 static void setup_env_path(void)
714 const gchar *old_path = g_getenv("PATH");
715 gchar *path = g_strconcat(gq_bindir, ":", old_path, NULL);
716 g_setenv("PATH", path, TRUE);
720 static void keys_load(void)
724 path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
725 history_list_load(path);
729 static void keys_save(void)
733 path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
734 history_list_save(path);
738 static void marks_load(void)
742 path = g_build_filename(get_rc_dir(), RC_MARKS_NAME, NULL);
743 marks_list_load(path);
747 static void marks_save(gboolean save)
751 path = g_build_filename(get_rc_dir(), RC_MARKS_NAME, NULL);
752 marks_list_save(path, save);
756 static void mkdir_if_not_exists(const gchar *path)
758 if (isdir(path)) return;
760 log_printf(_("Creating %s dir:%s\n"), GQ_APPNAME, path);
762 if (!recursive_mkdir_if_not_exists(path, 0755))
764 log_printf(_("Could not create dir:%s\n"), path);
769 /* We add to duplicate and modify gtk_accel_map_print() and gtk_accel_map_save()
770 * to improve the reliability in special cases (especially when disk is full)
771 * These functions are now using secure saving stuff.
773 static void gq_accel_map_print(
775 const gchar *accel_path,
777 GdkModifierType accel_mods,
780 GString *gstring = g_string_new(changed ? NULL : "; ");
781 SecureSaveInfo *ssi = data;
784 g_string_append(gstring, "(gtk_accel_path \"");
786 tmp = g_strescape(accel_path, NULL);
787 g_string_append(gstring, tmp);
790 g_string_append(gstring, "\" \"");
792 name = gtk_accelerator_name(accel_key, accel_mods);
793 tmp = g_strescape(name, NULL);
795 g_string_append(gstring, tmp);
798 g_string_append(gstring, "\")\n");
800 secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
802 g_string_free(gstring, TRUE);
805 static gboolean gq_accel_map_save(const gchar *path)
811 pathl = path_from_utf8(path);
812 ssi = secure_open(pathl);
816 log_printf(_("error saving file: %s\n"), path);
820 gstring = g_string_new("; ");
822 g_string_append(gstring, g_get_prgname());
823 g_string_append(gstring, " GtkAccelMap rc-file -*- scheme -*-\n");
824 g_string_append(gstring, "; this file is an automated accelerator map dump\n");
825 g_string_append(gstring, ";\n");
827 secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
829 g_string_free(gstring, TRUE);
831 gtk_accel_map_foreach((gpointer) ssi, gq_accel_map_print);
833 if (secure_close(ssi))
835 log_printf(_("error saving file: %s\nerror: %s\n"), path,
836 secsave_strerror(secsave_errno));
843 static gchar *accep_map_filename(void)
845 return g_build_filename(get_rc_dir(), "accels", NULL);
848 static void accel_map_save(void)
852 path = accep_map_filename();
853 gq_accel_map_save(path);
857 static void accel_map_load(void)
862 path = accep_map_filename();
863 pathl = path_from_utf8(path);
864 gtk_accel_map_load(pathl);
869 static void gtkrc_load(void)
874 /* If a gtkrc file exists in the rc directory, add it to the
875 * list of files to be parsed at the end of gtk_init() */
876 path = g_build_filename(get_rc_dir(), "gtkrc", NULL);
877 pathl = path_from_utf8(path);
878 if (access(pathl, R_OK) == 0)
879 gtk_rc_add_default_file(pathl);
884 static void exit_program_final(void)
886 LayoutWindow *lw = NULL;
888 LayoutWindow *tmp_lw;
892 /* make sure that external editors are loaded, we would save incomplete configuration otherwise */
893 layout_editors_reload_finish();
895 remote_close(remote_connection);
897 collect_manager_flush();
899 /* Save the named windows */
900 if (layout_window_list && layout_window_list->next)
902 list = layout_window_list;
906 if (!g_str_has_prefix(tmp_lw->options.id, "lw"))
908 save_layout(list->data);
914 save_options(options);
918 if (layout_valid(&lw))
923 /* Delete any files/folders in /tmp that have been created by the open archive function */
924 archive_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, NULL);
925 if (isdir(archive_dir))
927 archive_file = g_file_new_for_path(archive_dir);
928 rmdir_recursive(archive_file, NULL, NULL);
930 g_object_unref(archive_file);
933 /* If there are still sub-dirs created by another instance, this will fail
934 * but that does not matter */
935 archive_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, NULL);
936 if (isdir(archive_dir))
938 archive_file = g_file_new_for_path(archive_dir);
939 g_file_delete(archive_file, NULL, NULL);
941 g_object_unref(archive_file);
944 secure_close(command_line->ssi);
949 static GenericDialog *exit_dialog = NULL;
951 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer UNUSED(data))
954 generic_dialog_close(gd);
957 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer UNUSED(data))
960 generic_dialog_close(gd);
961 exit_program_final();
964 static gint exit_confirm_dlg(void)
972 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
976 if (!collection_window_modified_exists()) return FALSE;
980 if (layout_valid(&lw))
985 msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
986 exit_dialog = generic_dialog_new(msg,
987 "exit", parent, FALSE,
988 exit_confirm_cancel_cb, NULL);
990 msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
991 generic_dialog_add_message(exit_dialog, GTK_STOCK_DIALOG_QUESTION,
992 msg, _("Collections have been modified. Quit anyway?"), TRUE);
994 generic_dialog_add_button(exit_dialog, GTK_STOCK_QUIT, NULL, exit_confirm_exit_cb, TRUE);
996 gtk_widget_show(exit_dialog->dialog);
1001 static void exit_program_write_metadata_cb(gint success, const gchar *UNUSED(dest_path), gpointer UNUSED(data))
1003 if (success) exit_program();
1006 void exit_program(void)
1008 layout_image_full_screen_stop(NULL);
1010 if (metadata_write_queue_confirm(FALSE, exit_program_write_metadata_cb, NULL)) return;
1012 options->marks_save ? marks_save(TRUE) : marks_save(FALSE);
1014 if (exit_confirm_dlg()) return;
1016 exit_program_final();
1019 /* This code is supposed to handle situation when a file mmaped by image_loader
1020 * or by exif loader is truncated by some other process.
1021 * This is probably not completely correct according to posix, because
1022 * mmap is not in the list of calls that can be used safely in signal handler,
1023 * but anyway, the handler is called in situation when the application would
1025 * Ideas for improvement are welcome ;)
1027 /** @FIXME this probably needs some better ifdefs. Please report any compilation problems */
1029 #if defined(SIGBUS) && defined(SA_SIGINFO)
1030 static void sigbus_handler_cb(int UNUSED(signum), siginfo_t *info, void *UNUSED(context))
1032 unsigned long pagesize = sysconf(_SC_PAGE_SIZE);
1033 DEBUG_1("SIGBUS %p", info->si_addr);
1034 mmap((void *)(((unsigned long)info->si_addr / pagesize) * pagesize), pagesize, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1038 static void setup_sigbus_handler(void)
1040 #if defined(SIGBUS) && defined(SA_SIGINFO)
1041 struct sigaction sigbus_action;
1042 sigfillset(&sigbus_action.sa_mask);
1043 sigbus_action.sa_sigaction = sigbus_handler_cb;
1044 sigbus_action.sa_flags = SA_SIGINFO;
1046 sigaction(SIGBUS, &sigbus_action, NULL);
1050 static void set_theme_bg_color()
1053 GdkColor theme_color;
1054 GtkStyleContext *style_context;
1058 if (!options->image.use_custom_border_color)
1060 work = layout_window_list;
1063 style_context = gtk_widget_get_style_context(lw->window);
1064 gtk_style_context_get_background_color(style_context, GTK_STATE_FLAG_NORMAL, &bg_color);
1066 theme_color.red = bg_color.red * 65535;
1067 theme_color.green = bg_color.green * 65535;
1068 theme_color.blue = bg_color.blue * 65535;
1073 image_background_set_color(lw->image, &theme_color);
1078 view_window_colors_update();
1081 static gboolean theme_change_cb(GObject *UNUSED(gobject), GParamSpec *UNUSED(pspec), gpointer UNUSED(data))
1083 set_theme_bg_color();
1089 * @brief Set up the application paths
1091 * This function is required for use of AppImages. AppImages are
1092 * relocatable, and therefore cannot use fixed paths to various components.
1093 * These paths were originally #defines created during compilation.
1094 * They are now variables, all defined relative to one level above the
1095 * directory that the executable is run from.
1097 static void create_application_paths()
1103 length = wai_getExecutablePath(NULL, 0, NULL);
1104 path = (gchar *)malloc(length + 1);
1105 wai_getExecutablePath(path, length, NULL);
1106 path[length] = '\0';
1108 gq_executable_path = g_strdup(path);
1109 dirname = g_path_get_dirname(gq_executable_path);
1110 gq_prefix = g_path_get_dirname(dirname);
1112 gq_localedir = g_build_filename(gq_prefix, GQ_LOCALEDIR, NULL);
1113 gq_helpdir = g_build_filename(gq_prefix, GQ_HELPDIR, NULL);
1114 gq_htmldir = g_build_filename(gq_prefix, GQ_HTMLDIR, NULL);
1115 gq_appdir = g_build_filename(gq_prefix, GQ_APPDIR, NULL);
1116 gq_bindir = g_build_filename(gq_prefix, GQ_BINDIR, NULL);
1117 desktop_file_template = g_build_filename(gq_appdir, "template.desktop", NULL);
1123 gboolean stderr_channel_cb(GIOChannel *source, GIOCondition UNUSED(condition), gpointer UNUSED(data))
1125 static GString *message_str = NULL;
1126 gchar buf[10] = {0};
1131 message_str = g_string_new(NULL);
1134 g_io_channel_read_chars(source, buf, 1, &count, NULL);
1140 log_printf("%s", message_str->str);
1141 g_string_free(message_str, TRUE);
1146 message_str = g_string_append_c(message_str, buf[0]);
1156 gint main(gint argc, gchar *argv[])
1158 CollectionData *first_collection = NULL;
1160 CollectionData *cd = NULL;
1161 gboolean disable_clutter = FALSE;
1162 gboolean single_dir = TRUE;
1164 GtkSettings *default_settings;
1166 GIOChannel *stderr_channel;
1168 gdk_set_allowed_backends("x11,*");
1171 gdk_threads_enter();
1173 /* init execution time counter (debug only) */
1176 create_application_paths();
1178 /* setup locale, i18n */
1179 setlocale(LC_ALL, "");
1182 bindtextdomain(PACKAGE, gq_localedir);
1183 bind_textdomain_codeset(PACKAGE, "UTF-8");
1184 textdomain(PACKAGE);
1187 /* Tee stderr to log window */
1188 if (pipe(fd_stderr) == 0)
1190 if (dup2(fd_stderr[1], fileno(stderr)) != -1)
1192 close(fd_stderr[1]);
1193 stderr_channel = g_io_channel_unix_new(fd_stderr[0]);
1194 g_io_add_watch(stderr_channel, G_IO_IN, (GIOFunc)stderr_channel_cb, NULL);
1204 /* setup random seed for random slideshow */
1207 setup_sigbus_handler();
1209 /* register global notify functions */
1210 file_data_register_notify_func(cache_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
1211 file_data_register_notify_func(thumb_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
1212 file_data_register_notify_func(histogram_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
1213 file_data_register_notify_func(collect_manager_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
1214 file_data_register_notify_func(metadata_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
1219 parse_command_line_for_debug_option(argc, argv);
1220 DEBUG_1("%s main: gtk_init", get_exec_time());
1222 if (parse_command_line_for_clutter_option(argc, argv))
1224 disable_clutter = TRUE;
1225 gtk_init(&argc, &argv);
1229 if (gtk_clutter_init(&argc, &argv) != CLUTTER_INIT_SUCCESS)
1231 log_printf("Can't initialize clutter-gtk.\nStart Geeqie with the option \"geeqie --disable-clutter\"");
1232 runcmd("zenity --error --title=\"Geeqie\" --text \"Can't initialize clutter-gtk.\n\nStart Geeqie with the option:\n geeqie --disable-clutter\" --width=300");
1237 gtk_init(&argc, &argv);
1240 if (gtk_major_version < GTK_MAJOR_VERSION ||
1241 (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
1243 log_printf("!!! This is a friendly warning.\n");
1244 log_printf("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
1245 log_printf("!!! compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
1246 log_printf("!!! running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
1247 log_printf("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
1250 DEBUG_1("%s main: pixbuf_inline_register_stock_icons", get_exec_time());
1251 pixbuf_inline_register_stock_icons();
1253 DEBUG_1("%s main: setting default options before commandline handling", get_exec_time());
1254 options = init_options(NULL);
1255 setup_default_options(options);
1256 if (disable_clutter)
1258 options->disable_gpu = TRUE;
1261 /* Generate a unique identifier used by the open archive function */
1262 instance_identifier = g_strdup_printf("%x", g_random_int());
1264 DEBUG_1("%s main: mkdir_if_not_exists", get_exec_time());
1265 /* these functions don't depend on config file */
1266 mkdir_if_not_exists(get_rc_dir());
1267 mkdir_if_not_exists(get_collections_dir());
1268 mkdir_if_not_exists(get_thumbnails_cache_dir());
1269 mkdir_if_not_exists(get_metadata_cache_dir());
1270 mkdir_if_not_exists(get_window_layouts_dir());
1274 if (parse_command_line_for_cache_maintenance_option(argc, argv))
1276 process_command_line_for_cache_maintenance_option(argc, argv);
1280 DEBUG_1("%s main: parse_command_line", get_exec_time());
1281 parse_command_line(argc, argv);
1286 /* restore session from the config file */
1289 DEBUG_1("%s main: load_options", get_exec_time());
1290 if (!load_options(options))
1292 /* load_options calls these functions after it parses global options, we have to call it here if it fails */
1293 filter_add_defaults();
1298 /** @FIXME For the background of this see:
1299 * https://github.com/BestImageViewer/geeqie/issues/397
1300 * The feature CLUTTER_FEATURE_SWAP_EVENTS indictates if the
1301 * system is liable to exhibit this problem.
1302 * The user is provided with an override in Preferences/Behavior
1304 if (!options->override_disable_gpu && !options->disable_gpu)
1306 DEBUG_1("CLUTTER_FEATURE_SWAP_EVENTS %d",clutter_feature_available(CLUTTER_FEATURE_SWAP_EVENTS));
1307 if (clutter_feature_available(CLUTTER_FEATURE_SWAP_EVENTS) != 0)
1309 options->disable_gpu = TRUE;
1314 /* handle missing config file and commandline additions*/
1315 if (!layout_window_list)
1317 /* broken or no config file or no <layout> section */
1318 layout_new_from_default();
1321 layout_editors_reload_start();
1323 /* If no --list option, open a separate collection window for each
1324 * .gqv file on the command line
1326 if (command_line->collection_list && !command_line->startup_command_line_collection)
1330 work = command_line->collection_list;
1339 cw = collection_window_new(path);
1340 if (!first_collection && cw) first_collection = cw->cd;
1344 if (command_line->log_file)
1347 gchar *path = g_strdup(command_line->log_file);
1349 pathl = path_from_utf8(path);
1350 command_line->ssi = secure_open(pathl);
1353 /* If there is a files list on the command line and no --list option,
1354 * check if they are all in the same folder
1356 if (command_line->cmd_list && !(command_line->startup_command_line_collection))
1361 work = command_line->cmd_list;
1363 while (work && single_dir)
1367 dirname = g_path_get_dirname(work->data);
1370 path = g_strdup(dirname);
1374 if (g_strcmp0(path, dirname) != 0)
1385 /* Files from multiple folders, or --list option given
1386 * then open an unnamed collection and insert all files
1388 if ((command_line->cmd_list && !single_dir) || (command_line->startup_command_line_collection && command_line->cmd_list))
1393 cw = collection_window_new(NULL);
1396 collection_path_changed(cd);
1398 work = command_line->cmd_list;
1403 fd = file_data_new_simple(work->data);
1404 collection_add(cd, fd, FALSE);
1405 file_data_unref(fd);
1409 work = command_line->collection_list;
1412 collection_load(cd, (gchar *)work->data, COLLECTION_LOAD_APPEND);
1416 if (cd->list) layout_image_set_collection(NULL, cd, cd->list->data);
1418 /* mem leak, we never unref this collection when !startup_command_line_collection
1419 * (the image view of the main window does not hold a ref to the collection)
1420 * this is sort of unavoidable, for if it did hold a ref, next/back
1421 * may not work as expected when closing collection windows.
1423 * collection_unref(cd);
1427 else if (first_collection)
1429 layout_image_set_collection(NULL, first_collection,
1430 collection_get_first(first_collection));
1433 /* If the files on the command line are from one folder, select those files
1434 * unless it is a command line collection - then leave focus on collection window
1439 if (single_dir && command_line->cmd_list && !command_line->startup_command_line_collection)
1446 work = command_line->cmd_list;
1449 fd = file_data_new_simple((gchar *)work->data);
1450 selected = g_list_append(selected, fd);
1451 file_data_unref(fd);
1454 layout_select_list(lw, selected);
1457 buf = g_build_filename(get_rc_dir(), ".command", NULL);
1458 remote_connection = remote_server_init(buf, cd);
1463 default_settings = gtk_settings_get_default();
1464 g_signal_connect(default_settings, "notify::gtk-theme-name", G_CALLBACK(theme_change_cb), NULL);
1465 set_theme_bg_color();
1468 DEBUG_1("%s main: gtk_main", get_exec_time());
1471 gdk_threads_leave();
1474 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */