forked from rdesktop/rdesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xwin.c
5064 lines (4381 loc) · 117 KB
/
xwin.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
/* -*- c-basic-offset: 8 -*-
rdesktop: A Remote Desktop Protocol client.
User interface services - X Window System
Copyright (C) Matthew Chapman <matthewc.unsw.edu.au> 1999-2008
Copyright 2007-2008 Pierre Ossman <ossman@cendio.se> for Cendio AB
Copyright 2002-2011 Peter Astrand <astrand@cendio.se> for Cendio AB
Copyright 2012-2018 Henrik Andersson <hean01@cendio.se> for Cendio AB
Copyright 2017-2019 Alexander Zakharov <uglym8@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xproto.h>
#include <assert.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <errno.h>
#include <strings.h>
#include "rdesktop.h"
#include "xproto.h"
#include <X11/Xcursor/Xcursor.h>
#ifdef HAVE_XRANDR
#include <X11/extensions/Xrandr.h>
#endif
#ifdef __APPLE__
#include <sys/param.h>
#define HOST_NAME_MAX MAXHOSTNAMELEN
#endif
#ifdef __sun
#include <netdb.h>
#define HOST_NAME_MAX MAXHOSTNAMELEN
#endif
#ifdef __FreeBSD__
#define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
#endif
extern RD_BOOL g_user_quit;
extern RD_BOOL g_exit_mainloop;
extern window_size_type_t g_window_size_type;
extern uint32 g_requested_session_width;
extern uint32 g_requested_session_height;
extern uint16 g_session_width;
extern uint16 g_session_height;
extern int g_xpos;
extern int g_ypos;
extern int g_pos;
extern RD_BOOL g_sendmotion;
extern RD_BOOL g_fullscreen;
extern RD_BOOL g_grab_keyboard;
extern RD_BOOL g_hide_decorations;
extern RD_BOOL g_pending_resize;
extern RD_BOOL g_pending_resize_defer;
extern struct timeval g_pending_resize_defer_timer;
extern char g_title[];
extern char g_seamless_spawn_cmd[];
/* Color depth of the RDP session.
As of RDP 5.1, it may be 8, 15, 16 or 24. */
extern int g_server_depth;
extern int g_win_button_size;
/* This is a timer used to rate limit actual resizing */
static struct timeval g_resize_timer = { 0 };
Display *g_display;
Time g_last_gesturetime;
static int g_x_socket;
static Screen *g_screen;
Window g_wnd;
static RD_BOOL g_has_wm = False;
RD_BOOL g_dynamic_session_resize = True;
/* These are the last known window sizes. They are updated whenever the window size is changed. */
static uint32 g_window_width;
static uint32 g_window_height;
/* SeamlessRDP support */
typedef struct _seamless_group
{
Window wnd;
unsigned long id;
unsigned int refcnt;
} seamless_group;
typedef struct _seamless_window
{
Window wnd;
unsigned long id;
unsigned long behind;
seamless_group *group;
int xoffset, yoffset;
int width, height;
int state; /* normal/minimized/maximized. */
unsigned int desktop;
struct timeval *position_timer;
RD_BOOL outstanding_position;
unsigned int outpos_serial;
int outpos_xoffset, outpos_yoffset;
int outpos_width, outpos_height;
unsigned int icon_size;
unsigned int icon_offset;
char icon_buffer[32 * 32 * 4];
struct _seamless_window *next;
} seamless_window;
static seamless_window *g_seamless_windows = NULL;
static unsigned long g_seamless_focused = 0;
static RD_BOOL g_seamless_started = False; /* Server end is up and running */
RD_BOOL g_seamless_active = False; /* We are currently in seamless mode */
static RD_BOOL g_seamless_hidden = False; /* Desktop is hidden on server */
static RD_BOOL g_seamless_broken_restack = False; /* WM does not properly restack */
extern RD_BOOL g_seamless_rdp;
extern RD_BOOL g_seamless_persistent_mode;
extern uint32 g_embed_wnd;
RD_BOOL g_enable_compose = False;
RD_BOOL g_Unobscured; /* used for screenblt */
static GC g_gc = NULL;
static GC g_create_bitmap_gc = NULL;
static GC g_create_glyph_gc = NULL;
static XRectangle g_clip_rectangle;
static Visual *g_visual;
/* Color depth of the X11 visual of our window (e.g. 24 for True Color R8G8B visual).
This may be 32 for R8G8B8 visuals, and then the rest of the bits are undefined
as far as we're concerned. */
static int g_depth;
/* Bits-per-Pixel of the pixmaps we'll be using to draw on our window.
This may be larger than g_depth, in which case some of the bits would
be kept solely for alignment (e.g. 32bpp pixmaps on a 24bpp visual). */
static int g_bpp;
static XIM g_IM;
static XIC g_IC;
static XModifierKeymap *g_mod_map;
/* Maps logical (xmodmap -pp) pointing device buttons (0-based) back
to physical (1-based) indices. */
static unsigned char g_pointer_log_to_phys_map[32];
static Cursor g_current_cursor;
static RD_HCURSOR g_null_cursor = NULL;
static Atom g_protocol_atom, g_kill_atom;
extern Atom g_net_wm_state_atom;
extern Atom g_net_wm_desktop_atom;
extern Atom g_net_wm_ping_atom;
static RD_BOOL g_focused;
static RD_BOOL g_mouse_in_wnd;
/* Indicates that:
1) visual has 15, 16 or 24 depth and the same color channel masks
as its RDP equivalent (implies X server is LE),
2) host is LE
This will trigger an optimization whose real value is questionable.
*/
static RD_BOOL g_compatible_arch;
/* Indicates whether RDP's bitmaps and our XImages have the same
binary format. If so, we can avoid an expensive translation.
Note that this can be true when g_compatible_arch is false,
e.g.:
RDP(LE) <-> host(BE) <-> X-Server(LE)
('host' is the machine running rdesktop; the host simply memcpy's
so its endianness doesn't matter)
*/
static RD_BOOL g_no_translate_image = False;
/* endianness */
static RD_BOOL g_host_be;
static RD_BOOL g_xserver_be;
static int g_red_shift_r, g_blue_shift_r, g_green_shift_r;
static int g_red_shift_l, g_blue_shift_l, g_green_shift_l;
/* software backing store */
extern RD_BOOL g_ownbackstore;
static Pixmap g_backstore = 0;
/* Moving in single app mode */
static RD_BOOL g_moving_wnd;
static int g_move_x_offset = 0;
static int g_move_y_offset = 0;
static RD_BOOL g_using_full_workarea = False;
#ifdef WITH_RDPSND
extern RD_BOOL g_rdpsnd;
#endif
/* MWM decorations */
#define MWM_HINTS_DECORATIONS (1L << 1)
#define PROP_MOTIF_WM_HINTS_ELEMENTS 5
typedef struct
{
unsigned long flags;
unsigned long functions;
unsigned long decorations;
long inputMode;
unsigned long status;
}
PropMotifWmHints;
typedef struct
{
uint32 red;
uint32 green;
uint32 blue;
}
PixelColour;
#define ON_ALL_SEAMLESS_WINDOWS(func, args) \
do { \
seamless_window *sw; \
XRectangle rect; \
if (!g_seamless_windows) break; \
for (sw = g_seamless_windows; sw; sw = sw->next) { \
rect.x = g_clip_rectangle.x - sw->xoffset; \
rect.y = g_clip_rectangle.y - sw->yoffset; \
rect.width = g_clip_rectangle.width; \
rect.height = g_clip_rectangle.height; \
XSetClipRectangles(g_display, g_gc, 0, 0, &rect, 1, YXBanded); \
func args; \
} \
XSetClipRectangles(g_display, g_gc, 0, 0, &g_clip_rectangle, 1, YXBanded); \
} while (0)
static void
seamless_XFillPolygon(Drawable d, XPoint * points, int npoints, int xoffset, int yoffset)
{
points[0].x -= xoffset;
points[0].y -= yoffset;
XFillPolygon(g_display, d, g_gc, points, npoints, Complex, CoordModePrevious);
points[0].x += xoffset;
points[0].y += yoffset;
}
static void
seamless_XDrawLines(Drawable d, XPoint * points, int npoints, int xoffset, int yoffset)
{
points[0].x -= xoffset;
points[0].y -= yoffset;
XDrawLines(g_display, d, g_gc, points, npoints, CoordModePrevious);
points[0].x += xoffset;
points[0].y += yoffset;
}
#define FILL_RECTANGLE(x,y,cx,cy)\
{ \
XFillRectangle(g_display, g_wnd, g_gc, x, y, cx, cy); \
ON_ALL_SEAMLESS_WINDOWS(XFillRectangle, (g_display, sw->wnd, g_gc, x-sw->xoffset, y-sw->yoffset, cx, cy)); \
if (g_ownbackstore) \
XFillRectangle(g_display, g_backstore, g_gc, x, y, cx, cy); \
}
#define FILL_RECTANGLE_BACKSTORE(x,y,cx,cy)\
{ \
XFillRectangle(g_display, g_ownbackstore ? g_backstore : g_wnd, g_gc, x, y, cx, cy); \
}
#define FILL_POLYGON(p,np)\
{ \
XFillPolygon(g_display, g_wnd, g_gc, p, np, Complex, CoordModePrevious); \
if (g_ownbackstore) \
XFillPolygon(g_display, g_backstore, g_gc, p, np, Complex, CoordModePrevious); \
ON_ALL_SEAMLESS_WINDOWS(seamless_XFillPolygon, (sw->wnd, p, np, sw->xoffset, sw->yoffset)); \
}
#define DRAW_ELLIPSE(x,y,cx,cy,m)\
{ \
switch (m) \
{ \
case 0: /* Outline */ \
XDrawArc(g_display, g_wnd, g_gc, x, y, cx, cy, 0, 360*64); \
ON_ALL_SEAMLESS_WINDOWS(XDrawArc, (g_display, sw->wnd, g_gc, x-sw->xoffset, y-sw->yoffset, cx, cy, 0, 360*64)); \
if (g_ownbackstore) \
XDrawArc(g_display, g_backstore, g_gc, x, y, cx, cy, 0, 360*64); \
break; \
case 1: /* Filled */ \
XFillArc(g_display, g_wnd, g_gc, x, y, cx, cy, 0, 360*64); \
ON_ALL_SEAMLESS_WINDOWS(XFillArc, (g_display, sw->wnd, g_gc, x-sw->xoffset, y-sw->yoffset, cx, cy, 0, 360*64)); \
if (g_ownbackstore) \
XFillArc(g_display, g_backstore, g_gc, x, y, cx, cy, 0, 360*64); \
break; \
} \
}
/* colour maps */
extern RD_BOOL g_owncolmap;
static Colormap g_xcolmap;
static uint32 *g_colmap = NULL;
#define TRANSLATE(col) ( g_server_depth != 8 ? translate_colour(col) : g_owncolmap ? col : g_colmap[col] )
#define SET_FOREGROUND(col) XSetForeground(g_display, g_gc, TRANSLATE(col));
#define SET_BACKGROUND(col) XSetBackground(g_display, g_gc, TRANSLATE(col));
static int rop2_map[] = {
GXclear, /* 0 */
GXnor, /* DPon */
GXandInverted, /* DPna */
GXcopyInverted, /* Pn */
GXandReverse, /* PDna */
GXinvert, /* Dn */
GXxor, /* DPx */
GXnand, /* DPan */
GXand, /* DPa */
GXequiv, /* DPxn */
GXnoop, /* D */
GXorInverted, /* DPno */
GXcopy, /* P */
GXorReverse, /* PDno */
GXor, /* DPo */
GXset /* 1 */
};
#define SET_FUNCTION(rop2) { if (rop2 != ROP2_COPY) XSetFunction(g_display, g_gc, rop2_map[rop2]); }
#define RESET_FUNCTION(rop2) { if (rop2 != ROP2_COPY) XSetFunction(g_display, g_gc, GXcopy); }
static seamless_window *
sw_get_window_by_id(unsigned long id)
{
seamless_window *sw;
for (sw = g_seamless_windows; sw; sw = sw->next)
{
if (sw->id == id)
return sw;
}
return NULL;
}
static seamless_window *
sw_get_window_by_wnd(Window wnd)
{
seamless_window *sw;
for (sw = g_seamless_windows; sw; sw = sw->next)
{
if (sw->wnd == wnd)
return sw;
}
return NULL;
}
static void
sw_remove_window(seamless_window * win)
{
seamless_window *sw, **prevnext = &g_seamless_windows;
for (sw = g_seamless_windows; sw; sw = sw->next)
{
if (sw == win)
{
*prevnext = sw->next;
sw->group->refcnt--;
if (sw->group->refcnt == 0)
{
XDestroyWindow(g_display, sw->group->wnd);
xfree(sw->group);
}
xfree(sw->position_timer);
xfree(sw);
return;
}
prevnext = &sw->next;
}
return;
}
/* Move all windows except wnd to new desktop */
static void
sw_all_to_desktop(Window wnd, unsigned int desktop)
{
seamless_window *sw;
for (sw = g_seamless_windows; sw; sw = sw->next)
{
if (sw->wnd == wnd)
continue;
if (sw->desktop != desktop)
{
ewmh_move_to_desktop(sw->wnd, desktop);
sw->desktop = desktop;
}
}
}
/* Send our position */
static void
sw_update_position(seamless_window * sw)
{
XWindowAttributes wa;
int x, y;
Window child_return;
unsigned int serial;
XGetWindowAttributes(g_display, sw->wnd, &wa);
XTranslateCoordinates(g_display, sw->wnd, wa.root,
-wa.border_width, -wa.border_width, &x, &y, &child_return);
serial = seamless_send_position(sw->id, x, y, wa.width, wa.height, 0);
sw->outstanding_position = True;
sw->outpos_serial = serial;
sw->outpos_xoffset = x;
sw->outpos_yoffset = y;
sw->outpos_width = wa.width;
sw->outpos_height = wa.height;
}
/* Check if it's time to send our position */
static void
sw_check_timers()
{
seamless_window *sw;
struct timeval now;
gettimeofday(&now, NULL);
for (sw = g_seamless_windows; sw; sw = sw->next)
{
if (timerisset(sw->position_timer) && timercmp(sw->position_timer, &now, <))
{
timerclear(sw->position_timer);
sw_update_position(sw);
}
}
}
static void
sw_restack_window(seamless_window * sw, unsigned long behind)
{
seamless_window *sw_above;
/* Remove window from stack */
for (sw_above = g_seamless_windows; sw_above; sw_above = sw_above->next)
{
if (sw_above->behind == sw->id)
break;
}
if (sw_above)
sw_above->behind = sw->behind;
/* And then add it at the new position */
for (sw_above = g_seamless_windows; sw_above; sw_above = sw_above->next)
{
if (sw_above->behind == behind)
break;
}
if (sw_above)
sw_above->behind = sw->id;
sw->behind = behind;
}
static void
sw_handle_restack(seamless_window * sw)
{
Status status;
Window root, parent, *children;
unsigned int nchildren, i;
seamless_window *sw_below;
status = XQueryTree(g_display, RootWindowOfScreen(g_screen),
&root, &parent, &children, &nchildren);
if (!status || !nchildren)
return;
sw_below = NULL;
i = 0;
while (children[i] != sw->wnd)
{
i++;
if (i >= nchildren)
goto end;
}
for (i++; i < nchildren; i++)
{
sw_below = sw_get_window_by_wnd(children[i]);
if (sw_below)
break;
}
if (!sw_below && !sw->behind)
goto end;
if (sw_below && (sw_below->id == sw->behind))
goto end;
if (sw_below)
{
seamless_send_zchange(sw->id, sw_below->id, 0);
sw_restack_window(sw, sw_below->id);
}
else
{
seamless_send_zchange(sw->id, 0, 0);
sw_restack_window(sw, 0);
}
end:
XFree(children);
}
static seamless_group *
sw_find_group(unsigned long id, RD_BOOL dont_create)
{
seamless_window *sw;
seamless_group *sg;
XSetWindowAttributes attribs;
for (sw = g_seamless_windows; sw; sw = sw->next)
{
if (sw->group->id == id)
return sw->group;
}
if (dont_create)
return NULL;
sg = xmalloc(sizeof(seamless_group));
sg->wnd =
XCreateWindow(g_display, RootWindowOfScreen(g_screen), -1, -1, 1, 1, 0,
CopyFromParent, CopyFromParent, CopyFromParent, 0, &attribs);
ewmh_set_wm_pid(sg->wnd, getpid());
sg->id = id;
sg->refcnt = 0;
return sg;
}
static void
mwm_hide_decorations(Window wnd)
{
PropMotifWmHints motif_hints;
Atom hintsatom;
/* setup the property */
motif_hints.flags = MWM_HINTS_DECORATIONS;
motif_hints.decorations = 0;
/* get the atom for the property */
hintsatom = XInternAtom(g_display, "_MOTIF_WM_HINTS", False);
if (!hintsatom)
{
logger(GUI, Warning,
"Failed to get atom _MOTIF_WM_HINTS: probably your window manager does not support MWM hints\n");
return;
}
XChangeProperty(g_display, wnd, hintsatom, hintsatom, 32, PropModeReplace,
(unsigned char *) &motif_hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
}
typedef struct _sw_configurenotify_context
{
Window window;
unsigned long serial;
} sw_configurenotify_context;
/* Predicate procedure for sw_wait_configurenotify */
static Bool
sw_configurenotify_p(Display * display, XEvent * xevent, XPointer arg)
{
UNUSED(display);
sw_configurenotify_context *context = (sw_configurenotify_context *) arg;
if (xevent->xany.type == ConfigureNotify
&& xevent->xconfigure.window == context->window
&& xevent->xany.serial >= context->serial)
return True;
return False;
}
/* Wait for a ConfigureNotify, with a equal or larger serial, on the
specified window. The event will be removed from the queue. We
could use XMaskEvent(StructureNotifyMask), but we would then risk
throwing away crucial events like DestroyNotify.
After a ConfigureWindow, according to ICCCM section 4.1.5, we
should receive a ConfigureNotify, either a real or synthetic
one. This indicates that the configure has been "completed".
However, some WMs such as several versions of Metacity fail to
send synthetic events. See bug
http://bugzilla.gnome.org/show_bug.cgi?id=322840. We need to use a
timeout to avoid a hang. Tk uses the same approach. */
static void
sw_wait_configurenotify(Window wnd, unsigned long serial)
{
XEvent xevent;
sw_configurenotify_context context;
struct timeval now;
struct timeval future;
RD_BOOL got = False;
context.window = wnd;
context.serial = serial;
gettimeofday(&future, NULL);
future.tv_usec += 500000;
do
{
if (XCheckIfEvent(g_display, &xevent, sw_configurenotify_p, (XPointer) & context))
{
got = True;
break;
}
usleep(100000);
gettimeofday(&now, NULL);
}
while (timercmp(&now, &future, <));
if (!got)
{
logger(GUI, Warning,
"Broken Window Manager: Timeout while waiting for ConfigureNotify\n");
}
}
/* Get the toplevel window, in case of reparenting */
static Window
sw_get_toplevel(Window wnd)
{
Window root, parent;
Window *child_list;
unsigned int num_children;
while (1)
{
XQueryTree(g_display, wnd, &root, &parent, &child_list, &num_children);
if (root == parent)
{
break;
}
else if (!parent)
{
logger(GUI, Error, "sw_get_toplevel called with root window\n");
}
wnd = parent;
}
return wnd;
}
/* Check if wnd is already behind a window wrt stacking order */
static RD_BOOL
sw_window_is_behind(Window wnd, Window behind)
{
Window dummy1, dummy2;
Window *child_list;
unsigned int num_children;
int i;
RD_BOOL found_behind = False;
RD_BOOL found_wnd = False;
wnd = sw_get_toplevel(wnd);
behind = sw_get_toplevel(behind);
XQueryTree(g_display, RootWindowOfScreen(g_screen), &dummy1, &dummy2, &child_list,
&num_children);
for (i = num_children - 1; i >= 0; i--)
{
if (child_list[i] == behind)
{
found_behind = True;
}
else if (child_list[i] == wnd)
{
found_wnd = True;
break;
}
}
if (child_list)
XFree(child_list);
if (!found_wnd)
{
logger(GUI, Warning, "sw_window_is_behind: Unable to find window 0x%lx", wnd);
if (!found_behind)
{
logger(GUI, Warning,
"sw_window_is_behind: Unable to find behind window 0x%lx", behind);
}
}
return found_behind;
}
/* Test if the window manager correctly handles window restacking. In
particular, we are testing if it's possible to place a window
between two other windows. Many WMs such as Metacity can only stack
windows on the top or bottom. The window creation should mostly
match ui_seamless_create_window. */
static void
seamless_restack_test()
{
/* The goal is to have the middle window between top and
bottom. The middle window is initially at the top,
though. */
Window wnds[3]; /* top, middle and bottom */
int i;
XEvent xevent;
XWindowChanges values;
unsigned long restack_serial;
for (i = 0; i < 3; i++)
{
char name[64];
wnds[i] =
XCreateSimpleWindow(g_display, RootWindowOfScreen(g_screen), 0, 0, 20, 20,
0, 0, 0);
snprintf(name, sizeof(name), "SeamlessRDP restack test - window %d", i);
XStoreName(g_display, wnds[i], name);
ewmh_set_wm_name(wnds[i], name);
/* Hide decorations. Often this means that no
reparenting will be done, which makes the restack
easier. Besides, we want to mimic our other
seamless windows as much as possible. We must still
handle the case with reparenting, though. */
mwm_hide_decorations(wnds[i]);
/* Prevent windows from appearing in task bar */
XSetTransientForHint(g_display, wnds[i], RootWindowOfScreen(g_screen));
ewmh_set_window_popup(wnds[i]);
/* We need to catch MapNotify/ConfigureNotify */
XSelectInput(g_display, wnds[i], StructureNotifyMask);
}
/* Map Windows. Currently, we assume that XMapRaised places
the window on the top of the stack. Should be fairly safe;
the window is configured before it's mapped. */
XMapRaised(g_display, wnds[2]); /* bottom */
do
{
XWindowEvent(g_display, wnds[2], StructureNotifyMask, &xevent);
}
while (xevent.type != MapNotify);
XMapRaised(g_display, wnds[0]); /* top */
do
{
XWindowEvent(g_display, wnds[0], StructureNotifyMask, &xevent);
}
while (xevent.type != MapNotify);
XMapRaised(g_display, wnds[1]); /* middle */
do
{
XWindowEvent(g_display, wnds[1], StructureNotifyMask, &xevent);
}
while (xevent.type != MapNotify);
/* The stacking order should now be 1 - 0 - 2 */
if (!sw_window_is_behind(wnds[0], wnds[1]) || !sw_window_is_behind(wnds[2], wnds[1]))
{
/* Ok, technically a WM is allowed to stack windows arbitrarily, but... */
logger(GUI, Warning, "Broken Window Manager: Unable to test window restacking");
g_seamless_broken_restack = True;
for (i = 0; i < 3; i++)
XDestroyWindow(g_display, wnds[i]);
return;
}
/* Restack, using XReconfigureWMWindow, which should correctly
handle reparented windows as well as nonreparenting WMs. */
values.stack_mode = Below;
values.sibling = wnds[0];
restack_serial = XNextRequest(g_display);
XReconfigureWMWindow(g_display, wnds[1], DefaultScreen(g_display), CWStackMode | CWSibling,
&values);
sw_wait_configurenotify(wnds[1], restack_serial);
/* Now verify that middle is behind top but not behind
bottom */
if (!sw_window_is_behind(wnds[1], wnds[0]))
{
logger(GUI, Warning,
"Broken Window Manager: doesn't handle restack (restack request was ignored)");
g_seamless_broken_restack = True;
}
else if (sw_window_is_behind(wnds[1], wnds[2]))
{
logger(GUI, Warning,
"Broken Window Manager: doesn't handle restack (window was moved to bottom)");
g_seamless_broken_restack = True;
}
/* Destroy windows */
for (i = 0; i < 3; i++)
{
XDestroyWindow(g_display, wnds[i]);
do
{
XWindowEvent(g_display, wnds[i], StructureNotifyMask, &xevent);
}
while (xevent.type != DestroyNotify);
}
}
#define SPLITCOLOUR15(colour, rv) \
{ \
rv.red = ((colour >> 7) & 0xf8) | ((colour >> 12) & 0x7); \
rv.green = ((colour >> 2) & 0xf8) | ((colour >> 8) & 0x7); \
rv.blue = ((colour << 3) & 0xf8) | ((colour >> 2) & 0x7); \
}
#define SPLITCOLOUR16(colour, rv) \
{ \
rv.red = ((colour >> 8) & 0xf8) | ((colour >> 13) & 0x7); \
rv.green = ((colour >> 3) & 0xfc) | ((colour >> 9) & 0x3); \
rv.blue = ((colour << 3) & 0xf8) | ((colour >> 2) & 0x7); \
} \
#define SPLITCOLOUR24(colour, rv) \
{ \
rv.blue = (colour & 0xff0000) >> 16; \
rv.green = (colour & 0x00ff00) >> 8; \
rv.red = (colour & 0x0000ff); \
}
#define MAKECOLOUR(pc) \
((pc.red >> g_red_shift_r) << g_red_shift_l) \
| ((pc.green >> g_green_shift_r) << g_green_shift_l) \
| ((pc.blue >> g_blue_shift_r) << g_blue_shift_l) \
#define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }
#define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | (x & 0xff00)); }
#define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \
x = (x << 16) | (x >> 16); }
/* The following macros output the same octet sequences
on both BE and LE hosts: */
#define BOUT16(o, x) { *(o++) = x >> 8; *(o++) = x; }
#define BOUT24(o, x) { *(o++) = x >> 16; *(o++) = x >> 8; *(o++) = x; }
#define BOUT32(o, x) { *(o++) = x >> 24; *(o++) = x >> 16; *(o++) = x >> 8; *(o++) = x; }
#define LOUT16(o, x) { *(o++) = x; *(o++) = x >> 8; }
#define LOUT24(o, x) { *(o++) = x; *(o++) = x >> 8; *(o++) = x >> 16; }
#define LOUT32(o, x) { *(o++) = x; *(o++) = x >> 8; *(o++) = x >> 16; *(o++) = x >> 24; }
static uint32
translate_colour(uint32 colour)
{
PixelColour pc;
switch (g_server_depth)
{
case 15:
SPLITCOLOUR15(colour, pc);
break;
case 16:
SPLITCOLOUR16(colour, pc);
break;
case 24:
case 32:
SPLITCOLOUR24(colour, pc);
break;
default:
/* Avoid warning */
pc.red = 0;
pc.green = 0;
pc.blue = 0;
break;
}
return MAKECOLOUR(pc);
}
/* indent is confused by UNROLL8 */
/* *INDENT-OFF* */
/* repeat and unroll, similar to bitmap.c */
/* potentially any of the following translate */
/* functions can use repeat but just doing */
/* the most common ones */
#define UNROLL8(stm) { stm stm stm stm stm stm stm stm }
/* 2 byte output repeat */
#define REPEAT2(stm) \
{ \
while (out <= end - 8 * 2) \
UNROLL8(stm) \
while (out < end) \
{ stm } \
}
/* 3 byte output repeat */
#define REPEAT3(stm) \
{ \
while (out <= end - 8 * 3) \
UNROLL8(stm) \
while (out < end) \
{ stm } \
}
/* 4 byte output repeat */
#define REPEAT4(stm) \
{ \
while (out <= end - 8 * 4) \
UNROLL8(stm) \
while (out < end) \
{ stm } \
}
/* *INDENT-ON* */
static void
translate8to8(const uint8 * data, uint8 * out, uint8 * end)
{
while (out < end)
*(out++) = (uint8) g_colmap[*(data++)];
}
static void
translate8to16(const uint8 * data, uint8 * out, uint8 * end)
{
uint16 value;
if (g_compatible_arch)
{
/* *INDENT-OFF* */
REPEAT2
(
*((uint16 *) out) = g_colmap[*(data++)];
out += 2;
)
/* *INDENT-ON* */
}
else if (g_xserver_be)
{
while (out < end)
{
value = (uint16) g_colmap[*(data++)];
BOUT16(out, value);
}
}
else
{
while (out < end)
{
value = (uint16) g_colmap[*(data++)];
LOUT16(out, value);
}
}
}
/* little endian - conversion happens when colourmap is built */
static void
translate8to24(const uint8 * data, uint8 * out, uint8 * end)
{
uint32 value;
if (g_compatible_arch)
{
while (out < end)
{
value = g_colmap[*(data++)];
BOUT24(out, value);
}
}
else
{
while (out < end)
{
value = g_colmap[*(data++)];
LOUT24(out, value);
}
}
}
static void