dsh-gufa-code 1.3.49 → 1.3.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,608 @@
1
+ /**
2
+ * X11 能力实现(见 x11.h)。
3
+ */
4
+ #include "x11.h"
5
+
6
+ #include <X11/Xatom.h>
7
+ #include <X11/Xutil.h>
8
+
9
+ #include <stdio.h>
10
+ #include <string.h>
11
+ #include <unistd.h>
12
+
13
+ #include "log.h"
14
+
15
+ typedef struct {
16
+ Window win;
17
+ long pid;
18
+ int x;
19
+ int y;
20
+ int width;
21
+ int height;
22
+ gboolean viewable;
23
+ } GufaX11WindowInfo;
24
+
25
+ /* ── Xlib 错误处理:查询窗口时窗口可能刚好消失,BadWindow 属于预期情况 ── */
26
+ static int (*gufa_previous_error_handler)(Display *, XErrorEvent *) = NULL;
27
+ static gboolean gufa_error_handler_installed = FALSE;
28
+
29
+ static int gufa_x11_error_handler(Display *display, XErrorEvent *event) {
30
+ if (event != NULL &&
31
+ (event->error_code == BadWindow || event->error_code == BadDrawable ||
32
+ event->error_code == BadMatch || event->error_code == BadValue ||
33
+ event->error_code == BadAccess)) {
34
+ return 0;
35
+ }
36
+ if (gufa_previous_error_handler != NULL) {
37
+ return gufa_previous_error_handler(display, event);
38
+ }
39
+ return 0;
40
+ }
41
+
42
+ static void gufa_x11_install_error_handler(void) {
43
+ if (gufa_error_handler_installed) return;
44
+ gufa_error_handler_installed = TRUE;
45
+ XErrorHandler previous = XSetErrorHandler(gufa_x11_error_handler);
46
+ gufa_previous_error_handler = (int (*)(Display *, XErrorEvent *))previous;
47
+ }
48
+
49
+ /* ── 属性读取工具 ───────────────────────────────────────────────────── */
50
+
51
+ static gboolean gufa_x11_get_property(GufaX11 *x11, Window window, Atom atom, Atom requested_type,
52
+ unsigned char **data, Atom *actual_type, int *actual_format,
53
+ unsigned long *nitems) {
54
+ unsigned long bytes_after = 0;
55
+ *data = NULL;
56
+ *actual_type = None;
57
+ *actual_format = 0;
58
+ *nitems = 0;
59
+ if (x11 == NULL || !x11->ok || x11->dpy == NULL || window == 0) return FALSE;
60
+ int status = XGetWindowProperty(x11->dpy, window, atom, 0, 4096, False, requested_type,
61
+ actual_type, actual_format, nitems, &bytes_after, data);
62
+ if (status != Success || *data == NULL) {
63
+ if (*data != NULL) {
64
+ XFree(*data);
65
+ *data = NULL;
66
+ }
67
+ return FALSE;
68
+ }
69
+ return TRUE;
70
+ }
71
+
72
+ long gufa_x11_window_pid(GufaX11 *x11, Window window) {
73
+ unsigned char *data = NULL;
74
+ Atom type = None;
75
+ int format = 0;
76
+ unsigned long nitems = 0;
77
+ long pid = 0;
78
+ if (!gufa_x11_get_property(x11, window, x11->atom_net_wm_pid, XA_CARDINAL, &data, &type, &format, &nitems)) {
79
+ return 0;
80
+ }
81
+ if (format == 32 && nitems >= 1 && data != NULL) {
82
+ pid = (long)((unsigned long *)(void *)data)[0];
83
+ }
84
+ XFree(data);
85
+ return pid;
86
+ }
87
+
88
+ char *gufa_x11_window_title(GufaX11 *x11, Window window) {
89
+ unsigned char *data = NULL;
90
+ Atom type = None;
91
+ int format = 0;
92
+ unsigned long nitems = 0;
93
+ if (gufa_x11_get_property(x11, window, x11->atom_net_wm_name, x11->atom_utf8_string,
94
+ &data, &type, &format, &nitems)) {
95
+ char *title = NULL;
96
+ if (format == 8 && nitems > 0 && data != NULL) {
97
+ title = g_strndup((const char *)data, (gsize)nitems);
98
+ }
99
+ XFree(data);
100
+ if (title != NULL) return title;
101
+ }
102
+ char *wm_name = NULL;
103
+ if (x11->dpy != NULL && XFetchName(x11->dpy, window, &wm_name) != 0 && wm_name != NULL) {
104
+ char *copy = g_strdup(wm_name);
105
+ XFree(wm_name);
106
+ return copy;
107
+ }
108
+ return NULL;
109
+ }
110
+
111
+ char *gufa_x11_window_class(GufaX11 *x11, Window window) {
112
+ if (x11 == NULL || !x11->ok || window == 0) return NULL;
113
+ XClassHint hint;
114
+ memset(&hint, 0, sizeof(hint));
115
+ if (XGetClassHint(x11->dpy, window, &hint) == 0) return NULL;
116
+ char *result = NULL;
117
+ if (hint.res_class != NULL && *hint.res_class != '\0') {
118
+ result = g_strdup(hint.res_class);
119
+ } else if (hint.res_name != NULL && *hint.res_name != '\0') {
120
+ result = g_strdup(hint.res_name);
121
+ }
122
+ if (hint.res_name != NULL) XFree(hint.res_name);
123
+ if (hint.res_class != NULL) XFree(hint.res_class);
124
+ return result;
125
+ }
126
+
127
+ Window gufa_x11_active_window(GufaX11 *x11) {
128
+ unsigned char *data = NULL;
129
+ Atom type = None;
130
+ int format = 0;
131
+ unsigned long nitems = 0;
132
+ Window window = 0;
133
+ if (!gufa_x11_get_property(x11, x11->root, x11->atom_net_active_window, XA_WINDOW,
134
+ &data, &type, &format, &nitems)) {
135
+ return 0;
136
+ }
137
+ if (format == 32 && nitems >= 1 && data != NULL) {
138
+ window = (Window)((unsigned long *)(void *)data)[0];
139
+ }
140
+ XFree(data);
141
+ return window;
142
+ }
143
+
144
+ /* ── 窗口枚举 ───────────────────────────────────────────────────────── */
145
+
146
+ static gboolean gufa_x11_window_type_followable(GufaX11 *x11, Window window) {
147
+ unsigned char *data = NULL;
148
+ Atom type = None;
149
+ int format = 0;
150
+ unsigned long nitems = 0;
151
+ if (!gufa_x11_get_property(x11, window, x11->atom_net_wm_window_type, XA_ATOM, &data, &type, &format, &nitems)) {
152
+ return TRUE; /* 没有声明类型:当作普通窗口 */
153
+ }
154
+ gboolean followable = FALSE;
155
+ if (format == 32 && data != NULL) {
156
+ long *atoms = (long *)(void *)data;
157
+ for (unsigned long i = 0; i < nitems; i++) {
158
+ Atom value = (Atom)atoms[i];
159
+ if (value == x11->atom_net_wm_window_type_normal || value == x11->atom_net_wm_window_type_dialog) {
160
+ followable = TRUE;
161
+ break;
162
+ }
163
+ }
164
+ }
165
+ XFree(data);
166
+ return followable;
167
+ }
168
+
169
+ static void gufa_x11_collect_children(GufaX11 *x11, Window parent, int depth, Window skip,
170
+ gboolean include_unmapped, GArray *out) {
171
+ Window root_return = 0;
172
+ Window parent_return = 0;
173
+ Window *children = NULL;
174
+ unsigned int child_count = 0;
175
+ if (depth > 3) return;
176
+ if (XQueryTree(x11->dpy, parent, &root_return, &parent_return, &children, &child_count) == 0) return;
177
+ for (unsigned int i = 0; i < child_count; i++) {
178
+ Window window = children[i];
179
+ if (window == skip) continue;
180
+ XWindowAttributes attributes;
181
+ memset(&attributes, 0, sizeof(attributes));
182
+ if (XGetWindowAttributes(x11->dpy, window, &attributes) == 0) continue;
183
+ if (attributes.override_redirect) continue;
184
+ if (attributes.class != InputOutput) continue;
185
+ long pid = gufa_x11_window_pid(x11, window);
186
+ if (pid > 0) {
187
+ if (attributes.width < 40 || attributes.height < 40) continue;
188
+ if (!include_unmapped && attributes.map_state != IsViewable) continue;
189
+ int root_x = 0;
190
+ int root_y = 0;
191
+ Window child_return = 0;
192
+ if (XTranslateCoordinates(x11->dpy, window, x11->root, 0, 0, &root_x, &root_y, &child_return) == 0) {
193
+ root_x = 0;
194
+ root_y = 0;
195
+ }
196
+ GufaX11WindowInfo info;
197
+ info.win = window;
198
+ info.pid = pid;
199
+ info.x = root_x;
200
+ info.y = root_y;
201
+ info.width = attributes.width;
202
+ info.height = attributes.height;
203
+ info.viewable = attributes.map_state == IsViewable;
204
+ g_array_append_val(out, info);
205
+ } else if (depth < 2) {
206
+ /* 有 WM 时 root 的子窗口是 frame,真正的 client 在下一层 */
207
+ gufa_x11_collect_children(x11, window, depth + 1, skip, include_unmapped, out);
208
+ }
209
+ }
210
+ if (children != NULL) XFree(children);
211
+ }
212
+
213
+ /** 在 root 上枚举「带 _NET_WM_PID 的顶层 client 窗口」;include_unmapped 时也收其它工作区/最小化的窗口。 */
214
+ static GArray *gufa_x11_enumerate(GufaX11 *x11, Window skip, gboolean include_unmapped) {
215
+ GArray *out = g_array_new(FALSE, FALSE, sizeof(GufaX11WindowInfo));
216
+ if (x11 == NULL || !x11->ok) return out;
217
+ gufa_x11_collect_children(x11, x11->root, 0, skip, include_unmapped, out);
218
+ return out;
219
+ }
220
+
221
+ static void gufa_x11_window_list_clear(GufaX11WindowList *hidden) {
222
+ if (hidden == NULL) return;
223
+ g_free(hidden->wins);
224
+ hidden->wins = NULL;
225
+ hidden->n = 0;
226
+ }
227
+
228
+ static void gufa_x11_window_list_add(GufaX11WindowList *hidden, Window window) {
229
+ if (hidden == NULL || window == 0) return;
230
+ hidden->wins = g_renew(Window, hidden->wins, hidden->n + 1);
231
+ hidden->wins[hidden->n] = window;
232
+ hidden->n++;
233
+ }
234
+
235
+ static gboolean gufa_text_contains_ci(const char *haystack, const char *needle) {
236
+ if (haystack == NULL || needle == NULL || *needle == '\0') return FALSE;
237
+ char *haystack_lower = g_ascii_strdown(haystack, -1);
238
+ char *needle_lower = g_ascii_strdown(needle, -1);
239
+ gboolean found = strstr(haystack_lower, needle_lower) != NULL;
240
+ g_free(haystack_lower);
241
+ g_free(needle_lower);
242
+ return found;
243
+ }
244
+
245
+ static char *gufa_x11_process_comm(long pid);
246
+
247
+ /* ── 对外接口 ───────────────────────────────────────────────────────── */
248
+
249
+ gboolean gufa_x11_open(GufaX11 *x11) {
250
+ if (x11 == NULL) return FALSE;
251
+ memset(x11, 0, sizeof(*x11));
252
+ x11->self_pid = (long)getpid();
253
+ gufa_x11_install_error_handler();
254
+ x11->dpy = XOpenDisplay(NULL);
255
+ if (x11->dpy == NULL) {
256
+ gufa_log("x11: XOpenDisplay 失败(DISPLAY=%s),follow/隐藏主窗口不可用",
257
+ g_getenv("DISPLAY") != NULL ? g_getenv("DISPLAY") : "(unset)");
258
+ return FALSE;
259
+ }
260
+ x11->root = DefaultRootWindow(x11->dpy);
261
+ if (x11->root == 0) {
262
+ XCloseDisplay(x11->dpy);
263
+ x11->dpy = NULL;
264
+ return FALSE;
265
+ }
266
+ #define GUFA_ATOM(field, name) (x11->field = XInternAtom(x11->dpy, (name), False))
267
+ GUFA_ATOM(atom_net_wm_pid, "_NET_WM_PID");
268
+ GUFA_ATOM(atom_net_wm_name, "_NET_WM_NAME");
269
+ GUFA_ATOM(atom_utf8_string, "UTF8_STRING");
270
+ GUFA_ATOM(atom_net_active_window, "_NET_ACTIVE_WINDOW");
271
+ GUFA_ATOM(atom_net_client_list, "_NET_CLIENT_LIST");
272
+ GUFA_ATOM(atom_net_wm_window_type, "_NET_WM_WINDOW_TYPE");
273
+ GUFA_ATOM(atom_net_wm_window_type_normal, "_NET_WM_WINDOW_TYPE_NORMAL");
274
+ GUFA_ATOM(atom_net_wm_window_type_dialog, "_NET_WM_WINDOW_TYPE_DIALOG");
275
+ GUFA_ATOM(atom_net_wm_window_type_desktop, "_NET_WM_WINDOW_TYPE_DESKTOP");
276
+ GUFA_ATOM(atom_net_wm_window_type_dock, "_NET_WM_WINDOW_TYPE_DOCK");
277
+ GUFA_ATOM(atom_net_wm_window_type_toolbar, "_NET_WM_WINDOW_TYPE_TOOLBAR");
278
+ GUFA_ATOM(atom_net_wm_window_type_menu, "_NET_WM_WINDOW_TYPE_MENU");
279
+ GUFA_ATOM(atom_net_wm_window_type_utility, "_NET_WM_WINDOW_TYPE_UTILITY");
280
+ GUFA_ATOM(atom_net_wm_window_type_splash, "_NET_WM_WINDOW_TYPE_SPLASH");
281
+ GUFA_ATOM(atom_net_wm_window_type_dropdown, "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU");
282
+ GUFA_ATOM(atom_net_wm_window_type_tooltip, "_NET_WM_WINDOW_TYPE_TOOLTIP");
283
+ GUFA_ATOM(atom_net_wm_desktop, "_NET_WM_DESKTOP");
284
+ #undef GUFA_ATOM
285
+ x11->ok = TRUE;
286
+ gufa_log("x11: 已连接 X server %s(root=0x%lx self_pid=%ld)",
287
+ DisplayString(x11->dpy), (unsigned long)x11->root, x11->self_pid);
288
+ return TRUE;
289
+ }
290
+
291
+ void gufa_x11_close(GufaX11 *x11) {
292
+ if (x11 == NULL) return;
293
+ if (x11->dpy != NULL) {
294
+ XSync(x11->dpy, False);
295
+ XCloseDisplay(x11->dpy);
296
+ x11->dpy = NULL;
297
+ }
298
+ x11->ok = FALSE;
299
+ }
300
+
301
+ Window gufa_x11_find_window(GufaX11 *x11, long pid, const char *title_hint, Window skip) {
302
+ if (x11 == NULL || !x11->ok) return 0;
303
+ GArray *windows = gufa_x11_enumerate(x11, skip, TRUE);
304
+ /* 优先「当前可见」的候选(title 命中 > 面积最大);都没有时退回其它工作区/最小化的候选,
305
+ 找到后上层会发 _NET_ACTIVE_WINDOW 让 WM 切到目标窗口所在工作区。 */
306
+ Window visible_largest = 0;
307
+ long visible_largest_area = 0;
308
+ Window visible_title = 0;
309
+ long visible_title_area = 0;
310
+ Window hidden_largest = 0;
311
+ long hidden_largest_area = 0;
312
+ Window hidden_title = 0;
313
+ long hidden_title_area = 0;
314
+ gboolean want_title = title_hint != NULL && *title_hint != '\0';
315
+ GHashTable *seen = g_hash_table_new(g_direct_hash, g_direct_equal);
316
+ for (guint i = 0; i < windows->len; i++) {
317
+ GufaX11WindowInfo *info = &g_array_index(windows, GufaX11WindowInfo, i);
318
+ if (info->win == skip || info->pid == x11->self_pid) continue;
319
+ if (pid > 0 && info->pid != pid) continue;
320
+ if (!gufa_x11_window_type_followable(x11, info->win)) continue;
321
+ if (g_hash_table_contains(seen, (gpointer)(guintptr)info->win)) continue;
322
+ g_hash_table_add(seen, (gpointer)(guintptr)info->win);
323
+ long area = (long)info->width * (long)info->height;
324
+ if (area <= 0) continue;
325
+ if (want_title) {
326
+ char *title = gufa_x11_window_title(x11, info->win);
327
+ gboolean matches = gufa_text_contains_ci(title, title_hint);
328
+ g_free(title);
329
+ if (matches) {
330
+ if (info->viewable) {
331
+ if (area > visible_title_area) {
332
+ visible_title = info->win;
333
+ visible_title_area = area;
334
+ }
335
+ } else if (area > hidden_title_area) {
336
+ hidden_title = info->win;
337
+ hidden_title_area = area;
338
+ }
339
+ continue;
340
+ }
341
+ }
342
+ if (info->viewable) {
343
+ if (area > visible_largest_area) {
344
+ visible_largest = info->win;
345
+ visible_largest_area = area;
346
+ }
347
+ } else if (area > hidden_largest_area) {
348
+ hidden_largest = info->win;
349
+ hidden_largest_area = area;
350
+ }
351
+ }
352
+ g_hash_table_destroy(seen);
353
+ g_array_free(windows, TRUE);
354
+ if (visible_title != 0) return visible_title;
355
+ if (visible_largest != 0) return visible_largest;
356
+ if (hidden_title != 0) return hidden_title;
357
+ return hidden_largest;
358
+ }
359
+
360
+ gboolean gufa_x11_window_rect(GufaX11 *x11, Window window, GufaX11Rect *out) {
361
+ if (out != NULL) memset(out, 0, sizeof(*out));
362
+ if (x11 == NULL || !x11->ok || window == 0 || out == NULL) return FALSE;
363
+ XWindowAttributes attributes;
364
+ memset(&attributes, 0, sizeof(attributes));
365
+ if (XGetWindowAttributes(x11->dpy, window, &attributes) == 0) return FALSE;
366
+ int root_x = 0;
367
+ int root_y = 0;
368
+ Window child_return = 0;
369
+ if (XTranslateCoordinates(x11->dpy, window, x11->root, 0, 0, &root_x, &root_y, &child_return) == 0) {
370
+ return FALSE;
371
+ }
372
+ out->x = root_x;
373
+ out->y = root_y;
374
+ out->width = attributes.width;
375
+ out->height = attributes.height;
376
+ out->viewable = attributes.map_state == IsViewable;
377
+ out->valid = attributes.width > 0 && attributes.height > 0;
378
+ return TRUE;
379
+ }
380
+
381
+ gboolean gufa_x11_window_exists(GufaX11 *x11, Window window) {
382
+ if (x11 == NULL || !x11->ok || window == 0) return FALSE;
383
+ XWindowAttributes attributes;
384
+ memset(&attributes, 0, sizeof(attributes));
385
+ return XGetWindowAttributes(x11->dpy, window, &attributes) != 0;
386
+ }
387
+
388
+ void gufa_x11_activate_window(GufaX11 *x11, Window window, guint32 timestamp) {
389
+ if (x11 == NULL || !x11->ok || window == 0) return;
390
+ XEvent event;
391
+ memset(&event, 0, sizeof(event));
392
+ event.xclient.type = ClientMessage;
393
+ event.xclient.serial = 0;
394
+ event.xclient.send_event = True;
395
+ event.xclient.display = x11->dpy;
396
+ event.xclient.window = window;
397
+ event.xclient.message_type = x11->atom_net_active_window;
398
+ event.xclient.format = 32;
399
+ event.xclient.data.l[0] = 2; /* source indication: pager,避免部分 WM 的焦点抢占限制 */
400
+ event.xclient.data.l[1] = (long)timestamp;
401
+ event.xclient.data.l[2] = 0;
402
+ event.xclient.data.l[3] = 0;
403
+ event.xclient.data.l[4] = 0;
404
+ XSendEvent(x11->dpy, x11->root, False, SubstructureRedirectMask | SubstructureNotifyMask, &event);
405
+ XRaiseWindow(x11->dpy, window);
406
+ XFlush(x11->dpy);
407
+ }
408
+
409
+ void gufa_x11_map_window(GufaX11 *x11, Window window, gboolean do_raise) {
410
+ if (x11 == NULL || !x11->ok || window == 0) return;
411
+ XMapWindow(x11->dpy, window);
412
+ if (do_raise) XRaiseWindow(x11->dpy, window);
413
+ XFlush(x11->dpy);
414
+ }
415
+
416
+ void gufa_x11_copy_desktop(GufaX11 *x11, Window target, Window own) {
417
+ if (x11 == NULL || !x11->ok || target == 0 || own == 0) return;
418
+ unsigned char *data = NULL;
419
+ Atom type = None;
420
+ int format = 0;
421
+ unsigned long nitems = 0;
422
+ if (!gufa_x11_get_property(x11, target, x11->atom_net_wm_desktop, XA_CARDINAL, &data, &type, &format, &nitems)) {
423
+ return;
424
+ }
425
+ unsigned long desktop = 0xFFFFFFFFUL;
426
+ if (format == 32 && nitems >= 1 && data != NULL) {
427
+ desktop = ((unsigned long *)(void *)data)[0];
428
+ }
429
+ XFree(data);
430
+ if (desktop == 0xFFFFFFFFUL) return; /* 目标在所有工作区(sticky),不用动 */
431
+ /* 1) 初始属性(未被 WM 映射时有效) */
432
+ XChangeProperty(x11->dpy, own, x11->atom_net_wm_desktop, XA_CARDINAL, 32, PropModeReplace,
433
+ (const unsigned char *)&desktop, 1);
434
+ /* 2) EWMH ClientMessage:窗口已映射后要移动工作区必须发消息给 WM */
435
+ XEvent event;
436
+ memset(&event, 0, sizeof(event));
437
+ event.xclient.type = ClientMessage;
438
+ event.xclient.send_event = True;
439
+ event.xclient.display = x11->dpy;
440
+ event.xclient.window = own;
441
+ event.xclient.message_type = x11->atom_net_wm_desktop;
442
+ event.xclient.format = 32;
443
+ event.xclient.data.l[0] = (long)desktop;
444
+ event.xclient.data.l[1] = 1; /* source indication: application */
445
+ XSendEvent(x11->dpy, x11->root, False, SubstructureRedirectMask | SubstructureNotifyMask, &event);
446
+ XFlush(x11->dpy);
447
+ gufa_log("x11: 同步工作区 desktop=%lu target=0x%lx own=0x%lx", desktop,
448
+ (unsigned long)target, (unsigned long)own);
449
+ }
450
+
451
+ void gufa_x11_hide_windows_like_dsh(GufaX11 *x11, long owner_pid, const char *title_hint, GufaX11WindowList *hidden) {
452
+ gufa_x11_window_list_clear(hidden);
453
+ if (x11 == NULL || !x11->ok || hidden == NULL) return;
454
+ GArray *windows = gufa_x11_enumerate(x11, 0, FALSE);
455
+ GArray *matched = g_array_new(FALSE, FALSE, sizeof(Window));
456
+ /* 第一轮:精确 owner pid */
457
+ if (owner_pid > 0) {
458
+ for (guint i = 0; i < windows->len; i++) {
459
+ GufaX11WindowInfo *info = &g_array_index(windows, GufaX11WindowInfo, i);
460
+ if (info->pid != owner_pid) continue;
461
+ if (info->pid == x11->self_pid) continue;
462
+ if (info->width < 200 || info->height < 140) continue;
463
+ if (!gufa_x11_window_type_followable(x11, info->win)) continue;
464
+ Window window = info->win;
465
+ g_array_append_val(matched, window);
466
+ }
467
+ }
468
+ /* 第二轮:标题 / WM_CLASS 启发式(ownerPid 对不上时兜底,DSH 是 Electron 应用) */
469
+ if (matched->len == 0) {
470
+ const char *needles[] = {"deepseek harness", "deepseek-harness", "deepseek"};
471
+ for (guint i = 0; i < windows->len; i++) {
472
+ GufaX11WindowInfo *info = &g_array_index(windows, GufaX11WindowInfo, i);
473
+ if (info->pid == x11->self_pid) continue;
474
+ if (info->width < 200 || info->height < 140) continue;
475
+ if (!gufa_x11_window_type_followable(x11, info->win)) continue;
476
+ char *title = gufa_x11_window_title(x11, info->win);
477
+ char *wm_class = gufa_x11_window_class(x11, info->win);
478
+ char *comm = gufa_x11_process_comm(info->pid);
479
+ gboolean matched_title = FALSE;
480
+ if (title_hint != NULL && *title_hint != '\0' && gufa_text_contains_ci(title, title_hint)) {
481
+ matched_title = TRUE;
482
+ }
483
+ for (gsize n = 0; n < G_N_ELEMENTS(needles) && !matched_title; n++) {
484
+ if (gufa_text_contains_ci(title, needles[n]) || gufa_text_contains_ci(wm_class, needles[n]) ||
485
+ gufa_text_contains_ci(comm, needles[n])) {
486
+ matched_title = TRUE;
487
+ }
488
+ }
489
+ if (matched_title) {
490
+ if (matched->len >= 16) {
491
+ g_free(title);
492
+ g_free(wm_class);
493
+ g_free(comm);
494
+ break;
495
+ }
496
+ Window window = info->win;
497
+ g_array_append_val(matched, window);
498
+ }
499
+ g_free(title);
500
+ g_free(wm_class);
501
+ g_free(comm);
502
+ }
503
+ }
504
+ g_array_free(windows, TRUE);
505
+ if (matched->len > 16) g_array_set_size(matched, 16);
506
+ for (guint i = 0; i < matched->len; i++) {
507
+ Window window = g_array_index(matched, Window, i);
508
+ XUnmapWindow(x11->dpy, window);
509
+ gufa_x11_window_list_add(hidden, window);
510
+ gufa_log("x11: 隐藏 DSH 主窗口 0x%lx", (unsigned long)window);
511
+ }
512
+ if (matched->len == 0) {
513
+ gufa_log("x11: 没找到 DSH 主窗口(ownerPid=%ld),跳过隐藏", owner_pid);
514
+ } else {
515
+ gufa_log("x11: 已隐藏 DSH 主窗口 %u 个", hidden->n);
516
+ }
517
+ XSync(x11->dpy, False);
518
+ g_array_free(matched, TRUE);
519
+ }
520
+
521
+ void gufa_x11_restore_windows(GufaX11 *x11, GufaX11WindowList *hidden, guint32 timestamp) {
522
+ if (hidden == NULL || hidden->n == 0) return;
523
+ if (x11 == NULL || !x11->ok) {
524
+ gufa_x11_window_list_clear(hidden);
525
+ return;
526
+ }
527
+ for (guint i = 0; i < hidden->n; i++) {
528
+ Window window = hidden->wins[i];
529
+ if (!gufa_x11_window_exists(x11, window)) continue;
530
+ XMapWindow(x11->dpy, window);
531
+ XRaiseWindow(x11->dpy, window);
532
+ }
533
+ /* 映射请求先入队,再发 _NET_ACTIVE_WINDOW;X 请求按客户端顺序处理 */
534
+ gufa_x11_activate_window(x11, hidden->wins[0], timestamp);
535
+ XSync(x11->dpy, False);
536
+ gufa_log("x11: 已恢复 DSH 主窗口 %u 个", hidden->n);
537
+ gufa_x11_window_list_clear(hidden);
538
+ }
539
+
540
+ /* ── --list-windows 模式 ────────────────────────────────────────────── */
541
+
542
+ static char *gufa_x11_process_comm(long pid) {
543
+ char path[64];
544
+ snprintf(path, sizeof(path), "/proc/%ld/comm", pid);
545
+ char *contents = NULL;
546
+ gsize length = 0;
547
+ if (!g_file_get_contents(path, &contents, &length, NULL) || contents == NULL) return NULL;
548
+ g_strchomp(contents);
549
+ return contents;
550
+ }
551
+
552
+ static void gufa_x11_app_destroy(gpointer data) {
553
+ GufaX11App *app = (GufaX11App *)data;
554
+ if (app == NULL) return;
555
+ g_free(app->title);
556
+ g_free(app->owner);
557
+ g_free(app->wm_class);
558
+ g_free(app);
559
+ }
560
+
561
+ static gint gufa_x11_app_compare(gconstpointer a, gconstpointer b) {
562
+ const GufaX11App *left = *(const GufaX11App *const *)a;
563
+ const GufaX11App *right = *(const GufaX11App *const *)b;
564
+ if (left->frontmost != right->frontmost) return left->frontmost ? -1 : 1;
565
+ return g_utf8_collate(left->title != NULL ? left->title : "", right->title != NULL ? right->title : "");
566
+ }
567
+
568
+ GPtrArray *gufa_x11_list_apps(GufaX11 *x11) {
569
+ GPtrArray *apps = g_ptr_array_new_with_free_func(gufa_x11_app_destroy);
570
+ if (x11 == NULL || !x11->ok) return apps;
571
+ GArray *windows = gufa_x11_enumerate(x11, 0, TRUE);
572
+ Window active = gufa_x11_active_window(x11);
573
+ long active_pid = active != 0 ? gufa_x11_window_pid(x11, active) : 0;
574
+ GHashTable *seen = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
575
+ for (guint i = 0; i < windows->len; i++) {
576
+ GufaX11WindowInfo *info = &g_array_index(windows, GufaX11WindowInfo, i);
577
+ if (info->pid == x11->self_pid) continue;
578
+ if (info->width < 200 || info->height < 140) continue;
579
+ char *title = gufa_x11_window_title(x11, info->win);
580
+ if (title == NULL || *title == '\0') {
581
+ g_free(title);
582
+ continue;
583
+ }
584
+ char *key = g_strdup_printf("%ld:%s", info->pid, title);
585
+ if (g_hash_table_contains(seen, key)) {
586
+ g_free(key);
587
+ g_free(title);
588
+ continue;
589
+ }
590
+ g_hash_table_add(seen, key);
591
+ GufaX11App *app = g_new0(GufaX11App, 1);
592
+ app->win = info->win;
593
+ app->pid = info->pid;
594
+ app->title = title;
595
+ app->owner = gufa_x11_process_comm(info->pid);
596
+ app->wm_class = gufa_x11_window_class(x11, info->win);
597
+ app->frontmost = active != 0 && (info->win == active || (active_pid != 0 && info->pid == active_pid));
598
+ g_ptr_array_add(apps, app);
599
+ }
600
+ g_hash_table_destroy(seen);
601
+ g_array_free(windows, TRUE);
602
+ g_ptr_array_sort(apps, gufa_x11_app_compare);
603
+ return apps;
604
+ }
605
+
606
+ void gufa_x11_apps_free(GPtrArray *apps) {
607
+ if (apps != NULL) g_ptr_array_unref(apps);
608
+ }