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;
39 class GlxVisual : public Visual
44 GlxVisual(XVisualInfo *vi) :
54 class GlxDrawable : public Drawable
59 GlxDrawable(const Visual *vis, int w, int h) :
62 XVisualInfo *visinfo = dynamic_cast<const GlxVisual *>(visual)->visinfo;
64 Window root = RootWindow(display, screen);
66 /* window attributes */
67 XSetWindowAttributes attr;
68 attr.background_pixel = 0;
69 attr.border_pixel = 0;
70 attr.colormap = XCreateColormap(display, root, visinfo->visual, AllocNone);
71 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
74 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
78 window = XCreateWindow(
91 sizehints.width = width;
92 sizehints.height = height;
93 sizehints.flags = USSize | USPosition;
94 XSetNormalHints(display, window, &sizehints);
96 const char *name = "glretrace";
97 XSetStandardProperties(
98 display, window, name, name,
99 None, (char **)NULL, 0, &sizehints);
101 XMapWindow(display, window);
105 XDestroyWindow(display, window);
109 resize(int w, int h) {
111 Drawable::resize(w, h);
112 XResizeWindow(display, window, w, h);
116 void swapBuffers(void) {
117 glXSwapBuffers(display, window);
122 class GlxContext : public Context
127 GlxContext(const Visual *vis, GLXContext ctx) :
133 glXDestroyContext(display, context);
138 class GlxWindowSystem : public WindowSystem
143 display = XOpenDisplay(NULL);
145 std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
148 screen = DefaultScreen(display);
153 XCloseDisplay(display);
157 createVisual(bool doubleBuffer) {
158 int single_attribs[] = {
168 int double_attribs[] = {
179 XVisualInfo *visinfo;
181 visinfo = glXChooseVisual(display, screen, doubleBuffer ? double_attribs : single_attribs);
183 return new GlxVisual(visinfo);
187 createDrawable(const Visual *visual, int width, int height)
189 return new GlxDrawable(visual, width, height);
193 createContext(const Visual *visual, Context *shareContext)
195 XVisualInfo *visinfo = dynamic_cast<const GlxVisual *>(visual)->visinfo;
196 GLXContext share_context = NULL;
200 share_context = dynamic_cast<GlxContext*>(shareContext)->context;
203 context = glXCreateContext(display, visinfo,
204 share_context, True);
205 return new GlxContext(visual, context);
209 makeCurrent(Drawable *drawable, Context *context)
211 if (!drawable || !context) {
212 return glXMakeCurrent(display, None, NULL);
214 GlxDrawable *glxDrawable = dynamic_cast<GlxDrawable *>(drawable);
215 GlxContext *glxContext = dynamic_cast<GlxContext *>(context);
217 return glXMakeCurrent(display, glxDrawable->window, glxContext->context);
222 processEvents(void) {
223 while (XPending(display) > 0) {
225 XNextEvent(display, &event);
233 WindowSystem *createNativeWindowSystem(void) {
234 return new GlxWindowSystem();
238 } /* namespace glretrace */