aboutsummaryrefslogtreecommitdiff
path: root/xorg-server/hw/dmx/dmxextension.c
blob: 91479e7a5586861df4a91bf626c3d1df8020c7cf (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
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
/*
 * Copyright 2003-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.
 */

/*
 * Author:
 *   Rickard E. (Rik) Faith <faith@redhat.com>
 *   Kevin E. Martin <kem@redhat.com>
 *
 */

/** \file
 * This file provides the only interface to the X server extension support
 * in programs/Xserver/Xext.  Those programs should only include dmxext.h
 */

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

#include "dmx.h"
#include "dmxinit.h"
#include "dmxextension.h"
#include "dmxwindow.h"
#include "dmxcb.h"
#include "dmxcursor.h"
#include "dmxpixmap.h"
#include "dmxgc.h"
#include "dmxfont.h"
#include "dmxcmap.h"
#ifdef RENDER
#include "dmxpict.h"
#endif
#include "dmxinput.h"
#include "dmxsync.h"
#include "dmxscrinit.h"
#include "input/dmxinputinit.h"

#include "windowstr.h"
#include "inputstr.h"                 /* For DeviceIntRec */
#include <X11/extensions/dmxproto.h>  /* For DMX_BAD_* */
#include "cursorstr.h"

#undef Xmalloc
#undef Xcalloc
#undef Xrealloc
#undef Xfree


/* The default font is declared in dix/globals.c, but is not included in
 * _any_ header files. */
extern FontPtr  defaultFont;
    
/** This routine provides information to the DMX protocol extension
 * about a particular screen. */
Bool dmxGetScreenAttributes(int physical, DMXScreenAttributesPtr attr)
{
    DMXScreenInfo *dmxScreen;

    if (physical < 0 || physical >= dmxNumScreens) return FALSE;

    dmxScreen = &dmxScreens[physical];
    attr->displayName         = dmxScreen->name;
#ifdef PANORAMIX
    attr->logicalScreen       = noPanoramiXExtension ? dmxScreen->index : 0;
#else
    attr->logicalScreen       = dmxScreen->index;
#endif

    attr->screenWindowWidth   = dmxScreen->scrnWidth;
    attr->screenWindowHeight  = dmxScreen->scrnHeight;
    attr->screenWindowXoffset = dmxScreen->scrnX;
    attr->screenWindowYoffset = dmxScreen->scrnY;

    attr->rootWindowWidth     = dmxScreen->rootWidth;
    attr->rootWindowHeight    = dmxScreen->rootHeight;
    attr->rootWindowXoffset   = dmxScreen->rootX;
    attr->rootWindowYoffset   = dmxScreen->rootY;

    attr->rootWindowXorigin   = dmxScreen->rootXOrigin;
    attr->rootWindowYorigin   = dmxScreen->rootYOrigin;

    return TRUE;
}

/** This routine provides information to the DMX protocol extension
 * about a particular window. */
Bool dmxGetWindowAttributes(WindowPtr pWindow, DMXWindowAttributesPtr attr)
{
    dmxWinPrivPtr pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);

    attr->screen         = pWindow->drawable.pScreen->myNum;
    attr->window         = pWinPriv->window;

    attr->pos.x          = pWindow->drawable.x;
    attr->pos.y          = pWindow->drawable.y;
    attr->pos.width      = pWindow->drawable.width;
    attr->pos.height     = pWindow->drawable.height;

    if (!pWinPriv->window || pWinPriv->offscreen) {
        attr->vis.x      = 0;
        attr->vis.y      = 0;
        attr->vis.height = 0;
        attr->vis.width  = 0;
        return pWinPriv->window ? TRUE : FALSE;
    }

                                /* Compute display-relative coordinates */
    attr->vis.x          = pWindow->drawable.x;
    attr->vis.y          = pWindow->drawable.y;
    attr->vis.width      = pWindow->drawable.width;
    attr->vis.height     = pWindow->drawable.height;

    if (attr->pos.x < 0) {
        attr->vis.x     -= attr->pos.x;
        attr->vis.width  = attr->pos.x + attr->pos.width - attr->vis.x;
    }
    if (attr->pos.x + attr->pos.width > pWindow->drawable.pScreen->width) {
        if (attr->pos.x < 0)
            attr->vis.width  = pWindow->drawable.pScreen->width;
        else
            attr->vis.width  = pWindow->drawable.pScreen->width - attr->pos.x;
    }
    if (attr->pos.y < 0) {
        attr->vis.y     -= attr->pos.y;
        attr->vis.height = attr->pos.y + attr->pos.height - attr->vis.y;
    }
    if (attr->pos.y + attr->pos.height > pWindow->drawable.pScreen->height) {
        if (attr->pos.y < 0)
            attr->vis.height = pWindow->drawable.pScreen->height;
        else
            attr->vis.height = pWindow->drawable.pScreen->height - attr->pos.y;
    }

                                /* Convert to window-relative coordinates */
    attr->vis.x -= attr->pos.x;
    attr->vis.y -= attr->pos.y;

    return TRUE;
}

void dmxGetDesktopAttributes(DMXDesktopAttributesPtr attr)
{
    attr->width  = dmxGlobalWidth;
    attr->height = dmxGlobalHeight;
    attr->shiftX = 0;            /* NOTE: The upper left hand corner of */
    attr->shiftY = 0;            /*       the desktop is always <0,0>. */
}

/** Return the total number of devices, not just #dmxNumInputs.  The
 * number returned should be the same as that returned by
 * XListInputDevices. */
int dmxGetInputCount(void)
{
    int i, total;
    
    for (total = i = 0; i < dmxNumInputs; i++) total += dmxInputs[i].numDevs;
    return total;
}

/** Return information about the device with id = \a deviceId.  This
 * information is primarily for the #ProcDMXGetInputAttributes()
 * function, which does not have access to the appropriate data
 * structure. */
int dmxGetInputAttributes(int deviceId, DMXInputAttributesPtr attr)
{
    int          i, j;
    DMXInputInfo *dmxInput;

    if (deviceId < 0) return -1;
    for (i = 0; i < dmxNumInputs; i++) {
        dmxInput = &dmxInputs[i];
        for (j = 0; j < dmxInput->numDevs; j++) {
            DMXLocalInputInfoPtr dmxLocal = dmxInput->devs[j];
            if (deviceId != dmxLocal->pDevice->id) continue;
            attr->isCore             = !!dmxLocal->isCore;
            attr->sendsCore          = !!dmxLocal->sendsCore;
            attr->detached           = !!dmxInput->detached;
            attr->physicalScreen     = -1;
            attr->physicalId         = -1;
            attr->name               = NULL;
            switch (dmxLocal->extType) {
            case DMX_LOCAL_TYPE_LOCAL:
                attr->inputType      = 0;
                break;
            case DMX_LOCAL_TYPE_CONSOLE:
                attr->inputType      = 1;
                attr->name           = dmxInput->name;
                attr->physicalId     = dmxLocal->deviceId;
                break;
            case DMX_LOCAL_TYPE_BACKEND:
            case DMX_LOCAL_TYPE_COMMON:
                attr->inputType      = 2;
                attr->physicalScreen = dmxInput->scrnIdx;
                attr->name           = dmxInput->name;
                attr->physicalId     = dmxLocal->deviceId;
                break;
            }
            return 0;           /* Success */
        }
    }
    return -1;                  /* Failure */
}

