1 /* example.c - program to demonstrate some D-BUS stuffs.
2 * Copyright (C) 2006 Claessens Xavier
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <dbus/dbus-glib.h>
25 #include "marshallers.h"
27 #define DBUS_SERVICE "org.xchat.service"
28 #define DBUS_REMOTE "/org/xchat/Remote"
29 #define DBUS_REMOTE_CONNECTION_INTERFACE "org.xchat.connection"
30 #define DBUS_REMOTE_PLUGIN_INTERFACE "org.xchat.plugin"
36 write_error (char *message
,
39 if (error
== NULL
|| *error
== NULL
) {
42 g_printerr ("%s: %s\n", message
, (*error
)->message
);
43 g_clear_error (error
);
47 test_server_cb (DBusGProxy
*proxy
,
54 if (hook_id
== server_id
) {
55 g_print ("message: %s\n", word_eol
[0]);
60 test_command_cb (DBusGProxy
*proxy
,
69 if (hook_id
== command_id
) {
70 if (!dbus_g_proxy_call (proxy
, "Unhook",
73 G_TYPE_INVALID
, G_TYPE_INVALID
)) {
74 write_error ("Failed to complete unhook", &error
);
76 /* Now if you write "/test blah" again in the xchat window
77 * you'll get a "Unknown command" error message */
78 g_print ("test command received: %s\n", word_eol
[1]);
79 if (!dbus_g_proxy_call (proxy
, "Print",
81 G_TYPE_STRING
, "test command succeed",
84 write_error ("Failed to complete Print", &error
);
92 g_print ("Good bye !\n");
97 main (int argc
, char **argv
)
99 DBusGConnection
*connection
;
100 DBusGProxy
*remote_object
;
103 GError
*error
= NULL
;
107 connection
= dbus_g_bus_get (DBUS_BUS_SESSION
, &error
);
108 if (connection
== NULL
) {
109 write_error ("Couldn't connect to session bus", &error
);
113 remote_object
= dbus_g_proxy_new_for_name (connection
,
116 DBUS_REMOTE_CONNECTION_INTERFACE
);
117 if (!dbus_g_proxy_call (remote_object
, "Connect",
119 G_TYPE_STRING
, argv
[0],
120 G_TYPE_STRING
, "example",
121 G_TYPE_STRING
, "Example of a D-Bus client",
122 G_TYPE_STRING
, "1.0",
124 G_TYPE_STRING
, &path
, G_TYPE_INVALID
)) {
125 write_error ("Failed to complete Connect", &error
);
128 g_object_unref (remote_object
);
130 remote_object
= dbus_g_proxy_new_for_name (connection
,
133 DBUS_REMOTE_PLUGIN_INTERFACE
);
136 if (!dbus_g_proxy_call (remote_object
, "HookCommand",
138 G_TYPE_STRING
, "test",
140 G_TYPE_STRING
, "Simple D-BUS example",
141 G_TYPE_INT
, 1, G_TYPE_INVALID
,
142 G_TYPE_UINT
, &command_id
, G_TYPE_INVALID
)) {
143 write_error ("Failed to complete HookCommand", &error
);
146 g_print ("Command hook id=%d\n", command_id
);
148 if (!dbus_g_proxy_call (remote_object
, "HookServer",
150 G_TYPE_STRING
, "RAW LINE",
152 G_TYPE_INT
, 0, G_TYPE_INVALID
,
153 G_TYPE_UINT
, &server_id
, G_TYPE_INVALID
)) {
154 write_error ("Failed to complete HookServer", &error
);
157 g_print ("Server hook id=%d\n", server_id
);
159 dbus_g_object_register_marshaller (
160 g_cclosure_user_marshal_VOID__POINTER_POINTER_UINT_UINT
,
162 G_TYPE_STRV
, G_TYPE_STRV
, G_TYPE_UINT
, G_TYPE_UINT
,
165 dbus_g_object_register_marshaller (
166 g_cclosure_marshal_VOID__VOID
,
170 dbus_g_proxy_add_signal (remote_object
, "CommandSignal",
176 dbus_g_proxy_connect_signal (remote_object
, "CommandSignal",
177 G_CALLBACK (test_command_cb
),
180 dbus_g_proxy_add_signal (remote_object
, "ServerSignal",
186 dbus_g_proxy_connect_signal (remote_object
, "ServerSignal",
187 G_CALLBACK (test_server_cb
),
190 dbus_g_proxy_add_signal (remote_object
, "UnloadSignal",
192 dbus_g_proxy_connect_signal (remote_object
, "UnloadSignal",
193 G_CALLBACK (unload_cb
),
196 /* Now you can write on the xchat windows: "/test arg1 arg2 ..." */
197 mainloop
= g_main_loop_new (NULL
, FALSE
);
198 g_main_loop_run (mainloop
);