1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/utilsx11.cpp
3 // Purpose: Miscellaneous X11 functions
4 // Author: Mattia Barbon, Vaclav Slavik, Robert Roebling
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__WXX11__) || defined(__WXGTK__) || defined(__WXMOTIF__)
14 // for compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
17 #include "wx/unix/utilsx11.h"
18 #include "wx/iconbndl.h"
24 #pragma message disable nosimpint
27 #include <X11/Xatom.h>
29 #pragma message enable nosimpint
37 // Various X11 Atoms used in this file:
38 static Atom _NET_WM_ICON
= 0;
39 static Atom _NET_WM_STATE
= 0;
40 static Atom _NET_WM_STATE_FULLSCREEN
= 0;
41 static Atom _NET_WM_STATE_STAYS_ON_TOP
= 0;
42 static Atom _NET_WM_WINDOW_TYPE
= 0;
43 static Atom _NET_WM_WINDOW_TYPE_NORMAL
= 0;
44 static Atom _KDE_NET_WM_WINDOW_TYPE_OVERRIDE
= 0;
45 static Atom _WIN_LAYER
= 0;
46 static Atom KWIN_RUNNING
= 0;
48 static Atom _NET_SUPPORTING_WM_CHECK
= 0;
49 static Atom _NET_SUPPORTED
= 0;
52 #define wxMAKE_ATOM(name, display) \
53 if (name == 0) name = XInternAtom((display), #name, False)
56 // Is the window mapped?
57 static bool IsMapped(Display
*display
, Window window
)
59 XWindowAttributes attr
;
60 XGetWindowAttributes(display
, window
, &attr
);
61 return (attr
.map_state
!= IsUnmapped
);
66 // Suspends X11 errors. Used when we expect errors but they are not fatal
68 class wxX11ErrorsSuspender
71 wxX11ErrorsSuspender(Display
*d
) : m_display(d
)
73 m_old
= XSetErrorHandler(handler
);
75 ~wxX11ErrorsSuspender()
78 XSetErrorHandler(m_old
);
82 int (*m_old
)(Display
*, XErrorEvent
*);
83 static int handler(Display
*, XErrorEvent
*) { return 0; }
88 // ----------------------------------------------------------------------------
89 // Setting icons for window manager:
90 // ----------------------------------------------------------------------------
92 void wxSetIconsX11( WXDisplay
* display
, WXWindow window
,
93 const wxIconBundle
& ib
)
97 size_t i
, max
= ib
.m_icons
.GetCount();
99 for( i
= 0; i
< max
; ++i
)
100 if( ib
.m_icons
[i
].Ok() )
101 size
+= 2 + ib
.m_icons
[i
].GetWidth() * ib
.m_icons
[i
].GetHeight();
103 wxMAKE_ATOM(_NET_WM_ICON
, (Display
*)display
);
107 wxUint32
* data
= new wxUint32
[size
];
108 wxUint32
* ptr
= data
;
110 for( i
= 0; i
< max
; ++i
)
112 const wxImage image
= ib
.m_icons
[i
].ConvertToImage();
113 int width
= image
.GetWidth(), height
= image
.GetHeight();
114 unsigned char* imageData
= image
.GetData();
115 unsigned char* imageDataEnd
= imageData
+ ( width
* height
* 3 );
116 bool hasMask
= image
.HasMask();
117 unsigned char rMask
, gMask
, bMask
;
118 unsigned char r
, g
, b
, a
;
122 rMask
= image
.GetMaskRed();
123 gMask
= image
.GetMaskGreen();
124 bMask
= image
.GetMaskBlue();
126 else // no mask, but still init the variables to avoid warnings
136 while( imageData
< imageDataEnd
) {
140 if( hasMask
&& r
== rMask
&& g
== gMask
&& b
== bMask
)
145 *ptr
++ = ( a
<< 24 ) | ( r
<< 16 ) | ( g
<< 8 ) | b
;
151 XChangeProperty( (Display
*)display
,
156 (unsigned char*)data
, size
);
161 XDeleteProperty( (Display
*)display
,
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
173 // NB: Setting fullscreen mode under X11 is a complicated matter. There was
174 // no standard way of doing it until recently. ICCCM doesn't know the
175 // concept of fullscreen windows and the only way to make a window
176 // fullscreen is to remove decorations, resize it to cover entire screen
177 // and set WIN_LAYER_ABOVE_DOCK.
179 // This doesn't always work, though. Specifically, at least kwin from
180 // KDE 3 ignores the hint. The only way to make kwin accept our request
181 // is to emulate the way Qt does it. That is, unmap the window, set
182 // _NET_WM_WINDOW_TYPE to _KDE_NET_WM_WINDOW_TYPE_OVERRIDE (KDE extension),
183 // add _NET_WM_STATE_STAYS_ON_TOP (ditto) to _NET_WM_STATE and map
186 // Version 1.2 of Window Manager Specification (aka wm-spec aka
187 // Extended Window Manager Hints) introduced _NET_WM_STATE_FULLSCREEN
188 // window state which provides cleanest and simplest possible way of
189 // making a window fullscreen. WM-spec is a de-facto standard adopted
190 // by GNOME and KDE folks, but _NET_WM_STATE_FULLSCREEN isn't yet widely
191 // supported. As of January 2003, only GNOME 2's default WM Metacity
192 // implements, KDE will support it from version 3.2. At toolkits level,
193 // GTK+ >= 2.1.2 uses it as the only method of making windows fullscreen
194 // (that's why wxGTK will *not* switch to using gtk_window_fullscreen
195 // unless it has better compatiblity with older WMs).
198 // This is what wxWindows does in wxSetFullScreenStateX11:
199 // 1) if _NET_WM_STATE_FULLSCREEN is supported, use it
200 // 2) otherwise try WM-specific hacks (KDE, IceWM)
201 // 3) use _WIN_LAYER and hope that the WM will recognize it
202 // The code was tested with:
203 // twm, IceWM, WindowMaker, Metacity, kwin, sawfish, lesstif-mwm
206 #define WIN_LAYER_NORMAL 4
207 #define WIN_LAYER_ABOVE_DOCK 10
209 static void wxWinHintsSetLayer(Display
*display
, Window rootWnd
,
210 Window window
, int layer
)
212 wxX11ErrorsSuspender
noerrors(display
);
216 wxMAKE_ATOM( _WIN_LAYER
, display
);
218 if (IsMapped(display
, window
))
220 xev
.type
= ClientMessage
;
221 xev
.xclient
.type
= ClientMessage
;
222 xev
.xclient
.window
= window
;
223 xev
.xclient
.message_type
= _WIN_LAYER
;
224 xev
.xclient
.format
= 32;
225 xev
.xclient
.data
.l
[0] = (long)layer
;
226 xev
.xclient
.data
.l
[1] = CurrentTime
;
228 XSendEvent(display
, rootWnd
, False
,
229 SubstructureNotifyMask
, (XEvent
*) &xev
);
236 XChangeProperty(display
, window
,
237 _WIN_LAYER
, XA_CARDINAL
, 32,
238 PropModeReplace
, (unsigned char *)data
, 1);
245 static bool wxQueryWMspecSupport(Display
* WXUNUSED(display
),
246 Window
WXUNUSED(rootWnd
),
249 GdkAtom gatom
= gdk_x11_xatom_to_atom(feature
);
250 return gdk_net_wm_supports(gatom
);
253 static bool wxQueryWMspecSupport(Display
*display
, Window rootWnd
, Atom feature
)
255 wxMAKE_ATOM(_NET_SUPPORTING_WM_CHECK
, display
);
256 wxMAKE_ATOM(_NET_SUPPORTED
, display
);
258 // FIXME: We may want to cache these checks. Note that we can't simply
259 // remember the results in global variable because the WM may go
260 // away and be replaced by another one! One possible approach
261 // would be invalidate the case every 15 seconds or so. Since this
262 // code is currently only used by wxTopLevelWindow::ShowFullScreen,
263 // it is not important that it is not optimized.
265 // If the WM supports ICCCM (i.e. the root window has
266 // _NET_SUPPORTING_WM_CHECK property that points to a WM-owned
267 // window), we could watch for DestroyNotify event on the window
268 // and invalidate our cache when the windows goes away (= WM
269 // is replaced by another one). This is what GTK+ 2 does.
270 // Let's do it only if it is needed, it requires changes to
278 unsigned long nwins
, natoms
;
280 // Is the WM ICCCM supporting?
281 XGetWindowProperty(display
, rootWnd
,
282 _NET_SUPPORTING_WM_CHECK
, 0, LONG_MAX
,
283 False
, XA_WINDOW
, &type
, &format
, &nwins
,
284 &after
, (unsigned char **)&wins
);
285 if ( type
!= XA_WINDOW
|| nwins
<= 0 || wins
[0] == None
)
289 // Query for supported features:
290 XGetWindowProperty(display
, rootWnd
,
291 _NET_SUPPORTED
, 0, LONG_MAX
,
292 False
, XA_ATOM
, &type
, &format
, &natoms
,
293 &after
, (unsigned char **)&atoms
);
294 if ( type
!= XA_ATOM
|| atoms
== NULL
)
297 // Lookup the feature we want:
298 for (unsigned i
= 0; i
< natoms
; i
++)
300 if ( atoms
[i
] == feature
)
312 #define _NET_WM_STATE_REMOVE 0
313 #define _NET_WM_STATE_ADD 1
315 static void wxWMspecSetState(Display
*display
, Window rootWnd
,
316 Window window
, int operation
, Atom state
)
318 wxMAKE_ATOM(_NET_WM_STATE
, display
);
320 if ( IsMapped(display
, window
) )
323 xev
.type
= ClientMessage
;
324 xev
.xclient
.type
= ClientMessage
;
325 xev
.xclient
.serial
= 0;
326 xev
.xclient
.send_event
= True
;
327 xev
.xclient
.display
= display
;
328 xev
.xclient
.window
= window
;
329 xev
.xclient
.message_type
= _NET_WM_STATE
;
330 xev
.xclient
.format
= 32;
331 xev
.xclient
.data
.l
[0] = operation
;
332 xev
.xclient
.data
.l
[1] = state
;
333 xev
.xclient
.data
.l
[2] = None
;
335 XSendEvent(display
, rootWnd
,
337 SubstructureRedirectMask
| SubstructureNotifyMask
,
340 // FIXME - must modify _NET_WM_STATE property list if the window
344 static void wxWMspecSetFullscreen(Display
*display
, Window rootWnd
,
345 Window window
, bool fullscreen
)
347 wxMAKE_ATOM(_NET_WM_STATE_FULLSCREEN
, display
);
348 wxWMspecSetState(display
, rootWnd
,
350 fullscreen
? _NET_WM_STATE_ADD
: _NET_WM_STATE_REMOVE
,
351 _NET_WM_STATE_FULLSCREEN
);
355 // Is the user running KDE's kwin window manager? At least kwin from KDE 3
356 // sets KWIN_RUNNING property on the root window.
357 static bool wxKwinRunning(Display
*display
, Window rootWnd
)
359 wxMAKE_ATOM(KWIN_RUNNING
, display
);
364 unsigned long nitems
, after
;
365 if (XGetWindowProperty(display
, rootWnd
,
366 KWIN_RUNNING
, 0, 1, False
, KWIN_RUNNING
,
367 &type
, &format
, &nitems
, &after
,
368 (unsigned char**)&data
) != Success
)
373 bool retval
= (type
== KWIN_RUNNING
&&
374 nitems
== 1 && data
&& data
[0] == 1);
379 // KDE's kwin is Qt-centric so much than no normal method of fullscreen
380 // mode will work with it. We have to carefully emulate the Qt way.
381 static void wxSetKDEFullscreen(Display
*display
, Window rootWnd
,
382 Window w
, bool fullscreen
, wxRect
*origRect
)
387 wxMAKE_ATOM(_NET_WM_WINDOW_TYPE
, display
);
388 wxMAKE_ATOM(_NET_WM_WINDOW_TYPE_NORMAL
, display
);
389 wxMAKE_ATOM(_KDE_NET_WM_WINDOW_TYPE_OVERRIDE
, display
);
390 wxMAKE_ATOM(_NET_WM_STATE_STAYS_ON_TOP
, display
);
394 data
[0] = _KDE_NET_WM_WINDOW_TYPE_OVERRIDE
;
395 data
[1] = _NET_WM_WINDOW_TYPE_NORMAL
;
400 data
[0] = _NET_WM_WINDOW_TYPE_NORMAL
;
405 // it is neccessary to unmap the window, otherwise kwin will ignore us:
406 XSync(display
, False
);
408 bool wasMapped
= IsMapped(display
, w
);
411 XUnmapWindow(display
, w
);
412 XSync(display
, False
);
415 XChangeProperty(display
, w
, _NET_WM_WINDOW_TYPE
, XA_ATOM
, 32,
416 PropModeReplace
, (unsigned char *) &data
, lng
);
417 XSync(display
, False
);
421 XMapRaised(display
, w
);
422 XSync(display
, False
);
425 wxWMspecSetState(display
, rootWnd
, w
,
426 fullscreen
? _NET_WM_STATE_ADD
: _NET_WM_STATE_REMOVE
,
427 _NET_WM_STATE_STAYS_ON_TOP
);
428 XSync(display
, False
);
432 // NB: like many other WMs, kwin ignores first request for window
433 // position change after the window was mapped. This additional
434 // move+resize event will ensure that the window is restored in
435 // exactly same position as before it was made fullscreen (because
436 // wxTopLevelWindow::ShowFullScreen will call SetSize, thus
437 // setting the position for second time).
438 XMoveResizeWindow(display
, w
,
439 origRect
->x
, origRect
->y
,
440 origRect
->width
, origRect
->height
);
441 XSync(display
, False
);
446 wxX11FullScreenMethod
wxGetFullScreenMethodX11(WXDisplay
* display
,
449 Window root
= (Window
)rootWindow
;
450 Display
*disp
= (Display
*)display
;
452 // if WM supports _NET_WM_STATE_FULLSCREEN from wm-spec 1.2, use it:
453 wxMAKE_ATOM(_NET_WM_STATE_FULLSCREEN
, disp
);
454 if (wxQueryWMspecSupport(disp
, root
, _NET_WM_STATE_FULLSCREEN
))
456 wxLogTrace(_T("fullscreen"),
457 _T("detected _NET_WM_STATE_FULLSCREEN support"));
458 return wxX11_FS_WMSPEC
;
461 // if the user is running KDE's kwin WM, use a legacy hack because
462 // kwin doesn't understand any other method:
463 if (wxKwinRunning(disp
, root
))
465 wxLogTrace(_T("fullscreen"), _T("detected kwin"));
469 // finally, fall back to ICCCM heuristic method:
470 wxLogTrace(_T("fullscreen"), _T("unknown WM, using _WIN_LAYER"));
471 return wxX11_FS_GENERIC
;
475 void wxSetFullScreenStateX11(WXDisplay
* display
, WXWindow rootWindow
,
476 WXWindow window
, bool show
,
478 wxX11FullScreenMethod method
)
480 // NB: please see the comment under "Fullscreen mode:" title above
481 // for implications of changing this code.
483 Window wnd
= (Window
)window
;
484 Window root
= (Window
)rootWindow
;
485 Display
*disp
= (Display
*)display
;
487 if (method
== wxX11_FS_AUTODETECT
)
488 method
= wxGetFullScreenMethodX11(display
, rootWindow
);
492 case wxX11_FS_WMSPEC
:
493 wxWMspecSetFullscreen(disp
, root
, wnd
, show
);
496 wxSetKDEFullscreen(disp
, root
, wnd
, show
, origRect
);
499 wxWinHintsSetLayer(disp
, root
, wnd
,
500 show
? WIN_LAYER_ABOVE_DOCK
: WIN_LAYER_NORMAL
);