Ejemplo n.º 1
0
static int create_NativeHookThread(JNIEnv *env) {
	int status = JNI_ERR;

	// Class and Constructor for the GlobalScreen Object.
	jclass NativeHookThread_class = (*env)->FindClass(env, "org/jnativehook/GlobalScreen$NativeHookThread");
	if (NativeHookThread_class != NULL) {
		// Get the method ID for GlobalScreen.dispatchEvent().
		jmethodID dispatchEvent = (*env)->GetStaticMethodID(env, NativeHookThread_class, "dispatchEvent", "(Lorg/jnativehook/NativeInputEvent;)V");

		if ((*env)->ExceptionCheck(env) == JNI_FALSE) {
			org_jnativehook_GlobalScreen$NativeHookThread = malloc(sizeof(NativeHookThread));
			if (org_jnativehook_GlobalScreen$NativeHookThread != NULL) {
				// Populate our structure for later use.
				org_jnativehook_GlobalScreen$NativeHookThread->cls = (jclass) (*env)->NewGlobalRef(env, NativeHookThread_class);
				org_jnativehook_GlobalScreen$NativeHookThread->dispatchEvent = dispatchEvent;

				status = JNI_OK;
			}
			else {
				jni_ThrowException(env, "java/lang/OutOfMemoryError", "Failed to allocate native memory.");
				status = JNI_ENOMEM;
			}
		}
	}

	return status;
}
Ejemplo n.º 2
0
static inline int create_System(JNIEnv *env) {
	int status = JNI_ERR;

	// Class and Constructor for the System Object.
	jclass System_class = (*env)->FindClass(env, "java/lang/System");
	if (System_class != NULL) {
		// Get the method ID for System.setProperty().
		jmethodID setProperty = (*env)->GetStaticMethodID(env, System_class, "setProperty", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");

		// Get the method ID for System.clearProperty().
		jmethodID clearProperty = (*env)->GetStaticMethodID(env, System_class, "clearProperty", "(Ljava/lang/String;)Ljava/lang/String;");

		if ((*env)->ExceptionCheck(env) == JNI_FALSE) {
			java_lang_System = malloc(sizeof(System));
			if (java_lang_System != NULL) {
				// Populate our structure for later use.
				java_lang_System->cls = (jclass) (*env)->NewGlobalRef(env, System_class);
				java_lang_System->setProperty = setProperty;
				java_lang_System->clearProperty = clearProperty;

				status = JNI_OK;
			}
			else {
				jni_ThrowException(env, "java/lang/OutOfMemoryError", "Failed to allocate native memory.");
				status = JNI_ENOMEM;
			}
		}
	}

	return status;
}
Ejemplo n.º 3
0
static int create_Object(JNIEnv *env) {
	int status = JNI_ERR;

	// Class and Constructor for the Object object.
	jclass Object_class = (*env)->FindClass(env, "java/lang/Object");
	if (Object_class != NULL) {
		// Get the method ID for Object.notify().
		jmethodID notify = (*env)->GetMethodID(env, Object_class, "notify", "()V");

		if ((*env)->ExceptionCheck(env) == JNI_FALSE) {
			java_lang_Object = malloc(sizeof(Object));
			if (java_lang_Object != NULL) {
				// Populate our structure for later use.
				java_lang_Object->cls = (jclass) (*env)->NewGlobalRef(env, Object_class);
				java_lang_Object->notify = notify;

				status = JNI_OK;
			}
			else {
				jni_ThrowException(env, "java/lang/OutOfMemoryError", "Failed to allocate native memory.");
				status = JNI_ENOMEM;
			}
		}
	}

	return status;
}
Ejemplo n.º 4
0
static int create_Integer(JNIEnv *env) {
	int status = JNI_ERR;

	// Class and Constructor for the Object object.
	jclass Integer_class = (*env)->FindClass(env, "java/lang/Integer");
	if (Integer_class != NULL) {
		// Get the method ID for NativeMouseWheelEvent constructor.
		jmethodID init = (*env)->GetMethodID(env, Integer_class, "<init>", "(I)V");

		if ((*env)->ExceptionCheck(env) == JNI_FALSE) {
			java_lang_Integer = malloc(sizeof(Integer));
			if (java_lang_Integer != NULL) {
				// Populate our structure for later use.
				java_lang_Integer->cls = (jclass) (*env)->NewGlobalRef(env, Integer_class);
				java_lang_Integer->init = init;

				status = JNI_OK;
			}
			else {
				jni_ThrowException(env, "java/lang/OutOfMemoryError", "Failed to allocate native memory.");
				status = JNI_ENOMEM;
			}
		}
	}

	return status;
}
Ejemplo n.º 5
0
static int create_GlobalScreen(JNIEnv *env) {
	int status = JNI_ERR;

	// Class and Constructor for the GlobalScreen Object.
	jclass GlobalScreen_class = (*env)->FindClass(env, "org/jnativehook/GlobalScreen");
	if (GlobalScreen_class != NULL) {
		// Get the field ID for hookThread.
		jfieldID hookThread = (*env)->GetStaticFieldID(env, GlobalScreen_class, "hookThread", "Lorg/jnativehook/GlobalScreen$NativeHookThread;");

		if ((*env)->ExceptionCheck(env) == JNI_FALSE) {
			org_jnativehook_GlobalScreen = malloc(sizeof(GlobalScreen));
			if (org_jnativehook_GlobalScreen != NULL) {
				// Populate our structure for later use.
				org_jnativehook_GlobalScreen->cls = (jclass) (*env)->NewGlobalRef(env, GlobalScreen_class);
				org_jnativehook_GlobalScreen->hookThread = hookThread;

				status = JNI_OK;
			}
			else {
				jni_ThrowException(env, "java/lang/OutOfMemoryError", "Failed to allocate native memory.");
				status = JNI_ENOMEM;
			}
		}
	}

	return status;
}
Ejemplo n.º 6
0
static int create_NativeMonitorInfo(JNIEnv *env) {
	int status = JNI_ERR;

	// Class and Constructor for the NativeMonitorInfo Object.
	jclass NativeMonitorInfo_class = (*env)->FindClass(env, "org/jnativehook/NativeMonitorInfo");
	if (NativeMonitorInfo_class != NULL) {
		// Get the method ID for NativeInputEvent constructor.
		jmethodID init = (*env)->GetMethodID(env, NativeMonitorInfo_class, "<init>", "(SIISS)V");

		if ((*env)->ExceptionCheck(env) == JNI_FALSE) {
			org_jnativehook_NativeMonitorInfo = malloc(sizeof(NativeMonitorInfo));
			if (org_jnativehook_NativeMonitorInfo != NULL) {
				// Populate our structure for later use.
				org_jnativehook_NativeMonitorInfo->cls = (jclass) (*env)->NewGlobalRef(env, NativeMonitorInfo_class);
				org_jnativehook_NativeMonitorInfo->init = init;

				status = JNI_OK;
			}
			else {
				jni_ThrowException(env, "java/lang/OutOfMemoryError", "Failed to allocate native memory.");
				status = JNI_ENOMEM;
			}
		}
	}

	return status;
}
Ejemplo n.º 7
0
static int create_NativeMouseEvent(JNIEnv *env) {
	int status = JNI_ERR;

	// Class and Constructor for the NativeMouseEvent Object.
	jclass NativeMouseEvent_class = (*env)->FindClass(env, "org/jnativehook/mouse/NativeMouseEvent");
	if (NativeMouseEvent_class != NULL) {
		// Get the method ID for NativeMouseEvent constructor.
		jmethodID init = (*env)->GetMethodID(env, NativeMouseEvent_class, "<init>", "(IIIIII)V");

		// Get the method ID for NativeMouseEvent.getButton().
		jmethodID getButton = (*env)->GetMethodID(env, NativeMouseEvent_class, "getButton", "()I");

		// Get the method ID for NativeMouseEvent.getClickCount().
		jmethodID getClickCount = (*env)->GetMethodID(env, NativeMouseEvent_class, "getClickCount", "()I");

		// Get the method ID for NativeMouseEvent.getX().
		jmethodID getX = (*env)->GetMethodID(env, NativeMouseEvent_class, "getX", "()I");

		// Get the method ID for NativeMouseEvent.getY().
		jmethodID getY = (*env)->GetMethodID(env, NativeMouseEvent_class, "getY", "()I");

		if ((*env)->ExceptionCheck(env) == JNI_FALSE) {
			org_jnativehook_mouse_NativeMouseEvent = malloc(sizeof(NativeMouseEvent));
			if (org_jnativehook_mouse_NativeMouseEvent != NULL) {
				// Populate our structure for later use.
				org_jnativehook_mouse_NativeMouseEvent->cls = (jclass) (*env)->NewGlobalRef(env, NativeMouseEvent_class);
				org_jnativehook_mouse_NativeMouseEvent->parent = org_jnativehook_NativeInputEvent;
				org_jnativehook_mouse_NativeMouseEvent->init = init;
				org_jnativehook_mouse_NativeMouseEvent->getButton = getButton;
				org_jnativehook_mouse_NativeMouseEvent->getClickCount = getClickCount;
				org_jnativehook_mouse_NativeMouseEvent->getX = getX;
				org_jnativehook_mouse_NativeMouseEvent->getY = getY;

				status = JNI_OK;
			}
			else {
				jni_ThrowException(env, "java/lang/OutOfMemoryError", "Failed to allocate native memory.");
				status = JNI_ENOMEM;
			}
		}
	}

	return status;
}
Ejemplo n.º 8
0
static inline int create_Logger(JNIEnv *env) {
	int status = JNI_ERR;

	// Class for the Logger object.
	jclass Logger_class = (*env)->FindClass(env, "java/util/logging/Logger");
	if (Logger_class != NULL) {
		// Get the method ID for Logger.getLogger().
		jmethodID getLogger = (*env)->GetStaticMethodID(env, Logger_class, "getLogger", "(Ljava/lang/String;)Ljava/util/logging/Logger;");

		// Get the method ID for Logger.fine().
		jmethodID fine = (*env)->GetMethodID(env, Logger_class, "fine", "(Ljava/lang/String;)V");

		// Get the method ID for Logger.info().
		jmethodID info = (*env)->GetMethodID(env, Logger_class, "info", "(Ljava/lang/String;)V");

		// Get the method ID for Logger.warning().
		jmethodID warning = (*env)->GetMethodID(env, Logger_class, "warning", "(Ljava/lang/String;)V");

		// Get the method ID for Logger.severe().
		jmethodID severe = (*env)->GetMethodID(env, Logger_class, "severe", "(Ljava/lang/String;)V");

		if ((*env)->ExceptionCheck(env) == JNI_FALSE) {
			java_util_logging_Logger = malloc(sizeof(Logger));
			if (java_util_logging_Logger != NULL) {
				// Populate our structure for later use.
				java_util_logging_Logger->cls = (jclass) (*env)->NewGlobalRef(env, Logger_class);
				java_util_logging_Logger->getLogger = getLogger;
				java_util_logging_Logger->fine = fine;
				java_util_logging_Logger->info = info;
				java_util_logging_Logger->warning = warning;
				java_util_logging_Logger->severe = severe;

				status = JNI_OK;
			}
			else {
				jni_ThrowException(env, "java/lang/OutOfMemoryError", "Failed to allocate native memory.");
				status = JNI_ENOMEM;
			}
		}
	}

	return status;
}
Ejemplo n.º 9
0
static int create_NativeInputEvent(JNIEnv *env) {
	int status = JNI_ERR;

	// Class and Constructor for the NativeInputEvent Object.
	jclass NativeInputEvent_class = (*env)->FindClass(env, "org/jnativehook/NativeInputEvent");
	if (NativeInputEvent_class != NULL) {
		// Get the field ID for NativeInputEvent.when.
		jfieldID when = (*env)->GetFieldID(env, NativeInputEvent_class, "when", "J");

		// Get the field ID for NativeInputEvent.reserved.
		jfieldID reserved = (*env)->GetFieldID(env, NativeInputEvent_class, "reserved", "S");

		// Get the method ID for NativeInputEvent constructor.
		jmethodID init = (*env)->GetMethodID(env, NativeInputEvent_class, "<init>", "(Ljava/lang/Class;II)V");

		// Get the method ID for NativeInputEvent.getID().
		jmethodID getID = (*env)->GetMethodID(env, NativeInputEvent_class, "getID", "()I");

		// Get the method ID for NativeInputEvent.getModifiers().
		jmethodID getModifiers = (*env)->GetMethodID(env, NativeInputEvent_class, "getModifiers", "()I");

		if ((*env)->ExceptionCheck(env) == JNI_FALSE) {
			org_jnativehook_NativeInputEvent = malloc(sizeof(NativeInputEvent));
			if (org_jnativehook_NativeInputEvent != NULL) {
				// Populate our structure for later use.
				org_jnativehook_NativeInputEvent->cls = (jclass) (*env)->NewGlobalRef(env, NativeInputEvent_class);
				org_jnativehook_NativeInputEvent->when = when;
				org_jnativehook_NativeInputEvent->reserved = reserved;
				org_jnativehook_NativeInputEvent->init = init;
				org_jnativehook_NativeInputEvent->getID = getID;
				org_jnativehook_NativeInputEvent->getModifiers = getModifiers;

				status = JNI_OK;
			}
			else {
				jni_ThrowException(env, "java/lang/OutOfMemoryError", "Failed to allocate native memory.");
				status = JNI_ENOMEM;
			}
		}
	}

	return status;
}
Ejemplo n.º 10
0
// NOTE: This function executes on the hook thread!  If you need to block
// please do so on another thread via your own event dispatcher.
void jni_EventDispatcher(uiohook_event * const event) {
	jobject NativeInputEvent_object = NULL;

	switch (event->type) {
		/* The following start and stop functions are less than ideal for attaching JNI.
		 * TODO Consider moving threads out of the lib and into Java.
		 */
		case EVENT_THREAD_STARTED:
			if ((*jvm)->GetEnv(jvm, (void **)(&env), jvm_attach_args.version) == JNI_EDETACHED) {
				(*jvm)->AttachCurrentThread(jvm, (void **)(&env), &jvm_attach_args);
			}
			break;

		case EVENT_THREAD_STOPPED:
			// NOTE This callback may note be called from Windows under some circumstances.
			if ((*jvm)->GetEnv(jvm, (void **)(&env), jvm_attach_args.version) == JNI_OK) {
				if ((*jvm)->DetachCurrentThread(jvm) == JNI_OK) {
					env = NULL;
				}
			}
			break;

		case EVENT_HOOK_ENABLED:
		case EVENT_HOOK_DISABLED:
			break;

		case EVENT_KEY_PRESSED:
			if (env != NULL) {
				jint location;
				if (jni_ConvertToJavaLocation(event->data.keyboard.keycode, &location) == JNI_OK) {
					NativeInputEvent_object = (*env)->NewObject(
							env,
							org_jnativehook_keyboard_NativeKeyEvent->cls,
							org_jnativehook_keyboard_NativeKeyEvent->init,
							org_jnativehook_keyboard_NativeKeyEvent_NATIVE_KEY_PRESSED,
							(jlong)	event->time,
							(jint)	event->mask,
							(jint)	event->data.keyboard.rawcode,
							(jint)	event->data.keyboard.keycode,
							(jchar)	org_jnativehook_keyboard_NativeKeyEvent_CHAR_UNDEFINED,
							location);
				}
			}
			break;

		case EVENT_KEY_RELEASED:
			if (env != NULL) {
				jint location;
				if (jni_ConvertToJavaLocation(event->data.keyboard.keycode, &location) == JNI_OK) {
					NativeInputEvent_object = (*env)->NewObject(
							env,
							org_jnativehook_keyboard_NativeKeyEvent->cls,
							org_jnativehook_keyboard_NativeKeyEvent->init,
							org_jnativehook_keyboard_NativeKeyEvent_NATIVE_KEY_RELEASED,
							(jlong)	event->time,
							(jint)	event->mask,
							(jint)	event->data.keyboard.rawcode,
							(jint)	event->data.keyboard.keycode,
							(jchar)	org_jnativehook_keyboard_NativeKeyEvent_CHAR_UNDEFINED,
							location);
				}
			}
			break;

		case EVENT_KEY_TYPED:
			if (env != NULL) {
				jint location;
				if (jni_ConvertToJavaLocation(event->data.keyboard.keycode, &location) == JNI_OK) {
					NativeInputEvent_object = (*env)->NewObject(
							env,
							org_jnativehook_keyboard_NativeKeyEvent->cls,
							org_jnativehook_keyboard_NativeKeyEvent->init,
							org_jnativehook_keyboard_NativeKeyEvent_NATIVE_KEY_TYPED,
							(jlong)	event->time,
							(jint)	event->mask,
							(jint)	event->data.keyboard.rawcode,
							(jint)	org_jnativehook_keyboard_NativeKeyEvent_VK_UNDEFINED,
							(jchar)	event->data.keyboard.keychar,
							location);
				}
			}
			break;

		case EVENT_MOUSE_PRESSED:
			if (env != NULL) {
				NativeInputEvent_object = (*env)->NewObject(
							env,
							org_jnativehook_mouse_NativeMouseEvent->cls,
							org_jnativehook_mouse_NativeMouseEvent->init,
							org_jnativehook_mouse_NativeMouseEvent_NATIVE_MOUSE_PRESSED,
							(jlong)	event->time,
							(jint)	event->mask,
							(jint)	event->data.mouse.x,
							(jint)	event->data.mouse.y,
							(jint)	event->data.mouse.clicks,
							(jint)	event->data.mouse.button);
			}
			break;

		case EVENT_MOUSE_RELEASED:
			if (env != NULL) {
				NativeInputEvent_object = (*env)->NewObject(
							env,
							org_jnativehook_mouse_NativeMouseEvent->cls,
							org_jnativehook_mouse_NativeMouseEvent->init,
							org_jnativehook_mouse_NativeMouseEvent_NATIVE_MOUSE_RELEASED,
							(jlong)	event->time,
							(jint)	event->mask,
							(jint)	event->data.mouse.x,
							(jint)	event->data.mouse.y,
							(jint)	event->data.mouse.clicks,
							(jint)	event->data.mouse.button);
			}
			break;

		case EVENT_MOUSE_CLICKED:
			if (env != NULL) {
				NativeInputEvent_object = (*env)->NewObject(
							env,
							org_jnativehook_mouse_NativeMouseEvent->cls,
							org_jnativehook_mouse_NativeMouseEvent->init,
							org_jnativehook_mouse_NativeMouseEvent_NATIVE_MOUSE_CLICKED,
							(jlong)	event->time,
							(jint)	event->mask,
							(jint)	event->data.mouse.x,
							(jint)	event->data.mouse.y,
							(jint)	event->data.mouse.clicks,
							(jint)	event->data.mouse.button);
			}
			break;

		case EVENT_MOUSE_MOVED:
			if (env != NULL) {
				NativeInputEvent_object = (*env)->NewObject(
							env,
							org_jnativehook_mouse_NativeMouseEvent->cls,
							org_jnativehook_mouse_NativeMouseEvent->init,
							org_jnativehook_mouse_NativeMouseEvent_NATIVE_MOUSE_MOVED,
							(jlong)	event->time,
							(jint)	event->mask,
							(jint)	event->data.mouse.x,
							(jint)	event->data.mouse.y,
							(jint)	event->data.mouse.clicks,
							(jint)	event->data.mouse.button);
			}
			break;

		case EVENT_MOUSE_DRAGGED:
			if (env != NULL) {
				NativeInputEvent_object = (*env)->NewObject(
							env,
							org_jnativehook_mouse_NativeMouseEvent->cls,
							org_jnativehook_mouse_NativeMouseEvent->init,
							org_jnativehook_mouse_NativeMouseEvent_NATIVE_MOUSE_DRAGGED,
							(jlong)	event->time,
							(jint)	event->mask,
							(jint)	event->data.mouse.x,
							(jint)	event->data.mouse.y,
							(jint)	event->data.mouse.clicks,
							(jint)	event->data.mouse.button);
			}
			break;

		case EVENT_MOUSE_WHEEL:
			if (env != NULL) {
				NativeInputEvent_object = (*env)->NewObject(
							env,
							org_jnativehook_mouse_NativeMouseWheelEvent->cls,
							org_jnativehook_mouse_NativeMouseWheelEvent->init,
							org_jnativehook_mouse_NativeMouseEvent_NATIVE_MOUSE_WHEEL,
							(jlong)	event->time,
							(jint)	event->mask,
							(jint)	event->data.wheel.x,
							(jint)	event->data.wheel.y,
							(jint)	event->data.wheel.clicks,
							(jint)	event->data.wheel.type,
							(jint)	event->data.wheel.amount,
							(jint)	event->data.wheel.rotation);
			}
			break;
	}

	// NOTE It is assumed that we do not need to check env* for null because NativeInputEvent_object
	// cannot be assigned if env* is NULL.
	if (NativeInputEvent_object != NULL) {
		// Create the global screen references up front to save time in the callback.
		jobject GlobalScreen_object = (*env)->CallStaticObjectMethod(
				env,
				org_jnativehook_GlobalScreen->cls,
				org_jnativehook_GlobalScreen->getInstance);

		if (GlobalScreen_object != NULL) {
			(*env)->CallVoidMethod(
					env,
					GlobalScreen_object,
					org_jnativehook_GlobalScreen->dispatchEvent,
					NativeInputEvent_object);

			// Set the propagate flag from java.
			event->reserved = (unsigned short) (*env)->GetShortField(
					env,
					NativeInputEvent_object,
					org_jnativehook_NativeInputEvent->reserved);

			(*env)->DeleteLocalRef(env, GlobalScreen_object);
		}
		else {
			jni_Logger(env, LOG_LEVEL_ERROR,	"%s [%u]: Failed to acquire GlobalScreen singleton!\n",
					__FUNCTION__, __LINE__);

			jni_ThrowException(env, "java/lang/NullPointerException", "GlobalScreen singleton is null.");
		}

		(*env)->DeleteLocalRef(env, NativeInputEvent_object);
	}
}