summaryrefslogtreecommitdiff
path: root/src/examples/eldbus/connect-address.c
blob: 236a9b69eaa843c3119442891bc27624475b274f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * compile with:
 * gcc -o client client.c `pkg-config --cflags --libs eldbus ecore eina`
 *
 * This simple example connects to an IBus daemon running on the machine.
 * IBus daemon is a dbus daemon running apart from system or session buses.
 *
 * Requires ibus-daemon to be running, for instance:
 * $ ibus-daemon -v -r -s
 *
 * After starting ibus-daemon, check for its address on the file located on
 * ~/.config/ibus/bus/ directory. For instance:
 *
 * $ cat ~/.config/ibus/bus/809ee018614317e782464b8900000029-unix-0
 * # This file is created by ibus-daemon, please do not modify it
 * IBUS_ADDRESS=unix:abstract=/tmp/dbus-VOJaWARY,guid=e0082807a08a81ad22e0d3f551d48d5e
 * IBUS_DAEMON_PID=3697
 *
 * Export or set the IBUS_ADDRESS environment variable and run this program. To exit,
 * just hit ctrl+c
 */

#include <stdlib.h>
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <signal.h>

#include "Eldbus.h"
#include <Ecore.h>

#define IBUS_BUS "org.freedesktop.IBus"
#define PANEL_BUS "org.freedesktop.IBUS.Panel"
#define PANEL_INTERFACE "org.freedesktop.IBUS.Panel"

static int _conn_addr_log_dom = -1;
#define DBG(...) EINA_LOG_DOM_DBG(_conn_addr_log_dom, __VA_ARGS__)
#define INF(...) EINA_LOG_DOM_INFO(_conn_addr_log_dom, __VA_ARGS__)
#define ERR(...) EINA_LOG_DOM_ERR(_conn_addr_log_dom, __VA_ARGS__)

static Eldbus_Connection *conn;
static Eldbus_Signal_Handler *acquired;
static Eldbus_Signal_Handler *lost;

static void
on_name_owner_changed(void *data EINA_UNUSED, const char *bus, const char *old_id, const char *new_id EINA_UNUSED)
{
   INF("Name owner changed: bus=%s | old=%s | new=%s\n", bus, old_id, new_id);
}

static void
on_name_acquired(void *data EINA_UNUSED, const Eldbus_Message *msg)
{
   const char *error, *error_msg, *name;

   if (eldbus_message_error_get(msg, &error, &error_msg))
     {
        ERR("Name acquired message error: %s: %s\n", error, error_msg);
        return;
     }

   if (!eldbus_message_arguments_get(msg, "s", &name))
     {
        ERR("Error reading message arguments");
        return;
     }

   INF("Name acquired: %s", name);
}

static void
on_name_lost(void *data EINA_UNUSED, const Eldbus_Message *msg)
{
   const char *error, *error_msg, *name;

   if (eldbus_message_error_get(msg, &error, &error_msg))
     {
        ERR("Name lost message error: %s: %s\n", error, error_msg);
        return;
     }

   if (!eldbus_message_arguments_get(msg, "s", &name))
     {
        ERR("Error reading message arguments");
        return;
     }

   INF("Name lost: %s", name);
}

static void
name_request_cb(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
   const char *error, *error_msg;
   unsigned int reply;

   if (eldbus_message_error_get(msg, &error, &error_msg))
     {
        ERR("Name request message error: %s: %s\n", error, error_msg);
        return;
     }

   if (!eldbus_message_arguments_get(msg, "u", &reply))
     {
        ERR("Error reading message arguments");
        return;
     }

   switch (reply)
     {
      case ELDBUS_NAME_REQUEST_REPLY_PRIMARY_OWNER:
         INF("Got primary ownership");
         break;
      case ELDBUS_NAME_REQUEST_REPLY_IN_QUEUE:
         INF("Got queued for ownership");
         break;
      case ELDBUS_NAME_REQUEST_REPLY_EXISTS:
         INF("Already in queue for ownership");
         break;
      case ELDBUS_NAME_REQUEST_REPLY_ALREADY_OWNER:
         INF("Already primary owner");
         break;
      default:
         ERR("Unexpected reply: %d", reply);
         break;
     }
}

