summaryrefslogtreecommitdiff
path: root/xserver/hw/dmx/dmxwindow.c
blob: 24acc08e34a2d87cc47960d89696b480aef7da48 (plain)
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
/*
 * Copyright 2001-2004 Red Hat Inc., Durham, North Carolina.
 *
 * All Rights Reserved.
 *
 * 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 on 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 (including the
 * next paragraph) 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
 * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
 * 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.
 */

/*
 * Authors:
 *   Kevin E. Martin <kem@redhat.com>
 *
 */

/** \file
 * This file provides support for window-related functions. */

#ifdef HAVE_DMX_CONFIG_H
#include <dmx-config.h>
#endif

#include "dmx.h"
#include "dmxsync.h"
#include "dmxwindow.h"
#include "dmxpixmap.h"
#include "dmxcmap.h"
#include "dmxvisual.h"
#include "dmxinput.h"
#include "dmxextension.h"
#ifdef RENDER
#include "dmxpict.h"
#endif

#include "windowstr.h"

static void dmxDoRestackWindow(WindowPtr pWindow);
static void dmxDoChangeWindowAttributes(WindowPtr pWindow,
					unsigned long *mask,
					XSetWindowAttributes *attribs);

static void dmxDoSetShape(WindowPtr pWindow);

/** Initialize the private area for the window functions. */
Bool dmxInitWindow(ScreenPtr pScreen)
{
    if (!dixRequestPrivate(dmxWinPrivateKey, sizeof(dmxWinPrivRec)))
	return FALSE;

    return TRUE;
}


Window dmxCreateRootWindow(WindowPtr pWindow)
{
    ScreenPtr             pScreen   = pWindow->drawable.pScreen;
    DMXScreenInfo        *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxWinPrivPtr         pWinPriv  = DMX_GET_WINDOW_PRIV(pWindow);
    Window                parent;
    Visual               *visual;
    unsigned long         mask;
    XSetWindowAttributes  attribs;
    ColormapPtr           pCmap;
    dmxColormapPrivPtr    pCmapPriv;

    /* Create root window */

    parent = dmxScreen->scrnWin; /* This is our "Screen" window */
    visual = dmxScreen->beVisuals[dmxScreen->beDefVisualIndex].visual;

    pCmap = (ColormapPtr)LookupIDByType(wColormap(pWindow), RT_COLORMAP);
    pCmapPriv = DMX_GET_COLORMAP_PRIV(pCmap);

    mask = CWEventMask | CWBackingStore | CWColormap | CWBorderPixel;
    attribs.event_mask    = ExposureMask;
    attribs.backing_store = NotUseful;
    attribs.colormap      = pCmapPriv->cmap;
    attribs.border_pixel  = 0;

    /* Incorporate new attributes, if needed */
    if (pWinPriv->attribMask) {
	dmxDoChangeWindowAttributes(pWindow, &pWinPriv->attribMask, &attribs);
	mask |= pWinPriv->attribMask;
    }

    return XCreateWindow(dmxScreen->beDisplay,
			 parent,
			 pWindow->origin.x - wBorderWidth(pWindow),
			 pWindow->origin.y - wBorderWidth(pWindow),
			 pWindow->drawable.width,
			 pWindow->drawable.height,
			 pWindow->borderWidth,
			 pWindow->drawable.depth,
			 pWindow->drawable.class,
			 visual,
			 mask,
			 &attribs);
}

/** Change the location and size of the "screen" window.  Called from
 *  #dmxReconfigureScreenWindow(). */
void dmxResizeScreenWindow(ScreenPtr pScreen,
			   int x, int y, int w, int h)
{
    DMXScreenInfo  *dmxScreen = &dmxScreens[pScreen->myNum];
    unsigned int    m;
    XWindowChanges  c;

    if (!dmxScreen->beDisplay)
	return;

    /* Handle resizing on back-end server */
    m = CWX | CWY | CWWidth | CWHeight;
    c.x = x;
    c.y = y;
    c.width = w;
    c.height = h;

    XConfigureWindow(dmxScreen->beDisplay, dmxScreen->scrnWin, m, &c);
    dmxSync(dmxScreen, False);
}

/** Change the location and size of the "root" window.  Called from
 *  #dmxReconfigureRootWindow(). */
