5 #include <gio/gunixfdlist.h>
6 /* For STDOUT_FILENO */
10 /* ---------------------------------------------------------------------------------------------------- */
12 static GDBusNodeInfo *introspection_data = NULL;
14 /* Introspection data for the service we are exporting */
15 static const gchar introspection_xml[] =
17 " <interface name='org.gtk.GDBus.TestInterface'>"
18 " <annotation name='org.gtk.GDBus.Annotation' value='OnInterface'/>"
19 " <annotation name='org.gtk.GDBus.Annotation' value='AlsoOnInterface'/>"
20 " <method name='HelloWorld'>"
21 " <annotation name='org.gtk.GDBus.Annotation' value='OnMethod'/>"
22 " <arg type='s' name='greeting' direction='in'/>"
23 " <arg type='s' name='response' direction='out'/>"
25 " <method name='EmitSignal'>"
26 " <arg type='d' name='speed_in_mph' direction='in'>"
27 " <annotation name='org.gtk.GDBus.Annotation' value='OnArg'/>"
30 " <method name='GimmeStdout'/>"
31 " <signal name='VelocityChanged'>"
32 " <annotation name='org.gtk.GDBus.Annotation' value='Onsignal'/>"
33 " <arg type='d' name='speed_in_mph'/>"
34 " <arg type='s' name='speed_as_string'>"
35 " <annotation name='org.gtk.GDBus.Annotation' value='OnArg_NonFirst'/>"
38 " <property type='s' name='FluxCapicitorName' access='read'>"
39 " <annotation name='org.gtk.GDBus.Annotation' value='OnProperty'>"
40 " <annotation name='org.gtk.GDBus.Annotation' value='OnAnnotation_YesThisIsCrazy'/>"
43 " <property type='s' name='Title' access='readwrite'/>"
44 " <property type='s' name='ReadingAlwaysThrowsError' access='read'/>"
45 " <property type='s' name='WritingAlwaysThrowsError' access='readwrite'/>"
46 " <property type='s' name='OnlyWritable' access='write'/>"
47 " <property type='s' name='Foo' access='read'/>"
48 " <property type='s' name='Bar' access='read'/>"
52 /* ---------------------------------------------------------------------------------------------------- */
55 handle_method_call (GDBusConnection *connection,
57 const gchar *object_path,
58 const gchar *interface_name,
59 const gchar *method_name,
61 GDBusMethodInvocation *invocation,
64 if (g_strcmp0 (method_name, "HelloWorld") == 0)
66 const gchar *greeting;
68 g_variant_get (parameters, "(&s)", &greeting);
70 if (g_strcmp0 (greeting, "Return Unregistered") == 0)
72 g_dbus_method_invocation_return_error (invocation,
74 G_IO_ERROR_FAILED_HANDLED,
75 "As requested, here's a GError not registered (G_IO_ERROR_FAILED_HANDLED)");
77 else if (g_strcmp0 (greeting, "Return Registered") == 0)
79 g_dbus_method_invocation_return_error (invocation,
81 G_DBUS_ERROR_MATCH_RULE_NOT_FOUND,
82 "As requested, here's a GError that is registered (G_DBUS_ERROR_MATCH_RULE_NOT_FOUND)");
84 else if (g_strcmp0 (greeting, "Return Raw") == 0)
86 g_dbus_method_invocation_return_dbus_error (invocation,
87 "org.gtk.GDBus.SomeErrorName",
88 "As requested, here's a raw D-Bus error");
93 response = g_strdup_printf ("You greeted me with '%s'. Thanks!", greeting);
94 g_dbus_method_invocation_return_value (invocation,
95 g_variant_new ("(s)", response));
99 else if (g_strcmp0 (method_name, "EmitSignal") == 0)
102 gdouble speed_in_mph;
103 gchar *speed_as_string;
105 g_variant_get (parameters, "(d)", &speed_in_mph);
106 speed_as_string = g_strdup_printf ("%g mph!", speed_in_mph);
109 g_dbus_connection_emit_signal (connection,
114 g_variant_new ("(ds)",
118 g_assert_no_error (local_error);
119 g_free (speed_as_string);
121 g_dbus_method_invocation_return_value (invocation, NULL);
123 else if (g_strcmp0 (method_name, "GimmeStdout") == 0)
126 if (g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING)
129 GUnixFDList *fd_list;
132 fd_list = g_unix_fd_list_new ();
134 g_unix_fd_list_append (fd_list, STDOUT_FILENO, &error);
135 g_assert_no_error (error);
137 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
138 g_dbus_message_set_unix_fd_list (reply, fd_list);
141 g_dbus_connection_send_message (connection,
143 NULL, /* out_serial */
145 g_assert_no_error (error);
147 g_object_unref (invocation);
148 g_object_unref (fd_list);
149 g_object_unref (reply);
153 g_dbus_method_invocation_return_dbus_error (invocation,
154 "org.gtk.GDBus.Failed",
155 "Your message bus daemon does not support file descriptor passing (need D-Bus >= 1.3.0)");
158 g_dbus_method_invocation_return_dbus_error (invocation,
159 "org.gtk.GDBus.NotOnUnix",
160 "Your OS does not support file descriptor passing");
165 static gchar *_global_title = NULL;
167 static gboolean swap_a_and_b = FALSE;
170 handle_get_property (GDBusConnection *connection,
172 const gchar *object_path,
173 const gchar *interface_name,
174 const gchar *property_name,
181 if (g_strcmp0 (property_name, "FluxCapicitorName") == 0)
183 ret = g_variant_new_string ("DeLorean");
185 else if (g_strcmp0 (property_name, "Title") == 0)
187 if (_global_title == NULL)
188 _global_title = g_strdup ("Back To C!");
189 ret = g_variant_new_string (_global_title);
191 else if (g_strcmp0 (property_name, "ReadingAlwaysThrowsError") == 0)
196 "Hello %s. I thought I said reading this property "
197 "always results in an error. kthxbye",
200 else if (g_strcmp0 (property_name, "WritingAlwaysThrowsError") == 0)
202 ret = g_variant_new_string ("There's no home like home");
204 else if (g_strcmp0 (property_name, "Foo") == 0)
206 ret = g_variant_new_string (swap_a_and_b ? "Tock" : "Tick");
208 else if (g_strcmp0 (property_name, "Bar") == 0)
210 ret = g_variant_new_string (swap_a_and_b ? "Tick" : "Tock");
217 handle_set_property (GDBusConnection *connection,
219 const gchar *object_path,
220 const gchar *interface_name,
221 const gchar *property_name,
226 if (g_strcmp0 (property_name, "Title") == 0)
228 if (g_strcmp0 (_global_title, g_variant_get_string (value, NULL)) != 0)
230 GVariantBuilder *builder;
233 g_free (_global_title);
234 _global_title = g_variant_dup_string (value, NULL);
237 builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
238 g_variant_builder_add (builder,
241 g_variant_new_string (_global_title));
242 g_dbus_connection_emit_signal (connection,
245 "org.freedesktop.DBus.Properties",
247 g_variant_new ("(sa{sv}as)",
252 g_assert_no_error (local_error);
255 else if (g_strcmp0 (property_name, "ReadingAlwaysThrowsError") == 0)
257 /* do nothing - they can't read it after all! */
259 else if (g_strcmp0 (property_name, "WritingAlwaysThrowsError") == 0)
264 "Hello AGAIN %s. I thought I said writing this property "
265 "always results in an error. kthxbye",
269 return *error == NULL;
274 static const GDBusInterfaceVTable interface_vtable =
281 /* ---------------------------------------------------------------------------------------------------- */
284 on_timeout_cb (gpointer user_data)
286 GDBusConnection *connection = G_DBUS_CONNECTION (user_data);
287 GVariantBuilder *builder;
288 GVariantBuilder *invalidated_builder;
291 swap_a_and_b = !swap_a_and_b;
294 builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
295 invalidated_builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
296 g_variant_builder_add (builder,
299 g_variant_new_string (swap_a_and_b ? "Tock" : "Tick"));
300 g_variant_builder_add (builder,
303 g_variant_new_string (swap_a_and_b ? "Tick" : "Tock"));
304 g_dbus_connection_emit_signal (connection,
306 "/org/gtk/GDBus/TestObject",
307 "org.freedesktop.DBus.Properties",
309 g_variant_new ("(sa{sv}as)",
310 "org.gtk.GDBus.TestInterface",
312 invalidated_builder),
314 g_assert_no_error (error);
320 /* ---------------------------------------------------------------------------------------------------- */
323 on_bus_acquired (GDBusConnection *connection,
327 guint registration_id;
329 registration_id = g_dbus_connection_register_object (connection,
330 "/org/gtk/GDBus/TestObject",
331 introspection_data->interfaces[0],
333 NULL, /* user_data */
334 NULL, /* user_data_free_func */
335 NULL); /* GError** */
336 g_assert (registration_id > 0);
338 /* swap value of properties Foo and Bar every two seconds */
339 g_timeout_add_seconds (2,
345 on_name_acquired (GDBusConnection *connection,
352 on_name_lost (GDBusConnection *connection,
360 main (int argc, char *argv[])
367 /* We are lazy here - we don't want to manually provide
368 * the introspection data structures - so we just build
371 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
372 g_assert (introspection_data != NULL);
374 owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
375 "org.gtk.GDBus.TestServer",
376 G_BUS_NAME_OWNER_FLAGS_NONE,
383 loop = g_main_loop_new (NULL, FALSE);
384 g_main_loop_run (loop);
386 g_bus_unown_name (owner_id);
388 g_dbus_node_info_unref (introspection_data);