3 This file is part of libmspgbase
4 Copyright © 2007-2008 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
10 #include <X11/Xatom.h>
11 #include <X11/Xutil.h>
15 #include <msp/core/application.h>
16 #include <msp/core/except.h>
19 #include "display_priv.h"
26 LRESULT CALLBACK wndproc_(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
30 CREATESTRUCT *cs=reinterpret_cast<CREATESTRUCT *>(lparam);
31 SetWindowLong(hwnd, 0, reinterpret_cast<LONG>(cs->lpCreateParams));
35 Msp::Graphics::Window *wnd=reinterpret_cast<Msp::Graphics::Window *>(GetWindowLong(hwnd, 0));
36 Msp::Graphics::Window::Event ev;
40 if(wnd && wnd->event(ev))
44 return DefWindowProc(hwnd, msg, wparam, lparam);
47 Bool match_event_type(Display *, XEvent *event, XPointer arg)
49 return event->type==reinterpret_cast<int>(arg);
58 WindowOptions::WindowOptions():
66 Window::Window(Display &dpy, unsigned w, unsigned h, bool fs):
71 options.fullscreen=fs;
76 Window::Window(Display &dpy, const WindowOptions &opts):
87 CloseWindow(priv->window);
89 XDestroyWindow(display.get_private().display, priv->window);
91 if(priv->invisible_cursor)
92 XFreeCursor(display.get_private().display, priv->invisible_cursor);
95 display.remove_window(*this);
97 if(options.fullscreen)
98 display.restore_mode();
103 void Window::set_title(const string &title)
106 SetWindowText(priv->window, title.c_str());
108 vector<unsigned char> buf(title.begin(), title.end());
111 prop.encoding=XA_STRING;
113 prop.nitems=title.size();
114 XSetWMName(display.get_private().display, priv->window, &prop);
115 display.check_error();
119 void Window::reconfigure(const WindowOptions &opts)
121 bool fullscreen_changed=(opts.fullscreen!=options.fullscreen);
127 SetRect(&rect, 0, 0, options.width, options.height);
129 int style=(options.fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW);
130 if(!options.resizable)
131 style&=~WS_THICKFRAME;
132 int exstyle=(options.fullscreen ? WS_EX_APPWINDOW : WS_EX_OVERLAPPEDWINDOW);
133 AdjustWindowRectEx(&rect, style, false, exstyle);
135 if(fullscreen_changed)
138 SetWindowLong(priv->window, GWL_EXSTYLE, exstyle);
139 SetWindowLong(priv->window, GWL_STYLE, style);
143 if(options.fullscreen)
144 SetWindowPos(priv->window, 0, 0, 0, rect.right-rect.left, rect.bottom-rect.top, SWP_NOZORDER);
146 SetWindowPos(priv->window, 0, 0, 0, rect.right-rect.left, rect.bottom-rect.top, SWP_NOMOVE|SWP_NOZORDER);
148 ::Display *dpy=display.get_private().display;
150 if(fullscreen_changed)
154 // Wait for the window to be unmapped. This makes window managers happy.
156 XPeekIfEvent(dpy, &ev, match_event_type, reinterpret_cast<XPointer>(UnmapNotify));
158 XSetWindowAttributes attr;
159 attr.override_redirect=options.fullscreen;
160 XChangeWindowAttributes(dpy, priv->window, CWOverrideRedirect, &attr);
165 if(options.resizable)
169 hints.flags=PMinSize|PMaxSize;
170 hints.min_width=hints.max_width=options.width;
171 hints.min_height=hints.max_height=options.height;
173 XSetWMNormalHints(dpy, priv->window, &hints);
175 if(options.fullscreen)
176 XMoveResizeWindow(dpy, priv->window, 0, 0, options.width, options.height);
178 XResizeWindow(dpy, priv->window, options.width, options.height);
181 if(options.fullscreen)
182 display.set_mode(VideoMode(options.width, options.height));
183 else if(fullscreen_changed)
184 display.restore_mode();
187 void Window::set_keyboard_autorepeat(bool r)
192 void Window::show_cursor(bool s)
197 ::Display *dpy=display.get_private().display;
200 XUndefineCursor(dpy, priv->window);
203 if(!priv->invisible_cursor)
205 int screen=DefaultScreen(dpy);
207 Pixmap pm=XCreatePixmap(dpy, priv->window, 1, 1, 1);
208 GC gc=XCreateGC(dpy, pm, 0, 0);
209 XSetFunction(dpy, gc, GXclear);
210 XDrawPoint(dpy, pm, gc, 0, 0);
214 black.pixel=BlackPixel(dpy, screen);
215 XQueryColor(dpy, DefaultColormap(dpy, screen), &black);
217 priv->invisible_cursor=XCreatePixmapCursor(dpy, pm, pm, &black, &black, 0, 0);
219 XFreePixmap(dpy, pm);
221 XDefineCursor(dpy, priv->window, priv->invisible_cursor);
226 void Window::warp_pointer(int x, int y)
229 XWarpPointer(display.get_private().display, None, priv->window, 0, 0, 0, 0, x, y);
236 ShowWindow(priv->window, SW_SHOWNORMAL);
238 XMapRaised(display.get_private().display, priv->window);
245 ShowWindow(priv->window, SW_HIDE);
247 XUnmapWindow(display.get_private().display, priv->window);
257 static bool wndclass_created=false;
259 if(!wndclass_created)
263 wndcl.cbSize=sizeof(WNDCLASSEX);
265 wndcl.lpfnWndProc=&wndproc_;
267 wndcl.cbWndExtra=sizeof(Window *);
268 wndcl.hInstance=reinterpret_cast<HINSTANCE>(Application::get_data());
270 wndcl.hCursor=LoadCursor(0, IDC_ARROW);
271 wndcl.hbrBackground=0;
272 wndcl.lpszMenuName=0;
273 wndcl.lpszClassName="mspgbase";
276 if(!RegisterClassEx(&wndcl))
277 throw Exception("Couldn't register window class");
279 wndclass_created=true;
283 SetRect(&rect, 0, 0, options.width, options.height);
285 int style=(options.fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW);
286 if(!options.resizable)
287 style&=~WS_THICKFRAME;
288 int exstyle=(options.fullscreen ? WS_EX_APPWINDOW : WS_EX_OVERLAPPEDWINDOW);
289 AdjustWindowRectEx(&rect, style, false, exstyle);
291 priv->window=CreateWindowEx(exstyle,
295 CW_USEDEFAULT, CW_USEDEFAULT,
296 rect.right-rect.left, rect.bottom-rect.top,
299 reinterpret_cast<HINSTANCE>(Application::get_data()),
302 throw Exception("CreateWindowEx failed");
304 if(options.fullscreen)
305 display.set_mode(VideoMode(options.width, options.height));
308 ::Display *dpy=display.get_private().display;
310 priv->wm_delete_window=XInternAtom(dpy, "WM_DELETE_WINDOW", true);
311 priv->invisible_cursor=0;
313 XSetWindowAttributes attr;
314 attr.override_redirect=options.fullscreen;
315 attr.event_mask=ButtonPressMask|ButtonReleaseMask|PointerMotionMask|KeyPressMask|KeyReleaseMask|StructureNotifyMask|EnterWindowMask;
317 priv->window=XCreateWindow(dpy,
318 DefaultRootWindow(dpy),
320 options.width, options.height,
325 CWOverrideRedirect|CWEventMask, &attr);
327 XSetWMProtocols(dpy, priv->window, &priv->wm_delete_window, 1);
329 if(!options.resizable)
332 hints.flags=PMinSize|PMaxSize;
333 hints.min_width=hints.max_width=options.width;
334 hints.min_height=hints.max_height=options.height;
335 XSetWMNormalHints(dpy, priv->window, &hints);
338 if(options.fullscreen)
340 display.set_mode(VideoMode(options.width, options.height));
341 XWarpPointer(dpy, None, priv->window, 0, 0, 0, 0, options.width/2, options.height/2);
345 display.add_window(*this);
346 display.check_error();
349 bool Window::event(const Event &evnt)
352 WPARAM wp=evnt.wparam;
353 LPARAM lp=evnt.lparam;
357 signal_key_press.emit(wp, 0, wp);
360 signal_key_release.emit(wp, 0);
363 signal_button_press.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 1, 0);
366 signal_button_release.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 1, 0);
369 signal_button_press.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 2, 0);
372 signal_button_release.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 2, 0);
375 signal_button_press.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 3, 0);
378 signal_button_release.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 3, 0);
381 signal_pointer_motion.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp));
384 options.width=LOWORD(lp);
385 options.height=HIWORD(lp);
386 signal_resize.emit(options.width, options.height);
395 const XEvent &ev=evnt.xevent;
399 signal_button_press.emit(ev.xbutton.x, ev.xbutton.y, ev.xbutton.button, ev.xbutton.state);
402 signal_button_release.emit(ev.xbutton.x, ev.xbutton.y, ev.xbutton.button, ev.xbutton.state);
405 signal_pointer_motion.emit(ev.xmotion.x, ev.xmotion.y);
410 XLookupString(const_cast<XKeyEvent *>(&ev.xkey), buf, sizeof(buf), 0, 0);
411 // XXX Handle the result according to locale
412 signal_key_press.emit(XKeycodeToKeysym(display.get_private().display, ev.xkey.keycode, 0), ev.xkey.state, buf[0]);
416 signal_key_release.emit(XKeycodeToKeysym(display.get_private().display, ev.xkey.keycode, 0), ev.xkey.state);
418 case ConfigureNotify:
419 options.width=ev.xconfigure.width;
420 options.height=ev.xconfigure.height;
421 signal_resize.emit(options.width, options.height);
424 if(ev.xclient.data.l[0]==static_cast<long>(priv->wm_delete_window))
428 if(options.fullscreen)
429 XSetInputFocus(display.get_private().display, priv->window, RevertToParent, CurrentTime);
432 if(options.fullscreen)
433 XGrabPointer(display.get_private().display, priv->window, true, None, GrabModeAsync, GrabModeAsync, priv->window, None, CurrentTime);
442 } // namespace Graphics