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 " <method name='HelloWorld'>"
18 " <arg type='s' name='greeting' direction='in'/>"
19 " <arg type='s' name='response' direction='out'/>"
21 " <method name='EmitSignal'>"
22 " <arg type='d' name='speed_in_mph' direction='in'/>"
24 " <method name='GimmeStdout'/>"
25 " <signal name='VelocityChanged'>"
26 " <arg type='d' name='speed_in_mph'/>"
27 " <arg type='s' name='speed_as_string'/>"
29 " <property type='s' name='FluxCapicitorName' access='read'/>"
30 " <property type='s' name='Title' access='readwrite'/>"
31 " <property type='s' name='ReadingAlwaysThrowsError' access='read'/>"
32 " <property type='s' name='WritingAlwaysThrowsError' access='readwrite'/>"
33 " <property type='s' name='OnlyWritable' access='write'/>"
34 " <property type='s' name='Foo' access='read'/>"
35 " <property type='s' name='Bar' access='read'/>"
39 /* ---------------------------------------------------------------------------------------------------- */
42 handle_method_call (GDBusConnection *connection,
44 const gchar *object_path,
45 const gchar *interface_name,
46 const gchar *method_name,
48 GDBusMethodInvocation *invocation,
51 if (g_strcmp0 (method_name, "HelloWorld") == 0)
53 const gchar *greeting;
55 g_variant_get (parameters, "(s)", &greeting);
57 if (g_strcmp0 (greeting, "Return Unregistered") == 0)
59 g_dbus_method_invocation_return_error (invocation,
61 G_IO_ERROR_FAILED_HANDLED,
62 "As requested, here's a GError not registered (G_IO_ERROR_FAILED_HANDLED)");
64 else if (g_strcmp0 (greeting, "Return Registered") == 0)
66 g_dbus_method_invocation_return_error (invocation,
68 G_DBUS_ERROR_MATCH_RULE_NOT_FOUND,
69 "As requested, here's a GError that is registered (G_DBUS_ERROR_MATCH_RULE_NOT_FOUND)");
71 else if (g_strcmp0 (greeting, "Return Raw") == 0)
73 g_dbus_method_invocation_return_dbus_error (invocation,
74 "org.gtk.GDBus.SomeErrorName",
75 "As requested, here's a raw D-Bus error");
80 response = g_strdup_printf ("You greeted me with '%s'. Thanks!", greeting);
81 g_dbus_method_invocation_return_value (invocation,
82 g_variant_new ("(s)", response));
86 else if (g_strcmp0 (method_name, "EmitSignal") == 0)
90 gchar *speed_as_string;
92 g_variant_get (parameters, "(d)", &speed_in_mph);
93 speed_as_string = g_strdup_printf ("%g mph!", speed_in_mph);
96 g_dbus_connection_emit_signal (connection,
101 g_variant_new ("(ds)",
105 g_assert_no_error (local_error);
106 g_free (speed_as_string);
108 g_dbus_method_invocation_return_value (invocation, NULL);
110 else if (g_strcmp0 (method_name, "GimmeStdout") == 0)
113 if (g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING)
116 GUnixFDList *fd_list;
119 fd_list = g_unix_fd_list_new ();
121 g_unix_fd_list_append (fd_list, STDOUT_FILENO, &error);
122 g_assert_no_error (error);
124 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
125 g_dbus_message_set_unix_fd_list (reply, fd_list);
128 g_dbus_connection_send_message (connection,
130 NULL, /* out_serial */
132 g_assert_no_error (error);
134 g_object_unref (invocation);
135 g_object_unref (fd_list);
136 g_object_unref (reply);
140 g_dbus_method_invocation_return_dbus_error (invocation,
141 "org.gtk.GDBus.Failed",
142 "Your message bus daemon does not support file descriptor passing (need D-Bus >= 1.3.0)");
145 g_dbus_method_invocation_return_dbus_error (invocation,
146 "org.gtk.GDBus.NotOnUnix",
147 "Your OS does not support file descriptor passing");
152 static gchar *_global_title = NULL;
154 static gboolean swap_a_and_b = FALSE;
157 handle_get_property (GDBusConnection *connection,
159 const gchar *object_path,
160 const gchar *interface_name,
161 const gchar *property_name,
168 if (g_strcmp0 (property_name, "FluxCapicitorName") == 0)
170 ret = g_variant_new_string ("DeLorean");
172 else if (g_strcmp0 (property_name, "Title") == 0)
174 if (_global_title == NULL)
175 _global_title = g_strdup ("Back To C!");
176 ret = g_variant_new_string (_global_title);
178 else if (g_strcmp0 (property_name, "ReadingAlwaysThrowsError") == 0)
183 "Hello %s. I thought I said reading this property "
184 "always results in an error. kthxbye",
187 else if (g_strcmp0 (property_name, "WritingAlwaysThrowsError") == 0)
189 ret = g_variant_new_string ("There's no home like home");
191 else if (g_strcmp0 (property_name, "Foo") == 0)
193 ret = g_variant_new_string (swap_a_and_b ? "Tock" : "Tick");
195 else if (g_strcmp0 (property_name, "Bar") == 0)
197 ret = g_variant_new_string (swap_a_and_b ? "Tick" : "Tock");
204 handle_set_property (GDBusConnection *connection,
206 const gchar *object_path,
207 const gchar *interface_name,
208 const gchar *property_name,
213 if (g_strcmp0 (property_name, "Title") == 0)
215 if (g_strcmp0 (_global_title, g_variant_get_string (value, NULL)) != 0)
217 GVariantBuilder *builder;
220 g_free (_global_title);
221 _global_title = g_variant_dup_string (value, NULL);
224 builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
225 g_variant_builder_add (builder,
228 g_variant_new_string (_global_title));
229 g_dbus_connection_emit_signal (connection,
232 "org.freedesktop.DBus.Properties",
234 g_variant_new ("(sa{sv})",
238 g_assert_no_error (local_error);
241 else if (g_strcmp0 (property_name, "ReadingAlwaysThrowsError") == 0)
243 /* do nothing - they can't read it after all! */
245 else if (g_strcmp0 (property_name, "WritingAlwaysThrowsError") == 0)
250 "Hello AGAIN %s. I thought I said writing this property "
251 "always results in an error. kthxbye",
255 return *error == NULL;
260 static const GDBusInterfaceVTable interface_vtable =
267 /* ---------------------------------------------------------------------------------------------------- */
270 on_timeout_cb (gpointer user_data)
272 GDBusConnection *connection = G_DBUS_CONNECTION (user_data);
273 GVariantBuilder *builder;
276 swap_a_and_b = !swap_a_and_b;
279 builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
280 g_variant_builder_add (builder,
283 g_variant_new_string (swap_a_and_b ? "Tock" : "Tick"));
284 g_variant_builder_add (builder,
287 g_variant_new_string (swap_a_and_b ? "Tick" : "Tock"));
288 g_dbus_connection_emit_signal (connection,
290 "/org/gtk/GDBus/TestObject",
291 "org.freedesktop.DBus.Properties",
293 g_variant_new ("(sa{sv})",
294 "org.gtk.GDBus.TestInterface",
297 g_assert_no_error (error);
303 /* ---------------------------------------------------------------------------------------------------- */
306 on_bus_acquired (GDBusConnection *connection,
310 guint registration_id;
312 registration_id = g_dbus_connection_register_object (connection,
313 "/org/gtk/GDBus/TestObject",
314 "org.gtk.GDBus.TestInterface",
315 introspection_data->interfaces[0],
317 NULL, /* user_data */
318 NULL, /* user_data_free_func */
319 NULL); /* GError** */
320 g_assert (registration_id > 0);
322 /* swap value of properties Foo and Bar every two seconds */
323 g_timeout_add_seconds (2,
329 on_name_acquired (GDBusConnection *connection,
336 on_name_lost (GDBusConnection *connection,
344 main (int argc, char *argv[])
351 /* We are lazy here - we don't want to manually provide
352 * the introspection data structures - so we just build
355 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
356 g_assert (introspection_data != NULL);
358 owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
359 "org.gtk.GDBus.TestServer",
360 G_BUS_NAME_OWNER_FLAGS_NONE,
367 loop = g_main_loop_new (NULL, FALSE);
368 g_main_loop_run (loop);
370 g_bus_unown_name (owner_id);
372 g_dbus_node_info_unref (introspection_data);