add touch events

This commit is contained in:
Tony Crisci 2017-08-27 11:34:25 -04:00
parent ac503a47a7
commit ed126b0881
3 changed files with 163 additions and 16 deletions

View file

@ -45,8 +45,27 @@ struct sample_state {
struct wl_listener cursor_motion_absolute; struct wl_listener cursor_motion_absolute;
struct wl_listener cursor_button; struct wl_listener cursor_button;
struct wl_listener cursor_axis; struct wl_listener cursor_axis;
struct wl_listener touch_motion;
struct wl_listener touch_up;
struct wl_listener touch_down;
struct wl_listener touch_cancel;
list_t *touch_points;
}; };
struct touch_point {
int32_t slot;
double x, y;
};
static void warp_to_touch(struct sample_state *sample) {
wlr_log(L_DEBUG, "TODO: warp to touch");
for (size_t i = 0; i < sample->touch_points->length; ++i) {
struct touch_point *point = sample->touch_points->items[i];
wlr_log(L_DEBUG, "have point x=%f,y=%f", point->x, point->y);
}
}
static void handle_output_frame(struct output_state *output, struct timespec *ts) { static void handle_output_frame(struct output_state *output, struct timespec *ts) {
struct compositor_state *state = output->compositor; struct compositor_state *state = output->compositor;
struct sample_state *sample = state->data; struct sample_state *sample = state->data;
@ -149,8 +168,10 @@ static void handle_input_add(struct compositor_state *state, struct
struct sample_state *sample = state->data; struct sample_state *sample = state->data;
// TODO handle other input devices // TODO handle other input devices
if (device->type == WLR_INPUT_DEVICE_POINTER) { if (device->type == WLR_INPUT_DEVICE_POINTER ||
struct sample_input_device *s_device = calloc(1, sizeof(struct sample_input_device)); device->type == WLR_INPUT_DEVICE_TOUCH) {
struct sample_input_device *s_device;
s_device = calloc(1, sizeof(struct sample_input_device));
s_device->device = device; s_device->device = device;
wl_list_insert(&sample->devices, &s_device->link); wl_list_insert(&sample->devices, &s_device->link);
@ -216,10 +237,60 @@ static void handle_cursor_axis(struct wl_listener *listener, void *data) {
sizeof(sample->clear_color)); sizeof(sample->clear_color));
} }
static void handle_touch_up(struct wl_listener *listener, void *data) {
struct sample_state *sample = wl_container_of(listener, sample, touch_up);
struct wlr_event_touch_up *event = data;
for (size_t i = 0; i < sample->touch_points->length; ++i) {
struct touch_point *point = sample->touch_points->items[i];
if (point->slot == event->slot) {
list_del(sample->touch_points, i);
break;
}
}
warp_to_touch(sample);
}
static void handle_touch_down(struct wl_listener *listener, void *data) {
struct sample_state *sample = wl_container_of(listener, sample, touch_down);
struct wlr_event_touch_down *event = data;
struct touch_point *point = calloc(1, sizeof(struct touch_state));
point->slot = event->slot;
point->x = event->x_mm / event->width_mm;
point->y = event->y_mm / event->height_mm;
if (list_add(sample->touch_points, point) == -1) {
free(point);
}
warp_to_touch(sample);
}
static void handle_touch_motion(struct wl_listener *listener, void *data) {
struct sample_state *sample = wl_container_of(listener, sample, touch_motion);
struct wlr_event_touch_motion *event = data;
for (size_t i = 0; i < sample->touch_points->length; ++i) {
struct touch_point *point = sample->touch_points->items[i];
if (point->slot == event->slot) {
point->x = event->x_mm / event->width_mm;
point->y = event->y_mm / event->height_mm;
break;
}
}
warp_to_touch(sample);
}
static void handle_touch_cancel(struct wl_listener *listener, void *data) {
//struct sample_state *sample = wl_container_of(listener, sample, touch_cancel);
//struct wlr_event_touch_cancel *event = data;
wlr_log(L_DEBUG, "TODO: touch cancel");
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
struct sample_state state = { struct sample_state state = {
.default_color = { 0.25f, 0.25f, 0.25f, 1 }, .default_color = { 0.25f, 0.25f, 0.25f, 1 },
.clear_color = { 0.25f, 0.25f, 0.25f, 1 } .clear_color = { 0.25f, 0.25f, 0.25f, 1 },
.touch_points = list_create(),
}; };
state.config = parse_args(argc, argv); state.config = parse_args(argc, argv);
@ -227,6 +298,7 @@ int main(int argc, char *argv[]) {
wlr_cursor_map_to_region(state.cursor, state.config->cursor.mapped_geo); wlr_cursor_map_to_region(state.cursor, state.config->cursor.mapped_geo);
wl_list_init(&state.devices); wl_list_init(&state.devices);
// pointer events
wl_signal_add(&state.cursor->events.motion, &state.cursor_motion); wl_signal_add(&state.cursor->events.motion, &state.cursor_motion);
state.cursor_motion.notify = handle_cursor_motion; state.cursor_motion.notify = handle_cursor_motion;
@ -239,6 +311,19 @@ int main(int argc, char *argv[]) {
wl_signal_add(&state.cursor->events.axis, &state.cursor_axis); wl_signal_add(&state.cursor->events.axis, &state.cursor_axis);
state.cursor_axis.notify = handle_cursor_axis; state.cursor_axis.notify = handle_cursor_axis;
// touch events
wl_signal_add(&state.cursor->events.touch_up, &state.touch_up);
state.touch_up.notify = handle_touch_up;
wl_signal_add(&state.cursor->events.touch_down, &state.touch_down);
state.touch_down.notify = handle_touch_down;
wl_signal_add(&state.cursor->events.touch_motion, &state.touch_motion);
state.touch_motion.notify = handle_touch_motion;
wl_signal_add(&state.cursor->events.touch_cancel, &state.touch_cancel);
state.touch_cancel.notify = handle_touch_cancel;
struct compositor_state compositor = { 0 }; struct compositor_state compositor = { 0 };
compositor.data = &state; compositor.data = &state;
compositor.output_add_cb = handle_output_add; compositor.output_add_cb = handle_output_add;

View file

@ -20,6 +20,11 @@ struct wlr_cursor {
struct wl_signal motion_absolute; struct wl_signal motion_absolute;
struct wl_signal button; struct wl_signal button;
struct wl_signal axis; struct wl_signal axis;
struct wl_signal touch_up;
struct wl_signal touch_down;
struct wl_signal touch_motion;
struct wl_signal touch_cancel;
} events; } events;
}; };

View file

@ -18,6 +18,11 @@ struct wlr_cursor_device {
struct wl_listener motion_absolute; struct wl_listener motion_absolute;
struct wl_listener button; struct wl_listener button;
struct wl_listener axis; struct wl_listener axis;
struct wl_listener touch_down;
struct wl_listener touch_up;
struct wl_listener touch_motion;
struct wl_listener touch_cancel;
}; };
struct wlr_cursor_state { struct wlr_cursor_state {
@ -45,11 +50,18 @@ struct wlr_cursor *wlr_cursor_init() {
wl_list_init(&cur->state->devices); wl_list_init(&cur->state->devices);
// pointer signals
wl_signal_init(&cur->events.motion); wl_signal_init(&cur->events.motion);
wl_signal_init(&cur->events.motion_absolute); wl_signal_init(&cur->events.motion_absolute);
wl_signal_init(&cur->events.button); wl_signal_init(&cur->events.button);
wl_signal_init(&cur->events.axis); wl_signal_init(&cur->events.axis);
// touch signals
wl_signal_init(&cur->events.touch_up);
wl_signal_init(&cur->events.touch_down);
wl_signal_init(&cur->events.touch_motion);
wl_signal_init(&cur->events.touch_cancel);
cur->x = 100; cur->x = 100;
cur->y = 100; cur->y = 100;
@ -218,19 +230,48 @@ static void handle_pointer_axis(struct wl_listener *listener, void *data) {
wl_signal_emit(&device->cursor->events.axis, event); wl_signal_emit(&device->cursor->events.axis, event);
} }
static void handle_touch_up(struct wl_listener *listener, void *data) {
struct wlr_event_touch_up *event = data;
struct wlr_cursor_device *device;
device = wl_container_of(listener, device, touch_up);
wl_signal_emit(&device->cursor->events.touch_up, event);
}
static void handle_touch_down(struct wl_listener *listener, void *data) {
struct wlr_event_touch_down *event = data;
struct wlr_cursor_device *device;
device = wl_container_of(listener, device, touch_down);
wl_signal_emit(&device->cursor->events.touch_down, event);
}
static void handle_touch_motion(struct wl_listener *listener, void *data) {
struct wlr_event_touch_motion *event = data;
struct wlr_cursor_device *device;
device = wl_container_of(listener, device, touch_motion);
wl_signal_emit(&device->cursor->events.touch_motion, event);
}
static void handle_touch_cancel(struct wl_listener *listener, void *data) {
struct wlr_event_touch_cancel *event = data;
struct wlr_cursor_device *device;
device = wl_container_of(listener, device, touch_cancel);
wl_signal_emit(&device->cursor->events.touch_cancel, event);
}
void wlr_cursor_attach_input_device(struct wlr_cursor *cur, void wlr_cursor_attach_input_device(struct wlr_cursor *cur,
struct wlr_input_device *dev) { struct wlr_input_device *dev) {
if (dev->type != WLR_INPUT_DEVICE_POINTER && if (dev->type != WLR_INPUT_DEVICE_POINTER &&
dev->type != WLR_INPUT_DEVICE_TOUCH && dev->type != WLR_INPUT_DEVICE_TOUCH &&
dev->type != WLR_INPUT_DEVICE_TABLET_TOOL) { dev->type != WLR_INPUT_DEVICE_TABLET_TOOL) {
wlr_log(L_ERROR, "only device types of pointer, touch or tablet tool are" wlr_log(L_ERROR, "only device types of pointer, touch or tablet tool"
"supported"); "are supported");
return; return;
} }
// TODO support other device types // TODO support other device types
if (dev->type != WLR_INPUT_DEVICE_POINTER) { if (dev->type != WLR_INPUT_DEVICE_POINTER &&
wlr_log(L_ERROR, "TODO: support touch and tablet tool devices"); dev->type != WLR_INPUT_DEVICE_TOUCH) {
wlr_log(L_ERROR, "TODO: support tablet tool devices");
return; return;
} }
@ -242,7 +283,8 @@ void wlr_cursor_attach_input_device(struct wlr_cursor *cur,
} }
} }
struct wlr_cursor_device *device = calloc(1, sizeof(struct wlr_cursor_device)); struct wlr_cursor_device *device;
device = calloc(1, sizeof(struct wlr_cursor_device));
if (!device) { if (!device) {
wlr_log(L_ERROR, "Failed to allocate wlr_cursor_device"); wlr_log(L_ERROR, "Failed to allocate wlr_cursor_device");
return; return;
@ -252,17 +294,32 @@ void wlr_cursor_attach_input_device(struct wlr_cursor *cur,
device->device = dev; device->device = dev;
// listen to events // listen to events
wl_signal_add(&dev->pointer->events.motion, &device->motion);
device->motion.notify = handle_pointer_motion;
wl_signal_add(&dev->pointer->events.motion_absolute, &device->motion_absolute); if (dev->type == WLR_INPUT_DEVICE_POINTER) {
device->motion_absolute.notify = handle_pointer_motion_absolute; wl_signal_add(&dev->pointer->events.motion, &device->motion);
device->motion.notify = handle_pointer_motion;
wl_signal_add(&dev->pointer->events.button, &device->button); wl_signal_add(&dev->pointer->events.motion_absolute, &device->motion_absolute);
device->button.notify = handle_pointer_button; device->motion_absolute.notify = handle_pointer_motion_absolute;
wl_signal_add(&dev->pointer->events.axis, &device->axis); wl_signal_add(&dev->pointer->events.button, &device->button);
device->axis.notify = handle_pointer_axis; device->button.notify = handle_pointer_button;
wl_signal_add(&dev->pointer->events.axis, &device->axis);
device->axis.notify = handle_pointer_axis;
} else if (dev->type == WLR_INPUT_DEVICE_TOUCH) {
wl_signal_add(&dev->touch->events.motion, &device->touch_motion);
device->touch_motion.notify = handle_touch_motion;
wl_signal_add(&dev->touch->events.down, &device->touch_down);
device->touch_down.notify = handle_touch_down;
wl_signal_add(&dev->touch->events.up, &device->touch_up);
device->touch_up.notify = handle_touch_up;
wl_signal_add(&dev->touch->events.cancel, &device->touch_cancel);
device->touch_cancel.notify = handle_touch_cancel;
}
wl_list_insert(&cur->state->devices, &device->link); wl_list_insert(&cur->state->devices, &device->link);
} }