aboutsummaryrefslogtreecommitdiff
path: root/backend/libinput/tablet_tool.c
blob: 95bdd3fdcf55351511272f82bdd3f55fd0c5495a (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
#include <stdlib.h>
#include <assert.h>
#include <libinput.h>
#include <wlr/session.h>
#include <wlr/types/wlr_input_device.h>
#include <wlr/interfaces/wlr_tablet_tool.h>
#include <wlr/util/log.h>
#include "backend/libinput.h"

struct wlr_tablet_tool *wlr_libinput_tablet_tool_create(
		struct libinput_device *device) {
	assert(device);
	return wlr_tablet_tool_create(NULL, NULL);
}

void handle_tablet_tool_axis(struct libinput_event *event,
		struct libinput_device *device) {
	struct wlr_input_device *dev =
		get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, device);
	if (!dev) {
		wlr_log(L_DEBUG, "Got a tablet tool event for a device with no tablet tools?");
		return;
	}
	struct libinput_event_tablet_tool *tevent =
		libinput_event_get_tablet_tool_event(event);
	struct wlr_event_tablet_tool_axis *wlr_event =
		calloc(1, sizeof(struct wlr_event_tablet_tool_axis));
	wlr_event->time_sec = libinput_event_tablet_tool_get_time(tevent);
	wlr_event->time_usec = libinput_event_tablet_tool_get_time_usec(tevent);
	libinput_device_get_size(device, &wlr_event->width_mm, &wlr_event->height_mm);
	if (libinput_event_tablet_tool_x_has_changed(tevent)) {
		wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_X;
		wlr_event->x_mm = libinput_event_tablet_tool_get_x(tevent);
	}
	if (libinput_event_tablet_tool_y_has_changed(tevent)) {
		wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_Y;
		wlr_event->y_mm = libinput_event_tablet_tool_get_y(tevent);
	}
	if (libinput_event_tablet_tool_pressure_has_changed(tevent)) {
		wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_PRESSURE;
		wlr_event->pressure = libinput_event_tablet_tool_get_pressure(tevent);
	}
	if (libinput_event_tablet_tool_distance_has_changed(tevent)) {
		wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_DISTANCE;
		wlr_event->distance = libinput_event_tablet_tool_get_distance(tevent);
	}
	if (libinput_event_tablet_tool_tilt_x_has_changed(tevent)) {
		wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_TILT_X;
		wlr_event->tilt_x = libinput_event_tablet_tool_get_tilt_x(tevent);
	}
	if (libinput_event_tablet_tool_tilt_y_has_changed(tevent)) {
		wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_TILT_Y;
		wlr_event->tilt_y = libinput_event_tablet_tool_get_tilt_y(tevent);
	}
	if (libinput_event_tablet_tool_rotation_has_changed(tevent)) {
		wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_ROTATION;
		wlr_event->rotation = libinput_event_tablet_tool_get_rotation(tevent);
	}
	if (libinput_event_tablet_tool_slider_has_changed(tevent)) {
		wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_SLIDER;
		wlr_event->slider = libinput_event_tablet_tool_get_slider_position(tevent);
	}
	if (libinput_event_tablet_tool_wheel_has_changed(tevent)) {
		wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_WHEEL;
		wlr_event->wheel_delta = libinput_event_tablet_tool_get_wheel_delta(tevent);
	}
	wl_signal_emit(&dev->tablet_tool->events.axis, wlr_event);
}

void handle_tablet_tool_proximity(struct libinput_event *event,
		struct libinput_device *device) {
	struct wlr_input_device *dev =
		get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, device);
	if (!dev) {
		wlr_log(L_DEBUG, "Got a tablet tool event for a device with no tablet tools?");
		return;
	}
	struct libinput_event_tablet_tool *tevent =
		libinput_event_get_tablet_tool_event(event);
	struct wlr_event_tablet_tool_proximity *wlr_event =
		calloc(1, sizeof(struct wlr_event_tablet_tool_proximity));
	wlr_event->time_sec = libinput_event_tablet_tool_get_time(tevent);
	wlr_event->time_usec = libinput_event_tablet_tool_get_time_usec(tevent);
	switch (libinput_event_tablet_tool_get_proximity_state(tevent)) {
	case LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT:
		wlr_event->state = WLR_TABLET_TOOL_PROXIMITY_OUT;
		break;
	case LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN:
		wlr_event->state = WLR_TABLET_TOOL_PROXIMITY_IN;
		handle_tablet_tool_axis(event, device);
		break;
	}
	wl_signal_emit(&dev->tablet_tool->events.proximity, wlr_event);
}

void handle_tablet_tool_tip(struct libinput_event *event,
		struct libinput_device *device) {
	struct wlr_input_device *dev =
		get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, device);
	if (!dev) {
		wlr_log(L_DEBUG, "Got a tablet tool event for a device with no tablet tools?");
		return;
	}
	handle_tablet_tool_axis(event, device);
	struct libinput_event_tablet_tool *tevent =
		libinput_event_get_tablet_tool_event(event);
	struct wlr_event_tablet_tool_tip *wlr_event =
		calloc(1, sizeof(struct wlr_event_tablet_tool_tip));
	wlr_event->time_sec = libinput_event_tablet_tool_get_time(tevent);
	wlr_event->time_usec = libinput_event_tablet_tool_get_time_usec(tevent);
	switch (libinput_event_tablet_tool_get_tip_state(tevent)) {
	case LIBINPUT_TABLET_TOOL_TIP_UP:
		wlr_event->state = WLR_TABLET_TOOL_TIP_UP;
		break;
	case LIBINPUT_TABLET_TOOL_TIP_DOWN:
		wlr_event->state = WLR_TABLET_TOOL_TIP_DOWN;
		break;
	}
	wl_signal_emit(&dev->tablet_tool->events.tip, wlr_event);
}

void handle_tablet_tool_button(struct libinput_event *event,
		struct libinput_device *device) {
	struct wlr_input_device *dev =
		get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, device);
	if (!dev) {
		wlr_log(L_DEBUG, "Got a tablet tool event for a device with no tablet tools?");
		return;
	}
	handle_tablet_tool_axis(event, device);
	struct libinput_event_tablet_tool *tevent =
		libinput_event_get_tablet_tool_event(event);
	struct wlr_event_tablet_tool_button *wlr_event =
		calloc(1, sizeof(struct wlr_event_tablet_tool_button));
	wlr_event->time_sec = libinput_event_tablet_tool_get_time(tevent);
	wlr_event->time_usec = libinput_event_tablet_tool_get_time_usec(tevent);
	wlr_event->button = libinput_event_tablet_tool_get_button(tevent);
	switch (libinput_event_tablet_tool_get_button_state(tevent)) {
	case LIBINPUT_BUTTON_STATE_RELEASED:
		wlr_event->state = WLR_BUTTON_RELEASED;
		break;
	case LIBINPUT_BUTTON_STATE_PRESSED:
		wlr_event->state = WLR_BUTTON_PRESSED;
		break;
	}
	wl_signal_emit(&dev->tablet_tool->events.button, wlr_event);
}