summaryrefslogtreecommitdiff
path: root/hw/kdrive/ephyr/ephyrdriext.c
blob: 144c6e1ac3b0d5f36f477fdc66292a4df8d8f3ce (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
/*
 * Xephyr - A kdrive X server thats runs in a host X window.
 *          Authored by Matthew Allum <mallum@openedhand.com>
 * 
 * Copyright © 2007 OpenedHand Ltd 
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of OpenedHand Ltd not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission. OpenedHand Ltd makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * OpenedHand Ltd DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL OpenedHand Ltd BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 * This file is heavily copied from hw/xfree86/dri/xf86dri.c
 *
 * Authors:
 *    Dodji Seketeli <dodji@openedhand.com>
 */

#ifdef HAVE_CONFIG_H
#include <kdrive-config.h>
#endif

#include <string.h>

#include <X11/X.h>
#include <X11/Xproto.h>
#define _XF86DRI_SERVER_
#include <X11/dri/xf86dri.h>
#include <X11/dri/xf86driproto.h>
#include "misc.h"
#include "privates.h"
#include "dixstruct.h"
#include "extnsionst.h"
#include "colormapst.h"
#include "cursorstr.h"
#include "scrnintstr.h"
#include "windowstr.h"
#include "servermd.h"
#include "swaprep.h"
#include "ephyrdri.h"
#include "ephyrdriext.h"
#include "hostx.h"
#define _HAVE_XALLOC_DECLS
#include "ephyrlog.h"
#include "protocol-versions.h"

typedef struct {
    int foo;
} EphyrDRIWindowPrivRec;
typedef EphyrDRIWindowPrivRec *EphyrDRIWindowPrivPtr;

typedef struct {
    CreateWindowProcPtr CreateWindow;
    DestroyWindowProcPtr DestroyWindow;
    MoveWindowProcPtr MoveWindow;
    PositionWindowProcPtr PositionWindow;
    ClipNotifyProcPtr ClipNotify;
} EphyrDRIScreenPrivRec;
typedef EphyrDRIScreenPrivRec *EphyrDRIScreenPrivPtr;

static int DRIErrorBase;

static Bool ephyrDRIScreenInit(ScreenPtr a_screen);
static Bool ephyrDRICreateWindow(WindowPtr a_win);
static Bool ephyrDRIDestroyWindow(WindowPtr a_win);
static void ephyrDRIMoveWindow(WindowPtr a_win,
                               int a_x, int a_y,
                               WindowPtr a_siblings, VTKind a_kind);
static Bool ephyrDRIPositionWindow(WindowPtr a_win, int x, int y);
static void ephyrDRIClipNotify(WindowPtr a_win, int a_x, int a_y);

static Bool EphyrMirrorHostVisuals(ScreenPtr a_screen);
static Bool destroyHostPeerWindow(const WindowPtr a_win);
static Bool findWindowPairFromLocal(WindowPtr a_local,
                                    EphyrWindowPair ** a_pair);

static unsigned char DRIReqCode = 0;

static DevPrivateKeyRec ephyrDRIWindowKeyRec;

#define ephyrDRIWindowKey (&ephyrDRIWindowKeyRec)
static DevPrivateKeyRec ephyrDRIScreenKeyRec;

#define ephyrDRIScreenKey (&ephyrDRIScreenKeyRec)

#define GET_EPHYR_DRI_WINDOW_PRIV(win) ((EphyrDRIWindowPrivPtr) \
    dixLookupPrivate(&(win)->devPrivates, ephyrDRIWindowKey))
#define GET_EPHYR_DRI_SCREEN_PRIV(screen) ((EphyrDRIScreenPrivPtr) \
    dixLookupPrivate(&(screen)->devPrivates, ephyrDRIScreenKey))

static Bool
ephyrDRIScreenInit(ScreenPtr a_screen)
{
    Bool is_ok = FALSE;
    EphyrDRIScreenPrivPtr screen_priv = NULL;

    EPHYR_RETURN_VAL_IF_FAIL(a_screen, FALSE);

    screen_priv = GET_EPHYR_DRI_SCREEN_PRIV(a_screen);
    EPHYR_RETURN_VAL_IF_FAIL(screen_priv, FALSE);

    screen_priv->CreateWindow = a_screen->CreateWindow;
    screen_priv->DestroyWindow = a_screen->DestroyWindow;
    screen_priv->MoveWindow = a_screen->MoveWindow;
    screen_priv->PositionWindow = a_screen->PositionWindow;
    screen_priv->ClipNotify = a_screen->ClipNotify;

    a_screen->CreateWindow = ephyrDRICreateWindow;
    a_screen->DestroyWindow = ephyrDRIDestroyWindow;
    a_screen->MoveWindow = ephyrDRIMoveWindow;
    a_screen->PositionWindow = ephyrDRIPositionWindow;
    a_screen->ClipNotify = ephyrDRIClipNotify;

    is_ok = TRUE;

    return is_ok;
}

static Bool
ephyrDRICreateWindow(WindowPtr a_win)
{
    Bool is_ok = FALSE;
    ScreenPtr screen = NULL;
    EphyrDRIScreenPrivPtr screen_priv = NULL;

    EPHYR_RETURN_VAL_IF_FAIL(a_win, FALSE);
    screen = a_win->drawable.pScreen;
    EPHYR_RETURN_VAL_IF_FAIL(screen, FALSE);
    screen_priv = GET_EPHYR_DRI_SCREEN_PRIV(screen);
    EPHYR_RETURN_VAL_IF_FAIL(screen_priv && screen_priv->CreateWindow, FALSE);

    EPHYR_LOG("enter. win:%p\n", a_win);

    screen->CreateWindow = screen_priv->CreateWindow;
    is_ok = (*screen->CreateWindow) (a_win);
    screen->CreateWindow = ephyrDRICreateWindow;

    if (is_ok) {
        dixSetPrivate(&a_win->devPrivates, ephyrDRIWindowKey, NULL);
    }
    return is_ok;
}

