gtk2/gdk/win32/gdkkeys-win32.c
Tor Lillqvist 63f745738d gdk/win32/gdkprivate-win32.h New flags _gdk_input_locale_is_ime and
2003-07-25  Tor Lillqvist  <tml@iki.fi>

	* gdk/win32/gdkprivate-win32.h
	* gdk/win32/gdkglobals-win32.c: New flags _gdk_input_locale_is_ime
	and _gdk_keyboard_has_altgr.

	* gdk/win32/gdkevents-win32.c: Lots of changes. Most important
	ones detailled here.

	Code that has been ifdeffed out for a long time removed. Remove
	some really old doc comments that were left behind for some public
	functions, the official ones are in the X11 backend anyway. Change
	GDK_WINDOW_OBJECT() calls to GdkWindowObject casts. Reformat
	multi-line boolean expressions to have the operators at ends of
	lines.

	As mouse capture with SetCapture() indeed seems to work OK, no
	need to have the correspoinding macro USE_SETCAPTURE and ifdefs.

	Ifdef out the gdk-ping-msg stuff. I don't remember why it was
	needed at some time, and things seem to work fine now without
	(knock on wood).

	Ifdef out the search for some Latin locale keyboard layout being
	loaded. Not used currently, but might be needed after all, if we
	decide that we want to be able to generate ASCII control character
	events with a non-Latin keyboard.

	(assign_object): New helper function, handles the g_object_ref()
	and unref() calls when assigning GObject pointers.

	(generate_crossing_events): Also generate the GDK_NOTIFY_INTERIOR
	enter event when the pointer has moved to an ancestor window. Was
	left out by mistake.

	(gdk_window_is_ancestor): Renamed from gdk_window_is_child().

	(gdk_pointer_grab, gdk_pointer_ungrab): Implement the confine_to
	functionality, using ClipCursor().

	(find_window_for_mouse_event): Splice part of code into new
	function find_real_window_for_grabbed_mouse_event().

	(fixup_event, append_event, apply_filters): New functions, code
	refactored out from elsewhere.

	(synthesize_enter_or_leave_event, synthesize_leave_event,
	synthesize_enter_event,
	synthesize_leave_events,synthesize_enter_events): Also take a
	GdkCrossingMode parameter, in preparation to generating
	GDK_CROSSING_GRAB and GDK_CROSSING_UNGRAB events.

	(fixup_event, append_event, fill_key_event_string): New functions,
	code refactoring.

	(vk_from_char, build_keypress_event, build_keyrelease_event):
	Removed as part of dropping WM_CHAR handling.

	(build_key_event_state,gdk_event_translate): Call
	GetKeyboardState(), once, for each keyboard message, instead of
	several calls to GetKeyState() here and there.

	(gdk_event_translate): Fix bugs #104516, #104662, #115902. While
	at it, do some major refactoring, and some fixes for potential
	problems noticed while going through the code.

	Don't handle WM_CHAR at all. Only handle WM_KEYDOWN and
	WM_KEYUP. Don't need the state variables related to whether to
	wait for WM_CHAR or not, and whether the current key is
	AltGr. Remove lots of complexity. Thus don't need the
	use_ime_composition flag.

	Not handling WM_CHAR means dead key handling will have to be taken
	care of by GTK, but that seems to work fine, so no worry.

	Another side-effect is that Alt+keypad digits don't work any
	longer, but it's better to learn to use GTK's ISO14755 support is
	anyway.

	Be more careful in checking whether AltGr is involved. Only
	attempt to handle it if the keyboard actually has it. And
	explicitly check for *left* Control plus *right* Alt being
	pressed. Still, allow (left) Alt and/or (right) Control with AltGr
	chars.

	Handle keys using similar code as in the X11 backend. As we have
	built a keymap in gdkkeys-win32.c anyway, use it by calling
	gdk_keymap_translate_keyboard_state() to look up the keysym from
	the virtual key code and keyboard state. Build the key event
	string in exactly the same way as the X11 backend.

	If an IME is being used, don't generate GDK events for keys
	between receiving WM_IME_STARTCOMPOSITION and
	WM_IME_ENDCOMPOSITION, as those keys are for the IME.

	For WM_IME_COMPOSITION, handle all the Unicode chars returned from
	the IME, not just the first one.

	gdk_event_translate() is still quite complex, could split the
	message handler cases out into separate functions.

	On mouse events, when the mouse is grabbed, use
	find_real_window_for_grabbed_mouse_event() in order to be able to
	generate correct crossing events.

	No longer take a pre-allocated GdkEvent as parameter. Instead,
	allocate events as needed and append them to the queue. (This is
	different from how gdk_event_translate() in the X11 backend
	works.) This change made the code much clearer, especially in the
	cases where we have to generate several GDK events for one Windows
	message.  Return FALSE if DefWindowProc() should be called, TRUE
	if not. If DefWindowProc() should not be called, also return the
	value to be returned from the window procedure.

	(Previously, the interaction with gdk_event_translate()'s caller
	was much more complex, when we had to indicate whether the
	already-queued event should be left in the queue or removed, and
	in addition also had to indicate whether to call DefWindowProc()
	or not, and what value to return from the window procedure if
	not.)

	Don't use a separate "private" variable required to be pointing to
	the GdkWindowObject of the "window" variable at all times. Just
	use casts, even if looks a bit uglier.

	Notice destroyed windows as early as possible, and break out of
	the messsage switch.

	Use _gdk_pointer_root as current_window when the pointer is
	outside GDK's top-level windows.

	On WM_INPUTLANGCHANGE, set _gdk_input_locale_is_ime as
	appropriate, based on ImmIsIME().

	(gdk_event_translate, gdk_event_send_client_message_for_display,
	gdk_screen_broadcast_client_message): Implement client messages.
	Use a registered Windows message to pass GDK client messages. Note
	that the amount of user data is restricted to four bytes, as it is
	carried in the LPARAM. (The WPARAM is used for the message type
	"atom".)

	(real_window_procedure): Adapt for new gdk_event_translate()
	interface.

	* gdk/win32/gdkmain-win32.c (_gdk_windowing_init): Set
	_gdk_input_locale_is_ime initially.

	* gdk/win32/gdkwindow-win32.c: Use g_object_ref()/unref() instead
	of g_colormap_ref()/unref().

	(gdk_window_new): Made code a bit more like the X11 one, pretend
	to handle screens (although we just have one for now).

	* gdk/x11/gdkevents-x11.c
	(gdk_event_send_client_message_for_display,
	gdk_screen_broadcast_client_message): Document the user data
	limitation on Win32.

	* gdk/win32/gdkevents-win32.c (print_event): More complete enter
	and leave notify detail output.

	* gdk/win32/gdkkeys-win32.c (update_keymap): Make dead keys
	visible to GDK and GTK. Store the corresponding GDK_dead_* keysym
	for those, so that the GtkIMContextCimple compose tables will
	work. Deduce if the keyboard layout has the AltGr key, and set the
	above flag accordingly.