/** Reinitialized the cursor boundaries. */
static void dmxAdjustCursorBoundaries(void)
{
    int           i;

    dmxReInitOrigins();
    dmxInitOverlap();
    dmxComputeWidthHeight(DMX_NO_RECOMPUTE_BOUNDING_BOX);
    dmxConnectionBlockCallback();
    for (i = 0; i < dmxNumInputs; i++) {
        DMXInputInfo *dmxInput = &dmxInputs[i];
	if (!dmxInput->detached) dmxInputReInit(dmxInput);
    }

    dmxCheckCursor();

    for (i = 0; i < dmxNumInputs; i++) {
        DMXInputInfo *dmxInput = &dmxInputs[i];
	if (!dmxInput->detached) dmxInputLateReInit(dmxInput);
    }
}

/** Add an input with the specified attributes.  If the input is added,
 * the physical id is returned in \a deviceId. */
int dmxAddInput(DMXInputAttributesPtr attr, int *id)
{
    int retcode = BadValue;

    if (attr->inputType == 1)   /* console */
        retcode = dmxInputAttachConsole(attr->name, attr->sendsCore, id);
    else if (attr->inputType == 2)   /* backend */
        retcode = dmxInputAttachBackend(attr->physicalScreen,
                                        attr->sendsCore,id);

    if (retcode == Success) {
        /* Adjust the cursor boundaries */
        dmxAdjustCursorBoundaries();

        /* Force completion of the changes */
        dmxSync(NULL, TRUE);
    }

    return retcode;
}

/** Remove the input with physical id \a id. */
int dmxRemoveInput(int id)
{
    return dmxInputDetachId(id);
}

/** Return the value of #dmxNumScreens -- the total number of backend
 * screens in use (these are logical screens and may be larger than the
 * number of backend displays). */
unsigned long dmxGetNumScreens(void)
{
    return dmxNumScreens;
}

/** Make sure that #dmxCreateAndRealizeWindow has been called for \a
 * pWindow. */
void dmxForceWindowCreation(WindowPtr pWindow)
{
    dmxWinPrivPtr pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);
    if (!pWinPriv->window) dmxCreateAndRealizeWindow(pWindow, TRUE);
}

/** Flush pending syncs for all screens. */
void dmxFlushPendingSyncs(void)
{
    dmxSync(NULL, TRUE);
}

/** Update DMX's screen resources to match those of the newly moved
 *  and/or resized "root" window. */
void dmxUpdateScreenResources(ScreenPtr pScreen, int x, int y, int w, int h)
{
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    WindowPtr      pRoot     = WindowTable[pScreen->myNum];
    WindowPtr      pChild;
    Bool           anyMarked = FALSE;

    /* Handle special case where width and/or height are zero */
    if (w == 0 || h == 0) {
	w = 1;
	h = 1;
    }

    /* Change screen size */
    pScreen->width  = w;
    pScreen->height = h;

    /* Reset the root window's drawable's size */
    pRoot->drawable.width  = w;
    pRoot->drawable.height = h;

    /* Set the root window's new winSize and borderSize */
    pRoot->winSize.extents.x1 = 0;
    pRoot->winSize.extents.y1 = 0;
    pRoot->winSize.extents.x2 = w;
    pRoot->winSize.extents.y2 = h;

    pRoot->borderSize.extents.x1 = 0;
    pRoot->borderSize.extents.y1 = 0;
    pRoot->borderSize.extents.x2 = w;
    pRoot->borderSize.extents.y2 = h;

    /* Recompute this screen's mmWidth & mmHeight */
    pScreen->mmWidth =
	(w * 254 + dmxScreen->beXDPI * 5) / (dmxScreen->beXDPI * 10);
    pScreen->mmHeight =
	(h * 254 + dmxScreen->beYDPI * 5) / (dmxScreen->beYDPI * 10);

    /* Recompute this screen's window's clip rects as follows: */
    /*   1. Mark all of root's children's windows */
    for (pChild = pRoot->firstChild; pChild; pChild = pChild->nextSib)
	anyMarked |= pScreen->MarkOverlappedWindows(pChild, pChild,
						    (WindowPtr *)NULL);

    /*   2. Set the root window's borderClip */
    pRoot->borderClip.extents.x1 = 0;
    pRoot->borderClip.extents.y1 = 0;
    pRoot->borderClip.extents.x2 = w;
    pRoot->borderClip.extents.y2 = h;

    /*   3. Set the root window's clipList */
    if (anyMarked) {
	/* If any windows have been marked, set the root window's
	 * clipList to be broken since it will be recalculated in
	 * ValidateTree()
	 */
	REGION_BREAK(pScreen, &pRoot->clipList);
    } else {
	/* Otherwise, we just set it directly since there are no
	 * windows visible on this screen
	 */
	pRoot->clipList.extents.x1 = 0;
	pRoot->clipList.extents.y1 = 0;
	pRoot->clipList.extents.x2 = w;
	pRoot->clipList.extents.y2 = h;
    }

    /*   4. Revalidate all clip rects and generate expose events */
    if (anyMarked) {
	pScreen->ValidateTree(pRoot, NULL, VTBroken);
	pScreen->HandleExposures(pRoot);
	if (pScreen->PostValidateTree)
	    pScreen->PostValidateTree(pRoot, NULL, VTBroken);
    }
}

#ifdef PANORAMIX
#include "panoramiXsrv.h"

/** Change the "screen" window attributes by resizing the actual window
 *  on the back-end display (if necessary). */
static void dmxConfigureScreenWindow(int idx,
				     int x, int y, int w, int h)
{
    DMXScreenInfo *dmxScreen = &dmxScreens[idx];
    ScreenPtr      pScreen   = screenInfo.screens[idx];

    /* Resize "screen" window */
    if (dmxScreen->scrnX      != x ||
	dmxScreen->scrnY      != y ||
	dmxScreen->scrnWidth  != w ||
	dmxScreen->scrnHeight != h) {
	dmxResizeScreenWindow(pScreen, x, y, w, h);
    }

    /* Change "screen" window values */
    dmxScreen->scrnX      = x;
    dmxScreen->scrnY      = y;
    dmxScreen->scrnWidth  = w;
    dmxScreen->scrnHeight = h;
}
				     
/** Change the "root" window position and size by resizing the actual
 *  window on the back-end display (if necessary) and updating all of
 *  DMX's resources by calling #dmxUpdateScreenResources. */
static void dmxConfigureRootWindow(int idx, int x, int y, int w, int h)
{
    DMXScreenInfo *dmxScreen = &dmxScreens[idx];
    WindowPtr      pRoot     = WindowTable[idx];

    /* NOTE: Either this function or the ones that it calls must handle
     * the case where w == 0 || h == 0.  Currently, the functions that
     * this one calls handle that case. */

    /* 1. Resize "root" window */
    if (dmxScreen->rootX      != x ||
	dmxScreen->rootY      != y ||
	dmxScreen->rootWidth  != w ||
	dmxScreen->rootHeight != h) {
	dmxResizeRootWindow(pRoot, x, y, w, h);
    }

    /* 2. Update all of the screen's resources associated with this root
     *    window */
    if (dmxScreen->rootWidth  != w ||
	dmxScreen->rootHeight != h) {
	dmxUpdateScreenResources(screenInfo.screens[idx], x, y, w, h);
    }

    /* Change "root" window values */
    dmxScreen->rootX      = x;
    dmxScreen->rootY      = y;
    dmxScreen->rootWidth  = w;
    dmxScreen->rootHeight = h;
}

/** Change the "root" window's origin by updating DMX's internal data
 *  structures (dix and Xinerama) to use the new origin and adjust the
 *  positions of windows that overlap this "root" window. */
