Update of /cvsroot/java-game-lib/LWJGL/src/native/linux
In directory sc8-pr-cvs1:/tmp/cvs-serv31636
Modified Files:
org_lwjgl_input_Keyboard.cpp
Log Message:
Added poll() logic so behavior looks like DX
Index: org_lwjgl_input_Keyboard.cpp
CVS Browser:
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/java-game-lib/LWJGL/src/native/linux/org_lwjgl_input_Keyboard.cpp
===================================================================
RCS file: /cvsroot/java-game-lib/LWJGL/src/native/linux/org_lwjgl_input_Keyboard.cpp,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- org_lwjgl_input_Keyboard.cpp 26 Nov 2002 08:15:17 -0000 1.7
+++ org_lwjgl_input_Keyboard.cpp 26 Nov 2002 20:07:06 -0000 1.8
@@ -47,12 +47,23 @@
#define KEYBOARD_BUFFER_SIZE 50
#define KEYBOARD_SIZE 256
+#define KEY_EVENT_BACKLOG 5
+
unsigned char readBuffer[KEYBOARD_BUFFER_SIZE * 2];
jfieldID fid_readBuffer;
jfieldID fid_readBufferAddress;
unsigned char key_buf[KEYBOARD_SIZE];
unsigned char key_map[KEYBOARD_SIZE];
+typedef struct {
+ unsigned char keycode;
+ unsigned char state;
+} input_event;
+
+input_event saved_key_events[KEY_EVENT_BACKLOG];
+int list_start = 0;
+int list_end = 0;
+
bool keyboard_grabbed;
extern Display *disp;
@@ -159,16 +170,44 @@
ungrabKeyboard();
}
-int checkKeyEvents(unsigned char *result_buf) {
+input_event *nextEventElement(void) {
+ if (list_start == list_end)
+ return NULL;
+ input_event *result = &(saved_key_events[list_start]);
+ list_start = (list_start + 1)%KEY_EVENT_BACKLOG;
+ return result;
+}
+
+void putEventElement(unsigned char keycode, unsigned char state) {
+ int next_index = (list_end + 1)%KEY_EVENT_BACKLOG;
+ if (next_index == list_start)
+ return;
+ saved_key_events[list_end].keycode = keycode;
+ saved_key_events[list_end].state = state;
+ list_end = next_index;
+}
+
+unsigned char getKeycode(XEvent *event) {
+ unsigned char keycode = (unsigned char)((event->xkey.keycode - 8) & 0xff);
+ keycode = key_map[keycode];
+ return keycode;
+}
+
+/*
+ * 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)
+{
XEvent event;
- int count = 0;
- int buf_count = 0;
int state;
+
updateKeyboardGrab();
+
while (XCheckMaskEvent(disp, KeyPressMask | KeyReleaseMask, &event)) {
- count++;
- unsigned char keycode = (unsigned char)((event.xkey.keycode - 8) & 0xff);
- keycode = key_map[keycode];
+ unsigned char keycode = getKeycode(&event);
if (event.type == KeyPress) {
state = 1;
} else if (event.type == KeyRelease) {
@@ -176,25 +215,8 @@
} else
assert(0);
key_buf[keycode] = state;
- if (result_buf != NULL) {
- result_buf[buf_count++] = keycode;
- result_buf[buf_count++] = state;
- if (buf_count >= KEYBOARD_BUFFER_SIZE * 2)
- break;
- }
+ putEventElement(keycode, state);
}
- return count;
-}
-
-/*
- * 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)
-{
- checkKeyEvents(NULL);
memcpy((unsigned char*)buf, key_buf, KEYBOARD_SIZE*sizeof(unsigned char));
}
@@ -206,7 +228,40 @@
JNIEXPORT jint JNICALL Java_org_lwjgl_input_Keyboard_nRead
(JNIEnv * env, jclass clazz, jint keys)
{
- return checkKeyEvents((unsigned char *)keys);
+ XEvent event;
+ int count = 0;
+ int buf_count = 0;
+ int state;
+ input_event *input_ev;
+ unsigned char *result_buf = (unsigned char *)keys;
+
+ updateKeyboardGrab();
+
+ while ((input_ev = nextEventElement()) != NULL) {
+ count++;
+// printf("Reading a key %d %d count %d\n", (int)input_ev->keycode, (int)input_ev->state, count);
+ result_buf[buf_count++] = input_ev->keycode;
+ result_buf[buf_count++] = input_ev->state;
+ if (buf_count >= KEYBOARD_BUFFER_SIZE * 2)
+ return count;
+ }
+
+ while (XCheckMaskEvent(disp, KeyPressMask | KeyReleaseMask, &event)) {
+ count++;
+ unsigned char keycode = getKeycode(&event);
+ if (event.type == KeyPress) {
+ state = 1;
+ } else if (event.type == KeyRelease) {
+ state = 0;
+ } else
+ assert(0);
+ key_buf[keycode] = state;
+ result_buf[buf_count++] = keycode;
+ result_buf[buf_count++] = state;
+ if (buf_count >= KEYBOARD_BUFFER_SIZE * 2)
+ return count;
+ }
+ return count;
}
/*
|