Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: components/keyboard_lock/platform_key_hook_x11.cc

Issue 2879033002: Keyboard Lock Host implementation
Patch Set: Remove useless files Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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.
4
5 #include "components/keyboard_lock/platform_key_hook.h"
6
7 #include <X11/Xlib.h>
8 #undef Status
9
10 #include <unordered_set>
11 #include <vector>
12
13 #include "base/logging.h"
14 #include "base/macros.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "components/keyboard_lock/platform_key_event_filter.h"
18 #include "ui/aura/window.h"
19 #include "ui/aura/window_tree_host.h"
20 #include "ui/base/x/x11_window_event_manager.h"
21 #include "ui/events/platform/x11/x11_event_source.h"
22 #include "ui/gfx/x/x11_error_tracker.h"
23 #include "ui/gfx/x/x11_types.h"
24
25 namespace keyboard_lock {
26
27 namespace {
28
29 const unsigned int kSingleStates[] = { ShiftMask,
30 LockMask,
31 ControlMask,
32 Mod1Mask,
33 Mod2Mask,
34 Mod3Mask,
35 Mod4Mask,
36 Mod5Mask };
37
38 std::vector<int> all_states;
39
40 void InitializeStates() {
41 if (!all_states.empty()) {
42 return;
43 }
44
45 std::unordered_set<int> states {
46 kSingleStates, kSingleStates + arraysize(kSingleStates) };
47 for (size_t i = 0; i < arraysize(kSingleStates); i++) {
48 std::vector<int> new_states;
49 for (auto state : states) {
50 for (size_t j = 0; j < arraysize(kSingleStates); j++) {
51 new_states.push_back(state | kSingleStates[j]);
52 }
53 }
54 states.insert(new_states.begin(), new_states.end());
55 }
56
57 all_states = std::vector<int> { states.begin(), states.end() };
58 }
59
60 } // namespace
61
62 PlatformKeyHook::PlatformKeyHook(Browser* owner, KeyEventFilter* filter)
63 : owner_(owner),
64 filter_(filter) {
65 DCHECK(owner_);
66 ui::X11EventSource::GetInstance()->set_platform_key_event_filter(&filter_);
67 }
68
69 PlatformKeyHook::~PlatformKeyHook() = default;
70
71 bool PlatformKeyHook::RegisterKey(const std::vector<ui::KeyboardCode>& codes) {
72 InitializeStates();
73 gfx::X11ErrorTracker x11_error_tracker;
74 Display* display = gfx::GetXDisplay();
75 if (owner_->window() == nullptr ||
76 owner_->window()->GetNativeWindow() == nullptr ||
77 owner_->window()->GetNativeWindow()->GetHost() == nullptr) {
78 LOG(ERROR) << "No native window found.";
79 // This should not happen in production, JavaScript cannot be executed
80 // before CreateBrowserWindow().
81 return false;
82 }
83 const Window root =
84 owner_->window()->GetNativeWindow()->GetHost()->GetAcceleratedWidget();
85 for (ui::KeyboardCode code : codes) {
86 for (auto state : all_states) {
87 XGrabKey(display, code, state, root, False, GrabModeAsync, GrabModeAsync);
88 }
89 }
90 return true;
91 }
92
93 bool PlatformKeyHook::UnregisterKey(
94 const std::vector<ui::KeyboardCode>& codes) {
95 InitializeStates();
96 gfx::X11ErrorTracker x11_error_tracker;
97 Display* display = gfx::GetXDisplay();
98 const Window root = DefaultRootWindow(display);
99 for (ui::KeyboardCode code : codes) {
100 for (auto state : all_states) {
101 XUngrabKey(display, code, state, root);
102 }
103 }
104 return true;
105 }
106
107 } // namespace keyboard_lock
OLDNEW
« no previous file with comments | « components/keyboard_lock/platform_key_hook_win.cc ('k') | components/keyboard_lock/register_once_key_hook.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698