static Eina_Bool
main_loop_quit_idler(void *data EINA_UNUSED)
{
   eldbus_signal_handler_del(acquired);
   eldbus_signal_handler_del(lost);
   eldbus_connection_unref(conn);

   INF("Finishing");
   ecore_main_loop_quit();
   return ECORE_CALLBACK_CANCEL;
}

static void
name_release_cb(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
   const char *error, *error_msg;
   unsigned int reply;

   if (eldbus_message_error_get(msg, &error, &error_msg))
     {
        ERR("Name release message error: %s: %s\n", error, error_msg);
        return;
     }

   if (!eldbus_message_arguments_get(msg, "u", &reply))
     {
        ERR("Error reading message arguments");
        return;
     }

   switch (reply)
     {
      case ELDBUS_NAME_RELEASE_REPLY_RELEASED:
         INF("Name released");
         break;
      case ELDBUS_NAME_RELEASE_REPLY_NON_EXISTENT:
         INF("Name non existent");
         break;
      case ELDBUS_NAME_RELEASE_REPLY_NOT_OWNER:
         INF("Not owner");
         break;
      default:
         ERR("Unexpected reply: %d", reply);
         break;
     }

   ecore_idler_add(main_loop_quit_idler, NULL);
}

static void
finish(int foo EINA_UNUSED)
{
   eldbus_name_release(conn, PANEL_BUS, name_release_cb, NULL);
}

int
main(int argc EINA_UNUSED, char *argv[] EINA_UNUSED)
{
   const char *address;

   /* Init */
   if (!eina_init())
     {
        EINA_LOG_ERR("Failed to initialize Eina");
        return 1;
     }

   _conn_addr_log_dom = eina_log_domain_register("connect_address", EINA_COLOR_CYAN);

   if (_conn_addr_log_dom < 0)
     {
        EINA_LOG_ERR("Unable to create 'client' log domain");
        goto exit_eina;
     }

   if (!(address = getenv("IBUS_ADDRESS")))
     {
        ERR("IBUS_ADDRESS environment variable must be set");
        goto exit_eina;
     }

   if (!ecore_init())
     {
        EINA_LOG_ERR("Failed to initialize Ecore");
        goto exit_eina;
     }

   if (!eldbus_init())
     {
        EINA_LOG_ERR("Failed to initialize Eldbus");
        goto exit_ecore;
     }

   signal(SIGTERM, finish);
   signal(SIGINT, finish);

   /* Connect */
   printf("Connecting to IBus with address: %s\n", address);
   conn = eldbus_address_connection_get(address);

   if (!conn)
     {
        ERR("Failed to get dbus connection with address '%s'\n", address);
        goto end;
     }

   eldbus_name_owner_changed_callback_add(conn, IBUS_BUS, on_name_owner_changed,
                                         conn, EINA_TRUE);

   eldbus_name_owner_changed_callback_add(conn, PANEL_BUS, on_name_owner_changed,
                                         conn, EINA_TRUE);

   acquired = eldbus_signal_handler_add(conn,
                                        ELDBUS_FDO_BUS,
                                        ELDBUS_FDO_PATH,
                                        PANEL_INTERFACE,
                                        "NameAcquired",
                                        on_name_acquired,
                                        NULL);

   lost = eldbus_signal_handler_add(conn,
                                    ELDBUS_FDO_BUS,
                                    ELDBUS_FDO_PATH,
                                    PANEL_INTERFACE,
                                    "NameLost",
                                    on_name_lost,
                                    NULL);

   /* Request ownership of PANEL_BUS */
   eldbus_name_request(conn, PANEL_BUS,
                       ELDBUS_NAME_REQUEST_FLAG_REPLACE_EXISTING | ELDBUS_NAME_REQUEST_FLAG_ALLOW_REPLACEMENT,
                       name_request_cb, NULL);

   ecore_main_loop_begin();

end:
   eldbus_shutdown();

exit_ecore:
   ecore_shutdown();
   eina_log_domain_unregister(_conn_addr_log_dom);

exit_eina:
   eina_shutdown();

   return 0;
}