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 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
144 NULL, /* out_serial */
146 g_assert_no_error (error);
148 g_object_unref (invocation);
149 g_object_unref (fd_list);
150 g_object_unref (reply);
154 g_dbus_method_invocation_return_dbus_error (invocation,
155 "org.gtk.GDBus.Failed",
156 "Your message bus daemon does not support file descriptor passing (need D-Bus >= 1.3.0)");
159 g_dbus_method_invocation_return_dbus_error (invocation,
160 "org.gtk.GDBus.NotOnUnix",
161 "Your OS does not support file descriptor passing");
166 static gchar *_global_title = NULL;
168 static gboolean swap_a_and_b = FALSE;
171 handle_get_property (GDBusConnection *connection,
173 const gchar *object_path,
174 const gchar *interface_name,
175 const gchar *property_name,
182 if (g_strcmp0 (property_name, "FluxCapicitorName") == 0)
184 ret = g_variant_new_string ("DeLorean");
186 else if (g_strcmp0 (property_name, "Title") == 0)
188 if (_global_title == NULL)
189 _global_title = g_strdup ("Back To C!");
190 ret = g_variant_new_string (_global_title);
192 else if (g_strcmp0 (property_name, "ReadingAlwaysThrowsError") == 0)
197 "Hello %s. I thought I said reading this property "
198 "always results in an error. kthxbye",
201 else if (g_strcmp0 (property_name, "WritingAlwaysThrowsError") == 0)
203 ret = g_variant_new_string ("There's no home like home");
205 else if (g_strcmp0 (property_name, "Foo") == 0)
207 ret = g_variant_new_string (swap_a_and_b ? "Tock" : "Tick");
209 else if (g_strcmp0 (property_name, "Bar") == 0)
211 ret = g_variant_new_string (swap_a_and_b ? "Tick" : "Tock");
218 handle_set_property (GDBusConnection *connection,
220 const gchar *object_path,
221 const gchar *interface_name,
222 const gchar *property_name,
227 if (g_strcmp0 (property_name, "Title") == 0)
229 if (g_strcmp0 (_global_title, g_variant_get_string (value, NULL)) != 0)
231 GVariantBuilder *builder;
234 g_free (_global_title);
235 _global_title = g_variant_dup_string (value, NULL);
238 builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
239 g_variant_builder_add (builder,
242 g_variant_new_string (_global_title));
243 g_dbus_connection_emit_signal (connection,
246 "org.freedesktop.DBus.Properties",
248 g_variant_new ("(sa{sv}as)",
253 g_assert_no_error (local_error);
256 else if (g_strcmp0 (property_name, "ReadingAlwaysThrowsError") == 0)
258 /* do nothing - they can't read it after all! */
260 else if (g_strcmp0 (property_name, "WritingAlwaysThrowsError") == 0)
265 "Hello AGAIN %s. I thought I said writing this property "
266 "always results in an error. kthxbye",
270 return *error == NULL;
275 static const GDBusInterfaceVTable interface_vtable =
282 /* ---------------------------------------------------------------------------------------------------- */
285 on_timeout_cb (gpointer user_data)
287 GDBusConnection *connection = G_DBUS_CONNECTION (user_data);
288 GVariantBuilder *builder;
289 GVariantBuilder *invalidated_builder;
292 swap_a_and_b = !swap_a_and_b;
295 builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
296 invalidated_builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
297 g_variant_builder_add (builder,
300 g_variant_new_string (swap_a_and_b ? "Tock" : "Tick"));
301 g_variant_builder_add (builder,
304 g_variant_new_string (swap_a_and_b ? "Tick" : "Tock"));
305 g_dbus_connection_emit_signal (connection,
307 "/org/gtk/GDBus/TestObject",
308 "org.freedesktop.DBus.Properties",
310 g_variant_new ("(sa{sv}as)",
311 "org.gtk.GDBus.TestInterface",
313 invalidated_builder),
315 g_assert_no_error (error);
321 /* ---------------------------------------------------------------------------------------------------- */
324 on_bus_acquired (GDBusConnection *connection,
328 guint registration_id;
330 registration_id = g_dbus_connection_register_object (connection,
331 "/org/gtk/GDBus/TestObject",
332 introspection_data->interfaces[0],
334 NULL, /* user_data */
335 NULL, /* user_data_free_func */
336 NULL); /* GError** */
337 g_assert (registration_id > 0);
339 /* swap value of properties Foo and Bar every two seconds */
340 g_timeout_add_seconds (2,
346 on_name_acquired (GDBusConnection *connection,
353 on_name_lost (GDBusConnection *connection,
361 main (int argc, char *argv[])
366 /* We are lazy here - we don't want to manually provide
367 * the introspection data structures - so we just build
370 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
371 g_assert (introspection_data != NULL);
373 owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
374 "org.gtk.GDBus.TestServer",
375 G_BUS_NAME_OWNER_FLAGS_NONE,
382 loop = g_main_loop_new (NULL, FALSE);
383 g_main_loop_run (loop);
385 g_bus_unown_name (owner_id);
387 g_dbus_node_info_unref (introspection_data);