summaryrefslogtreecommitdiff
path: root/hw/dmx/dmx.c
blob: 14b7d98584ea25ecd35fae39be7de6dfefd2d055 (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
/* $XFree86$ */
/*
 * Copyright 2002-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:
 *   Rickard E. (Rik) Faith <faith@redhat.com>
 *
 */

/** \file
 * This file implements the server-side part of the DMX protocol. A
 * vector of fucntions is provided at extension initialization time, so
 * most all of the useful functions in this file are declared static and
 * do not appear in the doxygen documentation.
 *
 * Much of the low-level work is done by functions in #dmxextension.c
 *
 * Please see the Client-to-Server DMX Extension to the X Protocol
 * document for details about the protocol.  */

#include "X.h"
#include "Xproto.h"
#include "misc.h"
#include "os.h"
#include "dixstruct.h"
#define EXTENSION_PROC_ARGS void *
#include "extnsionst.h"
#include "opaque.h"

#include "dmxextension.h"
#include "dmxproto.h"

#define _DMX_SERVER_
#include "dmxext.h"

#ifdef PANORAMIX
#include "panoramiX.h"
extern unsigned long XRT_WINDOW;
extern int           PanoramiXNumScreens;
#endif

extern void DMXExtensionInit(void);

static unsigned char DMXCode;

static DISPATCH_PROC(ProcDMXDispatch);
static DISPATCH_PROC(ProcDMXQueryVersion);
static DISPATCH_PROC(ProcDMXSync);
static DISPATCH_PROC(ProcDMXForceWindowCreation);
static DISPATCH_PROC(ProcDMXGetScreenCount);
static DISPATCH_PROC(ProcDMXGetScreenAttributes);
static DISPATCH_PROC(ProcDMXChangeScreensAttributes);
static DISPATCH_PROC(ProcDMXAddScreen);
static DISPATCH_PROC(ProcDMXRemoveScreen);
static DISPATCH_PROC(ProcDMXGetWindowAttributes);
static DISPATCH_PROC(ProcDMXGetDesktopAttributes);
static DISPATCH_PROC(ProcDMXChangeDesktopAttributes);
static DISPATCH_PROC(ProcDMXGetInputCount);
static DISPATCH_PROC(ProcDMXGetInputAttributes);
static DISPATCH_PROC(ProcDMXAddInput);
static DISPATCH_PROC(ProcDMXRemoveInput);

static DISPATCH_PROC(SProcDMXDispatch);
static DISPATCH_PROC(SProcDMXQueryVersion);
static DISPATCH_PROC(SProcDMXSync);
static DISPATCH_PROC(SProcDMXForceWindowCreation);
static DISPATCH_PROC(SProcDMXGetScreenCount);
static DISPATCH_PROC(SProcDMXGetScreenAttributes);
static DISPATCH_PROC(SProcDMXChangeScreensAttributes);
static DISPATCH_PROC(SProcDMXAddScreen);
static DISPATCH_PROC(SProcDMXRemoveScreen);
static DISPATCH_PROC(SProcDMXGetWindowAttributes);
static DISPATCH_PROC(SProcDMXGetDesktopAttributes);
static DISPATCH_PROC(SProcDMXChangeDesktopAttributes);
static DISPATCH_PROC(SProcDMXGetInputCount);
static DISPATCH_PROC(SProcDMXGetInputAttributes);
static DISPATCH_PROC(SProcDMXAddInput);
static DISPATCH_PROC(SProcDMXRemoveInput);

static int _DMXXineramaActive(void)
{
#ifdef PANORAMIX
    return !noPanoramiXExtension;
#endif
    return 0;
}

static void DMXResetProc(ExtensionEntry *extEntry)
{
}

/** Initialize the extension. */
void DMXExtensionInit(void)
{
    ExtensionEntry *extEntry;
    
    if ((extEntry = AddExtension(DMX_EXTENSION_NAME, 0, 0,
                                 ProcDMXDispatch, SProcDMXDispatch,
                                 DMXResetProc, StandardMinorOpcode)))
	DMXCode = extEntry->base;
}

static void dmxSetScreenAttribute(int bit, DMXScreenAttributesPtr attr,
                                  CARD32 value)
{
    switch (1 << bit) {
    case DMXScreenWindowWidth:   attr->screenWindowWidth   = value; break;
    case DMXScreenWindowHeight:  attr->screenWindowHeight  = value; break;
    case DMXScreenWindowXoffset: attr->screenWindowXoffset = value; break;
    case DMXScreenWindowYoffset: attr->screenWindowYoffset = value; break;
    case DMXRootWindowWidth:     attr->rootWindowWidth     = value; break;
    case DMXRootWindowHeight:    attr->rootWindowHeight    = value; break;
    case DMXRootWindowXoffset:   attr->rootWindowXoffset   = value; break;
    case DMXRootWindowYoffset:   attr->rootWindowYoffset   = value; break;
    case DMXRootWindowXorigin:   attr->rootWindowXorigin   = value; break;
    case DMXRootWindowYorigin:   attr->rootWindowYorigin   = value; break;
    }
}

static int dmxFetchScreenAttributes(unsigned int mask,
                                    DMXScreenAttributesPtr attr,
                                    CARD32 *value_list)
{
    int    i;
    CARD32 *value = value_list;
    int    count  = 0;
        
    for (i = 0; i < 32; i++) {
        if (mask & (1 << i)) {
            dmxSetScreenAttribute(i, attr, *value);
            ++value;
            ++count;
        }
    }
    return count;
}

static void dmxSetDesktopAttribute(int bit, DMXDesktopAttributesPtr attr,
                                   CARD32 value)
{
    switch (1 << bit) {
    case DMXDesktopWidth:  attr->width  = value; break;
    case DMXDesktopHeight: attr->height = value; break;
    case DMXDesktopShiftX: attr->shiftX = value; break;
    case DMXDesktopShiftY: attr->shiftY = value; break;
    }
}

static int dmxFetchDesktopAttributes(unsigned int mask,
                                     DMXDesktopAttributesPtr attr,
                                     CARD32 *value_list)
{
    int    i;
    CARD32 *value = value_list;
    int    count  = 0;
        
    for (i = 0; i < 32; i++) {
        if (mask & (1 << i)) {
            dmxSetDesktopAttribute(i, attr, *value);
	    ++value;
            ++count;
        }
    }
    return count;
}

static void dmxSetInputAttribute(int bit, DMXInputAttributesPtr attr,
                                 CARD32 value)
{
    switch (1 << bit) {
    case DMXInputType:           attr->inputType      = value;   break;
    case DMXInputPhysicalScreen: attr->physicalScreen = value;   break;
    case DMXInputSendsCore:      attr->sendsCore      = !!value; break;
    }
}

static int dmxFetchInputAttributes(unsigned int mask,
                                   DMXInputAttributesPtr attr,
                                   CARD32 *value_list)
{
    int    i;
    CARD32 *value = value_list;
    int    count  = 0;

    for (i = 0; i < 32; i++) {
        if (mask & (1 << i)) {
            dmxSetInputAttribute(i, attr, *value);
            ++value;
            ++count;
        }
    }
    return count;
}

static int ProcDMXQueryVersion(ClientPtr client)
{
    xDMXQueryVersionReply rep;
    int                   n;

    REQUEST_SIZE_MATCH(xDMXQueryVersionReq);

    rep.type           = X_Reply;
    rep.sequenceNumber = client->sequence;
    rep.length         = 0;
    rep.majorVersion   = DMX_EXTENSION_MAJOR;
    rep.minorVersion   = DMX_EXTENSION_MINOR;
    rep.patchVersion   = DMX_EXTENSION_PATCH;
    if (client->swapped) {
    	swaps(&rep.sequenceNumber, n);
        swapl(&rep.length, n);
	swapl(&rep.majorVersion, n);
	swapl(&rep.minorVersion, n);
	swapl(&rep.patchVersion, n);
    }
    WriteToClient(client, sizeof(xDMXQueryVersionReply), (char *)&rep);
    return client->noClientException;
}

static int ProcDMXSync(ClientPtr client)
{
    xDMXSyncReply rep;
    int           n;

    REQUEST_SIZE_MATCH(xDMXSyncReq);

    dmxFlushPendingSyncs();

    rep.type           = X_Reply;
    rep.sequenceNumber = client->sequence;
    rep.length         = 0;
    rep.status         = 0;
    if (client->swapped) {
        swaps(&rep.sequenceNumber, n);
        swapl(&rep.length, n);
        swapl(&rep.status, n);
    }
    WriteToClient(client, sizeof(xDMXSyncReply), (char *)&rep);
    return client->noClientException;
}

static int ProcDMXForceWindowCreation(ClientPtr client)
{
    xDMXForceWindowCreationReply rep;
    REQUEST(xDMXForceWindowCreationReq);
    WindowPtr     pWin;
    int           n;

    REQUEST_SIZE_MATCH(xDMXForceWindowCreationReq);

#ifdef PANORAMIX
    if (!noPanoramiXExtension) {
        PanoramiXRes *win;
        int          i;

        if (!(win = SecurityLookupIDByType(client, stuff->window, XRT_WINDOW,
                                           SecurityReadAccess)))
            return -1;           /* BadWindow */

        FOR_NSCREENS(i) {
            if (!(pWin = SecurityLookupWindow(win->info[i].id, client,
                                              SecurityReadAccess)))
                return -1;       /* BadWindow */

            dmxForceWindowCreation(pWin);
        }
        goto doreply;
    }
#endif

    if (!(pWin = SecurityLookupWindow(stuff->window, client,
                                      SecurityReadAccess)))
        return -1;               /* BadWindow */

    dmxForceWindowCreation(pWin);
  doreply:
    dmxFlushPendingSyncs();
    rep.type           = X_Reply;
    rep.sequenceNumber = client->sequence;
    rep.length         = 0;
    rep.status         = 0;
    if (client->swapped) {
        swaps(&rep.sequenceNumber, n);
        swapl(&rep.length, n);
        swapl(&rep.status, n);
    }
    WriteToClient(client, sizeof(xDMXForceWindowCreationReply), (char *)&rep);
    return Success;
}

static int ProcDMXGetScreenCount(ClientPtr client)
{
    xDMXGetScreenCountReply rep;
    int                     n;

    REQUEST_SIZE_MATCH(xDMXGetScreenCountReq);

    rep.type           = X_Reply;
    rep.sequenceNumber = client->sequence;
    rep.length         = 0;
    rep.screenCount    = dmxGetNumScreens();
    if (client->swapped) {
    	swaps(&rep.sequenceNumber, n);
        swapl(&rep.length, n);
        swapl(&rep.screenCount, n);
    }
    WriteToClient(client, sizeof(xDMXGetScreenCountReply), (char *)&rep);
    return client->noClientException;
}

static int ProcDMXGetScreenAttributes(ClientPtr client)
{
    REQUEST(xDMXGetScreenAttributesReq);
    xDMXGetScreenAttributesReply rep;
    int                          n;
    int                          length;
    int                          paddedLength;
    DMXScreenAttributesRec       attr;

    REQUEST_SIZE_MATCH(xDMXGetScreenAttributesReq);

    if (stuff->physicalScreen < 0
        || stuff->physicalScreen >= dmxGetNumScreens()) return BadValue;

    if (!dmxGetScreenAttributes(stuff->physicalScreen, &attr))
        return BadValue;

    rep.logicalScreen       = attr.logicalScreen;
    rep.screenWindowWidth   = attr.screenWindowWidth;
    rep.screenWindowHeight  = attr.screenWindowHeight;
    rep.screenWindowXoffset = attr.screenWindowXoffset;
    rep.screenWindowYoffset = attr.screenWindowYoffset;
    rep.rootWindowWidth     = attr.rootWindowWidth;
    rep.rootWindowHeight    = attr.rootWindowHeight;
    rep.rootWindowXoffset   = attr.rootWindowXoffset;
    rep.rootWindowYoffset   = attr.rootWindowYoffset;
    rep.rootWindowXorigin   = attr.rootWindowXorigin;
    rep.rootWindowYorigin   = attr.rootWindowYorigin;
                                 
    length                  = attr.displayName ? strlen(attr.displayName) : 0;
    paddedLength            = (length + 3) & ~3;
    rep.type                = X_Reply;
    rep.sequenceNumber      = client->sequence;
    rep.length              = paddedLength >> 2;
    rep.displayNameLength   = length;

    if (client->swapped) {
    	swaps(&rep.sequenceNumber, n);
        swapl(&rep.length, n);
        swapl(&rep.displayNameLength, n);
        swapl(&rep.logicalScreen, n);
        swaps(&rep.screenWindowWidth, n);
        swaps(&rep.screenWindowHeight, n);
        swaps(&rep.screenWindowXoffset, n);
        swaps(&rep.screenWindowYoffset, n);
        swaps(&rep.rootWindowWidth, n);
        swaps(&rep.rootWindowHeight, n);
        swaps(&rep.rootWindowXoffset, n);
        swaps(&rep.rootWindowYoffset, n);
        swaps(&rep.rootWindowXorigin, n);
        swaps(&rep.rootWindowYorigin, n);
    }
    WriteToClient(client, sizeof(xDMXGetScreenAttributesReply), (char *)&rep);
    if (length) WriteToClient(client, length, (char *)attr.displayName);
    return client->noClientException;
}

static int ProcDMXChangeScreensAttributes(ClientPtr client)
{
    REQUEST(xDMXChangeScreensAttributesReq);
    xDMXChangeScreensAttributesReply rep;
    int                              n;
    int                              status = DMX_BAD_XINERAMA;
    unsigned int                     mask   = 0;
    unsigned int                     i;
    CARD32                           *screen_list;
    CARD32                           *mask_list;
    CARD32                           *value_list;
    DMXScreenAttributesPtr           attribs;
    int                              errorScreen = 0;
    unsigned int                     len;
    int                              ones = 0;
    

    REQUEST_AT_LEAST_SIZE(xDMXChangeScreensAttributesReq);
    len = client->req_len - (sizeof(xDMXChangeScreensAttributesReq) >> 2);
    if (len < stuff->screenCount + stuff->maskCount)
        return BadLength;

    screen_list = (CARD32 *)(stuff + 1);
    mask_list   = &screen_list[stuff->screenCount];
    value_list  = &mask_list[stuff->maskCount];

    for (i = 0; i < stuff->maskCount; i++) ones += Ones(mask_list[i]);
    if (len != stuff->screenCount + stuff->maskCount + ones)
        return BadLength;
    
    if (!_DMXXineramaActive()) goto noxinerama;

    if (!(attribs = ALLOCATE_LOCAL(stuff->screenCount * sizeof(*attribs))))
        return BadAlloc;

    for (i = 0; i < stuff->screenCount; i++) {
        int count;
        
        if (i < stuff->maskCount) mask = mask_list[i];
        dmxGetScreenAttributes(screen_list[i], &attribs[i]);
        count = dmxFetchScreenAttributes(mask, &attribs[i], value_list);
        value_list += count;
    }

#if PANORAMIX
    status = dmxConfigureScreenWindows(stuff->screenCount,
				       screen_list,
				       attribs,
				       &errorScreen);
#endif

    DEALLOCATE_LOCAL(attribs);

    if (status == BadValue) return status;

  noxinerama:
    rep.type           = X_Reply;
    rep.sequenceNumber = client->sequence;
    rep.length         = 0;
    rep.status         = status;
    rep.errorScreen    = errorScreen;
    if (client->swapped) {
        swaps(&rep.sequenceNumber, n);
        swapl(&rep.length, n);
        swapl(&rep.status, n);
        swapl(&rep.errorScreen, n);
    }
    WriteToClient(client,
                  sizeof(xDMXChangeScreensAttributesReply),
                  (char *)&rep);
    return client->noClientException;
}

static int ProcDMXAddScreen(ClientPtr client)
{
    REQUEST(xDMXAddScreenReq);
    xDMXAddScreenReply     rep;
    int                    n;
    int                    status = 0;
    CARD32                 *value_list;
    DMXScreenAttributesRec attr;
    int                    count;
    char                   *name;
    int                    len;
    int                    paddedLength;

    REQUEST_AT_LEAST_SIZE(xDMXAddScreenReq);
    paddedLength = (stuff->displayNameLength + 3) & ~3;
    len          = client->req_len - (sizeof(xDMXAddScreenReq) >> 2);
    if (len != Ones(stuff->valueMask) + paddedLength/4)
        return BadLength;

    memset(&attr, 0, sizeof(attr));
    dmxGetScreenAttributes(stuff->physicalScreen, &attr);
    value_list = (CARD32 *)(stuff + 1);
    count      = dmxFetchScreenAttributes(stuff->valueMask, &attr, value_list);
    
    if (!(name = ALLOCATE_LOCAL(stuff->displayNameLength + 1 + 4)))
        return BadAlloc;
    memcpy(name, &value_list[count], stuff->displayNameLength);
    name[stuff->displayNameLength] = '\0';
    attr.displayName = name;

    status = dmxAttachScreen(stuff->physicalScreen, &attr);

    DEALLOCATE_LOCAL(name);

    rep.type           = X_Reply;
    rep.sequenceNumber = client->sequence;
    rep.length         = 0;
    rep.status         = status;
    rep.physicalScreen = stuff->physicalScreen;
    if (client->swapped) {
        swaps(&rep.sequenceNumber, n);
        swapl(&rep.length, n);
        swapl(&rep.status, n);
        swapl(&rep.physicalScreen, n);
    }
    WriteToClient(client,
                  sizeof(xDMXAddScreenReply),
                  (char *)&rep);
    return client->noClientException;
}

static int ProcDMXRemoveScreen(ClientPtr client)
{
    REQUEST(xDMXRemoveScreenReq);
    xDMXRemoveScreenReply rep;
    int                   n;
    int                   status = 0;

    REQUEST_SIZE_MATCH(xDMXRemoveScreenReq);

    status = dmxDetachScreen(stuff->physicalScreen);

    rep.type           = X_Reply;
    rep.sequenceNumber = client->sequence;
    rep.length         = 0;
    rep.status         = status;
    if (client->swapped) {
        swaps(&rep.sequenceNumber, n);
        swapl(&rep.length, n);
        swapl(&rep.status, n);
    }
    WriteToClient(client,
                  sizeof(xDMXRemoveScreenReply),
                  (char *)&rep);
    return client->noClientException;
}


#ifdef PANORAMIX
static int dmxPopulatePanoramiX(ClientPtr client, Window window,
                                CARD32 *screens, CARD32 *windows,
                                xRectangle *pos, xRectangle *vis)
{
    WindowPtr              pWin;
    PanoramiXRes           *win;
    int                    i;
    int                    count = 0;
    DMXWindowAttributesRec attr;
    
    if (!(win = SecurityLookupIDByType(client, window, XRT_WINDOW,
                                       SecurityReadAccess)))
        return -1;               /* BadWindow */
    
    FOR_NSCREENS(i) {
        if (!(pWin = SecurityLookupWindow(win->info[i].id, client,
                                          SecurityReadAccess)))
            return -1;          /* BadWindow */
        if (dmxGetWindowAttributes(pWin, &attr)) {
            screens[count] = attr.screen;
            windows[count] = attr.window;
            pos[count]     = attr.pos;
            vis[count]     = attr.vis;
            ++count;            /* Only count existing windows */
        }
    }
    return count;
}
#endif

static int dmxPopulate(ClientPtr client, Window window, CARD32 *screens,
                       CARD32 *windows, xRectangle *pos, xRectangle *vis)
{
    WindowPtr              pWin;
    DMXWindowAttributesRec attr;

#ifdef PANORAMIX
    if (!noPanoramiXExtension)
        return dmxPopulatePanoramiX(client, window, screens, windows,
                                    pos, vis);
#endif
    
    if (!(pWin = SecurityLookupWindow(window, client, SecurityReadAccess)))
        return -1;               /* BadWindow */

    dmxGetWindowAttributes(pWin, &attr);
    *screens = attr.screen;
    *windows = attr.window;
    *pos     = attr.pos;
    *vis     = attr.vis;
    return 1;
}

static int dmxMaxNumScreens(void)
{
#ifdef PANORAMIX
    if (!noPanoramiXExtension) return PanoramiXNumScreens;
#endif
    return 1;
}

static int ProcDMXGetWindowAttributes(ClientPtr client)
{
    REQUEST(xDMXGetWindowAttributesReq);
    xDMXGetWindowAttributesReply rep;
    int                          i, n;
    CARD32                       *screens;
    CARD32                       *windows;
    xRectangle                   *pos, *vis;
    int                          count = dmxMaxNumScreens();

    REQUEST_SIZE_MATCH(xDMXGetWindowAttributesReq);

    if (!(screens = ALLOCATE_LOCAL(count * sizeof(*screens))))
        return BadAlloc;
    if (!(windows = ALLOCATE_LOCAL(count * sizeof(*windows)))) {
        DEALLOCATE_LOCAL(screens);
        return BadAlloc;
    }
    if (!(pos = ALLOCATE_LOCAL(count * sizeof(*pos)))) {
        DEALLOCATE_LOCAL(windows);
        DEALLOCATE_LOCAL(screens);
        return BadAlloc;
    }
    if (!(vis = ALLOCATE_LOCAL(count * sizeof(*vis)))) {
        DEALLOCATE_LOCAL(pos);
        DEALLOCATE_LOCAL(windows);
        DEALLOCATE_LOCAL(screens);
        return BadAlloc;
    }

    if ((count = dmxPopulate(client, stuff->window, screens, windows,
                             pos, vis)) < 0) {
        DEALLOCATE_LOCAL(vis);
        DEALLOCATE_LOCAL(pos);
        DEALLOCATE_LOCAL(windows);
        DEALLOCATE_LOCAL(screens);
        return BadWindow;
    }

    rep.type           = X_Reply;
    rep.sequenceNumber = client->sequence;
    rep.length         = count * 6;
    rep.screenCount    = count;
    if (client->swapped) {
    	swaps(&rep.sequenceNumber, n);
        swapl(&rep.length, n);
        swapl(&rep.screenCount, n);
        for (i = 0; i < count; i++) {
            swapl(&screens[i], n);
            swapl(&windows[i], n);
            
            swaps(&pos[i].x, n);
            swaps(&pos[i].y, n);
            swaps(&pos[i].width, n);
            swaps(&pos[i].height, n);
            
            swaps(&vis[i].x, n);
            swaps(&vis[i].y, n);
            swaps(&vis[i].width, n);
            swaps(&vis[i].height, n);
        }
    }

    dmxFlushPendingSyncs();

    WriteToClient(client, sizeof(xDMXGetWindowAttributesReply), (char *)&rep);
    if (count) {
        WriteToClient(client, count * sizeof(*screens), (char *)screens);
        WriteToClient(client, count * sizeof(*windows), (char *)windows);
        WriteToClient(client, count * sizeof(*pos),     (char *)pos);
        WriteToClient(client, count * sizeof(*vis),     (char *)vis);
    }

    DEALLOCATE_LOCAL(vis);
    DEALLOCATE_LOCAL(pos);
    DEALLOCATE_LOCAL(windows);
    DEALLOCATE_LOCAL(screens);

    return client->noClientException;
}

static int ProcDMXGetDesktopAttributes(ClientPtr client)
{
    xDMXGetDesktopAttributesReply rep;
    int                           n;
    DMXDesktopAttributesRec       attr;

    REQUEST_SIZE_MATCH(xDMXGetDesktopAttributesReq);

    dmxGetDesktopAttributes(&attr);

    rep.width               = attr.width;
    rep.height              = attr.height;
    rep.shiftX              = attr.shiftX;
    rep.shiftY              = attr.shiftY;

    rep.type                = X_Reply;
    rep.sequenceNumber      = client->sequence;
    rep.length              = 0;

    if (client->swapped) {
    	swaps(&rep.sequenceNumber, n);
        swapl(&rep.length, n);
        swapl(&rep.width, n);
        swapl(&rep.height, n);
        swapl(&rep.shiftX, n);
        swapl(&rep.shiftY, n);
    }
    WriteToClient(client, sizeof(xDMXGetDesktopAttributesReply), (char *)&rep);
    return client->noClientException;
}

static int ProcDMXChangeDesktopAttributes(ClientPtr client)
{
    REQUEST(xDMXChangeDesktopAttributesReq);
    xDMXChangeDesktopAttributesReply rep;
    int                              n;
    int                              status = DMX_BAD_XINERAMA;
    CARD32                           *value_list;
    DMXDesktopAttributesRec          attr;
    int                              len;

    REQUEST_AT_LEAST_SIZE(xDMXChangeDesktopAttributesReq);
    len = client->req_len - (sizeof(xDMXChangeDesktopAttributesReq) >> 2);
    if (len != Ones(stuff->valueMask))
        return BadLength;

    if (!_DMXXineramaActive()) goto noxinerama;

    value_list = (CARD32 *)(stuff + 1);
    
    dmxGetDesktopAttributes(&attr);
    dmxFetchDesktopAttributes(stuff->valueMask, &attr, value_list);

#if PANORAMIX
    status = dmxConfigureDesktop(&attr);
#endif
    if (status == BadValue) return status;

  noxinerama:
    rep.type           = X_Reply;
    rep.sequenceNumber = client->sequence;
    rep.length         = 0;
    rep.status         = status;
    if (client->swapped) {
        swaps(&rep.sequenceNumber, n);
        swapl(&rep.length, n);
        swapl(&rep.status, n);
    }
    WriteToClient(client,
                  sizeof(xDMXChangeDesktopAttributesReply),
                  (char *)&rep);
    return client->noClientException;
}

static int ProcDMXGetInputCount(ClientPtr client)
{
    xDMXGetInputCountReply rep;
    int                     n;

    REQUEST_SIZE_MATCH(xDMXGetInputCountReq);

    rep.type           = X_Reply;
    rep.sequenceNumber = client->sequence;
    rep.length         = 0;
    rep.inputCount     = dmxGetInputCount();
    if (client->swapped) {
    	swaps(&rep.sequenceNumber, n);
        swapl(&rep.length, n);
        swapl(&rep.inputCount, n);
    }
    WriteToClient(client, sizeof(xDMXGetInputCountReply), (char *)&rep);
    return client->noClientException;
}

static int ProcDMXGetInputAttributes(ClientPtr client)
{
    REQUEST(xDMXGetInputAttributesReq);
    xDMXGetInputAttributesReply rep;
    int                          n;
    int                          length;
    int                          paddedLength;
    DMXInputAttributesRec        attr;

    REQUEST_SIZE_MATCH(xDMXGetInputAttributesReq);

    if (dmxGetInputAttributes(stuff->deviceId, &attr)) return BadValue;
    rep.inputType      = attr.inputType;
    rep.physicalScreen = attr.physicalScreen;
    rep.physicalId     = attr.physicalId;
    rep.isCore         = attr.isCore;
    rep.sendsCore      = attr.sendsCore;
    rep.detached       = attr.detached;
    
    length             = attr.name ? strlen(attr.name) : 0;
    paddedLength       = (length + 3) & ~3;
    rep.type           = X_Reply;
    rep.sequenceNumber = client->sequence;
    rep.length         = paddedLength >> 2;
    rep.nameLength     = length;
    if (client->swapped) {
    	swaps(&rep.sequenceNumber, n);
        swapl(&rep.length, n);
        swapl(&rep.inputType, n);
        swapl(&rep.physicalScreen, n);
        swapl(&rep.physicalId, n);
        swapl(&rep.nameLength, n);
    }
    WriteToClient(client, sizeof(xDMXGetInputAttributesReply), (char *)&rep);
    if (length) WriteToClient(client, length, (char *)attr.name);
    return client->noClientException;
}

static int ProcDMXAddInput(ClientPtr client)
{
    REQUEST(xDMXAddInputReq);
    xDMXAddInputReply      rep;
    int                    n;
    int                    status = 0;
    CARD32                 *value_list;
    DMXInputAttributesRec  attr;
    int                    count;
    char                   *name;
    int                    len;
    int                    paddedLength;
    int                    id     = -1;

    REQUEST_AT_LEAST_SIZE(xDMXAddInputReq);
    paddedLength = (stuff->displayNameLength + 3) & ~3;
    len          = client->req_len - (sizeof(xDMXAddInputReq) >> 2);
    if (len != Ones(stuff->valueMask) + paddedLength/4)
        return BadLength;

    memset(&attr, 0, sizeof(attr));
    value_list = (CARD32 *)(stuff + 1);
    count      = dmxFetchInputAttributes(stuff->valueMask, &attr, value_list);
    
    if (!(name = ALLOCATE_LOCAL(stuff->displayNameLength + 1 + 4)))
        return BadAlloc;
    memcpy(name, &value_list[count], stuff->displayNameLength);
    name[stuff->displayNameLength] = '\0';
    attr.name = name;

    status = dmxAddInput(&attr, &id);

    DEALLOCATE_LOCAL(name);

    if (status) return status;

    rep.type           = X_Reply;
    rep.sequenceNumber = client->sequence;
    rep.length         = 0;
    rep.status         = status;
    rep.physicalId     = id;
    if (client->swapped) {
        swaps(&rep.sequenceNumber, n);
        swapl(&rep.length, n);
        swapl(&rep.status, n);
        swapl(&rep.physicalId, n);
    }
    WriteToClient(client, sizeof(xDMXAddInputReply), (char *)&rep);
    return client->noClientException;
}

static int ProcDMXRemoveInput(ClientPtr client)
{
    REQUEST(xDMXRemoveInputReq);
    xDMXRemoveInputReply     rep;
    int                      n;
    int                      status = 0;

    REQUEST_SIZE_MATCH(xDMXRemoveInputReq);

    status = dmxRemoveInput(stuff->physicalId);

    if (status) return status;

    rep.type           = X_Reply;
    rep.sequenceNumber = client->sequence;
    rep.length         = 0;
    rep.status         = status;
    if (client->swapped) {
        swaps(&rep.sequenceNumber, n);
        swapl(&rep.length, n);
        swapl(&rep.status, n);
    }
    WriteToClient(client, sizeof(xDMXRemoveInputReply), (char *)&rep);
    return client->noClientException;
}

static int ProcDMXDispatch(ClientPtr client)
{
    REQUEST(xReq);

    switch (stuff->data) {
    case X_DMXQueryVersion:         return ProcDMXQueryVersion(client);
    case X_DMXSync:                 return ProcDMXSync(client);
    case X_DMXForceWindowCreation:  return ProcDMXForceWindowCreation(client);
    case X_DMXGetScreenCount:       return ProcDMXGetScreenCount(client);
    case X_DMXGetScreenAttributes:  return ProcDMXGetScreenAttributes(client);
    case X_DMXChangeScreensAttributes:
        return ProcDMXChangeScreensAttributes(client);
    case X_DMXAddScreen:            return ProcDMXAddScreen(client);
    case X_DMXRemoveScreen:         return ProcDMXRemoveScreen(client);
    case X_DMXGetWindowAttributes:  return ProcDMXGetWindowAttributes(client);
    case X_DMXGetDesktopAttributes: return ProcDMXGetDesktopAttributes(client);
    case X_DMXChangeDesktopAttributes:
        return ProcDMXChangeDesktopAttributes(client);
    case X_DMXGetInputCount:        return ProcDMXGetInputCount(client);
    case X_DMXGetInputAttributes:   return ProcDMXGetInputAttributes(client);
    case X_DMXAddInput:             return ProcDMXAddInput(client);
    case X_DMXRemoveInput:          return ProcDMXRemoveInput(client);
        
    case X_DMXGetScreenInformationDEPRECATED:
    case X_DMXForceWindowCreationDEPRECATED:
    case X_DMXReconfigureScreenDEPRECATED:
        return BadImplementation;

    default:                        return BadRequest;
    }
}

static int SProcDMXQueryVersion(ClientPtr client)
{
    int n;
    REQUEST(xDMXQueryVersionReq);

    swaps(&stuff->length, n);
    REQUEST_SIZE_MATCH(xDMXQueryVersionReq);
    return ProcDMXQueryVersion(client);
}

static int SProcDMXSync(ClientPtr client)
{
    int n;
    REQUEST(xDMXSyncReq);

    swaps(&stuff->length, n);
    REQUEST_SIZE_MATCH(xDMXSyncReq);
    return ProcDMXSync(client);
}

static int SProcDMXForceWindowCreation(ClientPtr client)
{
    int n;
    REQUEST(xDMXForceWindowCreationReq);

    swaps(&stuff->length, n);
    REQUEST_SIZE_MATCH(xDMXForceWindowCreationReq);
    swaps(&stuff->window, n);
    return ProcDMXForceWindowCreation(client);
}

static int SProcDMXGetScreenCount(ClientPtr client)
{
    int n;
    REQUEST(xDMXGetScreenCountReq);

    swaps(&stuff->length, n);
    REQUEST_SIZE_MATCH(xDMXGetScreenCountReq);
    return ProcDMXGetScreenCount(client);
}

static int SProcDMXGetScreenAttributes(ClientPtr client)
{
    int n;
    REQUEST(xDMXGetScreenAttributesReq);

    swaps(&stuff->length, n);
    REQUEST_SIZE_MATCH(xDMXGetScreenAttributesReq);
    swapl(&stuff->physicalScreen, n);
    return ProcDMXGetScreenAttributes(client);
}

static int SProcDMXChangeScreensAttributes(ClientPtr client)
{
    int n;
    REQUEST(xDMXChangeScreensAttributesReq);

    swaps(&stuff->length, n);
    REQUEST_AT_LEAST_SIZE(xDMXGetScreenAttributesReq);
    swapl(&stuff->screenCount, n);
    swapl(&stuff->maskCount, n);
    SwapRestL(stuff);
    return ProcDMXGetScreenAttributes(client);
}

static int SProcDMXAddScreen(ClientPtr client)
{
    int n;
    int paddedLength;
    REQUEST(xDMXAddScreenReq);

    swaps(&stuff->length, n);
    REQUEST_AT_LEAST_SIZE(xDMXAddScreenReq);
    swapl(&stuff->displayNameLength, n);
    swapl(&stuff->valueMask, n);
    paddedLength = (stuff->displayNameLength + 3) & ~3;
    SwapLongs((CARD32 *)(stuff+1), LengthRestL(stuff) - paddedLength/4);
    return ProcDMXAddScreen(client);
}

static int SProcDMXRemoveScreen(ClientPtr client)
{
    int n;
    REQUEST(xDMXRemoveScreenReq);

    swaps(&stuff->length, n);
    REQUEST_SIZE_MATCH(xDMXRemoveScreenReq);
    swapl(&stuff->physicalScreen, n);
    return ProcDMXRemoveScreen(client);
}

static int SProcDMXGetWindowAttributes(ClientPtr client)
{
    int n;
    REQUEST(xDMXGetWindowAttributesReq);

    swaps(&stuff->length, n);
    REQUEST_SIZE_MATCH(xDMXGetWindowAttributesReq);
    swapl(&stuff->window, n);
    return ProcDMXGetWindowAttributes(client);
}

static int SProcDMXGetDesktopAttributes(ClientPtr client)
{
    int n;
    REQUEST(xDMXGetDesktopAttributesReq);

    swaps(&stuff->length, n);
    REQUEST_SIZE_MATCH(xDMXGetDesktopAttributesReq);
    return ProcDMXGetDesktopAttributes(client);
}

static int SProcDMXChangeDesktopAttributes(ClientPtr client)
{
    int n;
    REQUEST(xDMXChangeDesktopAttributesReq);

    swaps(&stuff->length, n);
    REQUEST_AT_LEAST_SIZE(xDMXChangeDesktopAttributesReq);
    swapl(&stuff->valueMask, n);
    SwapRestL(stuff);
    return ProcDMXChangeDesktopAttributes(client);
}

static int SProcDMXGetInputCount(ClientPtr client)
{
    int n;
    REQUEST(xDMXGetInputCountReq);

    swaps(&stuff->length, n);
    REQUEST_SIZE_MATCH(xDMXGetInputCountReq);
    return ProcDMXGetInputCount(client);
}

static int SProcDMXGetInputAttributes(ClientPtr client)
{
    int n;
    REQUEST(xDMXGetInputAttributesReq);

    swaps(&stuff->length, n);
    REQUEST_SIZE_MATCH(xDMXGetInputAttributesReq);
    swapl(&stuff->deviceId, n);
    return ProcDMXGetInputAttributes(client);
}

static int SProcDMXAddInput(ClientPtr client)
{
    int n;
    int paddedLength;
    REQUEST(xDMXAddInputReq);

    swaps(&stuff->length, n);
    REQUEST_AT_LEAST_SIZE(xDMXAddInputReq);
    swapl(&stuff->displayNameLength, n);
    swapl(&stuff->valueMask, n);
    paddedLength = (stuff->displayNameLength + 3) & ~3;
    SwapLongs((CARD32 *)(stuff+1), LengthRestL(stuff) - paddedLength/4);
    return ProcDMXAddInput(client);
}

static int SProcDMXRemoveInput(ClientPtr client)
{
    int n;
    REQUEST(xDMXRemoveInputReq);

    swaps(&stuff->length, n);
    REQUEST_SIZE_MATCH(xDMXRemoveInputReq);
    swapl(&stuff->physicalId, n);
    return ProcDMXRemoveInput(client);
}

static int SProcDMXDispatch (ClientPtr client)
{
    REQUEST(xReq);

    switch (stuff->data) {
    case X_DMXQueryVersion:         return SProcDMXQueryVersion(client);
    case X_DMXSync:                 return SProcDMXSync(client);
    case X_DMXForceWindowCreation:  return SProcDMXForceWindowCreation(client);
    case X_DMXGetScreenCount:       return SProcDMXGetScreenCount(client);
    case X_DMXGetScreenAttributes:  return SProcDMXGetScreenAttributes(client);
    case X_DMXChangeScreensAttributes:
        return SProcDMXChangeScreensAttributes(client);
    case X_DMXAddScreen:            return SProcDMXAddScreen(client);
    case X_DMXRemoveScreen:         return SProcDMXRemoveScreen(client);
    case X_DMXGetWindowAttributes:  return SProcDMXGetWindowAttributes(client);
    case X_DMXGetDesktopAttributes:
        return SProcDMXGetDesktopAttributes(client);
    case X_DMXChangeDesktopAttributes:
        return SProcDMXChangeDesktopAttributes(client);
    case X_DMXGetInputCount:        return SProcDMXGetInputCount(client);
    case X_DMXGetInputAttributes:   return SProcDMXGetInputAttributes(client);
    case X_DMXAddInput:             return SProcDMXAddInput(client);
    case X_DMXRemoveInput:          return SProcDMXRemoveInput(client);
        
    case X_DMXGetScreenInformationDEPRECATED:
    case X_DMXForceWindowCreationDEPRECATED:
    case X_DMXReconfigureScreenDEPRECATED:
        return BadImplementation;

    default:                        return BadRequest;
    }
}