1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/base/ime/input_method_ibus.h"
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#undef FocusIn
#undef FocusOut
#include <algorithm>
#include <cstring>
#include <set>
#include <vector>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/chromeos/chromeos_version.h"
#include "base/i18n/char_iterator.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/third_party/icu/icu_utf.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/ibus/ibus_client.h"
#include "chromeos/dbus/ibus/ibus_input_context_client.h"
#include "chromeos/dbus/ibus/ibus_text.h"
#include "ui/base/events/event.h"
#include "ui/base/events/event_constants.h"
#include "ui/base/events/event_utils.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/base/keycodes/keyboard_code_conversion.h"
#include "ui/base/keycodes/keyboard_code_conversion_x.h"
#include "ui/base/keycodes/keyboard_codes.h"
#include "ui/gfx/rect.h"
namespace {
const int kIBusReleaseMask = 1 << 30;
const char kClientName[] = "chrome";
const int kMaxRetryCount = 10;
// Following capability mask is introduced from
// http://ibus.googlecode.com/svn/docs/ibus-1.4/ibus-ibustypes.html#IBusCapabilite
const uint32 kIBusCapabilityPreeditText = 1U;
const uint32 kIBusCapabilityFocus = 8U;
const uint32 kIBusCapabilitySurroundingText = 32U;
XKeyEvent* GetKeyEvent(XEvent* event) {
DCHECK(event && (event->type == KeyPress || event->type == KeyRelease));
return &event->xkey;
}
// Converts X (and ibus) state to event flags.
int EventFlagsFromXState(unsigned int state) {
return (state & LockMask ? ui::EF_CAPS_LOCK_DOWN : 0) |
(state & ControlMask ? ui::EF_CONTROL_DOWN : 0) |
(state & ShiftMask ? ui::EF_SHIFT_DOWN : 0) |
(state & Mod1Mask ? ui::EF_ALT_DOWN : 0) |
(state & Button1Mask ? ui::EF_LEFT_MOUSE_BUTTON : 0) |
(state & Button2Mask ? ui::EF_MIDDLE_MOUSE_BUTTON : 0) |
(state & Button3Mask ? ui::EF_RIGHT_MOUSE_BUTTON : 0);
}
// Converts X state to ibus key and button state.
uint32 IBusStateFromXState(unsigned int state) {
return (state & (LockMask | ControlMask | ShiftMask | Mod1Mask |
Button1Mask | Button2Mask | Button3Mask));
}
chromeos::IBusInputContextClient* GetInputContextClient() {
return chromeos::DBusThreadManager::Get()->GetIBusInputContextClient();
}
// Converts gfx::Rect to ibus::Rect.
chromeos::ibus::Rect GfxRectToIBusRect(const gfx::Rect& rect) {
return chromeos::ibus::Rect(rect.x(), rect.y(), rect.width(), rect.height());
}
} // namespace
namespace ui {
// InputMethodIBus implementation -----------------------------------------
InputMethodIBus::InputMethodIBus(
internal::InputMethodDelegate* delegate)
: input_context_state_(INPUT_CONTEXT_STOP),
create_input_context_fail_count_(0),
context_focused_(false),
composing_text_(false),
composition_changed_(false),
suppress_next_result_(false),
current_keyevent_id_(0),
weak_ptr_factory_(this) {
SetDelegate(delegate);
// chromeos::IBusDaemonController is not available in case of some testing,
// e.g. content_browser test can't initialize IBusDaemonController.
DCHECK(!base::chromeos::IsRunningOnChromeOS() ||
chromeos::IBusDaemonController::GetInstance());
if (chromeos::IBusDaemonController::GetInstance())
chromeos::IBusDaemonController::GetInstance()->AddObserver(this);
}
InputMethodIBus::~InputMethodIBus() {
AbandonAllPendingKeyEvents();
if (IsContextReady())
DestroyContext();
if (GetInputContextClient())
GetInputContextClient()->SetInputContextHandler(NULL);
if (chromeos::IBusDaemonController::GetInstance())
chromeos::IBusDaemonController::GetInstance()->RemoveObserver(this);
}
void InputMethodIBus::OnFocus() {
InputMethodBase::OnFocus();
UpdateContextFocusState();
}
void InputMethodIBus::OnBlur() {
ConfirmCompositionText();
InputMethodBase::OnBlur();
UpdateContextFocusState();
}
void InputMethodIBus::Init(bool focused) {
// Initializes the connection to ibus daemon. It may happen asynchronously,
// and as soon as the connection is established, the |context_| will be
// created automatically.
// Create the input context if the connection is already established.
if (IsConnected())
CreateContext();
InputMethodBase::Init(focused);
}
void InputMethodIBus::ProcessKeyEventDone(uint32 id,
XEvent* event,
uint32 ibus_keyval,
uint32 ibus_keycode,
uint32 ibus_state,
bool is_handled) {
DCHECK(event);
std::set<uint32>::iterator it = pending_key_events_.find(id);
if (it == pending_key_events_.end())
return; // Abandoned key event.
if (event->type == KeyPress) {
if (is_handled) {
// IME event has a priority to be handled, so that character composer
// should be reset.
character_composer_.Reset();
} else {
// If IME does not handle key event, passes keyevent to character composer
// to be able to compose complex characters.
is_handled = ExecuteCharacterComposer(ibus_keyval, ibus_keycode,
ibus_state);
}
}
if (event->type == KeyPress || event->type == KeyRelease)
ProcessKeyEventPostIME(event, ibus_state, is_handled);
// Do not use |it| for erasing, ProcessKeyEventPostIME may change the
// |pending_key_events_|.
pending_key_events_.erase(id);
}
bool InputMethodIBus::DispatchKeyEvent(const base::NativeEvent& native_event) {
DCHECK(native_event && (native_event->type == KeyPress ||
native_event->type == KeyRelease));
DCHECK(system_toplevel_window_focused());
uint32 ibus_keyval = 0;
uint32 ibus_keycode = 0;
uint32 ibus_state = 0;
IBusKeyEventFromNativeKeyEvent(
native_event,
&ibus_keyval, &ibus_keycode, &ibus_state);
// If |context_| is not usable, then we can only dispatch the key event as is.
// We also dispatch the key event directly if the current text input type is
// TEXT_INPUT_TYPE_PASSWORD, to bypass the input method.
// Note: We need to send the key event to ibus even if the |context_| is not
// enabled, so that ibus can have a chance to enable the |context_|.
if (!context_focused_ ||
GetTextInputType() == TEXT_INPUT_TYPE_PASSWORD ||
!GetInputContextClient() ||
GetInputContextClient()->IsXKBLayout()) {
if (native_event->type == KeyPress) {
if (ExecuteCharacterComposer(ibus_keyval, ibus_keycode, ibus_state)) {
// Treating as PostIME event if character composer handles key event and
// generates some IME event,
ProcessKeyEventPostIME(native_event, ibus_state, true);
return true;
}
ProcessUnfilteredKeyPressEvent(native_event, ibus_state);
} else {
DispatchKeyEventPostIME(native_event);
}
return true;
}
pending_key_events_.insert(current_keyevent_id_);
// Since |native_event| might be treated as XEvent whose size is bigger than
// XKeyEvent e.g. in CopyNativeEvent() in ui/base/events/event.cc, allocating
// |event| as XKeyEvent and casting it to XEvent is unsafe. crbug.com/151884
XEvent* event = new XEvent;
*event = *native_event;
const chromeos::IBusInputContextClient::ProcessKeyEventCallback callback =
base::Bind(&InputMethodIBus::ProcessKeyEventDone,
weak_ptr_factory_.GetWeakPtr(),
current_keyevent_id_,
base::Owned(event), // Pass the ownership of |event|.
ibus_keyval,
ibus_keycode,
ibus_state);
GetInputContextClient()->ProcessKeyEvent(ibus_keyval,
ibus_keycode,
ibus_state,
callback,
base::Bind(callback, false));
++current_keyevent_id_;
// We don't want to suppress the result generated by this key event, but it
// may cause problem. See comment in ResetContext() method.
suppress_next_result_ = false;
return true;
}
bool InputMethodIBus::DispatchFabricatedKeyEvent(const ui::KeyEvent& event) {
// TODO(bryeung): The fabricated events should also pass through IME.
if (event.type() == ET_KEY_PRESSED) {
ProcessUnfilteredFabricatedKeyPressEvent(
ET_KEY_PRESSED, event.key_code(), event.flags());
} else {
DispatchFabricatedKeyEventPostIME(
ET_KEY_RELEASED,
event.key_code(),
event.flags());
}
return true;
}
void InputMethodIBus::OnTextInputTypeChanged(const TextInputClient* client) {
if (IsContextReady() && IsTextInputClientFocused(client)) {
ResetContext();
UpdateContextFocusState();
}
InputMethodBase::OnTextInputTypeChanged(client);
}
void InputMethodIBus::OnCaretBoundsChanged(const TextInputClient* client) {
if (!context_focused_ || !IsTextInputClientFocused(client))
return;
// The current text input type should not be NONE if |context_| is focused.
DCHECK(!IsTextInputTypeNone());
const gfx::Rect rect = GetTextInputClient()->GetCaretBounds();
gfx::Rect composition_head;
if (!GetTextInputClient()->GetCompositionCharacterBounds(0,
&composition_head)) {
composition_head = rect;
}
GetInputContextClient()->SetCursorLocation(
GfxRectToIBusRect(rect),
GfxRectToIBusRect(composition_head));
ui::Range text_range;
ui::Range selection_range;
string16 surrounding_text;
if (!GetTextInputClient()->GetTextRange(&text_range) ||
!GetTextInputClient()->GetTextFromRange(text_range, &surrounding_text) ||
!GetTextInputClient()->GetSelectionRange(&selection_range)) {
previous_surrounding_text_.clear();
previous_selection_range_ = ui::Range::InvalidRange();
return;
}
if (previous_selection_range_ == selection_range &&
previous_surrounding_text_ == surrounding_text)
return;
previous_selection_range_ = selection_range;
previous_surrounding_text_ = surrounding_text;
if (!selection_range.IsValid()) {
// TODO(nona): Ideally selection_range should not be invalid.
// TODO(nona): If javascript changes the focus on page loading, even (0,0)
// can not be obtained. Need investigation.
return;
}
// Here SetSurroundingText accepts relative position of |surrounding_text|, so
// we have to convert |selection_range| from node coordinates to
// |surrounding_text| coordinates.
GetInputContextClient()->SetSurroundingText(
UTF16ToUTF8(surrounding_text),
selection_range.start() - text_range.start(),
selection_range.end() - text_range.start());
}
void InputMethodIBus::CancelComposition(const TextInputClient* client) {
if (context_focused_ && IsTextInputClientFocused(client))
ResetContext();
}
std::string InputMethodIBus::GetInputLocale() {
// Not supported.
return "";
}
base::i18n::TextDirection InputMethodIBus::GetInputTextDirection() {
// Not supported.
return base::i18n::UNKNOWN_DIRECTION;
}
bool InputMethodIBus::IsActive() {
return true;
}
void InputMethodIBus::OnWillChangeFocusedClient(TextInputClient* focused_before,
TextInputClient* focused) {
ConfirmCompositionText();
}
void InputMethodIBus::OnDidChangeFocusedClient(TextInputClient* focused_before,
TextInputClient* focused) {
// Force to update the input type since client's TextInputStateChanged()
// function might not be called if text input types before the client loses
// focus and after it acquires focus again are the same.
OnTextInputTypeChanged(focused);
UpdateContextFocusState();
// Force to update caret bounds, in case the client thinks that the caret
// bounds has not changed.
OnCaretBoundsChanged(focused);
}
void InputMethodIBus::CreateContext() {
DCHECK(IsConnected());
if (input_context_state_ != INPUT_CONTEXT_STOP) {
DVLOG(1) << "Input context is already created or waiting ibus-daemon"
" response.";
return;
}
input_context_state_ = INPUT_CONTEXT_WAIT_CREATE_INPUT_CONTEXT_RESPONSE;
// Creates the input context asynchronously.
DCHECK(!IsContextReady());
chromeos::DBusThreadManager::Get()->GetIBusClient()->CreateInputContext(
kClientName,
base::Bind(&InputMethodIBus::CreateInputContextDone,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&InputMethodIBus::CreateInputContextFail,
weak_ptr_factory_.GetWeakPtr()));
}
void InputMethodIBus::SetUpSignalHandlers() {
DCHECK(IsContextReady());
// We should reset the handler to NULL before |this| is deleted so handler
// functions are not called after |this| is deleted.
GetInputContextClient()->SetInputContextHandler(this);
GetInputContextClient()->SetCapabilities(
kIBusCapabilityPreeditText | kIBusCapabilityFocus |
kIBusCapabilitySurroundingText);
UpdateContextFocusState();
// Since ibus-daemon is launched in an on-demand basis on Chrome OS, RWHVA (or
// equivalents) might call OnCaretBoundsChanged() before the daemon starts. To
// save the case, call OnCaretBoundsChanged() here.
OnCaretBoundsChanged(GetTextInputClient());
OnInputMethodChanged();
}
void InputMethodIBus::DestroyContext() {
if (input_context_state_ == INPUT_CONTEXT_STOP)
return;
input_context_state_ = INPUT_CONTEXT_STOP;
chromeos::IBusInputContextClient* input_context = GetInputContextClient();
if (!input_context)
return;
if (input_context->IsObjectProxyReady()) {
// We can't use IsContextReady here because we want to destroy object proxy
// regardless of connection. The IsContextReady contains connection check.
ResetInputContext();
DCHECK(!IsContextReady());
}
}
void InputMethodIBus::ConfirmCompositionText() {
TextInputClient* client = GetTextInputClient();
if (client && client->HasCompositionText())
client->ConfirmCompositionText();
ResetContext();
}
void InputMethodIBus::ResetContext() {
if (!context_focused_ || !GetTextInputClient())
return;
DCHECK(system_toplevel_window_focused());
// Because ibus runs in asynchronous mode, the input method may still send us
// results after sending out the reset request, so we use a flag to discard
// all results generated by previous key events. But because ibus does not
// have a mechanism to identify each key event and corresponding results, this
// approach will not work for some corner cases. For example if the user types
// very fast, then the next key event may come in before the |context_| is
// really reset. Then we actually cannot know whether or not the next
// result should be discard.
suppress_next_result_ = true;
composition_.Clear();
result_text_.clear();
composing_text_ = false;
composition_changed_ = false;
// We need to abandon all pending key events, but as above comment says, there
// is no reliable way to abandon all results generated by these abandoned key
// events.
AbandonAllPendingKeyEvents();
// This function runs asynchronously.
// Note: some input method engines may not support reset method, such as
// ibus-anthy. But as we control all input method engines by ourselves, we can
// make sure that all of the engines we are using support it correctly.
GetInputContextClient()->Reset();
character_composer_.Reset();
}
void InputMethodIBus::UpdateContextFocusState() {
if (!IsContextReady()) {
context_focused_ = false;
return;
}
const bool old_context_focused = context_focused_;
// Use switch here in case we are going to add more text input types.
switch (GetTextInputType()) {
case TEXT_INPUT_TYPE_NONE:
case TEXT_INPUT_TYPE_PASSWORD:
context_focused_ = false;
break;
default:
context_focused_ = true;
break;
}
// We only focus in |context_| when the focus is in a normal textfield.
// ibus_input_context_focus_{in|out}() run asynchronously.
if (old_context_focused && !context_focused_)
GetInputContextClient()->FocusOut();
else if (!old_context_focused && context_focused_)
GetInputContextClient()->FocusIn();
if (context_focused_) {
uint32 capability = kIBusCapabilityFocus | kIBusCapabilitySurroundingText;
if (CanComposeInline())
capability |= kIBusCapabilityPreeditText;
GetInputContextClient()->SetCapabilities(capability);
}
}
void InputMethodIBus::ProcessKeyEventPostIME(
const base::NativeEvent& native_event,
uint32 ibus_state,
bool handled) {
TextInputClient* client = GetTextInputClient();
if (!client) {
// As ibus works asynchronously, there is a chance that the focused client
// loses focus before this method gets called.
DispatchKeyEventPostIME(native_event);
return;
}
if (native_event->type == KeyPress && handled)
ProcessFilteredKeyPressEvent(native_event);
// In case the focus was changed by the key event. The |context_| should have
// been reset when the focused window changed.
if (client != GetTextInputClient())
return;
if (HasInputMethodResult())
ProcessInputMethodResult(native_event, handled);
// In case the focus was changed when sending input method results to the
// focused window.
if (client != GetTextInputClient())
return;
if (native_event->type == KeyPress && !handled)
ProcessUnfilteredKeyPressEvent(native_event, ibus_state);
else if (native_event->type == KeyRelease)
DispatchKeyEventPostIME(native_event);
}
void InputMethodIBus::IBusKeyEventFromNativeKeyEvent(
const base::NativeEvent& native_event,
uint32* ibus_keyval,
uint32* ibus_keycode,
uint32* ibus_state) {
DCHECK(native_event); // A fabricated event is not supported here.
XKeyEvent* x_key = GetKeyEvent(native_event);
// Yes, ibus uses X11 keysym. We cannot use XLookupKeysym(), which doesn't
// translate Shift and CapsLock states.
KeySym keysym = NoSymbol;
::XLookupString(x_key, NULL, 0, &keysym, NULL);
*ibus_keyval = keysym;
*ibus_keycode = x_key->keycode;
*ibus_state = IBusStateFromXState(x_key->state);
if (native_event->type == KeyRelease)
*ibus_state |= kIBusReleaseMask;
}
void InputMethodIBus::ProcessFilteredKeyPressEvent(
const base::NativeEvent& native_event) {
if (NeedInsertChar())
DispatchKeyEventPostIME(native_event);
else
DispatchFabricatedKeyEventPostIME(
ET_KEY_PRESSED,
VKEY_PROCESSKEY,
EventFlagsFromXState(GetKeyEvent(native_event)->state));
}
void InputMethodIBus::ProcessUnfilteredKeyPressEvent(
const base::NativeEvent& native_event,
uint32 ibus_state) {
// For a fabricated event, ProcessUnfilteredFabricatedKeyPressEvent should be
// called instead.
DCHECK(native_event);
TextInputClient* client = GetTextInputClient();
DispatchKeyEventPostIME(native_event);
// We shouldn't dispatch the character anymore if the key event dispatch
// caused focus change. For example, in the following scenario,
// 1. visit a web page which has a <textarea>.
// 2. click Omnibox.
// 3. enable Korean IME, press A, then press Tab to move the focus to the web
// page.
// We should return here not to send the Tab key event to RWHV.
if (client != GetTextInputClient())
return;
const uint32 event_flags = EventFlagsFromXState(ibus_state);
// If a key event was not filtered by |context_| and |character_composer_|,
// then it means the key event didn't generate any result text. So we need
// to send corresponding character to the focused text input client.
client = GetTextInputClient();
uint16 ch = 0;
if (!(event_flags & ui::EF_CONTROL_DOWN))
ch = ui::GetCharacterFromXEvent(native_event);
if (!ch) {
ch = ui::GetCharacterFromKeyCode(
ui::KeyboardCodeFromNative(native_event), event_flags);
}
if (client && ch)
client->InsertChar(ch, event_flags);
}
void InputMethodIBus::ProcessUnfilteredFabricatedKeyPressEvent(
EventType type,
KeyboardCode key_code,
int event_flags) {
TextInputClient* client = GetTextInputClient();
DispatchFabricatedKeyEventPostIME(type, key_code, event_flags);
if (client != GetTextInputClient())
return;
client = GetTextInputClient();
const uint16 ch = ui::GetCharacterFromKeyCode(key_code, event_flags);
if (client && ch)
client->InsertChar(ch, event_flags);
}
void InputMethodIBus::ProcessInputMethodResult(
const base::NativeEvent& native_event,
bool handled) {
TextInputClient* client = GetTextInputClient();
DCHECK(client);
if (result_text_.length()) {
if (handled && NeedInsertChar()) {
const uint32 state =
EventFlagsFromXState(GetKeyEvent(native_event)->state);
for (string16::const_iterator i = result_text_.begin();
i != result_text_.end(); ++i) {
client->InsertChar(*i, state);
}
} else {
client->InsertText(result_text_);
composing_text_ = false;
}
}
if (composition_changed_ && !IsTextInputTypeNone()) {
if (composition_.text.length()) {
composing_text_ = true;
client->SetCompositionText(composition_);
} else if (result_text_.empty()) {
client->ClearCompositionText();
}
}
// We should not clear composition text here, as it may belong to the next
// composition session.
result_text_.clear();
composition_changed_ = false;
}
bool InputMethodIBus::NeedInsertChar() const {
return GetTextInputClient() &&
(IsTextInputTypeNone() ||
(!composing_text_ && result_text_.length() == 1));
}
bool InputMethodIBus::HasInputMethodResult() const {
return result_text_.length() || composition_changed_;
}
void InputMethodIBus::SendFakeProcessKeyEvent(bool pressed) const {
DispatchFabricatedKeyEventPostIME(pressed ? ET_KEY_PRESSED : ET_KEY_RELEASED,
VKEY_PROCESSKEY,
0);
}
void InputMethodIBus::AbandonAllPendingKeyEvents() {
pending_key_events_.clear();
}
void InputMethodIBus::CommitText(const chromeos::IBusText& text) {
if (suppress_next_result_ || text.text().empty())
return;
// We need to receive input method result even if the text input type is
// TEXT_INPUT_TYPE_NONE, to make sure we can always send correct
// character for each key event to the focused text input client.
if (!GetTextInputClient())
return;
const string16 utf16_text = UTF8ToUTF16(text.text());
if (utf16_text.empty())
return;
// Append the text to the buffer, because commit signal might be fired
// multiple times when processing a key event.
result_text_.append(utf16_text);
// If we are not handling key event, do not bother sending text result if the
// focused text input client does not support text input.
if (pending_key_events_.empty() && !IsTextInputTypeNone()) {
SendFakeProcessKeyEvent(true);
GetTextInputClient()->InsertText(utf16_text);
SendFakeProcessKeyEvent(false);
result_text_.clear();
}
}
void InputMethodIBus::ForwardKeyEvent(uint32 keyval,
uint32 keycode,
uint32 state) {
KeyboardCode ui_key_code = KeyboardCodeFromXKeysym(keyval);
if (!ui_key_code)
return;
const EventType event_type =
(state & kIBusReleaseMask) ? ET_KEY_RELEASED : ET_KEY_PRESSED;
const int event_flags = EventFlagsFromXState(state);
// It is not clear when the input method will forward us a fake key event.
// If there is a pending key event, then we may already received some input
// method results, so we dispatch this fake key event directly rather than
// calling ProcessKeyEventPostIME(), which will clear pending input method
// results.
if (event_type == ET_KEY_PRESSED) {
ProcessUnfilteredFabricatedKeyPressEvent(event_type, ui_key_code,
event_flags);
} else {
DispatchFabricatedKeyEventPostIME(event_type, ui_key_code, event_flags);
}
}
void InputMethodIBus::ShowPreeditText() {
if (suppress_next_result_ || IsTextInputTypeNone())
return;
composing_text_ = true;
}
void InputMethodIBus::UpdatePreeditText(const chromeos::IBusText& text,
uint32 cursor_pos,
bool visible) {
if (suppress_next_result_ || IsTextInputTypeNone())
return;
// |visible| argument is very confusing. For example, what's the correct
// behavior when:
// 1. OnUpdatePreeditText() is called with a text and visible == false, then
// 2. OnShowPreeditText() is called afterwards.
//
// If it's only for clearing the current preedit text, then why not just use
// OnHidePreeditText()?
if (!visible) {
HidePreeditText();
return;
}
ExtractCompositionText(text, cursor_pos, &composition_);
composition_changed_ = true;
// In case OnShowPreeditText() is not called.
if (composition_.text.length())
composing_text_ = true;
// If we receive a composition text without pending key event, then we need to
// send it to the focused text input client directly.
if (pending_key_events_.empty()) {
SendFakeProcessKeyEvent(true);
GetTextInputClient()->SetCompositionText(composition_);
SendFakeProcessKeyEvent(false);
composition_changed_ = false;
composition_.Clear();
}
}
void InputMethodIBus::HidePreeditText() {
if (composition_.text.empty() || IsTextInputTypeNone())
return;
// Intentionally leaves |composing_text_| unchanged.
composition_changed_ = true;
composition_.Clear();
if (pending_key_events_.empty()) {
TextInputClient* client = GetTextInputClient();
if (client && client->HasCompositionText())
client->ClearCompositionText();
composition_changed_ = false;
}
}
void InputMethodIBus::DeleteSurroundingText(int32 offset, uint32 length) {
if (!composition_.text.empty())
return; // do nothing if there is ongoing composition.
if (offset < 0 && static_cast<uint32>(-1 * offset) != length)
return; // only preceding text can be deletable.
if (GetTextInputClient())
GetTextInputClient()->ExtendSelectionAndDelete(length, 0U);
}
void InputMethodIBus::ResetInputContext() {
context_focused_ = false;
ConfirmCompositionText();
// We are dead, so we need to ask the client to stop relying on us.
OnInputMethodChanged();
GetInputContextClient()->ResetObjectProxy();
}
void InputMethodIBus::CreateInputContextDone(
const dbus::ObjectPath& object_path) {
DCHECK_NE(INPUT_CONTEXT_RUNNING, input_context_state_);
if (input_context_state_ == INPUT_CONTEXT_STOP) {
// DestroyContext has already been called.
return;
}
chromeos::DBusThreadManager::Get()->GetIBusInputContextClient()
->Initialize(chromeos::DBusThreadManager::Get()->GetIBusBus(),
object_path);
input_context_state_ = INPUT_CONTEXT_RUNNING;
DCHECK(IsContextReady());
SetUpSignalHandlers();
}
void InputMethodIBus::CreateInputContextFail() {
DCHECK_NE(INPUT_CONTEXT_RUNNING, input_context_state_);
if (input_context_state_ == INPUT_CONTEXT_STOP) {
// CreateInputContext failed but the input context is no longer
// necessary, thus do nothing.
return;
}
if (++create_input_context_fail_count_ >= kMaxRetryCount) {
DVLOG(1) << "CreateInputContext failed even tried "
<< kMaxRetryCount << " times, give up.";
create_input_context_fail_count_ = 0;
input_context_state_ = INPUT_CONTEXT_STOP;
return;
}
// Try CreateInputContext again.
chromeos::DBusThreadManager::Get()->GetIBusClient()->CreateInputContext(
kClientName,
base::Bind(&InputMethodIBus::CreateInputContextDone,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&InputMethodIBus::CreateInputContextFail,
weak_ptr_factory_.GetWeakPtr()));
}
bool InputMethodIBus::IsConnected() {
return chromeos::DBusThreadManager::Get()->GetIBusBus() != NULL;
}
bool InputMethodIBus::IsContextReady() {
if (!IsConnected())
return false;
if (!GetInputContextClient())
return false;
return GetInputContextClient()->IsObjectProxyReady();
}
bool InputMethodIBus::ExecuteCharacterComposer(uint32 ibus_keyval,
uint32 ibus_keycode,
uint32 ibus_state) {
if (!character_composer_.FilterKeyPress(ibus_keyval,
ibus_keycode,
EventFlagsFromXState(ibus_state))) {
return false;
}
suppress_next_result_ = false;
chromeos::IBusText preedit;
preedit.set_text(
UTF16ToUTF8(character_composer_.preedit_string()));
UpdatePreeditText(preedit, preedit.text().size(),
!preedit.text().empty());
std::string commit_text =
UTF16ToUTF8(character_composer_.composed_character());
if (!commit_text.empty()) {
chromeos::IBusText ibus_text;
ibus_text.set_text(commit_text);
CommitText(ibus_text);
}
return true;
}
void InputMethodIBus::OnConnected() {
DCHECK(IsConnected());
// If already input context is initialized, do nothing.
if (IsContextReady())
return;
DestroyContext();
CreateContext();
}
void InputMethodIBus::OnDisconnected() {
DestroyContext();
}
void InputMethodIBus::ExtractCompositionText(
const chromeos::IBusText& text,
uint32 cursor_position,
CompositionText* out_composition) const {
out_composition->Clear();
out_composition->text = UTF8ToUTF16(text.text());
if (out_composition->text.empty())
return;
// ibus uses character index for cursor position and attribute range, but we
// use char16 offset for them. So we need to do conversion here.
std::vector<size_t> char16_offsets;
size_t length = out_composition->text.length();
base::i18n::UTF16CharIterator char_iterator(&out_composition->text);
do {
char16_offsets.push_back(char_iterator.array_pos());
} while (char_iterator.Advance());
// The text length in Unicode characters.
uint32 char_length = static_cast<uint32>(char16_offsets.size());
// Make sure we can convert the value of |char_length| as well.
char16_offsets.push_back(length);
size_t cursor_offset =
char16_offsets[std::min(char_length, cursor_position)];
out_composition->selection = Range(cursor_offset);
const std::vector<chromeos::IBusText::UnderlineAttribute>&
underline_attributes = text.underline_attributes();
const std::vector<chromeos::IBusText::SelectionAttribute>&
selection_attributes = text.selection_attributes();
if (!underline_attributes.empty()) {
for (size_t i = 0; i < underline_attributes.size(); ++i) {
const uint32 start = underline_attributes[i].start_index;
const uint32 end = underline_attributes[i].end_index;
if (start >= end)
continue;
CompositionUnderline underline(
char16_offsets[start], char16_offsets[end],
SK_ColorBLACK, false /* thick */);
if (underline_attributes[i].type ==
chromeos::IBusText::IBUS_TEXT_UNDERLINE_DOUBLE)
underline.thick = true;
else if (underline_attributes[i].type ==
chromeos::IBusText::IBUS_TEXT_UNDERLINE_ERROR)
underline.color = SK_ColorRED;
out_composition->underlines.push_back(underline);
}
}
if (!selection_attributes.empty()) {
LOG_IF(ERROR, selection_attributes.size() != 1)
<< "Chrome does not support multiple selection";
for (uint32 i = 0; i < selection_attributes.size(); ++i) {
const uint32 start = selection_attributes[i].start_index;
const uint32 end = selection_attributes[i].end_index;
if (start >= end)
continue;
CompositionUnderline underline(
char16_offsets[start], char16_offsets[end],
SK_ColorBLACK, true /* thick */);
out_composition->underlines.push_back(underline);
// If the cursor is at start or end of this underline, then we treat
// it as the selection range as well, but make sure to set the cursor
// position to the selection end.
if (underline.start_offset == cursor_offset) {
out_composition->selection.set_start(underline.end_offset);
out_composition->selection.set_end(cursor_offset);
} else if (underline.end_offset == cursor_offset) {
out_composition->selection.set_start(underline.start_offset);
out_composition->selection.set_end(cursor_offset);
}
}
}
// Use a black thin underline by default.
if (out_composition->underlines.empty()) {
out_composition->underlines.push_back(CompositionUnderline(
0, length, SK_ColorBLACK, false /* thick */));
}
}
} // namespace ui
|