void dmxResizeRootWindow(WindowPtr pRoot,
			 int x, int y, int w, int h)
{
    DMXScreenInfo  *dmxScreen = &dmxScreens[pRoot->drawable.pScreen->myNum];
    dmxWinPrivPtr   pWinPriv = DMX_GET_WINDOW_PRIV(pRoot);
    unsigned int    m;
    XWindowChanges  c;

    /* Handle resizing on back-end server */
    if (dmxScreen->beDisplay) {
	m = CWX | CWY | CWWidth | CWHeight;
	c.x = x;
	c.y = y;
	c.width = (w > 0) ? w : 1;
	c.height = (h > 0) ? h : 1;

	XConfigureWindow(dmxScreen->beDisplay, pWinPriv->window, m, &c);
    }

    if (w == 0 || h == 0) {
	if (pWinPriv->mapped) {
	    if (dmxScreen->beDisplay)
		XUnmapWindow(dmxScreen->beDisplay, pWinPriv->window);
	    pWinPriv->mapped = FALSE;
	}
    } else if (!pWinPriv->mapped) {
	if (dmxScreen->beDisplay)
	    XMapWindow(dmxScreen->beDisplay, pWinPriv->window);
	pWinPriv->mapped = TRUE;
    }

    if (dmxScreen->beDisplay)
	dmxSync(dmxScreen, False);
}

void dmxGetDefaultWindowAttributes(WindowPtr pWindow,
				   Colormap *cmap,
				   Visual **visual)
{
    ScreenPtr  pScreen = pWindow->drawable.pScreen;

    if (pWindow->drawable.class != InputOnly &&
	pWindow->optional &&
	pWindow->optional->visual != wVisual(pWindow->parent)) {

	/* Find the matching visual */
	*visual = dmxLookupVisualFromID(pScreen, wVisual(pWindow));

	/* Handle optional colormaps */
	if (pWindow->optional->colormap) {
	    ColormapPtr         pCmap;
	    dmxColormapPrivPtr  pCmapPriv;

	    pCmap = (ColormapPtr)LookupIDByType(wColormap(pWindow),
						RT_COLORMAP);
	    pCmapPriv = DMX_GET_COLORMAP_PRIV(pCmap);
	    *cmap = pCmapPriv->cmap;
	} else {
	    *cmap = dmxColormapFromDefaultVisual(pScreen, *visual);
	}
    } else {
	*visual = CopyFromParent;
	*cmap = (Colormap)0;
    }
}

static Window dmxCreateNonRootWindow(WindowPtr pWindow)
{
    ScreenPtr             pScreen = pWindow->drawable.pScreen;
    DMXScreenInfo        *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxWinPrivPtr         pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);
    Window                parent;
    unsigned long         mask = 0L;
    XSetWindowAttributes  attribs;
    dmxWinPrivPtr         pParentPriv = DMX_GET_WINDOW_PRIV(pWindow->parent);

    /* Create window on back-end server */

    parent = pParentPriv->window;

    /* The parent won't exist if this call to CreateNonRootWindow came
       from ReparentWindow and the grandparent window has not yet been
       created */
    if (!parent) {
	dmxCreateAndRealizeWindow(pWindow->parent, FALSE);
	parent = pParentPriv->window;
    }

    /* Incorporate new attributes, if needed */
    if (pWinPriv->attribMask) {
	dmxDoChangeWindowAttributes(pWindow, &pWinPriv->attribMask, &attribs);
	mask |= pWinPriv->attribMask;
    }

    /* Add in default attributes */
    if (pWindow->drawable.class != InputOnly) {
	mask |= CWBackingStore;
	attribs.backing_store = NotUseful;

	if (!(mask & CWColormap) && pWinPriv->cmap) {
	    mask |= CWColormap;
	    attribs.colormap = pWinPriv->cmap;
	    if (!(mask & CWBorderPixel)) {
		mask |= CWBorderPixel;
		attribs.border_pixel = 0;
	    }
	}
    }

    /* Handle case where subwindows are being mapped, but created out of
       order -- if current window has a previous sibling, then it cannot
       be created on top of the stack, so we must restack the windows */
    pWinPriv->restacked = (pWindow->prevSib != NullWindow);

    return XCreateWindow(dmxScreen->beDisplay,
			 parent,
			 pWindow->origin.x - wBorderWidth(pWindow),
			 pWindow->origin.y - wBorderWidth(pWindow),
			 pWindow->drawable.width,
			 pWindow->drawable.height,
			 pWindow->borderWidth,
			 pWindow->drawable.depth,
			 pWindow->drawable.class,
			 pWinPriv->visual,
			 mask,
			 &attribs);
}

/** This function handles lazy window creation and realization.  Window
 *  creation is handled by #dmxCreateNonRootWindow().  It also handles
 *  any stacking changes that have occured since the window was
 *  originally created by calling #dmxDoRestackWindow().  If the window
 *  is shaped, the shape is set on the back-end server by calling
 *  #dmxDoSetShape(), and if the window has pictures (from RENDER)
 *  associated with it, those pictures are created on the back-end
 *  server by calling #dmxCreatePictureList().  If \a doSync is TRUE,
 *  then #dmxSync() is called. */
