From efebf35181554c40dc633e53329617c2b3990c83 Mon Sep 17 00:00:00 2001 From: ben Date: Mon, 21 Oct 2024 12:04:09 -0700 Subject: get focused window and set window name --- src/x.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) (limited to 'src/x.c') diff --git a/src/x.c b/src/x.c index 828445f..61a0fc0 100644 --- a/src/x.c +++ b/src/x.c @@ -4,8 +4,12 @@ #include #include +// #include very versatile + #include "x.h" #include "types.h" +#include "config.h" +#include "scapx.h" Server_context_t *init_XCB_server(); xcb_window_t create_win(Server_context_t *info); @@ -22,8 +26,7 @@ Server_context_t *init_XCB_server() info->con = xcb_connect(NULL, &info->scr_nbr); if (xcb_connection_has_error(info->con) > 0) { fprintf(stderr, "Error opening display.\n"); - free(info); - return NULL; + free(info); return NULL; } info->iter = xcb_setup_roots_iterator(xcb_get_setup(info->con)); @@ -68,6 +71,7 @@ xcb_window_t create_win(Server_context_t *info) XCB_EVENT_MASK_POINTER_MOTION | // mouse is moving without clicking XCB_MOTION_NOTIFY | XCB_EVENT_MASK_BUTTON_MOTION | + XCB_EVENT_MASK_LEAVE_WINDOW | /* XCB_MOTION_NOTIFY | XCB_EVENT_MASK_EXPOSURE | @@ -98,7 +102,7 @@ xcb_window_t create_win(Server_context_t *info) return win; } -xcb_cursor_t cursor_set(xcb_connection_t *c, xcb_screen_t *scr, xcb_window_t win, u32 cid) +xcb_cursor_t cursor_set(xcb_connection_t *c, xcb_window_t win, u32 cid) { xcb_font_t font; xcb_cursor_t ptr; @@ -115,7 +119,6 @@ xcb_cursor_t cursor_set(xcb_connection_t *c, xcb_screen_t *scr, xcb_window_t win 0, 0, 0, 0, 0, 0); - mask = XCB_CW_CURSOR; value_list = ptr; xcb_change_window_attributes(c, win, mask, &value_list); @@ -156,3 +159,45 @@ void die(xcb_key_symbols_t *sym, Server_context_t *s_info, xcb_drawable_t win) free(s_info); exit(0); } + +xcb_window_t get_focused_win(xcb_connection_t *con) +{ + xcb_get_input_focus_reply_t *in_fo_reply = NULL; + + in_fo_reply = xcb_get_input_focus_reply(con, xcb_get_input_focus(con), NULL); + if (!in_fo_reply) { + DEBUG_INFO("could not get input focus reply: %s\n", ""); + return XCB_WINDOW_NONE; + } + + xcb_window_t focused_win = in_fo_reply->focus; + free(in_fo_reply); + + if (focused_win == XCB_WINDOW_NONE) { + DEBUG_INFO("could not retrieve focused window: %s\n", ""); + return XCB_WINDOW_NONE; + } + + return focused_win; +} + +xcb_atom_t get_atom(xcb_connection_t *c, const char *atom_name) +{ + xcb_intern_atom_cookie_t cookie = xcb_intern_atom(c, 0, strlen(atom_name), atom_name); + xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(c, cookie, NULL); + xcb_atom_t atom = reply->atom; + free(reply); + + return atom; +} + +void xcb_set_win_name(xcb_connection_t *c, xcb_window_t w, const char *title) +{ + xcb_atom_t WM_NAME = get_atom(c, "WM_NAME"); + xcb_atom_t STRING = get_atom(c, "STRING"); + xcb_change_property(c, XCB_PROP_MODE_REPLACE, w, WM_NAME, STRING, 8, + strlen(title), title); + + xcb_map_window (c, w); + xcb_flush (c); +} -- cgit v1.2.3