Developer-Dashboard

 view release on metacpan or  search on metacpan

native/system_menu_helper_linux.c  view on Meta::CPAN

/* ---------------------------- item model --------------------------------- */

static DDItem *item_from_json(JValue *obj, DDItem *parent)
{
    const char *name;
    const char *icon;
    JValue *children;
    DDItem *item;
    size_t i;

    if (!obj || obj->type != J_OBJECT) return NULL;
    name = j_get_string(obj, "name");
    icon = j_get_string(obj, "icon");
    if (!name || !icon) return NULL;

    item = (DDItem *)xcalloc(1, sizeof(*item));
    item->name_utf8 = xstrdup(name);
    item->icon_utf8 = xstrdup(icon);
    item->clickable = j_get(obj, "click") != NULL;
    item->click_stay = j_get_bool(obj, "click_stay", 0);
    item->parent = parent;

    children = j_get(obj, "menu");
    if (children && children->type == J_ARRAY) {
        item->children = (DDItem **)xcalloc(
            children->u.array.count ? children->u.array.count : 1,
            sizeof(DDItem *)
        );
        for (i = 0; i < children->u.array.count; i++) {
            DDItem *child = item_from_json(children->u.array.items[i], item);
            if (child) item->children[item->child_count++] = child;
        }
    }

    return item;
}

static void item_mark_removed(DDItem *item)
{
    size_t i;
    if (!item) return;
    item->removed = 1;
    for (i = 0; i < item->child_count; i++) item_mark_removed(item->children[i]);
}

/* Release an item subtree, its AppIndicator, and all temporary icon assets.
 * Input: owned menu item tree root.
 * Output: no value; releases native and heap resources, reporting filesystem failures.
 */
static void item_free(DDItem *item)
{
    size_t i;
    if (!item) return;
    for (i = 0; i < item->child_count; i++) item_free(item->children[i]);
    if (item->indicator) {
        app_indicator_set_status(item->indicator, APP_INDICATOR_STATUS_PASSIVE);
        g_object_unref(item->indicator);
    }
    for (i = 0; i < item->icon_file_count; i++) {
        if (item->icon_files[i] && unlink(item->icon_files[i]) != 0 && errno != ENOENT)
            fprintf(stderr, "System::Menu: unable to remove indicator icon '%s': %s\n",
                    item->icon_files[i], strerror(errno));
        free(item->icon_files[i]);
    }
    free(item->icon_files);
    free(item->icon_file_times);
    if (item->icon_theme_dir) {
        if (rmdir(item->icon_theme_dir) != 0 && errno != ENOENT)
            fprintf(stderr, "System::Menu: unable to remove icon directory '%s': %s\n",
                    item->icon_theme_dir, strerror(errno));
        g_free(item->icon_theme_dir);
    }
    free(item->children);
    free(item->name_utf8);
    free(item->icon_utf8);
    free(item);
}

static DDItem *find_item_rec(DDItem *item, const char *name)
{
    size_t i;
    DDItem *found;
    if (!item) return NULL;
    if (item->name_utf8 && strcmp(item->name_utf8, name) == 0) return item;
    for (i = 0; i < item->child_count; i++) {
        found = find_item_rec(item->children[i], name);
        if (found) return found;
    }
    return NULL;
}

static DDItem *find_item(const char *name)
{
    size_t i;
    DDItem *found;
    for (i = 0; i < g_root_count; i++) {
        found = find_item_rec(g_roots[i], name);
        if (found) return found;
    }
    return NULL;
}

/* Remove every active top-level indicator and release its complete item tree.
 * Input: none; uses the process-global root array.
 * Output: no value; leaves the global root array empty.
 */
static void clear_roots(void)
{
    size_t i;
    for (i = 0; i < g_root_count; i++) {
        DDItem *root = g_roots[i];
        if (root->indicator)
            app_indicator_set_status(root->indicator, APP_INDICATOR_STATUS_PASSIVE);
        item_free(root);
    }
    free(g_roots);
    g_roots = NULL;
    g_root_count = 0;
}

/* ---------------------------- icon rendering ----------------------------- */

