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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
/*
* Copyright © 2013 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <Ecore.h>
#include <Eldbus.h>
#include "wkb-ibus.h"
#include "wkb-ibus-defs.h"
int _wkb_ibus_log_dom = -1;
#define _check_message_errors(_msg) \
do \
{ \
const char *error, *error_msg; \
if (eldbus_message_error_get(_msg, &error, &error_msg)) \
{ \
ERR("Dbus message error: %s: %s", error, error_msg); \
return; \
} \
DBG("Message '%s' with signature '%s'", eldbus_message_member_get(_msg), eldbus_message_signature_get(_msg)); \
} while (0)
struct _wkb_ibus_context
{
char *address;
Ecore_Exe *ibus_daemon;
Eldbus_Connection *conn;
Eldbus_Service_Interface *config;
Eldbus_Service_Interface *panel;
Eldbus_Signal_Handler *name_acquired;
Eldbus_Signal_Handler *name_lost;
int refcount;
Eina_Bool address_pending;
};
static struct _wkb_ibus_context *ctx = NULL;
static void
_wkb_config_value_changed_cb(void *data, const Eldbus_Message *msg)
{
const char *section, *name;
Eldbus_Message_Iter *value;
_check_message_errors(msg);
if (!eldbus_message_arguments_get(msg, "ssv", §ion, &name, &value))
{
ERR("Error reading message arguments");
return;
}
DBG("section: '%s', name: '%s', value: '%p", section, name, value);
}
static void
_wkb_name_owner_changed_cb(void *data, const char *bus, const char *old_id, const char *new_id)
{
DBG("NameOwnerChanged Bus=%s | old=%s | new=%s", bus, old_id, new_id);
}
static void
_wkb_name_acquired_cb(void *data, const Eldbus_Message *msg)
{
const char *name;
_check_message_errors(msg);
if (!eldbus_message_arguments_get(msg, "s", &name))
{
ERR("Error reading message arguments");
return;
}
DBG("NameAcquired: '%s'", name);
if (strncmp(name, IBUS_INTERFACE_PANEL, strlen(IBUS_INTERFACE_PANEL)) == 0)
{
ctx->panel = wkb_ibus_panel_register(ctx->conn);
INF("Registering Panel Interface: %s", ctx->panel ? "Success" : "Fail");
}
else if (strncmp(name, IBUS_INTERFACE_CONFIG, strlen(IBUS_INTERFACE_CONFIG)) == 0)
{
ctx->config = wkb_ibus_config_register(ctx->conn);
INF("Registering Config Interface: %s", ctx->config ? "Success" : "Fail");
}
else
{
WRN("Unexpected name %s", name);
}
}
static void
_wkb_name_lost_cb(void *data, const Eldbus_Message *msg)
{
const char *name;
DBG("NameLost");
_check_message_errors(msg);
if (!eldbus_message_arguments_get(msg, "s", &name))
{
ERR("Error reading message arguments");
return;
}
DBG("Name = %s", name);
}
static Eina_Bool
_wkb_ibus_shutdown_idler(void *data)
{
wkb_ibus_shutdown();
return ECORE_CALLBACK_CANCEL;
}
static void
_wkb_name_request_cb(void *data, const Eldbus_Message *msg, Eldbus_Pending *pending)
{
const char *error, *error_msg;
unsigned int reply;
DBG("NameRequest callback");
if (eldbus_message_error_get(msg, &error, &error_msg))
{
ERR("DBus message error: %s: %s", error, error_msg);
goto error;
}
if (!eldbus_message_arguments_get(msg, "u", &reply))
{
ERR("Error reading message arguments");
goto error;
}
if (reply != ELDBUS_NAME_REQUEST_REPLY_PRIMARY_OWNER &&
reply != ELDBUS_NAME_REQUEST_REPLY_ALREADY_OWNER)
{
ERR("Not primary owner: reply=%d", reply);
goto error;
}
return;
error:
ecore_idler_add(_wkb_ibus_shutdown_idler, NULL);
}
static void
_wkb_name_release_cb(void *data, const Eldbus_Message *msg, Eldbus_Pending *pending)
{
unsigned int reply;
_check_message_errors(msg);
if (!eldbus_message_arguments_get(msg, "u", &reply))
{
ERR("Error reading message arguments");
return;
}
if (reply != ELDBUS_NAME_RELEASE_REPLY_RELEASED)
{
ERR("Unexpected name release reply: %d", reply);
return;
}
}
static void
_wkb_ibus_launch_daemon(void)
{
DBG("Launching ibus-daemon");
ctx->ibus_daemon = ecore_exe_run("ibus-daemon -s", NULL);
if (!ctx->ibus_daemon)
{
ERR("Error launching ibus-daemon process");
return;
}
}
static Eina_Bool
_wkb_ibus_query_address_cb(void *data, int type, void *event)
{
Ecore_Exe_Event_Data *exe_data = (Ecore_Exe_Event_Data *)event;
if (strncmp(exe_data->data, "(null)", exe_data->size) == 0)
{
INF("IBus daemon is not running.");
_wkb_ibus_launch_daemon();
goto end;
}
else if (strstr(exe_data->data, "unknown command") != NULL)
{
ERR("ibus command does not support the 'address' argument");
goto end;
}
free(ctx->address);
ctx->address = strndup(exe_data->data, exe_data->size);
end:
ecore_idler_add(ecore_exe_free, exe_data->exe);
ctx->address_pending = EINA_FALSE;
return ECORE_CALLBACK_DONE;
}
static void
_wkb_ibus_query_address(void)
{
const char *ibus_addr;
Ecore_Exe *ibus_exec = NULL;
/* Check for IBUS_ADDRESS environment variable */
if ((ibus_addr = getenv("IBUS_ADDRESS")) != NULL)
{
DBG("Got IBus address from IBUS_ADDRESS environment variable %s", ibus_addr);
ctx->address = strdup(ibus_addr);
return;
}
/* Get IBus address by invoking 'ibus address' from command line */
DBG("Querying IBus address from using 'ibus address' command");
ibus_exec = ecore_exe_pipe_run("ibus address",
ECORE_EXE_PIPE_READ |
ECORE_EXE_PIPE_READ_LINE_BUFFERED,
NULL);
if (!ibus_exec)
{
ERR("Unable to retrieve IBus address");
return;
}
ctx->address_pending = EINA_TRUE;
ecore_event_handler_add(ECORE_EXE_EVENT_DATA, _wkb_ibus_query_address_cb, NULL);
}
Eina_Bool
wkb_ibus_connect(void)
{
if (ctx->conn)
return EINA_TRUE;
if (!ctx->address)
{
INF("IBus address is not set.");
if (!ctx->address_pending)
_wkb_ibus_query_address();
return EINA_FALSE;
}
INF("Connecting to IBus at address '%s'", ctx->address);
ctx->conn = eldbus_address_connection_get(ctx->address);
if (!ctx->conn)
{
ERR("Error connecting to IBus");
return EINA_FALSE;
}
ctx->name_acquired = eldbus_signal_handler_add(ctx->conn,
ELDBUS_FDO_BUS,
ELDBUS_FDO_PATH,
ELDBUS_FDO_INTERFACE,
"NameAcquired",
_wkb_name_acquired_cb,
ctx);
ctx->name_lost = eldbus_signal_handler_add(ctx->conn,
ELDBUS_FDO_BUS,
ELDBUS_FDO_PATH,
ELDBUS_FDO_INTERFACE,
"NameLost",
_wkb_name_lost_cb,
ctx);
/* Config */
eldbus_name_owner_changed_callback_add(ctx->conn,
IBUS_SERVICE_CONFIG,
_wkb_name_owner_changed_cb,
ctx, EINA_TRUE);
DBG("Requesting ownership of " IBUS_SERVICE_CONFIG);
eldbus_name_request(ctx->conn, IBUS_SERVICE_CONFIG,
ELDBUS_NAME_REQUEST_FLAG_REPLACE_EXISTING | ELDBUS_NAME_REQUEST_FLAG_DO_NOT_QUEUE,
_wkb_name_request_cb, ctx);
/* Panel */
eldbus_name_owner_changed_callback_add(ctx->conn,
IBUS_SERVICE_PANEL,
_wkb_name_owner_changed_cb,
ctx, EINA_TRUE);
DBG("Requesting ownership of " IBUS_SERVICE_PANEL);
eldbus_name_request(ctx->conn, IBUS_SERVICE_PANEL,
ELDBUS_NAME_REQUEST_FLAG_REPLACE_EXISTING | ELDBUS_NAME_REQUEST_FLAG_DO_NOT_QUEUE,
_wkb_name_request_cb, ctx);
return EINA_TRUE;
}
int
wkb_ibus_init(void)
{
if (ctx && ctx->refcount > 0)
goto end;
if (!eina_init())
{
fprintf(stderr, "Error initializing Eina\n");
return 0;
}
_wkb_ibus_log_dom = eina_log_domain_register("wkb-ibus", EINA_COLOR_LIGHTCYAN);
if (_wkb_ibus_log_dom < 0)
{
EINA_LOG_ERR("Unable to register 'wkb-ibus' log domain");
eina_shutdown();
return 0;
}
if (!ctx && !(ctx = calloc(1, sizeof(*ctx))))
{
ERR("Error calloc\n");
eina_shutdown();
return 0;
}
_wkb_ibus_query_address();
end:
return ++ctx->refcount;
}
void
wkb_ibus_shutdown(void)
{
if (!ctx)
{
fprintf(stderr, "Not initialized\n");
return;
}
if (ctx->refcount == 0)
{
ERR("Refcount already 0");
goto end;
}
if (--(ctx->refcount) != 0)
return;
DBG("Shutting down");
wkb_ibus_disconnect();
free(ctx->address);
if (ctx->ibus_daemon)
{
DBG("Terminating ibus-daemon");
ecore_exe_terminate(ctx->ibus_daemon);
ecore_exe_free(ctx->ibus_daemon);
}
end:
free(ctx);
ctx = NULL;
ecore_main_loop_quit();
DBG("Main loop quit");
}
void
wkb_ibus_disconnect(void)
{
if (!ctx->conn)
{
ERR("Not connected");
return;
}
DBG("Disconnect");
eldbus_signal_handler_del(ctx->name_acquired);
eldbus_signal_handler_del(ctx->name_lost);
if (ctx->panel)
{
eldbus_name_release(ctx->conn, IBUS_SERVICE_PANEL, _wkb_name_release_cb, ctx);
eldbus_service_interface_unregister(ctx->panel);
ctx->panel = NULL;
}
if (ctx->config)
{
wkb_ibus_config_unregister();
eldbus_name_release(ctx->conn, IBUS_SERVICE_CONFIG, _wkb_name_release_cb, ctx);
eldbus_service_interface_unregister(ctx->config);
ctx->config = NULL;
}
eldbus_connection_unref(ctx->conn);
}
Eina_Bool
wkb_ibus_is_connected(void)
{
return ctx->conn != NULL;
}
|