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();
101 void Window::set_title(const string &title)
104 SetWindowText(priv->window, title.c_str());
106 vector<unsigned char> buf(title.begin(), title.end());
109 prop.encoding=XA_STRING;
111 prop.nitems=title.size();
112 XSetWMName(display.get_private().display, priv->window, &prop);
113 display.check_error();
117 void Window::reconfigure(const WindowOptions &opts)
119 bool fullscreen_changed=(opts.fullscreen!=options.fullscreen);
125 SetRect(&rect, 0, 0, options.width, options.height);
127 int style=(options.fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW);
128 if(!options.resizable)
129 style&=~WS_THICKFRAME;
130 int exstyle=(options.fullscreen ? WS_EX_APPWINDOW : WS_EX_OVERLAPPEDWINDOW);
131 AdjustWindowRectEx(&rect, style, false, exstyle);
133 if(fullscreen_changed)
136 SetWindowLong(priv->window, GWL_EXSTYLE, exstyle);
137 SetWindowLong(priv->window, GWL_STYLE, style);
141 if(options.fullscreen)
142 SetWindowPos(priv->window, 0, 0, 0, rect.right-rect.left, rect.bottom-rect.top, SWP_NOZORDER);
144 SetWindowPos(priv->window, 0, 0, 0, rect.right-rect.left, rect.bottom-rect.top, SWP_NOMOVE|SWP_NOZORDER);
146 ::Display *dpy=display.get_private().display;
148 if(fullscreen_changed)
152 // Wait for the window to be unmapped. This makes window managers happy.
154 XPeekIfEvent(dpy, &ev, match_event_type, reinterpret_cast<XPointer>(UnmapNotify));
156 XSetWindowAttributes attr;
157 attr.override_redirect=options.fullscreen;
158 XChangeWindowAttributes(dpy, priv->window, CWOverrideRedirect, &attr);
163 if(options.resizable)
167 hints.flags=PMinSize|PMaxSize;
168 hints.min_width=hints.max_width=options.width;
169 hints.min_height=hints.max_height=options.height;
171 XSetWMNormalHints(dpy, priv->window, &hints);
173 if(options.fullscreen)
174 XMoveResizeWindow(dpy, priv->window, 0, 0, options.width, options.height);
176 XResizeWindow(dpy, priv->window, options.width, options.height);
179 if(options.fullscreen)
180 display.set_mode(VideoMode(options.width, options.height));
181 else if(fullscreen_changed)
182 display.restore_mode();
185 void Window::show_cursor(bool s)
190 ::Display *dpy=display.get_private().display;
193 XUndefineCursor(dpy, priv->window);
196 if(!priv->invisible_cursor)
198 int screen=DefaultScreen(dpy);
200 Pixmap pm=XCreatePixmap(dpy, priv->window, 1, 1, 1);
201 GC gc=XCreateGC(dpy, pm, 0, 0);
202 XSetFunction(dpy, gc, GXclear);
203 XDrawPoint(dpy, pm, gc, 0, 0);
207 black.pixel=BlackPixel(dpy, screen);
208 XQueryColor(dpy, DefaultColormap(dpy, screen), &black);
210 priv->invisible_cursor=XCreatePixmapCursor(dpy, pm, pm, &black, &black, 0, 0);
212 XFreePixmap(dpy, pm);
214 XDefineCursor(dpy, priv->window, priv->invisible_cursor);
222 ShowWindow(priv->window, SW_SHOWNORMAL);
224 XMapRaised(display.get_private().display, priv->window);
231 ShowWindow(priv->window, SW_HIDE);
233 XUnmapWindow(display.get_private().display, priv->window);
242 static bool wndclass_created=false;
244 if(!wndclass_created)
248 wndcl.cbSize=sizeof(WNDCLASSEX);
250 wndcl.lpfnWndProc=&wndproc_;
252 wndcl.cbWndExtra=sizeof(Window *);
253 wndcl.hInstance=reinterpret_cast<HINSTANCE>(Application::get_data());
255 wndcl.hCursor=LoadCursor(0, IDC_ARROW);
256 wndcl.hbrBackground=0;
257 wndcl.lpszMenuName=0;
258 wndcl.lpszClassName="mspgbase";
261 if(!RegisterClassEx(&wndcl))
262 throw Exception("Couldn't register window class");
264 wndclass_created=true;
268 SetRect(&rect, 0, 0, options.width, options.height);
270 int style=(options.fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW);
271 if(!options.resizable)
272 style&=~WS_THICKFRAME;
273 int exstyle=(options.fullscreen ? WS_EX_APPWINDOW : WS_EX_OVERLAPPEDWINDOW);
274 AdjustWindowRectEx(&rect, style, false, exstyle);
276 priv->window=CreateWindowEx(exstyle,
280 CW_USEDEFAULT, CW_USEDEFAULT,
281 rect.right-rect.left, rect.bottom-rect.top,
284 reinterpret_cast<HINSTANCE>(Application::get_data()),
287 throw Exception("CreateWindowEx failed");
289 if(options.fullscreen)
290 display.set_mode(VideoMode(options.width, options.height));
293 ::Display *dpy=display.get_private().display;
295 priv->wm_delete_window=XInternAtom(dpy, "WM_DELETE_WINDOW", true);
296 priv->invisible_cursor=0;
298 XSetWindowAttributes attr;
299 attr.override_redirect=options.fullscreen;
300 attr.event_mask=ButtonPressMask|ButtonReleaseMask|PointerMotionMask|KeyPressMask|KeyReleaseMask|StructureNotifyMask|EnterWindowMask;
302 priv->window=XCreateWindow(dpy,
303 DefaultRootWindow(dpy),
305 options.width, options.height,
310 CWOverrideRedirect|CWEventMask, &attr);
312 XSetWMProtocols(dpy, priv->window, &priv->wm_delete_window, 1);
314 if(!options.resizable)
317 hints.flags=PMinSize|PMaxSize;
318 hints.min_width=hints.max_width=options.width;
319 hints.min_height=hints.max_height=options.height;
320 XSetWMNormalHints(dpy, priv->window, &hints);
323 if(options.fullscreen)
325 display.set_mode(VideoMode(options.width, options.height));
326 XWarpPointer(dpy, None, priv->window, 0, 0, 0, 0, options.width/2, options.height/2);
330 display.add_window(*this);
331 display.check_error();
334 bool Window::event(const Event &evnt)
337 WPARAM wp=evnt.wparam;
338 LPARAM lp=evnt.lparam;
342 signal_key_press.emit(wp, 0, wp);
345 signal_key_release.emit(wp, 0);
348 signal_button_press.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 1, 0);
351 signal_button_release.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 1, 0);
354 signal_button_press.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 2, 0);
357 signal_button_release.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 2, 0);
360 signal_button_press.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 3, 0);
363 signal_button_release.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 3, 0);
366 signal_pointer_motion.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp));
369 options.width=LOWORD(lp);
370 options.height=HIWORD(lp);
371 signal_resize.emit(options.width, options.height);
380 const XEvent &ev=evnt.xevent;
384 signal_button_press.emit(ev.xbutton.x, ev.xbutton.y, ev.xbutton.button, ev.xbutton.state);
387 signal_button_release.emit(ev.xbutton.x, ev.xbutton.y, ev.xbutton.button, ev.xbutton.state);
390 signal_pointer_motion.emit(ev.xmotion.x, ev.xmotion.y);
395 XLookupString(const_cast<XKeyEvent *>(&ev.xkey), buf, sizeof(buf), 0, 0);
396 // XXX Handle the result according to locale
397 signal_key_press.emit(XKeycodeToKeysym(display.get_private().display, ev.xkey.keycode, 0), ev.xkey.state, buf[0]);
401 signal_key_release.emit(XKeycodeToKeysym(display.get_private().display, ev.xkey.keycode, 0), ev.xkey.state);
403 case ConfigureNotify:
404 options.width=ev.xconfigure.width;
405 options.height=ev.xconfigure.height;
406 signal_resize.emit(options.width, options.height);
409 if(ev.xclient.data.l[0]==static_cast<long>(priv->wm_delete_window))
413 if(options.fullscreen)
414 XSetInputFocus(display.get_private().display, priv->window, RevertToParent, CurrentTime);
417 if(options.fullscreen)
418 XGrabPointer(display.get_private().display, priv->window, true, None, GrabModeAsync, GrabModeAsync, priv->window, None, CurrentTime);
427 } // namespace Graphics