2006-06-26 15:50:48 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2002-2004 LWJGL 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 'LWJGL' 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.
|
|
|
|
|
*/
|
|
|
|
|
package org.lwjgl.opengl;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is the Windows implementation of the Keyboard.
|
|
|
|
|
* @author elias_naur
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
import java.nio.IntBuffer;
|
|
|
|
|
import java.nio.CharBuffer;
|
|
|
|
|
|
|
|
|
|
import org.lwjgl.LWJGLException;
|
|
|
|
|
import org.lwjgl.LWJGLUtil;
|
|
|
|
|
import org.lwjgl.BufferUtils;
|
|
|
|
|
|
|
|
|
|
final class WindowsKeyboard {
|
|
|
|
|
private final static int BUFFER_SIZE = 50;
|
|
|
|
|
|
2006-11-20 00:02:25 +01:00
|
|
|
private final long hwnd;
|
2006-06-26 15:50:48 +02:00
|
|
|
private final WindowsDirectInput dinput;
|
|
|
|
|
private final WindowsDirectInputDevice keyboard;
|
|
|
|
|
private final IntBuffer temp_data_buffer;
|
|
|
|
|
private final ByteBuffer keyboard_state;
|
2006-10-11 22:26:35 +02:00
|
|
|
private final boolean unicode;
|
2006-06-26 15:50:48 +02:00
|
|
|
private final CharBuffer unicode_buffer;
|
2006-10-11 22:26:35 +02:00
|
|
|
private final ByteBuffer ascii_buffer;
|
2006-06-26 15:50:48 +02:00
|
|
|
|
2006-11-20 00:02:25 +01:00
|
|
|
private boolean grabbed;
|
|
|
|
|
|
2006-06-26 15:50:48 +02:00
|
|
|
public WindowsKeyboard(WindowsDirectInput dinput, long hwnd) throws LWJGLException {
|
2006-11-20 00:02:25 +01:00
|
|
|
this.hwnd = hwnd;
|
2006-06-26 15:50:48 +02:00
|
|
|
this.dinput = dinput;
|
|
|
|
|
try {
|
|
|
|
|
keyboard = dinput.createDevice(WindowsDirectInput.KEYBOARD_TYPE);
|
|
|
|
|
try {
|
|
|
|
|
keyboard.setDataFormat(WindowsDirectInput.KEYBOARD_TYPE);
|
|
|
|
|
keyboard.setBufferSize(BUFFER_SIZE);
|
2006-11-20 00:02:25 +01:00
|
|
|
acquireNonExclusive();
|
2006-06-26 15:50:48 +02:00
|
|
|
} catch (LWJGLException e) {
|
|
|
|
|
keyboard.release();
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
} catch (LWJGLException e) {
|
|
|
|
|
dinput.release();
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
keyboard.acquire();
|
2006-07-04 19:46:33 +02:00
|
|
|
temp_data_buffer = BufferUtils.createIntBuffer(BUFFER_SIZE*WindowsDirectInputDevice.DATA_SIZE);
|
2006-06-26 15:50:48 +02:00
|
|
|
keyboard_state = BufferUtils.createByteBuffer(256);
|
2006-10-11 22:26:35 +02:00
|
|
|
unicode = isWindowsNT();
|
|
|
|
|
if (unicode) {
|
|
|
|
|
unicode_buffer = BufferUtils.createCharBuffer(BUFFER_SIZE);
|
|
|
|
|
ascii_buffer = null;
|
|
|
|
|
} else {
|
|
|
|
|
unicode_buffer = null;
|
|
|
|
|
// ToAscii returns at most 2 characters
|
|
|
|
|
ascii_buffer = BufferUtils.createByteBuffer(2);
|
|
|
|
|
}
|
2006-06-26 15:50:48 +02:00
|
|
|
}
|
2006-10-11 22:26:35 +02:00
|
|
|
private static native boolean isWindowsNT();
|
|
|
|
|
|
2006-11-20 00:02:25 +01:00
|
|
|
private boolean acquire(int flags) {
|
|
|
|
|
try {
|
|
|
|
|
keyboard.setCooperateLevel(hwnd, flags);
|
|
|
|
|
keyboard.acquire();
|
|
|
|
|
return true;
|
|
|
|
|
} catch (LWJGLException e) {
|
|
|
|
|
LWJGLUtil.log("Failed to acquire keyboard: " + e);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean acquireNonExclusive() {
|
|
|
|
|
return acquire(WindowsDirectInputDevice.DISCL_NONEXCLUSIVE | WindowsDirectInputDevice.DISCL_FOREGROUND) ||
|
|
|
|
|
acquire(WindowsDirectInputDevice.DISCL_NONEXCLUSIVE | WindowsDirectInputDevice.DISCL_BACKGROUND);
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-26 15:50:48 +02:00
|
|
|
public void destroy() {
|
|
|
|
|
keyboard.unacquire();
|
|
|
|
|
keyboard.release();
|
|
|
|
|
dinput.release();
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-20 00:02:25 +01:00
|
|
|
public void grab(boolean grab) {
|
|
|
|
|
if(grab) {
|
|
|
|
|
if (!grabbed) {
|
|
|
|
|
flush();
|
|
|
|
|
grabbed = true;
|
|
|
|
|
keyboard.unacquire();
|
|
|
|
|
if (!acquire(WindowsDirectInputDevice.DISCL_EXCLUSIVE | WindowsDirectInputDevice.DISCL_FOREGROUND))
|
|
|
|
|
LWJGLUtil.log("Failed to reset cooperative mode");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (grabbed) {
|
|
|
|
|
grabbed = false;
|
|
|
|
|
keyboard.unacquire();
|
|
|
|
|
acquireNonExclusive();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-26 15:50:48 +02:00
|
|
|
public void poll(ByteBuffer keyDownBuffer) {
|
|
|
|
|
int ret = keyboard.acquire();
|
|
|
|
|
if (ret != WindowsDirectInput.DI_OK && ret != WindowsDirectInput.DI_NOEFFECT)
|
|
|
|
|
return;
|
|
|
|
|
keyboard.poll();
|
|
|
|
|
ret = keyboard.getDeviceState(keyDownBuffer);
|
|
|
|
|
switch (ret) {
|
|
|
|
|
case WindowsDirectInput.DI_OK:
|
|
|
|
|
break;
|
|
|
|
|
case WindowsDirectInput.DI_BUFFEROVERFLOW:
|
|
|
|
|
LWJGLUtil.log("Keyboard buffer overflow");
|
|
|
|
|
break;
|
|
|
|
|
case WindowsDirectInput.DIERR_INPUTLOST:
|
|
|
|
|
break;
|
|
|
|
|
case WindowsDirectInput.DIERR_NOTACQUIRED:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
LWJGLUtil.log("Failed to poll keyboard (0x" + Integer.toHexString(ret) + ")");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-04 01:16:26 +02:00
|
|
|
private void translateData(IntBuffer src, ByteBuffer dst) {
|
2006-07-03 21:09:47 +02:00
|
|
|
while (dst.hasRemaining() && src.hasRemaining()) {
|
2006-06-26 15:50:48 +02:00
|
|
|
int dwOfs = src.get();
|
2006-07-04 01:16:26 +02:00
|
|
|
dst.putInt(dwOfs);
|
|
|
|
|
byte dwData = (byte)src.get();
|
2006-06-26 15:50:48 +02:00
|
|
|
boolean key_down = (dwData & 0x80) != 0;
|
2006-07-04 01:16:26 +02:00
|
|
|
dst.put(key_down ? (byte)1 : (byte)0);
|
2006-07-04 19:07:13 +02:00
|
|
|
long dwTimeStamp = ((long)src.get()) & 0xFFFFFFFF;
|
|
|
|
|
long nanos = dwTimeStamp*1000000;
|
2006-06-26 15:50:48 +02:00
|
|
|
if (key_down) {
|
|
|
|
|
int virt_key = MapVirtualKey(dwOfs, 1);
|
|
|
|
|
if (virt_key != 0 && GetKeyboardState(keyboard_state) != 0) {
|
|
|
|
|
// Mark key down in the scan code
|
|
|
|
|
dwOfs = dwOfs & 0x7fff;
|
2006-10-11 22:26:35 +02:00
|
|
|
int num_chars;
|
|
|
|
|
if (unicode) {
|
|
|
|
|
unicode_buffer.clear();
|
|
|
|
|
num_chars = ToUnicode(virt_key,
|
2006-06-26 15:50:48 +02:00
|
|
|
dwOfs,
|
|
|
|
|
keyboard_state,
|
|
|
|
|
unicode_buffer,
|
|
|
|
|
unicode_buffer.capacity(), 0);
|
2006-10-11 22:26:35 +02:00
|
|
|
} else {
|
|
|
|
|
ascii_buffer.clear();
|
|
|
|
|
num_chars = ToAscii(virt_key,
|
|
|
|
|
dwOfs,
|
|
|
|
|
keyboard_state,
|
|
|
|
|
ascii_buffer,
|
|
|
|
|
0);
|
|
|
|
|
}
|
2006-06-26 15:50:48 +02:00
|
|
|
if (num_chars > 0) {
|
|
|
|
|
int current_char = 0;
|
|
|
|
|
do {
|
|
|
|
|
if (current_char >= 1) {
|
2006-07-04 01:16:26 +02:00
|
|
|
dst.putInt(0);
|
|
|
|
|
dst.put((byte)0);
|
2006-06-26 15:50:48 +02:00
|
|
|
}
|
2006-10-11 22:26:35 +02:00
|
|
|
int char_int;
|
|
|
|
|
if (unicode) {
|
|
|
|
|
char_int = ((int)unicode_buffer.get()) & 0xFFFF;
|
|
|
|
|
} else {
|
|
|
|
|
char_int = ((int)ascii_buffer.get()) & 0xFF;
|
|
|
|
|
}
|
2006-07-04 01:16:26 +02:00
|
|
|
dst.putInt(char_int);
|
2006-07-04 19:07:13 +02:00
|
|
|
dst.putLong(nanos);
|
2006-06-26 15:50:48 +02:00
|
|
|
current_char++;
|
2006-07-03 21:09:47 +02:00
|
|
|
} while (dst.hasRemaining() && current_char < num_chars);
|
2006-06-26 15:50:48 +02:00
|
|
|
} else {
|
2006-07-04 01:16:26 +02:00
|
|
|
dst.putInt(0);
|
2006-07-04 19:07:13 +02:00
|
|
|
dst.putLong(nanos);
|
2006-06-26 15:50:48 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
2006-07-04 01:16:26 +02:00
|
|
|
dst.putInt(0);
|
2006-07-04 19:07:13 +02:00
|
|
|
dst.putLong(nanos);
|
2006-06-26 15:50:48 +02:00
|
|
|
}
|
2006-07-04 19:07:13 +02:00
|
|
|
} else {
|
2006-07-04 01:16:26 +02:00
|
|
|
dst.putInt(0);
|
2006-07-04 19:07:13 +02:00
|
|
|
dst.putLong(nanos);
|
|
|
|
|
}
|
2006-06-26 15:50:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private static native int MapVirtualKey(int uCode, int uMapType);
|
|
|
|
|
private static native int ToUnicode(int wVirtKey, int wScanCode, ByteBuffer lpKeyState, CharBuffer pwszBuff, int cchBuff, int flags);
|
2006-10-11 22:26:35 +02:00
|
|
|
private static native int ToAscii(int wVirtKey, int wScanCode, ByteBuffer lpKeyState, ByteBuffer lpChar, int flags);
|
2006-06-26 15:50:48 +02:00
|
|
|
private static native int GetKeyboardState(ByteBuffer lpKeyState);
|
|
|
|
|
|
2006-11-20 00:02:25 +01:00
|
|
|
public void flush() {
|
|
|
|
|
processEvents();
|
|
|
|
|
temp_data_buffer.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void processEvents() {
|
2006-06-26 15:50:48 +02:00
|
|
|
int ret = keyboard.acquire();
|
|
|
|
|
if (ret != WindowsDirectInput.DI_OK && ret != WindowsDirectInput.DI_NOEFFECT)
|
2006-07-03 21:09:47 +02:00
|
|
|
return;
|
2006-06-26 15:50:48 +02:00
|
|
|
keyboard.poll();
|
|
|
|
|
temp_data_buffer.clear();
|
|
|
|
|
ret = keyboard.getDeviceData(temp_data_buffer);
|
|
|
|
|
switch (ret) {
|
|
|
|
|
case WindowsDirectInput.DI_OK:
|
|
|
|
|
break;
|
|
|
|
|
case WindowsDirectInput.DI_BUFFEROVERFLOW:
|
|
|
|
|
LWJGLUtil.log("Keyboard buffer overflow");
|
|
|
|
|
break;
|
|
|
|
|
case WindowsDirectInput.DIERR_INPUTLOST:
|
|
|
|
|
break;
|
|
|
|
|
case WindowsDirectInput.DIERR_NOTACQUIRED:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
LWJGLUtil.log("Failed to read keyboard (0x" + Integer.toHexString(ret) + ")");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2006-11-20 00:02:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void read(ByteBuffer buffer) {
|
|
|
|
|
processEvents();
|
2006-06-26 15:50:48 +02:00
|
|
|
temp_data_buffer.flip();
|
2006-07-03 21:09:47 +02:00
|
|
|
translateData(temp_data_buffer, buffer);
|
2006-06-26 15:50:48 +02:00
|
|
|
}
|
|
|
|
|
}
|