-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTinywlInputService.cpp
More file actions
219 lines (183 loc) · 7.83 KB
/
TinywlInputService.cpp
File metadata and controls
219 lines (183 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "aidl/android/hardware/input/common/Axis.h"
#include "aidl/android/hardware/input/common/MotionEvent.h"
#include "aidl/com/android/server/inputflinger/KeyEvent.h"
#include <aidl/tinywl/BnTinywlInput.h>
#include <android/keycodes.h>
#include <cstdint>
#include <stdint.h>
#include <wayland-server-core.h>
#include <wayland-server-protocol.h>
#include "TinywlInputService.h"
#include <sys/eventfd.h>
extern "C" {
#include <wlr/interfaces/wlr_keyboard.h>
#include <wlr/interfaces/wlr_pointer.h>
#include <linux/input-event-codes.h>
#include <wlr/util/log.h>
}
extern float PointerCoords_getAxisValue(const aidl::android::hardware::input::common::PointerCoords& coords, int32_t axis);
namespace tinywl {
const static struct wlr_pointer_impl tinywl_pointer_impl = {
.name = "tinywl-pointer",
};
const static struct wlr_keyboard_impl tinywl_keyboard_impl = {
.name = "tinywl-keyboard",
};
void TinywlInputService::sendPointerButtonEvent(const MotionEvent& in_event, struct tinywl_toplevel *toplevel) {
struct wlr_pointer_button_event wlr_event = {
.pointer = &pointer,
.time_msec = (uint32_t)in_event.eventTime,
.button = AKEYCODE_BUTTON_1,
.state = WL_POINTER_BUTTON_STATE_PRESSED,
};
switch (in_event.action) {
case Action::BUTTON_PRESS:
wlr_event.state = WL_POINTER_BUTTON_STATE_PRESSED;
break;
case Action::BUTTON_RELEASE:
wlr_event.state = WL_POINTER_BUTTON_STATE_RELEASED;
break;
default:
return;
}
switch (in_event.actionButton) {
case Button::PRIMARY:
wlr_event.button = BTN_LEFT;
break;
case Button::SECONDARY:
wlr_event.button = BTN_RIGHT;
break;
default:
return;
}
wl_signal_emit_mutable(&pointer.events.button, &wlr_event);
wl_signal_emit_mutable(&pointer.events.frame, &pointer);
}
void TinywlInputService::sendPointerPosition(const MotionEvent& in_event, struct tinywl_toplevel *toplevel) {
float x = PointerCoords_getAxisValue(in_event.pointerCoords.front(), static_cast<int32_t>(Axis::X));
float y = PointerCoords_getAxisValue(in_event.pointerCoords.front(), static_cast<int32_t>(Axis::Y));
x += toplevel->geo_box.x;
y += toplevel->geo_box.y;
struct wlr_pointer_motion_absolute_event wlr_event = {
.pointer = &pointer,
.time_msec = static_cast<uint32_t>(in_event.eventTime),
.x = x / width,
.y = y / height,
};
wl_signal_emit_mutable(&pointer.events.motion_absolute, &wlr_event);
wl_signal_emit_mutable(&pointer.events.frame, &pointer);
}
void TinywlInputService::sendScrollEvent(const MotionEvent& in_event, struct tinywl_toplevel *toplevel) {
float delta = PointerCoords_getAxisValue(in_event.pointerCoords.front(), static_cast<int32_t>(Axis::Y));
struct wlr_pointer_axis_event wlr_event = {
.pointer = &pointer,
.time_msec = static_cast<uint32_t>(in_event.eventTime),
.source = WL_POINTER_AXIS_SOURCE_WHEEL,
.orientation = WL_POINTER_AXIS_VERTICAL_SCROLL,
.delta = delta,
.delta_discrete = static_cast<int32_t>(delta * WLR_POINTER_AXIS_DISCRETE_STEP),
};
wl_signal_emit_mutable(&pointer.events.axis, &wlr_event);
wl_signal_emit_mutable(&pointer.events.frame, &pointer);
}
::ndk::ScopedAStatus TinywlInputService::onKeyEvent(const KeyEvent& in_event, long in_nativePtr, bool* _aidl_return) {
keyEventQueue.push(in_event, in_nativePtr);
eventfd_write(event_fd, 1); // Notify event loop
// std::cout << in_event.toString() << std::endl;
*_aidl_return = true;
return ::ndk::ScopedAStatus::ok();
}
::ndk::ScopedAStatus TinywlInputService::onMotionEvent(const MotionEvent& in_event, long in_nativePtr, bool* _aidl_return) {
motionEventQueue.push(in_event, in_nativePtr);
eventfd_write(event_fd, 1); // Notify event loop
// std::cout << in_event.toString() << std::endl;
*_aidl_return = true;
return ::ndk::ScopedAStatus::ok();
}
static int event_loop_fd_callback(int fd, uint32_t mask, void *data) {
auto thiz = reinterpret_cast<TinywlInputService *>(data);
if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
if (mask & WL_EVENT_ERROR) {
wlr_log(WLR_ERROR, "Failed to read event");
}
return 0;
}
eventfd_t event_count = 0;
if (eventfd_read(thiz->event_fd, &event_count) < 0) {
return 0;
}
while (auto in_event = thiz->motionEventQueue.pull()) {
// Process event
if (in_event->event.source != Source::MOUSE) {
continue;
}
struct tinywl_toplevel* toplevel = reinterpret_cast<struct tinywl_toplevel*>(in_event->nativePtr);
std::lock_guard<std::mutex> lock(thiz->mutex_);
if (thiz->toplevels.find(toplevel) == thiz->toplevels.end()) {
continue;
}
switch (in_event->event.action) {
case Action::BUTTON_PRESS:
case Action::BUTTON_RELEASE:
thiz->sendPointerButtonEvent(in_event->event, toplevel);
break;
case Action::MOVE:
case Action::HOVER_ENTER:
case Action::HOVER_EXIT:
thiz->sendPointerPosition(in_event->event, toplevel);
break;
case Action::SCROLL:
thiz->sendScrollEvent(in_event->event, toplevel);
break;
default:
// Skip other actions
continue;
}
}
while (auto in_event = thiz->keyEventQueue.pull()) {
// Process event
if (in_event->event.source != Source::KEYBOARD) {
continue;
}
struct tinywl_toplevel* toplevel = reinterpret_cast<struct tinywl_toplevel*>(in_event->nativePtr);
std::lock_guard<std::mutex> lock(thiz->mutex_);
if (thiz->toplevels.find(toplevel) == thiz->toplevels.end()) {
continue;
}
struct wlr_keyboard_key_event wlr_event = {
.time_msec = static_cast<uint32_t>(in_event->event.eventTime),
.keycode = (uint32_t)in_event->event.scanCode,
.update_state = true,
};
switch (in_event->event.action) {
case KeyEventAction::DOWN:
wlr_event.state = WL_KEYBOARD_KEY_STATE_PRESSED;
break;
case KeyEventAction::UP:
wlr_event.state = WL_KEYBOARD_KEY_STATE_RELEASED;
break;
default:
// Skip other actions like AKEY_EVENT_ACTION_MULTIPLE
continue;
}
wlr_keyboard_notify_key(&thiz->keyboard, &wlr_event);
}
return 0;
}
void TinywlInputService::setTinywlServer(struct tinywl_server* server) {
this->server = server;
wlr_keyboard_init(&keyboard, &tinywl_keyboard_impl, "tinywl-keyboard");
server->new_input.notify(&server->new_input, &keyboard.base);
wlr_pointer_init(&pointer, &tinywl_pointer_impl, "tinywl-pointer");
server->new_input.notify(&server->new_input, &pointer.base);
/* Event loop and file descriptors */
struct wl_event_loop *loop = wl_display_get_event_loop(server->wl_display);
event_fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK | EFD_SEMAPHORE);
uint32_t events = WL_EVENT_READABLE | WL_EVENT_ERROR | WL_EVENT_HANGUP;
event_source = wl_event_loop_add_fd(loop, event_fd, events, event_loop_fd_callback, this);
}
void TinywlInputService::closeFdsAndRemoveEventSources() {
wl_event_source_remove(event_source);
close(event_fd);
}
} // namespace tinywl