ozone: evdev: Move EventModifiersEvdev usage to EventFactoryEvdev
[chromium-blink-merge.git] / ui / events / ozone / evdev / event_converter_evdev_impl.cc
blob7b7283321eaea3b6dcb1192652e0ba28ffe855db
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/keyboard_util_evdev.h"
14 namespace ui {
16 namespace {
18 // Values for EV_KEY.
19 const int kKeyReleaseValue = 0;
20 const int kKeyRepeatValue = 2;
22 } // namespace
24 EventConverterEvdevImpl::EventConverterEvdevImpl(
25 int fd,
26 base::FilePath path,
27 int id,
28 InputDeviceType type,
29 const EventDeviceInfo& devinfo,
30 CursorDelegateEvdev* cursor,
31 const KeyEventDispatchCallback& key_callback,
32 const MouseMoveEventDispatchCallback& mouse_move_callback,
33 const MouseButtonEventDispatchCallback& mouse_button_callback)
34 : EventConverterEvdev(fd, path, id, type),
35 has_keyboard_(devinfo.HasKeyboard()),
36 has_touchpad_(devinfo.HasTouchpad()),
37 x_offset_(0),
38 y_offset_(0),
39 cursor_(cursor),
40 key_callback_(key_callback),
41 mouse_move_callback_(mouse_move_callback),
42 mouse_button_callback_(mouse_button_callback) {
45 EventConverterEvdevImpl::~EventConverterEvdevImpl() {
46 Stop();
47 close(fd_);
50 void EventConverterEvdevImpl::OnFileCanReadWithoutBlocking(int fd) {
51 input_event inputs[4];
52 ssize_t read_size = read(fd, inputs, sizeof(inputs));
53 if (read_size < 0) {
54 if (errno == EINTR || errno == EAGAIN)
55 return;
56 if (errno != ENODEV)
57 PLOG(ERROR) << "error reading device " << path_.value();
58 Stop();
59 return;
62 // TODO(spang): Re-implement this by releasing buttons & temporarily closing
63 // the device.
64 if (ignore_events_)
65 return;
67 DCHECK_EQ(read_size % sizeof(*inputs), 0u);
68 ProcessEvents(inputs, read_size / sizeof(*inputs));
71 bool EventConverterEvdevImpl::HasKeyboard() const {
72 return has_keyboard_;
75 bool EventConverterEvdevImpl::HasTouchpad() const {
76 return has_touchpad_;
79 void EventConverterEvdevImpl::SetAllowedKeys(
80 scoped_ptr<std::set<DomCode>> allowed_keys) {
81 DCHECK(HasKeyboard());
82 allowed_keys_ = allowed_keys.Pass();
85 void EventConverterEvdevImpl::AllowAllKeys() {
86 DCHECK(HasKeyboard());
87 allowed_keys_.reset();
90 void EventConverterEvdevImpl::ProcessEvents(const input_event* inputs,
91 int count) {
92 for (int i = 0; i < count; ++i) {
93 const input_event& input = inputs[i];
94 switch (input.type) {
95 case EV_KEY:
96 ConvertKeyEvent(input);
97 break;
98 case EV_REL:
99 ConvertMouseMoveEvent(input);
100 break;
101 case EV_SYN:
102 FlushEvents();
103 break;
108 void EventConverterEvdevImpl::ConvertKeyEvent(const input_event& input) {
109 // Ignore repeat events.
110 if (input.value == kKeyRepeatValue)
111 return;
113 // Mouse processing.
114 if (input.code >= BTN_MOUSE && input.code < BTN_JOYSTICK) {
115 DispatchMouseButton(input);
116 return;
119 // Keyboard processing.
120 DomCode key_code = KeycodeConverter::NativeKeycodeToDomCode(
121 EvdevCodeToNativeCode(input.code));
122 if (!allowed_keys_ || allowed_keys_->count(key_code)) {
123 key_callback_.Run(
124 KeyEventParams(id_, input.code, input.value != kKeyReleaseValue));
128 void EventConverterEvdevImpl::ConvertMouseMoveEvent(const input_event& input) {
129 if (!cursor_)
130 return;
131 switch (input.code) {
132 case REL_X:
133 x_offset_ = input.value;
134 break;
135 case REL_Y:
136 y_offset_ = input.value;
137 break;
141 void EventConverterEvdevImpl::DispatchMouseButton(const input_event& input) {
142 if (!cursor_)
143 return;
145 mouse_button_callback_.Run(MouseButtonEventParams(id_, cursor_->GetLocation(),
146 input.code, input.value,
147 /* allow_remap */ true));
150 void EventConverterEvdevImpl::FlushEvents() {
151 if (!cursor_ || (x_offset_ == 0 && y_offset_ == 0))
152 return;
154 cursor_->MoveCursor(gfx::Vector2dF(x_offset_, y_offset_));
156 mouse_move_callback_.Run(MouseMoveEventParams(id_, cursor_->GetLocation()));
158 x_offset_ = 0;
159 y_offset_ = 0;
162 } // namespace ui