lwjgl/src/native/linux/org_lwjgl_input_Mouse.cpp

343 lines
9.0 KiB
C++
Raw Normal View History

2002-11-15 05:40:55 -05:00
/*
* Copyright (c) 2002 Light Weight Java Game Library Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'Light Weight Java Game Library' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* $Id$
*
2002-11-15 06:10:32 -05:00
* Linux mouse handling.
2002-11-15 05:40:55 -05:00
*
2002-11-15 06:10:32 -05:00
* @author elias_naur <elias_naur@users.sourceforge.net>
2002-11-15 05:40:55 -05:00
* @version $Revision$
*/
#include <X11/X.h>
#include <X11/Xlib.h>
2002-11-20 08:54:58 -05:00
#include <X11/extensions/xf86vmode.h>
2002-11-15 05:40:55 -05:00
#include <assert.h>
#include <string.h>
#include "org_lwjgl_input_Mouse.h"
2002-11-17 11:49:16 -05:00
#define NUM_BUTTONS 3
2002-11-15 05:40:55 -05:00
#define POINTER_WARP_BORDER 10
#define WARP_RETRY 5
2002-11-15 05:40:55 -05:00
extern Display *disp;
extern Window win;
2002-11-20 08:54:58 -05:00
extern int screen;
extern int current_fullscreen;
extern int win_width;
extern int win_height;
2002-11-21 10:05:51 -05:00
bool pointer_grabbed;
2002-11-15 05:40:55 -05:00
jfieldID fid_button;
jfieldID fid_dx;
jfieldID fid_dy;
jfieldID fid_dz;
int last_x;
int last_y;
2002-11-17 11:49:16 -05:00
int last_z;
2002-11-15 05:40:55 -05:00
int current_x;
int current_y;
2002-11-17 11:49:16 -05:00
int current_z;
2002-11-15 05:40:55 -05:00
unsigned char buttons[NUM_BUTTONS];
2002-11-17 11:14:53 -05:00
Cursor blank_cursor;
extern int isFocused(void);
2002-11-15 05:40:55 -05:00
/*
* Class: org_lwjgl_input_Mouse
* Method: initIDs
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_initIDs
(JNIEnv * env, jclass clazz)
{
// Get a global class instance, just to be sure
static jobject globalClassLock = NULL;
if (globalClassLock == NULL) {
2002-11-19 03:50:57 -05:00
globalClassLock = env->NewGlobalRef(clazz);
2002-11-15 05:40:55 -05:00
}
// Now cache the field IDs:
if (fid_button == NULL) {
2002-11-19 03:50:57 -05:00
fid_button = env->GetStaticFieldID(clazz, "button", "[Z");
2002-11-15 05:40:55 -05:00
}
if (fid_dx == NULL) {
2002-11-19 03:50:57 -05:00
fid_dx = env->GetStaticFieldID(clazz, "dx", "I");
2002-11-15 05:40:55 -05:00
}
if (fid_dy == NULL) {
2002-11-19 03:50:57 -05:00
fid_dy = env->GetStaticFieldID(clazz, "dy", "I");
2002-11-15 05:40:55 -05:00
}
if (fid_dz == NULL) {
2002-11-19 03:50:57 -05:00
fid_dz = env->GetStaticFieldID(clazz, "dz", "I");
2002-11-15 05:40:55 -05:00
}
}
2002-11-17 11:14:53 -05:00
int blankCursor(void) {
2002-11-19 03:50:57 -05:00
unsigned int best_width, best_height;
2002-11-17 11:14:53 -05:00
if (XQueryBestCursor(disp, win, 1, 1, &best_width, &best_height) == 0) {
#ifdef _DEBUG
printf("Could not query best cursor size\n");
#endif
return 0;
}
Pixmap mask = XCreatePixmap(disp, win, best_width, best_height, 1);
XGCValues gc_values;
gc_values.foreground = 0;
GC gc = XCreateGC(disp, mask, GCForeground, &gc_values);
XFillRectangle(disp, mask, gc, 0, 0, best_width, best_height);
2002-11-17 11:49:16 -05:00
XFreeGC(disp, gc);
2002-11-17 11:14:53 -05:00
XColor dummy_color;
blank_cursor = XCreatePixmapCursor(disp, mask, mask, &dummy_color, &dummy_color, 0, 0);
XFreePixmap(disp, mask);
return 1;
}
int grabPointer(void) {
int result;
int mask = EnterWindowMask | LeaveWindowMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask;
2002-11-20 08:54:58 -05:00
if (current_fullscreen) {
XWarpPointer(disp, None, win, 0, 0, 0, 0, 0, 0);
result = XGrabPointer(disp, win, False, mask, GrabModeAsync, GrabModeAsync, win, blank_cursor, CurrentTime);
2002-11-20 08:54:58 -05:00
XF86VidModeSetViewPort(disp, screen, 0, 0); // make sure we have a centered window
} else
result = XGrabPointer(disp, win, False, mask, GrabModeAsync, GrabModeAsync, None, blank_cursor, CurrentTime);
if (result == GrabSuccess)
2002-11-21 10:05:51 -05:00
pointer_grabbed = true;
return result;
}
void ungrabPointer(void) {
2002-11-21 10:05:51 -05:00
pointer_grabbed = false;
XUngrabPointer(disp, CurrentTime);
}
int updatePointerGrab(void) {
if (current_fullscreen) {
if (!pointer_grabbed)
return grabPointer();
} else {
if (isFocused()) {
if (!pointer_grabbed)
return grabPointer();
} else {
if (pointer_grabbed)
ungrabPointer();
}
}
return GrabSuccess;
}
2002-11-15 05:40:55 -05:00
/*
* Class: org_lwjgl_input_Mouse
* Method: nCreate
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Mouse_nCreate
(JNIEnv * env, jclass clazz)
{
int i;
current_x = current_y = current_z = last_x = last_y = last_z = pointer_grabbed = 0;
2002-11-15 05:40:55 -05:00
for (i = 0; i < NUM_BUTTONS; i++)
buttons[i] = 0;
2002-11-17 11:14:53 -05:00
if (!blankCursor()) {
#ifdef _DEBUG
printf("Could create blank cursor\n");
2002-11-17 11:14:53 -05:00
#endif
return JNI_FALSE;
}
if (updatePointerGrab() != GrabSuccess) {
2002-11-15 05:40:55 -05:00
#ifdef _DEBUG
printf("Could not grab pointer\n");
2002-11-15 05:40:55 -05:00
#endif
return JNI_FALSE;
}
return JNI_TRUE;
}
2002-11-17 11:49:16 -05:00
/*
* Class: org_lwjgl_input_Mouse
* Method: nGetNumButtons
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetNumButtons(JNIEnv *env, jclass clazz) {
return (jint)NUM_BUTTONS;
}
/*
* Class: org_lwjgl_input_Mouse
* Method: nHasZValue
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Mouse_nHasZValue(JNIEnv *env, jclass clazz) {
return JNI_TRUE;
}
2002-11-15 05:40:55 -05:00
/*
* Class: org_lwjgl_input_Mouse
* Method: nDestroy
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nDestroy
(JNIEnv * env, jclass clazz)
{
if (pointer_grabbed)
ungrabPointer();
2002-11-17 11:14:53 -05:00
XFreeCursor(disp, blank_cursor);
2002-11-15 05:40:55 -05:00
}
int checkPointer() {
XEvent event;
int count = 0;
updatePointerGrab();
2002-11-15 05:40:55 -05:00
while (XCheckMaskEvent(disp, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &event)) {
count++;
switch (event.type) {
case ButtonPress:
2002-11-17 11:49:16 -05:00
switch (event.xbutton.button) {
case Button1:
buttons[0] = 1;
break;
case Button2:
buttons[1] = 1;
break;
case Button3:
buttons[2] = 1;
break;
case Button4:
current_z--;
break;
case Button5:
current_z++;
break;
default: assert(0);
}
2002-11-15 05:40:55 -05:00
break;
case ButtonRelease:
2002-11-17 11:49:16 -05:00
switch (event.xbutton.button) {
case Button1:
buttons[0] = 0;
break;
case Button2:
buttons[1] = 0;
break;
case Button3:
buttons[2] = 0;
break;
case Button4: /* Fall through */
case Button5:
break;
default: assert(0);
}
2002-11-15 05:40:55 -05:00
break;
case MotionNotify:
current_x = event.xmotion.x;
current_y = event.xmotion.y;
2002-11-15 05:40:55 -05:00
break;
default: assert(0);
}
}
return count;
}
void warpPointer(void) {
int i;
// Reset pointer to middle of screen if inside a certain inner border
if (current_x < POINTER_WARP_BORDER || current_y < POINTER_WARP_BORDER ||
current_x > win_width - POINTER_WARP_BORDER || current_y > win_height - POINTER_WARP_BORDER) {
current_x = last_x = win_width/2;
current_y = last_y = win_height/2;
XWarpPointer(disp, None, win, 0, 0, 0, 0, current_x, current_y);
XEvent event;
// Try to catch the warp pointer event
for (i = 0; i < WARP_RETRY; i++) {
XMaskEvent(disp, PointerMotionMask, &event);
if (event.xmotion.x > current_x - POINTER_WARP_BORDER &&
event.xmotion.x < current_x + POINTER_WARP_BORDER &&
event.xmotion.y > current_y - POINTER_WARP_BORDER &&
event.xmotion.y < current_y + POINTER_WARP_BORDER)
break;
#ifdef _DEBUG
printf("Skipped event searching for warp event %d, %d\n", event.xmotion.x, event.xmotion.y);
#endif
}
#ifdef _DEBUG
if (i == WARP_RETRY)
printf("Never got warp event\n");
#endif
}
}
2002-11-15 05:40:55 -05:00
/*
* Class: org_lwjgl_input_Mouse
* Method: nPoll
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nPoll
(JNIEnv * env, jclass clazz)
{
checkPointer();
int moved_x = current_x - last_x;
int moved_y = current_y - last_y;
2002-11-17 11:49:16 -05:00
int moved_z = current_z - last_z;
2002-11-19 03:50:57 -05:00
env->SetStaticIntField(clazz, fid_dx, (jint)moved_x);
env->SetStaticIntField(clazz, fid_dy, (jint)moved_y);
env->SetStaticIntField(clazz, fid_dz, (jint)moved_z);
2002-11-15 05:40:55 -05:00
last_x = current_x;
last_y = current_y;
2002-11-17 11:49:16 -05:00
last_z = current_z;
2002-11-19 03:50:57 -05:00
jbooleanArray buttonsArray = (jbooleanArray) env->GetStaticObjectField(clazz, fid_button);
unsigned char * class_buttons = (unsigned char *) env->GetPrimitiveArrayCritical(buttonsArray, NULL);
2002-11-15 05:40:55 -05:00
memcpy(class_buttons, buttons, NUM_BUTTONS*sizeof(unsigned char));
2002-11-19 03:50:57 -05:00
env->ReleasePrimitiveArrayCritical(buttonsArray, class_buttons, 0);
if (current_fullscreen)
warpPointer();
2002-11-15 05:40:55 -05:00
}
/*
* Class: org_lwjgl_input_Mouse
* Method: nEnableBuffer
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nEnableBuffer
(JNIEnv * env, jclass clazz) {
printf("*** FIXME: nEnableBuffer not implemented!\n*");
}