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 **************************************************************************/
45 static Display *display = NULL;
46 static int screen = 0;
48 static unsigned glxVersion = 0;
49 static const char *extensions = 0;
50 static bool has_GLX_ARB_create_context = false;
53 class GlxVisual : public Visual
71 static void describeEvent(const XEvent &event) {
75 std::cerr << "ConfigureNotify";
78 std::cerr << "Expose";
81 std::cerr << "KeyPress";
84 std::cerr << "MapNotify";
87 std::cerr << "ReparentNotify";
90 std::cerr << "Event " << event.type;
92 std::cerr << " " << event.xany.window << "\n";
96 static void waitForEvent(Window window, int type) {
100 XNextEvent(display, &event);
101 describeEvent(event);
102 } while (event.type != type ||
103 event.xany.window != window);
107 class GlxDrawable : public Drawable
112 GlxDrawable(const Visual *vis, int w, int h) :
115 XVisualInfo *visinfo = dynamic_cast<const GlxVisual *>(visual)->visinfo;
117 Window root = RootWindow(display, screen);
119 /* window attributes */
120 XSetWindowAttributes attr;
121 attr.background_pixel = 0;
122 attr.border_pixel = 0;
123 attr.colormap = XCreateColormap(display, root, visinfo->visual, AllocNone);
124 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
127 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
131 window = XCreateWindow(
141 XSizeHints sizehints;
144 sizehints.width = width;
145 sizehints.height = height;
146 sizehints.flags = USSize | USPosition;
147 XSetNormalHints(display, window, &sizehints);
149 const char *name = "glretrace";
150 XSetStandardProperties(
151 display, window, name, name,
152 None, (char **)NULL, 0, &sizehints);
158 XDestroyWindow(display, window);
162 resize(int w, int h) {
165 // We need to ensure that pending events are processed here, and XSync
166 // with discard = True guarantees that, but it appears the limited
167 // event processing we do so far is sufficient
168 //XSync(display, True);
170 Drawable::resize(w, h);
172 XResizeWindow(display, window, w, h);
174 // Tell the window manager to respect the requested size
175 XSizeHints *size_hints;
176 size_hints = XAllocSizeHints();
177 size_hints->max_width = size_hints->min_width = w;
178 size_hints->max_height = size_hints->min_height = h;
179 size_hints->flags = PMinSize | PMaxSize;
180 XSetWMNormalHints(display, window, size_hints);
183 waitForEvent(window, ConfigureNotify);
190 XMapWindow(display, window);
192 waitForEvent(window, Expose);
198 void swapBuffers(void) {
199 glXSwapBuffers(display, window);
204 class GlxContext : public Context
209 GlxContext(const Visual *vis, GLXContext ctx) :
215 glXDestroyContext(display, context);
221 display = XOpenDisplay(NULL);
223 std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
227 screen = DefaultScreen(display);
229 int major = 0, minor = 0;
230 glXQueryVersion(display, &major, &minor);
231 glxVersion = (major << 8) | minor;
233 extensions = glXQueryExtensionsString(display, screen);
234 has_GLX_ARB_create_context = checkExtension("GLX_ARB_create_context", extensions);
240 XCloseDisplay(display);
246 createVisual(bool doubleBuffer) {
247 GlxVisual *visual = new GlxVisual;
249 if (glxVersion >= 0x0103) {
250 Attributes<int> attribs;
251 attribs.add(GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT);
252 attribs.add(GLX_RENDER_TYPE, GLX_RGBA_BIT);
253 attribs.add(GLX_RED_SIZE, 1);
254 attribs.add(GLX_GREEN_SIZE, 1);
255 attribs.add(GLX_BLUE_SIZE, 1);
256 attribs.add(GLX_ALPHA_SIZE, 1);
257 attribs.add(GLX_DOUBLEBUFFER, doubleBuffer ? GL_TRUE : GL_FALSE);
258 attribs.add(GLX_DEPTH_SIZE, 1);
259 attribs.add(GLX_STENCIL_SIZE, 1);
263 GLXFBConfig * fbconfigs;
264 fbconfigs = glXChooseFBConfig(display, screen, attribs, &num_configs);
265 assert(num_configs && fbconfigs);
266 visual->fbconfig = fbconfigs[0];
267 assert(visual->fbconfig);
268 visual->visinfo = glXGetVisualFromFBConfig(display, visual->fbconfig);
269 assert(visual->visinfo);
271 Attributes<int> attribs;
272 attribs.add(GLX_RGBA);
273 attribs.add(GLX_RED_SIZE, 1);
274 attribs.add(GLX_GREEN_SIZE, 1);
275 attribs.add(GLX_BLUE_SIZE, 1);
276 attribs.add(GLX_ALPHA_SIZE, 1);
278 attribs.add(GLX_DOUBLEBUFFER);
280 attribs.add(GLX_DEPTH_SIZE, 1);
281 attribs.add(GLX_STENCIL_SIZE, 1);
284 visual->visinfo = glXChooseVisual(display, screen, attribs);
291 createDrawable(const Visual *visual, int width, int height)
293 return new GlxDrawable(visual, width, height);
297 createContext(const Visual *_visual, Context *shareContext)
299 const GlxVisual *visual = dynamic_cast<const GlxVisual *>(_visual);
300 GLXContext share_context = NULL;
304 share_context = dynamic_cast<GlxContext*>(shareContext)->context;
308 if (glxVersion >= 0x0104 && has_GLX_ARB_create_context) {
309 Attributes<int> attribs;
310 attribs.add(GLX_RENDER_TYPE, GLX_RGBA_TYPE);
312 attribs.add(GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB);
316 context = glXCreateContextAttribsARB(display, visual->fbconfig, share_context, True, attribs);
319 if (glxVersion >= 0x103) {
320 context = glXCreateNewContext(display, visual->fbconfig, GLX_RGBA_TYPE, share_context, True);
322 context = glXCreateContext(display, visual->visinfo, share_context, True);
325 return new GlxContext(visual, context);
329 makeCurrent(Drawable *drawable, Context *context)
331 if (!drawable || !context) {
332 return glXMakeCurrent(display, None, NULL);
334 GlxDrawable *glxDrawable = dynamic_cast<GlxDrawable *>(drawable);
335 GlxContext *glxContext = dynamic_cast<GlxContext *>(context);
337 return glXMakeCurrent(display, glxDrawable->window, glxContext->context);
342 processEvents(void) {
344 while (XPending(display) > 0) {
346 XNextEvent(display, &event);
347 describeEvent(event);
353 } /* namespace glws */