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