forked from apitrace/apitrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglws_xlib.cpp
317 lines (260 loc) · 8.11 KB
/
glws_xlib.cpp
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
/**************************************************************************
*
* Copyright 2014 VMware, Inc.
* Copyright 2011 Jose Fonseca
* 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 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 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 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 SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
**************************************************************************/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "glproc.hpp"
#include "glws.hpp"
#include "ws.hpp"
namespace glws {
Display *display = NULL;
int screen = 0;
// Ignore headless with NVIDIA proprietary drivers, as glReadPixels is not
// realiable for occluded windows due to the fragment ownership test,
//
// A simple workaround is to use a X desktop compositor, if we could detect
// when one is in use.
//
// The more comprehensive solution would be to use Pbuffers in headless mode.
static bool ignoreHeadless = false;
static void
describeEvent(const XEvent &event) {
if (0) {
switch (event.type) {
case ConfigureNotify:
std::cerr << "ConfigureNotify";
break;
case Expose:
std::cerr << "Expose";
break;
case KeyPress:
std::cerr << "KeyPress";
break;
case MapNotify:
std::cerr << "MapNotify";
break;
case ReparentNotify:
std::cerr << "ReparentNotify";
break;
default:
std::cerr << "Event " << event.type;
}
std::cerr << " " << event.xany.window << "\n";
}
}
static void
processEvent(XEvent &event) {
describeEvent(event);
switch (event.type) {
case KeyPress:
{
char buffer[32];
KeySym keysym;
XLookupString(&event.xkey, buffer, sizeof buffer - 1, &keysym, NULL);
if (keysym == XK_Escape) {
exit(0);
}
}
break;
}
}
static int
errorHandler(Display *dpy, XErrorEvent *error)
{
char buffer[512];
XGetErrorText(dpy, error->error_code, buffer, sizeof buffer);
std::cerr << "error: xlib: " << buffer;
if (error->request_code < 128) {
std::string request_code = std::to_string(error->request_code);
XGetErrorDatabaseText(dpy, "XRequest", request_code.c_str(), "", buffer, sizeof buffer);
std::cerr << " in " << buffer;
}
std::cerr << "\n";
return 0;
}
static int (*oldErrorHandler)(Display *, XErrorEvent *) = NULL;
void
initX(void)
{
#ifndef NDEBUG
_Xdebug = 1;
#endif
XInitThreads();
oldErrorHandler = XSetErrorHandler(errorHandler);
display = XOpenDisplay(NULL);
if (!display) {
std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
exit(1);
}
screen = DefaultScreen(display);
/*
* Detect NVIDIA GLX driver.
*/
int numExtensions = 0;
char **extensions;
extensions = XListExtensions(display, &numExtensions);
if (extensions) {
for (int i = 0; i < numExtensions; ++i) {
if (strcmp(extensions[i], "NV-GLX") == 0) {
ignoreHeadless = true;
break;
}
}
XFreeExtensionList(extensions);
}
}
void
cleanupX(void)
{
if (display) {
XCloseDisplay(display);
display = NULL;
}
XSetErrorHandler(oldErrorHandler);
oldErrorHandler = NULL;
}
bool
processEvents(void)
{
while (XPending(display) > 0) {
XEvent event;
XNextEvent(display, &event);
processEvent(event);
}
return true;
}
static XEvent
waitForEvent(Window window, int type, long request = 0)
{
XEvent event;
do {
XWindowEvent(display, window, StructureNotifyMask | KeyPressMask, &event);
processEvent(event);
} while (event.type != type || event.xany.serial < request);
return event;
}
void
processKeys(Window window)
{
XEvent event;
while (XCheckWindowEvent(display, window, StructureNotifyMask | KeyPressMask, &event)) {
processEvent(event);
}
}
Window
createWindow(XVisualInfo *visinfo,
const char *name,
int width, int height)
{
Window root = RootWindow(display, screen);
/* window attributes */
XSetWindowAttributes attr;
attr.background_pixel = 0;
attr.border_pixel = 0;
attr.colormap = XCreateColormap(display, root, visinfo->visual, AllocNone);
attr.event_mask = StructureNotifyMask | KeyPressMask;
unsigned long mask;
mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
int x = 0, y = 0;
Window window = XCreateWindow(
display, root,
x, y, width, height,
0,
visinfo->depth,
InputOutput,
visinfo->visual,
mask,
&attr);
XSizeHints *sizeHints = XAllocSizeHints();
sizeHints->x = x;
sizeHints->y = y;
sizeHints->width = width;
sizeHints->height = height;
sizeHints->flags = USSize | USPosition;
XSetWMNormalHints(display, window, sizeHints);
XSetStandardProperties(
display, window, name, name,
None, (char **)NULL, 0, sizeHints);
XFree(sizeHints);
XSync(display, False);
return window;
}
void
resizeWindow(Window window, int w, int h)
{
// We need to ensure that pending events are processed here, and XSync
// with discard = True guarantees that, but it appears the limited
// event processing we do so far is sufficient
if (0) {
XSync(display, True);
} else {
processEvents();
}
// Tell the window manager to respect the requested size
XSizeHints *sizeHints = XAllocSizeHints();
sizeHints->max_width = sizeHints->min_width = w;
sizeHints->max_height = sizeHints->min_height = h;
sizeHints->flags = PMinSize | PMaxSize;
XSetWMNormalHints(display, window, sizeHints);
XFree(sizeHints);
// We need to try multiple times because we can't distinguish the configure
// event due to our resize, from the configure event from other actions
// (e.g, user moving our window around.) It's not safe to wait
// indefinitely for the event with the desired size neither, because it's
// possible the window manager does not allow a window that size.
XEvent event;
for (unsigned attempt = 0; attempt < 4; ++attempt) {
long request = NextRequest(display);
XResizeWindow(display, window, w, h);
event = waitForEvent(window, ConfigureNotify, request);
assert(event.type == ConfigureNotify);
assert(event.xany.window == window);
assert(event.xany.serial >= request);
if (event.xconfigure.width == w &&
event.xconfigure.height == h) {
return;
}
}
std::cerr
<< "warning: xlib: expected " << w << "x" << h << " configure event, "
<< "got " << event.xconfigure.width << "x" << event.xconfigure.height << "\n";
}
void
showWindow(Window window)
{
if (ignoreHeadless || !ws::headless) {
long request = NextRequest(display);
XMapWindow(display, window);
waitForEvent(window, MapNotify, request);
}
}
void
setWindowName(Window window, const char *name)
{
XStoreName(display, window, const_cast<char *>(name));
}
} /* namespace glws */