Skip to content

Commit 3f9bc30

Browse files
authored
Merge pull request #4 from jstkdng/tmux-fix
Recursively find window ids
2 parents 8188e12 + 748e34c commit 3f9bc30

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

ueberzug/X/display.c

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <X11/Xlib.h>
44
#include <X11/extensions/XRes.h>
55
#include <X11/extensions/XShm.h>
6+
#include <stdio.h>
7+
#include <stdlib.h>
68

79
#include "util.h"
810
#include "display.h"
@@ -92,25 +94,50 @@ has_property(DisplayObject *self, Window window, Atom property) {
9294
return status == Success && !(actual_type_return == None && actual_format_return == 0);
9395
}
9496

97+
static void
98+
get_child_window_ids_helper(Display *display, Window window,
99+
Window **total_children, unsigned int *total_num_children) {
100+
Window _, *children;
101+
unsigned int num_children;
102+
103+
XQueryTree(display, window, &_, &_, &children, &num_children);
104+
if (!children) return;
105+
106+
if (!*total_children) {
107+
*total_children = calloc(num_children, sizeof(Window));
108+
} else {
109+
Window *tmp = reallocarray(*total_children, num_children + *total_num_children, sizeof(Window));
110+
if (tmp) {
111+
*total_children = tmp;
112+
}
113+
}
114+
115+
for (int i = 0; i < num_children; ++i) {
116+
(*total_children)[i + *total_num_children] = children[i];
117+
}
118+
*total_num_children += num_children;
119+
120+
for (int i = 0; i < num_children; ++i) {
121+
get_child_window_ids_helper(display, children[i], total_children, total_num_children);
122+
}
123+
124+
XFree(children);
125+
}
95126

96127
static PyObject *
97128
Display_get_child_window_ids(DisplayObject *self, PyObject *args, PyObject *kwds) {
98129
static char *kwlist[] = {"parent_id", NULL};
99130
Window parent = XDefaultRootWindow(self->info_display);
100-
Window _, *children;
101-
unsigned int children_count;
131+
Window *children = NULL;
132+
unsigned int children_count = 0;
102133

103134
if (!PyArg_ParseTupleAndKeywords(
104135
args, kwds, "|k", kwlist,
105136
&parent)) {
106137
Py_RETURN_ERROR;
107138
}
108139

109-
if (!XQueryTree(
110-
self->info_display, parent,
111-
&_, &_, &children, &children_count)) {
112-
raise(OSError, "failed to query child windows of %lu", parent);
113-
}
140+
get_child_window_ids_helper(self->info_display, parent, &children, &children_count);
114141

115142
PyObject *child_ids = PyList_New(0);
116143
if (children) {

ueberzug/xutil.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,6 @@ def get_parent_window_infos(display: X.Display):
127127

128128
if client_pids:
129129
pid_window_id_map = get_pid_window_id_map(display)
130-
# Insert current window's PID & WID to the end of map to support tabbed.
131-
# NOTE: Terminal (current window) must have WINDOWID as env. variable.
132-
if (os.environ.get('WINDOWID') != None):
133-
pid_window_id_map[os.getpid()] = int(os.environ.get('WINDOWID'))
134130

135131
for pid in client_pids:
136132
ppids = get_parent_pids(pid)

0 commit comments

Comments
 (0)