diff --git a/src/core/command.h b/src/core/command.h new file mode 100644 index 0000000..a9c25b2 --- /dev/null +++ b/src/core/command.h @@ -0,0 +1,38 @@ +#pragma once + +#include "core/types.h" +#include "core/wm_desc.h" + +typedef enum command_type_t { + ZDWM_COMMAND_MANAGE_WINDOW, + ZDWM_COMMAND_SWITCH_WORKSPACE +} command_type_t; + +typedef struct manage_window_command_t { + workspace_id_t workspace; + bool floating; + bool switch_to_workspace; + window_info_t info; +} manage_window_command_t; + +typedef struct switch_workspace_command_t { + output_id_t output; + workspace_id_t workspace; +} switch_workspace_command_t; + +/** + * @brief 非 owning 的命令值对象 + * @details + * command_t 自身不拥有任何堆内存;其当前所有 payload 中出现的指针字段均为借用 + * 语义。 + * + * 因此 command_t 支持按值浅拷贝,且不需要单独的 cleanup 接口。若接收方需要在 + * 源数据生命周期之外继续持有相关内容,必须自行复制。 + */ +typedef struct command_t { + command_type_t type; + union { + manage_window_command_t manage_window; + switch_workspace_command_t switch_workspace; + } as; +} command_t; diff --git a/src/core/command_buffer.c b/src/core/command_buffer.c new file mode 100644 index 0000000..77eee13 --- /dev/null +++ b/src/core/command_buffer.c @@ -0,0 +1,23 @@ +#include "core/command_buffer.h" + +#include "base/array.h" +#include "base/memory.h" +#include "core/command.h" + +void command_buffer_reset(command_buffer_t *buffer) { + if (!buffer->items || !buffer->capacity) return; + + p_clear(buffer->items, buffer->capacity); + buffer->count = 0; +} + +void command_buffer_cleanup(command_buffer_t *buffer) { + p_delete(&buffer->items); + buffer->count = 0; + buffer->capacity = 0; +} + +void command_buffer_push(command_buffer_t *buffer, const command_t *command) { + command_t *cmd = array_push(buffer->items, buffer->count, buffer->capacity); + *cmd = *command; +} diff --git a/src/core/command_buffer.h b/src/core/command_buffer.h new file mode 100644 index 0000000..6a834db --- /dev/null +++ b/src/core/command_buffer.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +#include "core/command.h" + +typedef struct command_buffer_t { + command_t *items; + size_t count; + size_t capacity; +} command_buffer_t; + +void command_buffer_reset(command_buffer_t *buffer); +void command_buffer_cleanup(command_buffer_t *buffer); +void command_buffer_push(command_buffer_t *buffer, const command_t *command); diff --git a/src/core/runtime.h b/src/core/runtime.h index bce3f0d..d46ae2a 100644 --- a/src/core/runtime.h +++ b/src/core/runtime.h @@ -3,6 +3,7 @@ #include #include "core/backend.h" +#include "core/command_buffer.h" #include "core/layout.h" #include "core/rules.h" #include "core/state.h" @@ -23,6 +24,7 @@ typedef struct runtime_t { bool running; bool will_restart; + command_buffer_t command_buffer; state_t state; layout_registry_t layouts; rules_t rules;