static void dmxSetRootWindowOrigin(int idx, int x, int y)
{
    DMXScreenInfo *dmxScreen = &dmxScreens[idx];
    ScreenPtr      pScreen   = screenInfo.screens[idx];
    WindowPtr      pRoot     = WindowTable[idx];
    WindowPtr      pChild;
    int            xoff;
    int            yoff;

    /* Change "root" window's origin */
    dmxScreen->rootXOrigin = x;
    dmxScreen->rootYOrigin = y;

    /* Compute offsets here in case <x,y> has been changed above */
    xoff = x - dixScreenOrigins[idx].x;
    yoff = y - dixScreenOrigins[idx].y;

    /* Adjust the root window's position in dixScreenOrigins */
    dixScreenOrigins[idx].x = dmxScreen->rootXOrigin;
    dixScreenOrigins[idx].y = dmxScreen->rootYOrigin;

    /* Recalculate the Xinerama regions and data structs */
    XineramaReinitData(pScreen);

    /* Adjust each of the root window's children */
    if (!idx) ReinitializeRootWindow(WindowTable[0], xoff, yoff);
    pChild = pRoot->firstChild;
    while (pChild) {
	/* Adjust child window's position */
	pScreen->MoveWindow(pChild,
			    pChild->origin.x - wBorderWidth(pChild) - xoff,
			    pChild->origin.y - wBorderWidth(pChild) - yoff,
			    pChild->nextSib,
			    VTMove);

	/* Note that the call to MoveWindow will eventually call
	 * dmxPositionWindow which will automatically create a
	 * window if it is now exposed on screen (for lazy window
	 * creation optimization) and it will properly set the
	 * offscreen flag.
	 */

	pChild = pChild->nextSib;
    }
}

/** Configure the attributes of each "screen" and "root" window. */
int dmxConfigureScreenWindows(int nscreens,
			      CARD32 *screens,
			      DMXScreenAttributesPtr attribs,
			      int *errorScreen)
{
    int           i;

    for (i = 0; i < nscreens; i++) {
	DMXScreenAttributesPtr  attr      = &attribs[i];
	int                     idx       = screens[i];
	DMXScreenInfo          *dmxScreen = &dmxScreens[idx];

	if (errorScreen) *errorScreen = i;

	if (!dmxScreen->beDisplay) return DMX_BAD_VALUE;

	/* Check for illegal values */
	if (idx < 0 || idx >= dmxNumScreens) return BadValue;

	/* The "screen" and "root" windows must have valid sizes */
	if (attr->screenWindowWidth <= 0 || attr->screenWindowHeight <= 0 ||
	    attr->rootWindowWidth   <  0 || attr->rootWindowHeight   <  0)
	    return DMX_BAD_VALUE;

	/* The "screen" window must fit entirely within the BE display */
	if (attr->screenWindowXoffset < 0 ||
	    attr->screenWindowYoffset < 0 ||
	    attr->screenWindowXoffset
	    + attr->screenWindowWidth  > (unsigned)dmxScreen->beWidth ||
	    attr->screenWindowYoffset
	    + attr->screenWindowHeight > (unsigned)dmxScreen->beHeight)
	    return DMX_BAD_VALUE;

	/* The "root" window must fit entirely within the "screen" window */
	if (attr->rootWindowXoffset < 0 ||
	    attr->rootWindowYoffset < 0 ||
	    attr->rootWindowXoffset
	    + attr->rootWindowWidth  > attr->screenWindowWidth ||
	    attr->rootWindowYoffset
	    + attr->rootWindowHeight > attr->screenWindowHeight)
	    return DMX_BAD_VALUE;

	/* The "root" window must not expose unaddressable coordinates */
	if (attr->rootWindowXorigin < 0 ||
	    attr->rootWindowYorigin < 0 ||
	    attr->rootWindowXorigin + attr->rootWindowWidth  > 32767 ||
	    attr->rootWindowYorigin + attr->rootWindowHeight > 32767)
	    return DMX_BAD_VALUE;

	/* The "root" window must fit within the global bounding box */
	if (attr->rootWindowXorigin
	    + attr->rootWindowWidth > (unsigned)dmxGlobalWidth ||
	    attr->rootWindowYorigin
	    + attr->rootWindowHeight > (unsigned)dmxGlobalHeight)
	    return DMX_BAD_VALUE;

	/* FIXME: Handle the rest of the illegal value checking */
    }

    /* No illegal values found */
    if (errorScreen) *errorScreen = 0;

    for (i = 0; i < nscreens; i++) {
	DMXScreenAttributesPtr  attr      = &attribs[i];
	int                     idx       = screens[i];
	DMXScreenInfo          *dmxScreen = &dmxScreens[idx];

	dmxLog(dmxInfo, "Changing screen #%d attributes "
	       "from %dx%d+%d+%d %dx%d+%d+%d +%d+%d "
	       "to %dx%d+%d+%d %dx%d+%d+%d +%d+%d\n",
	       idx,
	       dmxScreen->scrnWidth,      dmxScreen->scrnHeight,
	       dmxScreen->scrnX,          dmxScreen->scrnY,
	       dmxScreen->rootWidth,      dmxScreen->rootHeight,
	       dmxScreen->rootX,          dmxScreen->rootY,
	       dmxScreen->rootXOrigin,    dmxScreen->rootYOrigin,
	       attr->screenWindowWidth,   attr->screenWindowHeight,
	       attr->screenWindowXoffset, attr->screenWindowYoffset,
	       attr->rootWindowWidth,     attr->rootWindowHeight,
	       attr->rootWindowXoffset,   attr->rootWindowYoffset,
	       attr->rootWindowXorigin,   attr->rootWindowYorigin);

	/* Configure "screen" window */
	dmxConfigureScreenWindow(idx,
				 attr->screenWindowXoffset,
				 attr->screenWindowYoffset,
				 attr->screenWindowWidth,
				 attr->screenWindowHeight);

	/* Configure "root" window */
	dmxConfigureRootWindow(idx,
			       attr->rootWindowXoffset,
			       attr->rootWindowYoffset,
			       attr->rootWindowWidth,
			       attr->rootWindowHeight);


	/* Set "root" window's origin */
	dmxSetRootWindowOrigin(idx,
			       attr->rootWindowXorigin,
			       attr->rootWindowYorigin);
    }

    /* Adjust the cursor boundaries */
    dmxAdjustCursorBoundaries();

    /* Force completion of the changes */
    dmxSync(NULL, TRUE);

    return Success;
}

