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 class GlxVisual : public Visual
41 GlxVisual(XVisualInfo *vi) :
51 class GlxDrawable : public Drawable
57 GlxDrawable(const Visual *vis, Display *dpy, Window win) :
64 XDestroyWindow(display, window);
68 resize(unsigned w, unsigned h) {
70 Drawable::resize(w, h);
71 XResizeWindow(display, window, w, h);
75 void swapBuffers(void) {
76 glXSwapBuffers(display, window);
81 class GlxContext : public Context
87 GlxContext(const Visual *vis, Display *dpy, GLXContext ctx) :
94 glXDestroyContext(display, context);
99 class GlxWindowSystem : public WindowSystem
107 display = XOpenDisplay(NULL);
109 std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
112 screen = DefaultScreen(display);
116 XCloseDisplay(display);
120 createVisual(bool doubleBuffer) {
121 int single_attribs[] = {
131 int double_attribs[] = {
142 XVisualInfo *visinfo;
144 visinfo = glXChooseVisual(display, screen, doubleBuffer ? double_attribs : single_attribs);
146 return new GlxVisual(visinfo);
150 createDrawable(const Visual *visual)
152 XVisualInfo *visinfo = dynamic_cast<const GlxVisual *>(visual)->visinfo;
154 Window root = RootWindow(display, screen);
156 /* window attributes */
157 XSetWindowAttributes attr;
158 attr.background_pixel = 0;
159 attr.border_pixel = 0;
160 attr.colormap = XCreateColormap(display, root, visinfo->visual, AllocNone);
161 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
164 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
166 int x = 0, y = 0, width = 256, height = 256;
168 Window window = XCreateWindow(
178 XSizeHints sizehints;
181 sizehints.width = width;
182 sizehints.height = height;
183 sizehints.flags = USSize | USPosition;
184 XSetNormalHints(display, window, &sizehints);
186 const char *name = "glretrace";
187 XSetStandardProperties(
188 display, window, name, name,
189 None, (char **)NULL, 0, &sizehints);
191 XMapWindow(display, window);
193 return new GlxDrawable(visual, display, window);
197 createContext(const Visual *visual)
199 XVisualInfo *visinfo = dynamic_cast<const GlxVisual *>(visual)->visinfo;
200 GLXContext context = glXCreateContext(display, visinfo, NULL, True);
201 return new GlxContext(visual, display, context);
205 makeCurrent(Drawable *drawable, Context *context)
207 if (!drawable || !context) {
208 return glXMakeCurrent(display, None, NULL);
210 GlxDrawable *glxDrawable = dynamic_cast<GlxDrawable *>(drawable);
211 GlxContext *glxContext = dynamic_cast<GlxContext *>(context);
213 return glXMakeCurrent(display, glxDrawable->window, glxContext->context);
218 processEvents(void) {
219 while (XPending(display) > 0) {
221 XNextEvent(display, &event);
229 WindowSystem *createNativeWindowSystem(void) {
230 return new GlxWindowSystem();
234 } /* namespace glretrace */