Workaround for xkbcommon dead keys.
[chromium-blink-merge.git] / ui / events / ozone / layout / xkb / xkb_keyboard_layout_engine.h
blobd7a98545d3a435c884854f069798f9b72bcc1b4d
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"
18 namespace ui {
20 class EVENTS_OZONE_LAYOUT_EXPORT XkbKeyboardLayoutEngine
21 : public KeyboardLayoutEngine {
22 public:
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,
34 int flags,
35 DomKey* dom_key,
36 base::char16* character,
37 KeyboardCode* key_code,
38 uint32* platform_keycode) const override;
40 protected:
41 // Table for EventFlagsToXkbFlags().
42 struct XkbFlagMapEntry {
43 int ui_flag;
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,
52 int ui_flags,
53 xkb_keycode_t xkb_keycode,
54 xkb_mod_mask_t xkb_flags,
55 xkb_keysym_t xkb_keysym,
56 DomKey dom_key,
57 base::char16 character) const;
59 // Maps DomCode to xkb_keycode_t.
60 const XkbKeyCodeConverter& key_code_converter_;
62 private:
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,
83 int ui_flags) const;
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_;
91 } // namespace ui
93 #endif // UI_EVENTS_OZONE_LAYOUT_XKB_XKB_KEYBOARD_LAYOUT_ENGINE_H_