From 38d2a28438ba34129190123a80a80aa3735b3b90 Mon Sep 17 00:00:00 2001 From: Zedhugh Chen Date: Sat, 18 Apr 2026 22:24:03 +0800 Subject: [PATCH] =?UTF-8?q?refactor(core/policy):=20=E4=BB=8E=20policy=5Fc?= =?UTF-8?q?ontext=5Ft=20=E4=B8=AD=E6=8F=90=E5=8F=96=20event=20=E4=B8=BA?= =?UTF-8?q?=E7=8B=AC=E7=AB=8B=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit policy_context_t 应该只包含所有 policy 相关接口都需要用到的参数,而 event 只是 policy_route_event 接口需要的参数,故应该将其从 policy_context_t 中剔除 --- src/core/policy.c | 10 +++++++--- src/core/policy.h | 17 +++++++++++++++-- src/core/runtime.c | 13 ++++++------- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/core/policy.c b/src/core/policy.c index e78f9ca..d07af66 100644 --- a/src/core/policy.c +++ b/src/core/policy.c @@ -93,13 +93,17 @@ static bool route_map_request( return true; } -bool policy_route_event(const policy_context_t *ctx, command_buffer_t *out) { - switch (ctx->event->type) { +bool policy_route_event( + const policy_context_t *ctx, + const event_t *event, + command_buffer_t *out +) { + switch (event->type) { case ZDWM_EVENT_WINDOW_MAP_REQUEST: return route_map_request( ctx->state, ctx->rules, - &ctx->event->as.window_map_request, + &event->as.window_map_request, out ); default: diff --git a/src/core/policy.h b/src/core/policy.h index 6d8b33a..6eef655 100644 --- a/src/core/policy.h +++ b/src/core/policy.h @@ -9,10 +9,23 @@ typedef struct policy_context_t { const state_t *state; const rules_t *rules; - const event_t *event; } policy_context_t; -bool policy_route_event(const policy_context_t *ctx, command_buffer_t *out); +/** + * @brief 事件路由:将运行时事件翻译为语义命令 + * + * @param ctx 策略上下文 + * @param event 需要翻译的事件 + * @param out 命令输出缓冲区,路由产生的命令追加到此处 + * + * @return true 产生了至少一条需要执行的命令 + * @return false 产生了至少一条需要执行的命令 + */ +bool policy_route_event( + const policy_context_t *ctx, + const event_t *event, + command_buffer_t *out +); bool policy_apply_command( const policy_context_t *ctx, const command_buffer_t *command_buffer, diff --git a/src/core/runtime.c b/src/core/runtime.c index 7d7bf32..e669e93 100644 --- a/src/core/runtime.c +++ b/src/core/runtime.c @@ -110,6 +110,11 @@ void runtime_run(runtime_t *runtime) { command_buffer_t *command_buffer = &runtime->command_buffer; plan_t *plan = &runtime->plan; + policy_context_t ctx = { + .state = &runtime->state, + .rules = &runtime->rules, + }; + while (runtime->running) { event_t event = {0}; if (!backend_next_event(backend, &event)) break; @@ -117,13 +122,7 @@ void runtime_run(runtime_t *runtime) { command_buffer_reset(command_buffer); plan_reset(plan); - policy_context_t ctx = { - .state = &runtime->state, - .rules = &runtime->rules, - .event = &event, - }; - - policy_route_event(&ctx, command_buffer); + policy_route_event(&ctx, &event, command_buffer); policy_apply_command(&ctx, command_buffer, plan); if (plan->count) backend_apply_effect(backend, plan->effects, plan->count);