forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdarktable.c
More file actions
823 lines (730 loc) · 24.4 KB
/
Copy pathdarktable.c
File metadata and controls
823 lines (730 loc) · 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
/*
This file is part of darktable,
copyright (c) 2009--2012 johannes hanika.
copyright (c) 2010--2012 henrik andersson.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "common/darktable.h"
#include "common/collection.h"
#include "common/selection.h"
#include "common/exif.h"
#include "common/fswatch.h"
#include "common/pwstorage/pwstorage.h"
#ifdef HAVE_GPHOTO2
#include "common/camera_control.h"
#endif
#include "common/film.h"
#include "common/image.h"
#include "common/image_cache.h"
#include "common/imageio_module.h"
#include "common/mipmap_cache.h"
#include "common/opencl.h"
#include "common/points.h"
#include "lua/dt_lua.h"
#include "develop/imageop.h"
#include "develop/blend.h"
#include "libs/lib.h"
#include "views/view.h"
#include "control/control.h"
#include "control/jobs/control_jobs.h"
#include "control/signal.h"
#include "control/conf.h"
#include "gui/gtk.h"
#include "gui/presets.h"
#include "bauhaus/bauhaus.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <string.h>
#include <sys/param.h>
#include <unistd.h>
#include <locale.h>
#if !defined(__APPLE__) && !defined(__FreeBSD__)
#include <malloc.h>
#endif
#ifdef __APPLE__
#include <sys/malloc.h>
#endif
#if defined(__SUNOS__)
#include <sys/varargs.h>
#endif
#ifdef _OPENMP
# include <omp.h>
#endif
darktable_t darktable;
const char dt_supported_extensions[] = "3fr,arw,bay,bmq,cap,cine,cr2,crw,cs1,dc2,dcr,dng,erf,fff,exr,ia,iiq,jpg,jpeg,k25,kc2,kdc,mdc,mef,mos,mrw,nef,nrw,orf,pef,pfm,pxn,qtk,raf,raw,rdc,rw2,rwl,sr2,srf,srw,sti,tif,tiff,x3f";
static int usage(const char *argv0)
{
printf("usage: %s [-d {all,cache,camctl,control,dev,fswatch,memory,opencl,perf,pwstorage,sql}] [IMG_1234.{RAW,..}|image_folder/]", argv0);
#ifdef HAVE_OPENCL
printf(" [--disable-opencl]");
#endif
printf(" [--library <library file>]");
printf(" [--datadir <data directory>]");
printf(" [--moduledir <module directory>]");
printf(" [--tmpdir <tmp directory>]");
printf(" [--configdir <user config directory>]");
printf(" [--cachedir <user config directory>]");
printf("\n");
return 1;
}
typedef void (dt_signal_handler_t)(int) ;
// static dt_signal_handler_t *_dt_sigill_old_handler = NULL;
static dt_signal_handler_t *_dt_sigsegv_old_handler = NULL;
#if (defined(__APPLE__) && defined(APPLE_NEED_DPRINTF)) || \
(defined(__FreeBSD_version) && (__FreeBSD_version < 800071)) || \
defined(__SUNOS__)
static int dprintf(int fd,const char *fmt, ...)
{
va_list ap;
FILE *f = fdopen(fd,"a");
va_start(ap, fmt);
int rc = vfprintf(f, fmt, ap);
fclose(f);
va_end(ap);
return rc;
}
#endif
static
void _dt_sigsegv_handler(int param)
{
FILE *fd;
gchar buf[PIPE_BUF];
gchar *name_used;
int fout;
gboolean delete_file = FALSE;
if((fout = g_file_open_tmp("darktable_bt_XXXXXX.txt", &name_used, NULL)) == -1)
fout = STDOUT_FILENO; // just print everything to stdout
dprintf(fout, "this is %s reporting a segfault:\n\n", PACKAGE_STRING);
gchar *command = g_strdup_printf("gdb %s %d -batch -x %s/gdb_commands", darktable.progname, (int)getpid(), DARKTABLE_DATADIR);
if((fd = popen(command, "r")) != NULL)
{
gboolean read_something = FALSE;
while((fgets(buf, PIPE_BUF, fd)) != NULL)
{
read_something = TRUE;
dprintf(fout, "%s", buf);
}
pclose(fd);
if(fout != STDOUT_FILENO)
{
if(read_something)
g_printerr("backtrace written to %s\n", name_used);
else
{
delete_file = TRUE;
g_printerr("an error occured while trying to execute gdb. please check if gdb is installed on your system.\n");
}
}
}
else
{
delete_file = TRUE;
g_printerr("an error occured while trying to execute gdb.\n");
}
if(fout != STDOUT_FILENO)
close(fout);
if(delete_file)
g_unlink(name_used);
g_free(command);
g_free(name_used);
/* pass it further to the old handler*/
_dt_sigsegv_old_handler(param);
}
#if 0
static
void _dt_sigill_handler(int param)
{
fprintf(stderr, "[this doesn't seem to work]\n");
GtkWidget *dlg = gtk_message_dialog_new(NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_ERROR,GTK_BUTTONS_OK,_(
"darktable has trapped an illegal instruction which probably means that \
an invalid processor optimized codepath is used for your cpu, please try reproduce the crash running 'gdb darktable' from \
the console and post the backtrace log to mailing list with information about your CPU and where you got the package from."));
gtk_dialog_run(GTK_DIALOG(dlg));
/* pass it further to the old handler*/
_dt_sigill_old_handler(param);
}
#if defined(__i386__) && defined(__PIC__)
#define cpuid(level, a, b, c, d) \
__asm__ ("xchgl %%ebx, %1\n" \
"cpuid\n" \
"xchgl %%ebx, %1\n" \
: "=a" (a), "=r" (b), "=c" (c), "=d" (d) \
: "0" (level) \
)
#else
#define cpuid(level, a, b, c, d) \
__asm__ ("cpuid" \
: "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
: "0" (level) \
)
#endif
static
void dt_check_cpu(int argc,char **argv)
{
/* hook up SIGILL handler */
_dt_sigill_old_handler = signal(SIGILL,&_dt_sigill_handler);
/* call cpuid for SSE level */
int ax,bx,cx,dx;
cpuid(0x1,ax,bx,cx,dx);
ax = bx = 0;
char message[512]={0};
strcat(message,_("SIMD extensions found: "));
if((cx & 1) && (darktable.cpu_flags |= DT_CPU_FLAG_SSE3))
strcat(message,"SSE3 ");
if( ((dx >> 26) & 1) && (darktable.cpu_flags |= DT_CPU_FLAG_SSE2))
strcat(message,"SSE2 ");
if (((dx >> 25) & 1) && (darktable.cpu_flags |= DT_CPU_FLAG_SSE))
strcat(message,"SSE ");
if (!darktable.cpu_flags)
strcat(message,"none");
/* for now, bail out if SSE2 is not availble */
if(!(darktable.cpu_flags & DT_CPU_FLAG_SSE2))
{
gtk_init (&argc, &argv);
GtkWidget *dlg = gtk_message_dialog_new(NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_ERROR,GTK_BUTTONS_OK,_(
"darktable is very cpu intensive and uses SSE2 SIMD instructions \
for heavy calculations. This gives a better user experience but also defines a minimum \
processor requirement.\n\nThe processor in YOUR system does NOT support SSE2. \
darktable will now close down.\n\n%s"),message);
gtk_dialog_run(GTK_DIALOG(dlg));
exit(11);
}
}
#endif
/* TODO: make this case insensitive */
gboolean dt_supported_image(const gchar *filename)
{
gboolean supported = FALSE;
char **extensions = g_strsplit(dt_supported_extensions, ",", 100);
char *ext = g_strrstr(filename,".");
if(!ext) return FALSE;
for(char **i=extensions; *i!=NULL; i++)
if(!g_ascii_strncasecmp(ext+1, *i,strlen(*i)))
{
supported = TRUE;
break;
}
g_strfreev(extensions);
return supported;
}
static void strip_semicolons_from_keymap(const char* path)
{
char pathtmp[1024];
FILE *fin = fopen(path, "r");
FILE *fout;
int i;
int c = '\0';
snprintf(pathtmp, 1024, "%s_tmp", path);
fout = fopen(pathtmp, "w");
// First ignoring the first three lines
for(i = 0; i < 3; i++)
{
c = fgetc(fin);
while(c != '\n')
c = fgetc(fin);
}
// Then ignore the first two characters of each line, copying the rest out
while(c != EOF)
{
fseek(fin, 2, SEEK_CUR);
do
{
c = fgetc(fin);
if(c != EOF)
fputc(c, fout);
}while(c != '\n' && c != EOF);
}
fclose(fin);
fclose(fout);
g_file_delete(g_file_new_for_path(path), NULL, NULL);
g_file_move(g_file_new_for_path(pathtmp), g_file_new_for_path(path), 0,
NULL, NULL, NULL, NULL);
}
int dt_init(int argc, char *argv[], const int init_gui)
{
#ifndef __APPLE__
_dt_sigsegv_old_handler = signal(SIGSEGV,&_dt_sigsegv_handler);
#endif
#ifndef __SSE2__
fprintf(stderr, "[dt_init] unfortunately we depend on SSE2 instructions at this time.\n");
fprintf(stderr, "[dt_init] please contribute a backport patch (or buy a newer processor).\n");
return 1;
#endif
#ifdef M_MMAP_THRESHOLD
mallopt(M_MMAP_THRESHOLD,128*1024) ; /* use mmap() for large allocations */
#endif
bindtextdomain (GETTEXT_PACKAGE, DARKTABLE_LOCALEDIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
// init all pointers to 0:
memset(&darktable, 0, sizeof(darktable_t));
darktable.progname = argv[0];
// database
gchar *dbfilename_from_command = NULL;
char *datadirFromCommand = NULL;
char *moduledirFromCommand = NULL;
char *tmpdirFromCommand = NULL;
char *configdirFromCommand = NULL;
char *cachedirFromCommand = NULL;
darktable.num_openmp_threads = 1;
#ifdef _OPENMP
darktable.num_openmp_threads = omp_get_num_procs();
#endif
darktable.unmuted = 0;
char *image_to_load = NULL;
for(int k=1; k<argc; k++)
{
if(argv[k][0] == '-')
{
if(!strcmp(argv[k], "--help"))
{
return usage(argv[0]);
}
if(!strcmp(argv[k], "-h"))
{
return usage(argv[0]);
}
else if(!strcmp(argv[k], "--version"))
{
printf("this is "PACKAGE_STRING"\ncopyright (c) 2009-2012 johannes hanika\n"PACKAGE_BUGREPORT"\n");
return 1;
}
else if(!strcmp(argv[k], "--library"))
{
dbfilename_from_command = argv[++k];
}
else if(!strcmp(argv[k], "--datadir"))
{
datadirFromCommand = argv[++k];
}
else if(!strcmp(argv[k], "--moduledir"))
{
moduledirFromCommand = argv[++k];
}
else if(!strcmp(argv[k], "--tmpdir"))
{
tmpdirFromCommand = argv[++k];
}
else if(!strcmp(argv[k], "--configdir"))
{
configdirFromCommand = argv[++k];
}
else if(!strcmp(argv[k], "--cachedir"))
{
cachedirFromCommand = argv[++k];
}
else if(argv[k][1] == 'd' && argc > k+1)
{
if(!strcmp(argv[k+1], "all")) darktable.unmuted = 0xffffffff; // enable all debug information
else if(!strcmp(argv[k+1], "cache")) darktable.unmuted |= DT_DEBUG_CACHE; // enable debugging for lib/film/cache module
else if(!strcmp(argv[k+1], "control")) darktable.unmuted |= DT_DEBUG_CONTROL; // enable debugging for scheduler module
else if(!strcmp(argv[k+1], "dev")) darktable.unmuted |= DT_DEBUG_DEV; // develop module
else if(!strcmp(argv[k+1], "fswatch")) darktable.unmuted |= DT_DEBUG_FSWATCH; // fswatch module
else if(!strcmp(argv[k+1], "camctl")) darktable.unmuted |= DT_DEBUG_CAMCTL; // camera control module
else if(!strcmp(argv[k+1], "perf")) darktable.unmuted |= DT_DEBUG_PERF; // performance measurements
else if(!strcmp(argv[k+1], "pwstorage")) darktable.unmuted |= DT_DEBUG_PWSTORAGE; // pwstorage module
else if(!strcmp(argv[k+1], "opencl")) darktable.unmuted |= DT_DEBUG_OPENCL; // gpu accel via opencl
else if(!strcmp(argv[k+1], "sql")) darktable.unmuted |= DT_DEBUG_SQL; // SQLite3 queries
else if(!strcmp(argv[k+1], "memory")) darktable.unmuted |= DT_DEBUG_MEMORY; // some stats on mem usage now and then.
else if(!strcmp(argv[k+1], "lua")) darktable.unmuted |= DT_DEBUG_LUA; // lua errors are reported on console
else return usage(argv[0]);
k ++;
}
else if(argv[k][1] == 't' && argc > k+1)
{
darktable.num_openmp_threads = CLAMP(atol(argv[k+1]), 1, 100);
printf("[dt_init] using %d threads for openmp parallel sections\n", darktable.num_openmp_threads);
k ++;
}
}
else
{
image_to_load = argv[k];
}
}
if(darktable.unmuted & DT_DEBUG_MEMORY)
{
fprintf(stderr, "[memory] at startup\n");
dt_print_mem_usage();
}
#ifdef _OPENMP
omp_set_num_threads(darktable.num_openmp_threads);
#endif
dt_loc_init_datadir(datadirFromCommand);
dt_loc_init_plugindir(moduledirFromCommand);
if(dt_loc_init_tmp_dir(tmpdirFromCommand)) {
printf(_("ERROR : invalid temporary directory : %s\n"),darktable.tmpdir);
return usage(argv[0]);
}
dt_loc_init_user_config_dir(configdirFromCommand);
dt_loc_init_user_cache_dir(cachedirFromCommand);
g_type_init();
// does not work, as gtk is not inited yet.
// even if it were, it's a super bad idea to invoke gtk stuff from
// a signal handler.
/* check cput caps */
// dt_check_cpu(argc,argv);
#ifdef HAVE_GEGL
(void)setenv("GEGL_PATH", DARKTABLE_DATADIR "/gegl:/usr/lib/gegl-0.0", 1);
gegl_init(&argc, &argv);
#endif
// early lua init, doesn't depend on DT stuff yet
dt_lua_init();
// thread-safe init:
dt_exif_init();
char datadir[1024];
dt_loc_get_user_config_dir (datadir,1024);
char filename[1024];
snprintf(filename, 1024, "%s/darktablerc", datadir);
// Lua init
// intialize the config backend. this needs to be done first...
darktable.conf = (dt_conf_t *)malloc(sizeof(dt_conf_t));
memset(darktable.conf, 0, sizeof(dt_conf_t));
dt_conf_init(darktable.conf, filename);
// set the interface language
const gchar* lang = dt_conf_get_string("ui_last/gui_language");
if(lang != NULL && lang[0] != '\0')
{
if(setlocale(LC_ALL, lang) != NULL)
gtk_disable_setlocale();
}
// initialize the database
darktable.db = dt_database_init(dbfilename_from_command);
// Initialize the signal system
darktable.signals = dt_control_signal_init();
// Initialize the filesystem watcher
darktable.fswatch=dt_fswatch_new();
#ifdef HAVE_GPHOTO2
// Initialize the camera control
darktable.camctl=dt_camctl_new();
#endif
// get max lighttable thumbnail size:
darktable.thumbnail_width = CLAMPS(dt_conf_get_int("plugins/lighttable/thumbnail_width"), 200, 3000);
darktable.thumbnail_height = CLAMPS(dt_conf_get_int("plugins/lighttable/thumbnail_height"), 200, 3000);
// and make sure it can be mip-mapped all the way from mip4 to mip0
darktable.thumbnail_width /= 16;
darktable.thumbnail_width *= 16;
darktable.thumbnail_height /= 16;
darktable.thumbnail_height *= 16;
// Initialize the password storage engine
darktable.pwstorage=dt_pwstorage_new();
// FIXME: move there into dt_database_t
dt_pthread_mutex_init(&(darktable.db_insert), NULL);
dt_pthread_mutex_init(&(darktable.plugin_threadsafe), NULL);
darktable.control = (dt_control_t *)malloc(sizeof(dt_control_t));
memset(darktable.control, 0, sizeof(dt_control_t));
if(init_gui)
{
dt_control_init(darktable.control);
}
else
{
// this is in memory, so schema can't exist yet.
if(!strcmp(dbfilename_from_command, ":memory:"))
{
dt_control_create_database_schema();
dt_gui_presets_init(); // also init preset db schema.
}
darktable.control->running = 0;
darktable.control->accelerators = NULL;
dt_pthread_mutex_init(&darktable.control->run_mutex, NULL);
}
// initialize collection query
darktable.collection_listeners = NULL;
darktable.collection = dt_collection_new(NULL);
/* initialize sellection */
darktable.selection = dt_selection_new();
darktable.opencl = (dt_opencl_t *)malloc(sizeof(dt_opencl_t));
memset(darktable.opencl, 0, sizeof(dt_opencl_t));
dt_opencl_init(darktable.opencl, argc, argv);
darktable.blendop = (dt_blendop_t *)malloc(sizeof(dt_blendop_t));
memset(darktable.blendop, 0, sizeof(dt_blendop_t));
dt_develop_blend_init(darktable.blendop);
darktable.points = (dt_points_t *)malloc(sizeof(dt_points_t));
memset(darktable.points, 0, sizeof(dt_points_t));
dt_points_init(darktable.points, dt_get_num_threads());
// must come before mipmap_cache, because that one will need to access
// image dimensions stored in here:
darktable.image_cache = (dt_image_cache_t *)malloc(sizeof(dt_image_cache_t));
memset(darktable.image_cache, 0, sizeof(dt_image_cache_t));
dt_image_cache_init(darktable.image_cache);
darktable.mipmap_cache = (dt_mipmap_cache_t *)malloc(sizeof(dt_mipmap_cache_t));
memset(darktable.mipmap_cache, 0, sizeof(dt_mipmap_cache_t));
dt_mipmap_cache_init(darktable.mipmap_cache);
// The GUI must be initialized before the views, because the init()
// functions of the views depend on darktable.control->accels_* to register
// their keyboard accelerators
if(init_gui)
{
darktable.gui = (dt_gui_gtk_t *)malloc(sizeof(dt_gui_gtk_t));
memset(darktable.gui,0,sizeof(dt_gui_gtk_t));
if(dt_gui_gtk_init(darktable.gui, argc, argv)) return 1;
dt_bauhaus_init();
}
else darktable.gui = NULL;
darktable.view_manager = (dt_view_manager_t *)malloc(sizeof(dt_view_manager_t));
memset(darktable.view_manager, 0, sizeof(dt_view_manager_t));
dt_view_manager_init(darktable.view_manager);
// load the darkroom mode plugins once:
dt_iop_load_modules_so();
if(init_gui)
{
darktable.lib = (dt_lib_t *)malloc(sizeof(dt_lib_t));
memset(darktable.lib, 0, sizeof(dt_lib_t));
dt_lib_init(darktable.lib);
dt_control_load_config(darktable.control);
g_strlcpy(darktable.control->global_settings.dbname, filename, 512); // overwrite if relocated.
}
darktable.imageio = (dt_imageio_t *)malloc(sizeof(dt_imageio_t));
memset(darktable.imageio, 0, sizeof(dt_imageio_t));
dt_imageio_init(darktable.imageio);
if(init_gui)
{
// Loading the keybindings
char keyfile[1024];
// First dump the default keymapping
snprintf(keyfile, 1024, "%s/keyboardrc_default", datadir);
gtk_accel_map_save(keyfile);
// Removing extraneous semi-colons from the default keymap
strip_semicolons_from_keymap(keyfile);
// Then load any modified keys if available
snprintf(keyfile, 1024, "%s/keyboardrc", datadir);
if(g_file_test(keyfile, G_FILE_TEST_EXISTS))
gtk_accel_map_load(keyfile);
else
gtk_accel_map_save(keyfile); // Save the default keymap if none is present
}
int id = 0;
if(init_gui && image_to_load)
{
char* filename;
if(g_str_has_prefix(image_to_load, "file://"))
image_to_load += strlen("file://");
if(g_path_is_absolute(image_to_load) == FALSE)
{
char* current_dir = g_get_current_dir();
char* tmp_filename = g_build_filename(current_dir, image_to_load, NULL);
filename = (char*)g_malloc(sizeof(char)*MAXPATHLEN);
if(realpath(tmp_filename, filename) == NULL)
{
dt_control_log(_("found strange path `%s'"), tmp_filename);
g_free(current_dir);
g_free(tmp_filename);
g_free(filename);
return 0;
}
g_free(current_dir);
g_free(tmp_filename);
}
else
{
filename = g_strdup(image_to_load);
}
if(g_file_test(filename, G_FILE_TEST_IS_DIR))
{
// import a directory into a film roll
unsigned int last_char = strlen(filename)-1;
if(filename[last_char] == '/')
filename[last_char] = '\0';
id = dt_film_import(filename);
if(id)
{
dt_film_open(id);
dt_ctl_switch_mode_to(DT_LIBRARY);
}
else
{
dt_control_log(_("error loading directory `%s'"), filename);
}
}
else
{
// import a single image
gchar *directory = g_path_get_dirname((const gchar *)filename);
dt_film_t film;
const int filmid = dt_film_new(&film, directory);
id = dt_image_import(filmid, filename, TRUE);
g_free (directory);
if(id)
{
dt_film_open(filmid);
// make sure buffers are loaded (load full for testing)
dt_mipmap_buffer_t buf;
dt_mipmap_cache_read_get(darktable.mipmap_cache, &buf, id, DT_MIPMAP_FULL, DT_MIPMAP_BLOCKING);
if(!buf.buf)
{
id = 0;
dt_control_log(_("file `%s' has unknown format!"), filename);
}
else
{
dt_mipmap_cache_read_release(darktable.mipmap_cache, &buf);
DT_CTL_SET_GLOBAL(lib_image_mouse_over_id, id);
dt_ctl_switch_mode_to(DT_DEVELOP);
}
}
else
{
dt_control_log(_("error loading file `%s'"), filename);
}
}
g_free(filename);
}
if(init_gui && !id)
{
dt_ctl_switch_mode_to(DT_LIBRARY);
}
/* start the indexer background job */
dt_control_start_indexer();
if(darktable.unmuted & DT_DEBUG_MEMORY)
{
fprintf(stderr, "[memory] after successful startup\n");
dt_print_mem_usage();
}
/* init lua last, since it's user made stuff it must be in the real environment */
dt_lua_run_init();
return 0;
}
void dt_cleanup()
{
dt_ctl_switch_mode_to(DT_MODE_NONE);
const int init_gui = (darktable.gui != NULL);
if(init_gui)
{
dt_control_write_config(darktable.control);
dt_control_shutdown(darktable.control);
dt_lib_cleanup(darktable.lib);
free(darktable.lib);
}
dt_view_manager_cleanup(darktable.view_manager);
free(darktable.view_manager);
if(init_gui)
{
dt_imageio_cleanup(darktable.imageio);
free(darktable.imageio);
dt_gui_gtk_cleanup(darktable.gui);
free(darktable.gui);
}
dt_image_cache_cleanup(darktable.image_cache);
free(darktable.image_cache);
dt_mipmap_cache_cleanup(darktable.mipmap_cache);
free(darktable.mipmap_cache);
if(init_gui)
{
dt_control_cleanup(darktable.control);
free(darktable.control);
}
dt_conf_cleanup(darktable.conf);
free(darktable.conf);
dt_points_cleanup(darktable.points);
free(darktable.points);
dt_iop_unload_modules_so();
dt_opencl_cleanup(darktable.opencl);
free(darktable.opencl);
#ifdef HAVE_GPHOTO2
dt_camctl_destroy(darktable.camctl);
#endif
dt_pwstorage_destroy(darktable.pwstorage);
dt_fswatch_destroy(darktable.fswatch);
dt_database_destroy(darktable.db);
dt_bauhaus_cleanup();
dt_pthread_mutex_destroy(&(darktable.db_insert));
dt_pthread_mutex_destroy(&(darktable.plugin_threadsafe));
dt_exif_cleanup();
#ifdef HAVE_GEGL
gegl_exit();
#endif
}
void dt_print(dt_debug_thread_t thread, const char *msg, ...)
{
if(darktable.unmuted & thread)
{
va_list ap;
va_start(ap, msg);
vprintf(msg, ap);
va_end(ap);
fflush(stdout);
}
}
void dt_gettime_t(char *datetime, time_t t)
{
struct tm tt;
(void)localtime_r(&t, &tt);
strftime(datetime, 20, "%Y:%m:%d %H:%M:%S", &tt);
}
void dt_gettime(char *datetime)
{
dt_gettime_t(datetime, time(NULL));
}
void *dt_alloc_align(size_t alignment, size_t size)
{
#if defined(__MACH__) || defined(__APPLE__) || (defined(__FreeBSD_version) && __FreeBSD_version < 700013)
return malloc(size);
#else
void *ptr = NULL;
if(posix_memalign(&ptr, alignment, size)) return NULL;
return ptr;
#endif
}
void dt_show_times(const dt_times_t *start, const char *prefix, const char *suffix, ...)
{
dt_times_t end;
char buf[120]; /* Arbitrary size, should be lots big enough for everything used in DT */
int i;
/* Skip all the calculations an everything if -d perf isn't on */
if (darktable.unmuted & DT_DEBUG_PERF)
{
dt_get_times(&end);
i = sprintf(buf, "%s took %.3f secs (%.3f CPU)", prefix, end.clock - start->clock, end.user - start->user);
if (suffix != NULL)
{
va_list ap;
va_start(ap, suffix);
buf[i++] = ' ';
vsnprintf(buf + i, sizeof buf - i, suffix, ap);
va_end(ap);
}
dt_print(DT_DEBUG_PERF, "%s\n", buf);
}
}
void dt_configure_defaults()
{
const int threads = dt_get_num_threads();
const size_t mem = dt_get_total_memory();
const int bits = (sizeof(void*) == 4) ? 32 : 64;
fprintf(stderr, "[defaults] found a %d-bit system with %zu kb ram and %d cores\n", bits, mem, threads);
if(mem > (2u<<20) && threads > 4)
{
fprintf(stderr, "[defaults] setting high quality defaults\n");
dt_conf_set_int("worker_threads", 8);
dt_conf_set_int("cache_memory", 1u<<30);
dt_conf_set_int("plugins/lighttable/thumbnail_width", 1300);
dt_conf_set_int("plugins/lighttable/thumbnail_height", 1000);
}
if(mem < (1u<<20) || threads <= 2 || bits < 64)
{
fprintf(stderr, "[defaults] setting very conservative defaults\n");
dt_conf_set_int("worker_threads", 1);
dt_conf_set_int("cache_memory", 200u<<10);
dt_conf_set_int("plugins/lighttable/thumbnail_width", 800);
dt_conf_set_int("plugins/lighttable/thumbnail_height", 500);
}
}
// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
// vim: shiftwidth=2 expandtab tabstop=2 cindent
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-space on;