wlr-foreign-toplevel: convert to stack backed states array

This commit is contained in:
Consolatis 2024-12-20 21:36:25 +01:00 committed by Kirill Primak
parent 648c64b7e4
commit bd3724aa26

View file

@ -1,4 +1,3 @@
#include <assert.h>
#include <string.h>
#include <stdlib.h>
@ -10,6 +9,8 @@
#define FOREIGN_TOPLEVEL_MANAGEMENT_V1_VERSION 3
#define FOREIGN_TOPLEVEL_HANDLE_V1_STATE_COUNT 32
static const struct zwlr_foreign_toplevel_handle_v1_interface toplevel_handle_impl;
static struct wlr_foreign_toplevel_handle_v1 *toplevel_handle_from_resource(
@ -338,60 +339,38 @@ void wlr_foreign_toplevel_handle_v1_output_leave(
}
}
static bool fill_array_from_toplevel_state(struct wl_array *array,
uint32_t state) {
static void fill_array_from_toplevel_state(struct wl_array *states,
uint32_t data[], uint32_t state) {
size_t nstates = 0;
if (state & WLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_MAXIMIZED) {
uint32_t *index = wl_array_add(array, sizeof(uint32_t));
if (index == NULL) {
return false;
}
*index = ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_MAXIMIZED;
data[nstates++] = ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_MAXIMIZED;
}
if (state & WLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_MINIMIZED) {
uint32_t *index = wl_array_add(array, sizeof(uint32_t));
if (index == NULL) {
return false;
}
*index = ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_MINIMIZED;
data[nstates++] = ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_MINIMIZED;
}
if (state & WLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_ACTIVATED) {
uint32_t *index = wl_array_add(array, sizeof(uint32_t));
if (index == NULL) {
return false;
}
*index = ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_ACTIVATED;
data[nstates++] = ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_ACTIVATED;
}
if (state & WLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_FULLSCREEN) {
uint32_t *index = wl_array_add(array, sizeof(uint32_t));
if (index == NULL) {
return false;
}
*index = ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_FULLSCREEN;
data[nstates++] = ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_FULLSCREEN;
}
assert(nstates <= FOREIGN_TOPLEVEL_HANDLE_V1_STATE_COUNT);
return true;
*states = (struct wl_array){
.data = data,
.size = nstates * sizeof(data[0])
};
}
static void toplevel_send_state(struct wlr_foreign_toplevel_handle_v1 *toplevel) {
struct wl_array states;
wl_array_init(&states);
bool r = fill_array_from_toplevel_state(&states, toplevel->state);
if (!r) {
struct wl_resource *resource;
wl_resource_for_each(resource, &toplevel->resources) {
wl_resource_post_no_memory(resource);
}
wl_array_release(&states);
return;
}
struct wl_array state_array;
uint32_t states[FOREIGN_TOPLEVEL_HANDLE_V1_STATE_COUNT];
struct wl_resource *resource;
wl_resource_for_each(resource, &toplevel->resources) {
zwlr_foreign_toplevel_handle_v1_send_state(resource, &states);
fill_array_from_toplevel_state(&state_array, states, toplevel->state);
zwlr_foreign_toplevel_handle_v1_send_state(resource, &state_array);
}
wl_array_release(&states);
toplevel_update_idle_source(toplevel);
}
@ -600,17 +579,10 @@ static void toplevel_send_details_to_toplevel_resource(
send_output_to_resource(resource, output->output, true);
}
struct wl_array states;
wl_array_init(&states);
bool r = fill_array_from_toplevel_state(&states, toplevel->state);
if (!r) {
wl_resource_post_no_memory(resource);
wl_array_release(&states);
return;
}
zwlr_foreign_toplevel_handle_v1_send_state(resource, &states);
wl_array_release(&states);
struct wl_array state_array;
uint32_t states[FOREIGN_TOPLEVEL_HANDLE_V1_STATE_COUNT];
fill_array_from_toplevel_state(&state_array, states, toplevel->state);
zwlr_foreign_toplevel_handle_v1_send_state(resource, &state_array);
toplevel_resource_send_parent(resource, toplevel->parent);