gent-link 0.2.24

Connect Codex and Claude Code chats across machines with the Gent CLI and desktop companion.
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
// The dashboard's monitor, geometry and pointer state live on the GTK thread.
#include "linux_tray.h"
#include <gdk/gdkx.h>

typedef struct { gint width, height; } Size;
static const char saved_key[] = "gent-dashboard-user-size";
static GdkMonitor *output;
static char *output_key;
static GdkRectangle area;
static Size current;
static gboolean wayland_drag, x11_drag, fullscreen;
static gboolean dirty, wayland_released;
static guint settle_timer;
static guint placement_tick;
static guint x11_timer;
static struct { GdkWindowEdge edge; gint x, y; Size size; } press;

static void resized(Size next) {
    Size *saved = g_object_get_data(G_OBJECT(output), saved_key);
    if (!saved) {
        saved = g_new(Size, 1);
        *saved = current;
        g_object_set_data_full(G_OBJECT(output), saved_key, saved, g_free);
    }
    // An edit on one axis must preserve the preferred size of a clamped other axis.
    if (next.width != current.width) saved->width = next.width;
    if (next.height != current.height) saved->height = next.height;
    current = next;
    dirty = TRUE;
}

static void save_size(void) {
    if (!dirty) return;
    Size *saved = g_object_get_data(G_OBJECT(output), saved_key);
    if (output_key) gent_tray_size_save(output_key, saved->width, saved->height);
    dirty = FALSE;
}

static void hints(GtkWindow *window, Size min, Size max) {
    GdkGeometry limits = {.min_width = min.width, .min_height = min.height,
                         .max_width = max.width, .max_height = max.height};
    gtk_window_set_geometry_hints(window, NULL, &limits, GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE);
}

static void apply(GtkWindow *window, gboolean mapping) {
    // A fixed initial request lets a decorationless window float on tiling Wayland compositors.
    if (mapping && gent_tray_wayland()) hints(window, current, current);
    else hints(window, (Size){MIN(240, current.width), MIN(160, current.height)},
                       (Size){area.width, area.height});
    gtk_window_resize(window, current.width, current.height);
    if (!gent_tray_wayland()) gtk_window_move(window, area.x + area.width - current.width, area.y);
}

static void stop_drag(void) {
    save_size();
    wayland_released = wayland_drag = x11_drag = FALSE;
    g_clear_handle_id(&settle_timer, g_source_remove);
    g_clear_handle_id(&x11_timer, g_source_remove);
}

static void restore(GtkWindow *window, gboolean mapping) {
    stop_drag();
    Size *saved = g_object_get_data(G_OBJECT(output), saved_key);
    Size stored;
    if (!saved && output_key && gent_tray_size_read(output_key, &stored.width, &stored.height)) {
        saved = g_new(Size, 1);
        *saved = stored;
        g_object_set_data_full(G_OBJECT(output), saved_key, saved, g_free);
    }
    // Clipping to a smaller workarea keeps the chosen size for when space returns.
    current = saved ? (Size){MIN(saved->width, area.width), MIN(saved->height, area.height)}
                    : (Size){area.width / 2 + area.width % 2, area.height / 2 + area.height % 2};
    apply(window, mapping);
}

static void check_area(GtkWindow *window) {
    if (!output || fullscreen || !gtk_widget_get_mapped(GTK_WIDGET(window))) return;
    GdkRectangle next = gent_dashboard_workarea(output);
    if (!gdk_rectangle_equal(&area, &next)) {
        area = next;
        restore(window, FALSE);
    }
}

static void monitor_changed(GdkMonitor *monitor G_GNUC_UNUSED, GParamSpec *prop G_GNUC_UNUSED,
                            GtkWindow *window) {
    check_area(window);
}

static void select_output(GtkWindow *window, GdkMonitor *monitor) {
    stop_drag();
    g_free(output_key);
    output_key = gent_dashboard_output_key(monitor);
    if (monitor != output) {
        if (output) g_signal_handlers_disconnect_by_func(output, monitor_changed, window);
        g_set_object(&output, monitor);
        g_signal_connect_object(output, "notify::workarea", G_CALLBACK(monitor_changed), window, 0);
        g_signal_connect_object(output, "notify::geometry", G_CALLBACK(monitor_changed), window, 0);
    }
    area = gent_dashboard_workarea(output);
}

