aboutsummaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2017-06-14 14:50:09 -0400
committerDrew DeVault <sir@cmpwn.com>2017-06-14 14:50:09 -0400
commit3f24f8a1bee10fb3aadf8c57ca107fe5aaa7cffa (patch)
tree923093315e92cc5351644b145dbad74b4317d9dd /example
parentd6905f86cb9d430e0ba05c6a066ed350761116d1 (diff)
Flesh out touch events and add demo
Diffstat (limited to 'example')
-rw-r--r--example/CMakeLists.txt13
-rw-r--r--example/shared.c86
-rw-r--r--example/shared.h18
-rw-r--r--example/touch.c122
4 files changed, 235 insertions, 4 deletions
diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt
index ef11a822..a6085392 100644
--- a/example/CMakeLists.txt
+++ b/example/CMakeLists.txt
@@ -40,3 +40,16 @@ target_link_libraries(pointer
wlr-render
${XKBCOMMON_LIBRARIES}
)
+
+add_executable(touch
+ touch.c
+ shared.c
+ cat.c
+)
+
+target_link_libraries(touch
+ wlr-backend
+ wlr-session
+ wlr-render
+ ${XKBCOMMON_LIBRARIES}
+)
diff --git a/example/shared.c b/example/shared.c
index 6de16074..2592c924 100644
--- a/example/shared.c
+++ b/example/shared.c
@@ -114,6 +114,59 @@ static void pointer_add(struct wlr_input_device *device, struct compositor_state
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) {
struct wlr_input_device *device = data;
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:
pointer_add(device, state);
break;
+ case WLR_INPUT_DEVICE_TOUCH:
+ touch_add(device, state);
+ break;
default:
break;
}
@@ -156,10 +212,28 @@ static void pointer_remove(struct wlr_input_device *device, struct compositor_st
return;
}
wl_list_remove(&pstate->link);
- //wl_list_remove(&pstate->motion.link);
- wl_list_remove(&pstate->motion_absolute.link);
- //wl_list_remove(&pstate->button.link);
- //wl_list_remove(&pstate->axis.link);
+ wl_list_remove(&pstate->motion.link);
+ //wl_list_remove(&pstate->motion_absolute.link);
+ wl_list_remove(&pstate->button.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) {
@@ -172,6 +246,9 @@ static void input_remove_notify(struct wl_listener *listener, void *data) {
case WLR_INPUT_DEVICE_POINTER:
pointer_remove(device, state);
break;
+ case WLR_INPUT_DEVICE_TOUCH:
+ touch_remove(device, state);
+ break;
default:
break;
}
@@ -245,6 +322,7 @@ void compositor_init(struct compositor_state *state) {
wl_list_init(&state->keyboards);
wl_list_init(&state->pointers);
+ wl_list_init(&state->touch);
wl_list_init(&state->input_add.link);
state->input_add.notify = input_add_notify;
wl_list_init(&state->input_remove.link);
diff --git a/example/shared.h b/example/shared.h
index 7f0dade5..d09bcd0e 100644
--- a/example/shared.h
+++ b/example/shared.h
@@ -39,6 +39,17 @@ struct pointer_state {
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 {
void (*output_add_cb)(struct output_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_orientation orientation,
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_event_loop *event_loop;
@@ -63,6 +80,7 @@ struct compositor_state {
struct wl_list keyboards;
struct wl_list pointers;
+ struct wl_list touch;
struct wl_listener input_add;
struct wl_listener input_remove;
diff --git a/example/touch.c b/example/touch.c
new file mode 100644
index 00000000..7f4f3800
--- /dev/null
+++ b/example/touch.c
@@ -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);
+}