aboutsummaryrefslogtreecommitdiff
path: root/backend/libinput/tablet_pad.c
blob: 3cf7f142970e07fe0ba1a77aaea829108c82d1e7 (plain)
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
#include <assert.h>
#include <string.h>
#include <libinput.h>
#include <stdlib.h>
#include <wlr/interfaces/wlr_tablet_pad.h>
#include <wlr/util/log.h>
#include "backend/libinput.h"

const struct wlr_tablet_pad_impl libinput_tablet_pad_impl = {
	.name = "libinput-tablet-pad",
};

static void group_destroy(struct wlr_tablet_pad_group *group) {
	free(group->buttons);
	free(group->strips);
	free(group->rings);
	free(group);
}

static void add_pad_group_from_libinput(struct wlr_tablet_pad *pad,
		struct libinput_device *device, unsigned int index) {
	struct libinput_tablet_pad_mode_group *li_group =
		libinput_device_tablet_pad_get_mode_group(device, index);
	struct wlr_tablet_pad_group *group = calloc(1, sizeof(*group));
	if (!group) {
		wlr_log_errno(WLR_ERROR, "failed to allocate wlr_tablet_pad_group");
		return;
	}

	for (size_t i = 0; i < pad->ring_count; ++i) {
		if (libinput_tablet_pad_mode_group_has_ring(li_group, i)) {
			++group->ring_count;
		}
	}
	group->rings = calloc(group->ring_count, sizeof(unsigned int));
	if (group->rings == NULL) {
		goto group_fail;
	}

	size_t ring = 0;
	for (size_t i = 0; i < pad->ring_count; ++i) {
		if (libinput_tablet_pad_mode_group_has_ring(li_group, i)) {
			group->rings[ring++] = i;
		}
	}

	for (size_t i = 0; i < pad->strip_count; ++i) {
		if (libinput_tablet_pad_mode_group_has_strip(li_group, i)) {
			++group->strip_count;
		}
	}
	group->strips = calloc(group->strip_count, sizeof(unsigned int));
	if (group->strips == NULL) {
		goto group_fail;
	}
	size_t strip = 0;
	for (size_t i = 0; i < pad->strip_count; ++i) {
		if (libinput_tablet_pad_mode_group_has_strip(li_group, i)) {
			group->strips[strip++] = i;
		}
	}

	for (size_t i = 0; i < pad->button_count; ++i) {
		if (libinput_tablet_pad_mode_group_has_button(li_group, i)) {
			++group->button_count;
		}
	}
	group->buttons = calloc(group->button_count, sizeof(unsigned int));
	if (group->buttons == NULL) {
		goto group_fail;
	}
	size_t button = 0;
	for (size_t i = 0; i < pad->button_count; ++i) {
		if (libinput_tablet_pad_mode_group_has_button(li_group, i)) {
			group->buttons[button++] = i;
		}
	}

	group->mode_count = libinput_tablet_pad_mode_group_get_num_modes(li_group);

	libinput_tablet_pad_mode_group_ref(li_group);

	wl_list_insert(&pad->groups, &group->link);
	return;

group_fail:
	wlr_log(WLR_ERROR, "failed to configure wlr_tablet_pad_group");
	group_destroy(group);
}

void init_device_tablet_pad(struct wlr_libinput_input_device *dev) {
	struct libinput_device *handle = dev->handle;
	const char *name = libinput_device_get_name(handle);
	struct wlr_tablet_pad *wlr_tablet_pad = &dev->tablet_pad;
	wlr_tablet_pad_init(wlr_tablet_pad, &libinput_tablet_pad_impl, name);
	wlr_tablet_pad->base.vendor = libinput_device_get_id_vendor(handle);
	wlr_tablet_pad->base.product = libinput_device_get_id_product(handle);

	wlr_tablet_pad->button_count =
		libinput_device_tablet_pad_get_num_buttons(handle);
	wlr_tablet_pad->ring_count =
		libinput_device_tablet_pad_get_num_rings(handle);
	wlr_tablet_pad->strip_count =
		libinput_device_tablet_pad_get_num_strips(handle);

	struct udev_device *udev = libinput_device_get_udev_device(handle);
	char **dst = wl_array_add(&wlr_tablet_pad->paths, sizeof(char *));
	*dst = strdup(udev_device_get_syspath(udev));

	int groups = libinput_device_tablet_pad_get_num_mode_groups(handle);
	for (int i = 0; i < groups; ++i) {
		add_pad_group_from_libinput(wlr_tablet_pad, handle, i);
	}
}