static Bool
ephyrDRIDestroyWindow(WindowPtr a_win)
{
    Bool is_ok = FALSE;
    ScreenPtr screen = NULL;
    EphyrDRIScreenPrivPtr screen_priv = NULL;

    EPHYR_RETURN_VAL_IF_FAIL(a_win, FALSE);
    screen = a_win->drawable.pScreen;
    EPHYR_RETURN_VAL_IF_FAIL(screen, FALSE);
    screen_priv = GET_EPHYR_DRI_SCREEN_PRIV(screen);
    EPHYR_RETURN_VAL_IF_FAIL(screen_priv && screen_priv->DestroyWindow, FALSE);

    screen->DestroyWindow = screen_priv->DestroyWindow;
    if (screen->DestroyWindow) {
        is_ok = (*screen->DestroyWindow) (a_win);
    }
    screen->DestroyWindow = ephyrDRIDestroyWindow;

    if (is_ok) {
        EphyrDRIWindowPrivPtr win_priv = GET_EPHYR_DRI_WINDOW_PRIV(a_win);

        if (win_priv) {
            destroyHostPeerWindow(a_win);
            free(win_priv);
            dixSetPrivate(&a_win->devPrivates, ephyrDRIWindowKey, NULL);
            EPHYR_LOG("destroyed the remote peer window\n");
        }
    }
    return is_ok;
}

static void
ephyrDRIMoveWindow(WindowPtr a_win,
                   int a_x, int a_y, WindowPtr a_siblings, VTKind a_kind)
{
    Bool is_ok = FALSE;
    ScreenPtr screen = NULL;
    EphyrDRIScreenPrivPtr screen_priv = NULL;
    EphyrDRIWindowPrivPtr win_priv = NULL;
    EphyrWindowPair *pair = NULL;
    EphyrBox geo;
    int x = 0, y = 0;           /*coords relative to parent window */

    EPHYR_RETURN_IF_FAIL(a_win);

    EPHYR_LOG("enter\n");
    screen = a_win->drawable.pScreen;
    EPHYR_RETURN_IF_FAIL(screen);
    screen_priv = GET_EPHYR_DRI_SCREEN_PRIV(screen);
    EPHYR_RETURN_IF_FAIL(screen_priv && screen_priv->MoveWindow);

    screen->MoveWindow = screen_priv->MoveWindow;
    if (screen->MoveWindow) {
        (*screen->MoveWindow) (a_win, a_x, a_y, a_siblings, a_kind);
    }
    screen->MoveWindow = ephyrDRIMoveWindow;

    EPHYR_LOG("window: %p\n", a_win);
    if (!a_win->parent) {
        EPHYR_LOG("cannot move root window\n");
        is_ok = TRUE;
        goto out;
    }
    win_priv = GET_EPHYR_DRI_WINDOW_PRIV(a_win);
    if (!win_priv) {
        EPHYR_LOG("not a DRI peered window\n");
        is_ok = TRUE;
        goto out;
    }
    if (!findWindowPairFromLocal(a_win, &pair) || !pair) {
        EPHYR_LOG_ERROR("failed to get window pair\n");
        goto out;
    }
    /*compute position relative to parent window */
    x = a_win->drawable.x - a_win->parent->drawable.x;
    y = a_win->drawable.y - a_win->parent->drawable.y;
    /*set the geometry to pass to hostx_set_window_geometry */
    memset(&geo, 0, sizeof(geo));
    geo.x = x;
    geo.y = y;
    geo.width = a_win->drawable.width;
    geo.height = a_win->drawable.height;
    hostx_set_window_geometry(pair->remote, &geo);
    is_ok = TRUE;

 out:
    EPHYR_LOG("leave. is_ok:%d\n", is_ok);
    /*do cleanup here */
}

static Bool
ephyrDRIPositionWindow(WindowPtr a_win, int a_x, int a_y)
{
    Bool is_ok = FALSE;
    ScreenPtr screen = NULL;
    EphyrDRIScreenPrivPtr screen_priv = NULL;
    EphyrDRIWindowPrivPtr win_priv = NULL;
    EphyrWindowPair *pair = NULL;
    EphyrBox geo;

    EPHYR_RETURN_VAL_IF_FAIL(a_win, FALSE);

    EPHYR_LOG("enter\n");
    screen = a_win->drawable.pScreen;
    EPHYR_RETURN_VAL_IF_FAIL(screen, FALSE);
    screen_priv = GET_EPHYR_DRI_SCREEN_PRIV(screen);
    EPHYR_RETURN_VAL_IF_FAIL(screen_priv && screen_priv->PositionWindow, FALSE);

    screen->PositionWindow = screen_priv->PositionWindow;
    if (screen->PositionWindow) {
        (*screen->PositionWindow) (a_win, a_x, a_y);
    }
    screen->PositionWindow = ephyrDRIPositionWindow;

    EPHYR_LOG("window: %p\n", a_win);
    win_priv = GET_EPHYR_DRI_WINDOW_PRIV(a_win);
    if (!win_priv) {
        EPHYR_LOG("not a DRI peered window\n");
        is_ok = TRUE;
        goto out;
    }
    if (!findWindowPairFromLocal(a_win, &pair) || !pair) {
        EPHYR_LOG_ERROR("failed to get window pair\n");
        goto out;
    }
    /*set the geometry to pass to hostx_set_window_geometry */
    memset(&geo, 0, sizeof(geo));
    geo.x = a_x;
    geo.y = a_y;
    geo.width = a_win->drawable.width;
    geo.height = a_win->drawable.height;
    hostx_set_window_geometry(pair->remote, &geo);
    is_ok = TRUE;

 out:
    EPHYR_LOG("leave. is_ok:%d\n", is_ok);
    /*do cleanup here */
    return is_ok;
}

