Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / events / ozone / layout / xkb / xkb_keyboard_layout_engine.h
blob89729a0d95cd835f4deaa804a617d8ed056b48d3
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>
9 #include <vector>
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"
22 namespace ui {
24 class EVENTS_OZONE_LAYOUT_EXPORT XkbKeyboardLayoutEngine
25 : public KeyboardLayoutEngine {
26 public:
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,
38 int flags,
39 DomKey* dom_key,
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);
48 protected:
49 // Table for EventFlagsToXkbFlags().
50 struct XkbFlagMapEntry {
51 int ui_flag;
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_ = 0;
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, as a last resort, obtain a KeyboardCode using
62 // |DomCodeToUsLayoutMeaning()|.
63 KeyboardCode DifficultKeyboardCode(DomCode dom_code,
64 int ui_flags,
65 xkb_keycode_t xkb_keycode,
66 xkb_mod_mask_t xkb_flags,
67 xkb_keysym_t xkb_keysym,
68 DomKey dom_key,
69 base::char16 character) const;
71 // Maps DomCode to xkb_keycode_t.
72 const XkbKeyCodeConverter& key_code_converter_;
74 private:
75 struct XkbKeymapEntry {
76 std::string layout_name;
77 xkb_keymap* keymap;
79 std::vector<XkbKeymapEntry> xkb_keymaps_;
80 // Sets a new XKB keymap. This updates xkb_state_ (which takes ownership
81 // of the keymap), and updates xkb_flag_map_ for the new keymap.
82 void SetKeymap(xkb_keymap* keymap);
84 // Returns the XKB modifiers flags corresponding to the given EventFlags.
85 xkb_mod_mask_t EventFlagsToXkbFlags(int ui_flags) const;
87 // Determines the XKB KeySym and character associated with a key.
88 // Returns true on success. This is virtual for testing.
89 virtual bool XkbLookup(xkb_keycode_t xkb_keycode,
90 xkb_mod_mask_t xkb_flags,
91 xkb_keysym_t* xkb_keysym,
92 base::char16* character) const;
94 // Helper for difficult VKEY lookup. If |ui_flags| matches |base_flags|,
95 // returns |base_character|; otherwise returns the XKB character for
96 // the keycode and mapped |ui_flags|.
97 base::char16 XkbSubCharacter(xkb_keycode_t xkb_keycode,
98 xkb_mod_mask_t base_flags,
99 base::char16 base_character,
100 int ui_flags) const;
102 // Callback when keymap file is loaded complete.
103 void OnKeymapLoaded(const std::string& layout_name,
104 scoped_ptr<char, base::FreeDeleter> keymap_str);
106 // libxkbcommon uses explicit reference counting for its structures,
107 // so we need to trigger its cleanup.
108 scoped_ptr<xkb_context, XkbContextDeleter> xkb_context_;
109 scoped_ptr<xkb_state, XkbStateDeleter> xkb_state_;
111 std::string current_layout_name_;
113 // Support weak pointers for attach & detach callbacks.
114 base::WeakPtrFactory<XkbKeyboardLayoutEngine> weak_ptr_factory_;
117 } // namespace ui
119 #endif // UI_EVENTS_OZONE_LAYOUT_XKB_XKB_KEYBOARD_LAYOUT_ENGINE_H_