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)
115 if(options.fullscreen_exclusive)
116 XMoveResizeWindow(dpy, priv->window, 0, 0, options.width, options.height);
119 const Monitor::Settings &ms = options.fullscreen_monitor->current_settings;
120 XMoveResizeWindow(dpy, priv->window, ms.x, ms.y, options.width, options.height);
123 else if(options.user_position)
124 XMoveResizeWindow(dpy, priv->window, options.x, options.y, options.width, options.height);
126 XResizeWindow(dpy, priv->window, options.width, options.height);
128 if(fullscreen_changed && was_visible)
132 void Window::show_cursor(bool s)
134 DisplayHandle dpy = display.get_private().display;
137 XUndefineCursor(dpy, priv->window);
140 if(!priv->invisible_cursor)
142 int screen = DefaultScreen(dpy);
144 Pixmap pm = XCreatePixmap(dpy, priv->window, 1, 1, 1);
145 GC gc = XCreateGC(dpy, pm, 0, 0);
146 XSetFunction(dpy, gc, GXclear);
147 XDrawPoint(dpy, pm, gc, 0, 0);
151 black.pixel = BlackPixel(dpy, screen);
152 XQueryColor(dpy, DefaultColormap(dpy, screen), &black);
154 priv->invisible_cursor = XCreatePixmapCursor(dpy, pm, pm, &black, &black, 0, 0);
156 XFreePixmap(dpy, pm);
158 XDefineCursor(dpy, priv->window, priv->invisible_cursor);
162 void Window::warp_pointer(int x, int y)
164 XWarpPointer(display.get_private().display, None, priv->window, 0, 0, 0, 0, x, y);
167 void Window::platform_set_touch_input()
171 void Window::platform_show()
173 DisplayHandle dpy = display.get_private().display;
174 XMapRaised(dpy, priv->window);
175 if(options.user_position)
176 XMoveWindow(dpy, priv->window, options.x, options.y);
179 void Window::platform_hide()
181 XUnmapWindow(display.get_private().display, priv->window);
184 bool Window::event(const Event &evnt)
186 const XEvent &ev = evnt.xevent;
194 signal_input_event.emit(evnt);
196 case ConfigureNotify:
197 if((ev.xconfigure.width==static_cast<int>(options.width) && ev.xconfigure.height==static_cast<int>(options.height)) == resizing)
199 options.width = ev.xconfigure.width;
200 options.height = ev.xconfigure.height;
202 signal_resize.emit(options.width, options.height);
206 int x = ev.xconfigure.x;
207 int y = ev.xconfigure.y;
211 if(!ev.xconfigure.send_event)
213 /* If the window manager reparented us, the coordinates of a
214 real event are in the parent window's space. */
215 priv->rel_x = ev.xconfigure.x;
216 priv->rel_y = ev.xconfigure.y;
218 const Display::Private &dpy_priv = display.get_private();
220 XTranslateCoordinates(dpy_priv.display, priv->window, dpy_priv.root_window, 0, 0, &x, &y, &dummy);
223 // Use the coordinates of the window manager frame
228 if((x==options.x && y==options.y) == moving)
233 signal_move.emit(options.x, options.y);
239 priv->reparented = (ev.xreparent.parent!=display.get_private().root_window);
240 if(!priv->reparented)
247 if(ev.xclient.data.l[0]==static_cast<long>(priv->wm_delete_window))
251 if(options.fullscreen)
252 XSetInputFocus(display.get_private().display, priv->window, RevertToParent, CurrentTime);
255 if(options.fullscreen && options.fullscreen_exclusive)
256 XGrabPointer(display.get_private().display, priv->window, true, None, GrabModeAsync, GrabModeAsync, priv->window, None, CurrentTime);
259 signal_expose.emit(ev.xexpose.x, ev.xexpose.y, ev.xexpose.width, ev.xexpose.height, evnt);
262 signal_got_focus.emit();
265 signal_lost_focus.emit();
274 } // namespace Graphics