-
Notifications
You must be signed in to change notification settings - Fork 159
/
imlib.c
1877 lines (1596 loc) · 46.3 KB
/
imlib.c
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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* imlib.c
Copyright (C) 1999-2003 Tom Gilbert.
Copyright (C) 2010-2020 Birte Kristina Friesel.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies of the Software and its documentation and acknowledgment shall be
given in the documentation and software packages that this Software was
used.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "feh.h"
#include "filelist.h"
#include "signals.h"
#include "winwidget.h"
#include "options.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifdef HAVE_LIBCURL
#include <curl/curl.h>
#endif
#ifdef HAVE_LIBEXIF
#include "exif.h"
#endif
#ifdef HAVE_LIBMAGIC
#include <magic.h>
magic_t magic = NULL;
#endif
Display *disp = NULL;
Visual *vis = NULL;
Screen *scr = NULL;
Colormap cm;
int depth;
Atom wmDeleteWindow;
XContext xid_context = 0;
Window root = 0;
/* Xinerama support */
#ifdef HAVE_LIBXINERAMA
XineramaScreenInfo *xinerama_screens = NULL;
int xinerama_screen;
int num_xinerama_screens;
#endif /* HAVE_LIBXINERAMA */
gib_hash* conversion_cache = NULL;
int childpid = 0;
static int feh_file_is_raw(char *filename);
static char *feh_http_load_image(char *url);
static char *feh_dcraw_load_image(char *filename);
static char *feh_magick_load_image(char *filename);
#ifdef HAVE_LIBXINERAMA
void init_xinerama(void)
{
if (opt.xinerama && XineramaIsActive(disp)) {
int major, minor, px, py, i;
/* discarded */
Window dw;
int di;
unsigned int du;
XineramaQueryVersion(disp, &major, &minor);
xinerama_screens = XineramaQueryScreens(disp, &num_xinerama_screens);
if (opt.xinerama_index >= 0)
xinerama_screen = opt.xinerama_index;
else {
xinerama_screen = 0;
XQueryPointer(disp, root, &dw, &dw, &px, &py, &di, &di, &du);
for (i = 0; i < num_xinerama_screens; i++) {
if (XY_IN_RECT(px, py,
xinerama_screens[i].x_org,
xinerama_screens[i].y_org,
xinerama_screens[i].width,
xinerama_screens[i].height)) {
xinerama_screen = i;
break;
}
}
}
}
}
#endif /* HAVE_LIBXINERAMA */
void init_imlib_fonts(void)
{
/* Set up the font stuff */
imlib_add_path_to_font_path(".");
imlib_add_path_to_font_path(PREFIX "/share/feh/fonts");
return;
}
void init_x_and_imlib(void)
{
disp = XOpenDisplay(NULL);
if (!disp)
eprintf("Can't open X display. It *is* running, yeah?");
vis = DefaultVisual(disp, DefaultScreen(disp));
depth = DefaultDepth(disp, DefaultScreen(disp));
cm = DefaultColormap(disp, DefaultScreen(disp));
root = RootWindow(disp, DefaultScreen(disp));
scr = ScreenOfDisplay(disp, DefaultScreen(disp));
xid_context = XUniqueContext();
#ifdef HAVE_LIBXINERAMA
init_xinerama();
#endif /* HAVE_LIBXINERAMA */
imlib_context_set_display(disp);
imlib_context_set_visual(vis);
imlib_context_set_colormap(cm);
imlib_context_set_color_modifier(NULL);
imlib_context_set_progress_function(NULL);
imlib_context_set_operation(IMLIB_OP_COPY);
wmDeleteWindow = XInternAtom(disp, "WM_DELETE_WINDOW", False);
imlib_set_cache_size(opt.cache_size * 1024 * 1024);
return;
}
int feh_should_ignore_image(Imlib_Image * im)
{
if (opt.filter_by_dimensions) {
unsigned int w = gib_imlib_image_get_width(im);
unsigned int h = gib_imlib_image_get_height(im);
if (w < opt.min_width || w > opt.max_width || h < opt.min_height || h > opt.max_height) {
return 1;
}
}
return 0;
}
int feh_load_image_char(Imlib_Image * im, char *filename)
{
feh_file *file;
int i;
file = feh_file_new(filename);
i = feh_load_image(im, file);
feh_file_free(file);
return(i);
}
void feh_print_load_error(char *file, winwidget w, Imlib_Load_Error err, enum feh_load_error feh_err)
{
if (err == IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS)
eprintf("%s - Out of file descriptors while loading", file);
else if (!opt.quiet || w) {
switch (feh_err) {
case LOAD_ERROR_IMLIB:
// handled in the next switch/case statement
break;
case LOAD_ERROR_IMAGEMAGICK:
im_weprintf(w, "%s - No ImageMagick loader for that file format", file);
break;
case LOAD_ERROR_CURL:
im_weprintf(w, "%s - libcurl was unable to retrieve the file", file);
break;
case LOAD_ERROR_DCRAW:
im_weprintf(w, "%s - Unable to open preview via dcraw", file);
break;
case LOAD_ERROR_MAGICBYTES:
im_weprintf(w, "%s - Does not look like an image (magic bytes missing)", file);
break;
}
if (feh_err != LOAD_ERROR_IMLIB) {
return;
}
switch (err) {
case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST:
im_weprintf(w, "%s - File does not exist", file);
break;
case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY:
im_weprintf(w, "%s - Directory specified for image filename", file);
break;
case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ:
im_weprintf(w, "%s - No read access", file);
break;
case IMLIB_LOAD_ERROR_UNKNOWN:
case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT:
im_weprintf(w, "%s - No Imlib2 loader for that file format", file);
break;
case IMLIB_LOAD_ERROR_PATH_TOO_LONG:
im_weprintf(w, "%s - Path specified is too long", file);
break;
case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT:
im_weprintf(w, "%s - Path component does not exist", file);
break;
case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY:
im_weprintf(w, "%s - Path component is not a directory", file);
break;
case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE:
im_weprintf(w, "%s - Path points outside address space", file);
break;
case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS:
im_weprintf(w, "%s - Too many levels of symbolic links", file);
break;
case IMLIB_LOAD_ERROR_OUT_OF_MEMORY:
im_weprintf(w, "While loading %s - Out of memory", file);
break;
case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE:
im_weprintf(w, "%s - Cannot write to directory", file);
break;
case IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE:
im_weprintf(w, "%s - Cannot write - out of disk space", file);
break;
#if defined(IMLIB2_VERSION_MAJOR) && defined(IMLIB2_VERSION_MINOR) && (IMLIB2_VERSION_MAJOR > 1 || IMLIB2_VERSION_MINOR > 7)
case IMLIB_LOAD_ERROR_IMAGE_READ:
im_weprintf(w, "%s - Invalid image file", file);
break;
case IMLIB_LOAD_ERROR_IMAGE_FRAME:
im_weprintf(w, "%s - Requested frame not in image", file);
break;
#endif
default:
im_weprintf(w, "While loading %s - Unknown error (%d)",
file, err);
break;
}
}
}
#ifdef HAVE_LIBMAGIC
void uninit_magic(void)
{
if (!magic) {
return;
}
magic_close(magic);
magic = NULL;
}
void init_magic(void)
{
if (getenv("FEH_SKIP_MAGIC")) {
return;
}
if (!(magic = magic_open(MAGIC_NONE))) {
weprintf("unable to initialize magic library\n");
return;
}
if (magic_load(magic, NULL) != 0) {
weprintf("cannot load magic database: %s\n", magic_error(magic));
uninit_magic();
}
}
/*
* This is a workaround for an Imlib2 regression, causing unloadable image
* detection to be excessively slow (and, thus, causing feh to hang for a while
* when encountering an unloadable image). We use magic byte detection to
* avoid calling Imlib2 for files it probably cannot handle. See
* <https://phab.enlightenment.org/T8739> and
* <https://github.com/derf/feh/issues/505>.
*/
int feh_is_image(feh_file * file, int magic_flags)
{
const char * mime_type = NULL;
if (!magic) {
return 1;
}
magic_setflags(magic, MAGIC_MIME_TYPE | MAGIC_SYMLINK | magic_flags);
mime_type = magic_file(magic, file->filename);
if (!mime_type) {
return 0;
}
D(("file %s has mime type: %s\n", file->filename, mime_type));
if (strncmp(mime_type, "image/", 6) == 0) {
return 1;
}
/* no infinite loop on compressed content, please */
if (magic_flags) {
return 0;
}
/* imlib2 supports loading compressed images, let's have a look inside */
if (strcmp(mime_type, "application/gzip") == 0 ||
strcmp(mime_type, "application/x-bzip2") == 0 ||
strcmp(mime_type, "application/x-xz") == 0) {
return feh_is_image(file, MAGIC_COMPRESS);
}
return 0;
}
#else
int feh_is_image(__attribute__((unused)) feh_file * file, __attribute__((unused)) int magic_flags)
{
return 1;
}
#endif
int feh_load_image(Imlib_Image * im, feh_file * file)
{
Imlib_Load_Error err = IMLIB_LOAD_ERROR_NONE;
enum feh_load_error feh_err = LOAD_ERROR_IMLIB;
enum { SRC_IMLIB, SRC_HTTP, SRC_MAGICK, SRC_DCRAW } image_source = SRC_IMLIB;
char *tmpname = NULL;
char *real_filename = NULL;
D(("filename is %s, image is %p\n", file->filename, im));
if (!file || !file->filename)
return 0;
if (path_is_url(file->filename)) {
image_source = SRC_HTTP;
if ((tmpname = feh_http_load_image(file->filename)) == NULL) {
feh_err = LOAD_ERROR_CURL;
err = IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST;
}
}
else {
if (feh_is_image(file, 0)) {
*im = imlib_load_image_with_error_return(file->filename, &err);
} else {
feh_err = LOAD_ERROR_MAGICBYTES;
err = IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT;
}
}
if (opt.conversion_timeout >= 0 && (
(err == IMLIB_LOAD_ERROR_UNKNOWN) ||
(err == IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT))) {
if (feh_file_is_raw(file->filename)) {
image_source = SRC_DCRAW;
tmpname = feh_dcraw_load_image(file->filename);
if (!tmpname) {
feh_err = LOAD_ERROR_DCRAW;
}
} else {
image_source = SRC_MAGICK;
feh_err = LOAD_ERROR_IMLIB;
tmpname = feh_magick_load_image(file->filename);
if (!tmpname) {
feh_err = LOAD_ERROR_IMAGEMAGICK;
}
}
}
if (tmpname) {
*im = imlib_load_image_with_error_return(tmpname, &err);
if (!err && im) {
real_filename = file->filename;
file->filename = tmpname;
/*
* feh does not associate a non-native image with its temporary
* filename and may delete the temporary file right after loading.
* To ensure that it is still aware of image size, dimensions, etc.,
* file_info is preloaded here. To avoid a memory leak when loading
* a non-native file multiple times in a slideshow, the file_info
* struct is freed first. If file->info is not set, feh_file_info_free
* is a no-op.
*/
feh_file_info_free(file->info);
feh_file_info_load(file, *im);
file->filename = real_filename;
#ifdef HAVE_LIBEXIF
/*
* if we're called from within feh_reload_image, file->ed is already
* populated.
*/
if (file->ed) {
exif_data_unref(file->ed);
}
file->ed = exif_data_new_from_file(tmpname);
#endif
}
if (!opt.use_conversion_cache && ((image_source != SRC_HTTP) || !opt.keep_http))
unlink(tmpname);
// keep_http already performs an add_file_to_rm_filelist call
else if (opt.use_conversion_cache && !opt.keep_http)
// add_file_to_rm_filelist duplicates tmpname
add_file_to_rm_filelist(tmpname);
if (!opt.use_conversion_cache)
free(tmpname);
} else if (im) {
#ifdef HAVE_LIBEXIF
/*
* if we're called from within feh_reload_image, file->ed is already
* populated.
*/
if (file->ed) {
exif_data_unref(file->ed);
}
file->ed = exif_data_new_from_file(file->filename);
#endif
}
if ((err) || (!im)) {
if (opt.verbose && !opt.quiet) {
fputs("\n", stderr);
reset_output = 1;
}
feh_print_load_error(file->filename, NULL, err, feh_err);
D(("Load *failed*\n"));
return(0);
}
/*
* By default, Imlib2 unconditionally loads a cached file without checking
* if it was modified on disk. However, feh (or rather its users) should
* expect image changes to appear at the next reload. So we tell Imlib2 to
* always check the file modification time and only use a cached image if
* the mtime was not changed. The performance penalty is usually negligible.
*/
imlib_context_set_image(*im);
imlib_image_set_changes_on_disk();
#ifdef HAVE_LIBEXIF
int orientation = 0;
if (file->ed) {
ExifByteOrder byteOrder = exif_data_get_byte_order(file->ed);
ExifEntry *exifEntry = exif_data_get_entry(file->ed, EXIF_TAG_ORIENTATION);
if (exifEntry && opt.auto_rotate) {
orientation = exif_get_short(exifEntry->data, byteOrder);
}
}
if (orientation == 2)
gib_imlib_image_flip_horizontal(*im);
else if (orientation == 3)
gib_imlib_image_orientate(*im, 2);
else if (orientation == 4)
gib_imlib_image_flip_vertical(*im);
else if (orientation == 5) {
gib_imlib_image_orientate(*im, 3);
gib_imlib_image_flip_vertical(*im);
}
else if (orientation == 6)
gib_imlib_image_orientate(*im, 1);
else if (orientation == 7) {
gib_imlib_image_orientate(*im, 3);
gib_imlib_image_flip_horizontal(*im);
}
else if (orientation == 8)
gib_imlib_image_orientate(*im, 3);
#endif
D(("Loaded ok\n"));
return(1);
}
void feh_reload_image(winwidget w, int resize, int force_new)
{
char *new_title;
int len;
Imlib_Image tmp;
int old_w, old_h;
if (!w->file) {
im_weprintf(w, "couldn't reload, this image has no file associated with it.");
winwidget_render_image(w, 0, 0);
return;
}
D(("resize %d, force_new %d\n", resize, force_new));
free(FEH_FILE(w->file->data)->caption);
FEH_FILE(w->file->data)->caption = NULL;
len = strlen(w->name) + sizeof("Reloading: ") + 1;
new_title = emalloc(len);
snprintf(new_title, len, "Reloading: %s", w->name);
winwidget_rename(w, new_title);
free(new_title);
old_w = gib_imlib_image_get_width(w->im);
old_h = gib_imlib_image_get_height(w->im);
/*
* If we don't free the old image before loading the new one, Imlib2's
* caching will get in our way.
* However, if --reload is used (force_new == 0), we want to continue if
* the new image cannot be loaded, so we must not free the old image yet.
*/
if (force_new)
winwidget_free_image(w);
// if it's an external image, our own cache will also get in your way
char *sfn;
if (opt.use_conversion_cache && conversion_cache && (sfn = gib_hash_get(conversion_cache, FEH_FILE(w->file->data)->filename)) != NULL) {
free(sfn);
gib_hash_set(conversion_cache, FEH_FILE(w->file->data)->filename, NULL);
}
if ((feh_load_image(&tmp, FEH_FILE(w->file->data))) == 0) {
if (force_new)
eprintf("failed to reload image\n");
else {
im_weprintf(w, "Couldn't reload image. Is it still there?");
winwidget_render_image(w, 0, 0);
}
return;
}
if (!resize && ((old_w != gib_imlib_image_get_width(tmp)) ||
(old_h != gib_imlib_image_get_height(tmp))))
resize = 1;
if (!force_new)
winwidget_free_image(w);
w->im = tmp;
winwidget_reset_image(w);
w->mode = MODE_NORMAL;
if ((w->im_w != gib_imlib_image_get_width(w->im))
|| (w->im_h != gib_imlib_image_get_height(w->im)))
w->had_resize = 1;
if (w->has_rotated) {
Imlib_Image temp;
temp = gib_imlib_create_rotated_image(w->im, 0.0);
w->im_w = gib_imlib_image_get_width(temp);
w->im_h = gib_imlib_image_get_height(temp);
gib_imlib_free_image_and_decache(temp);
} else {
w->im_w = gib_imlib_image_get_width(w->im);
w->im_h = gib_imlib_image_get_height(w->im);
}
winwidget_render_image(w, resize, 0);
return;
}
static int feh_file_is_raw(char *filename)
{
childpid = fork();
if (childpid == -1) {
perror("fork");
return 0;
}
if (childpid == 0) {
int devnull = open("/dev/null", O_WRONLY);
dup2(devnull, 1);
dup2(devnull, 2);
execlp("dcraw", "dcraw", "-i", filename, NULL);
_exit(1);
} else {
int status;
do {
waitpid(childpid, &status, WUNTRACED);
if (WIFEXITED(status)) {
return !WEXITSTATUS(status);
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 0;
}
static char *feh_dcraw_load_image(char *filename)
{
char *basename;
char *tmpname;
char *sfn;
int fd = -1;
if (opt.use_conversion_cache) {
if (!conversion_cache)
conversion_cache = gib_hash_new();
if ((sfn = gib_hash_get(conversion_cache, filename)) != NULL)
return sfn;
}
basename = strrchr(filename, '/');
if (basename == NULL)
basename = filename;
else
basename++;
tmpname = feh_unique_filename("/tmp/", basename);
if (strlen(tmpname) > (NAME_MAX-6))
tmpname[NAME_MAX-7] = '\0';
sfn = estrjoin("_", tmpname, "XXXXXX", NULL);
free(tmpname);
fd = mkstemp(sfn);
if (fd == -1) {
free(sfn);
return NULL;
}
childpid = fork();
if (childpid == -1) {
weprintf("%s: Can't load with dcraw. Fork failed:", filename);
unlink(sfn);
free(sfn);
close(fd);
return NULL;
} else if (childpid == 0) {
dup2(fd, STDOUT_FILENO);
close(fd);
alarm(opt.conversion_timeout);
execlp("dcraw", "dcraw", "-c", "-e", filename, NULL);
_exit(1);
}
int status;
waitpid(-1, &status, 0);
if (WIFSIGNALED(status)) {
unlink(sfn);
free(sfn);
sfn = NULL;
if (!opt.quiet)
weprintf("%s - Conversion took too long, skipping", filename);
}
if ((sfn != NULL) && opt.use_conversion_cache)
gib_hash_set(conversion_cache, filename, sfn);
return sfn;
}
static char *feh_magick_load_image(char *filename)
{
char *argv_fn;
char *basename;
char *tmpname;
char *sfn;
char tempdir[] = "/tmp/.feh-magick-tmp-XXXXXX";
int fd = -1, devnull = -1;
int status;
char created_tempdir = 0;
if (opt.use_conversion_cache) {
if (!conversion_cache)
conversion_cache = gib_hash_new();
if ((sfn = gib_hash_get(conversion_cache, filename)) != NULL)
return sfn;
}
basename = strrchr(filename, '/');
if (basename == NULL)
basename = filename;
else
basename++;
tmpname = feh_unique_filename("/tmp/", basename);
if (strlen(tmpname) > (NAME_MAX-6))
tmpname[NAME_MAX-7] = '\0';
sfn = estrjoin("_", tmpname, "XXXXXX", NULL);
free(tmpname);
fd = mkstemp(sfn);
if (fd == -1) {
free(sfn);
return NULL;
}
/*
* We could use png:fd:(whatever mkstemp returned) as target filename
* for convert, but this seems to be broken in some ImageMagick versions.
* So we resort to png:(sfn) instead.
*/
argv_fn = estrjoin(":", "png", sfn, NULL);
/*
* By default, ImageMagick saves (occasionally lots of) temporary files
* in /tmp. It doesn't remove them if it runs into a timeout and is killed
* by us, no matter whether we use SIGINT, SIGTERM or SIGKILL. So, unless
* MAGICK_TMPDIR has already been set by the user, we create our own
* temporary directory for ImageMagick and remove its contents at the end of
* this function.
*/
if (getenv("MAGICK_TMPDIR") == NULL) {
if (mkdtemp(tempdir) == NULL) {
weprintf("%s: ImageMagick may leave temporary files in /tmp. mkdtemp failed:", filename);
} else {
created_tempdir = 1;
}
}
if ((childpid = fork()) < 0) {
weprintf("%s: Can't load with imagemagick. Fork failed:", filename);
unlink(sfn);
free(sfn);
sfn = NULL;
}
else if (childpid == 0) {
devnull = open("/dev/null", O_WRONLY);
dup2(devnull, 0);
if (opt.quiet) {
/* discard convert output */
dup2(devnull, 1);
dup2(devnull, 2);
}
/*
* convert only accepts SIGINT via killpg, a normal kill doesn't work
*/
setpgid(0, 0);
if (created_tempdir) {
// no error checking - this is a best-effort code path
setenv("MAGICK_TMPDIR", tempdir, 0);
}
execlp("convert", "convert", filename, argv_fn, NULL);
_exit(1);
}
else {
alarm(opt.conversion_timeout);
waitpid(childpid, &status, 0);
kill(childpid, SIGKILL);
if (opt.conversion_timeout > 0 && !alarm(0)) {
unlink(sfn);
free(sfn);
sfn = NULL;
if (!opt.quiet) {
weprintf("%s: Conversion took too long, skipping", filename);
}
}
close(fd);
childpid = 0;
}
if (created_tempdir) {
DIR *dir;
struct dirent *de;
if ((dir = opendir(tempdir)) == NULL) {
weprintf("%s: Cannot remove temporary ImageMagick files from %s:", filename, tempdir);
} else {
while ((de = readdir(dir)) != NULL) {
if (de->d_name[0] != '.') {
char *temporary_file_name = estrjoin("/", tempdir, de->d_name, NULL);
/*
* We assume that ImageMagick only creates temporary files and
* not directories.
*/
if (unlink(temporary_file_name) == -1) {
weprintf("unlink %s:", temporary_file_name);
}
free(temporary_file_name);
}
}
if (rmdir(tempdir) == -1) {
weprintf("rmdir %s:", tempdir);
}
}
closedir(dir);
}
free(argv_fn);
if ((sfn != NULL) && opt.use_conversion_cache)
gib_hash_set(conversion_cache, filename, sfn);
return sfn;
}
#ifdef HAVE_LIBCURL
#if LIBCURL_VERSION_NUM >= 0x072000 /* 07.32.0 */
static int curl_quit_function(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
#else
static int curl_quit_function(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
#endif
{
// ignore "unused parameter" warnings
(void)clientp;
(void)dltotal;
(void)dlnow;
(void)ultotal;
(void)ulnow;
if (sig_exit) {
/*
* The user wants to quit feh. Tell libcurl to abort the transfer and
* return control to the main loop, where we can quit gracefully.
*/
return 1;
}
return 0;
}
static char *feh_http_load_image(char *url)
{
CURL *curl;
CURLcode res;
char *sfn;
FILE *sfp;
int fd = -1;
char *ebuff;
char *tmpname;
char *basename;
char *path = NULL;
if (opt.use_conversion_cache) {
if (!conversion_cache)
conversion_cache = gib_hash_new();
if ((sfn = gib_hash_get(conversion_cache, url)) != NULL)
return sfn;
}
if (opt.keep_http) {
if (opt.output_dir)
path = opt.output_dir;
else
path = "";
} else
path = "/tmp/";
curl = curl_easy_init();
if (!curl) {
weprintf("open url: libcurl initialization failure");
return NULL;
}
basename = strrchr(url, '/') + 1;
#ifdef HAVE_MKSTEMPS
tmpname = estrjoin("_", "feh_curl_XXXXXX", basename, NULL);
if (strlen(tmpname) > NAME_MAX) {
tmpname[NAME_MAX] = '\0';
}
#else
if (strlen(basename) > NAME_MAX-7) {
tmpname = estrdup("feh_curl_XXXXXX");
} else {
tmpname = estrjoin("_", "feh_curl", basename, "XXXXXX", NULL);
}
#endif
sfn = estrjoin("", path, tmpname, NULL);
free(tmpname);
D(("sfn is %s\n", sfn))
#ifdef HAVE_MKSTEMPS
fd = mkstemps(sfn, strlen(basename) + 1);
#else
fd = mkstemp(sfn);
#endif
if (fd != -1) {
sfp = fdopen(fd, "w+");
if (sfp != NULL) {
#ifdef DEBUG
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
#endif
/*
* Do not allow requests to take longer than 30 minutes.
* This should be sufficiently high to accommodate use cases with
* unusually high latencies, while at the same time avoiding
* feh hanging indefinitely in unattended slideshows.
*/
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1800);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, sfp);
ebuff = emalloc(CURL_ERROR_SIZE);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, ebuff);
curl_easy_setopt(curl, CURLOPT_USERAGENT, PACKAGE "/" VERSION);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
#if LIBCURL_VERSION_NUM >= 0x072000 /* 07.32.0 */
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, curl_quit_function);
#else
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, curl_quit_function);
#endif
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
if (opt.insecure_ssl) {
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
} else if (getenv("CURL_CA_BUNDLE") != NULL) {
// Allow the user to specify custom CA certificates.
curl_easy_setopt(curl, CURLOPT_CAINFO,
getenv("CURL_CA_BUNDLE"));
}
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res != CURLE_OK) {
if (res != CURLE_ABORTED_BY_CALLBACK) {
weprintf("open url: %s", ebuff);
}
unlink(sfn);
close(fd);
free(sfn);
sfn = NULL;
}
free(ebuff);
fclose(sfp);
if (opt.use_conversion_cache)
gib_hash_set(conversion_cache, url, sfn);
return sfn;
} else {
weprintf("open url: fdopen failed:");
unlink(sfn);
free(sfn);
close(fd);
}
} else {
#ifdef HAVE_MKSTEMPS
weprintf("open url: mkstemps failed:");
#else
weprintf("open url: mkstemp failed:");
#endif
free(sfn);
}
curl_easy_cleanup(curl);
return NULL;
}
#else /* HAVE_LIBCURL */
char *feh_http_load_image(char *url)
{
weprintf(
"Cannot load image %s\nPlease recompile feh with libcurl support",
url
);
return NULL;
}
#endif /* HAVE_LIBCURL */
void feh_imlib_image_fill_text_bg(Imlib_Image im, int w, int h)
{
gib_imlib_image_set_has_alpha(im, 1);
imlib_context_set_blend(0);
if (opt.text_bg == TEXT_BG_CLEAR)
gib_imlib_image_fill_rectangle(im, 0, 0, w, h, 0, 0, 0, 0);
else
gib_imlib_image_fill_rectangle(im, 0, 0, w, h, 0, 0, 0, 127);
imlib_context_set_blend(1);
}
static Imlib_Font feh_load_font(winwidget w)
{
static Imlib_Font fn = NULL;
if (opt.font)
fn = gib_imlib_load_font(opt.font);
if (!fn) {
if (w && w->full_screen)
fn = gib_imlib_load_font(DEFAULT_FONT_BIG);
else
fn = gib_imlib_load_font(DEFAULT_FONT);
}
if (!fn) {