static GdkFilterReturn root_property(GdkXEvent *native, GdkEvent *event G_GNUC_UNUSED, gpointer data) {
    XEvent *x = native;
    if (x->type == PropertyNotify && gtk_widget_get_mapped(GTK_WIDGET(data))) {
        GdkDisplay *display = gtk_widget_get_display(GTK_WIDGET(data));
        if (x->xproperty.atom == gdk_x11_get_xatom_by_name_for_display(display, "_NET_WORKAREA") ||
            x->xproperty.atom == gdk_x11_get_xatom_by_name_for_display(display, "_NET_CURRENT_DESKTOP"))
            check_area(GTK_WINDOW(data));
    }
    return GDK_FILTER_CONTINUE;
}

static void unmapped(GtkWidget *widget, gpointer data G_GNUC_UNUSED) {
    stop_drag();
    if (placement_tick) {
        gtk_widget_remove_tick_callback(widget, placement_tick);
        placement_tick = 0;
    }
}

static void remove_filter(GtkWidget *widget, gpointer data G_GNUC_UNUSED) {
    gdk_window_remove_filter(gdk_get_default_root_window(), root_property, widget);
}

// GTK3 does not expose xdg_toplevel's resizing state. The compositor may own
// button-up, so save a settled configure burst even when no release arrives.
static gboolean settled(gpointer data) {
    GtkWidget *widget = GTK_WIDGET(data);
    settle_timer = 0;
    save_size();
    GdkDevice *pointer = gdk_seat_get_pointer(gdk_display_get_default_seat(gtk_widget_get_display(widget)));
    if (wayland_released || (pointer && gdk_device_get_window_at_position(pointer, NULL, NULL))) {
        GdkModifierType mods = 0;
        if (pointer) gdk_window_get_device_position(gtk_widget_get_window(widget), pointer, NULL, NULL, &mods);
        if (wayland_released || !(mods & GDK_BUTTON1_MASK)) stop_drag();
    }
    return G_SOURCE_REMOVE;
}

static void await_configure(GtkWindow *window) {
    g_clear_handle_id(&settle_timer, g_source_remove);
    settle_timer = g_timeout_add(300, settled, window);
}

static gboolean configured(GtkWindow *window, GdkEventConfigure *event,
                           gpointer data G_GNUC_UNUSED) {
    if (wayland_drag) {
        check_area(window);
        if (!wayland_drag) return FALSE;
        if (gdk_window_get_state(event->window) & (GDK_WINDOW_STATE_FULLSCREEN | GDK_WINDOW_STATE_MAXIMIZED | GDK_WINDOW_STATE_TILED)) {
            stop_drag();
            return FALSE;
        }
        if (event->width > 0 && event->height > 0 && event->width <= area.width && event->height <= area.height &&
            (event->width != current.width || event->height != current.height)) {
            resized((Size){event->width, event->height});
        }
        await_configure(window);
    }
    return FALSE;
}

static gboolean placed(GtkWidget *widget, GdkFrameClock *clock, gpointer data) {
    // Before wl_surface.enter GDK guesses the monitor from (0,0). Wait until
    // the mapped window has drawn and the compositor has presented its frames.
    gint64 first = *(gint64 *)data, last = gdk_frame_clock_get_frame_counter(clock);
    gint presented = 0;
    for (gint64 n = MAX(first + 1, gdk_frame_clock_get_history_start(clock)); n < last; ++n) {
        GdkFrameTimings *timings = gdk_frame_clock_get_timings(clock, n);
        if (timings && gdk_frame_timings_get_complete(timings) && ++presented == 3) break;
    }
    if (presented < 3 && last - first < 30) return G_SOURCE_CONTINUE;
    placement_tick = 0;
    if (presented < 3 || fullscreen) return G_SOURCE_REMOVE;
    GdkWindow *surface = gtk_widget_get_window(widget);
    GdkMonitor *actual = gdk_display_get_monitor_at_window(gtk_widget_get_display(widget), surface);
    if (actual && actual != output) {
        select_output(GTK_WINDOW(widget), actual);
        restore(GTK_WINDOW(widget), FALSE);
    }
    return G_SOURCE_REMOVE;
}

