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

Side by Side Diff: remoting/host/linux/x11_character_injector.cc

Issue 2346643003: [Remoting Host] Handle text event characters that are not presented on the keyboard (Closed)
Patch Set: Remove mock keyboard reference Created 4 years, 2 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 2016 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 "remoting/host/linux/x11_character_injector.h"
6
7 #include <X11/XKBlib.h>
8
9 #include <algorithm>
10
11 #include "base/bind.h"
12 #include "remoting/host/linux/x11_keyboard.h"
13
14 namespace {
15
16 constexpr base::TimeDelta kMappingExpireDuration =
17 base::TimeDelta::FromMilliseconds(200);
18
19 } // namespace
20
21 namespace remoting {
22
23 struct X11CharacterInjector::KeyInfo {
24 uint32_t keycode;
25 base::TimeTicks expire_at;
26
27 bool operator<(const KeyInfo& other) const {
28 // When expire time are the same, prefer larger keycode for higher test
29 // predictability and less impact on more important layout.
30 return expire_at != other.expire_at ? expire_at < other.expire_at
31 : keycode > other.keycode;
32 }
33 };
34
35 struct X11CharacterInjector::MapResult {
36 bool success;
37
38 uint32_t keycode;
39 uint32_t modifiers;
40
41 // If success == false and |retry_after| is not zero, user may retry
42 // AddNewCharacter() after |retry_after| has elapsed.
43 base::TimeDelta retry_after;
44 };
45
46 X11CharacterInjector::X11CharacterInjector(
47 std::unique_ptr<X11Keyboard> keyboard)
48 : keyboard_(std::move(keyboard)), weak_factory_(this) {
49 std::vector<uint32_t> keycodes = keyboard_->GetUnusedKeycodes();
50 for (int keycode : keycodes) {
51 available_keycodes_.push_back({keycode, base::TimeTicks()});
52 }
53 std::sort(available_keycodes_.begin(), available_keycodes_.end());
54 }
55
56 X11CharacterInjector::~X11CharacterInjector() {
57 // Clear all used key mappings.
58 for (const KeyInfo& info : available_keycodes_) {
59 if (!info.expire_at.is_null()) {
60 keyboard_->ChangeKeyMapping(info.keycode, 0);
61 }
62 }
63 keyboard_->Sync();
64 }
65
66 void X11CharacterInjector::Inject(uint32_t code_point) {
67 DCHECK(thread_checker_.CalledOnValidThread());
68 characters_queue_.push(code_point);
69 Schedule(base::TimeDelta());
70 }
71
72 void X11CharacterInjector::Schedule(base::TimeDelta delay) {
73 DCHECK(thread_checker_.CalledOnValidThread());
74 base::TimeTicks expected_consume_time = base::TimeTicks::Now() + delay;
75 if (consume_timer_.IsRunning() &&
76 scheduled_consume_time_ > expected_consume_time) {
77 return;
78 }
79 consume_timer_.Start(FROM_HERE, delay, this, &X11CharacterInjector::Consume);
80 scheduled_consume_time_ = expected_consume_time;
81 }
82
83 void X11CharacterInjector::Consume() {
84 DCHECK(thread_checker_.CalledOnValidThread());
85 DCHECK(!scheduled_consume_time_.is_null() &&
86 base::TimeTicks::Now() >= scheduled_consume_time_);
87 scheduled_consume_time_ = base::TimeTicks();
88 while (!characters_queue_.empty()) {
89 uint32_t code_point = characters_queue_.front();
90 MapResult result = MapCharacter(code_point);
91 if (!result.success) {
92 if (result.retry_after.is_zero()) {
93 continue;
94 }
95 Schedule(result.retry_after);
96 break;
97 }
98 keyboard_->PressKey(result.keycode, result.modifiers);
99
100 characters_queue_.pop();
101 }
102
103 keyboard_->Flush();
104 }
105
106 X11CharacterInjector::MapResult X11CharacterInjector::MapCharacter(
107 uint32_t code_point) {
108 MapResult result{false, 0, 0, base::TimeDelta()};
109 if (keyboard_->FindKeycode(code_point, &result.keycode, &result.modifiers)) {
110 uint32_t keycode = result.keycode;
111 auto position = std::find_if(
112 available_keycodes_.begin(), available_keycodes_.end(),
113 [keycode](const KeyInfo& info) { return info.keycode == keycode; });
114 if (position != available_keycodes_.end()) {
115 ResetKeyInfoExpirationTime(position);
116 }
117 result.success = true;
118 return result;
119 }
120
121 if (available_keycodes_.empty()) {
122 return result;
123 }
124
125 KeyInfo& info = available_keycodes_.front();
126
127 base::TimeTicks now = base::TimeTicks::Now();
128 if (info.expire_at > now) {
129 result.retry_after = info.expire_at - now;
130 return result;
131 }
132
133 if (!keyboard_->ChangeKeyMapping(info.keycode, code_point)) {
134 return result;
135 }
136
137 result.success = true;
138 result.keycode = info.keycode;
139
140 // Modifiers can always be 0 since |code_point| is mapped to both upper and
141 // lower case.
142
143 ResetKeyInfoExpirationTime(available_keycodes_.begin());
144
145 keyboard_->Sync();
146 return result;
147 }
148
149 void X11CharacterInjector::ResetKeyInfoExpirationTime(
150 std::vector<KeyInfo>::iterator position) {
151 position->expire_at = base::TimeTicks::Now() + kMappingExpireDuration;
152 while (position + 1 < available_keycodes_.end() &&
153 *(position + 1) < *position) {
154 std::swap(*position, *(position + 1));
155 position++;
156 }
157 }
158
159 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698