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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
|
/*
Functions to allocate and deallocate structures and structure data
By Jim Frost 1989.10.03, Bryan Henderson 2006.03.25.
See COPYRIGHT file for copyright information.
*/
#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
#define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */
#define _BSD_SOURCE /* Make sure strcaseeq() is in nstring.h */
#include <assert.h>
#include <ctype.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <X11/cursorfont.h>
#include <X11/Xatom.h>
#include <X11/Xdefs.h> /* Needed by Xutil.h */
#include <X11/X.h> /* Needed by Xutil.h */
#include <X11/Xlib.h> /* Needed by Xutil.h */
#include <X11/Xutil.h>
#include "mallocvar.h"
#include "nstring.h"
#include "pm.h"
#include "ximageinfo.h"
#include "send.h"
#include "image.h"
#include "window.h"
/* A viewer object is something in which you can display an image. You
can display multiple images in the same viewer, sequentially.
The viewer has a permanent window, called the viewport. When you display
an image, it has a second window, called the image window, which is a
child of the viewport.
*/
struct viewer {
Display * dispP;
int scrn;
Window imageWin;
Window viewportWin;
Colormap imageColormap;
Cursor cursor;
unsigned int xpos, ypos;
unsigned int width, height;
bool fullscreen;
bool userChoseGeometry;
Atom deleteAtom;
bool blank;
/* I'm just guessing, but it seems that a "paint" operation is
necessary the first time a viewport is used, but not when
displaying a new image in an old viewport. I assume that's
because the new viewport is blank, and that's what this
value means.
*/
Pixmap pixmap;
};
static void
setXloadimageClassHint(Display * const dispP,
Window const window) {
XClassHint classhint;
classhint.res_class = (char*)"Xloadimage";
classhint.res_name = (char*)"xloadimage";
XSetClassHint(dispP, window, &classhint);
}
static void
setDeleteWindow(viewer * const viewerP) {
Atom const protoAtom = XInternAtom(viewerP->dispP, "WM_PROTOCOLS", False);
viewerP->deleteAtom = XInternAtom(viewerP->dispP,
"WM_DELETE_WINDOW", False);
if ((protoAtom != None) && (viewerP->deleteAtom != None))
XChangeProperty(viewerP->dispP, viewerP->viewportWin,
protoAtom, XA_ATOM, 32, PropModeReplace,
(unsigned char *)&viewerP->deleteAtom, 1);
}
static void
getInitialViewerGeometry(const char * const geometryString,
bool const fullscreen,
Display * const dispP,
int const scrn,
unsigned int * const xposP,
unsigned int * const yposP,
unsigned int * const widthP,
unsigned int * const heightP,
bool * const userChoseP) {
unsigned int const defaultWidth = 10;
unsigned int const defaultHeight = 10;
if (fullscreen) {
*widthP = DisplayWidth(dispP, scrn);
*heightP = DisplayHeight(dispP, scrn);
*xposP = 0;
*yposP = 0;
*userChoseP = TRUE;
} else if (geometryString) {
const char * defGeom;
pm_asprintf(&defGeom, "%ux%u+0+0", defaultWidth, defaultHeight);
XGeometry(dispP, scrn, geometryString, defGeom, 0, 1, 1, 0, 0,
(int *)xposP, (int *)yposP,
(int *)widthP, (int *)heightP);
pm_strfree(defGeom);
*userChoseP = TRUE;
} else {
*widthP = defaultWidth;
*heightP = defaultHeight;
*xposP = 0;
*yposP = 0;
*userChoseP = FALSE;
}
}
void
createViewer(viewer ** const viewerPP,
Display * const dispP,
int const scrn,
const char * const geometryString,
bool const fullscreen) {
viewer * viewerP;
XSetWindowAttributes swa_view;
MALLOCVAR_NOFAIL(viewerP);
viewerP->dispP = dispP;
viewerP->scrn = scrn;
viewerP->fullscreen = fullscreen;
viewerP->imageWin = 0;
viewerP->viewportWin = 0;
getInitialViewerGeometry(geometryString, fullscreen, dispP, scrn,
&viewerP->xpos, &viewerP->ypos,
&viewerP->width, &viewerP->height,
&viewerP->userChoseGeometry);
viewerP->cursor = XCreateFontCursor(dispP, XC_watch);
swa_view.background_pixel = WhitePixel(dispP, scrn);
swa_view.backing_store = NotUseful;
swa_view.cursor = viewerP->cursor;
swa_view.event_mask =
ButtonPressMask | Button1MotionMask | KeyPressMask |
StructureNotifyMask | EnterWindowMask | LeaveWindowMask;
swa_view.save_under = FALSE;
viewerP->viewportWin =
XCreateWindow(dispP, RootWindow(dispP, scrn),
viewerP->xpos, viewerP->ypos,
viewerP->width, viewerP->height, 0,
DefaultDepth(dispP, scrn), InputOutput,
DefaultVisual(dispP, scrn),
CWBackingStore | CWBackPixel | CWCursor |
CWEventMask | CWSaveUnder,
&swa_view);
setXloadimageClassHint(viewerP->dispP, viewerP->viewportWin);
setDeleteWindow(viewerP);
viewerP->blank = TRUE;
*viewerPP = viewerP;
}
static void
determineRepaintStrategy(viewer * const viewerP,
bool const userWantsPixmap,
bool const verbose,
XImageInfo * const ximageinfoP,
Pixmap * const pixmapP) {
/* Decide how we're going to handle repaints. We have three modes:
use backing-store, use background pixmap, and use exposures.
If the server allows backing-store, we enable it and use it.
This really helps servers which are memory constrained. If the
server does not have backing-store, we try to send the image to
a pixmap and use that as backing-store. If that fails, we use
exposures to blit the image (which is ugly but it works).
'use_pixmap' forces background pixmap mode, which may improve
performance.
*/
ximageinfoP->drawable = viewerP->imageWin;
if ((DoesBackingStore(ScreenOfDisplay(viewerP->dispP, viewerP->scrn)) ==
NotUseful) ||
userWantsPixmap) {
*pixmapP = ximageToPixmap(viewerP->dispP, viewerP->imageWin,
ximageinfoP);
if (*pixmapP == None && verbose)
pm_message("Cannot create image in server; "
"repaints will be ugly!");
} else
*pixmapP = None;
}
static void
setImageWindowAttr(Display * const dispP,
int const scrn,
Window const imageWindow,
Colormap const cmap,
Pixmap const pixmap) {
/* build window attributes for the image window */
XSetWindowAttributes swa_img;
unsigned int wa_mask_img;
swa_img.bit_gravity = NorthWestGravity;
swa_img.save_under = FALSE;
swa_img.colormap = cmap;
swa_img.border_pixel = 0;
wa_mask_img = 0; /* initial value */
if (pixmap == None) {
/* No pixmap. Must paint over the wire. Ask for BackingStore
to cut down on the painting. But, ask for Exposures so we can
paint both viewables and backingstore.
*/
swa_img.background_pixel = WhitePixel(dispP,scrn);
wa_mask_img |= CWBackPixel;
swa_img.event_mask = ExposureMask;
wa_mask_img |= CWEventMask;
swa_img.backing_store = WhenMapped;
wa_mask_img |= CWBackingStore;
} else {
/* We have a pixmap so tile the window. to move the image we only
have to move the window and the server should do the rest.
*/
swa_img.background_pixmap = pixmap;
wa_mask_img |= CWBackPixmap;
swa_img.event_mask = 0; /* no exposures please */
wa_mask_img |= CWEventMask;
swa_img.backing_store = NotUseful;
wa_mask_img |= CWBackingStore;
}
XChangeWindowAttributes(dispP, imageWindow, wa_mask_img, &swa_img);
}
static void
createImageWindow(viewer * const viewerP,
XImageInfo * const ximageInfoP,
Image * const imageP,
Visual * const visualP,
bool const userWantsPixmap,
bool const verbose) {
XSetWindowAttributes swa_img;
swa_img.bit_gravity = NorthWestGravity;
swa_img.save_under = FALSE;
swa_img.colormap = ximageInfoP->cmap;
swa_img.border_pixel = 0;
viewerP->imageWin = XCreateWindow(viewerP->dispP, viewerP->viewportWin,
viewerP->xpos, viewerP->ypos,
imageP->width, imageP->height, 0,
ximageInfoP->depth, InputOutput, visualP,
CWBitGravity | CWColormap | CWSaveUnder |
CWBorderPixel, &swa_img);
viewerP->imageColormap = ximageInfoP->cmap;
setXloadimageClassHint(viewerP->dispP, viewerP->imageWin);
determineRepaintStrategy(viewerP, userWantsPixmap, verbose, ximageInfoP,
&viewerP->pixmap);
setImageWindowAttr(viewerP->dispP, viewerP->scrn,
viewerP->imageWin, ximageInfoP->cmap,
viewerP->pixmap);
}
static void
destroyImageWindow(viewer * const viewerP) {
if (viewerP->imageWin) {
if (viewerP->imageColormap &&
(viewerP->imageColormap != DefaultColormap(viewerP->dispP, viewerP->scrn)))
XFreeColormap(viewerP->dispP, viewerP->imageColormap);
XDestroyWindow(viewerP->dispP, viewerP->imageWin);
}
}
static void
changeCursor(viewer * const viewerP,
unsigned int const imageWidth,
unsigned int const imageHeight) {
Cursor cursor;
XSetWindowAttributes swa;
if ((viewerP->width >= imageWidth) && (viewerP->height >= imageHeight))
cursor = XCreateFontCursor(viewerP->dispP, XC_icon);
else if ((viewerP->width < imageWidth) && (viewerP->height >= imageHeight))
cursor = XCreateFontCursor(viewerP->dispP, XC_sb_h_double_arrow);
else if ((viewerP->width >= imageWidth) && (viewerP->height < imageHeight))
cursor = XCreateFontCursor(viewerP->dispP, XC_sb_v_double_arrow);
else
cursor = XCreateFontCursor(viewerP->dispP, XC_fleur);
swa.cursor = cursor;
XChangeWindowAttributes(viewerP->dispP, viewerP->viewportWin,
CWCursor, &swa);
XFreeCursor(viewerP->dispP, viewerP->cursor);
viewerP->cursor = cursor;
}
static void
placeImage(viewer * const viewerP,
int const width,
int const height,
int * const rxP, /* input and output */
int * const ryP) { /* input and output */
/*----------------------------------------------------------------------------
Move the X window of *viewerP on the right place on the display to contain
an image whose dimensions are 'width' by 'height'.
As input *rxP and *ryP are the current location of the window (offset from
the top left corner of the display of the top left corner of the window).
We update them with the new location.
If the image is narrower than the display, center it.
Otherwise, either left-justify it or right-justify it depending upon the
current position of the window:
If in the current position, the right edge of the image is within the
display, right-justify.
Otherwise left-justify.
Do the analogous thing for vertical placement.
-----------------------------------------------------------------------------*/
int pixx, pixy;
pixx = *rxP;
pixy = *ryP;
if (viewerP->width > width)
pixx = (viewerP->width - width) / 2;
else {
if ((pixx < 0) && (pixx + width < viewerP->width))
pixx = viewerP->width - width;
if (pixx > 0)
pixx = 0;
}
if (viewerP->height > height)
pixy = (viewerP->height - height) / 2;
else {
if ((pixy < 0) && (pixy + height < viewerP->height))
pixy = viewerP->height - height;
if (pixy > 0)
pixy = 0;
}
XMoveWindow(viewerP->dispP, viewerP->imageWin, pixx, pixy);
*rxP = pixx;
*ryP = pixy;
}
static void
blitImage(XImageInfo * const ximageinfoP,
unsigned int const width,
unsigned int const height,
int const xArg,
int const yArg,
int const wArg,
int const hArg) {
int w, h;
int x, y;
w = MIN(wArg, width);
h = MIN(hArg, height);
x = xArg;
y = yArg;
if (x < 0) {
XClearArea(ximageinfoP->disp, ximageinfoP->drawable,
x, y, -x, h, False);
w -= (0 - x);
x = 0;
}
if (y < 0) {
XClearArea(ximageinfoP->disp, ximageinfoP->drawable,
x, y, w, -y, False);
h -= (0 - y);
y = 0;
}
if (x + w > width) {
XClearArea(ximageinfoP->disp, ximageinfoP->drawable,
x + width, y, x + w - width, h, False);
w -= x + w - width;
}
if (y + h > height) {
XClearArea(ximageinfoP->disp, ximageinfoP->drawable,
x, y + height, w, y + h - height, False);
h -= y + h - height;
}
sendXImage(ximageinfoP, x, y, x, y, w, h);
}
void
destroyViewer(viewer * const viewerP) {
/*----------------------------------------------------------------------------
Clean up static window.
-----------------------------------------------------------------------------*/
if (viewerP->pixmap != None)
XFreePixmap(viewerP->dispP, viewerP->pixmap);
if (viewerP->imageWin)
XDestroyWindow(viewerP->dispP, viewerP->imageWin);
viewerP->imageWin = 0;
if (viewerP->viewportWin)
XDestroyWindow(viewerP->dispP, viewerP->viewportWin);
viewerP->viewportWin = 0;
XFreeCursor(viewerP->dispP, viewerP->cursor);
free(viewerP);
}
static void
setViewportColormap(viewer * const viewerP,
Visual * const visualP) {
/*----------------------------------------------------------------------------
Set the colormap and WM_COLORMAP_WINDOWS properly for the viewport.
-----------------------------------------------------------------------------*/
static Atom cmap_atom = None;
XSetWindowAttributes swa;
Window cmap_windows[2];
if (cmap_atom == None)
cmap_atom = XInternAtom(viewerP->dispP, "WM_COLORMAP_WINDOWS", False);
/* If the visual we're using is the same as the default visual (used by
the viewport window) then we can set the viewport window to use the
image's colormap. This keeps most window managers happy.
*/
if (visualP == DefaultVisual(viewerP->dispP, viewerP->scrn)) {
swa.colormap = viewerP->imageColormap;
XChangeWindowAttributes(viewerP->dispP, viewerP->viewportWin,
CWColormap, &swa);
XDeleteProperty(viewerP->dispP, viewerP->viewportWin, cmap_atom);
} else {
/* Smart window managers can handle it when we use a different colormap
in our subwindow so long as we set the WM_COLORMAP_WINDOWS property
ala ICCCM.
*/
cmap_windows[0] = viewerP->imageWin;
cmap_windows[1] = viewerP->viewportWin;
XChangeProperty(viewerP->dispP, viewerP->viewportWin,
cmap_atom, XA_WINDOW, 32,
PropModeReplace, (unsigned char *)cmap_windows, 2);
}
}
static const char *
iconName(const char * const s) {
/*----------------------------------------------------------------------------
Return an icon name suitable for an image titled 's'.
s == NULL means untitled.
-----------------------------------------------------------------------------*/
const char * retval;
char buf[BUFSIZ];
if (!s)
STRSCPY(buf, "Unnamed");
else {
char * t;
STRSCPY(buf, s);
/* chop off stuff following 1st word. This strips
info added by processing functions too.
*/
t = strchr(buf, ' ');
if (t)
*t = '\0';
/* Strip off leading path. if you don't use unix-style paths,
You might want to change this.
*/
t= strrchr(buf, '/');
if (t) {
char * p;
for (p = buf, ++t; *t; ++p, ++t)
*p = *t;
*p = '\0';
}
/* Chop off any filename extension */
t = strchr(buf, '.');
if (t)
*t = '\0';
}
retval = strdup(buf);
if (retval == NULL)
pm_error("Out of memory");
return retval;
}
/* visual class to name table */
static struct visual_class_name {
int class; /* numerical value of class */
const char * name; /* actual name of class */
} const VisualClassName[] = {
{TrueColor, "TrueColor" } ,
{DirectColor, "DirectColor" } ,
{PseudoColor, "PseudoColor" } ,
{StaticColor, "StaticColor" } ,
{GrayScale, "GrayScale" } ,
{StaticGray, "StaticGray" } ,
{StaticGray, "StaticGrey" } ,
{-1, NULL }
};
int
visualClassFromName(const char * const name) {
unsigned int a;
int class;
bool found;
for (a = 0, found = FALSE; VisualClassName[a].name; ++a) {
if (strcaseeq(VisualClassName[a].name, name)) {
/* Check for uniqueness. We special-case StaticGray
because we have two spellings but they are unique if
we find either.
*/
if (found && class != StaticGray)
pm_error("'%s' does not uniquely describe a visual class",
name);
class = VisualClassName[a].class;
found = TRUE;
}
}
if (!found)
pm_error("'%s' is not a visual class name", name);
return class;
}
static const char *
nameOfVisualClass(int const class) {
unsigned int a;
const char * name;
bool found;
for (a = 0, found = FALSE; VisualClassName[a].name; ++a) {
if (VisualClassName[a].class == class) {
name = VisualClassName[a].name;
found = TRUE;
}
}
if (found)
return name;
else
return "[Unknown Visual Class]";
}
/* find the best visual of a particular class with a particular depth
*/
static Visual *
bestVisualOfClassAndDepth(Display * const disp,
int const scrn,
int const class,
unsigned int const depth) {
long const vinfoMask =
VisualScreenMask | VisualClassMask | VisualDepthMask;
Visual * best;
XVisualInfo template;
XVisualInfo * infoP;
unsigned int nvisuals;
best = NULL; /* initial value */
template.screen = scrn;
template.class = class;
template.depth = depth;
infoP = XGetVisualInfo(disp, vinfoMask, &template, (int*)&nvisuals);
if (infoP) {
/* There are visuals with this depth */
/* Not sure what to do if this gives more than one visual of a
particular class and depth, so just return the first one.
*/
best = infoP->visual;
XFree((char *)infoP);
}
return best;
}
static void
bestVisual(Display * const disp,
int const scrn,
Image * const imageP,
Visual ** const rvisualPP,
unsigned int * const rdepthP) {
/*----------------------------------------------------------------------------
Try to determine the best available visual to use for a particular
image
-----------------------------------------------------------------------------*/
unsigned int depth, a;
Screen * screen;
Visual * visualP;
Visual * default_visualP;
/* Figure out the best depth the server allows. note that some servers
(such as the HP 11.3 server) actually say they allow some depths but
have no visuals that allow that depth. Seems silly to me ...
*/
depth = 0;
screen = ScreenOfDisplay(disp, scrn);
for (a = 0; a < screen->ndepths; ++a) {
if (screen->depths[a].nvisuals &&
((!depth ||
((depth < imageP->depth) && (screen->depths[a].depth > depth)) ||
((screen->depths[a].depth >= imageP->depth) &&
(screen->depths[a].depth < depth)))))
depth = screen->depths[a].depth;
}
if (!depth) {
/* this shouldn't happen */
pm_message("bestVisual: didn't find any depths?!?");
depth = DefaultDepth(disp, scrn);
}
/* given this depth, find the best possible visual
*/
default_visualP = DefaultVisual(disp, scrn);
switch (imageP->type) {
case ITRUE: {
/* If the default visual is DirectColor or TrueColor prioritize such
that we use the default type if it exists at this depth
*/
if (default_visualP->class == TrueColor) {
visualP = bestVisualOfClassAndDepth(disp, scrn, TrueColor, depth);
if (!visualP)
visualP = bestVisualOfClassAndDepth(disp, scrn,
DirectColor, depth);
} else {
visualP = bestVisualOfClassAndDepth(disp, scrn, DirectColor,
depth);
if (!visualP)
visualP = bestVisualOfClassAndDepth(disp, scrn, TrueColor,
depth);
}
if (!visualP || ((depth <= 8) &&
bestVisualOfClassAndDepth(disp, scrn, PseudoColor,
depth)))
visualP = bestVisualOfClassAndDepth(disp, scrn, PseudoColor,
depth);
if (!visualP)
visualP = bestVisualOfClassAndDepth(disp, scrn, StaticColor,
depth);
if (!visualP)
visualP = bestVisualOfClassAndDepth(disp, scrn, GrayScale, depth);
if (!visualP)
visualP = bestVisualOfClassAndDepth(disp, scrn, StaticGray, depth);
} break;
case IRGB: {
/* if it's an RGB image, we want PseudoColor if we can get it */
visualP = bestVisualOfClassAndDepth(disp, scrn, PseudoColor, depth);
if (!visualP)
visualP = bestVisualOfClassAndDepth(disp, scrn, DirectColor,
depth);
if (!visualP)
visualP = bestVisualOfClassAndDepth(disp, scrn, TrueColor, depth);
if (!visualP)
visualP = bestVisualOfClassAndDepth(disp, scrn, StaticColor,
depth);
if (!visualP)
visualP = bestVisualOfClassAndDepth(disp, scrn, GrayScale, depth);
if (!visualP)
visualP = bestVisualOfClassAndDepth(disp, scrn, StaticGray, depth);
} break;
case IBITMAP: {
visualP = bestVisualOfClassAndDepth(disp, scrn, PseudoColor, depth);
if (!visualP)
visualP = bestVisualOfClassAndDepth(disp, scrn, StaticColor,
depth);
if (!visualP)
visualP = bestVisualOfClassAndDepth(disp, scrn, GrayScale, depth);
if (!visualP)
visualP = bestVisualOfClassAndDepth(disp, scrn, StaticGray, depth);
/* It seems pretty wasteful to use a TrueColor or DirectColor visual
to display a bitmap (2-color) image, so we look for those last
*/
if (!visualP)
visualP = bestVisualOfClassAndDepth(disp, scrn, DirectColor,
depth);
if (!visualP)
visualP = bestVisualOfClassAndDepth(disp, scrn, TrueColor, depth);
} break;
}
if (!visualP) { /* this shouldn't happen */
pm_message("bestVisual: couldn't find one?!?");
depth = DefaultDepth(disp, scrn);
visualP = DefaultVisual(disp, scrn);
}
*rvisualPP = visualP;
*rdepthP = depth;
}
static void
bestVisualOfClass(Display * const disp,
int const scrn,
Image * const image,
int const visual_class,
Visual ** const rvisual,
unsigned int * const rdepth) {
/*----------------------------------------------------------------------------
Given a visual class, try to find the best visual of that class at
the best depth. Return a null visual and depth if we couldn't find
any visual of that type at any depth.
-----------------------------------------------------------------------------*/
Visual *visual;
Screen *screen;
unsigned int a, b, depth;
/* loop through depths looking for a visual of a good depth which matches
* our visual class.
*/
screen= ScreenOfDisplay(disp, scrn);
visual= (Visual *)NULL;
depth= 0;
for (a= 0; a < screen->ndepths; a++) {
for (b= 0; b < screen->depths[a].nvisuals; b++) {
if ((screen->depths[a].visuals[b].class == visual_class) &&
(!depth ||
((depth < image->depth) && (screen->depths[a].depth > depth)) ||
((screen->depths[a].depth >= image->depth) &&
(screen->depths[a].depth < depth)))) {
depth= screen->depths[a].depth;
visual= &(screen->depths[a].visuals[b]);
}
}
}
*rvisual= visual;
*rdepth= depth;
}
static void
getImageDispDimensions(viewer * const viewerP,
Image * const imageP,
unsigned int * const widthP,
unsigned int * const heightP) {
if (viewerP->userChoseGeometry) {
*widthP = viewerP->width;
*heightP = viewerP->height;
} else {
unsigned int const displayWidth =
DisplayWidth(viewerP->dispP, viewerP->scrn);
unsigned int const displayHeight =
DisplayHeight(viewerP->dispP, viewerP->scrn);
/* We don't use more than 90% of display real estate unless user
explicitly asked for it.
*/
*widthP = MIN(imageP->width, (unsigned)(displayWidth * 0.9));
*heightP = MIN(imageP->height, (unsigned)(displayHeight * 0.9));
}
}
static void
getVisualAndDepth(Image * const imageP,
Display * const dispP,
int const scrn,
bool const fit,
bool const visualSpec,
unsigned int const visualClass,
Visual ** const visualPP,
unsigned int * const depthP) {
/* If the user told us to fit the colormap, we must use the default
visual.
*/
if (fit) {
*visualPP = DefaultVisual(dispP, scrn);
*depthP = DefaultDepth(dispP, scrn);
} else {
Visual * visualP;
unsigned int depth;
visualP = NULL;
if (!visualSpec) {
/* Try to pick the best visual for the image. */
bestVisual(dispP, scrn, imageP, &visualP, &depth);
} else {
/* Try to find a visual of the specified class */
bestVisualOfClass(dispP, scrn, imageP, visualClass,
&visualP, &depth);
if (!visualP) {
bestVisual(dispP, scrn, imageP, &visualP, &depth);
pm_message("Server does not provide %s visual, using %s",
nameOfVisualClass(visualClass),
nameOfVisualClass(visualP->class));
}
}
*visualPP = visualP;
*depthP = depth;
}
}
static void
setNormalSizeHints(viewer * const viewerP,
Image * const imageP) {
XSizeHints sh;
sh.width = viewerP->width;
sh.height = viewerP->height;
if (viewerP->fullscreen) {
sh.min_width = sh.max_width = viewerP->width;
sh.min_height = sh.max_height = viewerP->height;
} else {
sh.min_width = 1;
sh.min_height = 1;
sh.max_width = imageP->width;
sh.max_height = imageP->height;
}
sh.width_inc = 1;
sh.height_inc = 1;
sh.flags = PMinSize | PMaxSize | PResizeInc;
if (viewerP->userChoseGeometry)
sh.flags |= USSize;
else
sh.flags |= PSize;
if (viewerP->userChoseGeometry) {
sh.x = viewerP->xpos;
sh.y = viewerP->ypos;
sh.flags |= USPosition;
}
XSetNormalHints(viewerP->dispP, viewerP->viewportWin, &sh);
sh.min_width = sh.max_width;
sh.min_height = sh.max_height;
XSetNormalHints(viewerP->dispP, viewerP->imageWin, &sh);
/* Image doesn't shrink */
}
static void
setWMHints(viewer * const viewerP) {
XWMHints wmh;
wmh.input = TRUE;
wmh.flags = InputHint;
XSetWMHints(viewerP->dispP, viewerP->viewportWin, &wmh);
}
#define CTL_C '\003'
typedef enum exitReason {
EXIT_NONE,
EXIT_QUIT,
EXIT_WM_KILL,
EXIT_DESTROYED
} exitReason;
static void
run(viewer * const viewerP,
Image * const imageP,
XImageInfo * const ximageInfoP,
int const initPixx,
int const initPixy,
bool const install,
exitReason * const exitReasonP) {
int lastx, lasty;
int pixx, pixy;
union {
XEvent event;
XAnyEvent any;
XButtonEvent button;
XKeyEvent key;
XConfigureEvent configure;
XExposeEvent expose;
XMotionEvent motion;
XResizeRequestEvent resize;
XClientMessageEvent message;
} event;
exitReason exitReason;
lastx = lasty = -1;
pixx = initPixx;
pixy = initPixy;
exitReason = EXIT_NONE; /* No reason to exit yet */
while (exitReason == EXIT_NONE) {
XNextEvent(viewerP->dispP, &event.event);
switch (event.any.type) {
case ButtonPress:
if (event.button.button == 1) {
lastx = event.button.x;
lasty = event.button.y;
}
break;
case KeyPress: {
char buf[128];
KeySym ks;
XComposeStatus status;
Status rc;
rc = XLookupString(&event.key, buf, 128, &ks, &status);
if (rc == 1) {
char const ret = buf[0];
char const lowerRet = tolower(ret);
switch (lowerRet) {
case CTL_C:
case 'q':
exitReason = EXIT_QUIT;
break;
}
}
} break;
case MotionNotify: {
int mousex, mousey;
if (imageP->width <= viewerP->width &&
imageP->height <= viewerP->height) {
/* we're AT&T */
} else {
mousex = event.button.x;
mousey = event.button.y;
while (XCheckTypedEvent(viewerP->dispP, MotionNotify,
(XEvent*)&event)) {
mousex = event.button.x;
mousey = event.button.y;
}
pixx -= (lastx - mousex);
pixy -= (lasty - mousey);
lastx = mousex;
lasty = mousey;
placeImage(viewerP, imageP->width, imageP->height,
&pixx, &pixy);
}
} break;
case ConfigureNotify:
viewerP->width = event.configure.width;
viewerP->height = event.configure.height;
placeImage(viewerP, imageP->width, imageP->height,
&pixx, &pixy);
/* Configure the cursor to indicate which directions we can drag
*/
changeCursor(viewerP, imageP->width, imageP->height);
break;
case DestroyNotify:
exitReason = EXIT_DESTROYED;
break;
case Expose:
blitImage(ximageInfoP, imageP->width, imageP->height,
event.expose.x, event.expose.y,
event.expose.width, event.expose.height);
break;
case EnterNotify:
if (install)
XInstallColormap(viewerP->dispP, ximageInfoP->cmap);
break;
case LeaveNotify:
if (install)
XUninstallColormap(viewerP->dispP, ximageInfoP->cmap);
break;
case ClientMessage:
/* If we get a client message for the viewport window
which has the value of the delete atom, it means the
window manager wants us to die.
*/
if ((event.message.window == viewerP->viewportWin) &&
(event.message.data.l[0] == viewerP->deleteAtom)) {
exitReason = EXIT_WM_KILL;
}
break;
}
}
*exitReasonP = exitReason;
}
static int
retvalueFromExitReason(exitReason const exitReason) {
int retval;
switch (exitReason) {
case EXIT_NONE: assert(false); break;
case EXIT_QUIT: retval = 0; break;
case EXIT_WM_KILL: retval = 0; break;
case EXIT_DESTROYED: retval = 20; break;
}
return retval;
}
static void
resizeViewer(viewer * const viewerP,
unsigned int const newWidth,
unsigned int const newHeight) {
if (viewerP->width != newWidth || viewerP->height != newHeight) {
XResizeWindow(viewerP->dispP, viewerP->viewportWin,
newWidth, newHeight);
viewerP->width = newWidth;
viewerP->height = newHeight;
}
}
void
displayInViewer(viewer * const viewerP,
struct Image * const imageP,
bool const install,
bool const userWantsPrivateCmap,
bool const fit,
bool const userWantsPixmap,
bool const visualSpec,
unsigned int const visualClass,
const char * const title,
bool const verbose,
int * const retvalP) {
XImageInfo * ximageInfoP;
Visual * visual;
unsigned int depth;
bool privateCmap;
int pixx, pixy;
exitReason exitReason;
unsigned int windowWidth, windowHeight;
pixx = -1; pixy = -1; /* initial values */
getImageDispDimensions(viewerP, imageP, &windowWidth, &windowHeight);
resizeViewer(viewerP, windowWidth, windowHeight);
getVisualAndDepth(imageP, viewerP->dispP, viewerP->scrn,
fit, visualSpec, visualClass,
&visual, &depth);
if (verbose && (visual != DefaultVisual(viewerP->dispP, viewerP->scrn)))
pm_message("Using %s visual", nameOfVisualClass(visual->class));
/* If we're in slideshow mode and the user told us to fit the colormap,
free it here.
*/
if (viewerP->blank) {
/* For the first image we display we can use the default cmap.
subsequent images use a private colormap (unless they're
bitmaps) so we don't get color erosion when switching
images.
*/
if (fit) {
XDestroyWindow(viewerP->dispP, viewerP->imageWin);
viewerP->imageWin = 0;
viewerP->imageColormap = 0;
privateCmap = userWantsPrivateCmap;
} else if (!BITMAPP(imageP))
privateCmap = TRUE;
} else
privateCmap = userWantsPrivateCmap;
ximageInfoP = imageToXImage(viewerP->dispP, viewerP->scrn, visual, depth,
imageP, privateCmap, fit, verbose);
if (!ximageInfoP)
pm_error("INTERNAL ERROR: Cannot convert Image to XImage");
destroyImageWindow(viewerP);
createImageWindow(viewerP, ximageInfoP, imageP, visual,
userWantsPixmap, verbose);
if (title)
XStoreName(viewerP->dispP, viewerP->viewportWin, title);
else
XStoreName(viewerP->dispP, viewerP->viewportWin, "Unnamed");
{
const char * const name = iconName(title);
XSetIconName(viewerP->dispP, viewerP->viewportWin, name);
pm_strfree(name);
}
setNormalSizeHints(viewerP, imageP);
setWMHints(viewerP);
setViewportColormap(viewerP, visual);
/* Map (display) windows */
XMapWindow(viewerP->dispP, viewerP->imageWin);
XMapWindow(viewerP->dispP, viewerP->viewportWin);
/* Start displaying image */
placeImage(viewerP, imageP->width, imageP->height, &pixx, &pixy);
if (!viewerP->blank) {
XResizeWindow(viewerP->dispP, viewerP->imageWin,
imageP->width, imageP->height);
/* Clear the image window. Ask for exposure if there is no tile. */
XClearArea(viewerP->dispP, viewerP->imageWin, 0, 0, 0, 0,
(viewerP->pixmap == None));
viewerP->blank = FALSE;
}
changeCursor(viewerP, imageP->width, imageP->height);
/* Process X events, continuously */
run(viewerP, imageP, ximageInfoP, pixx, pixy, install, &exitReason);
freeXImage(imageP, ximageInfoP);
*retvalP = retvalueFromExitReason(exitReason);
}
|