OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/events/ozone/evdev/event_converter_evdev_impl.h" | 5 #include "ui/events/ozone/evdev/event_converter_evdev_impl.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <linux/input.h> | 8 #include <linux/input.h> |
9 | 9 |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "ui/events/event.h" | 11 #include "ui/events/event.h" |
12 #include "ui/events/keycodes/dom4/keycode_converter.h" | 12 #include "ui/events/keycodes/dom4/keycode_converter.h" |
13 #include "ui/events/keycodes/keyboard_codes.h" | 13 #include "ui/events/ozone/evdev/keyboard_evdev.h" |
14 | 14 |
15 namespace ui { | 15 namespace ui { |
16 | 16 |
17 // Values for EV_KEY. | 17 // Values for EV_KEY. |
18 const int kKeyReleaseValue = 0; | 18 const int kKeyReleaseValue = 0; |
19 const int kKeyRepeatValue = 2; | 19 const int kKeyRepeatValue = 2; |
20 | 20 |
21 EventConverterEvdevImpl::EventConverterEvdevImpl( | 21 EventConverterEvdevImpl::EventConverterEvdevImpl( |
22 int fd, | 22 int fd, |
23 base::FilePath path, | 23 base::FilePath path, |
24 int id, | 24 int id, |
25 InputDeviceType type, | 25 InputDeviceType type, |
26 const EventDeviceInfo& devinfo, | 26 const EventDeviceInfo& devinfo, |
27 EventModifiersEvdev* modifiers, | 27 EventModifiersEvdev* modifiers, |
28 MouseButtonMapEvdev* button_map, | 28 MouseButtonMapEvdev* button_map, |
29 CursorDelegateEvdev* cursor, | 29 CursorDelegateEvdev* cursor, |
30 KeyboardEvdev* keyboard, | 30 KeyboardEvdev* keyboard, |
31 const EventDispatchCallback& callback) | 31 const EventDispatchCallback& callback) |
32 : EventConverterEvdev(fd, path, id, type), | 32 : EventConverterEvdev(fd, path, id, type), |
33 has_keyboard_(devinfo.HasKeyboard()), | 33 has_keyboard_(devinfo.HasKeyboard()), |
| 34 has_touchpad_(devinfo.HasTouchpad()), |
34 x_offset_(0), | 35 x_offset_(0), |
35 y_offset_(0), | 36 y_offset_(0), |
36 cursor_(cursor), | 37 cursor_(cursor), |
37 keyboard_(keyboard), | 38 keyboard_(keyboard), |
38 modifiers_(modifiers), | 39 modifiers_(modifiers), |
39 button_map_(button_map), | 40 button_map_(button_map), |
40 callback_(callback) { | 41 callback_(callback) { |
41 } | 42 } |
42 | 43 |
43 EventConverterEvdevImpl::~EventConverterEvdevImpl() { | 44 EventConverterEvdevImpl::~EventConverterEvdevImpl() { |
44 Stop(); | 45 Stop(); |
45 close(fd_); | 46 close(fd_); |
46 } | 47 } |
47 | 48 |
48 void EventConverterEvdevImpl::OnFileCanReadWithoutBlocking(int fd) { | 49 void EventConverterEvdevImpl::OnFileCanReadWithoutBlocking(int fd) { |
49 input_event inputs[4]; | 50 input_event inputs[4]; |
50 ssize_t read_size = read(fd, inputs, sizeof(inputs)); | 51 ssize_t read_size = read(fd, inputs, sizeof(inputs)); |
51 if (read_size < 0) { | 52 if (read_size < 0) { |
52 if (errno == EINTR || errno == EAGAIN) | 53 if (errno == EINTR || errno == EAGAIN) |
53 return; | 54 return; |
54 if (errno != ENODEV) | 55 if (errno != ENODEV) |
55 PLOG(ERROR) << "error reading device " << path_.value(); | 56 PLOG(ERROR) << "error reading device " << path_.value(); |
56 Stop(); | 57 Stop(); |
57 return; | 58 return; |
58 } | 59 } |
59 | 60 |
| 61 // TODO(spang): Re-implement this by releasing buttons & temporarily closing |
| 62 // the device. |
| 63 if (ignore_events_) |
| 64 return; |
| 65 |
60 DCHECK_EQ(read_size % sizeof(*inputs), 0u); | 66 DCHECK_EQ(read_size % sizeof(*inputs), 0u); |
61 ProcessEvents(inputs, read_size / sizeof(*inputs)); | 67 ProcessEvents(inputs, read_size / sizeof(*inputs)); |
62 } | 68 } |
63 | 69 |
64 bool EventConverterEvdevImpl::HasKeyboard() const { | 70 bool EventConverterEvdevImpl::HasKeyboard() const { |
65 return has_keyboard_; | 71 return has_keyboard_; |
66 } | 72 } |
67 | 73 |
| 74 bool EventConverterEvdevImpl::HasTouchpad() const { |
| 75 return has_touchpad_; |
| 76 } |
| 77 |
| 78 void EventConverterEvdevImpl::SetAllowedKeys( |
| 79 scoped_ptr<std::set<DomCode>> allowed_keys) { |
| 80 DCHECK(HasKeyboard()); |
| 81 allowed_keys_ = allowed_keys.Pass(); |
| 82 } |
| 83 |
| 84 void EventConverterEvdevImpl::AllowAllKeys() { |
| 85 DCHECK(HasKeyboard()); |
| 86 allowed_keys_.reset(); |
| 87 } |
| 88 |
68 void EventConverterEvdevImpl::ProcessEvents(const input_event* inputs, | 89 void EventConverterEvdevImpl::ProcessEvents(const input_event* inputs, |
69 int count) { | 90 int count) { |
70 for (int i = 0; i < count; ++i) { | 91 for (int i = 0; i < count; ++i) { |
71 const input_event& input = inputs[i]; | 92 const input_event& input = inputs[i]; |
72 switch (input.type) { | 93 switch (input.type) { |
73 case EV_KEY: | 94 case EV_KEY: |
74 ConvertKeyEvent(input); | 95 ConvertKeyEvent(input); |
75 break; | 96 break; |
76 case EV_REL: | 97 case EV_REL: |
77 ConvertMouseMoveEvent(input); | 98 ConvertMouseMoveEvent(input); |
(...skipping 10 matching lines...) Expand all Loading... |
88 if (input.value == kKeyRepeatValue) | 109 if (input.value == kKeyRepeatValue) |
89 return; | 110 return; |
90 | 111 |
91 // Mouse processing. | 112 // Mouse processing. |
92 if (input.code >= BTN_MOUSE && input.code < BTN_JOYSTICK) { | 113 if (input.code >= BTN_MOUSE && input.code < BTN_JOYSTICK) { |
93 DispatchMouseButton(input); | 114 DispatchMouseButton(input); |
94 return; | 115 return; |
95 } | 116 } |
96 | 117 |
97 // Keyboard processing. | 118 // Keyboard processing. |
98 keyboard_->OnKeyChange(input.code, input.value != kKeyReleaseValue); | 119 DomCode key_code = KeycodeConverter::NativeKeycodeToDomCode( |
| 120 KeyboardEvdev::EvdevCodeToNativeCode(input.code)); |
| 121 if (!allowed_keys_ || allowed_keys_->count(key_code)) |
| 122 keyboard_->OnKeyChange(input.code, input.value != kKeyReleaseValue); |
99 } | 123 } |
100 | 124 |
101 void EventConverterEvdevImpl::ConvertMouseMoveEvent(const input_event& input) { | 125 void EventConverterEvdevImpl::ConvertMouseMoveEvent(const input_event& input) { |
102 if (!cursor_) | 126 if (!cursor_) |
103 return; | 127 return; |
104 switch (input.code) { | 128 switch (input.code) { |
105 case REL_X: | 129 case REL_X: |
106 x_offset_ = input.value; | 130 x_offset_ = input.value; |
107 break; | 131 break; |
108 case REL_Y: | 132 case REL_Y: |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 new MouseEvent(ui::ET_MOUSE_MOVED, | 170 new MouseEvent(ui::ET_MOUSE_MOVED, |
147 cursor_->GetLocation(), | 171 cursor_->GetLocation(), |
148 cursor_->GetLocation(), | 172 cursor_->GetLocation(), |
149 modifiers_->GetModifierFlags(), | 173 modifiers_->GetModifierFlags(), |
150 /* changed_button_flags */ 0))); | 174 /* changed_button_flags */ 0))); |
151 x_offset_ = 0; | 175 x_offset_ = 0; |
152 y_offset_ = 0; | 176 y_offset_ = 0; |
153 } | 177 } |
154 | 178 |
155 } // namespace ui | 179 } // namespace ui |
OLD | NEW |