Use wl_list for touch points in examples/

This commit is contained in:
emersion 2017-11-20 21:26:20 +01:00
parent 151901123f
commit 64221c6da1
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 32 additions and 30 deletions

View File

@ -46,7 +46,7 @@ struct sample_state {
struct wl_listener touch_up; struct wl_listener touch_up;
struct wl_listener touch_down; struct wl_listener touch_down;
struct wl_listener touch_cancel; struct wl_listener touch_cancel;
struct wlr_list *touch_points; struct wl_list touch_points;
struct wl_listener tablet_tool_axis; struct wl_listener tablet_tool_axis;
struct wl_listener tablet_tool_proxmity; struct wl_listener tablet_tool_proxmity;
@ -57,22 +57,25 @@ struct sample_state {
struct touch_point { struct touch_point {
int32_t touch_id; int32_t touch_id;
double x, y; double x, y;
struct wl_list link;
}; };
static void warp_to_touch(struct sample_state *sample, static void warp_to_touch(struct sample_state *sample,
struct wlr_input_device *dev) { struct wlr_input_device *dev) {
if (sample->touch_points->length == 0) { if (wl_list_empty(&sample->touch_points)) {
return; return;
} }
double x = 0, y = 0; double x = 0, y = 0;
for (size_t i = 0; i < sample->touch_points->length; ++i) { size_t n = 0;
struct touch_point *point = sample->touch_points->items[i]; struct touch_point *point;
wl_list_for_each(point, &sample->touch_points, link) {
x += point->x; x += point->x;
y += point->y; y += point->y;
n++;
} }
x /= sample->touch_points->length; x /= n;
y /= sample->touch_points->length; y /= n;
wlr_cursor_warp_absolute(sample->cursor, dev, x, y); wlr_cursor_warp_absolute(sample->cursor, dev, x, y);
} }
@ -197,10 +200,11 @@ static void handle_cursor_axis(struct wl_listener *listener, void *data) {
static void handle_touch_up(struct wl_listener *listener, void *data) { static void handle_touch_up(struct wl_listener *listener, void *data) {
struct sample_state *sample = wl_container_of(listener, sample, touch_up); struct sample_state *sample = wl_container_of(listener, sample, touch_up);
struct wlr_event_touch_up *event = data; 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]; struct touch_point *point, *tmp;
wl_list_for_each_safe(point, tmp, &sample->touch_points, link) {
if (point->touch_id == event->touch_id) { if (point->touch_id == event->touch_id) {
wlr_list_del(sample->touch_points, i); wl_list_remove(&point->link);
break; break;
} }
} }
@ -215,9 +219,7 @@ static void handle_touch_down(struct wl_listener *listener, void *data) {
point->touch_id = event->touch_id; point->touch_id = event->touch_id;
point->x = event->x_mm / event->width_mm; point->x = event->x_mm / event->width_mm;
point->y = event->y_mm / event->height_mm; point->y = event->y_mm / event->height_mm;
if (wlr_list_add(sample->touch_points, point) == -1) { wl_list_insert(&sample->touch_points, &point->link);
free(point);
}
warp_to_touch(sample, event->device); warp_to_touch(sample, event->device);
} }
@ -226,8 +228,9 @@ static void handle_touch_motion(struct wl_listener *listener, void *data) {
struct sample_state *sample = struct sample_state *sample =
wl_container_of(listener, sample, touch_motion); wl_container_of(listener, sample, touch_motion);
struct wlr_event_touch_motion *event = data; 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]; struct touch_point *point;
wl_list_for_each(point, &sample->touch_points, link) {
if (point->touch_id == event->touch_id) { if (point->touch_id == event->touch_id) {
point->x = event->x_mm / event->width_mm; point->x = event->x_mm / event->width_mm;
point->y = event->y_mm / event->height_mm; point->y = event->y_mm / event->height_mm;
@ -257,7 +260,6 @@ 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 = wlr_list_create(),
}; };
state.config = parse_args(argc, argv); state.config = parse_args(argc, argv);
@ -266,6 +268,7 @@ int main(int argc, char *argv[]) {
wlr_cursor_attach_output_layout(state.cursor, state.layout); wlr_cursor_attach_output_layout(state.cursor, state.layout);
wlr_cursor_map_to_region(state.cursor, state.config->cursor.mapped_box); wlr_cursor_map_to_region(state.cursor, state.config->cursor.mapped_box);
wl_list_init(&state.devices); wl_list_init(&state.devices);
wl_list_init(&state.touch_points);
// pointer events // pointer events
wl_signal_add(&state.cursor->events.motion, &state.cursor_motion); wl_signal_add(&state.cursor->events.motion, &state.cursor_motion);

View File

@ -24,12 +24,13 @@
struct sample_state { struct sample_state {
struct wlr_renderer *renderer; struct wlr_renderer *renderer;
struct wlr_texture *cat_texture; struct wlr_texture *cat_texture;
struct wlr_list *touch_points; struct wl_list touch_points;
}; };
struct touch_point { struct touch_point {
int32_t touch_id; int32_t touch_id;
double x, y; double x, y;
struct wl_list link;
}; };
static void handle_output_frame(struct output_state *output, struct timespec *ts) { static void handle_output_frame(struct output_state *output, struct timespec *ts) {
@ -44,14 +45,14 @@ 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);
float matrix[16]; float matrix[16];
for (size_t i = 0; i < sample->touch_points->length; ++i) { struct touch_point *p;
struct touch_point *p = sample->touch_points->items[i]; wl_list_for_each(p, &sample->touch_points, link) {
wlr_texture_get_matrix(sample->cat_texture, &matrix, wlr_texture_get_matrix(sample->cat_texture, &matrix,
&wlr_output->transform_matrix, &wlr_output->transform_matrix,
(int)(p->x * width) - sample->cat_texture->width / 2, (int)(p->x * width) - sample->cat_texture->width / 2,
(int)(p->y * height) - sample->cat_texture->height / 2); (int)(p->y * height) - sample->cat_texture->height / 2);
wlr_render_with_matrix(sample->renderer, wlr_render_with_matrix(sample->renderer,
sample->cat_texture, &matrix); sample->cat_texture, &matrix);
} }
wlr_renderer_end(sample->renderer); wlr_renderer_end(sample->renderer);
@ -65,17 +66,15 @@ static void handle_touch_down(struct touch_state *tstate, int32_t touch_id,
point->touch_id = touch_id; point->touch_id = touch_id;
point->x = x / width; point->x = x / width;
point->y = y / height; point->y = y / height;
if (wlr_list_add(sample->touch_points, point) == -1) { wl_list_insert(&sample->touch_points, &point->link);
free(point);
}
} }
static void handle_touch_up(struct touch_state *tstate, int32_t touch_id) { static void handle_touch_up(struct touch_state *tstate, int32_t touch_id) {
struct sample_state *sample = tstate->compositor->data; struct sample_state *sample = tstate->compositor->data;
for (size_t i = 0; i < sample->touch_points->length; ++i) { struct touch_point *point, *tmp;
struct touch_point *point = sample->touch_points->items[i]; wl_list_for_each_safe(point, tmp, &sample->touch_points, link) {
if (point->touch_id == touch_id) { if (point->touch_id == touch_id) {
wlr_list_del(sample->touch_points, i); wl_list_remove(&point->link);
break; break;
} }
} }
@ -84,8 +83,8 @@ static void handle_touch_up(struct touch_state *tstate, int32_t touch_id) {
static void handle_touch_motion(struct touch_state *tstate, int32_t touch_id, static void handle_touch_motion(struct touch_state *tstate, int32_t touch_id,
double x, double y, double width, double height) { double x, double y, double width, double height) {
struct sample_state *sample = tstate->compositor->data; struct sample_state *sample = tstate->compositor->data;
for (size_t i = 0; i < sample->touch_points->length; ++i) { struct touch_point *point;
struct touch_point *point = sample->touch_points->items[i]; wl_list_for_each(point, &sample->touch_points, link) {
if (point->touch_id == touch_id) { if (point->touch_id == touch_id) {
point->x = x / width; point->x = x / width;
point->y = y / height; point->y = y / height;
@ -95,9 +94,9 @@ static void handle_touch_motion(struct touch_state *tstate, int32_t touch_id,
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
struct sample_state state = { struct sample_state state;
.touch_points = wlr_list_create() wl_list_init(&state.touch_points);
};
struct compositor_state compositor = { 0, struct compositor_state compositor = { 0,
.data = &state, .data = &state,
.output_frame_cb = handle_output_frame, .output_frame_cb = handle_output_frame,