-
Notifications
You must be signed in to change notification settings - Fork 109
/
sqUnixX11.c
7635 lines (6854 loc) · 230 KB
/
sqUnixX11.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
/* vim: set ts=8 : */
/* sqUnixX11.c -- support for display via the X Window System.
*
* Copyright (C) 1996-2008 by Ian Piumarta and other authors/contributors
* listed elsewhere in this file.
* All rights reserved.
*
* This file is part of Unix Squeak.
*
* 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 or substantial portions of the Software.
*
* 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 OR COPYRIGHT HOLDERS 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.
*/
/* Author: Ian Piumarta <ian.piumarta@squeakland.org>
*
* Support for more intelligent CLIPBOARD selection handling contributed by:
* Ned Konz <ned@bike-nomad.com>
*
* Support for displays deeper than 8 bits contributed by: Kazuki YASUMATSU
* <kyasu@crl.fujixerox.co.jp> <Kazuki.Yasumatsu@fujixerox.co.jp>
*
* Support for cursor and keypad editing keys based on code contributed by:
* Stefan Matthias Aust <sma@kiel.netsurf.de>
*
* Support for intelligent visual class selection contributed by:
* Bill Cattey <wdc@MIT.EDU>
*
* Support for European accented characters in selections, and
* Support for displays shallower than 8 bits contributed, and
* Support for browser plugins, and
* Support for accelerated OpenGL contributed by:
* Bert Freudenberg <bert@freudenbergs.de>
*
* Support for 24bpp TrueColour X display devices contributed by:
* Tim Rowledge <tim@sumeru.stanford.edu>
*
* Support for OSProcess plugin contributed by:
* Dave Lewis <lewis@mail.msen.com> Mon Oct 18 20:36:54 EDT 1999
*/
#include "sq.h"
#include "sqMemoryAccess.h"
#include "sqUnixMain.h"
#include "sqUnixGlobals.h"
#include "sqUnixCharConv.h"
#include "sqaio.h"
#undef HAVE_OPENGL_GL_H /* don't include Quartz OpenGL if configured */
#include "SqDisplay.h"
#if defined(ENABLE_FAST_BLT)
/* XXX referring to plugin variables *requires* BitBitPlugin to be included by VMM as an internal plugin */
# if defined(__arm__)
# include "../../../Cross/plugins/BitBltPlugin/BitBltArm.h"
# else
# error configuration error
# endif
#endif
#if defined(ioMSecs)
# undef ioMSecs
#endif
#define NO_ICON
#define PRINT_PS_FORMS
#define SQ_FORM_FILENAME "squeak-form.ppm"
#undef FULL_UPDATE_ON_EXPOSE
#if 0 /* The following is a pain. Leave it to the command line. */
# undef DEBUG_FOCUS
# undef DEBUG_XIM
# undef DEBUG_CONV
# undef DEBUG_EVENTS
# undef DEBUG_SELECTIONS
# undef DEBUG_BROWSER
# undef DEBUG_WINDOW
# undef DEBUG_VISUAL
#endif
#define USE_XICFONT_OPTION
#undef USE_XICFONT_RESOURCE
#undef USE_XICFONT_DEFAULT
#if defined(HAVE_LIBXEXT)
# define USE_XSHM
#endif
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <locale.h>
#if defined(HAVE_SYS_SELECT_H)
# include <sys/select.h>
#endif
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#define XK_MISCELLANY
#define XK_XKB_KEYS
#include <X11/keysym.h> /* /not/ keysymdef.h */
#if defined(SUGAR)
# include <X11/XF86keysym.h>
#endif
#if defined(USE_XSHM)
# include <sys/ipc.h>
# include <sys/shm.h>
# include <X11/extensions/XShm.h>
#endif
#if defined(HAVE_LIBXRENDER)
# include <X11/extensions/Xrender.h>
#endif
#if !defined(NO_ICON)
# include "squeakIcon.bitmap"
#endif
#if defined(HAVE_DLFCN_H)
# include <dlfcn.h>
#endif
#include <assert.h>
#define isAligned(T, V) (((V) % sizeof(T)) == 0)
#define align(T, V) (((V) / sizeof(T)) * sizeof(T))
/*** Variables -- Imported from Virtual Machine ***/
/*** Variables -- X11 Related ***/
/* name of Squeak windows in Xrm and the WM */
#ifdef PharoVM
# define xResClass "pharo-vm"
# define xResName "Pharo"
#else
# define xResClass "Squeak"
# define xResName "squeak"
#endif
# define VMOPTION(arg) "-"arg
char *displayName= 0; /* name of display, or 0 for $DISPLAY */
Display *stDisplay= null; /* Squeak display */
int isConnectedToXServer=0;/* True when connected to an X server */
int stXfd= -1; /* X connection file descriptor */
Window stParent= null; /* Squeak parent window */
Window stWindow= null; /* Squeak window */
int stWidth= 0;
int stHeight= 0;
int xWidth= 0;
int xHeight= 0;
Visual *stVisual; /* the default visual */
GC stGC; /* graphics context used for rendering */
Colormap stColormap= null; /* Squeak color map */
int scrW= 0;
int scrH= 0;
XImage *stImage= 0; /* ...and it's client-side pixmap */
char *stEmptySelection= ""; /* immutable "empty string" value */
char *stPrimarySelection; /* buffer holding selection */
int stPrimarySelectionSize;/* size of buffer holding selection */
int stOwnsSelection= 0; /* true if we own the X selection */
int stOwnsClipboard= 0; /* true if we own the X clipboard */
int usePrimaryFirst= 0; /* true if we should look to PRIMARY before CLIPBOARD */
Time stSelectionTime; /* Time of setting the selection */
Atom stSelectionName= None; /* None or XdndSelection */
Atom stSelectionType= None; /* type to send selection (multiple types should be supported) */
XColor stColorBlack; /* black pixel value in stColormap */
XColor stColorWhite; /* white pixel value in stColormap */
int savedWindowOrigin= -1; /* initial origin of window */
#define SELECTION_ATOM_COUNT 10
/* http://www.freedesktop.org/standards/clipboards-spec/clipboards.txt */
Atom selectionAtoms[SELECTION_ATOM_COUNT];
char *selectionAtomNames[SELECTION_ATOM_COUNT]= {
"CLIPBOARD",
#define xaClipboard selectionAtoms[0]
"CUT_BUFFER0",
#define xaCutBuffer0 selectionAtoms[1]
"TARGETS",
#define xaTargets selectionAtoms[2]
"MULTIPLE",
#define xaMultiple selectionAtoms[3]
"UTF8_STRING",
#define xaUTF8String selectionAtoms[4]
"COMPOUND_TEXT",
#define xaCompoundText selectionAtoms[5]
"TIMESTAMP",
#define xaTimestamp selectionAtoms[6]
"SQUEAK_SELECTION", /* used for XGetSelectionOwner() data */
#define selectionAtom selectionAtoms[7]
"INCR",
#define xaINCR selectionAtoms[8]
"XdndSelection",
#define xaXdndSelection selectionAtoms[9]
};
Atom wmProtocolsAtom; /* for window deletion messages */
Atom wmDeleteWindowAtom;
#if defined(USE_XSHM)
XShmSegmentInfo stShmInfo; /* shared memory descriptor */
int completions= 0; /* outstanding completion events */
int completionType; /* the type of XShmCompletionEvent */
int useXshm= 0; /* 1 if shared memory is in use */
int asyncUpdate= 0; /* 1 for asynchronous screen updates */
#endif
int mapDelBs= 0; /* 1 to map delete to backspace */
int optMapIndex= 0; /* Option key modifier map index */
int cmdMapIndex= 0; /* Command key modifier map index */
int stDepth= 0;
int stBitsPerPixel= 0;
unsigned int stColors[256];
unsigned int stDownGradingColors[256];
int stHasSameRGBMask16;
int stHasSameRGBMask32;
int stRNMask, stGNMask, stBNMask;
int stRShift, stGShift, stBShift;
char *stDisplayBitmap= 0;
Window browserWindow= 0; /* parent window */
int browserPipes[]= {-1, -1}; /* read/write fd for browser communication */
int headless= 0;
int useXdnd= 1; /* true if we should handle XDND protocol messages */
#if defined(SUGAR)
char *sugarBundleId= 0;
char *sugarActivityId= 0;
#endif
typedef int (*x2sqKey_t)(XKeyEvent *xevt, KeySym *symbolic);
static int x2sqKeyPlain(XKeyEvent *xevt, KeySym *symbolic);
static int x2sqKeyInput(XKeyEvent *xevt, KeySym *symbolic);
static int x2sqKeyCompositionInput(XKeyEvent *xevt, KeySym *symbolic);
static x2sqKey_t x2sqKey= x2sqKeyPlain;
static int multi_key_pressed = 0;
static KeySym multi_key_buffer = 0;
static int compositionInput = 0;
/* #define INIT_INPUT_WHEN_KEY_PRESSED */
/* #define INIT_INPUT_WHEN_FOCUSED_IN */
/* #define INIT_INPUT_WHEN_MAPPED */
#if defined(INIT_INPUT_WHEN_KEY_PRESSED)
# undef INIT_INPUT_WHEN_FOCUSED_IN
# undef INIT_INPUT_WHEN_MAPPED
#elif defined(INIT_INPUT_WHEN_FOCUSED_IN)
# undef INIT_INPUT_WHEN_MAPPED
#elif !defined(INIT_INPUT_WHEN_MAPPED)
# define INIT_INPUT_WHEN_MAPPED
#endif
#if defined(USE_XICFONT_RESOURCE)
# include <X11/Xresource.h>
# define xicFontResClass "XIC.FontSet"
# define xicFontResName "xic.fontSet"
#elif defined(USE_XICFONT_DEFAULT)
# define xicFontDefRes "fontSet"
#endif
#define xicDefaultFont "-*-*-medium-r-normal--*"
#if defined(USE_XICFONT_OPTION)
static char *inputFontStr= xicDefaultFont;
#endif
static XFontSet inputFont= NULL;
static XIMStyle inputStyle;
static XIC inputContext= 0;
static XPoint inputSpot= {0, 0};
static unsigned char inputString[128];
static unsigned char *inputBuf= inputString;
static unsigned char *pendingKey= NULL;
static int inputCount= 0;
/* static int inputSymbol= 0; */
static void initInputI18n();
#if !defined(INIT_INPUT_WHEN_KEY_PRESSED)
static void initInputNone();
static void (*initInput)()= initInputNone;
#endif
#define inBrowser() (-1 != browserPipes[0])
/* window states */
#define WIN_NORMAL 0
#define WIN_CHANGED 1
#define WIN_ZOOMED 2
int windowState= WIN_CHANGED;
#define noteWindowChange() \
{ \
if (windowState == WIN_NORMAL) \
windowState= WIN_CHANGED; \
}
#define recordKeystroke(ignored) 0
#include "sqUnixEvent.c"
#ifdef DEBUG_CONV
# define DCONV_PRINTF(...) printf(__VA_ARGS__)
# define DCONV_FPRINTF(...) fprintf(stderr,__VA_ARGS__)
#else
# define DCONV_PRINTF(...) 0
# define DCONV_FPRINTF(...) 0
#endif
#define SqueakWhite 0
#define SqueakBlack 1
int sleepWhenUnmapped= 0;
int noTitle= 0;
int fullScreen= 0;
int fullScreenDirect= 0;
int iconified= 0;
int withSpy= 0;
/*xxx REMOVE REFS TO THESE IN sqUnixSound*.* */
void feedback(int offset, int pixel) {}
int inModalLoop= 0, dpyPitch= 0, dpyPixels= 0;
/* we are interested in these events...
*/
#define EVENTMASK ButtonPressMask | ButtonReleaseMask | \
KeyPressMask | KeyReleaseMask | PointerMotionMask | \
ExposureMask | VisibilityChangeMask | FocusChangeMask
#define WM_EVENTMASK StructureNotifyMask | FocusChangeMask
/* largest X selection that we will attempt to handle (bytes) */
#define MAX_SELECTION_SIZE 100*1024
/* longest we're prepared to wait for the selection owner to convert it (seconds) */
#define SELECTION_TIMEOUT 3
/* To coordinate default window title with dndLaunchFile */
static char *defaultWindowLabel = shortImageName;
static long launchDropTimeoutMsecs = 1000; /* 1 second default launch drop timeout */
/*** Functions ***/
static void xHandler(int fd, void *data, int flags);
static void npHandler(int fd, void *data, int flags);
static void handleEvent(XEvent *event);
static int handleEvents(void);
static void waitForCompletions(void);
static Time getXTimestamp(void);
static void claimSelection(void);
#if defined(DEBUG_SELECTIONS)
static void printAtomName(Atom atom);
#endif
void setWindowSize(void);
void getMaskbit(unsigned long ul, int *nmask, int *shift);
void initDownGradingColors(void);
void copyReverseImageBytes(int *fromImageData, int *toImageData, int depth, int width, int height,
int affectedL, int affectedT, int affectedR, int affectedB);
void copyReverseImageWords(int *fromImageData, int *toImageData, int depth, int width, int height,
int affectedL, int affectedT, int affectedR, int affectedB);
#define declareCopyFunction(NAME) \
void NAME (int *fromImageData, int *toImageData, int width, int height, \
int affectedL, int affectedT, int affectedR, int affectedB)
declareCopyFunction(copyImage1To8);
declareCopyFunction(copyImage1To16);
declareCopyFunction(copyImage1To24);
declareCopyFunction(copyImage1To32);
declareCopyFunction(copyImage2To8);
declareCopyFunction(copyImage2To16);
declareCopyFunction(copyImage2To24);
declareCopyFunction(copyImage2To32);
declareCopyFunction(copyImage4To8);
declareCopyFunction(copyImage4To16);
declareCopyFunction(copyImage4To24);
declareCopyFunction(copyImage4To32);
declareCopyFunction(copyImage8To8);
declareCopyFunction(copyImage8To16);
declareCopyFunction(copyImage8To24);
declareCopyFunction(copyImage8To32);
declareCopyFunction(copyImage16To8);
declareCopyFunction(copyImage16To16);
declareCopyFunction(copyImage16To24);
declareCopyFunction(copyImage16To32);
declareCopyFunction(copyImage32To8);
declareCopyFunction(copyImage32To16);
declareCopyFunction(copyImage32To24);
declareCopyFunction(copyImage32To32);
declareCopyFunction(copyImage32To32Same);
#undef declareCopyFunction
static void redrawDisplay(int l, int r, int t, int b);
/* Selection functions */
typedef struct _SelectionChunk
{
unsigned char *data;
size_t size;
struct _SelectionChunk *next;
struct _SelectionChunk *last;
} SelectionChunk;
static int sendSelection(XSelectionRequestEvent *requestEv, int isMultiple);
static void getSelection(void);
static char *getSelectionData(Atom selection, Atom target, size_t *bytes);
static char *getSelectionFrom(Atom source);
static int translateCode(KeySym symbolic, int *modp, XKeyEvent *evt);
#if defined(USE_XSHM)
int XShmGetEventBase(Display *);
#endif
void browserProcessCommand(void); /* see sqUnixMozilla.c */
static inline int min(int x, int y) { return (x < y) ? x : y; }
#if 0
/* Conversion table from X to Squeak (reversible) */
static unsigned char X_to_Squeak[256] =
{
0, 1, 2, 3, 4, 5, 6, 7, /* 0 - 7 */
8, 9, 13, 11, 12, 10, 14, 15, /* 8 - 15 */
16, 17, 18, 19, 20, 21, 22, 23, /* 16 - 23 */
24, 25, 26, 27, 28, 29, 30, 31, /* 24 - 31 */
32, 33, 34, 35, 36, 37, 38, 39, /* 32 - 39 */
40, 41, 42, 43, 44, 45, 46, 47, /* 40 - 47 */
48, 49, 50, 51, 52, 53, 54, 55, /* 48 - 55 */
56, 57, 58, 59, 60, 61, 62, 63, /* 56 - 63 */
64, 65, 66, 67, 68, 69, 70, 71, /* 64 - 71 */
72, 73, 74, 75, 76, 77, 78, 79, /* 72 - 79 */
80, 81, 82, 83, 84, 85, 86, 87, /* 80 - 87 */
88, 89, 90, 91, 92, 93, 94, 95, /* 88 - 95 */
96, 97, 98, 99, 100, 101, 102, 103, /* 96 - 103 */
104, 105, 106, 107, 108, 109, 110, 111, /* 104 - 111 */
112, 113, 114, 115, 116, 117, 118, 119, /* 112 - 119 */
120, 121, 122, 123, 124, 125, 126, 127, /* 120 - 127 */
196, 197, 165, 201, 209, 247, 220, 225, /* 128 - 135 */
224, 226, 228, 227, 198, 176, 170, 248, /* 136 - 143 */
213, 206, 195, 207, 211, 212, 210, 219, /* 144 - 151 */
218, 221, 246, 245, 250, 249, 251, 252, /* 152 - 159 */
160, 193, 162, 163, 223, 180, 182, 164, /* 160 - 167 */
172, 169, 187, 199, 194, 173, 168, 255, /* 168 - 175 */
161, 177, 178, 179, 171, 181, 166, 183, /* 176 - 183 */
184, 185, 188, 200, 186, 189, 202, 192, /* 184 - 191 */
203, 231, 229, 204, 128, 129, 174, 130, /* 192 - 199 */
233, 131, 230, 232, 237, 234, 235, 236, /* 200 - 207 */
208, 132, 241, 238, 239, 205, 133, 215, /* 208 - 215 */
175, 244, 242, 243, 134, 217, 222, 167, /* 216 - 223 */
136, 135, 137, 139, 138, 140, 190, 141, /* 224 - 231 */
143, 142, 144, 145, 147, 146, 148, 149, /* 232 - 239 */
240, 150, 152, 151, 153, 155, 154, 214, /* 240 - 247 */
191, 157, 156, 158, 159, 253, 254, 216, /* 248 - 255 */
};
unsigned char Squeak_to_X[256];
void initCharmap(void)
{
int i;
for(i= 0; i < 256; i++)
Squeak_to_X[X_to_Squeak[i]]= i;
}
void st2ux(unsigned char *string)
{
if (!string) return;
while (*string)
{
*string= Squeak_to_X[*string];
string++;
}
}
void ux2st(unsigned char *string)
{
if (!string) return;
while (*string)
{
*string= X_to_Squeak[*string];
string++;
}
}
#endif
/*** X-related Functions ***/
/* Called prior to forking a squeak session.
*/
int synchronizeXDisplay(void)
{
if (isConnectedToXServer)
XSync(stDisplay, False);
return 0;
}
static Bool timestampPredicate(Display *dpy, XEvent *evt, XPointer arg)
{
return (( (PropertyNotify == evt->type))
&& (stWindow == evt->xproperty.window)
&& (xaTimestamp == evt->xproperty.atom)
&& (PropertyNewValue == evt->xproperty.state))
? True : False;
}
/* answer the real current server time */
static Time getXTimestamp(void)
{
unsigned char dummy;
XEvent evt;
XWindowAttributes xwa;
XGetWindowAttributes(stDisplay, stWindow, &xwa);
XSelectInput(stDisplay, stWindow, xwa.your_event_mask | PropertyChangeMask);
XChangeProperty(stDisplay, stWindow, xaTimestamp, XA_INTEGER, 8, PropModeAppend, &dummy, 0);
XIfEvent(stDisplay, &evt, timestampPredicate, 0);
XSelectInput(stDisplay, stWindow, xwa.your_event_mask);
return evt.xproperty.time;
}
#if defined(DEBUG_VISUAL)
static char *debugVisual(int x)
{
switch (x)
{
case 0: return "StaticGray";
case 1: return "GrayScale";
case 2: return "StaticColor";
case 3: return "PseudoColor";
case 4: return "TrueColor";
case 5: return "DirectColor";
default: return "Invalid";
}
}
#endif
static void noteResize(int w, int h)
{
xWidth= w;
xHeight= h;
#if defined(USE_XSHM)
if (asyncUpdate)
waitForCompletions();
#endif
noteWindowChange();
}
static int resized(void)
{
return ((stWidth != xWidth) || (stHeight != xHeight));
}
/*** selection handling ***/
#if defined(DEBUG_SELECTIONS)
static void dumpSelectionData(const char *data, int n, int newline)
{
if (NULL == data)
fprintf(stderr, "dumpSelectionData: data is NULL\n");
else
{
for (n= min(30, n); n > 0; --n)
{
unsigned char c= (unsigned char)*data++;
fprintf(stderr, (0x20 <= c && c <= 0x7e) ? "%c" : "<%02x>", c);
}
if (newline)
fprintf(stderr, "\n");
}
}
/* this is needed because the atom names must be freed */
static void printAtomName(Atom atom)
{
if (None == atom)
{
fprintf(stderr, "None");
}
else
{
char *atomName= XGetAtomName(stDisplay, atom);
fprintf(stderr, "%s", atomName);
XFree((void *)atomName);
}
}
#endif
static int allocateSelectionBuffer(int count)
{
/*if (count + 1 > stPrimarySelectionSize)*/ /* XXX test removed for dnd out; maybe should be left in? XXX */
{
if (stPrimarySelection != stEmptySelection)
{
free(stPrimarySelection);
stPrimarySelection= stEmptySelection;
stPrimarySelectionSize= 0;
}
if (!(stPrimarySelection= (char *)malloc(count + 1)))
{
fprintf(stderr, "failed to allocate X selection buffer\n");
stPrimarySelection= stEmptySelection;
stPrimarySelectionSize= 0;
return 0;
}
stPrimarySelectionSize= count;
}
return 1;
}
/* answers true if selection could be handled */
static int sendSelection(XSelectionRequestEvent *requestEv, int isMultiple)
{
int xError= 0;
XSelectionEvent notifyEv;
Atom targetProperty= ((None == requestEv->property)
? requestEv->target
: requestEv->property);
/* XSelectionRequestEvent is used for both clipboard and Xdnd. In
* the case of Xdnd, XSelectionEvent is answered asynchronously
* after the image prepares data because target (data type) is
* informed only when the SelectionRequest is sent.
* dndOutSelectionRequest() sends a SQDragRequest event to the image
* for that. Finally, the image calls
* HandMorph>>primitiveDndOutSend: to send the SelectionRequest.
*/
if (xaXdndSelection == requestEv->selection) return 0;
notifyEv.property= targetProperty;
#if defined(DEBUG_SELECTIONS)
fprintf(stderr, "%d selection request sel ", isMultiple);
printAtomName(requestEv->selection);
fprintf(stderr, " prop ");
printAtomName(requestEv->property);
fprintf(stderr, " target ");
printAtomName(requestEv->target);
fprintf(stderr, "\n");
#endif
if ((XA_STRING == requestEv->target) || (xaUTF8String == requestEv->target))
{
int len= strlen(stPrimarySelection);
char *buf= (char *)malloc(len * 3 + 1);
int n;
# if defined(DEBUG_SELECTIONS)
fprintf(stderr, "sendSelection: len=%d, sel=", len);
dumpSelectionData(stPrimarySelection, len, 1);
# endif
if (xaUTF8String == requestEv->target)
n= sq2uxUTF8(stPrimarySelection, len, buf, len * 3 + 1, 1);
else
n= sq2uxText(stPrimarySelection, len, buf, len * 3 + 1, 1);
# if defined(DEBUG_SELECTIONS)
fprintf(stderr, "sendSelection: n=%d, buf=", n);
dumpSelectionData(buf, n, 1);
# endif
XChangeProperty(requestEv->display, requestEv->requestor,
targetProperty, requestEv->target,
8, PropModeReplace, (const unsigned char *)buf, n);
free(buf);
}
else if ((stSelectionType == requestEv->target) && (None != stSelectionType))
{
/* In case of type other than image/png */
XChangeProperty(requestEv->display, requestEv->requestor,
targetProperty, requestEv->target,
8, PropModeReplace,
(const unsigned char *) stPrimarySelection,
stPrimarySelectionSize);
}
else if (xaTargets == requestEv->target)
{
/* If we don't report COMPOUND_TEXT in this list, KMail (and maybe other
* Qt/KDE apps) don't accept pastes from Squeak. Of course, they'll use
* UTF8_STRING anyway... */
Atom targets[7];
int targetsSize= 6;
targets[0]= xaTargets;
targets[1]= xaMultiple;
targets[2]= xaTimestamp; /* required by ICCCM */
targets[3]= xaUTF8String;
targets[4]= XA_STRING;
targets[5]= xaCompoundText;
if (stSelectionType != None)
{
targetsSize += 1;
targets[6]= stSelectionType;
}
xError= XChangeProperty(requestEv->display, requestEv->requestor,
targetProperty, XA_ATOM,
32, PropModeReplace, (unsigned char *)targets, targetsSize);
}
else if (xaCompoundText == requestEv->target)
{
/* COMPOUND_TEXT is handled here for older clients that don't handle UTF-8 */
XTextProperty textProperty;
char *list[]= { stPrimarySelection, NULL };
if (localeEncoding == sqTextEncoding)
xError= XmbTextListToTextProperty(requestEv->display, list, 1, XCompoundTextStyle, &textProperty);
# if defined(X_HAVE_UTF8_STRING)
else if (uxUTF8Encoding == sqTextEncoding)
xError= Xutf8TextListToTextProperty(requestEv->display, list, 1, XCompoundTextStyle, &textProperty);
# endif
else
{
int len= strlen(stPrimarySelection);
char *buf= (char *)malloc(len * 3 + 1);
list[0]= buf;
sq2uxText(stPrimarySelection, len, buf, len * 3 + 1, 1);
xError= XmbTextListToTextProperty(requestEv->display, list, 1, XCompoundTextStyle, &textProperty);
free(buf);
}
if (Success == xError)
{
xError= XChangeProperty(requestEv->display, requestEv->requestor,
targetProperty, xaCompoundText,
8, PropModeReplace, textProperty.value, textProperty.nitems);
XFree((void *)textProperty.value);
}
else
{
fprintf(stderr, "XmbTextListToTextProperty returns %d\n", xError);
notifyEv.property= None;
}
}
else if (xaTimestamp == requestEv->target)
{
xError= XChangeProperty(requestEv->display, requestEv->requestor,
targetProperty, XA_INTEGER,
32, PropModeReplace, (unsigned char *)&stSelectionTime, 1);
}
else if (xaMultiple == requestEv->target)
{
/* The ICCCM requires MULTIPLE, but I'm not sure who sends it. */
if (None == requestEv->property)
notifyEv.property= None; /* illegal request */
else
{
Atom* multipleAtoms= NULL;
int format;
Atom type;
unsigned long numberOfItems, bytesAfter;
xError= XGetWindowProperty(requestEv->display,
requestEv->requestor,
requestEv->property,
0, 100, False,
AnyPropertyType, /* XA_ATOM */
&type, &format,
&numberOfItems,
&bytesAfter,
(unsigned char **)&multipleAtoms);
if ((xError != Success) || (bytesAfter != 0)
|| (format != 32) || (type == None))
{
notifyEv.property= None;
}
else
{
unsigned long i;
for (i= 0; i < numberOfItems; i+= 2)
{
XSelectionRequestEvent individualRequestEv;
memcpy(&individualRequestEv, requestEv, sizeof(XSelectionRequestEvent));
individualRequestEv.target= multipleAtoms[i];
individualRequestEv.property= multipleAtoms[i+1];
if (individualRequestEv.target == None)
{
multipleAtoms[i+1]= None;
}
else
{
/* call this function recursively for each target/property pair */
if (!sendSelection(&individualRequestEv, i/2 + 1))
{
multipleAtoms[i+1]= None;
}
}
}
}
}
}
else
{
notifyEv.property= None; /* couldn't handle it */
}
#if defined(DEBUG_SELECTIONS)
if (xError == BadAlloc || xError == BadAtom || xError == BadMatch
|| xError == BadValue || xError == BadWindow)
fprintf(stderr, "sendSelection: XChangeProperty err %d\n", xError);
#endif
/* on MULTIPLE requests, we notify only once */
if (!isMultiple)
{
notifyEv.type= SelectionNotify;
notifyEv.display= requestEv->display;
notifyEv.requestor= requestEv->requestor;
notifyEv.selection= requestEv->selection;
notifyEv.target= requestEv->target;
/* notifyEv.property set above */
notifyEv.time= requestEv->time;
notifyEv.send_event= True;
XSendEvent(requestEv->display, requestEv->requestor, False, 0, (XEvent *)¬ifyEv);
XFlush(stDisplay);
}
return notifyEv.property != None;
}
static void getSelection(void)
{
char *data;
if (usePrimaryFirst)
{
data= getSelectionFrom(XA_PRIMARY); /* try PRIMARY first */
if (stEmptySelection == data)
data= getSelectionFrom(xaClipboard); /* then try CLIPBOARD (TODO CUT_BUFFER0?) */
}
else
{
data= getSelectionFrom(xaClipboard); /* try clipboard first */
if (stEmptySelection == data)
data= getSelectionFrom(XA_PRIMARY); /* then try PRIMARY */
}
}
static char *getSelectionFrom(Atom source)
{
char * data= NULL;
size_t bytes= 0;
/* request the selection */
Atom target= textEncodingUTF8 ? xaUTF8String : (localeEncoding ? xaCompoundText : XA_STRING);
data= getSelectionData(source, target, &bytes);
if (bytes == 0)
return stEmptySelection;
/* convert the encoding if necessary */
if (bytes && allocateSelectionBuffer(bytes))
{
if (textEncodingUTF8)
bytes= ux2sqUTF8(data, bytes, stPrimarySelection, bytes + 1, 1);
else if (localeEncoding)
{
char **strList= NULL;
int i, n, s= 0;
XTextProperty textProperty;
textProperty.encoding= xaCompoundText;
textProperty.format= 8;
textProperty.value= data;
textProperty.nitems= bytes;
# if defined(X_HAVE_UTF8_STRING)
if (uxUTF8Encoding == sqTextEncoding)
Xutf8TextPropertyToTextList(stDisplay, &textProperty, &strList, &n);
else
# endif
XmbTextPropertyToTextList(stDisplay, &textProperty, &strList, &n);
for (i= 0; i < n; ++i)
s+= strlen(strList[i]);
if (s > bytes)
{
bytes= min(s, MAX_SELECTION_SIZE - 1);
if (! allocateSelectionBuffer(bytes))
{
# if defined(DEBUG_SELECTIONS)
fprintf(stderr, "no bytes\n");
# endif
goto nobytes;
}
}
if ((localeEncoding == sqTextEncoding)
# if defined(X_HAVE_UTF8_STRING)
|| (uxUTF8Encoding == sqTextEncoding)
# endif
)
{
strcpy(stPrimarySelection, strList[0]);
for (i= 1; i < n; ++i)
strcat(stPrimarySelection, strList[i]);
}
else
{
char *to= stPrimarySelection;
for (i= 0; i < n - 1; ++i)
{
s= strlen(strList[i]);
s= ux2sqText(strList[i], s, to, bytes, 0);
bytes -= s;
to += s;
}
s= strlen(strList[n - 1]);
s= ux2sqText(strList[n - 1], s, to, bytes + 1, 1);
}
if (strList)
XFreeStringList(strList);
/* translate LF -> CR */
for (i= 0; stPrimarySelection[i] != '\0'; ++i)
if ('\n' == stPrimarySelection[i])
stPrimarySelection[i]= '\r';
}
else
bytes= ux2sqText(data, bytes, stPrimarySelection, bytes + 1, 1);
/* wrong type check was omitted */
}
else
{
# if defined(DEBUG_SELECTIONS)
fprintf(stderr, "no bytes\n");
# endif
}
nobytes:
# if defined(DEBUG_SELECTIONS)
fprintf(stderr, "selection=");
dumpSelectionData(stPrimarySelection, bytes, 1);
# endif
XFree((void *)data);
return stPrimarySelection;
}
/* Wait specific event to get property change.
* Return 1 if success.
*/