添加快捷键设置

This commit is contained in:
2025-08-19 21:37:57 +08:00
parent bb61bfcd2e
commit 2a7ab324be
8 changed files with 113 additions and 5 deletions

View File

@@ -8,8 +8,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wordexp.h>
#include "action.h"
#include "config.h"
#include "event-adapter.h"
#include "renderer.h"
@@ -30,6 +32,8 @@ static void init_monitor_bar(void);
static void get_xresource_config(void);
static void update_bars(void);
static void init_xcb_event_loop(void);
static void xcb_event_handler(generic_event_t *event, void *user_data);
static void key_press(key_press_event_t *event);
static gboolean handle_xcb_event(GIOChannel *channel, GIOCondition condition,
gpointer userdata);
static void cleanup(void);
@@ -47,6 +51,16 @@ static uint32_t bar_y_pad = bar_y_padding;
static uint32_t tag_x_pad = tag_x_padding;
static uint32_t bar_height = 0;
static bool need_restart = false;
void spawn(const user_action_arg_t *arg) {
g_spawn_command_line_async(arg->ptr, NULL);
}
void quit(const user_action_arg_t *arg) {
need_restart = arg->b;
g_main_loop_quit(loop);
}
/* 调试函数,开发完成后删除 */
static void print_monitor_list_info(monitor_t *monitor_list) {
printf("========================= monitor info =========================\n");
@@ -359,6 +373,7 @@ void update_bars(void) {
void init_xcb_event_loop(void) {
adapter = create_event_adapter();
event_adapter_set_handler(adapter, xcb_event_handler, NULL);
GIOChannel *channel = g_io_channel_unix_new(get_xcb_connection_fd());
g_io_channel_set_encoding(channel, NULL, NULL);
GIOCondition cond = G_IO_IN | G_IO_HUP | G_IO_ERR;
@@ -366,6 +381,27 @@ void init_xcb_event_loop(void) {
g_io_channel_unref(channel);
}
void xcb_event_handler(generic_event_t *event, void *user_data) {
switch (event->type) {
case EVENT_TYPE_KEY_PRESS:
key_press((key_press_event_t *)event);
break;
default:
break;
}
}
void key_press(key_press_event_t *event) {
const keybinding_t *key = NULL;
for (int i = 0; i < LENGTH(keys); i++) {
key = &keys[i];
if (key->key_symbol == event->key_symbol &&
key->modifiers == event->modifiers && key->func) {
key->func(&key->arg);
}
}
}
gboolean handle_xcb_event(GIOChannel *channel, GIOCondition condition,
gpointer userdata) {
if (condition & (G_IO_HUP | G_IO_ERR)) {
@@ -434,6 +470,8 @@ int main(int argc, char *argv[]) {
free_window_list(window_list);
grab_keybindings(keys, LENGTH(keys));
init_xcb_event_loop();
loop = g_main_loop_new(NULL, FALSE);
@@ -441,5 +479,10 @@ int main(int argc, char *argv[]) {
cleanup();
if (need_restart) {
need_restart = false;
execvp(argv[0], argv);
}
return 0;
}