Landing Recent QUIC Changes.
[chromium-blink-merge.git] / ui / events / ozone / evdev / event_converter_evdev_impl.cc
blobc40102821eb477b202cc5229050f6efd3eaf35ee
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
3 // found in the LICENSE file.
5 #include "ui/events/ozone/evdev/event_converter_evdev_impl.h"
7 #include <errno.h>
8 #include <linux/input.h>
10 #include "ui/events/event.h"
11 #include "ui/events/keycodes/dom4/keycode_converter.h"
12 #include "ui/events/ozone/evdev/device_event_dispatcher_evdev.h"
13 #include "ui/events/ozone/evdev/keyboard_util_evdev.h"
15 namespace ui {
17 namespace {
19 // Values for EV_KEY.
20 const int kKeyReleaseValue = 0;
21 const int kKeyRepeatValue = 2;
23 } // namespace
25 EventConverterEvdevImpl::EventConverterEvdevImpl(
26 int fd,
27 base::FilePath path,
28 int id,
29 InputDeviceType type,
30 const EventDeviceInfo& devinfo,
31 CursorDelegateEvdev* cursor,
32 DeviceEventDispatcherEvdev* dispatcher)
33 : EventConverterEvdev(fd, path, id, type),
34 has_keyboard_(devinfo.HasKeyboard()),
35 has_touchpad_(devinfo.HasTouchpad()),
36 x_offset_(0),
37 y_offset_(0),
38 cursor_(cursor),
39 dispatcher_(dispatcher) {
42 EventConverterEvdevImpl::~EventConverterEvdevImpl() {
43 Stop();
44 close(fd_);
47 void EventConverterEvdevImpl::OnFileCanReadWithoutBlocking(int fd) {
48 input_event inputs[4];
49 ssize_t read_size = read(fd, inputs, sizeof(inputs));
50 if (read_size < 0) {
51 if (errno == EINTR || errno == EAGAIN)
52 return;
53 if (errno != ENODEV)
54 PLOG(ERROR) << "error reading device " << path_.value();
55 Stop();
56 return;
59 // TODO(spang): Re-implement this by releasing buttons & temporarily closing
60 // the device.
61 if (ignore_events_)
62 return;
64 DCHECK_EQ(read_size % sizeof(*inputs), 0u);
65 ProcessEvents(inputs, read_size / sizeof(*inputs));
68 bool EventConverterEvdevImpl::HasKeyboard() const {
69 return has_keyboard_;
72 bool EventConverterEvdevImpl::HasTouchpad() const {
73 return has_touchpad_;
76 void EventConverterEvdevImpl::SetAllowedKeys(
77 scoped_ptr<std::set<DomCode>> allowed_keys) {
78 DCHECK(HasKeyboard());
79 allowed_keys_ = allowed_keys.Pass();
82 void EventConverterEvdevImpl::AllowAllKeys() {
83 DCHECK(HasKeyboard());
84 allowed_keys_.reset();
87 void EventConverterEvdevImpl::ProcessEvents(const input_event* inputs,
88 int count) {
89 for (int i = 0; i < count; ++i) {
90 const input_event& input = inputs[i];
91 switch (input.type) {
92 case EV_KEY:
93 ConvertKeyEvent(input);
94 break;
95 case EV_REL:
96 ConvertMouseMoveEvent(input);
97 break;
98 case EV_SYN:
99 if (input.code == SYN_DROPPED)
100 LOG(WARNING) << "kernel dropped input events";
101 FlushEvents();
102 break;
107 void EventConverterEvdevImpl::ConvertKeyEvent(const input_event& input) {
108 // Ignore repeat events.
109 if (input.value == kKeyRepeatValue)
110 return;
112 // Mouse processing.
113 if (input.code >= BTN_MOUSE && input.code < BTN_JOYSTICK) {
114 DispatchMouseButton(input);
115 return;
118 // Keyboard processing.
119 DomCode key_code = KeycodeConverter::NativeKeycodeToDomCode(
120 EvdevCodeToNativeCode(input.code));
121 if (!allowed_keys_ || allowed_keys_->count(key_code)) {
122 dispatcher_->DispatchKeyEvent(
123 KeyEventParams(id_, input.code, input.value != kKeyReleaseValue));
127 void EventConverterEvdevImpl::ConvertMouseMoveEvent(const input_event& input) {
128 if (!cursor_)
129 return;
130 switch (input.code) {
131 case REL_X:
132 x_offset_ = input.value;
133 break;
134 case REL_Y:
135 y_offset_ = input.value;
136 break;
140 void EventConverterEvdevImpl::DispatchMouseButton(const input_event& input) {
141 if (!cursor_)
142 return;
144 dispatcher_->DispatchMouseButtonEvent(
145 MouseButtonEventParams(id_, cursor_->GetLocation(), input.code,
146 input.value, /* allow_remap */ true));
149 void EventConverterEvdevImpl::FlushEvents() {
150 if (!cursor_ || (x_offset_ == 0 && y_offset_ == 0))
151 return;
153 cursor_->MoveCursor(gfx::Vector2dF(x_offset_, y_offset_));
155 dispatcher_->DispatchMouseMoveEvent(
156 MouseMoveEventParams(id_, cursor_->GetLocation()));
158 x_offset_ = 0;
159 y_offset_ = 0;
162 } // namespace ui