void finish_device_tablet_pad(struct wlr_libinput_input_device *dev) {
	struct wlr_tablet_pad_group *group, *tmp;
	wl_list_for_each_safe(group, tmp, &dev->tablet_pad.groups, link) {
		group_destroy(group);
	}

	wlr_tablet_pad_finish(&dev->tablet_pad);

	int groups = libinput_device_tablet_pad_get_num_mode_groups(dev->handle);
	for (int i = 0; i < groups; ++i) {
		struct libinput_tablet_pad_mode_group *li_group =
			libinput_device_tablet_pad_get_mode_group(dev->handle, i);
		libinput_tablet_pad_mode_group_unref(li_group);
	}
}

struct wlr_libinput_input_device *device_from_tablet_pad(
		struct wlr_tablet_pad *wlr_tablet_pad) {
	assert(wlr_tablet_pad->impl == &libinput_tablet_pad_impl);

	struct wlr_libinput_input_device *dev =
		wl_container_of(wlr_tablet_pad, dev, tablet_pad);
	return dev;
}

void handle_tablet_pad_button(struct libinput_event *event,
		struct wlr_tablet_pad *tablet_pad) {
	struct libinput_event_tablet_pad *pevent =
		libinput_event_get_tablet_pad_event(event);
	struct wlr_tablet_pad_button_event wlr_event = {
		.time_msec = usec_to_msec(libinput_event_tablet_pad_get_time_usec(pevent)),
		.button = libinput_event_tablet_pad_get_button_number(pevent),
		.mode = libinput_event_tablet_pad_get_mode(pevent),
		.group = libinput_tablet_pad_mode_group_get_index(
			libinput_event_tablet_pad_get_mode_group(pevent)),
	};
	switch (libinput_event_tablet_pad_get_button_state(pevent)) {
	case LIBINPUT_BUTTON_STATE_PRESSED:
		wlr_event.state = WLR_BUTTON_PRESSED;
		break;
	case LIBINPUT_BUTTON_STATE_RELEASED:
		wlr_event.state = WLR_BUTTON_RELEASED;
		break;
	}
	wl_signal_emit_mutable(&tablet_pad->events.button, &wlr_event);
}

void handle_tablet_pad_ring(struct libinput_event *event,
		struct wlr_tablet_pad *tablet_pad) {
	struct libinput_event_tablet_pad *pevent =
		libinput_event_get_tablet_pad_event(event);
	struct wlr_tablet_pad_ring_event wlr_event = {
		.time_msec = usec_to_msec(libinput_event_tablet_pad_get_time_usec(pevent)),
		.ring = libinput_event_tablet_pad_get_ring_number(pevent),
		.position = libinput_event_tablet_pad_get_ring_position(pevent),
		.mode = libinput_event_tablet_pad_get_mode(pevent),
	};
	switch (libinput_event_tablet_pad_get_ring_source(pevent)) {
	case LIBINPUT_TABLET_PAD_RING_SOURCE_UNKNOWN:
		wlr_event.source = WLR_TABLET_PAD_RING_SOURCE_UNKNOWN;
		break;
	case LIBINPUT_TABLET_PAD_RING_SOURCE_FINGER:
		wlr_event.source = WLR_TABLET_PAD_RING_SOURCE_FINGER;
		break;
	}
	wl_signal_emit_mutable(&tablet_pad->events.ring, &wlr_event);
}

void handle_tablet_pad_strip(struct libinput_event *event,
		struct wlr_tablet_pad *tablet_pad) {
	struct libinput_event_tablet_pad *pevent =
		libinput_event_get_tablet_pad_event(event);
	struct wlr_tablet_pad_strip_event wlr_event = {
		.time_msec = usec_to_msec(libinput_event_tablet_pad_get_time_usec(pevent)),
		.strip = libinput_event_tablet_pad_get_strip_number(pevent),
		.position = libinput_event_tablet_pad_get_strip_position(pevent),
		.mode = libinput_event_tablet_pad_get_mode(pevent),
	};
	switch (libinput_event_tablet_pad_get_strip_source(pevent)) {
	case LIBINPUT_TABLET_PAD_STRIP_SOURCE_UNKNOWN:
		wlr_event.source = WLR_TABLET_PAD_STRIP_SOURCE_UNKNOWN;
		break;
	case LIBINPUT_TABLET_PAD_STRIP_SOURCE_FINGER:
		wlr_event.source = WLR_TABLET_PAD_STRIP_SOURCE_FINGER;
		break;
	}
	wl_signal_emit_mutable(&tablet_pad->events.strip, &wlr_event);
}