事件循环框架

This commit is contained in:
2026-04-01 16:08:01 +08:00
parent 90a25fb120
commit 196dab9bb3
11 changed files with 119 additions and 9 deletions

View File

@@ -15,13 +15,7 @@
#include "base/log.h"
#include "base/memory.h"
#include "core/types.h"
struct backend_t {
xcb_connection_t *conn;
xcb_screen_t *screen;
int screenp;
bool have_xfixes;
};
#include "internal.h" /* IWYU pragma: keep */
backend_t *backend_create(const char *display_name) {
int screen_num = 0;

74
src/backend/x11/event.c Normal file
View File

@@ -0,0 +1,74 @@
#include <stdio.h>
#include <xcb/xcb.h>
#include <xcb/xcb_event.h>
#include <xcb/xproto.h>
#include "base/memory.h"
#include "core/backend.h"
#include "internal.h" /* IWYU pragma: keep */
static bool handle_map_request(event_t *event,
const xcb_map_request_event_t *xcb_event) {
p_clear(event, 1);
event->type = ZDWM_EVENT_WINDOW_MAP_REQUEST;
/* TODO: */
return true;
}
static bool handle_unmap_notify(event_t *event,
const xcb_unmap_notify_event_t *xcb_event) {
p_clear(event, 1);
event->type = ZDWM_EVENT_WINDOW_REMOVE;
/* TODO: */
return true;
}
static bool handle_destroy_notify(event_t *event,
const xcb_destroy_notify_event_t *xcb_event) {
p_clear(event, 1);
event->type = ZDWM_EVENT_WINDOW_REMOVE;
/* TODO: */
return true;
}
static bool handle_configure_request(
event_t *event, const xcb_configure_request_event_t *xcb_event) {
p_clear(event, 1);
event->type = ZDWM_EVENT_CONFIGURE_REQUEST;
/* TODO: */
return true;
}
bool backend_next_event(backend_t *backend, event_t *event) {
if (!backend || !backend->conn || !event) return false;
for (;;) {
xcb_generic_event_t *raw_event = xcb_wait_for_event(backend->conn);
if (!raw_event) return false;
uint8_t response_type = XCB_EVENT_RESPONSE_TYPE(raw_event);
bool handled = false;
printf("xcb event type: %u\n", response_type);
switch (response_type) {
#define EVENT(type, handler) \
case type: \
handled = handler(event, (void *)raw_event); \
break
EVENT(XCB_MAP_REQUEST, handle_map_request);
EVENT(XCB_UNMAP_NOTIFY, handle_unmap_notify);
EVENT(XCB_DESTROY_NOTIFY, handle_destroy_notify);
EVENT(XCB_CONFIGURE_REQUEST, handle_configure_request);
#undef EVENT
default:
break;
}
p_delete(&raw_event);
if (handled) return true;
}
}

View File

@@ -0,0 +1,12 @@
#pragma once
#include <xcb/xcb.h>
#include "core/backend.h" /* IWYU pragma: keep */
struct backend_t {
xcb_connection_t *conn;
xcb_screen_t *screen;
int screenp;
bool have_xfixes;
};

View File

@@ -2,6 +2,7 @@
#include <stddef.h>
#include "core/event.h"
#include "core/types.h"
typedef struct backend_t backend_t;
@@ -16,3 +17,5 @@ void backend_destroy(backend_t *backend);
backend_detect_t *backend_detect(backend_t *backend);
void backend_detect_destroy(backend_detect_t *detect);
bool backend_next_event(backend_t *backend, event_t *event);

View File

@@ -63,9 +63,10 @@ typedef struct pointer_enter_event_t {
} pointer_enter_event_t;
typedef struct window_map_request_event_t {
window_id_t id;
window_id_t window;
window_props_t props;
window_metadata_t metadata;
window_geometry_mode_t geometry;
} window_map_request_event_t;
typedef enum window_remove_reason_t {
@@ -154,7 +155,6 @@ typedef struct configure_request_event_t {
typedef struct event_t {
event_type_t type;
uint32_t time_ms;
union {
key_press_event_t key_press;
pointer_button_event_t pointer_button_press;

View File

@@ -4,6 +4,7 @@
#include "base/memory.h"
#include "core/backend.h"
#include "core/event.h"
#include "core/state.h"
#include "core/wm_desc.h"
@@ -90,3 +91,12 @@ void runtime_shutdown(runtime_t *runtime) {
if (runtime->config_module_handle) dlclose(runtime->config_module_handle);
runtime->config_module_handle = nullptr;
}
void runtime_run(runtime_t *runtime) {
for (;;) {
event_t event = {0};
if (!backend_next_event(runtime->backend, &event)) {
break;
}
}
}

View File

@@ -30,3 +30,4 @@ typedef struct runtime_t {
bool runtime_init(runtime_t *runtime, runtime_init_desc_t *desc);
void runtime_init_desc_cleanup(runtime_init_desc_t *desc);
void runtime_shutdown(runtime_t *runtime);
void runtime_run(runtime_t *runtime);

View File

@@ -34,6 +34,8 @@ typedef struct window_props_t {
window_id_t transient_for;
bool override_redirect;
bool skip_taskbar;
bool urgent;
window_type_t *types;
size_t type_count;
@@ -48,3 +50,9 @@ typedef struct window_metadata_t {
const char *class_name;
const char *instance_name;
} window_metadata_t;
typedef struct window_geometry_t {
window_geometry_mode_t mode;
rect_t rect;
bool fixed_size;
} window_geometry_t;