]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/x11/window.cpp
577f1d42d65f41af62b08d10c9d57f23167dbc12
[libs/gui.git] / source / graphics / x11 / window.cpp
1 #include <vector>
2 #include <X11/Xatom.h>
3 #include <X11/Xutil.h>
4 #ifdef WITH_XF86VIDMODE
5 #include <X11/extensions/xf86vmode.h>
6 #endif
7 #include <msp/core/systemerror.h>
8 #include "display_private.h"
9 #include "window.h"
10 #include "window_private.h"
11
12 using namespace std;
13
14 namespace {
15
16 Bool match_event_type(Display *, XEvent *event, XPointer arg)
17 {
18         return event->type==*reinterpret_cast<int *>(arg);
19 }
20
21 }
22
23 namespace Msp {
24 namespace Graphics {
25
26 void Window::platform_init()
27 {
28         DisplayHandle dpy = display.get_private().display;
29
30         priv->wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", true);
31         priv->invisible_cursor = 0;
32
33         XSetWindowAttributes attr;
34         attr.override_redirect = options.fullscreen;
35         attr.event_mask = ButtonPressMask|ButtonReleaseMask|PointerMotionMask|KeyPressMask|KeyReleaseMask|StructureNotifyMask|EnterWindowMask;
36
37         priv->window = XCreateWindow(dpy,
38                 DefaultRootWindow(dpy),
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_show()
158 {
159         XMapRaised(display.get_private().display, priv->window);
160 }
161
162 void Window::platform_hide()
163 {
164         XUnmapWindow(display.get_private().display, priv->window);
165 }
166
167 bool Window::event(const Event &evnt)
168 {
169         const XEvent &ev = evnt.xevent;
170         switch(ev.type)
171         {
172         case ButtonPress:
173         case ButtonRelease:
174         case MotionNotify:
175         case KeyPress:
176         case KeyRelease:
177                 signal_input_event.emit(evnt);
178                 break;
179         case ConfigureNotify:
180                 if((ev.xconfigure.width==static_cast<int>(options.width) && ev.xconfigure.height==static_cast<int>(options.height)) == resizing)
181                 {
182                         options.width = ev.xconfigure.width;
183                         options.height = ev.xconfigure.height;
184                         resizing = false;
185                         signal_resize.emit(options.width, options.height);
186                 }
187 #ifdef WITH_XF86VIDMODE
188                 if(options.fullscreen)
189                 {
190                         DisplayHandle dpy = display.get_private().display;
191                         int screen = DefaultScreen(dpy);
192                         XF86VidModeSetViewPort(dpy, screen, ev.xconfigure.x, ev.xconfigure.y);
193                 }
194 #endif
195                 break;
196         case ClientMessage:
197                 if(ev.xclient.data.l[0]==static_cast<long>(priv->wm_delete_window))
198                         signal_close.emit();
199                 break;
200         case EnterNotify:
201                 if(options.fullscreen)
202                         XSetInputFocus(display.get_private().display, priv->window, RevertToParent, CurrentTime);
203                 break;
204         case MapNotify:
205                 if(options.fullscreen)
206                         XGrabPointer(display.get_private().display, priv->window, true, None, GrabModeAsync, GrabModeAsync, priv->window, None, CurrentTime);
207                 break;
208         default:
209                 return false;
210         }
211
212         return true;
213 }
214
215 } // namespace Graphics
216 } // namespace Msp