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

@ -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);
}