void dmxCreateAndRealizeWindow(WindowPtr pWindow, Bool doSync)
{
    ScreenPtr      pScreen = pWindow->drawable.pScreen;
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxWinPrivPtr  pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);

    if (!dmxScreen->beDisplay) return;

    pWinPriv->window = dmxCreateNonRootWindow(pWindow);
    if (pWinPriv->restacked) dmxDoRestackWindow(pWindow);
    if (pWinPriv->isShaped) dmxDoSetShape(pWindow);
#ifdef RENDER
    if (pWinPriv->hasPict) dmxCreatePictureList(pWindow);
#endif
    if (pWinPriv->mapped) XMapWindow(dmxScreen->beDisplay,
				      pWinPriv->window);
    if (doSync) dmxSync(dmxScreen, False);
}

/** Create \a pWindow on the back-end server.  If the lazy window
 *  creation optimization is enabled, then the actual creation and
 *  realization of the window is handled by
 *  #dmxCreateAndRealizeWindow(). */
Bool dmxCreateWindow(WindowPtr pWindow)
{
    ScreenPtr             pScreen = pWindow->drawable.pScreen;
    DMXScreenInfo        *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxWinPrivPtr         pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);
    Bool                  ret = TRUE;

    DMX_UNWRAP(CreateWindow, dmxScreen, pScreen);
#if 0
    if (pScreen->CreateWindow)
	ret = pScreen->CreateWindow(pWindow);
#endif

    /* Set up the defaults */
    pWinPriv->window     = (Window)0;
    pWinPriv->offscreen  = TRUE;
    pWinPriv->mapped     = FALSE;
    pWinPriv->restacked  = FALSE;
    pWinPriv->attribMask = 0;
    pWinPriv->isShaped   = FALSE;
#ifdef RENDER
    pWinPriv->hasPict    = FALSE;
#endif
#ifdef GLXEXT
    pWinPriv->swapGroup  = NULL;
    pWinPriv->barrier    = 0;
#endif

    if (dmxScreen->beDisplay) {
	/* Only create the root window at this stage -- non-root windows are
	   created when they are mapped and are on-screen */
	if (!pWindow->parent) {
	    dmxScreen->rootWin = pWinPriv->window
		= dmxCreateRootWindow(pWindow);
	    if (dmxScreen->scrnX         != dmxScreen->rootX
		|| dmxScreen->scrnY      != dmxScreen->rootY
		|| dmxScreen->scrnWidth  != dmxScreen->rootWidth
		|| dmxScreen->scrnHeight != dmxScreen->rootHeight) {
		dmxResizeRootWindow(pWindow,
				    dmxScreen->rootX,
				    dmxScreen->rootY,
				    dmxScreen->rootWidth,
				    dmxScreen->rootHeight);
		dmxUpdateScreenResources(screenInfo.screens[dmxScreen->index],
					 dmxScreen->rootX,
					 dmxScreen->rootY,
					 dmxScreen->rootWidth,
					 dmxScreen->rootHeight);
		pWindow->origin.x = dmxScreen->rootX;
		pWindow->origin.y = dmxScreen->rootY;
	    }
	} else {
	    dmxGetDefaultWindowAttributes(pWindow,
					  &pWinPriv->cmap,
					  &pWinPriv->visual);

	    if (dmxLazyWindowCreation) {
		/* Save parent's visual for use later */
		if (pWinPriv->visual == CopyFromParent)
		    pWinPriv->visual =
			dmxLookupVisualFromID(pScreen,
					      wVisual(pWindow->parent));
	    } else {
		pWinPriv->window = dmxCreateNonRootWindow(pWindow);
	    }
	}

	dmxSync(dmxScreen, False);
    }

    DMX_WRAP(CreateWindow, dmxCreateWindow, dmxScreen, pScreen);

    return ret;
}

/** Destroy \a pWindow on the back-end server. */
Bool dmxBEDestroyWindow(WindowPtr pWindow)
{
    ScreenPtr      pScreen = pWindow->drawable.pScreen;
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxWinPrivPtr  pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);

    if (pWinPriv->window) {
	XDestroyWindow(dmxScreen->beDisplay, pWinPriv->window);
	pWinPriv->window = (Window)0;
	return TRUE;
    }

    return FALSE;
}

/** Destroy \a pWindow on the back-end server.  If any RENDER pictures
    were created, destroy them as well. */
Bool dmxDestroyWindow(WindowPtr pWindow)
{
    ScreenPtr      pScreen = pWindow->drawable.pScreen;
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    Bool           ret = TRUE;
    Bool           needSync = FALSE;
#ifdef GLXEXT
    dmxWinPrivPtr  pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);
#endif

    DMX_UNWRAP(DestroyWindow, dmxScreen, pScreen);