/** Configure the attributes of the global desktop. */
int dmxConfigureDesktop(DMXDesktopAttributesPtr attribs)
{
    if (attribs->width  <= 0 || attribs->width  >= 32767 ||
	attribs->height <= 0 || attribs->height >= 32767)
	return DMX_BAD_VALUE;

    /* If the desktop is shrinking, adjust the "root" windows on each
     * "screen" window to only show the visible desktop.  Also, handle
     * the special case where the desktop shrinks such that the it no
     * longer overlaps an portion of a "screen" window. */
    if (attribs->width < dmxGlobalWidth || attribs->height < dmxGlobalHeight) {
	int   i;
	for (i = 0; i < dmxNumScreens; i++) {
	    DMXScreenInfo *dmxScreen = &dmxScreens[i];
	    if (dmxScreen->rootXOrigin
		+ dmxScreen->rootWidth  > attribs->width ||
		dmxScreen->rootYOrigin
		+ dmxScreen->rootHeight > attribs->height) {
		int  w, h;
		if ((w = attribs->width  - dmxScreen->rootXOrigin) < 0) w = 0;
		if ((h = attribs->height - dmxScreen->rootYOrigin) < 0) h = 0;
		if (w > dmxScreen->scrnWidth)  w = dmxScreen->scrnWidth;
		if (h > dmxScreen->scrnHeight) h = dmxScreen->scrnHeight;
		if (w > dmxScreen->rootWidth)  w = dmxScreen->rootWidth;
		if (h > dmxScreen->rootHeight) h = dmxScreen->rootHeight;
		dmxConfigureRootWindow(i,
				       dmxScreen->rootX,
				       dmxScreen->rootY,
				       w, h);
	    }
	}
    }

    /* Set the global width/height */
    dmxSetWidthHeight(attribs->width, attribs->height);

    /* Handle shift[XY] changes */
    if (attribs->shiftX || attribs->shiftY) {
	int   i;
	for (i = 0; i < dmxNumScreens; i++) {
	    ScreenPtr  pScreen = screenInfo.screens[i];
	    WindowPtr  pChild  = WindowTable[i]->firstChild;
	    while (pChild) {
		/* Adjust child window's position */
		pScreen->MoveWindow(pChild,
				    pChild->origin.x - wBorderWidth(pChild)
				    - attribs->shiftX,
				    pChild->origin.y - wBorderWidth(pChild)
				    - attribs->shiftY,
				    pChild->nextSib,
				    VTMove);

		/* Note that the call to MoveWindow will eventually call
		 * dmxPositionWindow which will automatically create a
		 * window if it is now exposed on screen (for lazy
		 * window creation optimization) and it will properly
		 * set the offscreen flag.
		 */

		pChild = pChild->nextSib;
	    }
	}
    }

    /* Update connection block, Xinerama, etc. -- these appears to
     * already be handled in dmxConnectionBlockCallback(), which is
     * called from dmxAdjustCursorBoundaries() [below]. */

    /* Adjust the cursor boundaries */
    dmxAdjustCursorBoundaries();

    /* Force completion of the changes */
    dmxSync(NULL, TRUE);

    return Success;
}
#endif

/** Create the scratch GCs per depth. */
static void dmxBECreateScratchGCs(int scrnNum)
{
    ScreenPtr  pScreen = screenInfo.screens[scrnNum];
    GCPtr     *ppGC    = pScreen->GCperDepth;
    int        i;

    for (i = 0; i <= pScreen->numDepths; i++)
	dmxBECreateGC(pScreen, ppGC[i]);
}

#ifdef PANORAMIX
static Bool FoundPixImage;

/** Search the Xinerama XRT_PIXMAP resources for the pixmap that needs
 *  to have its image restored.  When it is found, see if there is
 *  another screen with the same image.  If so, copy the pixmap image
 *  from the existing screen to the newly created pixmap. */
static void dmxBERestorePixmapImage(pointer value, XID id, RESTYPE type,
				    pointer p)
{
    if ((type & TypeMask) == (XRT_PIXMAP & TypeMask)) {
	PixmapPtr      pDst     = (PixmapPtr)p;
	int            idx      = pDst->drawable.pScreen->myNum;
	PanoramiXRes  *pXinPix  = (PanoramiXRes *)value;
	PixmapPtr      pPix;
	int            i;

	pPix = (PixmapPtr)LookupIDByType(pXinPix->info[idx].id, RT_PIXMAP);
	if (pPix != pDst) return; /* Not a match.... Next! */

	for (i = 0; i < PanoramiXNumScreens; i++) {
	    PixmapPtr      pSrc;
	    dmxPixPrivPtr  pSrcPriv = NULL;

	    if (i == idx) continue; /* Self replication is bad */

	    pSrc =
		(PixmapPtr)LookupIDByType(pXinPix->info[i].id, RT_PIXMAP);
	    pSrcPriv = DMX_GET_PIXMAP_PRIV(pSrc);
	    if (pSrcPriv->pixmap) {
		DMXScreenInfo *dmxSrcScreen = &dmxScreens[i];
		DMXScreenInfo *dmxDstScreen = &dmxScreens[idx];
		dmxPixPrivPtr  pDstPriv = DMX_GET_PIXMAP_PRIV(pDst);
		XImage        *img;
		int            j;
		XlibGC         gc = NULL;

		/* This should never happen, but just in case.... */
		if (pSrc->drawable.width  != pDst->drawable.width ||
		    pSrc->drawable.height != pDst->drawable.height)
		    return;

		/* Copy from src pixmap to dst pixmap */
		img = XGetImage(dmxSrcScreen->beDisplay,
				pSrcPriv->pixmap,
				0, 0,
				pSrc->drawable.width, pSrc->drawable.height,
				-1,
				ZPixmap);

		for (j = 0; j < dmxDstScreen->beNumPixmapFormats; j++) {
		    if (dmxDstScreen->bePixmapFormats[j].depth == img->depth) {
			unsigned long  m;
			XGCValues      v;

			m = GCFunction | GCPlaneMask | GCClipMask;
			v.function = GXcopy;
			v.plane_mask = AllPlanes;
			v.clip_mask = None;

			gc = XCreateGC(dmxDstScreen->beDisplay,
				       dmxDstScreen->scrnDefDrawables[j],
				       m, &v);
			break;
		    }
		}

		if (gc) {
		    XPutImage(dmxDstScreen->beDisplay,
			      pDstPriv->pixmap,
			      gc, img, 0, 0, 0, 0,
			      pDst->drawable.width, pDst->drawable.height);
		    XFreeGC(dmxDstScreen->beDisplay, gc);
		    FoundPixImage = True;
		} else {
		    dmxLog(dmxWarning, "Could not create GC\n");
		}

		XDestroyImage(img);
		return;
	    }
	}
    }
}
#endif

/** Restore the pixmap image either from another screen or from an image
 *  that was saved when the screen was previously detached. */
static void dmxBERestorePixmap(PixmapPtr pPixmap)
{
#ifdef PANORAMIX
    int i;

    /* If Xinerama is not active, there's nothing we can do (see comment
     * in #else below for more info). */
    if (noPanoramiXExtension) {
	dmxLog(dmxWarning, "Cannot restore pixmap image\n");
	return;
    }

    FoundPixImage = False;
    for (i = currentMaxClients; --i >= 0; )
	if (clients[i])
	    FindAllClientResources(clients[i], dmxBERestorePixmapImage,
				   (pointer)pPixmap);

    /* No corresponding pixmap image was found on other screens, so we
     * need to copy it from the saved image when the screen was detached
     * (if available). */
    if (!FoundPixImage) {
	dmxPixPrivPtr  pPixPriv = DMX_GET_PIXMAP_PRIV(pPixmap);

	if (pPixPriv->detachedImage) {
	    ScreenPtr      pScreen   = pPixmap->drawable.pScreen;
	    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
	    XlibGC         gc        = NULL;

	    for (i = 0; i < dmxScreen->beNumPixmapFormats; i++) {
		if (dmxScreen->bePixmapFormats[i].depth ==
		    pPixPriv->detachedImage->depth) {
		    unsigned long  m;
		    XGCValues      v;

		    m = GCFunction | GCPlaneMask | GCClipMask;
		    v.function = GXcopy;
		    v.plane_mask = AllPlanes;
		    v.clip_mask = None;

		    gc = XCreateGC(dmxScreen->beDisplay,
				   dmxScreen->scrnDefDrawables[i],
				   m, &v);
		    break;
		}
	    }

	    if (gc) {
		XPutImage(dmxScreen->beDisplay,
			  pPixPriv->pixmap,
			  gc,
			  pPixPriv->detachedImage,
			  0, 0, 0, 0,
		      pPixmap->drawable.width, pPixmap->drawable.height);
		XFreeGC(dmxScreen->beDisplay, gc);
	    } else {
		dmxLog(dmxWarning, "Cannot restore pixmap image\n");
	    }

	    XDestroyImage(pPixPriv->detachedImage);
	    pPixPriv->detachedImage = NULL;
	} else {
	    dmxLog(dmxWarning, "Cannot restore pixmap image\n");
	}
    }
#else
    /* If Xinerama is not enabled, then there is no other copy of the
     * pixmap image that we can restore.  Saving all pixmap data is not
     * a feasible option since there is no mechanism for updating pixmap
     * data when a screen is detached, which means that the data that
     * was previously saved would most likely be out of date. */
    dmxLog(dmxWarning, "Cannot restore pixmap image\n");
    return;
#endif
}

