添加点击切换 tag 功能
This commit is contained in:
@@ -30,6 +30,7 @@ add_executable(${APP_NAME}
|
||||
color.c
|
||||
xcursor.c
|
||||
xwindow.c
|
||||
event.c
|
||||
)
|
||||
|
||||
# use C23
|
||||
|
||||
68
src/event.c
Normal file
68
src/event.c
Normal 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
3
src/event.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
void setup_event_loop(void);
|
||||
@@ -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) {
|
||||
|
||||
6
src/wm.c
6
src/wm.c
@@ -20,6 +20,7 @@
|
||||
#include "buffer.h"
|
||||
#include "color.h"
|
||||
#include "default_config.h"
|
||||
#include "event.h"
|
||||
#include "monitor.h"
|
||||
#include "text.h"
|
||||
#include "types.h"
|
||||
@@ -176,6 +177,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 +226,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 +249,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) {}
|
||||
@@ -333,6 +337,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);
|
||||
|
||||
Reference in New Issue
Block a user