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/key_event_converter_evdev.h" | 5 #include "ui/events/ozone/evdev/key_event_converter_evdev.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <linux/input.h> | 8 #include <linux/input.h> |
9 | 9 |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 | 187 |
188 bool IsLockButton(unsigned int code) { return code == KEY_CAPSLOCK; } | 188 bool IsLockButton(unsigned int code) { return code == KEY_CAPSLOCK; } |
189 | 189 |
190 } // namespace | 190 } // namespace |
191 | 191 |
192 KeyEventConverterEvdev::KeyEventConverterEvdev( | 192 KeyEventConverterEvdev::KeyEventConverterEvdev( |
193 int fd, | 193 int fd, |
194 base::FilePath path, | 194 base::FilePath path, |
195 EventModifiersEvdev* modifiers, | 195 EventModifiersEvdev* modifiers, |
196 const EventDispatchCallback& callback) | 196 const EventDispatchCallback& callback) |
197 : EventConverterEvdev(callback), | 197 : EventConverterEvdev(fd, path), |
198 fd_(fd), | 198 callback_(callback), |
199 path_(path), | |
200 modifiers_(modifiers) { | 199 modifiers_(modifiers) { |
201 // TODO(spang): Initialize modifiers using EVIOCGKEY. | 200 // TODO(spang): Initialize modifiers using EVIOCGKEY. |
202 } | 201 } |
203 | 202 |
204 KeyEventConverterEvdev::~KeyEventConverterEvdev() { | 203 KeyEventConverterEvdev::~KeyEventConverterEvdev() { |
205 Stop(); | 204 Stop(); |
206 close(fd_); | 205 close(fd_); |
207 } | 206 } |
208 | 207 |
209 void KeyEventConverterEvdev::Start() { | |
210 base::MessageLoopForUI::current()->WatchFileDescriptor( | |
211 fd_, true, base::MessagePumpLibevent::WATCH_READ, &controller_, this); | |
212 } | |
213 | |
214 void KeyEventConverterEvdev::Stop() { | |
215 controller_.StopWatchingFileDescriptor(); | |
216 } | |
217 | |
218 void KeyEventConverterEvdev::OnFileCanReadWithoutBlocking(int fd) { | 208 void KeyEventConverterEvdev::OnFileCanReadWithoutBlocking(int fd) { |
219 input_event inputs[4]; | 209 input_event inputs[4]; |
220 ssize_t read_size = read(fd, inputs, sizeof(inputs)); | 210 ssize_t read_size = read(fd, inputs, sizeof(inputs)); |
221 if (read_size < 0) { | 211 if (read_size < 0) { |
222 if (errno == EINTR || errno == EAGAIN) | 212 if (errno == EINTR || errno == EAGAIN) |
223 return; | 213 return; |
224 if (errno != ENODEV) | 214 if (errno != ENODEV) |
225 PLOG(ERROR) << "error reading device " << path_.value(); | 215 PLOG(ERROR) << "error reading device " << path_.value(); |
226 Stop(); | 216 Stop(); |
227 return; | 217 return; |
228 } | 218 } |
229 | 219 |
230 CHECK_EQ(read_size % sizeof(*inputs), 0u); | 220 CHECK_EQ(read_size % sizeof(*inputs), 0u); |
231 ProcessEvents(inputs, read_size / sizeof(*inputs)); | 221 ProcessEvents(inputs, read_size / sizeof(*inputs)); |
232 } | 222 } |
233 | 223 |
234 void KeyEventConverterEvdev::OnFileCanWriteWithoutBlocking(int fd) { | |
235 NOTREACHED(); | |
236 } | |
237 | |
238 void KeyEventConverterEvdev::ProcessEvents(const input_event* inputs, | 224 void KeyEventConverterEvdev::ProcessEvents(const input_event* inputs, |
239 int count) { | 225 int count) { |
240 for (int i = 0; i < count; ++i) { | 226 for (int i = 0; i < count; ++i) { |
241 const input_event& input = inputs[i]; | 227 const input_event& input = inputs[i]; |
242 if (input.type == EV_KEY) { | 228 if (input.type == EV_KEY) { |
243 ConvertKeyEvent(input.code, input.value); | 229 ConvertKeyEvent(input.code, input.value); |
244 } else if (input.type == EV_SYN) { | 230 } else if (input.type == EV_SYN) { |
245 // TODO(sadrul): Handle this case appropriately. | 231 // TODO(sadrul): Handle this case appropriately. |
246 } | 232 } |
247 } | 233 } |
(...skipping 15 matching lines...) Expand all Loading... |
263 } | 249 } |
264 } | 250 } |
265 | 251 |
266 int flags = modifiers_->GetModifierFlags(); | 252 int flags = modifiers_->GetModifierFlags(); |
267 | 253 |
268 KeyEvent key_event( | 254 KeyEvent key_event( |
269 down ? ET_KEY_PRESSED : ET_KEY_RELEASED, | 255 down ? ET_KEY_PRESSED : ET_KEY_RELEASED, |
270 code, | 256 code, |
271 KeycodeConverter::NativeKeycodeToCode(key + kXkbKeycodeOffset), | 257 KeycodeConverter::NativeKeycodeToCode(key + kXkbKeycodeOffset), |
272 flags); | 258 flags); |
273 DispatchEventToCallback(&key_event); | 259 callback_.Run(&key_event); |
274 } | 260 } |
275 | 261 |
276 } // namespace ui | 262 } // namespace ui |
OLD | NEW |