| 1 |
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */ |
| 2 |
/* vim:set et sts=4: */ |
| 3 |
/* ibus - The Input Bus |
| 4 |
* Copyright (C) 2019-2026 Takao Fujiwara <[email protected]> |
| 5 |
* Copyright (C) 2013 Intel Corporation |
| 6 |
* Copyright (C) 2013-2025 Red Hat, Inc. |
| 7 |
* |
| 8 |
* This library is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Lesser General Public License |
| 10 |
* as published by the Free Software Foundation; either version 2.1 of |
| 11 |
* the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public |
| 19 |
* License along with this library; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 21 |
* 02110-1301 USA. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "config.h" |
| 25 |
|
| 26 |
#include <errno.h> |
| 27 |
#include <glib-object.h> |
| 28 |
#include <ibus.h> |
| 29 |
#include <ibusinternal.h> |
| 30 |
#include <string.h> |
| 31 |
#include <sys/time.h> |
| 32 |
#include <wayland-client.h> |
| 33 |
#include <xkbcommon/xkbcommon.h> |
| 34 |
|
| 35 |
#include "input-method-unstable-v1-client-protocol.h" |
| 36 |
#include "input-method-unstable-v2-client-protocol.h" |
| 37 |
#include "text-input-unstable-v1-client-protocol.h" |
| 38 |
#include "text-input-unstable-v3-client-protocol.h" |
| 39 |
#include "virtual-keyboard-unstable-v1-client-protocol.h" |
| 40 |
#include "ibuswaylandim.h" |
| 41 |
|
| 42 |
enum { |
| 43 |
PROP_0 = 0, |
| 44 |
PROP_BUS, |
| 45 |
PROP_DISPLAY, |
| 46 |
PROP_LOG, |
| 47 |
PROP_VERBOSE |
| 48 |
}; |
| 49 |
|
| 50 |
enum { |
| 51 |
IBUS_FOCUS_IN, |
| 52 |
IBUS_FOCUS_OUT, |
| 53 |
LAST_SIGNAL, |
| 54 |
}; |
| 55 |
|
| 56 |
typedef enum |
| 57 |
{ |
| 58 |
INPUT_METHOD_V1, |
| 59 |
INPUT_METHOD_V2, |
| 60 |
} IMProtocolVersion; |
| 61 |
|
| 62 |
struct zwp_input_method_context_union { |
| 63 |
union { |
| 64 |
struct zwp_input_method_context_v1 *context_v1; |
| 65 |
struct zwp_input_method_v2 *input_method_v2; |
| 66 |
} u; |
| 67 |
}; |
| 68 |
|
| 69 |
struct zwp_keyboard_union { |
| 70 |
union { |
| 71 |
struct wl_keyboard *keyboard_v1; |
| 72 |
struct zwp_input_method_keyboard_grab_v2 *keyboard_v2; |
| 73 |
} u; |
| 74 |
}; |
| 75 |
|
| 76 |
struct zwp_input_method_union { |
| 77 |
union { |
| 78 |
struct zwp_input_method_v1 *input_method_v1; |
| 79 |
struct zwp_input_method_v2 *input_method_v2; |
| 80 |
} u; |
| 81 |
}; |
| 82 |
|
| 83 |
typedef struct _IBusWaylandSeat IBusWaylandSeat; |
| 84 |
struct _IBusWaylandSeat |
| 85 |
{ |
| 86 |
struct wl_seat *seat; |
| 87 |
uint32_t wl_name; |
| 88 |
char *name; |
| 89 |
|
| 90 |
/* Input Method V2 */ |
| 91 |
struct zwp_input_method_v2 *input_method_v2; |
| 92 |
struct zwp_input_method_keyboard_grab_v2 *keyboard_v2; |
| 93 |
struct zwp_virtual_keyboard_v1 *virtual_keyboard; |
| 94 |
struct zwp_input_popup_surface_v2 *input_popup_surface; |
| 95 |
gboolean active; |
| 96 |
gboolean pending_activate; |
| 97 |
gboolean pending_deactivate; |
| 98 |
gboolean has_keymap; |
| 99 |
}; |
| 100 |
|
| 101 |
typedef struct _IBusWaylandIMPrivate IBusWaylandIMPrivate; |
| 102 |
struct _IBusWaylandIMPrivate |
| 103 |
{ |
| 104 |
FILE *log; |
| 105 |
gboolean verbose; |
| 106 |
struct wl_display *display; |
| 107 |
IMProtocolVersion version; |
| 108 |
|
| 109 |
GPtrArray *seats; |
| 110 |
IBusWaylandSeat *seat; |
| 111 |
|
| 112 |
/* Input Method V1 */ |
| 113 |
struct zwp_input_method_v1 *input_method_v1; |
| 114 |
struct zwp_input_method_context_v1 *context; |
| 115 |
struct wl_keyboard *keyboard_v1; |
| 116 |
struct zwp_input_panel_v1 *panel; |
| 117 |
struct zwp_input_panel_surface_v1 *panel_surface; |
| 118 |
|
| 119 |
/* Input Method V2 */ |
| 120 |
struct zwp_input_method_manager_v2 *input_method_manager_v2; |
| 121 |
|
| 122 |
IBusBus *ibusbus; |
| 123 |
IBusInputContext *ibuscontext; |
| 124 |
IBusText *preedit_text; |
| 125 |
guint preedit_cursor_pos; |
| 126 |
guint preedit_mode; |
| 127 |
IBusModifierType modifiers; |
| 128 |
gboolean hiding_preedit_text; |
| 129 |
IBusInputHints ibus_hints; |
| 130 |
IBusInputPurpose ibus_purpose; |
| 131 |
|
| 132 |
#if ENABLE_SURROUNDING |
| 133 |
IBusText *surrounding_text; |
| 134 |
guint surrounding_cursor_pos; |
| 135 |
#endif |
| 136 |
|
| 137 |
struct xkb_context *xkb_context; |
| 138 |
|
| 139 |
struct xkb_keymap *keymap; |
| 140 |
struct xkb_state *state; |
| 141 |
struct xkb_state *state_system; |
| 142 |
|
| 143 |
xkb_mod_mask_t shift_mask; |
| 144 |
xkb_mod_mask_t lock_mask; |
| 145 |
xkb_mod_mask_t control_mask; |
| 146 |
xkb_mod_mask_t mod1_mask; |
| 147 |
xkb_mod_mask_t mod2_mask; |
| 148 |
xkb_mod_mask_t mod3_mask; |
| 149 |
xkb_mod_mask_t mod4_mask; |
| 150 |
xkb_mod_mask_t mod5_mask; |
| 151 |
xkb_mod_mask_t super_mask; |
| 152 |
xkb_mod_mask_t hyper_mask; |
| 153 |
xkb_mod_mask_t meta_mask; |
| 154 |
|
| 155 |
uint32_t im_serial; |
| 156 |
int32_t repeat_rate; |
| 157 |
int32_t repeat_delay; |
| 158 |
|
| 159 |
GCancellable *cancellable; |
| 160 |
}; |
| 161 |
|
| 162 |
struct _IBusWaylandKeyEvent |
| 163 |
{ |
| 164 |
struct zwp_input_method_context_v1 *context; |
| 165 |
uint32_t key_serial; |
| 166 |
uint32_t time; |
| 167 |
uint32_t key; |
| 168 |
enum wl_keyboard_key_state state; |
| 169 |
xkb_keysym_t sym; |
| 170 |
uint32_t modifiers; |
| 171 |
IBusWaylandIM *wlim; |
| 172 |
int count; |
| 173 |
guint count_cb_id; |
| 174 |
guint repeat_rate_id; |
| 175 |
char *ibus_object_path; |
| 176 |
gboolean retval; |
| 177 |
}; |
| 178 |
typedef struct _IBusWaylandKeyEvent IBusWaylandKeyEvent; |
| 179 |
|
| 180 |
struct _IBusWaylandSource |
| 181 |
{ |
| 182 |
GSource source; |
| 183 |
GPollFD pfd; |
| 184 |
uint32_t mask; |
| 185 |
struct wl_display *display; |
| 186 |
}; |
| 187 |
typedef struct _IBusWaylandSource IBusWaylandSource; |
| 188 |
|
| 189 |
G_DEFINE_TYPE_WITH_PRIVATE (IBusWaylandIM, ibus_wayland_im, IBUS_TYPE_OBJECT) |
| 190 |
|
| 191 |
static struct wl_registry *_registry; |
| 192 |
static struct zwp_virtual_keyboard_manager_v1 *_virtual_keyboard_manager; |
| 193 |
|
| 194 |
static char _use_sync_mode = 1; |
| 195 |
|
| 196 |
static guint wayland_im_signals[LAST_SIGNAL] = { 0 }; |
| 197 |
|
| 198 |
static void input_method_deactivate |
| 199 |
(void *data, |
| 200 |
struct zwp_input_method_union *input_method, |
| 201 |
struct zwp_input_method_context_v1 *context); |
| 202 |
static GObject *ibus_wayland_im_constructor (GType type, |
| 203 |
guint n_params, |
| 204 |
GObjectConstructParam |
| 205 |
*params); |
| 206 |
static void ibus_wayland_im_set_property (IBusWaylandIM *wlim, |
| 207 |
guint prop_id, |
| 208 |
const GValue *value, |
| 209 |
GParamSpec *pspec); |
| 210 |
static void ibus_wayland_im_get_property (IBusWaylandIM *wlim, |
| 211 |
guint prop_id, |
| 212 |
GValue *value, |
| 213 |
GParamSpec *pspec); |
| 214 |
static void ibus_wayland_im_destroy (IBusObject *object); |
| 215 |
static gboolean ibus_wayland_im_post_key (IBusWaylandIM *wlim, |
| 216 |
uint32_t key, |
| 217 |
uint32_t |
| 218 |
modifiers, |
| 219 |
uint32_t state, |
| 220 |
xkb_keysym_t sym, |
| 221 |
gboolean |
| 222 |
filtered); |
| 223 |
|
| 224 |
|
| 225 |
static char |
| 226 |
_get_char_env (const gchar *name, |
| 227 |
char defval) |
| 228 |
{ |
| 229 |
const gchar *value = g_getenv (name); |
| 230 |
|
| 231 |
if (value == NULL) |
| 232 |
return defval; |
| 233 |
|
| 234 |
if (g_strcmp0 (value, "") == 0 || |
| 235 |
g_strcmp0 (value, "0") == 0 || |
| 236 |
g_strcmp0 (value, "false") == 0 || |
| 237 |
g_strcmp0 (value, "False") == 0 || |
| 238 |
g_strcmp0 (value, "FALSE") == 0) { |
| 239 |
return 0; |
| 240 |
} else if (!g_strcmp0 (value, "2")) { |
| 241 |
return 2; |
| 242 |
} |
| 243 |
|
| 244 |
return 1; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
static gboolean |
| 249 |
ibus_wayland_source_prepare (GSource *base, |
| 250 |
gint *timeout) |
| 251 |
{ |
| 252 |
IBusWaylandSource *source = (IBusWaylandSource *) base; |
| 253 |
|
| 254 |
*timeout = -1; |
| 255 |
|
| 256 |
wl_display_flush (source->display); |
| 257 |
|
| 258 |
return FALSE; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
static gboolean |
| 263 |
ibus_wayland_source_check (GSource *base) |
| 264 |
{ |
| 265 |
IBusWaylandSource *source = (IBusWaylandSource *) base; |
| 266 |
|
| 267 |
if (source->pfd.revents & (G_IO_ERR | G_IO_HUP)) |
| 268 |
g_error ("Lost connection to wayland compositor"); |
| 269 |
|
| 270 |
return source->pfd.revents; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
static gboolean |
| 275 |
ibus_wayland_source_dispatch (GSource *base, |
| 276 |
GSourceFunc callback, |
| 277 |
gpointer data) |
| 278 |
{ |
| 279 |
IBusWaylandSource *source = (IBusWaylandSource *) base; |
| 280 |
|
| 281 |
if (source->pfd.revents) { |
| 282 |
wl_display_dispatch (source->display); |
| 283 |
source->pfd.revents = 0; |
| 284 |
} |
| 285 |
|
| 286 |
return TRUE; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
static void |
| 291 |
ibus_wayland_source_finalize (GSource *source) |
| 292 |
{ |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
static GSourceFuncs ibus_wayland_source_funcs = { |
| 297 |
ibus_wayland_source_prepare, |
| 298 |
ibus_wayland_source_check, |
| 299 |
ibus_wayland_source_dispatch, |
| 300 |
ibus_wayland_source_finalize |
| 301 |
}; |
| 302 |
|
| 303 |
|
| 304 |
GSource * |
| 305 |
ibus_wayland_source_new (struct wl_display *display) |
| 306 |
{ |
| 307 |
GSource *source; |
| 308 |
IBusWaylandSource *wlsource; |
| 309 |
|
| 310 |
source = g_source_new (&ibus_wayland_source_funcs, |
| 311 |
sizeof (IBusWaylandSource)); |
| 312 |
wlsource = (IBusWaylandSource *) source; |
| 313 |
|
| 314 |
wlsource->display = display; |
| 315 |
wlsource->pfd.fd = wl_display_get_fd (display); |
| 316 |
wlsource->pfd.events = G_IO_IN | G_IO_ERR | G_IO_HUP; |
| 317 |
g_source_add_poll (source, &wlsource->pfd); |
| 318 |
|
| 319 |
return source; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
static void |
| 324 |
ibus_wayland_im_commit_text (IBusWaylandIM *wlim, |
| 325 |
const char *str) |
| 326 |
{ |
| 327 |
IBusWaylandIMPrivate *priv; |
| 328 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 329 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 330 |
switch (priv->version) { |
| 331 |
case INPUT_METHOD_V1: |
| 332 |
zwp_input_method_context_v1_commit_string (priv->context, |
| 333 |
priv->im_serial, |
| 334 |
str); |
| 335 |
break; |
| 336 |
case INPUT_METHOD_V2: |
| 337 |
zwp_input_method_v2_commit_string (priv->seat->input_method_v2, str); |
| 338 |
zwp_input_method_v2_commit (priv->seat->input_method_v2, |
| 339 |
priv->im_serial); |
| 340 |
break; |
| 341 |
default: |
| 342 |
g_assert_not_reached (); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
static void |
| 348 |
ibus_wayland_im_key (IBusWaylandIM *wlim, |
| 349 |
uint32_t key_serial, |
| 350 |
uint32_t time, |
| 351 |
uint32_t key, |
| 352 |
uint32_t state) |
| 353 |
{ |
| 354 |
IBusWaylandIMPrivate *priv; |
| 355 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 356 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 357 |
switch (priv->version) { |
| 358 |
case INPUT_METHOD_V1: |
| 359 |
zwp_input_method_context_v1_key (priv->context, |
| 360 |
key_serial, |
| 361 |
time, |
| 362 |
key, |
| 363 |
state); |
| 364 |
break; |
| 365 |
case INPUT_METHOD_V2: |
| 366 |
/* wlroots/types/wlr_virtual_keyboard_v1.c:virtual_keyboard_key() |
| 367 |
* returns "Cannot send a keypress before defining a keymap" |
| 368 |
* if `has_keymap` is %FALSE. |
| 369 |
*/ |
| 370 |
if (priv->seat->has_keymap) { |
| 371 |
zwp_virtual_keyboard_v1_key (priv->seat->virtual_keyboard, |
| 372 |
time, key, state); |
| 373 |
} |
| 374 |
break; |
| 375 |
default: |
| 376 |
g_assert_not_reached (); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
static void |
| 382 |
ibus_wayland_im_keysym (IBusWaylandIM *wlim, |
| 383 |
uint32_t im_serial, |
| 384 |
guint keyval, |
| 385 |
uint32_t state, |
| 386 |
guint modifiers) |
| 387 |
{ |
| 388 |
IBusWaylandIMPrivate *priv; |
| 389 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 390 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 391 |
switch (priv->version) { |
| 392 |
case INPUT_METHOD_V1: |
| 393 |
zwp_input_method_context_v1_keysym (priv->context, |
| 394 |
im_serial, |
| 395 |
0, |
| 396 |
keyval, |
| 397 |
state, |
| 398 |
modifiers); |
| 399 |
break; |
| 400 |
case INPUT_METHOD_V2: |
| 401 |
g_warning ("TODO"); |
| 402 |
break; |
| 403 |
default: |
| 404 |
g_assert_not_reached (); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
static void |
| 410 |
_context_commit_text_cb (IBusInputContext *context, |
| 411 |
IBusText *text, |
| 412 |
IBusWaylandIM *wlim) |
| 413 |
{ |
| 414 |
ibus_wayland_im_commit_text (wlim, text->text); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
static void |
| 419 |
_context_forward_key_event_cb (IBusInputContext *context, |
| 420 |
guint keyval, |
| 421 |
guint keycode, |
| 422 |
guint modifiers, |
| 423 |
IBusWaylandIM *wlim) |
| 424 |
{ |
| 425 |
IBusWaylandIMPrivate *priv; |
| 426 |
uint32_t state; |
| 427 |
|
| 428 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 429 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 430 |
if (modifiers & IBUS_RELEASE_MASK) |
| 431 |
state = WL_KEYBOARD_KEY_STATE_RELEASED; |
| 432 |
else |
| 433 |
state = WL_KEYBOARD_KEY_STATE_PRESSED; |
| 434 |
|
| 435 |
ibus_wayland_im_keysym (wlim, |
| 436 |
priv->im_serial, |
| 437 |
keyval, |
| 438 |
state, |
| 439 |
modifiers); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
/** |
| 444 |
* ibus_wayland_im_update_preedit_style: |
| 445 |
* @wlim: An #IBusWaylandIM |
| 446 |
* |
| 447 |
* Convert RGB values to IBusAttrPreedit at first. |
| 448 |
* Convert IBusAttrPreedit to zwp_text_input_v1_preedit_style at second. |
| 449 |
* |
| 450 |
* RF. https://github.com/ibus/ibus/wiki/Wayland-Colors |
| 451 |
*/ |
| 452 |
static void |
| 453 |
ibus_wayland_im_update_preedit_style (IBusWaylandIM *wlim) |
| 454 |
{ |
| 455 |
IBusWaylandIMPrivate *priv; |
| 456 |
IBusAttrList *attrs; |
| 457 |
guint i; |
| 458 |
const char *str; |
| 459 |
uint32_t whole_wstyle = ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_DEFAULT; |
| 460 |
uint32_t prev_start = 0; |
| 461 |
uint32_t prev_end = 0; |
| 462 |
|
| 463 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 464 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 465 |
if (!priv->preedit_text) |
| 466 |
return; |
| 467 |
attrs = priv->preedit_text->attrs; |
| 468 |
if (!attrs) |
| 469 |
return; |
| 470 |
for (i = 0; ; i++) { |
| 471 |
IBusAttribute *attr = ibus_attr_list_get (attrs, i); |
| 472 |
IBusAttrPreedit istyle = IBUS_ATTR_PREEDIT_DEFAULT; |
| 473 |
uint32_t wstyle = ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_DEFAULT; |
| 474 |
uint32_t start, end; |
| 475 |
if (attr == NULL) |
| 476 |
break; |
| 477 |
switch (attr->type) { |
| 478 |
case IBUS_ATTR_TYPE_UNDERLINE: |
| 479 |
istyle = IBUS_ATTR_PREEDIT_WHOLE; |
| 480 |
break; |
| 481 |
case IBUS_ATTR_TYPE_FOREGROUND: |
| 482 |
switch (attr->value) { |
| 483 |
case 0x7F7F7F: /* typing-booster */ |
| 484 |
istyle = IBUS_ATTR_PREEDIT_PREDICTION; |
| 485 |
break; |
| 486 |
case 0xF90F0F: /* table */ |
| 487 |
istyle = IBUS_ATTR_PREEDIT_PREFIX; |
| 488 |
break; |
| 489 |
case 0x1EDC1A: /* table */ |
| 490 |
istyle = IBUS_ATTR_PREEDIT_SUFFIX; |
| 491 |
break; |
| 492 |
case 0xA40000: /* typing-booster, table */ |
| 493 |
istyle = IBUS_ATTR_PREEDIT_ERROR_SPELLING; |
| 494 |
break; |
| 495 |
case 0xFF00FF: /* typing-booster */ |
| 496 |
istyle = IBUS_ATTR_PREEDIT_ERROR_COMPOSE; |
| 497 |
break; |
| 498 |
case 0x0: /* Japanese */ |
| 499 |
case 0xFF000000: |
| 500 |
break; |
| 501 |
case 0xFFFFFF: /* hangul */ |
| 502 |
case 0xFFFFFFFF: |
| 503 |
istyle = IBUS_ATTR_PREEDIT_SELECTION; |
| 504 |
break; |
| 505 |
default: /* Custom */ |
| 506 |
istyle = IBUS_ATTR_PREEDIT_NONE; |
| 507 |
} |
| 508 |
break; |
| 509 |
case IBUS_ATTR_TYPE_BACKGROUND: |
| 510 |
switch (attr->value) { |
| 511 |
case 0xC8C8F0: /* Japanese */ |
| 512 |
case 0xFFC8C8F0: |
| 513 |
istyle = IBUS_ATTR_PREEDIT_SELECTION; |
| 514 |
break; |
| 515 |
default:; /* Custom */ |
| 516 |
} |
| 517 |
break; |
| 518 |
case IBUS_ATTR_TYPE_HINT: |
| 519 |
istyle = attr->value; |
| 520 |
break; |
| 521 |
default: |
| 522 |
istyle = IBUS_ATTR_PREEDIT_NONE; |
| 523 |
} |
| 524 |
switch (istyle) { |
| 525 |
case IBUS_ATTR_PREEDIT_NONE: |
| 526 |
wstyle = ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_NONE; |
| 527 |
break; |
| 528 |
case IBUS_ATTR_PREEDIT_WHOLE: |
| 529 |
wstyle = ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_UNDERLINE; |
| 530 |
break; |
| 531 |
case IBUS_ATTR_PREEDIT_SELECTION: |
| 532 |
wstyle = ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_SELECTION; |
| 533 |
break; |
| 534 |
case IBUS_ATTR_PREEDIT_PREDICTION: |
| 535 |
wstyle = ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_INACTIVE; |
| 536 |
break; |
| 537 |
case IBUS_ATTR_PREEDIT_PREFIX: |
| 538 |
wstyle = ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_HIGHLIGHT; |
| 539 |
break; |
| 540 |
case IBUS_ATTR_PREEDIT_SUFFIX: |
| 541 |
wstyle = ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_INACTIVE; |
| 542 |
break; |
| 543 |
case IBUS_ATTR_PREEDIT_ERROR_SPELLING: |
| 544 |
wstyle = ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_INCORRECT; |
| 545 |
break; |
| 546 |
case IBUS_ATTR_PREEDIT_ERROR_COMPOSE: |
| 547 |
wstyle = ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_INCORRECT; |
| 548 |
break; |
| 549 |
default:; |
| 550 |
} |
| 551 |
if (wstyle == ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_DEFAULT) |
| 552 |
continue; |
| 553 |
str = priv->preedit_text->text; |
| 554 |
start = g_utf8_offset_to_pointer (str, attr->start_index) - str; |
| 555 |
end = g_utf8_offset_to_pointer (str, attr->end_index) - str; |
| 556 |
/* Double styles cannot be applied likes the underline and |
| 557 |
* preedit color. */ |
| 558 |
if (start == 0 && strlen (str) == end && |
| 559 |
(i > 0 || ibus_attr_list_get (attrs, i + 1))) { |
| 560 |
whole_wstyle = wstyle; |
| 561 |
continue; |
| 562 |
} |
| 563 |
if (end < prev_start) { |
| 564 |
if (priv->log) { |
| 565 |
fprintf (priv->log, |
| 566 |
"Reverse order is not supported in end %d for %s " |
| 567 |
"against start %d.\n", end, str, prev_start); |
| 568 |
fflush (priv->log); |
| 569 |
} |
| 570 |
continue; |
| 571 |
} |
| 572 |
if (prev_end > end) { |
| 573 |
if (priv->log) { |
| 574 |
fprintf (priv->log, |
| 575 |
"Nested styles are not supported in end %d for %s " |
| 576 |
"against end %d.\n", end, str, prev_end); |
| 577 |
fflush (priv->log); |
| 578 |
} |
| 579 |
continue; |
| 580 |
} |
| 581 |
if (prev_end > start && prev_start >= start) |
| 582 |
start = prev_end; |
| 583 |
if (start >= end) { |
| 584 |
if (priv->log) { |
| 585 |
fprintf (priv->log, "Wrong start %d and end %d for %s.\n", |
| 586 |
start, end, str); |
| 587 |
fflush (priv->log); |
| 588 |
} |
| 589 |
return; |
| 590 |
} |
| 591 |
zwp_input_method_context_v1_preedit_styling (priv->context, |
| 592 |
start, |
| 593 |
end - start, |
| 594 |
wstyle); |
| 595 |
prev_start = start; |
| 596 |
prev_end = end; |
| 597 |
} |
| 598 |
if (whole_wstyle != ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_DEFAULT) { |
| 599 |
uint32_t whole_start = 0; |
| 600 |
uint32_t whole_end = strlen (str); |
| 601 |
uint32_t start, end; |
| 602 |
for (i = 0; ; i++) { |
| 603 |
IBusAttribute *attr = ibus_attr_list_get (attrs, i); |
| 604 |
if (!attr) |
| 605 |
break; |
| 606 |
start = g_utf8_offset_to_pointer (str, attr->start_index) - str; |
| 607 |
end = g_utf8_offset_to_pointer (str, attr->end_index) - str; |
| 608 |
if (start == 0 && strlen (str) == end) |
| 609 |
continue; |
| 610 |
if (start == 0) { |
| 611 |
whole_start = end; |
| 612 |
} else if (strlen (str) == end) { |
| 613 |
whole_end = start; |
| 614 |
} else { |
| 615 |
whole_end = start; |
| 616 |
if (whole_start < whole_end) { |
| 617 |
zwp_input_method_context_v1_preedit_styling ( |
| 618 |
priv->context, |
| 619 |
whole_start, |
| 620 |
whole_end - whole_start, |
| 621 |
whole_wstyle); |
| 622 |
} |
| 623 |
whole_start = end; |
| 624 |
whole_end = strlen (str); |
| 625 |
} |
| 626 |
} |
| 627 |
if (whole_start < whole_end) { |
| 628 |
zwp_input_method_context_v1_preedit_styling ( |
| 629 |
priv->context, |
| 630 |
whole_start, |
| 631 |
whole_end - whole_start, |
| 632 |
whole_wstyle); |
| 633 |
} |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
|
| 638 |
static void |
| 639 |
_context_show_preedit_text_cb (IBusInputContext *context, |
| 640 |
IBusWaylandIM *wlim) |
| 641 |
{ |
| 642 |
IBusWaylandIMPrivate *priv; |
| 643 |
uint32_t cursor; |
| 644 |
const char *commit = ""; |
| 645 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 646 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 647 |
/* CURSOR is byte offset. */ |
| 648 |
cursor = |
| 649 |
g_utf8_offset_to_pointer (priv->preedit_text->text, |
| 650 |
priv->preedit_cursor_pos) - |
| 651 |
priv->preedit_text->text; |
| 652 |
|
| 653 |
if (priv->preedit_mode == IBUS_ENGINE_PREEDIT_COMMIT) |
| 654 |
commit = priv->preedit_text->text; |
| 655 |
switch (priv->version) { |
| 656 |
case INPUT_METHOD_V1: |
| 657 |
zwp_input_method_context_v1_preedit_cursor (priv->context, |
| 658 |
cursor); |
| 659 |
ibus_wayland_im_update_preedit_style (wlim); |
| 660 |
zwp_input_method_context_v1_preedit_string (priv->context, |
| 661 |
priv->im_serial, |
| 662 |
priv->preedit_text->text, |
| 663 |
commit); |
| 664 |
break; |
| 665 |
case INPUT_METHOD_V2: |
| 666 |
zwp_input_method_v2_set_preedit_string ( |
| 667 |
priv->seat->input_method_v2, |
| 668 |
priv->preedit_text->text, |
| 669 |
cursor, |
| 670 |
strlen (priv->preedit_text->text)); |
| 671 |
zwp_input_method_v2_commit (priv->seat->input_method_v2, |
| 672 |
priv->im_serial); |
| 673 |
priv->hiding_preedit_text = FALSE; |
| 674 |
break; |
| 675 |
default: |
| 676 |
g_assert_not_reached (); |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
static void |
| 682 |
_context_hide_preedit_text_cb (IBusInputContext *context, |
| 683 |
IBusWaylandIM *wlim) |
| 684 |
{ |
| 685 |
IBusWaylandIMPrivate *priv; |
| 686 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 687 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 688 |
switch (priv->version) { |
| 689 |
case INPUT_METHOD_V1: |
| 690 |
zwp_input_method_context_v1_preedit_string (priv->context, |
| 691 |
priv->im_serial, |
| 692 |
"", |
| 693 |
""); |
| 694 |
break; |
| 695 |
case INPUT_METHOD_V2: |
| 696 |
zwp_input_method_v2_set_preedit_string (priv->seat->input_method_v2, |
| 697 |
"", 0, 0); |
| 698 |
zwp_input_method_v2_commit (priv->seat->input_method_v2, |
| 699 |
priv->im_serial); |
| 700 |
priv->hiding_preedit_text = TRUE; |
| 701 |
break; |
| 702 |
default: |
| 703 |
g_assert_not_reached (); |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
static void |
| 709 |
_context_update_preedit_text_cb (IBusInputContext *context, |
| 710 |
IBusText *text, |
| 711 |
gint cursor_pos, |
| 712 |
gboolean visible, |
| 713 |
guint mode, |
| 714 |
IBusWaylandIM *wlim) |
| 715 |
{ |
| 716 |
IBusWaylandIMPrivate *priv; |
| 717 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 718 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 719 |
if (priv->preedit_text) |
| 720 |
g_object_unref (priv->preedit_text); |
| 721 |
priv->preedit_text = g_object_ref_sink (text); |
| 722 |
priv->preedit_cursor_pos = cursor_pos; |
| 723 |
priv->preedit_mode = mode; |
| 724 |
|
| 725 |
if (visible) |
| 726 |
_context_show_preedit_text_cb (context, wlim); |
| 727 |
else |
| 728 |
_context_hide_preedit_text_cb (context, wlim); |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
#if ENABLE_SURROUNDING |
| 733 |
static void |
| 734 |
_context_delete_surrounding_text_cb (IBusInputContext *context, |
| 735 |
gint offset, |
| 736 |
guint nchars, |
| 737 |
IBusWaylandIM *wlim) |
| 738 |
{ |
| 739 |
IBusWaylandIMPrivate *priv; |
| 740 |
const char *start, *end; |
| 741 |
const char *before, *after; |
| 742 |
const char *cursor; |
| 743 |
uint32_t before_length; |
| 744 |
uint32_t after_length; |
| 745 |
|
| 746 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 747 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 748 |
|
| 749 |
if (!priv->surrounding_text) |
| 750 |
return; |
| 751 |
|
| 752 |
offset = MIN (offset, 0); |
| 753 |
|
| 754 |
start = priv->surrounding_text->text; |
| 755 |
end = start + strlen (priv->surrounding_text->text); |
| 756 |
cursor = start + priv->surrounding_cursor_pos; |
| 757 |
|
| 758 |
before = g_utf8_offset_to_pointer (cursor, offset); |
| 759 |
g_return_if_fail (before >= start); |
| 760 |
after = g_utf8_offset_to_pointer (cursor, offset + nchars); |
| 761 |
g_return_if_fail (after <= end); |
| 762 |
|
| 763 |
before_length = cursor - before; |
| 764 |
after_length = after - cursor; |
| 765 |
|
| 766 |
switch (priv->version) { |
| 767 |
case INPUT_METHOD_V1: |
| 768 |
zwp_input_method_context_v1_delete_surrounding_text ( |
| 769 |
priv->context, |
| 770 |
-before_length, |
| 771 |
before_length + after_length); |
| 772 |
break; |
| 773 |
case INPUT_METHOD_V2: |
| 774 |
zwp_input_method_v2_delete_surrounding_text ( |
| 775 |
priv->seat->input_method_v2, |
| 776 |
before_length, |
| 777 |
after_length); |
| 778 |
zwp_input_method_v2_commit (priv->seat->input_method_v2, |
| 779 |
priv->im_serial); |
| 780 |
break; |
| 781 |
default: |
| 782 |
g_assert_not_reached (); |
| 783 |
} |
| 784 |
} |
| 785 |
#endif |
| 786 |
|
| 787 |
|
| 788 |
static void |
| 789 |
handle_surrounding_text (void *data, |
| 790 |
struct zwp_input_method_context_union *context, |
| 791 |
const char *text, |
| 792 |
uint32_t cursor, |
| 793 |
uint32_t anchor) |
| 794 |
{ |
| 795 |
#if ENABLE_SURROUNDING |
| 796 |
IBusWaylandIM *wlim = data; |
| 797 |
IBusWaylandIMPrivate *priv; |
| 798 |
IBusText *ibustext; |
| 799 |
|
| 800 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 801 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 802 |
|
| 803 |
ibustext = ibus_text_new_from_string (text); |
| 804 |
|
| 805 |
if (priv->surrounding_text) |
| 806 |
g_object_unref (priv->surrounding_text); |
| 807 |
priv->surrounding_text = g_object_ref_sink (ibustext); |
| 808 |
priv->surrounding_cursor_pos = cursor; |
| 809 |
|
| 810 |
if (priv->ibuscontext != NULL && |
| 811 |
ibus_input_context_needs_surrounding_text (priv->ibuscontext)) { |
| 812 |
/* CURSOR_POS and ANCHOR_POS are character offset. */ |
| 813 |
guint cursor_pos = g_utf8_pointer_to_offset (text, text + cursor); |
| 814 |
guint anchor_pos = g_utf8_pointer_to_offset (text, text + anchor); |
| 815 |
|
| 816 |
ibus_input_context_set_surrounding_text (priv->ibuscontext, |
| 817 |
ibustext, |
| 818 |
cursor_pos, |
| 819 |
anchor_pos); |
| 820 |
} |
| 821 |
#endif |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
static void |
| 826 |
context_surrounding_text_v1 (void *data, |
| 827 |
struct zwp_input_method_context_v1 *context_v1, |
| 828 |
const char *text, |
| 829 |
uint32_t cursor, |
| 830 |
uint32_t anchor) |
| 831 |
{ |
| 832 |
struct zwp_input_method_context_union context; |
| 833 |
context.u.context_v1 = context_v1; |
| 834 |
handle_surrounding_text (data, &context, text, cursor, anchor); |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
static void |
| 839 |
context_reset_v1 (void *data, |
| 840 |
struct zwp_input_method_context_v1 *context_v1) |
| 841 |
{ |
| 842 |
} |
| 843 |
|
| 844 |
|
| 845 |
static void |
| 846 |
context_content_type_v1 (void *data, |
| 847 |
struct zwp_input_method_context_v1 *context_v1, |
| 848 |
uint32_t hints, |
| 849 |
uint32_t purpose) |
| 850 |
{ |
| 851 |
IBusWaylandIM *wlim = data; |
| 852 |
IBusWaylandIMPrivate *priv; |
| 853 |
IBusInputHints ibus_hints = IBUS_INPUT_HINT_NONE; |
| 854 |
IBusInputPurpose ibus_purpose = IBUS_INPUT_PURPOSE_FREE_FORM; |
| 855 |
|
| 856 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 857 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 858 |
|
| 859 |
/* ZWP_TEXT_INPUT_V1_CONTENT_HINT_PASSWORD == HIDDEN_TEXT & SENSITIVE_DATA |
| 860 |
* ZWP_TEXT_INPUT_V1_CONTENT_HINT_DEFAULT == AUTO_COMPLETION & |
| 861 |
* AUTO_CORRECTION & |
| 862 |
* AUTO_CAPITALIZATION |
| 863 |
*/ |
| 864 |
if (hints & ZWP_TEXT_INPUT_V1_CONTENT_HINT_AUTO_COMPLETION) |
| 865 |
ibus_hints |= IBUS_INPUT_HINT_WORD_COMPLETION; |
| 866 |
if (hints & ZWP_TEXT_INPUT_V1_CONTENT_HINT_AUTO_CORRECTION) |
| 867 |
ibus_hints |= IBUS_INPUT_HINT_SPELLCHECK; |
| 868 |
if (hints & ZWP_TEXT_INPUT_V1_CONTENT_HINT_AUTO_CAPITALIZATION) |
| 869 |
ibus_hints |= IBUS_INPUT_HINT_UPPERCASE_SENTENCES; |
| 870 |
if (hints & ZWP_TEXT_INPUT_V1_CONTENT_HINT_LOWERCASE) |
| 871 |
ibus_hints |= IBUS_INPUT_HINT_LOWERCASE; |
| 872 |
if (hints & ZWP_TEXT_INPUT_V1_CONTENT_HINT_UPPERCASE) |
| 873 |
ibus_hints |= IBUS_INPUT_HINT_UPPERCASE_CHARS; |
| 874 |
if (hints & ZWP_TEXT_INPUT_V1_CONTENT_HINT_TITLECASE) |
| 875 |
ibus_hints |= IBUS_INPUT_HINT_UPPERCASE_WORDS; |
| 876 |
if (hints & ZWP_TEXT_INPUT_V1_CONTENT_HINT_HIDDEN_TEXT) |
| 877 |
ibus_hints |= IBUS_INPUT_HINT_HIDDEN_TEXT; |
| 878 |
if (hints & ZWP_TEXT_INPUT_V1_CONTENT_HINT_SENSITIVE_DATA) |
| 879 |
ibus_hints |= IBUS_INPUT_HINT_PRIVATE; |
| 880 |
if (hints & ZWP_TEXT_INPUT_V1_CONTENT_HINT_LATIN) |
| 881 |
ibus_hints |= IBUS_INPUT_HINT_LATIN; |
| 882 |
if (hints & ZWP_TEXT_INPUT_V1_CONTENT_HINT_MULTILINE) |
| 883 |
ibus_hints |= IBUS_INPUT_HINT_MULTILINE; |
| 884 |
|
| 885 |
switch (purpose) { |
| 886 |
case ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_NORMAL: |
| 887 |
ibus_purpose = IBUS_INPUT_PURPOSE_FREE_FORM; |
| 888 |
break; |
| 889 |
case ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_ALPHA: |
| 890 |
ibus_purpose = IBUS_INPUT_PURPOSE_ALPHA; |
| 891 |
break; |
| 892 |
case ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_DIGITS: |
| 893 |
ibus_purpose = IBUS_INPUT_PURPOSE_DIGITS; |
| 894 |
break; |
| 895 |
case ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_NUMBER: |
| 896 |
ibus_purpose = IBUS_INPUT_PURPOSE_NUMBER; |
| 897 |
break; |
| 898 |
case ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_PHONE: |
| 899 |
ibus_purpose = IBUS_INPUT_PURPOSE_PHONE; |
| 900 |
break; |
| 901 |
case ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_URL: |
| 902 |
ibus_purpose = IBUS_INPUT_PURPOSE_URL; |
| 903 |
break; |
| 904 |
case ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_EMAIL: |
| 905 |
ibus_purpose = IBUS_INPUT_PURPOSE_EMAIL; |
| 906 |
break; |
| 907 |
case ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_NAME: |
| 908 |
ibus_purpose = IBUS_INPUT_PURPOSE_NAME; |
| 909 |
break; |
| 910 |
case ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_PASSWORD: |
| 911 |
ibus_purpose = IBUS_INPUT_PURPOSE_PASSWORD; |
| 912 |
break; |
| 913 |
case ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_DATE: |
| 914 |
ibus_purpose = IBUS_INPUT_PURPOSE_DATE; |
| 915 |
break; |
| 916 |
case ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_TIME: |
| 917 |
ibus_purpose = IBUS_INPUT_PURPOSE_TIME; |
| 918 |
break; |
| 919 |
case ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_DATETIME: |
| 920 |
ibus_purpose = IBUS_INPUT_PURPOSE_DATETIME; |
| 921 |
break; |
| 922 |
case ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_TERMINAL: |
| 923 |
ibus_purpose = IBUS_INPUT_PURPOSE_TERMINAL; |
| 924 |
break; |
| 925 |
default: |
| 926 |
g_warning ("Wrong purpose in the input-method context: %d", purpose); |
| 927 |
} |
| 928 |
|
| 929 |
priv->ibus_hints = ibus_hints; |
| 930 |
priv->ibus_purpose = ibus_purpose; |
| 931 |
|
| 932 |
/* Update priv->ibus_[hints|purpose] after _create_input_context_done() |
| 933 |
* is called. |
| 934 |
*/ |
| 935 |
if (G_UNLIKELY (priv->ibuscontext)) { |
| 936 |
ibus_input_context_set_content_type (priv->ibuscontext, |
| 937 |
ibus_purpose, |
| 938 |
ibus_hints); |
| 939 |
} |
| 940 |
} |
| 941 |
|
| 942 |
|
| 943 |
static void |
| 944 |
context_invoke_action_v1 (void *data, |
| 945 |
struct zwp_input_method_context_v1 *context_v1, |
| 946 |
uint32_t button, |
| 947 |
uint32_t index) |
| 948 |
{ |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
static void |
| 953 |
context_commit_state_v1 (void *data, |
| 954 |
struct zwp_input_method_context_v1 *context_v1, |
| 955 |
uint32_t serial) |
| 956 |
{ |
| 957 |
IBusWaylandIM *wlim = data; |
| 958 |
IBusWaylandIMPrivate *priv; |
| 959 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 960 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 961 |
priv->im_serial = serial; |
| 962 |
} |
| 963 |
|
| 964 |
|
| 965 |
static void |
| 966 |
context_preferred_language_v1 (void *data, |
| 967 |
struct zwp_input_method_context_v1 *context_v1, |
| 968 |
const char *language) |
| 969 |
{ |
| 970 |
} |
| 971 |
|
| 972 |
|
| 973 |
static const struct zwp_input_method_context_v1_listener context_listener_v1 = { |
| 974 |
.surrounding_text = context_surrounding_text_v1, |
| 975 |
.reset = context_reset_v1, |
| 976 |
.content_type = context_content_type_v1, |
| 977 |
.invoke_action= context_invoke_action_v1, |
| 978 |
.commit_state = context_commit_state_v1, |
| 979 |
.preferred_language = context_preferred_language_v1 |
| 980 |
}; |
| 981 |
|
| 982 |
|
| 983 |
static struct xkb_keymap * |
| 984 |
create_user_xkb_keymap (struct xkb_context *xkb_context, |
| 985 |
IBusEngineDesc *desc) |
| 986 |
{ |
| 987 |
struct xkb_rule_names names; |
| 988 |
const gchar *layout; |
| 989 |
|
| 990 |
g_assert (xkb_context); |
| 991 |
g_assert (desc); |
| 992 |
names.rules = "evdev"; |
| 993 |
names.model = "pc105"; |
| 994 |
layout = ibus_engine_desc_get_layout (desc); |
| 995 |
if (!layout || *layout == '\0' || !g_strcmp0 (layout, "default")) |
| 996 |
return NULL; |
| 997 |
names.layout = layout; |
| 998 |
names.variant = ibus_engine_desc_get_layout_variant (desc); |
| 999 |
names.options = g_getenv ("XKB_DEFAULT_OPTIONS"); |
| 1000 |
return xkb_keymap_new_from_names (xkb_context, &names, 0); |
| 1001 |
} |
| 1002 |
|
| 1003 |
|
| 1004 |
static struct xkb_keymap * |
| 1005 |
create_system_xkb_keymap (struct xkb_context *xkb_context, |
| 1006 |
uint32_t format, |
| 1007 |
int32_t fd, |
| 1008 |
uint32_t size) |
| 1009 |
{ |
| 1010 |
GMappedFile *map; |
| 1011 |
GError *error = NULL; |
| 1012 |
struct xkb_keymap *xkb_keymap; |
| 1013 |
|
| 1014 |
g_assert (xkb_context); |
| 1015 |
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { |
| 1016 |
close(fd); |
| 1017 |
return NULL; |
| 1018 |
} |
| 1019 |
|
| 1020 |
map = g_mapped_file_new_from_fd (fd, FALSE, &error); |
| 1021 |
if (map == NULL) { |
| 1022 |
if (error) { |
| 1023 |
g_warning ("Failed to map file fd %s", error->message); |
| 1024 |
g_error_free (error); |
| 1025 |
} |
| 1026 |
close (fd); |
| 1027 |
return NULL; |
| 1028 |
} |
| 1029 |
|
| 1030 |
xkb_keymap = xkb_map_new_from_string (xkb_context, |
| 1031 |
g_mapped_file_get_contents (map), |
| 1032 |
XKB_KEYMAP_FORMAT_TEXT_V1, |
| 1033 |
0); |
| 1034 |
g_mapped_file_unref (map); |
| 1035 |
close(fd); |
| 1036 |
return xkb_keymap; |
| 1037 |
} |
| 1038 |
|
| 1039 |
|
| 1040 |
static gboolean |
| 1041 |
ibus_wayland_im_update_xkb_state (IBusWaylandIM *wlim, |
| 1042 |
struct xkb_keymap *keymap) |
| 1043 |
{ |
| 1044 |
IBusWaylandIMPrivate *priv; |
| 1045 |
struct xkb_state *state; |
| 1046 |
|
| 1047 |
g_return_val_if_fail (IBUS_IS_WAYLAND_IM (wlim), FALSE); |
| 1048 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 1049 |
g_return_val_if_fail (keymap, FALSE); |
| 1050 |
g_return_val_if_fail ((state = xkb_state_new (keymap)), FALSE); |
| 1051 |
|
| 1052 |
priv->shift_mask = |
| 1053 |
1 << xkb_map_mod_get_index (keymap, "Shift"); |
| 1054 |
priv->lock_mask = |
| 1055 |
1 << xkb_map_mod_get_index (keymap, "Lock"); |
| 1056 |
priv->control_mask = |
| 1057 |
1 << xkb_map_mod_get_index (keymap, "Control"); |
| 1058 |
priv->mod1_mask = |
| 1059 |
1 << xkb_map_mod_get_index (keymap, "Mod1"); |
| 1060 |
priv->mod2_mask = |
| 1061 |
1 << xkb_map_mod_get_index (keymap, "Mod2"); |
| 1062 |
priv->mod3_mask = |
| 1063 |
1 << xkb_map_mod_get_index (keymap, "Mod3"); |
| 1064 |
priv->mod4_mask = |
| 1065 |
1 << xkb_map_mod_get_index (keymap, "Mod4"); |
| 1066 |
priv->mod5_mask = |
| 1067 |
1 << xkb_map_mod_get_index (keymap, "Mod5"); |
| 1068 |
priv->super_mask = |
| 1069 |
1 << xkb_map_mod_get_index (keymap, "Super"); |
| 1070 |
priv->hyper_mask = |
| 1071 |
1 << xkb_map_mod_get_index (keymap, "Hyper"); |
| 1072 |
priv->meta_mask = |
| 1073 |
1 << xkb_map_mod_get_index (keymap, "Meta"); |
| 1074 |
|
| 1075 |
if (priv->state) |
| 1076 |
xkb_state_unref (priv->state); |
| 1077 |
if (priv->keymap) |
| 1078 |
xkb_keymap_unref (priv->keymap); |
| 1079 |
priv->keymap = keymap; |
| 1080 |
priv->state = state; |
| 1081 |
return TRUE; |
| 1082 |
} |
| 1083 |
|
| 1084 |
|
| 1085 |
static void |
| 1086 |
_bus_global_engine_changed_cb (IBusBus *bus, |
| 1087 |
gchar *engine_name, |
| 1088 |
IBusWaylandIM *wlim) |
| 1089 |
{ |
| 1090 |
IBusWaylandIMPrivate *priv; |
| 1091 |
IBusEngineDesc *desc; |
| 1092 |
struct xkb_keymap *keymap; |
| 1093 |
|
| 1094 |
g_return_if_fail (IBUS_IS_BUS (bus)); |
| 1095 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 1096 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 1097 |
desc = ibus_bus_get_global_engine (bus); |
| 1098 |
g_assert (desc); |
| 1099 |
g_assert (!g_strcmp0 (ibus_engine_desc_get_name (desc), engine_name)); |
| 1100 |
keymap = create_user_xkb_keymap (priv->xkb_context, desc); |
| 1101 |
if (keymap && !ibus_wayland_im_update_xkb_state (wlim, keymap)) |
| 1102 |
xkb_keymap_unref (keymap); |
| 1103 |
g_object_unref (desc); |
| 1104 |
} |
| 1105 |
|
| 1106 |
|
| 1107 |
static void |
| 1108 |
input_method_keyboard_keymap (void *data, |
| 1109 |
struct zwp_keyboard_union *keyboard, |
| 1110 |
uint32_t format, |
| 1111 |
int32_t fd, |
| 1112 |
uint32_t size) |
| 1113 |
{ |
| 1114 |
IBusWaylandIM *wlim = data; |
| 1115 |
IBusWaylandIMPrivate *priv; |
| 1116 |
struct xkb_keymap *keymap; |
| 1117 |
|
| 1118 |
if (!IBUS_IS_WAYLAND_IM (wlim)) { |
| 1119 |
close (fd); |
| 1120 |
g_return_if_reached (); |
| 1121 |
} |
| 1122 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 1123 |
|
| 1124 |
/* wlroots/types/wlr_virtual_keyboard_v1.c:virtual_keyboard_keymap() |
| 1125 |
* calls wl_client_post_no_memory(client) in case mmap() is failed with |
| 1126 |
* `size` = 0. |
| 1127 |
*/ |
| 1128 |
if (priv->version != INPUT_METHOD_V1 && size != 0) { |
| 1129 |
zwp_virtual_keyboard_v1_keymap (priv->seat->virtual_keyboard, |
| 1130 |
format, fd, size); |
| 1131 |
/* wlroots/types/wlr_virtual_keyboard_v1.c:virtual_keyboard_keymap() |
| 1132 |
* sets %TRUE to `has_keymap`. |
| 1133 |
*/ |
| 1134 |
priv->seat->has_keymap = TRUE; |
| 1135 |
} |
| 1136 |
if (priv->keymap && priv->state && priv->state_system) { |
| 1137 |
close (fd); |
| 1138 |
return; |
| 1139 |
} |
| 1140 |
keymap = create_system_xkb_keymap (priv->xkb_context, format, fd, size); |
| 1141 |
if (keymap && !ibus_wayland_im_update_xkb_state (wlim, keymap)) |
| 1142 |
xkb_keymap_unref (keymap); |
| 1143 |
if (priv->state) |
| 1144 |
priv->state_system = xkb_state_ref (priv->state); |
| 1145 |
} |
| 1146 |
|
| 1147 |
|
| 1148 |
static void |
| 1149 |
ibus_wayland_seat_destroy (gpointer data) |
| 1150 |
{ |
| 1151 |
IBusWaylandSeat *seat = (IBusWaylandSeat *)data; |
| 1152 |
g_return_if_fail (seat); |
| 1153 |
g_clear_pointer (&seat->name, g_free); |
| 1154 |
g_clear_pointer (&seat->input_popup_surface, |
| 1155 |
zwp_input_popup_surface_v2_destroy); |
| 1156 |
g_clear_pointer (&seat->virtual_keyboard, zwp_virtual_keyboard_v1_destroy); |
| 1157 |
g_clear_pointer (&seat->keyboard_v2, |
| 1158 |
zwp_input_method_keyboard_grab_v2_destroy); |
| 1159 |
g_clear_pointer (&seat->input_method_v2, zwp_input_method_v2_destroy); |
| 1160 |
g_slice_free (IBusWaylandSeat, seat); |
| 1161 |
} |
| 1162 |
|
| 1163 |
|
| 1164 |
static IBusWaylandSeat * |
| 1165 |
_get_seat_with_name (GPtrArray *seats, |
| 1166 |
uint32_t wl_name, |
| 1167 |
guint *index) |
| 1168 |
{ |
| 1169 |
guint i; |
| 1170 |
g_return_val_if_fail (seats, NULL); |
| 1171 |
for (i = 0; i < seats->len; ++i) { |
| 1172 |
IBusWaylandSeat *seat = g_ptr_array_index (seats, i); |
| 1173 |
if (seat->wl_name == wl_name) { |
| 1174 |
if (index) |
| 1175 |
*index = i; |
| 1176 |
return seat; |
| 1177 |
} |
| 1178 |
} |
| 1179 |
return NULL; |
| 1180 |
} |
| 1181 |
|
| 1182 |
static gboolean |
| 1183 |
ibus_wayland_im_post_key (IBusWaylandIM *wlim, |
| 1184 |
uint32_t key, |
| 1185 |
uint32_t modifiers, |
| 1186 |
uint32_t state, |
| 1187 |
xkb_keysym_t sym, |
| 1188 |
gboolean filtered) |
| 1189 |
{ |
| 1190 |
IBusWaylandIMPrivate *priv; |
| 1191 |
uint32_t code = key + 8; |
| 1192 |
uint32_t ch; |
| 1193 |
|
| 1194 |
g_return_val_if_fail (IBUS_IS_WAYLAND_IM (wlim), FALSE); |
| 1195 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 1196 |
|
| 1197 |
/* ibus_wayland_im_commit_text() does not work without the activation. */ |
| 1198 |
switch (priv->version) { |
| 1199 |
case INPUT_METHOD_V1: |
| 1200 |
if (!priv->context) |
| 1201 |
return FALSE; |
| 1202 |
break; |
| 1203 |
case INPUT_METHOD_V2: |
| 1204 |
if (!priv->seat->active) |
| 1205 |
return FALSE; |
| 1206 |
break; |
| 1207 |
default: |
| 1208 |
g_assert_not_reached (); |
| 1209 |
} |
| 1210 |
if (!priv->state) |
| 1211 |
return FALSE; |
| 1212 |
if (!filtered) { |
| 1213 |
#if 0 |
| 1214 |
/* FIXME: Need to confirm any modifiers should be ignored. */ |
| 1215 |
if (!ibus_accelerator_valid ( |
| 1216 |
sym, |
| 1217 |
modifiers & IBUS_MODIFIER_MASK & ~IBUS_SHIFT_MASK)) { |
| 1218 |
filtered = TRUE; |
| 1219 |
} |
| 1220 |
#else |
| 1221 |
switch (sym) { |
| 1222 |
case IBUS_ISO_Level2_Latch: |
| 1223 |
case IBUS_ISO_Level3_Latch: |
| 1224 |
case IBUS_ISO_Level5_Latch: |
| 1225 |
filtered = TRUE; |
| 1226 |
break; |
| 1227 |
default:; |
| 1228 |
} |
| 1229 |
#endif |
| 1230 |
} |
| 1231 |
if (!filtered && (state != WL_KEYBOARD_KEY_STATE_RELEASED)) { |
| 1232 |
ch = xkb_state_key_get_utf32 (priv->state, code); |
| 1233 |
if (!(modifiers & IBUS_MODIFIER_MASK & ~IBUS_SHIFT_MASK) && |
| 1234 |
ch && ch != '\n' && ch != '\b' && ch != '\r' && ch != '\t' && |
| 1235 |
ch != '\033' && ch != '\x7f') { |
| 1236 |
gchar buff[8] = { 0, }; |
| 1237 |
buff[g_unichar_to_utf8 (ch, buff)] = '\0'; |
| 1238 |
ibus_wayland_im_commit_text (wlim, buff); |
| 1239 |
filtered = TRUE; |
| 1240 |
} |
| 1241 |
} |
| 1242 |
xkb_state_update_key (priv->state, code, |
| 1243 |
(state == WL_KEYBOARD_KEY_STATE_RELEASED) |
| 1244 |
? XKB_KEY_UP : XKB_KEY_DOWN); |
| 1245 |
return filtered; |
| 1246 |
} |
| 1247 |
|
| 1248 |
|
| 1249 |
static void |
| 1250 |
_process_key_event_done (GObject *object, |
| 1251 |
GAsyncResult *res, |
| 1252 |
gpointer user_data) |
| 1253 |
{ |
| 1254 |
IBusInputContext *context = (IBusInputContext *)object; |
| 1255 |
IBusWaylandKeyEvent *event = (IBusWaylandKeyEvent *)user_data; |
| 1256 |
GError *error = NULL; |
| 1257 |
gboolean retval = ibus_input_context_process_key_event_async_finish ( |
| 1258 |
context, |
| 1259 |
res, |
| 1260 |
&error); |
| 1261 |
IBusWaylandIMPrivate *priv = NULL; |
| 1262 |
|
| 1263 |
if (error != NULL) { |
| 1264 |
if (event && event->wlim && IBUS_IS_WAYLAND_IM (event->wlim)) { |
| 1265 |
priv = ibus_wayland_im_get_instance_private (event->wlim); |
| 1266 |
} |
| 1267 |
if (priv && priv->log) { |
| 1268 |
fprintf (priv->log, "Process Key Event failed: %s\n", |
| 1269 |
error->message); |
| 1270 |
fflush (priv->log); |
| 1271 |
} else { |
| 1272 |
g_warning ("Process Key Event failed: %s", error->message); |
| 1273 |
} |
| 1274 |
g_error_free (error); |
| 1275 |
} |
| 1276 |
g_return_if_fail (event); |
| 1277 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (event->wlim)); |
| 1278 |
|
| 1279 |
priv = ibus_wayland_im_get_instance_private (event->wlim); |
| 1280 |
/* Should ignore key events after input_method_deactivate() is called |
| 1281 |
* even if context->ref_count is not 0 yet but priv->ibuscontext is null |
| 1282 |
* because of the async time lag. |
| 1283 |
*/ |
| 1284 |
if (priv->ibuscontext) { |
| 1285 |
retval = ibus_wayland_im_post_key (event->wlim, |
| 1286 |
event->key, |
| 1287 |
event->modifiers, |
| 1288 |
event->state, |
| 1289 |
event->sym, |
| 1290 |
retval); |
| 1291 |
} |
| 1292 |
/* Check retral from ibus_wayland_im_post_key() */ |
| 1293 |
if (priv->ibuscontext && !retval) { |
| 1294 |
ibus_wayland_im_key (event->wlim, |
| 1295 |
event->key_serial, |
| 1296 |
event->time, |
| 1297 |
event->key, |
| 1298 |
event->state); |
| 1299 |
} |
| 1300 |
|
| 1301 |
g_slice_free (IBusWaylandKeyEvent, event); |
| 1302 |
} |
| 1303 |
|
| 1304 |
|
| 1305 |
static void |
| 1306 |
_process_key_event_reply_done (GObject *object, |
| 1307 |
GAsyncResult *res, |
| 1308 |
gpointer user_data) |
| 1309 |
{ |
| 1310 |
IBusInputContext *context = (IBusInputContext *)object; |
| 1311 |
IBusWaylandKeyEvent *event = (IBusWaylandKeyEvent *)user_data; |
| 1312 |
GError *error = NULL; |
| 1313 |
gboolean retval = ibus_input_context_process_key_event_async_finish ( |
| 1314 |
context, |
| 1315 |
res, |
| 1316 |
&error); |
| 1317 |
if (error != NULL) { |
| 1318 |
IBusWaylandIMPrivate *priv = NULL; |
| 1319 |
if (event && event->wlim && IBUS_IS_WAYLAND_IM (event->wlim)) { |
| 1320 |
priv = ibus_wayland_im_get_instance_private (event->wlim); |
| 1321 |
} |
| 1322 |
if (priv && priv->log) { |
| 1323 |
fprintf (priv->log, "Process Key Event failed: %s\n", |
| 1324 |
error->message); |
| 1325 |
fflush (priv->log); |
| 1326 |
} else { |
| 1327 |
g_warning ("Process Key Event failed: %s", error->message); |
| 1328 |
} |
| 1329 |
g_error_free (error); |
| 1330 |
} |
| 1331 |
g_return_if_fail (event); |
| 1332 |
event->retval = retval; |
| 1333 |
event->count = 0; |
| 1334 |
g_source_remove (event->count_cb_id); |
| 1335 |
} |
| 1336 |
|
| 1337 |
|
| 1338 |
static gboolean |
| 1339 |
_process_key_event_count_cb (gpointer user_data) |
| 1340 |
{ |
| 1341 |
IBusWaylandKeyEvent *event = (IBusWaylandKeyEvent *)user_data; |
| 1342 |
g_return_val_if_fail (event, G_SOURCE_REMOVE); |
| 1343 |
if (!event->count) |
| 1344 |
return G_SOURCE_REMOVE; |
| 1345 |
/* Wait for about 10 secs. */ |
| 1346 |
if (event->count++ == 10000) { |
| 1347 |
event->count = 0; |
| 1348 |
return G_SOURCE_REMOVE; |
| 1349 |
} |
| 1350 |
return G_SOURCE_CONTINUE; |
| 1351 |
} |
| 1352 |
|
| 1353 |
|
| 1354 |
static void |
| 1355 |
_process_key_event_sync (IBusWaylandIM *wlim, |
| 1356 |
IBusWaylandKeyEvent *event) |
| 1357 |
{ |
| 1358 |
IBusWaylandIMPrivate *priv; |
| 1359 |
gboolean retval; |
| 1360 |
|
| 1361 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 1362 |
g_assert (event); |
| 1363 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 1364 |
if (!priv->ibuscontext) |
| 1365 |
return; |
| 1366 |
retval = ibus_input_context_process_key_event (priv->ibuscontext, |
| 1367 |
event->sym, |
| 1368 |
event->key, |
| 1369 |
event->modifiers); |
| 1370 |
ibus_input_context_post_process_key_event (priv->ibuscontext); |
| 1371 |
retval = ibus_wayland_im_post_key (wlim, |
| 1372 |
event->key, |
| 1373 |
event->modifiers, |
| 1374 |
event->state, |
| 1375 |
event->sym, |
| 1376 |
retval); |
| 1377 |
if (!retval) { |
| 1378 |
ibus_wayland_im_key (wlim, |
| 1379 |
event->key_serial, |
| 1380 |
event->time, |
| 1381 |
event->key, |
| 1382 |
event->state); |
| 1383 |
} |
| 1384 |
} |
| 1385 |
|
| 1386 |
|
| 1387 |
static void |
| 1388 |
_process_key_event_async (IBusWaylandIM *wlim, |
| 1389 |
IBusWaylandKeyEvent *event) |
| 1390 |
{ |
| 1391 |
IBusWaylandIMPrivate *priv; |
| 1392 |
IBusWaylandKeyEvent *async_event; |
| 1393 |
|
| 1394 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 1395 |
g_assert (event); |
| 1396 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 1397 |
async_event = g_slice_new0 (IBusWaylandKeyEvent); |
| 1398 |
if (!async_event) { |
| 1399 |
if (priv->log) { |
| 1400 |
fprintf (priv->log, "Cannot allocate async data\n"); |
| 1401 |
fflush (priv->log); |
| 1402 |
} else { |
| 1403 |
g_warning ("Cannot allocate async data"); |
| 1404 |
} |
| 1405 |
_process_key_event_sync (wlim, event); |
| 1406 |
return; |
| 1407 |
} |
| 1408 |
async_event->context = priv->context; |
| 1409 |
async_event->key_serial = event->key_serial; |
| 1410 |
async_event->time = event->time; |
| 1411 |
async_event->key = event->key; |
| 1412 |
async_event->modifiers = event->modifiers & ~IBUS_RELEASE_MASK; |
| 1413 |
async_event->state = event->state; |
| 1414 |
async_event->wlim = wlim; |
| 1415 |
ibus_input_context_process_key_event_async (priv->ibuscontext, |
| 1416 |
event->sym, |
| 1417 |
event->key, |
| 1418 |
event->modifiers, |
| 1419 |
-1, |
| 1420 |
NULL, |
| 1421 |
_process_key_event_done, |
| 1422 |
async_event); |
| 1423 |
} |
| 1424 |
|
| 1425 |
|
| 1426 |
static void |
| 1427 |
_process_key_event_hybrid_async (IBusWaylandIM *wlim, |
| 1428 |
IBusWaylandKeyEvent *event) |
| 1429 |
{ |
| 1430 |
IBusWaylandIMPrivate *priv; |
| 1431 |
GSource *source; |
| 1432 |
IBusWaylandKeyEvent *async_event = NULL; |
| 1433 |
|
| 1434 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 1435 |
g_assert (event); |
| 1436 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 1437 |
source = g_timeout_source_new (1); |
| 1438 |
if (source) |
| 1439 |
async_event = g_slice_new0 (IBusWaylandKeyEvent); |
| 1440 |
if (!async_event) { |
| 1441 |
if (priv->log) { |
| 1442 |
fprintf (priv->log, "Cannot wait for the reply of the " |
| 1443 |
"process key event.\n"); |
| 1444 |
fflush (priv->log); |
| 1445 |
} else { |
| 1446 |
g_warning ("Cannot wait for the reply of the process key event."); |
| 1447 |
} |
| 1448 |
_process_key_event_sync (wlim, event); |
| 1449 |
if (source) |
| 1450 |
g_source_destroy (source); |
| 1451 |
return; |
| 1452 |
} |
| 1453 |
async_event->count = 1; |
| 1454 |
async_event->wlim = wlim; |
| 1455 |
g_source_attach (source, NULL); |
| 1456 |
g_source_unref (source); |
| 1457 |
async_event->count_cb_id = g_source_get_id (source); |
| 1458 |
ibus_input_context_process_key_event_async (priv->ibuscontext, |
| 1459 |
event->sym, |
| 1460 |
event->key, |
| 1461 |
event->modifiers, |
| 1462 |
-1, |
| 1463 |
NULL, |
| 1464 |
_process_key_event_reply_done, |
| 1465 |
async_event); |
| 1466 |
g_source_set_callback (source, _process_key_event_count_cb, |
| 1467 |
async_event, NULL); |
| 1468 |
while (async_event->count) |
| 1469 |
g_main_context_iteration (NULL, TRUE); |
| 1470 |
/* #2498 Checking source->ref_count might cause Nautilus hang up |
| 1471 |
*/ |
| 1472 |
if (priv->ibuscontext) { |
| 1473 |
async_event->retval = ibus_wayland_im_post_key (wlim, |
| 1474 |
event->key, |
| 1475 |
event->modifiers, |
| 1476 |
event->state, |
| 1477 |
event->sym, |
| 1478 |
async_event->retval); |
| 1479 |
} |
| 1480 |
if (priv->ibuscontext && !async_event->retval) { |
| 1481 |
ibus_wayland_im_key (wlim, |
| 1482 |
event->key_serial, |
| 1483 |
event->time, |
| 1484 |
event->key, |
| 1485 |
event->state); |
| 1486 |
} |
| 1487 |
g_slice_free (IBusWaylandKeyEvent, async_event); |
| 1488 |
} |
| 1489 |
|
| 1490 |
|
| 1491 |
static gboolean |
| 1492 |
_process_key_event_repeat_rate_cb (gpointer user_data) |
| 1493 |
{ |
| 1494 |
IBusWaylandKeyEvent *event = (IBusWaylandKeyEvent *)user_data; |
| 1495 |
IBusWaylandIM *wlim; |
| 1496 |
IBusWaylandIMPrivate *priv; |
| 1497 |
|
| 1498 |
g_return_val_if_fail (event, G_SOURCE_REMOVE); |
| 1499 |
|
| 1500 |
wlim = event->wlim; |
| 1501 |
if (!IBUS_IS_WAYLAND_IM (wlim)) { |
| 1502 |
g_warning ("Failed BUS_IS_WAYLAND_IM (wlim)"); |
| 1503 |
event->repeat_rate_id = 0; |
| 1504 |
return G_SOURCE_REMOVE; |
| 1505 |
} |
| 1506 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 1507 |
|
| 1508 |
if (!priv->ibuscontext) { |
| 1509 |
event->repeat_rate_id = 0; |
| 1510 |
return G_SOURCE_REMOVE; |
| 1511 |
} |
| 1512 |
if (g_strcmp0 (event->ibus_object_path, |
| 1513 |
g_dbus_proxy_get_object_path ( |
| 1514 |
G_DBUS_PROXY (priv->ibuscontext)))) { |
| 1515 |
event->repeat_rate_id = 0; |
| 1516 |
return G_SOURCE_REMOVE; |
| 1517 |
} |
| 1518 |
switch (_use_sync_mode) { |
| 1519 |
case 1: |
| 1520 |
_process_key_event_sync (wlim, event); |
| 1521 |
break; |
| 1522 |
case 2: |
| 1523 |
_process_key_event_hybrid_async (wlim, event); |
| 1524 |
break; |
| 1525 |
default: |
| 1526 |
_process_key_event_async (wlim, event); |
| 1527 |
} |
| 1528 |
return G_SOURCE_CONTINUE; |
| 1529 |
} |
| 1530 |
|
| 1531 |
|
| 1532 |
static gboolean |
| 1533 |
_process_key_event_repeat_delay_cb (gpointer user_data) |
| 1534 |
{ |
| 1535 |
IBusWaylandKeyEvent *event = (IBusWaylandKeyEvent *)user_data; |
| 1536 |
IBusWaylandIM *wlim; |
| 1537 |
IBusWaylandIMPrivate *priv; |
| 1538 |
GSource *source; |
| 1539 |
|
| 1540 |
g_return_val_if_fail (event, G_SOURCE_REMOVE); |
| 1541 |
|
| 1542 |
wlim = event->wlim; |
| 1543 |
if (!IBUS_IS_WAYLAND_IM (wlim)) { |
| 1544 |
g_warning ("Failed BUS_IS_WAYLAND_IM (wlim)"); |
| 1545 |
event->count_cb_id = 0; |
| 1546 |
return G_SOURCE_REMOVE; |
| 1547 |
} |
| 1548 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 1549 |
|
| 1550 |
/* The key release event was sent to non-Wayland apps likes xterm. */ |
| 1551 |
if (!priv->ibuscontext) { |
| 1552 |
event->count_cb_id = 0; |
| 1553 |
return G_SOURCE_REMOVE; |
| 1554 |
} |
| 1555 |
/* The focus is changed. */ |
| 1556 |
if (g_strcmp0 (event->ibus_object_path, |
| 1557 |
g_dbus_proxy_get_object_path ( |
| 1558 |
G_DBUS_PROXY (priv->ibuscontext)))) { |
| 1559 |
event->count_cb_id = 0; |
| 1560 |
g_clear_pointer (&event->ibus_object_path, g_free); |
| 1561 |
return G_SOURCE_REMOVE; |
| 1562 |
} |
| 1563 |
|
| 1564 |
if (event->count) |
| 1565 |
return G_SOURCE_CONTINUE; |
| 1566 |
|
| 1567 |
event->count = 1; |
| 1568 |
switch (_use_sync_mode) { |
| 1569 |
case 1: |
| 1570 |
_process_key_event_sync (event->wlim, event); |
| 1571 |
break; |
| 1572 |
case 2: |
| 1573 |
_process_key_event_hybrid_async (event->wlim, event); |
| 1574 |
break; |
| 1575 |
default: |
| 1576 |
_process_key_event_async (event->wlim, event); |
| 1577 |
} |
| 1578 |
source = g_timeout_source_new (priv->repeat_rate); |
| 1579 |
g_source_attach (source, NULL); |
| 1580 |
g_source_unref (source); |
| 1581 |
event->repeat_rate_id = g_source_get_id (source); |
| 1582 |
g_source_set_callback (source, _process_key_event_repeat_rate_cb, |
| 1583 |
event, NULL); |
| 1584 |
return G_SOURCE_CONTINUE; |
| 1585 |
} |
| 1586 |
|
| 1587 |
|
| 1588 |
static gboolean |
| 1589 |
key_event_check_repeat (IBusWaylandIM *wlim, |
| 1590 |
IBusWaylandKeyEvent *event) |
| 1591 |
{ |
| 1592 |
IBusWaylandIMPrivate *priv; |
| 1593 |
int i; |
| 1594 |
const guint16 *repeat_ignore = IBUS_COMPOSE_IGNORE_KEYLIST; |
| 1595 |
int repeat_ignore_length = G_N_ELEMENTS (IBUS_COMPOSE_IGNORE_KEYLIST); |
| 1596 |
GSource *source; |
| 1597 |
static IBusWaylandKeyEvent repeating_event = { 0, }; |
| 1598 |
|
| 1599 |
g_return_val_if_fail (IBUS_IS_WAYLAND_IM (wlim), FALSE); |
| 1600 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 1601 |
if G_UNLIKELY (!event->sym) |
| 1602 |
return FALSE; |
| 1603 |
/* FIXME: Should consider if xkb_keymap_key_repeats() is better. */ |
| 1604 |
for (i = 0; i < repeat_ignore_length; i++) { |
| 1605 |
if (event->sym == (uint32_t)repeat_ignore[i]) |
| 1606 |
return FALSE; |
| 1607 |
} |
| 1608 |
|
| 1609 |
if (event->state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
| 1610 |
if (repeating_event.repeat_rate_id) { |
| 1611 |
g_source_remove (repeating_event.repeat_rate_id); |
| 1612 |
repeating_event.repeat_rate_id = 0; |
| 1613 |
} |
| 1614 |
if (repeating_event.count_cb_id) { |
| 1615 |
/* Double KeyPress happen likes Ctrl-a-b */ |
| 1616 |
g_source_remove (repeating_event.count_cb_id); |
| 1617 |
repeating_event.count_cb_id = 0; |
| 1618 |
} |
| 1619 |
g_clear_pointer (&repeating_event.ibus_object_path, g_free); |
| 1620 |
if (!priv->ibuscontext) |
| 1621 |
return FALSE; |
| 1622 |
source = g_timeout_source_new (priv->repeat_delay); |
| 1623 |
g_source_attach (source, NULL); |
| 1624 |
g_source_unref (source); |
| 1625 |
/* Copy the keycode and modifier since Ctrl key has a delay. */ |
| 1626 |
memcpy (&repeating_event, event, sizeof (IBusWaylandKeyEvent)); |
| 1627 |
repeating_event.count_cb_id = g_source_get_id (source); |
| 1628 |
repeating_event.count = 0; |
| 1629 |
repeating_event.ibus_object_path = |
| 1630 |
g_strdup (g_dbus_proxy_get_object_path ( |
| 1631 |
G_DBUS_PROXY (priv->ibuscontext))); |
| 1632 |
g_source_set_callback (source, _process_key_event_repeat_delay_cb, |
| 1633 |
&repeating_event, NULL); |
| 1634 |
} else { |
| 1635 |
if (repeating_event.repeat_rate_id) { |
| 1636 |
g_source_remove (repeating_event.repeat_rate_id); |
| 1637 |
repeating_event.repeat_rate_id = 0; |
| 1638 |
} |
| 1639 |
if (repeating_event.count_cb_id) { |
| 1640 |
g_source_remove (repeating_event.count_cb_id); |
| 1641 |
repeating_event.count_cb_id = 0; |
| 1642 |
} |
| 1643 |
g_clear_pointer (&repeating_event.ibus_object_path, g_free); |
| 1644 |
} |
| 1645 |
return TRUE; |
| 1646 |
} |
| 1647 |
|
| 1648 |
|
| 1649 |
static void |
| 1650 |
input_method_keyboard_key (void *data, |
| 1651 |
struct zwp_keyboard_union *keyboard, |
| 1652 |
uint32_t key_serial, |
| 1653 |
uint32_t time, |
| 1654 |
uint32_t key, |
| 1655 |
uint32_t state) |
| 1656 |
{ |
| 1657 |
IBusWaylandIM *wlim = data; |
| 1658 |
IBusWaylandIMPrivate *priv; |
| 1659 |
IBusWaylandKeyEvent event = { 0, }; |
| 1660 |
uint32_t code; |
| 1661 |
|
| 1662 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 1663 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 1664 |
if (!priv->state) |
| 1665 |
return; |
| 1666 |
|
| 1667 |
if (!priv->ibuscontext) { |
| 1668 |
gboolean retval = ibus_wayland_im_post_key (wlim, |
| 1669 |
key, |
| 1670 |
priv->modifiers, |
| 1671 |
state, |
| 1672 |
0, |
| 1673 |
FALSE); |
| 1674 |
if (!retval) |
| 1675 |
ibus_wayland_im_key (wlim, key_serial, time, key, state); |
| 1676 |
return; |
| 1677 |
} |
| 1678 |
|
| 1679 |
event.key_serial = key_serial; |
| 1680 |
event.time = time; |
| 1681 |
event.key = key; |
| 1682 |
event.state = state; |
| 1683 |
code = key + 8; |
| 1684 |
event.sym = 0; |
| 1685 |
/* xkb_key_get_syms() does not return the capital syms with Shift key. */ |
| 1686 |
if (priv->state_system) |
| 1687 |
event.sym = xkb_state_key_get_one_sym (priv->state_system, code); |
| 1688 |
if (event.sym != IBUS_KEY_Multi_key) |
| 1689 |
event.sym = xkb_state_key_get_one_sym (priv->state, code); |
| 1690 |
event.modifiers = priv->modifiers; |
| 1691 |
if (state == WL_KEYBOARD_KEY_STATE_RELEASED) |
| 1692 |
event.modifiers |= IBUS_RELEASE_MASK; |
| 1693 |
event.wlim = wlim; |
| 1694 |
|
| 1695 |
key_event_check_repeat (wlim, &event); |
| 1696 |
switch (_use_sync_mode) { |
| 1697 |
case 1: |
| 1698 |
return _process_key_event_sync (wlim, &event); |
| 1699 |
case 2: |
| 1700 |
return _process_key_event_hybrid_async (wlim, &event); |
| 1701 |
default: |
| 1702 |
return _process_key_event_async (wlim, &event); |
| 1703 |
} |
| 1704 |
} |
| 1705 |
|
| 1706 |
|
| 1707 |
static void |
| 1708 |
input_method_keyboard_modifiers (void *data, |
| 1709 |
struct zwp_keyboard_union *keyboard, |
| 1710 |
uint32_t key_serial, |
| 1711 |
uint32_t mods_depressed, |
| 1712 |
uint32_t mods_latched, |
| 1713 |
uint32_t mods_locked, |
| 1714 |
uint32_t group) |
| 1715 |
{ |
| 1716 |
IBusWaylandIM *wlim = data; |
| 1717 |
IBusWaylandIMPrivate *priv; |
| 1718 |
xkb_mod_mask_t mask; |
| 1719 |
|
| 1720 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 1721 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 1722 |
xkb_state_update_mask (priv->state, mods_depressed, |
| 1723 |
mods_latched, mods_locked, 0, 0, group); |
| 1724 |
mask = xkb_state_serialize_mods (priv->state, |
| 1725 |
XKB_STATE_DEPRESSED | |
| 1726 |
XKB_STATE_LATCHED); |
| 1727 |
|
| 1728 |
priv->modifiers = 0; |
| 1729 |
if (mask & priv->shift_mask) |
| 1730 |
priv->modifiers |= IBUS_SHIFT_MASK; |
| 1731 |
if (mask & priv->lock_mask) |
| 1732 |
priv->modifiers |= IBUS_LOCK_MASK; |
| 1733 |
if (mask & priv->control_mask) |
| 1734 |
priv->modifiers |= IBUS_CONTROL_MASK; |
| 1735 |
if (mask & priv->mod1_mask) |
| 1736 |
priv->modifiers |= IBUS_MOD1_MASK; |
| 1737 |
if (mask & priv->mod2_mask) |
| 1738 |
priv->modifiers |= IBUS_MOD2_MASK; |
| 1739 |
if (mask & priv->mod3_mask) |
| 1740 |
priv->modifiers |= IBUS_MOD3_MASK; |
| 1741 |
if (mask & priv->mod4_mask) |
| 1742 |
priv->modifiers |= IBUS_MOD4_MASK; |
| 1743 |
if (mask & priv->mod5_mask) |
| 1744 |
priv->modifiers |= IBUS_MOD5_MASK; |
| 1745 |
if (mask & priv->super_mask) |
| 1746 |
priv->modifiers |= IBUS_SUPER_MASK; |
| 1747 |
if (mask & priv->hyper_mask) |
| 1748 |
priv->modifiers |= IBUS_HYPER_MASK; |
| 1749 |
if (mask & priv->meta_mask) |
| 1750 |
priv->modifiers |= IBUS_META_MASK; |
| 1751 |
|
| 1752 |
switch (priv->version) { |
| 1753 |
case INPUT_METHOD_V1: |
| 1754 |
zwp_input_method_context_v1_modifiers (priv->context, key_serial, |
| 1755 |
mods_depressed, mods_latched, |
| 1756 |
mods_locked, group); |
| 1757 |
break; |
| 1758 |
case INPUT_METHOD_V2: |
| 1759 |
/* wlroots/types/wlr_virtual_keyboard_v1.c:virtual_keyboard_modifiers() |
| 1760 |
* returns "Cannot send a modifier state before defining a keymap" |
| 1761 |
* if `has_keymap` is %FALSE. |
| 1762 |
*/ |
| 1763 |
if (priv->seat->has_keymap) { |
| 1764 |
zwp_virtual_keyboard_v1_modifiers (priv->seat->virtual_keyboard, |
| 1765 |
mods_depressed, mods_latched, |
| 1766 |
mods_locked, group); |
| 1767 |
} |
| 1768 |
break; |
| 1769 |
default: |
| 1770 |
g_assert_not_reached (); |
| 1771 |
} |
| 1772 |
} |
| 1773 |
|
| 1774 |
|
| 1775 |
static void |
| 1776 |
input_method_keyboard_repeat_info (void *data, |
| 1777 |
struct zwp_keyboard_union *keyboard, |
| 1778 |
int32_t rate, |
| 1779 |
int32_t delay) |
| 1780 |
{ |
| 1781 |
IBusWaylandIM *wlim = data; |
| 1782 |
IBusWaylandIMPrivate *priv; |
| 1783 |
|
| 1784 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 1785 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 1786 |
priv->repeat_rate = rate; |
| 1787 |
priv->repeat_delay = delay; |
| 1788 |
if (priv->verbose) { |
| 1789 |
fprintf (priv->log, "keyboard repeat info rate %d delay %d\n", |
| 1790 |
rate, delay); |
| 1791 |
fflush (priv->log); |
| 1792 |
} |
| 1793 |
} |
| 1794 |
|
| 1795 |
|
| 1796 |
static void |
| 1797 |
input_method_keyboard_keymap_v1 (void *data, |
| 1798 |
struct wl_keyboard *keyboard_v1, |
| 1799 |
uint32_t format, |
| 1800 |
int32_t fd, |
| 1801 |
uint32_t size) |
| 1802 |
{ |
| 1803 |
struct zwp_keyboard_union keyboard; |
| 1804 |
keyboard.u.keyboard_v1 = keyboard_v1; |
| 1805 |
input_method_keyboard_keymap (data, &keyboard, format, fd, size); |
| 1806 |
} |
| 1807 |
|
| 1808 |
|
| 1809 |
static void |
| 1810 |
input_method_keyboard_key_v1 (void *data, |
| 1811 |
struct wl_keyboard *keyboard_v1, |
| 1812 |
uint32_t key_serial, |
| 1813 |
uint32_t time, |
| 1814 |
uint32_t key, |
| 1815 |
uint32_t state) |
| 1816 |
{ |
| 1817 |
struct zwp_keyboard_union keyboard; |
| 1818 |
keyboard.u.keyboard_v1 = keyboard_v1; |
| 1819 |
input_method_keyboard_key (data, &keyboard, key_serial, time, key, state); |
| 1820 |
} |
| 1821 |
|
| 1822 |
|
| 1823 |
static void |
| 1824 |
input_method_keyboard_modifiers_v1 (void *data, |
| 1825 |
struct wl_keyboard *keyboard_v1, |
| 1826 |
uint32_t key_serial, |
| 1827 |
uint32_t mods_depressed, |
| 1828 |
uint32_t mods_latched, |
| 1829 |
uint32_t mods_locked, |
| 1830 |
uint32_t group) |
| 1831 |
{ |
| 1832 |
struct zwp_keyboard_union keyboard; |
| 1833 |
keyboard.u.keyboard_v1 = keyboard_v1; |
| 1834 |
input_method_keyboard_modifiers (data, |
| 1835 |
&keyboard, |
| 1836 |
key_serial, |
| 1837 |
mods_depressed, |
| 1838 |
mods_latched, |
| 1839 |
mods_locked, |
| 1840 |
group); |
| 1841 |
} |
| 1842 |
|
| 1843 |
|
| 1844 |
static void |
| 1845 |
input_method_keyboard_repeat_info_v1 (void *data, |
| 1846 |
struct wl_keyboard *keyboard_v1, |
| 1847 |
int32_t rate, |
| 1848 |
int32_t delay) |
| 1849 |
{ |
| 1850 |
struct zwp_keyboard_union keyboard; |
| 1851 |
keyboard.u.keyboard_v1 = keyboard_v1; |
| 1852 |
input_method_keyboard_repeat_info (data, &keyboard, rate, delay); |
| 1853 |
} |
| 1854 |
|
| 1855 |
|
| 1856 |
static void |
| 1857 |
input_method_keyboard_keymap_v2 (void *data, |
| 1858 |
struct zwp_input_method_keyboard_grab_v2 |
| 1859 |
*keyboard_v2, |
| 1860 |
uint32_t format, |
| 1861 |
int32_t fd, |
| 1862 |
uint32_t size) |
| 1863 |
{ |
| 1864 |
struct zwp_keyboard_union keyboard; |
| 1865 |
keyboard.u.keyboard_v2 = keyboard_v2; |
| 1866 |
input_method_keyboard_keymap (data, &keyboard, format, fd, size); |
| 1867 |
} |
| 1868 |
|
| 1869 |
|
| 1870 |
static void |
| 1871 |
input_method_keyboard_key_v2 (void *data, |
| 1872 |
struct zwp_input_method_keyboard_grab_v2 |
| 1873 |
*keyboard_v2, |
| 1874 |
uint32_t key_serial, |
| 1875 |
uint32_t time, |
| 1876 |
uint32_t key, |
| 1877 |
uint32_t state) |
| 1878 |
{ |
| 1879 |
struct zwp_keyboard_union keyboard; |
| 1880 |
keyboard.u.keyboard_v2 = keyboard_v2; |
| 1881 |
input_method_keyboard_key (data, &keyboard, key_serial, time, key, state); |
| 1882 |
} |
| 1883 |
|
| 1884 |
|
| 1885 |
static void |
| 1886 |
input_method_keyboard_modifiers_v2 (void *data, |
| 1887 |
struct zwp_input_method_keyboard_grab_v2 |
| 1888 |
*keyboard_v2, |
| 1889 |
uint32_t key_serial, |
| 1890 |
uint32_t mods_depressed, |
| 1891 |
uint32_t mods_latched, |
| 1892 |
uint32_t mods_locked, |
| 1893 |
uint32_t group) |
| 1894 |
{ |
| 1895 |
struct zwp_keyboard_union keyboard; |
| 1896 |
keyboard.u.keyboard_v2 = keyboard_v2; |
| 1897 |
input_method_keyboard_modifiers (data, |
| 1898 |
&keyboard, |
| 1899 |
key_serial, |
| 1900 |
mods_depressed, |
| 1901 |
mods_latched, |
| 1902 |
mods_locked, |
| 1903 |
group); |
| 1904 |
} |
| 1905 |
|
| 1906 |
|
| 1907 |
static void |
| 1908 |
input_method_keyboard_repeat_info_v2 (void *data, |
| 1909 |
struct zwp_input_method_keyboard_grab_v2 |
| 1910 |
*keyboard_v2, |
| 1911 |
int32_t rate, |
| 1912 |
int32_t delay) |
| 1913 |
{ |
| 1914 |
struct zwp_keyboard_union keyboard; |
| 1915 |
keyboard.u.keyboard_v2 = keyboard_v2; |
| 1916 |
input_method_keyboard_repeat_info (data, &keyboard, rate, delay); |
| 1917 |
} |
| 1918 |
|
| 1919 |
|
| 1920 |
static const struct wl_keyboard_listener keyboard_listener_v1 = { |
| 1921 |
.keymap = input_method_keyboard_keymap_v1, |
| 1922 |
.enter = NULL, /* enter */ |
| 1923 |
.leave = NULL, /* leave */ |
| 1924 |
.key = input_method_keyboard_key_v1, |
| 1925 |
.modifiers = input_method_keyboard_modifiers_v1, |
| 1926 |
#ifdef WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION |
| 1927 |
.repeat_info = input_method_keyboard_repeat_info_v1 |
| 1928 |
#endif |
| 1929 |
}; |
| 1930 |
|
| 1931 |
|
| 1932 |
static const struct zwp_input_method_keyboard_grab_v2_listener |
| 1933 |
keyboard_listener_v2 = { |
| 1934 |
.keymap = input_method_keyboard_keymap_v2, |
| 1935 |
.key = input_method_keyboard_key_v2, |
| 1936 |
.modifiers = input_method_keyboard_modifiers_v2, |
| 1937 |
.repeat_info = input_method_keyboard_repeat_info_v2, |
| 1938 |
}; |
| 1939 |
|
| 1940 |
|
| 1941 |
static void |
| 1942 |
_create_input_context_done (GObject *object, |
| 1943 |
GAsyncResult *res, |
| 1944 |
gpointer user_data) |
| 1945 |
{ |
| 1946 |
IBusWaylandIM *wlim = (IBusWaylandIM *) user_data; |
| 1947 |
IBusWaylandIMPrivate *priv; |
| 1948 |
GError *error = NULL; |
| 1949 |
IBusInputContext *context; |
| 1950 |
|
| 1951 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 1952 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 1953 |
context = ibus_bus_create_input_context_async_finish ( |
| 1954 |
priv->ibusbus, res, &error); |
| 1955 |
if (priv->cancellable != NULL) |
| 1956 |
g_clear_object (&priv->cancellable); |
| 1957 |
|
| 1958 |
if (context == NULL) { |
| 1959 |
g_warning ("Create input context failed: %s.", error->message); |
| 1960 |
g_error_free (error); |
| 1961 |
} |
| 1962 |
else { |
| 1963 |
guint32 capabilities = IBUS_CAP_FOCUS | IBUS_CAP_PREEDIT_TEXT; |
| 1964 |
priv->ibuscontext = context; |
| 1965 |
|
| 1966 |
g_signal_connect (priv->ibuscontext, "commit-text", |
| 1967 |
G_CALLBACK (_context_commit_text_cb), |
| 1968 |
wlim); |
| 1969 |
g_signal_connect (priv->ibuscontext, "forward-key-event", |
| 1970 |
G_CALLBACK (_context_forward_key_event_cb), |
| 1971 |
wlim); |
| 1972 |
|
| 1973 |
g_signal_connect (priv->ibuscontext, "update-preedit-text-with-mode", |
| 1974 |
G_CALLBACK (_context_update_preedit_text_cb), |
| 1975 |
wlim); |
| 1976 |
g_signal_connect (priv->ibuscontext, "show-preedit-text", |
| 1977 |
G_CALLBACK (_context_show_preedit_text_cb), |
| 1978 |
wlim); |
| 1979 |
g_signal_connect (priv->ibuscontext, "hide-preedit-text", |
| 1980 |
G_CALLBACK (_context_hide_preedit_text_cb), |
| 1981 |
wlim); |
| 1982 |
|
| 1983 |
#ifdef ENABLE_SURROUNDING |
| 1984 |
g_signal_connect (priv->ibuscontext, "delete-surrounding-text", |
| 1985 |
G_CALLBACK (_context_delete_surrounding_text_cb), |
| 1986 |
wlim); |
| 1987 |
|
| 1988 |
capabilities |= IBUS_CAP_SURROUNDING_TEXT; |
| 1989 |
#endif |
| 1990 |
if (_use_sync_mode == 1) |
| 1991 |
capabilities |= IBUS_CAP_SYNC_PROCESS_KEY_V2; |
| 1992 |
ibus_input_context_set_capabilities (priv->ibuscontext, |
| 1993 |
capabilities); |
| 1994 |
ibus_input_context_set_client_commit_preedit (priv->ibuscontext, TRUE); |
| 1995 |
ibus_input_context_set_preedit_format (priv->ibuscontext, |
| 1996 |
IBUS_PREEDIT_FORMAT_HINT); |
| 1997 |
if (_use_sync_mode == 1) { |
| 1998 |
ibus_input_context_set_post_process_key_event (priv->ibuscontext, |
| 1999 |
TRUE); |
| 2000 |
} |
| 2001 |
ibus_input_context_focus_in (priv->ibuscontext); |
| 2002 |
g_signal_emit (wlim, |
| 2003 |
wayland_im_signals[IBUS_FOCUS_IN], |
| 2004 |
0, |
| 2005 |
g_dbus_proxy_get_object_path ( |
| 2006 |
G_DBUS_PROXY (priv->ibuscontext))); |
| 2007 |
ibus_input_context_set_content_type (priv->ibuscontext, |
| 2008 |
priv->ibus_purpose, |
| 2009 |
priv->ibus_hints); |
| 2010 |
} |
| 2011 |
} |
| 2012 |
|
| 2013 |
|
| 2014 |
static void |
| 2015 |
input_method_activate (void *data, |
| 2016 |
struct zwp_input_method_union *input_method, |
| 2017 |
struct zwp_input_method_context_v1 *context) |
| 2018 |
{ |
| 2019 |
IBusWaylandIM *wlim = data; |
| 2020 |
IBusWaylandIMPrivate *priv; |
| 2021 |
|
| 2022 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 2023 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 2024 |
if (priv->context || priv->ibuscontext) |
| 2025 |
input_method_deactivate (data, input_method, context); |
| 2026 |
|
| 2027 |
priv->context = context; |
| 2028 |
if (context) |
| 2029 |
priv->im_serial = 0; |
| 2030 |
|
| 2031 |
switch (priv->version) { |
| 2032 |
case INPUT_METHOD_V1: |
| 2033 |
zwp_input_method_context_v1_add_listener (context, |
| 2034 |
&context_listener_v1, |
| 2035 |
wlim); |
| 2036 |
priv->keyboard_v1 = zwp_input_method_context_v1_grab_keyboard (context); |
| 2037 |
wl_keyboard_add_listener (priv->keyboard_v1, |
| 2038 |
&keyboard_listener_v1, |
| 2039 |
wlim); |
| 2040 |
break; |
| 2041 |
case INPUT_METHOD_V2: |
| 2042 |
priv->seat->virtual_keyboard = |
| 2043 |
zwp_virtual_keyboard_manager_v1_create_virtual_keyboard ( |
| 2044 |
_virtual_keyboard_manager, priv->seat->seat); |
| 2045 |
/* Regenerating `virtual_keyboard` causes `size` = 0 in |
| 2046 |
* input_method_keyboard_keymap() with focus changes in Sway session. |
| 2047 |
*/ |
| 2048 |
priv->seat->has_keymap = FALSE; |
| 2049 |
priv->seat->keyboard_v2 = zwp_input_method_v2_grab_keyboard ( |
| 2050 |
input_method->u.input_method_v2); |
| 2051 |
zwp_input_method_keyboard_grab_v2_add_listener ( |
| 2052 |
priv->seat->keyboard_v2, |
| 2053 |
&keyboard_listener_v2, |
| 2054 |
wlim); |
| 2055 |
break; |
| 2056 |
default: |
| 2057 |
g_assert_not_reached (); |
| 2058 |
} |
| 2059 |
|
| 2060 |
priv->ibus_hints = IBUS_INPUT_HINT_NONE; |
| 2061 |
priv->ibus_purpose = IBUS_INPUT_PURPOSE_FREE_FORM; |
| 2062 |
|
| 2063 |
g_assert (!priv->ibuscontext); |
| 2064 |
|
| 2065 |
priv->cancellable = g_cancellable_new (); |
| 2066 |
ibus_bus_create_input_context_async (priv->ibusbus, |
| 2067 |
"wayland", |
| 2068 |
-1, |
| 2069 |
priv->cancellable, |
| 2070 |
_create_input_context_done, |
| 2071 |
wlim); |
| 2072 |
} |
| 2073 |
|
| 2074 |
|
| 2075 |
static void |
| 2076 |
input_method_deactivate (void *data, |
| 2077 |
struct zwp_input_method_union *input_method, |
| 2078 |
struct zwp_input_method_context_v1 *context) |
| 2079 |
{ |
| 2080 |
IBusWaylandIM *wlim = data; |
| 2081 |
IBusWaylandIMPrivate *priv; |
| 2082 |
|
| 2083 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 2084 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 2085 |
if (priv->cancellable) { |
| 2086 |
/* Cancel any ongoing create input context request. */ |
| 2087 |
g_cancellable_cancel (priv->cancellable); |
| 2088 |
g_clear_object (&priv->cancellable); |
| 2089 |
} |
| 2090 |
|
| 2091 |
|
| 2092 |
if (priv->ibuscontext) { |
| 2093 |
gchar *object_path = g_strdup (g_dbus_proxy_get_object_path ( |
| 2094 |
G_DBUS_PROXY (priv->ibuscontext))); |
| 2095 |
ibus_input_context_focus_out (priv->ibuscontext); |
| 2096 |
g_signal_handlers_disconnect_by_func ( |
| 2097 |
priv->ibuscontext, |
| 2098 |
G_CALLBACK (_context_commit_text_cb), |
| 2099 |
wlim); |
| 2100 |
g_signal_handlers_disconnect_by_func ( |
| 2101 |
priv->ibuscontext, |
| 2102 |
G_CALLBACK (_context_forward_key_event_cb), |
| 2103 |
wlim); |
| 2104 |
g_signal_handlers_disconnect_by_func ( |
| 2105 |
priv->ibuscontext, |
| 2106 |
G_CALLBACK (_context_update_preedit_text_cb), |
| 2107 |
wlim); |
| 2108 |
g_signal_handlers_disconnect_by_func ( |
| 2109 |
priv->ibuscontext, |
| 2110 |
G_CALLBACK (_context_show_preedit_text_cb), |
| 2111 |
wlim); |
| 2112 |
g_signal_handlers_disconnect_by_func ( |
| 2113 |
priv->ibuscontext, |
| 2114 |
G_CALLBACK (_context_hide_preedit_text_cb), |
| 2115 |
wlim); |
| 2116 |
#ifdef ENABLE_SURROUNDING |
| 2117 |
g_signal_handlers_disconnect_by_func ( |
| 2118 |
priv->ibuscontext, |
| 2119 |
G_CALLBACK (_context_delete_surrounding_text_cb), |
| 2120 |
wlim); |
| 2121 |
#endif |
| 2122 |
g_clear_object (&priv->ibuscontext); |
| 2123 |
|
| 2124 |
g_signal_emit (wlim, |
| 2125 |
wayland_im_signals[IBUS_FOCUS_OUT], |
| 2126 |
0, |
| 2127 |
object_path); |
| 2128 |
g_free (object_path); |
| 2129 |
} |
| 2130 |
|
| 2131 |
if (priv->preedit_text) |
| 2132 |
g_clear_object (&priv->preedit_text); |
| 2133 |
#if ENABLE_SURROUNDING |
| 2134 |
if (priv->surrounding_text) |
| 2135 |
g_clear_object (&priv->surrounding_text); |
| 2136 |
#endif |
| 2137 |
|
| 2138 |
switch (priv->version) { |
| 2139 |
case INPUT_METHOD_V1: |
| 2140 |
if (priv->context) { |
| 2141 |
g_clear_pointer (&priv->context, |
| 2142 |
zwp_input_method_context_v1_destroy); |
| 2143 |
} |
| 2144 |
break; |
| 2145 |
case INPUT_METHOD_V2: |
| 2146 |
if (priv->seat->keyboard_v2) { |
| 2147 |
g_clear_pointer (&priv->seat->keyboard_v2, |
| 2148 |
zwp_input_method_keyboard_grab_v2_release); |
| 2149 |
} |
| 2150 |
if (priv->seat->virtual_keyboard) { |
| 2151 |
g_clear_pointer (&priv->seat->virtual_keyboard, |
| 2152 |
zwp_virtual_keyboard_v1_destroy); |
| 2153 |
} |
| 2154 |
break; |
| 2155 |
default: |
| 2156 |
g_assert_not_reached (); |
| 2157 |
} |
| 2158 |
} |
| 2159 |
|
| 2160 |
|
| 2161 |
static void |
| 2162 |
input_method_activate_v1 (void *data, |
| 2163 |
struct zwp_input_method_v1 *input_method_v1, |
| 2164 |
struct zwp_input_method_context_v1 *context) |
| 2165 |
{ |
| 2166 |
struct zwp_input_method_union input_method; |
| 2167 |
input_method.u.input_method_v1 = input_method_v1; |
| 2168 |
input_method_activate (data, &input_method, context); |
| 2169 |
} |
| 2170 |
|
| 2171 |
|
| 2172 |
static void |
| 2173 |
input_method_deactivate_v1 (void *data, |
| 2174 |
struct zwp_input_method_v1 *input_method_v1, |
| 2175 |
struct zwp_input_method_context_v1 *context) |
| 2176 |
{ |
| 2177 |
struct zwp_input_method_union input_method; |
| 2178 |
input_method.u.input_method_v1 = input_method_v1; |
| 2179 |
input_method_deactivate (data, &input_method, context); |
| 2180 |
} |
| 2181 |
|
| 2182 |
|
| 2183 |
static void |
| 2184 |
input_method_activate_v2 (void *data, |
| 2185 |
struct zwp_input_method_v2 *input_method_v2) |
| 2186 |
{ |
| 2187 |
IBusWaylandIM *wlim = data; |
| 2188 |
IBusWaylandIMPrivate *priv; |
| 2189 |
|
| 2190 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 2191 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 2192 |
priv->seat->pending_activate = TRUE; |
| 2193 |
} |
| 2194 |
|
| 2195 |
|
| 2196 |
static void |
| 2197 |
input_method_deactivate_v2 (void *data, |
| 2198 |
struct zwp_input_method_v2 *input_method_v2) |
| 2199 |
{ |
| 2200 |
IBusWaylandIM *wlim = data; |
| 2201 |
IBusWaylandIMPrivate *priv; |
| 2202 |
|
| 2203 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 2204 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 2205 |
priv->seat->pending_deactivate = TRUE; |
| 2206 |
} |
| 2207 |
|
| 2208 |
|
| 2209 |
static void |
| 2210 |
input_method_surrounding_text_v2 (void *data, |
| 2211 |
struct zwp_input_method_v2 *input_method_v2, |
| 2212 |
const char *text, |
| 2213 |
uint32_t cursor, |
| 2214 |
uint32_t anchor) |
| 2215 |
{ |
| 2216 |
struct zwp_input_method_context_union context; |
| 2217 |
context.u.input_method_v2 = input_method_v2; |
| 2218 |
handle_surrounding_text (data, &context, text, cursor, anchor); |
| 2219 |
} |
| 2220 |
|
| 2221 |
|
| 2222 |
static void |
| 2223 |
input_method_text_change_cause_v2 (void *data, |
| 2224 |
struct zwp_input_method_v2 *input_method_v2, |
| 2225 |
uint32_t cause) |
| 2226 |
{ |
| 2227 |
} |
| 2228 |
|
| 2229 |
|
| 2230 |
static void |
| 2231 |
input_method_content_type_v2 (void *data, |
| 2232 |
struct zwp_input_method_v2 *input_method_v2, |
| 2233 |
uint32_t hints, |
| 2234 |
uint32_t purpose) |
| 2235 |
{ |
| 2236 |
IBusWaylandIM *wlim = data; |
| 2237 |
IBusWaylandIMPrivate *priv; |
| 2238 |
IBusInputHints ibus_hints = IBUS_INPUT_HINT_NONE; |
| 2239 |
IBusInputPurpose ibus_purpose = IBUS_INPUT_PURPOSE_FREE_FORM; |
| 2240 |
|
| 2241 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 2242 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 2243 |
|
| 2244 |
if (hints & ZWP_TEXT_INPUT_V3_CONTENT_HINT_COMPLETION) |
| 2245 |
ibus_hints |= IBUS_INPUT_HINT_WORD_COMPLETION; |
| 2246 |
if (hints & ZWP_TEXT_INPUT_V3_CONTENT_HINT_SPELLCHECK) |
| 2247 |
ibus_hints |= IBUS_INPUT_HINT_SPELLCHECK; |
| 2248 |
if (hints & ZWP_TEXT_INPUT_V3_CONTENT_HINT_AUTO_CAPITALIZATION) |
| 2249 |
ibus_hints |= IBUS_INPUT_HINT_UPPERCASE_SENTENCES; |
| 2250 |
if (hints & ZWP_TEXT_INPUT_V3_CONTENT_HINT_LOWERCASE) |
| 2251 |
ibus_hints |= IBUS_INPUT_HINT_LOWERCASE; |
| 2252 |
if (hints & ZWP_TEXT_INPUT_V3_CONTENT_HINT_UPPERCASE) |
| 2253 |
ibus_hints |= IBUS_INPUT_HINT_UPPERCASE_CHARS; |
| 2254 |
if (hints & ZWP_TEXT_INPUT_V3_CONTENT_HINT_TITLECASE) |
| 2255 |
ibus_hints |= IBUS_INPUT_HINT_UPPERCASE_WORDS; |
| 2256 |
if (hints & ZWP_TEXT_INPUT_V3_CONTENT_HINT_HIDDEN_TEXT) |
| 2257 |
ibus_hints |= IBUS_INPUT_HINT_HIDDEN_TEXT; |
| 2258 |
if (hints & ZWP_TEXT_INPUT_V3_CONTENT_HINT_SENSITIVE_DATA) |
| 2259 |
ibus_hints |= IBUS_INPUT_HINT_PRIVATE; |
| 2260 |
if (hints & ZWP_TEXT_INPUT_V3_CONTENT_HINT_LATIN) |
| 2261 |
ibus_hints |= IBUS_INPUT_HINT_LATIN; |
| 2262 |
if (hints & ZWP_TEXT_INPUT_V3_CONTENT_HINT_MULTILINE) |
| 2263 |
ibus_hints |= IBUS_INPUT_HINT_MULTILINE; |
| 2264 |
|
| 2265 |
switch (purpose) { |
| 2266 |
case ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL: |
| 2267 |
ibus_purpose = IBUS_INPUT_PURPOSE_FREE_FORM; |
| 2268 |
break; |
| 2269 |
case ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_ALPHA: |
| 2270 |
ibus_purpose = IBUS_INPUT_PURPOSE_ALPHA; |
| 2271 |
break; |
| 2272 |
case ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_DIGITS: |
| 2273 |
ibus_purpose = IBUS_INPUT_PURPOSE_DIGITS; |
| 2274 |
break; |
| 2275 |
case ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NUMBER: |
| 2276 |
ibus_purpose = IBUS_INPUT_PURPOSE_NUMBER; |
| 2277 |
break; |
| 2278 |
case ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_PHONE: |
| 2279 |
ibus_purpose = IBUS_INPUT_PURPOSE_PHONE; |
| 2280 |
break; |
| 2281 |
case ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_URL: |
| 2282 |
ibus_purpose = IBUS_INPUT_PURPOSE_URL; |
| 2283 |
break; |
| 2284 |
case ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_EMAIL: |
| 2285 |
ibus_purpose = IBUS_INPUT_PURPOSE_EMAIL; |
| 2286 |
break; |
| 2287 |
case ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NAME: |
| 2288 |
ibus_purpose = IBUS_INPUT_PURPOSE_NAME; |
| 2289 |
break; |
| 2290 |
case ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_PASSWORD: |
| 2291 |
ibus_purpose = IBUS_INPUT_PURPOSE_PASSWORD; |
| 2292 |
break; |
| 2293 |
case ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_PIN: |
| 2294 |
ibus_purpose = IBUS_INPUT_PURPOSE_PIN; |
| 2295 |
break; |
| 2296 |
case ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_DATE: |
| 2297 |
ibus_purpose = IBUS_INPUT_PURPOSE_DATE; |
| 2298 |
break; |
| 2299 |
case ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_TIME: |
| 2300 |
ibus_purpose = IBUS_INPUT_PURPOSE_TIME; |
| 2301 |
break; |
| 2302 |
case ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_DATETIME: |
| 2303 |
ibus_purpose = IBUS_INPUT_PURPOSE_DATETIME; |
| 2304 |
break; |
| 2305 |
case ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_TERMINAL: |
| 2306 |
ibus_purpose = IBUS_INPUT_PURPOSE_TERMINAL; |
| 2307 |
break; |
| 2308 |
default: |
| 2309 |
g_warning ("Wrong purpose in the input-method context: %d", purpose); |
| 2310 |
} |
| 2311 |
|
| 2312 |
priv->ibus_hints = ibus_hints; |
| 2313 |
priv->ibus_purpose = ibus_purpose; |
| 2314 |
|
| 2315 |
/* Update priv->ibus_[hints|purpose] after _create_input_context_done() |
| 2316 |
* is called. |
| 2317 |
*/ |
| 2318 |
if (G_UNLIKELY (priv->ibuscontext)) { |
| 2319 |
ibus_input_context_set_content_type (priv->ibuscontext, |
| 2320 |
ibus_purpose, |
| 2321 |
ibus_hints); |
| 2322 |
} |
| 2323 |
} |
| 2324 |
|
| 2325 |
|
| 2326 |
static void |
| 2327 |
input_method_done_v2 (void *data, |
| 2328 |
struct zwp_input_method_v2 *input_method_v2) |
| 2329 |
{ |
| 2330 |
IBusWaylandIM *wlim = data; |
| 2331 |
IBusWaylandIMPrivate *priv; |
| 2332 |
struct zwp_input_method_union input_method; |
| 2333 |
|
| 2334 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 2335 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 2336 |
priv->im_serial++; |
| 2337 |
input_method.u.input_method_v2 = input_method_v2; |
| 2338 |
|
| 2339 |
/* Ghostty requires to receive the 'im_preedit_end' signal with |
| 2340 |
* zwp_input_method_v2_set_preedit_string() to recover the 'im_commit' |
| 2341 |
* signal but GtkIMContextWayland::text_input_preedit_apply() can emit |
| 2342 |
* the 'preedit-end' signal in case that |
| 2343 |
* GtkIMContextWayland->current_preedit.text is %NULL and it happens |
| 2344 |
* if zwp_text_input_v3_listener.done is emitted. |
| 2345 |
* To make sure zwp_text_input_v3_listener.done, |
| 2346 |
* zwp_input_method_v2_commit() is called after the im_serial is bumped. |
| 2347 |
*/ |
| 2348 |
if (priv->hiding_preedit_text && |
| 2349 |
!priv->seat->pending_activate && |
| 2350 |
priv->seat->active) { |
| 2351 |
zwp_input_method_v2_commit (priv->seat->input_method_v2, |
| 2352 |
priv->im_serial); |
| 2353 |
priv->hiding_preedit_text = FALSE; |
| 2354 |
} |
| 2355 |
|
| 2356 |
if (priv->seat->pending_activate && !priv->seat->active) { |
| 2357 |
priv->seat->active = TRUE; |
| 2358 |
input_method_activate (data, &input_method, NULL); |
| 2359 |
} else if (priv->seat->pending_deactivate && priv->seat->active) { |
| 2360 |
priv->seat->active = FALSE; |
| 2361 |
input_method_deactivate (data, &input_method, NULL); |
| 2362 |
} |
| 2363 |
priv->seat->pending_activate = FALSE; |
| 2364 |
priv->seat->pending_deactivate = FALSE; |
| 2365 |
} |
| 2366 |
|
| 2367 |
|
| 2368 |
static void |
| 2369 |
input_method_unavailable_v2 (void *data, |
| 2370 |
struct zwp_input_method_v2 *input_method_v2) |
| 2371 |
{ |
| 2372 |
} |
| 2373 |
|
| 2374 |
|
| 2375 |
static const struct zwp_input_method_v1_listener input_method_listener_v1 = { |
| 2376 |
.activate = input_method_activate_v1, |
| 2377 |
.deactivate = input_method_deactivate_v1 |
| 2378 |
}; |
| 2379 |
|
| 2380 |
|
| 2381 |
static const struct zwp_input_method_v2_listener input_method_listener_v2 = { |
| 2382 |
.activate = input_method_activate_v2, |
| 2383 |
.deactivate = input_method_deactivate_v2, |
| 2384 |
.surrounding_text= input_method_surrounding_text_v2, |
| 2385 |
.text_change_cause = input_method_text_change_cause_v2, |
| 2386 |
.content_type = input_method_content_type_v2, |
| 2387 |
.done = input_method_done_v2, |
| 2388 |
.unavailable = input_method_unavailable_v2 |
| 2389 |
}; |
| 2390 |
|
| 2391 |
|
| 2392 |
static void |
| 2393 |
seat_handle_capabilities(void *data, struct wl_seat *wl_seat, |
| 2394 |
enum wl_seat_capability caps) |
| 2395 |
{ |
| 2396 |
} |
| 2397 |
|
| 2398 |
static void |
| 2399 |
seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name) |
| 2400 |
{ |
| 2401 |
IBusWaylandSeat *seat = data; |
| 2402 |
g_assert (seat); |
| 2403 |
g_assert (name); |
| 2404 |
|
| 2405 |
g_free (seat->name); |
| 2406 |
seat->name = g_strdup(name); |
| 2407 |
} |
| 2408 |
|
| 2409 |
|
| 2410 |
static const struct wl_seat_listener seat_listener = { |
| 2411 |
.capabilities = seat_handle_capabilities, |
| 2412 |
.name = seat_handle_name, |
| 2413 |
}; |
| 2414 |
|
| 2415 |
|
| 2416 |
static void |
| 2417 |
registry_handle_global (void *data, |
| 2418 |
struct wl_registry *registry, |
| 2419 |
uint32_t name, |
| 2420 |
const char *interface, |
| 2421 |
uint32_t version) |
| 2422 |
{ |
| 2423 |
IBusWaylandIM *wlim = data; |
| 2424 |
IBusWaylandIMPrivate *priv; |
| 2425 |
|
| 2426 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 2427 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 2428 |
if (priv->verbose) { |
| 2429 |
fprintf (priv->log, |
| 2430 |
"wl_registry gets interface: %s name: %u version: %u\n", |
| 2431 |
interface, name, version); |
| 2432 |
fflush (priv->log); |
| 2433 |
} |
| 2434 |
priv->im_serial = 0; |
| 2435 |
if (!g_strcmp0 (interface, zwp_input_method_manager_v2_interface.name)) { |
| 2436 |
priv->version = INPUT_METHOD_V2; |
| 2437 |
priv->input_method_manager_v2 = |
| 2438 |
wl_registry_bind (registry, name, |
| 2439 |
&zwp_input_method_manager_v2_interface, 1); |
| 2440 |
} else if (!g_strcmp0 (interface, zwp_input_method_v1_interface.name)) { |
| 2441 |
if (version >= 4) |
| 2442 |
version = 4; |
| 2443 |
priv->version = INPUT_METHOD_V1; |
| 2444 |
priv->input_method_v1 = |
| 2445 |
wl_registry_bind (registry, name, |
| 2446 |
&zwp_input_method_v1_interface, version); |
| 2447 |
zwp_input_method_v1_add_listener (priv->input_method_v1, |
| 2448 |
&input_method_listener_v1, wlim); |
| 2449 |
} else if (!g_strcmp0 (interface, zwp_input_panel_v1_interface.name)) { |
| 2450 |
priv->panel = wl_registry_bind (registry, name, |
| 2451 |
&zwp_input_panel_v1_interface, 1); |
| 2452 |
} else if (!g_strcmp0 (interface, wl_seat_interface.name)) { |
| 2453 |
IBusWaylandSeat *seat = g_slice_new0 (IBusWaylandSeat); |
| 2454 |
if (version >= 5) |
| 2455 |
version = 5; |
| 2456 |
seat->seat = wl_registry_bind (registry, name, |
| 2457 |
&wl_seat_interface, version); |
| 2458 |
seat->wl_name = name; |
| 2459 |
wl_seat_add_listener (seat->seat, &seat_listener, seat); |
| 2460 |
g_ptr_array_add (priv->seats, seat); |
| 2461 |
/* Assume the constructor of IBusWaylandIM is done and the second |
| 2462 |
* seat is called here and don't have to wait for |
| 2463 |
* wl_display_roundtrip(). |
| 2464 |
*/ |
| 2465 |
if (priv->seat) { |
| 2466 |
seat->input_method_v2 = |
| 2467 |
zwp_input_method_manager_v2_get_input_method ( |
| 2468 |
priv->input_method_manager_v2, |
| 2469 |
seat->seat); |
| 2470 |
zwp_input_method_v2_add_listener (seat->input_method_v2, |
| 2471 |
&input_method_listener_v2, wlim); |
| 2472 |
} |
| 2473 |
priv->seat = seat; |
| 2474 |
} else if (!g_strcmp0 (interface, |
| 2475 |
zwp_virtual_keyboard_manager_v1_interface.name)) { |
| 2476 |
_virtual_keyboard_manager = |
| 2477 |
wl_registry_bind (registry, name, |
| 2478 |
&zwp_virtual_keyboard_manager_v1_interface, |
| 2479 |
1); |
| 2480 |
} |
| 2481 |
} |
| 2482 |
|
| 2483 |
|
| 2484 |
static void |
| 2485 |
registry_handle_global_remove (void *data, |
| 2486 |
struct wl_registry *registry, |
| 2487 |
uint32_t name) |
| 2488 |
{ |
| 2489 |
IBusWaylandIM *wlim = data; |
| 2490 |
IBusWaylandIMPrivate *priv; |
| 2491 |
IBusWaylandSeat *seat; |
| 2492 |
guint i = 0; |
| 2493 |
|
| 2494 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 2495 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 2496 |
if (priv->verbose) { |
| 2497 |
fprintf (priv->log, "wl_registry remove name: %u\n", name); |
| 2498 |
fflush (priv->log); |
| 2499 |
} |
| 2500 |
if (!(seat = _get_seat_with_name (priv->seats, name, &i))) |
| 2501 |
return; |
| 2502 |
if (priv->verbose) { |
| 2503 |
fprintf (priv->log, "Remove %s seat.\n", seat->name); |
| 2504 |
fflush (priv->log); |
| 2505 |
} |
| 2506 |
g_ptr_array_remove_index (priv->seats, i); |
| 2507 |
if (priv->seat == seat) { |
| 2508 |
if (!priv->seats->len) |
| 2509 |
priv->seat = NULL; |
| 2510 |
else |
| 2511 |
priv->seat = g_ptr_array_index (priv->seats, 0); |
| 2512 |
} |
| 2513 |
} |
| 2514 |
|
| 2515 |
|
| 2516 |
static const struct wl_registry_listener registry_listener = { |
| 2517 |
.global = registry_handle_global, |
| 2518 |
.global_remove = registry_handle_global_remove |
| 2519 |
}; |
| 2520 |
|
| 2521 |
|
| 2522 |
static void |
| 2523 |
ibus_wayland_im_class_init (IBusWaylandIMClass *class) |
| 2524 |
{ |
| 2525 |
GObjectClass *gobject_class = G_OBJECT_CLASS (class); |
| 2526 |
IBusObjectClass *ibus_object_class = IBUS_OBJECT_CLASS (class); |
| 2527 |
|
| 2528 |
gobject_class->constructor = ibus_wayland_im_constructor; |
| 2529 |
gobject_class->set_property = |
| 2530 |
(GObjectSetPropertyFunc)ibus_wayland_im_set_property; |
| 2531 |
gobject_class->get_property = |
| 2532 |
(GObjectGetPropertyFunc)ibus_wayland_im_get_property; |
| 2533 |
ibus_object_class->destroy = ibus_wayland_im_destroy; |
| 2534 |
|
| 2535 |
/* install properties */ |
| 2536 |
/** |
| 2537 |
* IBusWaylandIM:bus: |
| 2538 |
* |
| 2539 |
* The #IBusBus |
| 2540 |
*/ |
| 2541 |
g_object_class_install_property (gobject_class, |
| 2542 |
PROP_BUS, |
| 2543 |
g_param_spec_object ("bus", |
| 2544 |
"IBusBus", |
| 2545 |
"The #IBusBus", |
| 2546 |
IBUS_TYPE_BUS, |
| 2547 |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); |
| 2548 |
|
| 2549 |
/** |
| 2550 |
* IBusWaylandIM:wl_display: |
| 2551 |
* |
| 2552 |
* The struct wl_display |
| 2553 |
*/ |
| 2554 |
g_object_class_install_property (gobject_class, |
| 2555 |
PROP_DISPLAY, |
| 2556 |
g_param_spec_pointer ("wl_display", |
| 2557 |
"wl_display", |
| 2558 |
"The struct wl_display", |
| 2559 |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); |
| 2560 |
|
| 2561 |
/** |
| 2562 |
* IBusWaylandIM:log: |
| 2563 |
* |
| 2564 |
* The FILE of the logging file |
| 2565 |
* The default is $XDG_CACHE_HOME/wayland.log |
| 2566 |
*/ |
| 2567 |
g_object_class_install_property (gobject_class, |
| 2568 |
PROP_LOG, |
| 2569 |
g_param_spec_pointer ("log", |
| 2570 |
"loggin file", |
| 2571 |
"The FILE of the logging file", |
| 2572 |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); |
| 2573 |
|
| 2574 |
/** |
| 2575 |
* IBusWaylandIM:verbose: |
| 2576 |
* |
| 2577 |
* The verbose logging mode |
| 2578 |
* %TRUE if output the logging file with verbose, otherwise %FALSE. |
| 2579 |
*/ |
| 2580 |
g_object_class_install_property (gobject_class, |
| 2581 |
PROP_VERBOSE, |
| 2582 |
g_param_spec_boolean ("verbose", |
| 2583 |
"verbose mode", |
| 2584 |
"The verbose logging mode", |
| 2585 |
FALSE, |
| 2586 |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); |
| 2587 |
|
| 2588 |
/* install signals */ |
| 2589 |
/* this module can call ibus_input_context_focus_in() and the focus-in |
| 2590 |
* signal can reach the IBus panel and this also can call |
| 2591 |
* ibus_input_context_focus_out() but the focus-out signal does not reach |
| 2592 |
* the panel in case X11 client gets the focus because ibus-x11 calls |
| 2593 |
* ibus_input_context_focus_in() before this calls |
| 2594 |
* ibus_input_context_focus_out() and ibus-dameon ignores the double |
| 2595 |
* focus-out signal since the focus-out signal already happened with the |
| 2596 |
* focus-in signal. SO IBUS_FOCUS_OUT signal is needed at least to |
| 2597 |
* send the signal to the panel certainly. |
| 2598 |
*/ |
| 2599 |
wayland_im_signals[IBUS_FOCUS_IN] = |
| 2600 |
g_signal_new (I_("ibus-focus-in"), |
| 2601 |
G_TYPE_FROM_CLASS (class), |
| 2602 |
G_SIGNAL_RUN_LAST, |
| 2603 |
0, |
| 2604 |
NULL, NULL, |
| 2605 |
g_cclosure_marshal_VOID__STRING, |
| 2606 |
G_TYPE_NONE, |
| 2607 |
1, |
| 2608 |
G_TYPE_STRING); |
| 2609 |
g_signal_set_va_marshaller (wayland_im_signals[IBUS_FOCUS_IN], |
| 2610 |
G_TYPE_FROM_CLASS (class), |
| 2611 |
g_cclosure_marshal_VOID__STRINGv); |
| 2612 |
|
| 2613 |
wayland_im_signals[IBUS_FOCUS_OUT] = |
| 2614 |
g_signal_new (I_("ibus-focus-out"), |
| 2615 |
G_TYPE_FROM_CLASS (class), |
| 2616 |
G_SIGNAL_RUN_LAST, |
| 2617 |
0, |
| 2618 |
NULL, NULL, |
| 2619 |
g_cclosure_marshal_VOID__STRING, |
| 2620 |
G_TYPE_NONE, |
| 2621 |
1, |
| 2622 |
G_TYPE_STRING); |
| 2623 |
g_signal_set_va_marshaller (wayland_im_signals[IBUS_FOCUS_OUT], |
| 2624 |
G_TYPE_FROM_CLASS (class), |
| 2625 |
g_cclosure_marshal_VOID__STRINGv); |
| 2626 |
} |
| 2627 |
|
| 2628 |
|
| 2629 |
static gboolean |
| 2630 |
ibus_wayland_im_open_log (IBusWaylandIM *wlim) |
| 2631 |
{ |
| 2632 |
IBusWaylandIMPrivate *priv; |
| 2633 |
char *directory; |
| 2634 |
char *path; |
| 2635 |
struct timeval time_val; |
| 2636 |
struct tm local_time; |
| 2637 |
|
| 2638 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 2639 |
directory = g_build_filename (g_get_user_cache_dir (), "ibus", NULL); |
| 2640 |
g_return_val_if_fail (directory, FALSE); |
| 2641 |
errno = 0; |
| 2642 |
if (g_mkdir_with_parents (directory, 0700) != 0) { |
| 2643 |
g_error ("mkdir is failed in: %s: %s", |
| 2644 |
directory, g_strerror (errno)); |
| 2645 |
return FALSE; |
| 2646 |
} |
| 2647 |
path = g_build_filename (directory, "wayland.log", NULL); |
| 2648 |
g_free (directory); |
| 2649 |
if (!(priv->log = fopen (path, "w"))) { |
| 2650 |
g_error ("Cannot open log file: %s: %s", |
| 2651 |
path, g_strerror (errno)); |
| 2652 |
return FALSE; |
| 2653 |
} |
| 2654 |
g_free (path); |
| 2655 |
gettimeofday (&time_val, NULL); |
| 2656 |
localtime_r (&time_val.tv_sec, &local_time); |
| 2657 |
fprintf (priv->log, "Start %02d:%02d:%02d.%6d\n", |
| 2658 |
local_time.tm_hour, |
| 2659 |
local_time.tm_min, |
| 2660 |
local_time.tm_sec, |
| 2661 |
(int)time_val.tv_usec); |
| 2662 |
fflush (priv->log); |
| 2663 |
return TRUE; |
| 2664 |
} |
| 2665 |
|
| 2666 |
|
| 2667 |
static GObject* |
| 2668 |
ibus_wayland_im_constructor (GType type, |
| 2669 |
guint n_params, |
| 2670 |
GObjectConstructParam *params) |
| 2671 |
{ |
| 2672 |
GObject *object; |
| 2673 |
IBusWaylandIM *wlim; |
| 2674 |
IBusWaylandIMPrivate *priv; |
| 2675 |
IBusWaylandSeat *seat = NULL; |
| 2676 |
IBusEngineDesc *desc; |
| 2677 |
struct xkb_keymap *keymap = NULL; |
| 2678 |
|
| 2679 |
object = G_OBJECT_CLASS (ibus_wayland_im_parent_class)->constructor ( |
| 2680 |
type, n_params, params); |
| 2681 |
/* make bus object sink */ |
| 2682 |
g_object_ref_sink (object); |
| 2683 |
wlim = IBUS_WAYLAND_IM (object); |
| 2684 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 2685 |
if (!priv->log) |
| 2686 |
g_assert (ibus_wayland_im_open_log (wlim)); |
| 2687 |
if (!priv->display) |
| 2688 |
priv->display = wl_display_connect (NULL); |
| 2689 |
if (!priv->display) { |
| 2690 |
g_error ("Failed to connect to Wayland server: %s\n", |
| 2691 |
g_strerror (errno)); |
| 2692 |
} |
| 2693 |
priv->seats = g_ptr_array_new_with_free_func (ibus_wayland_seat_destroy); |
| 2694 |
priv->repeat_rate = 25; |
| 2695 |
priv->repeat_delay = 600; |
| 2696 |
|
| 2697 |
_registry = wl_display_get_registry (priv->display); |
| 2698 |
wl_registry_add_listener (_registry, ®istry_listener, wlim); |
| 2699 |
wl_display_roundtrip (priv->display); |
| 2700 |
if (priv->input_method_manager_v2 && priv->seat && |
| 2701 |
_virtual_keyboard_manager) { |
| 2702 |
seat = priv->seat; |
| 2703 |
seat->input_method_v2 = zwp_input_method_manager_v2_get_input_method ( |
| 2704 |
priv->input_method_manager_v2, |
| 2705 |
seat->seat); |
| 2706 |
zwp_input_method_v2_add_listener (seat->input_method_v2, |
| 2707 |
&input_method_listener_v2, wlim); |
| 2708 |
} |
| 2709 |
if ((!seat || !seat->input_method_v2) && !priv->input_method_v1) { |
| 2710 |
g_error ("No input_method global\n"); |
| 2711 |
} |
| 2712 |
|
| 2713 |
priv->xkb_context = xkb_context_new (0); |
| 2714 |
if (priv->xkb_context == NULL) { |
| 2715 |
g_error ("Failed to create XKB context\n"); |
| 2716 |
} |
| 2717 |
|
| 2718 |
if (!priv->ibusbus || !ibus_bus_is_connected (priv->ibusbus)) { |
| 2719 |
g_warning ("Cannot connect to ibus-daemon"); |
| 2720 |
g_object_unref (object); |
| 2721 |
return NULL; |
| 2722 |
} |
| 2723 |
desc = ibus_bus_get_global_engine (priv->ibusbus); |
| 2724 |
if (desc) |
| 2725 |
keymap = create_user_xkb_keymap (priv->xkb_context, desc); |
| 2726 |
if (keymap && !ibus_wayland_im_update_xkb_state (wlim, keymap)) |
| 2727 |
xkb_keymap_unref (keymap); |
| 2728 |
ibus_bus_set_watch_ibus_signal (priv->ibusbus, TRUE); |
| 2729 |
g_signal_connect (priv->ibusbus, "global-engine-changed", |
| 2730 |
G_CALLBACK (_bus_global_engine_changed_cb), |
| 2731 |
object); |
| 2732 |
|
| 2733 |
_use_sync_mode = _get_char_env ("IBUS_ENABLE_SYNC_MODE", 1); |
| 2734 |
|
| 2735 |
return object; |
| 2736 |
} |
| 2737 |
|
| 2738 |
|
| 2739 |
static void |
| 2740 |
ibus_wayland_im_init (IBusWaylandIM *wlim) |
| 2741 |
{ |
| 2742 |
} |
| 2743 |
|
| 2744 |
|
| 2745 |
static void |
| 2746 |
ibus_wayland_im_destroy (IBusObject *object) |
| 2747 |
{ |
| 2748 |
IBusWaylandIM *wlim = (IBusWaylandIM *)object; |
| 2749 |
IBusWaylandIMPrivate *priv; |
| 2750 |
|
| 2751 |
g_debug ("IBusWaylandIM is destroyed."); |
| 2752 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (object)); |
| 2753 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 2754 |
if (priv->panel) |
| 2755 |
g_clear_pointer (&priv->panel, zwp_input_panel_v1_destroy); |
| 2756 |
if (priv->input_method_v1) |
| 2757 |
g_clear_pointer (&priv->input_method_v1, zwp_input_method_v1_destroy); |
| 2758 |
if (priv->seats) { |
| 2759 |
g_ptr_array_free (priv->seats, TRUE); |
| 2760 |
priv->seats = NULL; |
| 2761 |
} |
| 2762 |
if (priv->input_method_manager_v2) { |
| 2763 |
g_clear_pointer (&priv->input_method_manager_v2, |
| 2764 |
zwp_input_method_manager_v2_destroy); |
| 2765 |
} |
| 2766 |
|
| 2767 |
|
| 2768 |
IBUS_OBJECT_CLASS (ibus_wayland_im_parent_class)->destroy (object); |
| 2769 |
} |
| 2770 |
|
| 2771 |
|
| 2772 |
static void |
| 2773 |
ibus_wayland_im_set_property (IBusWaylandIM *wlim, |
| 2774 |
guint prop_id, |
| 2775 |
const GValue *value, |
| 2776 |
GParamSpec *pspec) |
| 2777 |
{ |
| 2778 |
IBusWaylandIMPrivate *priv; |
| 2779 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 2780 |
|
| 2781 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 2782 |
switch (prop_id) { |
| 2783 |
case PROP_BUS: |
| 2784 |
g_assert (priv->ibusbus == NULL); |
| 2785 |
priv->ibusbus = g_value_get_object (value); |
| 2786 |
if (!IBUS_IS_BUS (priv->ibusbus)) { |
| 2787 |
g_warning ("bus is not IBusBus."); |
| 2788 |
priv->ibusbus = NULL; |
| 2789 |
} |
| 2790 |
break; |
| 2791 |
case PROP_DISPLAY: |
| 2792 |
g_assert (priv->display == NULL); |
| 2793 |
priv->display = g_value_get_pointer (value); |
| 2794 |
break; |
| 2795 |
case PROP_LOG: |
| 2796 |
g_assert (priv->log == NULL); |
| 2797 |
priv->log = g_value_get_pointer (value); |
| 2798 |
break; |
| 2799 |
case PROP_VERBOSE: |
| 2800 |
g_assert (!priv->verbose); |
| 2801 |
priv->verbose = g_value_get_boolean (value); |
| 2802 |
break; |
| 2803 |
default: |
| 2804 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (wlim, prop_id, pspec); |
| 2805 |
} |
| 2806 |
} |
| 2807 |
|
| 2808 |
|
| 2809 |
static void |
| 2810 |
ibus_wayland_im_get_property (IBusWaylandIM *wlim, |
| 2811 |
guint prop_id, |
| 2812 |
GValue *value, |
| 2813 |
GParamSpec *pspec) |
| 2814 |
{ |
| 2815 |
IBusWaylandIMPrivate *priv; |
| 2816 |
g_return_if_fail (IBUS_IS_WAYLAND_IM (wlim)); |
| 2817 |
|
| 2818 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 2819 |
switch (prop_id) { |
| 2820 |
case PROP_BUS: |
| 2821 |
g_value_set_object (value, priv->ibusbus); |
| 2822 |
break; |
| 2823 |
case PROP_DISPLAY: |
| 2824 |
g_value_set_pointer (value, priv->display); |
| 2825 |
break; |
| 2826 |
case PROP_LOG: |
| 2827 |
g_value_set_pointer (value, priv->log); |
| 2828 |
break; |
| 2829 |
case PROP_VERBOSE: |
| 2830 |
g_value_set_boolean (value, priv->verbose); |
| 2831 |
break; |
| 2832 |
default: |
| 2833 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (wlim, prop_id, pspec); |
| 2834 |
} |
| 2835 |
} |
| 2836 |
|
| 2837 |
|
| 2838 |
IBusWaylandIM * |
| 2839 |
ibus_wayland_im_new (const gchar *first_property_name, ...) |
| 2840 |
{ |
| 2841 |
va_list var_args; |
| 2842 |
GObject *object; |
| 2843 |
|
| 2844 |
g_assert (first_property_name); |
| 2845 |
|
| 2846 |
va_start (var_args, first_property_name); |
| 2847 |
object = g_object_new_valist (IBUS_TYPE_WAYLAND_IM, |
| 2848 |
first_property_name, |
| 2849 |
var_args); |
| 2850 |
va_end (var_args); |
| 2851 |
|
| 2852 |
return IBUS_WAYLAND_IM (object); |
| 2853 |
} |
| 2854 |
|
| 2855 |
gboolean |
| 2856 |
ibus_wayland_im_set_surface (IBusWaylandIM *wlim, |
| 2857 |
void *surface) |
| 2858 |
{ |
| 2859 |
IBusWaylandIMPrivate *priv; |
| 2860 |
struct wl_surface *_surface = surface; |
| 2861 |
|
| 2862 |
g_return_val_if_fail (wlim, FALSE); |
| 2863 |
priv = ibus_wayland_im_get_instance_private (wlim); |
| 2864 |
|
| 2865 |
switch (priv->version) { |
| 2866 |
case INPUT_METHOD_V1: |
| 2867 |
if (!_surface) |
| 2868 |
return TRUE; |
| 2869 |
if (!priv->panel) { |
| 2870 |
g_warning ("Need zwp_input_panel_v1 before the surface setting."); |
| 2871 |
return FALSE; |
| 2872 |
} |
| 2873 |
priv->panel_surface = |
| 2874 |
zwp_input_panel_v1_get_input_panel_surface (priv->panel, |
| 2875 |
_surface); |
| 2876 |
g_return_val_if_fail (priv->panel_surface, FALSE); |
| 2877 |
zwp_input_panel_surface_v1_set_overlay_panel (priv->panel_surface); |
| 2878 |
break; |
| 2879 |
case INPUT_METHOD_V2: |
| 2880 |
if (!priv->seat || !priv->seat->input_method_v2) { |
| 2881 |
g_warning ("Need zwp_input_method_v2 before the surface setting."); |
| 2882 |
return FALSE; |
| 2883 |
} |
| 2884 |
if (priv->seat->input_popup_surface) { |
| 2885 |
g_clear_pointer (&priv->seat->input_popup_surface, |
| 2886 |
zwp_input_popup_surface_v2_destroy); |
| 2887 |
} |
| 2888 |
if (!_surface) |
| 2889 |
return TRUE; |
| 2890 |
priv->seat->input_popup_surface = |
| 2891 |
zwp_input_method_v2_get_input_popup_surface ( |
| 2892 |
priv->seat->input_method_v2, |
| 2893 |
_surface); |
| 2894 |
break; |
| 2895 |
default: |
| 2896 |
g_assert_not_reached (); |
| 2897 |
} |
| 2898 |
return TRUE; |
| 2899 |
} |