1 /* SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later */
2 
3 #include <config.h>
4 #undef G_DISABLE_ASSERT
5 
6 /* -*- mode: C; c-file-style: "gnu" -*- */
7 #include <dbus/dbus-glib.h>
8 /* NOTE - outside of D-BUS core this would be
9  * include <dbus/dbus-glib-bindings.h>
10  */
11 #include "tools/dbus-glib-bindings.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <glib/gi18n.h>
16 #include <glib-object.h>
17 
18 #include "my-object.h"
19 #include "my-object-subclass.h"
20 
21 static GObject *obj;
22 static GObject *obj2;
23 static GObject *subobj;
24 GMainLoop *loop;
25 
26 #define TEST_SERVICE_NAME "org.freedesktop.DBus.GLib.TestService"
27 
28 int
main(int argc,char ** argv)29 main (int argc, char **argv)
30 {
31   DBusGConnection *connection;
32   GError *error;
33   DBusGProxy *driver_proxy;
34   guint32 request_name_ret;
35 
36   g_type_init ();
37   dbus_g_thread_init ();
38 
39   dbus_g_error_domain_register (MY_OBJECT_ERROR,
40 				NULL,
41 				MY_TYPE_ERROR);
42 
43   g_printerr ("Launching test-service-glib\n");
44 
45   loop = g_main_loop_new (NULL, FALSE);
46 
47   {
48     GLogLevelFlags fatal_mask;
49 
50     fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
51     fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
52     g_log_set_always_fatal (fatal_mask);
53   }
54 
55   error = NULL;
56   connection = dbus_g_bus_get (DBUS_BUS_STARTER,
57                                &error);
58   if (connection == NULL)
59     {
60       g_printerr ("Failed to open connection to bus: %s\n",
61                   error->message);
62       g_error_free (error);
63       exit (1);
64     }
65 
66   obj = g_object_new (MY_TYPE_OBJECT, NULL);
67   obj2 = g_object_new (MY_TYPE_OBJECT, NULL);
68   subobj = g_object_new (MY_TYPE_OBJECT_SUBCLASS, NULL);
69 
70   dbus_g_connection_register_g_object (connection,
71                                        "/org/freedesktop/DBus/GLib/Tests/MyTestObject",
72                                        obj);
73   /* Register a second time; we want the object to also be reachable through this interface */
74   dbus_g_connection_register_g_object (connection,
75                                        "/org/freedesktop/DBus/GLib/Tests/Compat/MyTestObjectCompat",
76                                        obj);
77   dbus_g_connection_register_g_object (connection,
78                                        "/org/freedesktop/DBus/GLib/Tests/MyTestObject2",
79                                        obj2);
80 
81   dbus_g_connection_register_g_object (connection,
82                                        "/org/freedesktop/DBus/GLib/Tests/MyTestObjectSubclass",
83                                        subobj);
84 
85   driver_proxy = dbus_g_proxy_new_for_name (connection,
86                                             DBUS_SERVICE_DBUS,
87                                             DBUS_PATH_DBUS,
88                                             DBUS_INTERFACE_DBUS);
89 
90   if (!org_freedesktop_DBus_request_name (driver_proxy,
91 					  TEST_SERVICE_NAME,
92 					  0, &request_name_ret, &error))
93     {
94       g_assert_nonnull (error);
95       g_printerr ("Failed to get name: %s\n",
96 		  error->message);
97       g_clear_error (&error);
98       exit (1);
99     }
100 
101   if (!(request_name_ret == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER))
102     {
103       g_printerr ("Got result code %u from requesting name\n", request_name_ret);
104       exit (1);
105     }
106 
107   g_printerr ("GLib test service has name '%s'\n", TEST_SERVICE_NAME);
108   g_printerr ("GLib test service entering main loop\n");
109 
110   g_main_loop_run (loop);
111 
112   g_printerr ("Successfully completed %s\n", argv[0]);
113 
114   return 0;
115 }
116