-
Notifications
You must be signed in to change notification settings - Fork 257
/
Fl_x.cxx
3160 lines (2797 loc) · 96.8 KB
/
Fl_x.cxx
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
//
// "$Id$"
//
// X specific code for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2016 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// http://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
// http://www.fltk.org/str.php
//
#ifdef WIN32
//# include "Fl_win32.cxx"
#elif defined(__APPLE__)
//# include "Fl_mac.cxx" // now Fl_cocoa.mm
#elif !defined(FL_DOXYGEN)
# define CONSOLIDATE_MOTION 1
/**** Define this if your keyboard lacks a backspace key... ****/
/* #define BACKSPACE_HACK 1 */
# include <config.h>
# include <FL/Fl.H>
# include <FL/x.H>
# include <FL/Fl_Window.H>
# include <FL/fl_utf8.h>
# include <FL/Fl_Tooltip.H>
# include <FL/fl_draw.H>
# include <FL/Fl_Paged_Device.H>
# include <FL/Fl_Shared_Image.H>
# include <FL/fl_ask.H>
# include <FL/filename.H>
# include <stdio.h>
# include <stdlib.h>
# include "flstring.h"
# include <unistd.h>
# include <time.h>
# include <sys/time.h>
# include <X11/Xmd.h>
# include <X11/Xlocale.h>
# include <X11/Xlib.h>
# include <X11/keysym.h>
# include "Xutf8.h"
#define USE_XRANDR (HAVE_DLSYM && HAVE_DLFCN_H) // means attempt to dynamically load libXrandr.so
#if USE_XRANDR
#include <dlfcn.h>
#define RRScreenChangeNotifyMask (1L << 0) // from X11/extensions/Xrandr.h
#define RRScreenChangeNotify 0 // from X11/extensions/Xrandr.h
typedef int (*XRRUpdateConfiguration_type)(XEvent *event);
static XRRUpdateConfiguration_type XRRUpdateConfiguration_f;
static int randrEventBase; // base of RandR-defined events
#endif
# if HAVE_XFIXES
# include <X11/extensions/Xfixes.h>
static int xfixes_event_base = 0;
static bool have_xfixes = false;
# endif
# include <X11/cursorfont.h>
# if HAVE_XCURSOR
# include <X11/Xcursor/Xcursor.h>
# endif
# if HAVE_XRENDER
# include <X11/extensions/Xrender.h>
# endif
////////////////////////////////////////////////////////////////
// interface to poll/select call:
# if USE_POLL
# include <poll.h>
static pollfd *pollfds = 0;
# else
# if HAVE_SYS_SELECT_H
# include <sys/select.h>
# endif /* HAVE_SYS_SELECT_H */
// The following #define is only needed for HP-UX 9.x and earlier:
//#define select(a,b,c,d,e) select((a),(int *)(b),(int *)(c),(int *)(d),(e))
static fd_set fdsets[3];
static int maxfd;
# define POLLIN 1
# define POLLOUT 4
# define POLLERR 8
# endif /* USE_POLL */
static int nfds = 0;
static int fd_array_size = 0;
struct FD {
# if !USE_POLL
int fd;
short events;
# endif
void (*cb)(int, void*);
void* arg;
};
static FD *fd = 0;
void Fl::add_fd(int n, int events, void (*cb)(int, void*), void *v) {
remove_fd(n,events);
int i = nfds++;
if (i >= fd_array_size) {
FD *temp;
fd_array_size = 2*fd_array_size+1;
if (!fd) temp = (FD*)malloc(fd_array_size*sizeof(FD));
else temp = (FD*)realloc(fd, fd_array_size*sizeof(FD));
if (!temp) return;
fd = temp;
# if USE_POLL
pollfd *tpoll;
if (!pollfds) tpoll = (pollfd*)malloc(fd_array_size*sizeof(pollfd));
else tpoll = (pollfd*)realloc(pollfds, fd_array_size*sizeof(pollfd));
if (!tpoll) return;
pollfds = tpoll;
# endif
}
fd[i].cb = cb;
fd[i].arg = v;
# if USE_POLL
pollfds[i].fd = n;
pollfds[i].events = events;
# else
fd[i].fd = n;
fd[i].events = events;
if (events & POLLIN) FD_SET(n, &fdsets[0]);
if (events & POLLOUT) FD_SET(n, &fdsets[1]);
if (events & POLLERR) FD_SET(n, &fdsets[2]);
if (n > maxfd) maxfd = n;
# endif
}
void Fl::add_fd(int n, void (*cb)(int, void*), void* v) {
Fl::add_fd(n, POLLIN, cb, v);
}
void Fl::remove_fd(int n, int events) {
int i,j;
# if !USE_POLL
maxfd = -1; // recalculate maxfd on the fly
# endif
for (i=j=0; i<nfds; i++) {
# if USE_POLL
if (pollfds[i].fd == n) {
int e = pollfds[i].events & ~events;
if (!e) continue; // if no events left, delete this fd
pollfds[j].events = e;
}
# else
if (fd[i].fd == n) {
int e = fd[i].events & ~events;
if (!e) continue; // if no events left, delete this fd
fd[i].events = e;
}
if (fd[i].fd > maxfd) maxfd = fd[i].fd;
# endif
// move it down in the array if necessary:
if (j<i) {
fd[j] = fd[i];
# if USE_POLL
pollfds[j] = pollfds[i];
# endif
}
j++;
}
nfds = j;
# if !USE_POLL
if (events & POLLIN) FD_CLR(n, &fdsets[0]);
if (events & POLLOUT) FD_CLR(n, &fdsets[1]);
if (events & POLLERR) FD_CLR(n, &fdsets[2]);
# endif
}
void Fl::remove_fd(int n) {
remove_fd(n, -1);
}
extern int fl_send_system_handlers(void *e);
#if CONSOLIDATE_MOTION
static Fl_Window* send_motion;
extern Fl_Window* fl_xmousewin;
#endif
static bool in_a_window; // true if in any of our windows, even destroyed ones
static void do_queued_events() {
in_a_window = true;
while (XEventsQueued(fl_display,QueuedAfterReading)) {
XEvent xevent;
XNextEvent(fl_display, &xevent);
if (fl_send_system_handlers(&xevent))
continue;
fl_handle(xevent);
}
// we send FL_LEAVE only if the mouse did not enter some other window:
if (!in_a_window) Fl::handle(FL_LEAVE, 0);
#if CONSOLIDATE_MOTION
else if (send_motion == fl_xmousewin) {
send_motion = 0;
Fl::handle(FL_MOVE, fl_xmousewin);
}
#endif
}
// these pointers are set by the Fl::lock() function:
static void nothing() {}
void (*fl_lock_function)() = nothing;
void (*fl_unlock_function)() = nothing;
// This is never called with time_to_wait < 0.0:
// It should return negative on error, 0 if nothing happens before
// timeout, and >0 if any callbacks were done.
int fl_wait(double time_to_wait) {
// OpenGL and other broken libraries call XEventsQueued
// unnecessarily and thus cause the file descriptor to not be ready,
// so we must check for already-read events:
if (fl_display && XQLength(fl_display)) {do_queued_events(); return 1;}
# if !USE_POLL
fd_set fdt[3];
fdt[0] = fdsets[0];
fdt[1] = fdsets[1];
fdt[2] = fdsets[2];
# endif
int n;
fl_unlock_function();
if (time_to_wait < 2147483.648) {
# if USE_POLL
n = ::poll(pollfds, nfds, int(time_to_wait*1000 + .5));
# else
timeval t;
t.tv_sec = int(time_to_wait);
t.tv_usec = int(1000000 * (time_to_wait-t.tv_sec));
n = ::select(maxfd+1,&fdt[0],&fdt[1],&fdt[2],&t);
# endif
} else {
# if USE_POLL
n = ::poll(pollfds, nfds, -1);
# else
n = ::select(maxfd+1,&fdt[0],&fdt[1],&fdt[2],0);
# endif
}
fl_lock_function();
if (n > 0) {
for (int i=0; i<nfds; i++) {
# if USE_POLL
if (pollfds[i].revents) fd[i].cb(pollfds[i].fd, fd[i].arg);
# else
int f = fd[i].fd;
short revents = 0;
if (FD_ISSET(f,&fdt[0])) revents |= POLLIN;
if (FD_ISSET(f,&fdt[1])) revents |= POLLOUT;
if (FD_ISSET(f,&fdt[2])) revents |= POLLERR;
if (fd[i].events & revents) fd[i].cb(f, fd[i].arg);
# endif
}
}
return n;
}
// fl_ready() is just like fl_wait(0.0) except no callbacks are done:
int fl_ready() {
if (XQLength(fl_display)) return 1;
if (!nfds) return 0; // nothing to select or poll
# if USE_POLL
return ::poll(pollfds, nfds, 0);
# else
timeval t;
t.tv_sec = 0;
t.tv_usec = 0;
fd_set fdt[3];
fdt[0] = fdsets[0];
fdt[1] = fdsets[1];
fdt[2] = fdsets[2];
return ::select(maxfd+1,&fdt[0],&fdt[1],&fdt[2],&t);
# endif
}
// replace \r\n by \n
static void convert_crlf(unsigned char *string, long& len) {
unsigned char *a, *b;
a = b = string;
while (*a) {
if (*a == '\r' && a[1] == '\n') { a++; len--; }
else *b++ = *a++;
}
*b = 0;
}
////////////////////////////////////////////////////////////////
Display *fl_display;
Window fl_message_window = 0;
int fl_screen;
XVisualInfo *fl_visual;
Colormap fl_colormap;
static XIM fl_xim_im = 0;
XIC fl_xim_ic = 0;
static Window fl_xim_win = 0;
static char fl_is_over_the_spot = 0;
static XRectangle status_area;
static Atom WM_DELETE_WINDOW;
static Atom WM_PROTOCOLS;
static Atom fl_MOTIF_WM_HINTS;
static Atom TARGETS;
static Atom CLIPBOARD;
static Atom TIMESTAMP;
static Atom PRIMARY_TIMESTAMP;
static Atom CLIPBOARD_TIMESTAMP;
Atom fl_XdndAware;
Atom fl_XdndSelection;
Atom fl_XdndEnter;
Atom fl_XdndTypeList;
Atom fl_XdndPosition;
Atom fl_XdndLeave;
Atom fl_XdndDrop;
Atom fl_XdndStatus;
Atom fl_XdndActionCopy;
Atom fl_XdndFinished;
//Atom fl_XdndProxy;
Atom fl_XdndURIList;
static Atom fl_Xatextplainutf;
static Atom fl_Xatextplainutf2; // STR#2930
static Atom fl_Xatextplain;
static Atom fl_XaText;
static Atom fl_XaCompoundText;
Atom fl_XaUtf8String;
static Atom fl_XaTextUriList;
static Atom fl_XaImageBmp;
static Atom fl_XaImagePNG;
static Atom fl_INCR;
static Atom fl_NET_WM_PID;
static Atom fl_NET_WM_NAME; // utf8 aware window label
static Atom fl_NET_WM_ICON_NAME; // utf8 aware window icon name
static Atom fl_NET_SUPPORTING_WM_CHECK;
static Atom fl_NET_WM_STATE;
static Atom fl_NET_WM_STATE_FULLSCREEN;
static Atom fl_NET_WM_FULLSCREEN_MONITORS;
static Atom fl_NET_WORKAREA;
static Atom fl_NET_WM_ICON;
static Atom fl_NET_ACTIVE_WINDOW;
/*
X defines 32-bit-entities to have a format value of max. 32,
although sizeof(atom) can be 8 (64 bits) on a 64-bit OS.
See also fl_open_display() for sizeof(atom) < 4.
Used for XChangeProperty (see STR #2419).
*/
static int atom_bits = 32;
static void fd_callback(int,void *) {
do_queued_events();
}
extern "C" {
static int io_error_handler(Display*) {
Fl::fatal("X I/O error");
return 0;
}
static int xerror_handler(Display* d, XErrorEvent* e) {
char buf1[128], buf2[128];
sprintf(buf1, "XRequest.%d", e->request_code);
XGetErrorDatabaseText(d,"",buf1,buf1,buf2,128);
XGetErrorText(d, e->error_code, buf1, 128);
Fl::warning("%s: %s 0x%lx", buf2, buf1, e->resourceid);
return 0;
}
}
extern char *fl_get_font_xfld(int fnum, int size);
static void fl_new_ic()
{
XVaNestedList preedit_attr = NULL;
XVaNestedList status_attr = NULL;
static XFontSet fs = NULL;
char *fnt;
char **missing_list = 0;
int missing_count = 0;
char *def_string;
static XRectangle spot;
int predit = 0;
int sarea = 0;
XIMStyles* xim_styles = NULL;
#if USE_XFT
#if defined(__GNUC__)
// FIXME: warning XFT support here
#endif /*__GNUC__*/
if (!fs) {
fnt = (char*)"-misc-fixed-*";
fs = XCreateFontSet(fl_display, fnt, &missing_list,
&missing_count, &def_string);
}
#else
if (!fs) {
bool must_free_fnt = true;
fnt = fl_get_font_xfld(0, 14);
if (!fnt) {fnt = (char*)"-misc-fixed-*";must_free_fnt=false;}
fs = XCreateFontSet(fl_display, fnt, &missing_list,
&missing_count, &def_string);
if (must_free_fnt) free(fnt);
}
#endif
if (missing_list) XFreeStringList(missing_list);
preedit_attr = XVaCreateNestedList(0,
XNSpotLocation, &spot,
XNFontSet, fs, NULL);
status_attr = XVaCreateNestedList(0,
XNAreaNeeded, &status_area,
XNFontSet, fs, NULL);
if (!XGetIMValues(fl_xim_im, XNQueryInputStyle,
&xim_styles, NULL, NULL)) {
int i;
XIMStyle *style;
for (i = 0, style = xim_styles->supported_styles;
i < xim_styles->count_styles; i++, style++) {
if (*style == (XIMPreeditPosition | XIMStatusArea)) {
sarea = 1;
predit = 1;
} else if (*style == (XIMPreeditPosition | XIMStatusNothing)) {
predit = 1;
}
}
}
XFree(xim_styles);
if (sarea) {
fl_xim_ic = XCreateIC(fl_xim_im,
XNInputStyle, (XIMPreeditPosition | XIMStatusArea),
XNPreeditAttributes, preedit_attr,
XNStatusAttributes, status_attr,
NULL);
}
if (!fl_xim_ic && predit) {
fl_xim_ic = XCreateIC(fl_xim_im,
XNInputStyle, (XIMPreeditPosition | XIMStatusNothing),
XNPreeditAttributes, preedit_attr,
NULL);
}
XFree(preedit_attr);
XFree(status_attr);
if (!fl_xim_ic) {
fl_is_over_the_spot = 0;
fl_xim_ic = XCreateIC(fl_xim_im,
XNInputStyle, (XIMPreeditNothing | XIMStatusNothing),
NULL);
} else {
fl_is_over_the_spot = 1;
XVaNestedList status_attr = NULL;
status_attr = XVaCreateNestedList(0, XNAreaNeeded, &status_area, NULL);
XGetICValues(fl_xim_ic, XNStatusAttributes, status_attr, NULL);
XFree(status_attr);
}
}
static XRectangle spot;
static int spotf = -1;
static int spots = -1;
void fl_reset_spot(void)
{
spot.x = -1;
spot.y = -1;
//if (fl_xim_ic) XUnsetICFocus(fl_xim_ic);
}
void fl_set_spot(int font, int size, int X, int Y, int W, int H, Fl_Window *win)
{
int change = 0;
XVaNestedList preedit_attr;
static XFontSet fs = NULL;
char **missing_list;
int missing_count;
char *def_string;
char *fnt = NULL;
bool must_free_fnt =true;
static XIC ic = NULL;
if (!fl_xim_ic || !fl_is_over_the_spot) return;
//XSetICFocus(fl_xim_ic);
if (X != spot.x || Y != spot.y) {
spot.x = X;
spot.y = Y;
spot.height = H;
spot.width = W;
change = 1;
}
if (font != spotf || size != spots) {
spotf = font;
spots = size;
change = 1;
if (fs) {
XFreeFontSet(fl_display, fs);
}
#if USE_XFT
#if defined(__GNUC__)
// FIXME: warning XFT support here
#endif /*__GNUC__*/
fnt = NULL; // fl_get_font_xfld(font, size);
if (!fnt) {fnt = (char*)"-misc-fixed-*";must_free_fnt=false;}
fs = XCreateFontSet(fl_display, fnt, &missing_list,
&missing_count, &def_string);
#else
fnt = fl_get_font_xfld(font, size);
if (!fnt) {fnt = (char*)"-misc-fixed-*";must_free_fnt=false;}
fs = XCreateFontSet(fl_display, fnt, &missing_list,
&missing_count, &def_string);
#endif
}
if (fl_xim_ic != ic) {
ic = fl_xim_ic;
change = 1;
}
if (fnt && must_free_fnt) free(fnt);
if (!change) return;
preedit_attr = XVaCreateNestedList(0,
XNSpotLocation, &spot,
XNFontSet, fs, NULL);
XSetICValues(fl_xim_ic, XNPreeditAttributes, preedit_attr, NULL);
XFree(preedit_attr);
}
void fl_set_status(int x, int y, int w, int h)
{
XVaNestedList status_attr;
status_area.x = x;
status_area.y = y;
status_area.width = w;
status_area.height = h;
if (!fl_xim_ic) return;
status_attr = XVaCreateNestedList(0, XNArea, &status_area, NULL);
XSetICValues(fl_xim_ic, XNStatusAttributes, status_attr, NULL);
XFree(status_attr);
}
static void fl_init_xim() {
static int xim_warning = 2;
if (xim_warning > 0) xim_warning--;
//XIMStyle *style;
XIMStyles *xim_styles;
if (!fl_display) return;
if (fl_xim_im) return;
fl_xim_im = XOpenIM(fl_display, NULL, NULL, NULL);
xim_styles = NULL;
fl_xim_ic = NULL;
if (fl_xim_im) {
XGetIMValues (fl_xim_im, XNQueryInputStyle,
&xim_styles, NULL, NULL);
} else {
if (xim_warning)
Fl::warning("XOpenIM() failed");
// if xim_styles is allocated, free it now
if (xim_styles) XFree(xim_styles);
return;
}
if (xim_styles && xim_styles->count_styles) {
fl_new_ic();
} else {
if (xim_warning)
Fl::warning("No XIM style found");
XCloseIM(fl_xim_im);
fl_xim_im = NULL;
// if xim_styles is allocated, free it now
if (xim_styles) XFree(xim_styles);
return;
}
if (!fl_xim_ic) {
if (xim_warning)
Fl::warning("XCreateIC() failed");
XCloseIM(fl_xim_im);
fl_xim_im = NULL;
}
// if xim_styles is still allocated, free it now
if(xim_styles) XFree(xim_styles);
}
void fl_xim_deactivate(void);
void fl_xim_activate(Window xid) {
if (!fl_xim_im)
return;
// If the focused window has changed, then use the brute force method
// of completely recreating the input context.
if (fl_xim_win != xid) {
fl_xim_deactivate();
fl_new_ic();
fl_xim_win = xid;
XSetICValues(fl_xim_ic,
XNFocusWindow, fl_xim_win,
XNClientWindow, fl_xim_win,
NULL);
}
fl_set_spot(spotf, spots, spot.x, spot.y, spot.width, spot.height);
}
void fl_xim_deactivate(void) {
if (!fl_xim_ic)
return;
XDestroyIC(fl_xim_ic);
fl_xim_ic = NULL;
fl_xim_win = 0;
}
void Fl::enable_im() {
Fl_Window *win;
win = Fl::first_window();
if (win && win->shown()) {
fl_xim_activate(fl_xid(win));
XSetICFocus(fl_xim_ic);
} else {
fl_new_ic();
}
}
void Fl::disable_im() {
fl_xim_deactivate();
}
void fl_open_display() {
if (fl_display) return;
setlocale(LC_CTYPE, "");
XSetLocaleModifiers("@im=");
XSetIOErrorHandler(io_error_handler);
XSetErrorHandler(xerror_handler);
Display *d = XOpenDisplay(0);
if (!d) Fl::fatal("Can't open display: %s",XDisplayName(0));
fl_open_display(d);
}
void fl_open_display(Display* d) {
fl_display = d;
WM_DELETE_WINDOW = XInternAtom(d, "WM_DELETE_WINDOW", 0);
WM_PROTOCOLS = XInternAtom(d, "WM_PROTOCOLS", 0);
fl_MOTIF_WM_HINTS = XInternAtom(d, "_MOTIF_WM_HINTS", 0);
TARGETS = XInternAtom(d, "TARGETS", 0);
CLIPBOARD = XInternAtom(d, "CLIPBOARD", 0);
TIMESTAMP = XInternAtom(d, "TIMESTAMP", 0);
PRIMARY_TIMESTAMP = XInternAtom(d, "PRIMARY_TIMESTAMP", 0);
CLIPBOARD_TIMESTAMP = XInternAtom(d, "CLIPBOARD_TIMESTAMP", 0);
fl_XdndAware = XInternAtom(d, "XdndAware", 0);
fl_XdndSelection = XInternAtom(d, "XdndSelection", 0);
fl_XdndEnter = XInternAtom(d, "XdndEnter", 0);
fl_XdndTypeList = XInternAtom(d, "XdndTypeList", 0);
fl_XdndPosition = XInternAtom(d, "XdndPosition", 0);
fl_XdndLeave = XInternAtom(d, "XdndLeave", 0);
fl_XdndDrop = XInternAtom(d, "XdndDrop", 0);
fl_XdndStatus = XInternAtom(d, "XdndStatus", 0);
fl_XdndActionCopy = XInternAtom(d, "XdndActionCopy", 0);
fl_XdndFinished = XInternAtom(d, "XdndFinished", 0);
//fl_XdndProxy = XInternAtom(d, "XdndProxy", 0);
fl_XdndEnter = XInternAtom(d, "XdndEnter", 0);
fl_XdndURIList = XInternAtom(d, "text/uri-list", 0);
fl_Xatextplainutf = XInternAtom(d, "text/plain;charset=UTF-8",0);
fl_Xatextplainutf2 = XInternAtom(d, "text/plain;charset=utf-8",0); // Firefox/Thunderbird needs this - See STR#2930
fl_Xatextplain = XInternAtom(d, "text/plain", 0);
fl_XaText = XInternAtom(d, "TEXT", 0);
fl_XaCompoundText = XInternAtom(d, "COMPOUND_TEXT", 0);
fl_XaUtf8String = XInternAtom(d, "UTF8_STRING", 0);
fl_XaTextUriList = XInternAtom(d, "text/uri-list", 0);
fl_XaImageBmp = XInternAtom(d, "image/bmp", 0);
fl_XaImagePNG = XInternAtom(d, "image/png", 0);
fl_INCR = XInternAtom(d, "INCR", 0);
fl_NET_WM_PID = XInternAtom(d, "_NET_WM_PID", 0);
fl_NET_WM_NAME = XInternAtom(d, "_NET_WM_NAME", 0);
fl_NET_WM_ICON_NAME = XInternAtom(d, "_NET_WM_ICON_NAME", 0);
fl_NET_SUPPORTING_WM_CHECK = XInternAtom(d, "_NET_SUPPORTING_WM_CHECK", 0);
fl_NET_WM_STATE = XInternAtom(d, "_NET_WM_STATE", 0);
fl_NET_WM_STATE_FULLSCREEN = XInternAtom(d, "_NET_WM_STATE_FULLSCREEN", 0);
fl_NET_WM_FULLSCREEN_MONITORS = XInternAtom(d, "_NET_WM_FULLSCREEN_MONITORS", 0);
fl_NET_WORKAREA = XInternAtom(d, "_NET_WORKAREA", 0);
fl_NET_WM_ICON = XInternAtom(d, "_NET_WM_ICON", 0);
fl_NET_ACTIVE_WINDOW = XInternAtom(d, "_NET_ACTIVE_WINDOW", 0);
if (sizeof(Atom) < 4)
atom_bits = sizeof(Atom) * 8;
Fl::add_fd(ConnectionNumber(d), POLLIN, fd_callback);
fl_screen = DefaultScreen(d);
fl_message_window =
XCreateSimpleWindow(d, RootWindow(d,fl_screen), 0,0,1,1,0, 0, 0);
// construct an XVisualInfo that matches the default Visual:
XVisualInfo templt; int num;
templt.visualid = XVisualIDFromVisual(DefaultVisual(d, fl_screen));
fl_visual = XGetVisualInfo(d, VisualIDMask, &templt, &num);
fl_colormap = DefaultColormap(d, fl_screen);
fl_init_xim();
#if !USE_COLORMAP
Fl::visual(FL_RGB);
#endif
#if HAVE_XFIXES
int error_base;
if (XFixesQueryExtension(fl_display, &xfixes_event_base, &error_base))
have_xfixes = true;
else
have_xfixes = false;
#endif
#if USE_XRANDR
void *libxrandr_addr = dlopen("libXrandr.so.2", RTLD_LAZY);
if (!libxrandr_addr) libxrandr_addr = dlopen("libXrandr.so", RTLD_LAZY);
if (libxrandr_addr) {
int error_base;
typedef Bool (*XRRQueryExtension_type)(Display*, int*, int*);
typedef void (*XRRSelectInput_type)(Display*, Window, int);
XRRQueryExtension_type XRRQueryExtension_f = (XRRQueryExtension_type)dlsym(libxrandr_addr, "XRRQueryExtension");
XRRSelectInput_type XRRSelectInput_f = (XRRSelectInput_type)dlsym(libxrandr_addr, "XRRSelectInput");
XRRUpdateConfiguration_f = (XRRUpdateConfiguration_type)dlsym(libxrandr_addr, "XRRUpdateConfiguration");
if (XRRQueryExtension_f && XRRSelectInput_f && XRRQueryExtension_f(d, &randrEventBase, &error_base))
XRRSelectInput_f(d, RootWindow(d, fl_screen), RRScreenChangeNotifyMask);
else XRRUpdateConfiguration_f = NULL;
}
#endif
// Listen for changes to _NET_WORKAREA
XSelectInput(d, RootWindow(d, fl_screen), PropertyChangeMask);
}
void fl_close_display() {
Fl::remove_fd(ConnectionNumber(fl_display));
XCloseDisplay(fl_display);
}
static int fl_workarea_xywh[4] = { -1, -1, -1, -1 };
static void fl_init_workarea() {
fl_open_display();
Atom actual;
unsigned long count, remaining;
int format;
long *xywh = 0;
/* If there are several screens, the _NET_WORKAREA property
does not give the work area of the main screen, but that of all screens together.
Therefore, we use this property only when there is a single screen,
and fall back to the main screen full area when there are several screens.
*/
if (Fl::screen_count() > 1 || XGetWindowProperty(fl_display, RootWindow(fl_display, fl_screen),
fl_NET_WORKAREA, 0, 4, False,
XA_CARDINAL, &actual, &format, &count, &remaining,
(unsigned char **)&xywh) || !xywh || !xywh[2] ||
!xywh[3])
{
Fl::screen_xywh(fl_workarea_xywh[0],
fl_workarea_xywh[1],
fl_workarea_xywh[2],
fl_workarea_xywh[3], 0);
}
else
{
fl_workarea_xywh[0] = (int)xywh[0];
fl_workarea_xywh[1] = (int)xywh[1];
fl_workarea_xywh[2] = (int)xywh[2];
fl_workarea_xywh[3] = (int)xywh[3];
}
if ( xywh ) { XFree(xywh); xywh = 0; }
}
int Fl::x() {
if (fl_workarea_xywh[0] < 0) fl_init_workarea();
return fl_workarea_xywh[0];
}
int Fl::y() {
if (fl_workarea_xywh[0] < 0) fl_init_workarea();
return fl_workarea_xywh[1];
}
int Fl::w() {
if (fl_workarea_xywh[0] < 0) fl_init_workarea();
return fl_workarea_xywh[2];
}
int Fl::h() {
if (fl_workarea_xywh[0] < 0) fl_init_workarea();
return fl_workarea_xywh[3];
}
void Fl::get_mouse(int &xx, int &yy) {
fl_open_display();
Window root = RootWindow(fl_display, fl_screen);
Window c; int mx,my,cx,cy; unsigned int mask;
XQueryPointer(fl_display,root,&root,&c,&mx,&my,&cx,&cy,&mask);
xx = mx;
yy = my;
}
////////////////////////////////////////////////////////////////
// Code used for paste and DnD into the program:
Fl_Widget *fl_selection_requestor;
char *fl_selection_buffer[2];
int fl_selection_length[2];
const char * fl_selection_type[2];
int fl_selection_buffer_length[2];
char fl_i_own_selection[2] = {0,0};
// Call this when a "paste" operation happens:
void Fl::paste(Fl_Widget &receiver, int clipboard, const char *type) {
if (fl_i_own_selection[clipboard]) {
// We already have it, do it quickly without window server.
// Notice that the text is clobbered if set_selection is
// called in response to FL_PASTE!
// However, for now, we only paste text in this function
if (fl_selection_type[clipboard] != Fl::clipboard_plain_text) return; //TODO: allow copy/paste of image within same app
Fl::e_text = fl_selection_buffer[clipboard];
Fl::e_length = fl_selection_length[clipboard];
if (!Fl::e_text) Fl::e_text = (char *)"";
receiver.handle(FL_PASTE);
return;
}
// otherwise get the window server to return it:
fl_selection_requestor = &receiver;
Atom property = clipboard ? CLIPBOARD : XA_PRIMARY;
Fl::e_clipboard_type = type;
XConvertSelection(fl_display, property, TARGETS, property,
fl_xid(Fl::first_window()), fl_event_time);
}
int Fl::clipboard_contains(const char *type)
{
XEvent event;
Atom actual; int format; unsigned long count, remaining, i = 0;
unsigned char* portion = NULL;
Fl_Window *win = Fl::first_window();
if (!win || !fl_xid(win)) return 0;
XConvertSelection(fl_display, CLIPBOARD, TARGETS, CLIPBOARD, fl_xid(win), CurrentTime);
XFlush(fl_display);
do {
XNextEvent(fl_display, &event);
if (event.type == SelectionNotify && event.xselection.property == None) return 0;
i++;
}
while (i < 50 && event.type != SelectionNotify);
if (i >= 50) return 0;
XGetWindowProperty(fl_display,
event.xselection.requestor,
event.xselection.property,
0, 4000, 0, 0,
&actual, &format, &count, &remaining, &portion);
if (actual != XA_ATOM) return 0;
Atom t;
int retval = 0;
if (strcmp(type, Fl::clipboard_plain_text) == 0) {
for (i = 0; i<count; i++) { // searching for text data
t = ((Atom*)portion)[i];
if (t == fl_Xatextplainutf ||
t == fl_Xatextplainutf2 ||
t == fl_Xatextplain ||
t == fl_XaUtf8String) {
retval = 1;
break;
}
}
}
else if (strcmp(type, Fl::clipboard_image) == 0) {
for (i = 0; i<count; i++) { // searching for image data
t = ((Atom*)portion)[i];
if (t == fl_XaImageBmp || t == fl_XaImagePNG) {
retval = 1;
break;
}
}
}
XFree(portion);
return retval;
}
static Window fl_dnd_source_window;
static Atom *fl_dnd_source_types; // null-terminated list of data types being supplied
static Atom fl_dnd_type;
static Atom fl_dnd_source_action;
static Atom fl_dnd_action;
void fl_sendClientMessage(Window window, Atom message,
unsigned long d0,
unsigned long d1=0,
unsigned long d2=0,
unsigned long d3=0,
unsigned long d4=0)
{
XEvent e;
e.xany.type = ClientMessage;
e.xany.window = window;
e.xclient.message_type = message;
e.xclient.format = 32;
e.xclient.data.l[0] = (long)d0;
e.xclient.data.l[1] = (long)d1;
e.xclient.data.l[2] = (long)d2;
e.xclient.data.l[3] = (long)d3;
e.xclient.data.l[4] = (long)d4;
XSendEvent(fl_display, window, 0, 0, &e);
}
/*
Get window property value (32 bit format)
Returns zero on success, -1 on error
'data' should be freed with XFree() using this pattern:
unsigned long *data = 0;
if (0 == get_xwinprop(....., &nitems, &data) ) { ..success.. }
else { ..fail.. }
if ( data ) { XFree(data); data=0; }
Note: 'data' can be non-zero, even if the return value is -1 (error) and
should hence be XFree'd *after* the if/else statement, as described above.
*/
static int get_xwinprop(Window wnd, Atom prop, long max_length,
unsigned long *nitems, unsigned long **data) {
Atom actual;
int format;
unsigned long bytes_after;
if (Success != XGetWindowProperty(fl_display, wnd, prop, 0, max_length,
False, AnyPropertyType, &actual, &format,
nitems, &bytes_after, (unsigned char**)data)) {
return -1;
}
if (actual == None || format != 32) {
return -1;
}
return 0;
}
////////////////////////////////////////////////////////////////
// Code for copying to clipboard and DnD out of the program:
void Fl::copy(const char *stuff, int len, int clipboard, const char *type) {
if (!stuff || len<0) return;
if (clipboard >= 2) {
copy(stuff, len, 0, type);
copy(stuff, len, 1, type);