1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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 NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
39 static Display *display = NULL;
40 static int screen = 0;
42 static unsigned glxVersion = 0;
43 static const char *extensions = 0;
44 static bool has_GLX_ARB_create_context = false;
47 class GlxVisual : public Visual
65 static void describeEvent(const XEvent &event) {
69 std::cerr << "ConfigureNotify";
72 std::cerr << "Expose";
75 std::cerr << "KeyPress";
78 std::cerr << "MapNotify";
81 std::cerr << "ReparentNotify";
84 std::cerr << "Event " << event.type;
86 std::cerr << " " << event.xany.window << "\n";
90 class GlxDrawable : public Drawable
95 GlxDrawable(const Visual *vis, int w, int h) :
98 XVisualInfo *visinfo = dynamic_cast<const GlxVisual *>(visual)->visinfo;
100 Window root = RootWindow(display, screen);
102 /* window attributes */
103 XSetWindowAttributes attr;
104 attr.background_pixel = 0;
105 attr.border_pixel = 0;
106 attr.colormap = XCreateColormap(display, root, visinfo->visual, AllocNone);
107 attr.event_mask = StructureNotifyMask;
110 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
114 window = XCreateWindow(
124 XSizeHints sizehints;
127 sizehints.width = width;
128 sizehints.height = height;
129 sizehints.flags = USSize | USPosition;
130 XSetNormalHints(display, window, &sizehints);
132 const char *name = "glretrace";
133 XSetStandardProperties(
134 display, window, name, name,
135 None, (char **)NULL, 0, &sizehints);
140 void waitForEvent(int type) {
143 XWindowEvent(display, window, StructureNotifyMask, &event);
144 describeEvent(event);
145 } while (event.type != type);
149 XDestroyWindow(display, window);
153 resize(int w, int h) {
154 if (w == width && h == height) {
160 // We need to ensure that pending events are processed here, and XSync
161 // with discard = True guarantees that, but it appears the limited
162 // event processing we do so far is sufficient
163 //XSync(display, True);
165 Drawable::resize(w, h);
167 XResizeWindow(display, window, w, h);
169 // Tell the window manager to respect the requested size
170 XSizeHints size_hints;
171 size_hints.max_width = size_hints.min_width = w;
172 size_hints.max_height = size_hints.min_height = h;
173 size_hints.flags = PMinSize | PMaxSize;
174 XSetWMNormalHints(display, window, &size_hints);
176 waitForEvent(ConfigureNotify);
188 XMapWindow(display, window);
190 waitForEvent(MapNotify);
197 void swapBuffers(void) {
198 glXSwapBuffers(display, window);
203 class GlxContext : public Context
208 GlxContext(const Visual *vis, GLXContext ctx) :
214 glXDestroyContext(display, context);
220 display = XOpenDisplay(NULL);
222 std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
226 screen = DefaultScreen(display);
228 int major = 0, minor = 0;
229 glXQueryVersion(display, &major, &minor);
230 glxVersion = (major << 8) | minor;
232 extensions = glXQueryExtensionsString(display, screen);
233 has_GLX_ARB_create_context = checkExtension("GLX_ARB_create_context", extensions);
239 XCloseDisplay(display);
245 createVisual(bool doubleBuffer) {
246 GlxVisual *visual = new GlxVisual;
248 if (glxVersion >= 0x0103) {
249 Attributes<int> attribs;
250 attribs.add(GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT);
251 attribs.add(GLX_RENDER_TYPE, GLX_RGBA_BIT);
252 attribs.add(GLX_RED_SIZE, 1);
253 attribs.add(GLX_GREEN_SIZE, 1);
254 attribs.add(GLX_BLUE_SIZE, 1);
255 attribs.add(GLX_ALPHA_SIZE, 1);
256 attribs.add(GLX_DOUBLEBUFFER, doubleBuffer ? GL_TRUE : GL_FALSE);
257 attribs.add(GLX_DEPTH_SIZE, 1);
258 attribs.add(GLX_STENCIL_SIZE, 1);
262 GLXFBConfig * fbconfigs;
263 fbconfigs = glXChooseFBConfig(display, screen, attribs, &num_configs);
264 assert(num_configs && fbconfigs);
265 visual->fbconfig = fbconfigs[0];
266 assert(visual->fbconfig);
267 visual->visinfo = glXGetVisualFromFBConfig(display, visual->fbconfig);
268 assert(visual->visinfo);
270 Attributes<int> attribs;
271 attribs.add(GLX_RGBA);
272 attribs.add(GLX_RED_SIZE, 1);
273 attribs.add(GLX_GREEN_SIZE, 1);
274 attribs.add(GLX_BLUE_SIZE, 1);
275 attribs.add(GLX_ALPHA_SIZE, 1);
277 attribs.add(GLX_DOUBLEBUFFER);
279 attribs.add(GLX_DEPTH_SIZE, 1);
280 attribs.add(GLX_STENCIL_SIZE, 1);
283 visual->visinfo = glXChooseVisual(display, screen, attribs);
290 createDrawable(const Visual *visual, int width, int height)
292 return new GlxDrawable(visual, width, height);
296 createContext(const Visual *_visual, Context *shareContext)
298 const GlxVisual *visual = dynamic_cast<const GlxVisual *>(_visual);
299 GLXContext share_context = NULL;
303 share_context = dynamic_cast<GlxContext*>(shareContext)->context;
306 if (glxVersion >= 0x0104 && has_GLX_ARB_create_context) {
307 Attributes<int> attribs;
308 attribs.add(GLX_RENDER_TYPE, GLX_RGBA_TYPE);
310 attribs.add(GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB);
314 context = glXCreateContextAttribsARB(display, visual->fbconfig, share_context, True, attribs);
316 if (glxVersion >= 0x103) {
317 context = glXCreateNewContext(display, visual->fbconfig, GLX_RGBA_TYPE, share_context, True);
319 context = glXCreateContext(display, visual->visinfo, share_context, True);
322 return new GlxContext(visual, context);
326 makeCurrent(Drawable *drawable, Context *context)
328 if (!drawable || !context) {
329 return glXMakeCurrent(display, None, NULL);
331 GlxDrawable *glxDrawable = dynamic_cast<GlxDrawable *>(drawable);
332 GlxContext *glxContext = dynamic_cast<GlxContext *>(context);
334 return glXMakeCurrent(display, glxDrawable->window, glxContext->context);
339 processEvents(void) {
340 while (XPending(display) > 0) {
342 XNextEvent(display, &event);
343 describeEvent(event);
349 } /* namespace glws */