static void border_pressed(GtkGestureMultiPress *, gint, gdouble, gdouble, GtkWindow *);
static void released(GtkGestureMultiPress *, gint, gdouble, gdouble, GtkWindow *);

// Nonnegative values cross the Rust/C boundary as popup::ResizeEdge.
enum { PANEL_CONTENT = -2, UNUSED_BORDER, TOP, BOTTOM, LEFT, BOTTOM_LEFT };
static const GdkWindowEdge edges[] = {
    [TOP] = GDK_WINDOW_EDGE_NORTH, [BOTTOM] = GDK_WINDOW_EDGE_SOUTH,
    [LEFT] = GDK_WINDOW_EDGE_WEST, [BOTTOM_LEFT] = GDK_WINDOW_EDGE_SOUTH_WEST
};

static gint border_hit(GtkWidget *widget, gdouble x, gdouble y) {
    gint w = gtk_widget_get_allocated_width(widget), h = gtk_widget_get_allocated_height(widget);
    gint scale = gtk_widget_get_scale_factor(widget), inset = 5 * scale;
    if (x >= inset && y >= inset && x < w - inset && y < h - inset) return PANEL_CONTENT;
    // Match the CSS media queries. WebKitGTK treats a seat with both mouse
    // and touch capabilities as having a fine primary pointer.
    GdkSeat *seat = gdk_display_get_default_seat(gtk_widget_get_display(widget));
    GdkSeatCapabilities caps = seat ? gdk_seat_get_capabilities(seat) : GDK_SEAT_CAPABILITY_NONE;
    gboolean coarse = (caps & GDK_SEAT_CAPABILITY_TOUCH) && !(caps & GDK_SEAT_CAPABILITY_POINTER);
    gint edge = coarse ? 24 : scale >= 2 ? 12 : 8;
    gint corner = coarse ? 24 : 16;
    if (y < edge) return TOP;
    if (y >= h - corner) {
        if (x < corner) return BOTTOM_LEFT;
        if (x < w - corner) return BOTTOM;
    } else if (x < edge) return LEFT;
    return UNUSED_BORDER;
}

// Tauri sets a native resize cursor before WebKit receives motion in this strip.
static gboolean unused_border_motion(GtkWidget *view, GdkEvent *event, GtkWindow *window) {
    if (event->type != GDK_MOTION_NOTIFY ||
        border_hit(view, event->motion.x, event->motion.y) != UNUSED_BORDER) return FALSE;
    gdk_window_set_cursor(gtk_widget_get_window(GTK_WIDGET(window)), NULL);
    gdk_window_set_cursor(event->motion.window, NULL);
    return TRUE;
}

static gboolean state_changed(GtkWindow *window, GdkEventWindowState *event, gpointer data G_GNUC_UNUSED) {
    if (!(event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)) return FALSE;
    stop_drag();
    fullscreen = (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN) != 0;
    if (!fullscreen && output && gtk_widget_get_mapped(GTK_WIDGET(window))) {
        area = gent_dashboard_workarea(output);
        restore(window, FALSE);
    }
    return FALSE;
}

void gent_dashboard_fullscreen(GtkWindow *window, gboolean enabled) {
    if (!enabled) {
        gtk_window_unfullscreen(window);
        fullscreen = FALSE;
        if (output && gtk_widget_get_mapped(GTK_WIDGET(window))) restore(window, FALSE);
        return;
    }
    if (!output || !gtk_widget_get_mapped(GTK_WIDGET(window))) return;
    stop_drag();
    fullscreen = TRUE;
    gtk_window_set_geometry_hints(window, NULL, NULL, 0);
    GdkDisplay *display = gtk_widget_get_display(GTK_WIDGET(window));
    for (gint i = 0; i < gdk_display_get_n_monitors(display); ++i) {
        if (gdk_display_get_monitor(display, i) == output) {
            gtk_window_fullscreen_on_monitor(window, gdk_display_get_default_screen(display), i);
            return;
        }
    }
    gtk_window_fullscreen(window);
}

