OLD | NEW |
(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 if (!cursor_) |
| 98 return; |
| 99 |
| 100 unsigned int modifier; |
| 101 if (input.code == BTN_LEFT) |
| 102 modifier = EVDEV_MODIFIER_LEFT_MOUSE_BUTTON; |
| 103 else if (input.code == BTN_RIGHT) |
| 104 modifier = EVDEV_MODIFIER_RIGHT_MOUSE_BUTTON; |
| 105 else if (input.code == BTN_MIDDLE) |
| 106 modifier = EVDEV_MODIFIER_MIDDLE_MOUSE_BUTTON; |
| 107 else |
| 108 return; |
| 109 |
| 110 int flag = modifiers_->GetEventFlagFromModifier(modifier); |
| 111 modifiers_->UpdateModifier(modifier, input.value); |
| 112 callback_.Run(make_scoped_ptr( |
| 113 new MouseEvent(input.value ? ET_MOUSE_PRESSED : ET_MOUSE_RELEASED, |
| 114 cursor_->location(), |
| 115 cursor_->location(), |
| 116 modifiers_->GetModifierFlags() | flag, |
| 117 flag))); |
| 118 } |
| 119 |
| 120 void EventConverterEvdevImpl::FlushEvents() { |
| 121 if (!cursor_ || (x_offset_ == 0 && y_offset_ == 0)) |
| 122 return; |
| 123 |
| 124 cursor_->MoveCursor(gfx::Vector2dF(x_offset_, y_offset_)); |
| 125 |
| 126 callback_.Run(make_scoped_ptr( |
| 127 new MouseEvent(ui::ET_MOUSE_MOVED, |
| 128 cursor_->location(), |
| 129 cursor_->location(), |
| 130 modifiers_->GetModifierFlags(), |
| 131 /* changed_button_flags */ 0))); |
| 132 x_offset_ = 0; |
| 133 y_offset_ = 0; |
| 134 } |
| 135 |
| 136 } // namespace ui |
OLD | NEW |