Skip to content

Commit fd7382a

Browse files
committed
Small refactoring.
1 parent 3ece0b9 commit fd7382a

File tree

4 files changed

+102
-63
lines changed

4 files changed

+102
-63
lines changed

src/java/org/jnativehook/GlobalScreen.java

Lines changed: 46 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,27 @@ public NativeHookException getException() {
325325
* Native implementation to stop the input hook. There is no other way to stop the hook.
326326
*/
327327
public native void disable() throws NativeHookException;
328+
329+
/**
330+
* Dispatches an event to the appropriate processor. This method is
331+
* generally called by the native library but may be used to synthesize
332+
* native events from Java without replaying them on the native system. If
333+
* you would like to send events to other applications, please use
334+
* {@link #postNativeEvent},
335+
* <p>
336+
*
337+
* <b>Note:</b> This method executes on the native system's event queue.
338+
* It is imperative that all processing be off-loaded to other threads.
339+
* Failure to do so might result in the delay of user input and the automatic
340+
* removal of the native hook.
341+
*
342+
* @param event the <code>NativeInputEvent</code> sent to the registered event listeners.
343+
*/
344+
protected static void dispatchEvent(NativeInputEvent event) {
345+
if (eventExecutor != null) {
346+
eventExecutor.execute(new EventDispatchTask(event));
347+
}
348+
}
328349
}
329350

330351

