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
|
/* -*- 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"
#import <UIKit/UIKit.h>
#include "mozilla/EventForwards.h"
#include "mozilla/Logging.h"
#include "mozilla/MiscEvents.h"
#include "mozilla/TextEventDispatcher.h"
#include "mozilla/TextEvents.h"
#include "mozilla/WidgetUtils.h"
#include "nsIWidget.h"
#include "nsObjCExceptions.h"
#include "nsString.h"
#include "nsWindow.h"
mozilla::LazyLogModule gIMELog("TextInputHandler");
namespace mozilla::widget {
static void GetStringForNSString(const NSString* aSrc, nsAString& aDist) {
NS_OBJC_BEGIN_TRY_IGNORE_BLOCK;
if (!aSrc) {
aDist.Truncate();
return;
}
aDist.SetLength([aSrc length]);
[aSrc getCharacters:reinterpret_cast<unichar*>(aDist.BeginWriting())
range:NSMakeRange(0, [aSrc length])];
NS_OBJC_END_TRY_IGNORE_BLOCK;
}
NS_IMPL_ISUPPORTS(TextInputHandler, TextEventDispatcherListener,
nsISupportsWeakReference)
TextInputHandler::TextInputHandler(nsWindow* aWidget)
: mWidget(aWidget), mDispatcher(aWidget->GetTextEventDispatcher()) {}
nsresult TextInputHandler::NotifyIME(TextEventDispatcher* aTextEventDispatcher,
const IMENotification& aNotification) {
return NS_OK;
}
IMENotificationRequests TextInputHandler::GetIMENotificationRequests() {
return IMENotificationRequests();
}
void TextInputHandler::OnRemovedFrom(
TextEventDispatcher* aTextEventDispatcher) {}
void TextInputHandler::WillDispatchKeyboardEvent(
TextEventDispatcher* aTextEventDispatcher,
WidgetKeyboardEvent& aKeyboardEvent, uint32_t aIndexOfKeypress,
void* aData) {}
bool TextInputHandler::InsertText(NSString* aText) {
nsString str;
GetStringForNSString(aText, str);
MOZ_LOG(gIMELog, LogLevel::Info,
("%p TextInputHandler::InsertText(aText=%s)", this,
NS_ConvertUTF16toUTF8(str).get()));
if (Destroyed()) {
return false;
}
if (str.Length() == 1) {
char16_t charCode = str[0];
if (charCode == 0x0a) {
return EmulateKeyboardEvent(NS_VK_RETURN, KEY_NAME_INDEX_Enter, charCode);
}
if (charCode == 0x08) {
return EmulateKeyboardEvent(NS_VK_BACK, KEY_NAME_INDEX_Backspace,
charCode);
}
if (uint32_t keyCode = WidgetUtils::ComputeKeyCodeFromChar(charCode)) {
return EmulateKeyboardEvent(keyCode, KEY_NAME_INDEX_USE_STRING, charCode);
}
}
nsEventStatus status = nsEventStatus_eIgnore;
RefPtr<nsWindow> widget(mWidget);
if (!DispatchKeyDownEvent(NS_VK_PROCESSKEY, KEY_NAME_INDEX_Process, 0,
status)) {
return false;
}
if (Destroyed()) {
return false;
}
mDispatcher->CommitComposition(status, &str, nullptr);
if (widget->Destroyed()) {
return false;
}
DispatchKeyUpEvent(NS_VK_PROCESSKEY, KEY_NAME_INDEX_Process, 0, status);
return true;
}
bool TextInputHandler::HandleCommand(Command aCommand) {
MOZ_LOG(gIMELog, LogLevel::Info,
("%p TextInputHandler::HandleCommand, aCommand=%s", this,
ToChar(aCommand)));
if (Destroyed()) {
return false;
}
if (aCommand != Command::DeleteCharBackward) {
return false;
}
nsEventStatus status = nsEventStatus_eIgnore;
if (!DispatchKeyDownEvent(NS_VK_BACK, KEY_NAME_INDEX_Backspace, 0, status)) {
return true;
}
if (Destroyed() || status == nsEventStatus_eConsumeNoDefault) {
return true;
}
// TODO: Focus check
if (!DispatchKeyPressEvent(NS_VK_BACK, KEY_NAME_INDEX_Backspace, 0, status)) {
return true;
}
if (Destroyed() || status == nsEventStatus_eConsumeNoDefault) {
return true;
}
// TODO: Focus check
DispatchKeyUpEvent(NS_VK_BACK, KEY_NAME_INDEX_Backspace, 0, status);
return true;
}
static uint32_t ComputeKeyModifiers(uint32_t aCharCode) {
if (aCharCode >= 'A' && aCharCode <= 'Z') {
return MODIFIER_SHIFT;
}
return 0;
}
static void InitKeyEvent(WidgetKeyboardEvent& aEvent, uint32_t aKeyCode,
KeyNameIndex aKeyNameIndex, char16_t aCharCode) {
aEvent.mKeyCode = aKeyCode;
aEvent.mIsRepeat = false;
aEvent.mKeyNameIndex = aKeyNameIndex;
// TODO(m_kato):
// How to get native key? Then, implement NativeKeyToDOM*.h for iOS
aEvent.mCodeNameIndex = CODE_NAME_INDEX_UNKNOWN;
if (aEvent.mKeyNameIndex == KEY_NAME_INDEX_USE_STRING) {
aEvent.mKeyValue = aCharCode;
}
aEvent.mModifiers = ComputeKeyModifiers(aCharCode);
aEvent.mLocation = eKeyLocationStandard;
aEvent.mTimeStamp = TimeStamp::Now();
}
bool TextInputHandler::DispatchKeyDownEvent(uint32_t aKeyCode,
KeyNameIndex aKeyNameIndex,
char16_t aCharCode,
nsEventStatus& aStatus) {
MOZ_ASSERT(aKeyCode);
MOZ_ASSERT(mWidget);
WidgetKeyboardEvent keydownEvent(true, eKeyDown, mWidget);
InitKeyEvent(keydownEvent, aKeyCode, aKeyNameIndex, aCharCode);
nsresult rv = mDispatcher->BeginNativeInputTransaction();
if (NS_FAILED(rv)) {
NS_WARNING("BeginNativeInputTransaction is failed");
return false;
}
return mDispatcher->DispatchKeyboardEvent(eKeyDown, keydownEvent, aStatus);
}
bool TextInputHandler::DispatchKeyUpEvent(uint32_t aKeyCode,
KeyNameIndex aKeyNameIndex,
char16_t aCharCode,
nsEventStatus& aStatus) {
MOZ_ASSERT(aKeyCode);
MOZ_ASSERT(mWidget);
WidgetKeyboardEvent keyupEvent(true, eKeyUp, mWidget);
InitKeyEvent(keyupEvent, aKeyCode, aKeyNameIndex, aCharCode);
nsresult rv = mDispatcher->BeginNativeInputTransaction();
if (NS_FAILED(rv)) {
NS_WARNING("BeginNativeInputTransaction is failed");
return false;
}
return mDispatcher->DispatchKeyboardEvent(eKeyUp, keyupEvent, aStatus);
}
bool TextInputHandler::DispatchKeyPressEvent(uint32_t aKeyCode,
KeyNameIndex aKeyNameIndex,
char16_t aCharCode,
nsEventStatus& aStatus) {
MOZ_ASSERT(aKeyCode);
MOZ_ASSERT(mWidget);
WidgetKeyboardEvent keypressEvent(true, eKeyPress, mWidget);
InitKeyEvent(keypressEvent, aKeyCode, aKeyNameIndex, aCharCode);
nsresult rv = mDispatcher->BeginNativeInputTransaction();
if (NS_FAILED(rv)) {
NS_WARNING("BeginNativeInputTransaction is failed");
return false;
}
return mDispatcher->MaybeDispatchKeypressEvents(keypressEvent, aStatus);
}
bool TextInputHandler::EmulateKeyboardEvent(uint32_t aKeyCode,
KeyNameIndex aKeyNameIndex,
char16_t aCharCode) {
MOZ_ASSERT(aCharCode);
MOZ_LOG(gIMELog, LogLevel::Info,
("%p TextInputHandler::EmulateKeyboardEvent(aKeyCode=%x, "
"aKeyNameIndex=%x, aCharCode=%x)",
this, aKeyCode, aKeyNameIndex, aCharCode));
nsEventStatus status = nsEventStatus_eIgnore;
if (!DispatchKeyDownEvent(aKeyCode, aKeyNameIndex, aCharCode, status)) {
return true;
}
if (Destroyed() || status == nsEventStatus_eConsumeNoDefault) {
return true;
}
// TODO: Focus check
if (!DispatchKeyPressEvent(aKeyCode, aKeyNameIndex, aCharCode, status)) {
return true;
}
if (Destroyed() || status == nsEventStatus_eConsumeNoDefault) {
return true;
}
// TODO: Focus check
DispatchKeyUpEvent(aKeyCode, aKeyNameIndex, aCharCode, status);
return true;
}
void TextInputHandler::OnDestroyed() { mWidget = nullptr; }
} // namespace mozilla::widget
|