#ifdef RENDER
    /* Destroy any picture list associated with this window */
    needSync |= dmxDestroyPictureList(pWindow);
#endif

    /* Destroy window on back-end server */
    needSync |= dmxBEDestroyWindow(pWindow);
    if (needSync) dmxSync(dmxScreen, FALSE);

#ifdef GLXEXT
    if (pWinPriv->swapGroup && pWinPriv->windowDestroyed)
	pWinPriv->windowDestroyed(pWindow);
#endif

#if 0
    if (pScreen->DestroyWindow)
	ret = pScreen->DestroyWindow(pWindow);
#endif
    DMX_WRAP(DestroyWindow, dmxDestroyWindow, dmxScreen, pScreen);

    return ret;
}

/** Change the position of \a pWindow to be \a x, \a y. */
Bool dmxPositionWindow(WindowPtr pWindow, int x, int y)
{
    ScreenPtr       pScreen = pWindow->drawable.pScreen;
    DMXScreenInfo  *dmxScreen = &dmxScreens[pScreen->myNum];
    Bool            ret = TRUE;
    dmxWinPrivPtr   pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);
    unsigned int    m;
    XWindowChanges  c;

    DMX_UNWRAP(PositionWindow, dmxScreen, pScreen);
#if 0
    if (pScreen->PositionWindow)
	ret = pScreen->PositionWindow(pWindow, x, y);
#endif

    /* Determine if the window is completely off the visible portion of
       the screen */
    pWinPriv->offscreen = DMX_WINDOW_OFFSCREEN(pWindow);

    /* If the window is now on-screen and it is mapped and it has not
       been created yet, create it and map it */
    if (!pWinPriv->window && pWinPriv->mapped && !pWinPriv->offscreen) {
	dmxCreateAndRealizeWindow(pWindow, TRUE);
    } else if (pWinPriv->window) {
	/* Position window on back-end server */
	m = CWX | CWY | CWWidth | CWHeight;
	c.x = pWindow->origin.x - wBorderWidth(pWindow);
	c.y = pWindow->origin.y - wBorderWidth(pWindow);
	c.width = pWindow->drawable.width;
	c.height = pWindow->drawable.height;
	if (pWindow->drawable.class != InputOnly) {
	    m |= CWBorderWidth;
	    c.border_width = pWindow->borderWidth;
	}

	XConfigureWindow(dmxScreen->beDisplay, pWinPriv->window, m, &c);
	dmxSync(dmxScreen, False);
    }

    DMX_WRAP(PositionWindow, dmxPositionWindow, dmxScreen, pScreen);

    return ret;
}

static void dmxDoChangeWindowAttributes(WindowPtr pWindow,
					unsigned long *mask,
					XSetWindowAttributes *attribs)
{
    dmxPixPrivPtr         pPixPriv;

    if (*mask & CWBackPixmap) {
	switch (pWindow->backgroundState) {
	case None:
	    attribs->background_pixmap = None;
	    break;

	case ParentRelative:
	    attribs->background_pixmap = ParentRelative;
	    break;

	case BackgroundPixmap:
	    pPixPriv = DMX_GET_PIXMAP_PRIV(pWindow->background.pixmap);
	    attribs->background_pixmap = pPixPriv->pixmap;
	    break;

	case BackgroundPixel:
	    *mask &= ~CWBackPixmap;
	    break;
	}
    }

    if (*mask & CWBackPixel) {
	if (pWindow->backgroundState == BackgroundPixel)
	    attribs->background_pixel = pWindow->background.pixel;
	else
	    *mask &= ~CWBackPixel;
    }

    if (*mask & CWBorderPixmap) {
	if (pWindow->borderIsPixel)
	    *mask &= ~CWBorderPixmap;
	else {
	    pPixPriv = DMX_GET_PIXMAP_PRIV(pWindow->border.pixmap);
	    attribs->border_pixmap = pPixPriv->pixmap;
	}
    }

    if (*mask & CWBorderPixel) {
	if (pWindow->borderIsPixel)
	    attribs->border_pixel = pWindow->border.pixel;
	else
	    *mask &= ~CWBorderPixel;
    }

    if (*mask & CWBitGravity)
	attribs->bit_gravity = pWindow->bitGravity;

    if (*mask & CWWinGravity)
	*mask &= ~CWWinGravity; /* Handled by dix */

    if (*mask & CWBackingStore)
	*mask &= ~CWBackingStore; /* Backing store not supported */

    if (*mask & CWBackingPlanes)
	*mask &= ~CWBackingPlanes; /* Backing store not supported */

    if (*mask & CWBackingPixel)
	*mask &= ~CWBackingPixel; /* Backing store not supported */

    if (*mask & CWOverrideRedirect)
	attribs->override_redirect = pWindow->overrideRedirect;

    if (*mask & CWSaveUnder)
	*mask &= ~CWSaveUnder; /* Save unders not supported */

    if (*mask & CWEventMask)
	*mask &= ~CWEventMask; /* Events are handled by dix */

    if (*mask & CWDontPropagate)
	*mask &= ~CWDontPropagate; /* Events are handled by dix */

    if (*mask & CWColormap) {
	ColormapPtr         pCmap;
	dmxColormapPrivPtr  pCmapPriv;

	pCmap = (ColormapPtr)LookupIDByType(wColormap(pWindow), RT_COLORMAP);
	pCmapPriv = DMX_GET_COLORMAP_PRIV(pCmap);
	attribs->colormap = pCmapPriv->cmap;
    }

    if (*mask & CWCursor)
	*mask &= ~CWCursor; /* Handled by the cursor code */
}

