New paste Repaste Download
#include <gio/gio.h>
#include <stdio.h>
static void
on_my_signal(GDBusConnection *connection,
             const gchar     *sender_name,
             const gchar     *object_path,
             const gchar     *interface_name,
             const gchar     *signal_name,
             GVariant        *parameters,
             gpointer         user_data)
{
    g_print("Received a signal: %s\n", signal_name ? signal_name : "(null)");
    g_print("interface: %s\n", interface_name ? interface_name : "(null)");
    g_print("arg 1: %x\n", &connection);
    g_print("arg 2: %x\n", &sender_name);
    g_print("arg 3: %x\n", &object_path);
}
int
main(int argc, char *argv[])
{
    GMainLoop *loop;
    GDBusConnection *conn;
    GError *error = NULL;
    loop = g_main_loop_new(NULL, FALSE);
    /* Connect to session bus (equivalent of g-bus-get-sync 'session) */
    conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
    if (!conn) {
        g_printerr("Cannot get session bus: %s\n", error->message);
        g_error_free(error);
        return 1;
    }
    /* Subscribe to DBus signals */
    guint subscription_id = g_dbus_connection_signal_subscribe(
        conn,
        "org.gnu.test.DBus",      /* sender */
        "org.gnu.test.DBus",      /* interface */
        "Signal1",                /* signal */
        "/org/gnu/test/DBus",     /* object path */
        NULL,                     /* arg0 filter */
        G_DBUS_SIGNAL_FLAGS_NONE,
        on_my_signal,             /* callback */
        NULL,                     /* user data */
        NULL                      /* user data free func */
    );
    g_print("subscription: %u\n", subscription_id);
    /*
     * Call MyMethod1
     */
    GVariant *result1 = g_dbus_connection_call_sync(
        conn,
        "org.gnu.test.DBus",         /* bus name */
        "/org/gnu/test/DBus",        /* object path */
        "org.gnu.test.DBus",         /* interface */
        "Method1",                 /* method */
        g_variant_new("(s)", "Lucky number"),
        G_VARIANT_TYPE("(x)"),       /* expected reply type */
        G_DBUS_CALL_FLAGS_NONE,
        -1,                          /* timeout */
        NULL,                        /* cancellable */
        &error
    );
    if (!result1) {
        g_printerr("Method1 call failed: %s\n", error->message);
        g_error_free(error);
        return 1;
    }
    gint64 out1;
    g_variant_get(result1, "(x)", &out1);
    g_print("Result of Method1: %ld\n", (long)out1);
    g_variant_unref(result1);
    /*
     * Call MyMethod2
     */
    GVariant *result2 = g_dbus_connection_call_sync(
        conn,
        "org.gnu.test.DBus",     /* bus name */
        "/org/gnu/test/DBus",    /* object path */
        "org.gnu.test.DBus",     /* interface */
        "Method2",                 /* method */
        g_variant_new("(x)", (gint64)10),
        G_VARIANT_TYPE("(sx)"),      /* expected reply type */
        G_DBUS_CALL_FLAGS_NONE,
        -1,
        NULL,
        &error
    );
    if (!result2) {
        g_printerr("Method2 call failed: %s\n", error->message);
        g_error_free(error);
        return 1;
    }
    gchar *str_out;
    gint64 num_out;
    g_variant_get(result2, "(sx)", &str_out, &num_out);
    g_print("Result of Method2:\n");
    g_print("%s\n", str_out);
    g_print("%ld\n", (long)num_out);
    g_free(str_out);
    g_variant_unref(result2);
    /* Run main loop to receive signals */
    g_main_loop_run(loop);
    g_main_loop_unref(loop);
    return 0;
}
Filename: None. Size: 4kb. View raw, , hex, or download this file.

This paste expires on 2026-01-22 09:30:17.245833+00:00. Pasted through web.