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>
12 #ifdef WITH_XF86VIDMODE
13 #include <X11/extensions/xf86vmode.h>
18 #include <msp/core/application.h>
19 #include <msp/core/except.h>
22 #include "display_priv.h"
29 LRESULT CALLBACK wndproc_(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
33 CREATESTRUCT *cs=reinterpret_cast<CREATESTRUCT *>(lparam);
34 SetWindowLong(hwnd, 0, reinterpret_cast<LONG>(cs->lpCreateParams));
38 Msp::Graphics::Window *wnd=reinterpret_cast<Msp::Graphics::Window *>(GetWindowLong(hwnd, 0));
39 Msp::Graphics::Window::Event ev;
43 if(wnd && wnd->event(ev))
47 return DefWindowProc(hwnd, msg, wparam, lparam);
50 Bool match_event_type(Display *, XEvent *event, XPointer arg)
52 return event->type==reinterpret_cast<int>(arg);
61 WindowOptions::WindowOptions():
69 Window::Window(Display &dpy, unsigned w, unsigned h, bool fs):
74 options.fullscreen=fs;
79 Window::Window(Display &dpy, const WindowOptions &opts):
94 static bool wndclass_created=false;
100 wndcl.cbSize=sizeof(WNDCLASSEX);
102 wndcl.lpfnWndProc=&wndproc_;
104 wndcl.cbWndExtra=sizeof(Window *);
105 wndcl.hInstance=reinterpret_cast<HINSTANCE>(Application::get_data());
107 wndcl.hCursor=LoadCursor(0, IDC_ARROW);
108 wndcl.hbrBackground=0;
109 wndcl.lpszMenuName=0;
110 wndcl.lpszClassName="mspgbase";
113 if(!RegisterClassEx(&wndcl))
114 throw Exception("Couldn't register window class");
116 wndclass_created=true;
120 SetRect(&rect, 0, 0, options.width, options.height);
122 int style=(options.fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW);
123 if(!options.resizable)
124 style&=~WS_THICKFRAME;
125 int exstyle=(options.fullscreen ? WS_EX_APPWINDOW : WS_EX_OVERLAPPEDWINDOW);
126 AdjustWindowRectEx(&rect, style, false, exstyle);
128 priv->window=CreateWindowEx(exstyle,
132 CW_USEDEFAULT, CW_USEDEFAULT,
133 rect.right-rect.left, rect.bottom-rect.top,
136 reinterpret_cast<HINSTANCE>(Application::get_data()),
139 throw Exception("CreateWindowEx failed");
142 ::Display *dpy=display.get_private().display;
144 priv->wm_delete_window=XInternAtom(dpy, "WM_DELETE_WINDOW", true);
145 priv->invisible_cursor=0;
147 XSetWindowAttributes attr;
148 attr.override_redirect=options.fullscreen;
149 attr.event_mask=ButtonPressMask|ButtonReleaseMask|PointerMotionMask|KeyPressMask|KeyReleaseMask|StructureNotifyMask|EnterWindowMask;
151 priv->window=XCreateWindow(dpy,
152 DefaultRootWindow(dpy),
154 options.width, options.height,
159 CWOverrideRedirect|CWEventMask, &attr);
161 XSetWMProtocols(dpy, priv->window, &priv->wm_delete_window, 1);
163 if(!options.resizable)
166 hints.flags=PMinSize|PMaxSize;
167 hints.min_width=hints.max_width=options.width;
168 hints.min_height=hints.max_height=options.height;
169 XSetWMNormalHints(dpy, priv->window, &hints);
174 display.add_window(*this);
175 display.check_error();
182 CloseWindow(priv->window);
184 XDestroyWindow(display.get_private().display, priv->window);
186 if(priv->invisible_cursor)
187 XFreeCursor(display.get_private().display, priv->invisible_cursor);
190 display.remove_window(*this);
192 if(options.fullscreen)
193 display.restore_mode();
198 void Window::set_title(const string &title)
201 SetWindowText(priv->window, title.c_str());
203 vector<unsigned char> buf(title.begin(), title.end());
206 prop.encoding=XA_STRING;
208 prop.nitems=title.size();
209 XSetWMName(display.get_private().display, priv->window, &prop);
210 display.check_error();
214 void Window::reconfigure(const WindowOptions &opts)
216 bool fullscreen_changed=(opts.fullscreen!=options.fullscreen);
217 resizing=(opts.width!=options.width || opts.height!=options.height);
223 SetRect(&rect, 0, 0, options.width, options.height);
225 int style=(options.fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW);
226 if(!options.resizable)
227 style&=~WS_THICKFRAME;
228 int exstyle=(options.fullscreen ? WS_EX_APPWINDOW : WS_EX_OVERLAPPEDWINDOW);
229 AdjustWindowRectEx(&rect, style, false, exstyle);
231 if(fullscreen_changed)
234 SetWindowLong(priv->window, GWL_EXSTYLE, exstyle);
235 SetWindowLong(priv->window, GWL_STYLE, style);
239 if(options.fullscreen)
240 SetWindowPos(priv->window, 0, 0, 0, rect.right-rect.left, rect.bottom-rect.top, SWP_NOZORDER);
242 SetWindowPos(priv->window, 0, 0, 0, rect.right-rect.left, rect.bottom-rect.top, SWP_NOMOVE|SWP_NOZORDER);
244 ::Display *dpy=display.get_private().display;
246 bool was_visible=visible;
247 if(fullscreen_changed)
253 // Wait for the window to be unmapped. This makes window managers happy.
255 XPeekIfEvent(dpy, &ev, match_event_type, reinterpret_cast<XPointer>(UnmapNotify));
258 XSetWindowAttributes attr;
259 attr.override_redirect=options.fullscreen;
260 XChangeWindowAttributes(dpy, priv->window, CWOverrideRedirect, &attr);
264 if(options.resizable)
268 hints.flags=PMinSize|PMaxSize;
269 hints.min_width=hints.max_width=options.width;
270 hints.min_height=hints.max_height=options.height;
272 XSetWMNormalHints(dpy, priv->window, &hints);
274 if(options.fullscreen)
275 XMoveResizeWindow(dpy, priv->window, 0, 0, options.width, options.height);
277 XResizeWindow(dpy, priv->window, options.width, options.height);
279 if(fullscreen_changed)
288 if(options.fullscreen)
289 display.set_mode(VideoMode(options.width, options.height));
290 else if(fullscreen_changed)
291 display.restore_mode();
295 void Window::set_keyboard_autorepeat(bool r)
300 void Window::show_cursor(bool s)
305 ::Display *dpy=display.get_private().display;
308 XUndefineCursor(dpy, priv->window);
311 if(!priv->invisible_cursor)
313 int screen=DefaultScreen(dpy);
315 Pixmap pm=XCreatePixmap(dpy, priv->window, 1, 1, 1);
316 GC gc=XCreateGC(dpy, pm, 0, 0);
317 XSetFunction(dpy, gc, GXclear);
318 XDrawPoint(dpy, pm, gc, 0, 0);
322 black.pixel=BlackPixel(dpy, screen);
323 XQueryColor(dpy, DefaultColormap(dpy, screen), &black);
325 priv->invisible_cursor=XCreatePixmapCursor(dpy, pm, pm, &black, &black, 0, 0);
327 XFreePixmap(dpy, pm);
329 XDefineCursor(dpy, priv->window, priv->invisible_cursor);
334 void Window::warp_pointer(int x, int y)
337 XWarpPointer(display.get_private().display, None, priv->window, 0, 0, 0, 0, x, y);
347 ShowWindow(priv->window, SW_SHOWNORMAL);
349 XMapRaised(display.get_private().display, priv->window);
353 if(options.fullscreen)
355 display.set_mode(VideoMode(options.width, options.height));
357 XWarpPointer(display.get_private().display, None, priv->window, 0, 0, 0, 0, options.width/2, options.height/2);
365 ShowWindow(priv->window, SW_HIDE);
367 XUnmapWindow(display.get_private().display, priv->window);
371 if(options.fullscreen)
372 display.restore_mode();
375 bool Window::event(const Event &evnt)
378 WPARAM wp=evnt.wparam;
379 LPARAM lp=evnt.lparam;
383 signal_key_press.emit(wp, 0, wp);
386 signal_key_release.emit(wp, 0);
389 signal_button_press.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 1, 0);
392 signal_button_release.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 1, 0);
395 signal_button_press.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 2, 0);
398 signal_button_release.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 2, 0);
401 signal_button_press.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 3, 0);
404 signal_button_release.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 3, 0);
408 unsigned btn = (HIWORD(wp)&0x8000) ? 5 : 4;
409 signal_button_press.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), btn, 0);
410 signal_button_release.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), btn, 0);
414 signal_pointer_motion.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp));
417 options.width=LOWORD(lp);
418 options.height=HIWORD(lp);
419 signal_resize.emit(options.width, options.height);
428 const XEvent &ev=evnt.xevent;
432 signal_button_press.emit(ev.xbutton.x, ev.xbutton.y, ev.xbutton.button, ev.xbutton.state);
435 signal_button_release.emit(ev.xbutton.x, ev.xbutton.y, ev.xbutton.button, ev.xbutton.state);
438 signal_pointer_motion.emit(ev.xmotion.x, ev.xmotion.y);
443 XLookupString(const_cast<XKeyEvent *>(&ev.xkey), buf, sizeof(buf), 0, 0);
444 // XXX Handle the result according to locale
445 signal_key_press.emit(XKeycodeToKeysym(display.get_private().display, ev.xkey.keycode, 0), ev.xkey.state, buf[0]);
449 signal_key_release.emit(XKeycodeToKeysym(display.get_private().display, ev.xkey.keycode, 0), ev.xkey.state);
451 case ConfigureNotify:
452 if((ev.xconfigure.width==static_cast<int>(options.width) && ev.xconfigure.height==static_cast<int>(options.height)) == resizing)
454 options.width=ev.xconfigure.width;
455 options.height=ev.xconfigure.height;
457 signal_resize.emit(options.width, options.height);
459 if(options.fullscreen)
461 ::Display *dpy=display.get_private().display;
462 int screen=DefaultScreen(dpy);
463 XF86VidModeSetViewPort(dpy, screen, ev.xconfigure.x, ev.xconfigure.y);
467 if(ev.xclient.data.l[0]==static_cast<long>(priv->wm_delete_window))
471 if(options.fullscreen)
472 XSetInputFocus(display.get_private().display, priv->window, RevertToParent, CurrentTime);
475 if(options.fullscreen)
476 XGrabPointer(display.get_private().display, priv->window, true, None, GrabModeAsync, GrabModeAsync, priv->window, None, CurrentTime);
485 } // namespace Graphics