void gent_dashboard_present(GtkWidget *webview, GdkMonitor *monitor) {
    GtkWidget *widget = gtk_widget_get_toplevel(webview);
    GtkWindow *window = GTK_WINDOW(widget);
    if (monitor) {
        if (!output) {
            g_signal_connect(webview, "event", G_CALLBACK(unused_border_motion), window);
            g_signal_connect(window, "window-state-event", G_CALLBACK(state_changed), NULL);
            g_signal_connect(window, "configure-event", G_CALLBACK(configured), NULL);
            g_signal_connect(window, "unmap", G_CALLBACK(unmapped), NULL);
            GtkGesture *gesture = gtk_gesture_multi_press_new(widget);
            gtk_event_controller_set_propagation_phase(GTK_EVENT_CONTROLLER(gesture), GTK_PHASE_CAPTURE);
            g_signal_connect(gesture, "pressed", G_CALLBACK(border_pressed), window);
            g_signal_connect(gesture, "released", G_CALLBACK(released), window);
            g_object_set_data_full(G_OBJECT(window), "gent-panel-resize", gesture, g_object_unref);
            if (!gent_tray_wayland()) {
                GdkWindow *root = gdk_get_default_root_window();
                gdk_window_set_events(root, gdk_window_get_events(root) | GDK_PROPERTY_CHANGE_MASK);
                gdk_window_add_filter(root, root_property, widget);
                g_signal_connect(window, "destroy", G_CALLBACK(remove_filter), NULL);
            }
        }
        select_output(window, monitor);
        restore(window, TRUE);
    }
    // Tao creates hidden windows with hidden children and accept-focus off.
    gtk_window_set_accept_focus(window, TRUE);
    gtk_widget_show_all(widget);
    gtk_window_present(window);
    if (gent_tray_wayland() && gdk_display_get_n_monitors(gtk_widget_get_display(widget)) > 1) {
        gint64 *frame = g_new(gint64, 1);
        *frame = gdk_frame_clock_get_frame_counter(gtk_widget_get_frame_clock(widget));
        placement_tick = gtk_widget_add_tick_callback(widget, placed, frame, g_free);
    }
}

static void resize(GtkWindow *window, Size start, gint64 dx, gint64 dy) {
    Size next = {
        dx ? CLAMP(start.width + dx, MIN(240, area.width), area.width) : start.width,
        dy ? CLAMP(start.height + dy, MIN(160, area.height), area.height) : start.height
    };
    if (next.width == current.width && next.height == current.height) return;
    resized(next);
    apply(window, FALSE);
}

static void update_drag(GtkWindow *window, gint x, gint y) {
    gint64 dx = (gint64)x - press.x, dy = (gint64)y - press.y;
    switch (press.edge) {
        case GDK_WINDOW_EDGE_NORTH: dx = 0; dy = -dy; break;
        case GDK_WINDOW_EDGE_SOUTH: dx = 0; break;
        case GDK_WINDOW_EDGE_WEST: dx = -dx; dy = 0; break;
        case GDK_WINDOW_EDGE_SOUTH_WEST: dx = -dx; break;
        default: return;
    }
    resize(window, press.size, dx, dy);
}

static gboolean track_x11(gpointer data) {
    GtkWindow *window = GTK_WINDOW(data);
    GdkDevice *device = gdk_seat_get_pointer(gdk_display_get_default_seat(gtk_widget_get_display(GTK_WIDGET(window))));
    gint x, y;
    GdkModifierType mods = 0;
    if (device) gdk_window_get_device_position(gdk_get_default_root_window(), device, &x, &y, &mods);
    if (mods & GDK_BUTTON1_MASK) {
        update_drag(window, x, y);
        return G_SOURCE_CONTINUE;
    }
    x11_timer = 0;
    if (device) update_drag(window, x, y);
    // GTK may still deliver the precise release coordinates after this poll.
    save_size();
    return G_SOURCE_REMOVE;
}

