1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef UI_EVENTS_OZONE_LAYOUT_XKB_XKB_KEYBOARD_LAYOUT_ENGINE_H_
6 #define UI_EVENTS_OZONE_LAYOUT_XKB_XKB_KEYBOARD_LAYOUT_ENGINE_H_
8 #include <xkbcommon/xkbcommon.h>
10 #include "base/containers/hash_tables.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string16.h"
13 #include "ui/events/ozone/layout/events_ozone_layout_export.h"
14 #include "ui/events/ozone/layout/keyboard_layout_engine.h"
15 #include "ui/events/ozone/layout/xkb/scoped_xkb.h"
16 #include "ui/events/ozone/layout/xkb/xkb_key_code_converter.h"
20 class EVENTS_OZONE_LAYOUT_EXPORT XkbKeyboardLayoutEngine
21 : public KeyboardLayoutEngine
{
23 XkbKeyboardLayoutEngine(const XkbKeyCodeConverter
& converter
);
24 virtual ~XkbKeyboardLayoutEngine();
26 // KeyboardLayoutEngine:
27 virtual bool CanSetCurrentLayout() const override
;
28 virtual bool SetCurrentLayoutByName(const std::string
& layout_name
) override
;
30 virtual bool UsesISOLevel5Shift() const override
;
31 virtual bool UsesAltGr() const override
;
33 virtual bool Lookup(DomCode dom_code
,
36 base::char16
* character
,
37 KeyboardCode
* key_code
,
38 uint32
* platform_keycode
) const override
;
41 // Table for EventFlagsToXkbFlags().
42 struct XkbFlagMapEntry
{
44 xkb_mod_mask_t xkb_flag
;
46 std::vector
<XkbFlagMapEntry
> xkb_flag_map_
;
48 // Determines the Windows-based KeyboardCode (VKEY) for a character key,
49 // accounting for non-US layouts. May return VKEY_UNKNOWN, in which case the
50 // caller should use |DomCodeToNonLocatedKeyboardCode()| as a last resort.
51 KeyboardCode
DifficultKeyboardCode(DomCode dom_code
,
53 xkb_keycode_t xkb_keycode
,
54 xkb_mod_mask_t xkb_flags
,
55 xkb_keysym_t xkb_keysym
,
57 base::char16 character
) const;
59 // Maps DomCode to xkb_keycode_t.
60 const XkbKeyCodeConverter
& key_code_converter_
;
63 // Sets a new XKB keymap. This updates xkb_state_ (which takes ownership
64 // of the keymap), and updates xkb_flag_map_ for the new keymap.
65 void SetKeymap(xkb_keymap
* keymap
);
67 // Returns the XKB modifiers flags corresponding to the given EventFlags.
68 xkb_mod_mask_t
EventFlagsToXkbFlags(int ui_flags
) const;
70 // Determines the XKB KeySym and character associated with a key.
71 // Returns true on success. This is virtual for testing.
72 virtual bool XkbLookup(xkb_keycode_t xkb_keycode
,
73 xkb_mod_mask_t xkb_flags
,
74 xkb_keysym_t
* xkb_keysym
,
75 base::char16
* character
) const;
77 // Helper for difficult VKEY lookup. If |ui_flags| matches |base_flags|,
78 // returns |base_character|; otherwise returns the XKB character for
79 // the keycode and mapped |ui_flags|.
80 base::char16
XkbSubCharacter(xkb_keycode_t xkb_keycode
,
81 xkb_mod_mask_t base_flags
,
82 base::char16 base_character
,
85 // libxkbcommon uses explicit reference counting for its structures,
86 // so we need to trigger its cleanup.
87 scoped_ptr
<xkb_context
, XkbContextDeleter
> xkb_context_
;
88 scoped_ptr
<xkb_state
, XkbStateDeleter
> xkb_state_
;
93 #endif // UI_EVENTS_OZONE_LAYOUT_XKB_XKB_KEYBOARD_LAYOUT_ENGINE_H_