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
);
83 int (*m_old
)(Display
*, XErrorEvent
*);
84 static int handler(Display
*, XErrorEvent
*) { return 0; }
89 // ----------------------------------------------------------------------------
90 // Setting icons for window manager:
91 // ----------------------------------------------------------------------------
93 void wxSetIconsX11( WXDisplay
* display
, WXWindow window
,
94 const wxIconBundle
& ib
)
98 size_t i
, max
= ib
.m_icons
.GetCount();
100 for( i
= 0; i
< max
; ++i
)
101 if( ib
.m_icons
[i
].Ok() )
102 size
+= 2 + ib
.m_icons
[i
].GetWidth() * ib
.m_icons
[i
].GetHeight();
104 wxMAKE_ATOM(_NET_WM_ICON
, (Display
*)display
);
108 // The code below is correct for 64-bit machines also.
109 // wxUint32* data = new wxUint32[size];
110 // wxUint32* ptr = data;
111 unsigned long* data
= new unsigned long[size
];
112 unsigned long* ptr
= data
;
114 for( i
= 0; i
< max
; ++i
)
116 const wxImage image
= ib
.m_icons
[i
].ConvertToImage();
117 int width
= image
.GetWidth(), height
= image
.GetHeight();
118 unsigned char* imageData
= image
.GetData();
119 unsigned char* imageDataEnd
= imageData
+ ( width
* height
* 3 );
120 bool hasMask
= image
.HasMask();
121 unsigned char rMask
, gMask
, bMask
;
122 unsigned char r
, g
, b
, a
;
126 rMask
= image
.GetMaskRed();
127 gMask
= image
.GetMaskGreen();
128 bMask
= image
.GetMaskBlue();
130 else // no mask, but still init the variables to avoid warnings
140 while( imageData
< imageDataEnd
) {
144 if( hasMask
&& r
== rMask
&& g
== gMask
&& b
== bMask
)
149 *ptr
++ = ( a
<< 24 ) | ( r
<< 16 ) | ( g
<< 8 ) | b
;
155 XChangeProperty( (Display
*)display
,
160 (unsigned char*)data
, size
);
165 XDeleteProperty( (Display
*)display
,
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
177 // NB: Setting fullscreen mode under X11 is a complicated matter. There was
178 // no standard way of doing it until recently. ICCCM doesn't know the
179 // concept of fullscreen windows and the only way to make a window
180 // fullscreen is to remove decorations, resize it to cover entire screen
181 // and set WIN_LAYER_ABOVE_DOCK.
183 // This doesn't always work, though. Specifically, at least kwin from
184 // KDE 3 ignores the hint. The only way to make kwin accept our request
185 // is to emulate the way Qt does it. That is, unmap the window, set
186 // _NET_WM_WINDOW_TYPE to _KDE_NET_WM_WINDOW_TYPE_OVERRIDE (KDE extension),
187 // add _NET_WM_STATE_STAYS_ON_TOP (ditto) to _NET_WM_STATE and map
190 // Version 1.2 of Window Manager Specification (aka wm-spec aka
191 // Extended Window Manager Hints) introduced _NET_WM_STATE_FULLSCREEN
192 // window state which provides cleanest and simplest possible way of
193 // making a window fullscreen. WM-spec is a de-facto standard adopted
194 // by GNOME and KDE folks, but _NET_WM_STATE_FULLSCREEN isn't yet widely
195 // supported. As of January 2003, only GNOME 2's default WM Metacity
196 // implements, KDE will support it from version 3.2. At toolkits level,
197 // GTK+ >= 2.1.2 uses it as the only method of making windows fullscreen
198 // (that's why wxGTK will *not* switch to using gtk_window_fullscreen
199 // unless it has better compatiblity with older WMs).
202 // This is what wxWindows does in wxSetFullScreenStateX11:
203 // 1) if _NET_WM_STATE_FULLSCREEN is supported, use it
204 // 2) otherwise try WM-specific hacks (KDE, IceWM)
205 // 3) use _WIN_LAYER and hope that the WM will recognize it
206 // The code was tested with:
207 // twm, IceWM, WindowMaker, Metacity, kwin, sawfish, lesstif-mwm
210 #define WIN_LAYER_NORMAL 4
211 #define WIN_LAYER_ABOVE_DOCK 10
213 static void wxWinHintsSetLayer(Display
*display
, Window rootWnd
,
214 Window window
, int layer
)
216 wxX11ErrorsSuspender
noerrors(display
);
220 wxMAKE_ATOM( _WIN_LAYER
, display
);
222 if (IsMapped(display
, window
))
224 xev
.type
= ClientMessage
;
225 xev
.xclient
.type
= ClientMessage
;
226 xev
.xclient
.window
= window
;
227 xev
.xclient
.message_type
= _WIN_LAYER
;
228 xev
.xclient
.format
= 32;
229 xev
.xclient
.data
.l
[0] = (long)layer
;
230 xev
.xclient
.data
.l
[1] = CurrentTime
;
232 XSendEvent(display
, rootWnd
, False
,
233 SubstructureNotifyMask
, (XEvent
*) &xev
);
240 XChangeProperty(display
, window
,
241 _WIN_LAYER
, XA_CARDINAL
, 32,
242 PropModeReplace
, (unsigned char *)data
, 1);
249 static bool wxQueryWMspecSupport(Display
* WXUNUSED(display
),
250 Window
WXUNUSED(rootWnd
),
253 GdkAtom gatom
= gdk_x11_xatom_to_atom(feature
);
254 return gdk_net_wm_supports(gatom
);
257 static bool wxQueryWMspecSupport(Display
*display
, Window rootWnd
, Atom feature
)
259 wxMAKE_ATOM(_NET_SUPPORTING_WM_CHECK
, display
);
260 wxMAKE_ATOM(_NET_SUPPORTED
, display
);
262 // FIXME: We may want to cache these checks. Note that we can't simply
263 // remember the results in global variable because the WM may go
264 // away and be replaced by another one! One possible approach
265 // would be invalidate the case every 15 seconds or so. Since this
266 // code is currently only used by wxTopLevelWindow::ShowFullScreen,
267 // it is not important that it is not optimized.
269 // If the WM supports ICCCM (i.e. the root window has
270 // _NET_SUPPORTING_WM_CHECK property that points to a WM-owned
271 // window), we could watch for DestroyNotify event on the window
272 // and invalidate our cache when the windows goes away (= WM
273 // is replaced by another one). This is what GTK+ 2 does.
274 // Let's do it only if it is needed, it requires changes to
282 unsigned long nwins
, natoms
;
284 // Is the WM ICCCM supporting?
285 XGetWindowProperty(display
, rootWnd
,
286 _NET_SUPPORTING_WM_CHECK
, 0, LONG_MAX
,
287 False
, XA_WINDOW
, &type
, &format
, &nwins
,
288 &after
, (unsigned char **)&wins
);
289 if ( type
!= XA_WINDOW
|| nwins
<= 0 || wins
[0] == None
)
293 // Query for supported features:
294 XGetWindowProperty(display
, rootWnd
,
295 _NET_SUPPORTED
, 0, LONG_MAX
,
296 False
, XA_ATOM
, &type
, &format
, &natoms
,
297 &after
, (unsigned char **)&atoms
);
298 if ( type
!= XA_ATOM
|| atoms
== NULL
)
301 // Lookup the feature we want:
302 for (unsigned i
= 0; i
< natoms
; i
++)
304 if ( atoms
[i
] == feature
)
316 #define _NET_WM_STATE_REMOVE 0
317 #define _NET_WM_STATE_ADD 1
319 static void wxWMspecSetState(Display
*display
, Window rootWnd
,
320 Window window
, int operation
, Atom state
)
322 wxMAKE_ATOM(_NET_WM_STATE
, display
);
324 if ( IsMapped(display
, window
) )
327 xev
.type
= ClientMessage
;
328 xev
.xclient
.type
= ClientMessage
;
329 xev
.xclient
.serial
= 0;
330 xev
.xclient
.send_event
= True
;
331 xev
.xclient
.display
= display
;
332 xev
.xclient
.window
= window
;
333 xev
.xclient
.message_type
= _NET_WM_STATE
;
334 xev
.xclient
.format
= 32;
335 xev
.xclient
.data
.l
[0] = operation
;
336 xev
.xclient
.data
.l
[1] = state
;
337 xev
.xclient
.data
.l
[2] = None
;
339 XSendEvent(display
, rootWnd
,
341 SubstructureRedirectMask
| SubstructureNotifyMask
,
344 // FIXME - must modify _NET_WM_STATE property list if the window
348 static void wxWMspecSetFullscreen(Display
*display
, Window rootWnd
,
349 Window window
, bool fullscreen
)
351 wxMAKE_ATOM(_NET_WM_STATE_FULLSCREEN
, display
);
352 wxWMspecSetState(display
, rootWnd
,
354 fullscreen
? _NET_WM_STATE_ADD
: _NET_WM_STATE_REMOVE
,
355 _NET_WM_STATE_FULLSCREEN
);
359 // Is the user running KDE's kwin window manager? At least kwin from KDE 3
360 // sets KWIN_RUNNING property on the root window.
361 static bool wxKwinRunning(Display
*display
, Window rootWnd
)
363 wxMAKE_ATOM(KWIN_RUNNING
, display
);
368 unsigned long nitems
, after
;
369 if (XGetWindowProperty(display
, rootWnd
,
370 KWIN_RUNNING
, 0, 1, False
, KWIN_RUNNING
,
371 &type
, &format
, &nitems
, &after
,
372 (unsigned char**)&data
) != Success
)
377 bool retval
= (type
== KWIN_RUNNING
&&
378 nitems
== 1 && data
&& data
[0] == 1);
383 // KDE's kwin is Qt-centric so much than no normal method of fullscreen
384 // mode will work with it. We have to carefully emulate the Qt way.
385 static void wxSetKDEFullscreen(Display
*display
, Window rootWnd
,
386 Window w
, bool fullscreen
, wxRect
*origRect
)
391 wxMAKE_ATOM(_NET_WM_WINDOW_TYPE
, display
);
392 wxMAKE_ATOM(_NET_WM_WINDOW_TYPE_NORMAL
, display
);
393 wxMAKE_ATOM(_KDE_NET_WM_WINDOW_TYPE_OVERRIDE
, display
);
394 wxMAKE_ATOM(_NET_WM_STATE_STAYS_ON_TOP
, display
);
398 data
[0] = _KDE_NET_WM_WINDOW_TYPE_OVERRIDE
;
399 data
[1] = _NET_WM_WINDOW_TYPE_NORMAL
;
404 data
[0] = _NET_WM_WINDOW_TYPE_NORMAL
;
409 // it is neccessary to unmap the window, otherwise kwin will ignore us:
410 XSync(display
, False
);
412 bool wasMapped
= IsMapped(display
, w
);
415 XUnmapWindow(display
, w
);
416 XSync(display
, False
);
419 XChangeProperty(display
, w
, _NET_WM_WINDOW_TYPE
, XA_ATOM
, 32,
420 PropModeReplace
, (unsigned char *) &data
, lng
);
421 XSync(display
, False
);
425 XMapRaised(display
, w
);
426 XSync(display
, False
);
429 wxWMspecSetState(display
, rootWnd
, w
,
430 fullscreen
? _NET_WM_STATE_ADD
: _NET_WM_STATE_REMOVE
,
431 _NET_WM_STATE_STAYS_ON_TOP
);
432 XSync(display
, False
);
436 // NB: like many other WMs, kwin ignores first request for window
437 // position change after the window was mapped. This additional
438 // move+resize event will ensure that the window is restored in
439 // exactly same position as before it was made fullscreen (because
440 // wxTopLevelWindow::ShowFullScreen will call SetSize, thus
441 // setting the position for second time).
442 XMoveResizeWindow(display
, w
,
443 origRect
->x
, origRect
->y
,
444 origRect
->width
, origRect
->height
);
445 XSync(display
, False
);
450 wxX11FullScreenMethod
wxGetFullScreenMethodX11(WXDisplay
* display
,
453 Window root
= (Window
)rootWindow
;
454 Display
*disp
= (Display
*)display
;
456 // if WM supports _NET_WM_STATE_FULLSCREEN from wm-spec 1.2, use it:
457 wxMAKE_ATOM(_NET_WM_STATE_FULLSCREEN
, disp
);
458 if (wxQueryWMspecSupport(disp
, root
, _NET_WM_STATE_FULLSCREEN
))
460 wxLogTrace(_T("fullscreen"),
461 _T("detected _NET_WM_STATE_FULLSCREEN support"));
462 return wxX11_FS_WMSPEC
;
465 // if the user is running KDE's kwin WM, use a legacy hack because
466 // kwin doesn't understand any other method:
467 if (wxKwinRunning(disp
, root
))
469 wxLogTrace(_T("fullscreen"), _T("detected kwin"));
473 // finally, fall back to ICCCM heuristic method:
474 wxLogTrace(_T("fullscreen"), _T("unknown WM, using _WIN_LAYER"));
475 return wxX11_FS_GENERIC
;
479 void wxSetFullScreenStateX11(WXDisplay
* display
, WXWindow rootWindow
,
480 WXWindow window
, bool show
,
482 wxX11FullScreenMethod method
)
484 // NB: please see the comment under "Fullscreen mode:" title above
485 // for implications of changing this code.
487 Window wnd
= (Window
)window
;
488 Window root
= (Window
)rootWindow
;
489 Display
*disp
= (Display
*)display
;
491 if (method
== wxX11_FS_AUTODETECT
)
492 method
= wxGetFullScreenMethodX11(display
, rootWindow
);
496 case wxX11_FS_WMSPEC
:
497 wxWMspecSetFullscreen(disp
, root
, wnd
, show
);
500 wxSetKDEFullscreen(disp
, root
, wnd
, show
, origRect
);
503 wxWinHintsSetLayer(disp
, root
, wnd
,
504 show
? WIN_LAYER_ABOVE_DOCK
: WIN_LAYER_NORMAL
);