Modularize

This commit is contained in:
2025-10-03 12:45:54 +01:00
parent 049d762cbb
commit f7451f6ee1
4 changed files with 230 additions and 141 deletions

View File

@@ -239,6 +239,61 @@ void set_wm_state(xcb_window_t win, int state) {
xcb_flush(conn);
}
xcb_window_t draw_rectangle(int x, int y, int width, int height,
uint32_t color) {
// Get the screen
const xcb_setup_t *setup = xcb_get_setup(conn);
xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
xcb_screen_t *screen = iter.data;
// Create an override-redirect window
xcb_window_t win = xcb_generate_id(conn);
uint32_t mask =
XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
uint32_t values[3];
values[0] = screen->black_pixel; // initial background pixel
values[1] = 1; // override_redirect = true
values[2] = XCB_EVENT_MASK_EXPOSURE; // we want exposure events
xcb_create_window(conn,
XCB_COPY_FROM_PARENT, // depth
win, // window ID
screen->root, // parent
x, y, // x, y
width, height, // width, height
0, // border width
XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual, mask,
values);
// Map the window (make it visible)
xcb_map_window(conn, win);
// Fill it with the solid color using a Graphics Context
xcb_gcontext_t gc = xcb_generate_id(conn);
uint32_t gc_values[] = {color, XCB_LINE_STYLE_SOLID};
xcb_create_gc(conn, gc, win, XCB_GC_FOREGROUND | XCB_GC_LINE_STYLE,
gc_values);
xcb_rectangle_t rect = {0, 0, width, height}; // relative to window
xcb_poly_fill_rectangle(conn, win, gc, 1, &rect);
// Flush the connection to send commands to the server
xcb_flush(conn);
return win;
}
void grab_pointer(xcb_window_t win) {
xcb_grab_pointer(conn, 0, win,
XCB_EVENT_MASK_BUTTON_RELEASE |
XCB_EVENT_MASK_BUTTON_MOTION |
XCB_EVENT_MASK_POINTER_MOTION_HINT,
XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, win, XCB_NONE,
XCB_CURRENT_TIME);
}
void ungrab_pointer(void) { xcb_ungrab_pointer(conn, XCB_CURRENT_TIME); }
// Wait for an event and return it as an Event structure
// This function is blocking
// The event is sent by value, so no need to free anything
@@ -294,13 +349,6 @@ Event wait_for_event(void) {
xcb_button_press_event_t *e = (xcb_button_press_event_t *)ev;
if (!e->child)
break;
// Grab pointer for dragging
xcb_grab_pointer(conn, 0, e->child,
XCB_EVENT_MASK_BUTTON_RELEASE |
XCB_EVENT_MASK_BUTTON_MOTION |
XCB_EVENT_MASK_POINTER_MOTION_HINT,
XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, e->child,
XCB_NONE, XCB_CURRENT_TIME);
ret.type = 6;
ret.window = e->child;
ret.is_root = e->child == scr->root;
@@ -312,7 +360,6 @@ Event wait_for_event(void) {
case XCB_MOTION_NOTIFY: {
xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t *)ev;
ret.type = 7;
ret.window = e->child;
ret.x = e->event_x;
ret.y = e->event_y;
ret.state = e->state;
@@ -320,8 +367,6 @@ Event wait_for_event(void) {
case XCB_BUTTON_RELEASE: {
xcb_button_release_event_t *e = (xcb_button_release_event_t *)ev;
// Ungrab pointer after dragging
xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
ret.type = 8;
ret.x = e->event_x;
ret.y = e->event_y;