static GdkPixbuf *render_text_icon(const char *text)
{
    const int size = 24;
    cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size, size);
    cairo_t *cr = cairo_create(surface);
    PangoLayout *layout = pango_cairo_create_layout(cr);
    PangoFontDescription *font = pango_font_description_from_string("Sans 13");
    PangoRectangle ink, logical;
    double scale = 1.0;
    double x, y;
    GdkPixbuf *pixbuf;

    cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
    cairo_set_source_rgba(cr, 0, 0, 0, 0);
    cairo_paint(cr);
    cairo_set_operator(cr, CAIRO_OPERATOR_OVER);

    pango_layout_set_font_description(layout, font);
    pango_layout_set_text(layout, text ? text : "", -1);
    pango_layout_get_pixel_extents(layout, &ink, &logical);

    if (logical.width > size - 2 && logical.width > 0)
        scale = (double)(size - 2) / (double)logical.width;
    if (logical.height * scale > size - 2 && logical.height > 0)
        scale = (double)(size - 2) / (double)logical.height;

    x = ((double)size - logical.width * scale) / 2.0 - logical.x * scale;
    y = ((double)size - logical.height * scale) / 2.0 - logical.y * scale;

    cairo_translate(cr, x, y);
    cairo_scale(cr, scale, scale);
    /* Ubuntu's top panel is normally dark. Color emoji keep their own glyph
     * colors; ordinary text such as "OK" uses white for readability. */
    cairo_set_source_rgba(cr, 1, 1, 1, 1);
    pango_cairo_show_layout(cr, layout);

    pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, size, size);

    pango_font_description_free(font);
    g_object_unref(layout);
    cairo_destroy(cr);
    cairo_surface_destroy(surface);
    return pixbuf;
}

/* Render and publish one root's current text icon through Ayatana AppIndicator.
 * Input: root item with an active AppIndicator and UTF-8 icon text.
 * Output: no value; updates the desktop icon and retains recent icon assets.
 */
static void update_status_icon(DDItem *item)
{
    GdkPixbuf *pixbuf;
    GError *error = NULL;
    char *filename;
    char *icon_name;
    size_t i;
    time_t now = time(NULL);
    if (!item || !item->indicator) return;

    if (!item->icon_theme_dir) {
        item->icon_theme_dir = g_dir_make_tmp("developer-dashboard-menu-XXXXXX", &error);
        if (!item->icon_theme_dir) {
            fprintf(stderr, "System::Menu: unable to create indicator icon directory: %s\n",
                    error ? error->message : "unknown error");
            if (error) g_error_free(error);
            exit(2);
        }
        app_indicator_set_icon_theme_path(item->indicator, item->icon_theme_dir);
    }

    /* Remove icon assets only after a generous grace period: desktop shells
     * request theme-path images asynchronously after the D-Bus update. */
    for (i = 0; i < item->icon_file_count; ) {
        if (now - item->icon_file_times[i] > 60) {
            if (unlink(item->icon_files[i]) != 0 && errno != ENOENT)
                fprintf(stderr, "System::Menu: unable to remove stale indicator icon '%s': %s\n",
                        item->icon_files[i], strerror(errno));
            free(item->icon_files[i]);
            item->icon_file_count--;
            item->icon_files[i] = item->icon_files[item->icon_file_count];
            item->icon_file_times[i] = item->icon_file_times[item->icon_file_count];
        }
        else {
            i++;
        }
    }

    pixbuf = render_text_icon(item->icon_utf8);
    if (pixbuf) {
        item->icon_sequence++;
        g_icon_sequence++;
        icon_name = g_strdup_printf("dd-icon-%lu", g_icon_sequence);
        filename = g_strdup_printf("%s/%s.png", item->icon_theme_dir,
                                    icon_name);
        if (!gdk_pixbuf_save(pixbuf, filename, "png", &error, NULL)) {
            fprintf(stderr, "System::Menu: unable to save indicator icon: %s\n",
                    error ? error->message : "unknown error");
            if (error) g_error_free(error);
            g_free(filename);
            g_free(icon_name);
            g_object_unref(pixbuf);
            exit(2);
        }
        item->icon_files = (char **)xrealloc(item->icon_files,
            sizeof(char *) * (item->icon_file_count + 1));
        item->icon_file_times = (time_t *)xrealloc(item->icon_file_times,
            sizeof(time_t) * (item->icon_file_count + 1));
        item->icon_files[item->icon_file_count] = xstrdup(filename);
        item->icon_file_times[item->icon_file_count] = now;
        item->icon_file_count++;
        app_indicator_set_icon_full(item->indicator, icon_name, item->name_utf8);
        g_free(filename);
        g_free(icon_name);
        g_object_unref(pixbuf);
    }
}

