|
29 | 29 | #include <glib.h>
|
30 | 30 | #include "glass_general.h"
|
31 | 31 | #include <gdk/gdkkeysyms.h>
|
| 32 | +#include <X11/XKBlib.h> |
32 | 33 |
|
33 | 34 | static gboolean key_initialized = FALSE;
|
34 | 35 | static GHashTable *keymap;
|
@@ -345,4 +346,63 @@ JNIEXPORT jint JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1getKeyCodeForC
|
345 | 346 | return gdk_keyval_to_glass(keyval);
|
346 | 347 | }
|
347 | 348 |
|
| 349 | +/* |
| 350 | + * Function to determine whether the Xkb extention is available. This is a |
| 351 | + * precaution against X protocol errors, although it should be available on all |
| 352 | + * Linux systems. |
| 353 | + */ |
| 354 | + |
| 355 | +static Bool xkbInitialized = False; |
| 356 | +static Bool xkbAvailable = False; |
| 357 | + |
| 358 | +static Bool isXkbAvailable(Display *display) { |
| 359 | + if (!xkbInitialized) { |
| 360 | + int xkbMajor = XkbMajorVersion; |
| 361 | + int xkbMinor = XkbMinorVersion; |
| 362 | + xkbAvailable = XkbQueryExtension(display, NULL, NULL, NULL, &xkbMajor, &xkbMinor); |
| 363 | + xkbInitialized = True; |
| 364 | + } |
| 365 | + return xkbAvailable; |
| 366 | +} |
| 367 | + |
| 368 | +/* |
| 369 | + * Class: com_sun_glass_ui_gtk_GtkApplication |
| 370 | + * Method: _isKeyLocked |
| 371 | + * Signature: (I)I |
| 372 | + */ |
| 373 | +JNIEXPORT jint JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1isKeyLocked |
| 374 | + (JNIEnv * env, jobject obj, jint keyCode) |
| 375 | +{ |
| 376 | + Display* display = gdk_x11_display_get_xdisplay(gdk_display_get_default()); |
| 377 | + if (!isXkbAvailable(display)) { |
| 378 | + return com_sun_glass_events_KeyEvent_KEY_LOCK_UNKNOWN; |
| 379 | + } |
| 380 | + |
| 381 | + Atom keyCodeAtom = None; |
| 382 | + switch (keyCode) { |
| 383 | + case com_sun_glass_events_KeyEvent_VK_CAPS_LOCK: |
| 384 | + keyCodeAtom = XInternAtom(display, "Caps Lock", True); |
| 385 | + break; |
| 386 | + |
| 387 | + case com_sun_glass_events_KeyEvent_VK_NUM_LOCK: |
| 388 | + keyCodeAtom = XInternAtom(display, "Num Lock", True); |
| 389 | + break; |
| 390 | + } |
| 391 | + |
| 392 | + if (keyCodeAtom == None) { |
| 393 | + return com_sun_glass_events_KeyEvent_KEY_LOCK_UNKNOWN; |
| 394 | + } |
| 395 | + |
| 396 | + Bool isLocked = False; |
| 397 | + if (XkbGetNamedIndicator(display, keyCodeAtom, NULL, &isLocked, NULL, NULL)) { |
| 398 | + if (isLocked) { |
| 399 | + return com_sun_glass_events_KeyEvent_KEY_LOCK_ON; |
| 400 | + } else { |
| 401 | + return com_sun_glass_events_KeyEvent_KEY_LOCK_OFF; |
| 402 | + } |
| 403 | + } |
| 404 | + |
| 405 | + return com_sun_glass_events_KeyEvent_KEY_LOCK_UNKNOWN; |
| 406 | +} |
| 407 | + |
348 | 408 | } // extern "C"
|
0 commit comments