static void
ephyrDRIClipNotify(WindowPtr a_win, int a_x, int a_y)
{
    Bool is_ok = FALSE;
    ScreenPtr screen = NULL;
    EphyrDRIScreenPrivPtr screen_priv = NULL;
    EphyrDRIWindowPrivPtr win_priv = NULL;
    EphyrWindowPair *pair = NULL;
    EphyrRect *rects = NULL;
    int i = 0;

    EPHYR_RETURN_IF_FAIL(a_win);

    EPHYR_LOG("enter\n");
    screen = a_win->drawable.pScreen;
    EPHYR_RETURN_IF_FAIL(screen);
    screen_priv = GET_EPHYR_DRI_SCREEN_PRIV(screen);
    EPHYR_RETURN_IF_FAIL(screen_priv && screen_priv->ClipNotify);

    screen->ClipNotify = screen_priv->ClipNotify;
    if (screen->ClipNotify) {
        (*screen->ClipNotify) (a_win, a_x, a_y);
    }
    screen->ClipNotify = ephyrDRIClipNotify;

    EPHYR_LOG("window: %p\n", a_win);
    win_priv = GET_EPHYR_DRI_WINDOW_PRIV(a_win);
    if (!win_priv) {
        EPHYR_LOG("not a DRI peered window\n");
        is_ok = TRUE;
        goto out;
    }
    if (!findWindowPairFromLocal(a_win, &pair) || !pair) {
        EPHYR_LOG_ERROR("failed to get window pair\n");
        goto out;
    }
    rects = calloc(RegionNumRects(&a_win->clipList), sizeof(EphyrRect));
    for (i = 0; i < RegionNumRects(&a_win->clipList); i++) {
        memmove(&rects[i],
                &RegionRects(&a_win->clipList)[i], sizeof(EphyrRect));
        rects[i].x1 -= a_win->drawable.x;
        rects[i].x2 -= a_win->drawable.x;
        rects[i].y1 -= a_win->drawable.y;
        rects[i].y2 -= a_win->drawable.y;
    }
    /*
     * push the clipping region of this window
     * to the peer window in the host
     */
    is_ok = hostx_set_window_bounding_rectangles
        (pair->remote, rects, RegionNumRects(&a_win->clipList));
    is_ok = TRUE;

 out:
    free(rects);
    rects = NULL;

    EPHYR_LOG("leave. is_ok:%d\n", is_ok);
    /*do cleanup here */
}

/**
 * Duplicates a visual of a_screen
 * In screen a_screen, for depth a_depth, find a visual which
 * bitsPerRGBValue and colormap size equal
 * a_bits_per_rgb_values and a_colormap_entries.
 * The ID of that duplicated visual is set to a_new_id.
 * That duplicated visual is then added to the list of visuals
 * of the screen.
 */
static Bool
EphyrDuplicateVisual(unsigned int a_screen,
                     short a_depth,
                     short a_class,
                     short a_bits_per_rgb_values,
                     short a_colormap_entries,
                     unsigned int a_red_mask,
                     unsigned int a_green_mask,
                     unsigned int a_blue_mask, unsigned int a_new_id)
{
    Bool is_ok = FALSE, found_visual = FALSE, found_depth = FALSE;
    ScreenPtr screen = NULL;
    VisualRec new_visual, *new_visuals = NULL;
    int i = 0;

    EPHYR_LOG("enter\n");
    if (a_screen >= screenInfo.numScreens) {
        EPHYR_LOG_ERROR("bad screen number\n");
        goto out;
    }
    memset(&new_visual, 0, sizeof(VisualRec));

    /*get the screen pointed to by a_screen */
    screen = screenInfo.screens[a_screen];
    EPHYR_RETURN_VAL_IF_FAIL(screen, FALSE);

    /*
     * In that screen, first look for an existing visual that has the
     * same characteristics as those passed in parameter
     * to this function and copy it.
     */
    for (i = 0; i < screen->numVisuals; i++) {
        if (screen->visuals[i].bitsPerRGBValue == a_bits_per_rgb_values &&
            screen->visuals[i].ColormapEntries == a_colormap_entries) {
            /*copy the visual found */
            memcpy(&new_visual, &screen->visuals[i], sizeof(new_visual));
            new_visual.vid = a_new_id;
            new_visual.class = a_class;
            new_visual.redMask = a_red_mask;
            new_visual.greenMask = a_green_mask;
            new_visual.blueMask = a_blue_mask;
            found_visual = TRUE;
            EPHYR_LOG("found a visual that matches visual id: %d\n", a_new_id);
            break;
        }
    }
    if (!found_visual) {
        EPHYR_LOG("did not find any visual matching %d\n", a_new_id);
        goto out;
    }
    /*
     * be prepare to extend screen->visuals to add new_visual to it
     */
    new_visuals = calloc(screen->numVisuals + 1, sizeof(VisualRec));
    memmove(new_visuals,
            screen->visuals, screen->numVisuals * sizeof(VisualRec));
    memmove(&new_visuals[screen->numVisuals], &new_visual, sizeof(VisualRec));
    /*
     * Now, in that same screen, update the screen->allowedDepths member.
     * In that array, each element represents the visuals applicable to
     * a given depth. So we need to add an entry matching the new visual
     * that we are going to add to screen->visuals
     */
    for (i = 0; i < screen->numDepths; i++) {
        VisualID *vids = NULL;
        DepthPtr cur_depth = NULL;

        /*find the entry matching a_depth */
        if (screen->allowedDepths[i].depth != a_depth)
            continue;
        cur_depth = &screen->allowedDepths[i];
        /*
         * extend the list of visual IDs in that entry,
         * so to add a_new_id in there.
         */
        vids = realloc(cur_depth->vids,
                       (cur_depth->numVids + 1) * sizeof(VisualID));
        if (!vids) {
            EPHYR_LOG_ERROR("failed to realloc numids\n");
            goto out;
        }
        vids[cur_depth->numVids] = a_new_id;
        /*
         * Okay now commit our change.
         * Do really update screen->allowedDepths[i]
         */
        cur_depth->numVids++;
        cur_depth->vids = vids;
        found_depth = TRUE;
    }
    if (!found_depth) {
        EPHYR_LOG_ERROR("failed to update screen[%d]->allowedDepth\n",
                        a_screen);
        goto out;
    }
    /*
     * Commit our change to screen->visuals
     */
    free(screen->visuals);
    screen->visuals = new_visuals;
    screen->numVisuals++;
    new_visuals = NULL;

    is_ok = TRUE;
 out:
    free(new_visuals);
    new_visuals = NULL;

    EPHYR_LOG("leave\n");
    return is_ok;
}

/**
 * Duplicates the visuals of the host X server.
 * This is necessary to have visuals that have the same
 * ID as those of the host X. It is important to have that for
 * GLX.
 */
static Bool
EphyrMirrorHostVisuals(ScreenPtr a_screen)
{
    Bool is_ok = FALSE;
    EphyrHostVisualInfo *visuals = NULL;
    int nb_visuals = 0, i = 0;

    EPHYR_LOG("enter\n");
    if (!hostx_get_visuals_info(&visuals, &nb_visuals)) {
        EPHYR_LOG_ERROR("failed to get host visuals\n");
        goto out;
    }
    for (i = 0; i < nb_visuals; i++) {
        if (!EphyrDuplicateVisual(a_screen->myNum,
                                  visuals[i].depth,
                                  visuals[i].class,
                                  visuals[i].bits_per_rgb,
                                  visuals[i].colormap_size,
                                  visuals[i].red_mask,
                                  visuals[i].green_mask,
                                  visuals[i].blue_mask, visuals[i].visualid)) {
            EPHYR_LOG_ERROR("failed to duplicate host visual %d\n",
                            (int) visuals[i].visualid);
        }
    }

    is_ok = TRUE;
 out:
    EPHYR_LOG("leave\n");
    return is_ok;
}

static int
ProcXF86DRIQueryVersion(register ClientPtr client)
{
    xXF86DRIQueryVersionReply rep = {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
        .majorVersion = SERVER_XF86DRI_MAJOR_VERSION,
        .minorVersion = SERVER_XF86DRI_MINOR_VERSION,
        .patchVersion = SERVER_XF86DRI_PATCH_VERSION
    };

    REQUEST_SIZE_MATCH(xXF86DRIQueryVersionReq);

    EPHYR_LOG("enter\n");

    if (client->swapped) {
        swaps(&rep.sequenceNumber);
        swapl(&rep.length);
        swaps(&rep.majorVersion);
        swaps(&rep.minorVersion);
        swapl(&rep.patchVersion);
    }
    WriteToClient(client, sizeof(xXF86DRIQueryVersionReply), &rep);
    EPHYR_LOG("leave\n");
    return Success;
}

static int
ProcXF86DRIQueryDirectRenderingCapable(register ClientPtr client)
{
    xXF86DRIQueryDirectRenderingCapableReply rep;
    Bool isCapable;

    REQUEST(xXF86DRIQueryDirectRenderingCapableReq);
    REQUEST_SIZE_MATCH(xXF86DRIQueryDirectRenderingCapableReq);

    EPHYR_LOG("enter\n");
    if (stuff->screen >= screenInfo.numScreens) {
        client->errorValue = stuff->screen;
        return BadValue;
    }

    if (!ephyrDRIQueryDirectRenderingCapable(stuff->screen, &isCapable)) {
        return BadValue;
    }

    if (!client->local || client->swapped)
        isCapable = 0;

    rep = (xXF86DRIQueryDirectRenderingCapableReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
        .isCapable = isCapable
    };

    if (client->swapped) {
        swaps(&rep.sequenceNumber);
        swapl(&rep.length);
    }

    WriteToClient(client, sizeof(xXF86DRIQueryDirectRenderingCapableReply),
                  &rep);
    EPHYR_LOG("leave\n");

    return Success;
}

static int
ProcXF86DRIOpenConnection(register ClientPtr client)
{
    xXF86DRIOpenConnectionReply rep;
    drm_handle_t hSAREA;
    char *busIdString = NULL;
    CARD32 busIdStringLength = 0;

    REQUEST(xXF86DRIOpenConnectionReq);
    REQUEST_SIZE_MATCH(xXF86DRIOpenConnectionReq);

    EPHYR_LOG("enter\n");
    if (stuff->screen >= screenInfo.numScreens) {
        client->errorValue = stuff->screen;
        return BadValue;
    }

    if (!ephyrDRIOpenConnection(stuff->screen, &hSAREA, &busIdString)) {
        return BadValue;
    }

    if (busIdString)
        busIdStringLength = strlen(busIdString);

    rep = (xXF86DRIOpenConnectionReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = bytes_to_int32(SIZEOF(xXF86DRIOpenConnectionReply) -
                                 SIZEOF(xGenericReply) +
                                 pad_to_int32(busIdStringLength)),
        .hSAREALow = (CARD32) (hSAREA & 0xffffffff),
#if defined(LONG64) && !defined(__linux__)
        .hSAREAHigh = (CARD32) (hSAREA >> 32),
#else
        .hSAREAHigh = 0,
#endif
        .busIdStringLength = busIdStringLength
    };

    WriteToClient(client, sizeof(xXF86DRIOpenConnectionReply), &rep);
    if (busIdStringLength)
        WriteToClient(client, busIdStringLength, busIdString);
    free(busIdString);
    EPHYR_LOG("leave\n");
    return Success;
}

static int
ProcXF86DRIAuthConnection(register ClientPtr client)
{
    xXF86DRIAuthConnectionReply rep;

    REQUEST(xXF86DRIAuthConnectionReq);
    REQUEST_SIZE_MATCH(xXF86DRIAuthConnectionReq);

    EPHYR_LOG("enter\n");
    if (stuff->screen >= screenInfo.numScreens) {
        client->errorValue = stuff->screen;
        return BadValue;
    }

    rep = (xXF86DRIAuthConnectionReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
        .authenticated = 1
    };

    if (!ephyrDRIAuthConnection(stuff->screen, stuff->magic)) {
        ErrorF("Failed to authenticate %lu\n", (unsigned long) stuff->magic);
        rep.authenticated = 0;
    }
    WriteToClient(client, sizeof(xXF86DRIAuthConnectionReply), &rep);
    EPHYR_LOG("leave\n");
    return Success;
}

static int
ProcXF86DRICloseConnection(register ClientPtr client)
{
    REQUEST(xXF86DRICloseConnectionReq);
    REQUEST_SIZE_MATCH(xXF86DRICloseConnectionReq);
    EPHYR_LOG("enter\n");
    if (stuff->screen >= screenInfo.numScreens) {
        client->errorValue = stuff->screen;
        return BadValue;
    }

    /*
       DRICloseConnection( screenInfo.screens[stuff->screen]);
     */

    EPHYR_LOG("leave\n");
    return Success;
}

static int
ProcXF86DRIGetClientDriverName(register ClientPtr client)
{
    xXF86DRIGetClientDriverNameReply rep =  {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .clientDriverNameLength = 0
    };
    char *clientDriverName;

    REQUEST(xXF86DRIGetClientDriverNameReq);
    REQUEST_SIZE_MATCH(xXF86DRIGetClientDriverNameReq);

    EPHYR_LOG("enter\n");
    if (stuff->screen >= screenInfo.numScreens) {
        client->errorValue = stuff->screen;
        return BadValue;
    }

    ephyrDRIGetClientDriverName(stuff->screen,
                                (int *) &rep.ddxDriverMajorVersion,
                                (int *) &rep.ddxDriverMinorVersion,
                                (int *) &rep.ddxDriverPatchVersion,
                                &clientDriverName);
    if (clientDriverName)
        rep.clientDriverNameLength = strlen(clientDriverName);
    rep.length = bytes_to_int32(SIZEOF(xXF86DRIGetClientDriverNameReply) -
                                SIZEOF(xGenericReply) +
                                pad_to_int32(rep.clientDriverNameLength));

    WriteToClient(client, sizeof(xXF86DRIGetClientDriverNameReply), &rep);
    if (rep.clientDriverNameLength)
        WriteToClient(client, rep.clientDriverNameLength, clientDriverName);
    EPHYR_LOG("leave\n");
    return Success;
}

static int
ProcXF86DRICreateContext(register ClientPtr client)
{
    xXF86DRICreateContextReply rep = {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0
    };
    ScreenPtr pScreen;
    VisualPtr visual;
    int i = 0;
    unsigned long context_id = 0;

    REQUEST(xXF86DRICreateContextReq);
    REQUEST_SIZE_MATCH(xXF86DRICreateContextReq);

    EPHYR_LOG("enter\n");
    if (stuff->screen >= screenInfo.numScreens) {
        client->errorValue = stuff->screen;
        return BadValue;
    }

    pScreen = screenInfo.screens[stuff->screen];
    visual = pScreen->visuals;

    /* Find the requested X visual */
    for (i = 0; i < pScreen->numVisuals; i++, visual++)
        if (visual->vid == stuff->visual)
            break;
    if (i == pScreen->numVisuals) {
        /* No visual found */
        return BadValue;
    }

    context_id = stuff->context;
    if (!ephyrDRICreateContext(stuff->screen,
                               stuff->visual,
                               &context_id,
                               (drm_context_t *) &rep.hHWContext)) {
        return BadValue;
    }

    WriteToClient(client, sizeof(xXF86DRICreateContextReply), &rep);
    EPHYR_LOG("leave\n");
    return Success;
}

static int
ProcXF86DRIDestroyContext(register ClientPtr client)
{
    REQUEST(xXF86DRIDestroyContextReq);
    REQUEST_SIZE_MATCH(xXF86DRIDestroyContextReq);
    EPHYR_LOG("enter\n");

    if (stuff->screen >= screenInfo.numScreens) {
        client->errorValue = stuff->screen;
        return BadValue;
    }

    if (!ephyrDRIDestroyContext(stuff->screen, stuff->context)) {
        return BadValue;
    }

    EPHYR_LOG("leave\n");
    return Success;
}

static Bool
getWindowVisual(const WindowPtr a_win, VisualPtr * a_visual)
{
    int i = 0, visual_id = 0;

    EPHYR_RETURN_VAL_IF_FAIL(a_win
                             && a_win->drawable.pScreen
                             && a_win->drawable.pScreen->visuals, FALSE);

    visual_id = wVisual(a_win);
    for (i = 0; i < a_win->drawable.pScreen->numVisuals; i++) {
        if (a_win->drawable.pScreen->visuals[i].vid == visual_id) {
            *a_visual = &a_win->drawable.pScreen->visuals[i];
            return TRUE;
        }
    }
    return FALSE;
}

#define NUM_WINDOW_PAIRS 256
static EphyrWindowPair window_pairs[NUM_WINDOW_PAIRS];

static Bool
appendWindowPairToList(WindowPtr a_local, int a_remote)
{
    int i = 0;

    EPHYR_RETURN_VAL_IF_FAIL(a_local, FALSE);

    EPHYR_LOG("(local,remote):(%p, %d)\n", a_local, a_remote);

    for (i = 0; i < NUM_WINDOW_PAIRS; i++) {
        if (window_pairs[i].local == NULL) {
            window_pairs[i].local = a_local;
            window_pairs[i].remote = a_remote;
            return TRUE;
        }
    }
    return FALSE;
}

static Bool
findWindowPairFromLocal(WindowPtr a_local, EphyrWindowPair ** a_pair)
{
    int i = 0;

    EPHYR_RETURN_VAL_IF_FAIL(a_pair && a_local, FALSE);

    for (i = 0; i < NUM_WINDOW_PAIRS; i++) {
        if (window_pairs[i].local == a_local) {
            *a_pair = &window_pairs[i];
            EPHYR_LOG("found (%p, %d)\n", (*a_pair)->local, (*a_pair)->remote);
            return TRUE;
        }
    }
    return FALSE;
}

Bool
findWindowPairFromRemote(int a_remote, EphyrWindowPair ** a_pair)
{
    int i = 0;

    EPHYR_RETURN_VAL_IF_FAIL(a_pair, FALSE);

    for (i = 0; i < NUM_WINDOW_PAIRS; i++) {
        if (window_pairs[i].remote == a_remote) {
            *a_pair = &window_pairs[i];
            EPHYR_LOG("found (%p, %d)\n", (*a_pair)->local, (*a_pair)->remote);
            return TRUE;
        }
    }
    return FALSE;
}