/** Create resources on the back-end server.  This function is called
 *  from #dmxAttachScreen() via the dix layer's FindAllResources
 *  function.  It walks all resources, compares them to the screen
 *  number passed in as \a n and calls the appropriate DMX function to
 *  create the associated resource on the back-end server. */
static void dmxBECreateResources(pointer value, XID id, RESTYPE type,
				 pointer n)
{
    int        scrnNum = (int)n;
    ScreenPtr  pScreen = screenInfo.screens[scrnNum];

    if ((type & TypeMask) == (RT_WINDOW & TypeMask)) {
	/* Window resources are created below in dmxBECreateWindowTree */
    } else if ((type & TypeMask) == (RT_PIXMAP & TypeMask)) {
	PixmapPtr  pPix = value;
	if (pPix->drawable.pScreen->myNum == scrnNum) {
	    dmxBECreatePixmap(pPix);
	    dmxBERestorePixmap(pPix);
	}
    } else if ((type & TypeMask) == (RT_GC & TypeMask)) {
	GCPtr  pGC = value;
	if (pGC->pScreen->myNum == scrnNum) {
	    /* Create the GC on the back-end server */
	    dmxBECreateGC(pScreen, pGC);
	    /* Create any pixmaps associated with this GC */
	    if (!pGC->tileIsPixel) {
		dmxBECreatePixmap(pGC->tile.pixmap);
		dmxBERestorePixmap(pGC->tile.pixmap);
	    }
	    if (pGC->stipple != pScreen->PixmapPerDepth[0]) {
		dmxBECreatePixmap(pGC->stipple);
		dmxBERestorePixmap(pGC->stipple);
	    }
	    if (pGC->font != defaultFont) {
		(void)dmxBELoadFont(pScreen, pGC->font);
	    }
	    /* Update the GC on the back-end server */
	    dmxChangeGC(pGC, -1L);
	}
    } else if ((type & TypeMask) == (RT_FONT & TypeMask)) {
	(void)dmxBELoadFont(pScreen, (FontPtr)value);
    } else if ((type & TypeMask) == (RT_CURSOR & TypeMask)) {
	dmxBECreateCursor(pScreen, (CursorPtr)value);
    } else if ((type & TypeMask) == (RT_COLORMAP & TypeMask)) {
	ColormapPtr  pCmap = value;
	if (pCmap->pScreen->myNum == scrnNum)
	    (void)dmxBECreateColormap((ColormapPtr)value);
#if 0
#ifdef RENDER
    /* TODO: Recreate Picture and GlyphSet resources */
    } else if ((type & TypeMask) == (PictureType & TypeMask)) {
	/* Picture resources are created when windows are created */
    } else if ((type & TypeMask) == (GlyphSetType & TypeMask)) {
	dmxBEFreeGlyphSet(pScreen, (GlyphSetPtr)value);
#endif
#endif
    } else {
	/* Other resource types??? */
    }
}

/** Create window hierachy on back-end server.  The window tree is
 *  created in a special order (bottom most subwindow first) so that the
 *  #dmxCreateNonRootWindow() function does not need to recursively call
 *  itself to create each window's parents.  This is required so that we
 *  have the opportunity to create each window's border and background
 *  pixmaps (where appropriate) before the window is created. */
static void dmxBECreateWindowTree(int idx)
{
    DMXScreenInfo *dmxScreen = &dmxScreens[idx];
    WindowPtr      pRoot     = WindowTable[idx];
    dmxWinPrivPtr  pWinPriv  = DMX_GET_WINDOW_PRIV(pRoot);
    WindowPtr      pWin;

    /* Create the pixmaps associated with the root window */
    if (!pRoot->borderIsPixel) {
	dmxBECreatePixmap(pRoot->border.pixmap);
	dmxBERestorePixmap(pRoot->border.pixmap);
    }
    if (pRoot->backgroundState == BackgroundPixmap) {
	dmxBECreatePixmap(pRoot->background.pixmap);
	dmxBERestorePixmap(pRoot->background.pixmap);
    }

    /* Create root window first */
    dmxScreen->rootWin = pWinPriv->window = dmxCreateRootWindow(pRoot);
    XMapWindow(dmxScreen->beDisplay, dmxScreen->rootWin);

    pWin = pRoot->lastChild;
    while (pWin) {
	pWinPriv = DMX_GET_WINDOW_PRIV(pWin);

	/* Create the pixmaps regardless of whether or not the
	 * window is created or not due to lazy window creation.
	 */
	if (!pWin->borderIsPixel) {
	    dmxBECreatePixmap(pWin->border.pixmap);
	    dmxBERestorePixmap(pWin->border.pixmap);
	}
	if (pWin->backgroundState == BackgroundPixmap) {
	    dmxBECreatePixmap(pWin->background.pixmap);
	    dmxBERestorePixmap(pWin->background.pixmap);
	}

	/* Reset the window attributes */
	dmxGetDefaultWindowAttributes(pWin,
				      &pWinPriv->cmap,
				      &pWinPriv->visual);

	/* Create the window */
	if (pWinPriv->mapped && !pWinPriv->offscreen)
	    dmxCreateAndRealizeWindow(pWin, TRUE);

	/* Next, create the bottom-most child */
	if (pWin->lastChild) {
	    pWin = pWin->lastChild;
	    continue;
	}

	/* If the window has no children, move on to the next higher window */
	while (!pWin->prevSib && (pWin != pRoot))
	    pWin = pWin->parent;

	if (pWin->prevSib) {
	    pWin = pWin->prevSib;
	    continue;
	}

	/* When we reach the root window, we are finished */
	if (pWin == pRoot)
	    break;
    }
}

/* Refresh screen by generating exposure events for all windows */
static void dmxForceExposures(int idx)
{
    ScreenPtr      pScreen   = screenInfo.screens[idx];
    WindowPtr  pRoot     = WindowTable[idx];
    Bool       anyMarked = FALSE;
    WindowPtr  pChild;

    for (pChild = pRoot->firstChild; pChild; pChild = pChild->nextSib)
	anyMarked |= pScreen->MarkOverlappedWindows(pChild, pChild,
						    (WindowPtr *)NULL);
    if (anyMarked) {
	/* If any windows have been marked, set the root window's
	 * clipList to be broken since it will be recalculated in
	 * ValidateTree()
	 */
	REGION_BREAK(pScreen, &pRoot->clipList);
	pScreen->ValidateTree(pRoot, NULL, VTBroken);
	pScreen->HandleExposures(pRoot);
	if (pScreen->PostValidateTree)
	    pScreen->PostValidateTree(pRoot, NULL, VTBroken);
    }
}

