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>
11 #include "base/containers/hash_tables.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/strings/string16.h"
16 #include "base/task_runner.h"
17 #include "ui/events/ozone/layout/events_ozone_layout_export.h"
18 #include "ui/events/ozone/layout/keyboard_layout_engine.h"
19 #include "ui/events/ozone/layout/xkb/scoped_xkb.h"
20 #include "ui/events/ozone/layout/xkb/xkb_key_code_converter.h"
24 class EVENTS_OZONE_LAYOUT_EXPORT XkbKeyboardLayoutEngine
25 : public KeyboardLayoutEngine
{
27 XkbKeyboardLayoutEngine(const XkbKeyCodeConverter
& converter
);
28 ~XkbKeyboardLayoutEngine() override
;
30 // KeyboardLayoutEngine:
31 bool CanSetCurrentLayout() const override
;
32 bool SetCurrentLayoutByName(const std::string
& layout_name
) override
;
34 bool UsesISOLevel5Shift() const override
;
35 bool UsesAltGr() const override
;
37 bool Lookup(DomCode dom_code
,
40 base::char16
* character
,
41 KeyboardCode
* key_code
,
42 uint32
* platform_keycode
) const override
;
44 static void ParseLayoutName(const std::string
& layout_name
,
45 std::string
* layout_id
,
46 std::string
* layout_variant
);
49 // Table for EventFlagsToXkbFlags().
50 struct XkbFlagMapEntry
{
52 xkb_mod_mask_t xkb_flag
;
54 std::vector
<XkbFlagMapEntry
> xkb_flag_map_
;
56 // Flag mask for num lock, which is always considered enabled.
57 xkb_mod_mask_t num_lock_mod_mask_
;
59 // Determines the Windows-based KeyboardCode (VKEY) for a character key,
60 // accounting for non-US layouts. May return VKEY_UNKNOWN, in which case the
61 // caller should use |DomCodeToNonLocatedKeyboardCode()| as a last resort.
62 KeyboardCode
DifficultKeyboardCode(DomCode dom_code
,
64 xkb_keycode_t xkb_keycode
,
65 xkb_mod_mask_t xkb_flags
,
66 xkb_keysym_t xkb_keysym
,
68 base::char16 character
) const;
70 // Maps DomCode to xkb_keycode_t.
71 const XkbKeyCodeConverter
& key_code_converter_
;
74 struct XkbKeymapEntry
{
75 std::string layout_name
;
78 std::vector
<XkbKeymapEntry
> xkb_keymaps_
;
79 // Sets a new XKB keymap. This updates xkb_state_ (which takes ownership
80 // of the keymap), and updates xkb_flag_map_ for the new keymap.
81 void SetKeymap(xkb_keymap
* keymap
);
83 // Returns the XKB modifiers flags corresponding to the given EventFlags.
84 xkb_mod_mask_t
EventFlagsToXkbFlags(int ui_flags
) const;
86 // Determines the XKB KeySym and character associated with a key.
87 // Returns true on success. This is virtual for testing.
88 virtual bool XkbLookup(xkb_keycode_t xkb_keycode
,
89 xkb_mod_mask_t xkb_flags
,
90 xkb_keysym_t
* xkb_keysym
,
91 base::char16
* character
) const;
93 // Helper for difficult VKEY lookup. If |ui_flags| matches |base_flags|,
94 // returns |base_character|; otherwise returns the XKB character for
95 // the keycode and mapped |ui_flags|.
96 base::char16
XkbSubCharacter(xkb_keycode_t xkb_keycode
,
97 xkb_mod_mask_t base_flags
,
98 base::char16 base_character
,
101 // Callback when keymap file is loaded complete.
102 void OnKeymapLoaded(const std::string
& layout_name
,
103 scoped_ptr
<char, base::FreeDeleter
> keymap_str
);
105 // libxkbcommon uses explicit reference counting for its structures,
106 // so we need to trigger its cleanup.
107 scoped_ptr
<xkb_context
, XkbContextDeleter
> xkb_context_
;
108 scoped_ptr
<xkb_state
, XkbStateDeleter
> xkb_state_
;
110 std::string current_layout_name_
;
112 // Support weak pointers for attach & detach callbacks.
113 base::WeakPtrFactory
<XkbKeyboardLayoutEngine
> weak_ptr_factory_
;
118 #endif // UI_EVENTS_OZONE_LAYOUT_XKB_XKB_KEYBOARD_LAYOUT_ENGINE_H_