cursor: store wlr_cursor inline in wlr_cursor_state

Removes one allocation, makes this a bit more consistent with the
rest of wlroots.
This commit is contained in:
Simon Ser 2023-06-23 18:07:41 +02:00 committed by Alexander Orzechowski
parent aca48124ad
commit f0b8a68654

View file

@ -74,7 +74,8 @@ struct wlr_cursor_output_cursor {
}; };
struct wlr_cursor_state { struct wlr_cursor_state {
struct wlr_cursor *cursor; struct wlr_cursor cursor;
struct wl_list devices; // wlr_cursor_device::link struct wl_list devices; // wlr_cursor_device::link
struct wl_list output_cursors; // wlr_cursor_output_cursor::link struct wl_list output_cursors; // wlr_cursor_output_cursor::link
struct wlr_output_layout *layout; struct wlr_output_layout *layout;
@ -87,21 +88,14 @@ struct wlr_cursor_state {
}; };
struct wlr_cursor *wlr_cursor_create(void) { struct wlr_cursor *wlr_cursor_create(void) {
struct wlr_cursor *cur = calloc(1, sizeof(struct wlr_cursor)); struct wlr_cursor_state *state = calloc(1, sizeof(struct wlr_cursor_state));
if (!cur) { if (!state) {
wlr_log(WLR_ERROR, "Failed to allocate wlr_cursor");
return NULL;
}
cur->state = calloc(1, sizeof(struct wlr_cursor_state));
if (!cur->state) {
wlr_log(WLR_ERROR, "Failed to allocate wlr_cursor_state"); wlr_log(WLR_ERROR, "Failed to allocate wlr_cursor_state");
free(cur);
return NULL; return NULL;
} }
struct wlr_cursor *cur = &state->cursor;
cur->state->cursor = cur; cur->state = state;
cur->state->mapped_output = NULL;
wl_list_init(&cur->state->devices); wl_list_init(&cur->state->devices);
wl_list_init(&cur->state->output_cursors); wl_list_init(&cur->state->output_cursors);
@ -214,7 +208,6 @@ void wlr_cursor_destroy(struct wlr_cursor *cur) {
} }
free(cur->state); free(cur->state);
free(cur);
} }
static struct wlr_cursor_device *get_cursor_device(struct wlr_cursor *cur, static struct wlr_cursor_device *get_cursor_device(struct wlr_cursor *cur,
@ -965,7 +958,7 @@ void wlr_cursor_detach_input_device(struct wlr_cursor *cur,
static void handle_layout_destroy(struct wl_listener *listener, void *data) { static void handle_layout_destroy(struct wl_listener *listener, void *data) {
struct wlr_cursor_state *state = struct wlr_cursor_state *state =
wl_container_of(listener, state, layout_destroy); wl_container_of(listener, state, layout_destroy);
cursor_detach_output_layout(state->cursor); cursor_detach_output_layout(&state->cursor);
} }
static void handle_layout_output_destroy(struct wl_listener *listener, static void handle_layout_output_destroy(struct wl_listener *listener,
@ -990,7 +983,7 @@ static void layout_add(struct wlr_cursor_state *state,
wlr_log(WLR_ERROR, "Failed to allocate wlr_cursor_output_cursor"); wlr_log(WLR_ERROR, "Failed to allocate wlr_cursor_output_cursor");
return; return;
} }
output_cursor->cursor = state->cursor; output_cursor->cursor = &state->cursor;
wl_list_init(&output_cursor->surface_destroy.link); wl_list_init(&output_cursor->surface_destroy.link);
wl_list_init(&output_cursor->surface_commit.link); wl_list_init(&output_cursor->surface_commit.link);
@ -1022,15 +1015,15 @@ static void handle_layout_change(struct wl_listener *listener, void *data) {
wl_container_of(listener, state, layout_change); wl_container_of(listener, state, layout_change);
struct wlr_output_layout *layout = data; struct wlr_output_layout *layout = data;
if (!wlr_output_layout_contains_point(layout, NULL, state->cursor->x, if (!wlr_output_layout_contains_point(layout, NULL, state->cursor.x,
state->cursor->y) && !wl_list_empty(&layout->outputs)) { state->cursor.y) && !wl_list_empty(&layout->outputs)) {
// the output we were on has gone away so go to the closest boundary // the output we were on has gone away so go to the closest boundary
// point (unless the layout is empty; compare warp_closest()) // point (unless the layout is empty; compare warp_closest())
double x, y; double x, y;
wlr_output_layout_closest_point(layout, NULL, state->cursor->x, wlr_output_layout_closest_point(layout, NULL, state->cursor.x,
state->cursor->y, &x, &y); state->cursor.y, &x, &y);
cursor_warp_unchecked(state->cursor, x, y); cursor_warp_unchecked(&state->cursor, x, y);
} }
} }