Index: remoting/host/linux/x11_key_mapper.cc |
diff --git a/remoting/host/linux/x11_key_mapper.cc b/remoting/host/linux/x11_key_mapper.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b7af88dd6558d08ca51421612e465767790e73d0 |
--- /dev/null |
+++ b/remoting/host/linux/x11_key_mapper.cc |
@@ -0,0 +1,109 @@ |
+// Copyright 2016 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. |
+ |
+#include "remoting/host/linux/x11_key_mapper.h" |
+ |
+#include <algorithm> |
+ |
+#include "base/memory/ptr_util.h" |
+#include "base/time/default_tick_clock.h" |
+#include "base/time/time.h" |
+#include "remoting/host/linux/x11_keyboard.h" |
+ |
+namespace { |
+ |
+constexpr base::TimeDelta kMappingExpireDuration = |
+ base::TimeDelta::FromMilliseconds(200); |
+ |
+} // namespace |
+ |
+namespace remoting { |
+ |
+struct X11KeyMapper::KeyInfo { |
+ uint32_t keycode; |
+ base::TimeTicks expire_at; |
+ |
+ bool operator<(const KeyInfo& other) const { |
+ // When expire time are the same, prefer larger keycode for higher test |
+ // predictability and less impact on more important layout. |
+ return expire_at != other.expire_at ? expire_at < other.expire_at |
+ : keycode > other.keycode; |
+ } |
+}; |
+ |
+X11KeyMapper::X11KeyMapper(X11Keyboard* keyboard) |
+ : keyboard_(keyboard), clock_(new base::DefaultTickClock()) { |
+ std::vector<uint32_t> keycodes = keyboard_->GetUnusedKeycodes(); |
+ for (int keycode : keycodes) { |
+ available_keycodes_.push_back({keycode, base::TimeTicks()}); |
+ } |
+ std::sort(available_keycodes_.begin(), available_keycodes_.end()); |
+} |
+ |
+X11KeyMapper::~X11KeyMapper() { |
+ // Clear all used key mappings. |
+ for (const KeyInfo& info : available_keycodes_) { |
+ if (!info.expire_at.is_null()) { |
+ keyboard_->ChangeKeyMapping(info.keycode, 0); |
+ } |
+ } |
+ keyboard_->Sync(); |
+} |
+ |
+X11KeyMapper::MapResult X11KeyMapper::MapCharacter(uint32_t code_point) { |
+ MapResult result{false, 0, 0, base::TimeDelta()}; |
+ if (keyboard_->FindKeycode(code_point, &result.keycode, &result.modifiers)) { |
+ uint32_t keycode = result.keycode; |
+ auto position = std::find_if( |
+ available_keycodes_.begin(), available_keycodes_.end(), |
+ [keycode](const KeyInfo& info) { return info.keycode == keycode; }); |
+ if (position != available_keycodes_.end()) { |
+ ResetKeyInfoExpirationTime(position); |
+ } |
+ result.success = true; |
+ return result; |
+ } |
+ |
+ if (available_keycodes_.empty()) { |
+ return result; |
+ } |
+ |
+ KeyInfo& info = available_keycodes_.front(); |
+ |
+ if (info.expire_at > clock_->NowTicks()) { |
+ result.retry_after = info.expire_at - clock_->NowTicks(); |
+ return result; |
+ } |
+ |
+ if (!keyboard_->ChangeKeyMapping(info.keycode, code_point)) { |
+ return result; |
+ } |
+ |
+ result.success = true; |
+ result.keycode = info.keycode; |
+ |
+ // Modifiers can always be 0 since |code_point| is mapped to both upper and |
+ // lower case. |
+ |
+ ResetKeyInfoExpirationTime(available_keycodes_.begin()); |
+ |
+ keyboard_->Sync(); |
+ return result; |
+} |
+ |
+void X11KeyMapper::SetClockForTesting(std::unique_ptr<base::TickClock> clock) { |
+ clock_ = std::move(clock); |
+} |
+ |
+void X11KeyMapper::ResetKeyInfoExpirationTime( |
+ std::vector<KeyInfo>::iterator position) { |
+ position->expire_at = clock_->NowTicks() + kMappingExpireDuration; |
+ while (position + 1 < available_keycodes_.end() && |
+ *(position + 1) < *position) { |
+ std::swap(*position, *(position + 1)); |
+ position++; |
+ } |
+} |
+ |
+} // namespace remoting |