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"
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"
20 const int kKeyReleaseValue
= 0;
21 const int kKeyRepeatValue
= 2;
25 EventConverterEvdevImpl::EventConverterEvdevImpl(
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()),
39 dispatcher_(dispatcher
) {
42 EventConverterEvdevImpl::~EventConverterEvdevImpl() {
47 void EventConverterEvdevImpl::OnFileCanReadWithoutBlocking(int fd
) {
48 input_event inputs
[4];
49 ssize_t read_size
= read(fd
, inputs
, sizeof(inputs
));
51 if (errno
== EINTR
|| errno
== EAGAIN
)
54 PLOG(ERROR
) << "error reading device " << path_
.value();
59 // TODO(spang): Re-implement this by releasing buttons & temporarily closing
64 DCHECK_EQ(read_size
% sizeof(*inputs
), 0u);
65 ProcessEvents(inputs
, read_size
/ sizeof(*inputs
));
68 bool EventConverterEvdevImpl::HasKeyboard() const {
72 bool EventConverterEvdevImpl::HasTouchpad() const {
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
,
89 for (int i
= 0; i
< count
; ++i
) {
90 const input_event
& input
= inputs
[i
];
93 ConvertKeyEvent(input
);
96 ConvertMouseMoveEvent(input
);
99 if (input
.code
== SYN_DROPPED
)
100 LOG(WARNING
) << "kernel dropped input events";
107 void EventConverterEvdevImpl::ConvertKeyEvent(const input_event
& input
) {
108 // Ignore repeat events.
109 if (input
.value
== kKeyRepeatValue
)
113 if (input
.code
>= BTN_MOUSE
&& input
.code
< BTN_JOYSTICK
) {
114 DispatchMouseButton(input
);
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
) {
130 switch (input
.code
) {
132 x_offset_
= input
.value
;
135 y_offset_
= input
.value
;
140 void EventConverterEvdevImpl::DispatchMouseButton(const input_event
& input
) {
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))
153 cursor_
->MoveCursor(gfx::Vector2dF(x_offset_
, y_offset_
));
155 dispatcher_
->DispatchMouseMoveEvent(
156 MouseMoveEventParams(id_
, cursor_
->GetLocation()));