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