lwjgl/src/java/org/lwjgl/opengl/MacOSXDisplay.java

590 lines
18 KiB
Java
Raw Normal View History

2004-11-25 17:31:38 -05:00
/*
2008-04-07 14:36:09 -04:00
* Copyright (c) 2002-2008 LWJGL Project
2004-11-11 11:03:19 -05:00
* All rights reserved.
2004-11-25 17:31:38 -05:00
*
2004-11-11 11:03:19 -05:00
* Redistribution and use in source and binary forms, with or without
2004-11-25 17:31:38 -05:00
* modification, are permitted provided that the following conditions are
2004-11-11 11:03:19 -05:00
* met:
2004-11-25 17:31:38 -05:00
*
* * Redistributions of source code must retain the above copyright
2004-11-11 11:03:19 -05:00
* 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.
*
2004-11-25 17:31:38 -05:00
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
2004-11-11 11:03:19 -05:00
* from this software without specific prior written permission.
2004-11-25 17:31:38 -05:00
*
2004-11-11 11:03:19 -05:00
* 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
2004-11-25 17:31:38 -05:00
* 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
2004-11-11 11:03:19 -05:00
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2004-11-25 17:31:38 -05:00
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2004-11-11 11:03:19 -05:00
* 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 Display implementation interface. Display delegates
* to implementors of this interface. There is one DisplayImplementation
* for each supported platform.
* @author elias_naur
*/
import java.awt.Canvas;
2008-06-05 09:36:57 -04:00
import java.awt.Cursor;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
2006-02-27 14:55:43 -05:00
import java.awt.Robot;
2004-11-20 11:46:44 -05:00
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.security.AccessController;
import java.security.PrivilegedAction;
2006-02-27 14:55:43 -05:00
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.List;
import org.lwjgl.BufferUtils;
import org.lwjgl.MemoryUtil;
2004-11-20 11:46:44 -05:00
import org.lwjgl.LWJGLException;
import org.lwjgl.LWJGLUtil;
2004-11-11 11:03:19 -05:00
2008-06-05 09:36:57 -04:00
import com.apple.eawt.Application;
import com.apple.eawt.ApplicationAdapter;
import com.apple.eawt.ApplicationEvent;
import static org.lwjgl.opengl.GL11.*;
2004-11-12 08:23:20 -05:00
final class MacOSXDisplay implements DisplayImplementation {
private static final int PBUFFER_HANDLE_SIZE = 24;
2004-11-25 17:31:38 -05:00
private static final int GAMMA_LENGTH = 256;
2004-11-16 09:08:31 -05:00
private MacOSXCanvasListener canvas_listener;
private Canvas canvas;
private Robot robot;
private MacOSXMouseEventQueue mouse_queue;
2004-11-11 11:03:19 -05:00
private KeyboardEventQueue keyboard_queue;
private java.awt.DisplayMode requested_mode;
/* Members for native window use */
private MacOSXNativeMouse mouse;
private MacOSXNativeKeyboard keyboard;
private ByteBuffer window;
private ByteBuffer context;
private int x;
private int y;
private int width;
private int height;
/* Whether we're using a native window or an AWT canvas */
private boolean native_mode;
2004-11-12 08:23:20 -05:00
private boolean close_requested;
2004-11-25 17:31:38 -05:00
MacOSXDisplay() {
try {
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
public Object run() throws Exception {
Application.getApplication().addApplicationListener(new ApplicationAdapter() {
public void handleQuit(ApplicationEvent event) {
doHandleQuit();
}
});
return null;
}
});
} catch (Throwable e) {
/**
* In an applet environment, referencing com.apple.eawt.Application can fail with
* a native exception. So log any exceptions instead of re-throwing.
*/
LWJGLUtil.log("Failed to register quit handler: " + e.getMessage());
}
2004-11-11 11:03:19 -05:00
}
2004-11-25 17:31:38 -05:00
private native ByteBuffer nCreateWindow(int x, int y, int width, int height, boolean fullscreen, boolean undecorated, ByteBuffer peer_info_handle, ByteBuffer window_handle) throws LWJGLException;
private native boolean nIsMiniaturized(ByteBuffer window_handle);
private native boolean nIsFocused(ByteBuffer window_handle);
private native void nSetResizable(ByteBuffer window_handle, boolean resizable);
private native void nResizeWindow(ByteBuffer window_handle, int x, int y, int width, int height);
private native boolean nWasResized(ByteBuffer window_handle);
private static boolean isUndecorated() {
return Display.getPrivilegedBoolean("org.lwjgl.opengl.Window.undecorated");
}
public void createWindow(final DrawableLWJGL drawable, DisplayMode mode, Canvas parent, int x, int y) throws LWJGLException {
boolean fullscreen = Display.isFullscreen();
hideUI(fullscreen);
2004-11-11 11:03:19 -05:00
close_requested = false;
DrawableGL gl_drawable = (DrawableGL)Display.getDrawable();
PeerInfo peer_info = gl_drawable.peer_info;
ByteBuffer peer_handle = peer_info.lockAndGetHandle();
2004-11-12 08:23:20 -05:00
try {
window = nCreateWindow(x, y, mode.getWidth(), mode.getHeight(),
fullscreen, isUndecorated(),
peer_handle, window);
native_mode = true;
this.x = x;
this.y = y;
this.width = mode.getWidth();
this.height = mode.getHeight();
this.canvas = parent;
2004-11-12 08:23:20 -05:00
} catch (LWJGLException e) {
destroyWindow();
throw e;
} finally {
peer_info.unlock();
}
2004-11-11 11:03:19 -05:00
}
public void doHandleQuit() {
2004-11-11 11:03:19 -05:00
synchronized (this) {
close_requested = true;
}
}
public native void nDestroyWindow(ByteBuffer window_handle);
2004-11-11 11:03:19 -05:00
public void destroyWindow() {
if (native_mode) {
nDestroyWindow(window);
} else {
if (canvas_listener != null) {
canvas_listener.disableListeners();
canvas_listener = null;
}
}
hideUI(false);
2004-11-11 11:03:19 -05:00
}
2004-11-25 17:31:38 -05:00
public int getGammaRampLength() {
2004-11-16 09:08:31 -05:00
return GAMMA_LENGTH;
}
2004-11-16 09:08:31 -05:00
public native void setGammaRamp(FloatBuffer gammaRamp) throws LWJGLException;
2004-11-11 11:03:19 -05:00
public String getAdapter() {
return null;
}
2004-11-25 17:31:38 -05:00
2004-11-11 11:03:19 -05:00
public String getVersion() {
return null;
}
2004-11-25 17:31:38 -05:00
private static boolean equals(java.awt.DisplayMode awt_mode, DisplayMode mode) {
2004-11-11 11:03:19 -05:00
return awt_mode.getWidth() == mode.getWidth() && awt_mode.getHeight() == mode.getHeight()
&& awt_mode.getBitDepth() == mode.getBitsPerPixel() && awt_mode.getRefreshRate() == mode.getFrequency();
}
public void switchDisplayMode(DisplayMode mode) throws LWJGLException {
java.awt.DisplayMode[] awt_modes = getDevice().getDisplayModes();
for ( java.awt.DisplayMode awt_mode : awt_modes ) {
if (equals(awt_mode, mode)) {
requested_mode = awt_mode;
2004-11-11 11:03:19 -05:00
return;
}
}
2004-11-11 11:03:19 -05:00
throw new LWJGLException(mode + " is not supported");
}
2004-11-25 17:31:38 -05:00
private static GraphicsDevice getDevice() {
GraphicsEnvironment g_env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = g_env.getDefaultScreenDevice();
return device;
}
2004-11-11 11:03:19 -05:00
public void resetDisplayMode() {
if (!native_mode) {
if (getDevice().getFullScreenWindow() != null)
getDevice().setFullScreenWindow(null);
}
2004-11-11 11:03:19 -05:00
requested_mode = null;
2004-11-16 09:08:31 -05:00
restoreGamma();
2004-11-11 11:03:19 -05:00
}
2004-11-16 09:08:31 -05:00
private native void restoreGamma();
2004-11-25 17:31:38 -05:00
private static DisplayMode createLWJGLDisplayMode(java.awt.DisplayMode awt_mode) {
2004-11-11 11:03:19 -05:00
int bit_depth;
int refresh_rate;
int awt_bit_depth = awt_mode.getBitDepth();
int awt_refresh_rate = awt_mode.getRefreshRate();
if (awt_bit_depth != java.awt.DisplayMode.BIT_DEPTH_MULTI)
bit_depth = awt_bit_depth;
else
bit_depth = 32; // Assume the best bit depth
if (awt_refresh_rate != java.awt.DisplayMode.REFRESH_RATE_UNKNOWN)
refresh_rate = awt_refresh_rate;
else
refresh_rate = 0;
return new DisplayMode(awt_mode.getWidth(), awt_mode.getHeight(), bit_depth, refresh_rate);
}
2004-11-25 17:31:38 -05:00
public DisplayMode init() throws LWJGLException {
return createLWJGLDisplayMode(getDevice().getDisplayMode());
2004-11-11 11:03:19 -05:00
}
2004-11-25 17:31:38 -05:00
public DisplayMode[] getAvailableDisplayModes() throws LWJGLException {
java.awt.DisplayMode[] awt_modes = getDevice().getDisplayModes();
List<DisplayMode> modes = new ArrayList<DisplayMode>();
for ( java.awt.DisplayMode awt_mode : awt_modes )
if ( awt_mode.getBitDepth() >= 16 )
modes.add(createLWJGLDisplayMode(awt_mode));
return modes.toArray(new DisplayMode[modes.size()]);
2004-11-11 11:03:19 -05:00
}
2004-11-25 17:31:38 -05:00
private native void nSetTitle(ByteBuffer window_handle, ByteBuffer title_buffer);
2004-11-11 11:03:19 -05:00
public void setTitle(String title) {
ByteBuffer buffer = MemoryUtil.encodeUTF8(title);
nSetTitle(window, buffer);
2004-11-11 11:03:19 -05:00
}
2004-11-25 17:31:38 -05:00
2004-11-11 11:03:19 -05:00
public boolean isCloseRequested() {
boolean result;
synchronized (this) {
result = close_requested;
2004-11-11 11:03:19 -05:00
close_requested = false;
}
return result;
}
public boolean isVisible() {
return true;
2004-11-11 11:03:19 -05:00
}
2004-11-25 17:31:38 -05:00
2004-11-11 11:03:19 -05:00
public boolean isActive() {
if (native_mode) {
boolean ret = nIsFocused(window);
return ret;
} else {
return canvas.isFocusOwner();
}
2004-11-11 11:03:19 -05:00
}
2004-11-25 17:31:38 -05:00
public Canvas getCanvas() {
return canvas;
}
2004-11-11 11:03:19 -05:00
public boolean isDirty() {
return false;
2004-11-11 11:03:19 -05:00
}
public PeerInfo createPeerInfo(PixelFormat pixel_format, ContextAttribs attribs) throws LWJGLException {
try {
return new MacOSXDisplayPeerInfo(pixel_format, attribs, true);
} catch (LWJGLException e) {
return new MacOSXDisplayPeerInfo(pixel_format, attribs, false);
}
}
2004-11-11 11:03:19 -05:00
private static final IntBuffer current_viewport = BufferUtils.createIntBuffer(16);
2004-11-11 11:03:19 -05:00
public void update() {
boolean should_update = true;
if (!native_mode) {
should_update = canvas_listener.syncShouldUpdateContext();
}
/*
* Workaround for the "white screen in fullscreen mode" problem
*
* Sometimes, switching from windowed mode to fullscreen or simply creating the Display
* in fullscreen mode will result in the context not being bound to the window correctly.
* The program runs fine, but the canvas background (white) is shown instead of the context
* front buffer.
*
* I've discovered that re-binding the context with another setView() won't fix the problem, while a
* clearDrawable() followed by a setView() does work. A straightforward workaround would be
* to simply run the combination at every update(). This is not sufficient, since the clearDrawable()
* call makes the the background appear shortly, causing visual artifacts.
* What we really need is a way to detect that a setView() failed, and then do the magic combo. I've not
* been able to find such a detection so alternatively I'm triggering the combo if the display is fullscreen
* (I've not seen the white screen problem in windowed mode) and if the canvas has gotten a paint message or
* if its update flag was set.
*
* My testing seems to indicate that the workaround works, since I've not seen the problem after the fix.
*
* - elias
*/
2011-05-17 12:53:57 -04:00
DrawableGL drawable = (DrawableGL)Display.getDrawable();
if (should_update) {
drawable.context.update();
2004-11-11 11:03:19 -05:00
/* This is necessary to make sure the context won't "forget" about the view size */
glGetInteger(GL_VIEWPORT, current_viewport);
glViewport(current_viewport.get(0), current_viewport.get(1), current_viewport.get(2), current_viewport.get(3));
}
/*
if (frame != null && mouse_queue != null) {
if (frame.syncShouldReleaseCursor())
MacOSXMouseEventQueue.nGrabMouse(false);
if (frame.syncShouldWarpCursor())
mouse_queue.warpCursor();
2004-11-11 11:03:19 -05:00
}
*/
2004-11-11 11:03:19 -05:00
}
/**
* This is an interface to the native Cocoa function,
* NSWindow:setStyleMask. It is used to set the window's border to
* undecorated.
*/
private void hideUI(boolean hide) {
//if (!LWJGLUtil.isMacOSXEqualsOrBetterThan(10, 4))
nHideUI(window, hide);
}
private native void nHideUI(ByteBuffer window_handle, boolean hide);
2004-11-11 11:03:19 -05:00
public void reshape(int x, int y, int width, int height) {
//if (native_mode) {
// nResizeWindow(window, x, y, width, height);
//}
2004-11-11 11:03:19 -05:00
}
/* Mouse */
public boolean hasWheel() {
return AWTUtil.hasWheel();
2004-11-11 11:03:19 -05:00
}
public int getButtonCount() {
return AWTUtil.getButtonCount();
2004-11-11 11:03:19 -05:00
}
2004-11-25 17:31:38 -05:00
public void createMouse() throws LWJGLException {
if (native_mode) {
mouse = new MacOSXNativeMouse(this, window);
mouse.register();
} else {
mouse_queue = new MacOSXMouseEventQueue(canvas);
mouse_queue.register();
}
2004-11-11 11:03:19 -05:00
}
public void destroyMouse() {
MacOSXMouseEventQueue.nGrabMouse(false);
if (native_mode) {
if (mouse != null) {
mouse.unregister();
}
mouse = null;
} else {
if (mouse_queue != null) {
mouse_queue.unregister();
}
mouse_queue = null;
}
2004-11-11 11:03:19 -05:00
}
2004-11-25 17:31:38 -05:00
2004-11-11 11:03:19 -05:00
public void pollMouse(IntBuffer coord_buffer, ByteBuffer buttons_buffer) {
if (native_mode) {
mouse.poll(coord_buffer, buttons_buffer);
} else {
mouse_queue.poll(coord_buffer, buttons_buffer);
}
2004-11-11 11:03:19 -05:00
}
2004-11-25 17:31:38 -05:00
public void readMouse(ByteBuffer buffer) {
if (native_mode) {
mouse.copyEvents(buffer);
} else {
mouse_queue.copyEvents(buffer);
}
2004-11-11 11:03:19 -05:00
}
public void grabMouse(boolean grab) {
if (native_mode) {
mouse.setGrabbed(grab);
} else {
mouse_queue.setGrabbed(grab);
}
2004-11-11 11:03:19 -05:00
}
public int getNativeCursorCapabilities() {
return AWTUtil.getNativeCursorCapabilities();
2004-11-11 11:03:19 -05:00
}
2004-11-25 17:31:38 -05:00
2005-04-12 07:45:06 -04:00
public void setCursorPosition(int x, int y) {
MacOSXMouseEventQueue.nWarpCursor(x, y);
2005-04-12 07:45:06 -04:00
}
2004-11-11 11:03:19 -05:00
public void setNativeCursor(Object handle) throws LWJGLException {
}
public int getMinCursorSize() {
return AWTUtil.getMinCursorSize();
2004-11-11 11:03:19 -05:00
}
public int getMaxCursorSize() {
return AWTUtil.getMaxCursorSize();
2004-11-11 11:03:19 -05:00
}
/* Keyboard */
public void createKeyboard() throws LWJGLException {
if (native_mode) {
this.keyboard = new MacOSXNativeKeyboard(window);
keyboard.register();
} else {
this.keyboard_queue = new KeyboardEventQueue(canvas);
keyboard_queue.register();
}
2004-11-11 11:03:19 -05:00
}
2004-11-25 17:31:38 -05:00
2004-11-11 11:03:19 -05:00
public void destroyKeyboard() {
if (native_mode) {
if (keyboard != null)
keyboard.unregister();
keyboard = null;
} else {
if (keyboard_queue != null)
keyboard_queue.unregister();
keyboard_queue = null;
}
2004-11-11 11:03:19 -05:00
}
2004-11-25 17:31:38 -05:00
2004-11-11 11:03:19 -05:00
public void pollKeyboard(ByteBuffer keyDownBuffer) {
if (native_mode) {
keyboard.poll(keyDownBuffer);
} else {
keyboard_queue.poll(keyDownBuffer);
}
2004-11-11 11:03:19 -05:00
}
2004-11-25 17:31:38 -05:00
public void readKeyboard(ByteBuffer buffer) {
if (native_mode) {
keyboard.copyEvents(buffer);
} else {
keyboard_queue.copyEvents(buffer);
}
}
2004-11-11 11:03:19 -05:00
/** Native cursor handles */
public Object createCursor(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, IntBuffer delays) throws LWJGLException {
return AWTUtil.createCursor(width, height, xHotspot, yHotspot, numImages, images, delays);
2004-11-11 11:03:19 -05:00
}
public void destroyCursor(Object cursor_handle) {
}
public int getPbufferCapabilities() {
if (LWJGLUtil.isMacOSXEqualsOrBetterThan(10, 3))
return Pbuffer.PBUFFER_SUPPORTED;
else
return 0;
}
public boolean isBufferLost(PeerInfo handle) {
return false;
}
public PeerInfo createPbuffer(int width, int height, PixelFormat pixel_format, ContextAttribs attribs,
IntBuffer pixelFormatCaps,
IntBuffer pBufferAttribs) throws LWJGLException {
return new MacOSXPbufferPeerInfo(width, height, pixel_format, attribs);
}
public void setPbufferAttrib(PeerInfo handle, int attrib, int value) {
throw new UnsupportedOperationException();
}
public void bindTexImageToPbuffer(PeerInfo handle, int buffer) {
throw new UnsupportedOperationException();
}
public void releaseTexImageFromPbuffer(PeerInfo handle, int buffer) {
throw new UnsupportedOperationException();
}
/**
* Sets one or more icons for the Display.
* <ul>
* <li>On Windows you should supply at least one 16x16 icon and one 32x32.</li>
* <li>Linux (and similar platforms) expect one 32x32 icon.</li>
* <li>Mac OS X should be supplied one 128x128 icon</li>
* </ul>
* The implementation will use the supplied ByteBuffers with image data in RGBA and perform any conversions nescesarry for the specific platform.
*
* @param icons Array of icons in RGBA mode
* @return number of icons used.
*/
public int setIcon(ByteBuffer[] icons) {
/* int size = 0;
int biggest = -1;
for (int i=0;i<icons.length;i++) {
if (icons[i].remaining() > size) {
biggest = i;
size = icons[i].remaining();
}
}
if (biggest == -1) {
return 0;
}
int width;
int height;
IntBuffer biggest_icon = icons[biggest].asIntBuffer();
int[] imageData = new int[biggest_icon.remaining()];
width = height = (int) Math.sqrt(imageData.length);
biggest_icon.get(imageData);
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
img.setRGB(0, 0, width, height, imageData, 0, width);
frame.setIconImage(img);
return 1;*/
// Don't use any icon, since Mac OS X windows don't have window icons
return 0;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public boolean isInsideWindow() {
return true;
}
public void setResizable(boolean resizable) {
if (native_mode) {
nSetResizable(window, resizable);
}
}
public boolean wasResized() {
if (native_mode) {
return nWasResized(window);
} else {
return canvas_listener.wasResized();
}
}
2004-11-11 11:03:19 -05:00
}