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), | |
67 weak_ptr_factory_(this) { | 66 weak_ptr_factory_(this) { |
68 repeat_delay_ = base::TimeDelta::FromMilliseconds(kRepeatDelayMs); | 67 repeat_delay_ = base::TimeDelta::FromMilliseconds(kRepeatDelayMs); |
69 repeat_interval_ = base::TimeDelta::FromMilliseconds(kRepeatIntervalMs); | 68 repeat_interval_ = base::TimeDelta::FromMilliseconds(kRepeatIntervalMs); |
70 } | 69 } |
71 | 70 |
72 KeyboardEvdev::~KeyboardEvdev() { | 71 KeyboardEvdev::~KeyboardEvdev() { |
73 } | 72 } |
74 | 73 |
75 void KeyboardEvdev::OnKeyChange(unsigned int key, | 74 void KeyboardEvdev::OnKeyChange(unsigned int key, |
76 bool down, | 75 bool down, |
77 base::TimeDelta timestamp, | 76 base::TimeDelta timestamp) { |
78 int device_id) { | |
79 if (key > KEY_MAX) | 77 if (key > KEY_MAX) |
80 return; | 78 return; |
81 | 79 |
82 if (down == key_state_.test(key)) | 80 if (down == key_state_.test(key)) |
83 return; | 81 return; |
84 | 82 |
85 // State transition: !(down) -> (down) | 83 // State transition: !(down) -> (down) |
86 if (down) | 84 if (down) |
87 key_state_.set(key); | 85 key_state_.set(key); |
88 else | 86 else |
89 key_state_.reset(key); | 87 key_state_.reset(key); |
90 | 88 |
91 UpdateKeyRepeat(key, down, device_id); | 89 UpdateKeyRepeat(key, down); |
92 DispatchKey(key, down, false /* repeat */, timestamp, device_id); | 90 DispatchKey(key, down, false /* repeat */, timestamp); |
93 } | 91 } |
94 | 92 |
95 void KeyboardEvdev::SetCapsLockEnabled(bool enabled) { | 93 void KeyboardEvdev::SetCapsLockEnabled(bool enabled) { |
96 modifiers_->SetModifierLock(EVDEV_MODIFIER_CAPS_LOCK, enabled); | 94 modifiers_->SetModifierLock(EVDEV_MODIFIER_CAPS_LOCK, enabled); |
97 } | 95 } |
98 | 96 |
99 bool KeyboardEvdev::IsCapsLockEnabled() { | 97 bool KeyboardEvdev::IsCapsLockEnabled() { |
100 return (modifiers_->GetModifierFlags() & EF_CAPS_LOCK_DOWN) != 0; | 98 return (modifiers_->GetModifierFlags() & EF_CAPS_LOCK_DOWN) != 0; |
101 } | 99 } |
102 | 100 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 // key may or may not be down, but usually isn't). There does need to | 132 // key may or may not be down, but usually isn't). There does need to |
135 // to be two different flags, since the physical CapsLock key is subject | 133 // to be two different flags, since the physical CapsLock key is subject |
136 // to remapping, but the caps lock state (which can be triggered in a | 134 // to remapping, but the caps lock state (which can be triggered in a |
137 // variety of ways) is not. | 135 // variety of ways) is not. |
138 if (modifier == EVDEV_MODIFIER_CAPS_LOCK) | 136 if (modifier == EVDEV_MODIFIER_CAPS_LOCK) |
139 modifiers_->UpdateModifier(EVDEV_MODIFIER_MOD3, down); | 137 modifiers_->UpdateModifier(EVDEV_MODIFIER_MOD3, down); |
140 else | 138 else |
141 modifiers_->UpdateModifier(modifier, down); | 139 modifiers_->UpdateModifier(modifier, down); |
142 } | 140 } |
143 | 141 |
144 void KeyboardEvdev::UpdateKeyRepeat(unsigned int key, | 142 void KeyboardEvdev::UpdateKeyRepeat(unsigned int key, bool down) { |
145 bool down, | |
146 int device_id) { | |
147 if (!repeat_enabled_) | 143 if (!repeat_enabled_) |
148 StopKeyRepeat(); | 144 StopKeyRepeat(); |
149 else if (key != repeat_key_ && down) | 145 else if (key != repeat_key_ && down) |
150 StartKeyRepeat(key, device_id); | 146 StartKeyRepeat(key); |
151 else if (key == repeat_key_ && !down) | 147 else if (key == repeat_key_ && !down) |
152 StopKeyRepeat(); | 148 StopKeyRepeat(); |
153 } | 149 } |
154 | 150 |
155 void KeyboardEvdev::StartKeyRepeat(unsigned int key, int device_id) { | 151 void KeyboardEvdev::StartKeyRepeat(unsigned int key) { |
156 repeat_key_ = key; | 152 repeat_key_ = key; |
157 repeat_device_id_ = device_id; | |
158 repeat_sequence_++; | 153 repeat_sequence_++; |
159 | 154 |
160 ScheduleKeyRepeat(repeat_delay_); | 155 ScheduleKeyRepeat(repeat_delay_); |
161 } | 156 } |
162 | 157 |
163 void KeyboardEvdev::StopKeyRepeat() { | 158 void KeyboardEvdev::StopKeyRepeat() { |
164 repeat_key_ = KEY_RESERVED; | 159 repeat_key_ = KEY_RESERVED; |
165 repeat_sequence_++; | 160 repeat_sequence_++; |
166 } | 161 } |
167 | 162 |
168 void KeyboardEvdev::ScheduleKeyRepeat(const base::TimeDelta& delay) { | 163 void KeyboardEvdev::ScheduleKeyRepeat(const base::TimeDelta& delay) { |
169 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 164 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
170 FROM_HERE, base::Bind(&KeyboardEvdev::OnRepeatTimeout, | 165 FROM_HERE, base::Bind(&KeyboardEvdev::OnRepeatTimeout, |
171 weak_ptr_factory_.GetWeakPtr(), repeat_sequence_), | 166 weak_ptr_factory_.GetWeakPtr(), repeat_sequence_), |
172 delay); | 167 delay); |
173 } | 168 } |
174 | 169 |
175 void KeyboardEvdev::OnRepeatTimeout(unsigned int sequence) { | 170 void KeyboardEvdev::OnRepeatTimeout(unsigned int sequence) { |
176 if (repeat_sequence_ != sequence) | 171 if (repeat_sequence_ != sequence) |
177 return; | 172 return; |
178 | 173 |
179 DispatchKey(repeat_key_, true /* down */, true /* repeat */, | 174 DispatchKey(repeat_key_, true /* down */, true /* repeat */, |
180 EventTimeForNow(), repeat_device_id_); | 175 EventTimeForNow()); |
181 | 176 |
182 ScheduleKeyRepeat(repeat_interval_); | 177 ScheduleKeyRepeat(repeat_interval_); |
183 } | 178 } |
184 | 179 |
185 void KeyboardEvdev::DispatchKey(unsigned int key, | 180 void KeyboardEvdev::DispatchKey(unsigned int key, |
186 bool down, | 181 bool down, |
187 bool repeat, | 182 bool repeat, |
188 base::TimeDelta timestamp, | 183 base::TimeDelta timestamp) { |
189 int device_id) { | |
190 DomCode dom_code = | 184 DomCode dom_code = |
191 KeycodeConverter::NativeKeycodeToDomCode(EvdevCodeToNativeCode(key)); | 185 KeycodeConverter::NativeKeycodeToDomCode(EvdevCodeToNativeCode(key)); |
192 // DomCode constants are not included here because of conflicts with | 186 // DomCode constants are not included here because of conflicts with |
193 // evdev preprocessor macros. | 187 // evdev preprocessor macros. |
194 if (!static_cast<int>(dom_code)) | 188 if (!static_cast<int>(dom_code)) |
195 return; | 189 return; |
196 int flags = modifiers_->GetModifierFlags(); | 190 int flags = modifiers_->GetModifierFlags(); |
197 DomKey dom_key; | 191 DomKey dom_key; |
198 KeyboardCode key_code; | 192 KeyboardCode key_code; |
199 uint16 character; | 193 uint16 character; |
200 uint32 platform_keycode = 0; | 194 uint32 platform_keycode = 0; |
201 if (!keyboard_layout_engine_->Lookup(dom_code, flags, &dom_key, &character, | 195 if (!keyboard_layout_engine_->Lookup(dom_code, flags, &dom_key, &character, |
202 &key_code, &platform_keycode)) { | 196 &key_code, &platform_keycode)) { |
203 return; | 197 return; |
204 } | 198 } |
205 if (!repeat) | 199 if (!repeat) |
206 UpdateModifier(ModifierDomKeyToEventFlag(dom_key), down); | 200 UpdateModifier(ModifierDomKeyToEventFlag(dom_key), down); |
207 | 201 |
208 KeyEvent event(down ? ET_KEY_PRESSED : ET_KEY_RELEASED, key_code, dom_code, | 202 KeyEvent event(down ? ET_KEY_PRESSED : ET_KEY_RELEASED, key_code, dom_code, |
209 modifiers_->GetModifierFlags(), dom_key, character, timestamp); | 203 modifiers_->GetModifierFlags(), dom_key, character, timestamp); |
210 event.set_source_device_id(device_id); | |
211 if (platform_keycode) | 204 if (platform_keycode) |
212 event.set_platform_keycode(platform_keycode); | 205 event.set_platform_keycode(platform_keycode); |
213 callback_.Run(&event); | 206 callback_.Run(&event); |
214 } | 207 } |
215 | 208 |
216 } // namespace ui | 209 } // namespace ui |
OLD | NEW |