#include <gio/gio.h>
int main(int argc, char **argv) {
GError *error = NULL;
GDBusConnection *connection = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
if (error) {
g_printerr("Failed to get bus connection: %s\n", error->message);
g_error_free(error);
return 1;
}
gchar *name_owner = NULL;
gboolean result = g_dbus_connection_call_sync(connection,
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
"ListNames",
NULL,
G_VARIANT_TYPE("(as)"),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&name_owner,
&error);
if (error) {
g_printerr("Failed to call ListNames: %s\n", error->message);
g_error_free(error);
return 1;
}
GVariant *name_array = g_variant_get_child_value(name_owner, 0);
gchar **names = g_variant_dup_strv(name_array, NULL);
g_variant_unref(name_owner);
g_variant_unref(name_array);
for (int i = 0; names[i] != NULL; i++) {
GDBusProxy *proxy = g_dbus_proxy_new_sync(connection,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
names[i],
"/",
"org.freedesktop.DBus.Introspectable",
NULL,
&error);
if (error) {
g_printerr("Failed to create proxy for %s: %s\n", names[i], error->message);
g_error_free(error);
continue;
}
gchar *xml_data = NULL;
result = g_dbus_proxy_call_sync(proxy,
"Introspect",
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&xml_data,
NULL,
&error);
if (error) {
g_printerr("Failed to call Introspect for %s: %s\n", names[i], error->message);
g_error_free(error);
continue;
}
GDBusNodeInfo *node_info = g_dbus_node_info_new_for_xml(xml_data, NULL);
GDBusInterfaceInfo **interfaces = node_info->interfaces;
for (int j = 0; interfaces[j] != NULL; j++) {
if (g_strcmp0(interfaces[j]->name, "org.example.MyInterface") == 0) {
GDBusProxy *my_proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
names[i],
"/",
"org.example.MyInterface",
NULL,
&error);
if (error) {
g_printerr("Failed to create proxy for %s: %s\n", names[i], error->message);
g_error_free(error);
continue;
}
GVariant *response = g_dbus_proxy_call_sync(my_proxy,
"MyMethod",
g_variant_new("(s)", "Hello"),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
NULL,
NULL,
&error);
if (error) {
g_printerr("Failed to call MyMethod for %s: %s\n", names[i], error->message);
g_error_free(error);
continue;
}
g_variant_unref(response);
g_object_unref(my_proxy);
}
}
g_dbus_node_info_unref(node_info);
g_free(xml_data);
g_object_unref(proxy);
}
g_strfreev(names);
g_dbus_connection_unref(connection);
return 0;
}