| 1 |
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */ |
| 2 |
/* vim:set et sts=4: */ |
| 3 |
/* ibus |
| 4 |
* Copyright (C) 2007-2015 Peng Huang <[email protected]> |
| 5 |
* Copyright (C) 2015-2025 Takao Fujiwara <[email protected]> |
| 6 |
* Copyright (C) 2007-2015 Red Hat, Inc. |
| 7 |
* |
| 8 |
* main.c: |
| 9 |
* |
| 10 |
* This library is free software; you can redistribute it and/or |
| 11 |
* modify it under the terms of the GNU Lesser General Public |
| 12 |
* License as published by the Free Software Foundation; either |
| 13 |
* version 2.1 of the License, or (at your option) any later version. |
| 14 |
* |
| 15 |
* This library is distributed in the hope that it will be useful, |
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 |
* Lesser General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU Lesser General Public |
| 21 |
* License along with this library; if not, write to the Free Software |
| 22 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 23 |
* USA |
| 24 |
*/ |
| 25 |
#define _GNU_SOURCE |
| 26 |
|
| 27 |
#include "config.h" |
| 28 |
|
| 29 |
#include <X11/Xproto.h> |
| 30 |
#include <X11/Xlib.h> |
| 31 |
#include <X11/keysym.h> |
| 32 |
#include <X11/Xutil.h> |
| 33 |
#ifdef HAVE_XFIXES |
| 34 |
#include <X11/extensions/Xfixes.h> |
| 35 |
#endif |
| 36 |
#include <XimProto.h> |
| 37 |
#include <IMdkit.h> |
| 38 |
#include <Xi18n.h> |
| 39 |
#include <stdio.h> |
| 40 |
#include <gtk/gtk.h> |
| 41 |
#include <gdk/gdk.h> |
| 42 |
#include <gdk/gdkx.h> |
| 43 |
#include <langinfo.h> |
| 44 |
#include <locale.h> |
| 45 |
#include <iconv.h> |
| 46 |
#include <signal.h> |
| 47 |
#include <stdlib.h> |
| 48 |
|
| 49 |
#include <getopt.h> |
| 50 |
|
| 51 |
#define ESC_SEQUENCE_ISO10646_1 "\033%G" |
| 52 |
/* Wait for about 120 secs to return a key from async process-key-event. */ |
| 53 |
#define MAX_WAIT_KEY_TIME 120000 |
| 54 |
|
| 55 |
#define LOG(level, fmt_args...) \ |
| 56 |
if (g_debug_level >= (level)) { \ |
| 57 |
g_debug (fmt_args); \ |
| 58 |
} |
| 59 |
|
| 60 |
#include <ibus.h> |
| 61 |
#include "gdk-private.h" |
| 62 |
#include "locales.h" |
| 63 |
|
| 64 |
struct _X11ICONN { |
| 65 |
GList *clients; |
| 66 |
}; |
| 67 |
typedef struct _X11ICONN X11ICONN; |
| 68 |
|
| 69 |
typedef struct _X11IC X11IC; |
| 70 |
struct _X11IC { |
| 71 |
IBusInputContext *context; |
| 72 |
Window client_window; |
| 73 |
Window focus_window; |
| 74 |
gint32 input_style; |
| 75 |
X11ICONN *conn; |
| 76 |
gint icid; |
| 77 |
gint connect_id; |
| 78 |
gchar *lang; |
| 79 |
gboolean has_preedit_area; |
| 80 |
GdkRectangle preedit_area; |
| 81 |
|
| 82 |
gchar *preedit_string; |
| 83 |
IBusAttrList *preedit_attrs; |
| 84 |
gint preedit_cursor; |
| 85 |
gboolean preedit_visible; |
| 86 |
gboolean preedit_started; |
| 87 |
gint onspot_preedit_length; |
| 88 |
}; |
| 89 |
|
| 90 |
static void _xim_set_cursor_location (X11IC *x11ic); |
| 91 |
static void _context_commit_text_cb (IBusInputContext *context, |
| 92 |
IBusText *text, |
| 93 |
X11IC *x11ic); |
| 94 |
static void _context_forward_key_event_cb |
| 95 |
(IBusInputContext *context, |
| 96 |
guint keyval, |
| 97 |
guint keycode, |
| 98 |
guint state, |
| 99 |
X11IC *x11ic); |
| 100 |
|
| 101 |
static void _context_update_preedit_text_cb |
| 102 |
(IBusInputContext *context, |
| 103 |
IBusText *text, |
| 104 |
gint cursor_pos, |
| 105 |
gboolean visible, |
| 106 |
X11IC *x11ic); |
| 107 |
static void _context_show_preedit_text_cb |
| 108 |
(IBusInputContext *context, |
| 109 |
X11IC *x11ic); |
| 110 |
static void _context_hide_preedit_text_cb |
| 111 |
(IBusInputContext *context, |
| 112 |
X11IC *x11ic); |
| 113 |
static void _context_enabled_cb (IBusInputContext *context, |
| 114 |
X11IC *x11ic); |
| 115 |
static void _context_disabled_cb (IBusInputContext *context, |
| 116 |
X11IC *x11ic); |
| 117 |
|
| 118 |
static GHashTable *_x11_ic_table = NULL; |
| 119 |
static GHashTable *_connections = NULL; |
| 120 |
static XIMS _xims = NULL; |
| 121 |
static gchar *_server_name = NULL; |
| 122 |
static gchar *_locale = NULL; |
| 123 |
|
| 124 |
static gboolean _kill_daemon = FALSE; |
| 125 |
static gint g_debug_level = 0; |
| 126 |
|
| 127 |
static IBusBus *_bus = NULL; |
| 128 |
|
| 129 |
static char _use_sync_mode = 1; |
| 130 |
|
| 131 |
static void |
| 132 |
_xim_preedit_start (XIMS xims, const X11IC *x11ic) |
| 133 |
{ |
| 134 |
IMPreeditStateStruct ips; |
| 135 |
ips.major_code = 0; |
| 136 |
ips.minor_code = 0; |
| 137 |
ips.icid = x11ic->icid; |
| 138 |
ips.connect_id = x11ic->connect_id; |
| 139 |
IMPreeditStart (xims, (XPointer)&ips); |
| 140 |
} |
| 141 |
|
| 142 |
static void |
| 143 |
_xim_preedit_end (XIMS xims, const X11IC *x11ic) |
| 144 |
{ |
| 145 |
IMPreeditStateStruct ips; |
| 146 |
ips.major_code = 0; |
| 147 |
ips.minor_code = 0; |
| 148 |
ips.icid = x11ic->icid; |
| 149 |
ips.connect_id = x11ic->connect_id; |
| 150 |
IMPreeditEnd (xims, (XPointer)&ips); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
static void |
| 155 |
_xim_preedit_callback_start (XIMS xims, const X11IC *x11ic) |
| 156 |
{ |
| 157 |
IMPreeditCBStruct pcb; |
| 158 |
|
| 159 |
pcb.major_code = XIM_PREEDIT_START; |
| 160 |
pcb.minor_code = 0; |
| 161 |
pcb.connect_id = x11ic->connect_id; |
| 162 |
pcb.icid = x11ic->icid; |
| 163 |
pcb.todo.return_value = 0; |
| 164 |
IMCallCallback (xims, (XPointer) & pcb); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
static void |
| 169 |
_xim_preedit_callback_done (XIMS xims, const X11IC *x11ic) |
| 170 |
{ |
| 171 |
IMPreeditCBStruct pcb; |
| 172 |
|
| 173 |
pcb.major_code = XIM_PREEDIT_DONE; |
| 174 |
pcb.minor_code = 0; |
| 175 |
pcb.connect_id = x11ic->connect_id; |
| 176 |
pcb.icid = x11ic->icid; |
| 177 |
pcb.todo.return_value = 0; |
| 178 |
IMCallCallback (xims, (XPointer) & pcb); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
static void |
| 183 |
_xim_preedit_callback_draw (XIMS xims, X11IC *x11ic, const gchar *preedit_string, IBusAttrList *attr_list) |
| 184 |
{ |
| 185 |
IMPreeditCBStruct pcb; |
| 186 |
XIMText text; |
| 187 |
XTextProperty tp; |
| 188 |
|
| 189 |
static XIMFeedback *feedback; |
| 190 |
static gint feedback_len = 0; |
| 191 |
guint j, i, len; |
| 192 |
|
| 193 |
if (preedit_string == NULL) |
| 194 |
return; |
| 195 |
|
| 196 |
len = g_utf8_strlen (preedit_string, -1); |
| 197 |
|
| 198 |
if (len + 1 > feedback_len) { |
| 199 |
feedback_len = (len + 1 + 63) & ~63; |
| 200 |
if (feedback) { |
| 201 |
feedback = g_renew (XIMFeedback, feedback, feedback_len); |
| 202 |
} |
| 203 |
else { |
| 204 |
feedback = g_new (XIMFeedback, feedback_len); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
for (i = 0; feedback && i < len; i++) { |
| 209 |
feedback[i] = 0; |
| 210 |
} |
| 211 |
|
| 212 |
if (attr_list != NULL) { |
| 213 |
for (i = 0;; i++) { |
| 214 |
XIMFeedback attr = 0; |
| 215 |
IBusAttribute *ibus_attr = ibus_attr_list_get (attr_list, i); |
| 216 |
if (ibus_attr == NULL) { |
| 217 |
break; |
| 218 |
} |
| 219 |
switch (ibus_attr->type) { |
| 220 |
case IBUS_ATTR_TYPE_UNDERLINE: |
| 221 |
if (ibus_attr->value == IBUS_ATTR_UNDERLINE_SINGLE) { |
| 222 |
attr = XIMUnderline; |
| 223 |
} |
| 224 |
break; |
| 225 |
case IBUS_ATTR_TYPE_BACKGROUND: |
| 226 |
{ |
| 227 |
if (ibus_attr->value != 0xffffff) { |
| 228 |
attr = XIMReverse; |
| 229 |
} |
| 230 |
break; |
| 231 |
} |
| 232 |
default: |
| 233 |
continue; |
| 234 |
} |
| 235 |
for (j = ibus_attr->start_index; j < ibus_attr->end_index; j++) { |
| 236 |
feedback[j] |= attr; |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
for (i = 0; feedback && i < len; i++) { |
| 242 |
if (feedback[i] == 0) { |
| 243 |
feedback[i] = XIMUnderline; |
| 244 |
} |
| 245 |
} |
| 246 |
if (feedback) |
| 247 |
feedback[len] = 0; |
| 248 |
|
| 249 |
pcb.major_code = XIM_PREEDIT_DRAW; |
| 250 |
pcb.connect_id = x11ic->connect_id; |
| 251 |
pcb.icid = x11ic->icid; |
| 252 |
|
| 253 |
pcb.todo.draw.caret = len; |
| 254 |
pcb.todo.draw.chg_first = 0; |
| 255 |
pcb.todo.draw.chg_length = x11ic->onspot_preedit_length; |
| 256 |
pcb.todo.draw.text = &text; |
| 257 |
|
| 258 |
text.feedback = feedback; |
| 259 |
|
| 260 |
if (len > 0) { |
| 261 |
int ret = Xutf8TextListToTextProperty ( |
| 262 |
GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), |
| 263 |
(char **)&preedit_string, |
| 264 |
1, XCompoundTextStyle, &tp); |
| 265 |
if (ret == EXIT_FAILURE) { |
| 266 |
XFree (tp.value); |
| 267 |
tp.value = (unsigned char *)g_strdup_printf ( |
| 268 |
"%s%s", |
| 269 |
ESC_SEQUENCE_ISO10646_1, |
| 270 |
preedit_string); |
| 271 |
} |
| 272 |
text.encoding_is_wchar = 0; |
| 273 |
text.length = strlen ((char*)tp.value); |
| 274 |
text.string.multi_byte = (char*)tp.value; |
| 275 |
IMCallCallback (xims, (XPointer) & pcb); |
| 276 |
XFree (tp.value); |
| 277 |
} else { |
| 278 |
text.encoding_is_wchar = 0; |
| 279 |
text.length = 0; |
| 280 |
text.string.multi_byte = ""; |
| 281 |
IMCallCallback (xims, (XPointer) & pcb); |
| 282 |
len = 0; |
| 283 |
} |
| 284 |
x11ic->onspot_preedit_length = len; |
| 285 |
} |
| 286 |
|
| 287 |
static int |
| 288 |
_xim_store_ic_values (X11IC *x11ic, IMChangeICStruct *call_data) |
| 289 |
{ |
| 290 |
XICAttribute *ic_attr = call_data->ic_attr; |
| 291 |
XICAttribute *pre_attr = call_data->preedit_attr; |
| 292 |
XICAttribute *sts_attr = call_data->status_attr; |
| 293 |
|
| 294 |
gint i; |
| 295 |
|
| 296 |
g_return_val_if_fail (x11ic != NULL, 0); |
| 297 |
for (i = 0; i < (int)call_data->ic_attr_num; ++i, ++ic_attr) { |
| 298 |
if (g_strcmp0 (XNInputStyle, ic_attr->name) == 0) { |
| 299 |
x11ic->input_style = *(gint32 *) ic_attr->value; |
| 300 |
} |
| 301 |
else if (g_strcmp0 (XNClientWindow, ic_attr->name) == 0) { |
| 302 |
x11ic->client_window = (Window)(*(CARD32 *) call_data->ic_attr[i].value); |
| 303 |
} |
| 304 |
else if (g_strcmp0 (XNFocusWindow, ic_attr->name) == 0) { |
| 305 |
x11ic->focus_window = (Window)(*(CARD32 *) call_data->ic_attr[i].value); |
| 306 |
} |
| 307 |
else { |
| 308 |
LOG (1, "Unknown ic attribute: %s", ic_attr->name); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
for (i = 0; i < (int)call_data->preedit_attr_num; ++i, ++pre_attr) { |
| 313 |
if (g_strcmp0 (XNSpotLocation, pre_attr->name) == 0) { |
| 314 |
x11ic->has_preedit_area = TRUE; |
| 315 |
x11ic->preedit_area.x = ((XPoint *)pre_attr->value)->x; |
| 316 |
x11ic->preedit_area.y = ((XPoint *)pre_attr->value)->y; |
| 317 |
} |
| 318 |
else { |
| 319 |
LOG (1, "Unknown preedit attribute: %s", pre_attr->name); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
for (i=0; i< (int) call_data->status_attr_num; ++i, ++sts_attr) { |
| 324 |
LOG (1, "Unknown status attribute: %s", sts_attr->name); |
| 325 |
} |
| 326 |
|
| 327 |
return 1; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
static int |
| 332 |
xim_create_ic (XIMS xims, IMChangeICStruct *call_data) |
| 333 |
{ |
| 334 |
static int base_icid = 1; |
| 335 |
X11IC *x11ic; |
| 336 |
guint32 capabilities = IBUS_CAP_FOCUS; |
| 337 |
|
| 338 |
call_data->icid = base_icid ++; |
| 339 |
|
| 340 |
LOG (1, "XIM_CREATE_IC ic=%d connect_id=%d", |
| 341 |
call_data->icid, call_data->connect_id); |
| 342 |
|
| 343 |
x11ic = g_slice_new0 (X11IC); |
| 344 |
g_return_val_if_fail (x11ic != NULL, 0); |
| 345 |
|
| 346 |
x11ic->icid = call_data->icid; |
| 347 |
x11ic->connect_id = call_data->connect_id; |
| 348 |
x11ic->conn = (X11ICONN *)g_hash_table_lookup (_connections, |
| 349 |
GINT_TO_POINTER ((gint) call_data->connect_id)); |
| 350 |
if (x11ic->conn == NULL) { |
| 351 |
g_slice_free (X11IC, x11ic); |
| 352 |
g_return_val_if_reached (0); |
| 353 |
} |
| 354 |
|
| 355 |
_xim_store_ic_values (x11ic, call_data); |
| 356 |
|
| 357 |
x11ic->context = ibus_bus_create_input_context (_bus, "xim"); |
| 358 |
|
| 359 |
if (x11ic->context == NULL) { |
| 360 |
g_slice_free (X11IC, x11ic); |
| 361 |
g_return_val_if_reached (0); |
| 362 |
} |
| 363 |
|
| 364 |
g_signal_connect (x11ic->context, "commit-text", |
| 365 |
G_CALLBACK (_context_commit_text_cb), x11ic); |
| 366 |
g_signal_connect (x11ic->context, "forward-key-event", |
| 367 |
G_CALLBACK (_context_forward_key_event_cb), x11ic); |
| 368 |
|
| 369 |
g_signal_connect (x11ic->context, "update-preedit-text", |
| 370 |
G_CALLBACK (_context_update_preedit_text_cb), x11ic); |
| 371 |
g_signal_connect (x11ic->context, "show-preedit-text", |
| 372 |
G_CALLBACK (_context_show_preedit_text_cb), x11ic); |
| 373 |
g_signal_connect (x11ic->context, "hide-preedit-text", |
| 374 |
G_CALLBACK (_context_hide_preedit_text_cb), x11ic); |
| 375 |
g_signal_connect (x11ic->context, "enabled", |
| 376 |
G_CALLBACK (_context_enabled_cb), x11ic); |
| 377 |
g_signal_connect (x11ic->context, "disabled", |
| 378 |
G_CALLBACK (_context_disabled_cb), x11ic); |
| 379 |
|
| 380 |
|
| 381 |
if (x11ic->input_style & XIMPreeditCallbacks) |
| 382 |
capabilities |= IBUS_CAP_PREEDIT_TEXT; |
| 383 |
ibus_input_context_set_capabilities (x11ic->context, capabilities); |
| 384 |
if (_use_sync_mode == 1) |
| 385 |
ibus_input_context_set_post_process_key_event (x11ic->context, TRUE); |
| 386 |
|
| 387 |
g_hash_table_insert (_x11_ic_table, |
| 388 |
GINT_TO_POINTER (x11ic->icid), (gpointer)x11ic); |
| 389 |
x11ic->conn->clients = g_list_append (x11ic->conn->clients, |
| 390 |
(gpointer)x11ic); |
| 391 |
return 1; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
static int |
| 396 |
xim_destroy_ic (XIMS xims, IMChangeICStruct *call_data) |
| 397 |
{ |
| 398 |
X11IC *x11ic; |
| 399 |
|
| 400 |
LOG (1, "XIM_DESTROY_IC ic=%d connect_id=%d", |
| 401 |
call_data->icid, call_data->connect_id); |
| 402 |
|
| 403 |
x11ic = (X11IC *)g_hash_table_lookup (_x11_ic_table, |
| 404 |
GINT_TO_POINTER ((gint) call_data->icid)); |
| 405 |
g_return_val_if_fail (x11ic != NULL, 0); |
| 406 |
|
| 407 |
if (x11ic->context) { |
| 408 |
ibus_proxy_destroy ((IBusProxy *)x11ic->context); |
| 409 |
g_object_unref (x11ic->context); |
| 410 |
x11ic->context = NULL; |
| 411 |
} |
| 412 |
|
| 413 |
g_hash_table_remove (_x11_ic_table, |
| 414 |
GINT_TO_POINTER ((gint) call_data->icid)); |
| 415 |
x11ic->conn->clients = g_list_remove (x11ic->conn->clients, (gconstpointer)x11ic); |
| 416 |
|
| 417 |
g_free (x11ic->preedit_string); |
| 418 |
x11ic->preedit_string = NULL; |
| 419 |
|
| 420 |
if (x11ic->preedit_attrs) { |
| 421 |
g_object_unref (x11ic->preedit_attrs); |
| 422 |
x11ic->preedit_attrs = NULL; |
| 423 |
} |
| 424 |
|
| 425 |
g_slice_free (X11IC, x11ic); |
| 426 |
|
| 427 |
return 1; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
static int |
| 432 |
xim_set_ic_focus (XIMS xims, IMChangeFocusStruct *call_data) |
| 433 |
{ |
| 434 |
X11IC *x11ic; |
| 435 |
|
| 436 |
LOG (1, "XIM_SET_IC_FOCUS ic=%d connect_id=%d", |
| 437 |
call_data->icid, call_data->connect_id); |
| 438 |
|
| 439 |
x11ic = (X11IC *) g_hash_table_lookup (_x11_ic_table, |
| 440 |
GINT_TO_POINTER ((gint) call_data->icid)); |
| 441 |
g_return_val_if_fail (x11ic != NULL, 0); |
| 442 |
|
| 443 |
ibus_input_context_focus_in (x11ic->context); |
| 444 |
_xim_set_cursor_location (x11ic); |
| 445 |
|
| 446 |
return 1; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
static int |
| 451 |
xim_unset_ic_focus (XIMS xims, IMChangeFocusStruct *call_data) |
| 452 |
{ |
| 453 |
X11IC *x11ic; |
| 454 |
|
| 455 |
LOG (1, "XIM_UNSET_IC_FOCUS ic=%d connect_id=%d", |
| 456 |
call_data->icid, call_data->connect_id); |
| 457 |
|
| 458 |
x11ic = (X11IC *) g_hash_table_lookup (_x11_ic_table, |
| 459 |
GINT_TO_POINTER ((gint) call_data->icid)); |
| 460 |
g_return_val_if_fail (x11ic != NULL, 0); |
| 461 |
|
| 462 |
ibus_input_context_focus_out (x11ic->context); |
| 463 |
|
| 464 |
return 1; |
| 465 |
|
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
static void |
| 470 |
_xim_forward_key_event_done (X11IC *x11ic, |
| 471 |
XEvent *event, |
| 472 |
gboolean processed) |
| 473 |
{ |
| 474 |
IMForwardEventStruct fe; |
| 475 |
if (processed) { |
| 476 |
if (!x11ic->has_preedit_area) { |
| 477 |
_xim_set_cursor_location (x11ic); |
| 478 |
} |
| 479 |
return; |
| 480 |
} |
| 481 |
g_assert (x11ic); |
| 482 |
g_assert (event); |
| 483 |
|
| 484 |
memset (&fe, 0, sizeof (fe)); |
| 485 |
fe.major_code = XIM_FORWARD_EVENT; |
| 486 |
fe.icid = x11ic->icid; |
| 487 |
fe.connect_id = x11ic->connect_id; |
| 488 |
fe.sync_bit = 0; |
| 489 |
fe.serial_number = 0L; |
| 490 |
fe.event = *event; |
| 491 |
IMForwardEvent (_xims, (XPointer) &fe); |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
typedef struct { |
| 496 |
int count; |
| 497 |
guint count_cb_id; |
| 498 |
gboolean retval; |
| 499 |
X11IC *x11ic; |
| 500 |
CARD16 connect_id; |
| 501 |
XEvent event; |
| 502 |
} ProcessKeyEventReplyData; |
| 503 |
|
| 504 |
|
| 505 |
static void |
| 506 |
_process_key_event_done (GObject *object, |
| 507 |
GAsyncResult *res, |
| 508 |
gpointer user_data) |
| 509 |
{ |
| 510 |
IBusInputContext *context = (IBusInputContext *)object; |
| 511 |
ProcessKeyEventReplyData *data = (ProcessKeyEventReplyData *)user_data; |
| 512 |
|
| 513 |
GError *error = NULL; |
| 514 |
gboolean retval = ibus_input_context_process_key_event_async_finish ( |
| 515 |
context, |
| 516 |
res, |
| 517 |
&error); |
| 518 |
|
| 519 |
if (error != NULL) { |
| 520 |
g_warning ("Process Key Event failed: %s.", error->message); |
| 521 |
g_error_free (error); |
| 522 |
} |
| 523 |
|
| 524 |
if (g_hash_table_lookup (_connections, |
| 525 |
GINT_TO_POINTER ((gint)data->connect_id)) |
| 526 |
== NULL) { |
| 527 |
g_slice_free (ProcessKeyEventReplyData, data); |
| 528 |
return; |
| 529 |
} |
| 530 |
|
| 531 |
if (retval == FALSE) |
| 532 |
_xim_forward_key_event_done (data->x11ic, &data->event, retval); |
| 533 |
g_slice_free (ProcessKeyEventReplyData, data); |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
static void |
| 538 |
_process_key_event_reply_done (GObject *object, |
| 539 |
GAsyncResult *res, |
| 540 |
gpointer user_data) |
| 541 |
{ |
| 542 |
IBusInputContext *context = (IBusInputContext *)object; |
| 543 |
ProcessKeyEventReplyData *data = (ProcessKeyEventReplyData *)user_data; |
| 544 |
GError *error = NULL; |
| 545 |
gboolean retval = ibus_input_context_process_key_event_async_finish ( |
| 546 |
context, |
| 547 |
res, |
| 548 |
&error); |
| 549 |
if (error != NULL) { |
| 550 |
g_warning ("Process Key Event failed: %s.", error->message); |
| 551 |
g_error_free (error); |
| 552 |
} |
| 553 |
g_return_if_fail (data); |
| 554 |
data->retval = retval; |
| 555 |
if (g_hash_table_lookup (_connections, |
| 556 |
GINT_TO_POINTER ((gint)data->connect_id)) |
| 557 |
== NULL) { |
| 558 |
return; |
| 559 |
} |
| 560 |
/* _xim_forward_key_event_done() should be called in |
| 561 |
* _process_key_event_reply_done() because g_main_context_iteration() |
| 562 |
* can call another xim_forward_event() and xim_forward_event() can be |
| 563 |
* nested and the first _process_key_event_reply_done() is returned |
| 564 |
* at last with g_main_context_iteration() so |
| 565 |
* if _xim_forward_key_event_done() is called out of |
| 566 |
* _process_key_event_reply_done(), the key events order |
| 567 |
* can be swapped. |
| 568 |
*/ |
| 569 |
_xim_forward_key_event_done (data->x11ic, &data->event, retval); |
| 570 |
data->count = 0; |
| 571 |
g_source_remove (data->count_cb_id); |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
static gboolean |
| 576 |
_process_key_event_count_cb (gpointer user_data) |
| 577 |
{ |
| 578 |
ProcessKeyEventReplyData *data = (ProcessKeyEventReplyData *)user_data; |
| 579 |
g_return_val_if_fail (data, G_SOURCE_REMOVE); |
| 580 |
if (!data->count) |
| 581 |
return G_SOURCE_REMOVE; |
| 582 |
if (data->count++ == MAX_WAIT_KEY_TIME) { |
| 583 |
g_warning ("Key event is not returned for %usecs.", MAX_WAIT_KEY_TIME); |
| 584 |
return G_SOURCE_REMOVE; |
| 585 |
} |
| 586 |
return G_SOURCE_CONTINUE; |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
static int |
| 591 |
_process_key_event_sync (X11IC *x11ic, |
| 592 |
IMForwardEventStruct *call_data, |
| 593 |
GdkEventKey *event) |
| 594 |
{ |
| 595 |
gboolean retval; |
| 596 |
|
| 597 |
g_assert (x11ic); |
| 598 |
g_assert (call_data); |
| 599 |
g_assert (event); |
| 600 |
retval = ibus_input_context_process_key_event ( |
| 601 |
x11ic->context, |
| 602 |
event->keyval, |
| 603 |
event->hardware_keycode - 8, |
| 604 |
event->state); |
| 605 |
ibus_input_context_post_process_key_event (x11ic->context); |
| 606 |
_xim_forward_key_event_done (x11ic, &call_data->event, retval); |
| 607 |
return 1; |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
static int |
| 612 |
_process_key_event_async (X11IC *x11ic, |
| 613 |
IMForwardEventStruct *call_data, |
| 614 |
GdkEventKey *event) |
| 615 |
{ |
| 616 |
ProcessKeyEventReplyData *data; |
| 617 |
|
| 618 |
g_assert (x11ic); |
| 619 |
g_assert (call_data); |
| 620 |
g_assert (event); |
| 621 |
if (!(data = g_slice_new0 (ProcessKeyEventReplyData))) { |
| 622 |
g_warning ("Cannot allocate async data"); |
| 623 |
return _process_key_event_sync (x11ic, call_data, event); |
| 624 |
} |
| 625 |
data->connect_id = call_data->connect_id; |
| 626 |
data->x11ic = x11ic; |
| 627 |
data->event = call_data->event; |
| 628 |
ibus_input_context_process_key_event_async (x11ic->context, |
| 629 |
event->keyval, |
| 630 |
event->hardware_keycode - 8, |
| 631 |
event->state, |
| 632 |
-1, |
| 633 |
NULL, |
| 634 |
_process_key_event_done, |
| 635 |
data); |
| 636 |
return 1; |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
static int |
| 641 |
_process_key_event_hybrid_async (X11IC *x11ic, |
| 642 |
IMForwardEventStruct *call_data, |
| 643 |
GdkEventKey *event) |
| 644 |
{ |
| 645 |
GSource *source; |
| 646 |
ProcessKeyEventReplyData *data = NULL; |
| 647 |
gboolean bus_retval; |
| 648 |
|
| 649 |
g_assert (x11ic); |
| 650 |
g_assert (call_data); |
| 651 |
g_assert (event); |
| 652 |
source = g_timeout_source_new (1); |
| 653 |
if (source) |
| 654 |
data = g_slice_new0 (ProcessKeyEventReplyData); |
| 655 |
if (!data) { |
| 656 |
int xim_retval; |
| 657 |
g_warning ("Cannot wait for the reply of the process key event."); |
| 658 |
xim_retval = _process_key_event_sync (x11ic, call_data, event); |
| 659 |
if (source) |
| 660 |
g_source_destroy (source); |
| 661 |
return xim_retval; |
| 662 |
} |
| 663 |
data->count = 1; |
| 664 |
g_source_attach (source, NULL); |
| 665 |
g_source_unref (source); |
| 666 |
data->count_cb_id = g_source_get_id (source); |
| 667 |
data->connect_id = call_data->connect_id; |
| 668 |
data->x11ic = x11ic; |
| 669 |
data->event = call_data->event; |
| 670 |
ibus_input_context_process_key_event_async (x11ic->context, |
| 671 |
event->keyval, |
| 672 |
event->hardware_keycode - 8, |
| 673 |
event->state, |
| 674 |
-1, |
| 675 |
NULL, |
| 676 |
_process_key_event_reply_done, |
| 677 |
data); |
| 678 |
g_source_set_callback (source, _process_key_event_count_cb, |
| 679 |
data, NULL); |
| 680 |
while (data->count > 0 && data->count < MAX_WAIT_KEY_TIME) |
| 681 |
g_main_context_iteration (NULL, TRUE); |
| 682 |
/* #2498 Checking source->ref_count might cause Nautilus hang up |
| 683 |
*/ |
| 684 |
bus_retval = data->retval; |
| 685 |
if (data->count == 0) { |
| 686 |
g_slice_free (ProcessKeyEventReplyData, data); |
| 687 |
return 1; |
| 688 |
} |
| 689 |
|
| 690 |
g_slice_free (ProcessKeyEventReplyData, data); |
| 691 |
if (g_hash_table_lookup (_connections, |
| 692 |
GINT_TO_POINTER ((gint)call_data->connect_id)) |
| 693 |
== NULL) { |
| 694 |
return 1; |
| 695 |
} |
| 696 |
_xim_forward_key_event_done (x11ic, &call_data->event, bus_retval); |
| 697 |
return 1; |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
static int |
| 702 |
xim_forward_event (XIMS xims, IMForwardEventStruct *call_data) |
| 703 |
{ |
| 704 |
X11IC *x11ic; |
| 705 |
XKeyEvent *xevent; |
| 706 |
GdkEventKey event; |
| 707 |
|
| 708 |
LOG (1, "XIM_FORWARD_EVENT ic=%d connect_id=%d", |
| 709 |
call_data->icid, call_data->connect_id); |
| 710 |
|
| 711 |
x11ic = (X11IC *) g_hash_table_lookup ( |
| 712 |
_x11_ic_table, |
| 713 |
GINT_TO_POINTER ((gint) call_data->icid)); |
| 714 |
g_return_val_if_fail (x11ic != NULL, 0); |
| 715 |
|
| 716 |
xevent = (XKeyEvent*) &(call_data->event); |
| 717 |
|
| 718 |
translate_key_event (gdk_display_get_default (), |
| 719 |
(GdkEvent *)&event, (XEvent *)xevent); |
| 720 |
|
| 721 |
event.send_event = xevent->send_event; |
| 722 |
event.window = NULL; |
| 723 |
|
| 724 |
if (event.type == GDK_KEY_RELEASE) { |
| 725 |
event.state |= IBUS_RELEASE_MASK; |
| 726 |
} |
| 727 |
|
| 728 |
switch (_use_sync_mode) { |
| 729 |
case 1: |
| 730 |
return _process_key_event_sync (x11ic, call_data, &event); |
| 731 |
case 2: |
| 732 |
return _process_key_event_hybrid_async (x11ic, call_data, &event); |
| 733 |
default: |
| 734 |
return _process_key_event_async (x11ic, call_data, &event); |
| 735 |
} |
| 736 |
g_assert_not_reached (); |
| 737 |
return 0; |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
static int |
| 742 |
xim_open (XIMS xims, IMOpenStruct *call_data) |
| 743 |
{ |
| 744 |
X11ICONN *conn; |
| 745 |
|
| 746 |
LOG (1, "XIM_OPEN connect_id=%d", |
| 747 |
call_data->connect_id); |
| 748 |
|
| 749 |
conn = (X11ICONN *) g_hash_table_lookup (_connections, |
| 750 |
GINT_TO_POINTER ((gint) call_data->connect_id)); |
| 751 |
g_return_val_if_fail (conn == NULL, 0); |
| 752 |
|
| 753 |
conn = g_slice_new0 (X11ICONN); |
| 754 |
|
| 755 |
g_hash_table_insert (_connections, |
| 756 |
(gpointer)(unsigned long)call_data->connect_id, |
| 757 |
(gpointer) conn); |
| 758 |
|
| 759 |
return 1; |
| 760 |
} |
| 761 |
|
| 762 |
static void |
| 763 |
_free_ic (gpointer data, gpointer user_data) |
| 764 |
{ |
| 765 |
X11IC *x11ic = (X11IC *) data; |
| 766 |
|
| 767 |
g_return_if_fail (x11ic != NULL); |
| 768 |
|
| 769 |
g_free (x11ic->preedit_string); |
| 770 |
x11ic->preedit_string = NULL; |
| 771 |
|
| 772 |
if (x11ic->preedit_attrs) { |
| 773 |
g_object_unref (x11ic->preedit_attrs); |
| 774 |
x11ic->preedit_attrs = NULL; |
| 775 |
} |
| 776 |
|
| 777 |
if (x11ic->context) { |
| 778 |
ibus_proxy_destroy ((IBusProxy *)x11ic->context); |
| 779 |
g_object_unref (x11ic->context); |
| 780 |
x11ic->context = NULL; |
| 781 |
} |
| 782 |
|
| 783 |
/* Remove the IC from g_client dictionary */ |
| 784 |
g_hash_table_remove (_x11_ic_table, |
| 785 |
GINT_TO_POINTER (x11ic->icid)); |
| 786 |
|
| 787 |
g_slice_free (X11IC, x11ic); |
| 788 |
} |
| 789 |
|
| 790 |
static int |
| 791 |
_free_x11_iconn_from_id (CARD16 connect_id) |
| 792 |
{ |
| 793 |
X11ICONN *conn; |
| 794 |
|
| 795 |
conn = (X11ICONN *) g_hash_table_lookup (_connections, |
| 796 |
GINT_TO_POINTER ((gint) connect_id)); |
| 797 |
|
| 798 |
if (conn == NULL) { |
| 799 |
return 0; |
| 800 |
} |
| 801 |
|
| 802 |
g_list_free_full (conn->clients, (GDestroyNotify) _free_ic); |
| 803 |
|
| 804 |
g_hash_table_remove (_connections, |
| 805 |
GINT_TO_POINTER ((gint) connect_id)); |
| 806 |
|
| 807 |
g_slice_free (X11ICONN, conn); |
| 808 |
|
| 809 |
return 1; |
| 810 |
} |
| 811 |
|
| 812 |
static int |
| 813 |
xim_close (XIMS xims, IMCloseStruct *call_data) |
| 814 |
{ |
| 815 |
CARD16 connect_id = call_data->connect_id; |
| 816 |
|
| 817 |
LOG (1, "XIM_CLOSE connect_id=%d", connect_id); |
| 818 |
|
| 819 |
return _free_x11_iconn_from_id (connect_id); |
| 820 |
} |
| 821 |
|
| 822 |
static int |
| 823 |
xim_disconnect_ic (XIMS xims, IMDisConnectStruct *call_data) |
| 824 |
{ |
| 825 |
CARD16 connect_id = call_data->connect_id; |
| 826 |
|
| 827 |
LOG (1, "XIM_DISCONNECT connect_id=%d", connect_id); |
| 828 |
|
| 829 |
_free_x11_iconn_from_id (connect_id); |
| 830 |
|
| 831 |
/* I am not sure if this can return 1 because I have not experienced |
| 832 |
* that xim_disconnect_ic() is called. But I wish connect_id is |
| 833 |
* released from _connections to avoid SEGV. */ |
| 834 |
return 0; |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
static void |
| 839 |
_xim_set_cursor_location (X11IC *x11ic) |
| 840 |
{ |
| 841 |
g_return_if_fail (x11ic != NULL); |
| 842 |
|
| 843 |
GdkRectangle preedit_area = x11ic->preedit_area; |
| 844 |
|
| 845 |
Window w = x11ic->focus_window ? |
| 846 |
x11ic->focus_window :x11ic->client_window; |
| 847 |
|
| 848 |
if (w) { |
| 849 |
XWindowAttributes xwa; |
| 850 |
Window child; |
| 851 |
|
| 852 |
XGetWindowAttributes (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), w, &xwa); |
| 853 |
if (preedit_area.x <= 0 && preedit_area.y <= 0) { |
| 854 |
XTranslateCoordinates (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), w, |
| 855 |
xwa.root, |
| 856 |
0, |
| 857 |
xwa.height, |
| 858 |
&preedit_area.x, |
| 859 |
&preedit_area.y, |
| 860 |
&child); |
| 861 |
} |
| 862 |
else { |
| 863 |
XTranslateCoordinates (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), w, |
| 864 |
xwa.root, |
| 865 |
preedit_area.x, |
| 866 |
preedit_area.y, |
| 867 |
&preedit_area.x, |
| 868 |
&preedit_area.y, |
| 869 |
&child); |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
ibus_input_context_set_cursor_location (x11ic->context, |
| 874 |
preedit_area.x, |
| 875 |
preedit_area.y, |
| 876 |
preedit_area.width, |
| 877 |
preedit_area.height); |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
static int |
| 882 |
xim_set_ic_values (XIMS xims, IMChangeICStruct *call_data) |
| 883 |
{ |
| 884 |
X11IC *x11ic; |
| 885 |
gint i; |
| 886 |
|
| 887 |
LOG (1, "XIM_SET_IC_VALUES ic=%d connect_id=%d", |
| 888 |
call_data->icid, call_data->connect_id); |
| 889 |
|
| 890 |
x11ic = (X11IC *) g_hash_table_lookup (_x11_ic_table, |
| 891 |
GINT_TO_POINTER ((gint) call_data->icid)); |
| 892 |
g_return_val_if_fail (x11ic != NULL, 0); |
| 893 |
|
| 894 |
i = _xim_store_ic_values (x11ic, call_data); |
| 895 |
|
| 896 |
if (i) { |
| 897 |
_xim_set_cursor_location (x11ic); |
| 898 |
} |
| 899 |
|
| 900 |
return 1; |
| 901 |
} |
| 902 |
|
| 903 |
static int |
| 904 |
xim_get_ic_values (XIMS xims, IMChangeICStruct *call_data) |
| 905 |
{ |
| 906 |
X11IC *x11ic; |
| 907 |
gint i; |
| 908 |
|
| 909 |
LOG (1, "XIM_GET_IC_VALUES ic=%d connect_id=%d", |
| 910 |
call_data->icid, call_data->connect_id); |
| 911 |
|
| 912 |
x11ic = (X11IC *) g_hash_table_lookup (_x11_ic_table, |
| 913 |
GINT_TO_POINTER ((gint) call_data->icid)); |
| 914 |
g_return_val_if_fail (x11ic != NULL, 0); |
| 915 |
|
| 916 |
XICAttribute *ic_attr = call_data->ic_attr; |
| 917 |
|
| 918 |
for (i = 0; i < (int) call_data->ic_attr_num; ++i, ++ic_attr) { |
| 919 |
if (g_strcmp0 (XNFilterEvents, ic_attr->name) == 0) { |
| 920 |
/* ic_attr->value will be freed in server side and ignore |
| 921 |
* leak of malloc with -Wanalyzer-malloc-leak flags in gcc 11.0.1 |
| 922 |
*/ |
| 923 |
#pragma GCC diagnostic push |
| 924 |
#pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak" |
| 925 |
ic_attr->value = (void *) malloc (sizeof (CARD32)); |
| 926 |
if (ic_attr->value) { |
| 927 |
*(CARD32 *) ic_attr->value = KeyPressMask | KeyReleaseMask; |
| 928 |
ic_attr->value_length = sizeof (CARD32); |
| 929 |
} else { |
| 930 |
g_warning ("Failed to malloc"); |
| 931 |
ic_attr->value_length = 0; |
| 932 |
} |
| 933 |
#pragma GCC diagnostic pop |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
return 1; |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
static int |
| 943 |
xim_reset_ic (XIMS xims, IMResetICStruct *call_data) |
| 944 |
{ |
| 945 |
X11IC *x11ic; |
| 946 |
|
| 947 |
LOG (1, "XIM_RESET_IC ic=%d connect_id=%d", |
| 948 |
call_data->icid, call_data->connect_id); |
| 949 |
|
| 950 |
x11ic = (X11IC *) g_hash_table_lookup (_x11_ic_table, |
| 951 |
GINT_TO_POINTER ((gint) call_data->icid)); |
| 952 |
g_return_val_if_fail (x11ic != NULL, 0); |
| 953 |
|
| 954 |
ibus_input_context_reset (x11ic->context); |
| 955 |
|
| 956 |
return 1; |
| 957 |
} |
| 958 |
|
| 959 |
int |
| 960 |
ims_protocol_handler (XIMS xims, IMProtocol *call_data) |
| 961 |
{ |
| 962 |
g_return_val_if_fail (xims != NULL, 1); |
| 963 |
g_return_val_if_fail (call_data != NULL, 1); |
| 964 |
|
| 965 |
switch (call_data->major_code) { |
| 966 |
case XIM_OPEN: |
| 967 |
return xim_open (xims, (IMOpenStruct *)call_data); |
| 968 |
case XIM_CLOSE: |
| 969 |
return xim_close (xims, (IMCloseStruct *)call_data); |
| 970 |
case XIM_DISCONNECT: |
| 971 |
return xim_disconnect_ic (xims, (IMDisConnectStruct *)call_data); |
| 972 |
case XIM_CREATE_IC: |
| 973 |
return xim_create_ic (xims, (IMChangeICStruct *)call_data); |
| 974 |
case XIM_DESTROY_IC: |
| 975 |
return xim_destroy_ic (xims, (IMChangeICStruct *)call_data); |
| 976 |
case XIM_SET_IC_VALUES: |
| 977 |
return xim_set_ic_values (xims, (IMChangeICStruct *)call_data); |
| 978 |
case XIM_GET_IC_VALUES: |
| 979 |
return xim_get_ic_values (xims, (IMChangeICStruct *)call_data); |
| 980 |
case XIM_FORWARD_EVENT: |
| 981 |
return xim_forward_event (xims, (IMForwardEventStruct *)call_data); |
| 982 |
case XIM_SET_IC_FOCUS: |
| 983 |
return xim_set_ic_focus (xims, (IMChangeFocusStruct *)call_data); |
| 984 |
case XIM_UNSET_IC_FOCUS: |
| 985 |
return xim_unset_ic_focus (xims, (IMChangeFocusStruct *)call_data); |
| 986 |
case XIM_RESET_IC: |
| 987 |
return xim_reset_ic (xims, (IMResetICStruct *)call_data); |
| 988 |
case XIM_TRIGGER_NOTIFY: |
| 989 |
LOG (1, "XIM_TRIGGER_NOTIFY"); |
| 990 |
return 0; |
| 991 |
case XIM_PREEDIT_START_REPLY: |
| 992 |
LOG (1, "XIM_PREEDIT_START_REPLY"); |
| 993 |
return 0; |
| 994 |
case XIM_PREEDIT_CARET_REPLY: |
| 995 |
LOG (1, "XIM_PREEDIT_CARET_REPLY"); |
| 996 |
return 0; |
| 997 |
case XIM_SYNC_REPLY: |
| 998 |
LOG (1, "XIM_SYNC_REPLY"); |
| 999 |
return 0; |
| 1000 |
default: |
| 1001 |
LOG (1, "Unknown (%d)", call_data->major_code); |
| 1002 |
return 0; |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
static void |
| 1007 |
_xim_forward_key_event (X11IC *x11ic, |
| 1008 |
guint keyval, |
| 1009 |
guint keycode, |
| 1010 |
guint state) |
| 1011 |
{ |
| 1012 |
XEvent xkp = {0}; |
| 1013 |
|
| 1014 |
g_return_if_fail (x11ic != NULL); |
| 1015 |
|
| 1016 |
xkp.xkey.type = (state & IBUS_RELEASE_MASK) ? KeyRelease : KeyPress; |
| 1017 |
xkp.xkey.serial = 0L; |
| 1018 |
xkp.xkey.send_event = False; |
| 1019 |
xkp.xkey.same_screen = True; |
| 1020 |
xkp.xkey.display = GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()); |
| 1021 |
xkp.xkey.window = |
| 1022 |
x11ic->focus_window ? x11ic->focus_window : x11ic->client_window; |
| 1023 |
xkp.xkey.subwindow = None; |
| 1024 |
xkp.xkey.root = DefaultRootWindow ( |
| 1025 |
GDK_DISPLAY_XDISPLAY (gdk_display_get_default ())); |
| 1026 |
|
| 1027 |
xkp.xkey.time = 0; |
| 1028 |
xkp.xkey.state = state; |
| 1029 |
xkp.xkey.keycode = (keycode == 0) ? 0 : keycode + 8; |
| 1030 |
|
| 1031 |
_xim_forward_key_event_done (x11ic, &xkp, FALSE); |
| 1032 |
} |
| 1033 |
|
| 1034 |
static void |
| 1035 |
_bus_disconnected_cb (IBusBus *bus, |
| 1036 |
gpointer user_data) |
| 1037 |
{ |
| 1038 |
g_debug ("Connection closed by ibus-daemon\n"); |
| 1039 |
g_clear_object (&_bus); |
| 1040 |
ibus_quit (); |
| 1041 |
} |
| 1042 |
|
| 1043 |
static void |
| 1044 |
_context_commit_text_cb (IBusInputContext *context, |
| 1045 |
IBusText *text, |
| 1046 |
X11IC *x11ic) |
| 1047 |
{ |
| 1048 |
g_assert (IBUS_IS_INPUT_CONTEXT (context)); |
| 1049 |
g_assert (IBUS_IS_TEXT (text)); |
| 1050 |
g_assert (x11ic != NULL); |
| 1051 |
|
| 1052 |
XTextProperty tp; |
| 1053 |
IMCommitStruct cms = {0}; |
| 1054 |
int ret; |
| 1055 |
|
| 1056 |
ret = Xutf8TextListToTextProperty ( |
| 1057 |
GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), |
| 1058 |
(gchar **)&(text->text), 1, XCompoundTextStyle, &tp); |
| 1059 |
/* XCompoundTextStyle uses the encoding escaped sequence + encoded chars |
| 1060 |
* matched to the specified multibyte characters: text->text, and |
| 1061 |
* libX11.so sorts the encoding sets by locale. |
| 1062 |
* If an encoded string fails to be matched, ibus-x11 specifies the |
| 1063 |
* ISO10641-1 encoding and that escaped sequence is "\033%G": |
| 1064 |
* https://gitlab.freedesktop.org/xorg/lib/libx11/-/blob/master/src/xlibi18n/lcCT.c |
| 1065 |
* , and the encoding is UTF-8 with utf8_wctomb(): |
| 1066 |
* https://gitlab.freedesktop.org/xorg/lib/libx11/-/blob/master/src/xlibi18n/lcUniConv/utf8.h |
| 1067 |
*/ |
| 1068 |
if (ret == EXIT_FAILURE) { |
| 1069 |
XFree (tp.value); |
| 1070 |
tp.value = (unsigned char *)g_strdup_printf ("%s%s", |
| 1071 |
ESC_SEQUENCE_ISO10646_1, |
| 1072 |
text->text); |
| 1073 |
} |
| 1074 |
|
| 1075 |
cms.major_code = XIM_COMMIT; |
| 1076 |
cms.icid = x11ic->icid; |
| 1077 |
cms.connect_id = x11ic->connect_id; |
| 1078 |
cms.flag = XimLookupChars; |
| 1079 |
cms.commit_string = (gchar *)tp.value; |
| 1080 |
IMCommitString (_xims, (XPointer) & cms); |
| 1081 |
|
| 1082 |
XFree (tp.value); |
| 1083 |
} |
| 1084 |
|
| 1085 |
static void |
| 1086 |
_context_forward_key_event_cb (IBusInputContext *context, |
| 1087 |
guint keyval, |
| 1088 |
guint keycode, |
| 1089 |
guint state, |
| 1090 |
X11IC *x11ic) |
| 1091 |
{ |
| 1092 |
g_assert (x11ic); |
| 1093 |
|
| 1094 |
_xim_forward_key_event (x11ic, keyval, keycode, state); |
| 1095 |
} |
| 1096 |
|
| 1097 |
static void |
| 1098 |
_update_preedit (X11IC *x11ic) |
| 1099 |
{ |
| 1100 |
if (x11ic->preedit_visible == FALSE && x11ic->preedit_started == TRUE) { |
| 1101 |
_xim_preedit_callback_draw (_xims, x11ic, "", NULL); |
| 1102 |
_xim_preedit_callback_done (_xims, x11ic); |
| 1103 |
x11ic->preedit_started = FALSE; |
| 1104 |
} |
| 1105 |
|
| 1106 |
if (x11ic->preedit_visible == TRUE && x11ic->preedit_started == FALSE) { |
| 1107 |
_xim_preedit_callback_start (_xims, x11ic); |
| 1108 |
x11ic->preedit_started = TRUE; |
| 1109 |
} |
| 1110 |
if (x11ic->preedit_visible == TRUE) { |
| 1111 |
_xim_preedit_callback_draw (_xims, x11ic, |
| 1112 |
x11ic->preedit_string, x11ic->preedit_attrs); |
| 1113 |
} |
| 1114 |
} |
| 1115 |
|
| 1116 |
static void |
| 1117 |
_context_update_preedit_text_cb (IBusInputContext *context, |
| 1118 |
IBusText *text, |
| 1119 |
gint cursor_pos, |
| 1120 |
gboolean visible, |
| 1121 |
X11IC *x11ic) |
| 1122 |
{ |
| 1123 |
g_assert (IBUS_IS_INPUT_CONTEXT (context)); |
| 1124 |
g_assert (IBUS_IS_TEXT (text)); |
| 1125 |
g_assert (x11ic); |
| 1126 |
|
| 1127 |
if (x11ic->preedit_string) { |
| 1128 |
g_free(x11ic->preedit_string); |
| 1129 |
} |
| 1130 |
x11ic->preedit_string = g_strdup(text->text); |
| 1131 |
|
| 1132 |
if (x11ic->preedit_attrs) { |
| 1133 |
g_object_unref (x11ic->preedit_attrs); |
| 1134 |
} |
| 1135 |
|
| 1136 |
g_object_ref(text->attrs); |
| 1137 |
x11ic->preedit_attrs = text->attrs; |
| 1138 |
|
| 1139 |
x11ic->preedit_cursor = cursor_pos; |
| 1140 |
x11ic->preedit_visible = visible; |
| 1141 |
|
| 1142 |
_update_preedit (x11ic); |
| 1143 |
} |
| 1144 |
|
| 1145 |
static void |
| 1146 |
_context_show_preedit_text_cb (IBusInputContext *context, |
| 1147 |
X11IC *x11ic) |
| 1148 |
{ |
| 1149 |
g_assert (IBUS_IS_INPUT_CONTEXT (context)); |
| 1150 |
g_assert (x11ic); |
| 1151 |
|
| 1152 |
x11ic->preedit_visible = TRUE; |
| 1153 |
_update_preedit (x11ic); |
| 1154 |
} |
| 1155 |
|
| 1156 |
static void |
| 1157 |
_context_hide_preedit_text_cb (IBusInputContext *context, |
| 1158 |
X11IC *x11ic) |
| 1159 |
{ |
| 1160 |
g_assert (IBUS_IS_INPUT_CONTEXT (context)); |
| 1161 |
g_assert (x11ic); |
| 1162 |
|
| 1163 |
x11ic->preedit_visible = FALSE; |
| 1164 |
_update_preedit (x11ic); |
| 1165 |
} |
| 1166 |
|
| 1167 |
static void |
| 1168 |
_context_enabled_cb (IBusInputContext *context, |
| 1169 |
X11IC *x11ic) |
| 1170 |
{ |
| 1171 |
g_assert (IBUS_IS_INPUT_CONTEXT (context)); |
| 1172 |
g_assert (x11ic); |
| 1173 |
|
| 1174 |
_xim_preedit_start (_xims, x11ic); |
| 1175 |
} |
| 1176 |
|
| 1177 |
static void |
| 1178 |
_context_disabled_cb (IBusInputContext *context, |
| 1179 |
X11IC *x11ic) |
| 1180 |
{ |
| 1181 |
g_assert (IBUS_IS_INPUT_CONTEXT (context)); |
| 1182 |
g_assert (x11ic); |
| 1183 |
|
| 1184 |
_xim_preedit_end (_xims, x11ic); |
| 1185 |
} |
| 1186 |
|
| 1187 |
static char |
| 1188 |
_get_char_env (const gchar *name, |
| 1189 |
char defval) |
| 1190 |
{ |
| 1191 |
const gchar *value = g_getenv (name); |
| 1192 |
|
| 1193 |
if (value == NULL) |
| 1194 |
return defval; |
| 1195 |
|
| 1196 |
if (g_strcmp0 (value, "") == 0 || |
| 1197 |
g_strcmp0 (value, "0") == 0 || |
| 1198 |
g_strcmp0 (value, "false") == 0 || |
| 1199 |
g_strcmp0 (value, "False") == 0 || |
| 1200 |
g_strcmp0 (value, "FALSE") == 0) { |
| 1201 |
return 0; |
| 1202 |
} else if (!g_strcmp0 (value, "2")) { |
| 1203 |
return 2; |
| 1204 |
} |
| 1205 |
|
| 1206 |
return 1; |
| 1207 |
} |
| 1208 |
|
| 1209 |
static void |
| 1210 |
_init_ibus (void) |
| 1211 |
{ |
| 1212 |
if (_bus != NULL) |
| 1213 |
return; |
| 1214 |
|
| 1215 |
ibus_init (); |
| 1216 |
|
| 1217 |
_bus = ibus_bus_new (); |
| 1218 |
|
| 1219 |
g_signal_connect (_bus, "disconnected", |
| 1220 |
G_CALLBACK (_bus_disconnected_cb), NULL); |
| 1221 |
|
| 1222 |
/* https://github.com/ibus/ibus/issues/1713 */ |
| 1223 |
_use_sync_mode = _get_char_env ("IBUS_ENABLE_SYNC_MODE", 1); |
| 1224 |
} |
| 1225 |
|
| 1226 |
static void |
| 1227 |
_xim_init_IMdkit () |
| 1228 |
{ |
| 1229 |
#if 0 |
| 1230 |
XIMStyle ims_styles_overspot [] = { |
| 1231 |
XIMPreeditPosition | XIMStatusNothing, |
| 1232 |
XIMPreeditNothing | XIMStatusNothing, |
| 1233 |
XIMPreeditPosition | XIMStatusCallbacks, |
| 1234 |
XIMPreeditNothing | XIMStatusCallbacks, |
| 1235 |
0 |
| 1236 |
}; |
| 1237 |
#endif |
| 1238 |
|
| 1239 |
XIMStyle ims_styles_onspot [] = { |
| 1240 |
XIMPreeditPosition | XIMStatusNothing, |
| 1241 |
XIMPreeditCallbacks | XIMStatusNothing, |
| 1242 |
XIMPreeditNothing | XIMStatusNothing, |
| 1243 |
XIMPreeditPosition | XIMStatusCallbacks, |
| 1244 |
XIMPreeditCallbacks | XIMStatusCallbacks, |
| 1245 |
XIMPreeditNothing | XIMStatusCallbacks, |
| 1246 |
0 |
| 1247 |
}; |
| 1248 |
|
| 1249 |
XIMEncoding ims_encodings[] = { |
| 1250 |
"COMPOUND_TEXT", |
| 1251 |
0 |
| 1252 |
}; |
| 1253 |
|
| 1254 |
GdkWindowAttr window_attr = { |
| 1255 |
.title = "ibus-xim", |
| 1256 |
.event_mask = GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK, |
| 1257 |
.wclass = GDK_INPUT_OUTPUT, |
| 1258 |
.window_type = GDK_WINDOW_TOPLEVEL, |
| 1259 |
.override_redirect = 1, |
| 1260 |
}; |
| 1261 |
|
| 1262 |
XIMStyles styles; |
| 1263 |
XIMEncodings encodings; |
| 1264 |
|
| 1265 |
GdkWindow *win; |
| 1266 |
|
| 1267 |
win = gdk_window_new (NULL, &window_attr, GDK_WA_TITLE); |
| 1268 |
|
| 1269 |
styles.count_styles = |
| 1270 |
sizeof (ims_styles_onspot)/sizeof (XIMStyle) - 1; |
| 1271 |
styles.supported_styles = ims_styles_onspot; |
| 1272 |
|
| 1273 |
encodings.count_encodings = |
| 1274 |
sizeof (ims_encodings)/sizeof (XIMEncoding) - 1; |
| 1275 |
encodings.supported_encodings = ims_encodings; |
| 1276 |
|
| 1277 |
_xims = IMOpenIM (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), |
| 1278 |
IMModifiers, "Xi18n", |
| 1279 |
#if GTK_CHECK_VERSION (3, 0, 0) |
| 1280 |
IMServerWindow, GDK_WINDOW_XID (win), |
| 1281 |
#else |
| 1282 |
IMServerWindow, GDK_WINDOW_XWINDOW (win), |
| 1283 |
#endif |
| 1284 |
IMServerName, _server_name != NULL ? _server_name : "ibus", |
| 1285 |
IMLocale, _locale != NULL ? _locale : LOCALES_STRING, |
| 1286 |
IMServerTransport, "X/", |
| 1287 |
IMInputStyles, &styles, |
| 1288 |
IMEncodingList, &encodings, |
| 1289 |
IMProtocolHandler, ims_protocol_handler, |
| 1290 |
IMFilterEventMask, KeyPressMask | KeyReleaseMask, |
| 1291 |
NULL); |
| 1292 |
|
| 1293 |
_init_ibus (); |
| 1294 |
|
| 1295 |
if (!ibus_bus_is_connected (_bus)) { |
| 1296 |
g_warning ("Can not connect to ibus daemon"); |
| 1297 |
exit (EXIT_FAILURE); |
| 1298 |
} |
| 1299 |
} |
| 1300 |
|
| 1301 |
static void |
| 1302 |
_atexit_cb () |
| 1303 |
{ |
| 1304 |
if (_bus && ibus_bus_is_connected (_bus)) { |
| 1305 |
ibus_bus_exit(_bus, False); |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
static void |
| 1310 |
_sighandler (int sig) |
| 1311 |
{ |
| 1312 |
/* rhbz#1767691 _sighandler() is called with SIGTERM |
| 1313 |
* and exit() causes SEGV during calling atexit functions. |
| 1314 |
* _atexit_cb() might be broken. _exit() does not call |
| 1315 |
* atexit functions. |
| 1316 |
*/ |
| 1317 |
_exit (EXIT_FAILURE); |
| 1318 |
} |
| 1319 |
|
| 1320 |
static void |
| 1321 |
_print_usage (FILE *fp, gchar *name) |
| 1322 |
{ |
| 1323 |
fprintf (fp, |
| 1324 |
"Usage:\n" |
| 1325 |
" %s --help Show this message\n" |
| 1326 |
" --server-name= -n Setup xim server name\n" |
| 1327 |
" --locale= -l Setup support locales\n" |
| 1328 |
" --locale-append= -a Append locales into the default support locales\n" |
| 1329 |
" --kill-daemon -k Kill ibus daemon when exit\n" |
| 1330 |
" --debug= -v Setup debug level\n", |
| 1331 |
name); |
| 1332 |
} |
| 1333 |
|
| 1334 |
static int |
| 1335 |
_xerror_handler (Display *dpy, XErrorEvent *e) |
| 1336 |
{ |
| 1337 |
g_debug ( |
| 1338 |
"XError: " |
| 1339 |
"serial=%lu error_code=%d request_code=%d minor_code=%d resourceid=%lu", |
| 1340 |
e->serial, e->error_code, e->request_code, e->minor_code, e->resourceid); |
| 1341 |
return 1; |
| 1342 |
} |
| 1343 |
|
| 1344 |
/* When log into GNOME3 desktop immediately after the system is booted, |
| 1345 |
* ibus-daemon is sometimes alive but ibus-x11 is dead after log out |
| 1346 |
* the session. Because gdk_x_io_error() is called as the callback of |
| 1347 |
* XSetIOErrorHandler() in gtk/gdk/x11/gdkmain-x11.c in ibus-x11. |
| 1348 |
* Now I assume the callback is called in logout. |
| 1349 |
*/ |
| 1350 |
static int |
| 1351 |
_xerror_io_handler (Display *dpy) |
| 1352 |
{ |
| 1353 |
if (_kill_daemon) |
| 1354 |
_atexit_cb (); |
| 1355 |
return 0; |
| 1356 |
} |
| 1357 |
|
| 1358 |
int |
| 1359 |
main (int argc, char **argv) |
| 1360 |
{ |
| 1361 |
gint option_index = 0; |
| 1362 |
gint c; |
| 1363 |
|
| 1364 |
/* GDK_DISPLAY_XDISPLAY() and GDK_WINDOW_XID() does not work |
| 1365 |
* with GdkWaylandDisplay. |
| 1366 |
*/ |
| 1367 |
#if GTK_CHECK_VERSION (3, 10, 0) |
| 1368 |
gdk_set_allowed_backends ("x11"); |
| 1369 |
#endif |
| 1370 |
|
| 1371 |
gdk_init (&argc, &argv); |
| 1372 |
XSetErrorHandler (_xerror_handler); |
| 1373 |
XSetIOErrorHandler (_xerror_io_handler); |
| 1374 |
|
| 1375 |
#ifdef HAVE_XFIXES |
| 1376 |
XFixesSetClientDisconnectMode(GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), |
| 1377 |
XFixesClientDisconnectFlagTerminate); |
| 1378 |
#endif |
| 1379 |
|
| 1380 |
while (1) { |
| 1381 |
static struct option long_options [] = { |
| 1382 |
{ "debug", 1, 0, 0}, |
| 1383 |
{ "server-name", 1, 0, 0}, |
| 1384 |
{ "locale", 1, 0, 0}, |
| 1385 |
{ "locale-append", 1, 0, 0}, |
| 1386 |
{ "help", 0, 0, 0}, |
| 1387 |
{ "kill-daemon", 0, 0, 0}, |
| 1388 |
{ 0, 0, 0, 0}, |
| 1389 |
}; |
| 1390 |
|
| 1391 |
c = getopt_long (argc, argv, "v:n:l:k:a", |
| 1392 |
long_options, &option_index); |
| 1393 |
|
| 1394 |
if (c == -1) break; |
| 1395 |
|
| 1396 |
switch (c) { |
| 1397 |
case 0: |
| 1398 |
if (g_strcmp0 (long_options[option_index].name, "debug") == 0) { |
| 1399 |
g_debug_level = atoi (optarg); |
| 1400 |
} |
| 1401 |
else if (g_strcmp0 (long_options[option_index].name, "server-name") == 0) { |
| 1402 |
g_free (_server_name); |
| 1403 |
_server_name = g_strdup (optarg); |
| 1404 |
} |
| 1405 |
else if (g_strcmp0 (long_options[option_index].name, "locale") == 0) { |
| 1406 |
g_free (_locale); |
| 1407 |
_locale = g_strdup (optarg); |
| 1408 |
} |
| 1409 |
else if (g_strcmp0 (long_options[option_index].name, "locale-append") == 0) { |
| 1410 |
gchar *tmp = g_strdup_printf ("%s,%s", |
| 1411 |
_locale != NULL ? _locale : LOCALES_STRING, optarg); |
| 1412 |
g_free (_locale); |
| 1413 |
_locale = tmp; |
| 1414 |
} |
| 1415 |
else if (g_strcmp0 (long_options[option_index].name, "help") == 0) { |
| 1416 |
_print_usage (stdout, argv[0]); |
| 1417 |
exit (EXIT_SUCCESS); |
| 1418 |
} |
| 1419 |
else if (g_strcmp0 (long_options[option_index].name, "kill-daemon") == 0) { |
| 1420 |
_kill_daemon = TRUE; |
| 1421 |
} |
| 1422 |
break; |
| 1423 |
case 'v': |
| 1424 |
g_debug_level = atoi (optarg); |
| 1425 |
break; |
| 1426 |
case 'n': |
| 1427 |
g_free (_server_name); |
| 1428 |
_server_name = g_strdup (optarg); |
| 1429 |
break; |
| 1430 |
case 'l': |
| 1431 |
g_free (_locale); |
| 1432 |
_locale = g_strdup (optarg); |
| 1433 |
break; |
| 1434 |
case 'a': { |
| 1435 |
gchar *tmp = g_strdup_printf ("%s,%s", |
| 1436 |
_locale != NULL ? _locale : LOCALES_STRING, optarg); |
| 1437 |
g_free (_locale); |
| 1438 |
_locale = tmp; |
| 1439 |
} |
| 1440 |
break; |
| 1441 |
case 'k': |
| 1442 |
_kill_daemon = TRUE; |
| 1443 |
break; |
| 1444 |
case '?': |
| 1445 |
default: |
| 1446 |
_print_usage (stderr, argv[0]); |
| 1447 |
exit (EXIT_FAILURE); |
| 1448 |
} |
| 1449 |
} |
| 1450 |
|
| 1451 |
_x11_ic_table = g_hash_table_new (g_direct_hash, g_direct_equal); |
| 1452 |
_connections = g_hash_table_new (g_direct_hash, g_direct_equal); |
| 1453 |
|
| 1454 |
signal (SIGINT, _sighandler); |
| 1455 |
signal (SIGTERM, _sighandler); |
| 1456 |
|
| 1457 |
if (_kill_daemon) |
| 1458 |
atexit (_atexit_cb); |
| 1459 |
|
| 1460 |
_xim_init_IMdkit (); |
| 1461 |
ibus_main(); |
| 1462 |
|
| 1463 |
exit (EXIT_SUCCESS); |
| 1464 |
} |