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