/** Compare the new and old screens to see if they are compatible. */
static Bool dmxCompareScreens(DMXScreenInfo *new, DMXScreenInfo *old)
{
    int i;

    if (new->beWidth != old->beWidth) return FALSE;
    if (new->beHeight != old->beHeight) return FALSE;
    if (new->beDepth != old->beDepth) return FALSE;
    if (new->beBPP != old->beBPP) return FALSE;

    if (new->beNumDepths != old->beNumDepths) return FALSE;
    for (i = 0; i < old->beNumDepths; i++)
	if (new->beDepths[i] != old->beDepths[i]) return FALSE;

    if (new->beNumPixmapFormats != old->beNumPixmapFormats) return FALSE;
    for (i = 0; i < old->beNumPixmapFormats; i++) {
	if (new->bePixmapFormats[i].depth !=
	    old->bePixmapFormats[i].depth) return FALSE;
	if (new->bePixmapFormats[i].bits_per_pixel !=
	    old->bePixmapFormats[i].bits_per_pixel) return FALSE;
	if (new->bePixmapFormats[i].scanline_pad !=
	    old->bePixmapFormats[i].scanline_pad) return FALSE;
    }

    if (new->beNumVisuals != old->beNumVisuals) return FALSE;
    for (i = 0; i < old->beNumVisuals; i++) {
	if (new->beVisuals[i].visualid !=
	    old->beVisuals[i].visualid) return FALSE;
	if (new->beVisuals[i].screen !=
	    old->beVisuals[i].screen) return FALSE;
	if (new->beVisuals[i].depth !=
	    old->beVisuals[i].depth) return FALSE;
	if (new->beVisuals[i].class !=
	    old->beVisuals[i].class) return FALSE;
	if (new->beVisuals[i].red_mask !=
	    old->beVisuals[i].red_mask) return FALSE;
	if (new->beVisuals[i].green_mask !=
	    old->beVisuals[i].green_mask) return FALSE;
	if (new->beVisuals[i].blue_mask !=
	    old->beVisuals[i].blue_mask) return FALSE;
	if (new->beVisuals[i].colormap_size !=
	    old->beVisuals[i].colormap_size) return FALSE;
	if (new->beVisuals[i].bits_per_rgb !=
	    old->beVisuals[i].bits_per_rgb) return FALSE;
    }

    if (new->beDefVisualIndex != old->beDefVisualIndex) return FALSE;

    return TRUE;
}

#ifdef RENDER
/** Restore Render's picture */
static void dmxBERestoreRenderPict(pointer value, XID id, pointer n)
{
    PicturePtr   pPicture = value;               /* The picture */
    DrawablePtr  pDraw    = pPicture->pDrawable; /* The picture's drawable */
    int          scrnNum  = (int)n;

    if (pDraw->pScreen->myNum != scrnNum) {
	/* Picture not on the screen we are restoring*/
	return;
    }

    if (pDraw->type == DRAWABLE_PIXMAP) {
	PixmapPtr  pPixmap = (PixmapPtr)pDraw;
	
	/* Create and restore the pixmap drawable */
	dmxBECreatePixmap(pPixmap);
	dmxBERestorePixmap(pPixmap);
    }

    dmxBECreatePicture(pPicture);
}

/** Restore Render's glyphs */
static void dmxBERestoreRenderGlyph(pointer value, XID id, pointer n)
{
    GlyphSetPtr      glyphSet   = value;
    int              scrnNum    = (int)n;
    dmxGlyphPrivPtr  glyphPriv  = DMX_GET_GLYPH_PRIV(glyphSet);
    DMXScreenInfo   *dmxScreen  = &dmxScreens[scrnNum];
    GlyphRefPtr      table;
    char            *images;
    Glyph           *gids;
    XGlyphInfo      *glyphs;
    char            *pos;
    int              beret;
    int              len_images = 0;
    int              i;
    int              ctr;

    if (glyphPriv->glyphSets[scrnNum]) {
	/* Only restore glyphs on the screen we are attaching */
	return;
    }

    /* First we must create the glyph set on the backend. */
    if ((beret = dmxBECreateGlyphSet(scrnNum, glyphSet)) != Success) {
	dmxLog(dmxWarning,
	       "\tdmxBERestoreRenderGlyph failed to create glyphset!\n");
	return;
    }

    /* Now for the complex part, restore the glyph data */
    table = glyphSet->hash.table;

    /* We need to know how much memory to allocate for this part */
    for (i = 0; i < glyphSet->hash.hashSet->size; i++) {
	GlyphRefPtr  gr = &table[i];
	GlyphPtr     gl = gr->glyph;

	if (!gl || gl == DeletedGlyph) continue;
	len_images += gl->size - sizeof(gl->info);
    }

    /* Now allocate the memory we need */
    images = xcalloc(len_images, sizeof(char));
    gids   = xalloc(glyphSet->hash.tableEntries*sizeof(Glyph));
    glyphs = xalloc(glyphSet->hash.tableEntries*sizeof(XGlyphInfo));

    pos = images;
    ctr = 0;
    
    /* Fill the allocated memory with the proper data */
    for (i = 0; i < glyphSet->hash.hashSet->size; i++) {
	GlyphRefPtr  gr = &table[i];
	GlyphPtr     gl = gr->glyph;

	if (!gl || gl == DeletedGlyph) continue;

	/* First lets put the data into gids */
	gids[ctr] = gr->signature;

	/* Next do the glyphs data structures */
	glyphs[ctr].width  = gl->info.width;
	glyphs[ctr].height = gl->info.height;
	glyphs[ctr].x      = gl->info.x;
	glyphs[ctr].y      = gl->info.y;
	glyphs[ctr].xOff   = gl->info.xOff;
	glyphs[ctr].yOff   = gl->info.yOff;

	/* Copy the images from the DIX's data into the buffer */
	memcpy(pos, gl+1, gl->size - sizeof(gl->info));
	pos += gl->size - sizeof(gl->info);
	ctr++;
    }
    
    /* Now restore the glyph data */
    XRenderAddGlyphs(dmxScreen->beDisplay, glyphPriv->glyphSets[scrnNum],
		     gids,glyphs, glyphSet->hash.tableEntries, images,
		     len_images);

    /* Clean up */
    xfree(len_images);
    xfree(gids);
    xfree(glyphs);    
}
#endif

