This repository has been archived by the owner on Jan 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaylandPointer.cpp
137 lines (120 loc) · 4.4 KB
/
WaylandPointer.cpp
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
#include <linux/input-event-codes.h>
#include "WaylandPointer.hpp"
WaylandPointer::WaylandPointer(WaylandXDGRegistry *registry)
{
this->cursor_surface = wl_compositor_create_surface(registry->wl_compositor);
this->registry = registry;
}
static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, uint32_t caps)
{
WaylandSeat *wayland_seat = static_cast<WaylandSeat *>(data);
wayland_seat->handle_capabilities(wl_seat, caps);
}
static void seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name)
{
WaylandSeat *wayland_seat = static_cast<WaylandSeat *>(data);
wayland_seat->handle_name(wl_seat, name);
}
const struct wl_seat_listener seat_listener = {
.capabilities = seat_handle_capabilities,
.name = seat_handle_name,
};
WaylandSeat::WaylandSeat(struct wl_seat *wl_seat)
{
this->wl_seat = wl_seat;
wl_seat_add_listener(this->wl_seat, &seat_listener, this);
}
static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer,
uint32_t serial, struct wl_surface *surface,
wl_fixed_t surface_x, wl_fixed_t surface_y)
{
WaylandPointer *pointer = static_cast<WaylandPointer *>(data);
struct wl_cursor_theme *cursor_theme = wl_cursor_theme_load(NULL, 16, pointer->registry->wl_shm);
struct wl_cursor_image *image = wl_cursor_theme_get_cursor(cursor_theme, "left_ptr")->images[0];
;
wl_surface_attach(pointer->cursor_surface,
wl_cursor_image_get_buffer(image), 0, 0);
wl_surface_damage(pointer->cursor_surface, 1, 0,
image->width, image->height);
wl_surface_commit(pointer->cursor_surface);
wl_pointer_set_cursor(wl_pointer, serial, pointer->cursor_surface,
image->hotspot_x, image->hotspot_y);
}
static void wl_pointer_leave(void *data, struct wl_pointer *wl_pointer,
uint32_t serial, struct wl_surface *surface)
{
}
static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer,
uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y)
{
WaylandPointer *pointer = static_cast<WaylandPointer *>(data);
int cur_x = wl_fixed_to_int(surface_x);
int cur_y = wl_fixed_to_int(surface_y);
pointer->delegate->on_mouse_motion(cur_x, cur_y);
}
static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
uint32_t serial, uint32_t time, uint32_t button, uint32_t state)
{
WaylandPointer *pointer = static_cast<WaylandPointer *>(data);
if (button == BTN_LEFT && state == 1)
{
pointer->delegate->on_mouse_click();
}
else if (button == BTN_LEFT && state == 0)
{
pointer->delegate->on_mouse_up();
}
}
static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
uint32_t time, uint32_t axis, wl_fixed_t value)
{
WaylandPointer *pointer = static_cast<WaylandPointer *>(data);
if (axis == 0)
{
pointer->delegate->on_mouse_scroll(value < 0);
}
}
static void wl_pointer_frame(void *data, struct wl_pointer *wl_pointer)
{
// Who cares
}
static void wl_pointer_axis_source(void *data, struct wl_pointer *wl_pointer,
uint32_t axis_source)
{
// Who cares
}
static void wl_pointer_axis_stop(void *data, struct wl_pointer *wl_pointer,
uint32_t time, uint32_t axis)
{
// Who cares
}
static void wl_pointer_axis_discrete(void *data, struct wl_pointer *wl_pointer,
uint32_t axis, int32_t discrete)
{
// Who cares
}
struct wl_pointer_listener pointer_listener = {
.enter = wl_pointer_enter,
.leave = wl_pointer_leave,
.motion = wl_pointer_motion,
.button = wl_pointer_button,
.axis = wl_pointer_axis,
.frame = wl_pointer_frame,
.axis_source = wl_pointer_axis_source,
.axis_stop = wl_pointer_axis_stop,
.axis_discrete = wl_pointer_axis_discrete,
};
void WaylandPointer::handle_capabilities(struct wl_seat *wl_seat, uint32_t caps)
{
if (caps & WL_SEAT_CAPABILITY_POINTER)
{
// std::cout << "Has pointer\n";
this->wl_pointer = wl_seat_get_pointer(wl_seat);
wl_pointer_add_listener(this->wl_pointer, &pointer_listener, this);
}
else if (this->wl_pointer != nullptr)
{
wl_pointer_release(this->wl_pointer);
this->wl_pointer = nullptr;
}
}