Switched mouse handling to Carbin events
This commit is contained in:
parent
7d5992c02e
commit
ada3a07b17
|
@ -47,5 +47,6 @@
|
||||||
extern void resetMode(JNIEnv *env);
|
extern void resetMode(JNIEnv *env);
|
||||||
extern bool switchMode(JNIEnv *env, long width, long height, long bpp, long freq);
|
extern bool switchMode(JNIEnv *env, long width, long height, long bpp, long freq);
|
||||||
extern void handleKeyboardEvent(EventRef event);
|
extern void handleKeyboardEvent(EventRef event);
|
||||||
|
extern void handleMouseEvent(EventRef event);
|
||||||
|
|
||||||
#endif /* _LWJGL_WINDOW_H_INCLUDED_ */
|
#endif /* _LWJGL_WINDOW_H_INCLUDED_ */
|
||||||
|
|
|
@ -79,23 +79,12 @@ static void init(JNIEnv *env) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void captureDisplay(void) {
|
|
||||||
if (!display_captured) {
|
|
||||||
display_captured = true;
|
|
||||||
CGDisplayCapture(kCGDirectMainDisplay);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void releaseDisplay(void) {
|
|
||||||
if (display_captured) {
|
|
||||||
display_captured = false;
|
|
||||||
CGDisplayRelease(kCGDirectMainDisplay);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool switchMode(JNIEnv *env, long width, long height, long bpp, long freq) {
|
bool switchMode(JNIEnv *env, long width, long height, long bpp, long freq) {
|
||||||
init(env);
|
init(env);
|
||||||
captureDisplay();
|
if (display_captured)
|
||||||
|
return false;
|
||||||
|
display_captured = true;
|
||||||
|
CGDisplayCapture(kCGDirectMainDisplay);
|
||||||
CFArrayRef modes = CGDisplayAvailableModes(kCGDirectMainDisplay);
|
CFArrayRef modes = CGDisplayAvailableModes(kCGDirectMainDisplay);
|
||||||
int size = CFArrayGetCount(modes);
|
int size = CFArrayGetCount(modes);
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
|
@ -121,9 +110,12 @@ bool switchMode(JNIEnv *env, long width, long height, long bpp, long freq) {
|
||||||
|
|
||||||
void resetMode(JNIEnv *env) {
|
void resetMode(JNIEnv *env) {
|
||||||
init(env);
|
init(env);
|
||||||
|
if (!display_captured)
|
||||||
|
return;
|
||||||
|
display_captured = false;
|
||||||
CGDisplayRestoreColorSyncSettings();
|
CGDisplayRestoreColorSyncSettings();
|
||||||
CGDisplaySwitchToMode(kCGDirectMainDisplay, original_mode);
|
CGDisplaySwitchToMode(kCGDirectMainDisplay, original_mode);
|
||||||
releaseDisplay();
|
CGDisplayRelease(kCGDirectMainDisplay);
|
||||||
saveOriginalMode(env);
|
saveOriginalMode(env);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,12 +56,11 @@ static jfieldID fid_dwheel;
|
||||||
static jfieldID fid_buttons;
|
static jfieldID fid_buttons;
|
||||||
|
|
||||||
static unsigned char button_states[NUM_BUTTONS];
|
static unsigned char button_states[NUM_BUTTONS];
|
||||||
static bool created;
|
|
||||||
static bool buffer_enabled;
|
static bool buffer_enabled;
|
||||||
static int x_axis_index = NUM_BUTTONS;
|
/*static int x_axis_index = NUM_BUTTONS;
|
||||||
static int y_axis_index = NUM_BUTTONS + 1;
|
static int y_axis_index = NUM_BUTTONS + 1;
|
||||||
static int z_axis_index = NUM_BUTTONS + 2;
|
static int z_axis_index = NUM_BUTTONS + 2;
|
||||||
static hid_device_t hid_dev;
|
static hid_device_t hid_dev;*/
|
||||||
static event_queue_t event_queue;
|
static event_queue_t event_queue;
|
||||||
|
|
||||||
static int last_dx;
|
static int last_dx;
|
||||||
|
@ -76,7 +75,7 @@ static void handleButton(unsigned char button_index, unsigned char state) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void pollMouseDevice() {
|
/*static void pollMouseDevice() {
|
||||||
hid_event_t event;
|
hid_event_t event;
|
||||||
cont:
|
cont:
|
||||||
while (nextDeviceEvent(&hid_dev, &event)) {
|
while (nextDeviceEvent(&hid_dev, &event)) {
|
||||||
|
@ -107,13 +106,71 @@ cont:
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
static void resetDeltas(void) {
|
static void resetDeltas(void) {
|
||||||
last_dx = 0;
|
last_dx = 0;
|
||||||
last_dy = 0;
|
last_dy = 0;
|
||||||
last_dz = 0;
|
last_dz = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void handleButtonEvent(EventRef event, unsigned char state) {
|
||||||
|
EventMouseButton button;
|
||||||
|
OSStatus err = GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
|
||||||
|
if (err != noErr) {
|
||||||
|
#ifdef _DEBUG
|
||||||
|
printf("Could not get button parameter from event\n");
|
||||||
|
#endif
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
handleButton(button, state);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void handleMovedEvent(EventRef event) {
|
||||||
|
HIPoint delta;
|
||||||
|
OSStatus err = GetEventParameter(event, kEventParamMouseDelta, typeHIPoint, NULL, sizeof(delta), NULL, &delta);
|
||||||
|
if (err != noErr) {
|
||||||
|
#ifdef _DEBUG
|
||||||
|
printf("Could not delta parameter from event\n");
|
||||||
|
#endif
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
last_dx += (int)delta.x;
|
||||||
|
last_dy += (int)delta.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void handleWheelEvent(EventRef event) {
|
||||||
|
long delta;
|
||||||
|
OSStatus err = GetEventParameter(event, kEventParamMouseWheelDelta, typeLongInteger, NULL, sizeof(delta), NULL, &delta);
|
||||||
|
if (err != noErr) {
|
||||||
|
#ifdef _DEBUG
|
||||||
|
printf("Could not delta parameter from event\n");
|
||||||
|
#endif
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
printf("wheel delta %d\n", (int)delta);
|
||||||
|
last_dz += (int)delta;
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleMouseEvent(EventRef event) {
|
||||||
|
UInt32 event_kind = GetEventKind(event);
|
||||||
|
switch (event_kind) {
|
||||||
|
case kEventMouseDown:
|
||||||
|
handleButtonEvent(event, 1);
|
||||||
|
break;
|
||||||
|
case kEventMouseUp:
|
||||||
|
handleButtonEvent(event, 0);
|
||||||
|
break;
|
||||||
|
case kEventMouseMoved:
|
||||||
|
handleMovedEvent(event);
|
||||||
|
break;
|
||||||
|
case kEventMouseWheelMoved:
|
||||||
|
handleWheelEvent(event);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Mouse_nHasWheel(JNIEnv *, jclass) {
|
JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Mouse_nHasWheel(JNIEnv *, jclass) {
|
||||||
return JNI_TRUE;
|
return JNI_TRUE;
|
||||||
}
|
}
|
||||||
|
@ -152,7 +209,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nCreate(JNIEnv * env, jclass c
|
||||||
button_states[i] = 0;
|
button_states[i] = 0;
|
||||||
}
|
}
|
||||||
initEventQueue(&event_queue);
|
initEventQueue(&event_queue);
|
||||||
hid_cookie_t hid_cookies[NUM_COOKIES];
|
/* hid_cookie_t hid_cookies[NUM_COOKIES];
|
||||||
for (int i = 0; i < NUM_BUTTONS; i++) {
|
for (int i = 0; i < NUM_BUTTONS; i++) {
|
||||||
hid_cookies[i].usage_page = kHIDPage_Button;
|
hid_cookies[i].usage_page = kHIDPage_Button;
|
||||||
hid_cookies[i].usage = i + 1;
|
hid_cookies[i].usage = i + 1;
|
||||||
|
@ -166,24 +223,22 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nCreate(JNIEnv * env, jclass c
|
||||||
if (!findDevice(&hid_dev, kHIDPage_GenericDesktop, kHIDUsage_GD_Mouse, NUM_COOKIES, hid_cookies, EVENT_BUFFER_SIZE)) {
|
if (!findDevice(&hid_dev, kHIDPage_GenericDesktop, kHIDUsage_GD_Mouse, NUM_COOKIES, hid_cookies, EVENT_BUFFER_SIZE)) {
|
||||||
throwException(env, "Could not find HID mouse device");
|
throwException(env, "Could not find HID mouse device");
|
||||||
return;
|
return;
|
||||||
}
|
}*/
|
||||||
CGAssociateMouseAndMouseCursorPosition(FALSE);
|
CGAssociateMouseAndMouseCursorPosition(FALSE);
|
||||||
CGDisplayHideCursor(CGMainDisplayID());
|
CGDisplayHideCursor(CGMainDisplayID());
|
||||||
created = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nDestroy(JNIEnv * env, jclass clazz) {
|
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nDestroy(JNIEnv * env, jclass clazz) {
|
||||||
shutdownDevice(&hid_dev);
|
//shutdownDevice(&hid_dev);
|
||||||
// if (!native_cursor) {
|
// if (!native_cursor) {
|
||||||
CGAssociateMouseAndMouseCursorPosition(TRUE);
|
|
||||||
CGDisplayShowCursor(CGMainDisplayID());
|
CGDisplayShowCursor(CGMainDisplayID());
|
||||||
|
CGAssociateMouseAndMouseCursorPosition(TRUE);
|
||||||
// }
|
// }
|
||||||
created = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nPoll(JNIEnv * env, jclass clazz) {
|
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nPoll(JNIEnv * env, jclass clazz) {
|
||||||
int dx, dy, dz;
|
int dx, dy, dz;
|
||||||
pollMouseDevice();
|
//pollMouseDevice();
|
||||||
dz = last_dz*WHEEL_SCALE;
|
dz = last_dz*WHEEL_SCALE;
|
||||||
//if (!native_cursor) {
|
//if (!native_cursor) {
|
||||||
dx = last_dx;
|
dx = last_dx;
|
||||||
|
@ -211,6 +266,5 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_input_Mouse_nEnableBuffer(JNIEnv *env,
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nRead(JNIEnv *env, jclass clazz) {
|
JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nRead(JNIEnv *env, jclass clazz) {
|
||||||
pollMouseDevice();
|
|
||||||
return copyEvents(&event_queue, 2);
|
return copyEvents(&event_queue, 2);
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,6 +94,7 @@ static bool createFullscreenContext(JNIEnv *env, jint bpp, jint alpha, jint dept
|
||||||
}
|
}
|
||||||
CGLSetFullScreen(context);
|
CGLSetFullScreen(context);
|
||||||
CGLSetCurrentContext(context);
|
CGLSetCurrentContext(context);
|
||||||
|
FlushEventQueue(GetMainEventQueue());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,8 +141,16 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_update(JNIEnv *env, jclass c
|
||||||
OSStatus err = ReceiveNextEvent(0, NULL, 0, true, &event);
|
OSStatus err = ReceiveNextEvent(0, NULL, 0, true, &event);
|
||||||
if (err == noErr) {
|
if (err == noErr) {
|
||||||
UInt32 event_class = GetEventClass(event);
|
UInt32 event_class = GetEventClass(event);
|
||||||
if (event_class == kEventClassKeyboard)
|
switch (event_class) {
|
||||||
handleKeyboardEvent(event);
|
case kEventClassKeyboard:
|
||||||
|
handleKeyboardEvent(event);
|
||||||
|
break;
|
||||||
|
case kEventClassMouse:
|
||||||
|
handleMouseEvent(event);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue