summaryrefslogtreecommitdiff
path: root/hw/kdrive/ephyr/ephyrglxext.c
blob: df285cfcb9bf7bdbfebf1c167c982fd67445bf76 (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
/*
 * 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.
 *
 * Authors:
 *    Dodji Seketeli <dodji@openedhand.com>
 */
#ifdef HAVE_CONFIG_H
#include <kdrive-config.h>
#endif

#include "extnsionst.h"
#include "ephyrglxext.h"
#include "ephyrhostglx.h"
#define _HAVE_XALLOC_DECLS
#include "ephyrlog.h"
#include <GL/glxproto.h>
#include "glx/glxserver.h"
#include "glx/indirect_table.h"
#include "glx/indirect_util.h"
#include "glx/unpack.h"
#include "hostx.h"

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

int ephyrGLXQueryVersion(__GLXclientState * cl, GLbyte * pc);
int ephyrGLXQueryVersionSwap(__GLXclientState * cl, GLbyte * pc);
int ephyrGLXGetVisualConfigs(__GLXclientState * cl, GLbyte * pc);
int ephyrGLXGetVisualConfigsSwap(__GLXclientState * cl, GLbyte * pc);
int ephyrGLXClientInfo(__GLXclientState * cl, GLbyte * pc);
int ephyrGLXClientInfoSwap(__GLXclientState * cl, GLbyte * pc);
int ephyrGLXQueryServerString(__GLXclientState * a_cl, GLbyte * a_pc);
int ephyrGLXQueryServerStringSwap(__GLXclientState * a_cl, GLbyte * a_pc);
int ephyrGLXGetFBConfigsSGIX(__GLXclientState * a_cl, GLbyte * a_pc);
int ephyrGLXGetFBConfigsSGIXSwap(__GLXclientState * a_cl, GLbyte * a_pc);
int ephyrGLXCreateContext(__GLXclientState * a_cl, GLbyte * a_pc);
int ephyrGLXCreateContextSwap(__GLXclientState * a_cl, GLbyte * a_pc);
int ephyrGLXDestroyContext(__GLXclientState * a_cl, GLbyte * a_pc);
int ephyrGLXDestroyContextSwap(__GLXclientState * a_cl, GLbyte * a_pc);
int ephyrGLXMakeCurrent(__GLXclientState * a_cl, GLbyte * a_pc);
int ephyrGLXMakeCurrentSwap(__GLXclientState * a_cl, GLbyte * a_pc);
int ephyrGLXGetString(__GLXclientState * a_cl, GLbyte * a_pc);
int ephyrGLXGetStringSwap(__GLXclientState * a_cl, GLbyte * a_pc);
int ephyrGLXGetIntegerv(__GLXclientState * a_cl, GLbyte * a_pc);
int ephyrGLXGetIntegervSwap(__GLXclientState * a_cl, GLbyte * a_pc);
int ephyrGLXIsDirect(__GLXclientState * a_cl, GLbyte * a_pc);
int ephyrGLXIsDirectSwap(__GLXclientState * a_cl, GLbyte * a_pc);

Bool
ephyrHijackGLXExtension(void)
{
    const void *(*dispatch_functions)[2];

    if (!hostx_has_glx()) {
        EPHYR_LOG("host X does not have GLX\n");
        return FALSE;
    }
    EPHYR_LOG("host X does have GLX\n");

    if (!Single_dispatch_info.dispatch_functions) {
        EPHYR_LOG_ERROR("could not get dispatch functions table\n");
        return FALSE;
    }
    /*
     * hijack some single entry point dispatch functions
     */
    dispatch_functions = Single_dispatch_info.dispatch_functions;
    EPHYR_RETURN_VAL_IF_FAIL(dispatch_functions, FALSE);

    dispatch_functions[X_GLXQueryVersion][0] = ephyrGLXQueryVersion;
    dispatch_functions[X_GLXQueryVersion][1] = ephyrGLXQueryVersionSwap;

    dispatch_functions[X_GLXGetVisualConfigs][0] = ephyrGLXGetVisualConfigs;
    dispatch_functions[X_GLXGetVisualConfigs][1] = ephyrGLXGetVisualConfigsSwap;
    dispatch_functions[X_GLXClientInfo][0] = ephyrGLXClientInfo;
    dispatch_functions[X_GLXClientInfo][1] = ephyrGLXClientInfoSwap;

    dispatch_functions[X_GLXQueryServerString][0] = ephyrGLXQueryServerString;
    dispatch_functions[X_GLXQueryServerString][1] =
        ephyrGLXQueryServerStringSwap;

    dispatch_functions[X_GLXCreateContext][0] = ephyrGLXCreateContext;
    dispatch_functions[X_GLXCreateContext][1] = ephyrGLXCreateContextSwap;

    dispatch_functions[X_GLXDestroyContext][0] = ephyrGLXDestroyContext;
    dispatch_functions[X_GLXDestroyContext][1] = ephyrGLXDestroyContextSwap;

    dispatch_functions[X_GLXMakeCurrent][0] = ephyrGLXMakeCurrent;
    dispatch_functions[X_GLXMakeCurrent][1] = ephyrGLXMakeCurrentSwap;

    dispatch_functions[X_GLXIsDirect][0] = ephyrGLXIsDirect;
    dispatch_functions[X_GLXIsDirect][1] = ephyrGLXIsDirectSwap;

    dispatch_functions[73][0] = ephyrGLXGetString;
    dispatch_functions[73][1] = ephyrGLXGetStringSwap;

    dispatch_functions[61][0] = ephyrGLXGetIntegerv;
    dispatch_functions[61][1] = ephyrGLXGetIntegervSwap;

    /*
     * hijack some vendor priv entry point dispatch functions
     */
    dispatch_functions = VendorPriv_dispatch_info.dispatch_functions;
    dispatch_functions[92][0] = ephyrGLXGetFBConfigsSGIX;
    dispatch_functions[92][1] = ephyrGLXGetFBConfigsSGIXSwap;
    EPHYR_LOG("hijacked glx entry points to forward requests to host X\n");

    return TRUE;
}

/*********************
 * implementation of
 * hijacked GLX entry
 * points
 ********************/

int
ephyrGLXQueryVersion(__GLXclientState * a_cl, GLbyte * a_pc)
{
    ClientPtr client = a_cl->client;
    xGLXQueryVersionReq *req = (xGLXQueryVersionReq *) a_pc;
    xGLXQueryVersionReply reply;
    int major, minor;
    int res = BadImplementation;

    EPHYR_LOG("enter\n");

    major = req->majorVersion;
    minor = req->minorVersion;

    if (!ephyrHostGLXQueryVersion(&major, &minor)) {
        EPHYR_LOG_ERROR("ephyrHostGLXQueryVersion() failed\n");
        goto out;
    }
    EPHYR_LOG("major:%d, minor:%d\n", major, minor);
    reply = (xGLXQueryVersionReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
        .majorVersion = major,
        .minorVersion = minor
    };

    if (client->swapped) {
        __glXSwapQueryVersionReply(client, &reply);
    }
    else {
        WriteToClient(client, sz_xGLXQueryVersionReply, &reply);
    }

    res = Success;
 out:
    EPHYR_LOG("leave\n");
    return res;
}

int
ephyrGLXQueryVersionSwap(__GLXclientState * a_cl, GLbyte * a_pc)
{
    xGLXQueryVersionReq *req = (xGLXQueryVersionReq *) a_pc;

    __GLX_DECLARE_SWAP_VARIABLES;

    __GLX_SWAP_SHORT(&req->length);
    __GLX_SWAP_INT(&req->majorVersion);
    __GLX_SWAP_INT(&req->minorVersion);
    return ephyrGLXQueryVersion(a_cl, a_pc);
}

static int
ephyrGLXGetVisualConfigsReal(__GLXclientState * a_cl,
                             GLbyte * a_pc, Bool a_do_swap)
{
    xGLXGetVisualConfigsReq *req = (xGLXGetVisualConfigsReq *) a_pc;
    ClientPtr client = a_cl->client;
    xGLXGetVisualConfigsReply reply;
    int32_t *props_buf = NULL, num_visuals = 0,
        num_props = 0, res = BadImplementation, i = 0,
        props_per_visual_size = 0, props_buf_size = 0;
    __GLX_DECLARE_SWAP_VARIABLES;
    __GLX_DECLARE_SWAP_ARRAY_VARIABLES;

    EPHYR_LOG("enter\n");

    if (!ephyrHostGLXGetVisualConfigs(req->screen,
                                      &num_visuals,
                                      &num_props,
                                      &props_buf_size, &props_buf)) {
        EPHYR_LOG_ERROR("ephyrHostGLXGetVisualConfigs() failed\n");
        goto out;
    }
    EPHYR_LOG("num_visuals:%d, num_props:%d\n", num_visuals, num_props);

    reply = (xGLXGetVisualConfigsReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = (num_visuals * __GLX_SIZE_CARD32 * num_props) >> 2,
        .numVisuals = num_visuals,
        .numProps = num_props
    };

    if (a_do_swap) {
        __GLX_SWAP_SHORT(&reply.sequenceNumber);
        __GLX_SWAP_INT(&reply.length);
        __GLX_SWAP_INT(&reply.numVisuals);
        __GLX_SWAP_INT(&reply.numProps);
        __GLX_SWAP_INT_ARRAY(props_buf, num_props);
    }
    WriteToClient(client, sz_xGLXGetVisualConfigsReply, &reply);
    props_per_visual_size = props_buf_size / num_visuals;
    for (i = 0; i < num_visuals; i++) {
        WriteToClient(client,
                      props_per_visual_size,
                      (char *) props_buf + i * props_per_visual_size);
    }
    res = Success;

 out:
    EPHYR_LOG("leave\n");
    free(props_buf);
    props_buf = NULL;

    return res;
}

static int
ephyrGLXGetFBConfigsSGIXReal(__GLXclientState * a_cl,
                             GLbyte * a_pc, Bool a_do_swap)
{
    xGLXGetFBConfigsSGIXReq *req = (xGLXGetFBConfigsSGIXReq *) a_pc;
    ClientPtr client = a_cl->client;
    xGLXGetVisualConfigsReply reply;
    int32_t *props_buf = NULL, num_visuals = 0,
        num_props = 0, res = BadImplementation, i = 0,
        props_per_visual_size = 0, props_buf_size = 0;
    __GLX_DECLARE_SWAP_VARIABLES;
    __GLX_DECLARE_SWAP_ARRAY_VARIABLES;

    EPHYR_LOG("enter\n");

    if (!ephyrHostGLXVendorPrivGetFBConfigsSGIX(req->screen,
                                                &num_visuals,
                                                &num_props,
                                                &props_buf_size, &props_buf)) {
        EPHYR_LOG_ERROR("ephyrHostGLXGetVisualConfigs() failed\n");
        goto out;
    }
    EPHYR_LOG("num_visuals:%d, num_props:%d\n", num_visuals, num_props);

    reply = (xGLXGetVisualConfigsReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = props_buf_size >> 2,
        .numVisuals = num_visuals,
        .numProps = num_props
    };

    if (a_do_swap) {
        __GLX_SWAP_SHORT(&reply.sequenceNumber);
        __GLX_SWAP_INT(&reply.length);
        __GLX_SWAP_INT(&reply.numVisuals);
        __GLX_SWAP_INT(&reply.numProps);
        __GLX_SWAP_INT_ARRAY(props_buf, num_props);
    }
    WriteToClient(client, sz_xGLXGetVisualConfigsReply, &reply);
    props_per_visual_size = props_buf_size / num_visuals;
    for (i = 0; i < num_visuals; i++) {
        WriteToClient(client,
                      props_per_visual_size,
                      &((char *) props_buf)[i * props_per_visual_size]);
    }
    res = Success;

 out:
    EPHYR_LOG("leave\n");
    free(props_buf);
    props_buf = NULL;

    return res;
}

int
ephyrGLXGetVisualConfigs(__GLXclientState * a_cl, GLbyte * a_pc)
{
    return ephyrGLXGetVisualConfigsReal(a_cl, a_pc, FALSE);
}

int
ephyrGLXGetVisualConfigsSwap(__GLXclientState * a_cl, GLbyte * a_pc)
{
    return ephyrGLXGetVisualConfigsReal(a_cl, a_pc, TRUE);
}

int
ephyrGLXClientInfo(__GLXclientState * a_cl, GLbyte * a_pc)
{
    int res = BadImplementation;
    xGLXClientInfoReq *req = (xGLXClientInfoReq *) a_pc;

    EPHYR_LOG("enter\n");
    if (!ephyrHostGLXSendClientInfo(req->major, req->minor, (char *) req + 1)) {
        EPHYR_LOG_ERROR("failed to send client info to host\n");
        goto out;
    }
    res = Success;

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

int
ephyrGLXClientInfoSwap(__GLXclientState * a_cl, GLbyte * a_pc)
{
    xGLXClientInfoReq *req = (xGLXClientInfoReq *) a_pc;

    __GLX_DECLARE_SWAP_VARIABLES;

    __GLX_SWAP_SHORT(&req->length);
    __GLX_SWAP_INT(&req->major);
    __GLX_SWAP_INT(&req->minor);
    __GLX_SWAP_INT(&req->numbytes);

    return ephyrGLXClientInfo(a_cl, a_pc);
}

int
ephyrGLXQueryServerString(__GLXclientState * a_cl, GLbyte * a_pc)
{
    int res = BadImplementation;
    ClientPtr client = a_cl->client;
    xGLXQueryServerStringReq *req = (xGLXQueryServerStringReq *) a_pc;
    xGLXQueryServerStringReply reply;
    char *server_string = NULL;
    int length = 0;

    EPHYR_LOG("enter\n");
    if (!ephyrHostGLXGetStringFromServer(req->screen,
                                         req->name,
                                         EPHYR_HOST_GLX_QueryServerString,
                                         &server_string)) {
        EPHYR_LOG_ERROR("failed to query string from host\n");
        goto out;
    }
    EPHYR_LOG("string: %s\n", server_string);
    length = strlen(server_string) + 1;
    reply = (xGLXQueryServerStringReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = __GLX_PAD(length) >> 2,
        .n = length
    };

    WriteToClient(client, sz_xGLXQueryServerStringReply, &reply);
    WriteToClient(client, (int) (reply.length << 2), server_string);

    res = Success;

 out:
    EPHYR_LOG("leave\n");
    free(server_string);
    server_string = NULL;

    return res;
}

int
ephyrGLXQueryServerStringSwap(__GLXclientState * a_cl, GLbyte * a_pc)
{
    EPHYR_LOG_ERROR("not yet implemented\n");
    return BadImplementation;
}

int
ephyrGLXGetFBConfigsSGIX(__GLXclientState * a_cl, GLbyte * a_pc)
{
    return ephyrGLXGetFBConfigsSGIXReal(a_cl, a_pc, FALSE);
}

int
ephyrGLXGetFBConfigsSGIXSwap(__GLXclientState * a_cl, GLbyte * a_pc)
{
    return ephyrGLXGetFBConfigsSGIXReal(a_cl, a_pc, TRUE);
}

static int
ephyrGLXCreateContextReal(xGLXCreateContextReq * a_req, Bool a_do_swap)
{
    int res = BadImplementation;
    EphyrHostWindowAttributes host_w_attrs;

    __GLX_DECLARE_SWAP_VARIABLES;

    EPHYR_RETURN_VAL_IF_FAIL(a_req, BadValue);
    EPHYR_LOG("enter\n");

    if (a_do_swap) {
        __GLX_SWAP_SHORT(&a_req->length);
        __GLX_SWAP_INT(&a_req->context);
        __GLX_SWAP_INT(&a_req->visual);
        __GLX_SWAP_INT(&a_req->screen);
        __GLX_SWAP_INT(&a_req->shareList);
    }

    EPHYR_LOG("context creation requested. localid:%d, "
              "screen:%d, visual:%d, direct:%d\n",
              (int) a_req->context, (int) a_req->screen,
              (int) a_req->visual, (int) a_req->isDirect);

    memset(&host_w_attrs, 0, sizeof(host_w_attrs));
    if (!hostx_get_window_attributes(hostx_get_window(a_req->screen),
                                     &host_w_attrs)) {
        EPHYR_LOG_ERROR("failed to get host window attrs\n");
        goto out;
    }

    EPHYR_LOG("host window visual id: %d\n", host_w_attrs.visualid);

    if (!ephyrHostGLXCreateContext(a_req->screen,
                                   host_w_attrs.visualid,
                                   a_req->context,
                                   a_req->shareList, a_req->isDirect)) {
        EPHYR_LOG_ERROR("ephyrHostGLXCreateContext() failed\n");
        goto out;
    }
    res = Success;
 out:
    EPHYR_LOG("leave\n");
    return res;
}

int
ephyrGLXCreateContext(__GLXclientState * cl, GLbyte * pc)
{
    xGLXCreateContextReq *req = (xGLXCreateContextReq *) pc;

    return ephyrGLXCreateContextReal(req, FALSE);
}

int
ephyrGLXCreateContextSwap(__GLXclientState * cl, GLbyte * pc)
{
    xGLXCreateContextReq *req = (xGLXCreateContextReq *) pc;

    return ephyrGLXCreateContextReal(req, TRUE);
}

static int
ephyrGLXDestroyContextReal(__GLXclientState * a_cl,
                           GLbyte * a_pc, Bool a_do_swap)
{
    int res = BadImplementation;
    ClientPtr client = a_cl->client;
    xGLXDestroyContextReq *req = (xGLXDestroyContextReq *) a_pc;

    EPHYR_LOG("enter. id:%d\n", (int) req->context);
    if (!ephyrHostDestroyContext(req->context)) {
        EPHYR_LOG_ERROR("ephyrHostDestroyContext() failed\n");
        client->errorValue = req->context;
        goto out;
    }
    res = Success;

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

int
ephyrGLXDestroyContext(__GLXclientState * a_cl, GLbyte * a_pc)
{
    return ephyrGLXDestroyContextReal(a_cl, a_pc, FALSE);
}

int
ephyrGLXDestroyContextSwap(__GLXclientState * a_cl, GLbyte * a_pc)
{
    return ephyrGLXDestroyContextReal(a_cl, a_pc, TRUE);
}

static int
ephyrGLXMakeCurrentReal(__GLXclientState * a_cl, GLbyte * a_pc, Bool a_do_swap)
{
    int res = BadImplementation;
    xGLXMakeCurrentReq *req = (xGLXMakeCurrentReq *) a_pc;
    xGLXMakeCurrentReply reply;
    DrawablePtr drawable = NULL;
    GLXContextTag contextTag = 0;
    int rc = 0;

    EPHYR_LOG("enter\n");
    rc = dixLookupDrawable(&drawable,
                           req->drawable, a_cl->client, 0, DixReadAccess);
    EPHYR_RETURN_VAL_IF_FAIL(drawable, BadValue);
    EPHYR_RETURN_VAL_IF_FAIL(drawable->pScreen, BadValue);
    EPHYR_LOG("screen nummber requested:%d\n", drawable->pScreen->myNum);

    if (!ephyrHostGLXMakeCurrent(hostx_get_window(drawable->pScreen->myNum),
                                 req->context,
                                 req->oldContextTag,
                                 (int *) &contextTag)) {
        EPHYR_LOG_ERROR("ephyrHostGLXMakeCurrent() failed\n");
        goto out;
    }
    reply = (xGLXMakeCurrentReply) {
        .type = X_Reply,
        .sequenceNumber = a_cl->client->sequence,
        .length = 0,
        .contextTag = contextTag
    };
    if (a_do_swap) {
        __GLX_DECLARE_SWAP_VARIABLES;
        __GLX_SWAP_SHORT(&reply.sequenceNumber);
        __GLX_SWAP_INT(&reply.length);
        __GLX_SWAP_INT(&reply.contextTag);
    }
    WriteToClient(a_cl->client, sz_xGLXMakeCurrentReply, &reply);

    res = Success;
 out:
    EPHYR_LOG("leave\n");
    return res;
}

int
ephyrGLXMakeCurrent(__GLXclientState * a_cl, GLbyte * a_pc)
{
    return ephyrGLXMakeCurrentReal(a_cl, a_pc, FALSE);
}

int
ephyrGLXMakeCurrentSwap(__GLXclientState * a_cl, GLbyte * a_pc)
{
    return ephyrGLXMakeCurrentReal(a_cl, a_pc, TRUE);
}

static int
ephyrGLXGetStringReal(__GLXclientState * a_cl, GLbyte * a_pc, Bool a_do_swap)
{
    ClientPtr client = NULL;
    int context_tag = 0, name = 0, res = BadImplementation, length = 0;
    char *string = NULL;

    __GLX_DECLARE_SWAP_VARIABLES;

    EPHYR_RETURN_VAL_IF_FAIL(a_cl && a_pc, BadValue);

    EPHYR_LOG("enter\n");

    client = a_cl->client;

    if (a_do_swap) {
        __GLX_SWAP_INT(a_pc + 4);
        __GLX_SWAP_INT(a_pc + __GLX_SINGLE_HDR_SIZE);
    }
    context_tag = __GLX_GET_SINGLE_CONTEXT_TAG(a_pc);
    a_pc += __GLX_SINGLE_HDR_SIZE;
    name = *(GLenum *) (a_pc + 0);
    EPHYR_LOG("context_tag:%d, name:%d\n", context_tag, name);
    if (!ephyrHostGLXGetStringFromServer(context_tag,
                                         name,
                                         EPHYR_HOST_GLX_GetString, &string)) {
        EPHYR_LOG_ERROR("failed to get string from server\n");
        goto out;
    }
    if (string) {
        length = strlen(string) + 1;
        EPHYR_LOG("got string:'%s', size:%d\n", string, length);
    }
    else {
        EPHYR_LOG("got string: string (null)\n");
    }
    __GLX_BEGIN_REPLY(length);
    __GLX_PUT_SIZE(length);
    __GLX_SEND_HEADER();
    if (a_do_swap) {
        __GLX_SWAP_REPLY_SIZE();
        __GLX_SWAP_REPLY_HEADER();
    }
    WriteToClient(client, length, string);

    res = Success;
 out:
    EPHYR_LOG("leave\n");
    return res;
}

int
ephyrGLXGetString(__GLXclientState * a_cl, GLbyte * a_pc)
{
    return ephyrGLXGetStringReal(a_cl, a_pc, FALSE);
}

int
ephyrGLXGetStringSwap(__GLXclientState * a_cl, GLbyte * a_pc)
{
    return ephyrGLXGetStringReal(a_cl, a_pc, TRUE);
}

static int
ephyrGLXGetIntegervReal(__GLXclientState * a_cl, GLbyte * a_pc, Bool a_do_swap)
{
    int res = BadImplementation;
    xGLXSingleReq *const req = (xGLXSingleReq *) a_pc;
    GLenum int_name;
    int value = 0;
    GLint answer_buf_room[200];
    GLint *buf = NULL;

    EPHYR_LOG("enter\n");

    a_pc += __GLX_SINGLE_HDR_SIZE;

    int_name = *(GLenum *) (a_pc + 0);
    if (!ephyrHostGetIntegerValue(req->contextTag, int_name, &value)) {
        EPHYR_LOG_ERROR("ephyrHostGetIntegerValue() failed\n");
        goto out;
    }
    buf = __glXGetAnswerBuffer(a_cl, sizeof(value),
                               answer_buf_room, sizeof(answer_buf_room), 4);

    if (!buf) {
        EPHYR_LOG_ERROR("failed to allocate reply buffer\n");
        res = BadAlloc;
        goto out;
    }
    __glXSendReply(a_cl->client, buf, 1, sizeof(value), GL_FALSE, 0);
    res = Success;

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

int
ephyrGLXGetIntegerv(__GLXclientState * a_cl, GLbyte * a_pc)
{
    return ephyrGLXGetIntegervReal(a_cl, a_pc, FALSE);
}

int
ephyrGLXGetIntegervSwap(__GLXclientState * a_cl, GLbyte * a_pc)
{
    return ephyrGLXGetIntegervReal(a_cl, a_pc, TRUE);
}

static int
ephyrGLXIsDirectReal(__GLXclientState * a_cl, GLbyte * a_pc, Bool a_do_swap)
{
    int res = BadImplementation;
    ClientPtr client = a_cl->client;
    xGLXIsDirectReq *req = (xGLXIsDirectReq *) a_pc;
    xGLXIsDirectReply reply;
    int is_direct = 0;

    EPHYR_RETURN_VAL_IF_FAIL(a_cl && a_pc, FALSE);

    EPHYR_LOG("enter\n");

    if (!ephyrHostIsContextDirect(req->context, (int *) &is_direct)) {
        EPHYR_LOG_ERROR("ephyrHostIsContextDirect() failed\n");
        goto out;
    }
    reply = (xGLXIsDirectReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
        .isDirect = is_direct
    };

    WriteToClient(client, sz_xGLXIsDirectReply, &reply);
    res = Success;

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

int
ephyrGLXIsDirect(__GLXclientState * a_cl, GLbyte * a_pc)
{
    return ephyrGLXIsDirectReal(a_cl, a_pc, FALSE);
}

int
ephyrGLXIsDirectSwap(__GLXclientState * a_cl, GLbyte * a_pc)
{
    return ephyrGLXIsDirectReal(a_cl, a_pc, TRUE);
}