/** Reattach previously detached back-end screen. */
int dmxAttachScreen(int idx, DMXScreenAttributesPtr attr)
{
    ScreenPtr      pScreen;
    DMXScreenInfo *dmxScreen;
    CARD32         scrnNum   = idx;
    DMXScreenInfo  oldDMXScreen;
    int            i;

    /* Return failure if dynamic addition/removal of screens is disabled */
    if (!dmxAddRemoveScreens) {
	dmxLog(dmxWarning,
	       "Attempting to add a screen, but the AddRemoveScreen\n");
	dmxLog(dmxWarning,
	       "extension has not been enabled.  To enable this extension\n");
	dmxLog(dmxWarning,
	       "add the \"-addremovescreens\" option either to the command\n");
	dmxLog(dmxWarning,
	       "line or in the configuration file.\n");
	return 1;
    }

    /* Cannot add a screen that does not exist */
    if (idx < 0 || idx >= dmxNumScreens) return 1;
    pScreen = screenInfo.screens[idx];
    dmxScreen = &dmxScreens[idx];

    /* Cannot attach to a screen that is already opened */
    if (dmxScreen->beDisplay) {
	dmxLog(dmxWarning,
	       "Attempting to add screen #%d but a screen already exists\n",
	       idx);
	return 1;
    }

    dmxLogOutput(dmxScreen, "Attaching screen #%d\n", idx);

    /* Save old info */
    oldDMXScreen = *dmxScreen;

    /* Copy the name to the new screen */
    dmxScreen->name = strdup(attr->displayName);

    /* Open display and get all of the screen info */
    if (!dmxOpenDisplay(dmxScreen)) {
	dmxLog(dmxWarning,
               "dmxOpenDisplay: Unable to open display %s\n",
               dmxScreen->name);

	/* Restore the old screen */
	*dmxScreen = oldDMXScreen;
	return 1;
    }

    dmxSetErrorHandler(dmxScreen);
    dmxCheckForWM(dmxScreen);
    dmxGetScreenAttribs(dmxScreen);

    if (!dmxGetVisualInfo(dmxScreen)) {
	dmxLog(dmxWarning, "dmxGetVisualInfo: No matching visuals found\n");
	XFree(dmxScreen->beVisuals);
	XCloseDisplay(dmxScreen->beDisplay);

	/* Restore the old screen */
	*dmxScreen = oldDMXScreen;
	return 1;
    }

    dmxGetColormaps(dmxScreen);
    dmxGetPixmapFormats(dmxScreen);

    /* Verify that the screen to be added has the same info as the
     * previously added screen. */
    if (!dmxCompareScreens(dmxScreen, &oldDMXScreen)) {
	dmxLog(dmxWarning,
	       "New screen data (%s) does not match previously\n",
	       dmxScreen->name);
	dmxLog(dmxWarning,
	       "attached screen data (%s)\n",
	       oldDMXScreen.name);
	dmxLog(dmxWarning,
	       "All data must match in order to attach to screen #%d\n",
	       idx);
	XFree(dmxScreen->beVisuals);
	XFree(dmxScreen->beDepths);
	XFree(dmxScreen->bePixmapFormats);
	XCloseDisplay(dmxScreen->beDisplay);

	/* Restore the old screen */
	*dmxScreen = oldDMXScreen;
	return 1;
    }

    /* Initialize the BE screen resources */
    dmxBEScreenInit(idx, screenInfo.screens[idx]);

    /* TODO: Handle GLX visual initialization.  GLXProxy needs to be
     * updated to handle dynamic addition/removal of screens. */

    /* Create default stipple */
    dmxBECreatePixmap(pScreen->PixmapPerDepth[0]);
    dmxBERestorePixmap(pScreen->PixmapPerDepth[0]);

    /* Create the scratch GCs */
    dmxBECreateScratchGCs(idx);

    /* Create the default font */
    (void)dmxBELoadFont(pScreen, defaultFont);

    /* Create all resources that don't depend on windows */
    for (i = currentMaxClients; --i >= 0; )
	if (clients[i])
	    FindAllClientResources(clients[i], dmxBECreateResources,
				   (pointer)idx);

    /* Create window hierarchy (top down) */
    dmxBECreateWindowTree(idx);

#ifdef RENDER
    /* Restore the picture state for RENDER */
    for (i = currentMaxClients; --i >= 0; )
	if (clients[i])
	    FindClientResourcesByType(clients[i],PictureType, 
				      dmxBERestoreRenderPict,(pointer)idx);

    /* Restore the glyph state for RENDER */
    for (i = currentMaxClients; --i >= 0; )
	if (clients[i])
	    FindClientResourcesByType(clients[i],GlyphSetType, 
				      dmxBERestoreRenderGlyph,(pointer)idx);
#endif

    /* Refresh screen by generating exposure events for all windows */
    dmxForceExposures(idx);

    dmxSync(&dmxScreens[idx], TRUE);

    /* We used these to compare the old and new screens.  They are no
     * longer needed since we have a newly attached screen, so we can
     * now free the old screen's resources. */
    XFree(oldDMXScreen.beVisuals);
    XFree(oldDMXScreen.beDepths);
    XFree(oldDMXScreen.bePixmapFormats);
    /* TODO: should oldDMXScreen.name be freed?? */

#ifdef PANORAMIX
    if (!noPanoramiXExtension)
	return dmxConfigureScreenWindows(1, &scrnNum, attr, NULL);
    else
#endif
	return 0; /* Success */
}

/*
 * Resources that may have state on the BE server and need to be freed:
 *
 * RT_NONE
 * RT_WINDOW
 * RT_PIXMAP
 * RT_GC
 * RT_FONT
 * RT_CURSOR
 * RT_COLORMAP
 * RT_CMAPENTRY
 * RT_OTHERCLIENT
 * RT_PASSIVEGRAB
 * XRT_WINDOW
 * XRT_PIXMAP
 * XRT_GC
 * XRT_COLORMAP
 * XRT_PICTURE
 * PictureType
 * PictFormatType
 * GlyphSetType
 * ClientType
 * EventType
 * RT_INPUTCLIENT
 * XETrapType
 * RTCounter
 * RTAwait
 * RTAlarmClient
 * RT_XKBCLIENT
 * RTContext
 * TagResType
 * StalledResType
 * SecurityAuthorizationResType
 * RTEventClient
 * __glXContextRes
 * __glXClientRes
 * __glXPixmapRes
 * __glXWindowRes
 * __glXPbufferRes
 */

#ifdef PANORAMIX
/** Search the Xinerama XRT_PIXMAP resources for the pixmap that needs
 *  to have its image saved. */
static void dmxBEFindPixmapImage(pointer value, XID id, RESTYPE type,
				 pointer p)
{
    if ((type & TypeMask) == (XRT_PIXMAP & TypeMask)) {
	PixmapPtr      pDst     = (PixmapPtr)p;
	int            idx      = pDst->drawable.pScreen->myNum;
	PanoramiXRes  *pXinPix  = (PanoramiXRes *)value;
	PixmapPtr      pPix;
	int            i;

	pPix = (PixmapPtr)LookupIDByType(pXinPix->info[idx].id, RT_PIXMAP);
	if (pPix != pDst) return; /* Not a match.... Next! */

	for (i = 0; i < PanoramiXNumScreens; i++) {
	    PixmapPtr      pSrc;
	    dmxPixPrivPtr  pSrcPriv = NULL;

	    if (i == idx) continue; /* Self replication is bad */

	    pSrc =
		(PixmapPtr)LookupIDByType(pXinPix->info[i].id, RT_PIXMAP);
	    pSrcPriv = DMX_GET_PIXMAP_PRIV(pSrc);
	    if (pSrcPriv->pixmap) {
		FoundPixImage = True;
		return;
	    }
	}
    }
}
#endif

/** Save the pixmap image only when there is not another screen with
 *  that pixmap from which the image can be read when the screen is
 *  reattached.  To do this, we first try to find a pixmap on another
 *  screen corresponding to the one we are trying to save.  If we find
 *  one, then we do not need to save the image data since during
 *  reattachment, the image data can be read from that other pixmap.
 *  However, if we do not find one, then we need to save the image data.
 *  The common case for these are for the default stipple and root
 *  tile. */
static void dmxBESavePixmap(PixmapPtr pPixmap)
{
#ifdef PANORAMIX
    int i;

    /* If Xinerama is not active, there's nothing we can do (see comment
     * in #else below for more info). */
    if (noPanoramiXExtension) return;

    FoundPixImage = False;
    for (i = currentMaxClients; --i >= 0; )
	if (clients[i])
	    FindAllClientResources(clients[i], dmxBEFindPixmapImage,
				   (pointer)pPixmap);

    /* Save the image only if there is no other screens that have a
     * pixmap that corresponds to the one we are trying to save. */
    if (!FoundPixImage) {
	dmxPixPrivPtr  pPixPriv = DMX_GET_PIXMAP_PRIV(pPixmap);

	if (!pPixPriv->detachedImage) {
	    ScreenPtr      pScreen   = pPixmap->drawable.pScreen;
	    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];

	    pPixPriv->detachedImage = XGetImage(dmxScreen->beDisplay,
						pPixPriv->pixmap,
						0, 0,
						pPixmap->drawable.width,
						pPixmap->drawable.height,
						-1,
						ZPixmap);
	    if (!pPixPriv->detachedImage)
		dmxLog(dmxWarning, "Cannot save pixmap image\n");
	}
    }
