forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextInputHandler.mm
5073 lines (4460 loc) · 190 KB
/
TextInputHandler.mm
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: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 sw=2 et 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 "TextInputHandler.h"
#include "mozilla/Logging.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/AutoRestore.h"
#include "mozilla/MiscEvents.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/StaticPrefs_intl.h"
#include "mozilla/Telemetry.h"
#include "mozilla/TextEventDispatcher.h"
#include "mozilla/TextEvents.h"
#include "mozilla/ToString.h"
#include "nsChildView.h"
#include "nsCocoaFeatures.h"
#include "nsObjCExceptions.h"
#include "nsBidiUtils.h"
#include "nsToolkit.h"
#include "nsCocoaUtils.h"
#include "WidgetUtils.h"
#include "nsPrintfCString.h"
using namespace mozilla;
using namespace mozilla::widget;
// For collecting other people's log, tell them `MOZ_LOG=IMEHandler:4,sync`
// rather than `MOZ_LOG=IMEHandler:5,sync` since using `5` may create too
// big file.
// Therefore you shouldn't use `LogLevel::Verbose` for logging usual behavior.
mozilla::LazyLogModule gIMELog("IMEHandler");
// For collecting other people's log, tell them `MOZ_LOG=KeyboardHandler:4,sync`
// rather than `MOZ_LOG=KeyboardHandler:5,sync` since using `5` may create too
// big file.
// Therefore you shouldn't use `LogLevel::Verbose` for logging usual behavior.
mozilla::LazyLogModule gKeyLog("KeyboardHandler");
// The behavior of `TextInputHandler` class is important both for logging
// keyboard handler and IME handler. Therefore, the behavior is logged when
// either `IMEHandler` or `KeyboardHandler` is set to `MOZ_LOG`. Therefore,
// you may not need to tell people `MOZ_LOG=IMEHandler:4,KeyboardHandler:4,sync`.
#define MOZ_LOG_KEY_OR_IME(aLogLevel, aArgs) \
MOZ_LOG(MOZ_LOG_TEST(gIMELog, aLogLevel) ? gIMELog : gKeyLog, aLogLevel, aArgs)
static const char* OnOrOff(bool aBool) { return aBool ? "ON" : "off"; }
static const char* TrueOrFalse(bool aBool) { return aBool ? "TRUE" : "FALSE"; }
static const char* GetKeyNameForNativeKeyCode(unsigned short aNativeKeyCode) {
switch (aNativeKeyCode) {
case kVK_Escape:
return "Escape";
case kVK_RightCommand:
return "Right-Command";
case kVK_Command:
return "Command";
case kVK_Shift:
return "Shift";
case kVK_CapsLock:
return "CapsLock";
case kVK_Option:
return "Option";
case kVK_Control:
return "Control";
case kVK_RightShift:
return "Right-Shift";
case kVK_RightOption:
return "Right-Option";
case kVK_RightControl:
return "Right-Control";
case kVK_ANSI_KeypadClear:
return "Clear";
case kVK_F1:
return "F1";
case kVK_F2:
return "F2";
case kVK_F3:
return "F3";
case kVK_F4:
return "F4";
case kVK_F5:
return "F5";
case kVK_F6:
return "F6";
case kVK_F7:
return "F7";
case kVK_F8:
return "F8";
case kVK_F9:
return "F9";
case kVK_F10:
return "F10";
case kVK_F11:
return "F11";
case kVK_F12:
return "F12";
case kVK_F13:
return "F13/PrintScreen";
case kVK_F14:
return "F14/ScrollLock";
case kVK_F15:
return "F15/Pause";
case kVK_ANSI_Keypad0:
return "NumPad-0";
case kVK_ANSI_Keypad1:
return "NumPad-1";
case kVK_ANSI_Keypad2:
return "NumPad-2";
case kVK_ANSI_Keypad3:
return "NumPad-3";
case kVK_ANSI_Keypad4:
return "NumPad-4";
case kVK_ANSI_Keypad5:
return "NumPad-5";
case kVK_ANSI_Keypad6:
return "NumPad-6";
case kVK_ANSI_Keypad7:
return "NumPad-7";
case kVK_ANSI_Keypad8:
return "NumPad-8";
case kVK_ANSI_Keypad9:
return "NumPad-9";
case kVK_ANSI_KeypadMultiply:
return "NumPad-*";
case kVK_ANSI_KeypadPlus:
return "NumPad-+";
case kVK_ANSI_KeypadMinus:
return "NumPad--";
case kVK_ANSI_KeypadDecimal:
return "NumPad-.";
case kVK_ANSI_KeypadDivide:
return "NumPad-/";
case kVK_ANSI_KeypadEquals:
return "NumPad-=";
case kVK_ANSI_KeypadEnter:
return "NumPad-Enter";
case kVK_Return:
return "Return";
case kVK_Powerbook_KeypadEnter:
return "NumPad-EnterOnPowerBook";
case kVK_PC_Insert:
return "Insert/Help";
case kVK_PC_Delete:
return "Delete";
case kVK_Tab:
return "Tab";
case kVK_PC_Backspace:
return "Backspace";
case kVK_Home:
return "Home";
case kVK_End:
return "End";
case kVK_PageUp:
return "PageUp";
case kVK_PageDown:
return "PageDown";
case kVK_LeftArrow:
return "LeftArrow";
case kVK_RightArrow:
return "RightArrow";
case kVK_UpArrow:
return "UpArrow";
case kVK_DownArrow:
return "DownArrow";
case kVK_PC_ContextMenu:
return "ContextMenu";
case kVK_Function:
return "Function";
case kVK_VolumeUp:
return "VolumeUp";
case kVK_VolumeDown:
return "VolumeDown";
case kVK_Mute:
return "Mute";
case kVK_ISO_Section:
return "ISO_Section";
case kVK_JIS_Yen:
return "JIS_Yen";
case kVK_JIS_Underscore:
return "JIS_Underscore";
case kVK_JIS_KeypadComma:
return "JIS_KeypadComma";
case kVK_JIS_Eisu:
return "JIS_Eisu";
case kVK_JIS_Kana:
return "JIS_Kana";
case kVK_ANSI_A:
return "A";
case kVK_ANSI_B:
return "B";
case kVK_ANSI_C:
return "C";
case kVK_ANSI_D:
return "D";
case kVK_ANSI_E:
return "E";
case kVK_ANSI_F:
return "F";
case kVK_ANSI_G:
return "G";
case kVK_ANSI_H:
return "H";
case kVK_ANSI_I:
return "I";
case kVK_ANSI_J:
return "J";
case kVK_ANSI_K:
return "K";
case kVK_ANSI_L:
return "L";
case kVK_ANSI_M:
return "M";
case kVK_ANSI_N:
return "N";
case kVK_ANSI_O:
return "O";
case kVK_ANSI_P:
return "P";
case kVK_ANSI_Q:
return "Q";
case kVK_ANSI_R:
return "R";
case kVK_ANSI_S:
return "S";
case kVK_ANSI_T:
return "T";
case kVK_ANSI_U:
return "U";
case kVK_ANSI_V:
return "V";
case kVK_ANSI_W:
return "W";
case kVK_ANSI_X:
return "X";
case kVK_ANSI_Y:
return "Y";
case kVK_ANSI_Z:
return "Z";
case kVK_ANSI_1:
return "1";
case kVK_ANSI_2:
return "2";
case kVK_ANSI_3:
return "3";
case kVK_ANSI_4:
return "4";
case kVK_ANSI_5:
return "5";
case kVK_ANSI_6:
return "6";
case kVK_ANSI_7:
return "7";
case kVK_ANSI_8:
return "8";
case kVK_ANSI_9:
return "9";
case kVK_ANSI_0:
return "0";
case kVK_ANSI_Equal:
return "Equal";
case kVK_ANSI_Minus:
return "Minus";
case kVK_ANSI_RightBracket:
return "RightBracket";
case kVK_ANSI_LeftBracket:
return "LeftBracket";
case kVK_ANSI_Quote:
return "Quote";
case kVK_ANSI_Semicolon:
return "Semicolon";
case kVK_ANSI_Backslash:
return "Backslash";
case kVK_ANSI_Comma:
return "Comma";
case kVK_ANSI_Slash:
return "Slash";
case kVK_ANSI_Period:
return "Period";
case kVK_ANSI_Grave:
return "Grave";
default:
return "undefined";
}
}
static const char* GetCharacters(const nsAString& aString) {
if (aString.IsEmpty()) {
return "";
}
nsAutoString escapedStr;
for (uint32_t i = 0; i < aString.Length(); i++) {
char16_t ch = aString.CharAt(i);
if (ch < 0x20) {
nsPrintfCString utf8str("(U+%04X)", ch);
escapedStr += NS_ConvertUTF8toUTF16(utf8str);
} else if (ch <= 0x7E) {
escapedStr += ch;
} else {
nsPrintfCString utf8str("(U+%04X)", ch);
escapedStr += ch;
escapedStr += NS_ConvertUTF8toUTF16(utf8str);
}
}
// the result will be freed automatically by cocoa.
NSString* result = nsCocoaUtils::ToNSString(escapedStr);
return [result UTF8String];
}
static const char* GetCharacters(const NSString* aString) {
nsAutoString str;
nsCocoaUtils::GetStringForNSString(aString, str);
return GetCharacters(str);
}
static const char* GetCharacters(const CFStringRef aString) {
const NSString* str = reinterpret_cast<const NSString*>(aString);
return GetCharacters(str);
}
static const char* GetNativeKeyEventType(NSEvent* aNativeEvent) {
switch ([aNativeEvent type]) {
case NSEventTypeKeyDown:
return "NSEventTypeKeyDown";
case NSEventTypeKeyUp:
return "NSEventTypeKeyUp";
case NSEventTypeFlagsChanged:
return "NSEventTypeFlagsChanged";
default:
return "not key event";
}
}
static const char* GetGeckoKeyEventType(const WidgetEvent& aEvent) {
switch (aEvent.mMessage) {
case eKeyDown:
return "eKeyDown";
case eKeyUp:
return "eKeyUp";
case eKeyPress:
return "eKeyPress";
default:
return "not key event";
}
}
static const char* GetWindowLevelName(NSInteger aWindowLevel) {
switch (aWindowLevel) {
case kCGBaseWindowLevelKey:
return "kCGBaseWindowLevelKey (NSNormalWindowLevel)";
case kCGMinimumWindowLevelKey:
return "kCGMinimumWindowLevelKey";
case kCGDesktopWindowLevelKey:
return "kCGDesktopWindowLevelKey";
case kCGBackstopMenuLevelKey:
return "kCGBackstopMenuLevelKey";
case kCGNormalWindowLevelKey:
return "kCGNormalWindowLevelKey";
case kCGFloatingWindowLevelKey:
return "kCGFloatingWindowLevelKey (NSFloatingWindowLevel)";
case kCGTornOffMenuWindowLevelKey:
return "kCGTornOffMenuWindowLevelKey (NSSubmenuWindowLevel, NSTornOffMenuWindowLevel)";
case kCGDockWindowLevelKey:
return "kCGDockWindowLevelKey (NSDockWindowLevel)";
case kCGMainMenuWindowLevelKey:
return "kCGMainMenuWindowLevelKey (NSMainMenuWindowLevel)";
case kCGStatusWindowLevelKey:
return "kCGStatusWindowLevelKey (NSStatusWindowLevel)";
case kCGModalPanelWindowLevelKey:
return "kCGModalPanelWindowLevelKey (NSModalPanelWindowLevel)";
case kCGPopUpMenuWindowLevelKey:
return "kCGPopUpMenuWindowLevelKey (NSPopUpMenuWindowLevel)";
case kCGDraggingWindowLevelKey:
return "kCGDraggingWindowLevelKey";
case kCGScreenSaverWindowLevelKey:
return "kCGScreenSaverWindowLevelKey (NSScreenSaverWindowLevel)";
case kCGMaximumWindowLevelKey:
return "kCGMaximumWindowLevelKey";
case kCGOverlayWindowLevelKey:
return "kCGOverlayWindowLevelKey";
case kCGHelpWindowLevelKey:
return "kCGHelpWindowLevelKey";
case kCGUtilityWindowLevelKey:
return "kCGUtilityWindowLevelKey";
case kCGDesktopIconWindowLevelKey:
return "kCGDesktopIconWindowLevelKey";
case kCGCursorWindowLevelKey:
return "kCGCursorWindowLevelKey";
case kCGNumberOfWindowLevelKeys:
return "kCGNumberOfWindowLevelKeys";
default:
return "unknown window level";
}
}
static bool IsControlChar(uint32_t aCharCode) { return aCharCode < ' ' || aCharCode == 0x7F; }
static uint32_t gHandlerInstanceCount = 0;
static void EnsureToLogAllKeyboardLayoutsAndIMEs() {
static bool sDone = false;
if (!sDone) {
sDone = true;
TextInputHandler::DebugPrintAllKeyboardLayouts();
IMEInputHandler::DebugPrintAllIMEModes();
}
}
inline NSRange MakeNSRangeFrom(const Maybe<OffsetAndData<uint32_t>>& aOffsetAndData) {
if (aOffsetAndData.isNothing()) {
return NSMakeRange(NSNotFound, 0);
}
return NSMakeRange(aOffsetAndData->StartOffset(), aOffsetAndData->Length());
}
#pragma mark -
/******************************************************************************
*
* TISInputSourceWrapper implementation
*
******************************************************************************/
TISInputSourceWrapper* TISInputSourceWrapper::sCurrentInputSource = nullptr;
// static
TISInputSourceWrapper& TISInputSourceWrapper::CurrentInputSource() {
if (!sCurrentInputSource) {
sCurrentInputSource = new TISInputSourceWrapper();
}
if (!sCurrentInputSource->IsInitializedByCurrentInputSource()) {
sCurrentInputSource->InitByCurrentInputSource();
}
return *sCurrentInputSource;
}
// static
void TISInputSourceWrapper::Shutdown() {
if (!sCurrentInputSource) {
return;
}
sCurrentInputSource->Clear();
delete sCurrentInputSource;
sCurrentInputSource = nullptr;
}
bool TISInputSourceWrapper::TranslateToString(UInt32 aKeyCode, UInt32 aModifiers, UInt32 aKbType,
nsAString& aStr) {
aStr.Truncate();
const UCKeyboardLayout* UCKey = GetUCKeyboardLayout();
MOZ_LOG(gKeyLog, LogLevel::Info,
("%p TISInputSourceWrapper::TranslateToString, aKeyCode=0x%X, "
"aModifiers=0x%X, aKbType=0x%X UCKey=%p\n "
"Shift: %s, Ctrl: %s, Opt: %s, Cmd: %s, CapsLock: %s, NumLock: %s",
this, static_cast<unsigned int>(aKeyCode), static_cast<unsigned int>(aModifiers),
static_cast<unsigned int>(aKbType), UCKey, OnOrOff(aModifiers & shiftKey),
OnOrOff(aModifiers & controlKey), OnOrOff(aModifiers & optionKey),
OnOrOff(aModifiers & cmdKey), OnOrOff(aModifiers & alphaLock),
OnOrOff(aModifiers & kEventKeyModifierNumLockMask)));
NS_ENSURE_TRUE(UCKey, false);
UInt32 deadKeyState = 0;
UniCharCount len;
UniChar chars[5];
OSStatus err = ::UCKeyTranslate(UCKey, aKeyCode, kUCKeyActionDown, aModifiers >> 8, aKbType,
kUCKeyTranslateNoDeadKeysMask, &deadKeyState, 5, &len, chars);
MOZ_LOG(gKeyLog, LogLevel::Info,
("%p TISInputSourceWrapper::TranslateToString, err=0x%X, len=%zu", this,
static_cast<int>(err), len));
NS_ENSURE_TRUE(err == noErr, false);
if (len == 0) {
return true;
}
if (!aStr.SetLength(len, fallible)) {
return false;
}
NS_ASSERTION(sizeof(char16_t) == sizeof(UniChar),
"size of char16_t and size of UniChar are different");
memcpy(aStr.BeginWriting(), chars, len * sizeof(char16_t));
MOZ_LOG(gKeyLog, LogLevel::Info,
("%p TISInputSourceWrapper::TranslateToString, aStr=\"%s\"", this,
NS_ConvertUTF16toUTF8(aStr).get()));
return true;
}
uint32_t TISInputSourceWrapper::TranslateToChar(UInt32 aKeyCode, UInt32 aModifiers,
UInt32 aKbType) {
nsAutoString str;
if (!TranslateToString(aKeyCode, aModifiers, aKbType, str) || str.Length() != 1) {
return 0;
}
return static_cast<uint32_t>(str.CharAt(0));
}
bool TISInputSourceWrapper::IsDeadKey(NSEvent* aNativeKeyEvent) {
if ([[aNativeKeyEvent characters] length]) {
return false;
}
// Assmue that if control key or command key is pressed, it's not a dead key.
NSUInteger cocoaState = [aNativeKeyEvent modifierFlags];
if (cocoaState & (NSEventModifierFlagControl | NSEventModifierFlagCommand)) {
return false;
}
UInt32 nativeKeyCode = [aNativeKeyEvent keyCode];
switch (nativeKeyCode) {
case kVK_ANSI_A:
case kVK_ANSI_B:
case kVK_ANSI_C:
case kVK_ANSI_D:
case kVK_ANSI_E:
case kVK_ANSI_F:
case kVK_ANSI_G:
case kVK_ANSI_H:
case kVK_ANSI_I:
case kVK_ANSI_J:
case kVK_ANSI_K:
case kVK_ANSI_L:
case kVK_ANSI_M:
case kVK_ANSI_N:
case kVK_ANSI_O:
case kVK_ANSI_P:
case kVK_ANSI_Q:
case kVK_ANSI_R:
case kVK_ANSI_S:
case kVK_ANSI_T:
case kVK_ANSI_U:
case kVK_ANSI_V:
case kVK_ANSI_W:
case kVK_ANSI_X:
case kVK_ANSI_Y:
case kVK_ANSI_Z:
case kVK_ANSI_1:
case kVK_ANSI_2:
case kVK_ANSI_3:
case kVK_ANSI_4:
case kVK_ANSI_5:
case kVK_ANSI_6:
case kVK_ANSI_7:
case kVK_ANSI_8:
case kVK_ANSI_9:
case kVK_ANSI_0:
case kVK_ANSI_Equal:
case kVK_ANSI_Minus:
case kVK_ANSI_RightBracket:
case kVK_ANSI_LeftBracket:
case kVK_ANSI_Quote:
case kVK_ANSI_Semicolon:
case kVK_ANSI_Backslash:
case kVK_ANSI_Comma:
case kVK_ANSI_Slash:
case kVK_ANSI_Period:
case kVK_ANSI_Grave:
case kVK_JIS_Yen:
case kVK_JIS_Underscore:
break;
default:
// Let's assume that dead key can be only a printable key in standard
// position.
return false;
}
// If TranslateToChar() returns non-zero value, that means that
// the key may input a character with different dead key state.
UInt32 kbType = GetKbdType();
UInt32 carbonState = nsCocoaUtils::ConvertToCarbonModifier(cocoaState);
return IsDeadKey(nativeKeyCode, carbonState, kbType);
}
bool TISInputSourceWrapper::IsDeadKey(UInt32 aKeyCode, UInt32 aModifiers, UInt32 aKbType) {
const UCKeyboardLayout* UCKey = GetUCKeyboardLayout();
MOZ_LOG(gKeyLog, LogLevel::Info,
("%p TISInputSourceWrapper::IsDeadKey, aKeyCode=0x%X, "
"aModifiers=0x%X, aKbType=0x%X UCKey=%p\n "
"Shift: %s, Ctrl: %s, Opt: %s, Cmd: %s, CapsLock: %s, NumLock: %s",
this, static_cast<unsigned int>(aKeyCode), static_cast<unsigned int>(aModifiers),
static_cast<unsigned int>(aKbType), UCKey, OnOrOff(aModifiers & shiftKey),
OnOrOff(aModifiers & controlKey), OnOrOff(aModifiers & optionKey),
OnOrOff(aModifiers & cmdKey), OnOrOff(aModifiers & alphaLock),
OnOrOff(aModifiers & kEventKeyModifierNumLockMask)));
if (NS_WARN_IF(!UCKey)) {
return false;
}
UInt32 deadKeyState = 0;
UniCharCount len;
UniChar chars[5];
OSStatus err = ::UCKeyTranslate(UCKey, aKeyCode, kUCKeyActionDown, aModifiers >> 8, aKbType, 0,
&deadKeyState, 5, &len, chars);
MOZ_LOG(gKeyLog, LogLevel::Info,
("%p TISInputSourceWrapper::IsDeadKey, err=0x%X, "
"len=%zu, deadKeyState=%u",
this, static_cast<int>(err), len, deadKeyState));
if (NS_WARN_IF(err != noErr)) {
return false;
}
return deadKeyState != 0;
}
void TISInputSourceWrapper::InitByInputSourceID(const char* aID) {
Clear();
if (!aID) return;
CFStringRef idstr = ::CFStringCreateWithCString(kCFAllocatorDefault, aID, kCFStringEncodingASCII);
InitByInputSourceID(idstr);
::CFRelease(idstr);
}
void TISInputSourceWrapper::InitByInputSourceID(const nsString& aID) {
Clear();
if (aID.IsEmpty()) return;
CFStringRef idstr = ::CFStringCreateWithCharacters(
kCFAllocatorDefault, reinterpret_cast<const UniChar*>(aID.get()), aID.Length());
InitByInputSourceID(idstr);
::CFRelease(idstr);
}
void TISInputSourceWrapper::InitByInputSourceID(const CFStringRef aID) {
Clear();
if (!aID) return;
const void* keys[] = {kTISPropertyInputSourceID};
const void* values[] = {aID};
CFDictionaryRef filter = ::CFDictionaryCreate(kCFAllocatorDefault, keys, values, 1, NULL, NULL);
NS_ASSERTION(filter, "failed to create the filter");
mInputSourceList = ::TISCreateInputSourceList(filter, true);
::CFRelease(filter);
if (::CFArrayGetCount(mInputSourceList) > 0) {
mInputSource = static_cast<TISInputSourceRef>(
const_cast<void*>(::CFArrayGetValueAtIndex(mInputSourceList, 0)));
if (IsKeyboardLayout()) {
mKeyboardLayout = mInputSource;
}
}
}
void TISInputSourceWrapper::InitByLayoutID(SInt32 aLayoutID, bool aOverrideKeyboard) {
// NOTE: Doument new layout IDs in TextInputHandler.h when you add ones.
switch (aLayoutID) {
case 0:
InitByInputSourceID("com.apple.keylayout.US");
break;
case 1:
InitByInputSourceID("com.apple.keylayout.Greek");
break;
case 2:
InitByInputSourceID("com.apple.keylayout.German");
break;
case 3:
InitByInputSourceID("com.apple.keylayout.Swedish-Pro");
break;
case 4:
InitByInputSourceID("com.apple.keylayout.DVORAK-QWERTYCMD");
break;
case 5:
InitByInputSourceID("com.apple.keylayout.Thai");
break;
case 6:
InitByInputSourceID("com.apple.keylayout.Arabic");
break;
case 7:
InitByInputSourceID("com.apple.keylayout.ArabicPC");
break;
case 8:
InitByInputSourceID("com.apple.keylayout.French");
break;
case 9:
InitByInputSourceID("com.apple.keylayout.Hebrew");
break;
case 10:
InitByInputSourceID("com.apple.keylayout.Lithuanian");
break;
case 11:
InitByInputSourceID("com.apple.keylayout.Norwegian");
break;
case 12:
InitByInputSourceID("com.apple.keylayout.Spanish");
break;
default:
Clear();
break;
}
mOverrideKeyboard = aOverrideKeyboard;
}
void TISInputSourceWrapper::InitByCurrentInputSource() {
Clear();
mInputSource = ::TISCopyCurrentKeyboardInputSource();
mKeyboardLayout = ::TISCopyInputMethodKeyboardLayoutOverride();
if (!mKeyboardLayout) {
mKeyboardLayout = ::TISCopyCurrentKeyboardLayoutInputSource();
}
// If this causes composition, the current keyboard layout may input non-ASCII
// characters such as Japanese Kana characters or Hangul characters.
// However, we need to set ASCII characters to DOM key events for consistency
// with other platforms.
if (IsOpenedIMEMode()) {
TISInputSourceWrapper tis(mKeyboardLayout);
if (!tis.IsASCIICapable()) {
mKeyboardLayout = ::TISCopyCurrentASCIICapableKeyboardLayoutInputSource();
}
}
}
void TISInputSourceWrapper::InitByCurrentKeyboardLayout() {
Clear();
mInputSource = ::TISCopyCurrentKeyboardLayoutInputSource();
mKeyboardLayout = mInputSource;
}
void TISInputSourceWrapper::InitByCurrentASCIICapableInputSource() {
Clear();
mInputSource = ::TISCopyCurrentASCIICapableKeyboardInputSource();
mKeyboardLayout = ::TISCopyInputMethodKeyboardLayoutOverride();
if (mKeyboardLayout) {
TISInputSourceWrapper tis(mKeyboardLayout);
if (!tis.IsASCIICapable()) {
mKeyboardLayout = nullptr;
}
}
if (!mKeyboardLayout) {
mKeyboardLayout = ::TISCopyCurrentASCIICapableKeyboardLayoutInputSource();
}
}
void TISInputSourceWrapper::InitByCurrentASCIICapableKeyboardLayout() {
Clear();
mInputSource = ::TISCopyCurrentASCIICapableKeyboardLayoutInputSource();
mKeyboardLayout = mInputSource;
}
void TISInputSourceWrapper::InitByCurrentInputMethodKeyboardLayoutOverride() {
Clear();
mInputSource = ::TISCopyInputMethodKeyboardLayoutOverride();
mKeyboardLayout = mInputSource;
}
void TISInputSourceWrapper::InitByTISInputSourceRef(TISInputSourceRef aInputSource) {
Clear();
mInputSource = aInputSource;
if (IsKeyboardLayout()) {
mKeyboardLayout = mInputSource;
}
}
void TISInputSourceWrapper::InitByLanguage(CFStringRef aLanguage) {
Clear();
mInputSource = ::TISCopyInputSourceForLanguage(aLanguage);
if (IsKeyboardLayout()) {
mKeyboardLayout = mInputSource;
}
}
const UCKeyboardLayout* TISInputSourceWrapper::GetUCKeyboardLayout() {
NS_ENSURE_TRUE(mKeyboardLayout, nullptr);
if (mUCKeyboardLayout) {
return mUCKeyboardLayout;
}
CFDataRef uchr = static_cast<CFDataRef>(
::TISGetInputSourceProperty(mKeyboardLayout, kTISPropertyUnicodeKeyLayoutData));
// We should be always able to get the layout here.
NS_ENSURE_TRUE(uchr, nullptr);
mUCKeyboardLayout = reinterpret_cast<const UCKeyboardLayout*>(CFDataGetBytePtr(uchr));
return mUCKeyboardLayout;
}
bool TISInputSourceWrapper::GetBoolProperty(const CFStringRef aKey) {
CFBooleanRef ret = static_cast<CFBooleanRef>(::TISGetInputSourceProperty(mInputSource, aKey));
return ::CFBooleanGetValue(ret);
}
bool TISInputSourceWrapper::GetStringProperty(const CFStringRef aKey, CFStringRef& aStr) {
aStr = static_cast<CFStringRef>(::TISGetInputSourceProperty(mInputSource, aKey));
return aStr != nullptr;
}
bool TISInputSourceWrapper::GetStringProperty(const CFStringRef aKey, nsAString& aStr) {
CFStringRef str;
GetStringProperty(aKey, str);
nsCocoaUtils::GetStringForNSString((const NSString*)str, aStr);
return !aStr.IsEmpty();
}
bool TISInputSourceWrapper::IsOpenedIMEMode() {
NS_ENSURE_TRUE(mInputSource, false);
if (!IsIMEMode()) return false;
return !IsASCIICapable();
}
bool TISInputSourceWrapper::IsIMEMode() {
NS_ENSURE_TRUE(mInputSource, false);
CFStringRef str;
GetInputSourceType(str);
NS_ENSURE_TRUE(str, false);
return ::CFStringCompare(kTISTypeKeyboardInputMode, str, 0) == kCFCompareEqualTo;
}
bool TISInputSourceWrapper::IsKeyboardLayout() {
NS_ENSURE_TRUE(mInputSource, false);
CFStringRef str;
GetInputSourceType(str);
NS_ENSURE_TRUE(str, false);
return ::CFStringCompare(kTISTypeKeyboardLayout, str, 0) == kCFCompareEqualTo;
}
bool TISInputSourceWrapper::GetLanguageList(CFArrayRef& aLanguageList) {
NS_ENSURE_TRUE(mInputSource, false);
aLanguageList = static_cast<CFArrayRef>(
::TISGetInputSourceProperty(mInputSource, kTISPropertyInputSourceLanguages));
return aLanguageList != nullptr;
}
bool TISInputSourceWrapper::GetPrimaryLanguage(CFStringRef& aPrimaryLanguage) {
NS_ENSURE_TRUE(mInputSource, false);
CFArrayRef langList;
NS_ENSURE_TRUE(GetLanguageList(langList), false);
if (::CFArrayGetCount(langList) == 0) return false;
aPrimaryLanguage = static_cast<CFStringRef>(::CFArrayGetValueAtIndex(langList, 0));
return aPrimaryLanguage != nullptr;
}
bool TISInputSourceWrapper::GetPrimaryLanguage(nsAString& aPrimaryLanguage) {
NS_ENSURE_TRUE(mInputSource, false);
CFStringRef primaryLanguage;
NS_ENSURE_TRUE(GetPrimaryLanguage(primaryLanguage), false);
nsCocoaUtils::GetStringForNSString((const NSString*)primaryLanguage, aPrimaryLanguage);
return !aPrimaryLanguage.IsEmpty();
}
bool TISInputSourceWrapper::IsForRTLLanguage() {
if (mIsRTL < 0) {
// Get the input character of the 'A' key of ANSI keyboard layout.
nsAutoString str;
bool ret = TranslateToString(kVK_ANSI_A, 0, eKbdType_ANSI, str);
NS_ENSURE_TRUE(ret, ret);
char16_t ch = str.IsEmpty() ? char16_t(0) : str.CharAt(0);
mIsRTL = UTF16_CODE_UNIT_IS_BIDI(ch);
}
return mIsRTL != 0;
}
bool TISInputSourceWrapper::IsForJapaneseLanguage() {
nsAutoString lang;
GetPrimaryLanguage(lang);
return lang.EqualsLiteral("ja");
}
bool TISInputSourceWrapper::IsInitializedByCurrentInputSource() {
return mInputSource == ::TISCopyCurrentKeyboardInputSource();
}
void TISInputSourceWrapper::Select() {
if (!mInputSource) return;
::TISSelectInputSource(mInputSource);
}
void TISInputSourceWrapper::Clear() {
// Clear() is always called when TISInputSourceWrappper is created.
EnsureToLogAllKeyboardLayoutsAndIMEs();
if (mInputSourceList) {
::CFRelease(mInputSourceList);
}
mInputSourceList = nullptr;
mInputSource = nullptr;
mKeyboardLayout = nullptr;
mIsRTL = -1;
mUCKeyboardLayout = nullptr;
mOverrideKeyboard = false;
}
bool TISInputSourceWrapper::IsPrintableKeyEvent(NSEvent* aNativeKeyEvent) const {
UInt32 nativeKeyCode = [aNativeKeyEvent keyCode];
bool isPrintableKey = !TextInputHandler::IsSpecialGeckoKey(nativeKeyCode);
if (isPrintableKey && [aNativeKeyEvent type] != NSEventTypeKeyDown &&
[aNativeKeyEvent type] != NSEventTypeKeyUp) {
NS_WARNING("Why would a printable key not be an NSEventTypeKeyDown or NSEventTypeKeyUp event?");
isPrintableKey = false;
}
return isPrintableKey;
}
UInt32 TISInputSourceWrapper::GetKbdType() const {
// If a keyboard layout override is set, we also need to force the keyboard
// type to something ANSI to avoid test failures on machines with JIS
// keyboards (since the pair of keyboard layout and physical keyboard type
// form the actual key layout). This assumes that the test setting the
// override was written assuming an ANSI keyboard.
return mOverrideKeyboard ? eKbdType_ANSI : ::LMGetKbdType();
}
void TISInputSourceWrapper::ComputeInsertStringForCharCode(NSEvent* aNativeKeyEvent,
const WidgetKeyboardEvent& aKeyEvent,
const nsAString* aInsertString,
nsAString& aResult) {
if (aInsertString) {
// If the caller expects that the aInsertString will be input, we shouldn't
// change it.
aResult = *aInsertString;
} else if (IsPrintableKeyEvent(aNativeKeyEvent)) {
// If IME is open, [aNativeKeyEvent characters] may be a character
// which will be appended to the composition string. However, especially,
// while IME is disabled, most users and developers expect the key event
// works as IME closed. So, we should compute the aResult with
// the ASCII capable keyboard layout.
// NOTE: Such keyboard layouts typically change the layout to its ASCII
// capable layout when Command key is pressed. And we don't worry
// when Control key is pressed too because it causes inputting
// control characters.
// Additionally, if the key event doesn't input any text, the event may be
// dead key event. In this case, the charCode value should be the dead
// character.
UInt32 nativeKeyCode = [aNativeKeyEvent keyCode];
if ((!aKeyEvent.IsMeta() && !aKeyEvent.IsControl() && IsOpenedIMEMode()) ||
![[aNativeKeyEvent characters] length]) {
UInt32 state = nsCocoaUtils::ConvertToCarbonModifier([aNativeKeyEvent modifierFlags]);
uint32_t ch = TranslateToChar(nativeKeyCode, state, GetKbdType());
if (ch) {
aResult = ch;
}
} else {
// If the caller isn't sure what string will be input, let's use
// characters of NSEvent.
nsCocoaUtils::GetStringForNSString([aNativeKeyEvent characters], aResult);
}
// If control key is pressed and the eventChars is a non-printable control
// character, we should convert it to ASCII alphabet.
if (aKeyEvent.IsControl() && !aResult.IsEmpty() && aResult[0] <= char16_t(26)) {
aResult = (aKeyEvent.IsShift() ^ aKeyEvent.IsCapsLocked())
? static_cast<char16_t>(aResult[0] + ('A' - 1))
: static_cast<char16_t>(aResult[0] + ('a' - 1));
}
// If Meta key is pressed, it may cause to switch the keyboard layout like
// Arabic, Russian, Hebrew, Greek and Dvorak-QWERTY.
else if (aKeyEvent.IsMeta() && !(aKeyEvent.IsControl() || aKeyEvent.IsAlt())) {
UInt32 kbType = GetKbdType();
UInt32 numLockState = aKeyEvent.IsNumLocked() ? kEventKeyModifierNumLockMask : 0;
UInt32 capsLockState = aKeyEvent.IsCapsLocked() ? alphaLock : 0;
UInt32 shiftState = aKeyEvent.IsShift() ? shiftKey : 0;
uint32_t uncmdedChar = TranslateToChar(nativeKeyCode, numLockState, kbType);
uint32_t cmdedChar = TranslateToChar(nativeKeyCode, cmdKey | numLockState, kbType);
// If we can make a good guess at the characters that the user would
// expect this key combination to produce (with and without Shift) then
// use those characters. This also corrects for CapsLock.
uint32_t ch = 0;
if (uncmdedChar == cmdedChar) {
// The characters produced with Command seem similar to those without
// Command.
ch = TranslateToChar(nativeKeyCode, shiftState | capsLockState | numLockState, kbType);
} else {
TISInputSourceWrapper USLayout("com.apple.keylayout.US");
uint32_t uncmdedUSChar = USLayout.TranslateToChar(nativeKeyCode, numLockState, kbType);
// If it looks like characters from US keyboard layout when Command key
// is pressed, we should compute a character in the layout.
if (uncmdedUSChar == cmdedChar) {
ch = USLayout.TranslateToChar(nativeKeyCode, shiftState | capsLockState | numLockState,
kbType);
}
}
// If there is a more preferred character for the commanded key event,
// we should use it.
if (ch) {
aResult = ch;
}
}
}