static Bool
createHostPeerWindow(const WindowPtr a_win, int *a_peer_win)
{
    Bool is_ok = FALSE;
    VisualPtr visual = NULL;
    EphyrBox geo;

    EPHYR_RETURN_VAL_IF_FAIL(a_win && a_peer_win, FALSE);
    EPHYR_RETURN_VAL_IF_FAIL(a_win->drawable.pScreen, FALSE);

    EPHYR_LOG("enter. a_win '%p'\n", a_win);
    if (!getWindowVisual(a_win, &visual)) {
        EPHYR_LOG_ERROR("failed to get window visual\n");
        goto out;
    }
    if (!visual) {
        EPHYR_LOG_ERROR("failed to create visual\n");
        goto out;
    }
    memset(&geo, 0, sizeof(geo));
    geo.x = a_win->drawable.x;
    geo.y = a_win->drawable.y;
    geo.width = a_win->drawable.width;
    geo.height = a_win->drawable.height;
    if (!hostx_create_window(a_win->drawable.pScreen->myNum,
                             &geo, visual->vid, a_peer_win)) {
        EPHYR_LOG_ERROR("failed to create host peer window\n");
        goto out;
    }
    if (!appendWindowPairToList(a_win, *a_peer_win)) {
        EPHYR_LOG_ERROR("failed to append window to pair list\n");
        goto out;
    }
    is_ok = TRUE;
 out:
    EPHYR_LOG("leave:remote win%d\n", *a_peer_win);
    return is_ok;
}

static Bool
destroyHostPeerWindow(const WindowPtr a_win)
{
    Bool is_ok = FALSE;
    EphyrWindowPair *pair = NULL;

    EPHYR_RETURN_VAL_IF_FAIL(a_win, FALSE);

    EPHYR_LOG("enter\n");

    if (!findWindowPairFromLocal(a_win, &pair) || !pair) {
        EPHYR_LOG_ERROR("failed to find peer to local window\n");
        goto out;
    }
    hostx_destroy_window(pair->remote);
    is_ok = TRUE;

 out:
    EPHYR_LOG("leave\n");
    return is_ok;
}

static int
ProcXF86DRICreateDrawable(ClientPtr client)
{
    xXF86DRICreateDrawableReply rep = {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0
    };
    DrawablePtr drawable = NULL;
    WindowPtr window = NULL;
    EphyrWindowPair *pair = NULL;
    EphyrDRIWindowPrivPtr win_priv = NULL;
    int rc = 0, remote_win = 0;

    REQUEST(xXF86DRICreateDrawableReq);
    REQUEST_SIZE_MATCH(xXF86DRICreateDrawableReq);

    EPHYR_LOG("enter\n");
    if (stuff->screen >= screenInfo.numScreens) {
        client->errorValue = stuff->screen;
        return BadValue;
    }

    rc = dixLookupDrawable(&drawable, stuff->drawable, client, 0,
                           DixReadAccess);
    if (rc != Success)
        return rc;
    if (drawable->type != DRAWABLE_WINDOW) {
        EPHYR_LOG_ERROR("non drawable windows are not yet supported\n");
        return BadImplementation;
    }
    EPHYR_LOG("lookedup drawable %p\n", drawable);
    window = (WindowPtr) drawable;
    if (findWindowPairFromLocal(window, &pair) && pair) {
        remote_win = pair->remote;
        EPHYR_LOG("found window '%p' paire with remote '%d'\n",
                  window, remote_win);
    }
    else if (!createHostPeerWindow(window, &remote_win)) {
        EPHYR_LOG_ERROR("failed to create host peer window\n");
        return BadAlloc;
    }

    if (!ephyrDRICreateDrawable(stuff->screen,
                                remote_win,
                                (drm_drawable_t *) &rep.hHWDrawable)) {
        EPHYR_LOG_ERROR("failed to create dri drawable\n");
        return BadValue;
    }

    win_priv = GET_EPHYR_DRI_WINDOW_PRIV(window);
    if (!win_priv) {
        win_priv = calloc(1, sizeof(EphyrDRIWindowPrivRec));
        if (!win_priv) {
            EPHYR_LOG_ERROR("failed to allocate window private\n");
            return BadAlloc;
        }
        dixSetPrivate(&window->devPrivates, ephyrDRIWindowKey, win_priv);
        EPHYR_LOG("paired window '%p' with remote '%d'\n", window, remote_win);
    }

    WriteToClient(client, sizeof(xXF86DRICreateDrawableReply), &rep);
    EPHYR_LOG("leave\n");
    return Success;
}

static int
ProcXF86DRIDestroyDrawable(register ClientPtr client)
{
    DrawablePtr drawable = NULL;
    WindowPtr window = NULL;
    EphyrWindowPair *pair = NULL;
    int rc = 0;

    REQUEST(xXF86DRIDestroyDrawableReq);
    REQUEST_SIZE_MATCH(xXF86DRIDestroyDrawableReq);

    EPHYR_LOG("enter\n");
    if (stuff->screen >= screenInfo.numScreens) {
        client->errorValue = stuff->screen;
        return BadValue;
    }

    rc = dixLookupDrawable(&drawable,
                           stuff->drawable, client, 0, DixReadAccess);
    if (rc != Success)
        return rc;
    if (drawable->type != DRAWABLE_WINDOW) {
        EPHYR_LOG_ERROR("non drawable windows are not yet supported\n");
        return BadImplementation;
    }
    window = (WindowPtr) drawable;
    if (!findWindowPairFromLocal(window, &pair) && pair) {
        EPHYR_LOG_ERROR("failed to find pair window\n");
        return BadImplementation;
    }
    if (!ephyrDRIDestroyDrawable(stuff->screen,
                                 pair->remote /*drawable in host x */ )) {
        EPHYR_LOG_ERROR("failed to destroy dri drawable\n");
        return BadImplementation;
    }
    pair->local = NULL;
    pair->remote = 0;

    EPHYR_LOG("leave\n");
    return Success;
}

