Additional debug features
[geeqie.git] / src / main.c
1 /*
2  * Copyright (C) 2006 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include <gdk/gdkkeysyms.h> /* for keyboard values */
23
24 #include <signal.h>
25 #include <sys/mman.h>
26
27 #include <math.h>
28 #ifdef G_OS_UNIX
29 #include <pwd.h>
30 #endif
31 #include <locale.h>
32
33 #include "main.h"
34
35 #include "cache.h"
36 #include "collect.h"
37 #include "collect-io.h"
38 #include "filedata.h"
39 #include "filefilter.h"
40 #include "history_list.h"
41 #include "image-overlay.h"
42 #include "layout.h"
43 #include "layout_image.h"
44 #include "layout_util.h"
45 #include "options.h"
46 #include "remote.h"
47 #include "secure_save.h"
48 #include "similar.h"
49 #include "ui_fileops.h"
50 #include "ui_utildlg.h"
51 #include "cache_maint.h"
52 #include "thumb.h"
53 #include "metadata.h"
54 #include "editors.h"
55 #include "exif.h"
56 #include "histogram.h"
57 #include "pixbuf_util.h"
58 #include "glua.h"
59
60 #ifdef HAVE_CLUTTER
61 #include <clutter-gtk/clutter-gtk.h>
62 #endif
63
64
65 gboolean thumb_format_changed = FALSE;
66 static RemoteConnection *remote_connection = NULL;
67
68 /*
69  *-----------------------------------------------------------------------------
70  * keyboard functions
71  *-----------------------------------------------------------------------------
72  */
73
74 void keyboard_scroll_calc(gint *x, gint *y, GdkEventKey *event)
75 {
76         static gint delta = 0;
77         static guint32 time_old = 0;
78         static guint keyval_old = 0;
79
80         if (event->state & GDK_CONTROL_MASK)
81                 {
82                 if (*x < 0) *x = G_MININT / 2;
83                 if (*x > 0) *x = G_MAXINT / 2;
84                 if (*y < 0) *y = G_MININT / 2;
85                 if (*y > 0) *y = G_MAXINT / 2;
86
87                 return;
88                 }
89
90         if (options->progressive_key_scrolling)
91                 {
92                 guint32 time_diff;
93
94                 time_diff = event->time - time_old;
95
96                 /* key pressed within 125ms ? (1/8 second) */
97                 if (time_diff > 125 || event->keyval != keyval_old) delta = 0;
98
99                 time_old = event->time;
100                 keyval_old = event->keyval;
101
102                 delta += 2;
103                 }
104         else
105                 {
106                 delta = 8;
107                 }
108
109         *x = *x * delta * options->keyboard_scroll_step;
110         *y = *y * delta * options->keyboard_scroll_step;
111 }
112
113
114
115 /*
116  *-----------------------------------------------------------------------------
117  * command line parser (private) hehe, who needs popt anyway?
118  *-----------------------------------------------------------------------------
119  */
120
121 static void parse_command_line_add_file(const gchar *file_path, gchar **path, gchar **file,
122                                         GList **list, GList **collection_list)
123 {
124         gchar *path_parsed;
125
126         path_parsed = g_strdup(file_path);
127         parse_out_relatives(path_parsed);
128
129         if (file_extension_match(path_parsed, GQ_COLLECTION_EXT))
130                 {
131                 *collection_list = g_list_append(*collection_list, path_parsed);
132                 }
133         else
134                 {
135                 if (!*path) *path = remove_level_from_path(path_parsed);
136                 if (!*file) *file = g_strdup(path_parsed);
137                 *list = g_list_prepend(*list, file_data_new_group(path_parsed));
138                 }
139 }
140
141 static void parse_command_line_add_dir(const gchar *dir, gchar **path, gchar **file,
142                                        GList **list)
143 {
144 #if 0
145         /* This is broken because file filter is not initialized yet.
146         */
147         GList *files;
148         gchar *path_parsed;
149         FileData *dir_fd;
150
151         path_parsed = g_strdup(dir);
152         parse_out_relatives(path_parsed);
153         dir_fd = file_data_new_dir(path_parsed);
154
155
156         if (filelist_read(dir_fd, &files, NULL))
157                 {
158                 GList *work;
159
160                 files = filelist_filter(files, FALSE);
161                 files = filelist_sort_path(files);
162
163                 work = files;
164                 while (work)
165                         {
166                         FileData *fd = work->data;
167                         if (!*path) *path = remove_level_from_path(fd->path);
168                         if (!*file) *file = g_strdup(fd->path);
169                         *list = g_list_prepend(*list, fd);
170
171                         work = work->next;
172                         }
173
174                 g_list_free(files);
175                 }
176
177         g_free(path_parsed);
178         file_data_unref(dir_fd);
179 #else
180         DEBUG_1("multiple directories specified, ignoring: %s", dir);
181 #endif
182 }
183
184 static void parse_command_line_process_dir(const gchar *dir, gchar **path, gchar **file,
185                                            GList **list, gchar **first_dir)
186 {
187
188         if (!*list && !*first_dir)
189                 {
190                 *first_dir = g_strdup(dir);
191                 }
192         else
193                 {
194                 if (*first_dir)
195                         {
196                         parse_command_line_add_dir(*first_dir, path, file, list);
197                         g_free(*first_dir);
198                         *first_dir = NULL;
199                         }
200                 parse_command_line_add_dir(dir, path, file, list);
201                 }
202 }
203
204 static void parse_command_line_process_file(const gchar *file_path, gchar **path, gchar **file,
205                                             GList **list, GList **collection_list, gchar **first_dir)
206 {
207
208         if (*first_dir)
209                 {
210                 parse_command_line_add_dir(*first_dir, path, file, list);
211                 g_free(*first_dir);
212                 *first_dir = NULL;
213                 }
214         parse_command_line_add_file(file_path, path, file, list, collection_list);
215 }
216
217 static void parse_command_line(gint argc, gchar *argv[])
218 {
219         GList *list = NULL;
220         GList *remote_list = NULL;
221         GList *remote_errors = NULL;
222         gboolean remote_do = FALSE;
223         gchar *first_dir = NULL;
224
225         command_line = g_new0(CommandLine, 1);
226
227         command_line->argc = argc;
228         command_line->argv = argv;
229         command_line->regexp = NULL;
230
231         if (argc > 1)
232                 {
233                 gint i;
234                 gchar *base_dir = get_current_dir();
235                 i = 1;
236                 while (i < argc)
237                         {
238                         gchar *cmd_line = path_to_utf8(argv[i]);
239                         gchar *cmd_all = g_build_filename(base_dir, cmd_line, NULL);
240
241                         if (cmd_line[0] == G_DIR_SEPARATOR && isdir(cmd_line))
242                                 {
243                                 parse_command_line_process_dir(cmd_line, &command_line->path, &command_line->file, &list, &first_dir);
244                                 }
245                         else if (isdir(cmd_all))
246                                 {
247                                 parse_command_line_process_dir(cmd_all, &command_line->path, &command_line->file, &list, &first_dir);
248                                 }
249                         else if (cmd_line[0] == G_DIR_SEPARATOR && isfile(cmd_line))
250                                 {
251                                 parse_command_line_process_file(cmd_line, &command_line->path, &command_line->file,
252                                                                 &list, &command_line->collection_list, &first_dir);
253                                 }
254                         else if (isfile(cmd_all))
255                                 {
256                                 parse_command_line_process_file(cmd_all, &command_line->path, &command_line->file,
257                                                                 &list, &command_line->collection_list, &first_dir);
258                                 }
259                         else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
260                                 {
261                                 /* do nothing but do not produce warnings */
262                                 }
263                         else if (strcmp(cmd_line, "+t") == 0 ||
264                                  strcmp(cmd_line, "--with-tools") == 0)
265                                 {
266                                 command_line->tools_show = TRUE;
267
268                                 remote_list = g_list_append(remote_list, "+t");
269                                 }
270                         else if (strcmp(cmd_line, "-t") == 0 ||
271                                  strcmp(cmd_line, "--without-tools") == 0)
272                                 {
273                                 command_line->tools_hide = TRUE;
274
275                                 remote_list = g_list_append(remote_list, "-t");
276                                 }
277                         else if (strcmp(cmd_line, "-f") == 0 ||
278                                  strcmp(cmd_line, "--fullscreen") == 0)
279                                 {
280                                 command_line->startup_full_screen = TRUE;
281                                 }
282                         else if (strcmp(cmd_line, "-s") == 0 ||
283                                  strcmp(cmd_line, "--slideshow") == 0)
284                                 {
285                                 command_line->startup_in_slideshow = TRUE;
286                                 }
287                         else if (strcmp(cmd_line, "-l") == 0 ||
288                                  strcmp(cmd_line, "--list") == 0)
289                                 {
290                                 command_line->startup_command_line_collection = TRUE;
291                                 }
292                         else if (strncmp(cmd_line, "--geometry=", 11) == 0)
293                                 {
294                                 if (!command_line->geometry) command_line->geometry = g_strdup(cmd_line + 11);
295                                 }
296                         else if (strcmp(cmd_line, "-r") == 0 ||
297                                  strcmp(cmd_line, "--remote") == 0)
298                                 {
299                                 if (!remote_do)
300                                         {
301                                         remote_do = TRUE;
302                                         remote_list = remote_build_list(remote_list, argc - i, &argv[i], &remote_errors);
303                                         }
304                                 }
305                         else if ((strcmp(cmd_line, "+w") == 0) ||
306                                                 strcmp(cmd_line, "--show-log-window") == 0)
307                                 {
308                                 command_line->log_window_show = TRUE;
309                                 }
310                         else if (strncmp(cmd_line, "-o:", 3) == 0)
311                                 {
312                                 command_line->log_file = g_strdup(cmd_line + 3);
313                                 }
314                         else if (strncmp(cmd_line, "--log-file:", 11) == 0)
315                                 {
316                                 command_line->log_file = g_strdup(cmd_line + 11);
317                                 }
318                         else if (strncmp(cmd_line, "-g:", 3) == 0)
319                                 {
320                                 set_regexp(g_strdup(cmd_line+3));
321                                 }
322                         else if (strncmp(cmd_line, "-grep:", 6) == 0)
323                                 {
324                                 set_regexp(g_strdup(cmd_line+3));
325                                 }
326                         else if (strcmp(cmd_line, "-rh") == 0 ||
327                                  strcmp(cmd_line, "--remote-help") == 0)
328                                 {
329                                 remote_help();
330                                 exit(0);
331                                 }
332                         else if (strcmp(cmd_line, "--blank") == 0)
333                                 {
334                                 command_line->startup_blank = TRUE;
335                                 }
336                         else if (strcmp(cmd_line, "-v") == 0 ||
337                                  strcmp(cmd_line, "--version") == 0)
338                                 {
339                                 printf_term("%s %s\n", GQ_APPNAME, VERSION);
340                                 exit(0);
341                                 }
342                         else if (strcmp(cmd_line, "--alternate") == 0)
343                                 {
344                                 /* enable faster experimental algorithm */
345                                 log_printf("Alternate similarity algorithm enabled\n");
346                                 image_sim_alternate_set(TRUE);
347                                 }
348                         else if (strcmp(cmd_line, "-h") == 0 ||
349                                  strcmp(cmd_line, "--help") == 0)
350                                 {
351                                 printf_term("%s %s\n", GQ_APPNAME, VERSION);
352                                 printf_term(_("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
353                                 print_term(_("valid options are:\n"));
354                                 print_term(_("  +t, --with-tools                 force show of tools\n"));
355                                 print_term(_("  -t, --without-tools              force hide of tools\n"));
356                                 print_term(_("  -f, --fullscreen                 start in full screen mode\n"));
357                                 print_term(_("  -s, --slideshow                  start in slideshow mode\n"));
358                                 print_term(_("  -l, --list [files] [collections] open collection window for command line\n"));
359                                 print_term(_("      --blank                      start with blank file list\n"));
360                                 print_term(_("      --geometry=XxY+XOFF+YOFF     set main window location\n"));
361                                 print_term(_("  -r, --remote                     send following commands to open window\n"));
362                                 print_term(_("  -rh,--remote-help                print remote command list\n"));
363 #ifdef DEBUG
364                                 print_term(_("      --debug[=level]              turn on debug output\n"));
365                                 print_term(_("  -g:<regexp>, --grep:<regexp>     filter debug output\n"));
366 #endif
367                                 print_term(_("  +w, --show-log-window            show log window\n"));
368                                 print_term(_("  -o:<file>, --log-file:<file>     save log data to file\n"));
369                                 print_term(_("  -v, --version                    print version info\n"));
370                                 print_term(_("  -h, --help                       show this message\n\n"));
371
372 #if 0
373                                 /* these options are not officially supported!
374                                  * only for testing new features, no need to translate them */
375                                 print_term(  "  --alternate                use alternate similarity algorithm\n");
376 #endif
377
378
379                                 exit(0);
380                                 }
381                         else if (!remote_do)
382                                 {
383                                 printf_term(_("invalid or ignored: %s\nUse --help for options\n"), cmd_line);
384                                 }
385
386                         g_free(cmd_all);
387                         g_free(cmd_line);
388                         i++;
389                         }
390                 g_free(base_dir);
391                 parse_out_relatives(command_line->path);
392                 parse_out_relatives(command_line->file);
393                 }
394
395         list = g_list_reverse(list);
396
397         if (!command_line->path && first_dir)
398                 {
399                 command_line->path = first_dir;
400                 first_dir = NULL;
401
402                 parse_out_relatives(command_line->path);
403                 }
404         g_free(first_dir);
405
406         if (remote_do)
407                 {
408                 if (remote_errors)
409                         {
410                         GList *work = remote_errors;
411
412                         printf_term(_("Invalid or ignored remote options: "));
413                         while (work)
414                                 {
415                                 gchar *opt = work->data;
416
417                                 printf_term("%s%s", (work == remote_errors) ? "" : ", ", opt);
418                                 work = work->next;
419                                 }
420
421                         printf_term(_("\nUse --remote-help for valid remote options.\n"));
422                         }
423
424                 remote_control(argv[0], remote_list, command_line->path, list, command_line->collection_list);
425                 }
426         g_list_free(remote_list);
427
428         if (list && list->next)
429                 {
430                 command_line->cmd_list = list;
431                 }
432         else
433                 {
434                 filelist_free(list);
435                 command_line->cmd_list = NULL;
436                 }
437
438         if (command_line->startup_blank)
439                 {
440                 g_free(command_line->path);
441                 command_line->path = NULL;
442                 g_free(command_line->file);
443                 command_line->file = NULL;
444                 filelist_free(command_line->cmd_list);
445                 command_line->cmd_list = NULL;
446                 string_list_free(command_line->collection_list);
447                 command_line->collection_list = NULL;
448                 }
449 }
450
451 static void parse_command_line_for_debug_option(gint argc, gchar *argv[])
452 {
453 #ifdef DEBUG
454         const gchar *debug_option = "--debug";
455         gint len = strlen(debug_option);
456
457         if (argc > 1)
458                 {
459                 gint i;
460
461                 for (i = 1; i < argc; i++)
462                         {
463                         const gchar *cmd_line = argv[i];
464                         if (strncmp(cmd_line, debug_option, len) == 0)
465                                 {
466                                 gint cmd_line_len = strlen(cmd_line);
467
468                                 /* we now increment the debug state for verbosity */
469                                 if (cmd_line_len == len)
470                                         debug_level_add(1);
471                                 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
472                                         {
473                                         gint n = atoi(cmd_line + len + 1);
474                                         if (n < 0) n = 1;
475                                         debug_level_add(n);
476                                         }
477                                 }
478                         }
479                 }
480
481         DEBUG_1("debugging output enabled (level %d)", get_debug_level());
482 #endif
483 }
484
485 /*
486  *-----------------------------------------------------------------------------
487  * startup, init, and exit
488  *-----------------------------------------------------------------------------
489  */
490
491 #define RC_HISTORY_NAME "history"
492
493 static void setup_env_path(void)
494 {
495         const gchar *old_path = g_getenv("PATH");
496         gchar *path = g_strconcat(GQ_BIN_DIR, ":", old_path, NULL);
497         g_setenv("PATH", path, TRUE);
498         g_free(path);
499 }
500
501 static void keys_load(void)
502 {
503         gchar *path;
504
505         path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
506         history_list_load(path);
507         g_free(path);
508 }
509
510 static void keys_save(void)
511 {
512         gchar *path;
513
514         path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
515         history_list_save(path);
516         g_free(path);
517 }
518
519 static void mkdir_if_not_exists(const gchar *path)
520 {
521         if (isdir(path)) return;
522
523         log_printf(_("Creating %s dir:%s\n"), GQ_APPNAME, path);
524
525         if (!recursive_mkdir_if_not_exists(path, 0755))
526                 {
527                 log_printf(_("Could not create dir:%s\n"), path);
528                 }
529 }
530
531
532 /* We add to duplicate and modify  gtk_accel_map_print() and gtk_accel_map_save()
533  * to improve the reliability in special cases (especially when disk is full)
534  * These functions are now using secure saving stuff.
535  */
536 static void gq_accel_map_print(
537                     gpointer    data,
538                     const gchar *accel_path,
539                     guint       accel_key,
540                     GdkModifierType accel_mods,
541                     gboolean    changed)
542 {
543         GString *gstring = g_string_new(changed ? NULL : "; ");
544         SecureSaveInfo *ssi = data;
545         gchar *tmp, *name;
546
547         g_string_append(gstring, "(gtk_accel_path \"");
548
549         tmp = g_strescape(accel_path, NULL);
550         g_string_append(gstring, tmp);
551         g_free(tmp);
552
553         g_string_append(gstring, "\" \"");
554
555         name = gtk_accelerator_name(accel_key, accel_mods);
556         tmp = g_strescape(name, NULL);
557         g_free(name);
558         g_string_append(gstring, tmp);
559         g_free(tmp);
560
561         g_string_append(gstring, "\")\n");
562
563         secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
564
565         g_string_free(gstring, TRUE);
566 }
567
568 static gboolean gq_accel_map_save(const gchar *path)
569 {
570         gchar *pathl;
571         SecureSaveInfo *ssi;
572         GString *gstring;
573
574         pathl = path_from_utf8(path);
575         ssi = secure_open(pathl);
576         g_free(pathl);
577         if (!ssi)
578                 {
579                 log_printf(_("error saving file: %s\n"), path);
580                 return FALSE;
581                 }
582
583         gstring = g_string_new("; ");
584         if (g_get_prgname())
585                 g_string_append(gstring, g_get_prgname());
586         g_string_append(gstring, " GtkAccelMap rc-file         -*- scheme -*-\n");
587         g_string_append(gstring, "; this file is an automated accelerator map dump\n");
588         g_string_append(gstring, ";\n");
589
590         secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
591
592         g_string_free(gstring, TRUE);
593
594         gtk_accel_map_foreach((gpointer) ssi, gq_accel_map_print);
595
596         if (secure_close(ssi))
597                 {
598                 log_printf(_("error saving file: %s\nerror: %s\n"), path,
599                            secsave_strerror(secsave_errno));
600                 return FALSE;
601                 }
602
603         return TRUE;
604 }
605
606 static gchar *accep_map_filename(void)
607 {
608         return g_build_filename(get_rc_dir(), "accels", NULL);
609 }
610
611 static void accel_map_save(void)
612 {
613         gchar *path;
614
615         path = accep_map_filename();
616         gq_accel_map_save(path);
617         g_free(path);
618 }
619
620 static void accel_map_load(void)
621 {
622         gchar *path;
623         gchar *pathl;
624
625         path = accep_map_filename();
626         pathl = path_from_utf8(path);
627         gtk_accel_map_load(pathl);
628         g_free(pathl);
629         g_free(path);
630 }
631
632 static void gtkrc_load(void)
633 {
634         gchar *path;
635         gchar *pathl;
636
637         /* If a gtkrc file exists in the rc directory, add it to the
638          * list of files to be parsed at the end of gtk_init() */
639         path = g_build_filename(get_rc_dir(), "gtkrc", NULL);
640         pathl = path_from_utf8(path);
641         if (access(pathl, R_OK) == 0)
642                 gtk_rc_add_default_file(pathl);
643         g_free(pathl);
644         g_free(path);
645 }
646
647 static void exit_program_final(void)
648 {
649         LayoutWindow *lw = NULL;
650
651          /* make sure that external editors are loaded, we would save incomplete configuration otherwise */
652         layout_editors_reload_finish();
653
654         remote_close(remote_connection);
655
656         collect_manager_flush();
657
658         save_options(options);
659         keys_save();
660         accel_map_save();
661
662         if (layout_valid(&lw))
663                 {
664                 layout_free(lw);
665                 }
666
667         secure_close(command_line->ssi);
668
669         gtk_main_quit();
670 }
671
672 static GenericDialog *exit_dialog = NULL;
673
674 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer data)
675 {
676         exit_dialog = NULL;
677         generic_dialog_close(gd);
678 }
679
680 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer data)
681 {
682         exit_dialog = NULL;
683         generic_dialog_close(gd);
684         exit_program_final();
685 }
686
687 static gint exit_confirm_dlg(void)
688 {
689         GtkWidget *parent;
690         LayoutWindow *lw;
691         gchar *msg;
692
693         if (exit_dialog)
694                 {
695                 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
696                 return TRUE;
697                 }
698
699         if (!collection_window_modified_exists()) return FALSE;
700
701         parent = NULL;
702         lw = NULL;
703         if (layout_valid(&lw))
704                 {
705                 parent = lw->window;
706                 }
707
708         msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
709         exit_dialog = generic_dialog_new(msg,
710                                 "exit", parent, FALSE,
711                                 exit_confirm_cancel_cb, NULL);
712         g_free(msg);
713         msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
714         generic_dialog_add_message(exit_dialog, GTK_STOCK_DIALOG_QUESTION,
715                                    msg, _("Collections have been modified. Quit anyway?"));
716         g_free(msg);
717         generic_dialog_add_button(exit_dialog, GTK_STOCK_QUIT, NULL, exit_confirm_exit_cb, TRUE);
718
719         gtk_widget_show(exit_dialog->dialog);
720
721         return TRUE;
722 }
723
724 static void exit_program_write_metadata_cb(gint success, const gchar *dest_path, gpointer data)
725 {
726         if (success) exit_program();
727 }
728
729 void exit_program(void)
730 {
731         layout_image_full_screen_stop(NULL);
732
733         if (metadata_write_queue_confirm(FALSE, exit_program_write_metadata_cb, NULL)) return;
734
735         if (exit_confirm_dlg()) return;
736
737         exit_program_final();
738 }
739
740 /* This code is supposed to handle situation when a file mmaped by image_loader
741  * or by exif loader is truncated by some other process.
742  * This is probably not completely correct according to posix, because
743  * mmap is not in the list of calls that can be used safely in signal handler,
744  * but anyway, the handler is called in situation when the application would
745  * crash otherwise.
746  * Ideas for improvement are welcome ;)
747  */
748 /* FIXME: this probably needs some better ifdefs. Please report any compilation problems */
749
750 #if defined(SIGBUS) && defined(SA_SIGINFO)
751 static void sigbus_handler_cb(int signum, siginfo_t *info, void *context)
752 {
753         unsigned long pagesize = sysconf(_SC_PAGE_SIZE);
754         DEBUG_1("SIGBUS %p", info->si_addr);
755         mmap((void *)(((unsigned long)info->si_addr / pagesize) * pagesize), pagesize, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
756 }
757 #endif
758
759 static void setup_sigbus_handler(void)
760 {
761 #if defined(SIGBUS) && defined(SA_SIGINFO)
762         struct sigaction sigbus_action;
763         sigfillset(&sigbus_action.sa_mask);
764         sigbus_action.sa_sigaction = sigbus_handler_cb;
765         sigbus_action.sa_flags = SA_SIGINFO;
766
767         sigaction(SIGBUS, &sigbus_action, NULL);
768 #endif
769 }
770
771 gint main(gint argc, gchar *argv[])
772 {
773         CollectionData *first_collection = NULL;
774         gchar *buf;
775         CollectionData *cd = NULL;
776
777 #ifdef HAVE_GTHREAD
778 #if !GLIB_CHECK_VERSION(2,32,0)
779         g_thread_init(NULL);
780 #endif
781         gdk_threads_init();
782         gdk_threads_enter();
783
784 #endif
785
786         /* init execution time counter (debug only) */
787         init_exec_time();
788
789         /* setup locale, i18n */
790         setlocale(LC_ALL, "");
791
792 #ifdef ENABLE_NLS
793         bindtextdomain(PACKAGE, GQ_LOCALEDIR);
794         bind_textdomain_codeset(PACKAGE, "UTF-8");
795         textdomain(PACKAGE);
796 #endif
797
798         exif_init();
799
800 #ifdef HAVE_LUA
801         lua_init();
802 #endif
803
804         /* setup random seed for random slideshow */
805         srand(time(NULL));
806
807         setup_sigbus_handler();
808
809         /* register global notify functions */
810         file_data_register_notify_func(cache_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
811         file_data_register_notify_func(thumb_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
812         file_data_register_notify_func(histogram_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
813         file_data_register_notify_func(collect_manager_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
814         file_data_register_notify_func(metadata_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
815
816
817         gtkrc_load();
818
819         parse_command_line_for_debug_option(argc, argv);
820         DEBUG_1("%s main: gtk_init", get_exec_time());
821 #ifdef HAVE_CLUTTER
822         if (gtk_clutter_init(&argc, &argv) != CLUTTER_INIT_SUCCESS)
823                 {
824                 log_printf("Can't initialize clutter-gtk.\n");
825                 exit(1);
826                 }
827 #else
828         gtk_init(&argc, &argv);
829 #endif
830
831         if (gtk_major_version < GTK_MAJOR_VERSION ||
832             (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
833                 {
834                 log_printf("!!! This is a friendly warning.\n");
835                 log_printf("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
836                 log_printf("!!!  compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
837                 log_printf("!!!   running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
838                 log_printf("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
839                 }
840
841         DEBUG_1("%s main: pixbuf_inline_register_stock_icons", get_exec_time());
842         pixbuf_inline_register_stock_icons();
843
844         DEBUG_1("%s main: setting default options before commandline handling", get_exec_time());
845         options = init_options(NULL);
846         setup_default_options(options);
847
848         DEBUG_1("%s main: parse_command_line", get_exec_time());
849         parse_command_line(argc, argv);
850
851         DEBUG_1("%s main: mkdir_if_not_exists", get_exec_time());
852         /* these functions don't depend on config file */
853         mkdir_if_not_exists(get_rc_dir());
854         mkdir_if_not_exists(get_collections_dir());
855         mkdir_if_not_exists(get_thumbnails_cache_dir());
856         mkdir_if_not_exists(get_metadata_cache_dir());
857
858         setup_env_path();
859
860         keys_load();
861         accel_map_load();
862
863         /* restore session from the config file */
864
865
866         DEBUG_1("%s main: load_options", get_exec_time());
867         if (!load_options(options))
868                 {
869                 /* load_options calls these functions after it parses global options, we have to call it here if it fails */
870                 filter_add_defaults();
871                 filter_rebuild();
872                 }
873
874         /* handle missing config file and commandline additions*/
875         if (!layout_window_list)
876                 {
877                 /* broken or no config file */
878                 layout_new_from_config(NULL, NULL, TRUE);
879                 }
880
881         layout_editors_reload_start();
882
883         if (command_line->collection_list && !command_line->startup_command_line_collection)
884                 {
885                 GList *work;
886
887                 work = command_line->collection_list;
888                 while (work)
889                         {
890                         CollectWindow *cw;
891                         const gchar *path;
892
893                         path = work->data;
894                         work = work->next;
895
896                         cw = collection_window_new(path);
897                         if (!first_collection && cw) first_collection = cw->cd;
898                         }
899                 }
900
901         if (command_line->log_file)
902                 {
903                 gchar *pathl;
904                 gchar *path = g_strdup(command_line->log_file);
905
906                 pathl = path_from_utf8(path);
907                 command_line->ssi = secure_open(pathl);
908                 }
909
910         if (command_line->cmd_list ||
911             (command_line->startup_command_line_collection && command_line->collection_list))
912                 {
913                 GList *work;
914
915                 if (command_line->startup_command_line_collection)
916                         {
917                         CollectWindow *cw;
918
919                         cw = collection_window_new("");
920                         cd = cw->cd;
921                         }
922                 else
923                         {
924                         cd = collection_new("");        /* if we pass NULL, untitled counter is falsely increm. */
925                         }
926
927                 g_free(cd->path);
928                 cd->path = NULL;
929                 g_free(cd->name);
930                 cd->name = g_strdup(_("Command line"));
931
932                 collection_path_changed(cd);
933
934                 work = command_line->cmd_list;
935                 while (work)
936                         {
937                         collection_add(cd, (FileData *)work->data, FALSE);
938                         work = work->next;
939                         }
940
941                 work = command_line->collection_list;
942                 while (work)
943                         {
944                         collection_load(cd, (gchar *)work->data, COLLECTION_LOAD_APPEND);
945                         work = work->next;
946                         }
947
948                 if (cd->list) layout_image_set_collection(NULL, cd, cd->list->data);
949
950                 /* mem leak, we never unref this collection when !startup_command_line_collection
951                  * (the image view of the main window does not hold a ref to the collection)
952                  * this is sort of unavoidable, for if it did hold a ref, next/back
953                  * may not work as expected when closing collection windows.
954                  *
955                  * collection_unref(cd);
956                  */
957
958                 }
959         else if (first_collection)
960                 {
961                 layout_image_set_collection(NULL, first_collection,
962                                             collection_get_first(first_collection));
963                 }
964
965         buf = g_build_filename(get_rc_dir(), ".command", NULL);
966         remote_connection = remote_server_init(buf, cd);
967         g_free(buf);
968
969         DEBUG_1("%s main: gtk_main", get_exec_time());
970         gtk_main();
971 #ifdef HAVE_GTHREAD
972         gdk_threads_leave();
973 #endif
974         return 0;
975 }
976 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */