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 **************************************************************************/
38 static Display *display = NULL;
39 static int screen = 0;
41 static unsigned glxVersion = 0;
42 static const char *extensions = 0;
43 static bool has_GLX_ARB_create_context = false;
46 class GlxVisual : public Visual
64 static void describeEvent(const XEvent &event) {
68 std::cerr << "ConfigureNotify";
71 std::cerr << "Expose";
74 std::cerr << "KeyPress";
77 std::cerr << "MapNotify";
80 std::cerr << "ReparentNotify";
83 std::cerr << "Event " << event.type;
85 std::cerr << " " << event.xany.window << "\n";
89 static void waitForEvent(Window window, int type) {
93 XNextEvent(display, &event);
95 } while (event.type != type ||
96 event.xany.window != window);
100 class GlxDrawable : public Drawable
105 GlxDrawable(const Visual *vis, int w, int h) :
108 XVisualInfo *visinfo = dynamic_cast<const GlxVisual *>(visual)->visinfo;
110 Window root = RootWindow(display, screen);
112 /* window attributes */
113 XSetWindowAttributes attr;
114 attr.background_pixel = 0;
115 attr.border_pixel = 0;
116 attr.colormap = XCreateColormap(display, root, visinfo->visual, AllocNone);
117 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
120 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
124 window = XCreateWindow(
134 XSizeHints sizehints;
137 sizehints.width = width;
138 sizehints.height = height;
139 sizehints.flags = USSize | USPosition;
140 XSetNormalHints(display, window, &sizehints);
142 const char *name = "glretrace";
143 XSetStandardProperties(
144 display, window, name, name,
145 None, (char **)NULL, 0, &sizehints);
151 XDestroyWindow(display, window);
155 resize(int w, int h) {
158 // We need to ensure that pending events are processed here, and XSync
159 // with discard = True guarantees that, but it appears the limited
160 // event processing we do so far is sufficient
161 //XSync(display, True);
163 Drawable::resize(w, h);
165 XResizeWindow(display, window, w, h);
167 // Tell the window manager to respect the requested size
168 XSizeHints *size_hints;
169 size_hints = XAllocSizeHints();
170 size_hints->max_width = size_hints->min_width = w;
171 size_hints->max_height = size_hints->min_height = h;
172 size_hints->flags = PMinSize | PMaxSize;
173 XSetWMNormalHints(display, window, size_hints);
176 waitForEvent(window, ConfigureNotify);
183 XMapWindow(display, window);
185 waitForEvent(window, Expose);
191 void swapBuffers(void) {
192 glXSwapBuffers(display, window);
197 class GlxContext : public Context
202 GlxContext(const Visual *vis, GLXContext ctx) :
208 glXDestroyContext(display, context);
214 display = XOpenDisplay(NULL);
216 std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
220 screen = DefaultScreen(display);
222 int major = 0, minor = 0;
223 glXQueryVersion(display, &major, &minor);
224 glxVersion = (major << 8) | minor;
226 extensions = glXQueryExtensionsString(display, screen);
227 has_GLX_ARB_create_context = checkExtension("GLX_ARB_create_context", extensions);
233 XCloseDisplay(display);
239 createVisual(bool doubleBuffer) {
240 GlxVisual *visual = new GlxVisual;
242 if (glxVersion >= 0x0103) {
243 Attributes<int> attribs;
244 attribs.add(GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT);
245 attribs.add(GLX_RENDER_TYPE, GLX_RGBA_BIT);
246 attribs.add(GLX_RED_SIZE, 1);
247 attribs.add(GLX_GREEN_SIZE, 1);
248 attribs.add(GLX_BLUE_SIZE, 1);
249 attribs.add(GLX_ALPHA_SIZE, 1);
250 attribs.add(GLX_DOUBLEBUFFER, doubleBuffer ? GL_TRUE : GL_FALSE);
251 attribs.add(GLX_DEPTH_SIZE, 1);
252 attribs.add(GLX_STENCIL_SIZE, 1);
256 GLXFBConfig * fbconfigs;
257 fbconfigs = glXChooseFBConfig(display, screen, attribs, &num_configs);
258 assert(num_configs && fbconfigs);
259 visual->fbconfig = fbconfigs[0];
260 assert(visual->fbconfig);
261 visual->visinfo = glXGetVisualFromFBConfig(display, visual->fbconfig);
262 assert(visual->visinfo);
264 Attributes<int> attribs;
265 attribs.add(GLX_RGBA);
266 attribs.add(GLX_RED_SIZE, 1);
267 attribs.add(GLX_GREEN_SIZE, 1);
268 attribs.add(GLX_BLUE_SIZE, 1);
269 attribs.add(GLX_ALPHA_SIZE, 1);
271 attribs.add(GLX_DOUBLEBUFFER);
273 attribs.add(GLX_DEPTH_SIZE, 1);
274 attribs.add(GLX_STENCIL_SIZE, 1);
277 visual->visinfo = glXChooseVisual(display, screen, attribs);
284 createDrawable(const Visual *visual, int width, int height)
286 return new GlxDrawable(visual, width, height);
290 createContext(const Visual *_visual, Context *shareContext)
292 const GlxVisual *visual = dynamic_cast<const GlxVisual *>(_visual);
293 GLXContext share_context = NULL;
297 share_context = dynamic_cast<GlxContext*>(shareContext)->context;
301 if (glxVersion >= 0x0104 && has_GLX_ARB_create_context) {
302 Attributes<int> attribs;
303 attribs.add(GLX_RENDER_TYPE, GLX_RGBA_TYPE);
305 attribs.add(GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB);
309 context = glXCreateContextAttribsARB(display, visual->fbconfig, share_context, True, attribs);
310 } else if (glxVersion >= 0x103) {
311 context = glXCreateNewContext(display, visual->fbconfig, GLX_RGBA_TYPE, share_context, True);
313 context = glXCreateContext(display, visual->visinfo, share_context, True);
316 return new GlxContext(visual, context);
320 makeCurrent(Drawable *drawable, Context *context)
322 if (!drawable || !context) {
323 return glXMakeCurrent(display, None, NULL);
325 GlxDrawable *glxDrawable = dynamic_cast<GlxDrawable *>(drawable);
326 GlxContext *glxContext = dynamic_cast<GlxContext *>(context);
328 return glXMakeCurrent(display, glxDrawable->window, glxContext->context);
333 processEvents(void) {
335 while (XPending(display) > 0) {
337 XNextEvent(display, &event);
338 describeEvent(event);
344 } /* namespace glws */