]> git.cworth.org Git - apitrace/blob - glws_glx.cpp
Understand D3DFMT_RAWZ too.
[apitrace] / glws_glx.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 #include "glimports.hpp"
27 #include "glws.hpp"
28
29
30 namespace glws {
31
32
33 class GlxVisual : public Visual
34 {
35 public:
36     XVisualInfo *visinfo;
37
38     GlxVisual(XVisualInfo *vi) :
39         visinfo(vi)
40     {}
41
42     ~GlxVisual() {
43         XFree(visinfo);
44     }
45 };
46
47
48 class GlxDrawable : public Drawable
49 {
50 public:
51     Display *display;
52     Window window;
53
54     GlxDrawable(const Visual *vis, Display *dpy, Window win) :
55         Drawable(vis),
56         display(dpy), 
57         window(win)
58     {}
59
60     ~GlxDrawable() {
61         XDestroyWindow(display, window);
62     }
63     
64     void
65     resize(unsigned w, unsigned h) {
66         Drawable::resize(w, h);
67         XResizeWindow(display, window, w, h);
68     }
69
70     void swapBuffers(void) {
71         glXSwapBuffers(display, window);
72     }
73 };
74
75
76 class GlxContext : public Context
77 {
78 public:
79     Display *display;
80     GLXContext context;
81     
82     GlxContext(const Visual *vis, Display *dpy, GLXContext ctx) :
83         Context(vis),
84         display(dpy), 
85         context(ctx)
86     {}
87
88     ~GlxContext() {
89         glXDestroyContext(display, context);
90     }
91 };
92
93
94 class GlxWindowSystem : public WindowSystem
95 {
96 private:
97     Display *display;
98     int screen;
99
100 public:
101     GlxWindowSystem() {
102         display = XOpenDisplay(NULL);
103         screen = DefaultScreen(display);
104     }
105
106     ~GlxWindowSystem() {
107         XCloseDisplay(display);
108     }
109
110     Visual *
111     createVisual(bool doubleBuffer) {
112         int single_attribs[] = {
113             GLX_RGBA,
114             GLX_RED_SIZE, 1,
115             GLX_GREEN_SIZE, 1,
116             GLX_BLUE_SIZE, 1,
117             GLX_DEPTH_SIZE, 1,
118             GLX_STENCIL_SIZE, 1,
119             None
120         };
121
122         int double_attribs[] = {
123             GLX_RGBA,
124             GLX_RED_SIZE, 1,
125             GLX_GREEN_SIZE, 1,
126             GLX_BLUE_SIZE, 1,
127             GLX_DOUBLEBUFFER,
128             GLX_DEPTH_SIZE, 1,
129             GLX_STENCIL_SIZE, 1,
130             None
131         };
132
133         XVisualInfo *visinfo;
134         
135         visinfo = glXChooseVisual(display, screen, doubleBuffer ? double_attribs : single_attribs);
136
137         return new GlxVisual(visinfo);
138     }
139     
140     Drawable *
141     createDrawable(const Visual *visual)
142     {
143         XVisualInfo *visinfo = dynamic_cast<const GlxVisual *>(visual)->visinfo;
144
145         Window root = RootWindow(display, screen);
146
147         /* window attributes */
148         XSetWindowAttributes attr;
149         attr.background_pixel = 0;
150         attr.border_pixel = 0;
151         attr.colormap = XCreateColormap(display, root, visinfo->visual, AllocNone);
152         attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
153         
154         unsigned long mask;
155         mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
156
157         int x = 0, y = 0, width = 256, height = 256;
158
159         Window window = XCreateWindow(
160             display, root, 
161             x, y, width, height,
162             0, 
163             visinfo->depth, 
164             InputOutput,
165             visinfo->visual, 
166             mask, 
167             &attr);
168
169         XSizeHints sizehints;
170         sizehints.x = x;
171         sizehints.y = y;
172         sizehints.width  = width;
173         sizehints.height = height;
174         sizehints.flags = USSize | USPosition;
175         XSetNormalHints(display, window, &sizehints);
176         
177         const char *name = "glretrace";
178         XSetStandardProperties(
179             display, window, name, name,
180             None, (char **)NULL, 0, &sizehints);
181
182         XMapWindow(display, window);
183         
184         return new GlxDrawable(visual, display, window);
185     }
186
187     Context *
188     createContext(const Visual *visual)
189     {
190         XVisualInfo *visinfo = dynamic_cast<const GlxVisual *>(visual)->visinfo;
191         GLXContext context = glXCreateContext(display, visinfo, NULL, True);
192         return new GlxContext(visual, display, context);
193     }
194
195     bool
196     makeCurrent(Drawable *drawable, Context *context)
197     {
198         if (!drawable || !context) {
199             return glXMakeCurrent(display, None, NULL);
200         } else {
201             GlxDrawable *glxDrawable = dynamic_cast<GlxDrawable *>(drawable);
202             GlxContext *glxContext = dynamic_cast<GlxContext *>(context);
203
204             return glXMakeCurrent(display, glxDrawable->window, glxContext->context);
205         }
206     }
207
208     bool
209     processEvents(void) {
210         while (XPending(display) > 0) {
211             XEvent event;
212             XNextEvent(display, &event);
213             // TODO
214         }
215         return true;
216     }
217 };
218
219
220 WindowSystem *createNativeWindowSystem(void) {
221     return new GlxWindowSystem();
222 }
223
224
225 } /* namespace glretrace */