/* ------------------------------ menus ------------------------------------ */

static void on_menu_activate(GtkMenuItem *menu_item, gpointer data)
{
    (void)menu_item;
    emit_click((DDItem *)data);
}

static gboolean on_stay_button_press(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
    DDItem *item = (DDItem *)data;
    (void)widget;
    if (event && event->button == 1) {
        emit_click(item);
        /* Stop GtkMenuShell from turning this into normal menu-item activation.
         * That keeps the current menu/submenu tracking session visible. */
        return TRUE;
    }
    return FALSE;
}

static GtkWidget *build_menu(DDItem *owner)
{
    GtkWidget *menu = gtk_menu_new();
    size_t i;

    for (i = 0; i < owner->child_count; i++) {
        DDItem *item = owner->children[i];
        GtkWidget *mi = gtk_menu_item_new_with_label(item->icon_utf8);
        item->menu_item = mi;

        if (item->child_count > 0) {
            item->menu = build_menu(item);
            gtk_menu_item_set_submenu(GTK_MENU_ITEM(mi), item->menu);
        }
        else if (item->clickable) {
            if (item->click_stay) {
                gtk_widget_add_events(mi, GDK_BUTTON_PRESS_MASK);
                g_signal_connect(mi, "button-press-event",
                    G_CALLBACK(on_stay_button_press), item);
            }
            else {
                g_signal_connect(mi, "activate", G_CALLBACK(on_menu_activate), item);
            }
        }
        else {
            gtk_widget_set_sensitive(mi, FALSE);
        }

        gtk_menu_shell_append(GTK_MENU_SHELL(menu), mi);
        gtk_widget_show(mi);
    }

    return menu;
}

/* Build the root menu; an item without children remains clickable via its menu.
 * Input: root item with its configuration children.
 * Output: GTK menu attached to the root AppIndicator.
 */
static GtkWidget *build_root_menu(DDItem *root)
{
    GtkWidget *menu;
    GtkWidget *menu_item;
    if (root->child_count > 0) return build_menu(root);
    menu = gtk_menu_new();
    menu_item = gtk_menu_item_new_with_label(root->icon_utf8);
    root->menu_item = menu_item;
    if (root->clickable)
        g_signal_connect(menu_item, "activate", G_CALLBACK(on_menu_activate), root);
    else
        gtk_widget_set_sensitive(menu_item, FALSE);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
    gtk_widget_show(menu_item);
    return menu;
}

static void install_roots(JValue *items)
{
    size_t i;
    clear_roots();
    if (!items || items->type != J_ARRAY) return;

    g_roots = (DDItem **)xcalloc(items->u.array.count ? items->u.array.count : 1,
                                 sizeof(DDItem *));

    for (i = 0; i < items->u.array.count; i++) {
        DDItem *root = item_from_json(items->u.array.items[i], NULL);
        char *indicator_id;
        if (!root) continue;

        root->menu = build_root_menu(root);
        indicator_id = g_strdup_printf("developer-dashboard-%lu", ++g_icon_sequence);
        /* The package's public convenience constructors are deprecated;
         * initialize the same documented GObject properties directly. */
        root->indicator = APP_INDICATOR(g_object_new(
            APP_INDICATOR_TYPE,
            "id", indicator_id,
            "icon-name", "applications-system",
            "category", APP_INDICATOR_CATEGORY_APPLICATION_STATUS,
            NULL));
        g_free(indicator_id);
        if (!root->indicator) {
            fprintf(stderr, "System::Menu: unable to create Ubuntu AppIndicator\n");
            item_free(root);
            continue;
        }
        app_indicator_set_menu(root->indicator, GTK_MENU(root->menu));
        update_status_icon(root);
        app_indicator_set_status(root->indicator, APP_INDICATOR_STATUS_ACTIVE);

        g_roots[g_root_count++] = root;
    }
}

