feat(core+backend/X11): 完成快捷键绑定和触发功能

This commit is contained in:
2026-04-27 01:09:23 +08:00
parent 19e97b730b
commit c93e78bbd6
13 changed files with 142 additions and 17 deletions

View File

@@ -14,14 +14,6 @@
#include "base/memory.h"
#include "core/types.h"
typedef struct key_binding_t {
const char *key_str;
modifier_mask_t modifiers;
xkb_keysym_t keysym;
zdwm_action_fn *fn;
zdwm_action_arg_t *arg;
} key_binding_t;
typedef struct binding_mode_t {
zdwm_binding_mode_id_t id;
const char *name;
@@ -271,3 +263,12 @@ bool binding_table_cycle_mode(binding_table_t *table, int delta) {
return true;
}
const key_binding_t *
binding_table_get_current_bindings(binding_table_t *table, size_t *count) {
auto mode = binding_table_get_mode(table, table->current_mode);
if (!mode || !mode->count) return nullptr;
*count = mode->count;
return mode->items;
}

View File

@@ -1,10 +1,21 @@
#pragma once
#include <stddef.h>
#include <zdwm/action.h>
#include <zdwm/types.h>
#include "core/types.h"
typedef struct binding_table_t binding_table_t;
typedef struct key_binding_t {
const char *key_str;
modifier_mask_t modifiers;
keysym_t keysym;
zdwm_action_fn *fn;
zdwm_action_arg_t *arg;
} key_binding_t;
/**
* @brief 添加新的模式
*
@@ -84,3 +95,18 @@ bool binding_table_set_current_mode(
* @return 无法切换返回 false 否则返回 true
*/
bool binding_table_cycle_mode(binding_table_t *table, int delta);
/**
* @brief 获取当前按键绑定列表
*
* @param table 按键绑定表
* @param count 保存按键个数的指针
*
* @return 返回当前按键列表的头指针,调用方仅只读访问,不持有对应的内存。
*
* @notice
* 1. count 指针不能为 nullptr ,若 count 传 nullptr 视为调用方错误
* 2. 调用方需判断返回的指针是否为 nullptr
*/
const key_binding_t *
binding_table_get_current_bindings(binding_table_t *table, size_t *count);

View File

@@ -49,11 +49,6 @@ typedef struct effect_window_list_t {
size_t count;
} effect_window_list_t;
typedef struct key_bind_t {
modifier_mask_t modifiers;
keysym_t keysym;
} key_bind_t;
typedef struct effect_bind_key_t {
const key_bind_t *keys;
size_t count;

View File

@@ -4,6 +4,7 @@
#include <zdwm/types.h>
#include "base/macros.h"
#include "core/binding.h"
#include "core/command.h"
#include "core/command_buffer.h"
#include "core/event.h"
@@ -14,6 +15,23 @@
#include "core/window.h"
#include "core/wm_desc.h"
static void route_key_press(
binding_table_t *binding_table,
const zdwm_action_ctx_t *action_ctx,
const key_press_event_t *e
) {
size_t count = 0;
auto bindings = binding_table_get_current_bindings(binding_table, &count);
if (!bindings) return;
for (size_t i = 0; i < count; ++i) {
auto binding = &bindings[i];
if (binding->modifiers == e->modifiers && binding->keysym == e->keysym) {
binding->fn(action_ctx, binding->arg);
}
}
}
static workspace_id_t derive_window_workspace(const state_t *state) {
return state->outputs[state->current_output_index].current_workspace_id;
}
@@ -115,6 +133,9 @@ void policy_route_event(
) {
auto state = ctx->state;
switch (event->type) {
case ZDWM_EVENT_KEY_PRESS:
route_key_press(ctx->bind_table, &ctx->action_ctx, &event->as.key_press);
break;
case ZDWM_EVENT_WINDOW_MAP_REQUEST:
route_map_request(state, ctx->rules, &event->as.window_map_request, out);
break;

View File

@@ -1,6 +1,8 @@
#pragma once
#include <zdwm/action.h>
#include "core/binding.h"
#include "core/command_buffer.h"
#include "core/event.h"
#include "core/plan.h"
@@ -8,6 +10,7 @@
#include "core/state.h"
typedef struct policy_context_t {
binding_table_t *bind_table;
state_t *state;
const rules_t *rules;
const zdwm_action_ctx_t action_ctx;

View File

@@ -1,6 +1,7 @@
#include "core/runtime.h"
#include <dlfcn.h>
#include <stddef.h>
#include "action.h"
#include "base/memory.h"
@@ -13,6 +14,7 @@
#include "core/policy.h"
#include "core/rules.h"
#include "core/state.h"
#include "core/types.h"
#include "core/wm_desc.h"
static bool runtime_workspace_desc_has_valid_layouts(
@@ -112,6 +114,32 @@ void runtime_shutdown(runtime_t *runtime) {
runtime->config_module_handle = nullptr;
}
void runtime_setup(runtime_t *runtime) {
size_t bind_count = 0;
auto bindings =
binding_table_get_current_bindings(runtime->binding_table, &bind_count);
if (!bindings) return;
auto backend = runtime->backend;
auto plan = &runtime->plan;
plan_reset(plan);
auto keys = p_new(key_bind_t, bind_count);
for (size_t i = 0; i < bind_count; ++i) {
keys[i].keysym = bindings[i].keysym;
keys[i].modifiers = bindings[i].modifiers;
}
effect_t effect_bind_key = {
.type = ZDWM_EFFECT_BIND_KEY,
.as.bind_key = {.count = bind_count, .keys = keys},
};
plan_push_effect(plan, &effect_bind_key);
backend_apply_effect(backend, plan->effects, plan->count);
plan_reset(plan);
}
void runtime_run(runtime_t *runtime) {
runtime->running = true;
@@ -120,6 +148,7 @@ void runtime_run(runtime_t *runtime) {
plan_t *plan = &runtime->plan;
policy_context_t ctx = {
.bind_table = runtime->binding_table,
.state = &runtime->state,
.rules = &runtime->rules,
.action_ctx = {

View File

@@ -40,4 +40,5 @@ typedef struct runtime_t {
bool runtime_init(runtime_t *runtime, runtime_init_desc_t *desc);
void runtime_init_desc_cleanup(runtime_init_desc_t *desc);
void runtime_shutdown(runtime_t *runtime);
void runtime_setup(runtime_t *runtime);
void runtime_run(runtime_t *runtime);

View File

@@ -62,3 +62,8 @@ typedef enum modifier_bit_t {
ZDWM_MOD_4 = 1u << 5,
ZDWM_MOD_5 = 1u << 6,
} modifier_bit_t;
typedef struct key_bind_t {
modifier_mask_t modifiers;
keysym_t keysym;
} key_bind_t;