2003-07-26 01:54:59 +00:00

735 lines
18 KiB
C

/* GDK - The GIMP Drawing Kit
* Copyright (C) 2000 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include "gdk.h"
#include "gdkprivate-win32.h"
#include "gdkinternals.h"
#include "gdkkeysyms.h"
#include "config.h"
guint _gdk_keymap_serial = 0;
gboolean _gdk_keyboard_has_altgr = FALSE;
static GdkKeymap *default_keymap = NULL;
static guint *keysym_tab = NULL;
#ifdef G_ENABLE_DEBUG
static void
print_keysym_tab (void)
{
gint vk;
g_print ("keymap:%s\n", _gdk_keyboard_has_altgr ? " (uses AltGr)" : "");
for (vk = 0; vk < 256; vk++)
{
gint state;
g_print ("%#.02x: ", vk);
for (state = 0; state < 4; state++)
{
gchar *name = gdk_keyval_name (keysym_tab[vk*4 + state]);
if (name == NULL)
name = "(none)";
g_print ("%s ", name);
}
g_print ("\n");
}
}
#endif
static void
update_keymap (void)
{
static guint current_serial = 0;
guchar key_state[256], temp_key_state[256];
wchar_t wcs[1];
guint scancode;
guint vk;
if (keysym_tab != NULL && current_serial == _gdk_keymap_serial)
return;
current_serial = _gdk_keymap_serial;
if (keysym_tab == NULL)
keysym_tab = g_new (guint, 4*256);
memset (key_state, 0, sizeof (key_state));
_gdk_keyboard_has_altgr = FALSE;
for (vk = 0; vk < 256; vk++)
{
if ((scancode = MapVirtualKey (vk, 0)) == 0)
keysym_tab[vk*4+0] =
keysym_tab[vk*4+1] =
keysym_tab[vk*4+2] =
keysym_tab[vk*4+3] = GDK_VoidSymbol;
else
{
gint shift;
key_state[vk] = 0x80;
for (shift = 0; shift < 4; shift++)
{
guint *ksymp = keysym_tab + vk*4 + shift;
guchar chars[2];
switch (shift)
{
case 0:
key_state[VK_SHIFT] = 0;
key_state[VK_CONTROL] = key_state[VK_MENU] = 0;
break;
case 1:
key_state[VK_SHIFT] = 0x80;
key_state[VK_CONTROL] = key_state[VK_MENU] = 0;
break;
case 2:
key_state[VK_SHIFT] = 0;
key_state[VK_CONTROL] = key_state[VK_MENU] = 0x80;
break;
case 3:
key_state[VK_SHIFT] = 0x80;
key_state[VK_CONTROL] = key_state[VK_MENU] = 0x80;
break;
}
*ksymp = 0;
/* First, handle those virtual keys that we always want
* as special GDK_* keysyms, even if ToAsciiEx might
* turn some them into a ASCII character (like TAB and
* ESC).
*/
switch (vk)
{
case VK_CANCEL:
*ksymp = GDK_Cancel; break;
case VK_BACK:
*ksymp = GDK_BackSpace; break;
case VK_TAB:
if (shift & 0x1)
*ksymp = GDK_ISO_Left_Tab;
else
*ksymp = GDK_Tab;
break;
case VK_CLEAR:
*ksymp = GDK_Clear; break;
case VK_RETURN:
*ksymp = GDK_Return; break;
case VK_SHIFT:
case VK_LSHIFT:
*ksymp = GDK_Shift_L; break;
case VK_CONTROL:
case VK_LCONTROL:
*ksymp = GDK_Control_L; break;
case VK_MENU:
case VK_LMENU:
*ksymp = GDK_Alt_L; break;
case VK_PAUSE:
*ksymp = GDK_Pause; break;
case VK_ESCAPE:
*ksymp = GDK_Escape; break;
case VK_PRIOR:
*ksymp = GDK_Prior; break;
case VK_NEXT:
*ksymp = GDK_Next; break;
case VK_END:
*ksymp = GDK_End; break;
case VK_HOME:
*ksymp = GDK_Home; break;
case VK_LEFT:
*ksymp = GDK_Left; break;
case VK_UP:
*ksymp = GDK_Up; break;
case VK_RIGHT:
*ksymp = GDK_Right; break;
case VK_DOWN:
*ksymp = GDK_Down; break;
case VK_SELECT:
*ksymp = GDK_Select; break;
case VK_PRINT:
*ksymp = GDK_Print; break;
case VK_EXECUTE:
*ksymp = GDK_Execute; break;
case VK_INSERT:
*ksymp = GDK_Insert; break;
case VK_DELETE:
*ksymp = GDK_Delete; break;
case VK_HELP:
*ksymp = GDK_Help; break;
case VK_LWIN:
*ksymp = GDK_Meta_L; break;
case VK_RWIN:
*ksymp = GDK_Meta_R; break;
case VK_MULTIPLY:
*ksymp = GDK_KP_Multiply; break;
case VK_ADD:
*ksymp = GDK_KP_Add; break;
case VK_SEPARATOR:
*ksymp = GDK_KP_Separator; break;
case VK_SUBTRACT:
*ksymp = GDK_KP_Subtract; break;
case VK_DECIMAL:
*ksymp = GDK_KP_Decimal; break;
case VK_DIVIDE:
*ksymp = GDK_KP_Divide; break;
case VK_F1:
*ksymp = GDK_F1; break;
case VK_F2:
*ksymp = GDK_F2; break;
case VK_F3:
*ksymp = GDK_F3; break;
case VK_F4:
*ksymp = GDK_F4; break;
case VK_F5:
*ksymp = GDK_F5; break;
case VK_F6:
*ksymp = GDK_F6; break;
case VK_F7:
*ksymp = GDK_F7; break;
case VK_F8:
*ksymp = GDK_F8; break;
case VK_F9:
*ksymp = GDK_F9; break;
case VK_F10:
*ksymp = GDK_F10; break;
case VK_F11:
*ksymp = GDK_F11; break;
case VK_F12:
*ksymp = GDK_F12; break;
case VK_F13:
*ksymp = GDK_F13; break;
case VK_F14:
*ksymp = GDK_F14; break;
case VK_F15:
*ksymp = GDK_F15; break;
case VK_F16:
*ksymp = GDK_F16; break;
case VK_F17:
*ksymp = GDK_F17; break;
case VK_F18:
*ksymp = GDK_F18; break;
case VK_F19:
*ksymp = GDK_F19; break;
case VK_F20:
*ksymp = GDK_F20; break;
case VK_F21:
*ksymp = GDK_F21; break;
case VK_F22:
*ksymp = GDK_F22; break;
case VK_F23:
*ksymp = GDK_F23; break;
case VK_F24:
*ksymp = GDK_F24; break;
case VK_NUMLOCK:
*ksymp = GDK_Num_Lock; break;
case VK_SCROLL:
*ksymp = GDK_Scroll_Lock; break;
case VK_RSHIFT:
*ksymp = GDK_Shift_R; break;
case VK_RCONTROL:
*ksymp = GDK_Control_R; break;
case VK_RMENU:
*ksymp = GDK_Alt_R; break;
}
if (*ksymp == 0)
{
gint k = ToAsciiEx (vk, scancode, key_state,
(LPWORD) chars, 0, _gdk_input_locale);
#if 0
g_print ("ToAsciiEx(%02x: %d: %d: %d, %02x%02x\n",
vk, scancode, shift, k, chars[0], chars[1]);
#endif
if (k == 1)
{
if (_gdk_input_codepage >= 1250 &&
_gdk_input_codepage <= 1258 &&
chars[0] >= GDK_space && chars[0] <= GDK_asciitilde)
*ksymp = chars[0];
else
{
if (MultiByteToWideChar (_gdk_input_codepage, 0,
chars, 1, wcs, 1) > 0)
*ksymp = gdk_unicode_to_keyval (wcs[0]);
}
}
else if (k == -1)
{
MultiByteToWideChar (_gdk_input_codepage, 0,
chars, 1, wcs, 1);
/* It is a dead key, and it's has been stored in
* the keyboard layout's state by ToAsciiEx().
* Yes, this is an incredibly silly API! Make
* the keyboard layout forget it by calling
* ToAsciiEx() once more, with the virtual key
* code and scancode for the spacebar, without
* shift or AltGr. Otherwise the next call to
* ToAsciiEx() with a different key would try to
* combine with the dead key.
*/
memmove (temp_key_state, key_state, sizeof (key_state));
temp_key_state[VK_SHIFT] =
temp_key_state[VK_CONTROL] =
temp_key_state[VK_MENU] = 0;
ToAsciiEx (VK_SPACE, MapVirtualKey (VK_SPACE, 0),
temp_key_state, (LPWORD) chars, 0,
_gdk_input_locale);
/* Use dead keysyms instead of "undead" ones */
switch (gdk_unicode_to_keyval (wcs[0]))
{
case GDK_grave:
*ksymp = GDK_dead_grave; break;
case GDK_acute:
*ksymp = GDK_dead_acute; break;
case GDK_asciicircum:
*ksymp = GDK_dead_circumflex; break;
case GDK_asciitilde:
*ksymp = GDK_dead_tilde; break;
case GDK_breve:
*ksymp = GDK_dead_breve; break;
case GDK_abovedot:
*ksymp = GDK_dead_abovedot; break;
case GDK_diaeresis:
*ksymp = GDK_dead_diaeresis; break;
case GDK_doubleacute:
*ksymp = GDK_dead_doubleacute; break;
case GDK_caron:
*ksymp = GDK_dead_caron; break;
case GDK_cedilla:
*ksymp = GDK_dead_cedilla; break;
case GDK_ogonek:
*ksymp = GDK_dead_ogonek; break;
case GDK_degree:
*ksymp = GDK_dead_abovering; break;
case GDK_periodcentered:
*ksymp = GDK_dead_abovedot; break;
case GDK_Greek_accentdieresis:
*ksymp = GDK_Greek_accentdieresis; break;
default:
GDK_NOTE (EVENTS,
g_print ("Unhandled dead key cp:%d vk:%02x, sc:%x, ch:%02x%s%s\n",
_gdk_input_codepage, vk,
scancode, chars[0],
(shift&0x1 ? " shift" : ""),
(shift&0x2 ? " altgr" : "")));
}
}
else if (k == 0)
{
/* Seems to be necessary to "reset" the keyboard layout
* in this case, too. Otherwise problems on NT4.
*/
memmove (temp_key_state, key_state, sizeof (key_state));
temp_key_state[VK_SHIFT] =
temp_key_state[VK_CONTROL] =
temp_key_state[VK_MENU] = 0;
ToAsciiEx (VK_SPACE, MapVirtualKey (VK_SPACE, 0),
temp_key_state, (LPWORD) chars, 0,
_gdk_input_locale);
}
else
{
GDK_NOTE (EVENTS, g_print ("ToAsciiEx returns %d "
"for vk:%02x, sc:%02x%s%s\n",
k, vk, scancode,
(shift&0x1 ? " shift" : ""),
(shift&0x2 ? " altgr" : "")));
}
}
if (*ksymp == 0)
*ksymp = GDK_VoidSymbol;
}
key_state[vk] = 0;
}
if ((keysym_tab[vk*4 + 2] != GDK_VoidSymbol &&
keysym_tab[vk*4] != keysym_tab[vk*4 + 2]) ||
(keysym_tab[vk*4 + 3] != GDK_VoidSymbol &&
keysym_tab[vk*4 + 1] != keysym_tab[vk*4 + 3]))
_gdk_keyboard_has_altgr = TRUE;
}
GDK_NOTE (EVENTS, print_keysym_tab ());
}
GdkKeymap*
gdk_keymap_get_for_display (GdkDisplay *display)
{
g_return_val_if_fail (display == gdk_display_get_default (), NULL);
if (default_keymap == NULL)
default_keymap = g_object_new (gdk_keymap_get_type (), NULL);
return default_keymap;
}
PangoDirection
gdk_keymap_get_direction (GdkKeymap *keymap)
{
update_keymap ();
switch (PRIMARYLANGID (LOWORD ((DWORD) _gdk_input_locale)))
{
case LANG_HEBREW:
case LANG_ARABIC:
#ifdef LANG_URDU
case LANG_URDU:
#endif
case LANG_FARSI:
/* Others? */
return PANGO_DIRECTION_RTL;
default:
return PANGO_DIRECTION_LTR;
}
}
gboolean
gdk_keymap_get_entries_for_keyval (GdkKeymap *keymap,
guint keyval,
GdkKeymapKey **keys,
gint *n_keys)
{
GArray *retval;
g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
g_return_val_if_fail (keys != NULL, FALSE);
g_return_val_if_fail (n_keys != NULL, FALSE);
g_return_val_if_fail (keyval != 0, FALSE);
retval = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
/* Accept only the default keymap */
if (keymap == NULL || keymap == gdk_keymap_get_default ())
{
gint vk;
update_keymap ();
for (vk = 0; vk < 256; vk++)
{
gint i;
for (i = 0; i < 4; i++)
{
if (keysym_tab[vk*4+i] == keyval)
{
GdkKeymapKey key;
key.keycode = vk;
/* 2 levels (normal, shift), two groups (normal, AltGr) */
key.group = i / 2;
key.level = i % 2;
g_array_append_val (retval, key);
}
}
}
}
#ifdef G_ENABLE_DEBUG
if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
{
gint i;
g_print ("gdk_keymap_get_entries_for_keyval: %#.04x (%s):",
keyval, gdk_keyval_name (keyval));
for (i = 0; i < retval->len; i++)
{
GdkKeymapKey *entry = (GdkKeymapKey *) retval->data + i;
g_print (" %#.02x %d %d", entry->keycode, entry->group, entry->level);
}
g_print ("\n");
}
#endif
if (retval->len > 0)
{
*keys = (GdkKeymapKey*) retval->data;
*n_keys = retval->len;
}
else
{
*keys = NULL;
*n_keys = 0;
}
g_array_free (retval, retval->len > 0 ? FALSE : TRUE);
return *n_keys > 0;
}
gboolean
gdk_keymap_get_entries_for_keycode (GdkKeymap *keymap,
guint hardware_keycode,
GdkKeymapKey **keys,
guint **keyvals,
gint *n_entries)
{
GArray *key_array;
GArray *keyval_array;
g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
g_return_val_if_fail (n_entries != NULL, FALSE);
if (hardware_keycode <= 0 ||
hardware_keycode >= 256)
{
if (keys)
*keys = NULL;
if (keyvals)
*keyvals = NULL;
*n_entries = 0;
return FALSE;
}
if (keys)
key_array = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
else
key_array = NULL;
if (keyvals)
keyval_array = g_array_new (FALSE, FALSE, sizeof (guint));
else
keyval_array = NULL;
/* Accept only the default keymap */
if (keymap == NULL || keymap == gdk_keymap_get_default ())
{
gint i;
update_keymap ();
for (i = 0; i < 4; i++)
{
if (key_array)
{
GdkKeymapKey key;
key.keycode = hardware_keycode;
key.group = i / 2;
key.level = i % 2;
g_array_append_val (key_array, key);
}
if (keyval_array)
g_array_append_val (keyval_array, keysym_tab[hardware_keycode*4+i]);
}
}
if ((key_array && key_array->len > 0) ||
(keyval_array && keyval_array->len > 0))
{
if (keys)
*keys = (GdkKeymapKey*) key_array->data;
if (keyvals)
*keyvals = (guint*) keyval_array->data;
if (key_array)
*n_entries = key_array->len;
else
*n_entries = keyval_array->len;
}
else
{
if (keys)
*keys = NULL;
if (keyvals)
*keyvals = NULL;
*n_entries = 0;
}
if (key_array)
g_array_free (key_array, key_array->len > 0 ? FALSE : TRUE);
if (keyval_array)
g_array_free (keyval_array, keyval_array->len > 0 ? FALSE : TRUE);
return *n_entries > 0;
}
guint
gdk_keymap_lookup_key (GdkKeymap *keymap,
const GdkKeymapKey *key)
{
guint sym;
g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), 0);
g_return_val_if_fail (key != NULL, 0);
g_return_val_if_fail (key->group < 4, 0);
/* Accept only the default keymap */
if (keymap != NULL && keymap != gdk_keymap_get_default ())
return 0;
update_keymap ();
if (key->keycode >= 256 ||
key->group < 0 || key->group >= 2 ||
key->level < 0 || key->level >= 2)
return 0;
sym = keysym_tab[key->keycode*4 + key->group*2 + key->level];
if (sym == GDK_VoidSymbol)
return 0;
else
return sym;
}
gboolean
gdk_keymap_translate_keyboard_state (GdkKeymap *keymap,
guint hardware_keycode,
GdkModifierType state,
gint group,
guint *keyval,
gint *effective_group,
gint *level,
GdkModifierType *consumed_modifiers)
{
guint tmp_keyval;
guint tmp_modifiers;
guint *keyvals;
gint shift_level;
gboolean ignore_shift = FALSE;
gboolean ignore_group = FALSE;
g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
g_return_val_if_fail (group < 4, FALSE);
GDK_NOTE (EVENTS, g_print ("gdk_keymap_translate_keyboard_state: keycode=%#x state=%#x group=%d\n",
hardware_keycode, state, group));
if (keyval)
*keyval = 0;
if (effective_group)
*effective_group = 0;
if (level)
*level = 0;
if (consumed_modifiers)
*consumed_modifiers = 0;
/* Accept only the default keymap */
if (keymap != NULL && keymap != gdk_keymap_get_default ())
return FALSE;
if (hardware_keycode >= 256)
return FALSE;
if (group < 0 || group >= 2)
return FALSE;
if ((state & GDK_SHIFT_MASK) && (state & GDK_LOCK_MASK))
shift_level = 0; /* shift disables shift lock */
else if ((state & GDK_SHIFT_MASK) || (state & GDK_LOCK_MASK))
shift_level = 1;
else
shift_level = 0;
update_keymap ();
keyvals = keysym_tab + hardware_keycode*4;
/* Drop group and shift if there are no keysymbols on
* the key for those.
*/
if (shift_level == 1 &&
keyvals[group*2 + 1] == GDK_VoidSymbol &&
keyvals[group*2] != GDK_VoidSymbol)
{
shift_level = 0;
ignore_shift = TRUE;
}
if (group == 1 &&
keyvals[2 + shift_level] == GDK_VoidSymbol &&
keyvals[0 + shift_level] != GDK_VoidSymbol)
{
group = 0;
ignore_group = TRUE;
}
if (keyvals[group *2 + shift_level] == GDK_VoidSymbol &&
keyvals[0 + 0] != GDK_VoidSymbol)
{
shift_level = 0;
group = 0;
ignore_group = TRUE;
ignore_shift = TRUE;
}
/* See whether the group and shift level actually mattered
* to know what to put in consumed_modifiers
*/
if (keyvals[group*2 + 1] == GDK_VoidSymbol ||
keyvals[group*2 + 0] == keyvals[group*2 + 1])
ignore_shift = TRUE;
if (keyvals[2 + shift_level] == GDK_VoidSymbol ||
keyvals[0 + shift_level] == keyvals[2 + shift_level])
ignore_group = TRUE;
tmp_keyval = keyvals[group*2 + shift_level];
tmp_modifiers = ignore_group ? 0 : GDK_MOD2_MASK;
tmp_modifiers |= ignore_shift ? 0 : (GDK_SHIFT_MASK | GDK_LOCK_MASK);
if (effective_group)
*effective_group = group;
if (level)
*level = shift_level;
if (consumed_modifiers)
*consumed_modifiers = tmp_modifiers;
if (keyval)
*keyval = tmp_keyval;
GDK_NOTE (EVENTS, g_print ("...group=%d level=%d cmods=%#x keyval=%s\n",
group, shift_level, tmp_modifiers, gdk_keyval_name (tmp_keyval)));
return tmp_keyval != GDK_VoidSymbol;
}