/** Change the window attributes of \a pWindow. */
Bool dmxChangeWindowAttributes(WindowPtr pWindow, unsigned long mask)
{
    ScreenPtr             pScreen = pWindow->drawable.pScreen;
    DMXScreenInfo        *dmxScreen = &dmxScreens[pScreen->myNum];
    Bool                  ret = TRUE;
    dmxWinPrivPtr         pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);
    XSetWindowAttributes  attribs;

    DMX_UNWRAP(ChangeWindowAttributes, dmxScreen, pScreen);
#if 0
    if (pScreen->ChangeWindowAttributes)
	ret = pScreen->ChangeWindowAttributes(pWindow, mask);
#endif

    /* Change window attribs on back-end server */
    dmxDoChangeWindowAttributes(pWindow, &mask, &attribs);

    /* Save mask for lazy window creation optimization */
    pWinPriv->attribMask |= mask;

    if (mask && pWinPriv->window) {
	XChangeWindowAttributes(dmxScreen->beDisplay, pWinPriv->window,
				mask, &attribs);
	dmxSync(dmxScreen, False);
    }

    DMX_WRAP(ChangeWindowAttributes, dmxChangeWindowAttributes, dmxScreen,
	     pScreen);

    return ret;
}

/** Realize \a pWindow on the back-end server.  If the lazy window
 *  creation optimization is enabled, the window is only realized when
 *  it at least partially overlaps the screen. */
Bool dmxRealizeWindow(WindowPtr pWindow)
{
    ScreenPtr      pScreen = pWindow->drawable.pScreen;
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    Bool           ret = TRUE;
    dmxWinPrivPtr  pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);

    DMX_UNWRAP(RealizeWindow, dmxScreen, pScreen);
#if 0
    if (pScreen->RealizeWindow)
	ret = pScreen->RealizeWindow(pWindow);
#endif

    /* Determine if the window is completely off the visible portion of
       the screen */
    pWinPriv->offscreen = DMX_WINDOW_OFFSCREEN(pWindow);

    /* If the window hasn't been created and it's not offscreen, then
       create it */
    if (!pWinPriv->window && !pWinPriv->offscreen) {
	dmxCreateAndRealizeWindow(pWindow, FALSE);
    }

    if (pWinPriv->window) {
	/* Realize window on back-end server */
	XMapWindow(dmxScreen->beDisplay, pWinPriv->window);
	dmxSync(dmxScreen, False);
    }

    /* Let the other functions know that the window is now mapped */
    pWinPriv->mapped = TRUE;

    DMX_WRAP(RealizeWindow, dmxRealizeWindow, dmxScreen, pScreen);

    dmxUpdateWindowInfo(DMX_UPDATE_REALIZE, pWindow);
    return ret;
}

/** Unrealize \a pWindow on the back-end server. */
Bool dmxUnrealizeWindow(WindowPtr pWindow)
{
    ScreenPtr      pScreen = pWindow->drawable.pScreen;
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    Bool           ret = TRUE;
    dmxWinPrivPtr  pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);

    DMX_UNWRAP(UnrealizeWindow, dmxScreen, pScreen);
#if 0
    if (pScreen->UnrealizeWindow)
	ret = pScreen->UnrealizeWindow(pWindow);
#endif

    if (pWinPriv->window) {
	/* Unrealize window on back-end server */
	XUnmapWindow(dmxScreen->beDisplay, pWinPriv->window);
	dmxSync(dmxScreen, False);
    }

    /* When unrealized (i.e., unmapped), the window is always considered
       off of the visible portion of the screen */
    pWinPriv->offscreen = TRUE;
    pWinPriv->mapped = FALSE;

#ifdef GLXEXT
    if (pWinPriv->swapGroup && pWinPriv->windowUnmapped)
	pWinPriv->windowUnmapped(pWindow);
