gent-link 0.2.23

Connect Codex and Claude Code chats across machines with the Gent CLI and desktop companion.
// GTK3's X11 fallback applies the global EWMH workarea only to the primary
// monitor. On older WMs use the panel's partial struts for each physical output.
#include "linux_tray.h"
#include <gdk/gdkx.h>

char *gent_dashboard_output_key(GdkMonitor *monitor) {
    GdkDisplay *display = gdk_monitor_get_display(monitor);
    const char *backend = GDK_IS_X11_DISPLAY(display) ? "x11" : "wayland";
    const char *model = gdk_monitor_get_model(monitor);
    const char *make = gdk_monitor_get_manufacturer(monitor);
    gint matches = 0;
    for (gint i = 0; i < gdk_display_get_n_monitors(display); ++i) {
        GdkMonitor *other = gdk_display_get_monitor(display, i);
        if (other == monitor) {
            // GTK3 exposes the XRandR or xdg-output connector via GdkScreen.
            G_GNUC_BEGIN_IGNORE_DEPRECATIONS
            g_autofree char *plug = gdk_screen_get_monitor_plug_name(gdk_display_get_default_screen(display), i);
            G_GNUC_END_IGNORE_DEPRECATIONS
            if (plug && *plug) return g_strdup_printf("%s:%s", backend, plug);
        }
        if (!g_strcmp0(model, gdk_monitor_get_model(other)) &&
            !g_strcmp0(make, gdk_monitor_get_manufacturer(other))) ++matches;
    }
    if (!model || !*model || matches != 1) return NULL;
    g_autofree char *vendor = g_uri_escape_string(make ? make : "", NULL, FALSE);
    g_autofree char *product = g_uri_escape_string(model, NULL, FALSE);
    return g_strdup_printf("%s-model:%s/%s", backend, vendor, product);
}

static gboolean cardinals(GdkWindow *window, const char *name, gulong *out, guint count) {
    g_autofree guchar *data = NULL;
    gint format = 0, length = 0;
    GdkAtom type;
    GdkAtom cardinal = gdk_atom_intern_static_string("CARDINAL");
    if (!gdk_property_get(window, gdk_atom_intern(name, FALSE), cardinal,
                          0, 4 * count, FALSE, &type, &format, &length, &data) ||
        type != cardinal || format != 32 || length != (gint)(count * sizeof(gulong)))
        return FALSE;
    memcpy(out, data, length);
    return TRUE;
}

GdkRectangle gent_dashboard_workarea(GdkMonitor *monitor) {
    GdkRectangle fallback, bounds;
    gdk_monitor_get_workarea(monitor, &fallback);
    GdkScreen *screen = gdk_display_get_default_screen(gdk_monitor_get_display(monitor));
    if (!GDK_IS_X11_SCREEN(screen) || gdk_x11_screen_supports_net_wm_hint(
            screen, gdk_atom_intern_static_string("_GTK_WORKAREAS")))
        return fallback;

    gdk_monitor_get_geometry(monitor, &bounds);
    GdkWindow *root = gdk_screen_get_root_window(screen);
    gint scale = gdk_window_get_scale_factor(root);
    gint64 root_width = (gint64)gdk_window_get_width(root) * scale;
    gint64 root_height = (gint64)gdk_window_get_height(root) * scale;
    gint64 x = (gint64)bounds.x * scale, y = (gint64)bounds.y * scale;
    gint64 right = x + (gint64)bounds.width * scale, bottom = y + (gint64)bounds.height * scale;
    gint64 left_limit = x, top_limit = y, right_limit = right, bottom_limit = bottom;
    guint32 desktop = gdk_x11_screen_get_current_desktop(screen);
    gboolean has_strut = FALSE;
    // A listed client may disappear before XGetWindowProperty completes.
    GdkDisplay *display = gdk_screen_get_display(screen);
    gdk_x11_display_error_trap_push(display);
    GList *windows = gdk_screen_get_window_stack(screen);
    for (GList *p = windows; p; p = p->next) {
        gulong s[12] = {0}, desk;
        if (!cardinals(p->data, "_NET_WM_STRUT_PARTIAL", s, 12)) {
            if (!cardinals(p->data, "_NET_WM_STRUT", s, 4)) continue;
            s[5] = s[7] = root_height - 1;
            s[9] = s[11] = root_width - 1;
        }
        if (!(s[0] || s[1] || s[2] || s[3])) continue;
        if (cardinals(p->data, "_NET_WM_DESKTOP", &desk, 1) && desk != desktop && desk != G_MAXUINT32)
            continue;
        has_strut = TRUE;
        if (s[0] && (gint64)s[4] < bottom && (gint64)s[5] >= y)
            left_limit = MAX(left_limit, (gint64)s[0]);
        if (s[1] && (gint64)s[6] < bottom && (gint64)s[7] >= y)
            right_limit = MIN(right_limit, root_width - (gint64)s[1]);
        if (s[2] && (gint64)s[8] < right && (gint64)s[9] >= x)
            top_limit = MAX(top_limit, (gint64)s[2]);
        if (s[3] && (gint64)s[10] < right && (gint64)s[11] >= x)
            bottom_limit = MIN(bottom_limit, root_height - (gint64)s[3]);
    }
    g_list_free_full(windows, g_object_unref);
    gdk_x11_display_error_trap_pop_ignored(display);
    if (!has_strut) return fallback;
    gint left = (left_limit + scale - 1) / scale, top = (top_limit + scale - 1) / scale;
    gint width = right_limit / scale - left, height = bottom_limit / scale - top;
    return width > 0 && height > 0 ? (GdkRectangle){left, top, width, height} : fallback;
}