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 **************************************************************************/
29 #include "glimports.hpp"
36 static Display *display = NULL;
37 static int screen = 0;
40 class GlxVisual : public Visual
45 GlxVisual(XVisualInfo *vi) :
55 static void describeEvent(const XEvent &event) {
59 std::cerr << "ConfigureNotify";
62 std::cerr << "Expose";
65 std::cerr << "KeyPress";
68 std::cerr << "MapNotify";
71 std::cerr << "ReparentNotify";
74 std::cerr << "Event " << event.type;
76 std::cerr << " " << event.xany.window << "\n";
80 static void waitForEvent(Window window, int type) {
84 XNextEvent(display, &event);
86 } while (event.type != type ||
87 event.xany.window != window);
91 class GlxDrawable : public Drawable
96 GlxDrawable(const Visual *vis, int w, int h) :
99 XVisualInfo *visinfo = dynamic_cast<const GlxVisual *>(visual)->visinfo;
101 Window root = RootWindow(display, screen);
103 /* window attributes */
104 XSetWindowAttributes attr;
105 attr.background_pixel = 0;
106 attr.border_pixel = 0;
107 attr.colormap = XCreateColormap(display, root, visinfo->visual, AllocNone);
108 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
111 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
115 window = XCreateWindow(
125 XSizeHints sizehints;
128 sizehints.width = width;
129 sizehints.height = height;
130 sizehints.flags = USSize | USPosition;
131 XSetNormalHints(display, window, &sizehints);
133 const char *name = "glretrace";
134 XSetStandardProperties(
135 display, window, name, name,
136 None, (char **)NULL, 0, &sizehints);
138 XMapWindow(display, window);
140 waitForEvent(window, Expose);
145 XDestroyWindow(display, window);
149 resize(int w, int h) {
152 // We need to ensure that pending events are processed here, and XSync
153 // with discard = True guarantees that, but it appears the limited
154 // event processing we do so far is sufficient
155 //XSync(display, True);
157 Drawable::resize(w, h);
159 XResizeWindow(display, window, w, h);
161 waitForEvent(window, ConfigureNotify);
166 void swapBuffers(void) {
167 glXSwapBuffers(display, window);
172 class GlxContext : public Context
177 GlxContext(const Visual *vis, GLXContext ctx) :
183 glXDestroyContext(display, context);
188 class GlxWindowSystem : public WindowSystem
193 display = XOpenDisplay(NULL);
195 std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
198 screen = DefaultScreen(display);
203 XCloseDisplay(display);
207 createVisual(bool doubleBuffer) {
208 int single_attribs[] = {
218 int double_attribs[] = {
229 XVisualInfo *visinfo;
231 visinfo = glXChooseVisual(display, screen, doubleBuffer ? double_attribs : single_attribs);
233 return new GlxVisual(visinfo);
237 createDrawable(const Visual *visual, int width, int height)
239 return new GlxDrawable(visual, width, height);
243 createContext(const Visual *visual, Context *shareContext)
245 XVisualInfo *visinfo = dynamic_cast<const GlxVisual *>(visual)->visinfo;
246 GLXContext share_context = NULL;
250 share_context = dynamic_cast<GlxContext*>(shareContext)->context;
253 context = glXCreateContext(display, visinfo,
254 share_context, True);
255 return new GlxContext(visual, context);
259 makeCurrent(Drawable *drawable, Context *context)
261 if (!drawable || !context) {
262 return glXMakeCurrent(display, None, NULL);
264 GlxDrawable *glxDrawable = dynamic_cast<GlxDrawable *>(drawable);
265 GlxContext *glxContext = dynamic_cast<GlxContext *>(context);
267 return glXMakeCurrent(display, glxDrawable->window, glxContext->context);
272 processEvents(void) {
274 while (XPending(display) > 0) {
276 XNextEvent(display, &event);
277 describeEvent(event);
284 WindowSystem *createNativeWindowSystem(void) {
285 return new GlxWindowSystem();
289 } /* namespace glretrace */