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