Flesh out touch events and add demo

master
Drew DeVault 8 years ago
parent d6905f86cb
commit 3f24f8a1be

@ -139,6 +139,21 @@ void wlr_libinput_event(struct wlr_backend_state *state,
case LIBINPUT_EVENT_POINTER_AXIS: case LIBINPUT_EVENT_POINTER_AXIS:
handle_pointer_axis(event, device); handle_pointer_axis(event, device);
break; break;
case LIBINPUT_EVENT_TOUCH_DOWN:
handle_touch_down(event, device);
break;
case LIBINPUT_EVENT_TOUCH_UP:
handle_touch_up(event, device);
break;
case LIBINPUT_EVENT_TOUCH_MOTION:
handle_touch_motion(event, device);
break;
case LIBINPUT_EVENT_TOUCH_CANCEL:
handle_touch_cancel(event, device);
break;
case LIBINPUT_EVENT_TOUCH_FRAME:
// no-op (at least for now)
break;
default: default:
wlr_log(L_DEBUG, "Unknown libinput event %d", event_type); wlr_log(L_DEBUG, "Unknown libinput event %d", event_type);
break; break;

@ -13,3 +13,81 @@ struct wlr_touch *wlr_libinput_touch_create(
assert(device); assert(device);
return wlr_touch_create(NULL, NULL); return wlr_touch_create(NULL, NULL);
} }
void handle_touch_down(struct libinput_event *event,
struct libinput_device *device) {
struct wlr_input_device *dev =
get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, device);
if (!dev) {
wlr_log(L_DEBUG, "Got a touch event for a device with no touch?");
return;
}
struct libinput_event_touch *tevent =
libinput_event_get_touch_event(event);
struct wlr_touch_down *wlr_event =
calloc(1, sizeof(struct wlr_touch_down));
wlr_event->time_sec = libinput_event_touch_get_time(tevent);
wlr_event->time_usec = libinput_event_touch_get_time_usec(tevent);
wlr_event->slot = libinput_event_touch_get_slot(tevent);
wlr_event->x_mm = libinput_event_touch_get_x(tevent);
wlr_event->y_mm = libinput_event_touch_get_y(tevent);
libinput_device_get_size(device, &wlr_event->width_mm, &wlr_event->height_mm);
wl_signal_emit(&dev->touch->events.down, wlr_event);
}
void handle_touch_up(struct libinput_event *event,
struct libinput_device *device) {
struct wlr_input_device *dev =
get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, device);
if (!dev) {
wlr_log(L_DEBUG, "Got a touch event for a device with no touch?");
return;
}
struct libinput_event_touch *tevent =
libinput_event_get_touch_event(event);
struct wlr_touch_up *wlr_event =
calloc(1, sizeof(struct wlr_touch_up));
wlr_event->time_sec = libinput_event_touch_get_time(tevent);
wlr_event->time_usec = libinput_event_touch_get_time_usec(tevent);
wlr_event->slot = libinput_event_touch_get_slot(tevent);
wl_signal_emit(&dev->touch->events.up, wlr_event);
}
void handle_touch_motion(struct libinput_event *event,
struct libinput_device *device) {
struct wlr_input_device *dev =
get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, device);
if (!dev) {
wlr_log(L_DEBUG, "Got a touch event for a device with no touch?");
return;
}
struct libinput_event_touch *tevent =
libinput_event_get_touch_event(event);
struct wlr_touch_motion *wlr_event =
calloc(1, sizeof(struct wlr_touch_motion));
wlr_event->time_sec = libinput_event_touch_get_time(tevent);
wlr_event->time_usec = libinput_event_touch_get_time_usec(tevent);
wlr_event->slot = libinput_event_touch_get_slot(tevent);
wlr_event->x_mm = libinput_event_touch_get_x(tevent);
wlr_event->y_mm = libinput_event_touch_get_y(tevent);
libinput_device_get_size(device, &wlr_event->width_mm, &wlr_event->height_mm);
wl_signal_emit(&dev->touch->events.motion, wlr_event);
}
void handle_touch_cancel(struct libinput_event *event,
struct libinput_device *device) {
struct wlr_input_device *dev =
get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, device);
if (!dev) {
wlr_log(L_DEBUG, "Got a touch event for a device with no touch?");
return;
}
struct libinput_event_touch *tevent =
libinput_event_get_touch_event(event);
struct wlr_touch_cancel *wlr_event =
calloc(1, sizeof(struct wlr_touch_cancel));
wlr_event->time_sec = libinput_event_touch_get_time(tevent);
wlr_event->time_usec = libinput_event_touch_get_time_usec(tevent);
wlr_event->slot = libinput_event_touch_get_slot(tevent);
wl_signal_emit(&dev->touch->events.cancel, wlr_event);
}

