Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(481)

Side by Side Diff: ui/events/ozone/evdev/event_converter_evdev_impl.cc

Issue 677113002: ozone: evdev: Handle mouse events (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more review and lint fixes Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "ui/events/ozone/evdev/event_converter_evdev_impl.h"
6
7 #include <errno.h>
8 #include <linux/input.h>
9
10 #include "base/message_loop/message_loop.h"
11 #include "ui/events/event.h"
12 #include "ui/events/keycodes/dom4/keycode_converter.h"
13 #include "ui/events/keycodes/keyboard_codes.h"
14
15 namespace ui {
16
17 EventConverterEvdevImpl::EventConverterEvdevImpl(
18 int fd,
19 base::FilePath path,
20 int id,
21 EventModifiersEvdev* modifiers,
22 CursorDelegateEvdev* cursor,
23 KeyboardEvdev* keyboard,
24 const EventDispatchCallback& callback)
25 : EventConverterEvdev(fd, path, id),
26 x_offset_(0),
27 y_offset_(0),
28 cursor_(cursor),
29 keyboard_(keyboard),
30 modifiers_(modifiers),
31 callback_(callback) {
32 }
33
34 EventConverterEvdevImpl::~EventConverterEvdevImpl() {
35 Stop();
36 close(fd_);
37 }
38
39 void EventConverterEvdevImpl::OnFileCanReadWithoutBlocking(int fd) {
40 input_event inputs[4];
41 ssize_t read_size = read(fd, inputs, sizeof(inputs));
42 if (read_size < 0) {
43 if (errno == EINTR || errno == EAGAIN)
44 return;
45 if (errno != ENODEV)
46 PLOG(ERROR) << "error reading device " << path_.value();
47 Stop();
48 return;
49 }
50
51 DCHECK_EQ(read_size % sizeof(*inputs), 0u);
52 ProcessEvents(inputs, read_size / sizeof(*inputs));
53 }
54
55 void EventConverterEvdevImpl::ProcessEvents(const input_event* inputs,
56 int count) {
57 for (int i = 0; i < count; ++i) {
58 const input_event& input = inputs[i];
59 switch (input.type) {
60 case EV_KEY:
61 ConvertKeyEvent(input);
62 break;
63 case EV_REL:
64 ConvertMouseMoveEvent(input);
65 break;
66 case EV_SYN:
67 FlushEvents();
68 break;
69 }
70 }
71 }
72
73 void EventConverterEvdevImpl::ConvertKeyEvent(const input_event& input) {
74 // Mouse processing.
75 if (input.code >= BTN_MOUSE && input.code < BTN_JOYSTICK) {
76 DispatchMouseButton(input);
77 return;
78 }
79 // Keyboard processing.
80 keyboard_->OnKeyChange(input.code, input.value != 0);
81 }
82
83 void EventConverterEvdevImpl::ConvertMouseMoveEvent(const input_event& input) {
84 if (!cursor_)
85 return;
86 switch (input.code) {
87 case REL_X:
88 x_offset_ = input.value;
89 break;
90 case REL_Y:
91 y_offset_ = input.value;
92 break;
93 }
94 }
95
96 void EventConverterEvdevImpl::DispatchMouseButton(const input_event& input) {
97 unsigned int modifier;
98 if (input.code == BTN_LEFT)
99 modifier = EVDEV_MODIFIER_LEFT_MOUSE_BUTTON;
100 else if (input.code == BTN_RIGHT)
101 modifier = EVDEV_MODIFIER_RIGHT_MOUSE_BUTTON;
102 else if (input.code == BTN_MIDDLE)
103 modifier = EVDEV_MODIFIER_MIDDLE_MOUSE_BUTTON;
104
105 if (!cursor_)
106 return;
spang 2014/10/27 20:14:21 Move this to the top of the function.
vignatti (out of this project) 2014/10/27 20:22:52 Done.
107
108 int flag = modifiers_->GetEventFlagFromModifier(modifier);
109 modifiers_->UpdateModifier(modifier, input.value);
110 callback_.Run(make_scoped_ptr(
111 new MouseEvent(input.value ? ET_MOUSE_PRESSED : ET_MOUSE_RELEASED,
112 cursor_->location(),
113 cursor_->location(),
114 modifiers_->GetModifierFlags() | flag,
115 flag)));
116 }
117
118 void EventConverterEvdevImpl::FlushEvents(void) {
119 if (!cursor_ || (x_offset_ == 0 && y_offset_ == 0))
120 return;
121
122 cursor_->MoveCursor(gfx::Vector2dF(x_offset_, y_offset_));
123
124 callback_.Run(make_scoped_ptr(
125 new MouseEvent(ui::ET_MOUSE_MOVED,
126 cursor_->location(),
127 cursor_->location(),
128 modifiers_->GetModifierFlags(),
129 /* changed_button_flags */ 0)));
130 x_offset_ = 0;
131 y_offset_ = 0;
132 }
133
134 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698