OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/events/ozone/evdev/keyboard_evdev.h" | 5 #include "ui/events/ozone/evdev/keyboard_evdev.h" |
6 | 6 |
7 #include "base/single_thread_task_runner.h" | 7 #include "base/single_thread_task_runner.h" |
8 #include "base/thread_task_runner_handle.h" | 8 #include "base/thread_task_runner_handle.h" |
9 #include "ui/events/event.h" | 9 #include "ui/events/event.h" |
10 #include "ui/events/event_constants.h" | 10 #include "ui/events/event_constants.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
56 | 56 |
57 KeyboardEvdev::KeyboardEvdev(EventModifiersEvdev* modifiers, | 57 KeyboardEvdev::KeyboardEvdev(EventModifiersEvdev* modifiers, |
58 KeyboardLayoutEngine* keyboard_layout_engine, | 58 KeyboardLayoutEngine* keyboard_layout_engine, |
59 const EventDispatchCallback& callback) | 59 const EventDispatchCallback& callback) |
60 : callback_(callback), | 60 : callback_(callback), |
61 modifiers_(modifiers), | 61 modifiers_(modifiers), |
62 keyboard_layout_engine_(keyboard_layout_engine), | 62 keyboard_layout_engine_(keyboard_layout_engine), |
63 repeat_enabled_(true), | 63 repeat_enabled_(true), |
64 repeat_key_(KEY_RESERVED), | 64 repeat_key_(KEY_RESERVED), |
65 repeat_sequence_(0), | 65 repeat_sequence_(0), |
66 repeat_device_id_(0), | |
spang
2015/04/08 22:54:30
We're using -1 for invalid/synthetic/injected devi
spang
2015/04/08 22:56:53
Oh, it appears I'm wrong. Nevermind.
kpschoedel
2015/04/09 18:51:41
Acknowledged.
| |
66 weak_ptr_factory_(this) { | 67 weak_ptr_factory_(this) { |
67 repeat_delay_ = base::TimeDelta::FromMilliseconds(kRepeatDelayMs); | 68 repeat_delay_ = base::TimeDelta::FromMilliseconds(kRepeatDelayMs); |
68 repeat_interval_ = base::TimeDelta::FromMilliseconds(kRepeatIntervalMs); | 69 repeat_interval_ = base::TimeDelta::FromMilliseconds(kRepeatIntervalMs); |
69 } | 70 } |
70 | 71 |
71 KeyboardEvdev::~KeyboardEvdev() { | 72 KeyboardEvdev::~KeyboardEvdev() { |
72 } | 73 } |
73 | 74 |
74 void KeyboardEvdev::OnKeyChange(unsigned int key, | 75 void KeyboardEvdev::OnKeyChange(unsigned int key, |
75 bool down, | 76 bool down, |
76 base::TimeDelta timestamp) { | 77 base::TimeDelta timestamp, |
78 int device_id) { | |
77 if (key > KEY_MAX) | 79 if (key > KEY_MAX) |
78 return; | 80 return; |
79 | 81 |
80 if (down == key_state_.test(key)) | 82 if (down == key_state_.test(key)) |
81 return; | 83 return; |
82 | 84 |
83 // State transition: !(down) -> (down) | 85 // State transition: !(down) -> (down) |
84 if (down) | 86 if (down) |
85 key_state_.set(key); | 87 key_state_.set(key); |
86 else | 88 else |
87 key_state_.reset(key); | 89 key_state_.reset(key); |
88 | 90 |
89 UpdateKeyRepeat(key, down); | 91 UpdateKeyRepeat(key, down, device_id); |
90 DispatchKey(key, down, false /* repeat */, timestamp); | 92 DispatchKey(key, down, false /* repeat */, timestamp, device_id); |
91 } | 93 } |
92 | 94 |
93 void KeyboardEvdev::SetCapsLockEnabled(bool enabled) { | 95 void KeyboardEvdev::SetCapsLockEnabled(bool enabled) { |
94 modifiers_->SetModifierLock(EVDEV_MODIFIER_CAPS_LOCK, enabled); | 96 modifiers_->SetModifierLock(EVDEV_MODIFIER_CAPS_LOCK, enabled); |
95 } | 97 } |
96 | 98 |
97 bool KeyboardEvdev::IsCapsLockEnabled() { | 99 bool KeyboardEvdev::IsCapsLockEnabled() { |
98 return (modifiers_->GetModifierFlags() & EF_CAPS_LOCK_DOWN) != 0; | 100 return (modifiers_->GetModifierFlags() & EF_CAPS_LOCK_DOWN) != 0; |
99 } | 101 } |
100 | 102 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
132 // key may or may not be down, but usually isn't). There does need to | 134 // key may or may not be down, but usually isn't). There does need to |
133 // to be two different flags, since the physical CapsLock key is subject | 135 // to be two different flags, since the physical CapsLock key is subject |
134 // to remapping, but the caps lock state (which can be triggered in a | 136 // to remapping, but the caps lock state (which can be triggered in a |
135 // variety of ways) is not. | 137 // variety of ways) is not. |
136 if (modifier == EVDEV_MODIFIER_CAPS_LOCK) | 138 if (modifier == EVDEV_MODIFIER_CAPS_LOCK) |
137 modifiers_->UpdateModifier(EVDEV_MODIFIER_MOD3, down); | 139 modifiers_->UpdateModifier(EVDEV_MODIFIER_MOD3, down); |
138 else | 140 else |
139 modifiers_->UpdateModifier(modifier, down); | 141 modifiers_->UpdateModifier(modifier, down); |
140 } | 142 } |
141 | 143 |
142 void KeyboardEvdev::UpdateKeyRepeat(unsigned int key, bool down) { | 144 void KeyboardEvdev::UpdateKeyRepeat(unsigned int key, |
145 bool down, | |
146 int device_id) { | |
143 if (!repeat_enabled_) | 147 if (!repeat_enabled_) |
144 StopKeyRepeat(); | 148 StopKeyRepeat(); |
145 else if (key != repeat_key_ && down) | 149 else if (key != repeat_key_ && down) |
146 StartKeyRepeat(key); | 150 StartKeyRepeat(key, device_id); |
147 else if (key == repeat_key_ && !down) | 151 else if (key == repeat_key_ && !down) |
148 StopKeyRepeat(); | 152 StopKeyRepeat(); |
149 } | 153 } |
150 | 154 |
151 void KeyboardEvdev::StartKeyRepeat(unsigned int key) { | 155 void KeyboardEvdev::StartKeyRepeat(unsigned int key, int device_id) { |
152 repeat_key_ = key; | 156 repeat_key_ = key; |
157 repeat_device_id_ = device_id; | |
153 repeat_sequence_++; | 158 repeat_sequence_++; |
154 | 159 |
155 ScheduleKeyRepeat(repeat_delay_); | 160 ScheduleKeyRepeat(repeat_delay_); |
156 } | 161 } |
157 | 162 |
158 void KeyboardEvdev::StopKeyRepeat() { | 163 void KeyboardEvdev::StopKeyRepeat() { |
164 repeat_key_ = KEY_RESERVED; | |
159 repeat_sequence_++; | 165 repeat_sequence_++; |
160 } | 166 } |
161 | 167 |
162 void KeyboardEvdev::ScheduleKeyRepeat(const base::TimeDelta& delay) { | 168 void KeyboardEvdev::ScheduleKeyRepeat(const base::TimeDelta& delay) { |
163 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 169 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
164 FROM_HERE, base::Bind(&KeyboardEvdev::OnRepeatTimeout, | 170 FROM_HERE, base::Bind(&KeyboardEvdev::OnRepeatTimeout, |
165 weak_ptr_factory_.GetWeakPtr(), repeat_sequence_), | 171 weak_ptr_factory_.GetWeakPtr(), repeat_sequence_), |
166 delay); | 172 delay); |
167 } | 173 } |
168 | 174 |
169 void KeyboardEvdev::OnRepeatTimeout(unsigned int sequence) { | 175 void KeyboardEvdev::OnRepeatTimeout(unsigned int sequence) { |
170 if (repeat_sequence_ != sequence) | 176 if (repeat_sequence_ != sequence) |
171 return; | 177 return; |
172 | 178 |
173 DispatchKey(repeat_key_, true /* down */, true /* repeat */, | 179 DispatchKey(repeat_key_, true /* down */, true /* repeat */, |
174 EventTimeForNow()); | 180 EventTimeForNow(), repeat_device_id_); |
175 | 181 |
176 ScheduleKeyRepeat(repeat_interval_); | 182 ScheduleKeyRepeat(repeat_interval_); |
177 } | 183 } |
178 | 184 |
179 void KeyboardEvdev::DispatchKey(unsigned int key, | 185 void KeyboardEvdev::DispatchKey(unsigned int key, |
180 bool down, | 186 bool down, |
181 bool repeat, | 187 bool repeat, |
182 base::TimeDelta timestamp) { | 188 base::TimeDelta timestamp, |
189 int device_id) { | |
183 DomCode dom_code = | 190 DomCode dom_code = |
184 KeycodeConverter::NativeKeycodeToDomCode(EvdevCodeToNativeCode(key)); | 191 KeycodeConverter::NativeKeycodeToDomCode(EvdevCodeToNativeCode(key)); |
185 // DomCode constants are not included here because of conflicts with | 192 // DomCode constants are not included here because of conflicts with |
186 // evdev preprocessor macros. | 193 // evdev preprocessor macros. |
187 if (!static_cast<int>(dom_code)) | 194 if (!static_cast<int>(dom_code)) |
188 return; | 195 return; |
189 int flags = modifiers_->GetModifierFlags(); | 196 int flags = modifiers_->GetModifierFlags(); |
190 DomKey dom_key; | 197 DomKey dom_key; |
191 KeyboardCode key_code; | 198 KeyboardCode key_code; |
192 uint16 character; | 199 uint16 character; |
193 uint32 platform_keycode = 0; | 200 uint32 platform_keycode = 0; |
194 if (!keyboard_layout_engine_->Lookup(dom_code, flags, &dom_key, &character, | 201 if (!keyboard_layout_engine_->Lookup(dom_code, flags, &dom_key, &character, |
195 &key_code, &platform_keycode)) { | 202 &key_code, &platform_keycode)) { |
196 return; | 203 return; |
197 } | 204 } |
198 if (!repeat) | 205 if (!repeat) |
199 UpdateModifier(ModifierDomKeyToEventFlag(dom_key), down); | 206 UpdateModifier(ModifierDomKeyToEventFlag(dom_key), down); |
200 | 207 |
201 KeyEvent event(down ? ET_KEY_PRESSED : ET_KEY_RELEASED, key_code, dom_code, | 208 KeyEvent event(down ? ET_KEY_PRESSED : ET_KEY_RELEASED, key_code, dom_code, |
202 modifiers_->GetModifierFlags(), dom_key, character, timestamp); | 209 modifiers_->GetModifierFlags(), dom_key, character, timestamp); |
210 event.set_source_device_id(device_id); | |
203 if (platform_keycode) | 211 if (platform_keycode) |
204 event.set_platform_keycode(platform_keycode); | 212 event.set_platform_keycode(platform_keycode); |
205 callback_.Run(&event); | 213 callback_.Run(&event); |
206 } | 214 } |
207 | 215 |
208 } // namespace ui | 216 } // namespace ui |
OLD | NEW |