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/keyboard_util_evdev.h"
19 const int kKeyReleaseValue
= 0;
20 const int kKeyRepeatValue
= 2;
24 EventConverterEvdevImpl::EventConverterEvdevImpl(
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()),
40 key_callback_(key_callback
),
41 mouse_move_callback_(mouse_move_callback
),
42 mouse_button_callback_(mouse_button_callback
) {
45 EventConverterEvdevImpl::~EventConverterEvdevImpl() {
50 void EventConverterEvdevImpl::OnFileCanReadWithoutBlocking(int fd
) {
51 input_event inputs
[4];
52 ssize_t read_size
= read(fd
, inputs
, sizeof(inputs
));
54 if (errno
== EINTR
|| errno
== EAGAIN
)
57 PLOG(ERROR
) << "error reading device " << path_
.value();
62 // TODO(spang): Re-implement this by releasing buttons & temporarily closing
67 DCHECK_EQ(read_size
% sizeof(*inputs
), 0u);
68 ProcessEvents(inputs
, read_size
/ sizeof(*inputs
));
71 bool EventConverterEvdevImpl::HasKeyboard() const {
75 bool EventConverterEvdevImpl::HasTouchpad() const {
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
,
92 for (int i
= 0; i
< count
; ++i
) {
93 const input_event
& input
= inputs
[i
];
96 ConvertKeyEvent(input
);
99 ConvertMouseMoveEvent(input
);
108 void EventConverterEvdevImpl::ConvertKeyEvent(const input_event
& input
) {
109 // Ignore repeat events.
110 if (input
.value
== kKeyRepeatValue
)
114 if (input
.code
>= BTN_MOUSE
&& input
.code
< BTN_JOYSTICK
) {
115 DispatchMouseButton(input
);
119 // Keyboard processing.
120 DomCode key_code
= KeycodeConverter::NativeKeycodeToDomCode(
121 EvdevCodeToNativeCode(input
.code
));
122 if (!allowed_keys_
|| allowed_keys_
->count(key_code
)) {
124 KeyEventParams(id_
, input
.code
, input
.value
!= kKeyReleaseValue
));
128 void EventConverterEvdevImpl::ConvertMouseMoveEvent(const input_event
& input
) {
131 switch (input
.code
) {
133 x_offset_
= input
.value
;
136 y_offset_
= input
.value
;
141 void EventConverterEvdevImpl::DispatchMouseButton(const input_event
& input
) {
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))
154 cursor_
->MoveCursor(gfx::Vector2dF(x_offset_
, y_offset_
));
156 mouse_move_callback_
.Run(MouseMoveEventParams(id_
, cursor_
->GetLocation()));