From 8f98f68de5b69aa21a250e25d2b0556397e263ce Mon Sep 17 00:00:00 2001 From: Tin Date: Fri, 10 Nov 2023 03:11:02 +0100 Subject: [PATCH] Simplify cursor checking Additional tweaks and docs improvements Signed-off-by: Tin --- doc/config_settings.yaml | 4 ++-- src/display-x11.cc | 5 ++--- src/x11.cc | 33 ++++++++++++++------------------- src/x11.h | 1 - 4 files changed, 18 insertions(+), 25 deletions(-) diff --git a/doc/config_settings.yaml b/doc/config_settings.yaml index 51d65d064..24594b3c7 100644 --- a/doc/config_settings.yaml +++ b/doc/config_settings.yaml @@ -369,7 +369,7 @@ values: - name: overwrite_file desc: Overwrite the file given as argument. - name: own_window - desc: Boolean, create own window to draw. + desc: Boolean, draw conky in own window instead of drawing on root window. - name: own_window_argb_value desc: |- When ARGB visuals are enabled, this use this to modify the @@ -388,7 +388,7 @@ values: desc: |- If own_window_transparent no, set a specified background colour. Takes either a hex value (e.g. '#ffffff'), - a shorthand hex value (e.g. '#fff'), or a valid RGB nam + a shorthand hex value (e.g. '#fff'), or a valid RGB name (see `/usr/lib/X11/rgb.txt`). default: black args: diff --git a/src/display-x11.cc b/src/display-x11.cc index 3558608dd..884ea8f8e 100644 --- a/src/display-x11.cc +++ b/src/display-x11.cc @@ -382,13 +382,12 @@ bool display_output_x11::main_loop_wait(double t) { XNextEvent(display, &ev); -#ifdef BUILD_XINPUT +#if defined(BUILD_MOUSE_EVENTS) && defined(BUILD_XINPUT) if (ev.type == GenericEvent && ev.xcookie.extension == window.xi_opcode) { XGetEventData(display, &ev.xcookie); if (ev.xcookie.evtype == XI_Motion) { auto *data = reinterpret_cast(ev.xcookie.data); - // XQueryPointer returns wrong results because conky is a weird window Window query_result = query_x11_window_at_pos(display, data->root_x, data->root_y); static bool cursor_inside = false; @@ -415,7 +414,7 @@ bool display_output_x11::main_loop_wait(double t) { XFreeEventData(display, &ev.xcookie); continue; } -#endif /* BUILD_XINPUT */ +#endif /* BUILD_MOUSE_EVENTS && BUILD_XINPUT */ // Any of the remaining events apply to conky window if (ev.xany.window != window.window) continue; diff --git a/src/x11.cc b/src/x11.cc index 8d6c2e116..5a31d71ac 100644 --- a/src/x11.cc +++ b/src/x11.cc @@ -1381,7 +1381,9 @@ void propagate_x11_event(XEvent &ev) { } #ifdef BUILD_MOUSE_EVENTS -Window last_descendant(Display* display, Window parent) { +// Assuming parent has a simple linear stack of descendants, this function +// returns the last leaf on the graph. +inline Window last_descendant(Display* display, Window parent) { Window ignored, *children; uint32_t count; @@ -1398,26 +1400,19 @@ Window last_descendant(Display* display, Window parent) { Window query_x11_window_at_pos(Display* display, int x, int y) { Window root = DefaultRootWindow(display); - Window root_return, parent_return, *windows; - unsigned int count; - - Window last = None; + // these values are ignored but NULL can't be passed + Window root_return; + int root_x_return, root_y_return, win_x_return, win_y_return; + unsigned int mask_return; - XWindowAttributes attrs; - if (XQueryTree(display, root, &root_return, &parent_return, &windows, &count) != 0) { - for (unsigned int i = 0; i < count; i++) { - if (XGetWindowAttributes(display, windows[i], &attrs)) { - if (attrs.map_state == IsViewable && x >= attrs.x && x < (attrs.x + attrs.width) && y >= attrs.y && y < (attrs.y + attrs.height)) { - last = windows[i]; - } - } - } + Window last = None; + XQueryPointer(display, window.root, &root_return, &last, + &root_x_return, &root_y_return, &win_x_return, &win_y_return, &mask_return + ); - if (count != 0) { - XFree(windows); - } - } - + // X11 correctly returns a window which covers conky area, but returned window + // is not window.window, but instead a parent node in some cases and the + // window.window we want to check for is a 1x1 child of that window. return last_descendant(display, last); } diff --git a/src/x11.h b/src/x11.h index 0a574abb1..116a9285d 100644 --- a/src/x11.h +++ b/src/x11.h @@ -150,7 +150,6 @@ InputEvent *xev_as_input_event(XEvent &ev); void propagate_x11_event(XEvent &ev); #ifdef BUILD_MOUSE_EVENTS - Window query_x11_window_at_pos(Display* display, int x, int y); #endif /* BUILD_MOUSE_EVENTS */