diff options
Diffstat (limited to 'src/x11_util.c')
-rw-r--r-- | src/x11_util.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/x11_util.c b/src/x11_util.c new file mode 100644 index 0000000..f5f1c87 --- /dev/null +++ b/src/x11_util.c @@ -0,0 +1,56 @@ +// Copyright 2020-2024 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#include "x11_util.h" + +#include <X11/X.h> +#include <X11/Xlib.h> + +#include <stddef.h> + +bool +suil_x11_is_valid_child(Display* const display, + const Window parent, + const Window child) +{ + Window root = 0U; + Window grandparent = 0U; + Window* children = NULL; + unsigned n_children = 0U; + + XQueryTree(display, parent, &root, &grandparent, &children, &n_children); + + for (unsigned i = 0U; i < n_children; ++i) { + if (children[i] == child) { + if (children) { + XFree(children); + } + return true; + } + } + + if (children) { + XFree(children); + } + + return false; +} + +Window +suil_x11_get_parent(Display* display, Window child) +{ + Window root = 0U; + Window parent = 0U; + Window* children = NULL; + unsigned count = 0U; + + if (child) { + if (XQueryTree(display, child, &root, &parent, &children, &count)) { + if (children) { + XFree(children); + } + } + } + + return (parent == root) ? 0 : parent; +} |