static int
ProcXF86DRIGetDrawableInfo(register ClientPtr client)
{
    xXF86DRIGetDrawableInfoReply rep = {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0
    };
    DrawablePtr drawable;
    WindowPtr window = NULL;
    EphyrWindowPair *pair = NULL;
    int X = 0, Y = 0, W = 0, H = 0, backX = 0, backY = 0, rc = 0, i = 0;
    drm_clip_rect_t *clipRects = NULL;
    drm_clip_rect_t *backClipRects = NULL;

    REQUEST(xXF86DRIGetDrawableInfoReq);
    REQUEST_SIZE_MATCH(xXF86DRIGetDrawableInfoReq);

    EPHYR_LOG("enter\n");
    if (stuff->screen >= screenInfo.numScreens) {
        client->errorValue = stuff->screen;
        return BadValue;
    }

    rc = dixLookupDrawable(&drawable, stuff->drawable, client, 0,
                           DixReadAccess);
    if (rc != Success || !drawable) {
        EPHYR_LOG_ERROR("could not get drawable\n");
        return rc;
    }

    if (drawable->type != DRAWABLE_WINDOW) {
        EPHYR_LOG_ERROR("non windows type drawables are not yes supported\n");
        return BadImplementation;
    }
    window = (WindowPtr) drawable;
    memset(&pair, 0, sizeof(pair));
    if (!findWindowPairFromLocal(window, &pair) || !pair) {
        EPHYR_LOG_ERROR("failed to find remote peer drawable\n");
        return BadMatch;
    }
    EPHYR_LOG("clip list of xephyr gl drawable:\n");
    for (i = 0; i < RegionNumRects(&window->clipList); i++) {
        EPHYR_LOG("x1:%d, y1:%d, x2:%d, y2:%d\n",
                  RegionRects(&window->clipList)[i].x1,
                  RegionRects(&window->clipList)[i].y1,
                  RegionRects(&window->clipList)[i].x2,
                  RegionRects(&window->clipList)[i].y2);
    }

    if (!ephyrDRIGetDrawableInfo(stuff->screen,
                                 pair->remote /*the drawable in hostx */ ,
                                 (unsigned int *) &rep.drawableTableIndex,
                                 (unsigned int *) &rep.drawableTableStamp,
                                 (int *) &X,
                                 (int *) &Y,
                                 (int *) &W,
                                 (int *) &H,
                                 (int *) &rep.numClipRects,
                                 &clipRects,
                                 &backX,
                                 &backY,
                                 (int *) &rep.numBackClipRects,
                                 &backClipRects)) {
        return BadValue;
    }
    EPHYR_LOG("num clip rects:%d, num back clip rects:%d\n",
              (int) rep.numClipRects, (int) rep.numBackClipRects);

    rep.drawableX = X;
    rep.drawableY = Y;
    rep.drawableWidth = W;
    rep.drawableHeight = H;
    rep.length = (SIZEOF(xXF86DRIGetDrawableInfoReply) - SIZEOF(xGenericReply));

    rep.backX = backX;
    rep.backY = backY;

    if (rep.numClipRects) {
        if (clipRects) {
            ScreenPtr pScreen = screenInfo.screens[stuff->screen];
            int i = 0;

            EPHYR_LOG("clip list of host gl drawable:\n");
            for (i = 0; i < rep.numClipRects; i++) {
                clipRects[i].x1 = max(clipRects[i].x1, 0);
                clipRects[i].y1 = max(clipRects[i].y1, 0);
                clipRects[i].x2 = min(clipRects[i].x2,
                                      pScreen->width + clipRects[i].x1);
                clipRects[i].y2 = min(clipRects[i].y2,
                                      pScreen->width + clipRects[i].y1);

                EPHYR_LOG("x1:%d, y1:%d, x2:%d, y2:%d\n",
                          clipRects[i].x1, clipRects[i].y1,
                          clipRects[i].x2, clipRects[i].y2);
            }
        }
        else {
            rep.numClipRects = 0;
        }
    }
    else {
        EPHYR_LOG("got zero host gl drawable clipping rects\n");
    }
    rep.length += sizeof(drm_clip_rect_t) * rep.numClipRects;
    backClipRects = clipRects;
    rep.numBackClipRects = rep.numClipRects;
    if (rep.numBackClipRects)
        rep.length += sizeof(drm_clip_rect_t) * rep.numBackClipRects;
    EPHYR_LOG("num host clip rects:%d\n", (int) rep.numClipRects);
    EPHYR_LOG("num host back clip rects:%d\n", (int) rep.numBackClipRects);

    rep.length = bytes_to_int32(rep.length);

    WriteToClient(client, sizeof(xXF86DRIGetDrawableInfoReply), &rep);

    if (rep.numClipRects) {
        WriteToClient(client,
                      sizeof(drm_clip_rect_t) * rep.numClipRects,
                      clipRects);
    }

    if (rep.numBackClipRects) {
        WriteToClient(client,
                      sizeof(drm_clip_rect_t) * rep.numBackClipRects,
                      backClipRects);
    }
    free(clipRects);
    clipRects = NULL;

    EPHYR_LOG("leave\n");

    return Success;
}

static int
ProcXF86DRIGetDeviceInfo(register ClientPtr client)
{
    xXF86DRIGetDeviceInfoReply rep = {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0
    };
    drm_handle_t hFrameBuffer;
    void *pDevPrivate;

    REQUEST(xXF86DRIGetDeviceInfoReq);
    REQUEST_SIZE_MATCH(xXF86DRIGetDeviceInfoReq);

    EPHYR_LOG("enter\n");
    if (stuff->screen >= screenInfo.numScreens) {
        client->errorValue = stuff->screen;
        return BadValue;
    }

    if (!ephyrDRIGetDeviceInfo(stuff->screen,
                               &hFrameBuffer,
                               (int *) &rep.framebufferOrigin,
                               (int *) &rep.framebufferSize,
                               (int *) &rep.framebufferStride,
                               (int *) &rep.devPrivateSize, &pDevPrivate)) {
        return BadValue;
    }

    rep.hFrameBufferLow = (CARD32) (hFrameBuffer & 0xffffffff);
#if defined(LONG64) && !defined(__linux__)
    rep.hFrameBufferHigh = (CARD32) (hFrameBuffer >> 32);
#else
    rep.hFrameBufferHigh = 0;
#endif

    if (rep.devPrivateSize) {
        rep.length = bytes_to_int32(SIZEOF(xXF86DRIGetDeviceInfoReply) -
                                    SIZEOF(xGenericReply) +
                                    pad_to_int32(rep.devPrivateSize));
    }

    WriteToClient(client, sizeof(xXF86DRIGetDeviceInfoReply), &rep);
    if (rep.length) {
        WriteToClient(client, rep.devPrivateSize, pDevPrivate);
    }
    EPHYR_LOG("leave\n");
    return Success;
}