#endif

    DMX_WRAP(UnrealizeWindow, dmxUnrealizeWindow, dmxScreen, pScreen);

    dmxUpdateWindowInfo(DMX_UPDATE_UNREALIZE, pWindow);
    return ret;
}

static void dmxDoRestackWindow(WindowPtr pWindow)
{
    ScreenPtr       pScreen = pWindow->drawable.pScreen;
    DMXScreenInfo  *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxWinPrivPtr   pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);
    WindowPtr       pNextSib = pWindow->nextSib;
    unsigned int    m;
    XWindowChanges  c;

    if (pNextSib == NullWindow) {
	/* Window is at the bottom of the stack */
	m = CWStackMode;
	c.sibling = (Window)0;
	c.stack_mode = Below;
	XConfigureWindow(dmxScreen->beDisplay, pWinPriv->window, m, &c);
    } else {
	/* Window is not at the bottom of the stack */
	dmxWinPrivPtr  pNextSibPriv = DMX_GET_WINDOW_PRIV(pNextSib);

	/* Handle case where siblings have not yet been created due to
           lazy window creation optimization by first finding the next
           sibling in the sibling list that has been created (if any)
           and then putting the current window just above that sibling,
           and if no next siblings have been created yet, then put it at
           the bottom of the stack (since it might have a previous
           sibling that should be above it). */
	while (!pNextSibPriv->window) {
	    pNextSib = pNextSib->nextSib;
	    if (pNextSib == NullWindow) {
		/* Window is at the bottom of the stack */
		m = CWStackMode;
		c.sibling = (Window)0;
		c.stack_mode = Below;
		XConfigureWindow(dmxScreen->beDisplay, pWinPriv->window, m, &c);
		return;
	    }
	    pNextSibPriv = DMX_GET_WINDOW_PRIV(pNextSib);
	}

	m = CWStackMode | CWSibling;
	c.sibling = pNextSibPriv->window;
	c.stack_mode = Above;
	XConfigureWindow(dmxScreen->beDisplay, pWinPriv->window, m, &c);
    }
}

/** Handle window restacking.  The actual restacking occurs in
 *  #dmxDoRestackWindow(). */
void dmxRestackWindow(WindowPtr pWindow, WindowPtr pOldNextSib)
{
    ScreenPtr       pScreen = pWindow->drawable.pScreen;
    DMXScreenInfo  *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxWinPrivPtr   pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);

    DMX_UNWRAP(RestackWindow, dmxScreen, pScreen);
#if 0
    if (pScreen->RestackWindow)
	pScreen->RestackWindow(pWindow, pOldNextSib);
#endif

    if (pOldNextSib != pWindow->nextSib) {
	/* Track restacking for lazy window creation optimization */
	pWinPriv->restacked = TRUE;

	/* Restack window on back-end server */
	if (pWinPriv->window) {
	    dmxDoRestackWindow(pWindow);
	    dmxSync(dmxScreen, False);
	}
    }

    DMX_WRAP(RestackWindow, dmxRestackWindow, dmxScreen, pScreen);
    dmxUpdateWindowInfo(DMX_UPDATE_RESTACK, pWindow);
}

static Bool dmxWindowExposurePredicate(Display *dpy, XEvent *ev, XPointer ptr)
{
    return (ev->type == Expose && ev->xexpose.window == *(Window *)ptr);
}

/** Handle exposures on \a pWindow.  Since window exposures are handled
 *  in DMX, the events that are generated by the back-end server are
 *  redundant, so we eat them here. */
void dmxWindowExposures(WindowPtr pWindow, RegionPtr prgn,
			RegionPtr other_exposed)
{
    ScreenPtr      pScreen = pWindow->drawable.pScreen;
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxWinPrivPtr  pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);
    XEvent         ev;

    DMX_UNWRAP(WindowExposures, dmxScreen, pScreen);

    dmxSync(dmxScreen, False);

    if (pWinPriv->window) {
	while (XCheckIfEvent(dmxScreen->beDisplay, &ev,
			     dmxWindowExposurePredicate,
			     (XPointer)&pWinPriv->window)) {
	    /* Handle expose events -- this should not be necessary
	       since the base window in which the root window was
	       created is guaranteed to be on top (override_redirect),
	       so we should just swallow these events.  If for some
	       reason the window is not on top, then we'd need to
	       collect these events and send them to the client later
	       (e.g., during the block handler as Xnest does). */
	}
    }

#if 1
    if (pScreen->WindowExposures)
	pScreen->WindowExposures(pWindow, prgn, other_exposed);
#endif
    DMX_WRAP(WindowExposures, dmxWindowExposures, dmxScreen, pScreen);
}

/** Move \a pWindow on the back-end server.  Determine whether or not it
 *  is on or offscreen, and realize it if it is newly on screen and the
 *  lazy window creation optimization is enabled. */
