| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include <gdk/gdk.h> |
| 26 |
#include <maliit-glib/maliitbus.h> |
| 27 |
|
| 28 |
#ifdef HAVE_X11 |
| 29 |
#include <gdk/gdkx.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#include "client-imcontext-gtk.h" |
| 33 |
#include "qt-gtk-translate.h" |
| 34 |
#include "debug.h" |
| 35 |
|
| 36 |
static GType _maliit_im_context_type = 0; |
| 37 |
static GtkIMContextClass *parent_class = NULL; |
| 38 |
|
| 39 |
static MaliitIMContext *focused_im_context = NULL; |
| 40 |
static GtkWidget *focused_widget = NULL; |
| 41 |
|
| 42 |
gboolean redirect_keys = FALSE; |
| 43 |
|
| 44 |
static void maliit_im_context_finalize(GObject *object); |
| 45 |
|
| 46 |
static void maliit_im_context_class_init(MaliitIMContextClass *klass); |
| 47 |
static void maliit_im_context_init(MaliitIMContext *maliit_im_context); |
| 48 |
|
| 49 |
static void maliit_im_context_focus_in(GtkIMContext *context); |
| 50 |
static void maliit_im_context_focus_out(GtkIMContext *context); |
| 51 |
static gboolean maliit_im_context_filter_key_event(GtkIMContext *context, GdkEventKey *event); |
| 52 |
static void maliit_im_context_reset(GtkIMContext *context); |
| 53 |
static void maliit_im_context_get_preedit_string(GtkIMContext *context, gchar **str, PangoAttrList **attrs, gint *cursor_pos); |
| 54 |
static void maliit_im_context_set_preedit_enabled(GtkIMContext *context, gboolean enabled); |
| 55 |
static void maliit_im_context_set_client_window(GtkIMContext *context, GdkWindow *window); |
| 56 |
static void maliit_im_context_set_cursor_location(GtkIMContext *context, GdkRectangle *area); |
| 57 |
static void maliit_im_context_update_widget_info(MaliitIMContext *im_context); |
| 58 |
|
| 59 |
static gboolean maliit_im_context_im_initiated_hide(MaliitContext *obj, GDBusMethodInvocation *invocation, gpointer user_data); |
| 60 |
static gboolean maliit_im_context_commit_string(MaliitContext *obj, GDBusMethodInvocation *invocation, const gchar *string, |
| 61 |
gint replacement_start, gint replacement_length, gint cursor_pos, |
| 62 |
gpointer user_data); |
| 63 |
static gboolean maliit_im_context_update_preedit(MaliitContext *obj, GDBusMethodInvocation *invocation, const gchar *string, |
| 64 |
GVariant *formatListData, gint replaceStart, gint replaceLength, gint cursorPos, |
| 65 |
gpointer user_data); |
| 66 |
static gboolean maliit_im_context_key_event(MaliitContext *obj, GDBusMethodInvocation *invocation, gint type, gint key, |
| 67 |
gint modifiers, const gchar *text, gboolean auto_repeat, gint count, |
| 68 |
guchar request_type, gpointer user_data); |
| 69 |
static gboolean maliit_im_context_set_redirect_keys(MaliitContext *obj, GDBusMethodInvocation *invocation, gboolean enabled, |
| 70 |
gpointer user_data); |
| 71 |
static gboolean maliit_im_context_notify_extended_attribute_changed (MaliitContext *obj, GDBusMethodInvocation *invocation, |
| 72 |
gint id, const gchar *target, const gchar *target_item, |
| 73 |
const gchar *attribute, GVariant *variant_value, |
| 74 |
gpointer user_data); |
| 75 |
static gboolean maliit_im_context_update_input_method_area (MaliitContext *obj, GDBusMethodInvocation *invocation, |
| 76 |
gint x, gint y, gint width, gint height, gpointer user_data); |
| 77 |
static void maliit_im_context_invoke_action(MaliitServer *obj, const char *action, const char* sequence, gpointer user_data); |
| 78 |
|
| 79 |
static GtkIMContext *maliit_im_context_get_slave_imcontext(void); |
| 80 |
|
| 81 |
#ifdef HAVE_X11 |
| 82 |
static const gchar *const WIDGET_INFO_WIN_ID = "winId"; |
| 83 |
#endif |
| 84 |
static const gchar *const WIDGET_INFO_FOCUS_STATE = "focusState"; |
| 85 |
static const gchar *const WIDGET_INFO_ATTRIBUTE_EXTENSION_ID = "toolbarId"; |
| 86 |
static const gchar *const WIDGET_INFO_ATTRIBUTE_EXTENSION_FILENAME = "toolbar"; |
| 87 |
static const gchar *const WIDGET_INFO_SURROUNDING_TEXT = "surroundingText"; |
| 88 |
static const gchar *const WIDGET_INFO_CURSOR_POSITION = "cursorPosition"; |
| 89 |
|
| 90 |
|
| 91 |
GType maliit_im_context_get_type() |
| 92 |
{ |
| 93 |
return _maliit_im_context_type; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
void |
| 98 |
maliit_im_context_register_type(GTypeModule *type_module) |
| 99 |
{ |
| 100 |
static const GTypeInfo maliit_im_context_info = { |
| 101 |
sizeof(MaliitIMContextClass), |
| 102 |
(GBaseInitFunc) NULL, |
| 103 |
(GBaseFinalizeFunc) NULL, |
| 104 |
(GClassInitFunc) maliit_im_context_class_init, |
| 105 |
NULL, |
| 106 |
NULL, |
| 107 |
sizeof(MaliitIMContext), |
| 108 |
0, |
| 109 |
(GInstanceInitFunc) maliit_im_context_init, |
| 110 |
NULL |
| 111 |
}; |
| 112 |
|
| 113 |
if (_maliit_im_context_type) |
| 114 |
return; |
| 115 |
|
| 116 |
if (type_module) { |
| 117 |
_maliit_im_context_type = |
| 118 |
g_type_module_register_type( |
| 119 |
type_module, |
| 120 |
GTK_TYPE_IM_CONTEXT, |
| 121 |
"MaliitIMContext", |
| 122 |
&maliit_im_context_info, |
| 123 |
(GTypeFlags)0); |
| 124 |
} else { |
| 125 |
_maliit_im_context_type = |
| 126 |
g_type_register_static( |
| 127 |
GTK_TYPE_IM_CONTEXT, |
| 128 |
"MaliitIMContext", |
| 129 |
&maliit_im_context_info, |
| 130 |
(GTypeFlags)0); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
static void |
| 138 |
slave_commit(GtkIMContext *slave, const char *text, gpointer data) |
| 139 |
{ |
| 140 |
UNUSED(slave); |
| 141 |
UNUSED(data); |
| 142 |
DBG("text = %s", text); |
| 143 |
if (focused_im_context && text) { |
| 144 |
g_signal_emit_by_name(focused_im_context, "commit", text); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
static void |
| 150 |
slave_preedit_changed(GtkIMContext *slave, gpointer data) |
| 151 |
{ |
| 152 |
UNUSED(data); |
| 153 |
gchar *str = NULL; |
| 154 |
gint cursor_pos = 0; |
| 155 |
PangoAttrList *attrs = NULL; |
| 156 |
|
| 157 |
STEP(); |
| 158 |
if (!focused_im_context || !slave) |
| 159 |
return; |
| 160 |
|
| 161 |
gtk_im_context_get_preedit_string(slave, &str, &attrs, &cursor_pos); |
| 162 |
|
| 163 |
if (str != NULL) { |
| 164 |
g_free(focused_im_context->preedit_str); |
| 165 |
focused_im_context->preedit_str = str; |
| 166 |
} |
| 167 |
|
| 168 |
focused_im_context->preedit_cursor_pos = cursor_pos; |
| 169 |
|
| 170 |
if (focused_im_context->preedit_attrs != NULL) |
| 171 |
pango_attr_list_unref(focused_im_context->preedit_attrs); |
| 172 |
|
| 173 |
focused_im_context->preedit_attrs = attrs; |
| 174 |
|
| 175 |
g_signal_emit_by_name(focused_im_context, "preedit-changed"); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
static GtkIMContext * |
| 180 |
maliit_im_context_get_slave_imcontext(void) |
| 181 |
{ |
| 182 |
static GtkIMContext *slave_ic = NULL; |
| 183 |
|
| 184 |
if (!slave_ic) { |
| 185 |
slave_ic = gtk_im_context_simple_new(); |
| 186 |
|
| 187 |
|
| 188 |
g_signal_connect(G_OBJECT(slave_ic), "preedit-changed", G_CALLBACK(slave_preedit_changed), NULL); |
| 189 |
g_signal_connect(G_OBJECT(slave_ic), "commit", G_CALLBACK(slave_commit), NULL); |
| 190 |
} |
| 191 |
|
| 192 |
return slave_ic; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
GtkIMContext * |
| 197 |
maliit_im_context_new(void) |
| 198 |
{ |
| 199 |
MaliitIMContext *ic = MALIIT_IM_CONTEXT(g_object_new(MALIIT_TYPE_IM_CONTEXT, NULL)); |
| 200 |
return GTK_IM_CONTEXT(ic); |
| 201 |
} |
| 202 |
|
| 203 |
static void |
| 204 |
maliit_im_context_dispose(GObject *object) |
| 205 |
{ |
| 206 |
MaliitIMContext *im_context = MALIIT_IM_CONTEXT(object); |
| 207 |
|
| 208 |
if (im_context->context) |
| 209 |
g_signal_handlers_disconnect_by_data (im_context->context, object); |
| 210 |
if (im_context->server) |
| 211 |
g_signal_handlers_disconnect_by_data (im_context->server, object); |
| 212 |
|
| 213 |
g_clear_object(&im_context->context); |
| 214 |
g_clear_object(&im_context->server); |
| 215 |
|
| 216 |
G_OBJECT_CLASS(parent_class)->dispose(object); |
| 217 |
} |
| 218 |
|
| 219 |
static void |
| 220 |
maliit_im_context_finalize(GObject *object) |
| 221 |
{ |
| 222 |
MaliitIMContext *im_context = MALIIT_IM_CONTEXT(object); |
| 223 |
|
| 224 |
if (im_context->widget_state) |
| 225 |
g_variant_unref(im_context->widget_state); |
| 226 |
|
| 227 |
if (im_context->client_window) |
| 228 |
g_object_unref(im_context->client_window); |
| 229 |
|
| 230 |
if (im_context->registry) |
| 231 |
g_object_unref(im_context->registry); |
| 232 |
|
| 233 |
G_OBJECT_CLASS(parent_class)->finalize(object); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
static void |
| 238 |
maliit_im_context_class_init(MaliitIMContextClass *klass) |
| 239 |
{ |
| 240 |
GObjectClass *gobject_class = G_OBJECT_CLASS(klass); |
| 241 |
parent_class = (GtkIMContextClass *)g_type_class_peek_parent(klass); |
| 242 |
GtkIMContextClass *imclass = GTK_IM_CONTEXT_CLASS(klass); |
| 243 |
|
| 244 |
gobject_class->dispose = maliit_im_context_dispose; |
| 245 |
gobject_class->finalize = maliit_im_context_finalize; |
| 246 |
|
| 247 |
imclass->focus_in = maliit_im_context_focus_in; |
| 248 |
imclass->focus_out = maliit_im_context_focus_out; |
| 249 |
imclass->filter_keypress = maliit_im_context_filter_key_event; |
| 250 |
imclass->reset = maliit_im_context_reset; |
| 251 |
imclass->set_client_window = maliit_im_context_set_client_window; |
| 252 |
imclass->get_preedit_string = maliit_im_context_get_preedit_string; |
| 253 |
imclass->set_cursor_location = maliit_im_context_set_cursor_location; |
| 254 |
imclass->set_use_preedit = maliit_im_context_set_preedit_enabled; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
static MaliitContext * |
| 259 |
get_context(MaliitIMContext *context) |
| 260 |
{ |
| 261 |
GError *error = NULL; |
| 262 |
|
| 263 |
if (!context->context && maliit_is_running()) { |
| 264 |
context->context = maliit_get_context_sync(NULL, &error); |
| 265 |
|
| 266 |
if (context->context) { |
| 267 |
g_object_ref(context->context); |
| 268 |
g_signal_connect(context->context, "handle-im-initiated-hide", |
| 269 |
G_CALLBACK(maliit_im_context_im_initiated_hide), context); |
| 270 |
g_signal_connect(context->context, "handle-commit-string", |
| 271 |
G_CALLBACK(maliit_im_context_commit_string), context); |
| 272 |
g_signal_connect(context->context, "handle-update-preedit", |
| 273 |
G_CALLBACK(maliit_im_context_update_preedit), context); |
| 274 |
g_signal_connect(context->context, "handle-key-event", |
| 275 |
G_CALLBACK(maliit_im_context_key_event), context); |
| 276 |
g_signal_connect(context->context, "handle-set-redirect-keys", |
| 277 |
G_CALLBACK(maliit_im_context_set_redirect_keys), context); |
| 278 |
g_signal_connect(context->context, "handle-notify-extended-attribute-changed", |
| 279 |
G_CALLBACK(maliit_im_context_notify_extended_attribute_changed), context); |
| 280 |
g_signal_connect(context->context, "handle-update-input-method-area", |
| 281 |
G_CALLBACK(maliit_im_context_update_input_method_area), context); |
| 282 |
} else { |
| 283 |
g_warning("Unable to connect to context: %s", error->message); |
| 284 |
g_clear_error(&error); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
return context->context; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
static MaliitServer * |
| 293 |
get_server(MaliitIMContext *context) |
| 294 |
{ |
| 295 |
GError *error = NULL; |
| 296 |
|
| 297 |
if (!context->server && maliit_is_running()) { |
| 298 |
get_context(context); |
| 299 |
|
| 300 |
context->server = maliit_get_server_sync(NULL, &error); |
| 301 |
|
| 302 |
if (context->server) { |
| 303 |
g_object_ref(context->server); |
| 304 |
g_signal_connect(context->server, "invoke-action", G_CALLBACK(maliit_im_context_invoke_action), context); |
| 305 |
} else { |
| 306 |
g_warning("Unable to connect to server: %s", error->message); |
| 307 |
g_clear_error(&error); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
return context->server; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
static void |
| 316 |
maliit_im_context_init(MaliitIMContext *self) |
| 317 |
{ |
| 318 |
self->client_window = NULL; |
| 319 |
|
| 320 |
self->cursor_location.x = -1; |
| 321 |
self->cursor_location.y = -1; |
| 322 |
self->cursor_location.width = 0; |
| 323 |
self->cursor_location.height = 0; |
| 324 |
|
| 325 |
self->preedit_str = NULL; |
| 326 |
self->preedit_attrs = NULL; |
| 327 |
self->preedit_cursor_pos = 0; |
| 328 |
|
| 329 |
self->focus_state = FALSE; |
| 330 |
|
| 331 |
if (maliit_is_running()) { |
| 332 |
get_context(self); |
| 333 |
|
| 334 |
self->registry = maliit_attribute_extension_registry_get_instance(); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
static void |
| 340 |
maliit_im_context_focus_in(GtkIMContext *context) |
| 341 |
{ |
| 342 |
MaliitIMContext *im_context = MALIIT_IM_CONTEXT(context); |
| 343 |
gboolean focus_changed = TRUE; |
| 344 |
GError *error = NULL; |
| 345 |
|
| 346 |
if (!maliit_is_running()) |
| 347 |
return; |
| 348 |
|
| 349 |
DBG("im_context = %p", im_context); |
| 350 |
|
| 351 |
if (focused_im_context && focused_im_context != im_context) |
| 352 |
maliit_im_context_focus_out(GTK_IM_CONTEXT(focused_im_context)); |
| 353 |
focused_im_context = im_context; |
| 354 |
|
| 355 |
im_context->focus_state = TRUE; |
| 356 |
maliit_im_context_update_widget_info(im_context); |
| 357 |
|
| 358 |
if (maliit_server_call_activate_context_sync(get_server(im_context), |
| 359 |
NULL, |
| 360 |
&error)) { |
| 361 |
if (maliit_server_call_update_widget_information_sync(get_server(im_context), |
| 362 |
im_context->widget_state, |
| 363 |
focus_changed, |
| 364 |
NULL, |
| 365 |
&error)) { |
| 366 |
if (!maliit_server_call_show_input_method_sync(get_server(im_context), |
| 367 |
NULL, |
| 368 |
&error)) { |
| 369 |
g_warning("Unable to show input method: %s", error->message); |
| 370 |
g_clear_error(&error); |
| 371 |
} |
| 372 |
} else { |
| 373 |
g_warning("Unable to update widget information: %s", error->message); |
| 374 |
g_clear_error(&error); |
| 375 |
} |
| 376 |
} else { |
| 377 |
g_warning("Unable to activate context: %s", error->message); |
| 378 |
g_clear_error(&error); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
static void |
| 386 |
maliit_im_context_focus_out(GtkIMContext *context) |
| 387 |
{ |
| 388 |
MaliitIMContext *im_context = MALIIT_IM_CONTEXT(context); |
| 389 |
GError *error = NULL; |
| 390 |
|
| 391 |
if (!maliit_is_running()) |
| 392 |
return; |
| 393 |
|
| 394 |
DBG("im_context = %p", im_context); |
| 395 |
|
| 396 |
maliit_im_context_reset(context); |
| 397 |
|
| 398 |
im_context->focus_state = FALSE; |
| 399 |
focused_im_context = NULL; |
| 400 |
focused_widget = NULL; |
| 401 |
|
| 402 |
maliit_im_context_update_widget_info(im_context); |
| 403 |
|
| 404 |
if (maliit_server_call_update_widget_information_sync(get_server(im_context), |
| 405 |
im_context->widget_state, |
| 406 |
TRUE, |
| 407 |
NULL, |
| 408 |
&error)) { |
| 409 |
if (!maliit_server_call_hide_input_method_sync(get_server(im_context), |
| 410 |
NULL, |
| 411 |
&error)) { |
| 412 |
g_warning("Unable to hide input method: %s", error->message); |
| 413 |
g_clear_error(&error); |
| 414 |
} |
| 415 |
} else { |
| 416 |
g_warning("Unable to update widget information: %s", error->message); |
| 417 |
g_clear_error(&error); |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
static gboolean |
| 425 |
maliit_im_context_filter_key_event(GtkIMContext *context, GdkEventKey *event) |
| 426 |
{ |
| 427 |
MaliitIMContext *im_context = MALIIT_IM_CONTEXT(context); |
| 428 |
int qevent_type = 0, qt_keycode = 0, qt_modifier = 0; |
| 429 |
gchar *text = ""; |
| 430 |
GError *error = NULL; |
| 431 |
|
| 432 |
if (!maliit_is_running()) { |
| 433 |
gchar string[10]; |
| 434 |
gunichar c = gdk_keyval_to_unicode(event->keyval); |
| 435 |
|
| 436 |
#if GTK_MAJOR_VERSION == 2 |
| 437 |
GdkModifierType no_text_input_mask = GDK_MOD1_MASK | GDK_CONTROL_MASK; |
| 438 |
#elif GTK_MAJOR_VERSION == 3 |
| 439 |
GdkDisplay *display = gdk_window_get_display(event->window); |
| 440 |
GdkKeymap *keymap = gdk_keymap_get_for_display(display); |
| 441 |
GdkModifierIntent intent = GDK_MODIFIER_INTENT_NO_TEXT_INPUT; |
| 442 |
GdkModifierType no_text_input_mask = gdk_keymap_get_modifier_mask(keymap, intent); |
| 443 |
#endif |
| 444 |
|
| 445 |
if (c && !g_unichar_iscntrl(c) && event->type == GDK_KEY_PRESS && !(event->state & no_text_input_mask)) { |
| 446 |
string[g_unichar_to_utf8(c, string)] = 0; |
| 447 |
|
| 448 |
g_signal_emit_by_name(im_context, "commit", string); |
| 449 |
|
| 450 |
return TRUE; |
| 451 |
} |
| 452 |
|
| 453 |
return FALSE; |
| 454 |
} |
| 455 |
|
| 456 |
focused_widget = gtk_get_event_widget((GdkEvent *)event); |
| 457 |
|
| 458 |
DBG("event type=0x%x, state=0x%x, keyval=0x%x, keycode=0x%x, group=%d", |
| 459 |
event->type, event->state, event->keyval, event->hardware_keycode, event->group); |
| 460 |
|
| 461 |
if (focused_im_context != im_context) |
| 462 |
maliit_im_context_focus_in(context); |
| 463 |
|
| 464 |
if ((event->state & IM_FORWARD_MASK) || !redirect_keys) { |
| 465 |
GtkIMContext *slave = maliit_im_context_get_slave_imcontext(); |
| 466 |
return gtk_im_context_filter_keypress(slave, event); |
| 467 |
} |
| 468 |
|
| 469 |
if (!gdk_key_event_to_qt(event, &qevent_type, &qt_keycode, &qt_modifier)) |
| 470 |
return FALSE; |
| 471 |
|
| 472 |
if (!maliit_server_call_process_key_event_sync(get_server(im_context), |
| 473 |
qevent_type, |
| 474 |
qt_keycode, |
| 475 |
qt_modifier, |
| 476 |
text, |
| 477 |
0, |
| 478 |
1, |
| 479 |
event->hardware_keycode, |
| 480 |
event->state, |
| 481 |
event->time, |
| 482 |
NULL, |
| 483 |
&error)) { |
| 484 |
g_warning("Unable to process key event: %s", error->message); |
| 485 |
g_clear_error(&error); |
| 486 |
} |
| 487 |
|
| 488 |
return TRUE; |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
static void |
| 493 |
maliit_im_context_reset(GtkIMContext *context) |
| 494 |
{ |
| 495 |
MaliitIMContext *im_context = MALIIT_IM_CONTEXT(context); |
| 496 |
GError *error = NULL; |
| 497 |
|
| 498 |
if (!maliit_is_running()) |
| 499 |
return; |
| 500 |
|
| 501 |
DBG("im_context = %p", im_context); |
| 502 |
|
| 503 |
if (im_context != focused_im_context) { |
| 504 |
return; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
if (focused_im_context && focused_im_context->preedit_str && focused_im_context->preedit_str[0]) { |
| 509 |
char *commit_string = focused_im_context->preedit_str; |
| 510 |
focused_im_context->preedit_str = g_strdup(""); |
| 511 |
focused_im_context->preedit_cursor_pos = 0; |
| 512 |
g_signal_emit_by_name(focused_im_context, "preedit-changed"); |
| 513 |
g_signal_emit_by_name(focused_im_context, "commit", commit_string); |
| 514 |
g_free(commit_string); |
| 515 |
} |
| 516 |
|
| 517 |
if (!maliit_server_call_reset_sync(get_server(im_context), NULL, &error)) { |
| 518 |
g_warning("Unable to reset: %s", error->message); |
| 519 |
g_clear_error(&error); |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
static void |
| 525 |
maliit_im_context_get_preedit_string(GtkIMContext *context, gchar **str, PangoAttrList **attrs, gint *cursor_pos) |
| 526 |
{ |
| 527 |
MaliitIMContext *im_context = MALIIT_IM_CONTEXT(context); |
| 528 |
|
| 529 |
if (!maliit_is_running()) { |
| 530 |
if (str) |
| 531 |
*str = g_strdup(""); |
| 532 |
|
| 533 |
if (attrs) |
| 534 |
*attrs = pango_attr_list_new(); |
| 535 |
|
| 536 |
if (cursor_pos) |
| 537 |
*cursor_pos = 0; |
| 538 |
|
| 539 |
return; |
| 540 |
} |
| 541 |
|
| 542 |
DBG("im_context = %p", im_context); |
| 543 |
|
| 544 |
if (str) { |
| 545 |
if (im_context->preedit_str) |
| 546 |
*str = g_strdup(im_context->preedit_str); |
| 547 |
else |
| 548 |
*str = g_strdup(""); |
| 549 |
} |
| 550 |
|
| 551 |
if (attrs) { |
| 552 |
if (im_context->preedit_attrs) { |
| 553 |
*attrs = im_context->preedit_attrs; |
| 554 |
pango_attr_list_ref(im_context->preedit_attrs); |
| 555 |
} else { |
| 556 |
*attrs = pango_attr_list_new(); |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
if (cursor_pos) |
| 561 |
*cursor_pos = im_context->preedit_cursor_pos; |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
static void |
| 566 |
maliit_im_context_set_preedit_enabled(GtkIMContext *context, gboolean enabled) |
| 567 |
{ |
| 568 |
UNUSED(context); |
| 569 |
UNUSED(enabled); |
| 570 |
|
| 571 |
if (!maliit_is_running()) |
| 572 |
return; |
| 573 |
|
| 574 |
|
| 575 |
return; |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
static void |
| 580 |
maliit_im_context_set_client_window(GtkIMContext *context, GdkWindow *window) |
| 581 |
{ |
| 582 |
MaliitIMContext *im_context = MALIIT_IM_CONTEXT(context); |
| 583 |
|
| 584 |
if (!maliit_is_running()) |
| 585 |
return; |
| 586 |
|
| 587 |
STEP(); |
| 588 |
|
| 589 |
if (im_context->client_window) |
| 590 |
g_object_unref(im_context->client_window); |
| 591 |
|
| 592 |
if (window) |
| 593 |
g_object_ref(window); |
| 594 |
|
| 595 |
im_context->client_window = window; |
| 596 |
|
| 597 |
|
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
static void |
| 602 |
maliit_im_context_set_cursor_location(GtkIMContext *context, GdkRectangle *area) |
| 603 |
{ |
| 604 |
MaliitIMContext *im_context = MALIIT_IM_CONTEXT(context); |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
if (!maliit_is_running()) |
| 609 |
return; |
| 610 |
|
| 611 |
im_context->cursor_location = *area; |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
void |
| 621 |
maliit_im_context_update_widget_info(MaliitIMContext *im_context) |
| 622 |
{ |
| 623 |
GVariantDict dict; |
| 624 |
|
| 625 |
|
| 626 |
g_variant_dict_init(&dict, NULL); |
| 627 |
|
| 628 |
|
| 629 |
g_variant_dict_insert(&dict, WIDGET_INFO_FOCUS_STATE, "b", im_context->focus_state); |
| 630 |
|
| 631 |
if (im_context->focus_state) { |
| 632 |
|
| 633 |
#ifdef HAVE_X11 |
| 634 |
#if GTK_MAJOR_VERSION == 2 |
| 635 |
if (im_context->client_window) { |
| 636 |
guint64 xid = GDK_WINDOW_XID(im_context->client_window); |
| 637 |
g_variant_dict_insert(&dict, WIDGET_INFO_WIN_ID, "t", xid); |
| 638 |
} |
| 639 |
#elif GTK_MAJOR_VERSION == 3 |
| 640 |
if (GDK_IS_X11_WINDOW(im_context->client_window)) { |
| 641 |
guint64 xid = GDK_WINDOW_XID(im_context->client_window); |
| 642 |
g_variant_dict_insert(&dict, WIDGET_INFO_WIN_ID, "t", xid); |
| 643 |
} |
| 644 |
#endif |
| 645 |
#endif |
| 646 |
|
| 647 |
|
| 648 |
if (im_context->client_window) { |
| 649 |
gpointer user_data = NULL; |
| 650 |
GtkWidget* widget = NULL; |
| 651 |
MaliitAttributeExtension *extension; |
| 652 |
|
| 653 |
gdk_window_get_user_data (im_context->client_window, &user_data); |
| 654 |
|
| 655 |
widget = GTK_WIDGET (user_data); |
| 656 |
|
| 657 |
user_data = g_object_get_qdata (G_OBJECT (widget), |
| 658 |
MALIIT_ATTRIBUTE_EXTENSION_DATA_QUARK); |
| 659 |
|
| 660 |
if (user_data) { |
| 661 |
extension = MALIIT_ATTRIBUTE_EXTENSION (user_data); |
| 662 |
|
| 663 |
g_variant_dict_insert (&dict, WIDGET_INFO_ATTRIBUTE_EXTENSION_ID, |
| 664 |
"i", maliit_attribute_extension_get_id (extension)); |
| 665 |
g_variant_dict_insert (&dict, WIDGET_INFO_ATTRIBUTE_EXTENSION_FILENAME, |
| 666 |
"s", maliit_attribute_extension_get_filename (extension)); |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
GtkIMContext *context = GTK_IM_CONTEXT(im_context); |
| 672 |
gchar *surrounding_text; |
| 673 |
gint cursor_index; |
| 674 |
if (gtk_im_context_get_surrounding(context, &surrounding_text, &cursor_index)) |
| 675 |
{ |
| 676 |
g_variant_dict_insert(&dict, WIDGET_INFO_SURROUNDING_TEXT, "s", surrounding_text); |
| 677 |
g_variant_dict_insert(&dict, WIDGET_INFO_CURSOR_POSITION, "i", cursor_index); |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
im_context->widget_state = g_variant_ref_sink(g_variant_dict_end(&dict)); |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
gboolean |
| 686 |
maliit_im_context_im_initiated_hide(MaliitContext *obj, |
| 687 |
GDBusMethodInvocation *invocation, |
| 688 |
gpointer user_data) |
| 689 |
{ |
| 690 |
MaliitIMContext *im_context = MALIIT_IM_CONTEXT(user_data); |
| 691 |
if (im_context != focused_im_context) |
| 692 |
return FALSE; |
| 693 |
|
| 694 |
if (focused_im_context && focused_im_context->client_window) { |
| 695 |
gpointer user_data = NULL; |
| 696 |
GtkWidget* parent_widget = NULL; |
| 697 |
|
| 698 |
gdk_window_get_user_data (focused_im_context->client_window, &user_data); |
| 699 |
|
| 700 |
parent_widget = GTK_WIDGET (user_data); |
| 701 |
|
| 702 |
while (parent_widget && !GTK_IS_WINDOW (parent_widget)) { |
| 703 |
parent_widget = gtk_widget_get_parent (parent_widget); |
| 704 |
} |
| 705 |
if (parent_widget) { |
| 706 |
gtk_window_set_focus (GTK_WINDOW (parent_widget), NULL); |
| 707 |
maliit_context_complete_im_initiated_hide (obj, invocation); |
| 708 |
return TRUE; |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
return FALSE; |
| 713 |
} |
| 714 |
|
| 715 |
gboolean |
| 716 |
maliit_im_context_commit_string(MaliitContext *obj, |
| 717 |
GDBusMethodInvocation *invocation, |
| 718 |
const gchar *string, |
| 719 |
int replacement_start G_GNUC_UNUSED, |
| 720 |
int replacement_length G_GNUC_UNUSED, |
| 721 |
int cursor_pos G_GNUC_UNUSED, |
| 722 |
gpointer user_data) |
| 723 |
{ |
| 724 |
DBG("string is:%s", string); |
| 725 |
|
| 726 |
MaliitIMContext *im_context = MALIIT_IM_CONTEXT(user_data); |
| 727 |
if (im_context != focused_im_context) |
| 728 |
return FALSE; |
| 729 |
|
| 730 |
if (focused_im_context) { |
| 731 |
g_free(focused_im_context->preedit_str); |
| 732 |
focused_im_context->preedit_str = g_strdup(""); |
| 733 |
focused_im_context->preedit_cursor_pos = 0; |
| 734 |
g_signal_emit_by_name(focused_im_context, "preedit-changed"); |
| 735 |
g_signal_emit_by_name(focused_im_context, "commit", string); |
| 736 |
maliit_context_complete_commit_string(obj, invocation); |
| 737 |
return TRUE; |
| 738 |
} |
| 739 |
|
| 740 |
return FALSE; |
| 741 |
} |
| 742 |
|
| 743 |
typedef enum |
| 744 |
{ |
| 745 |
MaliitPreeditDefault, |
| 746 |
MaliitPreeditNoCandidates, |
| 747 |
MaliitPreeditKeyPress, |
| 748 |
MaliitPreeditUnconvertible, |
| 749 |
MaliitPreeditActive |
| 750 |
} MaliitPreeditFace; |
| 751 |
|
| 752 |
static void |
| 753 |
get_byte_range_from_unicode_offsets (const gchar *string, |
| 754 |
gint utf8_start, |
| 755 |
gint utf8_length, |
| 756 |
gint *byte_start, |
| 757 |
gint *byte_end) |
| 758 |
{ |
| 759 |
gint start; |
| 760 |
gint end; |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
if (g_utf8_validate (string, -1, NULL)) { |
| 766 |
const gchar * const start_pointer = g_utf8_offset_to_pointer (string, utf8_start); |
| 767 |
const gchar * const end_pointer = g_utf8_offset_to_pointer (string, utf8_start + utf8_length); |
| 768 |
|
| 769 |
|
| 770 |
start = start_pointer - string; |
| 771 |
end = end_pointer - string; |
| 772 |
} else { |
| 773 |
start = utf8_start; |
| 774 |
end = utf8_start + utf8_length; |
| 775 |
} |
| 776 |
|
| 777 |
if (byte_start) { |
| 778 |
*byte_start = start; |
| 779 |
} |
| 780 |
if (byte_end) { |
| 781 |
*byte_end = end; |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
gboolean |
| 786 |
maliit_im_context_update_preedit(MaliitContext *obj, |
| 787 |
GDBusMethodInvocation *invocation, |
| 788 |
const gchar *string, |
| 789 |
GVariant *formatListData, |
| 790 |
gint replaceStart G_GNUC_UNUSED, |
| 791 |
gint replaceLength G_GNUC_UNUSED, |
| 792 |
gint cursorPos, |
| 793 |
gpointer user_data) |
| 794 |
{ |
| 795 |
MaliitIMContext *im_context = MALIIT_IM_CONTEXT(user_data); |
| 796 |
if (im_context != focused_im_context) |
| 797 |
return FALSE; |
| 798 |
|
| 799 |
DBG("im_context = %p string = %s cursorPos = %d", im_context, string, cursorPos); |
| 800 |
|
| 801 |
if (focused_im_context) { |
| 802 |
guint iter; |
| 803 |
PangoAttrList* attrs; |
| 804 |
|
| 805 |
g_free(focused_im_context->preedit_str); |
| 806 |
focused_im_context->preedit_str = g_strdup(string); |
| 807 |
|
| 808 |
if (cursorPos == -1) { |
| 809 |
cursorPos = g_utf8_strlen(string, -1); |
| 810 |
} |
| 811 |
focused_im_context->preedit_cursor_pos = cursorPos; |
| 812 |
|
| 813 |
|
| 814 |
attrs = pango_attr_list_new(); |
| 815 |
|
| 816 |
for (iter = 0; iter < g_variant_n_children(formatListData); ++iter) { |
| 817 |
gint start; |
| 818 |
gint length; |
| 819 |
MaliitPreeditFace preedit_face; |
| 820 |
gint byte_start; |
| 821 |
gint byte_end; |
| 822 |
PangoAttribute* new_attrs[2] = { NULL, NULL }; |
| 823 |
gint attr_iter; |
| 824 |
|
| 825 |
g_variant_get_child(formatListData, iter, "(iii)", &start, &length, &preedit_face); |
| 826 |
|
| 827 |
get_byte_range_from_unicode_offsets(string, start, length, &byte_start, &byte_end); |
| 828 |
|
| 829 |
switch (preedit_face) { |
| 830 |
case MaliitPreeditNoCandidates: |
| 831 |
new_attrs[0] = pango_attr_underline_new (PANGO_UNDERLINE_ERROR); |
| 832 |
new_attrs[1] = pango_attr_underline_color_new (65535, 0, 0); |
| 833 |
break; |
| 834 |
|
| 835 |
case MaliitPreeditUnconvertible: { |
| 836 |
const gint gray = (2 << 15) - 1; |
| 837 |
|
| 838 |
new_attrs[0] = pango_attr_foreground_new (gray, gray, gray); |
| 839 |
} break; |
| 840 |
|
| 841 |
case MaliitPreeditActive: |
| 842 |
new_attrs[0] = pango_attr_foreground_new(39168, 12800, 52224); |
| 843 |
new_attrs[1] = pango_attr_weight_new(PANGO_WEIGHT_BOLD); |
| 844 |
break; |
| 845 |
|
| 846 |
case MaliitPreeditKeyPress: |
| 847 |
case MaliitPreeditDefault: |
| 848 |
new_attrs[0] = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE); |
| 849 |
new_attrs[1] = pango_attr_underline_color_new(0, 0, 0); |
| 850 |
break; |
| 851 |
} |
| 852 |
|
| 853 |
for (attr_iter = 0; attr_iter < 2; ++attr_iter) { |
| 854 |
if (new_attrs[attr_iter]) { |
| 855 |
new_attrs[attr_iter]->start_index = byte_start; |
| 856 |
new_attrs[attr_iter]->end_index = byte_end; |
| 857 |
|
| 858 |
pango_attr_list_insert(attrs, new_attrs[attr_iter]); |
| 859 |
} |
| 860 |
} |
| 861 |
} |
| 862 |
|
| 863 |
if (focused_im_context->preedit_attrs) { |
| 864 |
pango_attr_list_unref (focused_im_context->preedit_attrs); |
| 865 |
} |
| 866 |
focused_im_context->preedit_attrs = attrs; |
| 867 |
|
| 868 |
g_signal_emit_by_name(focused_im_context, "preedit-changed"); |
| 869 |
|
| 870 |
maliit_context_complete_update_preedit(obj, invocation); |
| 871 |
return TRUE; |
| 872 |
} |
| 873 |
|
| 874 |
return FALSE; |
| 875 |
} |
| 876 |
|
| 877 |
gboolean |
| 878 |
maliit_im_context_key_event(MaliitContext *obj, |
| 879 |
GDBusMethodInvocation *invocation, |
| 880 |
gint type, |
| 881 |
gint key, |
| 882 |
gint modifiers, |
| 883 |
const gchar *text, |
| 884 |
gboolean auto_repeat G_GNUC_UNUSED, |
| 885 |
int count G_GNUC_UNUSED, |
| 886 |
guchar request_type G_GNUC_UNUSED, |
| 887 |
gpointer user_data) |
| 888 |
{ |
| 889 |
GdkEventKey *event = NULL; |
| 890 |
GdkWindow *window = NULL; |
| 891 |
|
| 892 |
STEP(); |
| 893 |
MaliitIMContext *im_context = MALIIT_IM_CONTEXT(user_data); |
| 894 |
if (im_context != focused_im_context) |
| 895 |
return FALSE; |
| 896 |
|
| 897 |
if (focused_im_context) |
| 898 |
window = focused_im_context->client_window; |
| 899 |
|
| 900 |
event = qt_key_event_to_gdk(type, key, modifiers, text, window); |
| 901 |
if (!event) |
| 902 |
return FALSE; |
| 903 |
|
| 904 |
event->send_event = TRUE; |
| 905 |
event->state |= IM_FORWARD_MASK; |
| 906 |
|
| 907 |
gdk_event_put((GdkEvent *)event); |
| 908 |
gdk_event_free((GdkEvent *)event); |
| 909 |
|
| 910 |
maliit_context_complete_key_event(obj, invocation); |
| 911 |
return TRUE; |
| 912 |
} |
| 913 |
|
| 914 |
static unsigned int |
| 915 |
find_signal(const char *action, const char *alternative, GtkWidget *widget) |
| 916 |
{ |
| 917 |
unsigned int signal = g_signal_lookup(action, G_OBJECT_TYPE(widget)); |
| 918 |
|
| 919 |
if (signal || alternative == NULL) { |
| 920 |
return signal; |
| 921 |
} |
| 922 |
|
| 923 |
return g_signal_lookup(alternative, G_OBJECT_TYPE(widget)); |
| 924 |
} |
| 925 |
|
| 926 |
void |
| 927 |
maliit_im_context_invoke_action(MaliitServer *obj G_GNUC_UNUSED, |
| 928 |
const char *action, |
| 929 |
const char *sequence G_GNUC_UNUSED, |
| 930 |
gpointer user_data) |
| 931 |
{ |
| 932 |
GtkWidget* widget = NULL; |
| 933 |
MaliitIMContext *im_context = MALIIT_IM_CONTEXT(user_data); |
| 934 |
|
| 935 |
if (im_context != focused_im_context) |
| 936 |
return; |
| 937 |
|
| 938 |
gdk_window_get_user_data (im_context->client_window, &user_data); |
| 939 |
widget = GTK_WIDGET (user_data); |
| 940 |
|
| 941 |
if (widget) { |
| 942 |
char *alternative = NULL; |
| 943 |
unsigned int signal; |
| 944 |
|
| 945 |
if (g_strcmp0(action, "copy") == 0 || |
| 946 |
g_strcmp0(action, "cut") == 0 || |
| 947 |
g_strcmp0(action, "paste") == 0) |
| 948 |
{ |
| 949 |
alternative = g_strdup_printf("%s-clipboard", action); |
| 950 |
} |
| 951 |
|
| 952 |
signal = find_signal(action, alternative, widget); |
| 953 |
g_free(alternative); |
| 954 |
|
| 955 |
if (signal) { |
| 956 |
g_signal_emit(widget, signal, 0); |
| 957 |
return; |
| 958 |
} |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
gboolean |
| 963 |
maliit_im_context_set_redirect_keys(MaliitContext *obj, |
| 964 |
GDBusMethodInvocation *invocation, |
| 965 |
gboolean enabled, |
| 966 |
gpointer user_data G_GNUC_UNUSED) |
| 967 |
{ |
| 968 |
DBG("enabled = %d", enabled); |
| 969 |
redirect_keys = enabled; |
| 970 |
maliit_context_complete_set_redirect_keys(obj, invocation); |
| 971 |
return TRUE; |
| 972 |
} |
| 973 |
|
| 974 |
gboolean |
| 975 |
maliit_im_context_notify_extended_attribute_changed (MaliitContext *obj, |
| 976 |
GDBusMethodInvocation *invocation, |
| 977 |
gint id, |
| 978 |
const gchar *target, |
| 979 |
const gchar *target_item, |
| 980 |
const gchar *attribute, |
| 981 |
GVariant *variant_value, |
| 982 |
gpointer user_data) |
| 983 |
{ |
| 984 |
MaliitIMContext *im_context = MALIIT_IM_CONTEXT(user_data); |
| 985 |
if (im_context != focused_im_context) |
| 986 |
return FALSE; |
| 987 |
|
| 988 |
maliit_attribute_extension_registry_update_attribute (focused_im_context->registry, |
| 989 |
id, |
| 990 |
target, |
| 991 |
target_item, |
| 992 |
attribute, |
| 993 |
variant_value); |
| 994 |
|
| 995 |
maliit_context_complete_notify_extended_attribute_changed(obj, invocation); |
| 996 |
return TRUE; |
| 997 |
} |
| 998 |
|
| 999 |
gboolean |
| 1000 |
maliit_im_context_update_input_method_area (MaliitContext *obj, |
| 1001 |
GDBusMethodInvocation *invocation, |
| 1002 |
gint x, |
| 1003 |
gint y, |
| 1004 |
gint width, |
| 1005 |
gint height, |
| 1006 |
gpointer user_data) |
| 1007 |
{ |
| 1008 |
MaliitIMContext *im_context = MALIIT_IM_CONTEXT(user_data); |
| 1009 |
GdkRectangle cursor_rect, osk_rect = { x, y, width, height }; |
| 1010 |
guint clear_area_id; |
| 1011 |
|
| 1012 |
if (!im_context->client_window) |
| 1013 |
return FALSE; |
| 1014 |
|
| 1015 |
if (im_context->keyboard_area.x == x && |
| 1016 |
im_context->keyboard_area.y == y && |
| 1017 |
im_context->keyboard_area.width == width && |
| 1018 |
im_context->keyboard_area.height == height) |
| 1019 |
return FALSE; |
| 1020 |
|
| 1021 |
clear_area_id = g_signal_lookup ("clear-area", GTK_TYPE_IM_CONTEXT); |
| 1022 |
|
| 1023 |
if (clear_area_id == 0) |
| 1024 |
return FALSE; |
| 1025 |
|
| 1026 |
im_context->keyboard_area = osk_rect; |
| 1027 |
|
| 1028 |
gdk_window_get_root_coords (im_context->client_window, |
| 1029 |
im_context->cursor_location.x, |
| 1030 |
im_context->cursor_location.y, |
| 1031 |
&cursor_rect.x, &cursor_rect.y); |
| 1032 |
cursor_rect.width = im_context->cursor_location.width; |
| 1033 |
cursor_rect.height = im_context->cursor_location.height; |
| 1034 |
|
| 1035 |
g_signal_emit (im_context, clear_area_id, 0, &osk_rect, &cursor_rect); |
| 1036 |
|
| 1037 |
maliit_context_complete_update_input_method_area(obj, invocation); |
| 1038 |
return TRUE; |
| 1039 |
} |