summaryrefslogtreecommitdiff
path: root/src/GLX/libglxproto.c
blob: 98bf71a67d5c4fd6cd115588131e1cd27a68fe52 (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
/*
 * Copyright (c) 2016, NVIDIA CORPORATION.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and/or associated documentation files (the
 * "Materials"), to deal in the Materials without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Materials, and to
 * permit persons to whom the Materials are furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * unaltered in all copies or substantial portions of the Materials.
 * Any additions, deletions, or changes to the original source files
 * must be clearly indicated in accompanying documentation.
 *
 * If only executable code is distributed, then the accompanying
 * documentation must state that "this software is based in part on the
 * work of the Khronos Group."
 *
 * THE MATERIALS ARE 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 NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 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
 * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
 */
#include <X11/Xlibint.h>

#include "libglxproto.h"

#include <GL/glx.h>
#include <GL/glxproto.h>

/*!
 * Reads a reply from the server, including any additional data.
 *
 * This function will read a reply from the server, and it will optionally
 * allocate a buffer and read any variable-length data in the reply.
 *
 * If \c replyData is not \c NULL and the reply contains additional data, then
 * this function will allocate a buffer and read the data into it.
 *
 * If \c replyData is \c NULL, then the caller must read any data in the reply.
 *
 * If the last request generates an error, then it will return the error code,
 * but it will not call the normal X error handler.
 *
 * \param dpyInfo The display connection.
 * \param[out] reply Returns the reply structure.
 * \param[out] replyData If not \c NULL, returns any additional reply data.
 * \return \c Success on a successful reply. If the server sent back an error,
 * then the error code is returned. If something else fails, then -1 is
 * returned.
 */
static Status ReadReply(__GLXdisplayInfo *dpyInfo, xReply *reply, void **replyData)
{
    Display *dpy = dpyInfo->dpy;
    _XAsyncHandler async;
    _XAsyncErrorState state = {};
    Status error = Success;

    state.min_sequence_number = state.max_sequence_number = dpy->request;
    state.major_opcode = dpyInfo->glxMajorOpcode;
    async.next = dpy->async_handlers;
    async.handler = _XAsyncErrorHandler;
    async.data = (XPointer) &state;
    dpy->async_handlers = &async;

    if (!_XReply(dpy, reply, 0, False)) {
        error = -1;
    }
    DeqAsyncHandler(dpy, &async);

    if (state.error_count) {
        error = state.last_error_received;
        if (error == Success) {
            assert(error != Success);
            error = -1;
        }
    }

    if (replyData != NULL) {
        void *data = NULL;
        if (error == Success && reply->generic.length > 0) {
            int length = reply->generic.length * 4;
            data = malloc(length);
            if (data != NULL) {
                _XRead(dpyInfo->dpy, (char *) data, length);
            } else {
                _XEatData(dpyInfo->dpy, length);
                error = -1;
            }
        }
        *replyData = data;;
    }

    return error;
}

char *__glXQueryServerString(__GLXdisplayInfo *dpyInfo, int screen, int name)
{
    Display *dpy = dpyInfo->dpy;
    xGLXQueryServerStringReq *req;
    xGLXSingleReply rep;
    char *ret = NULL;

    if (!dpyInfo->glxSupported) {
        return NULL;
    }

    LockDisplay(dpy);

    GetReq(GLXQueryServerString, req);
    req->reqType = dpyInfo->glxMajorOpcode;
    req->glxCode = X_GLXQueryServerString;
    req->screen = screen;
    req->name = name;

    ReadReply(dpyInfo, (xReply *) &rep, (void **) &ret);

    UnlockDisplay(dpy);
    SyncHandle();

    return ret;
}
int __glXGetDrawableScreen(__GLXdisplayInfo *dpyInfo, GLXDrawable drawable)
{
    Display *dpy = dpyInfo->dpy;
    xGLXGetDrawableAttributesReq *req;
    xGLXGetDrawableAttributesReply rep;
    int *attribs = NULL;
    Status st;

    if (drawable == None) {
        return -1;
    }
    if (!dpyInfo->glxSupported) {
        return 0;
    }

    LockDisplay(dpy);

    GetReq(GLXGetDrawableAttributes, req);
    req->reqType = dpyInfo->glxMajorOpcode;
    req->glxCode = X_GLXGetDrawableAttributes;
    req->drawable = drawable;

    st = ReadReply(dpyInfo, (xReply *) &rep, (void **) &attribs);

    UnlockDisplay(dpy);
    SyncHandle();

    if (st == Success) {
        int screen = 0;
        unsigned int i;

        if (attribs != NULL) {
            for (i=0; i<rep.numAttribs; i++) {
                if (attribs[i * 2] == GLX_SCREEN) {
                    screen = attribs[i * 2 + 1];
                    break;
                }
            }
            free(attribs);
        }
        return screen;
    } else {
        return -1;
    }
}