void dmxCopyWindow(WindowPtr pWindow, DDXPointRec ptOldOrg, RegionPtr prgnSrc)
{
    ScreenPtr       pScreen = pWindow->drawable.pScreen;
    DMXScreenInfo  *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxWinPrivPtr   pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);
    unsigned int    m;
    XWindowChanges  c;

    DMX_UNWRAP(CopyWindow, dmxScreen, pScreen);
#if 0
    if (pScreen->CopyWindow)
	pScreen->CopyWindow(pWindow, ptOldOrg, prgnSrc);
#endif

    /* Determine if the window is completely off the visible portion of
       the screen */
    pWinPriv->offscreen = DMX_WINDOW_OFFSCREEN(pWindow);

    /* If the window is now on-screen and it is mapped and it has not
       been created yet, create it and map it */
    if (!pWinPriv->window && pWinPriv->mapped && !pWinPriv->offscreen) {
	dmxCreateAndRealizeWindow(pWindow, TRUE);
    } else if (pWinPriv->window) {
	/* Move window on back-end server */
	m = CWX | CWY | CWWidth | CWHeight;
	c.x = pWindow->origin.x - wBorderWidth(pWindow);
	c.y = pWindow->origin.y - wBorderWidth(pWindow);
	c.width = pWindow->drawable.width;
	c.height = pWindow->drawable.height;

	XConfigureWindow(dmxScreen->beDisplay, pWinPriv->window, m, &c);
	dmxSync(dmxScreen, False);
    }

    DMX_WRAP(CopyWindow, dmxCopyWindow, dmxScreen, pScreen);
    dmxUpdateWindowInfo(DMX_UPDATE_COPY, pWindow);
}

/** Resize \a pWindow on the back-end server.  Determine whether or not
 *  it is on or offscreen, and realize it if it is newly on screen and
 *  the lazy window creation optimization is enabled. */
void dmxResizeWindow(WindowPtr pWindow, int x, int y,
		     unsigned int w, unsigned int h, WindowPtr pSib)
{
    ScreenPtr       pScreen = pWindow->drawable.pScreen;
    DMXScreenInfo  *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxWinPrivPtr   pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);
    dmxWinPrivPtr   pSibPriv;
    unsigned int    m;
    XWindowChanges  c;

    if (pSib)
	pSibPriv = DMX_GET_WINDOW_PRIV(pSib);

    DMX_UNWRAP(ResizeWindow, dmxScreen, pScreen);
#if 1
    if (pScreen->ResizeWindow)
	pScreen->ResizeWindow(pWindow, x, y, w, h, pSib);
#endif

    /* Determine if the window is completely off the visible portion of
       the screen */
    pWinPriv->offscreen = DMX_WINDOW_OFFSCREEN(pWindow);

    /* If the window is now on-screen and it is mapped and it has not
       been created yet, create it and map it */
    if (!pWinPriv->window && pWinPriv->mapped && !pWinPriv->offscreen) {
	dmxCreateAndRealizeWindow(pWindow, TRUE);
    } else if (pWinPriv->window) {
	/* Handle resizing on back-end server */
	m = CWX | CWY | CWWidth | CWHeight;
	c.x = pWindow->origin.x - wBorderWidth(pWindow);
	c.y = pWindow->origin.y - wBorderWidth(pWindow);
	c.width = pWindow->drawable.width;
	c.height = pWindow->drawable.height;

	XConfigureWindow(dmxScreen->beDisplay, pWinPriv->window, m, &c);
	dmxSync(dmxScreen, False);
    }

    DMX_WRAP(ResizeWindow, dmxResizeWindow, dmxScreen, pScreen);
    dmxUpdateWindowInfo(DMX_UPDATE_RESIZE, pWindow);
}

/** Reparent \a pWindow on the back-end server. */
void dmxReparentWindow(WindowPtr pWindow, WindowPtr pPriorParent)
{
    ScreenPtr      pScreen = pWindow->drawable.pScreen;
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxWinPrivPtr  pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);
    dmxWinPrivPtr  pParentPriv = DMX_GET_WINDOW_PRIV(pWindow->parent);

    DMX_UNWRAP(ReparentWindow, dmxScreen, pScreen);
#if 0
    if (pScreen->ReparentWindow)
	pScreen->ReparentWindow(pWindow, pPriorParent);
#endif

    if (pWinPriv->window) {
	if (!pParentPriv->window) {
	    dmxCreateAndRealizeWindow(pWindow->parent, FALSE);
	}

	/* Handle reparenting on back-end server */
	XReparentWindow(dmxScreen->beDisplay, pWinPriv->window,
			pParentPriv->window,
			pWindow->origin.x - wBorderWidth(pWindow),
			pWindow->origin.x - wBorderWidth(pWindow));
	dmxSync(dmxScreen, False);
    }

    DMX_WRAP(ReparentWindow, dmxReparentWindow, dmxScreen, pScreen);
    dmxUpdateWindowInfo(DMX_UPDATE_REPARENT, pWindow);
}

