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,1184 @@
1
+ /**
2
+ * 古法编程 · Linux 悬浮层 helper(X11 + GTK3 + WebKit2GTK)
3
+ *
4
+ * 由 DSH Host 通过 `spawn(gufa-float-linux, [payloadJson])` 启动;
5
+ * 协议见 desktop/float/PLATFORM-PROTOCOL.md,行为参考 desktop/float/GufaFloat.m(macOS)。
6
+ *
7
+ * 能力:
8
+ * - 无边框 / 置顶 / 可缩放窗口 + WebKit2GTK 内嵌 DSH Web GUI;
9
+ * - RGBA visual 透明(有合成器时);
10
+ * - cookie 或 authUrl 两条鉴权路径;
11
+ * - document-start 注入 localStorage(gufa-code:float / float-state / project / ui:v1);
12
+ * - 顶部 22px 拖动条(gtk_window_begin_move_drag)、右下角缩放手柄;
13
+ * - follow:250ms 轮询 X11 目标窗口(_NET_WM_PID),移动/缩放自己的窗口;
14
+ * - 启动 0.45s 后隐藏 DSH 主窗口(XUnmapWindow),退出时 XMapWindow + _NET_ACTIVE_WINDOW 恢复;
15
+ * - controlPath 300ms 轮询退出(退出前恢复 DSH 主窗口);
16
+ * - ciProbe 冒烟模式(stdout 打 JSON 后退出 0);
17
+ * - Wayland 会话明确降级:仅自由悬浮,不跟随、不隐藏主窗口。
18
+ */
19
+ #include <errno.h>
20
+ #include <signal.h>
21
+ #include <stdio.h>
22
+ #include <stdlib.h>
23
+ #include <string.h>
24
+ #include <unistd.h>
25
+
26
+ #include <glib-unix.h>
27
+ #include <glib.h>
28
+ #include <glib/gstdio.h>
29
+ #include <gtk/gtk.h>
30
+
31
+ #include <gdk/gdk.h>
32
+ #ifdef GDK_WINDOWING_X11
33
+ #include <gdk/gdkx.h>
34
+ #define GUFA_HAVE_GDK_X11 1
35
+ #else
36
+ #define GUFA_HAVE_GDK_X11 0
37
+ #endif
38
+
39
+ #include <jsc/jsc.h>
40
+ #include <libsoup/soup.h>
41
+ #include <webkit2/webkit2.h>
42
+
43
+ #include "json.h"
44
+ #include "log.h"
45
+ #include "x11.h"
46
+
47
+ #define GUFA_FLOAT_VERSION "1.0.0"
48
+ #define GUFA_DRAG_STRIP_HEIGHT 22
49
+ #define GUFA_DEFAULT_WIDTH 1180
50
+ #define GUFA_DEFAULT_HEIGHT 820
51
+ #define GUFA_MIN_WIDTH 320
52
+ #define GUFA_MIN_HEIGHT 240
53
+ #define GUFA_DSH_TITLE_HINT "DeepSeek Harness"
54
+ #define GUFA_MAX_HIDDEN_DSH_WINDOWS 16
55
+
56
+ typedef enum {
57
+ GUFA_STAGE_NONE = 0,
58
+ GUFA_STAGE_COOKIE,
59
+ GUFA_STAGE_AUTH,
60
+ GUFA_STAGE_APP
61
+ } GufaStage;
62
+
63
+ typedef struct _GufaApp GufaApp;
64
+
65
+ typedef struct {
66
+ GufaApp *app;
67
+ char *label;
68
+ } GufaJsRequest;
69
+
70
+ struct _GufaApp {
71
+ /* ── payload ── */
72
+ GufaJson *payload;
73
+ char *app_url;
74
+ char *auth_url;
75
+ char *cookie;
76
+ char *control_path;
77
+ char *build;
78
+ char *selftest;
79
+ char *follow_title;
80
+ char *bootstrap_js;
81
+ long follow_pid;
82
+ long owner_pid;
83
+ int alpha;
84
+ gboolean ci_probe;
85
+
86
+ /* ── GTK / WebKit ── */
87
+ GtkWidget *window;
88
+ GtkWidget *webview;
89
+ GtkWidget *overlay;
90
+ GtkWidget *menu;
91
+ WebKitUserContentManager *content_manager;
92
+ gboolean composited;
93
+ gboolean transparent;
94
+ gboolean gdk_is_x11;
95
+
96
+ /* ── X11 ── */
97
+ GufaX11 x11;
98
+ gboolean x11_ok;
99
+ Window own_xid;
100
+ Window follow_win;
101
+ GufaX11WindowList hidden_dsh;
102
+ gboolean follow_found_logged;
103
+ gboolean follow_missing_logged;
104
+ gboolean follow_minimized_logged;
105
+ int follow_apply_logs;
106
+
107
+ /* ── 状态 ── */
108
+ gboolean wayland_session;
109
+ pid_t host_pid;
110
+ gboolean watch_host;
111
+ GufaStage stage;
112
+ gboolean loaded;
113
+ gboolean loading_failed;
114
+ gboolean probe_done;
115
+ gboolean quitting;
116
+ int exit_code;
117
+ int applied_x;
118
+ int applied_y;
119
+ int applied_width;
120
+ int applied_height;
121
+ gboolean have_applied;
122
+
123
+ /* ── 定时器 ── */
124
+ guint control_timer;
125
+ guint follow_timer;
126
+ guint hide_timer;
127
+ guint probe_guard;
128
+ guint page_probe_timer;
129
+ guint selftest_timer;
130
+ guint selftest_snapshot_timer;
131
+ };
132
+
133
+ static guint32 gufa_server_time(GufaApp *app);
134
+ static void gufa_start_load(GufaApp *app);
135
+ static void gufa_load_app_url(GufaApp *app);
136
+ static void gufa_hide_dsh(GufaApp *app);
137
+ static void gufa_restore_dsh(GufaApp *app);
138
+ static void gufa_request_quit(GufaApp *app, const char *reason);
139
+ static void gufa_apply_follow_rect(GufaApp *app, const GufaX11Rect *target);
140
+ static void gufa_save_frame(GufaApp *app);
141
+
142
+ /* ── 通用小工具 ─────────────────────────────────────────────────────── */
143
+
144
+ static char *gufa_append_query(const char *url, const char *query) {
145
+ if (url == NULL) return NULL;
146
+ if (query == NULL || *query == '\0') return g_strdup(url);
147
+ return g_strdup_printf("%s%s%s", url, strchr(url, '?') != NULL ? "&" : "?", query);
148
+ }
149
+
150
+ /** 从 URL 里取 host(给 cookie 用),失败返回 NULL。 */
151
+ static char *gufa_url_host(const char *url) {
152
+ if (url == NULL) return NULL;
153
+ const char *start = strstr(url, "://");
154
+ start = start != NULL ? start + 3 : url;
155
+ const char *end = strpbrk(start, ":/?#");
156
+ size_t length = end != NULL ? (size_t)(end - start) : strlen(start);
157
+ if (length == 0) return NULL;
158
+ return g_strndup(start, length);
159
+ }
160
+
161
+ static guint32 gufa_server_time(GufaApp *app) {
162
+ #if GUFA_HAVE_GDK_X11
163
+ if (app != NULL && app->gdk_is_x11 && app->window != NULL) {
164
+ GdkWindow *gdk_window = gtk_widget_get_window(app->window);
165
+ if (gdk_window != NULL) {
166
+ return (guint32)gdk_x11_get_server_time(gdk_window);
167
+ }
168
+ }
169
+ #else
170
+ (void)app;
171
+ #endif
172
+ return 0;
173
+ }
174
+
175
+ static Window gufa_own_xid_from_widget(GufaApp *app) {
176
+ #if GUFA_HAVE_GDK_X11
177
+ if (app != NULL && app->gdk_is_x11 && app->window != NULL) {
178
+ GdkWindow *gdk_window = gtk_widget_get_window(app->window);
179
+ if (gdk_window != NULL) return (Window)GDK_WINDOW_XID(gdk_window);
180
+ }
181
+ #else
182
+ (void)app;
183
+ #endif
184
+ return 0;
185
+ }
186
+
187
+ static void gufa_remove_timer(guint *timer) {
188
+ if (timer != NULL && *timer != 0) {
189
+ g_source_remove(*timer);
190
+ *timer = 0;
191
+ }
192
+ }
193
+
194
+ /* ── payload / 注入脚本 ─────────────────────────────────────────────── */
195
+
196
+ static char *gufa_build_bootstrap_js(const GufaJson *payload) {
197
+ GString *js = g_string_new("try{localStorage.setItem('gufa-code:float','1');");
198
+ GufaJson *state = gufa_json_object_get(payload, "state");
199
+ if (state != NULL && gufa_json_get_type(state) == GUFA_JSON_OBJECT) {
200
+ char *state_json = gufa_json_serialize(state);
201
+ if (state_json != NULL) {
202
+ /* 把 state 作为 JS 对象字面量内联(JSON 是 JS 语法的子集),再 JSON.stringify 成字符串写进 localStorage */
203
+ g_string_append(js, "var s=");
204
+ g_string_append(js, state_json);
205
+ g_string_append(js,
206
+ ";localStorage.setItem('gufa-code:float-state',JSON.stringify(s));"
207
+ "if(s&&typeof s.projectRoot==='string'&&s.projectRoot){localStorage.setItem('gufa-code:project',JSON.stringify(s.projectRoot));}"
208
+ "if(s&&s.ui&&typeof s.ui==='object'){localStorage.setItem('gufa-code:ui:v1',JSON.stringify(s.ui));}");
209
+ g_free(state_json);
210
+ }
211
+ }
212
+ g_string_append(js, "}catch(e){}");
213
+ return g_string_free(js, FALSE);
214
+ }
215
+
216
+ static gboolean gufa_payload_fill(GufaApp *app, GufaJson *payload) {
217
+ app->payload = payload;
218
+ app->app_url = gufa_json_dup_string(gufa_json_object_get(payload, "appUrl"));
219
+ app->auth_url = gufa_json_dup_string(gufa_json_object_get(payload, "authUrl"));
220
+ app->cookie = gufa_json_dup_string(gufa_json_object_get(payload, "cookie"));
221
+ app->control_path = gufa_json_dup_string(gufa_json_object_get(payload, "controlPath"));
222
+ app->build = gufa_json_dup_string(gufa_json_object_get(payload, "build"));
223
+ app->selftest = gufa_json_dup_string(gufa_json_object_get(payload, "selftest"));
224
+ app->alpha = (int)gufa_json_get_int64(gufa_json_object_get(payload, "alpha"), 55);
225
+ app->ci_probe = gufa_json_get_bool(gufa_json_object_get(payload, "ciProbe"), FALSE);
226
+ app->owner_pid = gufa_json_get_int64(gufa_json_object_get(payload, "ownerPid"), 0);
227
+ GufaJson *follow = gufa_json_object_get(payload, "follow");
228
+ if (follow != NULL && gufa_json_get_type(follow) == GUFA_JSON_OBJECT) {
229
+ app->follow_pid = gufa_json_get_int64(gufa_json_object_get(follow, "pid"), 0);
230
+ app->follow_title = gufa_json_dup_string(gufa_json_object_get(follow, "title"));
231
+ }
232
+ app->bootstrap_js = gufa_build_bootstrap_js(payload);
233
+ if (app->app_url == NULL || *app->app_url == '\0') {
234
+ return FALSE;
235
+ }
236
+ if (app->auth_url == NULL || *app->auth_url == '\0') {
237
+ app->auth_url = g_strdup(app->app_url);
238
+ }
239
+ return TRUE;
240
+ }
241
+
242
+ /* ── 环境 / 后端 ────────────────────────────────────────────────────── */
243
+
244
+ static gboolean gufa_configure_backend(GufaApp *app) {
245
+ const char *wayland_display = g_getenv("WAYLAND_DISPLAY");
246
+ const char *x11_display = g_getenv("DISPLAY");
247
+ gboolean has_wayland = wayland_display != NULL && *wayland_display != '\0';
248
+ gboolean has_x11 = x11_display != NULL && *x11_display != '\0';
249
+ if (!has_wayland && !has_x11) {
250
+ g_printerr("gufa-float-linux: 没有可用的图形显示(DISPLAY / WAYLAND_DISPLAY 均为空)。"
251
+ "Linux 版悬浮层需要 X11 桌面(或 XWayland)。\n");
252
+ return FALSE;
253
+ }
254
+ if (has_wayland) {
255
+ app->wayland_session = TRUE;
256
+ gufa_log("wayland: 检测到 WAYLAND_DISPLAY=%s;Linux helper 仅支持 X11,"
257
+ "Wayland 原生窗口无法被读取位置/隐藏", wayland_display);
258
+ if (has_x11) {
259
+ const char *backend = g_getenv("GDK_BACKEND");
260
+ if (backend == NULL || *backend == '\0') {
261
+ g_setenv("GDK_BACKEND", "x11", TRUE);
262
+ gufa_log("wayland: 已优先切到 X11/XWayland 后端(GDK_BACKEND=x11),"
263
+ "可跟随/隐藏 X11 应用窗口;Wayland 原生应用仍不可见");
264
+ } else if (g_ascii_strcasecmp(backend, "wayland") == 0) {
265
+ gufa_log("wayland: 用户显式指定 GDK_BACKEND=wayland:follow 与隐藏主窗口降级为自由悬浮");
266
+ }
267
+ }
268
+ }
269
+ return TRUE;
270
+ }
271
+
272
+ static void gufa_prepare_probe_env(GufaApp *app) {
273
+ if (!app->ci_probe) return;
274
+ /* CI (xvfb-run) 没有 GPU/合成器,给 WebKit 明确的软件渲染兜底;这些变量只在 ciProbe 模式自动设置,
275
+ 真机不受影响(真机若遇白屏可用同样变量手动启动)。 */
276
+ if (g_getenv("WEBKIT_DISABLE_COMPOSITING_MODE") == NULL) g_setenv("WEBKIT_DISABLE_COMPOSITING_MODE", "1", TRUE);
277
+ if (g_getenv("WEBKIT_DISABLE_DMABUF_RENDERER") == NULL) g_setenv("WEBKIT_DISABLE_DMABUF_RENDERER", "1", TRUE);
278
+ if (g_getenv("LIBGL_ALWAYS_SOFTWARE") == NULL) g_setenv("LIBGL_ALWAYS_SOFTWARE", "1", TRUE);
279
+ if (g_getenv("WEBKIT_DISABLE_SANDBOX_THIS_IS_DANGEROUS") == NULL) {
280
+ g_setenv("WEBKIT_DISABLE_SANDBOX_THIS_IS_DANGEROUS", "1", TRUE);
281
+ }
282
+ }
283
+
284
+ /* ── CSS / 绘制 ─────────────────────────────────────────────────────── */
285
+
286
+ static void gufa_install_css(void) {
287
+ const char *css =
288
+ "window.gufa-float-window { background-color: rgba(0,0,0,0); }\n"
289
+ ".gufa-strip { background-color: rgba(0,0,0,0.30); }\n"
290
+ ".gufa-strip label { color: rgba(255,255,255,0.72); font-size: 10px; }\n";
291
+ GtkCssProvider *provider = gtk_css_provider_new();
292
+ GError *error = NULL;
293
+ if (!gtk_css_provider_load_from_data(provider, css, -1, &error)) {
294
+ gufa_log("css: 加载失败 %s", error != NULL ? error->message : "?");
295
+ g_clear_error(&error);
296
+ }
297
+ GdkScreen *screen = gdk_screen_get_default();
298
+ if (screen != NULL) {
299
+ gtk_style_context_add_provider_for_screen(screen, GTK_STYLE_PROVIDER(provider),
300
+ GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
301
+ }
302
+ g_object_unref(provider);
303
+ }
304
+
305
+ static gboolean gufa_on_window_draw(GtkWidget *widget, cairo_t *cr, gpointer user_data) {
306
+ GufaApp *app = (GufaApp *)user_data;
307
+ if (app == NULL || !app->transparent) return FALSE;
308
+ GtkAllocation allocation;
309
+ gtk_widget_get_allocation(widget, &allocation);
310
+ cairo_save(cr);
311
+ cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
312
+ cairo_set_source_rgba(cr, 0, 0, 0, 0);
313
+ cairo_paint(cr);
314
+ cairo_restore(cr);
315
+ return FALSE;
316
+ }
317
+
318
+ static gboolean gufa_on_grip_draw(GtkWidget *widget, cairo_t *cr, gpointer user_data) {
319
+ (void)user_data;
320
+ GtkAllocation allocation;
321
+ gtk_widget_get_allocation(widget, &allocation);
322
+ double width = allocation.width;
323
+ double height = allocation.height;
324
+ cairo_save(cr);
325
+ cairo_set_source_rgba(cr, 1, 1, 1, 0.55);
326
+ cairo_set_line_width(cr, 1.5);
327
+ cairo_move_to(cr, width - 3, height - 7);
328
+ cairo_line_to(cr, width - 7, height - 3);
329
+ cairo_move_to(cr, width - 3, height - 12);
330
+ cairo_line_to(cr, width - 12, height - 3);
331
+ cairo_stroke(cr);
332
+ cairo_restore(cr);
333
+ return FALSE;
334
+ }
335
+
336
+ /* ── 窗口拖动 / 缩放 / 菜单 ─────────────────────────────────────────── */
337
+
338
+ static gboolean gufa_on_strip_button_press(GtkWidget *widget, GdkEventButton *event, gpointer user_data) {
339
+ (void)widget;
340
+ GufaApp *app = (GufaApp *)user_data;
341
+ if (app == NULL || event == NULL) return FALSE;
342
+ if (event->type != GDK_BUTTON_PRESS) return FALSE;
343
+ if (event->button == 1) {
344
+ gtk_window_begin_move_drag(GTK_WINDOW(app->window), (gint)event->button,
345
+ (gint)event->x_root, (gint)event->y_root, (guint32)event->time);
346
+ return TRUE;
347
+ }
348
+ if (event->button == 3) {
349
+ if (app->menu != NULL) {
350
+ gtk_menu_popup_at_pointer(GTK_MENU(app->menu), (GdkEvent *)event);
351
+ return TRUE;
352
+ }
353
+ }
354
+ return FALSE;
355
+ }
356
+
357
+ static gboolean gufa_on_grip_button_press(GtkWidget *widget, GdkEventButton *event, gpointer user_data) {
358
+ (void)widget;
359
+ GufaApp *app = (GufaApp *)user_data;
360
+ if (app == NULL || event == NULL) return FALSE;
361
+ if (event->type != GDK_BUTTON_PRESS || event->button != 1) return FALSE;
362
+ gtk_window_begin_resize_drag(GTK_WINDOW(app->window), GDK_WINDOW_EDGE_SOUTH_EAST,
363
+ (gint)event->button, (gint)event->x_root, (gint)event->y_root,
364
+ (guint32)event->time);
365
+ return TRUE;
366
+ }
367
+
368
+ static void gufa_on_strip_realize(GtkWidget *widget, gpointer user_data) {
369
+ (void)user_data;
370
+ GdkWindow *gdk_window = gtk_widget_get_window(widget);
371
+ if (gdk_window == NULL) return;
372
+ GdkCursor *cursor = gdk_cursor_new_from_name(gdk_display_get_default(), "grab");
373
+ if (cursor != NULL) {
374
+ gdk_window_set_cursor(gdk_window, cursor);
375
+ g_object_unref(cursor);
376
+ }
377
+ }
378
+
379
+ static void gufa_on_grip_realize(GtkWidget *widget, gpointer user_data) {
380
+ (void)user_data;
381
+ GdkWindow *gdk_window = gtk_widget_get_window(widget);
382
+ if (gdk_window == NULL) return;
383
+ GdkCursor *cursor = gdk_cursor_new_from_name(gdk_display_get_default(), "se-resize");
384
+ if (cursor != NULL) {
385
+ gdk_window_set_cursor(gdk_window, cursor);
386
+ g_object_unref(cursor);
387
+ }
388
+ }
389
+
390
+ static void gufa_center_window(GufaApp *app, int *out_x, int *out_y, int *out_width, int *out_height) {
391
+ GdkRectangle geometry = {0, 0, GUFA_DEFAULT_WIDTH, GUFA_DEFAULT_HEIGHT};
392
+ GdkScreen *screen = app->window != NULL ? gtk_widget_get_screen(app->window) : gdk_screen_get_default();
393
+ if (screen != NULL) {
394
+ gint monitor = gdk_screen_get_primary_monitor(screen);
395
+ if (monitor < 0) monitor = 0;
396
+ if (monitor < gdk_screen_get_n_monitors(screen)) {
397
+ gdk_screen_get_monitor_geometry(screen, monitor, &geometry);
398
+ }
399
+ }
400
+ int width = MIN(GUFA_DEFAULT_WIDTH, (int)(geometry.width * 0.86));
401
+ int height = MIN(GUFA_DEFAULT_HEIGHT, (int)(geometry.height * 0.86));
402
+ if (width < GUFA_MIN_WIDTH) width = MIN(geometry.width, GUFA_MIN_WIDTH);
403
+ if (height < GUFA_MIN_HEIGHT) height = MIN(geometry.height, GUFA_MIN_HEIGHT);
404
+ if (out_x != NULL) *out_x = geometry.x + (geometry.width - width) / 2;
405
+ if (out_y != NULL) *out_y = geometry.y + (geometry.height - height) / 2;
406
+ if (out_width != NULL) *out_width = width;
407
+ if (out_height != NULL) *out_height = height;
408
+ }
409
+
410
+ static gboolean gufa_reset_frame_cb(gpointer user_data) {
411
+ GufaApp *app = (GufaApp *)user_data;
412
+ if (app == NULL || app->window == NULL) return G_SOURCE_REMOVE;
413
+ int x = 0, y = 0, width = 0, height = 0;
414
+ gufa_center_window(app, &x, &y, &width, &height);
415
+ gtk_window_move(GTK_WINDOW(app->window), x, y);
416
+ gtk_window_resize(GTK_WINDOW(app->window), width, height);
417
+ gufa_log("window: 重置位置和大小 %d,%d %dx%d", x, y, width, height);
418
+ return G_SOURCE_REMOVE;
419
+ }
420
+
421
+ static void gufa_on_menu_quit(GtkMenuItem *item, gpointer user_data) {
422
+ (void)item;
423
+ gufa_request_quit((GufaApp *)user_data, "menu");
424
+ }
425
+
426
+ static void gufa_on_menu_reset(GtkMenuItem *item, gpointer user_data) {
427
+ (void)item;
428
+ gufa_reset_frame_cb(user_data);
429
+ }
430
+
431
+ static void gufa_build_menu(GufaApp *app) {
432
+ GtkWidget *menu = gtk_menu_new();
433
+ GtkWidget *exit_item = gtk_menu_item_new_with_label("退出摸鱼(恢复主窗口)");
434
+ g_signal_connect(exit_item, "activate", G_CALLBACK(gufa_on_menu_quit), app);
435
+ gtk_menu_shell_append(GTK_MENU_SHELL(menu), exit_item);
436
+ GtkWidget *reset_item = gtk_menu_item_new_with_label("重置窗口位置和大小");
437
+ g_signal_connect(reset_item, "activate", G_CALLBACK(gufa_on_menu_reset), app);
438
+ gtk_menu_shell_append(GTK_MENU_SHELL(menu), reset_item);
439
+ gtk_menu_shell_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
440
+ char *mode_text = app->follow_pid > 0 ? g_strdup_printf("吸附模式:跟随 pid=%ld", app->follow_pid)
441
+ : g_strdup("自由悬浮模式");
442
+ GtkWidget *mode_item = gtk_menu_item_new_with_label(mode_text);
443
+ gtk_widget_set_sensitive(mode_item, FALSE);
444
+ gtk_menu_shell_append(GTK_MENU_SHELL(menu), mode_item);
445
+ g_free(mode_text);
446
+ gtk_widget_show_all(menu);
447
+ app->menu = menu;
448
+ }
449
+
450
+ /* ── 窗口位置保存/恢复 ──────────────────────────────────────────────── */
451
+
452
+ static char *gufa_frame_file_path(void) {
453
+ char *directory = g_build_filename(g_get_user_config_dir(), "gufa-float", NULL);
454
+ char *path = g_build_filename(directory, "linux-frame.txt", NULL);
455
+ g_free(directory);
456
+ return path;
457
+ }
458
+
459
+ static gboolean gufa_load_saved_frame(int *x, int *y, int *width, int *height) {
460
+ char *path = gufa_frame_file_path();
461
+ char *contents = NULL;
462
+ gboolean ok = FALSE;
463
+ if (g_file_get_contents(path, &contents, NULL, NULL) && contents != NULL) {
464
+ int fx = 0, fy = 0, fw = 0, fh = 0;
465
+ if (sscanf(contents, "%d %d %d %d", &fx, &fy, &fw, &fh) == 4 && fw >= 400 && fh >= 300 &&
466
+ fx > -20000 && fy > -20000 && fx < 20000 && fy < 20000) {
467
+ *x = fx;
468
+ *y = fy;
469
+ *width = fw;
470
+ *height = fh;
471
+ ok = TRUE;
472
+ }
473
+ }
474
+ g_free(contents);
475
+ g_free(path);
476
+ return ok;
477
+ }
478
+
479
+ static void gufa_save_frame(GufaApp *app) {
480
+ if (app == NULL || app->window == NULL || app->ci_probe) return;
481
+ int x = 0, y = 0, width = 0, height = 0;
482
+ gtk_window_get_position(GTK_WINDOW(app->window), &x, &y);
483
+ gtk_window_get_size(GTK_WINDOW(app->window), &width, &height);
484
+ if (width < 200 || height < 200) return;
485
+ char *directory = g_build_filename(g_get_user_config_dir(), "gufa-float", NULL);
486
+ g_mkdir_with_parents(directory, 0700);
487
+ char *path = gufa_frame_file_path();
488
+ char *text = g_strdup_printf("%d %d %d %d\n", x, y, width, height);
489
+ GError *error = NULL;
490
+ if (!g_file_set_contents(path, text, -1, &error)) {
491
+ gufa_log("window: 保存位置失败 %s", error != NULL ? error->message : "?");
492
+ g_clear_error(&error);
493
+ }
494
+ g_free(text);
495
+ g_free(path);
496
+ g_free(directory);
497
+ }
498
+
499
+ /* ── 鉴权 / 加载 ────────────────────────────────────────────────────── */
500
+
501
+ static void gufa_emit_probe_result(GufaApp *app, gboolean url_loaded);
502
+
503
+ static void gufa_on_cookie_added(GObject *source_object, GAsyncResult *result, gpointer user_data) {
504
+ GufaApp *app = (GufaApp *)user_data;
505
+ GError *error = NULL;
506
+ gboolean ok = webkit_cookie_manager_add_cookie_finish(WEBKIT_COOKIE_MANAGER(source_object), result, &error);
507
+ if (!ok) {
508
+ gufa_log("auth: 注入 cookie 失败(%s),回退到 authUrl 登录", error != NULL ? error->message : "?");
509
+ g_clear_error(&error);
510
+ app->stage = GUFA_STAGE_AUTH;
511
+ webkit_web_view_load_uri(WEBKIT_WEB_VIEW(app->webview), app->auth_url);
512
+ return;
513
+ }
514
+ gufa_log("auth: cookie 注入成功");
515
+ gufa_load_app_url(app);
516
+ }
517
+
518
+ static gboolean gufa_add_cookie_async(GufaApp *app) {
519
+ const char *cookie = app->cookie;
520
+ if (cookie == NULL || *cookie == '\0') return FALSE;
521
+ const char *equals = strchr(cookie, '=');
522
+ if (equals == NULL || equals == cookie) {
523
+ gufa_log("auth: cookie 格式不合法(缺少 '='),回退到 authUrl");
524
+ return FALSE;
525
+ }
526
+ char *name = g_strndup(cookie, (gsize)(equals - cookie));
527
+ const char *value = equals + 1;
528
+ char *host = gufa_url_host(app->app_url);
529
+ if (host == NULL) host = g_strdup("127.0.0.1");
530
+ SoupCookie *soup_cookie = soup_cookie_new(name, value, host, "/", 86400);
531
+ gufa_log("auth: 注入 cookie name=%s domain=%s", name, host);
532
+ g_free(name);
533
+ g_free(host);
534
+ if (soup_cookie == NULL) return FALSE;
535
+ WebKitWebContext *context = webkit_web_context_get_default();
536
+ WebKitWebsiteDataManager *data_manager = webkit_web_context_get_website_data_manager(context);
537
+ WebKitCookieManager *cookie_manager = webkit_website_data_manager_get_cookie_manager(data_manager);
538
+ webkit_cookie_manager_add_cookie(cookie_manager, soup_cookie, NULL, gufa_on_cookie_added, app);
539
+ soup_cookie_free(soup_cookie);
540
+ return TRUE;
541
+ }
542
+
543
+ static void gufa_load_app_url(GufaApp *app) {
544
+ char *url = gufa_append_query(app->app_url, "gufa-float=1");
545
+ app->stage = GUFA_STAGE_APP;
546
+ gufa_log("load: appUrl %s", url);
547
+ webkit_web_view_load_uri(WEBKIT_WEB_VIEW(app->webview), url);
548
+ g_free(url);
549
+ }
550
+
551
+ static void gufa_start_load(GufaApp *app) {
552
+ if (app->cookie != NULL && *app->cookie != '\0') {
553
+ app->stage = GUFA_STAGE_COOKIE;
554
+ gufa_log("auth: 使用 Host 传入的 cookie 鉴权");
555
+ if (gufa_add_cookie_async(app)) return;
556
+ gufa_log("auth: cookie 注入不可用,回退到 authUrl 登录");
557
+ }
558
+ app->stage = GUFA_STAGE_AUTH;
559
+ gufa_log("load: authUrl %s", app->auth_url != NULL ? app->auth_url : "(none)");
560
+ webkit_web_view_load_uri(WEBKIT_WEB_VIEW(app->webview), app->auth_url);
561
+ }
562
+
563
+ /* ── 控制文件 / 信号 / 退出 ─────────────────────────────────────────── */
564
+
565
+ static void gufa_restore_dsh(GufaApp *app) {
566
+ if (app == NULL) return;
567
+ if (!app->x11_ok) {
568
+ g_free(app->hidden_dsh.wins);
569
+ app->hidden_dsh.wins = NULL;
570
+ app->hidden_dsh.n = 0;
571
+ return;
572
+ }
573
+ if (app->hidden_dsh.n == 0) return;
574
+ gufa_x11_restore_windows(&app->x11, &app->hidden_dsh, gufa_server_time(app));
575
+ }
576
+
577
+ static void gufa_hide_dsh(GufaApp *app) {
578
+ if (app == NULL || !app->x11_ok) {
579
+ gufa_log("dsh-hide: X11 不可用(Wayland 会话或没有 X 连接),跳过隐藏 DSH 主窗口");
580
+ return;
581
+ }
582
+ gufa_x11_hide_windows_like_dsh(&app->x11, app->owner_pid, GUFA_DSH_TITLE_HINT, &app->hidden_dsh);
583
+ }
584
+
585
+ static void gufa_request_quit(GufaApp *app, const char *reason) {
586
+ if (app == NULL || app->quitting) return;
587
+ app->quitting = TRUE;
588
+ gufa_log("quit: reason=%s", reason != NULL ? reason : "?");
589
+ gufa_restore_dsh(app);
590
+ if (app->control_path != NULL && *app->control_path != '\0') {
591
+ g_unlink(app->control_path);
592
+ }
593
+ gufa_save_frame(app);
594
+ gtk_main_quit();
595
+ }
596
+
597
+ static gboolean gufa_on_control_tick(gpointer user_data) {
598
+ GufaApp *app = (GufaApp *)user_data;
599
+ if (app == NULL) return G_SOURCE_REMOVE;
600
+ if (app->quitting) {
601
+ app->control_timer = 0;
602
+ return G_SOURCE_REMOVE;
603
+ }
604
+ if (app->control_path != NULL && *app->control_path != '\0' &&
605
+ g_file_test(app->control_path, G_FILE_TEST_EXISTS)) {
606
+ gufa_log("control: 检测到退出控制文件 %s", app->control_path);
607
+ gufa_request_quit(app, "control-file");
608
+ app->control_timer = 0;
609
+ return G_SOURCE_REMOVE;
610
+ }
611
+ /* Host 挂了(helper 是 detached spawn,会被 init/reaper 收养):自行恢复 DSH 并退出,避免残留 */
612
+ if (app->watch_host && app->host_pid > 1 && getppid() != app->host_pid) {
613
+ gufa_log("control: Host 进程已退出(ppid %ld -> %ld),helper 自行退出并恢复主窗口",
614
+ (long)app->host_pid, (long)getppid());
615
+ gufa_request_quit(app, "host-exit");
616
+ app->control_timer = 0;
617
+ return G_SOURCE_REMOVE;
618
+ }
619
+ return G_SOURCE_CONTINUE;
620
+ }
621
+
622
+ static gboolean gufa_on_unix_signal(gpointer user_data) {
623
+ GufaApp *app = (GufaApp *)user_data;
624
+ gufa_request_quit(app, "signal");
625
+ return G_SOURCE_REMOVE;
626
+ }
627
+
628
+ static gboolean gufa_on_delete_event(GtkWidget *widget, GdkEvent *event, gpointer user_data) {
629
+ (void)widget;
630
+ (void)event;
631
+ gufa_request_quit((GufaApp *)user_data, "delete-event");
632
+ return TRUE;
633
+ }
634
+
635
+ /* ── 跟随吸附 ───────────────────────────────────────────────────────── */
636
+
637
+ static void gufa_apply_follow_rect(GufaApp *app, const GufaX11Rect *target) {
638
+ if (app == NULL || app->window == NULL || target == NULL) return;
639
+ int scale = app->window != NULL ? gtk_widget_get_scale_factor(app->window) : 1;
640
+ if (scale < 1) scale = 1;
641
+ int x = target->x / scale;
642
+ int y = target->y / scale;
643
+ int width = target->width / scale;
644
+ int height = target->height / scale;
645
+ if (width < GUFA_MIN_WIDTH) width = GUFA_MIN_WIDTH;
646
+ if (height < GUFA_MIN_HEIGHT) height = GUFA_MIN_HEIGHT;
647
+ if (app->have_applied && x == app->applied_x && y == app->applied_y && width == app->applied_width &&
648
+ height == app->applied_height) {
649
+ return;
650
+ }
651
+ app->applied_x = x;
652
+ app->applied_y = y;
653
+ app->applied_width = width;
654
+ app->applied_height = height;
655
+ app->have_applied = TRUE;
656
+ gtk_window_move(GTK_WINDOW(app->window), x, y);
657
+ gtk_window_resize(GTK_WINDOW(app->window), width, height);
658
+ if (app->follow_apply_logs < 6) {
659
+ app->follow_apply_logs++;
660
+ gufa_log("follow: 应用目标矩形 physical=%d,%d %dx%d -> window=%d,%d %dx%d scale=%d",
661
+ target->x, target->y, target->width, target->height, x, y, width, height, scale);
662
+ }
663
+ }
664
+
665
+ static gboolean gufa_on_follow_tick(gpointer user_data) {
666
+ GufaApp *app = (GufaApp *)user_data;
667
+ if (app == NULL || app->quitting) {
668
+ if (app != NULL) app->follow_timer = 0;
669
+ return G_SOURCE_REMOVE;
670
+ }
671
+ if (!app->x11_ok || app->follow_pid <= 0) {
672
+ app->follow_timer = 0;
673
+ return G_SOURCE_REMOVE;
674
+ }
675
+ if (app->follow_win == 0 || !gufa_x11_window_exists(&app->x11, app->follow_win)) {
676
+ Window found = gufa_x11_find_window(&app->x11, app->follow_pid,
677
+ app->follow_title != NULL ? app->follow_title : "",
678
+ app->own_xid);
679
+ if (found == 0) {
680
+ if (!app->follow_missing_logged) {
681
+ app->follow_missing_logged = TRUE;
682
+ gufa_log("follow: 暂未找到 pid=%ld 的目标窗口(继续每 250ms 重试)", app->follow_pid);
683
+ }
684
+ return G_SOURCE_CONTINUE;
685
+ }
686
+ app->follow_win = found;
687
+ app->follow_missing_logged = FALSE;
688
+ if (!app->follow_found_logged) {
689
+ app->follow_found_logged = TRUE;
690
+ gufa_log("follow: 找到目标窗口 0x%lx(pid=%ld),前置该 App", (unsigned long)found, app->follow_pid);
691
+ gufa_x11_activate_window(&app->x11, found, gufa_server_time(app));
692
+ if (app->own_xid != 0) gufa_x11_copy_desktop(&app->x11, found, app->own_xid);
693
+ }
694
+ }
695
+ GufaX11Rect rect;
696
+ memset(&rect, 0, sizeof(rect));
697
+ if (!gufa_x11_window_rect(&app->x11, app->follow_win, &rect) || !rect.valid) {
698
+ gufa_log("follow: 目标窗口矩形读取失败,保留当前位置(下一轮重新查找)");
699
+ app->follow_win = 0;
700
+ return G_SOURCE_CONTINUE;
701
+ }
702
+ if (!rect.viewable) {
703
+ if (!app->follow_minimized_logged) {
704
+ app->follow_minimized_logged = TRUE;
705
+ gufa_log("follow: 目标窗口不可见(最小化/切走),保持最后位置");
706
+ }
707
+ return G_SOURCE_CONTINUE;
708
+ }
709
+ app->follow_minimized_logged = FALSE;
710
+ gufa_apply_follow_rect(app, &rect);
711
+ return G_SOURCE_CONTINUE;
712
+ }
713
+
714
+ /* ── ciProbe / selftest / 页面诊断 ──────────────────────────────────── */
715
+
716
+ static void gufa_on_js_finished(GObject *source_object, GAsyncResult *result, gpointer user_data) {
717
+ GufaJsRequest *request = (GufaJsRequest *)user_data;
718
+ GError *error = NULL;
719
+ WebKitJavascriptResult *js_result =
720
+ webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(source_object), result, &error);
721
+ if (js_result == NULL) {
722
+ gufa_log("%s: js-error %s", request->label, error != NULL ? error->message : "?");
723
+ g_clear_error(&error);
724
+ } else {
725
+ JSCValue *value = webkit_javascript_result_get_js_value(js_result);
726
+ char *text = value != NULL ? jsc_value_to_string(value) : NULL;
727
+ gufa_log("%s: %s", request->label, text != NULL ? text : "(no result)");
728
+ g_free(text);
729
+ webkit_javascript_result_unref(js_result);
730
+ }
731
+ g_free(request->label);
732
+ g_free(request);
733
+ }
734
+
735
+ static void gufa_run_js(GufaApp *app, const char *label, const char *script) {
736
+ if (app == NULL || app->webview == NULL || script == NULL) return;
737
+ GufaJsRequest *request = g_new0(GufaJsRequest, 1);
738
+ request->app = app;
739
+ request->label = g_strdup(label != NULL ? label : "js");
740
+ webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(app->webview), script, NULL, gufa_on_js_finished, request);
741
+ }
742
+
743
+ static void gufa_emit_probe_result(GufaApp *app, gboolean url_loaded) {
744
+ if (app == NULL || app->probe_done) return;
745
+ app->probe_done = TRUE;
746
+ gufa_remove_timer(&app->probe_guard);
747
+ const char *uri = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(app->webview));
748
+ gufa_log("ciProbe: urlLoaded=%s uri=%s", url_loaded ? "true" : "false", uri != NULL ? uri : "(none)");
749
+ g_print("{\"ok\":true,\"platform\":\"linux\",\"urlLoaded\":%s}\n", url_loaded ? "true" : "false");
750
+ fflush(stdout);
751
+ app->exit_code = 0;
752
+ gufa_request_quit(app, "ciProbe");
753
+ }
754
+
755
+ static gboolean gufa_on_probe_guard(gpointer user_data) {
756
+ GufaApp *app = (GufaApp *)user_data;
757
+ if (app == NULL) return G_SOURCE_REMOVE;
758
+ app->probe_guard = 0;
759
+ if (app->probe_done) return G_SOURCE_REMOVE;
760
+ gufa_log("ciProbe: 8s 内页面未加载完成,按超时输出结果");
761
+ gufa_emit_probe_result(app, FALSE);
762
+ return G_SOURCE_REMOVE;
763
+ }
764
+
765
+ static gboolean gufa_on_page_probe_timer(gpointer user_data) {
766
+ GufaApp *app = (GufaApp *)user_data;
767
+ if (app == NULL) return G_SOURCE_REMOVE;
768
+ app->page_probe_timer = 0;
769
+ if (app->quitting) return G_SOURCE_REMOVE;
770
+ gufa_run_js(app, "page-probe",
771
+ "JSON.stringify({href:location.href,title:document.title,"
772
+ "float:localStorage.getItem('gufa-code:float'),"
773
+ "state:(localStorage.getItem('gufa-code:float-state')||'').slice(0,120),"
774
+ "body:((document.body&&document.body.innerText)||'').slice(0,160)})");
775
+ return G_SOURCE_REMOVE;
776
+ }
777
+
778
+ static gboolean gufa_on_selftest_snapshot(gpointer user_data) {
779
+ GufaApp *app = (GufaApp *)user_data;
780
+ if (app == NULL) return G_SOURCE_REMOVE;
781
+ app->selftest_snapshot_timer = 0;
782
+ if (app->quitting) return G_SOURCE_REMOVE;
783
+ gufa_run_js(app, "selftest-snapshot", "JSON.stringify(window.__gufaSelftest||{step:'none'})");
784
+ return G_SOURCE_REMOVE;
785
+ }
786
+
787
+ static gboolean gufa_on_selftest_timer(gpointer user_data) {
788
+ GufaApp *app = (GufaApp *)user_data;
789
+ if (app == NULL) return G_SOURCE_REMOVE;
790
+ app->selftest_timer = 0;
791
+ if (app->quitting) return G_SOURCE_REMOVE;
792
+ if (app->selftest != NULL && *app->selftest != '\0') {
793
+ gufa_run_js(app, "selftest", app->selftest);
794
+ app->selftest_snapshot_timer = g_timeout_add(6000, gufa_on_selftest_snapshot, app);
795
+ }
796
+ return G_SOURCE_REMOVE;
797
+ }
798
+
799
+ static gboolean gufa_on_hide_dsh_timer(gpointer user_data) {
800
+ GufaApp *app = (GufaApp *)user_data;
801
+ if (app == NULL) return G_SOURCE_REMOVE;
802
+ app->hide_timer = 0;
803
+ if (app->quitting) return G_SOURCE_REMOVE;
804
+ gufa_hide_dsh(app);
805
+ return G_SOURCE_REMOVE;
806
+ }
807
+
808
+ /* ── WebKit 信号 ────────────────────────────────────────────────────── */
809
+
810
+ static void gufa_on_load_changed(WebKitWebView *web_view, WebKitLoadEvent load_event, gpointer user_data) {
811
+ GufaApp *app = (GufaApp *)user_data;
812
+ if (app == NULL || app->quitting) return;
813
+ switch (load_event) {
814
+ case WEBKIT_LOAD_COMMITTED: {
815
+ const char *uri = webkit_web_view_get_uri(web_view);
816
+ gufa_log("load: committed %s", uri != NULL ? uri : "(none)");
817
+ break;
818
+ }
819
+ case WEBKIT_LOAD_FINISHED: {
820
+ const char *uri = webkit_web_view_get_uri(web_view);
821
+ const char *title = webkit_web_view_get_title(web_view);
822
+ gufa_log("load: finished stage=%d uri=%s title=%s", (int)app->stage,
823
+ uri != NULL ? uri : "(none)", title != NULL ? title : "(none)");
824
+ if (app->stage == GUFA_STAGE_AUTH) {
825
+ /* authUrl(token 登录)加载完成 -> 再导航到干净的 appUrl?gufa-float=1 */
826
+ gufa_load_app_url(app);
827
+ return;
828
+ }
829
+ if (app->stage == GUFA_STAGE_APP && !app->loaded) {
830
+ app->loaded = TRUE;
831
+ gufa_remove_timer(&app->probe_guard);
832
+ if (app->ci_probe) {
833
+ gufa_emit_probe_result(app, TRUE);
834
+ return;
835
+ }
836
+ app->page_probe_timer = g_timeout_add(6000, gufa_on_page_probe_timer, app);
837
+ if (app->selftest != NULL && *app->selftest != '\0') {
838
+ app->selftest_timer = g_timeout_add(5000, gufa_on_selftest_timer, app);
839
+ }
840
+ }
841
+ break;
842
+ }
843
+ default:
844
+ break;
845
+ }
846
+ }
847
+
848
+ static gboolean gufa_on_load_failed(WebKitWebView *web_view, WebKitLoadEvent load_event, const gchar *failing_uri,
849
+ GError *error, gpointer user_data) {
850
+ (void)web_view;
851
+ GufaApp *app = (GufaApp *)user_data;
852
+ if (app == NULL || app->quitting) return FALSE;
853
+ gufa_log("load: failed stage=%d event=%d uri=%s error=%s", (int)app->stage, (int)load_event,
854
+ failing_uri != NULL ? failing_uri : "(none)", error != NULL ? error->message : "?");
855
+ app->loading_failed = TRUE;
856
+ if (app->ci_probe) {
857
+ gufa_emit_probe_result(app, FALSE);
858
+ return FALSE;
859
+ }
860
+ if (app->stage == GUFA_STAGE_AUTH) {
861
+ /* token URL 失败时兜底直连 appUrl(cookie 可能已经有效,或 Host 提供了 cookie) */
862
+ gufa_log("load: authUrl 失败,尝试直接加载 appUrl");
863
+ gufa_load_app_url(app);
864
+ }
865
+ return FALSE;
866
+ }
867
+
868
+ static void gufa_on_web_process_terminated(WebKitWebView *web_view, WebKitWebProcessTerminationReason reason,
869
+ gpointer user_data) {
870
+ (void)web_view;
871
+ (void)user_data;
872
+ gufa_log("web: web process 终止 reason=%d", (int)reason);
873
+ }
874
+
875
+ /* ── 窗口构建 ───────────────────────────────────────────────────────── */
876
+
877
+ static void gufa_build_window(GufaApp *app) {
878
+ GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
879
+ app->window = window;
880
+ gtk_window_set_title(GTK_WINDOW(window), "古法编程悬浮层");
881
+ gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
882
+ gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
883
+ gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_UTILITY);
884
+ gtk_window_set_skip_taskbar_hint(GTK_WINDOW(window), TRUE);
885
+ gtk_window_set_skip_pager_hint(GTK_WINDOW(window), TRUE);
886
+ gtk_window_set_focus_on_map(GTK_WINDOW(window), FALSE);
887
+ gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_NONE);
888
+ gtk_window_set_accept_focus(GTK_WINDOW(window), TRUE);
889
+ gtk_window_set_default_size(GTK_WINDOW(window), GUFA_DEFAULT_WIDTH, GUFA_DEFAULT_HEIGHT);
890
+ gtk_style_context_add_class(gtk_widget_get_style_context(window), "gufa-float-window");
891
+
892
+ GdkScreen *screen = gtk_widget_get_screen(window);
893
+ GdkVisual *rgba_visual = screen != NULL ? gdk_screen_get_rgba_visual(screen) : NULL;
894
+ app->composited = screen != NULL ? gdk_screen_is_composited(screen) : FALSE;
895
+ app->transparent = app->composited && rgba_visual != NULL;
896
+ if (app->transparent) {
897
+ gtk_widget_set_visual(window, rgba_visual);
898
+ gtk_widget_set_app_paintable(window, TRUE);
899
+ g_signal_connect(window, "draw", G_CALLBACK(gufa_on_window_draw), app);
900
+ gufa_log("window: 使用 RGBA visual,窗口背景透明(合成器可用)");
901
+ } else {
902
+ gufa_log("window: 当前桌面没有合成器 / 无 RGBA visual,窗口将不透明(页面仍可正常使用)");
903
+ }
904
+ g_signal_connect(window, "delete-event", G_CALLBACK(gufa_on_delete_event), app);
905
+
906
+ GtkWidget *overlay = gtk_overlay_new();
907
+ gtk_container_add(GTK_CONTAINER(window), overlay);
908
+ app->overlay = overlay;
909
+
910
+ /* document-start 注入脚本:悬浮模式标记 + 主窗口状态快照 */
911
+ app->content_manager = webkit_user_content_manager_new();
912
+ if (app->bootstrap_js != NULL && *app->bootstrap_js != '\0') {
913
+ WebKitUserScript *script = webkit_user_script_new(app->bootstrap_js,
914
+ WEBKIT_USER_CONTENT_INJECT_TOP_FRAME,
915
+ WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START,
916
+ NULL, NULL);
917
+ if (script != NULL) {
918
+ webkit_user_content_manager_add_script(app->content_manager, script);
919
+ webkit_user_script_unref(script);
920
+ }
921
+ }
922
+ GtkWidget *webview = webkit_web_view_new_with_user_content_manager(app->content_manager);
923
+ app->webview = webview;
924
+ gtk_widget_set_size_request(webview, GUFA_MIN_WIDTH, GUFA_MIN_HEIGHT);
925
+ if (app->transparent) {
926
+ GdkRGBA transparent;
927
+ memset(&transparent, 0, sizeof(transparent));
928
+ webkit_web_view_set_background_color(WEBKIT_WEB_VIEW(webview), &transparent);
929
+ }
930
+ g_signal_connect(webview, "load-changed", G_CALLBACK(gufa_on_load_changed), app);
931
+ g_signal_connect(webview, "load-failed", G_CALLBACK(gufa_on_load_failed), app);
932
+ g_signal_connect(webview, "web-process-terminated", G_CALLBACK(gufa_on_web_process_terminated), app);
933
+ gtk_container_add(GTK_CONTAINER(overlay), webview);
934
+
935
+ /* 顶部 22px 拖动条 */
936
+ GtkWidget *strip = gtk_event_box_new();
937
+ gtk_event_box_set_visible_window(GTK_EVENT_BOX(strip), TRUE);
938
+ gtk_widget_set_size_request(strip, -1, GUFA_DRAG_STRIP_HEIGHT);
939
+ gtk_widget_set_halign(strip, GTK_ALIGN_FILL);
940
+ gtk_widget_set_valign(strip, GTK_ALIGN_START);
941
+ gtk_widget_add_events(strip, GDK_BUTTON_PRESS_MASK);
942
+ gtk_style_context_add_class(gtk_widget_get_style_context(strip), "gufa-strip");
943
+ gtk_widget_set_tooltip_text(strip, "按住拖动窗口;右键打开菜单");
944
+ GtkWidget *strip_label = gtk_label_new("古法编程 · 摸鱼中(拖动移动 · 右下角缩放 · 右键菜单)");
945
+ gtk_label_set_ellipsize(GTK_LABEL(strip_label), PANGO_ELLIPSIZE_END);
946
+ gtk_widget_set_halign(strip_label, GTK_ALIGN_CENTER);
947
+ gtk_widget_set_valign(strip_label, GTK_ALIGN_CENTER);
948
+ gtk_style_context_add_class(gtk_widget_get_style_context(strip_label), "gufa-strip-label");
949
+ gtk_container_add(GTK_CONTAINER(strip), strip_label);
950
+ g_signal_connect(strip, "button-press-event", G_CALLBACK(gufa_on_strip_button_press), app);
951
+ g_signal_connect(strip, "realize", G_CALLBACK(gufa_on_strip_realize), app);
952
+ gtk_overlay_add_overlay(GTK_OVERLAY(overlay), strip);
953
+
954
+ /* 右下角缩放手柄(无边框窗口自己实现 resize) */
955
+ GtkWidget *grip = gtk_drawing_area_new();
956
+ gtk_widget_set_size_request(grip, 18, 18);
957
+ gtk_widget_set_halign(grip, GTK_ALIGN_END);
958
+ gtk_widget_set_valign(grip, GTK_ALIGN_END);
959
+ gtk_widget_add_events(grip, GDK_BUTTON_PRESS_MASK);
960
+ g_signal_connect(grip, "draw", G_CALLBACK(gufa_on_grip_draw), app);
961
+ g_signal_connect(grip, "realize", G_CALLBACK(gufa_on_grip_realize), app);
962
+ g_signal_connect(grip, "button-press-event", G_CALLBACK(gufa_on_grip_button_press), app);
963
+ gtk_overlay_add_overlay(GTK_OVERLAY(overlay), grip);
964
+
965
+ gufa_build_menu(app);
966
+
967
+ int x = 0, y = 0, width = 0, height = 0;
968
+ if (!gufa_load_saved_frame(&x, &y, &width, &height)) {
969
+ gufa_center_window(app, &x, &y, &width, &height);
970
+ }
971
+ gtk_window_move(GTK_WINDOW(window), x, y);
972
+ gtk_window_resize(GTK_WINDOW(window), width, height);
973
+ gtk_widget_show_all(window);
974
+ gtk_window_set_keep_above(GTK_WINDOW(window), TRUE);
975
+ gtk_window_move(GTK_WINDOW(window), x, y);
976
+ gtk_window_resize(GTK_WINDOW(window), width, height);
977
+ app->own_xid = gufa_own_xid_from_widget(app);
978
+ gufa_log("window: %d,%d %dx%d own_xid=0x%lx scale=%d", x, y, width, height,
979
+ (unsigned long)app->own_xid, gtk_widget_get_scale_factor(window));
980
+ }
981
+
982
+ /* ── --list-windows 模式 ────────────────────────────────────────────── */
983
+
984
+ static int gufa_run_list_windows(const char *out_path) {
985
+ GufaX11 x11;
986
+ if (!gufa_x11_open(&x11)) {
987
+ g_printerr("gufa-float-linux: 无法连接 X11(DISPLAY=%s),--list-windows 需要 X11\n",
988
+ g_getenv("DISPLAY") != NULL ? g_getenv("DISPLAY") : "(unset)");
989
+ return 1;
990
+ }
991
+ GPtrArray *apps = gufa_x11_list_apps(&x11);
992
+ GString *json = g_string_new("{\"ok\":true,\"accessibility\":true,\"apps\":[");
993
+ for (guint i = 0; i < apps->len; i++) {
994
+ const GufaX11App *app = g_ptr_array_index(apps, i);
995
+ if (i > 0) g_string_append_c(json, ',');
996
+ char *title = gufa_json_quote(app->title);
997
+ char *owner = gufa_json_quote(app->owner != NULL ? app->owner : "");
998
+ char *wm_class = gufa_json_quote(app->wm_class != NULL ? app->wm_class : "");
999
+ g_string_append_printf(json,
1000
+ "{\"pid\":%ld,\"owner\":%s,\"title\":%s,\"bundleId\":%s,"
1001
+ "\"windowNumber\":%lu,\"frontmost\":%s}",
1002
+ app->pid, owner, title, wm_class, (unsigned long)app->win,
1003
+ app->frontmost ? "true" : "false");
1004
+ g_free(title);
1005
+ g_free(owner);
1006
+ g_free(wm_class);
1007
+ }
1008
+ g_string_append(json, "]}");
1009
+ int result = 0;
1010
+ if (out_path != NULL) {
1011
+ GError *error = NULL;
1012
+ if (!g_file_set_contents(out_path, json->str, (gssize)json->len, &error)) {
1013
+ g_printerr("gufa-float-linux: 写入 %s 失败:%s\n", out_path,
1014
+ error != NULL ? error->message : "?");
1015
+ g_clear_error(&error);
1016
+ result = 1;
1017
+ }
1018
+ } else {
1019
+ g_print("%s\n", json->str);
1020
+ }
1021
+ g_string_free(json, TRUE);
1022
+ gufa_x11_apps_free(apps);
1023
+ gufa_x11_close(&x11);
1024
+ return result;
1025
+ }
1026
+
1027
+ /* ── main ──────────────────────────────────────────────────────────── */
1028
+
1029
+ static void gufa_print_usage(void) {
1030
+ g_printerr("用法:gufa-float-linux '<payloadJson>'\n"
1031
+ " gufa-float-linux --list-windows [--out path]\n"
1032
+ " gufa-float-linux --version\n");
1033
+ }
1034
+
1035
+ int main(int argc, char **argv) {
1036
+ gufa_log_init();
1037
+ if (argc < 2) {
1038
+ gufa_print_usage();
1039
+ return 2;
1040
+ }
1041
+ if (strcmp(argv[1], "--list-windows") == 0) {
1042
+ const char *out_path = NULL;
1043
+ for (int i = 2; i + 1 < argc; i++) {
1044
+ if (strcmp(argv[i], "--out") == 0) out_path = argv[i + 1];
1045
+ }
1046
+ return gufa_run_list_windows(out_path);
1047
+ }
1048
+ if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-v") == 0) {
1049
+ g_print("gufa-float-linux %s (GTK3 + WebKit2GTK)\n", GUFA_FLOAT_VERSION);
1050
+ return 0;
1051
+ }
1052
+ if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
1053
+ gufa_print_usage();
1054
+ return 0;
1055
+ }
1056
+ if (argv[1][0] != '{') {
1057
+ g_printerr("gufa-float-linux: argv[1] 不是 JSON payload\n");
1058
+ gufa_print_usage();
1059
+ return 2;
1060
+ }
1061
+
1062
+ GError *json_error = NULL;
1063
+ GufaJson *payload = gufa_json_parse(argv[1], &json_error);
1064
+ if (payload == NULL || gufa_json_get_type(payload) != GUFA_JSON_OBJECT) {
1065
+ g_printerr("gufa-float-linux: argv[1] JSON 解析失败:%s\n",
1066
+ json_error != NULL ? json_error->message : "根节点不是 object");
1067
+ g_clear_error(&json_error);
1068
+ gufa_json_free(payload);
1069
+ return 2;
1070
+ }
1071
+
1072
+ GufaApp app;
1073
+ memset(&app, 0, sizeof(app));
1074
+ app.exit_code = 0;
1075
+ if (!gufa_payload_fill(&app, payload)) {
1076
+ g_printerr("gufa-float-linux: payload 缺少 appUrl,退出\n");
1077
+ gufa_json_free(payload);
1078
+ g_free(app.app_url);
1079
+ return 2;
1080
+ }
1081
+ gufa_log("start: version=%s build=%s appUrl=%s follow_pid=%ld ownerPid=%ld controlPath=%s ciProbe=%d alpha=%d",
1082
+ GUFA_FLOAT_VERSION, app.build != NULL ? app.build : "?", app.app_url, app.follow_pid,
1083
+ app.owner_pid, app.control_path != NULL ? app.control_path : "(none)", app.ci_probe ? 1 : 0,
1084
+ app.alpha);
1085
+
1086
+ if (!gufa_configure_backend(&app)) return 3;
1087
+ gufa_prepare_probe_env(&app);
1088
+
1089
+ int gtk_argc = argc;
1090
+ char **gtk_argv = argv;
1091
+ if (!gtk_init_check(&gtk_argc, &gtk_argv)) {
1092
+ g_printerr("gufa-float-linux: GTK 初始化失败(图形显示不可用)。Linux 版需要 X11 桌面或 XWayland。\n");
1093
+ gufa_json_free(payload);
1094
+ return 3;
1095
+ }
1096
+ #if GUFA_HAVE_GDK_X11
1097
+ {
1098
+ GdkDisplay *display = gdk_display_get_default();
1099
+ app.gdk_is_x11 = display != NULL && GDK_IS_X11_DISPLAY(display);
1100
+ gufa_log("display: type=%s", app.gdk_is_x11 ? "x11" : (display != NULL ? G_OBJECT_TYPE_NAME(display) : "none"));
1101
+ }
1102
+ #else
1103
+ app.gdk_is_x11 = FALSE;
1104
+ #endif
1105
+ if (app.gdk_is_x11) {
1106
+ app.x11_ok = gufa_x11_open(&app.x11);
1107
+ } else {
1108
+ gufa_log("display: 当前不是 X11 后端(Wayland?),follow 与隐藏 DSH 主窗口降级为不可用,仅自由悬浮");
1109
+ }
1110
+
1111
+ gufa_install_css();
1112
+ gufa_build_window(&app);
1113
+
1114
+ if (!app.ci_probe) {
1115
+ if (app.x11_ok) {
1116
+ app.hide_timer = g_timeout_add(450, gufa_on_hide_dsh_timer, &app);
1117
+ } else {
1118
+ gufa_log("dsh-hide: 非 X11 会话,跳过隐藏 DSH 主窗口");
1119
+ }
1120
+ if (app.follow_pid > 0) {
1121
+ if (app.x11_ok) {
1122
+ app.follow_timer = g_timeout_add(250, gufa_on_follow_tick, &app);
1123
+ gufa_log("follow: 启动 250ms 跟随定时器 pid=%ld title=%s", app.follow_pid,
1124
+ app.follow_title != NULL ? app.follow_title : "");
1125
+ } else {
1126
+ gufa_log("follow: X11 不可用,降级为自由悬浮(不跟随)");
1127
+ }
1128
+ } else {
1129
+ gufa_log("follow: 自由悬浮模式(不跟随)");
1130
+ }
1131
+ }
1132
+
1133
+ if (app.control_path != NULL && *app.control_path != '\0') {
1134
+ app.host_pid = getppid();
1135
+ app.watch_host = app.host_pid > 1;
1136
+ app.control_timer = g_timeout_add(300, gufa_on_control_tick, &app);
1137
+ gufa_log("control: 每 300ms 轮询 %s(host pid=%ld)", app.control_path, (long)app.host_pid);
1138
+ }
1139
+ g_unix_signal_add(SIGTERM, gufa_on_unix_signal, &app);
1140
+ g_unix_signal_add(SIGINT, gufa_on_unix_signal, &app);
1141
+
1142
+ if (app.ci_probe) {
1143
+ app.probe_guard = g_timeout_add(8000, gufa_on_probe_guard, &app);
1144
+ }
1145
+ gufa_start_load(&app);
1146
+
1147
+ gtk_main();
1148
+
1149
+ /* 兜底:任何未走 request_quit 的退出路径也要恢复 DSH 主窗口 */
1150
+ gufa_restore_dsh(&app);
1151
+ gufa_remove_timer(&app.control_timer);
1152
+ gufa_remove_timer(&app.follow_timer);
1153
+ gufa_remove_timer(&app.hide_timer);
1154
+ gufa_remove_timer(&app.probe_guard);
1155
+ gufa_remove_timer(&app.page_probe_timer);
1156
+ gufa_remove_timer(&app.selftest_timer);
1157
+ gufa_remove_timer(&app.selftest_snapshot_timer);
1158
+ gufa_save_frame(&app);
1159
+
1160
+ if (app.menu != NULL) {
1161
+ gtk_widget_destroy(app.menu);
1162
+ app.menu = NULL;
1163
+ }
1164
+ if (app.window != NULL) {
1165
+ gtk_widget_destroy(app.window);
1166
+ app.window = NULL;
1167
+ }
1168
+ if (app.content_manager != NULL) {
1169
+ g_object_unref(app.content_manager);
1170
+ app.content_manager = NULL;
1171
+ }
1172
+ gufa_x11_close(&app.x11);
1173
+ g_free(app.app_url);
1174
+ g_free(app.auth_url);
1175
+ g_free(app.cookie);
1176
+ g_free(app.control_path);
1177
+ g_free(app.build);
1178
+ g_free(app.selftest);
1179
+ g_free(app.follow_title);
1180
+ g_free(app.bootstrap_js);
1181
+ g_free(app.hidden_dsh.wins);
1182
+ gufa_json_free(app.payload);
1183
+ return app.exit_code;
1184
+ }