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 keyboard 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>
|
2003-01-11 16:03:22 -05:00
|
|
|
#include <X11/Xutil.h>
|
2002-11-15 05:40:55 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include "org_lwjgl_input_Keyboard.h"
|
|
|
|
|
|
|
|
#define KEYBOARD_BUFFER_SIZE 50
|
|
|
|
#define KEYBOARD_SIZE 256
|
2003-02-09 12:01:01 -05:00
|
|
|
#define KEY_EVENT_BACKLOG 40
|
2002-11-26 15:07:06 -05:00
|
|
|
|
2003-02-08 14:55:07 -05:00
|
|
|
static unsigned char readBuffer[KEYBOARD_BUFFER_SIZE * 2];
|
|
|
|
static jfieldID fid_readBuffer;
|
|
|
|
static unsigned char key_buf[KEYBOARD_SIZE];
|
|
|
|
static unsigned char key_map[KEYBOARD_SIZE];
|
2002-11-15 05:40:55 -05:00
|
|
|
|
2003-02-08 14:55:07 -05:00
|
|
|
static XKeyEvent saved_key_events[KEY_EVENT_BACKLOG];
|
|
|
|
static int list_start = 0;
|
|
|
|
static int list_end = 0;
|
2002-11-26 15:07:06 -05:00
|
|
|
|
2003-02-08 14:55:07 -05:00
|
|
|
static bool keyboard_grabbed;
|
|
|
|
static bool buffer_enabled;
|
|
|
|
static bool translation_enabled;
|
2002-11-20 05:33:37 -05:00
|
|
|
|
2002-11-15 05:40:55 -05:00
|
|
|
extern Display *disp;
|
|
|
|
extern Window win;
|
2002-11-20 05:33:37 -05:00
|
|
|
|
2003-02-08 14:55:07 -05:00
|
|
|
extern bool isFullscreen(void);
|
|
|
|
|
|
|
|
extern bool isFocused(void);
|
2003-02-09 12:01:01 -05:00
|
|
|
extern void handleMessages(void);
|
2002-11-15 05:40:55 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_lwjgl_input_Keyboard
|
|
|
|
* Method: initIDs
|
|
|
|
* Signature: ()V
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_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
|
|
|
}
|
|
|
|
|
2002-11-19 03:50:57 -05:00
|
|
|
fid_readBuffer = env->GetStaticFieldID(clazz, "readBuffer", "Ljava/nio/ByteBuffer;");
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
|
|
|
|
2002-11-20 05:33:37 -05:00
|
|
|
int grabKeyboard(void) {
|
|
|
|
int result = XGrabKeyboard(disp, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
|
|
|
|
if (result == GrabSuccess)
|
2002-11-21 10:05:51 -05:00
|
|
|
keyboard_grabbed = true;
|
2002-11-20 05:33:37 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ungrabKeyboard(void) {
|
2002-11-21 10:05:51 -05:00
|
|
|
keyboard_grabbed = false;
|
2002-11-20 05:33:37 -05:00
|
|
|
XUngrabKeyboard(disp, CurrentTime);
|
|
|
|
}
|
|
|
|
|
2003-02-09 12:01:01 -05:00
|
|
|
int updateKeyboardGrab() {
|
2003-02-08 14:55:07 -05:00
|
|
|
if (isFullscreen()) {
|
2002-11-20 05:33:37 -05:00
|
|
|
if (!keyboard_grabbed)
|
|
|
|
return grabKeyboard();
|
|
|
|
} else {
|
|
|
|
if (isFocused()) {
|
|
|
|
if (!keyboard_grabbed)
|
|
|
|
return grabKeyboard();
|
|
|
|
} else {
|
|
|
|
if (keyboard_grabbed)
|
|
|
|
ungrabKeyboard();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return GrabSuccess;
|
|
|
|
}
|
|
|
|
|
2002-11-15 05:40:55 -05:00
|
|
|
/*
|
|
|
|
* Class: org_lwjgl_input_Keyboard
|
|
|
|
* Method: nCreate
|
|
|
|
* Signature: ()Z
|
|
|
|
*/
|
|
|
|
JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Keyboard_nCreate
|
|
|
|
(JNIEnv * env, jclass clazz)
|
|
|
|
{
|
2003-01-11 16:03:22 -05:00
|
|
|
keyboard_grabbed = false;
|
|
|
|
translation_enabled = false;
|
|
|
|
buffer_enabled = false;
|
|
|
|
|
2002-11-20 05:33:37 -05:00
|
|
|
if (updateKeyboardGrab() != GrabSuccess) {
|
2002-11-15 05:40:55 -05:00
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Could not grab keyboard\n");
|
|
|
|
#endif
|
|
|
|
return JNI_FALSE;
|
|
|
|
}
|
2002-11-25 09:57:13 -05:00
|
|
|
for (int i = 0; i < KEYBOARD_SIZE; i++)
|
|
|
|
key_map[i] = i;
|
|
|
|
key_map[0x6b] = 0xdb; // Left doze key
|
|
|
|
key_map[0x6c] = 0xdc; // Right doze key
|
|
|
|
key_map[0x6d] = 0xdd; // Apps key
|
|
|
|
key_map[0x5a] = 0xc8; // Up arrow
|
|
|
|
key_map[0x5c] = 0xcb; // Left arrow
|
|
|
|
key_map[0x5e] = 0xcd; // Right arrow
|
|
|
|
key_map[0x60] = 0xd0; // Down arrow
|
|
|
|
key_map[0x59] = 0xc7; // Home
|
|
|
|
key_map[0x62] = 0xd2; // Insert
|
|
|
|
key_map[0x63] = 0xd3; // Delete
|
|
|
|
key_map[0x5f] = 0xcf; // End
|
|
|
|
key_map[0x5b] = 0xc9; // Page up
|
|
|
|
key_map[0x61] = 0xd1; // Page down
|
|
|
|
key_map[0x67] = 0xb7; // SysRQ
|
|
|
|
key_map[0x66] = 0xc5; // Pause
|
|
|
|
key_map[0x64] = 0x9c; // Numpad enter
|
2003-02-02 17:49:16 -05:00
|
|
|
key_map[0x65] = 0x9d; // Right control
|
2002-11-25 09:57:13 -05:00
|
|
|
key_map[0x68] = 0xb5; // Numpad divide
|
|
|
|
|
2002-11-15 05:40:55 -05:00
|
|
|
memset(key_buf, 0, KEYBOARD_SIZE*sizeof(unsigned char));
|
|
|
|
return JNI_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_lwjgl_input_Keyboard
|
|
|
|
* Method: nDestroy
|
|
|
|
* Signature: ()V
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_nDestroy
|
|
|
|
(JNIEnv * env, jclass clazz)
|
|
|
|
{
|
2002-11-21 10:05:51 -05:00
|
|
|
if (keyboard_grabbed)
|
|
|
|
ungrabKeyboard();
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
|
|
|
|
2003-01-11 16:03:22 -05:00
|
|
|
XKeyEvent *nextEventElement(void) {
|
2002-11-26 15:07:06 -05:00
|
|
|
if (list_start == list_end)
|
|
|
|
return NULL;
|
2003-01-11 16:03:22 -05:00
|
|
|
XKeyEvent *result = &(saved_key_events[list_start]);
|
2002-11-26 15:07:06 -05:00
|
|
|
list_start = (list_start + 1)%KEY_EVENT_BACKLOG;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-01-11 16:03:22 -05:00
|
|
|
void putEventElement(XKeyEvent *event) {
|
2002-11-26 15:07:06 -05:00
|
|
|
int next_index = (list_end + 1)%KEY_EVENT_BACKLOG;
|
|
|
|
if (next_index == list_start)
|
|
|
|
return;
|
2003-01-11 16:03:22 -05:00
|
|
|
saved_key_events[list_end] = *event;
|
2002-11-26 15:07:06 -05:00
|
|
|
list_end = next_index;
|
|
|
|
}
|
|
|
|
|
2003-01-11 16:03:22 -05:00
|
|
|
unsigned char getKeycode(XKeyEvent *event) {
|
|
|
|
unsigned char keycode = (unsigned char)((event->keycode - 8) & 0xff);
|
2002-11-26 15:07:06 -05:00
|
|
|
keycode = key_map[keycode];
|
|
|
|
return keycode;
|
|
|
|
}
|
|
|
|
|
2003-01-11 16:03:22 -05:00
|
|
|
int translateEvent(int *position, XKeyEvent *event) {
|
|
|
|
static char temp_translation_buffer[KEYBOARD_BUFFER_SIZE];
|
|
|
|
static XComposeStatus status;
|
|
|
|
int num_chars, i;
|
|
|
|
|
|
|
|
if (*position >= KEYBOARD_BUFFER_SIZE * 2)
|
|
|
|
return 0;
|
|
|
|
if (event->type == KeyRelease) {
|
|
|
|
readBuffer[(*position)++] = 0;
|
|
|
|
readBuffer[(*position)++] = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
num_chars = XLookupString(event, temp_translation_buffer, KEYBOARD_BUFFER_SIZE, NULL, &status);
|
|
|
|
if (num_chars > 0) {
|
|
|
|
num_chars--;
|
|
|
|
/* Assume little endian byte order */
|
|
|
|
readBuffer[(*position)++] = temp_translation_buffer[0];
|
|
|
|
readBuffer[(*position)++] = 0;
|
|
|
|
for (i = 0; i < num_chars; i++) {
|
|
|
|
readBuffer[(*position)++] = 0;
|
|
|
|
readBuffer[(*position)++] = 0;
|
|
|
|
readBuffer[(*position)++] = temp_translation_buffer[i + 1];
|
|
|
|
readBuffer[(*position)++] = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
readBuffer[(*position)++] = 0;
|
|
|
|
readBuffer[(*position)++] = 0;
|
|
|
|
}
|
|
|
|
return num_chars;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char eventState(XKeyEvent *event) {
|
|
|
|
if (event->type == KeyPress) {
|
|
|
|
return 1;
|
|
|
|
} else if (event->type == KeyRelease) {
|
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
2003-02-09 12:01:01 -05:00
|
|
|
void handleKeyEvent(XKeyEvent *event) {
|
|
|
|
unsigned char keycode = getKeycode(event);
|
|
|
|
unsigned char state = eventState(event);
|
|
|
|
key_buf[keycode] = state;
|
|
|
|
if (buffer_enabled)
|
|
|
|
putEventElement(event);
|
|
|
|
}
|
|
|
|
|
2002-11-26 15:07:06 -05:00
|
|
|
/*
|
|
|
|
* Class: org_lwjgl_input_Keyboard
|
|
|
|
* Method: nPoll
|
|
|
|
* Signature: (I)V
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_nPoll
|
|
|
|
(JNIEnv * env, jclass clazz, jint buf)
|
|
|
|
{
|
2002-11-15 05:40:55 -05:00
|
|
|
XEvent event;
|
2003-01-11 16:03:22 -05:00
|
|
|
unsigned char state;
|
2002-11-26 15:07:06 -05:00
|
|
|
|
2002-11-20 05:33:37 -05:00
|
|
|
updateKeyboardGrab();
|
2003-02-09 12:01:01 -05:00
|
|
|
handleMessages();
|
2002-11-26 15:07:06 -05:00
|
|
|
|
2002-11-15 05:40:55 -05:00
|
|
|
memcpy((unsigned char*)buf, key_buf, KEYBOARD_SIZE*sizeof(unsigned char));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_lwjgl_input_Keyboard
|
|
|
|
* Method: nRead
|
|
|
|
* Signature: (I)V
|
|
|
|
*/
|
2003-01-11 16:03:22 -05:00
|
|
|
JNIEXPORT int JNICALL Java_org_lwjgl_input_Keyboard_nRead
|
|
|
|
(JNIEnv * env, jclass clazz)
|
2002-11-15 05:40:55 -05:00
|
|
|
{
|
2002-11-26 15:07:06 -05:00
|
|
|
XEvent event;
|
2003-01-11 16:03:22 -05:00
|
|
|
XKeyEvent *key_event;
|
2002-11-26 15:07:06 -05:00
|
|
|
int buf_count = 0;
|
|
|
|
int state;
|
2003-01-11 16:03:22 -05:00
|
|
|
int num_events = 0;
|
2002-11-26 15:07:06 -05:00
|
|
|
|
|
|
|
updateKeyboardGrab();
|
2003-02-09 12:01:01 -05:00
|
|
|
handleMessages();
|
2002-11-26 15:07:06 -05:00
|
|
|
|
2003-01-11 16:03:22 -05:00
|
|
|
while (buf_count < KEYBOARD_BUFFER_SIZE * 2 && (key_event = nextEventElement()) != NULL) {
|
|
|
|
num_events++;
|
|
|
|
unsigned char keycode = getKeycode(key_event);
|
|
|
|
unsigned char state = eventState(key_event);
|
|
|
|
// printf("Reading a key %d %d count %d\n", (int)keycode, (int)state, num_events);
|
|
|
|
readBuffer[buf_count++] = keycode;
|
|
|
|
readBuffer[buf_count++] = state;
|
|
|
|
if (translation_enabled)
|
|
|
|
num_events += translateEvent(&buf_count, key_event);
|
2002-11-26 15:07:06 -05:00
|
|
|
}
|
|
|
|
|
2003-01-11 16:03:22 -05:00
|
|
|
return num_events;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_lwjgl_input_Keyboard
|
|
|
|
* Method: nEnableTranslation
|
|
|
|
* Signature: ()I
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_nEnableTranslation
|
|
|
|
(JNIEnv *env, jclass clazz)
|
|
|
|
{
|
|
|
|
translation_enabled = true;
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_lwjgl_input_Keyboard
|
|
|
|
* Method: nEnableBuffer
|
|
|
|
* Signature: ()I
|
|
|
|
*/
|
|
|
|
JNIEXPORT jint JNICALL Java_org_lwjgl_input_Keyboard_nEnableBuffer
|
|
|
|
(JNIEnv * env, jclass clazz)
|
|
|
|
{
|
2002-11-21 10:05:51 -05:00
|
|
|
jobject newBuffer = env->NewDirectByteBuffer(&readBuffer, KEYBOARD_BUFFER_SIZE * 2);
|
2002-11-19 03:50:57 -05:00
|
|
|
env->SetStaticObjectField(clazz, fid_readBuffer, newBuffer);
|
2003-01-11 16:03:22 -05:00
|
|
|
buffer_enabled = true;
|
2002-11-15 05:40:55 -05:00
|
|
|
return KEYBOARD_BUFFER_SIZE;
|
|
|
|
}
|