Compare commits

...

2 Commits

Author SHA1 Message Date
bf595e6c8a 设置 root 窗口鼠标样式 2025-11-21 01:00:51 +08:00
2aed03566c 添加点击切换 tag 功能 2025-11-21 00:39:05 +08:00
7 changed files with 101 additions and 0 deletions

View File

@@ -30,6 +30,7 @@ add_executable(${APP_NAME}
color.c
xcursor.c
xwindow.c
event.c
)
# use C23

68
src/event.c Normal file
View File

@@ -0,0 +1,68 @@
#include "event.h"
#include <glib.h>
#include <stdint.h>
#include <stdio.h>
#include <xcb/xcb.h>
#include <xcb/xcb_event.h>
#include <xcb/xproto.h>
#include "monitor.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;
}
}
}
}
}
static void handle_xcb_event(xcb_generic_event_t *event) {
uint8_t event_type = XCB_EVENT_RESPONSE_TYPE(event);
switch (event_type) {
#define EVENT(type, callback) \
case type: \
callback((void *)event); \
break
EVENT(XCB_BUTTON_PRESS, button_press);
#undef EVENT
}
}
static gboolean xcb_event_loop(GIOChannel *channel, GIOCondition condition,
gpointer user_data) {
if (condition & (G_IO_HUP | G_IO_ERR)) {
wm_quit();
return false;
}
xcb_generic_event_t *event = nullptr;
while ((event = xcb_poll_for_event(wm.xcb_conn))) {
handle_xcb_event(event);
p_delete(&event);
}
return true;
}
void setup_event_loop(void) {
int fd = xcb_get_file_descriptor(wm.xcb_conn);
GIOChannel *channel = g_io_channel_unix_new(fd);
g_io_channel_set_encoding(channel, nullptr, nullptr);
GIOCondition cond = G_IO_IN | G_IO_HUP | G_IO_ERR;
g_io_add_watch(channel, cond, xcb_event_loop, nullptr);
g_io_channel_unref(channel);
}

3
src/event.h Normal file
View File

@@ -0,0 +1,3 @@
#pragma once
void setup_event_loop(void);

View File

@@ -41,6 +41,19 @@ uint32_t monitor_initialize_tag(monitor_t *monitor, const char **tags,
return tag_count;
}
void monitor_select_tag(monitor_t *monitor, uint32_t tag_mask) {
if (monitor->selected_tag->mask == tag_mask) return;
for (tag_t *tag = monitor->tag_list; tag; tag = tag->next) {
if (tag->mask == tag_mask) {
monitor->selected_tag = tag;
monitor_draw_bar(monitor);
xcb_flush(wm.xcb_conn);
break;
}
}
}
static void tag_clean(tag_t *tag) {
tag_t *next_tag = nullptr;
for (tag_t *t = tag; t; t = next_tag) {

View File

@@ -20,11 +20,13 @@
#include "buffer.h"
#include "color.h"
#include "default_config.h"
#include "event.h"
#include "monitor.h"
#include "text.h"
#include "types.h"
#include "utils.h"
#include "xcursor.h"
#include "xwindow.h"
static void wm_setup_signal(void);
static void wm_check_other_wm(void);
@@ -176,6 +178,7 @@ static bool wm_detect_monitor_by_randr(void) {
prev_monitor->next = monitor;
} else {
wm.monitor_list = monitor;
wm.current_monitor = monitor;
}
prev_monitor = monitor;
@@ -224,6 +227,7 @@ static bool wm_detect_monitor_by_xinerama(void) {
prev_monitor->next = monitor;
} else {
wm.monitor_list = monitor;
wm.current_monitor = monitor;
}
prev_monitor = monitor;
@@ -246,6 +250,7 @@ void wm_detect_monitor(void) {
monitor->geometry.height = wm.screen->height_in_pixels;
wm.monitor_list = monitor;
wm.current_monitor = monitor;
}
void wm_scan_clients(void) {}
@@ -280,6 +285,7 @@ static void wm_setup(void) {
monitor_draw_bar(m);
}
xwindow_change_cursor(wm.screen->root, cursor_normal);
xcb_flush(wm.xcb_conn);
}
@@ -333,6 +339,8 @@ int main(int argc, char *argv[]) {
debug_show_monitor_list();
setup_event_loop();
if (wm.loop == nullptr) {
wm.loop = g_main_loop_new(nullptr, FALSE);
g_main_loop_run(wm.loop);

View File

@@ -100,3 +100,8 @@ visual_t *xwindow_get_xcb_visual(bool prefer_alpha) {
return visual;
}
void xwindow_change_cursor(xcb_window_t window, cursor_t cursor) {
xcb_params_cw_t params = {.cursor = xcursor_get_xcb_cursor(cursor)};
xcb_aux_change_window_attributes(wm.xcb_conn, window, XCB_CW_CURSOR, &params);
}

View File

@@ -3,9 +3,12 @@
#include <stdint.h>
#include <xcb/xproto.h>
#include "xcursor.h"
typedef struct visual_t {
uint8_t depth;
xcb_visualtype_t *visual;
} visual_t;
visual_t *xwindow_get_xcb_visual(bool prefer_alpha);
void xwindow_change_cursor(xcb_window_t window, cursor_t cursor);