Add mode support to libinput backend

And extend tablet example with tilt and ring support
This commit is contained in:
Drew DeVault 2018-03-07 20:57:55 -05:00
parent 36dcad13d0
commit bfc0e95d2c
6 changed files with 62 additions and 18 deletions

View file

@ -34,6 +34,7 @@ void handle_tablet_pad_button(struct libinput_event *event,
wlr_event.time_msec = wlr_event.time_msec =
usec_to_msec(libinput_event_tablet_pad_get_time_usec(pevent)); usec_to_msec(libinput_event_tablet_pad_get_time_usec(pevent));
wlr_event.button = libinput_event_tablet_pad_get_button_number(pevent); wlr_event.button = libinput_event_tablet_pad_get_button_number(pevent);
wlr_event.mode = libinput_event_tablet_pad_get_mode(pevent);
switch (libinput_event_tablet_pad_get_button_state(pevent)) { switch (libinput_event_tablet_pad_get_button_state(pevent)) {
case LIBINPUT_BUTTON_STATE_PRESSED: case LIBINPUT_BUTTON_STATE_PRESSED:
wlr_event.state = WLR_BUTTON_PRESSED; wlr_event.state = WLR_BUTTON_PRESSED;
@ -60,6 +61,7 @@ void handle_tablet_pad_ring(struct libinput_event *event,
usec_to_msec(libinput_event_tablet_pad_get_time_usec(pevent)); usec_to_msec(libinput_event_tablet_pad_get_time_usec(pevent));
wlr_event.ring = libinput_event_tablet_pad_get_ring_number(pevent); wlr_event.ring = libinput_event_tablet_pad_get_ring_number(pevent);
wlr_event.position = libinput_event_tablet_pad_get_ring_position(pevent); wlr_event.position = libinput_event_tablet_pad_get_ring_position(pevent);
wlr_event.mode = libinput_event_tablet_pad_get_mode(pevent);
switch (libinput_event_tablet_pad_get_ring_source(pevent)) { switch (libinput_event_tablet_pad_get_ring_source(pevent)) {
case LIBINPUT_TABLET_PAD_RING_SOURCE_UNKNOWN: case LIBINPUT_TABLET_PAD_RING_SOURCE_UNKNOWN:
wlr_event.source = WLR_TABLET_PAD_RING_SOURCE_UNKNOWN; wlr_event.source = WLR_TABLET_PAD_RING_SOURCE_UNKNOWN;
@ -86,6 +88,7 @@ void handle_tablet_pad_strip(struct libinput_event *event,
usec_to_msec(libinput_event_tablet_pad_get_time_usec(pevent)); usec_to_msec(libinput_event_tablet_pad_get_time_usec(pevent));
wlr_event.strip = libinput_event_tablet_pad_get_strip_number(pevent); wlr_event.strip = libinput_event_tablet_pad_get_strip_number(pevent);
wlr_event.position = libinput_event_tablet_pad_get_strip_position(pevent); wlr_event.position = libinput_event_tablet_pad_get_strip_position(pevent);
wlr_event.mode = libinput_event_tablet_pad_get_mode(pevent);
switch (libinput_event_tablet_pad_get_strip_source(pevent)) { switch (libinput_event_tablet_pad_get_strip_source(pevent)) {
case LIBINPUT_TABLET_PAD_STRIP_SOURCE_UNKNOWN: case LIBINPUT_TABLET_PAD_STRIP_SOURCE_UNKNOWN:
wlr_event.source = WLR_TABLET_PAD_STRIP_SOURCE_UNKNOWN; wlr_event.source = WLR_TABLET_PAD_STRIP_SOURCE_UNKNOWN;

View file

@ -71,8 +71,6 @@ void handle_tablet_tool_axis(struct libinput_event *event,
wlr_event.updated_axes |= WLR_TABLET_TOOL_AXIS_WHEEL; wlr_event.updated_axes |= WLR_TABLET_TOOL_AXIS_WHEEL;
wlr_event.wheel_delta = libinput_event_tablet_tool_get_wheel_delta(tevent); wlr_event.wheel_delta = libinput_event_tablet_tool_get_wheel_delta(tevent);
} }
wlr_log(L_DEBUG, "Tablet tool axis event %d @ %f,%f",
wlr_event.updated_axes, wlr_event.x_mm, wlr_event.y_mm);
wlr_signal_emit_safe(&wlr_dev->tablet_tool->events.axis, &wlr_event); wlr_signal_emit_safe(&wlr_dev->tablet_tool->events.axis, &wlr_event);
} }

View file

@ -294,6 +294,14 @@ static void tablet_pad_button_notify(struct wl_listener *listener, void *data) {
} }
} }
static void tablet_pad_ring_notify(struct wl_listener *listener, void *data) {
struct wlr_event_tablet_pad_ring *event = data;
struct tablet_pad_state *pstate = wl_container_of(listener, pstate, ring);
if (pstate->compositor->pad_ring_cb) {
pstate->compositor->pad_ring_cb(pstate, event->ring, event->position);
}
}
static void tablet_pad_destroy_notify(struct wl_listener *listener, void *data) { static void tablet_pad_destroy_notify(struct wl_listener *listener, void *data) {
struct tablet_pad_state *pstate = wl_container_of(listener, pstate, destroy); struct tablet_pad_state *pstate = wl_container_of(listener, pstate, destroy);
struct compositor_state *state = pstate->compositor; struct compositor_state *state = pstate->compositor;
@ -315,6 +323,8 @@ static void tablet_pad_add(struct wlr_input_device *device,
wl_signal_add(&device->events.destroy, &pstate->destroy); wl_signal_add(&device->events.destroy, &pstate->destroy);
pstate->button.notify = tablet_pad_button_notify; pstate->button.notify = tablet_pad_button_notify;
wl_signal_add(&device->tablet_pad->events.button, &pstate->button); wl_signal_add(&device->tablet_pad->events.button, &pstate->button);
pstate->ring.notify = tablet_pad_ring_notify;
wl_signal_add(&device->tablet_pad->events.ring, &pstate->ring);
wl_list_insert(&state->tablet_pads, &pstate->link); wl_list_insert(&state->tablet_pads, &pstate->link);
} }

View file

@ -73,6 +73,7 @@ struct tablet_pad_state {
struct wlr_input_device *device; struct wlr_input_device *device;
struct wl_listener destroy; struct wl_listener destroy;
struct wl_listener button; struct wl_listener button;
struct wl_listener ring;
struct wl_list link; struct wl_list link;
void *data; void *data;
}; };
@ -117,6 +118,8 @@ struct compositor_state {
uint32_t button, enum wlr_button_state state); uint32_t button, enum wlr_button_state state);
void (*pad_button_cb)(struct tablet_pad_state *s, void (*pad_button_cb)(struct tablet_pad_state *s,
uint32_t button, enum wlr_button_state state); uint32_t button, enum wlr_button_state state);
void (*pad_ring_cb)(struct tablet_pad_state *s,
uint32_t ring, double position);
struct wl_display *display; struct wl_display *display;
struct wl_event_loop *event_loop; struct wl_event_loop *event_loop;

View file

@ -14,6 +14,7 @@
#include <wlr/render.h> #include <wlr/render.h>
#include <wlr/backend.h> #include <wlr/backend.h>
#include <wlr/backend/session.h> #include <wlr/backend/session.h>
#include <wlr/types/wlr_box.h>
#include <wlr/types/wlr_output.h> #include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_tablet_tool.h> #include <wlr/types/wlr_tablet_tool.h>
#include <wlr/types/wlr_tablet_pad.h> #include <wlr/types/wlr_tablet_pad.h>
@ -28,7 +29,9 @@ struct sample_state {
double distance; double distance;
double pressure; double pressure;
double x_mm, y_mm; double x_mm, y_mm;
double x_tilt, y_tilt;
double width_mm, height_mm; double width_mm, height_mm;
double ring;
struct wl_list link; struct wl_list link;
float tool_color[4]; float tool_color[4];
float pad_color[4]; float pad_color[4];
@ -46,7 +49,7 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts
wlr_renderer_begin(sample->renderer, wlr_output); wlr_renderer_begin(sample->renderer, wlr_output);
wlr_renderer_clear(sample->renderer, &(float[]){0.25f, 0.25f, 0.25f, 1}); wlr_renderer_clear(sample->renderer, &(float[]){0.25f, 0.25f, 0.25f, 1});
float matrix[16], view[16]; float matrix[16];
float distance = 0.8f * (1 - sample->distance); float distance = 0.8f * (1 - sample->distance);
float tool_color[4] = { distance, distance, distance, 1 }; float tool_color[4] = { distance, distance, distance, 1 };
for (size_t i = 0; sample->button && i < 4; ++i) { for (size_t i = 0; sample->button && i < 4; ++i) {
@ -58,22 +61,31 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts
float pad_height = sample->height_mm * scale; float pad_height = sample->height_mm * scale;
float left = width / 2.0f - pad_width / 2.0f; float left = width / 2.0f - pad_width / 2.0f;
float top = height / 2.0f - pad_height / 2.0f; float top = height / 2.0f - pad_height / 2.0f;
wlr_matrix_translate(&matrix, left, top, 0); struct wlr_box box = {
wlr_matrix_scale(&view, pad_width, pad_height, 1); .x = left, .y = top,
wlr_matrix_mul(&matrix, &view, &view); .width = pad_width, .height = pad_height,
wlr_matrix_mul(&wlr_output->transform_matrix, &view, &matrix); };
wlr_matrix_project_box(&matrix, &box, 0, 0,
&wlr_output->transform_matrix);
wlr_render_colored_quad(sample->renderer, &sample->pad_color, &matrix); wlr_render_colored_quad(sample->renderer, &sample->pad_color, &matrix);
if (sample->proximity) { if (sample->proximity) {
wlr_matrix_translate(&matrix, struct wlr_box box = {
sample->x_mm * scale - 8 * (sample->pressure + 1) + left, .x = sample->x_mm * scale - 8 * (sample->pressure + 1) + left,
sample->y_mm * scale - 8 * (sample->pressure + 1) + top, 0); .y = sample->y_mm * scale - 8 * (sample->pressure + 1) + top,
wlr_matrix_scale(&view, .width = 16 * (sample->pressure + 1),
16 * (sample->pressure + 1), .height = 16 * (sample->pressure + 1),
16 * (sample->pressure + 1), 1); };
wlr_matrix_mul(&matrix, &view, &view); wlr_matrix_project_box(&matrix, &box, 0, sample->ring,
wlr_matrix_mul(&wlr_output->transform_matrix, &view, &matrix); &wlr_output->transform_matrix);
wlr_render_colored_ellipse(sample->renderer, &tool_color, &matrix); wlr_render_colored_quad(sample->renderer, &tool_color, &matrix);
box.x += sample->x_tilt;
box.y += sample->y_tilt;
box.width /= 2;
box.height /= 2;
wlr_matrix_project_box(&matrix, &box, 0, 0,
&wlr_output->transform_matrix);
wlr_render_colored_quad(sample->renderer, &tool_color, &matrix);
} }
wlr_renderer_end(sample->renderer); wlr_renderer_end(sample->renderer);
@ -97,6 +109,12 @@ static void handle_tool_axis(struct tablet_tool_state *tstate,
if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_PRESSURE)) { if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_PRESSURE)) {
sample->pressure = event->pressure; sample->pressure = event->pressure;
} }
if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_TILT_X)) {
sample->x_tilt = event->tilt_x;
}
if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_TILT_Y)) {
sample->y_tilt = event->tilt_y;
}
} }
static void handle_tool_proximity(struct tablet_tool_state *tstate, static void handle_tool_proximity(struct tablet_tool_state *tstate,
@ -125,7 +143,7 @@ static void handle_tool_button(struct tablet_tool_state *tstate,
static void handle_pad_button(struct tablet_pad_state *pstate, static void handle_pad_button(struct tablet_pad_state *pstate,
uint32_t button, enum wlr_button_state state) { uint32_t button, enum wlr_button_state state) {
struct sample_state *sample = pstate->compositor->data; struct sample_state *sample = pstate->compositor->data;
float default_color[4] = { 0.75, 0.75, 0.75, 1.0 }; float default_color[4] = { 0.5, 0.5, 0.5, 1.0 };
if (state == WLR_BUTTON_RELEASED) { if (state == WLR_BUTTON_RELEASED) {
memcpy(sample->pad_color, default_color, sizeof(default_color)); memcpy(sample->pad_color, default_color, sizeof(default_color));
} else { } else {
@ -139,11 +157,19 @@ static void handle_pad_button(struct tablet_pad_state *pstate,
} }
} }
static void handle_pad_ring(struct tablet_pad_state *pstate,
uint32_t ring, double position) {
struct sample_state *sample = pstate->compositor->data;
if (position != -1) {
sample->ring = -(position * (M_PI / 180.0));
}
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
wlr_log_init(L_DEBUG, NULL); wlr_log_init(L_DEBUG, NULL);
struct sample_state state = { struct sample_state state = {
.tool_color = { 1, 1, 1, 1 }, .tool_color = { 1, 1, 1, 1 },
.pad_color = { 0.75, 0.75, 0.75, 1.0 } .pad_color = { 0.5, 0.5, 0.5, 1.0 }
}; };
struct compositor_state compositor = { 0, struct compositor_state compositor = { 0,
.data = &state, .data = &state,
@ -152,6 +178,7 @@ int main(int argc, char *argv[]) {
.tool_proximity_cb = handle_tool_proximity, .tool_proximity_cb = handle_tool_proximity,
.tool_button_cb = handle_tool_button, .tool_button_cb = handle_tool_button,
.pad_button_cb = handle_pad_button, .pad_button_cb = handle_pad_button,
.pad_ring_cb = handle_pad_ring,
}; };
compositor_init(&compositor); compositor_init(&compositor);

View file

@ -29,6 +29,7 @@ struct wlr_event_tablet_pad_button {
uint32_t time_msec; uint32_t time_msec;
uint32_t button; uint32_t button;
enum wlr_button_state state; enum wlr_button_state state;
unsigned int mode;
}; };
enum wlr_tablet_pad_ring_source { enum wlr_tablet_pad_ring_source {
@ -41,6 +42,7 @@ struct wlr_event_tablet_pad_ring {
enum wlr_tablet_pad_ring_source source; enum wlr_tablet_pad_ring_source source;
uint32_t ring; uint32_t ring;
double position; double position;
unsigned int mode;
}; };
enum wlr_tablet_pad_strip_source { enum wlr_tablet_pad_strip_source {
@ -53,6 +55,7 @@ struct wlr_event_tablet_pad_strip {
enum wlr_tablet_pad_strip_source source; enum wlr_tablet_pad_strip_source source;
uint32_t strip; uint32_t strip;
double position; double position;
unsigned int mode;
}; };
#endif #endif