]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/x11/window.cpp
7513d3ffb14ebb958efe5e7d9b33a9f65e0482e4
[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|FocusChangeMask;
36
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,
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 if(options.user_position)
116                 XMoveResizeWindow(dpy, priv->window, options.x, options.y, options.width, options.height);
117         else
118                 XResizeWindow(dpy, priv->window, options.width, options.height);
119
120         if(fullscreen_changed && was_visible)
121                 show();
122 }
123
124 void Window::show_cursor(bool s)
125 {
126         DisplayHandle dpy = display.get_private().display;
127
128         if(s)
129                 XUndefineCursor(dpy, priv->window);
130         else
131         {
132                 if(!priv->invisible_cursor)
133                 {
134                         int screen = DefaultScreen(dpy);
135
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);
140                         XFreeGC(dpy, gc);
141
142                         XColor black;
143                         black.pixel = BlackPixel(dpy, screen);
144                         XQueryColor(dpy, DefaultColormap(dpy, screen), &black);
145
146                         priv->invisible_cursor = XCreatePixmapCursor(dpy, pm, pm, &black, &black, 0, 0);
147
148                         XFreePixmap(dpy, pm);
149                 }
150                 XDefineCursor(dpy, priv->window, priv->invisible_cursor);
151         }
152 }
153
154 void Window::warp_pointer(int x, int y)
155 {
156         XWarpPointer(display.get_private().display, None, priv->window, 0, 0, 0, 0, x, y);
157 }
158
159 void Window::platform_set_touch_input()
160 {
161 }
162
163 void Window::platform_show()
164 {
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);
169 }
170
171 void Window::platform_hide()
172 {
173         XUnmapWindow(display.get_private().display, priv->window);
174 }
175
176 bool Window::event(const Event &evnt)
177 {
178         const XEvent &ev = evnt.xevent;
179         switch(ev.type)
180         {
181         case ButtonPress:
182         case ButtonRelease:
183         case MotionNotify:
184         case KeyPress:
185         case KeyRelease:
186                 signal_input_event.emit(evnt);
187                 break;
188         case ConfigureNotify:
189                 if((ev.xconfigure.width==static_cast<int>(options.width) && ev.xconfigure.height==static_cast<int>(options.height)) == resizing)
190                 {
191                         options.width = ev.xconfigure.width;
192                         options.height = ev.xconfigure.height;
193                         resizing = false;
194                         signal_resize.emit(options.width, options.height);
195                 }
196
197                 {
198                         int x = ev.xconfigure.x;
199                         int y = ev.xconfigure.y;
200
201                         if(priv->reparented)
202                         {
203                                 if(!ev.xconfigure.send_event)
204                                 {
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;
209
210                                         const Display::Private &dpy_priv = display.get_private();
211                                         WindowHandle dummy;
212                                         XTranslateCoordinates(dpy_priv.display, priv->window, dpy_priv.root_window, 0, 0, &x, &y, &dummy);
213                                 }
214
215                                 // Use the coordinates of the window manager frame
216                                 x -= priv->rel_x;
217                                 y -= priv->rel_y;
218                         }
219
220                         if((x==options.x && y==options.y) == moving)
221                         {
222                                 options.x = x;
223                                 options.y = y;
224                                 moving = false;
225                                 signal_move.emit(options.x, options.y);
226                         }
227                 }
228
229                 break;
230         case ReparentNotify:
231                 priv->reparented = (ev.xreparent.parent!=display.get_private().root_window);
232                 if(!priv->reparented)
233                 {
234                         priv->rel_x = 0;
235                         priv->rel_y = 0;
236                 }
237                 break;
238         case ClientMessage:
239                 if(ev.xclient.data.l[0]==static_cast<long>(priv->wm_delete_window))
240                         signal_close.emit();
241                 break;
242         case EnterNotify:
243                 if(options.fullscreen)
244                         XSetInputFocus(display.get_private().display, priv->window, RevertToParent, CurrentTime);
245                 break;
246         case MapNotify:
247                 if(options.fullscreen)
248                         XGrabPointer(display.get_private().display, priv->window, true, None, GrabModeAsync, GrabModeAsync, priv->window, None, CurrentTime);
249                 break;
250         case Expose:
251                 signal_expose.emit(ev.xexpose.x, ev.xexpose.y, ev.xexpose.width, ev.xexpose.height, evnt);
252                 break;
253         case FocusIn:
254                 signal_got_focus.emit();
255                 break;
256         case FocusOut:
257                 signal_lost_focus.emit();
258                 break;
259         default:
260                 return false;
261         }
262
263         return true;
264 }
265
266 } // namespace Graphics
267 } // namespace Msp