feat: 添加按键绑定功能
This commit is contained in:
23
src/core/action.c
Normal file
23
src/core/action.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "core/action.h"
|
||||
|
||||
#include <paths.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "base/log.h"
|
||||
|
||||
void spawn(const char *command) {
|
||||
if (fork() == 0) {
|
||||
setsid();
|
||||
|
||||
if (fork() == 0) {
|
||||
execl(_PATH_BSHELL, _PATH_BSHELL, "-c", command, nullptr);
|
||||
fatal("execl fail: %s", command);
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
wait(0);
|
||||
}
|
||||
3
src/core/action.h
Normal file
3
src/core/action.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
void spawn(const char *command);
|
||||
194
src/core/binding.c
Normal file
194
src/core/binding.c
Normal file
@@ -0,0 +1,194 @@
|
||||
#include "core/binding.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <xkbcommon/xkbcommon-keysyms.h>
|
||||
#include <xkbcommon/xkbcommon.h>
|
||||
#include <zdwm/action.h>
|
||||
#include <zdwm/types.h>
|
||||
|
||||
#include "base/array.h"
|
||||
#include "base/memory.h"
|
||||
#include "core/event.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;
|
||||
key_binding_t *items;
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
} binding_mode_t;
|
||||
|
||||
typedef struct binding_table_t {
|
||||
binding_mode_t *modes;
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
zdwm_binding_mode_id_t default_mode;
|
||||
zdwm_binding_mode_id_t current_mode;
|
||||
} binding_table_t;
|
||||
|
||||
zdwm_binding_mode_id_t
|
||||
binding_table_add_mode(binding_table_t *table, const char *mode_name) {
|
||||
if (!table || !mode_name || strlen(mode_name) == 0) {
|
||||
return ZDWM_BINDING_MODE_ID_INVALID;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < table->count; ++i) {
|
||||
if (strcmp(table->modes[i].name, mode_name) == 0) {
|
||||
return ZDWM_BINDING_MODE_ID_INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
zdwm_binding_mode_id_t id = (zdwm_binding_mode_id_t)table->count;
|
||||
binding_mode_t *new_mode =
|
||||
array_push(table->modes, table->count, table->capacity);
|
||||
new_mode->name = mode_name;
|
||||
new_mode->id = id;
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
static binding_mode_t *
|
||||
binding_table_get_mode(binding_table_t *table, zdwm_binding_mode_id_t mode_id) {
|
||||
if (mode_id >= table->count) return nullptr;
|
||||
|
||||
binding_mode_t *mode = &table->modes[mode_id];
|
||||
if (mode->id == mode_id) return mode;
|
||||
|
||||
for (size_t i = 0; i < table->count; ++i) {
|
||||
mode = &table->modes[i];
|
||||
if (mode->id == mode_id) return mode;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 解析按键序列
|
||||
*
|
||||
* @details 按键序列不合法解析失败,解析成功会设置 modifiers 与 keysym 对应的值
|
||||
*
|
||||
* @param key_sequence 按键序列
|
||||
* @param modifiers 返回修饰键的指针
|
||||
* @param keysym 返回按键符号的指针
|
||||
*
|
||||
* @return 解析成功返回 true 否则返回 false
|
||||
*/
|
||||
bool parse_key_sequence(
|
||||
const char *key_sequence,
|
||||
modifier_mask_t *modifiers,
|
||||
xkb_keysym_t *keysym
|
||||
) {
|
||||
/* TODO: */
|
||||
return false;
|
||||
}
|
||||
|
||||
bool binding_table_add_bind(
|
||||
binding_table_t *table,
|
||||
zdwm_binding_mode_id_t mode_id,
|
||||
const char *key_sequence,
|
||||
zdwm_action_fn fn,
|
||||
zdwm_action_arg_t *arg
|
||||
) {
|
||||
binding_mode_t *mode = binding_table_get_mode(table, mode_id);
|
||||
if (!mode) return false;
|
||||
|
||||
modifier_mask_t modifiers = 0;
|
||||
xkb_keysym_t keysym = XKB_KEY_NoSymbol;
|
||||
if (!parse_key_sequence(key_sequence, &modifiers, &keysym)) return false;
|
||||
|
||||
key_binding_t *item = array_push(mode->items, mode->count, mode->capacity);
|
||||
item->key_str = key_sequence;
|
||||
item->modifiers = modifiers;
|
||||
item->keysym = keysym;
|
||||
item->fn = fn;
|
||||
item->arg = arg;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void binding_table_destroy(binding_table_t *table) {
|
||||
if (!table) return;
|
||||
|
||||
for (size_t i = 0; i < table->count; ++i) {
|
||||
binding_mode_t *mode = &table->modes[i];
|
||||
p_delete(&mode->items);
|
||||
mode->count = 0;
|
||||
mode->capacity = 0;
|
||||
}
|
||||
|
||||
p_delete(&table->modes);
|
||||
table->count = 0;
|
||||
table->capacity = 0;
|
||||
table->default_mode = ZDWM_BINDING_MODE_ID_INVALID;
|
||||
|
||||
p_delete(&table);
|
||||
}
|
||||
|
||||
binding_table_t *binding_table_create(void) {
|
||||
binding_table_t *table = p_new(binding_table_t, 1);
|
||||
|
||||
/* 默认情况下,默认模式和当前模式都是第一个添加的模式 */
|
||||
table->default_mode = 0;
|
||||
table->current_mode = 0;
|
||||
|
||||
return table;
|
||||
}
|
||||
|
||||
bool binding_table_set_default_mode(
|
||||
binding_table_t *table,
|
||||
zdwm_binding_mode_id_t mode_id
|
||||
) {
|
||||
if (mode_id >= table->count) return false;
|
||||
|
||||
table->default_mode = mode_id;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool binding_table_set_current_mode(
|
||||
binding_table_t *table,
|
||||
zdwm_binding_mode_id_t mode_id
|
||||
) {
|
||||
if (mode_id >= table->count) return false;
|
||||
|
||||
table->current_mode = mode_id;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool binding_table_cycle_mode(binding_table_t *table, int delta) {
|
||||
if (!table || !table->modes || table->count < 1) return false;
|
||||
|
||||
bool matched = false;
|
||||
size_t index = (size_t)table->current_mode;
|
||||
if (table->modes[index].id == table->current_mode) {
|
||||
matched = true;
|
||||
} else {
|
||||
for (size_t i = 0; i < table->count; ++i) {
|
||||
if (table->modes[i].id == table->current_mode) {
|
||||
index = i;
|
||||
matched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!matched) return false;
|
||||
|
||||
/* 只有一个模式没必要切换 */
|
||||
if (table->count == 1) return true;
|
||||
|
||||
int64_t count = (int64_t)table->count;
|
||||
int64_t next_index = ((int64_t)index + (int64_t)delta) % count;
|
||||
if (next_index < 0) next_index += count;
|
||||
table->current_mode = table->modes[next_index].id;
|
||||
|
||||
return true;
|
||||
}
|
||||
86
src/core/binding.h
Normal file
86
src/core/binding.h
Normal file
@@ -0,0 +1,86 @@
|
||||
#pragma once
|
||||
|
||||
#include <zdwm/action.h>
|
||||
#include <zdwm/types.h>
|
||||
|
||||
typedef struct binding_table_t binding_table_t;
|
||||
|
||||
/**
|
||||
* @brief 添加新的模式
|
||||
*
|
||||
* @details 按键模式名不能重复,如果添加重复的名字仅第一次成功,后续将添加失败
|
||||
*
|
||||
* @param table 按键绑定表
|
||||
* @param mode_name 按键模式名字,不能为 nullptr 或空字符串
|
||||
*
|
||||
* @return 添加成功返回新增模式的 id 否则返回 ZDWM_BINDING_MODE_ID_INVALID
|
||||
*/
|
||||
zdwm_binding_mode_id_t
|
||||
binding_table_add_mode(binding_table_t *table, const char *mode_name);
|
||||
|
||||
/**
|
||||
* @brief 添加按键绑定
|
||||
*
|
||||
* @details mode_id 对应的模式不存在或 key_sequence 不合法,都会添加失败
|
||||
*
|
||||
* @param table 按键绑定表
|
||||
* @param mode_id 模式 ID
|
||||
* @param key_sequence 按键序列的字符串表达
|
||||
* @param fn 用户行为函数
|
||||
* @param arg 用户行为函数所需的参数
|
||||
*
|
||||
* @return 添加成功返回 true 否则返回 false
|
||||
*/
|
||||
bool binding_table_add_bind(
|
||||
binding_table_t *table,
|
||||
zdwm_binding_mode_id_t mode_id,
|
||||
const char *key_sequence,
|
||||
zdwm_action_fn fn,
|
||||
zdwm_action_arg_t *arg
|
||||
);
|
||||
|
||||
/**
|
||||
* @brief 销毁按键绑定表
|
||||
*/
|
||||
void binding_table_destroy(binding_table_t *table);
|
||||
|
||||
/**
|
||||
* @brief 创建按键绑定表
|
||||
*/
|
||||
binding_table_t *binding_table_create(void);
|
||||
|
||||
/**
|
||||
* @brief 通过模式 ID 设置默认模式
|
||||
*
|
||||
* @param table 按键绑定表
|
||||
* @param mode_id 按键模式 ID
|
||||
*
|
||||
* @return 切换成功返回 true 否则返回 false
|
||||
*/
|
||||
bool binding_table_set_default_mode(
|
||||
binding_table_t *table,
|
||||
zdwm_binding_mode_id_t mode_id
|
||||
);
|
||||
|
||||
/**
|
||||
* @brief 通过模式 ID 设置当前模式
|
||||
*
|
||||
* @param table 按键绑定表
|
||||
* @param mode_id 按键模式 ID
|
||||
*
|
||||
* @return 切换成功返回 true 否则返回 false
|
||||
*/
|
||||
bool binding_table_set_current_mode(
|
||||
binding_table_t *table,
|
||||
zdwm_binding_mode_id_t mode_id
|
||||
);
|
||||
|
||||
/**
|
||||
* @brief 切换按键绑定模式
|
||||
*
|
||||
* @param table 按键绑定表
|
||||
* @param delta 切换变化量
|
||||
*
|
||||
* @return 无法切换返回 false 否则返回 true
|
||||
*/
|
||||
bool binding_table_cycle_mode(binding_table_t *table, int delta);
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <zdwm/action.h>
|
||||
#include "core/command_buffer.h"
|
||||
#include "core/event.h"
|
||||
#include "core/plan.h"
|
||||
@@ -9,6 +10,7 @@
|
||||
typedef struct policy_context_t {
|
||||
state_t *state;
|
||||
const rules_t *rules;
|
||||
const zdwm_action_ctx_t action_ctx;
|
||||
} policy_context_t;
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
#include <dlfcn.h>
|
||||
|
||||
#include "action.h"
|
||||
#include "base/memory.h"
|
||||
#include "core/backend.h"
|
||||
#include "core/binding.h"
|
||||
#include "core/command_buffer.h"
|
||||
#include "core/event.h"
|
||||
#include "core/layout.h"
|
||||
@@ -52,8 +54,10 @@ bool runtime_init(runtime_t *runtime, runtime_init_desc_t *desc) {
|
||||
layout_registry_move(&desc->layouts, &runtime->layouts);
|
||||
rules_move(&desc->rules, &runtime->rules);
|
||||
runtime->config_module_handle = desc->config_module_handle;
|
||||
runtime->binding_table = desc->binding_table;
|
||||
desc->backend = nullptr;
|
||||
desc->config_module_handle = nullptr;
|
||||
desc->binding_table = nullptr;
|
||||
|
||||
state_init(
|
||||
&runtime->state,
|
||||
@@ -73,6 +77,9 @@ bool runtime_init(runtime_t *runtime, runtime_init_desc_t *desc) {
|
||||
void runtime_init_desc_cleanup(runtime_init_desc_t *desc) {
|
||||
if (!desc) return;
|
||||
|
||||
binding_table_destroy(desc->binding_table);
|
||||
desc->binding_table = nullptr;
|
||||
|
||||
if (desc->backend) {
|
||||
backend_destroy(desc->backend);
|
||||
desc->backend = nullptr;
|
||||
@@ -97,6 +104,8 @@ void runtime_shutdown(runtime_t *runtime) {
|
||||
layout_registry_cleanup(&runtime->layouts);
|
||||
rules_cleanup(&runtime->rules);
|
||||
state_cleanup(&runtime->state);
|
||||
binding_table_destroy(runtime->binding_table);
|
||||
runtime->binding_table = nullptr;
|
||||
backend_destroy(runtime->backend);
|
||||
runtime->backend = nullptr;
|
||||
if (runtime->config_module_handle) dlclose(runtime->config_module_handle);
|
||||
@@ -111,8 +120,11 @@ void runtime_run(runtime_t *runtime) {
|
||||
plan_t *plan = &runtime->plan;
|
||||
|
||||
policy_context_t ctx = {
|
||||
.state = &runtime->state,
|
||||
.rules = &runtime->rules,
|
||||
.state = &runtime->state,
|
||||
.rules = &runtime->rules,
|
||||
.action_ctx = {
|
||||
.spawn = spawn,
|
||||
},
|
||||
};
|
||||
|
||||
while (runtime->running) {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <stddef.h>
|
||||
|
||||
#include "core/backend.h"
|
||||
#include "core/binding.h"
|
||||
#include "core/command_buffer.h"
|
||||
#include "core/layout.h"
|
||||
#include "core/plan.h"
|
||||
@@ -19,6 +20,7 @@ typedef struct runtime_init_desc_t {
|
||||
workspace_desc_t *workspaces;
|
||||
size_t workspace_count;
|
||||
void *config_module_handle;
|
||||
binding_table_t *binding_table;
|
||||
} runtime_init_desc_t;
|
||||
|
||||
typedef struct runtime_t {
|
||||
@@ -32,6 +34,7 @@ typedef struct runtime_t {
|
||||
rules_t rules;
|
||||
backend_t *backend;
|
||||
void *config_module_handle;
|
||||
binding_table_t *binding_table;
|
||||
} runtime_t;
|
||||
|
||||
bool runtime_init(runtime_t *runtime, runtime_init_desc_t *desc);
|
||||
|
||||
Reference in New Issue
Block a user