4 #include <msp/core/systemerror.h>
5 #include "display_private.h"
7 #include "window_private.h"
13 Bool match_event_type(Display *, XEvent *event, XPointer arg)
15 return event->type==*reinterpret_cast<int *>(arg);
23 void Window::platform_init()
25 DisplayHandle dpy = display.get_private().display;
27 priv->wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", true);
28 priv->invisible_cursor = 0;
29 priv->reparented = false;
33 XSetWindowAttributes attr;
34 attr.override_redirect = options.fullscreen;
35 attr.event_mask = ButtonPressMask|ButtonReleaseMask|PointerMotionMask|KeyPressMask|KeyReleaseMask|StructureNotifyMask|EnterWindowMask|ExposureMask|FocusChangeMask;
37 priv->window = XCreateWindow(dpy,
38 display.get_private().root_window,
39 0, 0, // User position is set when the window is mapped
40 options.width, options.height,
45 CWOverrideRedirect|CWEventMask, &attr);
47 XSetWMProtocols(dpy, priv->window, &priv->wm_delete_window, 1);
49 if(!options.resizable)
52 hints.flags = PMinSize|PMaxSize;
53 hints.min_width=hints.max_width = options.width;
54 hints.min_height=hints.max_height = options.height;
55 XSetWMNormalHints(dpy, priv->window, &hints);
59 void Window::platform_cleanup()
62 XDestroyWindow(display.get_private().display, priv->window);
64 if(priv->invisible_cursor)
65 XFreeCursor(display.get_private().display, priv->invisible_cursor);
68 void Window::set_title(const string &title)
70 vector<unsigned char> buf(title.begin(), title.end());
73 prop.encoding = XA_STRING;
75 prop.nitems = title.size();
76 XSetWMName(display.get_private().display, priv->window, &prop);
77 display.check_error();
80 void Window::platform_reconfigure(bool fullscreen_changed)
82 DisplayHandle dpy = display.get_private().display;
84 bool was_visible = visible;
85 if(fullscreen_changed)
91 // Wait for the window to be unmapped. This makes window managers happy.
93 int ev_type = UnmapNotify;
94 XPeekIfEvent(dpy, &ev, match_event_type, reinterpret_cast<char *>(&ev_type));
97 XSetWindowAttributes attr;
98 attr.override_redirect = options.fullscreen;
99 XChangeWindowAttributes(dpy, priv->window, CWOverrideRedirect, &attr);
103 if(options.resizable)
107 hints.flags = PMinSize|PMaxSize;
108 hints.min_width = hints.max_width = options.width;
109 hints.min_height = hints.max_height = options.height;
111 XSetWMNormalHints(dpy, priv->window, &hints);
113 if(options.fullscreen)
114 XMoveResizeWindow(dpy, priv->window, 0, 0, options.width, options.height);
115 else if(options.user_position)
116 XMoveResizeWindow(dpy, priv->window, options.x, options.y, options.width, options.height);
118 XResizeWindow(dpy, priv->window, options.width, options.height);
120 if(fullscreen_changed && was_visible)
124 void Window::show_cursor(bool s)
126 DisplayHandle dpy = display.get_private().display;
129 XUndefineCursor(dpy, priv->window);
132 if(!priv->invisible_cursor)
134 int screen = DefaultScreen(dpy);
136 Pixmap pm = XCreatePixmap(dpy, priv->window, 1, 1, 1);
137 GC gc = XCreateGC(dpy, pm, 0, 0);
138 XSetFunction(dpy, gc, GXclear);
139 XDrawPoint(dpy, pm, gc, 0, 0);
143 black.pixel = BlackPixel(dpy, screen);
144 XQueryColor(dpy, DefaultColormap(dpy, screen), &black);
146 priv->invisible_cursor = XCreatePixmapCursor(dpy, pm, pm, &black, &black, 0, 0);
148 XFreePixmap(dpy, pm);
150 XDefineCursor(dpy, priv->window, priv->invisible_cursor);
154 void Window::warp_pointer(int x, int y)
156 XWarpPointer(display.get_private().display, None, priv->window, 0, 0, 0, 0, x, y);
159 void Window::platform_set_touch_input()
163 void Window::platform_show()
165 DisplayHandle dpy = display.get_private().display;
166 XMapRaised(dpy, priv->window);
167 if(options.user_position)
168 XMoveWindow(dpy, priv->window, options.x, options.y);
171 void Window::platform_hide()
173 XUnmapWindow(display.get_private().display, priv->window);
176 bool Window::event(const Event &evnt)
178 const XEvent &ev = evnt.xevent;
186 signal_input_event.emit(evnt);
188 case ConfigureNotify:
189 if((ev.xconfigure.width==static_cast<int>(options.width) && ev.xconfigure.height==static_cast<int>(options.height)) == resizing)
191 options.width = ev.xconfigure.width;
192 options.height = ev.xconfigure.height;
194 signal_resize.emit(options.width, options.height);
198 int x = ev.xconfigure.x;
199 int y = ev.xconfigure.y;
203 if(!ev.xconfigure.send_event)
205 /* If the window manager reparented us, the coordinates of a
206 real event are in the parent window's space. */
207 priv->rel_x = ev.xconfigure.x;
208 priv->rel_y = ev.xconfigure.y;
210 const Display::Private &dpy_priv = display.get_private();
212 XTranslateCoordinates(dpy_priv.display, priv->window, dpy_priv.root_window, 0, 0, &x, &y, &dummy);
215 // Use the coordinates of the window manager frame
220 if((x==options.x && y==options.y) == moving)
225 signal_move.emit(options.x, options.y);
231 priv->reparented = (ev.xreparent.parent!=display.get_private().root_window);
232 if(!priv->reparented)
239 if(ev.xclient.data.l[0]==static_cast<long>(priv->wm_delete_window))
243 if(options.fullscreen)
244 XSetInputFocus(display.get_private().display, priv->window, RevertToParent, CurrentTime);
247 if(options.fullscreen)
248 XGrabPointer(display.get_private().display, priv->window, true, None, GrabModeAsync, GrabModeAsync, priv->window, None, CurrentTime);
251 signal_expose.emit(ev.xexpose.x, ev.xexpose.y, ev.xexpose.width, ev.xexpose.height, evnt);
254 signal_got_focus.emit();
257 signal_lost_focus.emit();
266 } // namespace Graphics