static void begin_drag(GtkWindow *window, GdkWindowEdge edge, GdkDevice *device, gint x, gint y, guint32 time) {
    stop_drag();
    if (gent_tray_wayland()) {
        gboolean horizontal = edge != GDK_WINDOW_EDGE_NORTH && edge != GDK_WINDOW_EDGE_SOUTH;
        gboolean vertical = edge != GDK_WINDOW_EDGE_WEST;
        hints(window, (Size){MIN(240, horizontal ? area.width : current.width),
                             MIN(160, vertical ? area.height : current.height)}, (Size){area.width, area.height});
        wayland_drag = TRUE;
        gdk_window_begin_resize_drag_for_device(gtk_widget_get_window(GTK_WIDGET(window)), edge,
                                                device, GDK_BUTTON_PRIMARY, x, y, time);
        return;
    }
    x11_drag = TRUE;
    press.edge = edge;
    press.x = x;
    press.y = y;
    press.size = current;
    x11_timer = g_timeout_add(16, track_x11, window);
}

static void border_pressed(GtkGestureMultiPress *gesture, gint count G_GNUC_UNUSED,
                           gdouble x, gdouble y, GtkWindow *window) {
    if (!output || fullscreen) return;
    stop_drag();
    GdkEventSequence *sequence = gtk_gesture_single_get_current_sequence(GTK_GESTURE_SINGLE(gesture));
    const GdkEvent *event = gtk_gesture_get_last_event(GTK_GESTURE(gesture), sequence);
    gdouble root_x, root_y;
    if (!event || !gdk_event_get_root_coords(event, &root_x, &root_y)) return;
    GtkWidget *widget = GTK_WIDGET(window);
    gint hit = border_hit(widget, x, y);
    if (hit == PANEL_CONTENT || (hit != UNUSED_BORDER && event->type != GDK_BUTTON_PRESS)) return;
    // Even a press on an inactive part of the native inset must be claimed:
    // otherwise Tauri starts a generic undecorated-window resize there.
    gtk_gesture_set_state(GTK_GESTURE(gesture), GTK_EVENT_SEQUENCE_CLAIMED);
    if (hit != UNUSED_BORDER)
        begin_drag(window, edges[hit], gdk_event_get_device(event), (gint)root_x, (gint)root_y, gdk_event_get_time(event));
}

static void released(GtkGestureMultiPress *gesture, gint count G_GNUC_UNUSED, gdouble x G_GNUC_UNUSED,
                     gdouble y G_GNUC_UNUSED, GtkWindow *window) {
    if (wayland_drag) {
        wayland_released = TRUE;
        await_configure(window);
        return;
    }
    if (!x11_drag) return;
    const GdkEvent *event = gtk_gesture_get_last_event(GTK_GESTURE(gesture), NULL);
    gdouble root_x, root_y;
    if (event && gdk_event_get_root_coords(event, &root_x, &root_y))
        update_drag(window, (gint)root_x, (gint)root_y);
    stop_drag();
}

gboolean gent_dashboard_start_resize(GtkWindow *window, gint edge) {
    GtkWidget *widget = GTK_WIDGET(window);
    if (!output || fullscreen || !gtk_widget_get_mapped(widget) || edge < 0 || (guint)edge >= G_N_ELEMENTS(edges)) return FALSE;
    GdkWindow *surface = gtk_widget_get_window(widget);
    GdkDevice *device = gdk_seat_get_pointer(gdk_display_get_default_seat(gdk_window_get_display(surface)));
    if (!device) return FALSE;
    GdkModifierType mods = 0;
    gint x, y;
    gdk_window_get_device_position(surface, device, &x, &y, &mods);
    if (!(mods & GDK_BUTTON1_MASK)) return FALSE;
    if (gent_tray_wayland()) gdk_window_get_root_coords(surface, x, y, &x, &y);
    else gdk_window_get_device_position(gdk_get_default_root_window(), device, &x, &y, NULL);
    begin_drag(window, edges[edge], device, x, y, gtk_get_current_event_time());
    return TRUE;
}

void gent_dashboard_resize_by(GtkWindow *window, gint dx, gint dy) {
    if (!output || fullscreen || !gtk_widget_get_mapped(GTK_WIDGET(window))) return;
    stop_drag();
    resize(window, current, dx, dy);
    save_size();
}