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
63 static void describeEvent(const XEvent &event) {
67 std::cerr << "ConfigureNotify";
70 std::cerr << "Expose";
73 std::cerr << "KeyPress";
76 std::cerr << "MapNotify";
79 std::cerr << "ReparentNotify";
82 std::cerr << "Event " << event.type;
84 std::cerr << " " << event.xany.window << "\n";
88 class GlxDrawable : public Drawable
93 GlxDrawable(const Visual *vis, int w, int h) :
96 XVisualInfo *visinfo = static_cast<const GlxVisual *>(visual)->visinfo;
98 Window root = RootWindow(display, screen);
100 /* window attributes */
101 XSetWindowAttributes attr;
102 attr.background_pixel = 0;
103 attr.border_pixel = 0;
104 attr.colormap = XCreateColormap(display, root, visinfo->visual, AllocNone);
105 attr.event_mask = StructureNotifyMask;
108 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
112 window = XCreateWindow(
122 XSizeHints sizehints;
125 sizehints.width = width;
126 sizehints.height = height;
127 sizehints.flags = USSize | USPosition;
128 XSetNormalHints(display, window, &sizehints);
130 const char *name = "glretrace";
131 XSetStandardProperties(
132 display, window, name, name,
133 None, (char **)NULL, 0, &sizehints);
138 void waitForEvent(int type) {
141 XWindowEvent(display, window, StructureNotifyMask, &event);
142 describeEvent(event);
143 } while (event.type != type);
147 XDestroyWindow(display, window);
151 resize(int w, int h) {
152 if (w == width && h == height) {
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.max_width = size_hints.min_width = w;
170 size_hints.max_height = size_hints.min_height = h;
171 size_hints.flags = PMinSize | PMaxSize;
172 XSetWMNormalHints(display, window, &size_hints);
174 waitForEvent(ConfigureNotify);
186 XMapWindow(display, window);
188 waitForEvent(MapNotify);
195 void swapBuffers(void) {
196 glXSwapBuffers(display, window);
201 class GlxContext : public Context
206 GlxContext(const Visual *vis, Profile prof, GLXContext ctx) :
212 glXDestroyContext(display, context);
218 display = XOpenDisplay(NULL);
220 std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
224 screen = DefaultScreen(display);
226 int major = 0, minor = 0;
227 glXQueryVersion(display, &major, &minor);
228 glxVersion = (major << 8) | minor;
230 extensions = glXQueryExtensionsString(display, screen);
231 has_GLX_ARB_create_context = checkExtension("GLX_ARB_create_context", extensions);
237 XCloseDisplay(display);
243 createVisual(bool doubleBuffer, Profile profile) {
244 if (profile != PROFILE_COMPAT &&
245 profile != PROFILE_CORE) {
249 GlxVisual *visual = new GlxVisual;
251 if (glxVersion >= 0x0103) {
252 Attributes<int> attribs;
253 attribs.add(GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT);
254 attribs.add(GLX_RENDER_TYPE, GLX_RGBA_BIT);
255 attribs.add(GLX_RED_SIZE, 1);
256 attribs.add(GLX_GREEN_SIZE, 1);
257 attribs.add(GLX_BLUE_SIZE, 1);
258 attribs.add(GLX_ALPHA_SIZE, 1);
259 attribs.add(GLX_DOUBLEBUFFER, doubleBuffer ? GL_TRUE : GL_FALSE);
260 attribs.add(GLX_DEPTH_SIZE, 1);
261 attribs.add(GLX_STENCIL_SIZE, 1);
265 GLXFBConfig * fbconfigs;
266 fbconfigs = glXChooseFBConfig(display, screen, attribs, &num_configs);
267 assert(num_configs && fbconfigs);
268 visual->fbconfig = fbconfigs[0];
269 assert(visual->fbconfig);
270 visual->visinfo = glXGetVisualFromFBConfig(display, visual->fbconfig);
271 assert(visual->visinfo);
273 Attributes<int> attribs;
274 attribs.add(GLX_RGBA);
275 attribs.add(GLX_RED_SIZE, 1);
276 attribs.add(GLX_GREEN_SIZE, 1);
277 attribs.add(GLX_BLUE_SIZE, 1);
278 attribs.add(GLX_ALPHA_SIZE, 1);
280 attribs.add(GLX_DOUBLEBUFFER);
282 attribs.add(GLX_DEPTH_SIZE, 1);
283 attribs.add(GLX_STENCIL_SIZE, 1);
286 visual->visinfo = glXChooseVisual(display, screen, attribs);
293 createDrawable(const Visual *visual, int width, int height)
295 return new GlxDrawable(visual, width, height);
299 createContext(const Visual *_visual, Context *shareContext, Profile profile, bool debug)
301 const GlxVisual *visual = static_cast<const GlxVisual *>(_visual);
302 GLXContext share_context = NULL;
306 share_context = static_cast<GlxContext*>(shareContext)->context;
309 if (glxVersion >= 0x0104 && has_GLX_ARB_create_context) {
310 Attributes<int> attribs;
312 attribs.add(GLX_RENDER_TYPE, GLX_RGBA_TYPE);
314 attribs.add(GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB);
321 // XXX: This will invariable return a 3.2 context, when supported.
322 // We probably should have a PROFILE_CORE_XX per version.
323 attribs.add(GLX_CONTEXT_MAJOR_VERSION_ARB, 3);
324 attribs.add(GLX_CONTEXT_MINOR_VERSION_ARB, 2);
325 attribs.add(GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB);
333 context = glXCreateContextAttribsARB(display, visual->fbconfig, share_context, True, attribs);
335 if (profile != PROFILE_COMPAT) {
339 if (glxVersion >= 0x103) {
340 context = glXCreateNewContext(display, visual->fbconfig, GLX_RGBA_TYPE, share_context, True);
342 context = glXCreateContext(display, visual->visinfo, share_context, True);
350 return new GlxContext(visual, profile, context);
354 makeCurrent(Drawable *drawable, Context *context)
356 if (!drawable || !context) {
357 return glXMakeCurrent(display, None, NULL);
359 GlxDrawable *glxDrawable = static_cast<GlxDrawable *>(drawable);
360 GlxContext *glxContext = static_cast<GlxContext *>(context);
362 return glXMakeCurrent(display, glxDrawable->window, glxContext->context);
367 processEvents(void) {
368 while (XPending(display) > 0) {
370 XNextEvent(display, &event);
371 describeEvent(event);
377 } /* namespace glws */