static void set_item_icon(DDItem *item, const char *icon)
{
    if (!item || item->removed || !icon) return;
    free(item->icon_utf8);
    item->icon_utf8 = xstrdup(icon);

    if (item->indicator) {
        update_status_icon(item);
    }
    else if (item->menu_item) {
        gtk_menu_item_set_label(GTK_MENU_ITEM(item->menu_item), item->icon_utf8);
    }
}

static void remove_item(DDItem *item)
{
    if (!item || item->removed) return;
    item_mark_removed(item);

    if (item->indicator) {
        app_indicator_set_status(item->indicator, APP_INDICATOR_STATUS_PASSIVE);
    }
    else if (item->menu_item) {
        gtk_widget_destroy(item->menu_item);
        item->menu_item = NULL;
    }
}

/* --------------------------- command processing -------------------------- */

static void handle_command(const char *line)
{
    JValue *cmd = j_parse(line);
    const char *op;

    if (!cmd || cmd->type != J_OBJECT) {
        j_free(cmd);
        return;
    }

    op = j_get_string(cmd, "op");
    if (!op) {
        j_free(cmd);
        return;
    }

    if (strcmp(op, "run") == 0) {
        install_roots(j_get(cmd, "items"));
    }

native/system_menu_helper_linux.c  view on Meta::CPAN

        if (name && icon) set_item_icon(find_item(name), icon);
    }
    else if (strcmp(op, "remove") == 0) {
        const char *name = j_get_string(cmd, "name");
        if (name) remove_item(find_item(name));
    }
    else if (strcmp(op, "stop") == 0) {
        j_free(cmd);
        clear_roots();
        gtk_main_quit();
        return;
    }

    j_free(cmd);
}

static gboolean on_stdin_ready(GIOChannel *source, GIOCondition condition, gpointer data)
{
    (void)data;
    if (condition & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
        clear_roots();
        gtk_main_quit();
        return FALSE;
    }

    while (condition & G_IO_IN) {
        gchar *line = NULL;
        gsize length = 0;
        GError *error = NULL;
        GIOStatus status = g_io_channel_read_line(source, &line, &length, NULL, &error);

        if (status == G_IO_STATUS_NORMAL && line) {
            while (length > 0 && (line[length - 1] == '\n' || line[length - 1] == '\r'))
                line[--length] = '\0';
            if (length > 0) handle_command(line);
            g_free(line);
            continue;
        }

        if (line) g_free(line);
        if (error) g_error_free(error);
        if (status == G_IO_STATUS_AGAIN) break;
        if (status == G_IO_STATUS_EOF) {
            clear_roots();
            gtk_main_quit();
            return FALSE;
        }
        break;
    }

    return TRUE;
}

int main(int argc, char **argv)
{
    GError *error = NULL;
    GIOFlags flags;

    if (!gtk_init_check(&argc, &argv)) {
        fprintf(stderr,
            "Developer::Dashboard::System::Menu: GTK could not connect to the Ubuntu desktop session. "
            "A graphical session with AppIndicator support is required.\n");
        return 2;
    }

    g_stdin_channel = g_io_channel_unix_new(STDIN_FILENO);
    g_io_channel_set_encoding(g_stdin_channel, NULL, &error);
    if (error) {
        fprintf(stderr, "System::Menu: stdin encoding setup failed: %s\n", error->message);
        g_error_free(error);
        return 2;
    }
    flags = g_io_channel_get_flags(g_stdin_channel);
    g_io_channel_set_flags(g_stdin_channel, flags | G_IO_FLAG_NONBLOCK, NULL);
    g_io_add_watch(g_stdin_channel, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
                   on_stdin_ready, NULL);

    gtk_main();

    clear_roots();
    if (g_stdin_channel) g_io_channel_unref(g_stdin_channel);
    return 0;
}



( run in 0.762 second using v1.01-cache-2.11-cpan-60884a8df76 )