summaryrefslogtreecommitdiff
path: root/src/examples/eldbus
diff options
context:
space:
mode:
authorEduardo Lima (Etrunko) <eduardo.lima@intel.com>2013-07-04 17:23:52 -0300
committerEduardo Lima (Etrunko) <eduardo.lima@intel.com>2013-07-04 17:23:52 -0300
commit27b7ab457e0369bbebec2e5de7526cb44a55e2ac (patch)
tree84a2d8be29f3b8627265b55de1629209b210a281 /src/examples/eldbus
parent5d4ff97e20945e5ac1448fd04cf180616d9f63e3 (diff)
Updated Eldbus connect_address example
See description on the header of the source file Signed-off-by: Eduardo Lima (Etrunko) <eduardo.lima@intel.com>
Diffstat (limited to 'src/examples/eldbus')
-rw-r--r--src/examples/eldbus/connect-address.c248
1 files changed, 211 insertions, 37 deletions
diff --git a/src/examples/eldbus/connect-address.c b/src/examples/eldbus/connect-address.c
index 11858f4..242960e 100644
--- a/src/examples/eldbus/connect-address.c
+++ b/src/examples/eldbus/connect-address.c
@@ -1,56 +1,200 @@
1//Compile with: 1/*
2// gcc -o client client.c `pkg-config --cflags --libs eldbus ecore eina` 2 * compile with:
3 * gcc -o client client.c `pkg-config --cflags --libs eldbus ecore eina`
4 *
5 * This simple example connects to an IBus daemon running on the machine.
6 * IBus daemon is a dbus daemon running apart from system or session buses.
7 *
8 * Requires ibus-daemon to be running, for instance:
9 * $ ibus-daemon -v -r -s
10 *
11 * After starting ibus-daemon, check for its address on the file located on
12 * ~/.config/ibus/bus/ directory. For instance:
13 *
14 * $ cat ~/.config/ibus/bus/809ee018614317e782464b8900000029-unix-0
15 * # This file is created by ibus-daemon, please do not modify it
16 * IBUS_ADDRESS=unix:abstract=/tmp/dbus-VOJaWARY,guid=e0082807a08a81ad22e0d3f551d48d5e
17 * IBUS_DAEMON_PID=3697
18 *
19 * Export or set the IBUS_ADDRESS environment variable and run this program. To exit,
20 * just hit ctrl+c
21 */
3 22
4#include <stdlib.h> 23#include <stdlib.h>
5 24
25#define _GNU_SOURCE
26#include <signal.h>
27
6#include "Eldbus.h" 28#include "Eldbus.h"
7#include <Ecore.h> 29#include <Ecore.h>
8 30
9#define BUS "org.freedesktop.IBus" 31#define IBUS_BUS "org.freedesktop.IBus"
10#define PATH "/org/freedesktop/IBus" 32#define PANEL_BUS "org.freedesktop.IBUS.Panel"
11#define INTERFACE "org.freedesktop.IBus" 33#define PANEL_INTERFACE "org.freedesktop.IBUS.Panel"
34
35static int _conn_addr_log_dom = -1;
36#define DBG(...) EINA_LOG_DOM_DBG(_conn_addr_log_dom, __VA_ARGS__)
37#define INF(...) EINA_LOG_DOM_INFO(_conn_addr_log_dom, __VA_ARGS__)
38#define ERR(...) EINA_LOG_DOM_ERR(_conn_addr_log_dom, __VA_ARGS__)
12 39
13static int _client_log_dom = -1; 40static Eldbus_Connection *conn;
14#define ERR(...) EINA_LOG_DOM_ERR(_client_log_dom, __VA_ARGS__) 41static Eldbus_Signal_Handler *acquired;
42static Eldbus_Signal_Handler *lost;
15 43
16static void 44static void
17_on_registry_changed(void *context EINA_UNUSED, const Eldbus_Message *msg EINA_UNUSED) 45on_name_owner_changed(void *data EINA_UNUSED, const char *bus, const char *old_id, const char *new_id EINA_UNUSED)
18{ 46{
19 printf("RegistryChanged\n\n"); 47 INF("Name owner changed: bus=%s | old=%s | new=%s\n", bus, old_id, new_id);
20} 48}
21 49
22static void 50static void
23_on_global_engine_changed(void *context EINA_UNUSED, const Eldbus_Message *msg) 51on_name_acquired(void *data EINA_UNUSED, const Eldbus_Message *msg)
24{ 52{
25 const char *txt; 53 const char *error, *error_msg, *name;
26 if (eldbus_message_arguments_get(msg, "s", &txt)) 54
27 printf("GlobalEngineChanged %s\n", txt); 55 if (eldbus_message_error_get(msg, &error, &error_msg))
56 {
57 ERR("Name acquired message error: %s: %s\n", error, error_msg);
58 return;
59 }
60
61 if (!eldbus_message_arguments_get(msg, "s", &name))
62 {
63 ERR("Error reading message arguments");
64 return;
65 }
66
67 INF("Name acquired: %s", name);
28} 68}
29 69
30static void 70static void
31on_name_owner_changed(void *data EINA_UNUSED, const char *bus, const char *old_id, const char *new_id EINA_UNUSED) 71on_name_lost(void *data EINA_UNUSED, const Eldbus_Message *msg)
32{ 72{
33 printf("Bus=%s | old=%s | new=%s\n", bus, old_id, new_id); 73 const char *error, *error_msg, *name;
74
75 if (eldbus_message_error_get(msg, &error, &error_msg))
76 {
77 ERR("Name lost message error: %s: %s\n", error, error_msg);
78 return;
79 }
80
81 if (!eldbus_message_arguments_get(msg, "s", &name))
82 {
83 ERR("Error reading message arguments");
84 return;
85 }
86
87 INF("Name lost: %s", name);
88}
89
90static void
91name_request_cb(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
92{
93 const char *error, *error_msg;
94 unsigned int reply;
95
96 if (eldbus_message_error_get(msg, &error, &error_msg))
97 {
98 ERR("Name request message error: %s: %s\n", error, error_msg);
99 return;
100 }
101
102 if (!eldbus_message_arguments_get(msg, "u", &reply))
103 {
104 ERR("Error reading message arguments");
105 return;
106 }
107
108 switch (reply)
109 {
110 case ELDBUS_NAME_REQUEST_REPLY_PRIMARY_OWNER:
111 INF("Got primary ownership");
112 break;
113 case ELDBUS_NAME_REQUEST_REPLY_IN_QUEUE:
114 INF("Got queued for ownership");
115 break;
116 case ELDBUS_NAME_REQUEST_REPLY_EXISTS:
117 INF("Already in queue for ownership");
118 break;
119 case ELDBUS_NAME_REQUEST_REPLY_ALREADY_OWNER:
120 INF("Already primary owner");
121 break;
122 default:
123 ERR("Unexpected reply: %d", reply);
124 break;
125 }
34} 126}
35 127
36static Eina_Bool 128static Eina_Bool
37finish(void *data EINA_UNUSED) 129main_loop_quit_idler(void *data EINA_UNUSED)
38{ 130{
131 eldbus_signal_handler_del(acquired);
132 eldbus_signal_handler_del(lost);
133 eldbus_connection_unref(conn);
134
135 INF("Finishing");
39 ecore_main_loop_quit(); 136 ecore_main_loop_quit();
40 return ECORE_CALLBACK_CANCEL; 137 return ECORE_CALLBACK_CANCEL;
41} 138}
42 139
140static void
141name_release_cb(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
142{
143 const char *error, *error_msg;
144 unsigned int reply;
145
146 if (eldbus_message_error_get(msg, &error, &error_msg))
147 {
148 ERR("Name release message error: %s: %s\n", error, error_msg);
149 return;
150 }
151
152 if (!eldbus_message_arguments_get(msg, "u", &reply))
153 {
154 ERR("Error reading message arguments");
155 return;
156 }
157
158 switch (reply)
159 {
160 case ELDBUS_NAME_RELEASE_REPLY_RELEASED:
161 INF("Name released");
162 break;
163 case ELDBUS_NAME_RELEASE_REPLY_NON_EXISTENT:
164 INF("Name non existent");
165 break;
166 case ELDBUS_NAME_RELEASE_REPLY_NOT_OWNER:
167 INF("Not owner");
168 break;
169 default:
170 ERR("Unexpected reply: %d", reply);
171 break;
172 }
173
174 ecore_idler_add(main_loop_quit_idler, NULL);
175}
176
177static void
178finish(int foo EINA_UNUSED)
179{
180 eldbus_name_release(conn, PANEL_BUS, name_release_cb, NULL);
181}
182
43int 183int
44main(void) 184main(int argc EINA_UNUSED, char *argv[] EINA_UNUSED)
45{ 185{
46 Eldbus_Connection *conn;
47 Eldbus_Object *obj;
48 Eldbus_Proxy *proxy;
49 const char *address; 186 const char *address;
50 187
51 eina_init(); 188 /* Init */
52 _client_log_dom = eina_log_domain_register("connect_address", EINA_COLOR_CYAN); 189 if (!eina_init())
53 if (_client_log_dom < 0) 190 {
191 EINA_LOG_ERR("Failed to initialize Eina");
192 return 1;
193 }
194
195 _conn_addr_log_dom = eina_log_domain_register("connect_address", EINA_COLOR_CYAN);
196
197 if (_conn_addr_log_dom < 0)
54 { 198 {
55 EINA_LOG_ERR("Unable to create 'client' log domain"); 199 EINA_LOG_ERR("Unable to create 'client' log domain");
56 goto exit_eina; 200 goto exit_eina;
@@ -62,37 +206,67 @@ main(void)
62 goto exit_eina; 206 goto exit_eina;
63 } 207 }
64 208
65 ecore_init(); 209 if (!ecore_init())
66 eldbus_init(); 210 {
211 EINA_LOG_ERR("Failed to initialize Ecore");
212 goto exit_eina;
213 }
214
215 if (!eldbus_init())
216 {
217 EINA_LOG_ERR("Failed to initialize Eldbus");
218 goto exit_ecore;
219 }
67 220
68 printf("Connecting to %s\n", address); 221 signal(SIGTERM, finish);
222 signal(SIGINT, finish);
223
224 /* Connect */
225 printf("Connecting to IBus with address: %s\n", address);
69 conn = eldbus_address_connection_get(address); 226 conn = eldbus_address_connection_get(address);
70 227
71 if (!conn) 228 if (!conn)
72 { 229 {
73 ERR("Failed to get dbus connection to address '%s'", address); 230 ERR("Failed to get dbus connection with address '%s'\n", address);
74 goto end; 231 goto end;
75 } 232 }
76 233
77 printf("CONNECTED!!!\n"); 234 eldbus_name_owner_changed_callback_add(conn, IBUS_BUS, on_name_owner_changed,
78 obj = eldbus_object_get(conn, BUS, PATH); 235 conn, EINA_TRUE);
79 proxy = eldbus_proxy_get(obj, INTERFACE);
80 eldbus_proxy_signal_handler_add(proxy, "RegistryChanged", _on_registry_changed, NULL);
81 eldbus_proxy_signal_handler_add(proxy, "GlobalEngineChanged", _on_global_engine_changed, NULL);
82 236
83 eldbus_name_owner_changed_callback_add(conn, BUS, on_name_owner_changed, 237 eldbus_name_owner_changed_callback_add(conn, PANEL_BUS, on_name_owner_changed,
84 conn, EINA_TRUE); 238 conn, EINA_TRUE);
85 ecore_timer_add(30, finish, NULL);
86 239
87 ecore_main_loop_begin(); 240 acquired = eldbus_signal_handler_add(conn,
241 ELDBUS_FDO_BUS,
242 ELDBUS_FDO_PATH,
243 PANEL_INTERFACE,
244 "NameAcquired",
245 on_name_acquired,
246 NULL);
88 247
89 eldbus_connection_unref(conn); 248 lost = eldbus_signal_handler_add(conn,
249 ELDBUS_FDO_BUS,
250 ELDBUS_FDO_PATH,
251 PANEL_INTERFACE,
252 "NameLost",
253 on_name_lost,
254 NULL);
255
256 /* Request ownership of PANEL_BUS */
257 eldbus_name_request(conn, PANEL_BUS,
258 ELDBUS_NAME_REQUEST_FLAG_REPLACE_EXISTING | ELDBUS_NAME_REQUEST_FLAG_ALLOW_REPLACEMENT,
259 name_request_cb, NULL);
260
261 ecore_main_loop_begin();
90 262
91end: 263end:
92 eldbus_shutdown(); 264 eldbus_shutdown();
265
266exit_ecore:
93 ecore_shutdown(); 267 ecore_shutdown();
268 eina_log_domain_unregister(_conn_addr_log_dom);
94 269
95 eina_log_domain_unregister(_client_log_dom);
96exit_eina: 270exit_eina:
97 eina_shutdown(); 271 eina_shutdown();
98 272