00001 #include <dbus/dbus-glib.h> 00002 #include <stdio.h> 00003 #include <stdlib.h> 00004 00005 static void lose (const char *fmt, ...) G_GNUC_NORETURN G_GNUC_PRINTF (1, 2); 00006 static void lose_gerror (const char *prefix, GError *error) G_GNUC_NORETURN; 00007 00008 static void 00009 lose (const char *str, ...) 00010 { 00011 va_list args; 00012 00013 va_start (args, str); 00014 00015 vfprintf (stderr, str, args); 00016 fputc ('\n', stderr); 00017 00018 va_end (args); 00019 00020 exit (1); 00021 } 00022 00023 static void 00024 lose_gerror (const char *prefix, GError *error) 00025 { 00026 lose ("%s: %s", prefix, error->message); 00027 } 00028 00029 typedef struct TestObject TestObject; 00030 typedef struct TestObjectClass TestObjectClass; 00031 00032 GType test_object_get_type (void); 00033 00034 struct TestObject 00035 { 00036 GObject parent; 00037 }; 00038 00039 struct TestObjectClass 00040 { 00041 GObjectClass parent; 00042 }; 00043 00044 enum 00045 { 00046 HELLO_SIGNAL, 00047 LAST_SIGNAL 00048 }; 00049 00050 static guint signals[LAST_SIGNAL] = { 0 }; 00051 00052 #define TEST_TYPE_OBJECT (test_object_get_type ()) 00053 #define TEST_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), TEST_TYPE_OBJECT, TestObject)) 00054 #define TEST_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TEST_TYPE_OBJECT, TestObjectClass)) 00055 #define TEST_IS_OBJECT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), TEST_TYPE_OBJECT)) 00056 #define TEST_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TEST_TYPE_OBJECT)) 00057 #define TEST_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TEST_TYPE_OBJECT, TestObjectClass)) 00058 00059 G_DEFINE_TYPE(TestObject, test_object, G_TYPE_OBJECT) 00060 00061 gboolean test_object_emit_hello_signal (TestObject *obj, GError **error); 00062 00063 #include "example-signal-emitter-glue.h" 00064 00065 static void 00066 test_object_init (TestObject *obj) 00067 { 00068 } 00069 00070 static void 00071 test_object_class_init (TestObjectClass *klass) 00072 { 00073 signals[HELLO_SIGNAL] = 00074 g_signal_new ("hello_signal", 00075 G_OBJECT_CLASS_TYPE (klass), 00076 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED, 00077 0, 00078 NULL, NULL, 00079 g_cclosure_marshal_VOID__STRING, 00080 G_TYPE_NONE, 1, G_TYPE_STRING); 00081 } 00082 00083 gboolean 00084 test_object_emit_hello_signal (TestObject *obj, GError **error) 00085 { 00086 g_signal_emit (obj, signals[HELLO_SIGNAL], 0, "Hello"); 00087 return TRUE; 00088 } 00089 00090 00091 int 00092 main (int argc, char **argv) 00093 { 00094 DBusGConnection *bus; 00095 DBusGProxy *bus_proxy; 00096 GError *error = NULL; 00097 TestObject *obj; 00098 GMainLoop *mainloop; 00099 guint request_name_result; 00100 00101 g_type_init (); 00102 00103 dbus_g_object_type_install_info (TEST_TYPE_OBJECT, &dbus_glib_test_object_object_info); 00104 00105 mainloop = g_main_loop_new (NULL, FALSE); 00106 00107 bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error); 00108 if (!bus) 00109 lose_gerror ("Couldn't connect to session bus", error); 00110 00111 bus_proxy = dbus_g_proxy_new_for_name (bus, "org.freedesktop.DBus", 00112 "/org/freedesktop/DBus", 00113 "org.freedesktop.DBus"); 00114 00115 if (!dbus_g_proxy_call (bus_proxy, "RequestName", &error, 00116 G_TYPE_STRING, "org.designfu.TestService", 00117 G_TYPE_UINT, 0, 00118 G_TYPE_INVALID, 00119 G_TYPE_UINT, &request_name_result, 00120 G_TYPE_INVALID)) 00121 lose_gerror ("Failed to acquire org.designfu.TestService", error); 00122 00123 obj = g_object_new (TEST_TYPE_OBJECT, NULL); 00124 00125 dbus_g_connection_register_g_object (bus, "/org/designfu/TestService/object", G_OBJECT (obj)); 00126 00127 printf ("test service running\n"); 00128 00129 g_main_loop_run (mainloop); 00130 00131 exit (0); 00132 }