diff options
| author | 2026-07-06 14:16:36 +0500 | |
|---|---|---|
| committer | 2026-07-06 14:16:36 +0500 | |
| commit | 7f65ffdbb61d679ff2292e8aca7ff7510360a072 (patch) | |
| tree | 9b68728ca45d41270bd0fc67c45433588e8583f8 /xhorse.c | |
| parent | 43a0df3f491c17e7efe42cd126c430bd43907a86 (diff) | |
| download | xhorse-7f65ffdbb61d679ff2292e8aca7ff7510360a072.tar.gz xhorse-7f65ffdbb61d679ff2292e8aca7ff7510360a072.tar.bz2 xhorse-7f65ffdbb61d679ff2292e8aca7ff7510360a072.zip | |
Diffstat (limited to 'xhorse.c')
| -rw-r--r-- | xhorse.c | 137 |
1 files changed, 85 insertions, 52 deletions
@@ -7,15 +7,25 @@ const int ESC_BUTTON = 9; +typedef struct xhorse_position +{ + int32_t x, y; +} xhorse_position_t; + +typedef struct xhorse_size +{ + int32_t w, h; +} xhorse_size_t; + typedef struct xhorse { xcb_connection_t *conn; xcb_screen_t *scr; xcb_window_t win; - int16_t root_x, root_y; - int16_t win_x, win_y; - bool dragging; + xhorse_position_t p_root, p_win; + xhorse_size_t s_win; + bool f_running, f_dragging; } xhorse_t; bool xh_connect(xhorse_t *xh) @@ -36,6 +46,37 @@ void xh_disconnect(xhorse_t *xh) xcb_disconnect(xh->conn); } +void xh_update_window_state(xhorse_t *xh) +{ + xcb_translate_coordinates_cookie_t coords_c; + xcb_get_geometry_cookie_t geom_c; + + xcb_translate_coordinates_reply_t *coords_r; + xcb_get_geometry_reply_t *geom_r; + + coords_c = xcb_translate_coordinates(xh->conn, xh->win, xh->scr->root, 0, 0); + coords_r = xcb_translate_coordinates_reply(xh->conn, coords_c, NULL); + + geom_c = xcb_get_geometry(xh->conn, xh->win); + geom_r = xcb_get_geometry_reply(xh->conn, geom_c, NULL); + + if (NULL != coords_r) + { + xh->p_win.x = coords_r->dst_x; + xh->p_win.y = coords_r->dst_y; + + free(coords_r); + } + + if (NULL != geom_r) + { + xh->s_win.w = geom_r->width; + xh->s_win.h = geom_r->height; + + free(geom_r); + } +} + void xh_setup(xhorse_t *xh) { const xcb_setup_t *set = xcb_get_setup(xh->conn); @@ -50,7 +91,6 @@ void xh_setup(xhorse_t *xh) }; xh->win = xcb_generate_id(xh->conn); - xh->win_x = xh->win_y = 0; xcb_void_cookie_t cookie = xcb_create_window(xh->conn, /* depth */ XCB_COPY_FROM_PARENT, xh->win, xh->scr->root, /* x */ 0, /* y */ 0, /* w */ 150, /* h */ 75, @@ -58,85 +98,74 @@ void xh_setup(xhorse_t *xh) xh->scr->root_visual, mask, vals); xcb_map_window(xh->conn, xh->win); + + xh->f_running = true; + xh_update_window_state(xh); } -void xh_update_current_position(xhorse_t *xh) +void xh_handle_btn_press(xhorse_t *xh, xcb_generic_event_t *ev) { - xcb_translate_coordinates_cookie_t cookie = - xcb_translate_coordinates(xh->conn, xh->win, xh->scr->root, 0, 0); + xh_update_window_state(xh); - xcb_translate_coordinates_reply_t *reply = - xcb_translate_coordinates_reply(xh->conn, cookie, NULL); + if (false == xh->f_dragging) + xh->f_dragging = true; - if (reply) - { - xh->win_x = reply->dst_x; - xh->win_y = reply->dst_y; + xcb_key_press_event_t *kp_ev = (xcb_key_press_event_t *)ev; + xh->p_root.x = kp_ev->root_x; + xh->p_root.y = kp_ev->root_y; +} - free(reply); - } +void xh_handle_btn_release(xhorse_t *xh, xcb_generic_event_t *ev) +{ + if (true == xh->f_dragging) + xh->f_dragging = false; +} + +void xh_handle_key_press(xhorse_t *xh, xcb_generic_event_t *ev) +{ + xcb_keycode_t btn = ((xcb_key_press_event_t *)ev)->detail; + if (ESC_BUTTON == btn) + xh->f_running = false; } void xh_handle_motion(xhorse_t *xh, xcb_generic_event_t *generic_ev) { - if (!xh->dragging) return; + if (!xh->f_dragging) return; xcb_motion_notify_event_t *motion_ev = (xcb_motion_notify_event_t *)generic_ev; int32_t pos[2] = { - xh->win_x + (motion_ev->root_x - xh->root_x), - xh->win_y + (motion_ev->root_y - xh->root_y) + xh->p_win.x + (motion_ev->root_x - xh->p_root.x), + xh->p_win.y + (motion_ev->root_y - xh->p_root.y) }; xcb_configure_window(xh->conn, xh->win, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, pos); xcb_flush(xh->conn); } -void xh_event_loop(xhorse_t *xh) +void xh_poll_for_events(xhorse_t *xh) { - xcb_generic_event_t *generic_ev; + xcb_generic_event_t *ev; - while ((generic_ev = xcb_wait_for_event(xh->conn))) + while (NULL != (ev = xcb_poll_for_event(xh->conn))) { - uint8_t response_type = generic_ev->response_type & ~0x80; - - switch (response_type) + switch (ev->response_type & ~0x80) { - case XCB_EXPOSE: + case XCB_BUTTON_PRESS: + xh_handle_btn_press(xh, ev); + break; case XCB_BUTTON_RELEASE: - xh->dragging = false; + xh_handle_btn_release(xh, ev); break; - case XCB_KEY_PRESS: - { - xcb_key_press_event_t *press_ev = (xcb_key_press_event_t *)generic_ev; - - if (ESC_BUTTON == press_ev->detail) - return; - - break; - } - - case XCB_BUTTON_PRESS: - { - xcb_button_press_event_t *mouse_ev = (xcb_button_press_event_t *)generic_ev; - - xh_update_current_position(xh); - xh->root_x = mouse_ev->root_x; - xh->root_y = mouse_ev->root_y; - xh->dragging = true; - + xh_handle_key_press(xh, ev); break; - } - case XCB_MOTION_NOTIFY: - { - xh_handle_motion(xh, generic_ev); + xh_handle_motion(xh, ev); break; - } } - free(generic_ev); + free(ev); } } @@ -152,7 +181,11 @@ int main(void) xh_setup(xh); xcb_flush(xh->conn); - xh_event_loop(xh); + while (xh->f_running) + { + xh_poll_for_events(xh); + // xh_gravity_step(xh) + } xh_disconnect(xh); free(xh); |
