remove mmx asm
[rofl0r-ixchat.git] / src / common / dbus / example.c
blob1d072785e25fd221f0b71ea17f1a7210a96fa773
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.
18 * Claessens Xavier
19 * xclaesse@gmail.com
22 #include <config.h>
23 #include <dbus/dbus-glib.h>
24 #include <stdlib.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"
32 guint command_id;
33 guint server_id;
35 static void
36 write_error (char *message,
37 GError **error)
39 if (error == NULL || *error == NULL) {
40 return;
42 g_printerr ("%s: %s\n", message, (*error)->message);
43 g_clear_error (error);
46 static void
47 test_server_cb (DBusGProxy *proxy,
48 char *word[],
49 char *word_eol[],
50 guint hook_id,
51 guint context_id,
52 gpointer user_data)
54 if (hook_id == server_id) {
55 g_print ("message: %s\n", word_eol[0]);
59 static void
60 test_command_cb (DBusGProxy *proxy,
61 char *word[],
62 char *word_eol[],
63 guint hook_id,
64 guint context_id,
65 gpointer user_data)
67 GError *error = NULL;
69 if (hook_id == command_id) {
70 if (!dbus_g_proxy_call (proxy, "Unhook",
71 &error,
72 G_TYPE_UINT, hook_id,
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",
80 &error,
81 G_TYPE_STRING, "test command succeed",
82 G_TYPE_INVALID,
83 G_TYPE_INVALID)) {
84 write_error ("Failed to complete Print", &error);
89 static void
90 unload_cb (void)
92 g_print ("Good bye !\n");
93 exit (EXIT_SUCCESS);
96 int
97 main (int argc, char **argv)
99 DBusGConnection *connection;
100 DBusGProxy *remote_object;
101 GMainLoop *mainloop;
102 gchar *path;
103 GError *error = NULL;
105 g_type_init ();
107 connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
108 if (connection == NULL) {
109 write_error ("Couldn't connect to session bus", &error);
110 return EXIT_FAILURE;
113 remote_object = dbus_g_proxy_new_for_name (connection,
114 DBUS_SERVICE,
115 DBUS_REMOTE,
116 DBUS_REMOTE_CONNECTION_INTERFACE);
117 if (!dbus_g_proxy_call (remote_object, "Connect",
118 &error,
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",
123 G_TYPE_INVALID,
124 G_TYPE_STRING, &path, G_TYPE_INVALID)) {
125 write_error ("Failed to complete Connect", &error);
126 return EXIT_FAILURE;
128 g_object_unref (remote_object);
130 remote_object = dbus_g_proxy_new_for_name (connection,
131 DBUS_SERVICE,
132 path,
133 DBUS_REMOTE_PLUGIN_INTERFACE);
134 g_free (path);
136 if (!dbus_g_proxy_call (remote_object, "HookCommand",
137 &error,
138 G_TYPE_STRING, "test",
139 G_TYPE_INT, 0,
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);
144 return EXIT_FAILURE;
146 g_print ("Command hook id=%d\n", command_id);
148 if (!dbus_g_proxy_call (remote_object, "HookServer",
149 &error,
150 G_TYPE_STRING, "RAW LINE",
151 G_TYPE_INT, 0,
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);
155 return EXIT_FAILURE;
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,
161 G_TYPE_NONE,
162 G_TYPE_STRV, G_TYPE_STRV, G_TYPE_UINT, G_TYPE_UINT,
163 G_TYPE_INVALID);
165 dbus_g_object_register_marshaller (
166 g_cclosure_marshal_VOID__VOID,
167 G_TYPE_NONE,
168 G_TYPE_INVALID);
170 dbus_g_proxy_add_signal (remote_object, "CommandSignal",
171 G_TYPE_STRV,
172 G_TYPE_STRV,
173 G_TYPE_UINT,
174 G_TYPE_UINT,
175 G_TYPE_INVALID);
176 dbus_g_proxy_connect_signal (remote_object, "CommandSignal",
177 G_CALLBACK (test_command_cb),
178 NULL, NULL);
180 dbus_g_proxy_add_signal (remote_object, "ServerSignal",
181 G_TYPE_STRV,
182 G_TYPE_STRV,
183 G_TYPE_UINT,
184 G_TYPE_UINT,
185 G_TYPE_INVALID);
186 dbus_g_proxy_connect_signal (remote_object, "ServerSignal",
187 G_CALLBACK (test_server_cb),
188 NULL, NULL);
190 dbus_g_proxy_add_signal (remote_object, "UnloadSignal",
191 G_TYPE_INVALID);
192 dbus_g_proxy_connect_signal (remote_object, "UnloadSignal",
193 G_CALLBACK (unload_cb),
194 NULL, NULL);
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);
200 return EXIT_SUCCESS;