forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIMContextWrapper.cpp
2333 lines (2059 loc) · 81.9 KB
/
IMContextWrapper.cpp
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
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set ts=4 et sw=4 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/Logging.h"
#include "prtime.h"
#include "IMContextWrapper.h"
#include "nsGtkKeyUtils.h"
#include "nsWindow.h"
#include "mozilla/AutoRestore.h"
#include "mozilla/Likely.h"
#include "mozilla/MiscEvents.h"
#include "mozilla/Preferences.h"
#include "mozilla/TextEventDispatcher.h"
#include "mozilla/TextEvents.h"
#include "WritingModes.h"
namespace mozilla {
namespace widget {
PRLogModuleInfo* gGtkIMLog = nullptr;
static inline const char*
ToChar(bool aBool)
{
return aBool ? "true" : "false";
}
static const char*
GetRangeTypeName(uint32_t aRangeType)
{
switch (aRangeType) {
case NS_TEXTRANGE_RAWINPUT:
return "NS_TEXTRANGE_RAWINPUT";
case NS_TEXTRANGE_CONVERTEDTEXT:
return "NS_TEXTRANGE_CONVERTEDTEXT";
case NS_TEXTRANGE_SELECTEDRAWTEXT:
return "NS_TEXTRANGE_SELECTEDRAWTEXT";
case NS_TEXTRANGE_SELECTEDCONVERTEDTEXT:
return "NS_TEXTRANGE_SELECTEDCONVERTEDTEXT";
case NS_TEXTRANGE_CARETPOSITION:
return "NS_TEXTRANGE_CARETPOSITION";
default:
return "UNKNOWN SELECTION TYPE!!";
}
}
static const char*
GetEnabledStateName(uint32_t aState)
{
switch (aState) {
case IMEState::DISABLED:
return "DISABLED";
case IMEState::ENABLED:
return "ENABLED";
case IMEState::PASSWORD:
return "PASSWORD";
case IMEState::PLUGIN:
return "PLUG_IN";
default:
return "UNKNOWN ENABLED STATUS!!";
}
}
static const char*
GetEventType(GdkEventKey* aKeyEvent)
{
switch (aKeyEvent->type) {
case GDK_KEY_PRESS:
return "GDK_KEY_PRESS";
case GDK_KEY_RELEASE:
return "GDK_KEY_RELEASE";
default:
return "Unknown";
}
}
class GetWritingModeName : public nsAutoCString
{
public:
explicit GetWritingModeName(const WritingMode& aWritingMode)
{
if (!aWritingMode.IsVertical()) {
AssignLiteral("Horizontal");
return;
}
if (aWritingMode.IsVerticalLR()) {
AssignLiteral("Vertical (LTR)");
return;
}
AssignLiteral("Vertical (RTL)");
}
virtual ~GetWritingModeName() {}
};
class GetTextRangeStyleText final : public nsAutoCString
{
public:
explicit GetTextRangeStyleText(const TextRangeStyle& aStyle)
{
if (!aStyle.IsDefined()) {
AssignLiteral("{ IsDefined()=false }");
return;
}
if (aStyle.IsLineStyleDefined()) {
AppendLiteral("{ mLineStyle=");
AppendLineStyle(aStyle.mLineStyle);
if (aStyle.IsUnderlineColorDefined()) {
AppendLiteral(", mUnderlineColor=");
AppendColor(aStyle.mUnderlineColor);
} else {
AppendLiteral(", IsUnderlineColorDefined=false");
}
} else {
AppendLiteral("{ IsLineStyleDefined()=false");
}
if (aStyle.IsForegroundColorDefined()) {
AppendLiteral(", mForegroundColor=");
AppendColor(aStyle.mForegroundColor);
} else {
AppendLiteral(", IsForegroundColorDefined()=false");
}
if (aStyle.IsBackgroundColorDefined()) {
AppendLiteral(", mBackgroundColor=");
AppendColor(aStyle.mBackgroundColor);
} else {
AppendLiteral(", IsBackgroundColorDefined()=false");
}
AppendLiteral(" }");
}
void AppendLineStyle(uint8_t aLineStyle)
{
switch (aLineStyle) {
case TextRangeStyle::LINESTYLE_NONE:
AppendLiteral("LINESTYLE_NONE");
break;
case TextRangeStyle::LINESTYLE_SOLID:
AppendLiteral("LINESTYLE_SOLID");
break;
case TextRangeStyle::LINESTYLE_DOTTED:
AppendLiteral("LINESTYLE_DOTTED");
break;
case TextRangeStyle::LINESTYLE_DASHED:
AppendLiteral("LINESTYLE_DASHED");
break;
case TextRangeStyle::LINESTYLE_DOUBLE:
AppendLiteral("LINESTYLE_DOUBLE");
break;
case TextRangeStyle::LINESTYLE_WAVY:
AppendLiteral("LINESTYLE_WAVY");
break;
default:
AppendPrintf("Invalid(0x%02X)", aLineStyle);
break;
}
}
void AppendColor(nscolor aColor)
{
AppendPrintf("{ R=0x%02X, G=0x%02X, B=0x%02X, A=0x%02X }",
NS_GET_R(aColor), NS_GET_G(aColor), NS_GET_B(aColor),
NS_GET_A(aColor));
}
virtual ~GetTextRangeStyleText() {};
};
const static bool kUseSimpleContextDefault = MOZ_WIDGET_GTK == 2;
/******************************************************************************
* IMContextWrapper
******************************************************************************/
IMContextWrapper* IMContextWrapper::sLastFocusedContext = nullptr;
bool IMContextWrapper::sUseSimpleContext;
NS_IMPL_ISUPPORTS(IMContextWrapper,
TextEventDispatcherListener,
nsISupportsWeakReference)
IMContextWrapper::IMContextWrapper(nsWindow* aOwnerWindow)
: mOwnerWindow(aOwnerWindow)
, mLastFocusedWindow(nullptr)
, mContext(nullptr)
, mSimpleContext(nullptr)
, mDummyContext(nullptr)
, mComposingContext(nullptr)
, mCompositionStart(UINT32_MAX)
, mProcessingKeyEvent(nullptr)
, mCompositionState(eCompositionState_NotComposing)
, mIsIMFocused(false)
, mIsDeletingSurrounding(false)
, mLayoutChanged(false)
, mSetCursorPositionOnKeyEvent(true)
, mPendingResettingIMContext(false)
{
if (!gGtkIMLog) {
gGtkIMLog = PR_NewLogModule("nsGtkIMModuleWidgets");
}
static bool sFirstInstance = true;
if (sFirstInstance) {
sFirstInstance = false;
sUseSimpleContext =
Preferences::GetBool(
"intl.ime.use_simple_context_on_password_field",
kUseSimpleContextDefault);
}
Init();
}
void
IMContextWrapper::Init()
{
MOZ_LOG(gGtkIMLog, LogLevel::Info,
("GTKIM: %p Init(), mOwnerWindow=%p",
this, mOwnerWindow));
MozContainer* container = mOwnerWindow->GetMozContainer();
NS_PRECONDITION(container, "container is null");
GdkWindow* gdkWindow = gtk_widget_get_window(GTK_WIDGET(container));
// NOTE: gtk_im_*_new() abort (kill) the whole process when it fails.
// So, we don't need to check the result.
// Normal context.
mContext = gtk_im_multicontext_new();
gtk_im_context_set_client_window(mContext, gdkWindow);
g_signal_connect(mContext, "preedit_changed",
G_CALLBACK(IMContextWrapper::OnChangeCompositionCallback), this);
g_signal_connect(mContext, "retrieve_surrounding",
G_CALLBACK(IMContextWrapper::OnRetrieveSurroundingCallback), this);
g_signal_connect(mContext, "delete_surrounding",
G_CALLBACK(IMContextWrapper::OnDeleteSurroundingCallback), this);
g_signal_connect(mContext, "commit",
G_CALLBACK(IMContextWrapper::OnCommitCompositionCallback), this);
g_signal_connect(mContext, "preedit_start",
G_CALLBACK(IMContextWrapper::OnStartCompositionCallback), this);
g_signal_connect(mContext, "preedit_end",
G_CALLBACK(IMContextWrapper::OnEndCompositionCallback), this);
// Simple context
if (sUseSimpleContext) {
mSimpleContext = gtk_im_context_simple_new();
gtk_im_context_set_client_window(mSimpleContext, gdkWindow);
g_signal_connect(mSimpleContext, "preedit_changed",
G_CALLBACK(&IMContextWrapper::OnChangeCompositionCallback),
this);
g_signal_connect(mSimpleContext, "retrieve_surrounding",
G_CALLBACK(&IMContextWrapper::OnRetrieveSurroundingCallback),
this);
g_signal_connect(mSimpleContext, "delete_surrounding",
G_CALLBACK(&IMContextWrapper::OnDeleteSurroundingCallback),
this);
g_signal_connect(mSimpleContext, "commit",
G_CALLBACK(&IMContextWrapper::OnCommitCompositionCallback),
this);
g_signal_connect(mSimpleContext, "preedit_start",
G_CALLBACK(IMContextWrapper::OnStartCompositionCallback),
this);
g_signal_connect(mSimpleContext, "preedit_end",
G_CALLBACK(IMContextWrapper::OnEndCompositionCallback),
this);
}
// Dummy context
mDummyContext = gtk_im_multicontext_new();
gtk_im_context_set_client_window(mDummyContext, gdkWindow);
}
IMContextWrapper::~IMContextWrapper()
{
if (this == sLastFocusedContext) {
sLastFocusedContext = nullptr;
}
MOZ_LOG(gGtkIMLog, LogLevel::Info,
("GTKIM: %p ~IMContextWrapper()", this));
}
NS_IMETHODIMP
IMContextWrapper::NotifyIME(TextEventDispatcher* aTextEventDispatcher,
const IMENotification& aNotification)
{
switch (aNotification.mMessage) {
case REQUEST_TO_COMMIT_COMPOSITION:
case REQUEST_TO_CANCEL_COMPOSITION: {
nsWindow* window =
static_cast<nsWindow*>(aTextEventDispatcher->GetWidget());
return EndIMEComposition(window);
}
case NOTIFY_IME_OF_FOCUS:
OnFocusChangeInGecko(true);
return NS_OK;
case NOTIFY_IME_OF_BLUR:
OnFocusChangeInGecko(false);
return NS_OK;
case NOTIFY_IME_OF_POSITION_CHANGE:
OnLayoutChange();
return NS_OK;
case NOTIFY_IME_OF_COMPOSITION_UPDATE:
OnUpdateComposition();
return NS_OK;
case NOTIFY_IME_OF_SELECTION_CHANGE: {
nsWindow* window =
static_cast<nsWindow*>(aTextEventDispatcher->GetWidget());
OnSelectionChange(window, aNotification);
return NS_OK;
}
default:
return NS_ERROR_NOT_IMPLEMENTED;
}
}
NS_IMETHODIMP_(void)
IMContextWrapper::OnRemovedFrom(TextEventDispatcher* aTextEventDispatcher)
{
// XXX When input transaction is being stolen by add-on, what should we do?
}
NS_IMETHODIMP_(void)
IMContextWrapper::WillDispatchKeyboardEvent(
TextEventDispatcher* aTextEventDispatcher,
WidgetKeyboardEvent& aKeyboardEvent,
uint32_t aIndexOfKeypress,
void* aData)
{
KeymapWrapper::WillDispatchKeyboardEvent(aKeyboardEvent,
static_cast<GdkEventKey*>(aData));
}
TextEventDispatcher*
IMContextWrapper::GetTextEventDispatcher()
{
if (NS_WARN_IF(!mLastFocusedWindow)) {
return nullptr;
}
TextEventDispatcher* dispatcher =
mLastFocusedWindow->GetTextEventDispatcher();
// nsIWidget::GetTextEventDispatcher() shouldn't return nullptr.
MOZ_RELEASE_ASSERT(dispatcher);
return dispatcher;
}
nsIMEUpdatePreference
IMContextWrapper::GetIMEUpdatePreference() const
{
// While a plugin has focus, IMContextWrapper doesn't need any
// notifications.
if (mInputContext.mIMEState.mEnabled == IMEState::PLUGIN) {
return nsIMEUpdatePreference();
}
nsIMEUpdatePreference::Notifications notifications =
nsIMEUpdatePreference::NOTIFY_SELECTION_CHANGE;
// If it's not enabled, we don't need position change notification.
if (IsEnabled()) {
notifications |= nsIMEUpdatePreference::NOTIFY_POSITION_CHANGE;
}
nsIMEUpdatePreference updatePreference(notifications);
// We shouldn't notify IME of selection change caused by changes of
// composition string. Therefore, we don't need to be notified selection
// changes which are caused by compositionchange events handled.
updatePreference.DontNotifyChangesCausedByComposition();
return updatePreference;
}
void
IMContextWrapper::OnDestroyWindow(nsWindow* aWindow)
{
MOZ_LOG(gGtkIMLog, LogLevel::Info,
("GTKIM: %p OnDestroyWindow(aWindow=%p), mLastFocusedWindow=%p, "
"mOwnerWindow=%p, mLastFocusedModule=%p",
this, aWindow, mLastFocusedWindow, mOwnerWindow, sLastFocusedContext));
NS_PRECONDITION(aWindow, "aWindow must not be null");
if (mLastFocusedWindow == aWindow) {
EndIMEComposition(aWindow);
if (mIsIMFocused) {
Blur();
}
mLastFocusedWindow = nullptr;
}
if (mOwnerWindow != aWindow) {
return;
}
if (sLastFocusedContext == this) {
sLastFocusedContext = nullptr;
}
/**
* NOTE:
* The given window is the owner of this, so, we must release the
* contexts now. But that might be referred from other nsWindows
* (they are children of this. But we don't know why there are the
* cases). So, we need to clear the pointers that refers to contexts
* and this if the other referrers are still alive. See bug 349727.
*/
if (mContext) {
PrepareToDestroyContext(mContext);
gtk_im_context_set_client_window(mContext, nullptr);
g_object_unref(mContext);
mContext = nullptr;
}
if (mSimpleContext) {
gtk_im_context_set_client_window(mSimpleContext, nullptr);
g_object_unref(mSimpleContext);
mSimpleContext = nullptr;
}
if (mDummyContext) {
// mContext and mDummyContext have the same slaveType and signal_data
// so no need for another workaround_gtk_im_display_closed.
gtk_im_context_set_client_window(mDummyContext, nullptr);
g_object_unref(mDummyContext);
mDummyContext = nullptr;
}
if (NS_WARN_IF(mComposingContext)) {
g_object_unref(mComposingContext);
mComposingContext = nullptr;
}
mOwnerWindow = nullptr;
mLastFocusedWindow = nullptr;
mInputContext.mIMEState.mEnabled = IMEState::DISABLED;
MOZ_LOG(gGtkIMLog, LogLevel::Debug,
("GTKIM: %p OnDestroyWindow(), succeeded, Completely destroyed",
this));
}
// Work around gtk bug http://bugzilla.gnome.org/show_bug.cgi?id=483223:
// (and the similar issue of GTK+ IIIM)
// The GTK+ XIM and IIIM modules register handlers for the "closed" signal
// on the display, but:
// * The signal handlers are not disconnected when the module is unloaded.
//
// The GTK+ XIM module has another problem:
// * When the signal handler is run (with the module loaded) it tries
// XFree (and fails) on a pointer that did not come from Xmalloc.
//
// To prevent these modules from being unloaded, use static variables to
// hold ref of GtkIMContext class.
// For GTK+ XIM module, to prevent the signal handler from being run,
// find the signal handlers and remove them.
//
// GtkIMContextXIMs share XOpenIM connections and display closed signal
// handlers (where possible).
void
IMContextWrapper::PrepareToDestroyContext(GtkIMContext* aContext)
{
#if (MOZ_WIDGET_GTK == 2)
GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT(aContext);
GtkIMContext *slave = multicontext->slave;
#else
GtkIMContext *slave = nullptr; //TODO GTK3
#endif
if (!slave) {
return;
}
GType slaveType = G_TYPE_FROM_INSTANCE(slave);
const gchar *im_type_name = g_type_name(slaveType);
if (strcmp(im_type_name, "GtkIMContextIIIM") == 0) {
// Add a reference to prevent the IIIM module from being unloaded
static gpointer gtk_iiim_context_class =
g_type_class_ref(slaveType);
// Mute unused variable warning:
(void)gtk_iiim_context_class;
}
}
void
IMContextWrapper::OnFocusWindow(nsWindow* aWindow)
{
if (MOZ_UNLIKELY(IsDestroyed())) {
return;
}
MOZ_LOG(gGtkIMLog, LogLevel::Info,
("GTKIM: %p OnFocusWindow(aWindow=%p), mLastFocusedWindow=%p",
this, aWindow, mLastFocusedWindow));
mLastFocusedWindow = aWindow;
Focus();
}
void
IMContextWrapper::OnBlurWindow(nsWindow* aWindow)
{
if (MOZ_UNLIKELY(IsDestroyed())) {
return;
}
MOZ_LOG(gGtkIMLog, LogLevel::Info,
("GTKIM: %p OnBlurWindow(aWindow=%p), mLastFocusedWindow=%p, "
"mIsIMFocused=%s",
this, aWindow, mLastFocusedWindow, ToChar(mIsIMFocused)));
if (!mIsIMFocused || mLastFocusedWindow != aWindow) {
return;
}
Blur();
}
bool
IMContextWrapper::OnKeyEvent(nsWindow* aCaller,
GdkEventKey* aEvent,
bool aKeyDownEventWasSent /* = false */)
{
NS_PRECONDITION(aEvent, "aEvent must be non-null");
if (!mInputContext.mIMEState.MaybeEditable() ||
MOZ_UNLIKELY(IsDestroyed())) {
return false;
}
MOZ_LOG(gGtkIMLog, LogLevel::Info,
("GTKIM: %p OnKeyEvent(aCaller=%p, aKeyDownEventWasSent=%s), "
"mCompositionState=%s, current context=%p, active context=%p, "
"aEvent(%p): { type=%s, keyval=%s, unicode=0x%X }",
this, aCaller, ToChar(aKeyDownEventWasSent),
GetCompositionStateName(), GetCurrentContext(), GetActiveContext(),
aEvent, GetEventType(aEvent), gdk_keyval_name(aEvent->keyval),
gdk_keyval_to_unicode(aEvent->keyval)));
if (aCaller != mLastFocusedWindow) {
MOZ_LOG(gGtkIMLog, LogLevel::Error,
("GTKIM: %p OnKeyEvent(), FAILED, the caller isn't focused "
"window, mLastFocusedWindow=%p",
this, mLastFocusedWindow));
return false;
}
// Even if old IM context has composition, key event should be sent to
// current context since the user expects so.
GtkIMContext* currentContext = GetCurrentContext();
if (MOZ_UNLIKELY(!currentContext)) {
MOZ_LOG(gGtkIMLog, LogLevel::Error,
("GTKIM: %p OnKeyEvent(), FAILED, there are no context",
this));
return false;
}
if (mSetCursorPositionOnKeyEvent) {
SetCursorPosition(currentContext);
mSetCursorPositionOnKeyEvent = false;
}
mKeyDownEventWasSent = aKeyDownEventWasSent;
mFilterKeyEvent = true;
mProcessingKeyEvent = aEvent;
gboolean isFiltered =
gtk_im_context_filter_keypress(currentContext, aEvent);
mProcessingKeyEvent = nullptr;
// We filter the key event if the event was not committed (because
// it's probably part of a composition) or if the key event was
// committed _and_ changed. This way we still let key press
// events go through as simple key press events instead of
// composed characters.
bool filterThisEvent = isFiltered && mFilterKeyEvent;
if (IsComposingOnCurrentContext() && !isFiltered) {
if (aEvent->type == GDK_KEY_PRESS) {
if (!mDispatchedCompositionString.IsEmpty()) {
// If there is composition string, we shouldn't dispatch
// any keydown events during composition.
filterThisEvent = true;
} else {
// A Hangul input engine for SCIM doesn't emit preedit_end
// signal even when composition string becomes empty. On the
// other hand, we should allow to make composition with empty
// string for other languages because there *might* be such
// IM. For compromising this issue, we should dispatch
// compositionend event, however, we don't need to reset IM
// actually.
DispatchCompositionCommitEvent(currentContext, &EmptyString());
filterThisEvent = false;
}
} else {
// Key release event may not be consumed by IM, however, we
// shouldn't dispatch any keyup event during composition.
filterThisEvent = true;
}
}
MOZ_LOG(gGtkIMLog, LogLevel::Debug,
("GTKIM: %p OnKeyEvent(), succeeded, filterThisEvent=%s "
"(isFiltered=%s, mFilterKeyEvent=%s), mCompositionState=%s",
this, ToChar(filterThisEvent), ToChar(isFiltered),
ToChar(mFilterKeyEvent), GetCompositionStateName()));
return filterThisEvent;
}
void
IMContextWrapper::OnFocusChangeInGecko(bool aFocus)
{
MOZ_LOG(gGtkIMLog, LogLevel::Info,
("GTKIM: %p OnFocusChangeInGecko(aFocus=%s), "
"mCompositionState=%s, mIsIMFocused=%s",
this, ToChar(aFocus), GetCompositionStateName(),
ToChar(mIsIMFocused)));
// We shouldn't carry over the removed string to another editor.
mSelectedString.Truncate();
mSelection.Clear();
}
void
IMContextWrapper::ResetIME()
{
MOZ_LOG(gGtkIMLog, LogLevel::Info,
("GTKIM: %p ResetIME(), mCompositionState=%s, mIsIMFocused=%s",
this, GetCompositionStateName(), ToChar(mIsIMFocused)));
GtkIMContext* activeContext = GetActiveContext();
if (MOZ_UNLIKELY(!activeContext)) {
MOZ_LOG(gGtkIMLog, LogLevel::Error,
("GTKIM: %p ResetIME(), FAILED, there are no context",
this));
return;
}
RefPtr<IMContextWrapper> kungFuDeathGrip(this);
RefPtr<nsWindow> lastFocusedWindow(mLastFocusedWindow);
mPendingResettingIMContext = false;
gtk_im_context_reset(activeContext);
// The last focused window might have been destroyed by a DOM event handler
// which was called by us during a call of gtk_im_context_reset().
if (!lastFocusedWindow ||
NS_WARN_IF(lastFocusedWindow != mLastFocusedWindow) ||
lastFocusedWindow->Destroyed()) {
return;
}
nsAutoString compositionString;
GetCompositionString(activeContext, compositionString);
MOZ_LOG(gGtkIMLog, LogLevel::Debug,
("GTKIM: %p ResetIME() called gtk_im_context_reset(), "
"activeContext=%p, mCompositionState=%s, compositionString=%s, "
"mIsIMFocused=%s",
this, activeContext, GetCompositionStateName(),
NS_ConvertUTF16toUTF8(compositionString).get(),
ToChar(mIsIMFocused)));
// XXX IIIMF (ATOK X3 which is one of the Language Engine of it is still
// used in Japan!) sends only "preedit_changed" signal with empty
// composition string synchronously. Therefore, if composition string
// is now empty string, we should assume that the IME won't send
// "commit" signal.
if (IsComposing() && compositionString.IsEmpty()) {
// WARNING: The widget might have been gone after this.
DispatchCompositionCommitEvent(activeContext, &EmptyString());
}
}
nsresult
IMContextWrapper::EndIMEComposition(nsWindow* aCaller)
{
if (MOZ_UNLIKELY(IsDestroyed())) {
return NS_OK;
}
MOZ_LOG(gGtkIMLog, LogLevel::Info,
("GTKIM: %p EndIMEComposition(aCaller=%p), "
"mCompositionState=%s",
this, aCaller, GetCompositionStateName()));
if (aCaller != mLastFocusedWindow) {
MOZ_LOG(gGtkIMLog, LogLevel::Error,
("GTKIM: %p EndIMEComposition(), FAILED, the caller isn't "
"focused window, mLastFocusedWindow=%p",
this, mLastFocusedWindow));
return NS_OK;
}
if (!IsComposing()) {
return NS_OK;
}
// Currently, GTK has API neither to commit nor to cancel composition
// forcibly. Therefore, TextComposition will recompute commit string for
// the request even if native IME will cause unexpected commit string.
// So, we don't need to emulate commit or cancel composition with
// proper composition events.
// XXX ResetIME() might not enough for finishing compositoin on some
// environments. We should emulate focus change too because some IMEs
// may commit or cancel composition at blur.
ResetIME();
return NS_OK;
}
void
IMContextWrapper::OnLayoutChange()
{
if (MOZ_UNLIKELY(IsDestroyed())) {
return;
}
if (IsComposing()) {
SetCursorPosition(GetActiveContext());
} else {
// If not composing, candidate window position is updated before key
// down
mSetCursorPositionOnKeyEvent = true;
}
mLayoutChanged = true;
}
void
IMContextWrapper::OnUpdateComposition()
{
if (MOZ_UNLIKELY(IsDestroyed())) {
return;
}
if (!IsComposing()) {
// Composition has been committed. So we need update selection for
// caret later
mSelection.Clear();
EnsureToCacheSelection();
mSetCursorPositionOnKeyEvent = true;
}
// If we've already set candidate window position, we don't need to update
// the position with update composition notification.
if (!mLayoutChanged) {
SetCursorPosition(GetActiveContext());
}
}
void
IMContextWrapper::SetInputContext(nsWindow* aCaller,
const InputContext* aContext,
const InputContextAction* aAction)
{
if (MOZ_UNLIKELY(IsDestroyed())) {
return;
}
MOZ_LOG(gGtkIMLog, LogLevel::Info,
("GTKIM: %p SetInputContext(aCaller=%p, aContext={ mIMEState={ "
"mEnabled=%s }, mHTMLInputType=%s })",
this, aCaller, GetEnabledStateName(aContext->mIMEState.mEnabled),
NS_ConvertUTF16toUTF8(aContext->mHTMLInputType).get()));
if (aCaller != mLastFocusedWindow) {
MOZ_LOG(gGtkIMLog, LogLevel::Error,
("GTKIM: %p SetInputContext(), FAILED, "
"the caller isn't focused window, mLastFocusedWindow=%p",
this, mLastFocusedWindow));
return;
}
if (!mContext) {
MOZ_LOG(gGtkIMLog, LogLevel::Error,
("GTKIM: %p SetInputContext(), FAILED, "
"there are no context",
this));
return;
}
if (sLastFocusedContext != this) {
mInputContext = *aContext;
MOZ_LOG(gGtkIMLog, LogLevel::Debug,
("GTKIM: %p SetInputContext(), succeeded, "
"but we're not active",
this));
return;
}
bool changingEnabledState =
aContext->mIMEState.mEnabled != mInputContext.mIMEState.mEnabled ||
aContext->mHTMLInputType != mInputContext.mHTMLInputType;
// Release current IME focus if IME is enabled.
if (changingEnabledState && mInputContext.mIMEState.MaybeEditable()) {
EndIMEComposition(mLastFocusedWindow);
Blur();
}
mInputContext = *aContext;
if (changingEnabledState) {
#if (MOZ_WIDGET_GTK == 3)
static bool sInputPurposeSupported = !gtk_check_version(3, 6, 0);
if (sInputPurposeSupported && mInputContext.mIMEState.MaybeEditable()) {
GtkIMContext* currentContext = GetCurrentContext();
if (currentContext) {
GtkInputPurpose purpose = GTK_INPUT_PURPOSE_FREE_FORM;
const nsString& inputType = mInputContext.mHTMLInputType;
// Password case has difficult issue. Desktop IMEs disable
// composition if input-purpose is password.
// For disabling IME on |ime-mode: disabled;|, we need to check
// mEnabled value instead of inputType value. This hack also
// enables composition on
// <input type="password" style="ime-mode: enabled;">.
// This is right behavior of ime-mode on desktop.
//
// On the other hand, IME for tablet devices may provide a
// specific software keyboard for password field. If so,
// the behavior might look strange on both:
// <input type="text" style="ime-mode: disabled;">
// <input type="password" style="ime-mode: enabled;">
//
// Temporarily, we should focus on desktop environment for now.
// I.e., let's ignore tablet devices for now. When somebody
// reports actual trouble on tablet devices, we should try to
// look for a way to solve actual problem.
if (mInputContext.mIMEState.mEnabled == IMEState::PASSWORD) {
purpose = GTK_INPUT_PURPOSE_PASSWORD;
} else if (inputType.EqualsLiteral("email")) {
purpose = GTK_INPUT_PURPOSE_EMAIL;
} else if (inputType.EqualsLiteral("url")) {
purpose = GTK_INPUT_PURPOSE_URL;
} else if (inputType.EqualsLiteral("tel")) {
purpose = GTK_INPUT_PURPOSE_PHONE;
} else if (inputType.EqualsLiteral("number")) {
purpose = GTK_INPUT_PURPOSE_NUMBER;
}
g_object_set(currentContext, "input-purpose", purpose, nullptr);
}
}
#endif // #if (MOZ_WIDGET_GTK == 3)
// Even when aState is not enabled state, we need to set IME focus.
// Because some IMs are updating the status bar of them at this time.
// Be aware, don't use aWindow here because this method shouldn't move
// focus actually.
Focus();
// XXX Should we call Blur() when it's not editable? E.g., it might be
// better to close VKB automatically.
}
}
InputContext
IMContextWrapper::GetInputContext()
{
mInputContext.mIMEState.mOpen = IMEState::OPEN_STATE_NOT_SUPPORTED;
return mInputContext;
}
GtkIMContext*
IMContextWrapper::GetCurrentContext() const
{
if (IsEnabled()) {
return mContext;
}
if (mInputContext.mIMEState.mEnabled == IMEState::PASSWORD) {
return mSimpleContext;
}
return mDummyContext;
}
bool
IMContextWrapper::IsValidContext(GtkIMContext* aContext) const
{
if (!aContext) {
return false;
}
return aContext == mContext ||
aContext == mSimpleContext ||
aContext == mDummyContext;
}
bool
IMContextWrapper::IsEnabled() const
{
return mInputContext.mIMEState.mEnabled == IMEState::ENABLED ||
mInputContext.mIMEState.mEnabled == IMEState::PLUGIN ||
(!sUseSimpleContext &&
mInputContext.mIMEState.mEnabled == IMEState::PASSWORD);
}
void
IMContextWrapper::Focus()
{
MOZ_LOG(gGtkIMLog, LogLevel::Info,
("GTKIM: %p Focus(), sLastFocusedContext=%p",
this, sLastFocusedContext));
if (mIsIMFocused) {
NS_ASSERTION(sLastFocusedContext == this,
"We're not active, but the IM was focused?");
return;
}
GtkIMContext* currentContext = GetCurrentContext();
if (!currentContext) {
MOZ_LOG(gGtkIMLog, LogLevel::Error,
("GTKIM: %p Focus(), FAILED, there are no context",
this));
return;
}
if (sLastFocusedContext && sLastFocusedContext != this) {
sLastFocusedContext->Blur();
}
sLastFocusedContext = this;
gtk_im_context_focus_in(currentContext);
mIsIMFocused = true;
mSetCursorPositionOnKeyEvent = true;
if (!IsEnabled()) {
// We should release IME focus for uim and scim.
// These IMs are using snooper that is released at losing focus.
Blur();
}
}
void
IMContextWrapper::Blur()
{
MOZ_LOG(gGtkIMLog, LogLevel::Info,
("GTKIM: %p Blur(), mIsIMFocused=%s",
this, ToChar(mIsIMFocused)));
if (!mIsIMFocused) {
return;
}
GtkIMContext* currentContext = GetCurrentContext();
if (!currentContext) {
MOZ_LOG(gGtkIMLog, LogLevel::Error,
("GTKIM: %p Blur(), FAILED, there are no context",
this));
return;
}
gtk_im_context_focus_out(currentContext);
mIsIMFocused = false;
}
void
IMContextWrapper::OnSelectionChange(nsWindow* aCaller,
const IMENotification& aIMENotification)
{
mSelection.Assign(aIMENotification);
if (MOZ_UNLIKELY(IsDestroyed())) {
return;
}
const IMENotification::SelectionChangeDataBase& selectionChangeData =
aIMENotification.mSelectionChangeData;
MOZ_LOG(gGtkIMLog, LogLevel::Info,
("GTKIM: %p OnSelectionChange(aCaller=0x%p, aIMENotification={ "
"mSelectionChangeData={ mOffset=%u, Length()=%u, mReversed=%s, "
"mWritingMode=%s, mCausedByComposition=%s, "
"mCausedBySelectionEvent=%s, mOccurredDuringComposition=%s "
"} }), mCompositionState=%s, mIsDeletingSurrounding=%s",
this, aCaller, selectionChangeData.mOffset,
selectionChangeData.Length(),
ToChar(selectionChangeData.mReversed),
GetWritingModeName(selectionChangeData.GetWritingMode()).get(),
ToChar(selectionChangeData.mCausedByComposition),
ToChar(selectionChangeData.mCausedBySelectionEvent),
ToChar(selectionChangeData.mOccurredDuringComposition),
GetCompositionStateName(), ToChar(mIsDeletingSurrounding)));
if (aCaller != mLastFocusedWindow) {
MOZ_LOG(gGtkIMLog, LogLevel::Error,
("GTKIM: %p OnSelectionChange(), FAILED, "
"the caller isn't focused window, mLastFocusedWindow=%p",
this, mLastFocusedWindow));
return;
}
if (!IsComposing()) {
// Now we have no composition (mostly situation on calling this method)
// If we have it, it will set by NOTIFY_IME_OF_COMPOSITION_UPDATE.
mSetCursorPositionOnKeyEvent = true;
}
// The focused editor might have placeholder text with normal text node.
// In such case, the text node must be removed from a compositionstart
// event handler. So, we're dispatching eCompositionStart,
// we should ignore selection change notification.
if (mCompositionState == eCompositionState_CompositionStartDispatched) {