-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow-manager.c
More file actions
executable file
·462 lines (395 loc) · 15.5 KB
/
Copy pathwindow-manager.c
File metadata and controls
executable file
·462 lines (395 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <sys/wait.h>
#include <unistd.h>
#include "window-manager.h"
#define MAX_WINDOWS 100
int scan_all_windows(Window **windows) {
Display *display = XOpenDisplay(NULL);
if (!display) return 0;
Window root = DefaultRootWindow(display);
Atom net_client_list = XInternAtom(display, "_NET_CLIENT_LIST", False);
Atom actual_type;
int actual_format;
unsigned long nitems, bytes_after;
unsigned char *data = NULL;
int status = XGetWindowProperty(display, root, net_client_list,
0, ~0L, False, XA_WINDOW,
&actual_type, &actual_format,
&nitems, &bytes_after, &data);
if (status != Success || data == NULL) {
XCloseDisplay(display);
return 0;
}
Window *client_list = (Window *)data;
int count = (nitems < MAX_WINDOWS) ? nitems : MAX_WINDOWS;
*windows = malloc(count * sizeof(Window));
if (!*windows) {
XFree(data);
XCloseDisplay(display);
return 0;
}
for (int i = 0; i < count; i++) {
(*windows)[i] = client_list[i];
}
XFree(data);
XCloseDisplay(display);
return count;
}
void free_window_list(Window *windows) {
free(windows);
}
/* Convert Nautilus window titles to full paths */
char* nautilus_path_lookup(Display *display, Window window, char *title) {
/* Try to get window object path via D-Bus */
Atom atom = XInternAtom(display, "_GTK_WINDOW_OBJECT_PATH", False);
Atom actual_type;
int actual_format;
unsigned long nitems, bytes_after;
unsigned char *data = NULL;
int status = XGetWindowProperty(display, window, atom,
0, ~0L, False, AnyPropertyType,
&actual_type, &actual_format,
&nitems, &bytes_after, &data);
if (status == Success && data != NULL && nitems > 0) {
char *window_path = (char*)data;
/* Try to query D-Bus for window information */
/* Note: This would require GIO integration which may be complex */
/* For now, we'll use window_path if it contains useful info */
/* Check if window_path contains a window number */
if (strstr(window_path, "/window/")) {
/* Extract window number */
char *window_num = strrchr(window_path, '/') + 1;
if (window_num) {
/* We have the window number, we could use it to query D-Bus */
/* But since we don't have GIO in the build, we'll use fallback */
}
}
XFree(data);
}
/* Map common Nautilus folder names to full paths */
if (strcmp(title, "主文件夹") == 0) {
free(title);
return strdup("/home/zsx");
}
else if (strcmp(title, "下载") == 0) {
free(title);
return strdup("/home/zsx/下载");
}
else if (strcmp(title, "Desktop") == 0 || strcmp(title, "桌面") == 0) {
free(title);
return strdup("/home/zsx/桌面");
}
else if (strcmp(title, "Documents") == 0 || strcmp(title, "文档") == 0) {
free(title);
return strdup("/home/zsx/文档");
}
else if (strcmp(title, "Pictures") == 0 || strcmp(title, "图片") == 0) {
free(title);
return strdup("/home/zsx/图片");
}
else if (strcmp(title, "Videos") == 0 || strcmp(title, "视频") == 0) {
free(title);
return strdup("/home/zsx/视频");
}
else if (strcmp(title, "Music") == 0 || strcmp(title, "音乐") == 0) {
free(title);
return strdup("/home/zsx/音乐");
}
else if (strcmp(title, "Templates") == 0 || strcmp(title, "模板") == 0) {
free(title);
return strdup("/home/zsx/模板");
}
else if (strcmp(title, "Public") == 0 || strcmp(title, "公共") == 0) {
free(title);
return strdup("/home/zsx/公共");
}
/* If no match found, check if title already looks like a path */
if (title[0] == '/') {
/* Already a path, return as-is */
return title;
}
/* If no match found, return original title */
return title;
}
char* get_window_title(Display *display, Window window) {
XTextProperty prop;
char **list = NULL;
int count = 0;
char *title = NULL;
/* Try to get _NET_WM_NAME first (more detailed info) */
Atom net_wm_name = XInternAtom(display, "_NET_WM_NAME", False);
if (XGetTextProperty(display, window, &prop, net_wm_name)) {
Status status = Xutf8TextPropertyToTextList(display, &prop, &list, &count);
if (status < Success || count <= 0 || !list[0]) {
status = XmbTextPropertyToTextList(display, &prop, &list, &count);
}
if (count > 0 && list[0]) {
title = strdup(list[0]);
XFreeStringList(list);
XFree(prop.value);
/* Check if this is a Nautilus window and convert paths */
XClassHint hint;
if (XGetClassHint(display, window, &hint) && hint.res_class) {
if (strcmp(hint.res_class, "org.gnome.Nautilus") == 0) {
title = nautilus_path_lookup(display, window, title);
}
XFree(hint.res_class);
if (hint.res_name) XFree(hint.res_name);
}
return title;
}
XFree(prop.value);
}
/* Fallback to regular WM_NAME */
if (XGetWMName(display, window, &prop)) {
/* Try UTF-8 first, fallback to locale-specific */
Status status = Xutf8TextPropertyToTextList(display, &prop, &list, &count);
if (status < Success || count <= 0 || !list[0]) {
/* Fallback to XmbTextPropertyToTextList */
status = XmbTextPropertyToTextList(display, &prop, &list, &count);
}
if (count > 0 && list[0]) {
title = strdup(list[0]);
XFreeStringList(list);
XFree(prop.value);
/* Check if this is a Nautilus window and convert paths */
XClassHint hint;
if (XGetClassHint(display, window, &hint) && hint.res_class) {
if (strcmp(hint.res_class, "org.gnome.Nautilus") == 0) {
title = nautilus_path_lookup(display, window, title);
}
XFree(hint.res_class);
if (hint.res_name) XFree(hint.res_name);
}
return title;
}
XFree(prop.value);
}
return strdup("(Untitled)");
}
char* get_window_class(Display *display, Window window) {
XClassHint hint;
char *class_name = "(Unknown)";
if (XGetClassHint(display, window, &hint)) {
if (hint.res_class) {
class_name = strdup(hint.res_class);
XFree(hint.res_class);
}
if (hint.res_name) {
XFree(hint.res_name);
}
}
return class_name;
}
int get_window_state(Display *display, Window window) {
/* First check if window exists */
XWindowAttributes attrs;
if (!XGetWindowAttributes(display, window, &attrs)) {
return STATE_NOT_RUNNING; /* Window doesn't exist */
}
/* X server can return success for a stale XID whose backing object
* already went away. The attributes then report 1x1 0-depth — a
* tell that the anchor is pointing at a dead resource. Without this
* check, --run-app happily calls minimize_window on a phantom and
* the user sees "I pressed Ctrl+F10 but nothing happened". */
if (attrs.width < 50 || attrs.height < 50 || attrs.depth == 0) {
return STATE_NOT_RUNNING;
}
Atom net_wm_state = XInternAtom(display, "_NET_WM_STATE", False);
Atom net_wm_state_hidden = XInternAtom(display, "_NET_WM_STATE_HIDDEN", False);
Atom actual_type;
int actual_format;
unsigned long nitems, bytes_after;
unsigned char *data = NULL;
int status = XGetWindowProperty(display, window, net_wm_state,
0, ~0L, False, XA_ATOM,
&actual_type, &actual_format,
&nitems, &bytes_after, &data);
if (status != Success || data == NULL) {
return STATE_VISIBLE;
}
Atom *states = (Atom *)data;
int is_hidden = 0;
for (unsigned long i = 0; i < nitems; i++) {
if (states[i] == net_wm_state_hidden) {
is_hidden = 1;
break;
}
}
XFree(data);
return is_hidden ? STATE_HIDDEN : STATE_VISIBLE;
}
void launch_application(const char *app_name) {
fprintf(stderr, "Launching: %s\n", app_name);
fflush(stderr);
pid_t pid = fork();
if (pid == 0) {
setsid();
execlp(app_name, app_name, NULL);
perror("exec failed");
exit(1);
} else if (pid < 0) {
perror("fork failed");
}
}
int is_window_on_top(Display *display, Window window) {
/* target 窗口是普通窗口里"最顶上"那个吗?
*
* _NET_CLIENT_LIST_STACKING 是 bottom→top 的 stacking 顺序。
* 越靠后 = 视觉上越靠前。mutter 把 _NET_WM_WINDOW_TYPE_DESKTOP
* 永远放最前 (stack 头部),普通窗口怎么 raise 也越不过去 —— 所以
* 普通窗口的"最顶"位置是 stack 末端,跳过 DESKTOP / DOCK / SPLASH
* 之后的最后一个。
*
* 实现:从 stack 末尾倒着扫,跳过系统窗口,第一个普通窗口如果不是
* target,target 就被压在底下。 */
Window root = DefaultRootWindow(display);
Atom stacking = XInternAtom(display, "_NET_CLIENT_LIST_STACKING", False);
Atom wm_window_type = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
Atom desktop_type = XInternAtom(display, "_NET_WM_WINDOW_TYPE_DESKTOP", False);
Atom dock_type = XInternAtom(display, "_NET_WM_WINDOW_TYPE_DOCK", False);
Atom splash_type = XInternAtom(display, "_NET_WM_WINDOW_TYPE_SPLASH", False);
Atom actual_type;
int actual_format;
unsigned long nitems, bytes_after;
unsigned char *data = NULL;
if (XGetWindowProperty(display, root, stacking, 0, ~0L, False, XA_WINDOW,
&actual_type, &actual_format,
&nitems, &bytes_after, &data) != Success || !data) {
return 0;
}
Window *list = (Window *)data;
int on_top = 0;
/* stack 是 bottom→top,从后往前扫找普通窗口里的最顶 */
long i;
for (i = (long)nitems - 1; i >= 0; i--) {
Window w = list[i];
Atom a_type;
int a_format;
unsigned long a_nitems, a_after;
unsigned char *a_data = NULL;
if (XGetWindowProperty(display, w, wm_window_type, 0, ~0L, False, XA_ATOM,
&a_type, &a_format, &a_nitems, &a_after, &a_data) == Success
&& a_data) {
Atom *types = (Atom *)a_data;
int is_system = 0;
for (unsigned long j = 0; j < a_nitems; j++) {
if (types[j] == desktop_type || types[j] == dock_type
|| types[j] == splash_type) {
is_system = 1;
break;
}
}
XFree(a_data);
if (is_system) continue; /* 系统窗口跳过,看更前面的 */
}
/* 这是 stack 里最顶的普通窗口,看是不是 target */
on_top = (w == window);
break;
}
XFree(data);
return on_top;
}
void raise_window(Display *display, Window window) {
/* 窗口被压在底下时把它抢到焦点并让用户视觉上看到它跳到前面。
* mutter 的策略:
* - _NET_ACTIVE_WINDOW source=1 (app request): mutter 只给焦点不 raise,看起来无效果
* - _NET_ACTIVE_WINDOW source=2 (pager): mutter 会同时 raise + 给焦点
* - XRaiseWindow 单独发不行,mutter 把它当作 "app 想偷焦点" 拒绝
* - XSetInputFocus 单独发也不行,mutter focus-stealing 防护拦下
* 三连 (XRaiseWindow + source=2 + XSetInputFocus) 实际只需要 source=2 就够了
* —— 多发的两条反而会触发 mutter 防护,焦点夺不到。所以只发 source=2。 */
fprintf(stderr, "Raising window: 0x%lx\n", window);
fflush(stderr);
Window root = DefaultRootWindow(display);
Atom net_active = XInternAtom(display, "_NET_ACTIVE_WINDOW", False);
XEvent event;
memset(&event, 0, sizeof(event));
event.xclient.type = ClientMessage;
event.xclient.send_event = True;
event.xclient.window = window;
event.xclient.message_type = net_active;
event.xclient.format = 32;
event.xclient.data.l[0] = 2; /* source = pager,mutter 同时 raise + 给焦点 */
event.xclient.data.l[1] = CurrentTime;
event.xclient.data.l[2] = 0;
XSendEvent(display, root, False,
SubstructureRedirectMask | SubstructureNotifyMask, &event);
XFlush(display);
/* mutter 处理 _NET_ACTIVE_WINDOW 是异步的,睡眠 50ms 让它完成 raise/激活,
* 否则调用方立刻读 _NET_ACTIVE_WINDOW 会读到旧的焦点窗口。 */
usleep(50 * 1000);
}
void minimize_window(Display *display, Window window) {
fprintf(stderr, "Minimizing window: 0x%lx\n", window);
fflush(stderr);
XIconifyWindow(display, window, DefaultScreen(display));
XFlush(display);
}
void activate_window(Display *display, Window window) {
/* STATE_HIDDEN → STATE_VISIBLE 的恢复动作。
* mutter 的策略: _NET_ACTIVE_WINDOW source=1 只给焦点不 raise,
* source=2 (pager) 才会同时 raise + 给焦点 + unmappped 也能恢复显示。
* 用 source=2 是为了让"按一次 F1 看到窗口"的需求成立。 */
fprintf(stderr, "Activating window: 0x%lx\n", window);
fflush(stderr);
Window root = DefaultRootWindow(display);
Atom net_active_window = XInternAtom(display, "_NET_ACTIVE_WINDOW", False);
XEvent event;
memset(&event, 0, sizeof(event));
event.xclient.type = ClientMessage;
event.xclient.serial = 0;
event.xclient.send_event = True;
event.xclient.window = window;
event.xclient.message_type = net_active_window;
event.xclient.format = 32;
event.xclient.data.l[0] = 2; /* source = pager,mutter 同时 raise + 给焦点 */
event.xclient.data.l[1] = CurrentTime;
event.xclient.data.l[2] = 0;
XSendEvent(display, root, False,
SubstructureRedirectMask | SubstructureNotifyMask,
&event);
XSync(display, False);
/* mutter 处理 _NET_ACTIVE_WINDOW 是异步的,等 50ms 让它完成 raise/激活。
* 否则调用方立刻读 _NET_ACTIVE_WINDOW 会读到旧焦点。 */
usleep(50 * 1000);
}
WindowState read_state_file() {
FILE *fp = fopen("/tmp/window-toggle-state", "r");
if (!fp) return STATE_NOT_RUNNING;
int state = STATE_NOT_RUNNING;
if (fscanf(fp, "%d", &state) != 1) {
fclose(fp);
return STATE_NOT_RUNNING;
}
fclose(fp);
return (WindowState)state;
}
void write_state_file(WindowState state) {
FILE *fp = fopen("/tmp/window-toggle-state", "w");
if (!fp) return;
fprintf(fp, "%d", state);
fclose(fp);
}
Window read_active_window() {
FILE *fp = fopen("/tmp/window-toggle-active", "r");
if (!fp) return 0;
Window window_id = 0;
if (fscanf(fp, "%lu", &window_id) != 1) {
fclose(fp);
return 0;
}
fclose(fp);
return window_id;
}
void write_active_window(Window window) {
FILE *fp = fopen("/tmp/window-toggle-active", "w");
if (!fp) return;
fprintf(fp, "%lu", window);
fclose(fp);
}