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), |
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() { |
159 repeat_key_ = KEY_RESERVED; | 164 repeat_key_ = KEY_RESERVED; |
160 repeat_sequence_++; | 165 repeat_sequence_++; |
161 } | 166 } |
162 | 167 |
163 void KeyboardEvdev::ScheduleKeyRepeat(const base::TimeDelta& delay) { | 168 void KeyboardEvdev::ScheduleKeyRepeat(const base::TimeDelta& delay) { |
164 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 169 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
165 FROM_HERE, base::Bind(&KeyboardEvdev::OnRepeatTimeout, | 170 FROM_HERE, base::Bind(&KeyboardEvdev::OnRepeatTimeout, |
166 weak_ptr_factory_.GetWeakPtr(), repeat_sequence_), | 171 weak_ptr_factory_.GetWeakPtr(), repeat_sequence_), |
167 delay); | 172 delay); |
168 } | 173 } |
169 | 174 |
170 void KeyboardEvdev::OnRepeatTimeout(unsigned int sequence) { | 175 void KeyboardEvdev::OnRepeatTimeout(unsigned int sequence) { |
171 if (repeat_sequence_ != sequence) | 176 if (repeat_sequence_ != sequence) |
172 return; | 177 return; |
173 | 178 |
174 DispatchKey(repeat_key_, true /* down */, true /* repeat */, | 179 DispatchKey(repeat_key_, true /* down */, true /* repeat */, |
175 EventTimeForNow()); | 180 EventTimeForNow(), repeat_device_id_); |
176 | 181 |
177 ScheduleKeyRepeat(repeat_interval_); | 182 ScheduleKeyRepeat(repeat_interval_); |
178 } | 183 } |
179 | 184 |
180 void KeyboardEvdev::DispatchKey(unsigned int key, | 185 void KeyboardEvdev::DispatchKey(unsigned int key, |
181 bool down, | 186 bool down, |
182 bool repeat, | 187 bool repeat, |
183 base::TimeDelta timestamp) { | 188 base::TimeDelta timestamp, |
| 189 int device_id) { |
184 DomCode dom_code = | 190 DomCode dom_code = |
185 KeycodeConverter::NativeKeycodeToDomCode(EvdevCodeToNativeCode(key)); | 191 KeycodeConverter::NativeKeycodeToDomCode(EvdevCodeToNativeCode(key)); |
186 // DomCode constants are not included here because of conflicts with | 192 // DomCode constants are not included here because of conflicts with |
187 // evdev preprocessor macros. | 193 // evdev preprocessor macros. |
188 if (!static_cast<int>(dom_code)) | 194 if (!static_cast<int>(dom_code)) |
189 return; | 195 return; |
190 int flags = modifiers_->GetModifierFlags(); | 196 int flags = modifiers_->GetModifierFlags(); |
191 DomKey dom_key; | 197 DomKey dom_key; |
192 KeyboardCode key_code; | 198 KeyboardCode key_code; |
193 uint16 character; | 199 uint16 character; |
194 uint32 platform_keycode = 0; | 200 uint32 platform_keycode = 0; |
195 if (!keyboard_layout_engine_->Lookup(dom_code, flags, &dom_key, &character, | 201 if (!keyboard_layout_engine_->Lookup(dom_code, flags, &dom_key, &character, |
196 &key_code, &platform_keycode)) { | 202 &key_code, &platform_keycode)) { |
197 return; | 203 return; |
198 } | 204 } |
199 if (!repeat) | 205 if (!repeat) |
200 UpdateModifier(ModifierDomKeyToEventFlag(dom_key), down); | 206 UpdateModifier(ModifierDomKeyToEventFlag(dom_key), down); |
201 | 207 |
202 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, |
203 modifiers_->GetModifierFlags(), dom_key, character, timestamp); | 209 modifiers_->GetModifierFlags(), dom_key, character, timestamp); |
| 210 event.set_source_device_id(device_id); |
204 if (platform_keycode) | 211 if (platform_keycode) |
205 event.set_platform_keycode(platform_keycode); | 212 event.set_platform_keycode(platform_keycode); |
206 callback_.Run(&event); | 213 callback_.Run(&event); |
207 } | 214 } |
208 | 215 |
209 } // namespace ui | 216 } // namespace ui |
OLD | NEW |