Windows: Implemented AWTInputAdapter.

This commit is contained in:
Elias Naur 2006-10-30 15:46:42 +00:00
parent 4c5a2fe644
commit b2e21da777
12 changed files with 243 additions and 19 deletions

View File

@ -508,6 +508,7 @@
</javah>
<javah classpath="${lwjgl.bin}" destdir="${lwjgl.src.native}/windows" force="yes">
<class name="org.lwjgl.opengl.WindowsAWTInput" />
<class name="org.lwjgl.opengl.WindowsKeyboard" />
<class name="org.lwjgl.opengl.WindowsDirectInput8" />
<class name="org.lwjgl.opengl.WindowsDirectInputDevice8" />

View File

@ -0,0 +1,150 @@
/*
* 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;
import java.nio.IntBuffer;
import java.nio.ByteBuffer;
import org.lwjgl.LWJGLException;
import org.lwjgl.LWJGLUtil;
import org.lwjgl.BufferUtils;
import java.awt.Cursor;
import java.awt.Point;
/**
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision: 2586 $
* $Id: LinuxAWTGLCanvasPeerInfo.java 2586 2006-10-20 11:51:34Z elias_naur $
*/
final class WindowsAWTInput extends AbstractAWTInput {
private final static int WS_CHILD = 0x40000000;
private final Cursor blank_cursor;
private Cursor cached_cursor;
private long cached_hwnd;
private WindowsMouse cached_mouse;
private boolean has_grabbed;
public WindowsAWTInput(AWTGLCanvas canvas) throws LWJGLException {
super(canvas);
int w = AWTUtil.getMinCursorSize();
int h = AWTUtil.getMinCursorSize();
blank_cursor = AWTUtil.createCursor(w, h, 0, 0, 1, BufferUtils.createIntBuffer(w*h), null);
}
public synchronized void destroy() {
super.destroy();
if (cached_mouse != null) {
grab(false);
cached_mouse.destroy();
}
}
public synchronized void processInput(PeerInfo peer_info) {
WindowsPeerInfo windows_peerinfo = (WindowsPeerInfo)peer_info;
long hwnd = windows_peerinfo.getHwnd();
try {
hwnd = findTopLevelWindow(hwnd);
if (cached_mouse == null || hwnd != cached_hwnd) {
has_grabbed = false;
cached_hwnd = hwnd;
if (cached_mouse != null)
cached_mouse.destroy();
cached_mouse = new WindowsMouse(WindowsDisplay.createDirectInput(), hwnd);
}
if (isGrabbed() && getCanvas().getCursor() != blank_cursor) {
cached_cursor = getCanvas().getCursor();
/**
* For some reason, DirectInput won't let us blank the cursor
* with the EXCLUSIVE access mode, so we'll work around it with a
* custom blank cursor
*/
getCanvas().setCursor(blank_cursor);
}
grab(isGrabbed());
} catch (LWJGLException e) {
LWJGLUtil.log("Failed to create windows mouse: " + e);
}
}
private static native int getWindowStyles(long hwnd) throws LWJGLException;
private static native long getParentWindow(long hwnd);
private static long findTopLevelWindow(long hwnd) throws LWJGLException {
int window_styles = getWindowStyles(hwnd);
while ((window_styles & WS_CHILD) != 0) {
hwnd = getParentWindow(hwnd);
window_styles = getWindowStyles(hwnd);
}
return hwnd;
}
private void grab(boolean grab) {
if (has_grabbed != grab) {
cached_mouse.grab(grab);
has_grabbed = grab;
if (!grab)
getCanvas().setCursor(cached_cursor);
}
}
public synchronized void grabMouse(boolean grab) {
if (grab != isGrabbed()) {
super.grabMouse(grab);
/* Only ungrab since grabbing can only occur in processInput
* when the hwnd is guaranteed valid
*/
if (cached_mouse != null && !grab)
grab(grab);
}
}
public void update() {
}
public synchronized void pollMouse(IntBuffer coord_buffer, ByteBuffer buttons) {
if (isGrabbed()) {
if (cached_mouse != null)
cached_mouse.poll(coord_buffer, buttons);
} else
super.pollMouse(coord_buffer, buttons);
}
public synchronized void readMouse(ByteBuffer buffer) {
if (isGrabbed()) {
if (cached_mouse != null)
cached_mouse.read(buffer);
} else
super.readMouse(buffer);
}
}

View File