static int
ProcXF86DRIDispatch(register ClientPtr client)
{
    REQUEST(xReq);
    EPHYR_LOG("enter\n");

    switch (stuff->data) {
    case X_XF86DRIQueryVersion:{
        EPHYR_LOG("leave\n");
        return ProcXF86DRIQueryVersion(client);
    }
    case X_XF86DRIQueryDirectRenderingCapable:{
        EPHYR_LOG("leave\n");
        return ProcXF86DRIQueryDirectRenderingCapable(client);
    }
    }

    if (!client->local)
        return DRIErrorBase + XF86DRIClientNotLocal;

    switch (stuff->data) {
    case X_XF86DRIOpenConnection:{
        EPHYR_LOG("leave\n");
        return ProcXF86DRIOpenConnection(client);
    }
    case X_XF86DRICloseConnection:{
        EPHYR_LOG("leave\n");
        return ProcXF86DRICloseConnection(client);
    }
    case X_XF86DRIGetClientDriverName:{
        EPHYR_LOG("leave\n");
        return ProcXF86DRIGetClientDriverName(client);
    }
    case X_XF86DRICreateContext:{
        EPHYR_LOG("leave\n");
        return ProcXF86DRICreateContext(client);
    }
    case X_XF86DRIDestroyContext:{
        EPHYR_LOG("leave\n");
        return ProcXF86DRIDestroyContext(client);
    }
    case X_XF86DRICreateDrawable:{
        EPHYR_LOG("leave\n");
        return ProcXF86DRICreateDrawable(client);
    }
    case X_XF86DRIDestroyDrawable:{
        EPHYR_LOG("leave\n");
        return ProcXF86DRIDestroyDrawable(client);
    }
    case X_XF86DRIGetDrawableInfo:{
        EPHYR_LOG("leave\n");
        return ProcXF86DRIGetDrawableInfo(client);
    }
    case X_XF86DRIGetDeviceInfo:{
        EPHYR_LOG("leave\n");
        return ProcXF86DRIGetDeviceInfo(client);
    }
    case X_XF86DRIAuthConnection:{
        EPHYR_LOG("leave\n");
        return ProcXF86DRIAuthConnection(client);
    }
        /* {Open,Close}FullScreen are deprecated now */
    default:{
        EPHYR_LOG("leave\n");
        return BadRequest;
    }
    }
}

static int
SProcXF86DRIQueryVersion(register ClientPtr client)
{
    REQUEST(xXF86DRIQueryVersionReq);
    swaps(&stuff->length);
    return ProcXF86DRIQueryVersion(client);
}

static int
SProcXF86DRIQueryDirectRenderingCapable(register ClientPtr client)
{
    REQUEST(xXF86DRIQueryDirectRenderingCapableReq);
    swaps(&stuff->length);
    swapl(&stuff->screen);
    return ProcXF86DRIQueryDirectRenderingCapable(client);
}

static int
SProcXF86DRIDispatch(register ClientPtr client)
{
    REQUEST(xReq);

    EPHYR_LOG("enter\n");
    /*
     * Only local clients are allowed DRI access, but remote clients still need
     * these requests to find out cleanly.
     */
    switch (stuff->data) {
    case X_XF86DRIQueryVersion:{
        EPHYR_LOG("leave\n");
        return SProcXF86DRIQueryVersion(client);
    }
    case X_XF86DRIQueryDirectRenderingCapable:{
        EPHYR_LOG("leave\n");
        return SProcXF86DRIQueryDirectRenderingCapable(client);
    }
    default:{
        EPHYR_LOG("leave\n");
        return DRIErrorBase + XF86DRIClientNotLocal;
    }
    }
}

Bool
ephyrDRIExtensionInit(ScreenPtr a_screen)
{
    Bool is_ok = FALSE;
    ExtensionEntry *extEntry = NULL;
    EphyrDRIScreenPrivPtr screen_priv = NULL;

    EPHYR_LOG("enter\n");
    if (!hostx_has_dri()) {
        EPHYR_LOG("host does not have DRI extension\n");
        goto out;
    }
    EPHYR_LOG("host X does have DRI extension\n");
    if (!hostx_has_xshape()) {
        EPHYR_LOG("host does not have XShape extension\n");
        goto out;
    }
    EPHYR_LOG("host X does have XShape extension\n");

#ifdef XF86DRI_EVENTS
    EventType = CreateNewResourceType(XF86DRIFreeEvents, "DRIEvents");
    if (!EventType) {
        EPHYR_LOG_ERROR("failed to register DRI event resource type\n");
        goto out;
    }
#endif

    if ((extEntry = AddExtension(XF86DRINAME,
                                 XF86DRINumberEvents,
                                 XF86DRINumberErrors,
                                 ProcXF86DRIDispatch,
                                 SProcXF86DRIDispatch,
                                 NULL, StandardMinorOpcode))) {
        DRIReqCode = (unsigned char) extEntry->base;
        DRIErrorBase = extEntry->errorBase;
    }
    else {
        EPHYR_LOG_ERROR("failed to register DRI extension\n");
        goto out;
    }
    if (!dixRegisterPrivateKey(&ephyrDRIScreenKeyRec, PRIVATE_SCREEN, 0))
        goto out;
    if (!dixRegisterPrivateKey(&ephyrDRIWindowKeyRec, PRIVATE_WINDOW, 0))
        goto out;
    screen_priv = calloc(1, sizeof(EphyrDRIScreenPrivRec));
    if (!screen_priv) {
        EPHYR_LOG_ERROR("failed to allocate screen_priv\n");
        goto out;
    }
    dixSetPrivate(&a_screen->devPrivates, ephyrDRIScreenKey, screen_priv);

    if (!ephyrDRIScreenInit(a_screen)) {
        EPHYR_LOG_ERROR("ephyrDRIScreenInit() failed\n");
        goto out;
    }
    EphyrMirrorHostVisuals(a_screen);
    is_ok = TRUE;
 out:
    EPHYR_LOG("leave\n");
    return is_ok;
}