Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.desktop/macosx/native/libawt_lwawt/awt/CInputMethod.m
66645 views
1
/*
2
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
#import <Cocoa/Cocoa.h>
27
#include <objc/objc-runtime.h>
28
29
#import "sun_lwawt_macosx_CInputMethod.h"
30
#import "sun_lwawt_macosx_CInputMethodDescriptor.h"
31
#import "ThreadUtilities.h"
32
#import "AWTView.h"
33
#import "JNIUtilities.h"
34
35
#import <JavaRuntimeSupport/JavaRuntimeSupport.h>
36
37
#define JAVA_LIST @"JAVA_LIST"
38
#define CURRENT_KB_DESCRIPTION @"CURRENT_KB_DESCRIPTION"
39
40
//
41
// NOTE: This returns a JNI Local Ref. Any code that calls must call DeleteLocalRef with the return value.
42
//
43
static jobject CreateLocaleObjectFromNSString(JNIEnv *env, NSString *name)
44
{
45
DECLARE_CLASS_RETURN(jc_localeClass, "java/util/Locale", NULL);
46
DECLARE_METHOD_RETURN(jm_localeCons, jc_localeClass, "<init>", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", NULL);
47
// Break apart the string into its components.
48
// First, duplicate the NSString into a C string, since we're going to modify it.
49
char * language = strdup([name UTF8String]);
50
char * country;
51
char * variant;
52
53
// Convert _ to NULL -- this gives us three null terminated strings in place.
54
for (country = language; *country != '_' && *country != '\0'; country++);
55
if (*country == '_') {
56
*country++ = '\0';
57
for (variant = country; *variant != '_' && *variant != '\0'; variant++);
58
if (*variant == '_') {
59
*variant++ = '\0';
60
}
61
} else {
62
variant = country;
63
}
64
65
// Create the java.util.Locale object
66
jobject localeObj = NULL;
67
jobject langObj = (*env)->NewStringUTF(env, language);
68
if (langObj != NULL) {
69
jobject ctryObj = (*env)->NewStringUTF(env, country);
70
if(ctryObj != NULL) {
71
jobject vrntObj = (*env)->NewStringUTF(env, variant);
72
if (vrntObj != NULL) {
73
localeObj = (*env)->NewObject(env, jc_localeClass, jm_localeCons,langObj, ctryObj,
74
vrntObj);
75
CHECK_EXCEPTION();
76
(*env)->DeleteLocalRef(env, vrntObj);
77
}
78
(*env)->DeleteLocalRef(env, ctryObj);
79
}
80
(*env)->DeleteLocalRef(env, langObj);
81
}
82
// Clean up and return.
83
free(language);
84
return localeObj;
85
}
86
87
static id inputMethodController = nil;
88
89
static void initializeInputMethodController() {
90
static BOOL checkedJRSInputMethodController = NO;
91
if (!checkedJRSInputMethodController && (inputMethodController == nil)) {
92
id jrsInputMethodController = objc_lookUpClass("JRSInputMethodController");
93
if (jrsInputMethodController != nil) {
94
inputMethodController = [jrsInputMethodController performSelector:@selector(controller)];
95
}
96
checkedJRSInputMethodController = YES;
97
}
98
}
99
100
101
@interface CInputMethod : NSObject {}
102
@end
103
104
@implementation CInputMethod
105
106
+ (void) setKeyboardLayout:(NSString *)theLocale
107
{
108
AWT_ASSERT_APPKIT_THREAD;
109
if (!inputMethodController) return;
110
111
[inputMethodController performSelector:@selector(setCurrentInputMethodForLocale) withObject:theLocale];
112
}
113
114
+ (void) _nativeNotifyPeerWithView:(AWTView *)view inputMethod:(jobject) inputMethod {
115
AWT_ASSERT_APPKIT_THREAD;
116
117
if (!view) return;
118
[view setInputMethod:inputMethod]; // inputMethod is a GlobalRef or null to disable.
119
}
120
121
+ (void) _nativeEndComposition:(AWTView *)view {
122
if (view == nil) return;
123
124
[view abandonInput];
125
}
126
127
128
@end
129
130
/*
131
* Class: sun_lwawt_macosx_CInputMethod
132
* Method: nativeInit
133
* Signature: ();
134
*/
135
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CInputMethod_nativeInit
136
(JNIEnv *env, jclass klass)
137
{
138
initializeInputMethodController();
139
}
140
141
/*
142
* Class: sun_lwawt_macosx_CInputMethod
143
* Method: nativeGetCurrentInputMethodInfo
144
* Signature: ()Ljava/lang/String;
145
*/
146
JNIEXPORT jobject JNICALL Java_sun_lwawt_macosx_CInputMethod_nativeGetCurrentInputMethodInfo
147
(JNIEnv *env, jclass klass)
148
{
149
if (!inputMethodController) return NULL;
150
jobject returnValue = 0;
151
__block NSString *keyboardInfo = NULL;
152
JNI_COCOA_ENTER(env);
153
154
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
155
keyboardInfo = [inputMethodController performSelector:@selector(currentInputMethodName)];
156
[keyboardInfo retain];
157
}];
158
159
if (keyboardInfo == nil) return NULL;
160
returnValue = NSStringToJavaString(env, keyboardInfo);
161
[keyboardInfo release];
162
163
JNI_COCOA_EXIT(env);
164
return returnValue;
165
}
166
167
/*
168
* Class: sun_lwawt_macosx_CInputMethod
169
* Method: nativeActivate
170
* Signature: (JLsun/lwawt/macosx/CInputMethod;)V
171
*/
172
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CInputMethod_nativeNotifyPeer
173
(JNIEnv *env, jobject this, jlong nativePeer, jobject inputMethod)
174
{
175
JNI_COCOA_ENTER(env);
176
AWTView *view = (AWTView *)jlong_to_ptr(nativePeer);
177
jobject inputMethodRef = (*env)->NewGlobalRef(env, inputMethod);
178
[ThreadUtilities performOnMainThreadWaiting:NO block:^(){
179
[CInputMethod _nativeNotifyPeerWithView:view inputMethod:inputMethodRef];
180
}];
181
182
JNI_COCOA_EXIT(env);
183
184
}
185
186
/*
187
* Class: sun_lwawt_macosx_CInputMethod
188
* Method: nativeEndComposition
189
* Signature: (J)V
190
*/
191
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CInputMethod_nativeEndComposition
192
(JNIEnv *env, jobject this, jlong nativePeer)
193
{
194
JNI_COCOA_ENTER(env);
195
AWTView *view = (AWTView *)jlong_to_ptr(nativePeer);
196
197
[ThreadUtilities performOnMainThreadWaiting:NO block:^(){
198
[CInputMethod _nativeEndComposition:view];
199
}];
200
201
JNI_COCOA_EXIT(env);
202
}
203
204
/*
205
* Class: sun_lwawt_macosx_CInputMethod
206
* Method: getNativeLocale
207
* Signature: ()Ljava/util/Locale;
208
*/
209
JNIEXPORT jobject JNICALL Java_sun_lwawt_macosx_CInputMethod_getNativeLocale
210
(JNIEnv *env, jobject this)
211
{
212
if (!inputMethodController) return NULL;
213
jobject returnValue = 0;
214
__block NSString *isoAbbreviation;
215
JNI_COCOA_ENTER(env);
216
217
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
218
isoAbbreviation = (NSString *) [inputMethodController performSelector:@selector(currentInputMethodLocale)];
219
[isoAbbreviation retain];
220
}];
221
222
if (isoAbbreviation == nil) return NULL;
223
224
static NSString *sLastKeyboardStr = nil;
225
static jobject sLastKeyboardLocaleObj = NULL;
226
227
if (![isoAbbreviation isEqualTo:sLastKeyboardStr]) {
228
[sLastKeyboardStr release];
229
sLastKeyboardStr = [isoAbbreviation retain];
230
jobject localObj = CreateLocaleObjectFromNSString(env, isoAbbreviation);
231
[isoAbbreviation release];
232
233
if (sLastKeyboardLocaleObj) {
234
(*env)->DeleteGlobalRef(env, sLastKeyboardLocaleObj);
235
sLastKeyboardLocaleObj = NULL;
236
}
237
if (localObj != NULL) {
238
sLastKeyboardLocaleObj = (*env)->NewGlobalRef(env, localObj);
239
(*env)->DeleteLocalRef(env, localObj);
240
}
241
}
242
243
returnValue = sLastKeyboardLocaleObj;
244
245
JNI_COCOA_EXIT(env);
246
return returnValue;
247
}
248
249
250
/*
251
* Class: sun_lwawt_macosx_CInputMethod
252
* Method: setNativeLocale
253
* Signature: (Ljava/lang/String;Z)Z
254
*/
255
JNIEXPORT jboolean JNICALL Java_sun_lwawt_macosx_CInputMethod_setNativeLocale
256
(JNIEnv *env, jobject this, jstring locale, jboolean isActivating)
257
{
258
JNI_COCOA_ENTER(env);
259
NSString *localeStr = JavaStringToNSString(env, locale);
260
[localeStr retain];
261
262
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
263
[CInputMethod setKeyboardLayout:localeStr];
264
}];
265
266
[localeStr release];
267
JNI_COCOA_EXIT(env);
268
return JNI_TRUE;
269
}
270
271
/*
272
* Class: sun_lwawt_macosx_CInputMethodDescriptor
273
* Method: nativeInit
274
* Signature: ();
275
*/
276
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CInputMethodDescriptor_nativeInit
277
(JNIEnv *env, jclass klass)
278
{
279
initializeInputMethodController();
280
}
281
282
/*
283
* Class: sun_lwawt_macosx_CInputMethodDescriptor
284
* Method: nativeGetAvailableLocales
285
* Signature: ()[Ljava/util/Locale;
286
*/
287
JNIEXPORT jobject JNICALL Java_sun_lwawt_macosx_CInputMethodDescriptor_nativeGetAvailableLocales
288
(JNIEnv *env, jclass klass)
289
{
290
DECLARE_CLASS_RETURN(jc_arrayListClass, "java/util/ArrayList", NULL);
291
DECLARE_METHOD_RETURN(jm_arrayListCons, jc_arrayListClass, "<init>", "()V", NULL);
292
DECLARE_METHOD_RETURN(jm_listAdd, jc_arrayListClass, "add", "(Ljava/lang/Object;)Z", NULL);
293
DECLARE_METHOD_RETURN(jm_listContains, jc_arrayListClass, "contains", "(Ljava/lang/Object;)Z", NULL);
294
295
if (!inputMethodController) return NULL;
296
jobject returnValue = 0;
297
298
__block NSArray *selectableArray = nil;
299
JNI_COCOA_ENTER(env);
300
301
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
302
selectableArray = (NSArray *)[inputMethodController performSelector:@selector(availableInputMethodLocales)];
303
[selectableArray retain];
304
}];
305
306
if (selectableArray == nil) return NULL;
307
308
// Create an ArrayList to return back to the caller.
309
returnValue = (*env)->NewObject(env, jc_arrayListClass, jm_arrayListCons);
310
CHECK_EXCEPTION_NULL_RETURN(returnValue, NULL);
311
312
for(NSString *locale in selectableArray) {
313
jobject localeObj = CreateLocaleObjectFromNSString(env, locale);
314
if (localeObj == NULL) {
315
break;
316
}
317
318
if ((*env)->CallBooleanMethod(env, returnValue, jm_listContains, localeObj) == JNI_FALSE) {
319
if ((*env)->ExceptionOccurred(env)) (*env)->ExceptionClear(env);
320
(*env)->CallBooleanMethod(env, returnValue, jm_listAdd, localeObj);
321
}
322
if ((*env)->ExceptionOccurred(env)) (*env)->ExceptionClear(env);
323
324
(*env)->DeleteLocalRef(env, localeObj);
325
}
326
[selectableArray release];
327
JNI_COCOA_EXIT(env);
328
return returnValue;
329
}
330
331
332