File: keyboard_state.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (138 lines) | stat: -rw-r--r-- 4,374 bytes parent folder | download | duplicates (9)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/x/keyboard_state.h"

#include "base/i18n/case_conversion.h"
#include "base/memory/raw_ptr.h"
#include "ui/gfx/x/connection.h"
#include "ui/gfx/x/future.h"
#include "ui/gfx/x/keysyms/keysyms.h"
#include "ui/gfx/x/xkb.h"
#include "ui/gfx/x/xproto.h"

namespace x11 {

namespace {

constexpr KeySym kNoSymbol = static_cast<KeySym>(0);

void ConvertCaseImpl(uint32_t sym, uint32_t* lower, uint32_t* upper);

void ConvertCase(KeySym sym, KeySym* lower, KeySym* upper) {
  uint32_t lower32;
  uint32_t upper32;
  ConvertCaseImpl(static_cast<uint32_t>(sym), &lower32, &upper32);
  *lower = static_cast<KeySym>(lower32);
  *upper = static_cast<KeySym>(upper32);
}

bool IsPublicOrPrivateKeypadKey(KeySym keysym) {
  auto key = static_cast<uint32_t>(keysym);
  return (key >= XK_KP_Space && key <= XK_KP_Equal) ||
         (key >= 0x11000000 && key <= 0x1100FFFF);
}

int GetXkbGroupFromState(int state) {
  return (state >> 13) & 0x3;
}

#include "third_party/libx11/src/KeyBind.c"
#include "third_party/libx11/src/xkb/XKBBind.c"
#include "third_party/libxcb-keysyms/keysyms/keysyms.c"

}  // namespace

KeyboardState::KeyboardState() = default;

KeyboardState::~KeyboardState() = default;

// Non-XKB (core protocol) implementation of KeysymToKeycode and
// KeycodeToKeysym.
class CoreKeyboardState : public KeyboardState {
 public:
  explicit CoreKeyboardState(Connection* connection) : connection_(connection) {
    UpdateMapping();
  }

  ~CoreKeyboardState() override = default;

  KeyCode KeysymToKeycode(uint32_t keysym) const override {
    auto min_keycode = static_cast<uint8_t>(connection_->setup().min_keycode);
    auto max_keycode = static_cast<uint8_t>(connection_->setup().max_keycode);
    int count = max_keycode - min_keycode + 1;
    CHECK_EQ(count * keyboard_mapping_.keysyms_per_keycode,
             static_cast<int>(keyboard_mapping_.keysyms.size()));
    for (size_t i = 0; i < keyboard_mapping_.keysyms.size(); i++) {
      auto keycode = min_keycode + i / keyboard_mapping_.keysyms_per_keycode;
      if (keyboard_mapping_.keysyms[i] == static_cast<KeySym>(keysym))
        return static_cast<KeyCode>(keycode);
    }
    return {};
  }

  uint32_t KeycodeToKeysym(KeyCode keycode, uint32_t modifiers) const override {
    auto sym = static_cast<uint32_t>(KeycodeToKeysymCoreImpl(
        keycode, modifiers, connection_, keyboard_mapping_, lock_meaning_,
        mode_switch_, num_lock_));
    return sym == XK_VoidSymbol ? 0 : sym;
  }

 private:
  void UpdateMapping() override {
    UpdateMappingImpl(connection_, &keyboard_mapping_, &lock_meaning_,
                      &mode_switch_, &num_lock_);
  }

  const raw_ptr<Connection> connection_;
  GetKeyboardMappingReply keyboard_mapping_;
  uint16_t lock_meaning_ = 0;
  uint8_t mode_switch_ = 0;
  uint8_t num_lock_ = 0;
};

// XKB implementation of KeysymToKeycode and KeycodeToKeysym.
class XkbKeyboardState : public KeyboardState {
 public:
  explicit XkbKeyboardState(Connection* connection) : connection_(connection) {
    UpdateMapping();
  }

  ~XkbKeyboardState() override = default;

  KeyCode KeysymToKeycode(uint32_t keysym) const override {
    int first_keycode = static_cast<int>(map_.firstKeySym);
    for (int keycode = 0; keycode < map_.nKeySyms; keycode++) {
      for (auto sym : map_.syms_rtrn->at(keycode).syms) {
        if (static_cast<uint32_t>(sym) == keysym)
          return static_cast<KeyCode>(keycode + first_keycode);
      }
    }
    return {};
  }

  uint32_t KeycodeToKeysym(KeyCode key, uint32_t modifiers) const override {
    return KeycodeToKeysymXkbImpl(key, modifiers, map_);
  }

 private:
  void UpdateMapping() override {
    auto future = connection_->xkb().GetMap(
        {static_cast<Xkb::DeviceSpec>(Xkb::Id::UseCoreKbd),
         Xkb::MapPart::KeyTypes | Xkb::MapPart::KeySyms});
    if (auto response = future.Sync())
      map_ = std::move(*response.reply);
  }

  const raw_ptr<Connection> connection_;
  Xkb::GetMapReply map_;
};

std::unique_ptr<KeyboardState> CreateKeyboardState(Connection* connection) {
  if (connection->xkb().present())
    return std::make_unique<XkbKeyboardState>(connection);
  return std::make_unique<CoreKeyboardState>(connection);
}

}  // namespace x11