OLD | NEW |
(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_key_mapper.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/time/default_tick_clock.h" |
| 11 #include "base/time/time.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 X11KeyMapper::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 X11KeyMapper::X11KeyMapper(X11Keyboard* keyboard) |
| 36 : keyboard_(keyboard), clock_(new base::DefaultTickClock()) { |
| 37 std::vector<uint32_t> keycodes = keyboard_->GetUnusedKeycodes(); |
| 38 for (int keycode : keycodes) { |
| 39 available_keycodes_.push_back({keycode, base::TimeTicks()}); |
| 40 } |
| 41 std::sort(available_keycodes_.begin(), available_keycodes_.end()); |
| 42 } |
| 43 |
| 44 X11KeyMapper::~X11KeyMapper() { |
| 45 // Clear all used key mappings. |
| 46 for (const KeyInfo& info : available_keycodes_) { |
| 47 if (!info.expire_at.is_null()) { |
| 48 keyboard_->ChangeKeyMapping(info.keycode, 0); |
| 49 } |
| 50 } |
| 51 keyboard_->Sync(); |
| 52 } |
| 53 |
| 54 X11KeyMapper::MapResult X11KeyMapper::MapCharacter(uint32_t code_point) { |
| 55 MapResult result{false, 0, 0, base::TimeDelta()}; |
| 56 if (keyboard_->FindKeycode(code_point, &result.keycode, &result.modifiers)) { |
| 57 uint32_t keycode = result.keycode; |
| 58 auto position = std::find_if( |
| 59 available_keycodes_.begin(), available_keycodes_.end(), |
| 60 [keycode](const KeyInfo& info) { return info.keycode == keycode; }); |
| 61 if (position != available_keycodes_.end()) { |
| 62 ResetKeyInfoExpirationTime(position); |
| 63 } |
| 64 result.success = true; |
| 65 return result; |
| 66 } |
| 67 |
| 68 if (available_keycodes_.empty()) { |
| 69 return result; |
| 70 } |
| 71 |
| 72 KeyInfo& info = available_keycodes_.front(); |
| 73 |
| 74 if (info.expire_at > clock_->NowTicks()) { |
| 75 result.retry_after = info.expire_at - clock_->NowTicks(); |
| 76 return result; |
| 77 } |
| 78 |
| 79 if (!keyboard_->ChangeKeyMapping(info.keycode, code_point)) { |
| 80 return result; |
| 81 } |
| 82 |
| 83 result.success = true; |
| 84 result.keycode = info.keycode; |
| 85 |
| 86 // Modifiers can always be 0 since |code_point| is mapped to both upper and |
| 87 // lower case. |
| 88 |
| 89 ResetKeyInfoExpirationTime(available_keycodes_.begin()); |
| 90 |
| 91 keyboard_->Sync(); |
| 92 return result; |
| 93 } |
| 94 |
| 95 void X11KeyMapper::SetClockForTesting(std::unique_ptr<base::TickClock> clock) { |
| 96 clock_ = std::move(clock); |
| 97 } |
| 98 |
| 99 void X11KeyMapper::ResetKeyInfoExpirationTime( |
| 100 std::vector<KeyInfo>::iterator position) { |
| 101 position->expire_at = clock_->NowTicks() + kMappingExpireDuration; |
| 102 while (position + 1 < available_keycodes_.end() && |
| 103 *(position + 1) < *position) { |
| 104 std::swap(*position, *(position + 1)); |
| 105 position++; |
| 106 } |
| 107 } |
| 108 |
| 109 } // namespace remoting |
OLD | NEW |