File: x11_keyboard_hook.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (129 lines) | stat: -rw-r--r-- 4,440 bytes parent folder | download | duplicates (7)
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
// Copyright 2021 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/ozone/platform/x11/x11_keyboard_hook.h"

#include <memory>
#include <utility>

#include "base/check_op.h"
#include "ui/events/event.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/dom/keycode_converter.h"
#include "ui/gfx/x/future.h"
#include "ui/gfx/x/xproto.h"

namespace ui {

namespace {

static X11KeyboardHook* g_instance = nullptr;

// GrabKey essentially requires the modifier mask to explicitly be specified.
// One can specify 'x11::ModMask::Any' however doing so means the call to
// GrabKey will fail if that key has been grabbed with any combination of
// modifiers.  The common practice is to call GrabKey with each individual
// modifier mask to avoid that problem.
const x11::ModMask kModifierMasks[] = {
    {},                  // No additional modifier.
    x11::ModMask::c_2,   // Num lock
    x11::ModMask::Lock,  // Caps lock
    x11::ModMask::c_5,   // Scroll lock
    x11::ModMask::c_2 | x11::ModMask::Lock,
    x11::ModMask::c_2 | x11::ModMask::c_5,
    x11::ModMask::Lock | x11::ModMask::c_5,
    x11::ModMask::c_2 | x11::ModMask::Lock | x11::ModMask::c_5};

// This is the set of keys to lock when the website requests that all keys be
// locked.
const DomCode kDomCodesForLockAllKeys[] = {
    DomCode::ESCAPE,        DomCode::CONTEXT_MENU, DomCode::CONTROL_LEFT,
    DomCode::SHIFT_LEFT,    DomCode::ALT_LEFT,     DomCode::META_LEFT,
    DomCode::CONTROL_RIGHT, DomCode::SHIFT_RIGHT,  DomCode::ALT_RIGHT,
    DomCode::META_RIGHT};

}  // namespace

X11KeyboardHook::X11KeyboardHook(
    std::optional<base::flat_set<DomCode>> dom_codes,
    BaseKeyboardHook::KeyEventCallback callback,
    gfx::AcceleratedWidget accelerated_widget)
    : BaseKeyboardHook(std::move(dom_codes), std::move(callback)),
      connection_(x11::Connection::Get()),
      window_(static_cast<x11::Window>(accelerated_widget)) {
  RegisterHook(this->dom_codes());
}

X11KeyboardHook::~X11KeyboardHook() {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);

  DCHECK_EQ(g_instance, this);
  g_instance = nullptr;

  // Use UngrabKey for each key that has been grabbed.  UngrabKeyboard
  // purportedly releases all keys when called and would not require the nested
  // loops, however in practice the keys are not actually released.
  for (int native_key_code : grabbed_keys_) {
    for (auto modifier : kModifierMasks) {
      connection_->UngrabKey(
          {static_cast<x11::KeyCode>(native_key_code), window_, modifier});
    }
  }
}

void X11KeyboardHook::RegisterHook(
    const std::optional<base::flat_set<DomCode>>& dom_codes) {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);

  // Only one instance of this class can be registered at a time.
  DCHECK(!g_instance);
  g_instance = this;

  if (dom_codes.has_value()) {
    CaptureSpecificKeys(dom_codes);
  } else {
    CaptureAllKeys();
  }
}

void X11KeyboardHook::CaptureAllKeys() {
  // We could have used the XGrabKeyboard API here instead of calling XGrabKeys
  // on a hard-coded set of shortcut keys.  Calling XGrabKeyboard would make
  // this work much simpler, however it has side-effects which prevents its use.
  // An example side-effect is that it prevents the lock screen from starting as
  // the screensaver process also calls XGrabKeyboard but will receive an error
  // since it was already grabbed by the window with KeyboardLock.
  for (auto kDomCodesForLockAllKey : kDomCodesForLockAllKeys) {
    CaptureKeyForDomCode(kDomCodesForLockAllKey);
  }
}

void X11KeyboardHook::CaptureSpecificKeys(
    const std::optional<base::flat_set<DomCode>>& dom_codes) {
  for (DomCode dom_code : dom_codes.value()) {
    CaptureKeyForDomCode(dom_code);
  }
}

void X11KeyboardHook::CaptureKeyForDomCode(DomCode dom_code) {
  int native_key_code = KeycodeConverter::DomCodeToNativeKeycode(dom_code);
  if (native_key_code == KeycodeConverter::InvalidNativeKeycode()) {
    return;
  }

  for (auto modifier : kModifierMasks) {
    connection_->GrabKey({
        .owner_events = false,
        .grab_window = window_,
        .modifiers = modifier,
        .key = static_cast<x11::KeyCode>(native_key_code),
        .pointer_mode = x11::GrabMode::Async,
        .keyboard_mode = x11::GrabMode::Async,
    });
  }

  grabbed_keys_.push_back(native_key_code);
}

}  // namespace ui