]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/x11/window.cpp
Track window positions
[libs/gui.git] / source / graphics / x11 / window.cpp
1 #include <vector>
2 #include <X11/Xatom.h>
3 #include <X11/Xutil.h>
4 #include <msp/core/systemerror.h>
5 #include "display_private.h"
6 #include "window.h"
7 #include "window_private.h"
8
9 using namespace std;
10
11 namespace {
12
13 Bool match_event_type(Display *, XEvent *event, XPointer arg)
14 {
15         return event->type==*reinterpret_cast<int *>(arg);
16 }
17
18 }
19
20 namespace Msp {
21 namespace Graphics {
22
23 void Window::platform_init()
24 {
25         DisplayHandle dpy = display.get_private().display;
26
27         priv->wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", true);
28         priv->invisible_cursor = 0;
29         priv->reparented = false;
30         priv->rel_x = 0;
31         priv->rel_y = 0;
32
33         XSetWindowAttributes attr;
34         attr.override_redirect = options.fullscreen;
35         attr.event_mask = ButtonPressMask|ButtonReleaseMask|PointerMotionMask|KeyPressMask|KeyReleaseMask|StructureNotifyMask|EnterWindowMask|ExposureMask;
36
37         priv->window = XCreateWindow(dpy,
38                 display.get_private().root_window,
39                 0, 0,
40                 options.width, options.height,
41                 0,
42                 CopyFromParent,
43                 InputOutput,
44                 CopyFromParent,
45                 CWOverrideRedirect|CWEventMask, &attr);
46
47         XSetWMProtocols(dpy, priv->window, &priv->wm_delete_window, 1);
48
49         if(!options.resizable)
50         {
51                 XSizeHints hints;
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);
56         }
57 }
58
59 void Window::platform_cleanup()
60 {
61         if(priv->window)
62                 XDestroyWindow(display.get_private().display, priv->window);
63
64         if(priv->invisible_cursor)
65                 XFreeCursor(display.get_private().display, priv->invisible_cursor);
66 }
67
68 void Window::set_title(const string &title)
69 {
70         vector<unsigned char> buf(title.begin(), title.end());
71         XTextProperty prop;
72         prop.value = &buf[0];
73         prop.encoding = XA_STRING;
74         prop.format = 8;
75         prop.nitems = title.size();
76         XSetWMName(display.get_private().display, priv->window, &prop);
77         display.check_error();
78 }
79
80 void Window::platform_reconfigure(bool fullscreen_changed)
81 {
82         DisplayHandle dpy = display.get_private().display;
83
84         bool was_visible = visible;
85         if(fullscreen_changed)
86         {
87                 if(was_visible)
88                 {
89                         hide();
90
91                         // Wait for the window to be unmapped.  This makes window managers happy.
92                         XEvent ev;
93                         int ev_type = UnmapNotify;
94                         XPeekIfEvent(dpy, &ev, match_event_type, reinterpret_cast<char *>(&ev_type));
95                 }
96
97                 XSetWindowAttributes attr;
98                 attr.override_redirect = options.fullscreen;
99                 XChangeWindowAttributes(dpy, priv->window, CWOverrideRedirect, &attr);
100         }
101
102         XSizeHints hints;
103         if(options.resizable)
104                 hints.flags = 0;
105         else
106         {
107                 hints.flags = PMinSize|PMaxSize;
108                 hints.min_width = hints.max_width = options.width;
109                 hints.min_height = hints.max_height = options.height;
110         }
111         XSetWMNormalHints(dpy, priv->window, &hints);
112
113         if(options.fullscreen)
114                 XMoveResizeWindow(dpy, priv->window, 0, 0, options.width, options.height);
115         else
116                 XResizeWindow(dpy, priv->window, options.width, options.height);
117
118         if(fullscreen_changed && was_visible)
119                 show();
120 }
121
122 void Window::show_cursor(bool s)
123 {
124         DisplayHandle dpy = display.get_private().display;
125
126         if(s)
127                 XUndefineCursor(dpy, priv->window);
128         else
129         {
130                 if(!priv->invisible_cursor)
131                 {
132                         int screen = DefaultScreen(dpy);
133
134                         Pixmap pm = XCreatePixmap(dpy, priv->window, 1, 1, 1);
135                         GC gc = XCreateGC(dpy, pm, 0, 0);
136                         XSetFunction(dpy, gc, GXclear);
137                         XDrawPoint(dpy, pm, gc, 0, 0);
138                         XFreeGC(dpy, gc);
139
140                         XColor black;
141                         black.pixel = BlackPixel(dpy, screen);
142                         XQueryColor(dpy, DefaultColormap(dpy, screen), &black);
143
144                         priv->invisible_cursor = XCreatePixmapCursor(dpy, pm, pm, &black, &black, 0, 0);
145
146                         XFreePixmap(dpy, pm);
147                 }
148                 XDefineCursor(dpy, priv->window, priv->invisible_cursor);
149         }
150 }
151
152 void Window::warp_pointer(int x, int y)
153 {
154         XWarpPointer(display.get_private().display, None, priv->window, 0, 0, 0, 0, x, y);
155 }
156
157 void Window::platform_set_touch_input()
158 {
159 }
160
161 void Window::platform_show()
162 {
163         XMapRaised(display.get_private().display, priv->window);
164 }
165
166 void Window::platform_hide()
167 {
168         XUnmapWindow(display.get_private().display, priv->window);
169 }
170
171 bool Window::event(const Event &evnt)
172 {
173         const XEvent &ev = evnt.xevent;
174         switch(ev.type)
175         {
176         case ButtonPress:
177         case ButtonRelease:
178         case MotionNotify:
179         case KeyPress:
180         case KeyRelease:
181                 signal_input_event.emit(evnt);
182                 break;
183         case ConfigureNotify:
184                 if((ev.xconfigure.width==static_cast<int>(options.width) && ev.xconfigure.height==static_cast<int>(options.height)) == resizing)
185                 {
186                         options.width = ev.xconfigure.width;
187                         options.height = ev.xconfigure.height;
188                         resizing = false;
189                         signal_resize.emit(options.width, options.height);
190                 }
191
192                 {
193                         int x = ev.xconfigure.x;
194                         int y = ev.xconfigure.y;
195
196                         if(priv->reparented)
197                         {
198                                 if(!ev.xconfigure.send_event)
199                                 {
200                                         /* If the window manager reparented us, the coordinates of a
201                                         real event are in the parent window's space. */
202                                         priv->rel_x = ev.xconfigure.x;
203                                         priv->rel_y = ev.xconfigure.y;
204
205                                         const Display::Private &dpy_priv = display.get_private();
206                                         WindowHandle dummy;
207                                         XTranslateCoordinates(dpy_priv.display, priv->window, dpy_priv.root_window, 0, 0, &x, &y, &dummy);
208                                 }
209
210                                 // Use the coordinates of the window manager frame
211                                 x -= priv->rel_x;
212                                 y -= priv->rel_y;
213                         }
214
215                         if(x!=options.x || y!=options.y)
216                         {
217                                 options.x = x;
218                                 options.y = y;
219                                 signal_move.emit(options.x, options.y);
220                         }
221                 }
222
223                 break;
224         case ReparentNotify:
225                 priv->reparented = (ev.xreparent.parent!=display.get_private().root_window);
226                 if(!priv->reparented)
227                 {
228                         priv->rel_x = 0;
229                         priv->rel_y = 0;
230                 }
231                 break;
232         case ClientMessage:
233                 if(ev.xclient.data.l[0]==static_cast<long>(priv->wm_delete_window))
234                         signal_close.emit();
235                 break;
236         case EnterNotify:
237                 if(options.fullscreen)
238                         XSetInputFocus(display.get_private().display, priv->window, RevertToParent, CurrentTime);
239                 break;
240         case MapNotify:
241                 if(options.fullscreen)
242                         XGrabPointer(display.get_private().display, priv->window, true, None, GrabModeAsync, GrabModeAsync, priv->window, None, CurrentTime);
243                 break;
244         case Expose:
245                 signal_expose.emit(ev.xexpose.x, ev.xexpose.y, ev.xexpose.width, ev.xexpose.height, evnt);
246                 break;
247         default:
248                 return false;
249         }
250
251         return true;
252 }
253
254 } // namespace Graphics
255 } // namespace Msp