]> git.saurik.com Git - wxWidgets.git/blame - src/unix/utilsx11.cpp
build fix for wxUSE_LOG==0
[wxWidgets.git] / src / unix / utilsx11.cpp
CommitLineData
f618020a
MB
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/unix/utilsx11.cpp
7e86b10b 3// Purpose: Miscellaneous X11 functions (for wxCore)
8166ab43 4// Author: Mattia Barbon, Vaclav Slavik, Robert Roebling
f618020a
MB
5// Modified by:
6// Created: 25.03.02
7// RCS-ID: $Id$
77ffb593 8// Copyright: (c) wxWidgets team
65571936 9// Licence: wxWindows licence
f618020a
MB
10/////////////////////////////////////////////////////////////////////////////
11
12#if defined(__WXX11__) || defined(__WXGTK__) || defined(__WXMOTIF__)
13
14f355c2
VS
14// for compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
f618020a 17#include "wx/unix/utilsx11.h"
e4db172a
WS
18
19#ifndef WX_PRECOMP
20 #include "wx/log.h"
670f9935 21 #include "wx/app.h"
923d28da 22 #include "wx/icon.h"
155ecd4c 23 #include "wx/image.h"
e4db172a
WS
24#endif
25
f618020a 26#include "wx/iconbndl.h"
50dd85fa 27#include "wx/apptrait.h"
f618020a 28
130d96dc
JJ
29#ifdef __VMS
30#pragma message disable nosimpint
31#endif
f618020a
MB
32#include <X11/Xlib.h>
33#include <X11/Xatom.h>
0c9d7ace 34#include <X11/Xutil.h>
130d96dc
JJ
35#ifdef __VMS
36#pragma message enable nosimpint
37#endif
f618020a 38
06c093e8 39#ifdef __WXGTK__
8166ab43
VS
40#include <gdk/gdk.h>
41#include <gdk/gdkx.h>
42#endif
43
44// Various X11 Atoms used in this file:
8166ab43
VS
45static Atom _NET_WM_STATE = 0;
46static Atom _NET_WM_STATE_FULLSCREEN = 0;
47static Atom _NET_WM_STATE_STAYS_ON_TOP = 0;
48static Atom _NET_WM_WINDOW_TYPE = 0;
49static Atom _NET_WM_WINDOW_TYPE_NORMAL = 0;
50static Atom _KDE_NET_WM_WINDOW_TYPE_OVERRIDE = 0;
51static Atom _WIN_LAYER = 0;
52static Atom KWIN_RUNNING = 0;
53#ifndef __WXGTK20__
54static Atom _NET_SUPPORTING_WM_CHECK = 0;
55static Atom _NET_SUPPORTED = 0;
56#endif
57
58#define wxMAKE_ATOM(name, display) \
59 if (name == 0) name = XInternAtom((display), #name, False)
60
61
17a1ebd1
VZ
62// X11 Window is an int type, so use the macro to suppress warnings when
63// converting to it
64#define WindowCast(w) (Window)(wxPtrToUInt(w))
65
8166ab43
VS
66// Is the window mapped?
67static bool IsMapped(Display *display, Window window)
68{
69 XWindowAttributes attr;
70 XGetWindowAttributes(display, window, &attr);
71 return (attr.map_state != IsUnmapped);
72}
73
74
75
76// Suspends X11 errors. Used when we expect errors but they are not fatal
77// for us.
fc9b078c
VZ
78extern "C"
79{
80 typedef int (*wxX11ErrorHandler)(Display *, XErrorEvent *);
81
865bb325
VZ
82 static int wxX11ErrorsSuspender_handler(Display*, XErrorEvent*) { return 0; }
83}
fc9b078c 84
8166ab43
VS
85class wxX11ErrorsSuspender
86{
87public:
88 wxX11ErrorsSuspender(Display *d) : m_display(d)
89 {
865bb325 90 m_old = XSetErrorHandler(wxX11ErrorsSuspender_handler);
8166ab43
VS
91 }
92 ~wxX11ErrorsSuspender()
93 {
94 XFlush(m_display);
95 XSetErrorHandler(m_old);
96 }
6ed892f3 97
8166ab43
VS
98private:
99 Display *m_display;
fc9b078c 100 wxX11ErrorHandler m_old;
8166ab43
VS
101};
102
103
104
105// ----------------------------------------------------------------------------
106// Setting icons for window manager:
107// ----------------------------------------------------------------------------
108
3c029873
VZ
109#if wxUSE_IMAGE && !wxUSE_NANOX
110
111static Atom _NET_WM_ICON = 0;
112
52734360
VZ
113void
114wxSetIconsX11(WXDisplay* display, WXWindow window, const wxIconBundle& ib)
f618020a
MB
115{
116 size_t size = 0;
f618020a 117
52734360
VZ
118 const size_t numIcons = ib.GetIconCount();
119 for ( size_t i = 0; i < numIcons; ++i )
120 {
121 const wxIcon icon = ib.GetIconByIndex(i);
122
123 size += 2 + icon.GetWidth() * icon.GetHeight();
124 }
f618020a 125
8166ab43 126 wxMAKE_ATOM(_NET_WM_ICON, (Display*)display);
f618020a 127
52734360 128 if ( size > 0 )
f618020a 129 {
23c98a6e
RR
130 unsigned long* data = new unsigned long[size];
131 unsigned long* ptr = data;
f618020a 132
52734360 133 for ( size_t i = 0; i < numIcons; ++i )
f618020a 134 {
52734360
VZ
135 const wxImage image = ib.GetIconByIndex(i).ConvertToImage();
136 int width = image.GetWidth(),
137 height = image.GetHeight();
f618020a
MB
138 unsigned char* imageData = image.GetData();
139 unsigned char* imageDataEnd = imageData + ( width * height * 3 );
140 bool hasMask = image.HasMask();
141 unsigned char rMask, gMask, bMask;
142 unsigned char r, g, b, a;
143
144 if( hasMask )
145 {
146 rMask = image.GetMaskRed();
147 gMask = image.GetMaskGreen();
148 bMask = image.GetMaskBlue();
149 }
0fc5dbf5
VZ
150 else // no mask, but still init the variables to avoid warnings
151 {
152 rMask =
153 gMask =
154 bMask = 0;
155 }
f618020a
MB
156
157 *ptr++ = width;
158 *ptr++ = height;
159
52734360
VZ
160 while ( imageData < imageDataEnd )
161 {
f618020a
MB
162 r = imageData[0];
163 g = imageData[1];
164 b = imageData[2];
165 if( hasMask && r == rMask && g == gMask && b == bMask )
166 a = 0;
167 else
168 a = 255;
169
170 *ptr++ = ( a << 24 ) | ( r << 16 ) | ( g << 8 ) | b;
171
172 imageData += 3;
173 }
174 }
175
176 XChangeProperty( (Display*)display,
17a1ebd1 177 WindowCast(window),
8166ab43 178 _NET_WM_ICON,
f618020a
MB
179 XA_CARDINAL, 32,
180 PropModeReplace,
181 (unsigned char*)data, size );
182 delete[] data;
183 }
184 else
185 {
186 XDeleteProperty( (Display*)display,
17a1ebd1 187 WindowCast(window),
8166ab43 188 _NET_WM_ICON );
f618020a
MB
189 }
190}
191
3c029873 192#endif // wxUSE_IMAGE && !wxUSE_NANOX
8166ab43
VS
193
194// ----------------------------------------------------------------------------
195// Fullscreen mode:
196// ----------------------------------------------------------------------------
197
198// NB: Setting fullscreen mode under X11 is a complicated matter. There was
199// no standard way of doing it until recently. ICCCM doesn't know the
200// concept of fullscreen windows and the only way to make a window
201// fullscreen is to remove decorations, resize it to cover entire screen
202// and set WIN_LAYER_ABOVE_DOCK.
203//
17a1ebd1 204// This doesn't always work, though. Specifically, at least kwin from
8166ab43
VS
205// KDE 3 ignores the hint. The only way to make kwin accept our request
206// is to emulate the way Qt does it. That is, unmap the window, set
207// _NET_WM_WINDOW_TYPE to _KDE_NET_WM_WINDOW_TYPE_OVERRIDE (KDE extension),
208// add _NET_WM_STATE_STAYS_ON_TOP (ditto) to _NET_WM_STATE and map
209// the window again.
210//
211// Version 1.2 of Window Manager Specification (aka wm-spec aka
212// Extended Window Manager Hints) introduced _NET_WM_STATE_FULLSCREEN
213// window state which provides cleanest and simplest possible way of
214// making a window fullscreen. WM-spec is a de-facto standard adopted
215// by GNOME and KDE folks, but _NET_WM_STATE_FULLSCREEN isn't yet widely
17a1ebd1 216// supported. As of January 2003, only GNOME 2's default WM Metacity
8166ab43
VS
217// implements, KDE will support it from version 3.2. At toolkits level,
218// GTK+ >= 2.1.2 uses it as the only method of making windows fullscreen
219// (that's why wxGTK will *not* switch to using gtk_window_fullscreen
d6922577 220// unless it has better compatibility with older WMs).
8166ab43 221//
17a1ebd1 222//
77ffb593 223// This is what wxWidgets does in wxSetFullScreenStateX11:
8166ab43
VS
224// 1) if _NET_WM_STATE_FULLSCREEN is supported, use it
225// 2) otherwise try WM-specific hacks (KDE, IceWM)
226// 3) use _WIN_LAYER and hope that the WM will recognize it
227// The code was tested with:
228// twm, IceWM, WindowMaker, Metacity, kwin, sawfish, lesstif-mwm
229
230
231#define WIN_LAYER_NORMAL 4
232#define WIN_LAYER_ABOVE_DOCK 10
233
234static void wxWinHintsSetLayer(Display *display, Window rootWnd,
235 Window window, int layer)
236{
237 wxX11ErrorsSuspender noerrors(display);
17a1ebd1 238
8166ab43
VS
239 XEvent xev;
240
241 wxMAKE_ATOM( _WIN_LAYER, display );
242
243 if (IsMapped(display, window))
244 {
245 xev.type = ClientMessage;
246 xev.xclient.type = ClientMessage;
247 xev.xclient.window = window;
248 xev.xclient.message_type = _WIN_LAYER;
249 xev.xclient.format = 32;
250 xev.xclient.data.l[0] = (long)layer;
251 xev.xclient.data.l[1] = CurrentTime;
252
253 XSendEvent(display, rootWnd, False,
254 SubstructureNotifyMask, (XEvent*) &xev);
255 }
256 else
257 {
258 long data[1];
259
260 data[0] = layer;
261 XChangeProperty(display, window,
262 _WIN_LAYER, XA_CARDINAL, 32,
263 PropModeReplace, (unsigned char *)data, 1);
264 }
265}
266
267
268
269#ifdef __WXGTK20__
270static bool wxQueryWMspecSupport(Display* WXUNUSED(display),
271 Window WXUNUSED(rootWnd),
272 Atom (feature))
273{
274 GdkAtom gatom = gdk_x11_xatom_to_atom(feature);
275 return gdk_net_wm_supports(gatom);
276}
277#else
278static bool wxQueryWMspecSupport(Display *display, Window rootWnd, Atom feature)
279{
280 wxMAKE_ATOM(_NET_SUPPORTING_WM_CHECK, display);
281 wxMAKE_ATOM(_NET_SUPPORTED, display);
17a1ebd1 282
8166ab43
VS
283 // FIXME: We may want to cache these checks. Note that we can't simply
284 // remember the results in global variable because the WM may go
285 // away and be replaced by another one! One possible approach
286 // would be invalidate the case every 15 seconds or so. Since this
287 // code is currently only used by wxTopLevelWindow::ShowFullScreen,
288 // it is not important that it is not optimized.
289 //
290 // If the WM supports ICCCM (i.e. the root window has
291 // _NET_SUPPORTING_WM_CHECK property that points to a WM-owned
292 // window), we could watch for DestroyNotify event on the window
293 // and invalidate our cache when the windows goes away (= WM
294 // is replaced by another one). This is what GTK+ 2 does.
295 // Let's do it only if it is needed, it requires changes to
296 // the event loop.
17a1ebd1 297
8166ab43
VS
298 Atom type;
299 Window *wins;
300 Atom *atoms;
301 int format;
302 unsigned long after;
303 unsigned long nwins, natoms;
17a1ebd1 304
8166ab43
VS
305 // Is the WM ICCCM supporting?
306 XGetWindowProperty(display, rootWnd,
5a2db5f1 307 _NET_SUPPORTING_WM_CHECK, 0, LONG_MAX,
8166ab43
VS
308 False, XA_WINDOW, &type, &format, &nwins,
309 &after, (unsigned char **)&wins);
310 if ( type != XA_WINDOW || nwins <= 0 || wins[0] == None )
55034339 311 return false;
8166ab43
VS
312 XFree(wins);
313
314 // Query for supported features:
315 XGetWindowProperty(display, rootWnd,
7f891ef1 316 _NET_SUPPORTED, 0, LONG_MAX,
8166ab43
VS
317 False, XA_ATOM, &type, &format, &natoms,
318 &after, (unsigned char **)&atoms);
319 if ( type != XA_ATOM || atoms == NULL )
55034339 320 return false;
8166ab43
VS
321
322 // Lookup the feature we want:
323 for (unsigned i = 0; i < natoms; i++)
324 {
325 if ( atoms[i] == feature )
326 {
327 XFree(atoms);
55034339 328 return true;
8166ab43
VS
329 }
330 }
331 XFree(atoms);
55034339 332 return false;
8166ab43 333}
f618020a 334#endif
8166ab43
VS
335
336
337#define _NET_WM_STATE_REMOVE 0
338#define _NET_WM_STATE_ADD 1
339
340static void wxWMspecSetState(Display *display, Window rootWnd,
341 Window window, int operation, Atom state)
342{
343 wxMAKE_ATOM(_NET_WM_STATE, display);
17a1ebd1 344
8166ab43
VS
345 if ( IsMapped(display, window) )
346 {
347 XEvent xev;
348 xev.type = ClientMessage;
349 xev.xclient.type = ClientMessage;
350 xev.xclient.serial = 0;
351 xev.xclient.send_event = True;
352 xev.xclient.display = display;
353 xev.xclient.window = window;
354 xev.xclient.message_type = _NET_WM_STATE;
355 xev.xclient.format = 32;
356 xev.xclient.data.l[0] = operation;
357 xev.xclient.data.l[1] = state;
358 xev.xclient.data.l[2] = None;
17a1ebd1 359
8166ab43
VS
360 XSendEvent(display, rootWnd,
361 False,
362 SubstructureRedirectMask | SubstructureNotifyMask,
363 &xev);
364 }
365 // FIXME - must modify _NET_WM_STATE property list if the window
366 // wasn't mapped!
367}
368
369static void wxWMspecSetFullscreen(Display *display, Window rootWnd,
370 Window window, bool fullscreen)
371{
372 wxMAKE_ATOM(_NET_WM_STATE_FULLSCREEN, display);
373 wxWMspecSetState(display, rootWnd,
374 window,
375 fullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE,
376 _NET_WM_STATE_FULLSCREEN);
377}
378
379
380// Is the user running KDE's kwin window manager? At least kwin from KDE 3
381// sets KWIN_RUNNING property on the root window.
382static bool wxKwinRunning(Display *display, Window rootWnd)
383{
384 wxMAKE_ATOM(KWIN_RUNNING, display);
17a1ebd1 385
e8769ed1 386 unsigned char* data;
8166ab43
VS
387 Atom type;
388 int format;
389 unsigned long nitems, after;
390 if (XGetWindowProperty(display, rootWnd,
391 KWIN_RUNNING, 0, 1, False, KWIN_RUNNING,
392 &type, &format, &nitems, &after,
e8769ed1 393 &data) != Success)
8166ab43 394 {
55034339 395 return false;
8166ab43
VS
396 }
397
398 bool retval = (type == KWIN_RUNNING &&
e8769ed1 399 nitems == 1 && data && ((long*)data)[0] == 1);
8166ab43
VS
400 XFree(data);
401 return retval;
402}
403
17a1ebd1 404// KDE's kwin is Qt-centric so much than no normal method of fullscreen
8166ab43
VS
405// mode will work with it. We have to carefully emulate the Qt way.
406static void wxSetKDEFullscreen(Display *display, Window rootWnd,
407 Window w, bool fullscreen, wxRect *origRect)
408{
409 long data[2];
410 unsigned lng;
411
412 wxMAKE_ATOM(_NET_WM_WINDOW_TYPE, display);
413 wxMAKE_ATOM(_NET_WM_WINDOW_TYPE_NORMAL, display);
414 wxMAKE_ATOM(_KDE_NET_WM_WINDOW_TYPE_OVERRIDE, display);
415 wxMAKE_ATOM(_NET_WM_STATE_STAYS_ON_TOP, display);
416
417 if (fullscreen)
418 {
419 data[0] = _KDE_NET_WM_WINDOW_TYPE_OVERRIDE;
420 data[1] = _NET_WM_WINDOW_TYPE_NORMAL;
421 lng = 2;
422 }
423 else
424 {
425 data[0] = _NET_WM_WINDOW_TYPE_NORMAL;
426 data[1] = None;
427 lng = 1;
428 }
429
d6922577 430 // it is necessary to unmap the window, otherwise kwin will ignore us:
1cff04de 431 XSync(display, False);
17a1ebd1 432
8166ab43
VS
433 bool wasMapped = IsMapped(display, w);
434 if (wasMapped)
1cff04de 435 {
8166ab43 436 XUnmapWindow(display, w);
1cff04de
VS
437 XSync(display, False);
438 }
439
8166ab43 440 XChangeProperty(display, w, _NET_WM_WINDOW_TYPE, XA_ATOM, 32,
55034339 441 PropModeReplace, (unsigned char *) &data[0], lng);
1cff04de
VS
442 XSync(display, False);
443
8166ab43 444 if (wasMapped)
1cff04de 445 {
8166ab43 446 XMapRaised(display, w);
1cff04de
VS
447 XSync(display, False);
448 }
17a1ebd1
VZ
449
450 wxWMspecSetState(display, rootWnd, w,
8166ab43
VS
451 fullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE,
452 _NET_WM_STATE_STAYS_ON_TOP);
1cff04de 453 XSync(display, False);
8166ab43
VS
454
455 if (!fullscreen)
456 {
d6922577 457 // NB: like many other WMs, kwin ignores the first request for a window
8166ab43
VS
458 // position change after the window was mapped. This additional
459 // move+resize event will ensure that the window is restored in
17a1ebd1
VZ
460 // exactly the same position as before it was made fullscreen
461 // (because wxTopLevelWindow::ShowFullScreen will call SetSize, thus
d6922577 462 // setting the position for the second time).
8166ab43
VS
463 XMoveResizeWindow(display, w,
464 origRect->x, origRect->y,
465 origRect->width, origRect->height);
1cff04de 466 XSync(display, False);
8166ab43
VS
467 }
468}
469
470
471wxX11FullScreenMethod wxGetFullScreenMethodX11(WXDisplay* display,
472 WXWindow rootWindow)
473{
17a1ebd1 474 Window root = WindowCast(rootWindow);
8166ab43
VS
475 Display *disp = (Display*)display;
476
477 // if WM supports _NET_WM_STATE_FULLSCREEN from wm-spec 1.2, use it:
478 wxMAKE_ATOM(_NET_WM_STATE_FULLSCREEN, disp);
479 if (wxQueryWMspecSupport(disp, root, _NET_WM_STATE_FULLSCREEN))
480 {
9a83f860
VZ
481 wxLogTrace(wxT("fullscreen"),
482 wxT("detected _NET_WM_STATE_FULLSCREEN support"));
8166ab43
VS
483 return wxX11_FS_WMSPEC;
484 }
17a1ebd1 485
8166ab43
VS
486 // if the user is running KDE's kwin WM, use a legacy hack because
487 // kwin doesn't understand any other method:
488 if (wxKwinRunning(disp, root))
489 {
9a83f860 490 wxLogTrace(wxT("fullscreen"), wxT("detected kwin"));
8166ab43
VS
491 return wxX11_FS_KDE;
492 }
17a1ebd1 493
8166ab43 494 // finally, fall back to ICCCM heuristic method:
9a83f860 495 wxLogTrace(wxT("fullscreen"), wxT("unknown WM, using _WIN_LAYER"));
8166ab43
VS
496 return wxX11_FS_GENERIC;
497}
498
499
500void wxSetFullScreenStateX11(WXDisplay* display, WXWindow rootWindow,
501 WXWindow window, bool show,
502 wxRect *origRect,
503 wxX11FullScreenMethod method)
504{
505 // NB: please see the comment under "Fullscreen mode:" title above
506 // for implications of changing this code.
507
17a1ebd1
VZ
508 Window wnd = WindowCast(window);
509 Window root = WindowCast(rootWindow);
8166ab43
VS
510 Display *disp = (Display*)display;
511
512 if (method == wxX11_FS_AUTODETECT)
513 method = wxGetFullScreenMethodX11(display, rootWindow);
514
515 switch (method)
516 {
517 case wxX11_FS_WMSPEC:
518 wxWMspecSetFullscreen(disp, root, wnd, show);
519 break;
520 case wxX11_FS_KDE:
521 wxSetKDEFullscreen(disp, root, wnd, show, origRect);
522 break;
523 default:
524 wxWinHintsSetLayer(disp, root, wnd,
525 show ? WIN_LAYER_ABOVE_DOCK : WIN_LAYER_NORMAL);
526 break;
527 }
528}
529
97698dc4
RD
530
531
532// ----------------------------------------------------------------------------
533// keycode translations
534// ----------------------------------------------------------------------------
535
536#include <X11/keysym.h>
537
538// FIXME what about tables??
539
571d991b 540int wxCharCodeXToWX(WXKeySym keySym)
97698dc4
RD
541{
542 int id;
543 switch (keySym)
544 {
545 case XK_Shift_L:
546 case XK_Shift_R:
547 id = WXK_SHIFT; break;
548 case XK_Control_L:
549 case XK_Control_R:
550 id = WXK_CONTROL; break;
17a1ebd1 551 case XK_Meta_L:
97698dc4
RD
552 case XK_Meta_R:
553 id = WXK_ALT; break;
dd479613
VZ
554 case XK_Caps_Lock:
555 id = WXK_CAPITAL; break;
97698dc4
RD
556 case XK_BackSpace:
557 id = WXK_BACK; break;
558 case XK_Delete:
559 id = WXK_DELETE; break;
560 case XK_Clear:
561 id = WXK_CLEAR; break;
562 case XK_Tab:
563 id = WXK_TAB; break;
564 case XK_numbersign:
565 id = '#'; break;
566 case XK_Return:
567 id = WXK_RETURN; break;
568 case XK_Escape:
569 id = WXK_ESCAPE; break;
570 case XK_Pause:
571 case XK_Break:
572 id = WXK_PAUSE; break;
573 case XK_Num_Lock:
574 id = WXK_NUMLOCK; break;
575 case XK_Scroll_Lock:
576 id = WXK_SCROLL; break;
577
578 case XK_Home:
579 id = WXK_HOME; break;
580 case XK_End:
581 id = WXK_END; break;
582 case XK_Left:
583 id = WXK_LEFT; break;
584 case XK_Right:
585 id = WXK_RIGHT; break;
586 case XK_Up:
587 id = WXK_UP; break;
588 case XK_Down:
589 id = WXK_DOWN; break;
590 case XK_Next:
faa94f3e 591 id = WXK_PAGEDOWN; break;
97698dc4 592 case XK_Prior:
faa94f3e 593 id = WXK_PAGEUP; break;
97698dc4
RD
594 case XK_Menu:
595 id = WXK_MENU; break;
596 case XK_Select:
597 id = WXK_SELECT; break;
598 case XK_Cancel:
599 id = WXK_CANCEL; break;
600 case XK_Print:
601 id = WXK_PRINT; break;
602 case XK_Execute:
603 id = WXK_EXECUTE; break;
604 case XK_Insert:
605 id = WXK_INSERT; break;
606 case XK_Help:
607 id = WXK_HELP; break;
608
609 case XK_KP_Multiply:
dd479613 610 id = WXK_NUMPAD_MULTIPLY; break;
97698dc4 611 case XK_KP_Add:
dd479613 612 id = WXK_NUMPAD_ADD; break;
97698dc4 613 case XK_KP_Subtract:
dd479613 614 id = WXK_NUMPAD_SUBTRACT; break;
97698dc4 615 case XK_KP_Divide:
dd479613 616 id = WXK_NUMPAD_DIVIDE; break;
97698dc4 617 case XK_KP_Decimal:
dd479613 618 id = WXK_NUMPAD_DECIMAL; break;
97698dc4 619 case XK_KP_Equal:
dd479613 620 id = WXK_NUMPAD_EQUAL; break;
97698dc4 621 case XK_KP_Space:
dd479613 622 id = WXK_NUMPAD_SPACE; break;
97698dc4 623 case XK_KP_Tab:
dd479613 624 id = WXK_NUMPAD_TAB; break;
97698dc4 625 case XK_KP_Enter:
dd479613 626 id = WXK_NUMPAD_ENTER; break;
97698dc4
RD
627 case XK_KP_0:
628 id = WXK_NUMPAD0; break;
629 case XK_KP_1:
630 id = WXK_NUMPAD1; break;
631 case XK_KP_2:
632 id = WXK_NUMPAD2; break;
633 case XK_KP_3:
634 id = WXK_NUMPAD3; break;
635 case XK_KP_4:
636 id = WXK_NUMPAD4; break;
637 case XK_KP_5:
638 id = WXK_NUMPAD5; break;
639 case XK_KP_6:
640 id = WXK_NUMPAD6; break;
641 case XK_KP_7:
642 id = WXK_NUMPAD7; break;
643 case XK_KP_8:
644 id = WXK_NUMPAD8; break;
645 case XK_KP_9:
646 id = WXK_NUMPAD9; break;
dd479613
VZ
647 case XK_KP_Insert:
648 id = WXK_NUMPAD_INSERT; break;
649 case XK_KP_End:
650 id = WXK_NUMPAD_END; break;
651 case XK_KP_Down:
652 id = WXK_NUMPAD_DOWN; break;
653 case XK_KP_Page_Down:
654 id = WXK_NUMPAD_PAGEDOWN; break;
655 case XK_KP_Left:
656 id = WXK_NUMPAD_LEFT; break;
657 case XK_KP_Right:
658 id = WXK_NUMPAD_RIGHT; break;
659 case XK_KP_Home:
660 id = WXK_NUMPAD_HOME; break;
661 case XK_KP_Up:
662 id = WXK_NUMPAD_UP; break;
663 case XK_KP_Page_Up:
664 id = WXK_NUMPAD_PAGEUP; break;
97698dc4
RD
665 case XK_F1:
666 id = WXK_F1; break;
667 case XK_F2:
668 id = WXK_F2; break;
669 case XK_F3:
670 id = WXK_F3; break;
671 case XK_F4:
672 id = WXK_F4; break;
673 case XK_F5:
674 id = WXK_F5; break;
675 case XK_F6:
676 id = WXK_F6; break;
677 case XK_F7:
678 id = WXK_F7; break;
679 case XK_F8:
680 id = WXK_F8; break;
681 case XK_F9:
682 id = WXK_F9; break;
683 case XK_F10:
684 id = WXK_F10; break;
685 case XK_F11:
686 id = WXK_F11; break;
687 case XK_F12:
688 id = WXK_F12; break;
689 case XK_F13:
690 id = WXK_F13; break;
691 case XK_F14:
692 id = WXK_F14; break;
693 case XK_F15:
694 id = WXK_F15; break;
695 case XK_F16:
696 id = WXK_F16; break;
697 case XK_F17:
698 id = WXK_F17; break;
699 case XK_F18:
700 id = WXK_F18; break;
701 case XK_F19:
702 id = WXK_F19; break;
703 case XK_F20:
704 id = WXK_F20; break;
705 case XK_F21:
706 id = WXK_F21; break;
707 case XK_F22:
708 id = WXK_F22; break;
709 case XK_F23:
710 id = WXK_F23; break;
711 case XK_F24:
712 id = WXK_F24; break;
713 default:
714 id = (keySym <= 255) ? (int)keySym : -1;
715 }
716
717 return id;
718}
719
571d991b 720WXKeySym wxCharCodeWXToX(int id)
97698dc4 721{
571d991b 722 WXKeySym keySym;
97698dc4
RD
723
724 switch (id)
725 {
726 case WXK_CANCEL: keySym = XK_Cancel; break;
727 case WXK_BACK: keySym = XK_BackSpace; break;
728 case WXK_TAB: keySym = XK_Tab; break;
729 case WXK_CLEAR: keySym = XK_Clear; break;
730 case WXK_RETURN: keySym = XK_Return; break;
731 case WXK_SHIFT: keySym = XK_Shift_L; break;
732 case WXK_CONTROL: keySym = XK_Control_L; break;
17a1ebd1 733 case WXK_ALT: keySym = XK_Meta_L; break;
dd479613 734 case WXK_CAPITAL: keySym = XK_Caps_Lock; break;
97698dc4
RD
735 case WXK_MENU : keySym = XK_Menu; break;
736 case WXK_PAUSE: keySym = XK_Pause; break;
737 case WXK_ESCAPE: keySym = XK_Escape; break;
738 case WXK_SPACE: keySym = ' '; break;
faa94f3e
WS
739 case WXK_PAGEUP: keySym = XK_Prior; break;
740 case WXK_PAGEDOWN: keySym = XK_Next; break;
97698dc4
RD
741 case WXK_END: keySym = XK_End; break;
742 case WXK_HOME : keySym = XK_Home; break;
743 case WXK_LEFT : keySym = XK_Left; break;
744 case WXK_UP: keySym = XK_Up; break;
745 case WXK_RIGHT: keySym = XK_Right; break;
746 case WXK_DOWN : keySym = XK_Down; break;
747 case WXK_SELECT: keySym = XK_Select; break;
748 case WXK_PRINT: keySym = XK_Print; break;
749 case WXK_EXECUTE: keySym = XK_Execute; break;
750 case WXK_INSERT: keySym = XK_Insert; break;
751 case WXK_DELETE: keySym = XK_Delete; break;
752 case WXK_HELP : keySym = XK_Help; break;
dd479613
VZ
753 case WXK_NUMPAD0: keySym = XK_KP_0; break; case WXK_NUMPAD_INSERT: keySym = XK_KP_Insert; break;
754 case WXK_NUMPAD1: keySym = XK_KP_1; break; case WXK_NUMPAD_END: keySym = XK_KP_End; break;
755 case WXK_NUMPAD2: keySym = XK_KP_2; break; case WXK_NUMPAD_DOWN: keySym = XK_KP_Down; break;
756 case WXK_NUMPAD3: keySym = XK_KP_3; break; case WXK_NUMPAD_PAGEDOWN: keySym = XK_KP_Page_Down; break;
757 case WXK_NUMPAD4: keySym = XK_KP_4; break; case WXK_NUMPAD_LEFT: keySym = XK_KP_Left; break;
97698dc4 758 case WXK_NUMPAD5: keySym = XK_KP_5; break;
dd479613
VZ
759 case WXK_NUMPAD6: keySym = XK_KP_6; break; case WXK_NUMPAD_RIGHT: keySym = XK_KP_Right; break;
760 case WXK_NUMPAD7: keySym = XK_KP_7; break; case WXK_NUMPAD_HOME: keySym = XK_KP_Home; break;
761 case WXK_NUMPAD8: keySym = XK_KP_8; break; case WXK_NUMPAD_UP: keySym = XK_KP_Up; break;
762 case WXK_NUMPAD9: keySym = XK_KP_9; break; case WXK_NUMPAD_PAGEUP: keySym = XK_KP_Page_Up; break;
763 case WXK_NUMPAD_DECIMAL: keySym = XK_KP_Decimal; break; case WXK_NUMPAD_DELETE: keySym = XK_KP_Delete; break;
764 case WXK_NUMPAD_MULTIPLY: keySym = XK_KP_Multiply; break;
765 case WXK_NUMPAD_ADD: keySym = XK_KP_Add; break;
766 case WXK_NUMPAD_SUBTRACT: keySym = XK_KP_Subtract; break;
767 case WXK_NUMPAD_DIVIDE: keySym = XK_KP_Divide; break;
768 case WXK_NUMPAD_ENTER: keySym = XK_KP_Enter; break;
769 case WXK_NUMPAD_SEPARATOR: keySym = XK_KP_Separator; break;
97698dc4
RD
770 case WXK_F1: keySym = XK_F1; break;
771 case WXK_F2: keySym = XK_F2; break;
772 case WXK_F3: keySym = XK_F3; break;
773 case WXK_F4: keySym = XK_F4; break;
774 case WXK_F5: keySym = XK_F5; break;
775 case WXK_F6: keySym = XK_F6; break;
776 case WXK_F7: keySym = XK_F7; break;
777 case WXK_F8: keySym = XK_F8; break;
778 case WXK_F9: keySym = XK_F9; break;
779 case WXK_F10: keySym = XK_F10; break;
780 case WXK_F11: keySym = XK_F11; break;
781 case WXK_F12: keySym = XK_F12; break;
782 case WXK_F13: keySym = XK_F13; break;
783 case WXK_F14: keySym = XK_F14; break;
784 case WXK_F15: keySym = XK_F15; break;
785 case WXK_F16: keySym = XK_F16; break;
786 case WXK_F17: keySym = XK_F17; break;
787 case WXK_F18: keySym = XK_F18; break;
788 case WXK_F19: keySym = XK_F19; break;
789 case WXK_F20: keySym = XK_F20; break;
790 case WXK_F21: keySym = XK_F21; break;
791 case WXK_F22: keySym = XK_F22; break;
792 case WXK_F23: keySym = XK_F23; break;
793 case WXK_F24: keySym = XK_F24; break;
794 case WXK_NUMLOCK: keySym = XK_Num_Lock; break;
795 case WXK_SCROLL: keySym = XK_Scroll_Lock; break;
796 default: keySym = id <= 255 ? (KeySym)id : 0;
797 }
798
799 return keySym;
800}
801
802
803// ----------------------------------------------------------------------------
804// check current state of a key
805// ----------------------------------------------------------------------------
806
1751226c 807bool wxGetKeyState(wxKeyCode key)
97698dc4 808{
44353523
VZ
809 wxASSERT_MSG(key != WXK_LBUTTON && key != WXK_RBUTTON && key !=
810 WXK_MBUTTON, wxT("can't use wxGetKeyState() for mouse buttons"));
811
b886fae6 812 Display *pDisplay = (Display*) wxGetDisplay();
17a1ebd1 813
97698dc4
RD
814 int iKey = wxCharCodeWXToX(key);
815 int iKeyMask = 0;
816 Window wDummy1, wDummy2;
817 int iDummy3, iDummy4, iDummy5, iDummy6;
818 unsigned int iMask;
97698dc4
RD
819 KeyCode keyCode = XKeysymToKeycode(pDisplay,iKey);
820 if (keyCode == NoSymbol)
821 return false;
17a1ebd1 822
dd479613 823 if ( IsModifierKey(iKey) ) // If iKey is a modifier key, use a different method
97698dc4 824 {
c12291f3 825 XModifierKeymap *map = XGetModifierMapping(pDisplay);
9a83f860 826 wxCHECK_MSG( map, false, wxT("failed to get X11 modifiers map") );
c12291f3 827
b886fae6 828 for (int i = 0; i < 8; ++i)
97698dc4 829 {
b886fae6
VZ
830 if ( map->modifiermap[map->max_keypermod * i] == keyCode)
831 {
832 iKeyMask = 1 << i;
833 }
97698dc4 834 }
b886fae6
VZ
835
836 XQueryPointer(pDisplay, DefaultRootWindow(pDisplay), &wDummy1, &wDummy2,
837 &iDummy3, &iDummy4, &iDummy5, &iDummy6, &iMask );
838 XFreeModifiermap(map);
839 return (iMask & iKeyMask) != 0;
97698dc4 840 }
17a1ebd1 841
dd479613 842 // From the XLib manual:
c12291f3
VZ
843 // The XQueryKeymap() function returns a bit vector for the logical state of the keyboard,
844 // where each bit set to 1 indicates that the corresponding key is currently pressed down.
845 // The vector is represented as 32 bytes. Byte N (from 0) contains the bits for keys 8N to 8N + 7
dd479613
VZ
846 // with the least-significant bit in the byte representing key 8N.
847 char key_vector[32];
848 XQueryKeymap(pDisplay, key_vector);
c12291f3 849 return key_vector[keyCode >> 3] & (1 << (keyCode & 7));
97698dc4
RD
850}
851
7e86b10b
FM
852// ----------------------------------------------------------------------------
853// Launch document with default app
854// ----------------------------------------------------------------------------
855
856bool wxLaunchDefaultApplication(const wxString& document, int flags)
857{
858 wxUnusedVar(flags);
859
860 // Our best best is to use xdg-open from freedesktop.org cross-desktop
861 // compatibility suite xdg-utils
862 // (see http://portland.freedesktop.org/wiki/) -- this is installed on
863 // most modern distributions and may be tweaked by them to handle
864 // distribution specifics.
865 wxString path, xdg_open;
866 if ( wxGetEnv("PATH", &path) &&
867 wxFindFileInPath(&xdg_open, path, "xdg-open") )
868 {
869 if ( wxExecute(xdg_open + " " + document) )
870 return true;
871 }
872
873 return false;
874}
875
876// ----------------------------------------------------------------------------
877// Launch default browser
878// ----------------------------------------------------------------------------
879
880bool wxDoLaunchDefaultBrowser(const wxString& url, int flags)
881{
882 wxUnusedVar(flags);
883
884 // Our best best is to use xdg-open from freedesktop.org cross-desktop
885 // compatibility suite xdg-utils
886 // (see http://portland.freedesktop.org/wiki/) -- this is installed on
887 // most modern distributions and may be tweaked by them to handle
888 // distribution specifics. Only if that fails, try to find the right
889 // browser ourselves.
890 wxString path, xdg_open;
891 if ( wxGetEnv("PATH", &path) &&
892 wxFindFileInPath(&xdg_open, path, "xdg-open") )
893 {
894 if ( wxExecute(xdg_open + " " + url) )
895 return true;
896 }
897
898 wxString desktop = wxTheApp->GetTraits()->GetDesktopEnvironment();
899
900 // GNOME and KDE desktops have some applications which should be always installed
901 // together with their main parts, which give us the
902 if (desktop == wxT("GNOME"))
903 {
904 wxArrayString errors;
905 wxArrayString output;
906
907 // gconf will tell us the path of the application to use as browser
908 long res = wxExecute( wxT("gconftool-2 --get /desktop/gnome/applications/browser/exec"),
909 output, errors, wxEXEC_NODISABLE );
910 if (res >= 0 && errors.GetCount() == 0)
911 {
912 wxString cmd = output[0];
9a83f860 913 cmd << wxT(' ') << url;
7e86b10b
FM
914 if (wxExecute(cmd))
915 return true;
916 }
917 }
918 else if (desktop == wxT("KDE"))
919 {
920 // kfmclient directly opens the given URL
921 if (wxExecute(wxT("kfmclient openURL ") + url))
922 return true;
923 }
924
925 return false;
926}
927
17a1ebd1 928#endif // __WXX11__ || __WXGTK__ || __WXMOTIF__