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 **************************************************************************/
26 #include "glimports.hpp"
33 class GlxVisual : public Visual
38 GlxVisual(XVisualInfo *vi) :
48 class GlxDrawable : public Drawable
54 GlxDrawable(const Visual *vis, Display *dpy, Window win) :
61 XDestroyWindow(display, window);
65 resize(unsigned w, unsigned h) {
66 Drawable::resize(w, h);
67 XResizeWindow(display, window, w, h);
70 void swapBuffers(void) {
71 glXSwapBuffers(display, window);
76 class GlxContext : public Context
82 GlxContext(const Visual *vis, Display *dpy, GLXContext ctx) :
89 glXDestroyContext(display, context);
94 class GlxWindowSystem : public WindowSystem
102 display = XOpenDisplay(NULL);
103 screen = DefaultScreen(display);
107 XCloseDisplay(display);
111 createVisual(bool doubleBuffer) {
112 int single_attribs[] = {
121 int double_attribs[] = {
131 XVisualInfo *visinfo;
133 visinfo = glXChooseVisual(display, screen, doubleBuffer ? double_attribs : single_attribs);
135 return new GlxVisual(visinfo);
139 createDrawable(const Visual *visual)
141 XVisualInfo *visinfo = dynamic_cast<const GlxVisual *>(visual)->visinfo;
143 Window root = RootWindow(display, screen);
145 /* window attributes */
146 XSetWindowAttributes attr;
147 attr.background_pixel = 0;
148 attr.border_pixel = 0;
149 attr.colormap = XCreateColormap(display, root, visinfo->visual, AllocNone);
150 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
153 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
155 int x = 0, y = 0, width = 256, height = 256;
157 Window window = XCreateWindow(
167 XSizeHints sizehints;
170 sizehints.width = width;
171 sizehints.height = height;
172 sizehints.flags = USSize | USPosition;
173 XSetNormalHints(display, window, &sizehints);
175 const char *name = "glretrace";
176 XSetStandardProperties(
177 display, window, name, name,
178 None, (char **)NULL, 0, &sizehints);
180 XMapWindow(display, window);
182 return new GlxDrawable(visual, display, window);
186 createContext(const Visual *visual)
188 XVisualInfo *visinfo = dynamic_cast<const GlxVisual *>(visual)->visinfo;
189 GLXContext context = glXCreateContext(display, visinfo, NULL, True);
190 return new GlxContext(visual, display, context);
194 makeCurrent(Drawable *drawable, Context *context)
196 if (!drawable || !context) {
197 return glXMakeCurrent(display, NULL, NULL);
199 GlxDrawable *glxDrawable = dynamic_cast<GlxDrawable *>(drawable);
200 GlxContext *glxContext = dynamic_cast<GlxContext *>(context);
202 return glXMakeCurrent(display, glxDrawable->window, glxContext->context);
207 processEvents(void) {
208 while (XPending(display) > 0) {
210 XNextEvent(display, &event);
218 WindowSystem *createNativeWindowSystem(void) {
219 return new GlxWindowSystem();
223 } /* namespace glretrace */