2 #include "window_private.h"
6 #include <msp/core/systemerror.h>
7 #include "display_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);
29 XSetWindowAttributes attr;
30 attr.override_redirect = options.fullscreen;
31 attr.event_mask = ButtonPressMask|ButtonReleaseMask|PointerMotionMask|KeyPressMask|KeyReleaseMask|StructureNotifyMask|EnterWindowMask|ExposureMask|FocusChangeMask;
33 // User position is set when the window is mapped
36 if(options.fullscreen && !options.fullscreen_exclusive)
38 const Monitor::Settings &ms = options.fullscreen_monitor->current_settings;
43 priv->window = XCreateWindow(dpy,
44 display.get_private().root_window,
46 options.width, options.height,
51 CWOverrideRedirect|CWEventMask, &attr);
53 XSetWMProtocols(dpy, priv->window, &priv->wm_delete_window, 1);
55 if(!options.resizable)
58 hints.flags = PMinSize|PMaxSize;
59 hints.min_width = hints.max_width = options.width;
60 hints.min_height = hints.max_height = options.height;
61 XSetWMNormalHints(dpy, priv->window, &hints);
65 void Window::platform_cleanup()
68 XDestroyWindow(display.get_private().display, priv->window);
70 if(priv->invisible_cursor)
71 XFreeCursor(display.get_private().display, priv->invisible_cursor);
74 void Window::set_title(const string &title)
76 vector<unsigned char> buf(title.begin(), title.end());
79 prop.encoding = XA_STRING;
81 prop.nitems = title.size();
82 XSetWMName(display.get_private().display, priv->window, &prop);
83 display.check_error();
86 void Window::platform_reconfigure(bool fullscreen_changed)
88 DisplayHandle dpy = display.get_private().display;
90 bool was_visible = visible;
91 if(fullscreen_changed)
97 // Wait for the window to be unmapped. This makes window managers happy.
99 int ev_type = UnmapNotify;
100 XPeekIfEvent(dpy, &ev, match_event_type, reinterpret_cast<char *>(&ev_type));
103 XSetWindowAttributes attr;
104 attr.override_redirect = options.fullscreen;
105 XChangeWindowAttributes(dpy, priv->window, CWOverrideRedirect, &attr);
109 if(options.resizable)
113 hints.flags = PMinSize|PMaxSize;
114 hints.min_width = hints.max_width = options.width;
115 hints.min_height = hints.max_height = options.height;
117 XSetWMNormalHints(dpy, priv->window, &hints);
119 if(options.fullscreen)
121 if(options.fullscreen_exclusive)
122 XMoveResizeWindow(dpy, priv->window, 0, 0, options.width, options.height);
125 const Monitor::Settings &ms = options.fullscreen_monitor->current_settings;
126 XMoveResizeWindow(dpy, priv->window, ms.x, ms.y, options.width, options.height);
129 else if(options.user_position)
130 XMoveResizeWindow(dpy, priv->window, options.x, options.y, options.width, options.height);
132 XResizeWindow(dpy, priv->window, options.width, options.height);
134 if(fullscreen_changed && was_visible)
138 void Window::show_cursor(bool s)
140 DisplayHandle dpy = display.get_private().display;
143 XUndefineCursor(dpy, priv->window);
146 if(!priv->invisible_cursor)
148 int screen = DefaultScreen(dpy);
150 Pixmap pm = XCreatePixmap(dpy, priv->window, 1, 1, 1);
151 GC gc = XCreateGC(dpy, pm, 0, 0);
152 XSetFunction(dpy, gc, GXclear);
153 XDrawPoint(dpy, pm, gc, 0, 0);
157 black.pixel = BlackPixel(dpy, screen);
158 XQueryColor(dpy, DefaultColormap(dpy, screen), &black);
160 priv->invisible_cursor = XCreatePixmapCursor(dpy, pm, pm, &black, &black, 0, 0);
162 XFreePixmap(dpy, pm);
164 XDefineCursor(dpy, priv->window, priv->invisible_cursor);
168 void Window::warp_pointer(int x, int y)
170 XWarpPointer(display.get_private().display, None, priv->window, 0, 0, 0, 0, x, y);
173 void Window::platform_set_touch_input()
177 void Window::platform_show()
179 DisplayHandle dpy = display.get_private().display;
180 XMapRaised(dpy, priv->window);
181 if(options.user_position && !options.fullscreen)
182 XMoveWindow(dpy, priv->window, options.x, options.y);
185 void Window::platform_hide()
187 XUnmapWindow(display.get_private().display, priv->window);
190 bool Window::event(const Event &evnt)
192 const XEvent &ev = evnt.xevent;
200 signal_input_event.emit(evnt);
202 case ConfigureNotify:
203 if((ev.xconfigure.width==static_cast<int>(options.width) && ev.xconfigure.height==static_cast<int>(options.height)) == resizing)
205 options.width = ev.xconfigure.width;
206 options.height = ev.xconfigure.height;
208 signal_resize.emit(options.width, options.height);
212 int x = ev.xconfigure.x;
213 int y = ev.xconfigure.y;
217 if(!ev.xconfigure.send_event)
219 /* If the window manager reparented us, the coordinates of a
220 real event are in the parent window's space. */
221 priv->rel_x = ev.xconfigure.x;
222 priv->rel_y = ev.xconfigure.y;
224 const Display::Private &dpy_priv = display.get_private();
226 XTranslateCoordinates(dpy_priv.display, priv->window, dpy_priv.root_window, 0, 0, &x, &y, &dummy);
229 // Use the coordinates of the window manager frame
234 if((x==options.x && y==options.y) == moving)
239 signal_move.emit(options.x, options.y);
245 priv->reparented = (ev.xreparent.parent!=display.get_private().root_window);
246 if(!priv->reparented)
253 if(ev.xclient.data.l[0]==static_cast<long>(priv->wm_delete_window))
257 if(options.fullscreen)
258 XSetInputFocus(display.get_private().display, priv->window, RevertToParent, CurrentTime);
261 if(options.fullscreen && options.fullscreen_exclusive)
262 XGrabPointer(display.get_private().display, priv->window, true, None, GrabModeAsync, GrabModeAsync, priv->window, None, CurrentTime);
265 signal_expose.emit(ev.xexpose.x, ev.xexpose.y, ev.xexpose.width, ev.xexpose.height, evnt);
268 signal_got_focus.emit();
271 signal_lost_focus.emit();
280 } // namespace Graphics