glx: implement __glXQueryServerString using XCB
[mesa.git] / src / glx / x11 / glx_query.c
1 /*
2 * (C) Copyright IBM Corporation 2004
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file glx_query.c
27 * Generic utility functions to query internal data from the server.
28 *
29 * \author Ian Romanick <idr@us.ibm.com>
30 */
31
32 #include "glxclient.h"
33
34 #if defined(USE_XCB)
35 # include <X11/Xlib-xcb.h>
36 # include <xcb/xcb.h>
37 # include <xcb/glx.h>
38 #endif
39
40 /**
41 * GLX protocol structure for the ficticious "GXLGenericGetString" request.
42 *
43 * This is a non-existant protocol packet. It just so happens that all of
44 * the real protocol packets used to request a string from the server have
45 * an identical binary layout. The only difference between them is the
46 * meaning of the \c for_whom field and the value of the \c glxCode.
47 */
48 typedef struct GLXGenericGetString
49 {
50 CARD8 reqType;
51 CARD8 glxCode;
52 CARD16 length B16;
53 CARD32 for_whom B32;
54 CARD32 name B32;
55 } xGLXGenericGetStringReq;
56
57 /* These defines are only needed to make the GetReq macro happy.
58 */
59 #define sz_xGLXGenericGetStringReq 12
60 #define X_GLXGenericGetString 0
61
62 /**
63 * Query the Server GLX string.
64 * This routine will allocate the necessay space for the string.
65 */
66 char *
67 __glXGetStringFromServer(Display * dpy, int opcode, CARD32 glxCode,
68 CARD32 for_whom, CARD32 name)
69 {
70 xGLXGenericGetStringReq *req;
71 xGLXSingleReply reply;
72 int length;
73 int numbytes;
74 char *buf;
75
76
77 LockDisplay(dpy);
78
79
80 /* All of the GLX protocol requests for getting a string from the server
81 * look the same. The exact meaning of the for_whom field is usually
82 * either the screen number (for glXQueryServerString) or the context tag
83 * (for GLXSingle).
84 */
85
86 GetReq(GLXGenericGetString, req);
87 req->reqType = opcode;
88 req->glxCode = glxCode;
89 req->for_whom = for_whom;
90 req->name = name;
91
92 _XReply(dpy, (xReply *) & reply, 0, False);
93
94 length = reply.length * 4;
95 numbytes = reply.size;
96
97 buf = (char *) Xmalloc(numbytes);
98 if (buf != NULL) {
99 _XRead(dpy, buf, numbytes);
100 length -= numbytes;
101 }
102
103 _XEatData(dpy, length);
104
105 UnlockDisplay(dpy);
106 SyncHandle();
107
108 return buf;
109 }
110
111 #ifdef USE_XCB
112 char *
113 __glXQueryServerString(Display* dpy,
114 CARD32 screen,
115 CARD32 name)
116 {
117 xcb_connection_t *c = XGetXCBConnection(dpy);
118 xcb_glx_query_server_string_reply_t* reply =
119 xcb_glx_query_server_string_reply(c,
120 xcb_glx_query_server_string(c,
121 screen,
122 name),
123 NULL);
124
125 /* The spec doesn't mention this, but the Xorg server replies with
126 * a string already terminated with '\0'. */
127 uint32_t len = xcb_glx_query_server_string_string_length(reply);
128 char* buf = Xmalloc(len);
129 memcpy(buf, xcb_glx_query_server_string_string(reply), len);
130 free(reply);
131
132 return buf;
133 }
134 #endif /* USE_XCB */