From ec93e6c023df940a30bc3536f100a23534ea19a0 Mon Sep 17 00:00:00 2001 From: Zedhugh Chen Date: Fri, 21 Nov 2025 17:45:55 +0800 Subject: [PATCH] =?UTF-8?q?=E7=82=B9=E5=87=BB=E5=88=87=E6=8D=A2=20tag=20?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=81=9A=E6=88=90=20button=20=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CMakeLists.txt | 1 + src/action.c | 8 ++++++++ src/action.h | 42 ++++++++++++++++++++++++++++++++++++++++++ src/default_config.h | 10 +++++++++- src/event.c | 40 +++++++++++++++++++++++++++++++--------- 5 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 src/action.c create mode 100644 src/action.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d04d8c9..d95ba2a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -31,6 +31,7 @@ add_executable(${APP_NAME} xcursor.c xwindow.c event.c + action.c ) # use C23 diff --git a/src/action.c b/src/action.c new file mode 100644 index 0000000..fbf12ee --- /dev/null +++ b/src/action.c @@ -0,0 +1,8 @@ +#include "action.h" + +#include "monitor.h" +#include "wm.h" + +void select_tag_of_current_monitor(const user_action_arg_t *arg) { + monitor_select_tag(wm.current_monitor, arg->ui); +} diff --git a/src/action.h b/src/action.h new file mode 100644 index 0000000..8659b79 --- /dev/null +++ b/src/action.h @@ -0,0 +1,42 @@ +#pragma once + +#include +#include + +typedef union user_action_arg_t { + bool b; + int32_t i; + uint32_t ui; + void *ptr; +} user_action_arg_t; + +typedef enum click_area_t { + click_none, + click_tag, +} click_area_t; + +typedef enum modifier_t { + modifier_none = 0, + modifier_shift = XCB_MOD_MASK_SHIFT, + modifier_control = XCB_MOD_MASK_CONTROL, + modifier_alt = XCB_MOD_MASK_1, + modifier_super = XCB_MOD_MASK_4, + modifier_any = XCB_MOD_MASK_ANY, +} modifier_t; + +typedef enum button_index_t { + button_any = XCB_BUTTON_INDEX_ANY, + button_left = XCB_BUTTON_INDEX_1, + button_right = XCB_BUTTON_INDEX_2, + button_middle = XCB_BUTTON_INDEX_3, +} button_index_t; + +typedef struct button_t { + click_area_t click_area; + modifier_t modifiers; + button_index_t button; + void (*func)(const user_action_arg_t *arg); + const user_action_arg_t arg; +} button_t; + +void select_tag_of_current_monitor(const user_action_arg_t *arg); diff --git a/src/default_config.h b/src/default_config.h index 24d446f..dcf6410 100644 --- a/src/default_config.h +++ b/src/default_config.h @@ -1,6 +1,10 @@ +#pragma once + #include -static const char *font_family = "monospace"; +#include "action.h" + +static const char *const font_family = "monospace"; static const int font_size = 10; static const int default_dpi = 144; @@ -15,3 +19,7 @@ static const char *const tag_bg = "#222222"; static const char *const active_tag_bg = "#005577"; static const char *const tag_color = "#bbbbbb"; static const char *const active_tag_color = "#eeeeee"; + +static const button_t button_list[] = { + {click_tag, modifier_none, button_left, select_tag_of_current_monitor, {0}}, +}; diff --git a/src/event.c b/src/event.c index 149db30..57d83a7 100644 --- a/src/event.c +++ b/src/event.c @@ -7,26 +7,47 @@ #include #include -#include "monitor.h" +#include "action.h" +#include "default_config.h" #include "types.h" #include "utils.h" #include "wm.h" static void button_press(xcb_button_press_event_t *ev) { - if (ev->event == wm.current_monitor->bar_window) { - if (ev->event_x >= wm.current_monitor->tag_extent.start && - ev->event_x <= wm.current_monitor->tag_extent.end) { - for (tag_t *tag = wm.current_monitor->tag_list; tag; tag = tag->next) { - if (ev->event_x >= tag->bar_extent.start && - ev->event_x <= tag->bar_extent.end) { - monitor_select_tag(wm.current_monitor, tag->mask); - break; + click_area_t click_area = click_none; + user_action_arg_t arg = {0}; + + for (monitor_t *m = wm.monitor_list; m; m = m->next) { + if (ev->event == m->bar_window) { + if (wm.current_monitor != m) wm.current_monitor = m; + + if (ev->event_x >= m->tag_extent.start && + ev->event_x <= m->tag_extent.end) { + click_area = click_tag; + + for (tag_t *tag = m->tag_list; tag; tag = tag->next) { + if (ev->event_x >= tag->bar_extent.start && + ev->event_x <= tag->bar_extent.end) { + arg.ui = tag->mask; + break; + } } } + break; + } + } + + for (int i = 0; i < countof(button_list); i++) { + if (click_area == button_list[i].click_area && + button_list[i].button == ev->detail && + button_list[i].modifiers == ev->state) { + button_list[i].func(&arg); } } } +static void key_press(xcb_key_press_event_t *ev) { printf("key press\n"); } + static void handle_xcb_event(xcb_generic_event_t *event) { uint8_t event_type = XCB_EVENT_RESPONSE_TYPE(event); @@ -37,6 +58,7 @@ static void handle_xcb_event(xcb_generic_event_t *event) { break EVENT(XCB_BUTTON_PRESS, button_press); + EVENT(XCB_KEY_PRESS, key_press); #undef EVENT }