#else
    /* NOTE: The only time there is a pixmap on another screen that
     * corresponds to the one we are trying to save is when Xinerama is
     * active.  Otherwise, the pixmap image data is only stored on a
     * single screen, which means that once it is detached, that data is
     * lost.  We could save the data here, but then that would require
     * us to implement the ability for Xdmx to keep the pixmap up to
     * date while the screen is detached, which is beyond the scope of
     * the current project. */
    return;
#endif
}

/** Destroy resources on the back-end server.  This function is called
 *  from #dmxDetachScreen() via the dix layer's FindAllResources
 *  function.  It walks all resources, compares them to the screen
 *  number passed in as \a n and calls the appropriate DMX function to
 *  free the associated resource on the back-end server. */
static void dmxBEDestroyResources(pointer value, XID id, RESTYPE type,
				  pointer n)
{
    int        scrnNum = (int)n;
    ScreenPtr  pScreen = screenInfo.screens[scrnNum];

    if ((type & TypeMask) == (RT_WINDOW & TypeMask)) {
	/* Window resources are destroyed below in dmxBEDestroyWindowTree */
    } else if ((type & TypeMask) == (RT_PIXMAP & TypeMask)) {
	PixmapPtr  pPix = value;
	if (pPix->drawable.pScreen->myNum == scrnNum) {
	    dmxBESavePixmap(pPix);
	    dmxBEFreePixmap(pPix);
	}
    } else if ((type & TypeMask) == (RT_GC & TypeMask)) {
	GCPtr  pGC = value;
	if (pGC->pScreen->myNum == scrnNum)
	    dmxBEFreeGC(pGC);
    } else if ((type & TypeMask) == (RT_FONT & TypeMask)) {
	dmxBEFreeFont(pScreen, (FontPtr)value);
    } else if ((type & TypeMask) == (RT_CURSOR & TypeMask)) {
	dmxBEFreeCursor(pScreen, (CursorPtr)value);
    } else if ((type & TypeMask) == (RT_COLORMAP & TypeMask)) {
	ColormapPtr  pCmap = value;
	if (pCmap->pScreen->myNum == scrnNum)
	    dmxBEFreeColormap((ColormapPtr)value);
#ifdef RENDER
    } else if ((type & TypeMask) == (PictureType & TypeMask)) {
	PicturePtr  pPict = value;
	if (pPict->pDrawable->pScreen->myNum == scrnNum) {
	    /* Free the pixmaps on the backend if needed */
	    if (pPict->pDrawable->type == DRAWABLE_PIXMAP) {
		PixmapPtr pPixmap = (PixmapPtr)(pPict->pDrawable);
		dmxBESavePixmap(pPixmap);
		dmxBEFreePixmap(pPixmap);
	    }
	    dmxBEFreePicture((PicturePtr)value);
	}
    } else if ((type & TypeMask) == (GlyphSetType & TypeMask)) {
	dmxBEFreeGlyphSet(pScreen, (GlyphSetPtr)value);
#endif
    } else {
	/* Other resource types??? */
    }
}

/** Destroy the scratch GCs that are created per depth. */
static void dmxBEDestroyScratchGCs(int scrnNum)
{
    ScreenPtr  pScreen = screenInfo.screens[scrnNum];
    GCPtr     *ppGC    = pScreen->GCperDepth;
    int        i;

    for (i = 0; i <= pScreen->numDepths; i++)
	dmxBEFreeGC(ppGC[i]);
}

/** Destroy window hierachy on back-end server.  To ensure that all
 *  XDestroyWindow() calls succeed, they must be performed in a bottom
 *  up order so that windows are not destroyed before their children.
 *  XDestroyWindow(), which is called from #dmxBEDestrowWindow(), will
 *  destroy a window as well as all of it's children. */
static void dmxBEDestroyWindowTree(int idx)
{
    WindowPtr  pWin   = WindowTable[idx];
    WindowPtr  pChild = pWin;

    while (1) {
	if (pChild->firstChild) {
	    pChild = pChild->firstChild;
	    continue;
	}

	/* Destroy the window */
	dmxBEDestroyWindow(pChild);

	/* Make sure we destroy the window's border and background
	 * pixmaps if they exist */
	if (!pChild->borderIsPixel) {
	    dmxBESavePixmap(pChild->border.pixmap);
	    dmxBEFreePixmap(pChild->border.pixmap);
	}
	if (pChild->backgroundState == BackgroundPixmap) {
	    dmxBESavePixmap(pChild->background.pixmap);
	    dmxBEFreePixmap(pChild->background.pixmap);
	}

	while (!pChild->nextSib && (pChild != pWin)) {
	    pChild = pChild->parent;
	    dmxBEDestroyWindow(pChild);
	    if (!pChild->borderIsPixel) {
		dmxBESavePixmap(pChild->border.pixmap);
		dmxBEFreePixmap(pChild->border.pixmap);
	    }
	    if (pChild->backgroundState == BackgroundPixmap) {
		dmxBESavePixmap(pChild->background.pixmap);
		dmxBEFreePixmap(pChild->background.pixmap);
	    }
	}

	if (pChild == pWin)
	    break;

	pChild = pChild->nextSib;
    }
}

/** Detach back-end screen. */
int dmxDetachScreen(int idx)
{
    DMXScreenInfo *dmxScreen = &dmxScreens[idx];
    int            i;

    /* Return failure if dynamic addition/removal of screens is disabled */
    if (!dmxAddRemoveScreens) {
	dmxLog(dmxWarning,
	       "Attempting to remove a screen, but the AddRemoveScreen\n");
	dmxLog(dmxWarning,
	       "extension has not been enabled.  To enable this extension\n");
	dmxLog(dmxWarning,
	       "add the \"-addremovescreens\" option either to the command\n");
	dmxLog(dmxWarning,
	       "line or in the configuration file.\n");
	return 1;
    }

    /* Cannot remove a screen that does not exist */
    if (idx < 0 || idx >= dmxNumScreens) return 1;

    /* Cannot detach from a screen that is not opened */
    if (!dmxScreen->beDisplay) {
	dmxLog(dmxWarning,
	       "Attempting to remove screen #%d but it has not been opened\n",
	       idx);
	return 1;
    }

    dmxLogOutput(dmxScreen, "Detaching screen #%d\n", idx);

    /* Detach input */
    dmxInputDetachAll(dmxScreen);

    /* Save all relevant state (TODO) */

    /* Free all non-window resources related to this screen */
    for (i = currentMaxClients; --i >= 0; )
	if (clients[i])
	    FindAllClientResources(clients[i], dmxBEDestroyResources,
				   (pointer)idx);

    /* Free scratch GCs */
    dmxBEDestroyScratchGCs(idx);

    /* Free window resources related to this screen */
    dmxBEDestroyWindowTree(idx);

    /* Free default stipple */
    dmxBESavePixmap(screenInfo.screens[idx]->PixmapPerDepth[0]);
    dmxBEFreePixmap(screenInfo.screens[idx]->PixmapPerDepth[0]);

    /* Free the remaining screen resources and close the screen */
    dmxBECloseScreen(screenInfo.screens[idx]);

    /* Adjust the cursor boundaries (paints detached console window) */
    dmxAdjustCursorBoundaries();

    return 0; /* Success */
}