@ -40,3 +40,16 @@ target_link_libraries(pointer
wlr-render wlr-render
${XKBCOMMON_LIBRARIES} ${XKBCOMMON_LIBRARIES}
) )
add_executable(touch
touch.c
shared.c
cat.c
)
target_link_libraries(touch
wlr-backend
wlr-session
wlr-render
${XKBCOMMON_LIBRARIES}
)

@ -114,6 +114,59 @@ static void pointer_add(struct wlr_input_device *device, struct compositor_state
wl_list_insert(&state->pointers, &pstate->link); wl_list_insert(&state->pointers, &pstate->link);
} }
static void touch_down_notify(struct wl_listener *listener, void *data) {
struct wlr_touch_down *event = data;
struct touch_state *tstate = wl_container_of(listener, tstate, down);
if (tstate->compositor->touch_down_cb) {
tstate->compositor->touch_down_cb(tstate, event->slot,
event->x_mm, event->y_mm, event->width_mm, event->height_mm);
}
}
static void touch_motion_notify(struct wl_listener *listener, void *data) {
struct wlr_touch_motion *event = data;
struct touch_state *tstate = wl_container_of(listener, tstate, motion);
if (tstate->compositor->touch_motion_cb) {
tstate->compositor->touch_motion_cb(tstate, event->slot,
event->x_mm, event->y_mm, event->width_mm, event->height_mm);
}
}
static void touch_up_notify(struct wl_listener *listener, void *data) {
struct wlr_touch_up *event = data;
struct touch_state *tstate = wl_container_of(listener, tstate, up);
if (tstate->compositor->touch_up_cb) {
tstate->compositor->touch_up_cb(tstate, event->slot);
}
}
static void touch_cancel_notify(struct wl_listener *listener, void *data) {
struct wlr_touch_cancel *event = data;
struct touch_state *tstate = wl_container_of(listener, tstate, cancel);
if (tstate->compositor->touch_cancel_cb) {
tstate->compositor->touch_cancel_cb(tstate, event->slot);
}
}
static void touch_add(struct wlr_input_device *device, struct compositor_state *state) {
struct touch_state *tstate = calloc(sizeof(struct touch_state), 1);
tstate->device = device;
tstate->compositor = state;
wl_list_init(&tstate->down.link);
wl_list_init(&tstate->motion.link);
wl_list_init(&tstate->up.link);
wl_list_init(&tstate->cancel.link);
tstate->down.notify = touch_down_notify;
tstate->motion.notify = touch_motion_notify;
tstate->up.notify = touch_up_notify;
tstate->cancel.notify = touch_cancel_notify;
wl_signal_add(&device->touch->events.down, &tstate->down);
wl_signal_add(&device->touch->events.motion, &tstate->motion);
wl_signal_add(&device->touch->events.up, &tstate->up);
wl_signal_add(&device->touch->events.cancel, &tstate->cancel);
wl_list_insert(&state->touch, &tstate->link);
}
static void input_add_notify(struct wl_listener *listener, void *data) { static void input_add_notify(struct wl_listener *listener, void *data) {
struct wlr_input_device *device = data; struct wlr_input_device *device = data;
struct compositor_state *state = wl_container_of(listener, state, input_add); struct compositor_state *state = wl_container_of(listener, state, input_add);
@ -124,6 +177,9 @@ static void input_add_notify(struct wl_listener *listener, void *data) {
case WLR_INPUT_DEVICE_POINTER: case WLR_INPUT_DEVICE_POINTER:
pointer_add(device, state); pointer_add(device, state);
break; break;
case WLR_INPUT_DEVICE_TOUCH:
touch_add(device, state);
break;
default: default:
break; break;
} }
@ -156,10 +212,28 @@ static void pointer_remove(struct wlr_input_device *device, struct compositor_st
return; return;
} }
wl_list_remove(&pstate->link); wl_list_remove(&pstate->link);
//wl_list_remove(&pstate->motion.link); wl_list_remove(&pstate->motion.link);
wl_list_remove(&pstate->motion_absolute.link); //wl_list_remove(&pstate->motion_absolute.link);
//wl_list_remove(&pstate->button.link); wl_list_remove(&pstate->button.link);
//wl_list_remove(&pstate->axis.link); wl_list_remove(&pstate->axis.link);
}
static void touch_remove(struct wlr_input_device *device, struct compositor_state *state) {
struct touch_state *tstate = NULL, *_tstate;
wl_list_for_each(_tstate, &state->touch, link) {
if (_tstate->device == device) {
tstate = _tstate;
break;
}
}
if (!tstate) {
return;
}
wl_list_remove(&tstate->link);
wl_list_remove(&tstate->down.link);
wl_list_remove(&tstate->motion.link);
wl_list_remove(&tstate->up.link);
wl_list_remove(&tstate->cancel.link);
} }
static void input_remove_notify(struct wl_listener *listener, void *data) { static void input_remove_notify(struct wl_listener *listener, void *data) {
@ -172,6 +246,9 @@ static void input_remove_notify(struct wl_listener *listener, void *data) {
case WLR_INPUT_DEVICE_POINTER: case WLR_INPUT_DEVICE_POINTER:
pointer_remove(device, state); pointer_remove(device, state);
break; break;
case WLR_INPUT_DEVICE_TOUCH:
touch_remove(device, state);
break;
default: default:
break; break;
} }
@ -245,6 +322,7 @@ void compositor_init(struct compositor_state *state) {
wl_list_init(&state->keyboards); wl_list_init(&state->keyboards);
wl_list_init(&state->pointers); wl_list_init(&state->pointers);
wl_list_init(&state->touch);
wl_list_init(&state->input_add.link); wl_list_init(&state->input_add.link);
state->input_add.notify = input_add_notify; state->input_add.notify = input_add_notify;
wl_list_init(&state->input_remove.link); wl_list_init(&state->input_remove.link);

@ -39,6 +39,17 @@ struct pointer_state {
void *data; void *data;
}; };
struct touch_state {
struct compositor_state *compositor;
struct wlr_input_device *device;
struct wl_listener down;
struct wl_listener up;
struct wl_listener motion;
struct wl_listener cancel;
struct wl_list link;
void *data;
};
struct compositor_state { struct compositor_state {
void (*output_add_cb)(struct output_state *s); void (*output_add_cb)(struct output_state *s);
void (*keyboard_add_cb)(struct keyboard_state *s); void (*keyboard_add_cb)(struct keyboard_state *s);
@ -55,6 +66,12 @@ struct compositor_state {
enum wlr_axis_source source, enum wlr_axis_source source,
enum wlr_axis_orientation orientation, enum wlr_axis_orientation orientation,
double delta); double delta);
void (*touch_down_cb)(struct touch_state *s, int32_t slot,
double x, double y, double width, double height);
void (*touch_motion_cb)(struct touch_state *s, int32_t slot,
double x, double y, double width, double height);
void (*touch_up_cb)(struct touch_state *s, int32_t slot);
void (*touch_cancel_cb)(struct touch_state *s, int32_t slot);
struct wl_display *display; struct wl_display *display;
struct wl_event_loop *event_loop; struct wl_event_loop *event_loop;
@ -63,6 +80,7 @@ struct compositor_state {
struct wl_list keyboards; struct wl_list keyboards;
struct wl_list pointers; struct wl_list pointers;
struct wl_list touch;
struct wl_listener input_add; struct wl_listener input_add;
struct wl_listener input_remove; struct wl_listener input_remove;

@ -0,0 +1,122 @@
#define _POSIX_C_SOURCE 199309L
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <math.h>
#include <wayland-server.h>
#include <wayland-server-protocol.h>
#include <xkbcommon/xkbcommon.h>
#include <GLES3/gl3.h>
#include <wlr/common/list.h>
#include <wlr/render/matrix.h>
#include <wlr/render/gles3.h>
#include <wlr/render.h>
#include <wlr/backend.h>
#include <wlr/session.h>
#include <wlr/types.h>
#include "shared.h"
#include "cat.h"
struct sample_state {
struct wlr_renderer *renderer;
struct wlr_surface *cat_texture;
list_t *touch_points;
};
struct touch_point {
int32_t slot;
double x, y;
};
static void handle_output_frame(struct output_state *output, struct timespec *ts) {
struct compositor_state *state = output->compositor;
struct sample_state *sample = state->data;
struct wlr_output *wlr_output = output->output;
int32_t width, height;
wlr_output_effective_resolution(wlr_output, &width, &height);
wlr_renderer_begin(sample->renderer, wlr_output);
float matrix[16];
for (size_t i = 0; i < sample->touch_points->length; ++i) {
struct touch_point *p = sample->touch_points->items[i];
wlr_surface_get_matrix(sample->cat_texture, &matrix,
&wlr_output->transform_matrix,
(int)(p->x * width) - sample->cat_texture->width / 2,
(int)(p->y * height) - sample->cat_texture->height / 2);
wlr_render_with_matrix(sample->renderer,
sample->cat_texture, &matrix);
}
wlr_renderer_end(sample->renderer);
}
static void handle_keyboard_key(struct keyboard_state *kbstate,
xkb_keysym_t sym, enum wlr_key_state key_state) {
if (sym == XKB_KEY_Escape) {
kbstate->compositor->exit = true;
}
}
static void handle_touch_down(struct touch_state *tstate, int32_t slot,
double x, double y, double width, double height) {
struct sample_state *sample = tstate->compositor->data;
struct touch_point *point = calloc(1, sizeof(struct touch_state));
point->slot = slot;
point->x = x / width;
point->y = y / height;
list_add(sample->touch_points, point);
}
static void handle_touch_up(struct touch_state *tstate, int32_t slot) {
struct sample_state *sample = tstate->compositor->data;
for (size_t i = 0; i < sample->touch_points->length; ++i) {
struct touch_point *point = sample->touch_points->items[i];
if (point->slot == slot) {
list_del(sample->touch_points, i);
break;
}
}
}
static void handle_touch_motion(struct touch_state *tstate, int32_t slot,
double x, double y, double width, double height) {
struct sample_state *sample = tstate->compositor->data;
for (size_t i = 0; i < sample->touch_points->length; ++i) {
struct touch_point *point = sample->touch_points->items[i];
if (point->slot == slot) {
point->x = x / width;
point->y = y / height;
break;
}
}
}
int main(int argc, char *argv[]) {
struct sample_state state = {
.touch_points = list_create()
};
struct compositor_state compositor;
compositor_init(&compositor);
compositor.output_frame_cb = handle_output_frame;
compositor.keyboard_key_cb = handle_keyboard_key;
compositor.touch_down_cb = handle_touch_down;
compositor.touch_up_cb = handle_touch_up;
compositor.touch_motion_cb = handle_touch_motion;
state.renderer = wlr_gles3_renderer_init();
state.cat_texture = wlr_render_surface_init(state.renderer);
wlr_surface_attach_pixels(state.cat_texture, GL_RGB,
cat_tex.width, cat_tex.height, cat_tex.pixel_data);
compositor.data = &state;
compositor_run(&compositor);
wlr_surface_destroy(state.cat_texture);
wlr_renderer_destroy(state.renderer);
}

@ -56,7 +56,5 @@ void handle_touch_motion(struct libinput_event *event,
struct libinput_device *device); struct libinput_device *device);
void handle_touch_cancel(struct libinput_event *event, void handle_touch_cancel(struct libinput_event *event,
struct libinput_device *device); struct libinput_device *device);
void handle_touch_frame(struct libinput_event *event,
struct libinput_device *device);
#endif #endif

@ -146,10 +146,37 @@ struct wlr_touch {
struct wl_signal up; struct wl_signal up;
struct wl_signal motion; struct wl_signal motion;
struct wl_signal cancel; struct wl_signal cancel;
struct wl_signal frame;
} events; } events;
}; };
struct wlr_touch_down {
uint32_t time_sec;
uint64_t time_usec;
int32_t slot;
double x_mm, y_mm;
double width_mm, height_mm;
};
struct wlr_touch_up {
uint32_t time_sec;
uint64_t time_usec;
int32_t slot;
};
struct wlr_touch_motion {
uint32_t time_sec;
uint64_t time_usec;
int32_t slot;
double x_mm, y_mm;
double width_mm, height_mm;
};
struct wlr_touch_cancel {
uint32_t time_sec;
uint64_t time_usec;
int32_t slot;
};
// TODO: tablet & tablet tool // TODO: tablet & tablet tool
// TODO: gestures // TODO: gestures
// TODO: switch // TODO: switch

@ -13,7 +13,6 @@ struct wlr_touch *wlr_touch_create(struct wlr_touch_impl *impl,
wl_signal_init(&touch->events.down); wl_signal_init(&touch->events.down);
wl_signal_init(&touch->events.up); wl_signal_init(&touch->events.up);
wl_signal_init(&touch->events.motion); wl_signal_init(&touch->events.motion);
wl_signal_init(&touch->events.frame);
wl_signal_init(&touch->events.cancel); wl_signal_init(&touch->events.cancel);
return touch; return touch;
} }

Loading…
Cancel
Save