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
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 class GlxDrawable : public Drawable
94 GlxDrawable(const Visual *vis, int w, int h) :
97 XVisualInfo *visinfo = static_cast<const GlxVisual *>(visual)->visinfo;
99 Window root = RootWindow(display, screen);
101 /* window attributes */
102 XSetWindowAttributes attr;
103 attr.background_pixel = 0;
104 attr.border_pixel = 0;
105 attr.colormap = XCreateColormap(display, root, visinfo->visual, AllocNone);
106 attr.event_mask = StructureNotifyMask;
109 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
113 window = XCreateWindow(
123 XSizeHints sizehints;
126 sizehints.width = width;
127 sizehints.height = height;
128 sizehints.flags = USSize | USPosition;
129 XSetNormalHints(display, window, &sizehints);
131 const char *name = "glretrace";
132 XSetStandardProperties(
133 display, window, name, name,
134 None, (char **)NULL, 0, &sizehints);
139 void waitForEvent(int type) {
142 XWindowEvent(display, window, StructureNotifyMask, &event);
143 describeEvent(event);
144 } while (event.type != type);
148 XDestroyWindow(display, window);
152 resize(int w, int h) {
153 if (w == width && h == height) {
159 // We need to ensure that pending events are processed here, and XSync
160 // with discard = True guarantees that, but it appears the limited
161 // event processing we do so far is sufficient
162 //XSync(display, True);
164 Drawable::resize(w, h);
166 XResizeWindow(display, window, w, h);
168 // Tell the window manager to respect the requested size
169 XSizeHints size_hints;
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);
175 waitForEvent(ConfigureNotify);
187 XMapWindow(display, window);
189 waitForEvent(MapNotify);
196 void swapBuffers(void) {
197 glXSwapBuffers(display, window);
202 class GlxContext : public Context
207 GlxContext(const Visual *vis, Profile prof, GLXContext ctx) :
213 glXDestroyContext(display, context);
219 display = XOpenDisplay(NULL);
221 std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
225 screen = DefaultScreen(display);
227 int major = 0, minor = 0;
228 glXQueryVersion(display, &major, &minor);
229 glxVersion = (major << 8) | minor;
231 extensions = glXQueryExtensionsString(display, screen);
232 has_GLX_ARB_create_context = checkExtension("GLX_ARB_create_context", extensions);
238 XCloseDisplay(display);
244 createVisual(bool doubleBuffer) {
245 GlxVisual *visual = new GlxVisual;
247 if (glxVersion >= 0x0103) {
248 Attributes<int> attribs;
249 attribs.add(GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT);
250 attribs.add(GLX_RENDER_TYPE, GLX_RGBA_BIT);
251 attribs.add(GLX_RED_SIZE, 1);
252 attribs.add(GLX_GREEN_SIZE, 1);
253 attribs.add(GLX_BLUE_SIZE, 1);
254 attribs.add(GLX_ALPHA_SIZE, 1);
255 attribs.add(GLX_DOUBLEBUFFER, doubleBuffer ? GL_TRUE : GL_FALSE);
256 attribs.add(GLX_DEPTH_SIZE, 1);
257 attribs.add(GLX_STENCIL_SIZE, 1);
261 GLXFBConfig * fbconfigs;
262 fbconfigs = glXChooseFBConfig(display, screen, attribs, &num_configs);
263 assert(num_configs && fbconfigs);
264 visual->fbconfig = fbconfigs[0];
265 assert(visual->fbconfig);
266 visual->visinfo = glXGetVisualFromFBConfig(display, visual->fbconfig);
267 assert(visual->visinfo);
269 Attributes<int> attribs;
270 attribs.add(GLX_RGBA);
271 attribs.add(GLX_RED_SIZE, 1);
272 attribs.add(GLX_GREEN_SIZE, 1);
273 attribs.add(GLX_BLUE_SIZE, 1);
274 attribs.add(GLX_ALPHA_SIZE, 1);
276 attribs.add(GLX_DOUBLEBUFFER);
278 attribs.add(GLX_DEPTH_SIZE, 1);
279 attribs.add(GLX_STENCIL_SIZE, 1);
282 visual->visinfo = glXChooseVisual(display, screen, attribs);
289 createDrawable(const Visual *visual, int width, int height)
291 return new GlxDrawable(visual, width, height);
295 createContext(const Visual *_visual, Context *shareContext, Profile profile)
297 const GlxVisual *visual = static_cast<const GlxVisual *>(_visual);
298 GLXContext share_context = NULL;
301 if (profile != PROFILE_COMPAT) {
306 share_context = static_cast<GlxContext*>(shareContext)->context;
309 if (glxVersion >= 0x0104 && has_GLX_ARB_create_context) {
310 Attributes<int> attribs;
311 attribs.add(GLX_RENDER_TYPE, GLX_RGBA_TYPE);
313 attribs.add(GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB);
317 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, profile, context);
329 makeCurrent(Drawable *drawable, Context *context)
331 if (!drawable || !context) {
332 return glXMakeCurrent(display, None, NULL);
334 GlxDrawable *glxDrawable = static_cast<GlxDrawable *>(drawable);
335 GlxContext *glxContext = static_cast<GlxContext *>(context);
337 return glXMakeCurrent(display, glxDrawable->window, glxContext->context);
342 processEvents(void) {
343 while (XPending(display) > 0) {
345 XNextEvent(display, &event);
346 describeEvent(event);
352 } /* namespace glws */