@ -66,7 +66,7 @@ final class WindowsCanvasImplementation implements AWTCanvasImplementation {
}
public AWTCanvasInputImplementation createInput(AWTGLCanvas canvas) throws LWJGLException {
throw new UnsupportedOperationException();
return new WindowsAWTInput(canvas);
}
public PeerInfo createPeerInfo(AWTGLCanvas canvas, PixelFormat pixel_format) throws LWJGLException {

View File

@ -76,14 +76,14 @@ abstract class WindowsDirectInputDevice {
public void setDataFormat(int type) throws LWJGLException {
int ret = setDataFormat(di_device, type);
if (ret != WindowsDirectInput.DI_OK)
throw new LWJGLException("Failed to set data format (" + ret + ")");
throw new LWJGLException("Failed to set data format (" + Integer.toHexString(ret) + ")");
}
protected abstract int setDataFormat(long di_device, int type);
public void setCooperateLevel(long hwnd, int flags) throws LWJGLException {
int ret = setCooperativeLevel(di_device, hwnd, flags);
if (ret != WindowsDirectInput.DI_OK)
throw new LWJGLException("Failed to set cooperative level (" + ret + ")");
throw new LWJGLException("Failed to set cooperative level (" + Integer.toHexString(ret) + ")");
}
protected abstract int setCooperativeLevel(long di_device, long hwnd, int flags);
@ -95,7 +95,7 @@ abstract class WindowsDirectInputDevice {
public void setBufferSize(int buffer_size) throws LWJGLException {
int ret = setBufferSize(di_device, buffer_size);
if (ret != WindowsDirectInput.DI_OK && ret != WindowsDirectInput.DI_PROPNOEFFECT && ret != WindowsDirectInput.DI_POLLEDDEVICE)
throw new LWJGLException("Failed to set buffer size (" + ret + ")");
throw new LWJGLException("Failed to set buffer size (" + Integer.toHexString(ret) + ")");
int event_buffer_size = getEventSize()*buffer_size;
event_buffer = BufferUtils.createByteBuffer(event_buffer_size);
}

View File

@ -289,6 +289,7 @@ final class WindowsDisplay implements DisplayImplementation {
peer_info = new WindowsDisplayPeerInfo(pixel_format);
return peer_info;
}
public void update() {
nUpdate();
if (did_maximize) {
@ -581,7 +582,7 @@ final class WindowsDisplay implements DisplayImplementation {
}
}
private static WindowsDirectInput createDirectInput() throws LWJGLException {
static WindowsDirectInput createDirectInput() throws LWJGLException {
try {
return new WindowsDirectInput8(getDllInstance());
} catch (LWJGLException e) {

View File

@ -52,4 +52,9 @@ abstract class WindowsPeerInfo extends PeerInfo {
nChoosePixelFormat(getHandle(), origin_x, origin_y, pixel_format, pixel_format_caps, use_hdc_bpp, support_window, support_pbuffer, double_buffered);
}
private static native void nChoosePixelFormat(ByteBuffer peer_info_handle, int origin_x, int origin_y, PixelFormat pixel_format, IntBuffer pixel_format_caps, boolean use_hdc_bpp, boolean support_window, boolean support_pbuffer, boolean double_buffered) throws LWJGLException;
public final long getHwnd() {
return nGetHwnd(getHandle());
}
private static native long nGetHwnd(ByteBuffer handle);
}

View File

@ -47,12 +47,15 @@
#include "extgl_wgl.h"
typedef struct {
struct {
HPBUFFERARB pbuffer;
// Contains the function pointers that
// created the pbuffer
WGLExtensions extensions;
} pbuffer;
union {
HWND hwnd;
struct {
HPBUFFERARB pbuffer;
// Contains the function pointers that
// created the pbuffer
WGLExtensions extensions;
} pbuffer;
} u;
HDC drawable_hdc;
} WindowsPeerInfo;

View File

@ -169,23 +169,23 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_WindowsPbufferPeerInfo_nCreate
throwException(env, "Could not get Pbuffer DC");
return;
}
peer_info->pbuffer.extensions = extensions;
peer_info->pbuffer.pbuffer = Pbuffer;
peer_info->u.pbuffer.extensions = extensions;
peer_info->u.pbuffer.pbuffer = Pbuffer;
peer_info->drawable_hdc = Pbuffer_dc;
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_WindowsPbufferPeerInfo_nDestroy
(JNIEnv *env, jclass clazz, jobject peer_info_handle) {
WindowsPeerInfo *peer_info = (WindowsPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle);
peer_info->pbuffer.extensions.wglReleasePbufferDCARB(peer_info->pbuffer.pbuffer, peer_info->drawable_hdc);
peer_info->pbuffer.extensions.wglDestroyPbufferARB(peer_info->pbuffer.pbuffer);
peer_info->u.pbuffer.extensions.wglReleasePbufferDCARB(peer_info->u.pbuffer.pbuffer, peer_info->drawable_hdc);
peer_info->u.pbuffer.extensions.wglDestroyPbufferARB(peer_info->u.pbuffer.pbuffer);
}
JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_WindowsPbufferPeerInfo_nIsBufferLost
(JNIEnv *env, jclass clazz, jobject peer_info_handle) {
WindowsPeerInfo *peer_info = (WindowsPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle);
BOOL buffer_lost;
peer_info->pbuffer.extensions.wglQueryPbufferARB(peer_info->pbuffer.pbuffer, WGL_PBUFFER_LOST_ARB, &buffer_lost);
peer_info->u.pbuffer.extensions.wglQueryPbufferARB(peer_info->u.pbuffer.pbuffer, WGL_PBUFFER_LOST_ARB, &buffer_lost);
return buffer_lost ? JNI_TRUE : JNI_FALSE;
}
@ -198,17 +198,17 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_WindowsPbufferPeerInfo_nSetPbufferA
attribs[1] = value;
attribs[2] = 0;
peer_info->pbuffer.extensions.wglSetPbufferAttribARB(peer_info->pbuffer.pbuffer, attribs);
peer_info->u.pbuffer.extensions.wglSetPbufferAttribARB(peer_info->u.pbuffer.pbuffer, attribs);
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_WindowsPbufferPeerInfo_nBindTexImageToPbuffer
(JNIEnv *env, jclass clazz, jobject peer_info_handle, jint buffer) {
WindowsPeerInfo *peer_info = (WindowsPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle);
peer_info->pbuffer.extensions.wglBindTexImageARB(peer_info->pbuffer.pbuffer, buffer);
peer_info->u.pbuffer.extensions.wglBindTexImageARB(peer_info->u.pbuffer.pbuffer, buffer);
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_WindowsPbufferPeerInfo_nReleaseTexImageFromPbuffer
(JNIEnv *env, jclass clazz, jobject peer_info_handle, jint buffer) {
WindowsPeerInfo *peer_info = (WindowsPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle);
peer_info->pbuffer.extensions.wglReleaseTexImageARB(peer_info->pbuffer.pbuffer, buffer);
peer_info->u.pbuffer.extensions.wglReleaseTexImageARB(peer_info->u.pbuffer.pbuffer, buffer);
}

View File

@ -51,4 +51,5 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_WindowsAWTGLCanvasPeerInfo_nInitHan
AWTSurfaceLock *surface = (AWTSurfaceLock *)(*env)->GetDirectBufferAddress(env, lock_buffer_handle);
JAWT_Win32DrawingSurfaceInfo *win32_dsi = (JAWT_Win32DrawingSurfaceInfo *)surface->dsi->platformInfo;
peer_info->drawable_hdc = win32_dsi->hdc;
peer_info->u.hwnd = win32_dsi->hwnd;
}

View File

@ -0,0 +1,57 @@
/*
* 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.
*/
/**
* $Id: org_lwjgl_opengl_WindowsAWTGLCanvasPeerInfo.c 2575 2006-09-19 14:17:13Z elias_naur $
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision: 2575 $
*/
#include <jni.h>
#include "context.h"
#include "org_lwjgl_opengl_WindowsAWTInput.h"
JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_WindowsAWTInput_getWindowStyles(JNIEnv *env, jclass unused, jlong hwnd_ptr) {
HWND hwnd = (HWND)(intptr_t)hwnd_ptr;
WINDOWINFO window_info;
if (!GetWindowInfo(hwnd, &window_info)) {
throwFormattedException(env, "Failed to get window info (%d).", GetLastError());
return 0;
}
return window_info.dwStyle;
}
JNIEXPORT jlong JNICALL Java_org_lwjgl_opengl_WindowsAWTInput_getParentWindow(JNIEnv *env, jclass unused, jlong hwnd_ptr) {
HWND hwnd = (HWND)(intptr_t)hwnd_ptr;
return (intptr_t)GetParent(hwnd);
}

View File

@ -47,4 +47,5 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_WindowsDisplayPeerInfo_nInitDC
(JNIEnv *env, jclass clazz, jobject peer_info_handle) {
WindowsPeerInfo *peer_info = (WindowsPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle);
peer_info->drawable_hdc = getCurrentHDC();
peer_info->u.hwnd = getCurrentHWND();
}

View File

@ -58,3 +58,8 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_WindowsPeerInfo_nChoosePixelFormat
// Let it throw
applyPixelFormat(env, peer_info->drawable_hdc, pixel_format_id);
}
JNIEXPORT jlong JNICALL Java_org_lwjgl_opengl_WindowsPeerInfo_nGetHwnd(JNIEnv *env, jclass unused, jobject peer_info_handle) {
WindowsPeerInfo *peer_info = (WindowsPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle);
return peer_info->u.hwnd;
}