@@ -500,24 +521,20 @@ else if (event instanceof NativeMouseWheelEvent) {
500521
private void processKeyEvent(NativeKeyEvent e) {
501522
NativeKeyListener[] listeners = eventListeners.getListeners(NativeKeyListener.class);
502523

503-
switch (e.getID()) {
504-
case NativeKeyEvent.NATIVE_KEY_PRESSED:
505-
for (int i = 0; i < listeners.length; i++) {
524+
for (int i = 0; i < listeners.length; i++) {
525+
switch (e.getID()) {
526+
case NativeKeyEvent.NATIVE_KEY_PRESSED:
506527
listeners[i].nativeKeyPressed(e);
507-
}
508-
break;
528+
break;
509529

510-
case NativeKeyEvent.NATIVE_KEY_TYPED:
511-
for (int i = 0; i < listeners.length; i++) {
530+
case NativeKeyEvent.NATIVE_KEY_TYPED:
512531
listeners[i].nativeKeyTyped(e);
513-
}
514-
break;
532+
break;
515533

516-
case NativeKeyEvent.NATIVE_KEY_RELEASED:
517-
for (int i = 0; i < listeners.length; i++) {
534+
case NativeKeyEvent.NATIVE_KEY_RELEASED:
518535
listeners[i].nativeKeyReleased(e);
519-
}
520-
break;
536+
break;
537+
}
521538
}
522539
}
523540

@@ -533,24 +550,20 @@ private void processKeyEvent(NativeKeyEvent e) {
533550
private void processButtonEvent(NativeMouseEvent e) {
534551
NativeMouseListener[] listeners = eventListeners.getListeners(NativeMouseListener.class);
535552

536-
switch (e.getID()) {
537-
case NativeMouseEvent.NATIVE_MOUSE_CLICKED:
538-
for (int i = 0; i < listeners.length; i++) {
553+
for (int i = 0; i < listeners.length; i++) {
554+
switch (e.getID()) {
555+
case NativeMouseEvent.NATIVE_MOUSE_CLICKED:
539556
listeners[i].nativeMouseClicked(e);
540-
}
541-
break;
557+
break;
542558

543-
case NativeMouseEvent.NATIVE_MOUSE_PRESSED:
544-
for (int i = 0; i < listeners.length; i++) {
559+
case NativeMouseEvent.NATIVE_MOUSE_PRESSED:
545560
listeners[i].nativeMousePressed(e);
546-
}
547-
break;
561+
break;
548562

549-
case NativeMouseEvent.NATIVE_MOUSE_RELEASED:
550-
for (int i = 0; i < listeners.length; i++) {
563+
case NativeMouseEvent.NATIVE_MOUSE_RELEASED:
551564
listeners[i].nativeMouseReleased(e);
552-
}
553-
break;
565+
break;
566+
}
554567
}
555568
}
556569

@@ -566,18 +579,16 @@ private void processButtonEvent(NativeMouseEvent e) {
566579
private void processMouseEvent(NativeMouseEvent e) {
567580
NativeMouseMotionListener[] listeners = eventListeners.getListeners(NativeMouseMotionListener.class);
568581

569-
switch (e.getID()) {
570-
case NativeMouseEvent.NATIVE_MOUSE_MOVED:
571-
for (int i = 0; i < listeners.length; i++) {
582+
for (int i = 0; i < listeners.length; i++) {
583+
switch (e.getID()) {
584+
case NativeMouseEvent.NATIVE_MOUSE_MOVED:
572585
listeners[i].nativeMouseMoved(e);
573-
}
574-
break;
586+
break;
575587

576-
case NativeMouseEvent.NATIVE_MOUSE_DRAGGED:
577-
for (int i = 0; i < listeners.length; i++) {
588+
case NativeMouseEvent.NATIVE_MOUSE_DRAGGED:
578589
listeners[i].nativeMouseDragged(e);
579-
}
580-
break;
590+
break;
591+
}
581592
}
582593
}
583594

@@ -600,27 +611,6 @@ private void processMouseWheelEvent(NativeMouseWheelEvent e) {
600611
}
601612
}
602613

603-
/**
604-
* Dispatches an event to the appropriate processor. This method is
605-
* generally called by the native library but may be used to synthesize
606-
* native events from Java without replaying them on the native system. If
607-
* you would like to send events to other applications, please use
608-
* {@link #postNativeEvent},
609-
* <p>
610-
*
611-
* <b>Note:</b> This method executes on the native system's event queue.
612-
* It is imperative that all processing be off-loaded to other threads.
613-
* Failure to do so might result in the delay of user input and the automatic
614-
* removal of the native hook.
615-
*
616-
* @param event the <code>NativeInputEvent</code> sent to the registered event listeners.
617-
*/
618-
protected static void dispatchEvent(NativeInputEvent event) {
619-
if (eventExecutor != null) {
620-
eventExecutor.execute(new EventDispatchTask(event));
621-
}
622-
}
623-
624614
/**
625615
* Set a different executor service for native event delivery. By default,
626616
* JNativeHook utilizes a single thread executor to dispatch events from

src/jni/jni_EventDispatcher.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ void jni_EventDispatcher(uiohook_event * const event) {
204204
// Dispatch the event.
205205
(*env)->CallStaticVoidMethod(
206206
env,
207-
org_jnativehook_GlobalScreen->cls,
208-
org_jnativehook_GlobalScreen->dispatchEvent,
207+
org_jnativehook_GlobalScreen$NativeHookThread->cls,
208+
org_jnativehook_GlobalScreen$NativeHookThread->dispatchEvent,
209209
NativeInputEvent_obj);
210210

211211
// Set the propagate flag from java.

src/jni/jni_Globals.c

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "jni_Logger.h"
2525

2626
GlobalScreen *org_jnativehook_GlobalScreen = NULL;
27+
NativeHookThread *org_jnativehook_GlobalScreen$NativeHookThread = NULL;
2728
NativeHookException *org_jnativehook_NativeHookException = NULL;
2829
NativeMonitorInfo *org_jnativehook_NativeMonitorInfo = NULL;
2930
NativeInputEvent *org_jnativehook_NativeInputEvent = NULL;
@@ -44,16 +45,12 @@ static int create_GlobalScreen(JNIEnv *env) {
4445
// Get the field ID for hookThread.
4546
jfieldID hookThread = (*env)->GetStaticFieldID(env, GlobalScreen_class, "hookThread", "Lorg/jnativehook/GlobalScreen$NativeHookThread;");
4647

47-
// Get the method ID for GlobalScreen.dispatchEvent().
48-
jmethodID dispatchEvent = (*env)->GetStaticMethodID(env, GlobalScreen_class, "dispatchEvent", "(Lorg/jnativehook/NativeInputEvent;)V");
49-
5048
if ((*env)->ExceptionCheck(env) == JNI_FALSE) {
5149
org_jnativehook_GlobalScreen = malloc(sizeof(GlobalScreen));
5250
if (org_jnativehook_GlobalScreen != NULL) {
5351
// Populate our structure for later use.
5452
org_jnativehook_GlobalScreen->cls = (jclass) (*env)->NewGlobalRef(env, GlobalScreen_class);
5553
org_jnativehook_GlobalScreen->hookThread = hookThread;
56-
org_jnativehook_GlobalScreen->dispatchEvent = dispatchEvent;
5754

5855
status = JNI_OK;
5956
}
@@ -81,6 +78,48 @@ static void destroy_GlobalScreen(JNIEnv *env) {
8178
}
8279

8380

81+
static int create_NativeHookThread(JNIEnv *env) {
82+
int status = JNI_ERR;
83+
84+
// Class and Constructor for the GlobalScreen Object.
85+
jclass NativeHookThread_class = (*env)->FindClass(env, "org/jnativehook/GlobalScreen$NativeHookThread");
86+
if (NativeHookThread_class != NULL) {
87+
// Get the method ID for GlobalScreen.dispatchEvent().
88+
jmethodID dispatchEvent = (*env)->GetStaticMethodID(env, NativeHookThread_class, "dispatchEvent", "(Lorg/jnativehook/NativeInputEvent;)V");
89+
90+
if ((*env)->ExceptionCheck(env) == JNI_FALSE) {
91+
org_jnativehook_GlobalScreen$NativeHookThread = malloc(sizeof(NativeHookThread));
92+
if (org_jnativehook_GlobalScreen$NativeHookThread != NULL) {
93+
// Populate our structure for later use.
94+
org_jnativehook_GlobalScreen$NativeHookThread->cls = (jclass) (*env)->NewGlobalRef(env, NativeHookThread_class);
95+
org_jnativehook_GlobalScreen$NativeHookThread->dispatchEvent = dispatchEvent;
96+
97+
status = JNI_OK;
98+
}
99+
else {
100+
jni_ThrowException(env, "java/lang/OutOfMemoryError", "Failed to allocate native memory.");
101+
status = JNI_ENOMEM;
102+
}
103+
}
104+
}
105+
106+
return status;
107+
}
108+
109+
static void destroy_NativeHookThread(JNIEnv *env) {
110+
if (org_jnativehook_GlobalScreen != NULL) {
111+
// The class *should* never be null if the struct was allocated, but we will check anyway.
112+
if (org_jnativehook_GlobalScreen$NativeHookThread->cls != NULL) {
113+
(*env)->DeleteGlobalRef(env, org_jnativehook_GlobalScreen$NativeHookThread->cls);
114+
}
115+
116+
// Free struct memory.
117+
free(org_jnativehook_GlobalScreen$NativeHookThread);
118+
org_jnativehook_GlobalScreen$NativeHookThread = NULL;
119+
}
120+
}
121+
122+
84123
static int create_NativeHookException(JNIEnv *env) {
85124
int status = JNI_ERR;
86125

@@ -581,6 +620,10 @@ static inline void destroy_Logger(JNIEnv *env) {
581620
int jni_CreateGlobals(JNIEnv *env) {
582621
int status = create_GlobalScreen(env);
583622

623+
if (status == JNI_OK && (*env)->ExceptionCheck(env) == JNI_FALSE) {
624+
status = create_NativeHookThread(env);
625+
}
626+
584627
if (status == JNI_OK && (*env)->ExceptionCheck(env) == JNI_FALSE) {
585628
status = create_NativeHookException(env);
586629
}
@@ -631,6 +674,7 @@ int jni_CreateGlobals(JNIEnv *env) {
631674

632675
int jni_DestroyGlobals(JNIEnv *env) {
633676
destroy_GlobalScreen(env);
677+
destroy_NativeHookThread(env);
634678
destroy_NativeHookException(env);
635679
destroy_NativeMonitorInfo(env);
636680
destroy_NativeInputEvent(env);

src/jni/jni_Globals.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ extern JavaVMAttachArgs jvm_attach_args;
3535
typedef struct _org_jnativehook_GlobalScreen {
3636
jclass cls;
3737
jfieldID hookThread;
38-
jmethodID dispatchEvent;
3938
} GlobalScreen;
4039

40+
typedef struct org_jnativehook_GlobalScreen$NativeHookThread {
41+
jclass cls;
42+
jmethodID dispatchEvent;
43+
} NativeHookThread;
44+
4145
typedef struct _org_jnativehook_NativeHookException {
4246
jclass cls;
4347
jmethodID init;
@@ -112,6 +116,7 @@ typedef struct _java_util_logging_Logger {
112116

113117
// Global variables for Java object struct representation.
114118
extern GlobalScreen *org_jnativehook_GlobalScreen;
119+
extern NativeHookThread *org_jnativehook_GlobalScreen$NativeHookThread;
115120
extern NativeHookException *org_jnativehook_NativeHookException;
116121
extern NativeMonitorInfo *org_jnativehook_NativeMonitorInfo;
117122
extern NativeInputEvent *org_jnativehook_NativeInputEvent;

0 commit comments

Comments
 (0)