5 /* For STDOUT_FILENO */
9 /* ---------------------------------------------------------------------------------------------------- */
11 static GDBusNodeInfo *introspection_data = NULL;
13 /* Introspection data for the service we are exporting */
14 static const gchar introspection_xml[] =
16 " <interface name='org.gtk.GDBus.TestInterface'>"
17 " <annotation name='org.gtk.GDBus.Annotation' value='OnInterface'/>"
18 " <annotation name='org.gtk.GDBus.Annotation' value='AlsoOnInterface'/>"
19 " <method name='HelloWorld'>"
20 " <annotation name='org.gtk.GDBus.Annotation' value='OnMethod'/>"
21 " <arg type='s' name='greeting' direction='in'/>"
22 " <arg type='s' name='response' direction='out'/>"
24 " <method name='EmitSignal'>"
25 " <arg type='d' name='speed_in_mph' direction='in'>"
26 " <annotation name='org.gtk.GDBus.Annotation' value='OnArg'/>"
29 " <method name='GimmeStdout'/>"
30 " <signal name='VelocityChanged'>"
31 " <annotation name='org.gtk.GDBus.Annotation' value='Onsignal'/>"
32 " <arg type='d' name='speed_in_mph'/>"
33 " <arg type='s' name='speed_as_string'>"
34 " <annotation name='org.gtk.GDBus.Annotation' value='OnArg_NonFirst'/>"
37 " <property type='s' name='FluxCapicitorName' access='read'>"
38 " <annotation name='org.gtk.GDBus.Annotation' value='OnProperty'>"
39 " <annotation name='org.gtk.GDBus.Annotation' value='OnAnnotation_YesThisIsCrazy'/>"
42 " <property type='s' name='Title' access='readwrite'/>"
43 " <property type='s' name='ReadingAlwaysThrowsError' access='read'/>"
44 " <property type='s' name='WritingAlwaysThrowsError' access='readwrite'/>"
45 " <property type='s' name='OnlyWritable' access='write'/>"
46 " <property type='s' name='Foo' access='read'/>"
47 " <property type='s' name='Bar' access='read'/>"
51 /* ---------------------------------------------------------------------------------------------------- */
54 handle_method_call (GDBusConnection *connection,
56 const gchar *object_path,
57 const gchar *interface_name,
58 const gchar *method_name,
60 GDBusMethodInvocation *invocation,
63 if (g_strcmp0 (method_name, "HelloWorld") == 0)
65 const gchar *greeting;
67 g_variant_get (parameters, "(&s)", &greeting);
69 if (g_strcmp0 (greeting, "Return Unregistered") == 0)
71 g_dbus_method_invocation_return_error (invocation,
73 G_IO_ERROR_FAILED_HANDLED,
74 "As requested, here's a GError not registered (G_IO_ERROR_FAILED_HANDLED)");
76 else if (g_strcmp0 (greeting, "Return Registered") == 0)
78 g_dbus_method_invocation_return_error (invocation,
80 G_DBUS_ERROR_MATCH_RULE_NOT_FOUND,
81 "As requested, here's a GError that is registered (G_DBUS_ERROR_MATCH_RULE_NOT_FOUND)");
83 else if (g_strcmp0 (greeting, "Return Raw") == 0)
85 g_dbus_method_invocation_return_dbus_error (invocation,
86 "org.gtk.GDBus.SomeErrorName",
87 "As requested, here's a raw D-Bus error");
92 response = g_strdup_printf ("You greeted me with '%s'. Thanks!", greeting);
93 g_dbus_method_invocation_return_value (invocation,
94 g_variant_new ("(s)", response));
98 else if (g_strcmp0 (method_name, "EmitSignal") == 0)
101 gdouble speed_in_mph;
102 gchar *speed_as_string;
104 g_variant_get (parameters, "(d)", &speed_in_mph);
105 speed_as_string = g_strdup_printf ("%g mph!", speed_in_mph);
108 g_dbus_connection_emit_signal (connection,
113 g_variant_new ("(ds)",
117 g_assert_no_error (local_error);
118 g_free (speed_as_string);
120 g_dbus_method_invocation_return_value (invocation, NULL);
122 else if (g_strcmp0 (method_name, "GimmeStdout") == 0)
125 if (g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING)
128 GUnixFDList *fd_list;
131 fd_list = g_unix_fd_list_new ();
133 g_unix_fd_list_append (fd_list, STDOUT_FILENO, &error);
134 g_assert_no_error (error);
136 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
137 g_dbus_message_set_unix_fd_list (reply, fd_list);
140 g_dbus_connection_send_message (connection,
142 NULL, /* out_serial */
144 g_assert_no_error (error);
146 g_object_unref (invocation);
147 g_object_unref (fd_list);
148 g_object_unref (reply);
152 g_dbus_method_invocation_return_dbus_error (invocation,
153 "org.gtk.GDBus.Failed",
154 "Your message bus daemon does not support file descriptor passing (need D-Bus >= 1.3.0)");
157 g_dbus_method_invocation_return_dbus_error (invocation,
158 "org.gtk.GDBus.NotOnUnix",
159 "Your OS does not support file descriptor passing");
164 static gchar *_global_title = NULL;
166 static gboolean swap_a_and_b = FALSE;
169 handle_get_property (GDBusConnection *connection,
171 const gchar *object_path,
172 const gchar *interface_name,
173 const gchar *property_name,
180 if (g_strcmp0 (property_name, "FluxCapicitorName") == 0)
182 ret = g_variant_new_string ("DeLorean");
184 else if (g_strcmp0 (property_name, "Title") == 0)
186 if (_global_title == NULL)
187 _global_title = g_strdup ("Back To C!");
188 ret = g_variant_new_string (_global_title);
190 else if (g_strcmp0 (property_name, "ReadingAlwaysThrowsError") == 0)
195 "Hello %s. I thought I said reading this property "
196 "always results in an error. kthxbye",
199 else if (g_strcmp0 (property_name, "WritingAlwaysThrowsError") == 0)
201 ret = g_variant_new_string ("There's no home like home");
203 else if (g_strcmp0 (property_name, "Foo") == 0)
205 ret = g_variant_new_string (swap_a_and_b ? "Tock" : "Tick");
207 else if (g_strcmp0 (property_name, "Bar") == 0)
209 ret = g_variant_new_string (swap_a_and_b ? "Tick" : "Tock");
216 handle_set_property (GDBusConnection *connection,
218 const gchar *object_path,
219 const gchar *interface_name,
220 const gchar *property_name,
225 if (g_strcmp0 (property_name, "Title") == 0)
227 if (g_strcmp0 (_global_title, g_variant_get_string (value, NULL)) != 0)
229 GVariantBuilder *builder;
232 g_free (_global_title);
233 _global_title = g_variant_dup_string (value, NULL);
236 builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
237 g_variant_builder_add (builder,
240 g_variant_new_string (_global_title));
241 g_dbus_connection_emit_signal (connection,
244 "org.freedesktop.DBus.Properties",
246 g_variant_new ("(sa{sv}as)",
251 g_assert_no_error (local_error);
254 else if (g_strcmp0 (property_name, "ReadingAlwaysThrowsError") == 0)
256 /* do nothing - they can't read it after all! */
258 else if (g_strcmp0 (property_name, "WritingAlwaysThrowsError") == 0)
263 "Hello AGAIN %s. I thought I said writing this property "
264 "always results in an error. kthxbye",
268 return *error == NULL;
273 static const GDBusInterfaceVTable interface_vtable =
280 /* ---------------------------------------------------------------------------------------------------- */
283 on_timeout_cb (gpointer user_data)
285 GDBusConnection *connection = G_DBUS_CONNECTION (user_data);
286 GVariantBuilder *builder;
287 GVariantBuilder *invalidated_builder;
290 swap_a_and_b = !swap_a_and_b;
293 builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
294 invalidated_builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
295 g_variant_builder_add (builder,
298 g_variant_new_string (swap_a_and_b ? "Tock" : "Tick"));
299 g_variant_builder_add (builder,
302 g_variant_new_string (swap_a_and_b ? "Tick" : "Tock"));
303 g_dbus_connection_emit_signal (connection,
305 "/org/gtk/GDBus/TestObject",
306 "org.freedesktop.DBus.Properties",
308 g_variant_new ("(sa{sv}as)",
309 "org.gtk.GDBus.TestInterface",
311 invalidated_builder),
313 g_assert_no_error (error);
319 /* ---------------------------------------------------------------------------------------------------- */
322 on_bus_acquired (GDBusConnection *connection,
326 guint registration_id;
328 registration_id = g_dbus_connection_register_object (connection,
329 "/org/gtk/GDBus/TestObject",
330 introspection_data->interfaces[0],
332 NULL, /* user_data */
333 NULL, /* user_data_free_func */
334 NULL); /* GError** */
335 g_assert (registration_id > 0);
337 /* swap value of properties Foo and Bar every two seconds */
338 g_timeout_add_seconds (2,
344 on_name_acquired (GDBusConnection *connection,
351 on_name_lost (GDBusConnection *connection,
359 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);