forked from PojavLauncherTeam/PojavLauncher
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathinput_bridge_v3.c
More file actions
599 lines (520 loc) · 27.8 KB
/
Copy pathinput_bridge_v3.c
File metadata and controls
599 lines (520 loc) · 27.8 KB
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
/*
* V3 input bridge implementation.
*
* Status:
* - Active development
* - Works with some bugs:
* + Modded versions gives broken stuff..
*
*
* - Implements glfwSetCursorPos() to handle grab camera pos correctly.
*/
#include <assert.h>
#include <dlfcn.h>
#include <jni.h>
#include <libgen.h>
#include <stdlib.h>
#include <string.h>
#include <stdatomic.h>
#include <math.h>
#define TAG __FILE_NAME__
#include "log.h"
#include "utils.h"
#include "environ/environ.h"
#include "jvm_hooks/jvm_hooks.h"
#define EVENT_TYPE_CHAR 1000
#define EVENT_TYPE_CHAR_MODS 1001
#define EVENT_TYPE_CURSOR_ENTER 1002
#define EVENT_TYPE_KEY 1005
#define EVENT_TYPE_MOUSE_BUTTON 1006
#define EVENT_TYPE_SCROLL 1007
#define TRY_ATTACH_ENV(env_name, vm, error_message, then) JNIEnv* env_name;\
do { \
env_name = get_attached_env(vm); \
if(env_name == NULL) { \
printf(error_message); \
then \
} \
} while(0)
static void registerFunctions(JNIEnv *env);
jint JNI_OnLoad(JavaVM* vm, __attribute__((unused)) void* reserved) {
if (pojav_environ->dalvikJavaVMPtr == NULL) {
LOGI("Saving DVM environ...");
//Save dalvik global JavaVM pointer
pojav_environ->dalvikJavaVMPtr = vm;
// Sets up the stuff that GLFW/JVM needs to communicate to Android
// These methods are called from GLFW/JVM and connect to Android-side impls
// These aren't separated out into a method because these can be ran so long as we are in Android-land
// so that means this library must be loaded at least once in Android-land
JNIEnv *dvEnv;
(*vm)->GetEnv(vm, (void**) &dvEnv, JNI_VERSION_1_4);
pojav_environ->bridgeClazz = (*dvEnv)->NewGlobalRef(dvEnv,(*dvEnv) ->FindClass(dvEnv,"org/lwjgl/glfw/CallbackBridge"));
pojav_environ->method_accessAndroidClipboard = (*dvEnv)->GetStaticMethodID(dvEnv, pojav_environ->bridgeClazz, "accessAndroidClipboard", "(ILjava/lang/String;)Ljava/lang/String;");
pojav_environ->method_onGrabStateChanged = (*dvEnv)->GetStaticMethodID(dvEnv, pojav_environ->bridgeClazz, "onGrabStateChanged", "(Z)V");
pojav_environ->method_onDirectInputEnable = (*dvEnv)->GetStaticMethodID(dvEnv, pojav_environ->bridgeClazz, "onDirectInputEnable", "()V");
pojav_environ->method_getAndroidDPI = (*dvEnv)->GetStaticMethodID(dvEnv, pojav_environ->bridgeClazz, "getAndroidDPI", "()F");
pojav_environ->isUseStackQueueCall = JNI_FALSE;
} else if (pojav_environ->dalvikJavaVMPtr != vm) {
LOGI("Saving JVM environ...");
pojav_environ->runtimeJavaVMPtr = vm;
JNIEnv *vmEnv;
(*pojav_environ->runtimeJavaVMPtr)->GetEnv(pojav_environ->runtimeJavaVMPtr, (void**) &vmEnv, JNI_VERSION_1_4);
hookExec(vmEnv);
installLwjglDlopenHook(vmEnv);
installEMUIIteratorMititgation(vmEnv);
}
if(pojav_environ->dalvikJavaVMPtr == vm) {
//perform in all DVM instances, not only during first ever set up
JNIEnv *env;
(*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4);
registerFunctions(env);
}
pojav_environ->isGrabbing = JNI_FALSE;
return JNI_VERSION_1_4;
}
// Sets up the stuff that Android needs to communicate to GLFW/JVM
// These methods are called from Android and connect to GLFW/JVM-side impls
// These are separated out into a method because GLFW loads much later than when we need to dlopen
// pojavexec since it does more than just GLFW.
// TODO: Add checks in case someone forgets to run this method. Probably see if pojav_environ->vmGlfwClass is null or not
JNIEXPORT void JNICALL Java_org_lwjgl_glfw_GLFW_nativeInitializeGLFWNativeBridge(__attribute__((unused)) JNIEnv* env, __attribute__((unused)) jclass clazz) {
JNIEnv *vmEnv;
(*pojav_environ->runtimeJavaVMPtr)->GetEnv(pojav_environ->runtimeJavaVMPtr, (void**) &vmEnv, JNI_VERSION_1_4);
pojav_environ->vmGlfwClass = (*vmEnv)->NewGlobalRef(vmEnv, (*vmEnv)->FindClass(vmEnv, "org/lwjgl/glfw/GLFW"));
pojav_environ->method_glftSetWindowAttrib = (*vmEnv)->GetStaticMethodID(vmEnv, pojav_environ->vmGlfwClass, "glfwSetWindowAttrib", "(JII)V");
pojav_environ->method_internalWindowSizeChanged = (*vmEnv)->GetStaticMethodID(vmEnv, pojav_environ->vmGlfwClass, "internalWindowSizeChanged", "(J)V");
pojav_environ->method_internalChangeMonitorSize = (*vmEnv)->GetStaticMethodID(vmEnv, pojav_environ->vmGlfwClass, "internalChangeMonitorSize", "(II)V");
jfieldID field_keyDownBuffer = (*vmEnv)->GetStaticFieldID(vmEnv, pojav_environ->vmGlfwClass, "keyDownBuffer", "Ljava/nio/ByteBuffer;");
jobject keyDownBufferJ = (*vmEnv)->GetStaticObjectField(vmEnv, pojav_environ->vmGlfwClass, field_keyDownBuffer);
pojav_environ->keyDownBuffer = (*vmEnv)->GetDirectBufferAddress(vmEnv, keyDownBufferJ);
jfieldID field_mouseDownBuffer = (*vmEnv)->GetStaticFieldID(vmEnv, pojav_environ->vmGlfwClass, "mouseDownBuffer", "Ljava/nio/ByteBuffer;");
jobject mouseDownBufferJ = (*vmEnv)->GetStaticObjectField(vmEnv, pojav_environ->vmGlfwClass, field_mouseDownBuffer);
pojav_environ->mouseDownBuffer = (*vmEnv)->GetDirectBufferAddress(vmEnv, mouseDownBufferJ);
}
#define ADD_CALLBACK_WWIN(NAME) \
JNIEXPORT jlong JNICALL Java_org_lwjgl_glfw_GLFW_nglfwSet##NAME##Callback(JNIEnv * env, jclass cls, jlong window, jlong callbackptr) { \
void** oldCallback = (void**) &pojav_environ->GLFW_invoke_##NAME; \
pojav_environ->GLFW_invoke_##NAME = (GLFW_invoke_##NAME##_func*) (uintptr_t) callbackptr; \
return (jlong) (uintptr_t) *oldCallback; \
}
ADD_CALLBACK_WWIN(Char)
ADD_CALLBACK_WWIN(CharMods)
ADD_CALLBACK_WWIN(CursorEnter)
ADD_CALLBACK_WWIN(CursorPos)
ADD_CALLBACK_WWIN(Key)
ADD_CALLBACK_WWIN(MouseButton)
ADD_CALLBACK_WWIN(Scroll)
#undef ADD_CALLBACK_WWIN
void updateMonitorSize(int width, int height) {
(*pojav_environ->glfwThreadVmEnv)->CallStaticVoidMethod(pojav_environ->glfwThreadVmEnv, pojav_environ->vmGlfwClass, pojav_environ->method_internalChangeMonitorSize, width, height);
}
void updateWindowSize(void* window) {
(*pojav_environ->glfwThreadVmEnv)->CallStaticVoidMethod(pojav_environ->glfwThreadVmEnv, pojav_environ->vmGlfwClass, pojav_environ->method_internalWindowSizeChanged, (jlong)window);
}
void pojavPumpEvents(void* window) {
if(pojav_environ->shouldUpdateMouse) {
// Floored because some anticheats (Hypixel) don't like the input being too accurate.
// Actual GLFW actually uses doubles so this is totally wrong on their end.
pojav_environ->GLFW_invoke_CursorPos(window, floor(pojav_environ->cursorX),
floor(pojav_environ->cursorY));
}
if(pojav_environ->shouldUpdateMonitorSize) {
updateWindowSize(window);
}
size_t index = pojav_environ->outEventIndex;
size_t targetIndex = pojav_environ->outTargetIndex;
while (targetIndex != index) {
GLFWInputEvent event = pojav_environ->events[index];
switch (event.type) {
case EVENT_TYPE_CHAR:
if(pojav_environ->GLFW_invoke_Char) pojav_environ->GLFW_invoke_Char(window, event.i1);
break;
case EVENT_TYPE_CHAR_MODS:
if(pojav_environ->GLFW_invoke_CharMods) pojav_environ->GLFW_invoke_CharMods(window, event.i1, event.i2);
break;
case EVENT_TYPE_KEY:
if(pojav_environ->GLFW_invoke_Key) pojav_environ->GLFW_invoke_Key(window, event.i1, event.i2, event.i3, event.i4);
break;
case EVENT_TYPE_MOUSE_BUTTON:
if(pojav_environ->GLFW_invoke_MouseButton) pojav_environ->GLFW_invoke_MouseButton(window, event.i1, event.i2, event.i3);
break;
case EVENT_TYPE_CURSOR_ENTER:
if(pojav_environ->GLFW_invoke_CursorEnter) pojav_environ->GLFW_invoke_CursorEnter(window, event.i1);
break;
case EVENT_TYPE_SCROLL:
if(pojav_environ->GLFW_invoke_Scroll) pojav_environ->GLFW_invoke_Scroll(window, event.i1, event.i2);
break;
}
index++;
if (index >= EVENT_WINDOW_SIZE)
index -= EVENT_WINDOW_SIZE;
}
// The out target index is updated by the rewinder
}
/** Prepare the library for sending out callbacks to all windows */
void pojavStartPumping() {
size_t counter = atomic_load_explicit(&pojav_environ->eventCounter, memory_order_acquire);
size_t index = pojav_environ->outEventIndex;
unsigned targetIndex = index + counter;
if (targetIndex >= EVENT_WINDOW_SIZE)
targetIndex -= EVENT_WINDOW_SIZE;
// Only accessed by one unique thread, no need for atomic store
pojav_environ->inEventCount = counter;
pojav_environ->outTargetIndex = targetIndex;
//PumpEvents is called for every window, so this logic should be there in order to correctly distribute events to all windows.
if((pojav_environ->cLastX != pojav_environ->cursorX || pojav_environ->cLastY != pojav_environ->cursorY) && pojav_environ->GLFW_invoke_CursorPos) {
pojav_environ->cLastX = pojav_environ->cursorX;
pojav_environ->cLastY = pojav_environ->cursorY;
pojav_environ->shouldUpdateMouse = true;
}
if(pojav_environ->shouldUpdateMonitorSize) {
// Perform a monitor size update here to avoid doing it on every single window
updateMonitorSize(pojav_environ->savedWidth, pojav_environ->savedHeight);
// Mark the monitor size as consumed (since GLFW was made aware of it)
pojav_environ->monitorSizeConsumed = true;
}
}
/** Prepare the library for the next round of new events */
void pojavStopPumping() {
pojav_environ->outEventIndex = pojav_environ->outTargetIndex;
// New events may have arrived while pumping, so remove only the difference before the start and end of execution
atomic_fetch_sub_explicit(&pojav_environ->eventCounter, pojav_environ->inEventCount, memory_order_acquire);
// Make sure the next frame won't send mouse or monitor updates if it's unnecessary
pojav_environ->shouldUpdateMouse = false;
// Only reset the update flag if the monitor size was consumed by pojavStartPumping. This
// will delay the update to next frame if it had occured between pojavStartPumping and pojavStopPumping,
// but it's better than not having it apply at all
if(pojav_environ->shouldUpdateMonitorSize && pojav_environ->monitorSizeConsumed) {
pojav_environ->shouldUpdateMonitorSize = false;
pojav_environ->monitorSizeConsumed = false;
}
}
JNIEXPORT void JNICALL
Java_org_lwjgl_glfw_GLFW_nglfwGetCursorPos(JNIEnv *env, __attribute__((unused)) jclass clazz, __attribute__((unused)) jlong window, jobject xpos,
jobject ypos) {
*(double*)(*env)->GetDirectBufferAddress(env, xpos) = pojav_environ->cursorX;
*(double*)(*env)->GetDirectBufferAddress(env, ypos) = pojav_environ->cursorY;
}
JNIEXPORT void JNICALL JavaCritical_org_lwjgl_glfw_GLFW_nglfwGetCursorPosA(__attribute__((unused)) jlong window, jint lengthx, jdouble* xpos, jint lengthy, jdouble* ypos) {
*xpos = pojav_environ->cursorX;
*ypos = pojav_environ->cursorY;
}
JNIEXPORT void JNICALL
Java_org_lwjgl_glfw_GLFW_nglfwGetCursorPosA(JNIEnv *env, __attribute__((unused)) jclass clazz, __attribute__((unused)) jlong window,
jdoubleArray xpos, jdoubleArray ypos) {
(*env)->SetDoubleArrayRegion(env, xpos, 0,1, &pojav_environ->cursorX);
(*env)->SetDoubleArrayRegion(env, ypos, 0,1, &pojav_environ->cursorY);
}
JNIEXPORT void JNICALL JavaCritical_org_lwjgl_glfw_GLFW_glfwSetCursorPos(__attribute__((unused)) jlong window, jdouble xpos,
jdouble ypos) {
pojav_environ->cLastX = pojav_environ->cursorX = xpos;
pojav_environ->cLastY = pojav_environ->cursorY = ypos;
}
JNIEXPORT void JNICALL
Java_org_lwjgl_glfw_GLFW_glfwSetCursorPos(__attribute__((unused)) JNIEnv *env, __attribute__((unused)) jclass clazz, __attribute__((unused)) jlong window, jdouble xpos,
jdouble ypos) {
JavaCritical_org_lwjgl_glfw_GLFW_glfwSetCursorPos(window, xpos, ypos);
}
void sendData(int type, int i1, int i2, int i3, int i4) {
GLFWInputEvent *event = &pojav_environ->events[pojav_environ->inEventIndex];
event->type = type;
event->i1 = i1;
event->i2 = i2;
event->i3 = i3;
event->i4 = i4;
if (++pojav_environ->inEventIndex >= EVENT_WINDOW_SIZE)
pojav_environ->inEventIndex -= EVENT_WINDOW_SIZE;
atomic_fetch_add_explicit(&pojav_environ->eventCounter, 1, memory_order_acquire);
}
void critical_set_stackqueue(jboolean use_input_stack_queue) {
pojav_environ->isUseStackQueueCall = (int) use_input_stack_queue;
}
void noncritical_set_stackqueue(__attribute__((unused)) JNIEnv *env, __attribute__((unused)) jclass clazz, jboolean use_input_stack_queue) {
critical_set_stackqueue(use_input_stack_queue);
}
JNIEXPORT jstring JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeClipboard(JNIEnv* env, __attribute__((unused)) jclass clazz, jint action, jbyteArray copySrc) {
#ifdef DEBUG
LOGD("Debug: Clipboard access is going on\n", pojav_environ->isUseStackQueueCall);
#endif
JNIEnv *dalvikEnv;
(*pojav_environ->dalvikJavaVMPtr)->AttachCurrentThread(pojav_environ->dalvikJavaVMPtr, &dalvikEnv, NULL);
assert(dalvikEnv != NULL);
assert(pojav_environ->bridgeClazz != NULL);
LOGD("Clipboard: Converting string\n");
char *copySrcC;
jstring copyDst = NULL;
if (copySrc) {
copySrcC = (char *)((*env)->GetByteArrayElements(env, copySrc, NULL));
copyDst = (*dalvikEnv)->NewStringUTF(dalvikEnv, copySrcC);
}
LOGD("Clipboard: Calling 2nd\n");
jstring pasteDst = convertStringJVM(dalvikEnv, env, (jstring) (*dalvikEnv)->CallStaticObjectMethod(dalvikEnv, pojav_environ->bridgeClazz, pojav_environ->method_accessAndroidClipboard, action, copyDst));
if (copySrc) {
(*dalvikEnv)->DeleteLocalRef(dalvikEnv, copyDst);
(*env)->ReleaseByteArrayElements(env, copySrc, (jbyte *)copySrcC, 0);
}
(*pojav_environ->dalvikJavaVMPtr)->DetachCurrentThread(pojav_environ->dalvikJavaVMPtr);
return pasteDst;
}
JNIEXPORT jboolean JNICALL JavaCritical_org_lwjgl_glfw_CallbackBridge_nativeSetInputReady(jboolean inputReady) {
#ifdef DEBUG
LOGD("Debug: Changing input state, isReady=%d, pojav_environ->isUseStackQueueCall=%d\n", inputReady, pojav_environ->isUseStackQueueCall);
#endif
LOGI("Input ready: %i", inputReady);
pojav_environ->isInputReady = inputReady;
return pojav_environ->isUseStackQueueCall;
}
JNIEXPORT jboolean JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSetInputReady(__attribute__((unused)) JNIEnv* env, __attribute__((unused)) jclass clazz, jboolean inputReady) {
return JavaCritical_org_lwjgl_glfw_CallbackBridge_nativeSetInputReady(inputReady);
}
JNIEXPORT void JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSetGrabbing(__attribute__((unused)) JNIEnv* env, __attribute__((unused)) jclass clazz, jboolean grabbing) {
TRY_ATTACH_ENV(dvm_env, pojav_environ->dalvikJavaVMPtr, "nativeSetGrabbing failed!\n", return;);
(*dvm_env)->CallStaticVoidMethod(dvm_env, pojav_environ->bridgeClazz, pojav_environ->method_onGrabStateChanged, grabbing);
pojav_environ->isGrabbing = grabbing;
}
JNIEXPORT jboolean JNICALL
Java_org_lwjgl_glfw_CallbackBridge_nativeEnableGamepadDirectInput(__attribute__((unused)) JNIEnv *env, __attribute__((unused)) jclass clazz) {
TRY_ATTACH_ENV(dvm_env, pojav_environ->dalvikJavaVMPtr, "nativeEnableGamepadDirectInput failed!\n", return JNI_FALSE;);
(*dvm_env)->CallStaticVoidMethod(dvm_env, pojav_environ->bridgeClazz, pojav_environ->method_onDirectInputEnable);
return JNI_TRUE;
}
JNIEXPORT jfloat JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeGetAndroidDPI(JNIEnv* env, __attribute__((unused)) jclass clazz) {
TRY_ATTACH_ENV(dvm_env, pojav_environ->dalvikJavaVMPtr, "getAndroidDPI failed!\n",);
jfloat result = (*dvm_env)->CallStaticFloatMethod(dvm_env, pojav_environ->bridgeClazz,
pojav_environ->method_getAndroidDPI);
return result;
}
jboolean critical_send_char(jchar codepoint) {
if (pojav_environ->GLFW_invoke_Char && pojav_environ->isInputReady) {
if (pojav_environ->isUseStackQueueCall) {
sendData(EVENT_TYPE_CHAR, codepoint, 0, 0, 0);
} else {
pojav_environ->GLFW_invoke_Char((void*) pojav_environ->showingWindow, (unsigned int) codepoint);
}
return JNI_TRUE;
}
return JNI_FALSE;
}
jboolean noncritical_send_char(__attribute__((unused)) JNIEnv* env, __attribute__((unused)) jclass clazz, jchar codepoint) {
return critical_send_char(codepoint);
}
jboolean critical_send_char_mods(jchar codepoint, jint mods) {
if (pojav_environ->GLFW_invoke_CharMods && pojav_environ->isInputReady) {
if (pojav_environ->isUseStackQueueCall) {
sendData(EVENT_TYPE_CHAR_MODS, (int) codepoint, mods, 0, 0);
} else {
pojav_environ->GLFW_invoke_CharMods((void*) pojav_environ->showingWindow, codepoint, mods);
}
return JNI_TRUE;
}
return JNI_FALSE;
}
jboolean noncritical_send_char_mods(__attribute__((unused)) JNIEnv* env, __attribute__((unused)) jclass clazz, jchar codepoint, jint mods) {
return critical_send_char_mods(codepoint, mods);
}
/*
JNIEXPORT void JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSendCursorEnter(JNIEnv* env, jclass clazz, jint entered) {
if (pojav_environ->GLFW_invoke_CursorEnter && pojav_environ->isInputReady) {
pojav_environ->GLFW_invoke_CursorEnter(pojav_environ->showingWindow, entered);
}
}
*/
void critical_send_cursor_pos(jfloat x, jfloat y) {
#ifdef DEBUG
LOGD("Sending cursor position \n");
#endif
if (pojav_environ->GLFW_invoke_CursorPos && pojav_environ->isInputReady) {
#ifdef DEBUG
LOGD("pojav_environ->GLFW_invoke_CursorPos && pojav_environ->isInputReady \n");
#endif
if (!pojav_environ->isCursorEntered) {
if (pojav_environ->GLFW_invoke_CursorEnter) {
pojav_environ->isCursorEntered = true;
if (pojav_environ->isUseStackQueueCall) {
sendData(EVENT_TYPE_CURSOR_ENTER, 1, 0, 0, 0);
} else {
pojav_environ->GLFW_invoke_CursorEnter((void*) pojav_environ->showingWindow, 1);
}
}
}
if (!pojav_environ->isUseStackQueueCall) {
// Truncated by int cast in LWJGLX
pojav_environ->GLFW_invoke_CursorPos((void*) pojav_environ->showingWindow, (double) (x), (double) (y));
} else {
// Floored in pojavPumpEvents
pojav_environ->cursorX = x;
pojav_environ->cursorY = y;
}
}
}
void noncritical_send_cursor_pos(__attribute__((unused)) JNIEnv* env, __attribute__((unused)) jclass clazz, jfloat x, jfloat y) {
critical_send_cursor_pos(x, y);
}
#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
void critical_send_key(jint key, jint scancode, jint action, jint mods) {
if (pojav_environ->GLFW_invoke_Key && pojav_environ->isInputReady) {
pojav_environ->keyDownBuffer[max(0, key-31)] = (jbyte) action;
if (pojav_environ->isUseStackQueueCall) {
sendData(EVENT_TYPE_KEY, key, scancode, action, mods);
} else {
pojav_environ->GLFW_invoke_Key((void*) pojav_environ->showingWindow, key, scancode, action, mods);
}
}
}
void noncritical_send_key(__attribute__((unused)) JNIEnv* env, __attribute__((unused)) jclass clazz, jint key, jint scancode, jint action, jint mods) {
critical_send_key(key, scancode, action, mods);
}
void critical_send_mouse_button(jint button, jint action, jint mods) {
if (pojav_environ->GLFW_invoke_MouseButton && pojav_environ->isInputReady) {
pojav_environ->mouseDownBuffer[max(0, button)] = (jbyte) action;
if (pojav_environ->isUseStackQueueCall) {
sendData(EVENT_TYPE_MOUSE_BUTTON, button, action, mods, 0);
} else {
pojav_environ->GLFW_invoke_MouseButton((void*) pojav_environ->showingWindow, button, action, mods);
}
}
}
void noncritical_send_mouse_button(__attribute__((unused)) JNIEnv* env, __attribute__((unused)) jclass clazz, jint button, jint action, jint mods) {
critical_send_mouse_button(button, action, mods);
}
void critical_send_screen_size(jint width, jint height) {
pojav_environ->savedWidth = width;
pojav_environ->savedHeight = height;
// Even if there was call to pojavStartPumping that consumed the size, this call
// might happen right after it (or right before pojavStopPumping)
// So unmark the size as "consumed"
pojav_environ->monitorSizeConsumed = false;
pojav_environ->shouldUpdateMonitorSize = true;
// Don't use the direct updates for screen dimensions.
// This is done to ensure that we have predictable conditions to correctly call
// updateMonitorSize() and updateWindowSize() while on the render thread with an attached
// JNIEnv.
}
void noncritical_send_screen_size(__attribute__((unused)) JNIEnv* env, __attribute__((unused)) jclass clazz, jint width, jint height) {
critical_send_screen_size(width, height);
}
void critical_send_scroll(jdouble xoffset, jdouble yoffset) {
if (pojav_environ->GLFW_invoke_Scroll && pojav_environ->isInputReady) {
if (pojav_environ->isUseStackQueueCall) {
sendData(EVENT_TYPE_SCROLL, (int)xoffset, (int)yoffset, 0, 0);
} else {
pojav_environ->GLFW_invoke_Scroll((void*) pojav_environ->showingWindow, (double) xoffset, (double) yoffset);
}
}
}
void noncritical_send_scroll(__attribute__((unused)) JNIEnv* env, __attribute__((unused)) jclass clazz, jdouble xoffset, jdouble yoffset) {
critical_send_scroll(xoffset, yoffset);
}
JNIEXPORT void JNICALL Java_org_lwjgl_glfw_GLFW_nglfwSetShowingWindow(__attribute__((unused)) JNIEnv* env, __attribute__((unused)) jclass clazz, jlong window) {
pojav_environ->showingWindow = (jlong) window;
}
JNIEXPORT void JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSetWindowAttrib(__attribute__((unused)) JNIEnv* env, __attribute__((unused)) jclass clazz, jint attrib, jint value) {
// Check for stack queue no longer necessary here as the JVM crash's origin is resolved
if (!pojav_environ->showingWindow) {
// If the window is not shown, there is nothing to do yet.
return;
}
// We cannot use pojav_environ->runtimeJNIEnvPtr_JRE here because that environment is attached
// on the thread that loaded pojavexec (which is the thread that first references the GLFW class)
// But this method is only called from the Android UI thread
// Technically the better solution would be to have a permanently attached env pointer stored
// in environ for the Android UI thread but this is the only place that uses it
// (very rarely, only in lifecycle callbacks) so i dont care
TRY_ATTACH_ENV(jvm_env, pojav_environ->runtimeJavaVMPtr, "nativeSetWindowAttrib failed: %i", return;);
(*jvm_env)->CallStaticVoidMethod(
jvm_env, pojav_environ->vmGlfwClass,
pojav_environ->method_glftSetWindowAttrib,
(jlong) pojav_environ->showingWindow, attrib, value
);
// Attaching every time is annoying, so stick the attachment to the Android GUI thread around
}
const static JNINativeMethod critical_fcns[] = {
{"nativeSetUseInputStackQueue", "(Z)V", critical_set_stackqueue},
{"nativeSendChar", "(C)Z", critical_send_char},
{"nativeSendCharMods", "(CI)Z", critical_send_char_mods},
{"nativeSendKey", "(IIII)V", critical_send_key},
{"nativeSendCursorPos", "(FF)V", critical_send_cursor_pos},
{"nativeSendMouseButton", "(III)V", critical_send_mouse_button},
{"nativeSendScroll", "(DD)V", critical_send_scroll},
{"nativeSendScreenSize", "(II)V", critical_send_screen_size}
};
const static JNINativeMethod noncritical_fcns[] = {
{"nativeSetUseInputStackQueue", "(Z)V", noncritical_set_stackqueue},
{"nativeSendChar", "(C)Z", noncritical_send_char},
{"nativeSendCharMods", "(CI)Z", noncritical_send_char_mods},
{"nativeSendKey", "(IIII)V", noncritical_send_key},
{"nativeSendCursorPos", "(FF)V", noncritical_send_cursor_pos},
{"nativeSendMouseButton", "(III)V", noncritical_send_mouse_button},
{"nativeSendScroll", "(DD)V", noncritical_send_scroll},
{"nativeSendScreenSize", "(II)V", noncritical_send_screen_size}
};
static bool criticalNativeAvailable;
void dvm_testCriticalNative(void* arg0, void* arg1, void* arg2, void* arg3) {
if(arg0 != 0 && arg2 == 0 && arg3 == 0) {
criticalNativeAvailable = false;
}else if (arg0 == 0 && arg1 == 0){
criticalNativeAvailable = true;
}else {
criticalNativeAvailable = false; // just to be safe
}
}
static bool tryCriticalNative(JNIEnv *env) {
static const JNINativeMethod testJNIMethod[] = {
{ "testCriticalNative", "(II)V", dvm_testCriticalNative}
};
jclass criticalNativeTest = (*env)->FindClass(env, "net/kdt/pojavlaunch/CriticalNativeTest");
if(criticalNativeTest == NULL) {
LOGD("No CriticalNativeTest class found !");
(*env)->ExceptionClear(env);
return false;
}
jmethodID criticalNativeTestMethod = (*env)->GetStaticMethodID(env, criticalNativeTest, "invokeTest", "()V");
(*env)->RegisterNatives(env, criticalNativeTest, testJNIMethod, 1);
(*env)->CallStaticVoidMethod(env, criticalNativeTest, criticalNativeTestMethod);
(*env)->UnregisterNatives(env, criticalNativeTest);
return criticalNativeAvailable;
}
static void registerFunctions(JNIEnv *env) {
bool use_critical_cc = tryCriticalNative(env);
jclass bridge_class = (*env)->FindClass(env, "org/lwjgl/glfw/CallbackBridge");
if(use_critical_cc) {
LOGI("CriticalNative is available. Enjoy the 4.6x times faster input!");
}else{
LOGI("CriticalNative is not available. Upgrade, maybe?");
}
(*env)->RegisterNatives(env,
bridge_class,
use_critical_cc ? critical_fcns : noncritical_fcns,
sizeof(critical_fcns)/sizeof(critical_fcns[0]));
}
JNIEXPORT jlong JNICALL
Java_org_lwjgl_glfw_GLFW_internalGetGamepadDataPointer(JNIEnv *env, jclass clazz) {
return (jlong) &pojav_environ->gamepadState;
}
JNIEXPORT jobject JNICALL
Java_org_lwjgl_glfw_CallbackBridge_nativeCreateGamepadButtonBuffer(JNIEnv *env, jclass clazz) {
return (*env)->NewDirectByteBuffer(env, &pojav_environ->gamepadState.buttons, sizeof(pojav_environ->gamepadState.buttons));
}
JNIEXPORT jobject JNICALL
Java_org_lwjgl_glfw_CallbackBridge_nativeCreateGamepadAxisBuffer(JNIEnv *env, jclass clazz) {
return (*env)->NewDirectByteBuffer(env, &pojav_environ->gamepadState.axes, sizeof(pojav_environ->gamepadState.axes));
}
// HACK: Legacy4J has faulty detection that hardwires us to GLFW unless we init SDL ourselves.
// This is a horribly made function that should really have more checks around it but meh.
#define SDL_INIT_JOYSTICK 0x00000200u
#define SDL_INIT_GAMEPAD 0x00002000u
#define SDL_INIT_EVENTS 0x00004000u
static inline void initSubsystem(void) {
typedef int (*SDL_Init_Func)(uint32_t flags);
void* handle = dlopen("libSDL3.so", RTLD_NOW);
SDL_Init_Func SDL_Init = (SDL_Init_Func)dlsym(handle, "SDL_Init");
SDL_Init(SDL_INIT_GAMEPAD | SDL_INIT_JOYSTICK | SDL_INIT_EVENTS);
}
JNIEXPORT void JNICALL
Java_net_kdt_pojavlaunch_Tools_00024SDL_initializeControllerSubsystems(JNIEnv *env, jclass clazz){
// Please ensure that you have already dlopen'ed SDL3 before calling this.
initSubsystem();
}