31
|
1 |
#include <dbus/dbus-glib.h>
|
|
2 |
#include <stdio.h>
|
|
3 |
#include <stdlib.h>
|
|
4 |
#include <unistd.h>
|
|
5 |
|
|
6 |
static void lose (const char *fmt, ...) G_GNUC_NORETURN G_GNUC_PRINTF (1, 2);
|
|
7 |
static void lose_gerror (const char *prefix, GError *error) G_GNUC_NORETURN;
|
|
8 |
|
|
9 |
static void
|
|
10 |
lose (const char *str, ...)
|
|
11 |
{
|
|
12 |
va_list args;
|
|
13 |
|
|
14 |
va_start (args, str);
|
|
15 |
|
|
16 |
vfprintf (stderr, str, args);
|
|
17 |
fputc ('\n', stderr);
|
|
18 |
|
|
19 |
va_end (args);
|
|
20 |
|
|
21 |
exit (1);
|
|
22 |
}
|
|
23 |
|
|
24 |
static void
|
|
25 |
lose_gerror (const char *prefix, GError *error)
|
|
26 |
{
|
|
27 |
lose ("%s: %s", prefix, error->message);
|
|
28 |
}
|
|
29 |
|
|
30 |
static gboolean
|
|
31 |
emit_signal (gpointer arg)
|
|
32 |
{
|
|
33 |
DBusGProxy *proxy = arg;
|
|
34 |
|
|
35 |
dbus_g_proxy_call_no_reply (proxy, "emitHelloSignal", G_TYPE_INVALID);
|
|
36 |
return TRUE;
|
|
37 |
}
|
|
38 |
|
|
39 |
static void
|
|
40 |
hello_signal_handler (DBusGProxy *proxy, const char *hello_string, gpointer user_data)
|
|
41 |
{
|
|
42 |
printf ("Received signal and it says: %s\n", hello_string);
|
|
43 |
}
|
|
44 |
|
|
45 |
int
|
|
46 |
main ()
|
|
47 |
{
|
|
48 |
DBusGConnection *bus;
|
|
49 |
DBusGProxy *remote_object;
|
|
50 |
GError *error = NULL;
|
|
51 |
GMainLoop *mainloop;
|
|
52 |
FILE *fp;
|
|
53 |
|
|
54 |
g_type_init ();
|
|
55 |
|
|
56 |
|
|
57 |
fp = popen("z:\\sys\\bin\\example-signal-emitter.exe 1","r");
|
|
58 |
sleep(30);
|
|
59 |
fp = popen("z:\\sys\\bin\\example-signal-emitter.exe 2","r");
|
|
60 |
sleep(10);
|
|
61 |
|
|
62 |
mainloop = g_main_loop_new (NULL, FALSE);
|
|
63 |
|
|
64 |
bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
|
|
65 |
if (!bus)
|
|
66 |
lose_gerror ("Couldn't connect to session bus", error);
|
|
67 |
|
|
68 |
/* We use _for_name_owner in order to track this particular service
|
|
69 |
* instance, which lets us receive signals.
|
|
70 |
*/
|
|
71 |
remote_object = dbus_g_proxy_new_for_name (bus,
|
|
72 |
"org.designfu.TestService",
|
|
73 |
"/org/designfu/TestService/object",
|
|
74 |
"org.designfu.TestService");
|
|
75 |
if (!remote_object)
|
|
76 |
lose_gerror ("Failed to get name owner", error);
|
|
77 |
|
|
78 |
/* IMPORTANT:
|
|
79 |
*
|
|
80 |
* Note because this signal's signature is VOID__STRING, we do not
|
|
81 |
* need to register a marshaller, since there is a builtin one.
|
|
82 |
* However for other signatures, you must generate a marshaller,
|
|
83 |
* then call dbus_g_object_register_marshaller. It would look like
|
|
84 |
* this:
|
|
85 |
*
|
|
86 |
* dbus_g_object_register_marshaller (g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, G_TYPE_STRING, G_TYPE_INVALID);
|
|
87 |
*
|
|
88 |
*/
|
|
89 |
|
|
90 |
/* Tell DBus what the type signature of the signal callback is; this
|
|
91 |
* allows us to sanity-check incoming messages before invoking the
|
|
92 |
* callback. You need to do this once for each proxy you create,
|
|
93 |
* not every time you want to connect to the signal.
|
|
94 |
*/
|
|
95 |
dbus_g_proxy_add_signal (remote_object, "HelloSignal", G_TYPE_STRING, G_TYPE_INVALID);
|
|
96 |
|
|
97 |
/* Actually connect to the signal. Note you can call
|
|
98 |
* dbus_g_proxy_connect_signal multiple times for one invocation of
|
|
99 |
* dbus_g_proxy_add_signal.
|
|
100 |
*/
|
|
101 |
dbus_g_proxy_connect_signal (remote_object, "HelloSignal", G_CALLBACK (hello_signal_handler),
|
|
102 |
NULL, NULL);
|
|
103 |
|
|
104 |
|
|
105 |
g_timeout_add (2000, emit_signal, remote_object);
|
|
106 |
|
|
107 |
g_main_loop_run (mainloop);
|
|
108 |
|
|
109 |
exit (0);
|
|
110 |
}
|