Debian Codegraph @

cvd_src/glwindow.cc

raw symbols SCIP
1 #include <cvd/glwindow.h>
2 #include <exception>
3
4 #include <GL/glx.h>
5 #include <X11/Xlib.h>
6 #include <X11/keysym.h>
7
8 using namespace CVD;
9
10 CVD::Exceptions::GLWindow::CreationError::CreationError(std::string w)
11 : CVD::Exceptions::GLWindow::All { "GLWindow creation error: " + w }
12 {
13 }
14
15 CVD::Exceptions::GLWindow::RuntimeError::RuntimeError(std::string w)
16 : CVD::Exceptions::GLWindow::All { "GLWindow error: " + w }
17 {
18 }
19
20 struct GLWindow::State
21 {
22 ImageRef size;
23 ImageRef position;
24 std::string title;
25 Display* display;
26 Window window;
27 Atom delete_atom;
28 Cursor null_cursor;
29 GLXContext context;
30 };
31
32 void CVD::GLWindow::init(const ImageRef& size, int bpp, const std::string& title, const std::string& disp)
33 {
34 Display* display = XOpenDisplay(disp == "" ? NULL : const_cast<char*>(disp.c_str()));
35 if(display == 0)
36 throw Exceptions::GLWindow::CreationError("Cannot open X display");
37
38 int visualAttributes[] = {
39 GLX_RGBA,
40 GLX_DOUBLEBUFFER,
41 GLX_RED_SIZE, bpp / 3,
42 GLX_GREEN_SIZE, bpp / 3,
43 GLX_BLUE_SIZE, bpp / 3,
44 GLX_DEPTH_SIZE, 8,
45 GLX_STENCIL_SIZE, 8,
46 None
47 };
48 XVisualInfo* visualInfo = glXChooseVisual(display, DefaultScreen(display), visualAttributes);
49 if(visualInfo == 0)
50 {
51 XCloseDisplay(display);
52 throw Exceptions::GLWindow::CreationError("glXChooseVisual failed");
53 }
54
55 Window rootWindow = RootWindow(display, visualInfo->screen);
56 XWindowAttributes windowAttributes;
57
58 XGetWindowAttributes(display, rootWindow, &windowAttributes);
59
60 XSetWindowAttributes attributes;
61 attributes.border_pixel = 0;
62 attributes.colormap = XCreateColormap(display, rootWindow, visualInfo->visual, AllocNone);
63 attributes.event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask | ExposureMask;
64
65 Window window = XCreateWindow(display,
66 rootWindow,
67 0, 0, size.x, size.y,
68 0, visualInfo->depth,
69 InputOutput,
70 visualInfo->visual,
71 CWBorderPixel | CWColormap | CWEventMask,
72 &attributes);
73 XStoreName(display, window, title.c_str());
74 XClassHint classHint;
75 char res_name[] = "cvd";
76 classHint.res_class = res_name;
77 classHint.res_name = (char*)title.c_str();
78 XSetClassHint(display, window, &classHint);
79 XMapWindow(display, window);
80 XEvent ev;
81 do
82 {
83 XNextEvent(display, &ev);
84 } while(ev.type != MapNotify);
85
86 Atom delete_atom = XInternAtom(display, "WM_DELETE_WINDOW", True);
87 XSetWMProtocols(display, window, &delete_atom, 1);
88
89 GLXContext context = glXCreateContext(display, visualInfo, 0, True);
90 if(context == 0)
91 {
92 XDestroyWindow(display, window);
93 XCloseDisplay(display);
94 throw Exceptions::GLWindow::CreationError("glXCreateContext failed");
95 }
96
97 if(glXMakeCurrent(display, window, context) == False)
98 {
99 glXDestroyContext(display, context);
100 XDestroyWindow(display, window);
101 XCloseDisplay(display);
102 throw Exceptions::GLWindow::CreationError("glXMakeCurrent failed");
103 }
104 glLoadIdentity();
105 glViewport(0, 0, size.x, size.y);
106 glMatrixMode(GL_PROJECTION);
107 glLoadIdentity();
108 glColor3f(1.0f, 1.0f, 1.0f);
109 glRasterPos2f(-1, 1);
110 glOrtho(-0.375, size.x - 0.375, size.y - 0.375, -0.375, -1, 1); //offsets to make (0,0) the top left pixel (rather than off the display)
111 glPixelZoom(1, -1);
112
113 XColor black = { 0, 0, 0, 0, 0, 0 };
114 XFontStruct* fixed = XLoadQueryFont(display, "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-1");
115 Cursor null_cursor = XCreateGlyphCursor(display, fixed->fid, fixed->fid, ' ', ' ', &black, &black);
116 XFreeFont(display, fixed);
117
118 state = new State();
119 state->size = size;
120 state->title = title;
121 state->display = display;
122 state->window = window;
123 state->delete_atom = delete_atom;
124 state->null_cursor = null_cursor;
125 state->context = context;
126 }
127
128 CVD::GLWindow::~GLWindow()
129 {
130 glXMakeCurrent(state->display, None, 0);
131 glXDestroyContext(state->display, state->context);
132
133 XUnmapWindow(state->display, state->window);
134 XDestroyWindow(state->display, state->window);
135 XCloseDisplay(state->display);
136 delete state;
137 }
138
139 ImageRef CVD::GLWindow::size() const { return state->size; }
140
141 void CVD::GLWindow::set_size(const ImageRef& s_)
142 {
143 // we don't set state->size here, so that it changes through the event system
144 // and we react to it there
145 XResizeWindow(state->display, state->window, s_.x, s_.y);
146 }
147
148 ImageRef CVD::GLWindow::position() const { return state->position; }
149
150 void CVD::GLWindow::set_position(const ImageRef& p_)
151 {
152 state->position = p_;
153 XMoveWindow(state->display, state->window, p_.x, p_.y);
154 XFlush(state->display);
155 }
156
157 void CVD::GLWindow::set_cursor_position(const ImageRef& where)
158 {
159 XWarpPointer(state->display, None, state->window, 0, 0, 0, 0, where.x, where.y);
160 }
161
162 ImageRef CVD::GLWindow::cursor_position() const
163 {
164 Window wtmp;
165 int itmp;
166 unsigned int utmp;
167 ImageRef where;
168 XQueryPointer(state->display, state->window, &wtmp, &wtmp, &itmp, &itmp, &where.x, &where.y, &utmp);
169 return where;
170 }
171
172 void CVD::GLWindow::show_cursor(bool show)
173 {
174 if(show)
175 XUndefineCursor(state->display, state->window);
176 else
177 XDefineCursor(state->display, state->window, state->null_cursor);
178 }
179
180 std::string CVD::GLWindow::title() const
181 {
182 return state->title;
183 }
184
185 void CVD::GLWindow::set_title(const std::string& title)
186 {
187 state->title = title;
188 XStoreName(state->display, state->window, title.c_str());
189 }
190
191 void CVD::GLWindow::swap_buffers()
192 {
193 glXSwapBuffers(state->display, state->window);
194 }
195
196 inline int convertButton(unsigned int button)
197 {
198 switch(button)
199 {
200 case Button1:
201 return GLWindow::BUTTON_LEFT;
202 case Button2:
203 return GLWindow::BUTTON_MIDDLE;
204 case Button3:
205 return GLWindow::BUTTON_RIGHT;
206 case Button4:
207 return GLWindow::BUTTON_WHEEL_UP;
208 case Button5:
209 return GLWindow::BUTTON_WHEEL_DOWN;
210 }
211 return 0;
212 }
213
214 inline int convertButtonState(unsigned int state)
215 {
216 int ret = 0;
217 if(state & Button1Mask)
218 ret |= GLWindow::BUTTON_LEFT;
219 if(state & Button2Mask)
220 ret |= GLWindow::BUTTON_MIDDLE;
221 if(state & Button3Mask)
222 ret |= GLWindow::BUTTON_RIGHT;
223 if(state & ControlMask)
224 ret |= GLWindow::BUTTON_MOD_CTRL;
225 if(state & ShiftMask)
226 ret |= GLWindow::BUTTON_MOD_SHIFT;
227 return ret;
228 }
229
230 void CVD::GLWindow::handle_events(EventHandler& handler)
231 {
232 XEvent event;
233 KeySym k;
234 while(XPending(state->display))
235 {
236 XNextEvent(state->display, &event);
237 switch(event.type)
238 {
239 case ButtonPress:
240 handler.on_mouse_down(*this, ImageRef(event.xbutton.x, event.xbutton.y),
241 convertButtonState(event.xbutton.state), convertButton(event.xbutton.button));
242 break;
243 case ButtonRelease:
244 handler.on_mouse_up(*this, ImageRef(event.xbutton.x, event.xbutton.y),
245 convertButtonState(event.xbutton.state), convertButton(event.xbutton.button));
246 break;
247 case MotionNotify:
248 handler.on_mouse_move(*this, ImageRef(event.xmotion.x, event.xmotion.y), convertButtonState(event.xbutton.state));
249 break;
250 case KeyPress:
251 {
252 XLookupString(&event.xkey, 0, 0, &k, 0);
253 handler.on_key_down(*this, static_cast<unsigned int>(k)); //KeySyms are 29 bits only
254 break;
255 }
256 case KeyRelease:
257 XLookupString(&event.xkey, 0, 0, &k, 0);
258 handler.on_key_up(*this, static_cast<unsigned int>(k));
259 break;
260 //case UnmapNotify: active = 0; break;
261 //case MapNotify: active = 1; break;
262 case ConfigureNotify:
263 if(event.xconfigure.width != state->size.x || event.xconfigure.height != state->size.y)
264 {
265 activate();
266 state->size = ImageRef(event.xconfigure.width, event.xconfigure.height);
267 glViewport(0, 0, state->size.x, state->size.y);
268 //glRasterPos2f(0,0);
269 //glPixelZoom(float(event.xconfigure.width)/myWidth,-float(event.xconfigure.height)/myHeight);
270 handler.on_resize(*this, state->size);
271 }
272 break;
273 case Expose:
274 handler.on_event(*this, EVENT_EXPOSE);
275 break;
276 case ClientMessage:
277 if(event.xclient.data.l[0] == (int)state->delete_atom)
278 handler.on_event(*this, EVENT_CLOSE);
279 else
280 handler.on_event(*this, static_cast<unsigned int>(event.xclient.message_type));
281 break;
282 default:
283 handler.on_event(*this, event.type);
284 break;
285 }
286 }
287 }
288
289 class SaveEvents : public GLWindow::EventHandler
290 {
291 private:
292 std::vector<GLWindow::Event>& events;
293
294 public:
295 SaveEvents(std::vector<GLWindow::Event>& events_)
296 : events(events_)
297 {
298 }
299 void on_key_down(GLWindow&, unsigned int key) override
300 {
301 GLWindow::Event e;
302 e.type = GLWindow::Event::KEY_DOWN;
303 e.which = key;
304 events.push_back(e);
305 }
306 void on_key_up(GLWindow&, unsigned int key) override
307 {
308 GLWindow::Event e;
309 e.type = GLWindow::Event::KEY_UP;
310 e.which = key;
311 events.push_back(e);
312 }
313
314 void on_mouse_move(GLWindow&, ImageRef where, unsigned int state) override
315 {
316 GLWindow::Event e;
317 e.type = GLWindow::Event::MOUSE_MOVE;
318 e.state = state;
319 e.where = where;
320 events.push_back(e);
321 }
322
323 void on_mouse_down(GLWindow&, ImageRef where, unsigned int state, unsigned int button) override
324 {
325 GLWindow::Event e;
326 e.type = GLWindow::Event::MOUSE_DOWN;
327 e.state = state;
328 e.which = button;
329 e.where = where;
330 events.push_back(e);
331 }
332
333 void on_mouse_up(GLWindow&, ImageRef where, unsigned int state, unsigned int button) override
334 {
335 GLWindow::Event e;
336 e.type = GLWindow::Event::MOUSE_UP;
337 e.state = state;
338 e.which = button;
339 e.where = where;
340 events.push_back(e);
341 }
342
343 void on_resize(GLWindow&, ImageRef size) override
344 {
345 GLWindow::Event e;
346 e.type = GLWindow::Event::RESIZE;
347 e.size = size;
348 events.push_back(e);
349 }
350
351 void on_event(GLWindow&, unsigned int event) override
352 {
353 GLWindow::Event e;
354 e.type = GLWindow::Event::EVENT;
355 e.which = event;
356 events.push_back(e);
357 }
358 };
359
360 void CVD::GLWindow::get_events(std::vector<Event>& events)
361 {
362 SaveEvents saver(events);
363 handle_events(saver);
364 }
365
366 bool CVD::GLWindow::EventSummary::should_quit() const
367 {
368 return key_down.count(XK_Escape) || events.count(GLWindow::EVENT_CLOSE);
369 }
370
371 class MakeSummary : public GLWindow::EventHandler
372 {
373 private:
374 GLWindow::EventSummary& summary;
375
376 public:
377 MakeSummary(GLWindow::EventSummary& summary_)
378 : summary(summary_)
379 {
380 }
381
382 void on_key_down(GLWindow&, unsigned int key) override { ++summary.key_down[key]; }
383 void on_key_up(GLWindow&, unsigned int key) override { ++summary.key_up[key]; }
384 void on_mouse_move(GLWindow&, ImageRef where, unsigned int) override
385 {
386 summary.cursor = where;
387 summary.cursor_moved = true;
388 }
389 void on_mouse_down(GLWindow&, ImageRef where, unsigned int state, unsigned int button) override { summary.mouse_down[button] = std::make_pair(where, state); }
390 void on_mouse_up(GLWindow&, ImageRef where, unsigned int state, unsigned int button) override { summary.mouse_up[button] = std::make_pair(where, state); }
391 void on_event(GLWindow&, unsigned int event) override { ++summary.events[event]; }
392 };
393
394 void GLWindow::get_events(EventSummary& summary)
395 {
396 summary.cursor = cursor_position();
397 MakeSummary ms(summary);
398 handle_events(ms);
399 }
400
401 bool CVD::GLWindow::has_events() const
402 {
403 return XPending(state->display);
404 }
405
406 void CVD::GLWindow::activate()
407 {
408 if(glXMakeCurrent(state->display, state->window, state->context) == False)
409 throw Exceptions::GLWindow::RuntimeError("glXMakeCurrent failed");
410 }