/** Change border width for \a pWindow to \a width pixels. */
void dmxChangeBorderWidth(WindowPtr pWindow, unsigned int width)
{
    ScreenPtr       pScreen = pWindow->drawable.pScreen;
    DMXScreenInfo  *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxWinPrivPtr   pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);
    unsigned int    m;
    XWindowChanges  c;

    DMX_UNWRAP(ChangeBorderWidth, dmxScreen, pScreen);
#if 1
    if (pScreen->ChangeBorderWidth)
	pScreen->ChangeBorderWidth(pWindow, width);
#endif

    /* NOTE: Do we need to check for on/off screen here? */

    if (pWinPriv->window) {
	/* Handle border width change on back-end server */
	m = CWBorderWidth;
	c.border_width = width;

	XConfigureWindow(dmxScreen->beDisplay, pWinPriv->window, m, &c);
	dmxSync(dmxScreen, False);
    }

    DMX_WRAP(ChangeBorderWidth, dmxChangeBorderWidth, dmxScreen, pScreen);
}

static void dmxDoSetShape(WindowPtr pWindow)
{
    ScreenPtr       pScreen = pWindow->drawable.pScreen;
    DMXScreenInfo  *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxWinPrivPtr   pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);
    int             nBox;
    BoxPtr          pBox;
    int             nRect;
    XRectangle     *pRect;
    XRectangle     *pRectFirst;

    /* First, set the bounding shape */
    if (wBoundingShape(pWindow)) {
	pBox = REGION_RECTS(wBoundingShape(pWindow));
	nRect = nBox = REGION_NUM_RECTS(wBoundingShape(pWindow));
	pRectFirst = pRect = xalloc(nRect * sizeof(*pRect));
	while (nBox--) {
	    pRect->x      = pBox->x1;
	    pRect->y      = pBox->y1;
	    pRect->width  = pBox->x2 - pBox->x1;
	    pRect->height = pBox->y2 - pBox->y1;
	    pBox++;
	    pRect++;
	}
	XShapeCombineRectangles(dmxScreen->beDisplay, pWinPriv->window,
				ShapeBounding, 0, 0,
				pRectFirst, nRect,
				ShapeSet, YXBanded);
	xfree(pRectFirst);
    } else {
	XShapeCombineMask(dmxScreen->beDisplay, pWinPriv->window,
			  ShapeBounding, 0, 0, None, ShapeSet);
    }

    /* Next, set the clip shape */
    if (wClipShape(pWindow)) {
	pBox = REGION_RECTS(wClipShape(pWindow));
	nRect = nBox = REGION_NUM_RECTS(wClipShape(pWindow));
	pRectFirst = pRect = xalloc(nRect * sizeof(*pRect));
	while (nBox--) {
	    pRect->x      = pBox->x1;
	    pRect->y      = pBox->y1;
	    pRect->width  = pBox->x2 - pBox->x1;
	    pRect->height = pBox->y2 - pBox->y1;
	    pBox++;
	    pRect++;
	}
	XShapeCombineRectangles(dmxScreen->beDisplay, pWinPriv->window,
				ShapeClip, 0, 0,
				pRectFirst, nRect,
				ShapeSet, YXBanded);
	xfree(pRectFirst);
    } else {
	XShapeCombineMask(dmxScreen->beDisplay, pWinPriv->window,
			  ShapeClip, 0, 0, None, ShapeSet);
    }

    if (XShapeInputSelected(dmxScreen->beDisplay, pWinPriv->window)) {
	ErrorF("Input selected for window %x on Screen %d\n",
	       (unsigned int)pWinPriv->window, pScreen->myNum);
    }
}

/** Set shape of \a pWindow on the back-end server. */
void dmxSetShape(WindowPtr pWindow)
{
    ScreenPtr       pScreen = pWindow->drawable.pScreen;
    DMXScreenInfo  *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxWinPrivPtr   pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);

    DMX_UNWRAP(SetShape, dmxScreen, pScreen);
#if 1
    if (pScreen->SetShape)
	pScreen->SetShape(pWindow);
#endif

    if (pWinPriv->window) {
	/* Handle setting the current shape on the back-end server */
	dmxDoSetShape(pWindow);
	dmxSync(dmxScreen, False);
    } else {
	pWinPriv->isShaped = TRUE;
    }

    DMX_WRAP(SetShape, dmxSetShape, dmxScreen, pScreen);
}