summaryrefslogtreecommitdiffstats
path: root/ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.h
blob: ef52db750fa0187db8cee57c6b98361fd20ca3dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_EVENTS_OZONE_LAYOUT_XKB_XKB_KEYBOARD_LAYOUT_ENGINE_H_
#define UI_EVENTS_OZONE_LAYOUT_XKB_XKB_KEYBOARD_LAYOUT_ENGINE_H_

#include <stdint.h>
#include <xkbcommon/xkbcommon.h>

#include <vector>

#include "base/containers/hash_tables.h"
#include "base/memory/free_deleter.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string16.h"
#include "base/task_runner.h"
#include "ui/events/keycodes/scoped_xkb.h"
#include "ui/events/ozone/layout/events_ozone_layout_export.h"
#include "ui/events/ozone/layout/keyboard_layout_engine.h"
#include "ui/events/ozone/layout/xkb/xkb_key_code_converter.h"

namespace ui {

class EVENTS_OZONE_LAYOUT_EXPORT XkbKeyboardLayoutEngine
    : public KeyboardLayoutEngine {
 public:
  XkbKeyboardLayoutEngine(const XkbKeyCodeConverter& converter);
  ~XkbKeyboardLayoutEngine() override;

  void SetKeymapFromStringForTest(const char* keymap_string);

  // KeyboardLayoutEngine:
  bool CanSetCurrentLayout() const override;
  bool SetCurrentLayoutByName(const std::string& layout_name) override;

  bool UsesISOLevel5Shift() const override;
  bool UsesAltGr() const override;

  bool Lookup(DomCode dom_code,
              int flags,
              DomKey* dom_key,
              KeyboardCode* key_code) const override;

  static void ParseLayoutName(const std::string& layout_name,
                              std::string* layout_id,
                              std::string* layout_variant);

 protected:
  // Table for EventFlagsToXkbFlags().
  struct XkbFlagMapEntry {
    int ui_flag;
    xkb_mod_mask_t xkb_flag;
  };
  std::vector<XkbFlagMapEntry> xkb_flag_map_;

  // Flag mask for num lock, which is always considered enabled.
  xkb_mod_mask_t num_lock_mod_mask_ = 0;

  // Determines the Windows-based KeyboardCode (VKEY) for a character key,
  // accounting for non-US layouts. May return VKEY_UNKNOWN, in which case the
  // caller should, as a last resort, obtain a KeyboardCode using
  // |DomCodeToUsLayoutDomKey()|.
  KeyboardCode DifficultKeyboardCode(DomCode dom_code,
                                     int ui_flags,
                                     xkb_keycode_t xkb_keycode,
                                     xkb_mod_mask_t xkb_flags,
                                     xkb_keysym_t xkb_keysym,
                                     base::char16 character) const;

  // Maps DomCode to xkb_keycode_t.
  const XkbKeyCodeConverter& key_code_converter_;

 private:
  struct XkbKeymapEntry {
    std::string layout_name;
    xkb_keymap* keymap;
  };
  std::vector<XkbKeymapEntry> xkb_keymaps_;
  // Sets a new XKB keymap. This updates xkb_state_ (which takes ownership
  // of the keymap), and updates xkb_flag_map_ for the new keymap.
  void SetKeymap(xkb_keymap* keymap);

  // Returns the XKB modifiers flags corresponding to the given EventFlags.
  xkb_mod_mask_t EventFlagsToXkbFlags(int ui_flags) const;

  // Determines the XKB KeySym and character associated with a key.
  // Returns true on success. This is virtual for testing.
  virtual bool XkbLookup(xkb_keycode_t xkb_keycode,
                         xkb_mod_mask_t xkb_flags,
                         xkb_keysym_t* xkb_keysym,
                         uint32_t* character) const;

  // Helper for difficult VKEY lookup. If |ui_flags| matches |base_flags|,
  // returns |base_character|; otherwise returns the XKB character for
  // the keycode and mapped |ui_flags|.
  base::char16 XkbSubCharacter(xkb_keycode_t xkb_keycode,
                               xkb_mod_mask_t base_flags,
                               base::char16 base_character,
                               int ui_flags) const;

  // Callback when keymap file is loaded complete.
  void OnKeymapLoaded(const std::string& layout_name,
                      scoped_ptr<char, base::FreeDeleter> keymap_str);

  // libxkbcommon uses explicit reference counting for its structures,
  // so we need to trigger its cleanup.
  scoped_ptr<xkb_context, XkbContextDeleter> xkb_context_;
  scoped_ptr<xkb_state, XkbStateDeleter> xkb_state_;

  std::string current_layout_name_;

  // Support weak pointers for attach & detach callbacks.
  base::WeakPtrFactory<XkbKeyboardLayoutEngine> weak_ptr_factory_;
};

}  // namespace ui

#endif  // UI_EVENTS_OZONE_LAYOUT_XKB_XKB_KEYBOARD_LAYOUT_ENGINE_H_