1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:expandtab:shiftwidth=4:tabstop=4:
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #ifndef __nsGdkKeyUtils_h__
9 #define __nsGdkKeyUtils_h__
12 #include "mozilla/EventForwards.h"
20 * KeymapWrapper is a wrapper class of GdkKeymap. GdkKeymap doesn't support
21 * all our needs, therefore, we need to access lower level APIs.
22 * But such code is usually complex and might be slow. Against such issues,
23 * we should cache some information.
25 * This class provides only static methods. The methods is using internal
26 * singleton instance which is initialized by default GdkKeymap. When the
27 * GdkKeymap is destroyed, the singleton instance will be destroyed.
34 * Compute an our DOM keycode from a GDK keyval.
36 static uint32_t ComputeDOMKeyCode(const GdkEventKey
* aGdkKeyEvent
);
39 * Compute a DOM key name index from aGdkKeyEvent.
41 KeyNameIndex
ComputeDOMKeyNameIndex(const GdkEventKey
* aGdkKeyEvent
);
44 * Modifier is list of modifiers which we support in widget level.
47 NOT_MODIFIER
= 0x0000,
62 * Modifiers is used for combination of Modifier.
63 * E.g., |Modifiers modifiers = (SHIFT | CTRL);| means Shift and Ctrl.
65 typedef uint32_t Modifiers
;
68 * GetCurrentModifierState() returns current modifier key state.
69 * The "current" means actual state of hardware keyboard when this is
70 * called. I.e., if some key events are not still dispatched by GDK,
71 * the state may mismatch with GdkEventKey::state.
73 * @return Current modifier key state.
75 static guint
GetCurrentModifierState();
78 * AreModifiersCurrentlyActive() checks the "current" modifier state
79 * on aGdkWindow with the keymap of the singleton instance.
81 * @param aModifiers One or more of Modifier values except
83 * @return TRUE if all of modifieres in aModifiers are
84 * active. Otherwise, FALSE.
86 static bool AreModifiersCurrentlyActive(Modifiers aModifiers
);
89 * AreModifiersActive() just checks whether aModifierState indicates
90 * all modifiers in aModifiers are active or not.
92 * @param aModifiers One or more of Modifier values except
94 * @param aModifierState GDK's modifier states.
95 * @return TRUE if aGdkModifierType indecates all of
96 * modifiers in aModifier are active.
99 static bool AreModifiersActive(Modifiers aModifiers
,
100 guint aModifierState
);
103 * InitInputEvent() initializes the aInputEvent with aModifierState.
105 static void InitInputEvent(WidgetInputEvent
& aInputEvent
,
106 guint aModifierState
);
109 * InitKeyEvent() intializes aKeyEvent's modifier key related members
110 * and keycode related values.
112 * @param aKeyEvent It's an WidgetKeyboardEvent which needs to be
114 * @param aGdkKeyEvent A native GDK key event.
116 static void InitKeyEvent(WidgetKeyboardEvent
& aKeyEvent
,
117 GdkEventKey
* aGdkKeyEvent
);
120 * IsKeyPressEventNecessary() returns TRUE when aGdkKeyEvent should cause
121 * a DOM keypress event. Otherwise, FALSE.
123 static bool IsKeyPressEventNecessary(GdkEventKey
* aGdkKeyEvent
);
128 * GetInstance() returns a KeymapWrapper instance.
130 * @return A singleton instance of KeymapWrapper.
132 static KeymapWrapper
* GetInstance();
140 * Initializing methods.
143 void InitXKBExtension();
144 void InitBySystemSettings();
147 * mModifierKeys stores each hardware key information.
150 guint mHardwareKeycode
;
153 ModifierKey(guint aHardwareKeycode
) :
154 mHardwareKeycode(aHardwareKeycode
), mMask(0)
158 nsTArray
<ModifierKey
> mModifierKeys
;
161 * GetModifierKey() returns modifier key information of the hardware
162 * keycode. If the key isn't a modifier key, returns nullptr.
164 ModifierKey
* GetModifierKey(guint aHardwareKeycode
);
167 * mModifierMasks is bit masks for each modifier. The index should be one
168 * of ModifierIndex values.
179 COUNT_OF_MODIFIER_INDEX
181 guint mModifierMasks
[COUNT_OF_MODIFIER_INDEX
];
183 guint
GetModifierMask(Modifier aModifier
) const;
186 * @param aGdkKeyval A GDK defined modifier key value such as
188 * @return Returns Modifier values for aGdkKeyval.
189 * If the given key code isn't a modifier key,
190 * returns NOT_MODIFIER.
192 static Modifier
GetModifierForGDKKeyval(guint aGdkKeyval
);
195 static const char* GetModifierName(Modifier aModifier
);
199 * mGdkKeymap is a wrapped instance by this class.
201 GdkKeymap
* mGdkKeymap
;
204 * The base event code of XKB extension.
206 int mXKBBaseEventCode
;
209 * Pointer of the singleton instance.
211 static KeymapWrapper
* sInstance
;
216 static void OnKeysChanged(GdkKeymap
* aKeymap
, KeymapWrapper
* aKeymapWrapper
);
217 static void OnDestroyKeymap(KeymapWrapper
* aKeymapWrapper
,
218 GdkKeymap
*aGdkKeymap
);
221 * GetCharCodeFor() Computes what character is inputted by the key event
222 * with aModifierState and aGroup.
224 * @param aGdkKeyEvent Native key event, must not be nullptr.
225 * @param aModifierState Combination of GdkModifierType which you
226 * want to test with aGdkKeyEvent.
227 * @param aGroup Set group in the mGdkKeymap.
228 * @return charCode which is inputted by aGdkKeyEvent.
229 * If failed, this returns 0.
231 static uint32_t GetCharCodeFor(const GdkEventKey
*aGdkKeyEvent
);
232 uint32_t GetCharCodeFor(const GdkEventKey
*aGdkKeyEvent
,
233 guint aModifierState
,
237 * GetKeyLevel() returns level of the aGdkKeyEvent in mGdkKeymap.
239 * @param aGdkKeyEvent Native key event, must not be nullptr.
240 * @return Using level. Typically, this is 0 or 1.
241 * If failed, this returns -1.
243 gint
GetKeyLevel(GdkEventKey
*aGdkKeyEvent
);
246 * GetFirstLatinGroup() returns group of mGdkKeymap which can input an
247 * ASCII character by GDK_A.
249 * @return group value of GdkEventKey.
251 gint
GetFirstLatinGroup();
254 * IsLatinGroup() checkes whether the keyboard layout of aGroup is
255 * ASCII alphabet inputtable or not.
257 * @param aGroup The group value of GdkEventKey.
258 * @return TRUE if the keyboard layout can input
259 * ASCII alphabet. Otherwise, FALSE.
261 bool IsLatinGroup(guint8 aGroup
);
264 * IsBasicLatinLetterOrNumeral() Checks whether the aCharCode is an
265 * alphabet or a numeric character in ASCII.
267 * @param aCharCode Charcode which you want to test.
268 * @return TRUE if aCharCode is an alphabet or a numeric
269 * in ASCII range. Otherwise, FALSE.
271 static bool IsBasicLatinLetterOrNumeral(uint32_t aCharCode
);
274 * GetGDKKeyvalWithoutModifier() returns the keyval for aGdkKeyEvent when
275 * ignoring the modifier state except NumLock. (NumLock is a key to change
276 * some key's meaning.)
278 static guint
GetGDKKeyvalWithoutModifier(const GdkEventKey
*aGdkKeyEvent
);
281 * GetDOMKeyCodeFromKeyPairs() returns DOM keycode for aGdkKeyval if
282 * it's in KeyPair table.
284 static uint32_t GetDOMKeyCodeFromKeyPairs(guint aGdkKeyval
);
287 * InitKeypressEvent() intializes keyCode, charCode and
288 * alternativeCharCodes of keypress event.
290 * @param aKeyEvent An NS_KEY_PRESS event, must not be nullptr.
291 * The modifier related members and keyCode must
292 * be initialized already.
293 * @param aGdkKeyEvent A native key event which causes dispatching
296 void InitKeypressEvent(WidgetKeyboardEvent
& aKeyEvent
,
297 GdkEventKey
* aGdkKeyEvent
);
300 } // namespace widget
301 } // namespace mozilla
303 #endif /* __nsGdkKeyUtils_h__ */