From c09f5698b4cf4571f169f8d0c1aa9ad94bead83d Mon Sep 17 00:00:00 2001 From: Brian Matzon Date: Thu, 1 Nov 2012 20:54:17 +0000 Subject: [PATCH 001/111] Comitting EmuMogs 28008537/src.zip drop to branch --- src/java/org/lwjgl/opengl/Display.java | 2 +- .../lwjgl/opengl/MacOSXCanvasPeerInfo.java | 10 +- .../opengl/MacOSXContextImplementation.java | 6 +- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 286 ++++++----- .../lwjgl/opengl/MacOSXMouseEventQueue.java | 21 +- .../lwjgl/opengl/MacOSXNativeKeyboard.java | 283 +++++++++++ .../org/lwjgl/opengl/MacOSXNativeMouse.java | 192 ++++++++ .../org/lwjgl/opengl/MouseEventQueue.java | 35 +- src/native/macosx/context.h | 62 ++- src/native/macosx/context.m | 11 +- src/native/macosx/org_lwjgl_opengl_Display.m | 451 +++++++++++++++++- .../org_lwjgl_opengl_MacOSXCanvasPeerInfo.m | 206 +------- ...lwjgl_opengl_MacOSXContextImplementation.m | 80 ++-- .../org_lwjgl_opengl_MacOSXNativeKeyboard.m | 58 +++ .../org_lwjgl_opengl_MacOSXNativeMouse.m | 78 +++ .../org_lwjgl_opengl_MacOSXPbufferPeerInfo.m | 2 +- 16 files changed, 1375 insertions(+), 408 deletions(-) create mode 100644 src/java/org/lwjgl/opengl/MacOSXNativeKeyboard.java create mode 100644 src/java/org/lwjgl/opengl/MacOSXNativeMouse.java create mode 100644 src/native/macosx/org_lwjgl_opengl_MacOSXNativeKeyboard.m create mode 100644 src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m diff --git a/src/java/org/lwjgl/opengl/Display.java b/src/java/org/lwjgl/opengl/Display.java index e875529e..121740bb 100644 --- a/src/java/org/lwjgl/opengl/Display.java +++ b/src/java/org/lwjgl/opengl/Display.java @@ -242,7 +242,7 @@ public final class Display { try { if ( was_fullscreen && !isFullscreen() ) display_impl.resetDisplayMode(); - else if ( isFullscreen() ) + else if ( isFullscreen() ) switchDisplayMode(); createWindow(); makeCurrentAndSetSwapInterval(); diff --git a/src/java/org/lwjgl/opengl/MacOSXCanvasPeerInfo.java b/src/java/org/lwjgl/opengl/MacOSXCanvasPeerInfo.java index 3ebf4502..8aeade7f 100644 --- a/src/java/org/lwjgl/opengl/MacOSXCanvasPeerInfo.java +++ b/src/java/org/lwjgl/opengl/MacOSXCanvasPeerInfo.java @@ -44,21 +44,15 @@ import org.lwjgl.LWJGLUtil; * $Id$ */ abstract class MacOSXCanvasPeerInfo extends MacOSXPeerInfo { - private final AWTSurfaceLock awt_surface = new AWTSurfaceLock(); - protected MacOSXCanvasPeerInfo(PixelFormat pixel_format, ContextAttribs attribs, boolean support_pbuffer) throws LWJGLException { super(pixel_format, attribs, true, true, support_pbuffer, true); } protected void initHandle(Canvas component) throws LWJGLException { - // Allow the use of a Core Animation Layer only when using non fullscreen Display.setParent() or AWTGLCanvas - final boolean allowCALayer = ((Display.getParent() != null && !Display.isFullscreen()) || component instanceof AWTGLCanvas) && awt_surface.isApplet(component) && LWJGLUtil.isMacOSXEqualsOrBetterThan(10, 6); - - nInitHandle(awt_surface.lockAndGetHandle(component), getHandle(), allowCALayer); + nInitHandle(getHandle()); } - private static native void nInitHandle(ByteBuffer surface_buffer, ByteBuffer peer_info_handle, boolean allowCALayer) throws LWJGLException; + private static native void nInitHandle(ByteBuffer peer_info_handle) throws LWJGLException; protected void doUnlock() throws LWJGLException { - awt_surface.unlock(); } } diff --git a/src/java/org/lwjgl/opengl/MacOSXContextImplementation.java b/src/java/org/lwjgl/opengl/MacOSXContextImplementation.java index c47d8c87..5ea1e80f 100644 --- a/src/java/org/lwjgl/opengl/MacOSXContextImplementation.java +++ b/src/java/org/lwjgl/opengl/MacOSXContextImplementation.java @@ -90,7 +90,7 @@ final class MacOSXContextImplementation implements ContextImplementation { try { synchronized ( context ) { clearDrawable(context.getHandle()); - setView(peer_handle, context.getHandle()); + setView(peer_handle); } } finally { peer_info.unlock(); @@ -100,14 +100,14 @@ final class MacOSXContextImplementation implements ContextImplementation { public void makeCurrent(PeerInfo peer_info, ByteBuffer handle) throws LWJGLException { ByteBuffer peer_handle = peer_info.lockAndGetHandle(); try { - setView(peer_handle, handle); + setView(peer_handle); nMakeCurrent(handle); } finally { peer_info.unlock(); } } - private static native void setView(ByteBuffer peer_handle, ByteBuffer context_handle) throws LWJGLException; + private static native void setView(ByteBuffer peer_handle) throws LWJGLException; private static native void nMakeCurrent(ByteBuffer context_handle) throws LWJGLException; diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index c6032c72..65b5f900 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -40,6 +40,8 @@ package org.lwjgl.opengl; import java.awt.Canvas; import java.awt.Cursor; +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; import java.awt.Robot; import java.nio.ByteBuffer; import java.nio.FloatBuffer; @@ -51,6 +53,7 @@ import java.util.ArrayList; import java.util.List; import org.lwjgl.BufferUtils; +import org.lwjgl.MemoryUtil; import org.lwjgl.LWJGLException; import org.lwjgl.LWJGLUtil; @@ -65,14 +68,25 @@ final class MacOSXDisplay implements DisplayImplementation { private static final int GAMMA_LENGTH = 256; private MacOSXCanvasListener canvas_listener; - private MacOSXFrame frame; private Canvas canvas; private Robot robot; private MacOSXMouseEventQueue mouse_queue; 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; - /* States */ private boolean close_requested; MacOSXDisplay() { @@ -96,49 +110,65 @@ final class MacOSXDisplay implements DisplayImplementation { } } - public void createWindow(final DrawableLWJGL drawable, DisplayMode mode, Canvas parent, int x, int y) throws LWJGLException { + 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); close_requested = false; + + DrawableGL gl_drawable = (DrawableGL)Display.getDrawable(); + PeerInfo peer_info = gl_drawable.peer_info; + ByteBuffer peer_handle = peer_info.lockAndGetHandle(); try { - if (parent == null) { - frame = new MacOSXFrame(mode, requested_mode, fullscreen, x, y); - canvas = frame.getCanvas(); - } else { - frame = null; - canvas = parent; - } - canvas_listener = new MacOSXCanvasListener(canvas); - robot = AWTUtil.createRobot(canvas); + 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; } catch (LWJGLException e) { destroyWindow(); throw e; - } + } finally { + peer_info.unlock(); + } } - private void doHandleQuit() { + public void doHandleQuit() { synchronized (this) { close_requested = true; } } + public native void nDestroyWindow(ByteBuffer window_handle); + public void destroyWindow() { - if (canvas_listener != null) { - canvas_listener.disableListeners(); - canvas_listener = null; - } - if (frame != null) { - AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - if (MacOSXFrame.getDevice().getFullScreenWindow() == frame) - MacOSXFrame.getDevice().setFullScreenWindow(null); - return null; - } - }); - if (frame.isDisplayable()) - frame.dispose(); - frame = null; - } + if (native_mode) { + nDestroyWindow(window); + } else { + if (canvas_listener != null) { + canvas_listener.disableListeners(); + canvas_listener = null; + } + } hideUI(false); } @@ -162,7 +192,7 @@ final class MacOSXDisplay implements DisplayImplementation { } public void switchDisplayMode(DisplayMode mode) throws LWJGLException { - java.awt.DisplayMode[] awt_modes = MacOSXFrame.getDevice().getDisplayModes(); + java.awt.DisplayMode[] awt_modes = getDevice().getDisplayModes(); for ( java.awt.DisplayMode awt_mode : awt_modes ) { if (equals(awt_mode, mode)) { requested_mode = awt_mode; @@ -172,9 +202,17 @@ final class MacOSXDisplay implements DisplayImplementation { throw new LWJGLException(mode + " is not supported"); } + private static GraphicsDevice getDevice() { + GraphicsEnvironment g_env = GraphicsEnvironment.getLocalGraphicsEnvironment(); + GraphicsDevice device = g_env.getDefaultScreenDevice(); + return device; + } + public void resetDisplayMode() { - if (MacOSXFrame.getDevice().getFullScreenWindow() != null) - MacOSXFrame.getDevice().setFullScreenWindow(null); + if (!native_mode) { + if (getDevice().getFullScreenWindow() != null) + getDevice().setFullScreenWindow(null); + } requested_mode = null; restoreGamma(); } @@ -198,11 +236,11 @@ final class MacOSXDisplay implements DisplayImplementation { } public DisplayMode init() throws LWJGLException { - return createLWJGLDisplayMode(MacOSXFrame.getDevice().getDisplayMode()); + return createLWJGLDisplayMode(getDevice().getDisplayMode()); } public DisplayMode[] getAvailableDisplayModes() throws LWJGLException { - java.awt.DisplayMode[] awt_modes = MacOSXFrame.getDevice().getDisplayModes(); + java.awt.DisplayMode[] awt_modes = getDevice().getDisplayModes(); List modes = new ArrayList(); for ( java.awt.DisplayMode awt_mode : awt_modes ) if ( awt_mode.getBitDepth() >= 16 ) @@ -210,26 +248,33 @@ final class MacOSXDisplay implements DisplayImplementation { return modes.toArray(new DisplayMode[modes.size()]); } + private native void nSetTitle(ByteBuffer window_handle, ByteBuffer title_buffer); + public void setTitle(String title) { - if (frame != null) - frame.setTitle(title); + ByteBuffer buffer = MemoryUtil.encodeUTF8(title); + nSetTitle(window, buffer); } public boolean isCloseRequested() { boolean result; synchronized (this) { - result = close_requested || (frame != null && frame.syncIsCloseRequested()); + result = close_requested; close_requested = false; } return result; } public boolean isVisible() { - return frame == null || frame.syncIsVisible(); + return true; } public boolean isActive() { - return canvas.isFocusOwner(); + if (native_mode) { + boolean ret = nIsFocused(window); + return ret; + } else { + return canvas.isFocusOwner(); + } } public Canvas getCanvas() { @@ -237,7 +282,7 @@ final class MacOSXDisplay implements DisplayImplementation { } public boolean isDirty() { - return frame != null && frame.getCanvas().syncIsDirty(); + return false; } public PeerInfo createPeerInfo(PixelFormat pixel_format, ContextAttribs attribs) throws LWJGLException { @@ -250,7 +295,10 @@ final class MacOSXDisplay implements DisplayImplementation { private static final IntBuffer current_viewport = BufferUtils.createIntBuffer(16); public void update() { - boolean should_update = canvas_listener.syncShouldUpdateContext(); + boolean should_update = true; + if (!native_mode) { + should_update = canvas_listener.syncShouldUpdateContext(); + } /* * Workaround for the "white screen in fullscreen mode" problem * @@ -273,46 +321,38 @@ final class MacOSXDisplay implements DisplayImplementation { * - elias */ DrawableGL drawable = (DrawableGL)Display.getDrawable(); - if (Display.isFullscreen() && (frame != null && frame.getCanvas().syncCanvasPainted() || should_update)) { - try { - MacOSXContextImplementation.resetView(drawable.peer_info, drawable.context); - } catch (LWJGLException e) { - LWJGLUtil.log("Failed to reset context: " + e); - } - } if (should_update) { drawable.context.update(); /* 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(); } + */ } /** - * This is an interface to the native Carbon call - * SetSystemUIMode. It is used to hide the dock in a way - * that will prevent AWT from shifting the fullscreen window - * - * The workaround is not necessary on 10.4, and since 10.4 shows - * instability problems calling SetSystemUIMode, we'll only call it - * when the OS version is 10.3 or lower. + * 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(hide); + //if (!LWJGLUtil.isMacOSXEqualsOrBetterThan(10, 4)) + nHideUI(window, hide); } - private native void nHideUI(boolean hide); + private native void nHideUI(ByteBuffer window_handle, boolean hide); public void reshape(int x, int y, int width, int height) { - if (frame != null) - frame.resize(x, y, width, height); + //if (native_mode) { + // nResizeWindow(window, x, y, width, height); + //} } /* Mouse */ @@ -325,28 +365,52 @@ final class MacOSXDisplay implements DisplayImplementation { } public void createMouse() throws LWJGLException { - this.mouse_queue = new MacOSXMouseEventQueue(canvas); - mouse_queue.register(); + if (native_mode) { + mouse = new MacOSXNativeMouse(this, window); + mouse.register(); + } else { + mouse_queue = new MacOSXMouseEventQueue(canvas); + mouse_queue.register(); + } } public void destroyMouse() { - if (mouse_queue != null) { - MacOSXMouseEventQueue.nGrabMouse(false); - mouse_queue.unregister(); - } - this.mouse_queue = null; + MacOSXMouseEventQueue.nGrabMouse(false); + if (native_mode) { + if (mouse != null) { + mouse.unregister(); + } + mouse = null; + } else { + if (mouse_queue != null) { + mouse_queue.unregister(); + } + mouse_queue = null; + } } public void pollMouse(IntBuffer coord_buffer, ByteBuffer buttons_buffer) { - mouse_queue.poll(coord_buffer, buttons_buffer); + if (native_mode) { + mouse.poll(coord_buffer, buttons_buffer); + } else { + mouse_queue.poll(coord_buffer, buttons_buffer); + } } public void readMouse(ByteBuffer buffer) { - mouse_queue.copyEvents(buffer); + if (native_mode) { + mouse.copyEvents(buffer); + } else { + mouse_queue.copyEvents(buffer); + } } public void grabMouse(boolean grab) { - mouse_queue.setGrabbed(grab); + if (native_mode) { + mouse.setGrabbed(grab); + } else { + mouse_queue.setGrabbed(grab); + } } public int getNativeCursorCapabilities() { @@ -354,13 +418,10 @@ final class MacOSXDisplay implements DisplayImplementation { } public void setCursorPosition(int x, int y) { - AWTUtil.setCursorPosition(canvas, robot, x, y); + MacOSXMouseEventQueue.nWarpCursor(x, y); } public void setNativeCursor(Object handle) throws LWJGLException { - Cursor awt_cursor = (Cursor)handle; - if (frame != null) - frame.setCursor(awt_cursor); } public int getMinCursorSize() { @@ -373,48 +434,43 @@ final class MacOSXDisplay implements DisplayImplementation { /* Keyboard */ public void createKeyboard() throws LWJGLException { - this.keyboard_queue = new KeyboardEventQueue(canvas); - keyboard_queue.register(); + if (native_mode) { + this.keyboard = new MacOSXNativeKeyboard(window); + keyboard.register(); + } else { + this.keyboard_queue = new KeyboardEventQueue(canvas); + keyboard_queue.register(); + } } public void destroyKeyboard() { - if (keyboard_queue != null) - keyboard_queue.unregister(); - this.keyboard_queue = null; + if (native_mode) { + if (keyboard != null) + keyboard.unregister(); + keyboard = null; + } else { + if (keyboard_queue != null) + keyboard_queue.unregister(); + keyboard_queue = null; + } } public void pollKeyboard(ByteBuffer keyDownBuffer) { - keyboard_queue.poll(keyDownBuffer); + if (native_mode) { + keyboard.poll(keyDownBuffer); + } else { + keyboard_queue.poll(keyDownBuffer); + } } public void readKeyboard(ByteBuffer buffer) { - keyboard_queue.copyEvents(buffer); + if (native_mode) { + keyboard.copyEvents(buffer); + } else { + keyboard_queue.copyEvents(buffer); + } } -/* public int isStateKeySet(int key) { - int awt_key; - switch (key) { - case Keyboard.KEY_CAPITAL: - awt_key = KeyEvent.VK_CAPS_LOCK; - break; - case Keyboard.KEY_NUMLOCK: - awt_key = KeyEvent.VK_NUM_LOCK; - break; - case Keyboard.KEY_SYSRQ: - awt_key = KeyEvent.VK_SCROLL_LOCK; - break; - default: - return Keyboard.STATE_UNKNOWN; - } - try { - boolean state = Toolkit.getDefaultToolkit().getLockingKeyState(awt_key); - return state ? Keyboard.STATE_ON : Keyboard.STATE_OFF; - } catch (Exception e) { - LWJGLUtil.log("Failed to query key state: " + e); - return Keyboard.STATE_UNKNOWN; - } - } -*/ /** 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); @@ -497,19 +553,19 @@ final class MacOSXDisplay implements DisplayImplementation { } public int getX() { - return frame.getX(); + return x; } public int getY() { - return frame.getY(); + return y; } public int getWidth() { - return frame.getWidth(); + return width; } public int getHeight() { - return frame.getHeight(); + return height; } public boolean isInsideWindow() { @@ -517,11 +573,17 @@ final class MacOSXDisplay implements DisplayImplementation { } public void setResizable(boolean resizable) { - frame.setResizable(resizable); + if (native_mode) { + nSetResizable(window, resizable); + } } public boolean wasResized() { - return canvas_listener.wasResized(); + if (native_mode) { + return nWasResized(window); + } else { + return canvas_listener.wasResized(); + } } } diff --git a/src/java/org/lwjgl/opengl/MacOSXMouseEventQueue.java b/src/java/org/lwjgl/opengl/MacOSXMouseEventQueue.java index b232becd..5e4d221a 100644 --- a/src/java/org/lwjgl/opengl/MacOSXMouseEventQueue.java +++ b/src/java/org/lwjgl/opengl/MacOSXMouseEventQueue.java @@ -44,13 +44,13 @@ import java.nio.IntBuffer; import org.lwjgl.BufferUtils; final class MacOSXMouseEventQueue extends MouseEventQueue { - private final IntBuffer delta_buffer = BufferUtils.createIntBuffer(2); - - private boolean skip_event; - private static boolean is_grabbed; + private IntBuffer delta_buffer = BufferUtils.createIntBuffer(2); + + private boolean skip_event; + private static boolean is_grabbed; MacOSXMouseEventQueue(Component component) { - super(component); + super(component); } public void setGrabbed(boolean grab) { @@ -96,18 +96,11 @@ final class MacOSXMouseEventQueue extends MouseEventQueue { // If we're going to warp the cursor position, we'll skip the next event to avoid bogus delta values skip_event = isGrabbed(); } -/* if (isGrabbed()) { - Rectangle bounds = getComponent().getBounds(); - Point location_on_screen = getComponent().getLocationOnScreen(); - int x = location_on_screen.x + bounds.width/2; - int y = location_on_screen.y + bounds.height/2; - nWarpCursor(x, y); - }*/ } private static native void getMouseDeltas(IntBuffer delta_buffer); - private static native void nWarpCursor(int x, int y); + public static native void nWarpCursor(int x, int y); - static native void nGrabMouse(boolean grab); + public static native void nGrabMouse(boolean grab); } diff --git a/src/java/org/lwjgl/opengl/MacOSXNativeKeyboard.java b/src/java/org/lwjgl/opengl/MacOSXNativeKeyboard.java new file mode 100644 index 00000000..ee12eef9 --- /dev/null +++ b/src/java/org/lwjgl/opengl/MacOSXNativeKeyboard.java @@ -0,0 +1,283 @@ +/* + * Copyright (c) 2002-2008 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; + +/** + * An AWT implementation of a LWJGL compatible Keyboard event queue. + * @author elias_naur + */ + +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.util.HashMap; +import java.awt.Component; +import java.nio.ByteBuffer; + +import org.lwjgl.input.Keyboard; + +final class MacOSXNativeKeyboard extends EventQueue { + private final byte[] key_states = new byte[Keyboard.KEYBOARD_SIZE]; + + /** Event scratch array */ + private final ByteBuffer event = ByteBuffer.allocate(Keyboard.EVENT_SIZE); + + private ByteBuffer window_handle; + + private boolean has_deferred_event; + private long deferred_nanos; + private int deferred_key_code; + private byte deferred_key_state; + private int deferred_character; + + private HashMap nativeToLwjglMap; + + MacOSXNativeKeyboard(ByteBuffer window_handle) { + super(Keyboard.EVENT_SIZE); + nativeToLwjglMap = new HashMap(); + initKeyboardMappings(); + this.window_handle = window_handle; + } + + private native void nRegisterKeyListener(ByteBuffer window_handle); + + private native void nUnregisterKeyListener(ByteBuffer window_handle); + + private void initKeyboardMappings() { + nativeToLwjglMap.put((Short)(short)0x1D, Keyboard.KEY_0); + nativeToLwjglMap.put((Short)(short)0x12, Keyboard.KEY_1); + nativeToLwjglMap.put((Short)(short)0x13, Keyboard.KEY_2); + nativeToLwjglMap.put((Short)(short)0x14, Keyboard.KEY_3); + nativeToLwjglMap.put((Short)(short)0x15, Keyboard.KEY_4); + nativeToLwjglMap.put((Short)(short)0x17, Keyboard.KEY_5); + nativeToLwjglMap.put((Short)(short)0x16, Keyboard.KEY_6); + nativeToLwjglMap.put((Short)(short)0x1A, Keyboard.KEY_7); + nativeToLwjglMap.put((Short)(short)0x1C, Keyboard.KEY_8); + nativeToLwjglMap.put((Short)(short)0x19, Keyboard.KEY_9); + nativeToLwjglMap.put((Short)(short)0x00, Keyboard.KEY_A); + nativeToLwjglMap.put((Short)(short)0x0B, Keyboard.KEY_B); + nativeToLwjglMap.put((Short)(short)0x08, Keyboard.KEY_C); + nativeToLwjglMap.put((Short)(short)0x02, Keyboard.KEY_D); + nativeToLwjglMap.put((Short)(short)0x0E, Keyboard.KEY_E); + nativeToLwjglMap.put((Short)(short)0x03, Keyboard.KEY_F); + nativeToLwjglMap.put((Short)(short)0x05, Keyboard.KEY_G); + nativeToLwjglMap.put((Short)(short)0x04, Keyboard.KEY_H); + nativeToLwjglMap.put((Short)(short)0x22, Keyboard.KEY_I); + nativeToLwjglMap.put((Short)(short)0x26, Keyboard.KEY_J); + nativeToLwjglMap.put((Short)(short)0x28, Keyboard.KEY_K); + nativeToLwjglMap.put((Short)(short)0x25, Keyboard.KEY_L); + nativeToLwjglMap.put((Short)(short)0x2E, Keyboard.KEY_M); + nativeToLwjglMap.put((Short)(short)0x2D, Keyboard.KEY_N); + nativeToLwjglMap.put((Short)(short)0x1F, Keyboard.KEY_O); + nativeToLwjglMap.put((Short)(short)0x23, Keyboard.KEY_P); + nativeToLwjglMap.put((Short)(short)0x0C, Keyboard.KEY_Q); + nativeToLwjglMap.put((Short)(short)0x0F, Keyboard.KEY_R); + nativeToLwjglMap.put((Short)(short)0x01, Keyboard.KEY_S); + nativeToLwjglMap.put((Short)(short)0x11, Keyboard.KEY_T); + nativeToLwjglMap.put((Short)(short)0x20, Keyboard.KEY_U); + nativeToLwjglMap.put((Short)(short)0x09, Keyboard.KEY_V); + nativeToLwjglMap.put((Short)(short)0x0D, Keyboard.KEY_W); + nativeToLwjglMap.put((Short)(short)0x07, Keyboard.KEY_X); + nativeToLwjglMap.put((Short)(short)0x10, Keyboard.KEY_Y); + nativeToLwjglMap.put((Short)(short)0x06, Keyboard.KEY_Z); + nativeToLwjglMap.put((Short)(short)0x2B, Keyboard.KEY_COMMA); + nativeToLwjglMap.put((Short)(short)0x2C, Keyboard.KEY_SLASH); + nativeToLwjglMap.put((Short)(short)0x2F, Keyboard.KEY_PERIOD); + nativeToLwjglMap.put((Short)(short)0x32, Keyboard.KEY_CIRCUMFLEX); + nativeToLwjglMap.put((Short)(short)0x29, Keyboard.KEY_SEMICOLON); + nativeToLwjglMap.put((Short)(short)0x129, Keyboard.KEY_COLON); + nativeToLwjglMap.put((Short)(short)0x2A, Keyboard.KEY_BACKSLASH); + nativeToLwjglMap.put((Short)(short)0x52, Keyboard.KEY_NUMPAD0); + nativeToLwjglMap.put((Short)(short)0x53, Keyboard.KEY_NUMPAD1); + nativeToLwjglMap.put((Short)(short)0x54, Keyboard.KEY_NUMPAD2); + nativeToLwjglMap.put((Short)(short)0x55, Keyboard.KEY_NUMPAD3); + nativeToLwjglMap.put((Short)(short)0x56, Keyboard.KEY_NUMPAD4); + nativeToLwjglMap.put((Short)(short)0x57, Keyboard.KEY_NUMPAD5); + nativeToLwjglMap.put((Short)(short)0x58, Keyboard.KEY_NUMPAD6); + nativeToLwjglMap.put((Short)(short)0x59, Keyboard.KEY_NUMPAD7); + nativeToLwjglMap.put((Short)(short)0x5B, Keyboard.KEY_NUMPAD8); + nativeToLwjglMap.put((Short)(short)0x5C, Keyboard.KEY_NUMPAD9); + nativeToLwjglMap.put((Short)(short)0x7A, Keyboard.KEY_F1); + nativeToLwjglMap.put((Short)(short)0x78, Keyboard.KEY_F2); + nativeToLwjglMap.put((Short)(short)0x63, Keyboard.KEY_F3); + nativeToLwjglMap.put((Short)(short)0x76, Keyboard.KEY_F4); + nativeToLwjglMap.put((Short)(short)0x60, Keyboard.KEY_F5); + nativeToLwjglMap.put((Short)(short)0x61, Keyboard.KEY_F6); + nativeToLwjglMap.put((Short)(short)0x62, Keyboard.KEY_F7); + nativeToLwjglMap.put((Short)(short)0x64, Keyboard.KEY_F8); + nativeToLwjglMap.put((Short)(short)0x65, Keyboard.KEY_F9); + nativeToLwjglMap.put((Short)(short)0x6D, Keyboard.KEY_F10); + nativeToLwjglMap.put((Short)(short)0x67, Keyboard.KEY_F11); + nativeToLwjglMap.put((Short)(short)0x6F, Keyboard.KEY_F12); + nativeToLwjglMap.put((Short)(short)0x69, Keyboard.KEY_F13); + nativeToLwjglMap.put((Short)(short)0x6B, Keyboard.KEY_F14); + nativeToLwjglMap.put((Short)(short)0x71, Keyboard.KEY_F15); + nativeToLwjglMap.put((Short)(short)0x72, Keyboard.KEY_HOME); + nativeToLwjglMap.put((Short)(short)0x18, Keyboard.KEY_EQUALS); + nativeToLwjglMap.put((Short)(short)0x7B, Keyboard.KEY_LEFT); + nativeToLwjglMap.put((Short)(short)0x7C, Keyboard.KEY_RIGHT); + nativeToLwjglMap.put((Short)(short)0x7E, Keyboard.KEY_UP); + nativeToLwjglMap.put((Short)(short)0x7D, Keyboard.KEY_DOWN); + nativeToLwjglMap.put((Short)(short)0x31, Keyboard.KEY_SPACE); + nativeToLwjglMap.put((Short)(short)0x30, Keyboard.KEY_TAB); + nativeToLwjglMap.put((Short)(short)0x35, Keyboard.KEY_ESCAPE); + nativeToLwjglMap.put((Short)(short)0x74, Keyboard.KEY_PRIOR); + nativeToLwjglMap.put((Short)(short)0x79, Keyboard.KEY_NEXT); + nativeToLwjglMap.put((Short)(short)0x41, Keyboard.KEY_DECIMAL); + nativeToLwjglMap.put((Short)(short)0x43, Keyboard.KEY_MULTIPLY); + nativeToLwjglMap.put((Short)(short)0x45, Keyboard.KEY_ADD); + nativeToLwjglMap.put((Short)(short)0x4B, Keyboard.KEY_DIVIDE); + nativeToLwjglMap.put((Short)(short)0x1B, Keyboard.KEY_MINUS); + nativeToLwjglMap.put((Short)(short)0x4E, Keyboard.KEY_SUBTRACT); + nativeToLwjglMap.put((Short)(short)0x1E, Keyboard.KEY_RBRACKET); + nativeToLwjglMap.put((Short)(short)0x21, Keyboard.KEY_LBRACKET); + nativeToLwjglMap.put((Short)(short)0x33, Keyboard.KEY_BACK); + nativeToLwjglMap.put((Short)(short)0x24, Keyboard.KEY_RETURN); + nativeToLwjglMap.put((Short)(short)0xF0, Keyboard.KEY_CAPITAL); + nativeToLwjglMap.put((Short)(short)0x39, Keyboard.KEY_CAPITAL); + nativeToLwjglMap.put((Short)(short)0xF1, Keyboard.KEY_LSHIFT); + nativeToLwjglMap.put((Short)(short)0x38, Keyboard.KEY_LSHIFT); + nativeToLwjglMap.put((Short)(short)0x3C, Keyboard.KEY_RSHIFT); + nativeToLwjglMap.put((Short)(short)0xF2, Keyboard.KEY_LCONTROL); + nativeToLwjglMap.put((Short)(short)0xF3, Keyboard.KEY_LMENU); + nativeToLwjglMap.put((Short)(short)0x3A, Keyboard.KEY_LMENU); + nativeToLwjglMap.put((Short)(short)0x3D, Keyboard.KEY_RMENU); + nativeToLwjglMap.put((Short)(short)0xF4, Keyboard.KEY_RETURN); + nativeToLwjglMap.put((Short)(short)0xF5, Keyboard.KEY_NUMLOCK); + nativeToLwjglMap.put((Short)(short)0x27, Keyboard.KEY_APOSTROPHE); + + /*KEY_MAP[KeyEvent.VK_ALT_GRAPH] = Keyboard.KEY_RMENU; + KEY_MAP[KeyEvent.VK_AT] = Keyboard.KEY_AT; + KEY_MAP[KeyEvent.VK_BACK_SPACE] = Keyboard.KEY_BACK; + KEY_MAP[KeyEvent.VK_CAPS_LOCK] = Keyboard.KEY_CAPITAL; + KEY_MAP[KeyEvent.VK_COLON] = Keyboard.KEY_COLON; + KEY_MAP[KeyEvent.VK_CONVERT] = Keyboard.KEY_CONVERT; + KEY_MAP[KeyEvent.VK_END] = Keyboard.KEY_END; + KEY_MAP[KeyEvent.VK_INSERT] = Keyboard.KEY_INSERT; + KEY_MAP[KeyEvent.VK_KANA] = Keyboard.KEY_KANA; + KEY_MAP[KeyEvent.VK_KANJI] = Keyboard.KEY_KANJI; + KEY_MAP[KeyEvent.VK_NUM_LOCK] = Keyboard.KEY_NUMLOCK; + KEY_MAP[KeyEvent.VK_PAUSE] = Keyboard.KEY_PAUSE; + KEY_MAP[KeyEvent.VK_SCROLL_LOCK] = Keyboard.KEY_SCROLL; + KEY_MAP[KeyEvent.VK_SEPARATOR] = Keyboard.KEY_DECIMAL; + KEY_MAP[KeyEvent.VK_STOP] = Keyboard.KEY_STOP;*/ + } + + public void register() { + nRegisterKeyListener(window_handle); + } + + public void unregister() { + nUnregisterKeyListener(window_handle); + } + + public void putKeyboardEvent(int key_code, byte state, int character, long nanos, boolean repeat) { + event.clear(); + event.putInt(key_code).put(state).putInt(character).putLong(nanos).put(repeat ? (byte)1 : (byte)0); + event.flip(); + putEvent(event); + } + + public synchronized void poll(ByteBuffer key_down_buffer) { + flushDeferredEvent(); + int old_position = key_down_buffer.position(); + key_down_buffer.put(key_states); + key_down_buffer.position(old_position); + } + + public synchronized void copyEvents(ByteBuffer dest) { + flushDeferredEvent(); + super.copyEvents(dest); + } + + private synchronized void handleKey(int key_code, byte state, int character, long nanos) { + if (character == KeyEvent.CHAR_UNDEFINED) + character = Keyboard.CHAR_NONE; + if (state == 1) { + boolean repeat = false; + if (has_deferred_event) { + if ((nanos == deferred_nanos && deferred_key_code == key_code)) { + has_deferred_event = false; + repeat = true; // Repeat event + } else + flushDeferredEvent(); + } + putKeyEvent(key_code, state, character, nanos, repeat); + } else { + flushDeferredEvent(); + has_deferred_event = true; + deferred_nanos = nanos; + deferred_key_code = key_code; + deferred_key_state = state; + deferred_character = character; + } + } + + private void flushDeferredEvent() { + if (has_deferred_event) { + putKeyEvent(deferred_key_code, deferred_key_state, deferred_character, deferred_nanos, false); + has_deferred_event = false; + } + } + + public void putKeyEvent(int key_code, byte state, int character, long nanos, boolean repeat) { + /* Ignore repeating presses */ + int mapped_code = getMappedKeyCode((short)key_code); + if (mapped_code < 0) { + System.out.println("Unrecognized keycode: " + key_code); + /* Unrecognized / unmapped code, do nothing */ + return; + } + if ( key_states[mapped_code] == state ) + repeat = true; + key_states[mapped_code] = state; + int key_int_char = character & 0xffff; + putKeyboardEvent(mapped_code, state, key_int_char, nanos, repeat); + } + + private int getMappedKeyCode(short key_code) { + if (nativeToLwjglMap.containsKey(key_code)) { + return nativeToLwjglMap.get(key_code); + } + return -1; + } + + public void keyPressed(int key_code, int character, long nanos) { + handleKey(key_code, (byte)1, character, nanos); + } + + public void keyReleased(int key_code, int character, long nanos) { + handleKey(key_code, (byte)0, character, nanos); + } + + public void keyTyped(KeyEvent e) { + } +} diff --git a/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java b/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java new file mode 100644 index 00000000..599b6441 --- /dev/null +++ b/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java @@ -0,0 +1,192 @@ +/* + * Copyright (c) 2002-2008 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; + +/** + * A Cocoa implementation of a LWJGL compatible Mouse. + * @author mojang + */ + +import java.nio.ByteBuffer; +import java.nio.IntBuffer; + +import org.lwjgl.input.Mouse; + +import java.lang.reflect.*; +import java.lang.Integer; +import java.lang.Long; + +import org.lwjgl.BufferUtils; + +final class MacOSXNativeMouse extends EventQueue { + private static final int WHEEL_SCALE = 120; + private static final int NUM_BUTTONS = 3; + + private ByteBuffer window_handle; + private MacOSXDisplay display; + + private boolean grabbed; + + /** The accumulated mouse deltas returned by poll() */ + private float accum_dx; + private float accum_dy; + private int accum_dz; + + /** The last mouse position */ + private float last_x; + private float last_y; + + /** Saved control key state for ctrl-click right button emulation */ + private boolean saved_control_state; + + private final ByteBuffer event = ByteBuffer.allocate(Mouse.EVENT_SIZE); + private IntBuffer delta_buffer = BufferUtils.createIntBuffer(2); + private int skip_event; + + private final byte[] buttons = new byte[NUM_BUTTONS]; + + MacOSXNativeMouse(MacOSXDisplay display, ByteBuffer window_handle) { + super(Mouse.EVENT_SIZE); + this.display = display; + this.window_handle = window_handle; + } + + private native void nWarpCursor(ByteBuffer window_handle, int x, int y); + + public static native void nGrabMouse(boolean grab); + + private native void nRegisterMouseListener(ByteBuffer window_handle); + + private native void nUnregisterMouseListener(ByteBuffer window_handle); + + public synchronized void register() { + nRegisterMouseListener(window_handle); + } + + public synchronized void unregister() { + nUnregisterMouseListener(window_handle); + } + + public synchronized void setGrabbed(boolean grabbed) { + this.grabbed = grabbed; + nGrabMouse(grabbed); + skip_event = 1; + accum_dx = accum_dy = 0; + } + + public synchronized boolean isGrabbed() { + return grabbed; + } + + protected void resetCursorToCenter() { + clearEvents(); + accum_dx = accum_dy = 0; + if (display != null) { + last_x = display.getWidth() / 2; + last_y = display.getHeight() / 2; + } + } + + private void putMouseEvent(byte button, byte state, int dz, long nanos) { + if (grabbed) + putMouseEventWithCoords(button, state, 0, 0, dz, nanos); + else + putMouseEventWithCoords(button, state, (int)last_x, (int)last_y, dz, nanos); + } + + protected void putMouseEventWithCoords(byte button, byte state, int coord1, int coord2, int dz, long nanos) { + event.clear(); + event.put(button).put(state).putInt(coord1).putInt(coord2).putInt(dz).putLong(nanos); + event.flip(); + putEvent(event); + } + + public synchronized void poll(IntBuffer coord_buffer, ByteBuffer buttons_buffer) { + if (grabbed) { + coord_buffer.put(0, (int)accum_dx); + coord_buffer.put(1, (int)accum_dy); + } else { + coord_buffer.put(0, (int)last_x); + coord_buffer.put(1, (int)last_y); + } + coord_buffer.put(2, accum_dz); + accum_dx = accum_dy = accum_dz = 0; + int old_position = buttons_buffer.position(); + buttons_buffer.put(buttons, 0, buttons.length); + buttons_buffer.position(old_position); + } + + private void setCursorPos(float x, float y, long nanos) { + if ( grabbed ) + return; + float dx = x - last_x; + float dy = y - last_y; + addDelta(dx, dy); + last_x = x; + last_y = y; + putMouseEventWithCoords((byte)-1, (byte)0, (int)x, (int)y, 0, nanos); + } + + protected void addDelta(float dx, float dy) { + accum_dx += dx; + accum_dy += -dy; + } + + public synchronized void setButton(int button, int state, long nanos) { + buttons[button] = (byte)state; + putMouseEvent((byte)button, (byte)state, 0, nanos); + } + + public synchronized void mouseMoved(float x, float y, float dx, float dy, float dz, long nanos) { + if (skip_event > 0) { + skip_event--; + if (skip_event == 0) { + last_x = x; + last_y = y; + } + return; + } + if (grabbed) { + if ( dx != 0 || dy != 0 ) { + putMouseEventWithCoords((byte)-1, (byte)0, (int)dx, (int)-dy, 0, nanos); + addDelta(dx, dy); + } + } else { + setCursorPos(x, y, nanos); + } + if ( dz != 0 ) { + int wheel_amount = (int)(-dz * WHEEL_SCALE); + accum_dz += wheel_amount; + putMouseEvent((byte)-1, (byte)0, wheel_amount, nanos); + } + } +} diff --git a/src/java/org/lwjgl/opengl/MouseEventQueue.java b/src/java/org/lwjgl/opengl/MouseEventQueue.java index fa537cd6..356b471f 100644 --- a/src/java/org/lwjgl/opengl/MouseEventQueue.java +++ b/src/java/org/lwjgl/opengl/MouseEventQueue.java @@ -81,15 +81,19 @@ class MouseEventQueue extends EventQueue implements MouseListener, MouseMotionLi public synchronized void register() { resetCursorToCenter(); - component.addMouseListener(this); - component.addMouseMotionListener(this); - component.addMouseWheelListener(this); + if (component != null) { + component.addMouseListener(this); + component.addMouseMotionListener(this); + component.addMouseWheelListener(this); + } } public synchronized void unregister() { - component.removeMouseListener(this); - component.removeMouseMotionListener(this); - component.removeMouseWheelListener(this); + if (component != null) { + component.removeMouseListener(this); + component.removeMouseMotionListener(this); + component.removeMouseWheelListener(this); + } } protected Component getComponent() { @@ -105,18 +109,23 @@ class MouseEventQueue extends EventQueue implements MouseListener, MouseMotionLi return grabbed; } - private int transformY(int y) { - return component.getHeight() - 1 - y; + protected int transformY(int y) { + if (component != null) { + return component.getHeight() - 1 - y; + } + return y; } protected void resetCursorToCenter() { clearEvents(); accum_dx = accum_dy = 0; - Point cursor_location = AWTUtil.getCursorPosition(component); - if (cursor_location != null) { - last_x = cursor_location.x; - last_y = cursor_location.y; - } + if (component != null) { + Point cursor_location = AWTUtil.getCursorPosition(component); + if (cursor_location != null) { + last_x = cursor_location.x; + last_y = cursor_location.y; + } + } } private void putMouseEvent(byte button, byte state, int dz, long nanos) { diff --git a/src/native/macosx/context.h b/src/native/macosx/context.h index 188a0b32..3a6af76d 100644 --- a/src/native/macosx/context.h +++ b/src/native/macosx/context.h @@ -47,15 +47,65 @@ #include #include "common_tools.h" +@class NSOpenGLContext, NSOpenGLPixelFormat, MacOSXOpenGLView, MacOSXKeyableWindow; + typedef struct { - NSOpenGLPixelFormat *pixel_format; - bool window; + MacOSXKeyableWindow *window; + + NSRect display_rect; + + MacOSXOpenGLView *view; + NSOpenGLContext *context; + + // Native objects for Java callbacks + jobject jdisplay; + jobject jmouse; + jobject jkeyboard; + jboolean resized; + + // Cached for window creation + NSApplicationPresentationOptions window_options; +} MacOSXWindowInfo; + +@interface MacOSXKeyableWindow : NSWindow + +- (BOOL)canBecomeKeyWindow; +@end + +@interface MacOSXOpenGLView : NSView +{ + @public + MacOSXWindowInfo* _parent; + + @private + NSOpenGLContext* _openGLContext; + NSOpenGLPixelFormat* _pixelFormat; + NSUInteger _lastModifierFlags; + NSUInteger _modifierFlags; +} + ++ (NSOpenGLPixelFormat*)defaultPixelFormat; +- (id)initWithFrame:(NSRect)frameRect pixelFormat:(NSOpenGLPixelFormat*)format; +- (void)setOpenGLContext:(NSOpenGLContext*)context; +- (NSOpenGLContext*)openGLContext; +- (void)clearGLContext; +- (void)prepareOpenGL; +- (void)update; +- (void)lockFocus; +- (void)setPixelFormat:(NSOpenGLPixelFormat*)pixelFormat; +- (NSOpenGLPixelFormat*)pixelFormat; +- (void)setParent:(MacOSXWindowInfo*)parent; +- (BOOL)acceptsFirstResponder; +@end + +typedef struct { + bool isWindowed; bool canDrawGL; - union { - NSView *nsview; - NSOpenGLPixelBuffer *pbuffer; - }; + MacOSXWindowInfo *window_info; + NSOpenGLPixelFormat *pixel_format; + NSOpenGLPixelBuffer *pbuffer; } MacOSXPeerInfo; + NSOpenGLPixelFormat *choosePixelFormat(JNIEnv *env, jobject pixel_format, bool gl32, bool use_display_bpp, bool support_window, bool support_pbuffer, bool double_buffered); #endif diff --git a/src/native/macosx/context.m b/src/native/macosx/context.m index 510eb2d4..0240b2f7 100644 --- a/src/native/macosx/context.m +++ b/src/native/macosx/context.m @@ -91,7 +91,16 @@ NSOpenGLPixelFormat *choosePixelFormat(JNIEnv *env, jobject pixel_format, bool g int bpp; jclass cls_pixel_format = (*env)->GetObjectClass(env, pixel_format); if (use_display_bpp) - bpp = CGDisplayBitsPerPixel(kCGDirectMainDisplay); + { + CGDisplayModeRef mode = CGDisplayCopyDisplayMode(kCGDirectMainDisplay); + CFStringRef pixEnc = CGDisplayModeCopyPixelEncoding(mode); + if (CFStringCompare(pixEnc, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) + bpp = 32; + else if(CFStringCompare(pixEnc, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) + bpp = 16; + else if(CFStringCompare(pixEnc, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) + bpp = 8; + } else bpp = (int)(*env)->GetIntField(env, pixel_format, (*env)->GetFieldID(env, cls_pixel_format, "bpp", "I")); diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 96002d83..13955657 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -39,18 +39,433 @@ * @version $Revision$ */ +#import #import #import -#import #import #import -//#import "display.h" #import "common_tools.h" #import "org_lwjgl_opengl_MacOSXDisplay.h" #import "org_lwjgl_MacOSXSysImplementation.h" +#import "context.h" #define WAIT_DELAY 100 +static NSOpenGLPixelFormat *default_format = nil; + +@implementation MacOSXKeyableWindow +- (BOOL)canBecomeKeyWindow; +{ + return YES; +} +@end + +@implementation MacOSXOpenGLView + ++ (NSOpenGLPixelFormat*)defaultPixelFormat +{ + NSOpenGLPixelFormatAttribute defaultAttribs[] = { + NSOpenGLPFADoubleBuffer, + NSOpenGLPFADepthSize, 16, + NSOpenGLPFAColorSize, 32, + 0 + }; + if (default_format == nil) { + default_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:defaultAttribs]; + } + return default_format; +} + +- (void) windowWillClose:(NSNotification *)notification +{ + MacOSXKeyableWindow *closingWindow = [notification object]; + + if (_parent != nil && closingWindow == _parent->window) { + JNIEnv *env = attachCurrentThread(); + jclass display_class = (*env)->GetObjectClass(env, _parent->jdisplay); + jmethodID close_callback = (*env)->GetMethodID(env, display_class, "doHandleQuit", "()V"); + (*env)->CallVoidMethod(env, _parent->jdisplay, close_callback); + } +} + +- (id)initWithFrame:(NSRect)frameRect pixelFormat:(NSOpenGLPixelFormat*)format +{ + self = [super initWithFrame:frameRect]; + _lastModifierFlags = 0; + _modifierFlags = 0; + if (self != nil) { + _pixelFormat = [format retain]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(_surfaceNeedsUpdate:) + name:NSViewGlobalFrameDidChangeNotification + object:self]; + } + return self; +} + +- (void) _surfaceNeedsUpdate:(NSNotification*)notification +{ + [self update]; +} + +- (void)setOpenGLContext:(NSOpenGLContext*)context +{ + _openGLContext = context; +} + +- (NSOpenGLContext*)openGLContext +{ + return _openGLContext; +} + +- (void)clearGLContext +{ + [_openGLContext release]; + _openGLContext = nil; +} + +- (void)prepareOpenGL +{ + +} + +- (void)update +{ + [_openGLContext update]; +} + +- (void)lockFocus +{ + NSOpenGLContext* context = [self openGLContext]; + + [super lockFocus]; + if ([context view] != self) { + [context setView:self]; + } + + [context makeCurrentContext]; +} + +- (void)setPixelFormat:(NSOpenGLPixelFormat*)pixelFormat +{ + _pixelFormat = [pixelFormat retain]; +} + +- (NSOpenGLPixelFormat*)pixelFormat +{ + return _pixelFormat; +} + +- (BOOL)acceptsFirstResponder { + return YES; +} + +- (void)setParent:(MacOSXWindowInfo*)parent { + // Un-register for native window close events if we have a parent window already + if (_parent != nil) { + [[NSNotificationCenter defaultCenter] removeObserver:self + name:NSWindowWillCloseNotification + object:_parent->window]; + } + _parent = parent; + // Register for native window close events if we now have a parent window + if (_parent != nil) { + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(windowWillClose:) name:NSWindowWillCloseNotification + object:_parent->window]; + } +} + +- (void)keyDown:(NSEvent *)event { + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil || _parent->jkeyboard == nil) { + return; + } + long time = [event timestamp] * 1000000000; + jclass keyboard_class = (*env)->GetObjectClass(env, _parent->jkeyboard); + jmethodID keydown = (*env)->GetMethodID(env, keyboard_class, "keyPressed", "(IIJ)V"); + const char* charbuf = [[event characters] cStringUsingEncoding:NSASCIIStringEncoding]; + int charcode = (charbuf == nil) ? 0 : charbuf[0]; + (*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, [event keyCode], charcode, time); +} + +- (void)keyUp:(NSEvent *)event { + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil || _parent->jkeyboard == nil) { + return; + } + long time = [event timestamp] * 1000000000; + jclass keyboard_class = (*env)->GetObjectClass(env, _parent->jkeyboard); + jmethodID keyup = (*env)->GetMethodID(env, keyboard_class, "keyReleased", "(IIJ)V"); + const char* charbuf = [[event characters] cStringUsingEncoding:NSASCIIStringEncoding]; + int charcode = (charbuf == nil) ? 0 : charbuf[0]; + (*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, [event keyCode], charcode, time); +} + +- (void)flagsChanged:(NSEvent *)event { + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil || _parent->jkeyboard == nil) { + return; + } + long time = [event timestamp] * 1000000000; + jclass keyboard_class = (*env)->GetObjectClass(env, _parent->jkeyboard); + jmethodID keydown = (*env)->GetMethodID(env, keyboard_class, "keyPressed", "(IIJ)V"); + jmethodID keyup = (*env)->GetMethodID(env, keyboard_class, "keyReleased", "(IIJ)V"); + _lastModifierFlags = _modifierFlags; + _modifierFlags = [event modifierFlags]; + NSUInteger flagDown = ~_lastModifierFlags & _modifierFlags; + NSUInteger flagUp = _lastModifierFlags & ~_modifierFlags; + if (flagDown & NSAlphaShiftKeyMask) { + (*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, 0xf0, 0, time); + } + if (flagUp & NSAlphaShiftKeyMask) { + (*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, 0xf0, 0, time); + } + if (flagDown & NSShiftKeyMask) { + (*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, 0xf1, 0, time); + } + if (flagUp & NSShiftKeyMask) { + (*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, 0xf1, 0, time); + } + if (flagDown & NSControlKeyMask) { + (*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, 0xf2, 0, time); + } + if (flagUp & NSControlKeyMask) { + (*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, 0xf2, 0, time); + } + if (flagDown & NSAlternateKeyMask) { + (*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, 0xf3, 0, time); + } + if (flagUp & NSAlternateKeyMask) { + (*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, 0xf3, 0, time); + } + if (flagDown & NSCommandKeyMask) { + (*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, 0xf4, 0, time); + } + if (flagUp & NSCommandKeyMask) { + (*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, 0xf4, 0, time); + } + if (flagDown & NSNumericPadKeyMask) { + (*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, 0xf5, 0, time); + } + if (flagUp & NSNumericPadKeyMask) { + (*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, 0xf5, 0, time); + } + //const char* charbuf = [[event characters] cStringUsingEncoding:NSASCIIStringEncoding]; + //(*env)->CallVoidMethod(env, _parent->jkeyboard, keymod, (jint)[event keyCode], (jint)charbuf[0], time); +} + +- (void)mouseButtonState:(NSEvent *)event :(int)button :(int)state { + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil || _parent->jkeyboard == nil) { + return; + } + long time = [event timestamp] * 1000000000; + jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); + jmethodID mousebutton = (*env)->GetMethodID(env, mouse_class, "setButton", "(IIJ)V"); + (*env)->CallVoidMethod(env, _parent->jmouse, mousebutton, button, state, time); +} + +- (void)mouseDown:(NSEvent *)event { + [self mouseButtonState:event :0 :1]; +} + +- (void)rightMouseDown:(NSEvent *)event { + [self mouseButtonState:event :1 :1]; +} + +- (void)otherMouseDown:(NSEvent *)event { + [self mouseButtonState:event :2 :1]; +} + +- (void)mouseUp:(NSEvent *)event { + [self mouseButtonState:event :0 :0]; +} + +- (void)rightMouseUp:(NSEvent *)event { + [self mouseButtonState:event :1 :0]; +} + +- (void)otherMouseUp:(NSEvent *)event { + [self mouseButtonState:event :2 :0]; +} + +- (void)mouseDragged:(NSEvent *)event { + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil) { + return; + } + long time = [event timestamp] * 1000000000; + jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); + jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); + NSPoint loc = [self convertPoint:[event locationInWindow] toView:self]; + (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time); +} + +- (void)mouseMoved:(NSEvent *)event { + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil || _parent->jmouse == nil) { + return; + } + long time = [event timestamp] * 1000000000; + jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); + jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); + NSPoint loc = [self convertPoint:[event locationInWindow] toView:self]; + (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time); +} + +- (void)scrollWheel:(NSEvent *)event { + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil) { + return; + } + long time = [event timestamp] * 1000000000; + float dz = [event scrollingDeltaY]; + if (![event hasPreciseScrollingDeltas]) { + dz *= 12; // or so + } + jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); + jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); + NSPoint loc = [self convertPoint:[event locationInWindow] toView:self]; + (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], dz, time); +} + +- (void)viewDidMoveToWindow +{ + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(windowResized:) + name:NSWindowDidResizeNotification + object:[self window]]; +} + +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self]; + [super dealloc]; +} + +- (void)windowResized:(NSNotification *)notification; +{ + if (_parent != nil) { + _parent->display_rect = [[self window] frame]; + _parent->resized = JNI_TRUE; + } +} +@end + +JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nIsMiniaturized(JNIEnv *env, jobject this, jobject window_handle) { + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + return (jboolean)[window_info->window isMiniaturized]; +} + +JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nIsFocused(JNIEnv *env, jobject this, jobject window_handle) { + return JNI_TRUE; + //MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + //return (jboolean)([window_info->window isKeyWindow] || [window_info->window isMainWindow]); +} + +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nResizeWindow(JNIEnv *env, jobject this, jobject window_handle, jint x, jint y, jint width, jint height) { + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + window_info->display_rect = NSMakeRect(x, y, width, height); + [window_info->window setFrame:window_info->display_rect display:false]; + [window_info->view update]; +} + +JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nWasResized(JNIEnv *env, jobject this, jobject window_handle) { + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + jboolean was_resized = window_info->resized; + window_info->resized = JNI_FALSE; + return was_resized; +} + +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nSetResizable(JNIEnv *env, jobject this, jobject window_handle, jboolean resizable) { + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + NSUInteger style_mask = [window_info->window styleMask]; + if (resizable == true) { + style_mask |= NSResizableWindowMask; + } else { + style_mask &= ~NSResizableWindowMask; + } + [window_info->window setStyleMask:style_mask]; +} + +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv *env, jobject this, jobject window_handle) { + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + + if (window_info->window != nil) { + [window_info->window close]; + } + window_info->window = nil; + + if (window_info->view != nil) { + [window_info->view release]; + } + window_info->view = nil; + //[window_info->window release]; +} + +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nSetTitle(JNIEnv *env, jobject this, jobject window_handle, jobject title_buffer) { + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + const char *title_cstr = (const char *)(*env)->GetDirectBufferAddress(env, title_buffer); + NSString *title = [[NSString alloc] initWithUTF8String:title_cstr]; + [window_info->window setTitle:title]; +} + +JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIEnv *env, jobject this, jint x, jint y, jint width, jint height, jboolean fullscreen, jboolean undecorated, jobject peer_info_handle, jobject window_handle) { + + if (window_handle == NULL) { + window_handle = newJavaManagedByteBuffer(env, sizeof(MacOSXWindowInfo)); + if (window_handle == NULL) { + throwException(env, "Could not create handle buffer"); + return NULL; + } + } + + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); + + // Cache the necessary info for window-close callbacks into the JVM + if (window_info->jdisplay == NULL) { + window_info->jdisplay = (*env)->NewGlobalRef(env, this); + } + + window_info->display_rect = NSMakeRect(x, y, width, height); + int default_window_mask = NSBorderlessWindowMask; + if (!undecorated && !fullscreen) { + printf("Resizeable\n"); fflush(stdout); + default_window_mask = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask; + } + window_info->window = (MacOSXKeyableWindow*)[[NSApplication sharedApplication] mainWindow]; + if (window_info->window == nil) { + window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:default_window_mask backing:NSBackingStoreBuffered defer:YES]; + } + + NSRect view_rect = NSMakeRect(0.0, 0.0, width, height); + window_info->view = [[MacOSXOpenGLView alloc] initWithFrame:view_rect pixelFormat:peer_info->pixel_format]; + if (window_info->context != nil) { + printf("Setting context\n"); fflush(stdout); + [window_info->view setOpenGLContext:window_info->context]; + } + + // Inform the view of its parent window info; used to register for window-close callbacks + [window_info->view setParent:window_info]; + + [window_info->window setContentView:window_info->view]; + [window_info->window makeKeyAndOrderFront:[NSApplication sharedApplication]]; + [window_info->window makeFirstResponder:window_info->view]; + [window_info->window setReleasedWhenClosed:YES]; + [window_info->window setInitialFirstResponder:window_info->view]; + + if (window_info->window_options != NSApplicationPresentationDefault) { + printf("Non-default\n"); fflush(stdout); + [[NSApplication sharedApplication] setPresentationOptions:window_info->window_options]; + } + + peer_info->window_info = window_info; + + return window_handle; +} + JNIEXPORT jint JNICALL Java_org_lwjgl_DefaultSysImplementation_getJNIVersion (JNIEnv *env, jobject ignored) { return org_lwjgl_MacOSXSysImplementation_JNI_VERSION; @@ -69,10 +484,30 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_setGammaRamp(JNIEnv * } } -JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nHideUI(JNIEnv *env, jobject this, jboolean hide) { - if (hide == JNI_TRUE) { - SetSystemUIMode(kUIModeContentSuppressed, 0); - } else { - SetSystemUIMode(kUIModeNormal, 0); - } +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nHideUI(JNIEnv *env, jobject this, jobject window_handle, jboolean hide) { + if (window_handle == NULL) { + printf("Window handle is null\n"); + return; + } + + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + if(floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) { + NSApplicationPresentationOptions options = NSApplicationPresentationDefault; + if (hide == JNI_TRUE) { + options = NSApplicationPresentationFullScreen; + options |= NSApplicationPresentationHideDock; + options |= NSApplicationPresentationHideMenuBar; + } + printf("Setting options\n"); + window_info->window_options = options; + if (window_info->window != nil) { + [[NSApplication sharedApplication] setPresentationOptions:options]; + } + } else { + if (hide == JNI_TRUE) { + SetSystemUIMode(kUIModeContentSuppressed, 0); + } else { + SetSystemUIMode(kUIModeNormal, 0); + } + } } diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m index 1593fb0d..3c982b8d 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m @@ -42,217 +42,17 @@ #import #include -#include -#include "awt_tools.h" #include "org_lwjgl_opengl_MacOSXCanvasPeerInfo.h" #include "context.h" #include "common_tools.h" -@interface AttachLayerOnMainThread : NSObject { - MacOSXPeerInfo *peer_info; - JAWT_MacOSXDrawingSurfaceInfo *macosx_dsi; -} - -- (void) attachLayer; - -- (MacOSXPeerInfo*) peer_info; -- (JAWT_MacOSXDrawingSurfaceInfo) macosx_dsi; - -- (void) setPeer_info: (MacOSXPeerInfo*)input; -- (void) setMacosx_dsi: (JAWT_MacOSXDrawingSurfaceInfo*)input; - -@end - JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle -(JNIEnv *env, jclass clazz, jobject lock_buffer_handle, jobject peer_info_handle, jboolean allowCALayer) { +(JNIEnv *env, jclass clazz, jobject peer_info_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); - AWTSurfaceLock *surface = (AWTSurfaceLock *)(*env)->GetDirectBufferAddress(env, lock_buffer_handle); - JAWT_MacOSXDrawingSurfaceInfo *macosx_dsi = (JAWT_MacOSXDrawingSurfaceInfo *)surface->dsi->platformInfo; - - if (allowCALayer) { - // check for CALayer support - if(surface->awt.version & 0x80000000) { //JAWT_MACOSX_USE_CALAYER) { - jint width = surface->dsi->bounds.width; - jint height = surface->dsi->bounds.height; - - if(peer_info->pbuffer == NULL || peer_info->window || width != [peer_info->pbuffer pixelsWide] || height != [peer_info->pbuffer pixelsHigh]) { - if(peer_info->pbuffer != NULL) { - [peer_info->pbuffer release]; - } - - // make pbuffer - NSOpenGLPixelBuffer *pbuffer = nil; - NSLog(@"Make pbuffer: %d x %d", width, height); - pbuffer = [[NSOpenGLPixelBuffer alloc] initWithTextureTarget:GL_TEXTURE_RECTANGLE_EXT - textureInternalFormat:GL_RGBA - textureMaxMipMapLevel:0 - pixelsWide:width - pixelsHigh:height]; - - peer_info->pbuffer = pbuffer; - peer_info->window = false; - peer_info->canDrawGL = true; - } - - if (macosx_dsi != NULL) { - - AttachLayerOnMainThread *attachLayerOnMainThread = [[AttachLayerOnMainThread new] autorelease]; - attachLayerOnMainThread.peer_info = peer_info; - attachLayerOnMainThread.macosx_dsi = macosx_dsi; - - [JNFRunLoop performOnMainThread:@selector(attachLayer) - on:attachLayerOnMainThread - withObject:nil - waitUntilDone:YES]; - } - - [pool release]; - return; - } - } - - peer_info->nsview = macosx_dsi->cocoaViewRef; - peer_info->window = true; + + peer_info->isWindowed = true; [pool release]; } - -@interface PBufferGLLayer : NSOpenGLLayer { - MacOSXPeerInfo *peer_info; - GLuint textureID; -} - -- (MacOSXPeerInfo*) peer_info; -- (GLuint) textureID; - -- (void) setPeer_info: (MacOSXPeerInfo*)input; -- (void) setTextureID: (GLuint)input; - -@end - -// Object class to CALayer on AppKit Thread -@implementation AttachLayerOnMainThread - -- (void) attachLayer { - // attach the "root layer" to the AWT Canvas surface layers - id surfaceLayers = (id )macosx_dsi;//dsi->platformInfo; - if(surfaceLayers.layer == NULL) { - PBufferGLLayer *caGLLayer = [[PBufferGLLayer new] autorelease]; - caGLLayer.peer_info = peer_info; - caGLLayer.asynchronous = YES; - caGLLayer.needsDisplayOnBoundsChange = YES; - caGLLayer.opaque = YES; - surfaceLayers.layer = caGLLayer; - } -} - -- (MacOSXPeerInfo*) peer_info { - return peer_info; -} - -- (JAWT_MacOSXDrawingSurfaceInfo*) macosx_dsi { - return macosx_dsi; -} - -- (void) setPeer_info: (MacOSXPeerInfo*)input { - peer_info = input; -} - -- (void) setMacosx_dsi: (JAWT_MacOSXDrawingSurfaceInfo*)input { - macosx_dsi = input; -} - -@end - -// rotates a red square when asked to draw -@implementation PBufferGLLayer - -// override to draw custom GL content --(void)drawInCGLContext:(CGLContextObj)glContext - pixelFormat:(CGLPixelFormatObj)pixelFormat - forLayerTime:(CFTimeInterval)timeInterval - displayTime:(const CVTimeStamp *)timeStamp { - - if(!peer_info || !peer_info->pbuffer) { - return; - } - - peer_info->canDrawGL = false; - - NSOpenGLPixelBuffer *pbuffer = self.peer_info->pbuffer; - - // set the current context - CGLSetCurrentContext(glContext); - - GLsizei width = [pbuffer pixelsWide]; - GLsizei height = [pbuffer pixelsHigh]; - - if(textureID == 0) { - glGenTextures(1, &textureID); - } - glBindTexture(GL_TEXTURE_RECTANGLE_EXT, self.textureID); - CGLTexImagePBuffer(glContext,[pbuffer CGLPBufferObj], GL_FRONT); - - glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - - glEnable(GL_TEXTURE_RECTANGLE_EXT); - - static GLfloat verts[] = { - -1.0, -1.0, - -1.0, 1.0, - 1.0, 1.0, - 1.0, -1.0 - }; - - GLfloat tex[] = { - 0.0, 0.0, - 0.0, height, - width, height, - width, 0.0 - }; - - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glVertexPointer(2, GL_FLOAT, 0, verts); - glTexCoordPointer(2, GL_FLOAT, 0, tex); - - glDrawArrays(GL_QUADS, 0, 4); - - glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - - glDisable(GL_TEXTURE_RECTANGLE_EXT); - - // call super to finalize the drawing - by default all it does is call glFlush() - [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp]; -} - --(BOOL)canDrawInCGLContext:(CGLContextObj)glContext - pixelFormat:(CGLPixelFormatObj)pixelFormat - forLayerTime:(CFTimeInterval)timeInterval - displayTime:(const CVTimeStamp *)timeStamp { - return (peer_info->canDrawGL && !peer_info->window) ? YES : NO; -} - -- (MacOSXPeerInfo*) peer_info { - return peer_info; -} - -- (GLuint) textureID { - return textureID; -} - -- (void) setPeer_info: (MacOSXPeerInfo*)input { - peer_info = input; -} - -- (void) setTextureID: (GLuint)input { - textureID = input; -} - -@end \ No newline at end of file diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m index aecebe1c..36d40565 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m @@ -47,7 +47,7 @@ #import "common_tools.h" typedef struct { - NSOpenGLContext *context; + NSOpenGLContext *context; MacOSXPeerInfo *peer_info; } MacOSXContext; @@ -57,8 +57,9 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nCre MacOSXPeerInfo *peer_info; MacOSXContext *shared_context_info; MacOSXContext *context_info; - NSOpenGLContext *context; - NSOpenGLContext *shared_context = NULL; + NSOpenGLContext *context; + NSOpenGLContext *shared_context; + printf("nCreate\n"); jobject context_handle = newJavaManagedByteBuffer(env, sizeof(MacOSXContext)); if (context_handle == NULL) { throwException(env, "Could not create handle buffer"); @@ -67,16 +68,19 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nCre peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); if (shared_context_handle != NULL) { shared_context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, shared_context_handle); - shared_context = shared_context_info->context; - } - context = [[NSOpenGLContext alloc] initWithFormat:peer_info->pixel_format shareContext:shared_context]; - if (context == NULL) { - throwException(env, "Could not create context"); - return NULL; + shared_context = shared_context_info->context; } + context = [[NSOpenGLContext alloc] initWithFormat:peer_info->pixel_format shareContext:shared_context]; + if (context == NULL) { + throwException(env, "Could not create context"); + return NULL; + } + [peer_info->window_info->view setOpenGLContext:context]; + peer_info->window_info->context = context; context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); - context_info->context = context; + context_info->context = context; context_info->peer_info = peer_info; + [pool release]; return context_handle; } @@ -84,19 +88,19 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nCre JNIEXPORT jlong JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_getCGLShareGroup (JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); - CGLContextObj cgl_context = [context_info->context CGLContextObj]; + MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; + CGLContextObj cgl_context = [[peer_info->window_info->view openGLContext] CGLContextObj]; CGLShareGroupObj share_group = CGLGetShareGroup(cgl_context); [pool release]; - return share_group; + return (jlong)share_group; } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nSwapBuffers (JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); - [context_info->context flushBuffer]; - context_info->peer_info->canDrawGL = true; + MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; + [[peer_info->window_info->view openGLContext] flushBuffer]; + peer_info->canDrawGL = true; [pool release]; } @@ -104,37 +108,35 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nSwapBu JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nUpdate (JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); - [context_info->context update]; - context_info->peer_info->canDrawGL = true; + MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; + [[peer_info->window_info->view openGLContext] update]; + peer_info->canDrawGL = true; [pool release]; } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_clearDrawable (JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); - [context_info->context clearDrawable]; + MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; + printf("clearDrawable\n"); + [[peer_info->window_info->view openGLContext] clearDrawable]; [pool release]; } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nReleaseCurrentContext (JNIEnv *env, jclass clazz) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + printf("nReleaseCurrentContexta\n"); [NSOpenGLContext clearCurrentContext]; [pool release]; } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_setView - (JNIEnv *env, jclass clazz, jobject peer_info_handle, jobject context_handle) { + (JNIEnv *env, jclass clazz, jobject peer_info_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); - MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); - if (peer_info->window) { - [context_info->context setView: peer_info->nsview]; - } else { - [context_info->context setPixelBuffer:peer_info->pbuffer cubeMapFace:0 mipMapLevel:0 currentVirtualScreen:0]; - } + MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); + printf("setView\n"); + [[peer_info->window_info->view openGLContext] setView: peer_info->window_info->view]; peer_info->canDrawGL = true; [pool release]; } @@ -142,16 +144,17 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_setView JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nMakeCurrent (JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); - [context_info->context makeCurrentContext]; + MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; + printf("nMakeCurrent\n"); + [[peer_info->window_info->view openGLContext] makeCurrentContext]; [pool release]; } JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nIsCurrent (JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); - bool result = context_info->context == [NSOpenGLContext currentContext]; + MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; + bool result = [peer_info->window_info->view openGLContext] == [NSOpenGLContext currentContext]; [pool release]; return result ? JNI_TRUE : JNI_FALSE; } @@ -159,17 +162,18 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nIs JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nSetSwapInterval (JNIEnv *env, jclass clazz, jobject context_handle, jint int_value) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); + MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; GLint value = int_value; - [context_info->context setValues:&value forParameter:NSOpenGLCPSwapInterval]; + [[peer_info->window_info->view openGLContext] setValues:&value forParameter:NSOpenGLCPSwapInterval]; [pool release]; } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nDestroy (JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); - [context_info->context clearDrawable]; - [context_info->context release]; + MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; + printf("nDestroy\n"); + [[peer_info->window_info->view openGLContext] clearDrawable]; + [[peer_info->window_info->view openGLContext] release]; [pool release]; } diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeKeyboard.m b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeKeyboard.m new file mode 100644 index 00000000..aec26e84 --- /dev/null +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeKeyboard.m @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2002-2012 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_MacOSXNativeKeyboard.m 3055 2012-08-29 0:46:00Z mojang $ + * + * Mac OS X native keyboard functions. + * + * @author mojang + * @version $Revision: 3055 $ + */ + +#import +#import +#import +#import +#import "common_tools.h" +#import "org_lwjgl_opengl_MacOSXNativeKeyboard.h" +#import "context.h" + +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeKeyboard_nRegisterKeyListener(JNIEnv *env, jobject this, jobject window_handle) { + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + window_info->jkeyboard = (*env)->NewGlobalRef(env, this); +} + +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeKeyboard_nUnregisterKeyListener(JNIEnv *env, jobject this, jobject window_handle) { + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + window_info->jkeyboard = NULL; +} diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m new file mode 100644 index 00000000..db774a89 --- /dev/null +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2002-2012 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_MacOSXNativeKeyboard.m 3055 2012-08-29 0:46:00Z mojang $ + * + * Mac OS X native keyboard functions. + * + * @author mojang + * @version $Revision: 3055 $ + */ + +#import +#import +#import +#import +#import "common_tools.h" +#import "org_lwjgl_opengl_MacOSXNativeMouse.h" +#import "context.h" + +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nGrabMouse(JNIEnv *env, jclass this, jboolean grab) { + CGAssociateMouseAndMouseCursorPosition(grab == JNI_TRUE ? FALSE : TRUE); + if (grab) + CGDisplayHideCursor(kCGDirectMainDisplay); + else + CGDisplayShowCursor(kCGDirectMainDisplay); +} + +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nWarpCursor(JNIEnv *env, jclass this, jobject window_handle, jint x, jint y) { + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + NSPoint point = NSMakePoint(x, y); + point = [window_info->view convertPoint:point fromView:window_info->view]; + CGPoint p; + p.x = point.x; + p.y = point.y; + CGWarpMouseCursorPosition(p); +} + +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nRegisterMouseListener(JNIEnv *env, jobject _this, jobject window_handle) { + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + [window_info->window setAcceptsMouseMovedEvents:YES]; + window_info->jmouse = (*env)->NewGlobalRef(env, _this); +} + +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nUnregisterMouseListener(JNIEnv *env, jobject this, jobject window_handle) { + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + [window_info->window setAcceptsMouseMovedEvents:NO]; + window_info->jmouse = nil; +} diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXPbufferPeerInfo.m b/src/native/macosx/org_lwjgl_opengl_MacOSXPbufferPeerInfo.m index 46fa5655..e1e41dff 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXPbufferPeerInfo.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXPbufferPeerInfo.m @@ -65,7 +65,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXPbufferPeerInfo_nCreate(JNIEn } MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); peer_info->pbuffer = pbuffer; - peer_info->window = false; + peer_info->isWindowed = false; [pool release]; } From 2c811ac289b201588e7388e0561f3a238da09db1 Mon Sep 17 00:00:00 2001 From: kappa1 Date: Thu, 1 Nov 2012 22:21:23 +0000 Subject: [PATCH 002/111] Updated macosx_ant build.xml, removed PPC support, bumped the minimum OS X SDK for 32/64bit native to SDK 10.5. Commented out some code in Display.m that required SDK 10.7+, namely NSApplicationPresentationFullScreen, etc --- platform_build/macosx_ant/build.xml | 18 +++++++++--------- src/native/macosx/org_lwjgl_opengl_Display.m | 9 +++++---- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/platform_build/macosx_ant/build.xml b/platform_build/macosx_ant/build.xml index b1696a34..399947c8 100644 --- a/platform_build/macosx_ant/build.xml +++ b/platform_build/macosx_ant/build.xml @@ -3,7 +3,7 @@ - + @@ -12,7 +12,7 @@ - + @@ -46,21 +46,21 @@ - + - + - + @@ -72,12 +72,12 @@ - + @@ -95,7 +95,7 @@ - + diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 13955657..058ce4c8 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -315,7 +315,7 @@ static NSOpenGLPixelFormat *default_format = nil; } - (void)scrollWheel:(NSEvent *)event { - JNIEnv *env = attachCurrentThread(); + /*JNIEnv *env = attachCurrentThread(); if (env == nil || event == nil || _parent == nil) { return; } @@ -328,6 +328,7 @@ static NSOpenGLPixelFormat *default_format = nil; jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); NSPoint loc = [self convertPoint:[event locationInWindow] toView:self]; (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], dz, time); + */ } - (void)viewDidMoveToWindow @@ -492,9 +493,9 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nHideUI(JNIEnv *env, MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); if(floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) { - NSApplicationPresentationOptions options = NSApplicationPresentationDefault; + /*NSApplicationPresentationOptions options = NSApplicationPresentationDefault; if (hide == JNI_TRUE) { - options = NSApplicationPresentationFullScreen; + options = NSApplicationPresentationFullScreen; // this requires OS X 10.7+ to compile options |= NSApplicationPresentationHideDock; options |= NSApplicationPresentationHideMenuBar; } @@ -502,7 +503,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nHideUI(JNIEnv *env, window_info->window_options = options; if (window_info->window != nil) { [[NSApplication sharedApplication] setPresentationOptions:options]; - } + }*/ } else { if (hide == JNI_TRUE) { SetSystemUIMode(kUIModeContentSuppressed, 0); From 460b6f05dd36e301ed1ea9538e2e1c440dfcd118 Mon Sep 17 00:00:00 2001 From: kappa1 Date: Thu, 1 Nov 2012 22:39:22 +0000 Subject: [PATCH 003/111] Fix memory leaks by adding an NSAutoreleasePool when creating a windows and draining it on window destroy --- src/native/macosx/org_lwjgl_opengl_Display.m | 35 +++++++++++--------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 058ce4c8..e121110b 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -53,6 +53,8 @@ static NSOpenGLPixelFormat *default_format = nil; +static NSAutoreleasePool *pool; + @implementation MacOSXKeyableWindow - (BOOL)canBecomeKeyWindow; { @@ -390,21 +392,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nSetResizable(JNIEnv [window_info->window setStyleMask:style_mask]; } -JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv *env, jobject this, jobject window_handle) { - MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - - if (window_info->window != nil) { - [window_info->window close]; - } - window_info->window = nil; - - if (window_info->view != nil) { - [window_info->view release]; - } - window_info->view = nil; - //[window_info->window release]; -} - JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nSetTitle(JNIEnv *env, jobject this, jobject window_handle, jobject title_buffer) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); const char *title_cstr = (const char *)(*env)->GetDirectBufferAddress(env, title_buffer); @@ -421,6 +408,8 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE return NULL; } } + + pool = [[NSAutoreleasePool alloc] init]; MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); @@ -467,6 +456,22 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE return window_handle; } +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv *env, jobject this, jobject window_handle) { + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + + if (window_info->window != nil) { + [window_info->window close]; + } + window_info->window = nil; + + if (window_info->view != nil) { + [window_info->view release]; + } + window_info->view = nil; + //[window_info->window release]; + [pool drain]; +} + JNIEXPORT jint JNICALL Java_org_lwjgl_DefaultSysImplementation_getJNIVersion (JNIEnv *env, jobject ignored) { return org_lwjgl_MacOSXSysImplementation_JNI_VERSION; From 94c23298c8685a5af556c07a458bf97061fffca9 Mon Sep 17 00:00:00 2001 From: kappa1 Date: Sat, 3 Nov 2012 18:06:08 +0000 Subject: [PATCH 004/111] Fix a native crash on startup of some applications --- src/native/macosx/org_lwjgl_opengl_Display.m | 6 +++--- .../macosx/org_lwjgl_opengl_MacOSXContextImplementation.m | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index e121110b..5a945b95 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -497,7 +497,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nHideUI(JNIEnv *env, } MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - if(floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) { + //if(floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) { /*NSApplicationPresentationOptions options = NSApplicationPresentationDefault; if (hide == JNI_TRUE) { options = NSApplicationPresentationFullScreen; // this requires OS X 10.7+ to compile @@ -509,11 +509,11 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nHideUI(JNIEnv *env, if (window_info->window != nil) { [[NSApplication sharedApplication] setPresentationOptions:options]; }*/ - } else { + //} else { if (hide == JNI_TRUE) { SetSystemUIMode(kUIModeContentSuppressed, 0); } else { SetSystemUIMode(kUIModeNormal, 0); } - } + //} } diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m index 36d40565..2bc6c909 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m @@ -58,7 +58,7 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nCre MacOSXContext *shared_context_info; MacOSXContext *context_info; NSOpenGLContext *context; - NSOpenGLContext *shared_context; + NSOpenGLContext *shared_context = NULL; printf("nCreate\n"); jobject context_handle = newJavaManagedByteBuffer(env, sizeof(MacOSXContext)); if (context_handle == NULL) { From 0c4008c885a82089e7c0d1dd91fce5268dec19e2 Mon Sep 17 00:00:00 2001 From: kappa1 Date: Sun, 4 Nov 2012 20:04:30 +0000 Subject: [PATCH 005/111] Remove unused dependency on JavaNativeFoundation --- src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m index 3c982b8d..73518e66 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m @@ -39,7 +39,7 @@ */ #import -#import +//#import #include #include "org_lwjgl_opengl_MacOSXCanvasPeerInfo.h" From 4eaea5850c43aae94af7aff494d3c166eda0ad8e Mon Sep 17 00:00:00 2001 From: kappa1 Date: Sun, 4 Nov 2012 20:11:03 +0000 Subject: [PATCH 006/111] Tiny Clean up and remove some more references to JNF --- platform_build/macosx_ant/build.xml | 23 +++----------------- src/native/macosx/org_lwjgl_opengl_Display.m | 19 ++++++++-------- 2 files changed, 12 insertions(+), 30 deletions(-) diff --git a/platform_build/macosx_ant/build.xml b/platform_build/macosx_ant/build.xml index 399947c8..3b32645a 100644 --- a/platform_build/macosx_ant/build.xml +++ b/platform_build/macosx_ant/build.xml @@ -3,7 +3,6 @@ - @@ -12,7 +11,6 @@ - @@ -20,7 +18,7 @@ - + @@ -35,7 +33,7 @@ - + @@ -46,16 +44,8 @@ - - @@ -72,17 +62,11 @@ - - + @@ -95,7 +79,6 @@ - diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 5a945b95..8e96b522 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -317,20 +317,19 @@ static NSAutoreleasePool *pool; } - (void)scrollWheel:(NSEvent *)event { - /*JNIEnv *env = attachCurrentThread(); + JNIEnv *env = attachCurrentThread(); if (env == nil || event == nil || _parent == nil) { return; } long time = [event timestamp] * 1000000000; - float dz = [event scrollingDeltaY]; - if (![event hasPreciseScrollingDeltas]) { - dz *= 12; // or so - } - jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); - jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); - NSPoint loc = [self convertPoint:[event locationInWindow] toView:self]; - (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], dz, time); - */ + //float dz = [event scrollingDeltaY]; // An OS X 10.7+ API + //if (![event hasPreciseScrollingDeltas]) { // Also an OS X 10.7 API + // dz *= 12; // or so + //} + //jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); + //jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); + //NSPoint loc = [self convertPoint:[event locationInWindow] toView:self]; + //(*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], dz, time); } - (void)viewDidMoveToWindow From bb8045b47136107ef8a608a448bca0126773f644 Mon Sep 17 00:00:00 2001 From: kappa1 Date: Mon, 5 Nov 2012 23:17:34 +0000 Subject: [PATCH 007/111] Added support to the OS X build.xml for Xcode location discovery, Mac OS SDK discovery, JavaVM.framework discovery and gcc vs gcc-4.2 discovery, special thanks to MC78 for providing this patch --- platform_build/macosx_ant/build.xml | 87 +---------------------------- 1 file changed, 1 insertion(+), 86 deletions(-) diff --git a/platform_build/macosx_ant/build.xml b/platform_build/macosx_ant/build.xml index 3b32645a..d755c80d 100644 --- a/platform_build/macosx_ant/build.xml +++ b/platform_build/macosx_ant/build.xml @@ -1,86 +1 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + \ No newline at end of file From ce6f4b71f0d50d1f01713855fda42d0da9c0b79b Mon Sep 17 00:00:00 2001 From: kappa1 Date: Mon, 5 Nov 2012 23:19:46 +0000 Subject: [PATCH 008/111] Remove unneeded old hack for OS X 10.3 in the mac Sys class implementation which loaded the AWT Toolkit --- src/java/org/lwjgl/MacOSXSysImplementation.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/java/org/lwjgl/MacOSXSysImplementation.java b/src/java/org/lwjgl/MacOSXSysImplementation.java index 54506609..dbb4b059 100644 --- a/src/java/org/lwjgl/MacOSXSysImplementation.java +++ b/src/java/org/lwjgl/MacOSXSysImplementation.java @@ -44,11 +44,6 @@ import com.apple.eio.FileManager; final class MacOSXSysImplementation extends J2SESysImplementation { private static final int JNI_VERSION = 23; - static { - // Make sure AWT is properly initialized. This avoids hangs on Mac OS X 10.3 - Toolkit.getDefaultToolkit(); - } - public int getRequiredJNIVersion() { return JNI_VERSION; } From 9655b45e55fd16e97343ca583ee51852f7991d23 Mon Sep 17 00:00:00 2001 From: kappa1 Date: Tue, 6 Nov 2012 00:34:36 +0000 Subject: [PATCH 009/111] remove unused AWT import --- src/java/org/lwjgl/MacOSXSysImplementation.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/java/org/lwjgl/MacOSXSysImplementation.java b/src/java/org/lwjgl/MacOSXSysImplementation.java index dbb4b059..55fa6cef 100644 --- a/src/java/org/lwjgl/MacOSXSysImplementation.java +++ b/src/java/org/lwjgl/MacOSXSysImplementation.java @@ -31,8 +31,6 @@ */ package org.lwjgl; -import java.awt.Toolkit; - import com.apple.eio.FileManager; /** From 3e96cded2bb9962241aa5890ba560d08939f1c84 Mon Sep 17 00:00:00 2001 From: kappa1 Date: Tue, 6 Nov 2012 21:45:45 +0000 Subject: [PATCH 010/111] upload mc78's latest build.xml fixes, gets rid of the depreciated warnings --- platform_build/macosx_ant/build.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform_build/macosx_ant/build.xml b/platform_build/macosx_ant/build.xml index d755c80d..0cde0b42 100644 --- a/platform_build/macosx_ant/build.xml +++ b/platform_build/macosx_ant/build.xml @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file From 7291d72274ff2998e1a825d641ed9a5589065319 Mon Sep 17 00:00:00 2001 From: kappa1 Date: Tue, 6 Nov 2012 21:55:11 +0000 Subject: [PATCH 011/111] Fix main build file to generate MacOSXNativeKeyboard and MacOSXNativeMouse native headers --- build.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build.xml b/build.xml index 0efd2524..3d2247d3 100644 --- a/build.xml +++ b/build.xml @@ -322,6 +322,8 @@ + + From 49f92189ece184391cc454999044d5d302f98ccb Mon Sep 17 00:00:00 2001 From: kappa1 Date: Sat, 10 Nov 2012 00:42:34 +0000 Subject: [PATCH 012/111] Switch to using the correct Cocoa method for Display.isCloseRequested() --- src/native/macosx/org_lwjgl_opengl_Display.m | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 8e96b522..d38254fa 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -78,16 +78,14 @@ static NSAutoreleasePool *pool; return default_format; } -- (void) windowWillClose:(NSNotification *)notification -{ - MacOSXKeyableWindow *closingWindow = [notification object]; - - if (_parent != nil && closingWindow == _parent->window) { +- (BOOL)windowShouldClose:(id)sender { + if (_parent != nil) { JNIEnv *env = attachCurrentThread(); jclass display_class = (*env)->GetObjectClass(env, _parent->jdisplay); jmethodID close_callback = (*env)->GetMethodID(env, display_class, "doHandleQuit", "()V"); (*env)->CallVoidMethod(env, _parent->jdisplay, close_callback); } + return NO; } - (id)initWithFrame:(NSRect)frameRect pixelFormat:(NSOpenGLPixelFormat*)format @@ -163,18 +161,10 @@ static NSAutoreleasePool *pool; } - (void)setParent:(MacOSXWindowInfo*)parent { - // Un-register for native window close events if we have a parent window already - if (_parent != nil) { - [[NSNotificationCenter defaultCenter] removeObserver:self - name:NSWindowWillCloseNotification - object:_parent->window]; - } _parent = parent; - // Register for native window close events if we now have a parent window + // Set this NSView as delegate to get native window close events for windowShouldClose method if (_parent != nil) { - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(windowWillClose:) name:NSWindowWillCloseNotification - object:_parent->window]; + [_parent->window setDelegate:self]; } } From f7012a3020ba5a0640c93b79c2cc999c9e895995 Mon Sep 17 00:00:00 2001 From: kappa1 Date: Sun, 11 Nov 2012 15:48:08 +0000 Subject: [PATCH 013/111] Run some Cocoa methods in nCreateWindow on the main thread (Thread-0) instead of the JNI thread. --- src/native/macosx/org_lwjgl_opengl_Display.m | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index d38254fa..801d25d1 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -430,10 +430,14 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE [window_info->view setParent:window_info]; [window_info->window setContentView:window_info->view]; - [window_info->window makeKeyAndOrderFront:[NSApplication sharedApplication]]; - [window_info->window makeFirstResponder:window_info->view]; - [window_info->window setReleasedWhenClosed:YES]; - [window_info->window setInitialFirstResponder:window_info->view]; + //[window_info->window makeKeyAndOrderFront:[NSApplication sharedApplication]]; + //[window_info->window makeFirstResponder:window_info->view]; + //[window_info->window setReleasedWhenClosed:YES]; + //[window_info->window setInitialFirstResponder:window_info->view]; + [window_info->window performSelectorOnMainThread:@selector(makeKeyAndOrderFront:) withObject:[NSApplication sharedApplication] waitUntilDone:NO]; + [window_info->window performSelectorOnMainThread:@selector(makeFirstResponder:) withObject:window_info->view waitUntilDone:NO]; + [window_info->window performSelectorOnMainThread:@selector(setReleasedWhenClosed:) withObject:window_info->window waitUntilDone:NO]; + [window_info->window performSelectorOnMainThread:@selector(setInitialFirstResponder:) withObject:window_info->view waitUntilDone:NO]; if (window_info->window_options != NSApplicationPresentationDefault) { printf("Non-default\n"); fflush(stdout); From a749ad5c1071cb8331e50ab8c99a36bff673e88b Mon Sep 17 00:00:00 2001 From: kappa1 Date: Sun, 11 Nov 2012 18:43:14 +0000 Subject: [PATCH 014/111] implement missing functionality for Display.getWidth() and Display.getHeight() not updating correctly on resize --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 10 +++++++--- src/native/macosx/org_lwjgl_opengl_Display.m | 12 ++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 65b5f900..a83a6657 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -121,6 +121,10 @@ final class MacOSXDisplay implements DisplayImplementation { private native void nResizeWindow(ByteBuffer window_handle, int x, int y, int width, int height); private native boolean nWasResized(ByteBuffer window_handle); + + private native int nGetWidth(ByteBuffer window_handle); + + private native int nGetHeight(ByteBuffer window_handle); private static boolean isUndecorated() { return Display.getPrivilegedBoolean("org.lwjgl.opengl.Window.undecorated"); @@ -561,11 +565,11 @@ final class MacOSXDisplay implements DisplayImplementation { } public int getWidth() { - return width; + return nGetWidth(window); } - + public int getHeight() { - return height; + return nGetHeight(window); } public boolean isInsideWindow() { diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 801d25d1..8f4d355c 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -370,6 +370,18 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nWasResized(JNIEn return was_resized; } +JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nGetWidth(JNIEnv *env, jobject this, jobject window_handle) { + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + jint width = window_info->display_rect.size.width; + return width; +} + +JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nGetHeight(JNIEnv *env, jobject this, jobject window_handle) { + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + jint height = window_info->display_rect.size.height; + return height; +} + JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nSetResizable(JNIEnv *env, jobject this, jobject window_handle, jboolean resizable) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); NSUInteger style_mask = [window_info->window styleMask]; From 7bbfcadcef58343b0015e14528e5757a7764f5b4 Mon Sep 17 00:00:00 2001 From: kappa1 Date: Sun, 11 Nov 2012 19:03:50 +0000 Subject: [PATCH 015/111] fix resizing when using Display.setParent() --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index a83a6657..42efd4b7 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -583,11 +583,7 @@ final class MacOSXDisplay implements DisplayImplementation { } public boolean wasResized() { - if (native_mode) { - return nWasResized(window); - } else { - return canvas_listener.wasResized(); - } + return nWasResized(window); } } From 4f4cb832afe25946258624fdcb361dc00c30e20e Mon Sep 17 00:00:00 2001 From: kappa1 Date: Tue, 13 Nov 2012 21:13:11 +0000 Subject: [PATCH 016/111] Cleaned up code by removing unneeded java classes MacOSXFrame and MacOSXMouseEventQueue and native code by removing mouse.c --- build.xml | 1 - src/java/org/lwjgl/opengl/MacOSXDisplay.java | 151 +++-------- src/java/org/lwjgl/opengl/MacOSXFrame.java | 238 ------------------ .../lwjgl/opengl/MacOSXMouseEventQueue.java | 106 -------- .../org/lwjgl/opengl/MacOSXNativeMouse.java | 4 + src/native/macosx/org_lwjgl_input_Mouse.c | 73 ------ 6 files changed, 36 insertions(+), 537 deletions(-) delete mode 100644 src/java/org/lwjgl/opengl/MacOSXFrame.java delete mode 100644 src/java/org/lwjgl/opengl/MacOSXMouseEventQueue.java delete mode 100644 src/native/macosx/org_lwjgl_input_Mouse.c diff --git a/build.xml b/build.xml index 3d2247d3..cd03ab97 100644 --- a/build.xml +++ b/build.xml @@ -316,7 +316,6 @@ - diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 42efd4b7..eaefb87a 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -70,7 +70,7 @@ final class MacOSXDisplay implements DisplayImplementation { private MacOSXCanvasListener canvas_listener; private Canvas canvas; private Robot robot; - private MacOSXMouseEventQueue mouse_queue; + //private MacOSXMouseEventQueue mouse_queue; private KeyboardEventQueue keyboard_queue; private java.awt.DisplayMode requested_mode; @@ -84,30 +84,10 @@ final class MacOSXDisplay implements DisplayImplementation { private int width; private int height; - /* Whether we're using a native window or an AWT canvas */ - private boolean native_mode; - - private boolean close_requested; + private boolean close_requested; MacOSXDisplay() { - try { - AccessController.doPrivileged(new PrivilegedExceptionAction() { - 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()); - } + } 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; @@ -142,7 +122,6 @@ final class MacOSXDisplay implements DisplayImplementation { 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(); @@ -165,15 +144,8 @@ final class MacOSXDisplay implements DisplayImplementation { public native void nDestroyWindow(ByteBuffer window_handle); public void destroyWindow() { - if (native_mode) { - nDestroyWindow(window); - } else { - if (canvas_listener != null) { - canvas_listener.disableListeners(); - canvas_listener = null; - } - } - hideUI(false); + nDestroyWindow(window); + hideUI(false); } public int getGammaRampLength() { @@ -213,11 +185,7 @@ final class MacOSXDisplay implements DisplayImplementation { } public void resetDisplayMode() { - if (!native_mode) { - if (getDevice().getFullScreenWindow() != null) - getDevice().setFullScreenWindow(null); - } - requested_mode = null; + requested_mode = null; restoreGamma(); } @@ -273,12 +241,8 @@ final class MacOSXDisplay implements DisplayImplementation { } public boolean isActive() { - if (native_mode) { - boolean ret = nIsFocused(window); - return ret; - } else { - return canvas.isFocusOwner(); - } + boolean ret = nIsFocused(window); + return ret; } public Canvas getCanvas() { @@ -300,10 +264,7 @@ final class MacOSXDisplay implements DisplayImplementation { private static final IntBuffer current_viewport = BufferUtils.createIntBuffer(16); 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 @@ -331,14 +292,6 @@ final class MacOSXDisplay implements DisplayImplementation { 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(); - } - */ } /** @@ -369,52 +322,29 @@ final class MacOSXDisplay implements DisplayImplementation { } public void createMouse() throws LWJGLException { - if (native_mode) { - mouse = new MacOSXNativeMouse(this, window); - mouse.register(); - } else { - mouse_queue = new MacOSXMouseEventQueue(canvas); - mouse_queue.register(); - } + mouse = new MacOSXNativeMouse(this, window); + mouse.register(); } 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; + //MacOSXMouseEventQueue.nGrabMouse(false); + + if (mouse != null) { + mouse.unregister(); } + mouse = null; } 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); - } + mouse.poll(coord_buffer, buttons_buffer); } public void readMouse(ByteBuffer buffer) { - if (native_mode) { - mouse.copyEvents(buffer); - } else { - mouse_queue.copyEvents(buffer); - } + mouse.copyEvents(buffer); } public void grabMouse(boolean grab) { - if (native_mode) { - mouse.setGrabbed(grab); - } else { - mouse_queue.setGrabbed(grab); - } + mouse.setGrabbed(grab); } public int getNativeCursorCapabilities() { @@ -422,7 +352,10 @@ final class MacOSXDisplay implements DisplayImplementation { } public void setCursorPosition(int x, int y) { - MacOSXMouseEventQueue.nWarpCursor(x, y); + if (mouse != null) { + mouse.warpCursor(x, y); + } + //MacOSXMouseEventQueue.nWarpCursor(x, y); } public void setNativeCursor(Object handle) throws LWJGLException { @@ -438,41 +371,23 @@ final class MacOSXDisplay implements DisplayImplementation { /* 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(); - } + this.keyboard = new MacOSXNativeKeyboard(window); + keyboard.register(); } public void destroyKeyboard() { - if (native_mode) { - if (keyboard != null) - keyboard.unregister(); - keyboard = null; - } else { - if (keyboard_queue != null) - keyboard_queue.unregister(); - keyboard_queue = null; - } + if (keyboard != null) { + keyboard.unregister(); + } + keyboard = null; } public void pollKeyboard(ByteBuffer keyDownBuffer) { - if (native_mode) { - keyboard.poll(keyDownBuffer); - } else { - keyboard_queue.poll(keyDownBuffer); - } + keyboard.poll(keyDownBuffer); } public void readKeyboard(ByteBuffer buffer) { - if (native_mode) { - keyboard.copyEvents(buffer); - } else { - keyboard_queue.copyEvents(buffer); - } + keyboard.copyEvents(buffer); } /** Native cursor handles */ @@ -577,9 +492,7 @@ final class MacOSXDisplay implements DisplayImplementation { } public void setResizable(boolean resizable) { - if (native_mode) { - nSetResizable(window, resizable); - } + nSetResizable(window, resizable); } public boolean wasResized() { diff --git a/src/java/org/lwjgl/opengl/MacOSXFrame.java b/src/java/org/lwjgl/opengl/MacOSXFrame.java deleted file mode 100644 index 18bdcfac..00000000 --- a/src/java/org/lwjgl/opengl/MacOSXFrame.java +++ /dev/null @@ -1,238 +0,0 @@ -/* - * Copyright (c) 2002-2008 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 Mac OS X AWT Frame. It contains thread safe - * methods to manipulateit from non-AWT threads - * @author elias_naur - */ - -import java.awt.BorderLayout; -import java.awt.Frame; -import java.awt.GraphicsDevice; -import java.awt.GraphicsEnvironment; -import java.awt.Insets; -import java.awt.Rectangle; -import java.awt.event.ComponentEvent; -import java.awt.event.ComponentListener; -import java.awt.event.WindowEvent; -import java.awt.event.WindowListener; -import java.security.AccessController; -import java.security.PrivilegedActionException; -import java.security.PrivilegedExceptionAction; - -import org.lwjgl.LWJGLException; - -final class MacOSXFrame extends Frame implements WindowListener, ComponentListener { - - private static final long serialVersionUID = -5823294716668988777L; - - private final MacOSXGLCanvas canvas; - private boolean close_requested; - - /* States */ - private Rectangle bounds; - private boolean active; - private boolean minimized; - private boolean should_warp_cursor; - private boolean should_release_cursor; - - MacOSXFrame(DisplayMode mode, final java.awt.DisplayMode requested_mode, boolean fullscreen, int x, int y) throws LWJGLException { - setResizable(Display.isResizable()); - addWindowListener(this); - addComponentListener(this); - canvas = new MacOSXGLCanvas(); - canvas.setFocusTraversalKeysEnabled(false); - add(canvas, BorderLayout.CENTER); - boolean undecorated = Display.getPrivilegedBoolean("org.lwjgl.opengl.Window.undecorated"); - setUndecorated(fullscreen || undecorated); - if ( fullscreen ) { - try { - AccessController.doPrivileged(new PrivilegedExceptionAction() { - public Object run() throws Exception { - getDevice().setFullScreenWindow(MacOSXFrame.this); - getDevice().setDisplayMode(requested_mode); - java.awt.DisplayMode real_mode = getDevice().getDisplayMode(); - /** For some strange reason, the display mode is sometimes silently capped even though the mode is reported as supported */ - if ( requested_mode.getWidth() != real_mode.getWidth() || requested_mode.getHeight() != real_mode.getHeight() ) { - getDevice().setFullScreenWindow(null); - if (isDisplayable()) - dispose(); - throw new LWJGLException("AWT capped mode: requested mode = " + requested_mode.getWidth() + "x" + requested_mode.getHeight() + - " but got " + real_mode.getWidth() + " " + real_mode.getHeight()); - } - return null; - } - }); - } catch (PrivilegedActionException e) { - throw new LWJGLException(e); - } - } - pack(); - resize(x, y, mode.getWidth(), mode.getHeight()); - setVisible(true); - requestFocus(); - canvas.requestFocus(); - updateBounds(); - } - - public void resize(int x, int y, int width, int height) { - Insets insets = getInsets(); - setBounds(x, y, width + insets.left + insets.right, height + insets.top + insets.bottom); - } - - public int getWidth() { - Insets insets = getInsets(); - return super.getWidth() - insets.left - insets.right; - } - - public int getHeight() { - Insets insets = getInsets(); - return super.getHeight() - insets.top - insets.bottom; - } - - public Rectangle syncGetBounds() { - synchronized ( this ) { - return bounds; - } - } - - public void componentShown(ComponentEvent e) { - } - - public void componentHidden(ComponentEvent e) { - } - - private void updateBounds() { - synchronized ( this ) { - bounds = getBounds(); - } - } - - public void componentResized(ComponentEvent e) { - updateBounds(); - } - - public void componentMoved(ComponentEvent e) { - updateBounds(); - } - - public static GraphicsDevice getDevice() { - GraphicsEnvironment g_env = GraphicsEnvironment.getLocalGraphicsEnvironment(); - GraphicsDevice device = g_env.getDefaultScreenDevice(); - return device; - } - - public void windowIconified(WindowEvent e) { - synchronized ( this ) { - minimized = true; - } - } - - public void windowDeiconified(WindowEvent e) { - synchronized ( this ) { - minimized = false; - } - } - - public void windowOpened(WindowEvent e) { - } - - public void windowClosed(WindowEvent e) { - } - - public void windowClosing(WindowEvent e) { - synchronized ( this ) { - close_requested = true; - } - } - - public void windowDeactivated(WindowEvent e) { - synchronized ( this ) { - active = false; - should_release_cursor = true; - should_warp_cursor = false; - } - } - - public void windowActivated(WindowEvent e) { - synchronized ( this ) { - active = true; - should_warp_cursor = true; - should_release_cursor = false; - } - } - - public boolean syncIsCloseRequested() { - boolean result; - synchronized ( this ) { - result = close_requested; - close_requested = false; - } - return result; - } - - public boolean syncIsVisible() { - synchronized ( this ) { - return !minimized; - } - } - - public boolean syncIsActive() { - synchronized ( this ) { - return active; - } - } - - public MacOSXGLCanvas getCanvas() { - return canvas; - } - - public boolean syncShouldReleaseCursor() { - boolean result; - synchronized ( this ) { - result = should_release_cursor; - should_release_cursor = false; - } - return result; - } - - public boolean syncShouldWarpCursor() { - boolean result; - synchronized ( this ) { - result = should_warp_cursor; - should_warp_cursor = false; - } - return result; - } -} diff --git a/src/java/org/lwjgl/opengl/MacOSXMouseEventQueue.java b/src/java/org/lwjgl/opengl/MacOSXMouseEventQueue.java deleted file mode 100644 index 5e4d221a..00000000 --- a/src/java/org/lwjgl/opengl/MacOSXMouseEventQueue.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (c) 2002-2008 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; - -/** - * An AWT implementation of a LWJGL compatible Mouse event queue. - * @author elias_naur - */ - -import java.awt.Component; -import java.awt.Point; -import java.awt.Rectangle; -import java.nio.IntBuffer; - -import org.lwjgl.BufferUtils; - -final class MacOSXMouseEventQueue extends MouseEventQueue { - private IntBuffer delta_buffer = BufferUtils.createIntBuffer(2); - - private boolean skip_event; - private static boolean is_grabbed; - - MacOSXMouseEventQueue(Component component) { - super(component); - } - - public void setGrabbed(boolean grab) { - if (is_grabbed != grab) { - super.setGrabbed(grab); - warpCursor(); - grabMouse(grab); - } - } - - private static synchronized void grabMouse(boolean grab) { - is_grabbed = grab; - if (!grab) - nGrabMouse(grab); - } - - protected void resetCursorToCenter() { - super.resetCursorToCenter(); - /* Clear accumulated deltas */ - getMouseDeltas(delta_buffer); - } - - protected void updateDeltas(long nanos) { - super.updateDeltas(nanos); - synchronized ( this ) { - getMouseDeltas(delta_buffer); - int dx = delta_buffer.get(0); - int dy = -delta_buffer.get(1); - if (skip_event) { - skip_event = false; - nGrabMouse(isGrabbed()); - return; - } - if ( dx != 0 || dy != 0 ) { - putMouseEventWithCoords((byte)-1, (byte)0, dx, dy, 0, nanos); - addDelta(dx, dy); - } - } - } - - void warpCursor() { - synchronized (this) { - // If we're going to warp the cursor position, we'll skip the next event to avoid bogus delta values - skip_event = isGrabbed(); - } - } - - private static native void getMouseDeltas(IntBuffer delta_buffer); - - public static native void nWarpCursor(int x, int y); - - public static native void nGrabMouse(boolean grab); -} diff --git a/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java b/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java index 599b6441..a427d8ca 100644 --- a/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java +++ b/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java @@ -92,6 +92,10 @@ final class MacOSXNativeMouse extends EventQueue { nRegisterMouseListener(window_handle); } + public synchronized void warpCursor(int x, int y) { + nWarpCursor(window_handle, x, y); + } + public synchronized void unregister() { nUnregisterMouseListener(window_handle); } diff --git a/src/native/macosx/org_lwjgl_input_Mouse.c b/src/native/macosx/org_lwjgl_input_Mouse.c deleted file mode 100644 index a9840e6c..00000000 --- a/src/native/macosx/org_lwjgl_input_Mouse.c +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 2002-2008 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$ - * - * Mac OS X mouse handling. - * - * @author elias_naur - * @version $Revision$ - */ - -#include -#include -#include "org_lwjgl_opengl_MacOSXMouseEventQueue.h" -#include "common_tools.h" - -JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXMouseEventQueue_nGrabMouse(JNIEnv *env, jclass unused, jboolean grab) { - CGAssociateMouseAndMouseCursorPosition(grab == JNI_TRUE ? FALSE : TRUE); - if (grab) - CGDisplayHideCursor(kCGDirectMainDisplay); - else - CGDisplayShowCursor(kCGDirectMainDisplay); -} - -JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXMouseEventQueue_nWarpCursor(JNIEnv *env, jclass unused, jint x, jint y) { - CGPoint p; - p.x = x; - p.y = y; - CGWarpMouseCursorPosition(p); -} - -JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXMouseEventQueue_getMouseDeltas(JNIEnv *env, jclass unused, jobject delta_buffer) { - CGMouseDelta dx, dy; - CGGetLastMouseDelta(&dx, &dy); - int buffer_length = (*env)->GetDirectBufferCapacity(env, delta_buffer); - if (buffer_length != 2) { - printfDebugJava(env, "Delta buffer not large enough!"); - return; - } - jint *buffer = (*env)->GetDirectBufferAddress(env, delta_buffer); - buffer[0] = dx; - buffer[1] = dy; -} From 141b9c263269492ae8b0f4dc7d6e3ec2719c8f4e Mon Sep 17 00:00:00 2001 From: kappa1 Date: Wed, 14 Nov 2012 21:06:09 +0000 Subject: [PATCH 017/111] Implement Mouse Dragging when using the Right Mouse Button or the Scroll Wheel Button --- src/native/macosx/org_lwjgl_opengl_Display.m | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 8f4d355c..2255ffd1 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -294,6 +294,30 @@ static NSAutoreleasePool *pool; (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time); } +- (void)rightMouseDragged:(NSEvent *)event { + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil) { + return; + } + long time = [event timestamp] * 1000000000; + jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); + jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); + NSPoint loc = [self convertPoint:[event locationInWindow] toView:self]; + (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time); +} + +- (void)otherMouseDragged:(NSEvent *)event { + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil) { + return; + } + long time = [event timestamp] * 1000000000; + jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); + jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); + NSPoint loc = [self convertPoint:[event locationInWindow] toView:self]; + (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time); +} + - (void)mouseMoved:(NSEvent *)event { JNIEnv *env = attachCurrentThread(); if (env == nil || event == nil || _parent == nil || _parent->jmouse == nil) { From 5d3cffc6af1e2867a9850ac53a7685976b625309 Mon Sep 17 00:00:00 2001 From: Erik Broes Date: Fri, 16 Nov 2012 22:46:28 +0100 Subject: [PATCH 018/111] Added .gitignore --- .gitignore | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..d8a35feb --- /dev/null +++ b/.gitignore @@ -0,0 +1,81 @@ +/*.class +/*.class~ +/*.java~ +/.classpath +/.nbattrs +/.project +/bin +/dist +/doc/javadoc +/libs +/libs/lwjgl-debug.jar +/libs/lwjgl.jar +/libs/lwjgl_test.jar +/libs/lwjgl_util.jar +/libs/lwjgl_util_applet.jar +/libs/macosx/liblwjgl.jnilib +/libs/windows/lwjgl.dll +/libs/windows/lwjgl64.dll +/src/generated +/src/native/common/Debug +/src/native/common/Release +/src/native/common/org_lwjgl_DefaultSysImplementation.h +/src/native/common/org_lwjgl_NativeSysImplementation.h +/src/native/common/org_lwjgl_Sys.h +/src/native/common/org_lwjgl_input_Cursor.h +/src/native/common/org_lwjgl_input_Cursor_CursorElement.h +/src/native/common/org_lwjgl_input_Keyboard.h +/src/native/common/org_lwjgl_input_Keyboard_KeyEvent.h +/src/native/common/org_lwjgl_input_Mouse.h +/src/native/common/org_lwjgl_openal_AL.h +/src/native/common/org_lwjgl_openal_ALC.h +/src/native/common/org_lwjgl_opencl_CL.h +/src/native/common/org_lwjgl_opencl_CallbackUtil.h +/src/native/common/org_lwjgl_opengl_AWTSurfaceLock.h +/src/native/common/org_lwjgl_opengl_CallbackUtil.h +/src/native/common/org_lwjgl_opengl_Display.h +/src/native/common/org_lwjgl_opengl_GLContext.h +/src/native/common/org_lwjgl_opengl_GLContext_CapabilitiesCacheEntry.h +/src/native/common/org_lwjgl_opengl_Pbuffer.h +/src/native/generated +/src/native/linux/org_lwjgl_LinuxSysImplementation.h +/src/native/linux/org_lwjgl_opengl_LinuxAWTGLCanvasPeerInfo.h +/src/native/linux/org_lwjgl_opengl_LinuxCanvasImplementation.h +/src/native/linux/org_lwjgl_opengl_LinuxContextImplementation.h +/src/native/linux/org_lwjgl_opengl_LinuxDisplay.h +/src/native/linux/org_lwjgl_opengl_LinuxDisplayPeerInfo.h +/src/native/linux/org_lwjgl_opengl_LinuxDisplay_Compiz.h +/src/native/linux/org_lwjgl_opengl_LinuxDisplay_Compiz_Provider.h +/src/native/linux/org_lwjgl_opengl_LinuxEvent.h +/src/native/linux/org_lwjgl_opengl_LinuxKeyboard.h +/src/native/linux/org_lwjgl_opengl_LinuxMouse.h +/src/native/linux/org_lwjgl_opengl_LinuxPbufferPeerInfo.h +/src/native/linux/org_lwjgl_opengl_LinuxPeerInfo.h +/src/native/macosx/org_lwjgl_MacOSXSysImplementation.h +/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.h +/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.h +/src/native/macosx/org_lwjgl_opengl_MacOSXDisplay.h +/src/native/macosx/org_lwjgl_opengl_MacOSXMouseEventQueue.h +/src/native/macosx/org_lwjgl_opengl_MacOSXPbufferPeerInfo.h +/src/native/macosx/org_lwjgl_opengl_MacOSXPeerInfo.h +/src/native/windows/Debug +/src/native/windows/Release +/src/native/windows/opengl/org_lwjgl_opengl_WindowsAWTGLCanvasPeerInfo.h +/src/native/windows/opengl/org_lwjgl_opengl_WindowsContextImplementation.h +/src/native/windows/opengl/org_lwjgl_opengl_WindowsDisplayPeerInfo.h +/src/native/windows/opengl/org_lwjgl_opengl_WindowsPbufferPeerInfo.h +/src/native/windows/opengl/org_lwjgl_opengl_WindowsPeerInfo.h +/src/native/windows/org_lwjgl_NativeSysImplementation.h +/src/native/windows/org_lwjgl_Win32SysImplementation.h +/src/native/windows/org_lwjgl_WindowsSysImplementation.h +/src/native/windows/org_lwjgl_opengl_Win32Display.h +/src/native/windows/org_lwjgl_opengl_WindowsAWTGLCanvasPeerInfo.h +/src/native/windows/org_lwjgl_opengl_WindowsContextImplementation.h +/src/native/windows/org_lwjgl_opengl_WindowsDisplay.h +/src/native/windows/org_lwjgl_opengl_WindowsDisplayPeerInfo.h +/src/native/windows/org_lwjgl_opengl_WindowsDisplay_Rect.h +/src/native/windows/org_lwjgl_opengl_WindowsKeyboard.h +/src/native/windows/org_lwjgl_opengl_WindowsPbufferPeerInfo.h +/src/native/windows/org_lwjgl_opengl_WindowsPeerInfo.h +/src/native/windows/org_lwjgl_opengl_WindowsRegistry.h +/temp From 5f37b11857ea09f67a6cfc568216e80008c008c3 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 20 Nov 2012 23:18:11 +0000 Subject: [PATCH 019/111] just a test commit to see if git is working, will undo in next patch --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index eaefb87a..0a795ae3 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -34,7 +34,7 @@ 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. + * for each supported platform. Git test * @author elias_naur */ From 9931e5dbfc5a2798782e5d64b36816ff0d4dfdda Mon Sep 17 00:00:00 2001 From: kappaOne Date: Thu, 22 Nov 2012 22:43:43 +0000 Subject: [PATCH 020/111] Fix native crash when closing the OS X Display window using the close button --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 2 +- src/native/macosx/org_lwjgl_opengl_Display.m | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 0a795ae3..eaefb87a 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -34,7 +34,7 @@ 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. Git test + * for each supported platform. * @author elias_naur */ diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 2255ffd1..3697217c 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -139,7 +139,7 @@ static NSAutoreleasePool *pool; NSOpenGLContext* context = [self openGLContext]; [super lockFocus]; - if ([context view] != self) { + if ([context view] != self && [context view] != nil) { [context setView:self]; } From 7f46fb2fbd4b56ebb101dd9b0a9c4e6ee5d4c59e Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 25 Nov 2012 15:07:20 +0000 Subject: [PATCH 021/111] Reimplement fullscreen support, correct resizing behaviour on window creation and to work reliably during runtime. --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 6 +- src/native/macosx/context.h | 1 + src/native/macosx/org_lwjgl_opengl_Display.m | 107 +++++++++++-------- 3 files changed, 69 insertions(+), 45 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index eaefb87a..fe9e9a82 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -90,7 +90,7 @@ final class MacOSXDisplay implements DisplayImplementation { } - 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 ByteBuffer nCreateWindow(int x, int y, int width, int height, boolean fullscreen, boolean undecorated, boolean resizable, ByteBuffer peer_info_handle, ByteBuffer window_handle) throws LWJGLException; private native boolean nIsMiniaturized(ByteBuffer window_handle); @@ -112,6 +112,8 @@ final class MacOSXDisplay implements DisplayImplementation { public void createWindow(final DrawableLWJGL drawable, DisplayMode mode, Canvas parent, int x, int y) throws LWJGLException { boolean fullscreen = Display.isFullscreen(); + boolean resizable = Display.isResizable(); + hideUI(fullscreen); close_requested = false; @@ -120,7 +122,7 @@ final class MacOSXDisplay implements DisplayImplementation { ByteBuffer peer_handle = peer_info.lockAndGetHandle(); try { window = nCreateWindow(x, y, mode.getWidth(), mode.getHeight(), - fullscreen, isUndecorated(), + fullscreen, isUndecorated(), resizable, peer_handle, window); this.x = x; this.y = y; diff --git a/src/native/macosx/context.h b/src/native/macosx/context.h index 3a6af76d..4e14a332 100644 --- a/src/native/macosx/context.h +++ b/src/native/macosx/context.h @@ -61,6 +61,7 @@ typedef struct { jobject jdisplay; jobject jmouse; jobject jkeyboard; + jboolean fullscreen; jboolean resized; // Cached for window creation diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 3697217c..9d2124b4 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -139,7 +139,7 @@ static NSAutoreleasePool *pool; NSOpenGLContext* context = [self openGLContext]; [super lockFocus]; - if ([context view] != self && [context view] != nil) { + if ([context view] != nil && [context view] != self) { [context setView:self]; } @@ -424,7 +424,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nSetTitle(JNIEnv *env [window_info->window setTitle:title]; } -JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIEnv *env, jobject this, jint x, jint y, jint width, jint height, jboolean fullscreen, jboolean undecorated, jobject peer_info_handle, jobject window_handle) { +JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIEnv *env, jobject this, jint x, jint y, jint width, jint height, jboolean fullscreen, jboolean undecorated, jboolean resizable, jobject peer_info_handle, jobject window_handle) { if (window_handle == NULL) { window_handle = newJavaManagedByteBuffer(env, sizeof(MacOSXWindowInfo)); @@ -435,7 +435,7 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE } pool = [[NSAutoreleasePool alloc] init]; - + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); @@ -444,17 +444,6 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE window_info->jdisplay = (*env)->NewGlobalRef(env, this); } - window_info->display_rect = NSMakeRect(x, y, width, height); - int default_window_mask = NSBorderlessWindowMask; - if (!undecorated && !fullscreen) { - printf("Resizeable\n"); fflush(stdout); - default_window_mask = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask; - } - window_info->window = (MacOSXKeyableWindow*)[[NSApplication sharedApplication] mainWindow]; - if (window_info->window == nil) { - window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:default_window_mask backing:NSBackingStoreBuffered defer:YES]; - } - NSRect view_rect = NSMakeRect(0.0, 0.0, width, height); window_info->view = [[MacOSXOpenGLView alloc] initWithFrame:view_rect pixelFormat:peer_info->pixel_format]; if (window_info->context != nil) { @@ -462,43 +451,75 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE [window_info->view setOpenGLContext:window_info->context]; } - // Inform the view of its parent window info; used to register for window-close callbacks + [[NSApplication sharedApplication] setDelegate:window_info->view]; + + + if (!fullscreen) { + + int default_window_mask = NSBorderlessWindowMask; // undecorated + + if (!undecorated) { + default_window_mask = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask; + } + + if (resizable) { + default_window_mask |= NSResizableWindowMask; + } + + window_info->display_rect = NSMakeRect(x, y, width, height); + + window_info->window = (MacOSXKeyableWindow*)[[NSApplication sharedApplication] mainWindow]; + if (window_info->window == nil) { + window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:default_window_mask backing:NSBackingStoreBuffered defer:YES]; + } + + [window_info->window setContentView:window_info->view]; + + } + else { + [window_info->view enterFullScreenMode: [NSScreen mainScreen] withOptions: nil ]; + window_info->window = [window_info->view window]; + } + + // Inform the view of its parent window info; used to register for window-close callbacks [window_info->view setParent:window_info]; - - [window_info->window setContentView:window_info->view]; - //[window_info->window makeKeyAndOrderFront:[NSApplication sharedApplication]]; - //[window_info->window makeFirstResponder:window_info->view]; - //[window_info->window setReleasedWhenClosed:YES]; - //[window_info->window setInitialFirstResponder:window_info->view]; + + //[window_info->window makeKeyAndOrderFront:[NSApplication sharedApplication]]; + //[window_info->window makeFirstResponder:window_info->view]; + //[window_info->window setReleasedWhenClosed:YES]; + //[window_info->window setInitialFirstResponder:window_info->view]; + [window_info->window performSelectorOnMainThread:@selector(makeFirstResponder:) withObject:window_info->view waitUntilDone:NO]; + [window_info->window performSelectorOnMainThread:@selector(setInitialFirstResponder:) withObject:window_info->view waitUntilDone:NO]; [window_info->window performSelectorOnMainThread:@selector(makeKeyAndOrderFront:) withObject:[NSApplication sharedApplication] waitUntilDone:NO]; - [window_info->window performSelectorOnMainThread:@selector(makeFirstResponder:) withObject:window_info->view waitUntilDone:NO]; - [window_info->window performSelectorOnMainThread:@selector(setReleasedWhenClosed:) withObject:window_info->window waitUntilDone:NO]; - [window_info->window performSelectorOnMainThread:@selector(setInitialFirstResponder:) withObject:window_info->view waitUntilDone:NO]; - - if (window_info->window_options != NSApplicationPresentationDefault) { - printf("Non-default\n"); fflush(stdout); - [[NSApplication sharedApplication] setPresentationOptions:window_info->window_options]; - } - + [window_info->window performSelectorOnMainThread:@selector(setReleasedWhenClosed:) withObject:window_info->window waitUntilDone:NO]; + + window_info->fullscreen = fullscreen; + peer_info->window_info = window_info; - + return window_handle; } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv *env, jobject this, jobject window_handle) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - if (window_info->window != nil) { - [window_info->window close]; - } - window_info->window = nil; + if (window_info->fullscreen) { + [window_info->view exitFullScreenModeWithOptions: nil]; + } + else { + if (window_info->window != nil) { + [window_info->window close]; + } + } + //window_info->window = nil; - if (window_info->view != nil) { - [window_info->view release]; - } - window_info->view = nil; + //if (window_info->view != nil) { + // [window_info->view release]; + // window_info->view = nil; + //} //[window_info->window release]; - [pool drain]; + + //[pool drain]; } JNIEXPORT jint JNICALL Java_org_lwjgl_DefaultSysImplementation_getJNIVersion @@ -525,7 +546,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nHideUI(JNIEnv *env, return; } - MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + //MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); //if(floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) { /*NSApplicationPresentationOptions options = NSApplicationPresentationDefault; if (hide == JNI_TRUE) { @@ -539,10 +560,10 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nHideUI(JNIEnv *env, [[NSApplication sharedApplication] setPresentationOptions:options]; }*/ //} else { - if (hide == JNI_TRUE) { + /*if (hide == JNI_TRUE) { SetSystemUIMode(kUIModeContentSuppressed, 0); } else { SetSystemUIMode(kUIModeNormal, 0); - } + }*/ //} } From 7b5d596c9b32918dde99b29ddf0cf37b1cd6a7ce Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 25 Nov 2012 15:33:56 +0000 Subject: [PATCH 022/111] remove observer as it was causing a crash on exit --- src/native/macosx/org_lwjgl_opengl_Display.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 9d2124b4..14e83b37 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -95,10 +95,10 @@ static NSAutoreleasePool *pool; _modifierFlags = 0; if (self != nil) { _pixelFormat = [format retain]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(_surfaceNeedsUpdate:) - name:NSViewGlobalFrameDidChangeNotification - object:self]; + //[[NSNotificationCenter defaultCenter] addObserver:self + // selector:@selector(_surfaceNeedsUpdate:) + // name:NSViewGlobalFrameDidChangeNotification + // object:self]; } return self; } From c2159989f597ead1707bfa7e40f6490dbe4395d2 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 25 Nov 2012 17:12:42 +0000 Subject: [PATCH 023/111] remove unused code for hiding OS menubar and dock as its no longer needed. --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 14 ---------- src/native/macosx/org_lwjgl_opengl_Display.m | 28 -------------------- 2 files changed, 42 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index fe9e9a82..9fde3262 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -114,7 +114,6 @@ final class MacOSXDisplay implements DisplayImplementation { boolean fullscreen = Display.isFullscreen(); boolean resizable = Display.isResizable(); - hideUI(fullscreen); close_requested = false; DrawableGL gl_drawable = (DrawableGL)Display.getDrawable(); @@ -147,7 +146,6 @@ final class MacOSXDisplay implements DisplayImplementation { public void destroyWindow() { nDestroyWindow(window); - hideUI(false); } public int getGammaRampLength() { @@ -296,18 +294,6 @@ final class MacOSXDisplay implements DisplayImplementation { } } - /** - * 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); - public void reshape(int x, int y, int width, int height) { //if (native_mode) { // nResizeWindow(window, x, y, width, height); diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 14e83b37..9081fcc6 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -539,31 +539,3 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_setGammaRamp(JNIEnv * throwException(env, "Could not set display gamma"); } } - -JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nHideUI(JNIEnv *env, jobject this, jobject window_handle, jboolean hide) { - if (window_handle == NULL) { - printf("Window handle is null\n"); - return; - } - - //MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - //if(floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) { - /*NSApplicationPresentationOptions options = NSApplicationPresentationDefault; - if (hide == JNI_TRUE) { - options = NSApplicationPresentationFullScreen; // this requires OS X 10.7+ to compile - options |= NSApplicationPresentationHideDock; - options |= NSApplicationPresentationHideMenuBar; - } - printf("Setting options\n"); - window_info->window_options = options; - if (window_info->window != nil) { - [[NSApplication sharedApplication] setPresentationOptions:options]; - }*/ - //} else { - /*if (hide == JNI_TRUE) { - SetSystemUIMode(kUIModeContentSuppressed, 0); - } else { - SetSystemUIMode(kUIModeNormal, 0); - }*/ - //} -} From 9a968e8c41f9a5dd120c01bc71f05a7ed8f3d41b Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 25 Nov 2012 18:38:46 +0000 Subject: [PATCH 024/111] fix crash on exit by removing NSView before destroying the NSWindow, so that it doesn't draw after being removed --- src/native/macosx/org_lwjgl_opengl_Display.m | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 9081fcc6..6707bd79 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -139,7 +139,7 @@ static NSAutoreleasePool *pool; NSOpenGLContext* context = [self openGLContext]; [super lockFocus]; - if ([context view] != nil && [context view] != self) { + if ([context view] != self && [context view] != nil) { [context setView:self]; } @@ -503,6 +503,10 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv *env, jobject this, jobject window_handle) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + if (window_info->view != nil) { + [window_info->view removeFromSuperviewWithoutNeedingDisplay]; + } + if (window_info->fullscreen) { [window_info->view exitFullScreenModeWithOptions: nil]; } From b5a87d818e32c6c30f56031fbd74b9b69355cd93 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 25 Nov 2012 20:14:48 +0000 Subject: [PATCH 025/111] Implement ScrollWheel functionality again --- src/java/org/lwjgl/opengl/MacOSXNativeMouse.java | 2 +- src/native/macosx/org_lwjgl_opengl_Display.m | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java b/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java index a427d8ca..12415a35 100644 --- a/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java +++ b/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java @@ -188,7 +188,7 @@ final class MacOSXNativeMouse extends EventQueue { setCursorPos(x, y, nanos); } if ( dz != 0 ) { - int wheel_amount = (int)(-dz * WHEEL_SCALE); + int wheel_amount = (int)(-dy * WHEEL_SCALE); accum_dz += wheel_amount; putMouseEvent((byte)-1, (byte)0, wheel_amount, nanos); } diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 6707bd79..efb6e3e1 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -327,7 +327,7 @@ static NSAutoreleasePool *pool; jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); NSPoint loc = [self convertPoint:[event locationInWindow] toView:self]; - (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time); + (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time); } - (void)scrollWheel:(NSEvent *)event { @@ -340,10 +340,10 @@ static NSAutoreleasePool *pool; //if (![event hasPreciseScrollingDeltas]) { // Also an OS X 10.7 API // dz *= 12; // or so //} - //jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); - //jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); - //NSPoint loc = [self convertPoint:[event locationInWindow] toView:self]; - //(*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], dz, time); + jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); + jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); + NSPoint loc = [self convertPoint:[event locationInWindow] toView:self]; + (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 1.0f, time); } - (void)viewDidMoveToWindow From 6eccb8b971afa2d7ad764dc442fc6f7989049df1 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 25 Nov 2012 20:24:51 +0000 Subject: [PATCH 026/111] clean up debug code printf's --- src/native/macosx/org_lwjgl_opengl_Display.m | 26 +++---------------- ...lwjgl_opengl_MacOSXContextImplementation.m | 13 +++------- 2 files changed, 8 insertions(+), 31 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index efb6e3e1..69b9b2e5 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -49,8 +49,6 @@ #import "org_lwjgl_MacOSXSysImplementation.h" #import "context.h" -#define WAIT_DELAY 100 - static NSOpenGLPixelFormat *default_format = nil; static NSAutoreleasePool *pool; @@ -95,10 +93,10 @@ static NSAutoreleasePool *pool; _modifierFlags = 0; if (self != nil) { _pixelFormat = [format retain]; - //[[NSNotificationCenter defaultCenter] addObserver:self - // selector:@selector(_surfaceNeedsUpdate:) - // name:NSViewGlobalFrameDidChangeNotification - // object:self]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(_surfaceNeedsUpdate:) + name:NSViewGlobalFrameDidChangeNotification + object:self]; } return self; } @@ -336,10 +334,6 @@ static NSAutoreleasePool *pool; return; } long time = [event timestamp] * 1000000000; - //float dz = [event scrollingDeltaY]; // An OS X 10.7+ API - //if (![event hasPreciseScrollingDeltas]) { // Also an OS X 10.7 API - // dz *= 12; // or so - //} jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); NSPoint loc = [self convertPoint:[event locationInWindow] toView:self]; @@ -447,7 +441,6 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE NSRect view_rect = NSMakeRect(0.0, 0.0, width, height); window_info->view = [[MacOSXOpenGLView alloc] initWithFrame:view_rect pixelFormat:peer_info->pixel_format]; if (window_info->context != nil) { - printf("Setting context\n"); fflush(stdout); [window_info->view setOpenGLContext:window_info->context]; } @@ -484,10 +477,6 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE // Inform the view of its parent window info; used to register for window-close callbacks [window_info->view setParent:window_info]; - //[window_info->window makeKeyAndOrderFront:[NSApplication sharedApplication]]; - //[window_info->window makeFirstResponder:window_info->view]; - //[window_info->window setReleasedWhenClosed:YES]; - //[window_info->window setInitialFirstResponder:window_info->view]; [window_info->window performSelectorOnMainThread:@selector(makeFirstResponder:) withObject:window_info->view waitUntilDone:NO]; [window_info->window performSelectorOnMainThread:@selector(setInitialFirstResponder:) withObject:window_info->view waitUntilDone:NO]; [window_info->window performSelectorOnMainThread:@selector(makeKeyAndOrderFront:) withObject:[NSApplication sharedApplication] waitUntilDone:NO]; @@ -515,13 +504,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv [window_info->window close]; } } - //window_info->window = nil; - - //if (window_info->view != nil) { - // [window_info->view release]; - // window_info->view = nil; - //} - //[window_info->window release]; //[pool drain]; } diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m index 2bc6c909..ad0faeb4 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m @@ -59,7 +59,7 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nCre MacOSXContext *context_info; NSOpenGLContext *context; NSOpenGLContext *shared_context = NULL; - printf("nCreate\n"); + jobject context_handle = newJavaManagedByteBuffer(env, sizeof(MacOSXContext)); if (context_handle == NULL) { throwException(env, "Could not create handle buffer"); @@ -118,7 +118,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_clearDr (JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; - printf("clearDrawable\n"); [[peer_info->window_info->view openGLContext] clearDrawable]; [pool release]; } @@ -126,8 +125,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_clearDr JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nReleaseCurrentContext (JNIEnv *env, jclass clazz) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - printf("nReleaseCurrentContexta\n"); - [NSOpenGLContext clearCurrentContext]; + [NSOpenGLContext clearCurrentContext]; [pool release]; } @@ -135,7 +133,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_setView (JNIEnv *env, jclass clazz, jobject peer_info_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); - printf("setView\n"); [[peer_info->window_info->view openGLContext] setView: peer_info->window_info->view]; peer_info->canDrawGL = true; [pool release]; @@ -145,8 +142,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nMakeCu (JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; - printf("nMakeCurrent\n"); - [[peer_info->window_info->view openGLContext] makeCurrentContext]; + [[peer_info->window_info->view openGLContext] makeCurrentContext]; [pool release]; } @@ -172,8 +168,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nDestro (JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; - printf("nDestroy\n"); - [[peer_info->window_info->view openGLContext] clearDrawable]; + [[peer_info->window_info->view openGLContext] clearDrawable]; [[peer_info->window_info->view openGLContext] release]; [pool release]; } From ef62a8371be95f7670a8066380f8578ef51e6dbc Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 25 Nov 2012 20:34:45 +0000 Subject: [PATCH 027/111] remove white flash when switch to/from fullscreen --- src/native/macosx/org_lwjgl_opengl_Display.m | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 69b9b2e5..06bf21bb 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -361,6 +361,13 @@ static NSAutoreleasePool *pool; _parent->resized = JNI_TRUE; } } + +- (void) drawRect:(NSRect)backgroundColor { + // set black as the default background color + // for the nsview to avoid white flash on fullscreen + [[NSColor blackColor] setFill]; + NSRectFill(backgroundColor); +} @end JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nIsMiniaturized(JNIEnv *env, jobject this, jobject window_handle) { From ee6976370745f4d20e8dfeab1cdd5b865a62f1cc Mon Sep 17 00:00:00 2001 From: Erik Broes Date: Tue, 27 Nov 2012 16:15:43 +0100 Subject: [PATCH 028/111] Don't resolve a key to a deprecated name. --- src/java/org/lwjgl/input/Keyboard.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/java/org/lwjgl/input/Keyboard.java b/src/java/org/lwjgl/input/Keyboard.java index a5d733f9..a5bc920f 100644 --- a/src/java/org/lwjgl/input/Keyboard.java +++ b/src/java/org/lwjgl/input/Keyboard.java @@ -229,7 +229,8 @@ public class Keyboard { && Modifier.isPublic(field.getModifiers()) && Modifier.isFinal(field.getModifiers()) && field.getType().equals(int.class) - && field.getName().startsWith("KEY_") ) { + && field.getName().startsWith("KEY_") + && !field.getName().endsWith("WIN") ) { /* Don't use deprecated names */ int key = field.getInt(null); String name = field.getName().substring(4); From 9d4f22034c1be0ac04c9782200a9bf29e03fe15f Mon Sep 17 00:00:00 2001 From: Erik Broes Date: Tue, 27 Nov 2012 15:14:58 +0100 Subject: [PATCH 029/111] Allow the use of F16-F19, SECTION, INSERT, HOME, END, DELETE, NUMPADENTER, CLEAR, LMETA keys. --- src/java/org/lwjgl/input/Keyboard.java | 6 ++++++ .../org/lwjgl/opengl/MacOSXNativeKeyboard.java | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/java/org/lwjgl/input/Keyboard.java b/src/java/org/lwjgl/input/Keyboard.java index a5bc920f..c37f98f2 100644 --- a/src/java/org/lwjgl/input/Keyboard.java +++ b/src/java/org/lwjgl/input/Keyboard.java @@ -158,7 +158,11 @@ public class Keyboard { public static final int KEY_F13 = 0x64; /* (NEC PC98) */ public static final int KEY_F14 = 0x65; /* (NEC PC98) */ public static final int KEY_F15 = 0x66; /* (NEC PC98) */ + public static final int KEY_F16 = 0x67; /* Extended Function keys - (Mac) */ + public static final int KEY_F17 = 0x68; + public static final int KEY_F18 = 0x69; public static final int KEY_KANA = 0x70; /* (Japanese keyboard) */ + public static final int KEY_F19 = 0x71; /* Extended Function keys - (Mac) */ public static final int KEY_CONVERT = 0x79; /* (Japanese keyboard) */ public static final int KEY_NOCONVERT = 0x7B; /* (Japanese keyboard) */ public static final int KEY_YEN = 0x7D; /* (Japanese keyboard) */ @@ -173,6 +177,7 @@ public class Keyboard { public static final int KEY_UNLABELED = 0x97; /* (J3100) */ public static final int KEY_NUMPADENTER = 0x9C; /* Enter on numeric keypad */ public static final int KEY_RCONTROL = 0x9D; + public static final int KEY_SECTION = 0xA7; /* Section symbol (Mac) */ public static final int KEY_NUMPADCOMMA = 0xB3; /* , on numeric keypad (NEC PC98) */ public static final int KEY_DIVIDE = 0xB5; /* / on numeric keypad */ public static final int KEY_SYSRQ = 0xB7; @@ -188,6 +193,7 @@ public class Keyboard { public static final int KEY_NEXT = 0xD1; /* PgDn on arrow keypad */ public static final int KEY_INSERT = 0xD2; /* Insert on arrow keypad */ public static final int KEY_DELETE = 0xD3; /* Delete on arrow keypad */ + public static final int KEY_CLEAR = 0xDA; /* Clear key (Mac) */ public static final int KEY_LMETA = 0xDB; /* Left Windows/Option key */ /** * The left windows key, mapped to KEY_LMETA diff --git a/src/java/org/lwjgl/opengl/MacOSXNativeKeyboard.java b/src/java/org/lwjgl/opengl/MacOSXNativeKeyboard.java index ee12eef9..dadc1cf4 100644 --- a/src/java/org/lwjgl/opengl/MacOSXNativeKeyboard.java +++ b/src/java/org/lwjgl/opengl/MacOSXNativeKeyboard.java @@ -108,6 +108,7 @@ final class MacOSXNativeKeyboard extends EventQueue { nativeToLwjglMap.put((Short)(short)0x07, Keyboard.KEY_X); nativeToLwjglMap.put((Short)(short)0x10, Keyboard.KEY_Y); nativeToLwjglMap.put((Short)(short)0x06, Keyboard.KEY_Z); + nativeToLwjglMap.put((Short)(short)0x0A, Keyboard.KEY_SECTION); nativeToLwjglMap.put((Short)(short)0x2B, Keyboard.KEY_COMMA); nativeToLwjglMap.put((Short)(short)0x2C, Keyboard.KEY_SLASH); nativeToLwjglMap.put((Short)(short)0x2F, Keyboard.KEY_PERIOD); @@ -115,6 +116,7 @@ final class MacOSXNativeKeyboard extends EventQueue { nativeToLwjglMap.put((Short)(short)0x29, Keyboard.KEY_SEMICOLON); nativeToLwjglMap.put((Short)(short)0x129, Keyboard.KEY_COLON); nativeToLwjglMap.put((Short)(short)0x2A, Keyboard.KEY_BACKSLASH); + nativeToLwjglMap.put((Short)(short)0x47, Keyboard.KEY_CLEAR); nativeToLwjglMap.put((Short)(short)0x52, Keyboard.KEY_NUMPAD0); nativeToLwjglMap.put((Short)(short)0x53, Keyboard.KEY_NUMPAD1); nativeToLwjglMap.put((Short)(short)0x54, Keyboard.KEY_NUMPAD2); @@ -125,6 +127,7 @@ final class MacOSXNativeKeyboard extends EventQueue { nativeToLwjglMap.put((Short)(short)0x59, Keyboard.KEY_NUMPAD7); nativeToLwjglMap.put((Short)(short)0x5B, Keyboard.KEY_NUMPAD8); nativeToLwjglMap.put((Short)(short)0x5C, Keyboard.KEY_NUMPAD9); + nativeToLwjglMap.put((Short)(short)0x4C, Keyboard.KEY_NUMPADENTER); nativeToLwjglMap.put((Short)(short)0x7A, Keyboard.KEY_F1); nativeToLwjglMap.put((Short)(short)0x78, Keyboard.KEY_F2); nativeToLwjglMap.put((Short)(short)0x63, Keyboard.KEY_F3); @@ -140,7 +143,14 @@ final class MacOSXNativeKeyboard extends EventQueue { nativeToLwjglMap.put((Short)(short)0x69, Keyboard.KEY_F13); nativeToLwjglMap.put((Short)(short)0x6B, Keyboard.KEY_F14); nativeToLwjglMap.put((Short)(short)0x71, Keyboard.KEY_F15); - nativeToLwjglMap.put((Short)(short)0x72, Keyboard.KEY_HOME); + nativeToLwjglMap.put((Short)(short)0x6A, Keyboard.KEY_F16); + nativeToLwjglMap.put((Short)(short)0x40, Keyboard.KEY_F17); + nativeToLwjglMap.put((Short)(short)0x4F, Keyboard.KEY_F18); + nativeToLwjglMap.put((Short)(short)0x50, Keyboard.KEY_F19); + nativeToLwjglMap.put((Short)(short)0x72, Keyboard.KEY_INSERT); + nativeToLwjglMap.put((Short)(short)0x73, Keyboard.KEY_HOME); + nativeToLwjglMap.put((Short)(short)0x77, Keyboard.KEY_END); + nativeToLwjglMap.put((Short)(short)0x75, Keyboard.KEY_DELETE); nativeToLwjglMap.put((Short)(short)0x18, Keyboard.KEY_EQUALS); nativeToLwjglMap.put((Short)(short)0x7B, Keyboard.KEY_LEFT); nativeToLwjglMap.put((Short)(short)0x7C, Keyboard.KEY_RIGHT); @@ -170,7 +180,7 @@ final class MacOSXNativeKeyboard extends EventQueue { nativeToLwjglMap.put((Short)(short)0xF3, Keyboard.KEY_LMENU); nativeToLwjglMap.put((Short)(short)0x3A, Keyboard.KEY_LMENU); nativeToLwjglMap.put((Short)(short)0x3D, Keyboard.KEY_RMENU); - nativeToLwjglMap.put((Short)(short)0xF4, Keyboard.KEY_RETURN); + nativeToLwjglMap.put((Short)(short)0xF4, Keyboard.KEY_LMETA); nativeToLwjglMap.put((Short)(short)0xF5, Keyboard.KEY_NUMLOCK); nativeToLwjglMap.put((Short)(short)0x27, Keyboard.KEY_APOSTROPHE); From 52d46a840853c1e155c1ab3bbb7d97c45f65dff1 Mon Sep 17 00:00:00 2001 From: Erik Broes Date: Tue, 27 Nov 2012 20:12:15 +0100 Subject: [PATCH 030/111] Shrink .gitignore --- .gitignore | 74 +++++------------------------------------------------- 1 file changed, 6 insertions(+), 68 deletions(-) diff --git a/.gitignore b/.gitignore index d8a35feb..0a61cfa6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ -/*.class -/*.class~ -/*.java~ +*.*~ +.*.sw[a-p] /.classpath /.nbattrs /.project @@ -8,74 +7,13 @@ /dist /doc/javadoc /libs -/libs/lwjgl-debug.jar -/libs/lwjgl.jar -/libs/lwjgl_test.jar -/libs/lwjgl_util.jar -/libs/lwjgl_util_applet.jar -/libs/macosx/liblwjgl.jnilib -/libs/windows/lwjgl.dll -/libs/windows/lwjgl64.dll /src/generated +/src/native/generated /src/native/common/Debug /src/native/common/Release -/src/native/common/org_lwjgl_DefaultSysImplementation.h -/src/native/common/org_lwjgl_NativeSysImplementation.h -/src/native/common/org_lwjgl_Sys.h -/src/native/common/org_lwjgl_input_Cursor.h -/src/native/common/org_lwjgl_input_Cursor_CursorElement.h -/src/native/common/org_lwjgl_input_Keyboard.h -/src/native/common/org_lwjgl_input_Keyboard_KeyEvent.h -/src/native/common/org_lwjgl_input_Mouse.h -/src/native/common/org_lwjgl_openal_AL.h -/src/native/common/org_lwjgl_openal_ALC.h -/src/native/common/org_lwjgl_opencl_CL.h -/src/native/common/org_lwjgl_opencl_CallbackUtil.h -/src/native/common/org_lwjgl_opengl_AWTSurfaceLock.h -/src/native/common/org_lwjgl_opengl_CallbackUtil.h -/src/native/common/org_lwjgl_opengl_Display.h -/src/native/common/org_lwjgl_opengl_GLContext.h -/src/native/common/org_lwjgl_opengl_GLContext_CapabilitiesCacheEntry.h -/src/native/common/org_lwjgl_opengl_Pbuffer.h -/src/native/generated -/src/native/linux/org_lwjgl_LinuxSysImplementation.h -/src/native/linux/org_lwjgl_opengl_LinuxAWTGLCanvasPeerInfo.h -/src/native/linux/org_lwjgl_opengl_LinuxCanvasImplementation.h -/src/native/linux/org_lwjgl_opengl_LinuxContextImplementation.h -/src/native/linux/org_lwjgl_opengl_LinuxDisplay.h -/src/native/linux/org_lwjgl_opengl_LinuxDisplayPeerInfo.h -/src/native/linux/org_lwjgl_opengl_LinuxDisplay_Compiz.h -/src/native/linux/org_lwjgl_opengl_LinuxDisplay_Compiz_Provider.h -/src/native/linux/org_lwjgl_opengl_LinuxEvent.h -/src/native/linux/org_lwjgl_opengl_LinuxKeyboard.h -/src/native/linux/org_lwjgl_opengl_LinuxMouse.h -/src/native/linux/org_lwjgl_opengl_LinuxPbufferPeerInfo.h -/src/native/linux/org_lwjgl_opengl_LinuxPeerInfo.h -/src/native/macosx/org_lwjgl_MacOSXSysImplementation.h -/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.h -/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.h -/src/native/macosx/org_lwjgl_opengl_MacOSXDisplay.h -/src/native/macosx/org_lwjgl_opengl_MacOSXMouseEventQueue.h -/src/native/macosx/org_lwjgl_opengl_MacOSXPbufferPeerInfo.h -/src/native/macosx/org_lwjgl_opengl_MacOSXPeerInfo.h /src/native/windows/Debug /src/native/windows/Release -/src/native/windows/opengl/org_lwjgl_opengl_WindowsAWTGLCanvasPeerInfo.h -/src/native/windows/opengl/org_lwjgl_opengl_WindowsContextImplementation.h -/src/native/windows/opengl/org_lwjgl_opengl_WindowsDisplayPeerInfo.h -/src/native/windows/opengl/org_lwjgl_opengl_WindowsPbufferPeerInfo.h -/src/native/windows/opengl/org_lwjgl_opengl_WindowsPeerInfo.h -/src/native/windows/org_lwjgl_NativeSysImplementation.h -/src/native/windows/org_lwjgl_Win32SysImplementation.h -/src/native/windows/org_lwjgl_WindowsSysImplementation.h -/src/native/windows/org_lwjgl_opengl_Win32Display.h -/src/native/windows/org_lwjgl_opengl_WindowsAWTGLCanvasPeerInfo.h -/src/native/windows/org_lwjgl_opengl_WindowsContextImplementation.h -/src/native/windows/org_lwjgl_opengl_WindowsDisplay.h -/src/native/windows/org_lwjgl_opengl_WindowsDisplayPeerInfo.h -/src/native/windows/org_lwjgl_opengl_WindowsDisplay_Rect.h -/src/native/windows/org_lwjgl_opengl_WindowsKeyboard.h -/src/native/windows/org_lwjgl_opengl_WindowsPbufferPeerInfo.h -/src/native/windows/org_lwjgl_opengl_WindowsPeerInfo.h -/src/native/windows/org_lwjgl_opengl_WindowsRegistry.h /temp +/src/native/*/org_lwjgl_*.h +/src/native/*/*/org_lwjgl_*.h +/src/native/linux/org_lwjgl_opengl_Display.c From 129d0711c5d53e45d7c2e7aa1bf7dabdae77551d Mon Sep 17 00:00:00 2001 From: Erik Broes Date: Thu, 29 Nov 2012 18:27:08 +0100 Subject: [PATCH 031/111] Modifiers now report the correct side they are pressed. Added 'fn'-key as modifier. --- src/java/org/lwjgl/input/Keyboard.java | 3 +- .../lwjgl/opengl/MacOSXNativeKeyboard.java | 106 +++++++++--------- src/native/macosx/context.h | 2 - src/native/macosx/org_lwjgl_opengl_Display.m | 72 +++++------- 4 files changed, 82 insertions(+), 101 deletions(-) diff --git a/src/java/org/lwjgl/input/Keyboard.java b/src/java/org/lwjgl/input/Keyboard.java index c37f98f2..00c7652b 100644 --- a/src/java/org/lwjgl/input/Keyboard.java +++ b/src/java/org/lwjgl/input/Keyboard.java @@ -182,6 +182,7 @@ public class Keyboard { public static final int KEY_DIVIDE = 0xB5; /* / on numeric keypad */ public static final int KEY_SYSRQ = 0xB7; public static final int KEY_RMENU = 0xB8; /* right Alt */ + public static final int KEY_FUNCTION = 0xC4; /* Function (Mac) */ public static final int KEY_PAUSE = 0xC5; /* Pause */ public static final int KEY_HOME = 0xC7; /* Home on arrow keypad */ public static final int KEY_UP = 0xC8; /* UpArrow on arrow keypad */ @@ -194,7 +195,7 @@ public class Keyboard { public static final int KEY_INSERT = 0xD2; /* Insert on arrow keypad */ public static final int KEY_DELETE = 0xD3; /* Delete on arrow keypad */ public static final int KEY_CLEAR = 0xDA; /* Clear key (Mac) */ - public static final int KEY_LMETA = 0xDB; /* Left Windows/Option key */ + public static final int KEY_LMETA = 0xDB; /* Left Windows/Option key */ /** * The left windows key, mapped to KEY_LMETA * diff --git a/src/java/org/lwjgl/opengl/MacOSXNativeKeyboard.java b/src/java/org/lwjgl/opengl/MacOSXNativeKeyboard.java index dadc1cf4..5c631e56 100644 --- a/src/java/org/lwjgl/opengl/MacOSXNativeKeyboard.java +++ b/src/java/org/lwjgl/opengl/MacOSXNativeKeyboard.java @@ -71,6 +71,7 @@ final class MacOSXNativeKeyboard extends EventQueue { private native void nUnregisterKeyListener(ByteBuffer window_handle); + // These are from: private void initKeyboardMappings() { nativeToLwjglMap.put((Short)(short)0x1D, Keyboard.KEY_0); nativeToLwjglMap.put((Short)(short)0x12, Keyboard.KEY_1); @@ -108,15 +109,28 @@ final class MacOSXNativeKeyboard extends EventQueue { nativeToLwjglMap.put((Short)(short)0x07, Keyboard.KEY_X); nativeToLwjglMap.put((Short)(short)0x10, Keyboard.KEY_Y); nativeToLwjglMap.put((Short)(short)0x06, Keyboard.KEY_Z); - nativeToLwjglMap.put((Short)(short)0x0A, Keyboard.KEY_SECTION); + + nativeToLwjglMap.put((Short)(short)0x2A, Keyboard.KEY_BACKSLASH); nativeToLwjglMap.put((Short)(short)0x2B, Keyboard.KEY_COMMA); + nativeToLwjglMap.put((Short)(short)0x18, Keyboard.KEY_EQUALS); + nativeToLwjglMap.put((Short)(short)0x21, Keyboard.KEY_LBRACKET); + nativeToLwjglMap.put((Short)(short)0x1B, Keyboard.KEY_MINUS); + nativeToLwjglMap.put((Short)(short)0x27, Keyboard.KEY_APOSTROPHE); + nativeToLwjglMap.put((Short)(short)0x1E, Keyboard.KEY_RBRACKET); + nativeToLwjglMap.put((Short)(short)0x29, Keyboard.KEY_SEMICOLON); nativeToLwjglMap.put((Short)(short)0x2C, Keyboard.KEY_SLASH); nativeToLwjglMap.put((Short)(short)0x2F, Keyboard.KEY_PERIOD); nativeToLwjglMap.put((Short)(short)0x32, Keyboard.KEY_CIRCUMFLEX); - nativeToLwjglMap.put((Short)(short)0x29, Keyboard.KEY_SEMICOLON); - nativeToLwjglMap.put((Short)(short)0x129, Keyboard.KEY_COLON); - nativeToLwjglMap.put((Short)(short)0x2A, Keyboard.KEY_BACKSLASH); + + nativeToLwjglMap.put((Short)(short)0x41, Keyboard.KEY_DECIMAL); + nativeToLwjglMap.put((Short)(short)0x43, Keyboard.KEY_MULTIPLY); + nativeToLwjglMap.put((Short)(short)0x45, Keyboard.KEY_ADD); nativeToLwjglMap.put((Short)(short)0x47, Keyboard.KEY_CLEAR); + nativeToLwjglMap.put((Short)(short)0x4B, Keyboard.KEY_DIVIDE); + nativeToLwjglMap.put((Short)(short)0x4C, Keyboard.KEY_NUMPADENTER); + nativeToLwjglMap.put((Short)(short)0x4E, Keyboard.KEY_SUBTRACT); + nativeToLwjglMap.put((Short)(short)0x51, Keyboard.KEY_NUMPADEQUALS); + nativeToLwjglMap.put((Short)(short)0x52, Keyboard.KEY_NUMPAD0); nativeToLwjglMap.put((Short)(short)0x53, Keyboard.KEY_NUMPAD1); nativeToLwjglMap.put((Short)(short)0x54, Keyboard.KEY_NUMPAD2); @@ -127,7 +141,26 @@ final class MacOSXNativeKeyboard extends EventQueue { nativeToLwjglMap.put((Short)(short)0x59, Keyboard.KEY_NUMPAD7); nativeToLwjglMap.put((Short)(short)0x5B, Keyboard.KEY_NUMPAD8); nativeToLwjglMap.put((Short)(short)0x5C, Keyboard.KEY_NUMPAD9); - nativeToLwjglMap.put((Short)(short)0x4C, Keyboard.KEY_NUMPADENTER); + + + nativeToLwjglMap.put((Short)(short)0x24, Keyboard.KEY_RETURN); + nativeToLwjglMap.put((Short)(short)0x30, Keyboard.KEY_TAB); + nativeToLwjglMap.put((Short)(short)0x31, Keyboard.KEY_SPACE); + nativeToLwjglMap.put((Short)(short)0x33, Keyboard.KEY_BACK); + nativeToLwjglMap.put((Short)(short)0x35, Keyboard.KEY_ESCAPE); + nativeToLwjglMap.put((Short)(short)0x36, Keyboard.KEY_RMETA); // not in Events.h - works on MBP + nativeToLwjglMap.put((Short)(short)0x37, Keyboard.KEY_LMETA); + nativeToLwjglMap.put((Short)(short)0x38, Keyboard.KEY_LSHIFT); + nativeToLwjglMap.put((Short)(short)0x39, Keyboard.KEY_CAPITAL); + nativeToLwjglMap.put((Short)(short)0x3A, Keyboard.KEY_LMENU); + nativeToLwjglMap.put((Short)(short)0x3B, Keyboard.KEY_LCONTROL); + nativeToLwjglMap.put((Short)(short)0x3C, Keyboard.KEY_RSHIFT); + nativeToLwjglMap.put((Short)(short)0x3D, Keyboard.KEY_RMENU); + nativeToLwjglMap.put((Short)(short)0x3E, Keyboard.KEY_RCONTROL); + + nativeToLwjglMap.put((Short)(short)0x3F, Keyboard.KEY_FUNCTION); + nativeToLwjglMap.put((Short)(short)0x77, Keyboard.KEY_END); + nativeToLwjglMap.put((Short)(short)0x7A, Keyboard.KEY_F1); nativeToLwjglMap.put((Short)(short)0x78, Keyboard.KEY_F2); nativeToLwjglMap.put((Short)(short)0x63, Keyboard.KEY_F3); @@ -147,58 +180,25 @@ final class MacOSXNativeKeyboard extends EventQueue { nativeToLwjglMap.put((Short)(short)0x40, Keyboard.KEY_F17); nativeToLwjglMap.put((Short)(short)0x4F, Keyboard.KEY_F18); nativeToLwjglMap.put((Short)(short)0x50, Keyboard.KEY_F19); - nativeToLwjglMap.put((Short)(short)0x72, Keyboard.KEY_INSERT); - nativeToLwjglMap.put((Short)(short)0x73, Keyboard.KEY_HOME); - nativeToLwjglMap.put((Short)(short)0x77, Keyboard.KEY_END); + // nativeToLwjglMap.put((Short)(short)0x5A, Keyboard.KEY_F20); + nativeToLwjglMap.put((Short)(short)0x75, Keyboard.KEY_DELETE); - nativeToLwjglMap.put((Short)(short)0x18, Keyboard.KEY_EQUALS); + nativeToLwjglMap.put((Short)(short)0x72, Keyboard.KEY_INSERT); // 'Help' in Events.h + nativeToLwjglMap.put((Short)(short)0x73, Keyboard.KEY_HOME); + // nativeToLwjglMap.put((Short)(short)0xA4, Keyboard.KEY_MUTE); + nativeToLwjglMap.put((Short)(short)0x79, Keyboard.KEY_NEXT); + nativeToLwjglMap.put((Short)(short)0x74, Keyboard.KEY_PRIOR); + // nativeToLwjglMap.put((Short)(short)0x49, Keyboard.KEY_VOLUMEDOWN); + // nativeToLwjglMap.put((Short)(short)0x48, Keyboard.KEY_VOLUMEUP); nativeToLwjglMap.put((Short)(short)0x7B, Keyboard.KEY_LEFT); nativeToLwjglMap.put((Short)(short)0x7C, Keyboard.KEY_RIGHT); - nativeToLwjglMap.put((Short)(short)0x7E, Keyboard.KEY_UP); nativeToLwjglMap.put((Short)(short)0x7D, Keyboard.KEY_DOWN); - nativeToLwjglMap.put((Short)(short)0x31, Keyboard.KEY_SPACE); - nativeToLwjglMap.put((Short)(short)0x30, Keyboard.KEY_TAB); - nativeToLwjglMap.put((Short)(short)0x35, Keyboard.KEY_ESCAPE); - nativeToLwjglMap.put((Short)(short)0x74, Keyboard.KEY_PRIOR); - nativeToLwjglMap.put((Short)(short)0x79, Keyboard.KEY_NEXT); - nativeToLwjglMap.put((Short)(short)0x41, Keyboard.KEY_DECIMAL); - nativeToLwjglMap.put((Short)(short)0x43, Keyboard.KEY_MULTIPLY); - nativeToLwjglMap.put((Short)(short)0x45, Keyboard.KEY_ADD); - nativeToLwjglMap.put((Short)(short)0x4B, Keyboard.KEY_DIVIDE); - nativeToLwjglMap.put((Short)(short)0x1B, Keyboard.KEY_MINUS); - nativeToLwjglMap.put((Short)(short)0x4E, Keyboard.KEY_SUBTRACT); - nativeToLwjglMap.put((Short)(short)0x1E, Keyboard.KEY_RBRACKET); - nativeToLwjglMap.put((Short)(short)0x21, Keyboard.KEY_LBRACKET); - nativeToLwjglMap.put((Short)(short)0x33, Keyboard.KEY_BACK); - nativeToLwjglMap.put((Short)(short)0x24, Keyboard.KEY_RETURN); - nativeToLwjglMap.put((Short)(short)0xF0, Keyboard.KEY_CAPITAL); - nativeToLwjglMap.put((Short)(short)0x39, Keyboard.KEY_CAPITAL); - nativeToLwjglMap.put((Short)(short)0xF1, Keyboard.KEY_LSHIFT); - nativeToLwjglMap.put((Short)(short)0x38, Keyboard.KEY_LSHIFT); - nativeToLwjglMap.put((Short)(short)0x3C, Keyboard.KEY_RSHIFT); - nativeToLwjglMap.put((Short)(short)0xF2, Keyboard.KEY_LCONTROL); - nativeToLwjglMap.put((Short)(short)0xF3, Keyboard.KEY_LMENU); - nativeToLwjglMap.put((Short)(short)0x3A, Keyboard.KEY_LMENU); - nativeToLwjglMap.put((Short)(short)0x3D, Keyboard.KEY_RMENU); - nativeToLwjglMap.put((Short)(short)0xF4, Keyboard.KEY_LMETA); - nativeToLwjglMap.put((Short)(short)0xF5, Keyboard.KEY_NUMLOCK); - nativeToLwjglMap.put((Short)(short)0x27, Keyboard.KEY_APOSTROPHE); - - /*KEY_MAP[KeyEvent.VK_ALT_GRAPH] = Keyboard.KEY_RMENU; - KEY_MAP[KeyEvent.VK_AT] = Keyboard.KEY_AT; - KEY_MAP[KeyEvent.VK_BACK_SPACE] = Keyboard.KEY_BACK; - KEY_MAP[KeyEvent.VK_CAPS_LOCK] = Keyboard.KEY_CAPITAL; - KEY_MAP[KeyEvent.VK_COLON] = Keyboard.KEY_COLON; - KEY_MAP[KeyEvent.VK_CONVERT] = Keyboard.KEY_CONVERT; - KEY_MAP[KeyEvent.VK_END] = Keyboard.KEY_END; - KEY_MAP[KeyEvent.VK_INSERT] = Keyboard.KEY_INSERT; - KEY_MAP[KeyEvent.VK_KANA] = Keyboard.KEY_KANA; - KEY_MAP[KeyEvent.VK_KANJI] = Keyboard.KEY_KANJI; - KEY_MAP[KeyEvent.VK_NUM_LOCK] = Keyboard.KEY_NUMLOCK; - KEY_MAP[KeyEvent.VK_PAUSE] = Keyboard.KEY_PAUSE; - KEY_MAP[KeyEvent.VK_SCROLL_LOCK] = Keyboard.KEY_SCROLL; - KEY_MAP[KeyEvent.VK_SEPARATOR] = Keyboard.KEY_DECIMAL; - KEY_MAP[KeyEvent.VK_STOP] = Keyboard.KEY_STOP;*/ + nativeToLwjglMap.put((Short)(short)0x7E, Keyboard.KEY_UP); + + nativeToLwjglMap.put((Short)(short)0x0A, Keyboard.KEY_SECTION); + + nativeToLwjglMap.put((Short)(short)0x6E, Keyboard.KEY_APPS); // not in Events.h + nativeToLwjglMap.put((Short)(short)0x129, Keyboard.KEY_COLON); // not in Events.h -- do we need it? } public void register() { diff --git a/src/native/macosx/context.h b/src/native/macosx/context.h index 4e14a332..681617b0 100644 --- a/src/native/macosx/context.h +++ b/src/native/macosx/context.h @@ -81,8 +81,6 @@ typedef struct { @private NSOpenGLContext* _openGLContext; NSOpenGLPixelFormat* _pixelFormat; - NSUInteger _lastModifierFlags; - NSUInteger _modifierFlags; } + (NSOpenGLPixelFormat*)defaultPixelFormat; diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 06bf21bb..1c819237 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -89,8 +89,6 @@ static NSAutoreleasePool *pool; - (id)initWithFrame:(NSRect)frameRect pixelFormat:(NSOpenGLPixelFormat*)format { self = [super initWithFrame:frameRect]; - _lastModifierFlags = 0; - _modifierFlags = 0; if (self != nil) { _pixelFormat = [format retain]; [[NSNotificationCenter defaultCenter] addObserver:self @@ -198,51 +196,35 @@ static NSAutoreleasePool *pool; return; } long time = [event timestamp] * 1000000000; + + NSUInteger mask = ~0; + switch([event keyCode]) { + case kVK_Control : mask = 0x0001; break; + case kVK_Shift : mask = 0x0002; break; + case kVK_RightShift : mask = 0x0004; break; + case kVK_Command : mask = 0x0008; break; + case 0x36 : mask = 0x0010; break; // Should be: kVK_RightCommand -- missing O.o + case kVK_Option : mask = 0x0020; break; + case kVK_RightOption : mask = 0x0040; break; + case kVK_RightControl: mask = 0x2000; break; + case kVK_CapsLock : mask = NSAlphaShiftKeyMask; break; + case kVK_Function : mask = NSFunctionKeyMask; break; + // case 0x?? : mask = NSNumericPadKeyMask; break; // Didn't have the keycode for this one :( + default: + printf("Unknown modifier with keycode: %d\n", [event keyCode]); + return; + } + jclass keyboard_class = (*env)->GetObjectClass(env, _parent->jkeyboard); - jmethodID keydown = (*env)->GetMethodID(env, keyboard_class, "keyPressed", "(IIJ)V"); - jmethodID keyup = (*env)->GetMethodID(env, keyboard_class, "keyReleased", "(IIJ)V"); - _lastModifierFlags = _modifierFlags; - _modifierFlags = [event modifierFlags]; - NSUInteger flagDown = ~_lastModifierFlags & _modifierFlags; - NSUInteger flagUp = _lastModifierFlags & ~_modifierFlags; - if (flagDown & NSAlphaShiftKeyMask) { - (*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, 0xf0, 0, time); + + jmethodID keyMethod; + if (([event modifierFlags] & mask) == mask) { + keyMethod = (*env)->GetMethodID(env, keyboard_class, "keyPressed", "(IIJ)V"); + } else { + keyMethod = (*env)->GetMethodID(env, keyboard_class, "keyReleased", "(IIJ)V"); } - if (flagUp & NSAlphaShiftKeyMask) { - (*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, 0xf0, 0, time); - } - if (flagDown & NSShiftKeyMask) { - (*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, 0xf1, 0, time); - } - if (flagUp & NSShiftKeyMask) { - (*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, 0xf1, 0, time); - } - if (flagDown & NSControlKeyMask) { - (*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, 0xf2, 0, time); - } - if (flagUp & NSControlKeyMask) { - (*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, 0xf2, 0, time); - } - if (flagDown & NSAlternateKeyMask) { - (*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, 0xf3, 0, time); - } - if (flagUp & NSAlternateKeyMask) { - (*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, 0xf3, 0, time); - } - if (flagDown & NSCommandKeyMask) { - (*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, 0xf4, 0, time); - } - if (flagUp & NSCommandKeyMask) { - (*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, 0xf4, 0, time); - } - if (flagDown & NSNumericPadKeyMask) { - (*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, 0xf5, 0, time); - } - if (flagUp & NSNumericPadKeyMask) { - (*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, 0xf5, 0, time); - } - //const char* charbuf = [[event characters] cStringUsingEncoding:NSASCIIStringEncoding]; - //(*env)->CallVoidMethod(env, _parent->jkeyboard, keymod, (jint)[event keyCode], (jint)charbuf[0], time); + + (*env)->CallVoidMethod(env, _parent->jkeyboard, keyMethod, [event keyCode], 0, time); } - (void)mouseButtonState:(NSEvent *)event :(int)button :(int)state { From b6a89d3bd415b20125e97cda9808b779d5164948 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Fri, 30 Nov 2012 01:02:04 +0000 Subject: [PATCH 032/111] Correct Mouse Coordinates in Fullscreen Mode --- src/native/macosx/org_lwjgl_opengl_Display.m | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 1c819237..35955093 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -270,7 +270,7 @@ static NSAutoreleasePool *pool; long time = [event timestamp] * 1000000000; jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); - NSPoint loc = [self convertPoint:[event locationInWindow] toView:self]; + NSPoint loc = [self convertPoint:[event locationInWindow] toView:nil]; (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time); } @@ -282,7 +282,7 @@ static NSAutoreleasePool *pool; long time = [event timestamp] * 1000000000; jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); - NSPoint loc = [self convertPoint:[event locationInWindow] toView:self]; + NSPoint loc = [self convertPoint:[event locationInWindow] toView:nil]; (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time); } @@ -294,7 +294,7 @@ static NSAutoreleasePool *pool; long time = [event timestamp] * 1000000000; jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); - NSPoint loc = [self convertPoint:[event locationInWindow] toView:self]; + NSPoint loc = [self convertPoint:[event locationInWindow] toView:nil]; (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time); } @@ -306,7 +306,7 @@ static NSAutoreleasePool *pool; long time = [event timestamp] * 1000000000; jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); - NSPoint loc = [self convertPoint:[event locationInWindow] toView:self]; + NSPoint loc = [self convertPoint:[event locationInWindow] toView:nil]; (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time); } @@ -318,7 +318,7 @@ static NSAutoreleasePool *pool; long time = [event timestamp] * 1000000000; jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); - NSPoint loc = [self convertPoint:[event locationInWindow] toView:self]; + NSPoint loc = [self convertPoint:[event locationInWindow] toView:nil]; (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 1.0f, time); } @@ -344,11 +344,11 @@ static NSAutoreleasePool *pool; } } -- (void) drawRect:(NSRect)backgroundColor { +- (void) drawRect:(NSRect)rect { // set black as the default background color // for the nsview to avoid white flash on fullscreen [[NSColor blackColor] setFill]; - NSRectFill(backgroundColor); + NSRectFill(rect); } @end @@ -461,6 +461,11 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE else { [window_info->view enterFullScreenMode: [NSScreen mainScreen] withOptions: nil ]; window_info->window = [window_info->view window]; + + // adjust the NSView bounds to correct mouse coordinates in fullscreen + NSSize windowSize = [window_info->window frame].size; + NSSize newBounds = NSMakeSize(windowSize.width/width*windowSize.width, windowSize.height/height*windowSize.height); + [window_info->view setBoundsSize:newBounds]; } // Inform the view of its parent window info; used to register for window-close callbacks From b72cdb3064b7a0c362cb2a25ffc8fafe98984d6c Mon Sep 17 00:00:00 2001 From: kappaOne Date: Fri, 30 Nov 2012 01:11:03 +0000 Subject: [PATCH 033/111] Wait for NSView to be correctly removed before closing window --- src/native/macosx/org_lwjgl_opengl_Display.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 35955093..397c99b3 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -487,7 +487,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); if (window_info->view != nil) { - [window_info->view removeFromSuperviewWithoutNeedingDisplay]; + [window_info->view performSelectorOnMainThread:@selector(removeFromSuperviewWithoutNeedingDisplay) withObject:nil waitUntilDone:YES]; } if (window_info->fullscreen) { From b3b5e95bf4655bf78e0f2970bd427d44272a5fc2 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Fri, 30 Nov 2012 23:24:12 +0000 Subject: [PATCH 034/111] Use a fixed back buffer for proper resolution in fullscreen mode --- src/native/macosx/org_lwjgl_opengl_Display.m | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 397c99b3..045b57ec 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -457,8 +457,18 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE [window_info->window setContentView:window_info->view]; + // disable any fixed backbuffer size to allow resizing + CGLContextObj cgcontext = (CGLContextObj)[[window_info->view openGLContext] CGLContextObj]; + CGLDisable(cgcontext, kCGLCESurfaceBackingSize); } else { + // set a fixed backbuffer size for fullscreen + CGLContextObj cgcontext = (CGLContextObj)[[window_info->view openGLContext] CGLContextObj]; + GLint dim[2] = {width, height}; + CGLSetParameter(cgcontext, kCGLCPSurfaceBackingSize, dim); + CGLEnable(cgcontext, kCGLCESurfaceBackingSize); + + // enter fullscreen mode [window_info->view enterFullScreenMode: [NSScreen mainScreen] withOptions: nil ]; window_info->window = [window_info->view window]; From 415f25bf97bb1c96f4c564d89920ca1bbf6b62b9 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Mon, 3 Dec 2012 21:39:54 +0000 Subject: [PATCH 035/111] Do Context clean up on proper thread, remove hacks, enable memory clean up on Mac Display.destroy() --- src/native/macosx/org_lwjgl_opengl_Display.m | 8 ++------ .../macosx/org_lwjgl_opengl_MacOSXContextImplementation.m | 4 +++- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 045b57ec..a1ba898f 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -135,7 +135,7 @@ static NSAutoreleasePool *pool; NSOpenGLContext* context = [self openGLContext]; [super lockFocus]; - if ([context view] != self && [context view] != nil) { + if ([context view] != self) { [context setView:self]; } @@ -496,10 +496,6 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv *env, jobject this, jobject window_handle) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - if (window_info->view != nil) { - [window_info->view performSelectorOnMainThread:@selector(removeFromSuperviewWithoutNeedingDisplay) withObject:nil waitUntilDone:YES]; - } - if (window_info->fullscreen) { [window_info->view exitFullScreenModeWithOptions: nil]; } @@ -509,7 +505,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv } } - //[pool drain]; + [pool drain]; } JNIEXPORT jint JNICALL Java_org_lwjgl_DefaultSysImplementation_getJNIVersion diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m index ad0faeb4..528b42ec 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m @@ -168,7 +168,9 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nDestro (JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; - [[peer_info->window_info->view openGLContext] clearDrawable]; + + // clearDrawable on main thread to ensure its not in use + [[peer_info->window_info->view openGLContext] performSelectorOnMainThread:@selector(clearDrawable) withObject:nil waitUntilDone:YES]; [[peer_info->window_info->view openGLContext] release]; [pool release]; } From 804f09da47f8ae8a77879bb40dee6cf02c3e92f0 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Mon, 3 Dec 2012 23:04:30 +0000 Subject: [PATCH 036/111] Fixed resizing when using Display.setParent --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 5 +++-- src/native/macosx/org_lwjgl_opengl_Display.m | 13 +++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 9fde3262..80f0b1eb 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -90,7 +90,7 @@ final class MacOSXDisplay implements DisplayImplementation { } - private native ByteBuffer nCreateWindow(int x, int y, int width, int height, boolean fullscreen, boolean undecorated, boolean resizable, ByteBuffer peer_info_handle, ByteBuffer window_handle) throws LWJGLException; + private native ByteBuffer nCreateWindow(int x, int y, int width, int height, boolean fullscreen, boolean undecorated, boolean resizable, boolean parented, ByteBuffer peer_info_handle, ByteBuffer window_handle) throws LWJGLException; private native boolean nIsMiniaturized(ByteBuffer window_handle); @@ -113,6 +113,7 @@ final class MacOSXDisplay implements DisplayImplementation { public void createWindow(final DrawableLWJGL drawable, DisplayMode mode, Canvas parent, int x, int y) throws LWJGLException { boolean fullscreen = Display.isFullscreen(); boolean resizable = Display.isResizable(); + boolean parented = (parent != null); close_requested = false; @@ -122,7 +123,7 @@ final class MacOSXDisplay implements DisplayImplementation { try { window = nCreateWindow(x, y, mode.getWidth(), mode.getHeight(), fullscreen, isUndecorated(), resizable, - peer_handle, window); + parented, peer_handle, window); this.x = x; this.y = y; this.width = mode.getWidth(); diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index a1ba898f..ea46ddec 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -158,10 +158,6 @@ static NSAutoreleasePool *pool; - (void)setParent:(MacOSXWindowInfo*)parent { _parent = parent; - // Set this NSView as delegate to get native window close events for windowShouldClose method - if (_parent != nil) { - [_parent->window setDelegate:self]; - } } - (void)keyDown:(NSEvent *)event { @@ -407,7 +403,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nSetTitle(JNIEnv *env [window_info->window setTitle:title]; } -JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIEnv *env, jobject this, jint x, jint y, jint width, jint height, jboolean fullscreen, jboolean undecorated, jboolean resizable, jobject peer_info_handle, jobject window_handle) { +JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIEnv *env, jobject this, jint x, jint y, jint width, jint height, jboolean fullscreen, jboolean undecorated, jboolean resizable, jboolean parented, jobject peer_info_handle, jobject window_handle) { if (window_handle == NULL) { window_handle = newJavaManagedByteBuffer(env, sizeof(MacOSXWindowInfo)); @@ -457,6 +453,11 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE [window_info->window setContentView:window_info->view]; + if (!parented) { + // set NSView as delegate of NSWindow to get windowShouldClose events + [window_info->window setDelegate:window_info->view]; + } + // disable any fixed backbuffer size to allow resizing CGLContextObj cgcontext = (CGLContextObj)[[window_info->view openGLContext] CGLContextObj]; CGLDisable(cgcontext, kCGLCESurfaceBackingSize); @@ -478,7 +479,7 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE [window_info->view setBoundsSize:newBounds]; } - // Inform the view of its parent window info; used to register for window-close callbacks + // Inform the view of its parent window info; [window_info->view setParent:window_info]; [window_info->window performSelectorOnMainThread:@selector(makeFirstResponder:) withObject:window_info->view waitUntilDone:NO]; From 76238f038c52004ad3977ba346b1f0acb7e111ad Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 4 Dec 2012 22:11:16 +0000 Subject: [PATCH 037/111] remove unneeded call to release memory on close of window --- src/native/macosx/org_lwjgl_opengl_Display.m | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index ea46ddec..1fa43c86 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -467,7 +467,7 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE CGLContextObj cgcontext = (CGLContextObj)[[window_info->view openGLContext] CGLContextObj]; GLint dim[2] = {width, height}; CGLSetParameter(cgcontext, kCGLCPSurfaceBackingSize, dim); - CGLEnable(cgcontext, kCGLCESurfaceBackingSize); + CGLEnable(cgcontext, kCGLCESurfaceBackingSize); // enter fullscreen mode [window_info->view enterFullScreenMode: [NSScreen mainScreen] withOptions: nil ]; @@ -480,12 +480,11 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE } // Inform the view of its parent window info; - [window_info->view setParent:window_info]; + [window_info->view setParent:window_info]; [window_info->window performSelectorOnMainThread:@selector(makeFirstResponder:) withObject:window_info->view waitUntilDone:NO]; [window_info->window performSelectorOnMainThread:@selector(setInitialFirstResponder:) withObject:window_info->view waitUntilDone:NO]; [window_info->window performSelectorOnMainThread:@selector(makeKeyAndOrderFront:) withObject:[NSApplication sharedApplication] waitUntilDone:NO]; - [window_info->window performSelectorOnMainThread:@selector(setReleasedWhenClosed:) withObject:window_info->window waitUntilDone:NO]; window_info->fullscreen = fullscreen; From 83aeba591036ef953bd11c87d62c35d942d555f2 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 4 Dec 2012 22:22:42 +0000 Subject: [PATCH 038/111] remove use of depreciated CGTableCount --- src/native/macosx/org_lwjgl_opengl_Display.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 1fa43c86..5322cd4c 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -519,7 +519,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_restoreGamma(JNIEnv * JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_setGammaRamp(JNIEnv *env, jobject this, jobject gamma_buffer) { const CGGammaValue *values = (*env)->GetDirectBufferAddress(env, gamma_buffer); - CGTableCount table_size = (*env)->GetDirectBufferCapacity(env, gamma_buffer); + uint32_t table_size = (*env)->GetDirectBufferCapacity(env, gamma_buffer); CGDisplayErr err = CGSetDisplayTransferByTable(kCGDirectMainDisplay, table_size, values, values, values); if (err != CGDisplayNoErr) { throwException(env, "Could not set display gamma"); From 11d6db5f35e211294bfbcea88387426208af913a Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 4 Dec 2012 22:28:35 +0000 Subject: [PATCH 039/111] remove use of the now depreciated CGTableCount --- src/native/macosx/org_lwjgl_opengl_Display.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 5322cd4c..1fa43c86 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -519,7 +519,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_restoreGamma(JNIEnv * JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_setGammaRamp(JNIEnv *env, jobject this, jobject gamma_buffer) { const CGGammaValue *values = (*env)->GetDirectBufferAddress(env, gamma_buffer); - uint32_t table_size = (*env)->GetDirectBufferCapacity(env, gamma_buffer); + CGTableCount table_size = (*env)->GetDirectBufferCapacity(env, gamma_buffer); CGDisplayErr err = CGSetDisplayTransferByTable(kCGDirectMainDisplay, table_size, values, values, values); if (err != CGDisplayNoErr) { throwException(env, "Could not set display gamma"); From 8c4aeef4f4f09c65af5bbf1c73837841872a324a Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 4 Dec 2012 22:45:47 +0000 Subject: [PATCH 040/111] remove use of depreciated CGTableCount --- src/native/macosx/org_lwjgl_opengl_Display.m | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 1fa43c86..779888cf 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -368,21 +368,21 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nResizeWindow(JNIEnv JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nWasResized(JNIEnv *env, jobject this, jobject window_handle) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - jboolean was_resized = window_info->resized; - window_info->resized = JNI_FALSE; - return was_resized; + jboolean was_resized = window_info->resized; + window_info->resized = JNI_FALSE; + return was_resized; } JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nGetWidth(JNIEnv *env, jobject this, jobject window_handle) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - jint width = window_info->display_rect.size.width; - return width; + jint width = window_info->display_rect.size.width; + return width; } JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nGetHeight(JNIEnv *env, jobject this, jobject window_handle) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - jint height = window_info->display_rect.size.height; - return height; + jint height = window_info->display_rect.size.height; + return height; } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nSetResizable(JNIEnv *env, jobject this, jobject window_handle, jboolean resizable) { @@ -519,7 +519,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_restoreGamma(JNIEnv * JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_setGammaRamp(JNIEnv *env, jobject this, jobject gamma_buffer) { const CGGammaValue *values = (*env)->GetDirectBufferAddress(env, gamma_buffer); - CGTableCount table_size = (*env)->GetDirectBufferCapacity(env, gamma_buffer); + uint32_t table_size = (*env)->GetDirectBufferCapacity(env, gamma_buffer); CGDisplayErr err = CGSetDisplayTransferByTable(kCGDirectMainDisplay, table_size, values, values, values); if (err != CGDisplayNoErr) { throwException(env, "Could not set display gamma"); From c2081c5c1aba09072846fcbca2c53a8a4bb300a7 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Wed, 5 Dec 2012 23:08:53 +0000 Subject: [PATCH 041/111] Remove old/redundant code for OS X 10.4 or lower. --- src/java/org/lwjgl/opengl/MacOSXPeerInfo.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXPeerInfo.java b/src/java/org/lwjgl/opengl/MacOSXPeerInfo.java index 74ba523c..17942a3b 100644 --- a/src/java/org/lwjgl/opengl/MacOSXPeerInfo.java +++ b/src/java/org/lwjgl/opengl/MacOSXPeerInfo.java @@ -45,9 +45,7 @@ import org.lwjgl.LWJGLUtil; abstract class MacOSXPeerInfo extends PeerInfo { MacOSXPeerInfo(PixelFormat pixel_format, ContextAttribs attribs, boolean use_display_bpp, boolean support_window, boolean support_pbuffer, boolean double_buffered) throws LWJGLException { super(createHandle()); - if (pixel_format.isFloatingPoint() && !LWJGLUtil.isMacOSXEqualsOrBetterThan(10, 4)) - throw new LWJGLException("Floating point pixel format requested, but it requires MacOS X 10.4 or newer"); - + boolean gl32 = attribs != null && attribs.getMajorVersion() == 3 && attribs.getMinorVersion() == 2 && attribs.isProfileCore(); if ( gl32 && !LWJGLUtil.isMacOSXEqualsOrBetterThan(10, 7) ) throw new LWJGLException("OpenGL 3.2 requested, but it requires MacOS X 10.7 or newer"); From 2cc6affcd1bfeed198db5086b32c9c3838de18aa Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 9 Dec 2012 15:39:26 +0000 Subject: [PATCH 042/111] Use correct back buffer size when creating a new context and going straight into fullscreen --- src/native/macosx/org_lwjgl_opengl_Display.m | 5 ++--- ...org_lwjgl_opengl_MacOSXContextImplementation.m | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 779888cf..8d76e87e 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -431,6 +431,7 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE [[NSApplication sharedApplication] setDelegate:window_info->view]; + window_info->display_rect = NSMakeRect(x, y, width, height); if (!fullscreen) { @@ -444,8 +445,6 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE default_window_mask |= NSResizableWindowMask; } - window_info->display_rect = NSMakeRect(x, y, width, height); - window_info->window = (MacOSXKeyableWindow*)[[NSApplication sharedApplication] mainWindow]; if (window_info->window == nil) { window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:default_window_mask backing:NSBackingStoreBuffered defer:YES]; @@ -464,7 +463,7 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE } else { // set a fixed backbuffer size for fullscreen - CGLContextObj cgcontext = (CGLContextObj)[[window_info->view openGLContext] CGLContextObj]; + CGLContextObj cgcontext = (CGLContextObj)[window_info->context CGLContextObj]; GLint dim[2] = {width, height}; CGLSetParameter(cgcontext, kCGLCPSurfaceBackingSize, dim); CGLEnable(cgcontext, kCGLCESurfaceBackingSize); diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m index 528b42ec..e135e193 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m @@ -75,6 +75,21 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nCre throwException(env, "Could not create context"); return NULL; } + + if (peer_info->window_info->fullscreen) { + // set a fixed backbuffer size for fullscreen + CGLContextObj cgcontext = (CGLContextObj)[context CGLContextObj]; + NSSize displaySize = peer_info->window_info->display_rect.size; + GLint dim[2] = {displaySize.width, displaySize.height}; + CGLSetParameter(cgcontext, kCGLCPSurfaceBackingSize, dim); + CGLEnable(cgcontext, kCGLCESurfaceBackingSize); + } + else { + // disable any fixed backbuffer size to allow resizing + CGLContextObj cgcontext = (CGLContextObj)[context CGLContextObj]; + CGLDisable(cgcontext, kCGLCESurfaceBackingSize); + } + [peer_info->window_info->view setOpenGLContext:context]; peer_info->window_info->context = context; context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); From 07499c30dd1a6b7bc0632b4cd9492bcf436e3ba8 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 9 Dec 2012 16:27:19 +0000 Subject: [PATCH 043/111] fix default viewport size when switching to fullscreen mode --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 40 ++++++++------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 80f0b1eb..0689c7cb 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -110,7 +110,7 @@ final class MacOSXDisplay implements DisplayImplementation { return Display.getPrivilegedBoolean("org.lwjgl.opengl.Window.undecorated"); } - public void createWindow(final DrawableLWJGL drawable, DisplayMode mode, Canvas parent, int x, int y) throws LWJGLException { + public void createWindow(final DrawableLWJGL drawable, DisplayMode mode, Canvas parent, int x, int y) throws LWJGLException { boolean fullscreen = Display.isFullscreen(); boolean resizable = Display.isResizable(); boolean parented = (parent != null); @@ -129,6 +129,16 @@ final class MacOSXDisplay implements DisplayImplementation { this.width = mode.getWidth(); this.height = mode.getHeight(); this.canvas = parent; + + if (fullscreen) { + // when going to fullscreen viewport is set to screen size by Cocoa, ignore this value + skipViewportValue = true; + // if starting in fullscreen then set initial viewport to displaymode size + if (current_viewport.get(2) == 0 && current_viewport.get(3) == 0) { + current_viewport.put(2, width); + current_viewport.put(3, height); + } + } } catch (LWJGLException e) { destroyWindow(); throw e; @@ -262,35 +272,17 @@ final class MacOSXDisplay implements DisplayImplementation { } } - private static final IntBuffer current_viewport = BufferUtils.createIntBuffer(16); + private boolean skipViewportValue = false; + private static final IntBuffer current_viewport = BufferUtils.createIntBuffer(16); public void update() { boolean should_update = true; - /* - * 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 - */ + DrawableGL drawable = (DrawableGL)Display.getDrawable(); if (should_update) { drawable.context.update(); /* This is necessary to make sure the context won't "forget" about the view size */ - glGetInteger(GL_VIEWPORT, current_viewport); + if (skipViewportValue) skipViewportValue = false; + else glGetInteger(GL_VIEWPORT, current_viewport); glViewport(current_viewport.get(0), current_viewport.get(1), current_viewport.get(2), current_viewport.get(3)); } } From 2a820ed94faf9a3d71037c1bb3e47ae5f54f6927 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 9 Dec 2012 21:22:45 +0000 Subject: [PATCH 044/111] remove unused variable --- src/native/macosx/context.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/native/macosx/context.h b/src/native/macosx/context.h index 681617b0..34a69571 100644 --- a/src/native/macosx/context.h +++ b/src/native/macosx/context.h @@ -63,9 +63,6 @@ typedef struct { jobject jkeyboard; jboolean fullscreen; jboolean resized; - - // Cached for window creation - NSApplicationPresentationOptions window_options; } MacOSXWindowInfo; @interface MacOSXKeyableWindow : NSWindow From ccf738dfafc69650f24d65bdfa7ff9042cdeda22 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 9 Dec 2012 22:12:02 +0000 Subject: [PATCH 045/111] Implement OS X Mouse.setCursorPosition() when in fullscreen mode --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 2 +- .../org/lwjgl/opengl/MacOSXNativeMouse.java | 6 ++--- .../org_lwjgl_opengl_MacOSXNativeMouse.m | 22 ++++++++++++++----- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 0689c7cb..29f7dee0 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -334,7 +334,7 @@ final class MacOSXDisplay implements DisplayImplementation { public void setCursorPosition(int x, int y) { if (mouse != null) { - mouse.warpCursor(x, y); + mouse.setCursorPosition(x, y); } //MacOSXMouseEventQueue.nWarpCursor(x, y); } diff --git a/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java b/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java index 12415a35..0e1424e2 100644 --- a/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java +++ b/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java @@ -80,7 +80,7 @@ final class MacOSXNativeMouse extends EventQueue { this.window_handle = window_handle; } - private native void nWarpCursor(ByteBuffer window_handle, int x, int y); + private native void nSetCursorPosition(ByteBuffer window_handle, int x, int y); public static native void nGrabMouse(boolean grab); @@ -92,8 +92,8 @@ final class MacOSXNativeMouse extends EventQueue { nRegisterMouseListener(window_handle); } - public synchronized void warpCursor(int x, int y) { - nWarpCursor(window_handle, x, y); + public synchronized void setCursorPosition(int x, int y) { + nSetCursorPosition(window_handle, x, y); } public synchronized void unregister() { diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m index db774a89..7c531649 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m @@ -55,13 +55,25 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nGrabMouse(JNIEnv CGDisplayShowCursor(kCGDirectMainDisplay); } -JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nWarpCursor(JNIEnv *env, jclass this, jobject window_handle, jint x, jint y) { +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nSetCursorPosition(JNIEnv *env, jclass this, jobject window_handle, jint x, jint y) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - NSPoint point = NSMakePoint(x, y); - point = [window_info->view convertPoint:point fromView:window_info->view]; + CGPoint p; - p.x = point.x; - p.y = point.y; + + if (window_info->fullscreen) { + NSSize screenSize = [[NSScreen mainScreen] frame].size; + NSSize displaySize = window_info->display_rect.size; + + p.x = (screenSize.width / displaySize.width) * x; + p.y = (screenSize.height / displaySize.height) * y; + } + else { + NSPoint point = NSMakePoint(x, y); + + p.x = point.x; + p.y = point.y; + } + CGWarpMouseCursorPosition(p); } From 93d1d3ea037296121f6b8262431fa8b62b496686 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 11 Dec 2012 21:24:04 +0000 Subject: [PATCH 046/111] fix 'invalid drawable' error message --- src/native/macosx/org_lwjgl_opengl_Display.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 8d76e87e..85b70306 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -447,7 +447,7 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE window_info->window = (MacOSXKeyableWindow*)[[NSApplication sharedApplication] mainWindow]; if (window_info->window == nil) { - window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:default_window_mask backing:NSBackingStoreBuffered defer:YES]; + window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:default_window_mask backing:NSBackingStoreBuffered defer:NO]; } [window_info->window setContentView:window_info->view]; From a576c3fb0fd69b9e13b4ffbc188daef3d7cd294b Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 11 Dec 2012 21:30:04 +0000 Subject: [PATCH 047/111] fix mouse scroll wheel direction --- src/java/org/lwjgl/opengl/MacOSXNativeMouse.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java b/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java index 0e1424e2..8736696b 100644 --- a/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java +++ b/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java @@ -188,7 +188,7 @@ final class MacOSXNativeMouse extends EventQueue { setCursorPos(x, y, nanos); } if ( dz != 0 ) { - int wheel_amount = (int)(-dy * WHEEL_SCALE); + int wheel_amount = (int)(dy * WHEEL_SCALE); accum_dz += wheel_amount; putMouseEvent((byte)-1, (byte)0, wheel_amount, nanos); } From 970fc2d7d932d3f7aac87864b0a8db2e98fdf773 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 11 Dec 2012 22:59:30 +0000 Subject: [PATCH 048/111] Implement OS X Mouse.setCursorPosition() for windowed mode --- .../org_lwjgl_opengl_MacOSXNativeMouse.m | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m index 7c531649..e35c34a5 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m @@ -61,18 +61,29 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nSetCursorPositio CGPoint p; if (window_info->fullscreen) { - NSSize screenSize = [[NSScreen mainScreen] frame].size; - NSSize displaySize = window_info->display_rect.size; - - p.x = (screenSize.width / displaySize.width) * x; - p.y = (screenSize.height / displaySize.height) * y; - } - else { NSPoint point = NSMakePoint(x, y); + // convert point to window/screen coordinates + point = [window_info->view convertPoint:point fromView:nil]; + p.x = point.x; p.y = point.y; } + else { + NSRect screenRect = [[NSScreen mainScreen] frame]; + NSRect viewRect = [window_info->view frame]; + NSRect winRect = [window_info->window frame]; + + // get window coords of the view origin + NSPoint viewPoint = [window_info->view convertPoint:viewRect.origin fromView:nil]; + + // convert y to screen coordinates, origin bottom left + p.y = winRect.origin.y + viewPoint.y + (viewRect.size.height - y - 1); + + p.x = winRect.origin.x + viewPoint.x + x; + // flip y coordinates (origin top left) to allow use with CGWarpMouseCursorPosition + p.y = screenRect.size.height - p.y - 1; + } CGWarpMouseCursorPosition(p); } From 70842b92aaac4f12b8b67f8337dcd68dbe772b21 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 16 Dec 2012 16:02:40 +0000 Subject: [PATCH 049/111] Implement Display.getX() and Display.getY() for OS X --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 20 ++++++++------------ src/native/macosx/org_lwjgl_opengl_Display.m | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 29f7dee0..32ee96a4 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -79,10 +79,6 @@ final class MacOSXDisplay implements DisplayImplementation { private MacOSXNativeKeyboard keyboard; private ByteBuffer window; private ByteBuffer context; - private int x; - private int y; - private int width; - private int height; private boolean close_requested; @@ -102,6 +98,10 @@ final class MacOSXDisplay implements DisplayImplementation { private native boolean nWasResized(ByteBuffer window_handle); + private native int nGetX(ByteBuffer window_handle); + + private native int nGetY(ByteBuffer window_handle); + private native int nGetWidth(ByteBuffer window_handle); private native int nGetHeight(ByteBuffer window_handle); @@ -124,10 +124,6 @@ final class MacOSXDisplay implements DisplayImplementation { window = nCreateWindow(x, y, mode.getWidth(), mode.getHeight(), fullscreen, isUndecorated(), resizable, parented, peer_handle, window); - this.x = x; - this.y = y; - this.width = mode.getWidth(); - this.height = mode.getHeight(); this.canvas = parent; if (fullscreen) { @@ -135,8 +131,8 @@ final class MacOSXDisplay implements DisplayImplementation { skipViewportValue = true; // if starting in fullscreen then set initial viewport to displaymode size if (current_viewport.get(2) == 0 && current_viewport.get(3) == 0) { - current_viewport.put(2, width); - current_viewport.put(3, height); + current_viewport.put(2, mode.getWidth()); + current_viewport.put(3, mode.getHeight()); } } } catch (LWJGLException e) { @@ -453,11 +449,11 @@ final class MacOSXDisplay implements DisplayImplementation { } public int getX() { - return x; + return nGetX(window); } public int getY() { - return y; + return nGetY(window); } public int getWidth() { diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 85b70306..c9c0e41d 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -396,6 +396,23 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nSetResizable(JNIEnv [window_info->window setStyleMask:style_mask]; } +JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nGetX(JNIEnv *env, jobject this, jobject window_handle) { + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + jint x = [[window_info->view window] frame].origin.x; + return x; +} + +JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nGetY(JNIEnv *env, jobject this, jobject window_handle) { + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + + NSRect screenRect = [[NSScreen mainScreen] frame]; + NSRect winRect = [[window_info->view window] frame]; + + // get top corner of window frame, also flip coords so origin is in top left + jint y = screenRect.size.height - (winRect.origin.y + winRect.size.height) - 1; + return y; +} + JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nSetTitle(JNIEnv *env, jobject this, jobject window_handle, jobject title_buffer) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); const char *title_cstr = (const char *)(*env)->GetDirectBufferAddress(env, title_buffer); From 45992fc378ebb5133e588a5254fd99e6a0418311 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 16 Dec 2012 16:19:44 +0000 Subject: [PATCH 050/111] Implement Display.isActive() on OS X --- src/native/macosx/org_lwjgl_opengl_Display.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index c9c0e41d..0226388d 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -354,9 +354,9 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nIsMiniaturized(J } JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nIsFocused(JNIEnv *env, jobject this, jobject window_handle) { - return JNI_TRUE; - //MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - //return (jboolean)([window_info->window isKeyWindow] || [window_info->window isMainWindow]); + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + // Display is focused if nswindow is key window and nsview is first responder in that nswindow + return (jboolean)([[window_info->view window] isKeyWindow] && [[window_info->view window] firstResponder] == window_info->view); } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nResizeWindow(JNIEnv *env, jobject this, jobject window_handle, jint x, jint y, jint width, jint height) { From 21f994a0391c416468141763707e5a98a57ae8d3 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 16 Dec 2012 16:24:33 +0000 Subject: [PATCH 051/111] remove unused code --- src/native/macosx/org_lwjgl_opengl_Display.m | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 0226388d..f6510bf8 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -446,8 +446,6 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE [window_info->view setOpenGLContext:window_info->context]; } - [[NSApplication sharedApplication] setDelegate:window_info->view]; - window_info->display_rect = NSMakeRect(x, y, width, height); if (!fullscreen) { From d235ae5bf06fece5842d7490c824ff13c1fe89a7 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 16 Dec 2012 17:50:48 +0000 Subject: [PATCH 052/111] Fix crash on exit when using Display.setParent() on OS X --- src/native/macosx/org_lwjgl_opengl_Display.m | 106 ++++++++---------- ...lwjgl_opengl_MacOSXContextImplementation.m | 10 +- 2 files changed, 55 insertions(+), 61 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index f6510bf8..5145d5e6 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -62,16 +62,15 @@ static NSAutoreleasePool *pool; @implementation MacOSXOpenGLView -+ (NSOpenGLPixelFormat*)defaultPixelFormat -{ - NSOpenGLPixelFormatAttribute defaultAttribs[] = { ++ (NSOpenGLPixelFormat*)defaultPixelFormat { + NSOpenGLPixelFormatAttribute defaultAttribs[] = { NSOpenGLPFADoubleBuffer, NSOpenGLPFADepthSize, 16, NSOpenGLPFAColorSize, 32, 0 }; - if (default_format == nil) { - default_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:defaultAttribs]; + if (default_format == nil) { + default_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:defaultAttribs]; } return default_format; } @@ -86,91 +85,84 @@ static NSAutoreleasePool *pool; return NO; } -- (id)initWithFrame:(NSRect)frameRect pixelFormat:(NSOpenGLPixelFormat*)format -{ - self = [super initWithFrame:frameRect]; - if (self != nil) { - _pixelFormat = [format retain]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(_surfaceNeedsUpdate:) - name:NSViewGlobalFrameDidChangeNotification - object:self]; +- (id)initWithFrame:(NSRect)frameRect pixelFormat:(NSOpenGLPixelFormat*)format { + self = [super initWithFrame:frameRect]; + if (self != nil) { + _pixelFormat = [format retain]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(_surfaceNeedsUpdate:) + name:NSViewGlobalFrameDidChangeNotification + object:self]; } return self; } -- (void) _surfaceNeedsUpdate:(NSNotification*)notification -{ +- (void) _surfaceNeedsUpdate:(NSNotification*)notification { [self update]; } -- (void)setOpenGLContext:(NSOpenGLContext*)context -{ +- (void)setOpenGLContext:(NSOpenGLContext*)context { _openGLContext = context; } -- (NSOpenGLContext*)openGLContext -{ +- (NSOpenGLContext*)openGLContext { return _openGLContext; } -- (void)clearGLContext -{ - [_openGLContext release]; - _openGLContext = nil; +- (void)clearGLContext { + [_openGLContext release]; + _openGLContext = nil; } -- (void)prepareOpenGL -{ - +- (void)prepareOpenGL { + } -- (void)update -{ - [_openGLContext update]; +- (void)update { + [_openGLContext update]; } -- (void)lockFocus -{ - NSOpenGLContext* context = [self openGLContext]; - - [super lockFocus]; +- (void)lockFocus { + [super lockFocus]; + + NSOpenGLContext* context = [self openGLContext]; + + if (context == nil) return; + if ([context view] != self) { - [context setView:self]; - } - - [context makeCurrentContext]; + [context setView:self]; + } + + [context makeCurrentContext]; } -- (void)setPixelFormat:(NSOpenGLPixelFormat*)pixelFormat -{ - _pixelFormat = [pixelFormat retain]; +- (void)setPixelFormat:(NSOpenGLPixelFormat*)pixelFormat { + _pixelFormat = [pixelFormat retain]; } -- (NSOpenGLPixelFormat*)pixelFormat -{ - return _pixelFormat; +- (NSOpenGLPixelFormat*)pixelFormat { + return _pixelFormat; } - (BOOL)acceptsFirstResponder { - return YES; + return YES; } - (void)setParent:(MacOSXWindowInfo*)parent { - _parent = parent; + _parent = parent; } - (void)keyDown:(NSEvent *)event { - JNIEnv *env = attachCurrentThread(); - if (env == nil || event == nil || _parent == nil || _parent->jkeyboard == nil) { - return; - } - long time = [event timestamp] * 1000000000; - jclass keyboard_class = (*env)->GetObjectClass(env, _parent->jkeyboard); - jmethodID keydown = (*env)->GetMethodID(env, keyboard_class, "keyPressed", "(IIJ)V"); - const char* charbuf = [[event characters] cStringUsingEncoding:NSASCIIStringEncoding]; - int charcode = (charbuf == nil) ? 0 : charbuf[0]; - (*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, [event keyCode], charcode, time); + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil || _parent->jkeyboard == nil) { + return; + } + long time = [event timestamp] * 1000000000; + jclass keyboard_class = (*env)->GetObjectClass(env, _parent->jkeyboard); + jmethodID keydown = (*env)->GetMethodID(env, keyboard_class, "keyPressed", "(IIJ)V"); + const char* charbuf = [[event characters] cStringUsingEncoding:NSASCIIStringEncoding]; + int charcode = (charbuf == nil) ? 0 : charbuf[0]; + (*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, [event keyCode], charcode, time); } - (void)keyUp:(NSEvent *)event { diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m index e135e193..4abf8ba4 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m @@ -182,10 +182,12 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nSetSwa JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nDestroy (JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; - + + MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); // clearDrawable on main thread to ensure its not in use - [[peer_info->window_info->view openGLContext] performSelectorOnMainThread:@selector(clearDrawable) withObject:nil waitUntilDone:YES]; - [[peer_info->window_info->view openGLContext] release]; + [context_info->context performSelectorOnMainThread:@selector(clearDrawable) withObject:nil waitUntilDone:YES]; + [context_info->peer_info->window_info->view setOpenGLContext:nil]; + [context_info->context release]; + [pool release]; } From f7c64c89dce6e412d1e3905514654f5832ae5186 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 18 Dec 2012 22:10:44 +0000 Subject: [PATCH 053/111] Don't use Display.setParent when fullscreen set, some refactoring --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 2 +- src/native/macosx/org_lwjgl_opengl_Display.m | 363 +++++++++---------- 2 files changed, 182 insertions(+), 183 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 32ee96a4..459080fc 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -113,7 +113,7 @@ final class MacOSXDisplay implements DisplayImplementation { public void createWindow(final DrawableLWJGL drawable, DisplayMode mode, Canvas parent, int x, int y) throws LWJGLException { boolean fullscreen = Display.isFullscreen(); boolean resizable = Display.isResizable(); - boolean parented = (parent != null); + boolean parented = (parent != null) && !fullscreen; close_requested = false; diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 5145d5e6..bea2e93a 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -64,24 +64,24 @@ static NSAutoreleasePool *pool; + (NSOpenGLPixelFormat*)defaultPixelFormat { NSOpenGLPixelFormatAttribute defaultAttribs[] = { - NSOpenGLPFADoubleBuffer, - NSOpenGLPFADepthSize, 16, - NSOpenGLPFAColorSize, 32, - 0 - }; + NSOpenGLPFADoubleBuffer, + NSOpenGLPFADepthSize, 16, + NSOpenGLPFAColorSize, 32, + 0 + }; if (default_format == nil) { default_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:defaultAttribs]; - } - return default_format; + } + return default_format; } - (BOOL)windowShouldClose:(id)sender { if (_parent != nil) { - JNIEnv *env = attachCurrentThread(); - jclass display_class = (*env)->GetObjectClass(env, _parent->jdisplay); - jmethodID close_callback = (*env)->GetMethodID(env, display_class, "doHandleQuit", "()V"); - (*env)->CallVoidMethod(env, _parent->jdisplay, close_callback); - } + JNIEnv *env = attachCurrentThread(); + jclass display_class = (*env)->GetObjectClass(env, _parent->jdisplay); + jmethodID close_callback = (*env)->GetMethodID(env, display_class, "doHandleQuit", "()V"); + (*env)->CallVoidMethod(env, _parent->jdisplay, close_callback); + } return NO; } @@ -90,23 +90,23 @@ static NSAutoreleasePool *pool; if (self != nil) { _pixelFormat = [format retain]; [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(_surfaceNeedsUpdate:) - name:NSViewGlobalFrameDidChangeNotification - object:self]; - } - return self; + selector:@selector(_surfaceNeedsUpdate:) + name:NSViewGlobalFrameDidChangeNotification + object:self]; + } + return self; } - (void) _surfaceNeedsUpdate:(NSNotification*)notification { - [self update]; + [self update]; } - (void)setOpenGLContext:(NSOpenGLContext*)context { - _openGLContext = context; + _openGLContext = context; } - (NSOpenGLContext*)openGLContext { - return _openGLContext; + return _openGLContext; } - (void)clearGLContext { @@ -166,196 +166,194 @@ static NSAutoreleasePool *pool; } - (void)keyUp:(NSEvent *)event { - JNIEnv *env = attachCurrentThread(); - if (env == nil || event == nil || _parent == nil || _parent->jkeyboard == nil) { - return; - } - long time = [event timestamp] * 1000000000; - jclass keyboard_class = (*env)->GetObjectClass(env, _parent->jkeyboard); - jmethodID keyup = (*env)->GetMethodID(env, keyboard_class, "keyReleased", "(IIJ)V"); - const char* charbuf = [[event characters] cStringUsingEncoding:NSASCIIStringEncoding]; - int charcode = (charbuf == nil) ? 0 : charbuf[0]; - (*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, [event keyCode], charcode, time); + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil || _parent->jkeyboard == nil) { + return; + } + long time = [event timestamp] * 1000000000; + jclass keyboard_class = (*env)->GetObjectClass(env, _parent->jkeyboard); + jmethodID keyup = (*env)->GetMethodID(env, keyboard_class, "keyReleased", "(IIJ)V"); + const char* charbuf = [[event characters] cStringUsingEncoding:NSASCIIStringEncoding]; + int charcode = (charbuf == nil) ? 0 : charbuf[0]; + (*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, [event keyCode], charcode, time); } - (void)flagsChanged:(NSEvent *)event { - JNIEnv *env = attachCurrentThread(); - if (env == nil || event == nil || _parent == nil || _parent->jkeyboard == nil) { - return; - } - long time = [event timestamp] * 1000000000; + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil || _parent->jkeyboard == nil) { + return; + } + long time = [event timestamp] * 1000000000; - NSUInteger mask = ~0; - switch([event keyCode]) { - case kVK_Control : mask = 0x0001; break; - case kVK_Shift : mask = 0x0002; break; - case kVK_RightShift : mask = 0x0004; break; - case kVK_Command : mask = 0x0008; break; - case 0x36 : mask = 0x0010; break; // Should be: kVK_RightCommand -- missing O.o - case kVK_Option : mask = 0x0020; break; - case kVK_RightOption : mask = 0x0040; break; - case kVK_RightControl: mask = 0x2000; break; - case kVK_CapsLock : mask = NSAlphaShiftKeyMask; break; - case kVK_Function : mask = NSFunctionKeyMask; break; - // case 0x?? : mask = NSNumericPadKeyMask; break; // Didn't have the keycode for this one :( - default: - printf("Unknown modifier with keycode: %d\n", [event keyCode]); - return; - } + NSUInteger mask = ~0; + switch([event keyCode]) { + case kVK_Control : mask = 0x0001; break; + case kVK_Shift : mask = 0x0002; break; + case kVK_RightShift : mask = 0x0004; break; + case kVK_Command : mask = 0x0008; break; + case 0x36 : mask = 0x0010; break; // Should be: kVK_RightCommand -- missing O.o + case kVK_Option : mask = 0x0020; break; + case kVK_RightOption : mask = 0x0040; break; + case kVK_RightControl: mask = 0x2000; break; + case kVK_CapsLock : mask = NSAlphaShiftKeyMask; break; + case kVK_Function : mask = NSFunctionKeyMask; break; + // case 0x?? : mask = NSNumericPadKeyMask; break; // Didn't have the keycode for this one :( + default: + NSLog(@"Unknown modifier with keycode: %d\n", [event keyCode]); + return; + } - jclass keyboard_class = (*env)->GetObjectClass(env, _parent->jkeyboard); + jclass keyboard_class = (*env)->GetObjectClass(env, _parent->jkeyboard); - jmethodID keyMethod; - if (([event modifierFlags] & mask) == mask) { - keyMethod = (*env)->GetMethodID(env, keyboard_class, "keyPressed", "(IIJ)V"); - } else { - keyMethod = (*env)->GetMethodID(env, keyboard_class, "keyReleased", "(IIJ)V"); - } + jmethodID keyMethod; + if (([event modifierFlags] & mask) == mask) { + keyMethod = (*env)->GetMethodID(env, keyboard_class, "keyPressed", "(IIJ)V"); + } else { + keyMethod = (*env)->GetMethodID(env, keyboard_class, "keyReleased", "(IIJ)V"); + } - (*env)->CallVoidMethod(env, _parent->jkeyboard, keyMethod, [event keyCode], 0, time); + (*env)->CallVoidMethod(env, _parent->jkeyboard, keyMethod, [event keyCode], 0, time); } - (void)mouseButtonState:(NSEvent *)event :(int)button :(int)state { - JNIEnv *env = attachCurrentThread(); - if (env == nil || event == nil || _parent == nil || _parent->jkeyboard == nil) { - return; - } - long time = [event timestamp] * 1000000000; - jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); - jmethodID mousebutton = (*env)->GetMethodID(env, mouse_class, "setButton", "(IIJ)V"); - (*env)->CallVoidMethod(env, _parent->jmouse, mousebutton, button, state, time); + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil || _parent->jkeyboard == nil) { + return; + } + long time = [event timestamp] * 1000000000; + jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); + jmethodID mousebutton = (*env)->GetMethodID(env, mouse_class, "setButton", "(IIJ)V"); + (*env)->CallVoidMethod(env, _parent->jmouse, mousebutton, button, state, time); } - (void)mouseDown:(NSEvent *)event { - [self mouseButtonState:event :0 :1]; + [self mouseButtonState:event :0 :1]; } - (void)rightMouseDown:(NSEvent *)event { - [self mouseButtonState:event :1 :1]; + [self mouseButtonState:event :1 :1]; } - (void)otherMouseDown:(NSEvent *)event { - [self mouseButtonState:event :2 :1]; + [self mouseButtonState:event :2 :1]; } - (void)mouseUp:(NSEvent *)event { - [self mouseButtonState:event :0 :0]; + [self mouseButtonState:event :0 :0]; } - (void)rightMouseUp:(NSEvent *)event { - [self mouseButtonState:event :1 :0]; + [self mouseButtonState:event :1 :0]; } - (void)otherMouseUp:(NSEvent *)event { - [self mouseButtonState:event :2 :0]; + [self mouseButtonState:event :2 :0]; } - (void)mouseDragged:(NSEvent *)event { - JNIEnv *env = attachCurrentThread(); - if (env == nil || event == nil || _parent == nil) { - return; - } - long time = [event timestamp] * 1000000000; - jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); - jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); - NSPoint loc = [self convertPoint:[event locationInWindow] toView:nil]; - (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time); + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil) { + return; + } + long time = [event timestamp] * 1000000000; + jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); + jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); + NSPoint loc = [self convertPoint:[event locationInWindow] toView:nil]; + (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time); } - (void)rightMouseDragged:(NSEvent *)event { - JNIEnv *env = attachCurrentThread(); - if (env == nil || event == nil || _parent == nil) { - return; - } - long time = [event timestamp] * 1000000000; - jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); - jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); - NSPoint loc = [self convertPoint:[event locationInWindow] toView:nil]; - (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time); + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil) { + return; + } + long time = [event timestamp] * 1000000000; + jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); + jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); + NSPoint loc = [self convertPoint:[event locationInWindow] toView:nil]; + (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time); } - (void)otherMouseDragged:(NSEvent *)event { - JNIEnv *env = attachCurrentThread(); - if (env == nil || event == nil || _parent == nil) { - return; - } - long time = [event timestamp] * 1000000000; - jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); - jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); - NSPoint loc = [self convertPoint:[event locationInWindow] toView:nil]; - (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time); + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil) { + return; + } + long time = [event timestamp] * 1000000000; + jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); + jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); + NSPoint loc = [self convertPoint:[event locationInWindow] toView:nil]; + (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time); } - (void)mouseMoved:(NSEvent *)event { - JNIEnv *env = attachCurrentThread(); - if (env == nil || event == nil || _parent == nil || _parent->jmouse == nil) { - return; - } - long time = [event timestamp] * 1000000000; - jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); - jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); - NSPoint loc = [self convertPoint:[event locationInWindow] toView:nil]; + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil || _parent->jmouse == nil) { + return; + } + long time = [event timestamp] * 1000000000; + jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); + jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); + NSPoint loc = [self convertPoint:[event locationInWindow] toView:nil]; (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time); } - (void)scrollWheel:(NSEvent *)event { - JNIEnv *env = attachCurrentThread(); - if (env == nil || event == nil || _parent == nil) { - return; - } - long time = [event timestamp] * 1000000000; - jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); - jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); - NSPoint loc = [self convertPoint:[event locationInWindow] toView:nil]; - (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 1.0f, time); + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil) { + return; + } + long time = [event timestamp] * 1000000000; + jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse); + jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V"); + NSPoint loc = [self convertPoint:[event locationInWindow] toView:nil]; + (*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 1.0f, time); } -- (void)viewDidMoveToWindow -{ - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(windowResized:) - name:NSWindowDidResizeNotification - object:[self window]]; +- (void)viewDidMoveToWindow { + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(windowResized:) + name:NSWindowDidResizeNotification + object:[self window]]; } -- (void)dealloc -{ - [[NSNotificationCenter defaultCenter] removeObserver:self]; - [super dealloc]; +- (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; + [super dealloc]; } -- (void)windowResized:(NSNotification *)notification; +- (void)windowResized:(NSNotification *)notification; { - if (_parent != nil) { - _parent->display_rect = [[self window] frame]; - _parent->resized = JNI_TRUE; - } + if (_parent != nil) { + _parent->display_rect = [[self window] frame]; + _parent->resized = JNI_TRUE; + } } - (void) drawRect:(NSRect)rect { // set black as the default background color // for the nsview to avoid white flash on fullscreen - [[NSColor blackColor] setFill]; - NSRectFill(rect); + [[NSColor blackColor] setFill]; + NSRectFill(rect); } @end JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nIsMiniaturized(JNIEnv *env, jobject this, jobject window_handle) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - return (jboolean)[window_info->window isMiniaturized]; + return (jboolean)[window_info->window isMiniaturized]; } JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nIsFocused(JNIEnv *env, jobject this, jobject window_handle) { - MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); // Display is focused if nswindow is key window and nsview is first responder in that nswindow - return (jboolean)([[window_info->view window] isKeyWindow] && [[window_info->view window] firstResponder] == window_info->view); + return (jboolean)([[window_info->view window] isKeyWindow] && [[window_info->view window] firstResponder] == window_info->view); } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nResizeWindow(JNIEnv *env, jobject this, jobject window_handle, jint x, jint y, jint width, jint height) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - window_info->display_rect = NSMakeRect(x, y, width, height); - [window_info->window setFrame:window_info->display_rect display:false]; - [window_info->view update]; + window_info->display_rect = NSMakeRect(x, y, width, height); + [window_info->window setFrame:window_info->display_rect display:false]; + [window_info->view update]; } JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nWasResized(JNIEnv *env, jobject this, jobject window_handle) { @@ -379,13 +377,13 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nGetHeight(JNIEnv *en JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nSetResizable(JNIEnv *env, jobject this, jobject window_handle, jboolean resizable) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - NSUInteger style_mask = [window_info->window styleMask]; - if (resizable == true) { - style_mask |= NSResizableWindowMask; - } else { - style_mask &= ~NSResizableWindowMask; - } - [window_info->window setStyleMask:style_mask]; + NSUInteger style_mask = [window_info->window styleMask]; + if (resizable == true) { + style_mask |= NSResizableWindowMask; + } else { + style_mask &= ~NSResizableWindowMask; + } + [window_info->window setStyleMask:style_mask]; } JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nGetX(JNIEnv *env, jobject this, jobject window_handle) { @@ -408,35 +406,30 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nGetY(JNIEnv *env, jo JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nSetTitle(JNIEnv *env, jobject this, jobject window_handle, jobject title_buffer) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); const char *title_cstr = (const char *)(*env)->GetDirectBufferAddress(env, title_buffer); - NSString *title = [[NSString alloc] initWithUTF8String:title_cstr]; - [window_info->window setTitle:title]; + NSString *title = [[NSString alloc] initWithUTF8String:title_cstr]; + [window_info->window setTitle:title]; } JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIEnv *env, jobject this, jint x, jint y, jint width, jint height, jboolean fullscreen, jboolean undecorated, jboolean resizable, jboolean parented, jobject peer_info_handle, jobject window_handle) { - - if (window_handle == NULL) { - window_handle = newJavaManagedByteBuffer(env, sizeof(MacOSXWindowInfo)); - if (window_handle == NULL) { - throwException(env, "Could not create handle buffer"); - return NULL; - } - } + + if (window_handle == NULL) { + window_handle = newJavaManagedByteBuffer(env, sizeof(MacOSXWindowInfo)); + if (window_handle == NULL) { + throwException(env, "Could not create handle buffer"); + return NULL; + } + } pool = [[NSAutoreleasePool alloc] init]; MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); - - // Cache the necessary info for window-close callbacks into the JVM - if (window_info->jdisplay == NULL) { - window_info->jdisplay = (*env)->NewGlobalRef(env, this); - } - - NSRect view_rect = NSMakeRect(0.0, 0.0, width, height); - window_info->view = [[MacOSXOpenGLView alloc] initWithFrame:view_rect pixelFormat:peer_info->pixel_format]; - if (window_info->context != nil) { - [window_info->view setOpenGLContext:window_info->context]; - } + MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); + + NSRect view_rect = NSMakeRect(0.0, 0.0, width, height); + window_info->view = [[MacOSXOpenGLView alloc] initWithFrame:view_rect pixelFormat:peer_info->pixel_format]; + if (window_info->context != nil) { + [window_info->view setOpenGLContext:window_info->context]; + } window_info->display_rect = NSMakeRect(x, y, width, height); @@ -452,14 +445,21 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE default_window_mask |= NSResizableWindowMask; } - window_info->window = (MacOSXKeyableWindow*)[[NSApplication sharedApplication] mainWindow]; - if (window_info->window == nil) { - window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:default_window_mask backing:NSBackingStoreBuffered defer:NO]; + if (parented) { + window_info->window = (MacOSXKeyableWindow*)[[NSApplication sharedApplication] mainWindow]; + + [window_info->window setContentView:window_info->view]; } - - [window_info->window setContentView:window_info->view]; - - if (!parented) { + else { + window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:default_window_mask backing:NSBackingStoreBuffered defer:NO]; + + [window_info->window setContentView:window_info->view]; + + // Cache the necessary info for window-close callbacks into the JVM + if (window_info->jdisplay == NULL) { + window_info->jdisplay = (*env)->NewGlobalRef(env, this); + } + // set NSView as delegate of NSWindow to get windowShouldClose events [window_info->window setDelegate:window_info->view]; } @@ -494,9 +494,9 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE window_info->fullscreen = fullscreen; - peer_info->window_info = window_info; + peer_info->window_info = window_info; - return window_handle; + return window_handle; } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv *env, jobject this, jobject window_handle) { @@ -514,8 +514,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv [pool drain]; } -JNIEXPORT jint JNICALL Java_org_lwjgl_DefaultSysImplementation_getJNIVersion - (JNIEnv *env, jobject ignored) { +JNIEXPORT jint JNICALL Java_org_lwjgl_DefaultSysImplementation_getJNIVersion(JNIEnv *env, jobject ignored) { return org_lwjgl_MacOSXSysImplementation_JNI_VERSION; } @@ -530,4 +529,4 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_setGammaRamp(JNIEnv * if (err != CGDisplayNoErr) { throwException(env, "Could not set display gamma"); } -} +} \ No newline at end of file From 284f6135f35de996382e3965bfebbb2ac465d8dc Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sat, 22 Dec 2012 20:20:48 +0000 Subject: [PATCH 054/111] Refactoring to prepare for implementing OS X CALayer based Display.setParent, temporarily breaks Display.setParent on Java 7 and Java 6 Applets. --- .../lwjgl/opengl/MacOSXCanvasPeerInfo.java | 7 ++++-- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 5 +++- src/native/macosx/context.h | 3 ++- src/native/macosx/org_lwjgl_opengl_Display.m | 23 +++++++++++++++---- .../org_lwjgl_opengl_MacOSXCanvasPeerInfo.m | 16 +++++++++---- 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXCanvasPeerInfo.java b/src/java/org/lwjgl/opengl/MacOSXCanvasPeerInfo.java index 8aeade7f..684a7a5d 100644 --- a/src/java/org/lwjgl/opengl/MacOSXCanvasPeerInfo.java +++ b/src/java/org/lwjgl/opengl/MacOSXCanvasPeerInfo.java @@ -44,15 +44,18 @@ import org.lwjgl.LWJGLUtil; * $Id$ */ abstract class MacOSXCanvasPeerInfo extends MacOSXPeerInfo { + private final AWTSurfaceLock awt_surface = new AWTSurfaceLock(); + protected MacOSXCanvasPeerInfo(PixelFormat pixel_format, ContextAttribs attribs, boolean support_pbuffer) throws LWJGLException { super(pixel_format, attribs, true, true, support_pbuffer, true); } protected void initHandle(Canvas component) throws LWJGLException { - nInitHandle(getHandle()); + nInitHandle(awt_surface.lockAndGetHandle(component), getHandle()); } - private static native void nInitHandle(ByteBuffer peer_info_handle) throws LWJGLException; + private static native void nInitHandle(ByteBuffer surface_buffer, ByteBuffer peer_info_handle) throws LWJGLException; protected void doUnlock() throws LWJGLException { + awt_surface.unlock(); } } diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 459080fc..f62e564e 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -115,6 +115,9 @@ final class MacOSXDisplay implements DisplayImplementation { boolean resizable = Display.isResizable(); boolean parented = (parent != null) && !fullscreen; + if (parented) this.canvas = parent; + else this.canvas = null; + close_requested = false; DrawableGL gl_drawable = (DrawableGL)Display.getDrawable(); @@ -124,7 +127,7 @@ final class MacOSXDisplay implements DisplayImplementation { window = nCreateWindow(x, y, mode.getWidth(), mode.getHeight(), fullscreen, isUndecorated(), resizable, parented, peer_handle, window); - this.canvas = parent; + if (fullscreen) { // when going to fullscreen viewport is set to screen size by Cocoa, ignore this value diff --git a/src/native/macosx/context.h b/src/native/macosx/context.h index 34a69571..7042e7ad 100644 --- a/src/native/macosx/context.h +++ b/src/native/macosx/context.h @@ -54,7 +54,7 @@ typedef struct { NSRect display_rect; - MacOSXOpenGLView *view; + MacOSXOpenGLView *view; NSOpenGLContext *context; // Native objects for Java callbacks @@ -100,6 +100,7 @@ typedef struct { MacOSXWindowInfo *window_info; NSOpenGLPixelFormat *pixel_format; NSOpenGLPixelBuffer *pbuffer; + NSView *parent; } MacOSXPeerInfo; diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index bea2e93a..a97e3594 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -446,9 +446,17 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE } if (parented) { - window_info->window = (MacOSXKeyableWindow*)[[NSApplication sharedApplication] mainWindow]; + window_info->window = [peer_info->parent window]; - [window_info->window setContentView:window_info->view]; + if (window_info->window != nil) { + [peer_info->parent addSubview:window_info->view]; + [window_info->view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; + } + else { + // failed to get parent window, create a new window + window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:default_window_mask backing:NSBackingStoreBuffered defer:NO]; + [window_info->window setContentView:window_info->view]; + } } else { window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:default_window_mask backing:NSBackingStoreBuffered defer:NO]; @@ -505,9 +513,16 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv if (window_info->fullscreen) { [window_info->view exitFullScreenModeWithOptions: nil]; } - else { + else { if (window_info->window != nil) { - [window_info->window close]; + // if the nsview has no parent then close window + if ([window_info->window contentView] == window_info->view) { + [window_info->window close]; + } + else { + // the nsview has a parent, so remove it from there + [window_info->view removeFromSuperviewWithoutNeedingDisplay]; + } } } diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m index 73518e66..4b2483f7 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m @@ -42,17 +42,23 @@ //#import #include +#include +#include "awt_tools.h" #include "org_lwjgl_opengl_MacOSXCanvasPeerInfo.h" #include "context.h" #include "common_tools.h" JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle -(JNIEnv *env, jclass clazz, jobject peer_info_handle) { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; +(JNIEnv *env, jclass clazz, jobject lock_buffer_handle, jobject peer_info_handle) { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); - - peer_info->isWindowed = true; - + + AWTSurfaceLock *surface = (AWTSurfaceLock *)(*env)->GetDirectBufferAddress(env, lock_buffer_handle); + JAWT_MacOSXDrawingSurfaceInfo *macosx_dsi = (JAWT_MacOSXDrawingSurfaceInfo *)surface->dsi->platformInfo; + peer_info->parent = macosx_dsi->cocoaViewRef; + + peer_info->isWindowed = true; + [pool release]; } From 0d6eac80f7b8c957355e68777561a9c694886655 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 23 Dec 2012 22:09:57 +0000 Subject: [PATCH 055/111] Implement initial work for enabling CALayer rendering --- src/java/org/lwjgl/opengl/AWTSurfaceLock.java | 22 +---- src/native/macosx/org_lwjgl_opengl_Display.m | 9 ++- .../org_lwjgl_opengl_MacOSXCanvasPeerInfo.m | 80 ++++++++++++++++++- ...lwjgl_opengl_MacOSXContextImplementation.m | 2 +- 4 files changed, 86 insertions(+), 27 deletions(-) diff --git a/src/java/org/lwjgl/opengl/AWTSurfaceLock.java b/src/java/org/lwjgl/opengl/AWTSurfaceLock.java index a428115f..da1f6d56 100644 --- a/src/java/org/lwjgl/opengl/AWTSurfaceLock.java +++ b/src/java/org/lwjgl/opengl/AWTSurfaceLock.java @@ -82,8 +82,8 @@ final class AWTSurfaceLock { // It is only needed on first call, so we avoid it on all subsequent calls // due to performance.. - // Allow the use of a Core Animation Layer only when using non fullscreen Display.setParent() or AWTGLCanvas - final boolean allowCALayer = ((Display.getParent() != null && !Display.isFullscreen()) || component instanceof AWTGLCanvas) && isApplet(component) && LWJGLUtil.isMacOSXEqualsOrBetterThan(10, 6); + // Allow the use of a Core Animation Layer only when using non fullscreen Display.setParent() or AWTGLCanvas and OS X 10.6+ + final boolean allowCALayer = ((Display.getParent() != null && !Display.isFullscreen()) || component instanceof AWTGLCanvas) && LWJGLUtil.isMacOSXEqualsOrBetterThan(10, 6); if (firstLockSucceeded) return lockAndInitHandle(lock_buffer, component, allowCALayer); @@ -107,22 +107,4 @@ final class AWTSurfaceLock { } private static native void nUnlock(ByteBuffer lock_buffer) throws LWJGLException; - - /** - * This method will return true if the component is running in an applet - */ - public boolean isApplet(Canvas component) { - - Component parent = component.getParent(); - - while (parent != null) { - if (parent instanceof Applet) { - return true; - } - parent = parent.getParent(); - } - - // not an applet - return false; - } } diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index a97e3594..fffe738b 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -446,8 +446,13 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE } if (parented) { - window_info->window = [peer_info->parent window]; - + if (peer_info->parent != nil) { + window_info->window = [peer_info->parent window]; + } + else { + window_info->window = nil; + } + if (window_info->window != nil) { [peer_info->parent addSubview:window_info->view]; [window_info->view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m index 4b2483f7..ef7dff2a 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m @@ -39,8 +39,6 @@ */ #import -//#import - #include #include #include "awt_tools.h" @@ -48,17 +46,91 @@ #include "context.h" #include "common_tools.h" + +@interface GLLayer : NSOpenGLLayer {} +- (void) attachLayer; +- (void) removeLayer; +@end + JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle (JNIEnv *env, jclass clazz, jobject lock_buffer_handle, jobject peer_info_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); - AWTSurfaceLock *surface = (AWTSurfaceLock *)(*env)->GetDirectBufferAddress(env, lock_buffer_handle); JAWT_MacOSXDrawingSurfaceInfo *macosx_dsi = (JAWT_MacOSXDrawingSurfaceInfo *)surface->dsi->platformInfo; - peer_info->parent = macosx_dsi->cocoaViewRef; + // check for CALayer support + if(surface->awt.version & 0x80000000) { //JAWT_MACOSX_USE_CALAYER) { + + if (macosx_dsi != NULL) { + // get the root layer of the AWT Canvas + id surfaceLayers = (id )macosx_dsi; + GLLayer *glLayer = [[GLLayer new] autorelease]; + [glLayer performSelectorOnMainThread:@selector(attachLayer:) withObject:surfaceLayers waitUntilDone:NO]; + } + + peer_info->isWindowed = true; + peer_info->parent = nil; + + [pool release]; + return; + } + + // no CALayer support, fallback to using legacy method of getting the NSView of an AWT Canvas + peer_info->parent = macosx_dsi->cocoaViewRef; peer_info->isWindowed = true; [pool release]; } + +@implementation GLLayer + +- (void) attachLayer:(id)surfaceLayers { + self.asynchronous = YES; + self.needsDisplayOnBoundsChange = YES; + self.opaque = NO; + self.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable; + surfaceLayers.layer = self; +} + +- (void) removeLayer:(id)surfaceLayers { + surfaceLayers.layer = nil; +} + +-(void)drawInCGLContext:(CGLContextObj)glContext + pixelFormat:(CGLPixelFormatObj)pixelFormat + forLayerTime:(CFTimeInterval)timeInterval + displayTime:(const CVTimeStamp *)timeStamp { + + // set the current context + CGLSetCurrentContext(glContext); + + // draw a single red quad spinning around based on the current time + GLfloat rotate = timeInterval * 60.0; // 60 degrees per second + glClearColor(0.0, 0.0, 0.0, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glRotatef(rotate, 0.0, 0.0, 1.0); + glBegin(GL_QUADS); + glColor3f(1.0, 1.0, 0.0); + glVertex2f(-0.5, -0.5); + glVertex2f(-0.5, 0.5); + glVertex2f( 0.5, 0.5); + glVertex2f( 0.5, -0.5); + glEnd(); + glPopMatrix(); + + // call super to finalize the drawing - by default all it does is call glFlush() + [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp]; +} + +-(BOOL)canDrawInCGLContext:(CGLContextObj)glContext + pixelFormat:(CGLPixelFormatObj)pixelFormat + forLayerTime:(CFTimeInterval)timeInterval + displayTime:(const CVTimeStamp *)timeStamp { + return YES; +} + +@end diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m index 4abf8ba4..830fd882 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m @@ -82,7 +82,7 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nCre NSSize displaySize = peer_info->window_info->display_rect.size; GLint dim[2] = {displaySize.width, displaySize.height}; CGLSetParameter(cgcontext, kCGLCPSurfaceBackingSize, dim); - CGLEnable(cgcontext, kCGLCESurfaceBackingSize); + CGLEnable(cgcontext, kCGLCESurfaceBackingSize); } else { // disable any fixed backbuffer size to allow resizing From fffa8704181669e2379cb75c588232f4e43029a4 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 25 Dec 2012 13:51:24 +0000 Subject: [PATCH 056/111] correct behavior on OS X Display.getY() by using the screen the window is on rather than the screen with keyboard focus --- src/native/macosx/org_lwjgl_opengl_Display.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index fffe738b..20eae51a 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -395,7 +395,7 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nGetX(JNIEnv *env, jo JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nGetY(JNIEnv *env, jobject this, jobject window_handle) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - NSRect screenRect = [[NSScreen mainScreen] frame]; + NSRect screenRect = [[[window_info->view window] screen] frame]; NSRect winRect = [[window_info->view window] frame]; // get top corner of window frame, also flip coords so origin is in top left From a4c5be3781d4665bf7f097b7a13de7abace22af2 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 25 Dec 2012 14:00:09 +0000 Subject: [PATCH 057/111] fix OS X Mouse.setCursorPosition to use CGDisplayMoveCursorToPoint instead of CGWarpMouseCursorPosition to allow working on multiple monitor setups, also obtain correct window screen instead of screen with keyboard focus --- src/native/macosx/org_lwjgl_opengl_Display.m | 2 +- src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 20eae51a..7890acc4 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -395,7 +395,7 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nGetX(JNIEnv *env, jo JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nGetY(JNIEnv *env, jobject this, jobject window_handle) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - NSRect screenRect = [[[window_info->view window] screen] frame]; + NSRect screenRect = [[window_info->window screen] frame]; NSRect winRect = [[window_info->view window] frame]; // get top corner of window frame, also flip coords so origin is in top left diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m index e35c34a5..37947810 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m @@ -70,7 +70,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nSetCursorPositio p.y = point.y; } else { - NSRect screenRect = [[NSScreen mainScreen] frame]; + NSRect screenRect = [[window_info->window screen] frame]; NSRect viewRect = [window_info->view frame]; NSRect winRect = [window_info->window frame]; @@ -81,11 +81,11 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nSetCursorPositio p.y = winRect.origin.y + viewPoint.y + (viewRect.size.height - y - 1); p.x = winRect.origin.x + viewPoint.x + x; - // flip y coordinates (origin top left) to allow use with CGWarpMouseCursorPosition + // flip y coordinates (origin top left) to allow use with CGDisplayMoveCursorToPoint p.y = screenRect.size.height - p.y - 1; } - CGWarpMouseCursorPosition(p); + CGDisplayMoveCursorToPoint(CGMainDisplayID(), p); } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nRegisterMouseListener(JNIEnv *env, jobject _this, jobject window_handle) { From d0759e9c128065595d148e66cb8ab091a469e0ab Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 25 Dec 2012 14:14:05 +0000 Subject: [PATCH 058/111] remove unneeded code in MacOSXDisplay.getPbufferCapabilities() --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 91 ++++++++++---------- 1 file changed, 45 insertions(+), 46 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index f62e564e..73f8831c 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -73,14 +73,17 @@ final class MacOSXDisplay implements DisplayImplementation { //private MacOSXMouseEventQueue mouse_queue; 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 boolean close_requested; + + /* Members for native window use */ + private MacOSXNativeMouse mouse; + private MacOSXNativeKeyboard keyboard; + private ByteBuffer window; + private ByteBuffer context; + + private boolean skipViewportValue = false; + private static final IntBuffer current_viewport = BufferUtils.createIntBuffer(16); + + private boolean close_requested; MacOSXDisplay() { @@ -119,31 +122,31 @@ final class MacOSXDisplay implements DisplayImplementation { else this.canvas = null; close_requested = false; - - DrawableGL gl_drawable = (DrawableGL)Display.getDrawable(); - PeerInfo peer_info = gl_drawable.peer_info; + + DrawableGL gl_drawable = (DrawableGL)Display.getDrawable(); + PeerInfo peer_info = gl_drawable.peer_info; ByteBuffer peer_handle = peer_info.lockAndGetHandle(); try { - window = nCreateWindow(x, y, mode.getWidth(), mode.getHeight(), - fullscreen, isUndecorated(), resizable, - parented, peer_handle, window); + window = nCreateWindow(x, y, mode.getWidth(), mode.getHeight(), + fullscreen, isUndecorated(), resizable, + parented, peer_handle, window); - if (fullscreen) { - // when going to fullscreen viewport is set to screen size by Cocoa, ignore this value - skipViewportValue = true; - // if starting in fullscreen then set initial viewport to displaymode size - if (current_viewport.get(2) == 0 && current_viewport.get(3) == 0) { - current_viewport.put(2, mode.getWidth()); - current_viewport.put(3, mode.getHeight()); - } - } + if (fullscreen) { + // when going to fullscreen viewport is set to screen size by Cocoa, ignore this value + skipViewportValue = true; + // if starting in fullscreen then set initial viewport to displaymode size + if (current_viewport.get(2) == 0 && current_viewport.get(3) == 0) { + current_viewport.put(2, mode.getWidth()); + current_viewport.put(3, mode.getHeight()); + } + } } catch (LWJGLException e) { destroyWindow(); throw e; - } finally { - peer_info.unlock(); - } + } finally { + peer_info.unlock(); + } } public void doHandleQuit() { @@ -271,11 +274,9 @@ final class MacOSXDisplay implements DisplayImplementation { } } - private boolean skipViewportValue = false; - private static final IntBuffer current_viewport = BufferUtils.createIntBuffer(16); public void update() { boolean should_update = true; - + DrawableGL drawable = (DrawableGL)Display.getDrawable(); if (should_update) { drawable.context.update(); @@ -351,8 +352,8 @@ final class MacOSXDisplay implements DisplayImplementation { /* Keyboard */ public void createKeyboard() throws LWJGLException { - this.keyboard = new MacOSXNativeKeyboard(window); - keyboard.register(); + this.keyboard = new MacOSXNativeKeyboard(window); + keyboard.register(); } public void destroyKeyboard() { @@ -376,13 +377,11 @@ final class MacOSXDisplay implements DisplayImplementation { } public void destroyCursor(Object cursor_handle) { + } public int getPbufferCapabilities() { - if (LWJGLUtil.isMacOSXEqualsOrBetterThan(10, 3)) - return Pbuffer.PBUFFER_SUPPORTED; - else - return 0; + return Pbuffer.PBUFFER_SUPPORTED; } public boolean isBufferLost(PeerInfo handle) { @@ -420,7 +419,7 @@ final class MacOSXDisplay implements DisplayImplementation { * @return number of icons used. */ public int setIcon(ByteBuffer[] icons) { -/* int size = 0; + /*int size = 0; int biggest = -1; for (int i=0;i Date: Fri, 28 Dec 2012 01:11:12 +0000 Subject: [PATCH 059/111] Add further work to the CALayer to prepare it for allowing an FBO to be blit to its internal FBO --- .../org_lwjgl_opengl_MacOSXCanvasPeerInfo.m | 53 +++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m index ef7dff2a..7ee29dc9 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m @@ -47,9 +47,14 @@ #include "common_tools.h" -@interface GLLayer : NSOpenGLLayer {} -- (void) attachLayer; -- (void) removeLayer; +@interface GLLayer : NSOpenGLLayer { + MacOSXPeerInfo *peer_info; +} +- (void) attachLayer: (id)surfaceLayers; +- (void) removeLayer: (id)surfaceLayers; + +- (MacOSXPeerInfo*) peer_info; +- (void) setPeer_info: (MacOSXPeerInfo*)input; @end JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle @@ -67,7 +72,8 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle // get the root layer of the AWT Canvas id surfaceLayers = (id )macosx_dsi; GLLayer *glLayer = [[GLLayer new] autorelease]; - [glLayer performSelectorOnMainThread:@selector(attachLayer:) withObject:surfaceLayers waitUntilDone:NO]; + [glLayer performSelectorOnMainThread:@selector(attachLayer:) withObject:surfaceLayers waitUntilDone:YES]; + [glLayer setPeer_info:peer_info]; } peer_info->isWindowed = true; @@ -98,15 +104,47 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle surfaceLayers.layer = nil; } +- (MacOSXPeerInfo*) peer_info { + return peer_info; +} + +- (void) setPeer_info: (MacOSXPeerInfo*)input { + peer_info = input; +} + -(void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp { + if(!peer_info) { + return; + } + // set the current context CGLSetCurrentContext(glContext); - // draw a single red quad spinning around based on the current time + GLint originalFBO; + GLint originalReadFBO; + GLint originalDrawFBO; + + // get and save the current fbo values + glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &originalFBO); + glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING_EXT, &originalReadFBO); + glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING_EXT, &originalDrawFBO); + + // copy/blit the LWJGL FBO to this CALayers FBO + // TODO + /*glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, lwjglFBO); + glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, originalFBO); + + glBlitFramebufferEXT(0, 0, width, height, + 0, 0, width, height, + GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, + GL_NEAREST);*/ + + + // for testing, draw a single yellow quad spinning around based on the current time GLfloat rotate = timeInterval * 60.0; // 60 degrees per second glClearColor(0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); @@ -122,6 +160,11 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle glEnd(); glPopMatrix(); + + // restore original fbo read and draw values + glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, originalReadFBO); + glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, originalDrawFBO); + // call super to finalize the drawing - by default all it does is call glFlush() [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp]; } From 790f895220cceb21324bc1957b8eae6ceba4d251 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Fri, 4 Jan 2013 00:11:12 +0000 Subject: [PATCH 060/111] Set CALayer OpenGL Context to be shared with the current LWJGL context. Make code OS X 10.5 compatible (change from NSOpenGLLayer to CAOpenGLLayer) --- src/native/macosx/context.h | 19 ++++- .../org_lwjgl_opengl_MacOSXCanvasPeerInfo.m | 77 ++++++++++--------- ...lwjgl_opengl_MacOSXContextImplementation.m | 35 +++++---- 3 files changed, 75 insertions(+), 56 deletions(-) diff --git a/src/native/macosx/context.h b/src/native/macosx/context.h index 7042e7ad..892f1ab0 100644 --- a/src/native/macosx/context.h +++ b/src/native/macosx/context.h @@ -46,6 +46,7 @@ #include #include #include "common_tools.h" +#include @class NSOpenGLContext, NSOpenGLPixelFormat, MacOSXOpenGLView, MacOSXKeyableWindow; @@ -94,15 +95,29 @@ typedef struct { - (BOOL)acceptsFirstResponder; @end +@interface GLLayer : CAOpenGLLayer { + @public + JAWT_MacOSXDrawingSurfaceInfo *macosx_dsi; + MacOSXWindowInfo *window_info; + + @private + CGLContextObj contextObject; +} + +- (void) attachLayer; +- (void) removeLayer; + +@end + typedef struct { + bool isCALayer; bool isWindowed; - bool canDrawGL; MacOSXWindowInfo *window_info; NSOpenGLPixelFormat *pixel_format; NSOpenGLPixelBuffer *pbuffer; NSView *parent; + GLLayer *glLayer; } MacOSXPeerInfo; - NSOpenGLPixelFormat *choosePixelFormat(JNIEnv *env, jobject pixel_format, bool gl32, bool use_display_bpp, bool support_window, bool support_pbuffer, bool double_buffered); #endif diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m index 7ee29dc9..d12cbdf0 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m @@ -46,17 +46,6 @@ #include "context.h" #include "common_tools.h" - -@interface GLLayer : NSOpenGLLayer { - MacOSXPeerInfo *peer_info; -} -- (void) attachLayer: (id)surfaceLayers; -- (void) removeLayer: (id)surfaceLayers; - -- (MacOSXPeerInfo*) peer_info; -- (void) setPeer_info: (MacOSXPeerInfo*)input; -@end - JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle (JNIEnv *env, jclass clazz, jobject lock_buffer_handle, jobject peer_info_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; @@ -69,13 +58,12 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle if(surface->awt.version & 0x80000000) { //JAWT_MACOSX_USE_CALAYER) { if (macosx_dsi != NULL) { - // get the root layer of the AWT Canvas - id surfaceLayers = (id )macosx_dsi; - GLLayer *glLayer = [[GLLayer new] autorelease]; - [glLayer performSelectorOnMainThread:@selector(attachLayer:) withObject:surfaceLayers waitUntilDone:YES]; - [glLayer setPeer_info:peer_info]; + peer_info->glLayer = [GLLayer new]; + peer_info->glLayer->macosx_dsi = macosx_dsi; + peer_info->glLayer->window_info = peer_info->window_info; } + peer_info->isCALayer = true; peer_info->isWindowed = true; peer_info->parent = nil; @@ -85,6 +73,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle // no CALayer support, fallback to using legacy method of getting the NSView of an AWT Canvas peer_info->parent = macosx_dsi->cocoaViewRef; + peer_info->isCALayer = false; peer_info->isWindowed = true; [pool release]; @@ -92,35 +81,28 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle @implementation GLLayer -- (void) attachLayer:(id)surfaceLayers { +- (void) attachLayer { self.asynchronous = YES; self.needsDisplayOnBoundsChange = YES; self.opaque = NO; self.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable; + + // get root layer of the AWT Canvas and add self to it + id surfaceLayers = (id )macosx_dsi; surfaceLayers.layer = self; } -- (void) removeLayer:(id)surfaceLayers { +- (void) removeLayer { + // remove self from root layer + id surfaceLayers = (id )macosx_dsi; surfaceLayers.layer = nil; } -- (MacOSXPeerInfo*) peer_info { - return peer_info; -} - -- (void) setPeer_info: (MacOSXPeerInfo*)input { - peer_info = input; -} - -(void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp { - if(!peer_info) { - return; - } - // set the current context CGLSetCurrentContext(glContext); @@ -133,16 +115,18 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING_EXT, &originalReadFBO); glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING_EXT, &originalDrawFBO); + /*glClearColor(0.0, 0.0, 0.0, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + // copy/blit the LWJGL FBO to this CALayers FBO // TODO - /*glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, lwjglFBO); + glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 1);//lwjglFBO); glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, originalFBO); - - glBlitFramebufferEXT(0, 0, width, height, - 0, 0, width, height, - GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, - GL_NEAREST);*/ - + + glBlitFramebufferEXT(0, 0, 640, 480,//width, height, + 0, 0, 640, 480,//width, height, + GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, + GL_NEAREST);*/ // for testing, draw a single yellow quad spinning around based on the current time GLfloat rotate = timeInterval * 60.0; // 60 degrees per second @@ -173,7 +157,24 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp { - return YES; + return YES; +} + +- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat { + CGLCreateContext(pixelFormat, [window_info->context CGLContextObj], &contextObject); + return contextObject; +} + +- (void)releaseCGLContext:(CGLContextObj)glContext { + CGLDestroyContext(contextObject); +} + +- (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask { + return CGLGetPixelFormat([window_info->context CGLContextObj]); +} + +- (void)releaseCGLPixelFormat:(CGLPixelFormatObj)pixelFormat { + } @end diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m index 830fd882..4af0c256 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m @@ -71,10 +71,10 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nCre shared_context = shared_context_info->context; } context = [[NSOpenGLContext alloc] initWithFormat:peer_info->pixel_format shareContext:shared_context]; - if (context == NULL) { - throwException(env, "Could not create context"); - return NULL; - } + if (context == NULL) { + throwException(env, "Could not create context"); + return NULL; + } if (peer_info->window_info->fullscreen) { // set a fixed backbuffer size for fullscreen @@ -90,12 +90,12 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nCre CGLDisable(cgcontext, kCGLCESurfaceBackingSize); } - [peer_info->window_info->view setOpenGLContext:context]; - peer_info->window_info->context = context; + [peer_info->window_info->view setOpenGLContext:context]; + peer_info->window_info->context = context; context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); - context_info->context = context; - context_info->peer_info = peer_info; - + context_info->context = context; + context_info->peer_info = peer_info; + [pool release]; return context_handle; } @@ -115,8 +115,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nSwapBu NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; [[peer_info->window_info->view openGLContext] flushBuffer]; - peer_info->canDrawGL = true; - [pool release]; + [pool release]; } @@ -125,8 +124,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nUpdate NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; [[peer_info->window_info->view openGLContext] update]; - peer_info->canDrawGL = true; - [pool release]; + [pool release]; } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_clearDrawable @@ -147,9 +145,14 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nReleas JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_setView (JNIEnv *env, jclass clazz, jobject peer_info_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); - [[peer_info->window_info->view openGLContext] setView: peer_info->window_info->view]; - peer_info->canDrawGL = true; + MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); + [[peer_info->window_info->view openGLContext] setView: peer_info->window_info->view]; + + if (peer_info->isCALayer) { + // if using a CALayer, attach it to AWT Canvas and create a shared opengl context with current context + [peer_info->glLayer performSelectorOnMainThread:@selector(attachLayer) withObject:nil waitUntilDone:NO]; + } + [pool release]; } From 0dda33cd2d7d25c416256137cd323b1c1f30f978 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 13 Jan 2013 15:34:24 +0000 Subject: [PATCH 061/111] Implement CALayer rendering, no input yet --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 8 ++ src/native/macosx/context.h | 6 + src/native/macosx/org_lwjgl_opengl_Display.m | 50 ++++---- .../org_lwjgl_opengl_MacOSXCanvasPeerInfo.m | 116 ++++++++++++------ ...lwjgl_opengl_MacOSXContextImplementation.m | 9 +- 5 files changed, 126 insertions(+), 63 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 73f8831c..ea2d0168 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -84,6 +84,8 @@ final class MacOSXDisplay implements DisplayImplementation { private static final IntBuffer current_viewport = BufferUtils.createIntBuffer(16); private boolean close_requested; + + private boolean native_mode = true; MacOSXDisplay() { @@ -123,6 +125,8 @@ final class MacOSXDisplay implements DisplayImplementation { close_requested = false; + native_mode = isNativeMode(); + DrawableGL gl_drawable = (DrawableGL)Display.getDrawable(); PeerInfo peer_info = gl_drawable.peer_info; ByteBuffer peer_handle = peer_info.lockAndGetHandle(); @@ -148,6 +152,10 @@ final class MacOSXDisplay implements DisplayImplementation { peer_info.unlock(); } } + + private boolean isNativeMode() { + return true; + } public void doHandleQuit() { synchronized (this) { diff --git a/src/native/macosx/context.h b/src/native/macosx/context.h index 892f1ab0..e274eb5a 100644 --- a/src/native/macosx/context.h +++ b/src/native/macosx/context.h @@ -102,10 +102,16 @@ typedef struct { @private CGLContextObj contextObject; + int fboID; + int imageRenderBufferID; + int depthRenderBufferID; + int fboWidth; + int fboHeight; } - (void) attachLayer; - (void) removeLayer; +- (void) blitFrameBuffer; @end diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 7890acc4..93eaadac 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -435,35 +435,29 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE if (!fullscreen) { - int default_window_mask = NSBorderlessWindowMask; // undecorated - - if (!undecorated) { - default_window_mask = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask; - } - - if (resizable) { - default_window_mask |= NSResizableWindowMask; - } - if (parented) { - if (peer_info->parent != nil) { - window_info->window = [peer_info->parent window]; + if (peer_info->isCALayer) { + window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO]; + [window_info->window setContentView:window_info->view]; } else { - window_info->window = nil; - } - - if (window_info->window != nil) { + window_info->window = [peer_info->parent window]; [peer_info->parent addSubview:window_info->view]; [window_info->view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; } - else { - // failed to get parent window, create a new window - window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:default_window_mask backing:NSBackingStoreBuffered defer:NO]; - [window_info->window setContentView:window_info->view]; - } } else { + + int default_window_mask = NSBorderlessWindowMask; // undecorated + + if (!undecorated) { + default_window_mask = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask; + } + + if (resizable) { + default_window_mask |= NSResizableWindowMask; + } + window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:default_window_mask backing:NSBackingStoreBuffered defer:NO]; [window_info->window setContentView:window_info->view]; @@ -501,9 +495,15 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE // Inform the view of its parent window info; [window_info->view setParent:window_info]; - [window_info->window performSelectorOnMainThread:@selector(makeFirstResponder:) withObject:window_info->view waitUntilDone:NO]; - [window_info->window performSelectorOnMainThread:@selector(setInitialFirstResponder:) withObject:window_info->view waitUntilDone:NO]; - [window_info->window performSelectorOnMainThread:@selector(makeKeyAndOrderFront:) withObject:[NSApplication sharedApplication] waitUntilDone:NO]; + if (peer_info->isCALayer) { + // hidden window when using CALayer + [window_info->window orderOut:nil]; + } + else { + [window_info->window performSelectorOnMainThread:@selector(makeFirstResponder:) withObject:window_info->view waitUntilDone:NO]; + [window_info->window performSelectorOnMainThread:@selector(setInitialFirstResponder:) withObject:window_info->view waitUntilDone:NO]; + [window_info->window performSelectorOnMainThread:@selector(makeKeyAndOrderFront:) withObject:[NSApplication sharedApplication] waitUntilDone:NO]; + } window_info->fullscreen = fullscreen; @@ -525,7 +525,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv [window_info->window close]; } else { - // the nsview has a parent, so remove it from there + // the nsview has a parent, remove it from there [window_info->view removeFromSuperviewWithoutNeedingDisplay]; } } diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m index d12cbdf0..1b49e57d 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m @@ -98,6 +98,68 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle surfaceLayers.layer = nil; } +- (void) blitFrameBuffer { + + // get the size of the CALayer/AWT Canvas + int width = self.bounds.size.width; + int height = self.bounds.size.height; + + if (width != fboWidth || height != fboHeight) { + + // store current fbo/renderbuffers for later deletion + int oldFboID = fboID; + int oldImageRenderBufferID = imageRenderBufferID; + int oldDepthRenderBufferID = depthRenderBufferID; + + // set the size of the offscreen frame buffer window + window_info->display_rect = NSMakeRect(0, 0, width, height); + [window_info->window setFrame:window_info->display_rect display:false]; + + // create new fbo + int tempFBO; + glGenFramebuffersEXT(1, &tempFBO); + + // create new render buffers + glGenRenderbuffersEXT(1, &imageRenderBufferID); + glGenRenderbuffersEXT(1, &depthRenderBufferID); + + // switch to new fbo to attach render buffers + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, tempFBO); + + // initialize and attach image render buffer + glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, imageRenderBufferID); + glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, width, height); + glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, imageRenderBufferID); + + // initialize and attach depth render buffer + glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthRenderBufferID); + glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height); + glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthRenderBufferID); + + // set new fbo and its sizes + fboID = tempFBO; + fboWidth = width; + fboHeight = height; + + // clean up the old fbo and renderBuffers + glDeleteFramebuffersEXT(1, &oldFboID); + glDeleteRenderbuffersEXT(1, &oldImageRenderBufferID); + glDeleteRenderbuffersEXT(1, &oldDepthRenderBufferID); + } + else { + glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0); + glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, fboID); + + glBlitFramebufferEXT(0, 0, width, height, + 0, 0, width, height, + GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, + GL_NEAREST); + } + + // restore default framebuffer + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); +} + -(void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval @@ -106,48 +168,30 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle // set the current context CGLSetCurrentContext(glContext); - GLint originalFBO; + if (fboID == 0) { + // clear garbage background before lwjgl fbo is set + glClearColor(0.0, 0.0, 0.0, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + } + + // get the size of the CALayer + int width = fboWidth; + int height = fboHeight; + GLint originalReadFBO; - GLint originalDrawFBO; // get and save the current fbo values - glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &originalFBO); glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING_EXT, &originalReadFBO); - glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING_EXT, &originalDrawFBO); - /*glClearColor(0.0, 0.0, 0.0, 1.0); - glClear(GL_COLOR_BUFFER_BIT); + // read the LWJGL FBO and blit it into this CALayers FBO + glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, fboID); + glBlitFramebufferEXT(0, 0, width, height, + 0, 0, width, height, + GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, + GL_NEAREST); - // copy/blit the LWJGL FBO to this CALayers FBO - // TODO - glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 1);//lwjglFBO); - glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, originalFBO); - - glBlitFramebufferEXT(0, 0, 640, 480,//width, height, - 0, 0, 640, 480,//width, height, - GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, - GL_NEAREST);*/ - - // for testing, draw a single yellow quad spinning around based on the current time - GLfloat rotate = timeInterval * 60.0; // 60 degrees per second - glClearColor(0.0, 0.0, 0.0, 1.0); - glClear(GL_COLOR_BUFFER_BIT); - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glRotatef(rotate, 0.0, 0.0, 1.0); - glBegin(GL_QUADS); - glColor3f(1.0, 1.0, 0.0); - glVertex2f(-0.5, -0.5); - glVertex2f(-0.5, 0.5); - glVertex2f( 0.5, 0.5); - glVertex2f( 0.5, -0.5); - glEnd(); - glPopMatrix(); - - - // restore original fbo read and draw values + // restore original fbo read value glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, originalReadFBO); - glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, originalDrawFBO); // call super to finalize the drawing - by default all it does is call glFlush() [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp]; @@ -177,4 +221,4 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle } -@end +@end \ No newline at end of file diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m index 4af0c256..ae6625d8 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m @@ -115,10 +115,15 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nSwapBu NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; [[peer_info->window_info->view openGLContext] flushBuffer]; + + if (peer_info->isCALayer) { + // blit the contents of buffer to CALayer + [peer_info->glLayer blitFrameBuffer]; + } + [pool release]; } - JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nUpdate (JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; @@ -128,7 +133,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nUpdate } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_clearDrawable - (JNIEnv *env, jclass clazz, jobject context_handle) { +(JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; [[peer_info->window_info->view openGLContext] clearDrawable]; From 03b669fb4dc8b7c0a603070dc23243b9e3096b2f Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 13 Jan 2013 16:13:13 +0000 Subject: [PATCH 062/111] Fix garbage being displayed when resizing in CALayer mode --- .../macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m index 1b49e57d..b6019fab 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m @@ -136,6 +136,18 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height); glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthRenderBufferID); + // clear garbage background on new fbo + glClearColor(0.0, 0.0, 0.0, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + + // blit previous fbo to the new fbo + glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, fboID); + glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, tempFBO); + glBlitFramebufferEXT(0, 0, width, height, + 0, 0, width, height, + GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, + GL_NEAREST); + // set new fbo and its sizes fboID = tempFBO; fboWidth = width; From f2ad9f470da9a8647ae80e212a0857b16ad6d949 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 13 Jan 2013 18:37:20 +0000 Subject: [PATCH 063/111] Tweak CALayer resizing behavior --- .../org_lwjgl_opengl_MacOSXCanvasPeerInfo.m | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m index b6019fab..b4ad9350 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m @@ -111,10 +111,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle int oldImageRenderBufferID = imageRenderBufferID; int oldDepthRenderBufferID = depthRenderBufferID; - // set the size of the offscreen frame buffer window - window_info->display_rect = NSMakeRect(0, 0, width, height); - [window_info->window setFrame:window_info->display_rect display:false]; - // create new fbo int tempFBO; glGenFramebuffersEXT(1, &tempFBO); @@ -140,19 +136,25 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle glClearColor(0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); - // blit previous fbo to the new fbo - glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, fboID); + // blit frameBuffer to the new fbo + glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0); glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, tempFBO); glBlitFramebufferEXT(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST); + glFinish(); // finish before using new fbo and resizing the window + // set new fbo and its sizes fboID = tempFBO; fboWidth = width; fboHeight = height; + // set the size of the offscreen frame buffer window + window_info->display_rect = NSMakeRect(0, 0, width, height); + [window_info->window setFrame:window_info->display_rect display:false]; + // clean up the old fbo and renderBuffers glDeleteFramebuffersEXT(1, &oldFboID); glDeleteRenderbuffersEXT(1, &oldImageRenderBufferID); @@ -180,16 +182,16 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle // set the current context CGLSetCurrentContext(glContext); - if (fboID == 0) { - // clear garbage background before lwjgl fbo is set + // get the size of the CALayer/AWT Canvas + int width = self.bounds.size.width; + int height = self.bounds.size.height; + + if (width != fboWidth || height != fboHeight) { + // clear garbage background before lwjgl fbo blit glClearColor(0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); } - // get the size of the CALayer - int width = fboWidth; - int height = fboHeight; - GLint originalReadFBO; // get and save the current fbo values From fe2f06f36e45e2c35feff51f5efec1e347923563 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Mon, 14 Jan 2013 23:35:46 +0000 Subject: [PATCH 064/111] Implement Keyboard and Mouse input when using CALayer mode --- build.xml | 1 + src/java/org/lwjgl/opengl/MacOSXDisplay.java | 130 ++++++++++++++---- .../lwjgl/opengl/MacOSXMouseEventQueue.java | 113 +++++++++++++++ src/native/macosx/org_lwjgl_opengl_Display.m | 13 +- .../macosx/org_lwjgl_opengl_MacOSXAWTMouse.m | 73 ++++++++++ .../org_lwjgl_opengl_MacOSXCanvasPeerInfo.m | 4 +- ...lwjgl_opengl_MacOSXContextImplementation.m | 7 + 7 files changed, 311 insertions(+), 30 deletions(-) create mode 100644 src/java/org/lwjgl/opengl/MacOSXMouseEventQueue.java create mode 100644 src/native/macosx/org_lwjgl_opengl_MacOSXAWTMouse.m diff --git a/build.xml b/build.xml index cd03ab97..bb544cfd 100644 --- a/build.xml +++ b/build.xml @@ -323,6 +323,7 @@ + diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index ea2d0168..e6b0bde4 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -70,7 +70,7 @@ final class MacOSXDisplay implements DisplayImplementation { private MacOSXCanvasListener canvas_listener; private Canvas canvas; private Robot robot; - //private MacOSXMouseEventQueue mouse_queue; + private MacOSXMouseEventQueue mouse_queue; private KeyboardEventQueue keyboard_queue; private java.awt.DisplayMode requested_mode; @@ -154,7 +154,13 @@ final class MacOSXDisplay implements DisplayImplementation { } private boolean isNativeMode() { - return true; + //return true; + + if (Display.isFullscreen() || Display.getParent() == null) { + return true; + } + + return false; } public void doHandleQuit() { @@ -163,10 +169,22 @@ final class MacOSXDisplay implements DisplayImplementation { } } + public native void nDestroyCALayer(ByteBuffer peer_info_handle); + public native void nDestroyWindow(ByteBuffer window_handle); public void destroyWindow() { - nDestroyWindow(window); + + if (!native_mode) { + DrawableGL gl_drawable = (DrawableGL)Display.getDrawable(); + PeerInfo peer_info = gl_drawable.peer_info; + if (peer_info != null) { + ByteBuffer peer_handle = peer_info.getHandle(); + nDestroyCALayer(peer_handle); + } + } + + nDestroyWindow(window); } public int getGammaRampLength() { @@ -262,8 +280,12 @@ final class MacOSXDisplay implements DisplayImplementation { } public boolean isActive() { - boolean ret = nIsFocused(window); - return ret; + if (native_mode) { + return nIsFocused(window); + } + else { + return Display.getParent().hasFocus(); + } } public Canvas getCanvas() { @@ -311,29 +333,57 @@ final class MacOSXDisplay implements DisplayImplementation { } public void createMouse() throws LWJGLException { - mouse = new MacOSXNativeMouse(this, window); - mouse.register(); + if (native_mode) { + mouse = new MacOSXNativeMouse(this, window); + mouse.register(); + } + else { + this.mouse_queue = new MacOSXMouseEventQueue(canvas); + mouse_queue.register(); + } } public void destroyMouse() { - //MacOSXMouseEventQueue.nGrabMouse(false); - - if (mouse != null) { - mouse.unregister(); - } - mouse = null; + if (native_mode) { + if (mouse != null) { + mouse.unregister(); + } + mouse = null; + } + else { + if (mouse_queue != null) { + MacOSXMouseEventQueue.nGrabMouse(false); + mouse_queue.unregister(); + } + this.mouse_queue = null; + } } public void pollMouse(IntBuffer coord_buffer, ByteBuffer buttons_buffer) { - mouse.poll(coord_buffer, buttons_buffer); + if (native_mode) { + mouse.poll(coord_buffer, buttons_buffer); + } + else { + mouse_queue.poll(coord_buffer, buttons_buffer); + } } public void readMouse(ByteBuffer buffer) { - mouse.copyEvents(buffer); + if (native_mode) { + mouse.copyEvents(buffer); + } + else { + mouse_queue.copyEvents(buffer); + } } public void grabMouse(boolean grab) { - mouse.setGrabbed(grab); + if (native_mode) { + mouse.setGrabbed(grab); + } + else { + mouse_queue.setGrabbed(grab); + } } public int getNativeCursorCapabilities() { @@ -341,10 +391,14 @@ final class MacOSXDisplay implements DisplayImplementation { } public void setCursorPosition(int x, int y) { - if (mouse != null) { - mouse.setCursorPosition(x, y); + if (native_mode) { + if (mouse != null) { + mouse.setCursorPosition(x, y); + } + } + else { + //MacOSXMouseEventQueue.nWarpCursor(x, y); } - //MacOSXMouseEventQueue.nWarpCursor(x, y); } public void setNativeCursor(Object handle) throws LWJGLException { @@ -360,23 +414,47 @@ final class MacOSXDisplay implements DisplayImplementation { /* Keyboard */ public void createKeyboard() throws LWJGLException { - this.keyboard = new MacOSXNativeKeyboard(window); - keyboard.register(); + if (native_mode) { + this.keyboard = new MacOSXNativeKeyboard(window); + keyboard.register(); + } + else { + this.keyboard_queue = new KeyboardEventQueue(canvas); + keyboard_queue.register(); + } } public void destroyKeyboard() { - if (keyboard != null) { - keyboard.unregister(); + if (native_mode) { + if (keyboard != null) { + keyboard.unregister(); + } + keyboard = null; + } + else { + if (keyboard_queue != null) { + keyboard_queue.unregister(); + } + this.keyboard_queue = null; } - keyboard = null; } public void pollKeyboard(ByteBuffer keyDownBuffer) { - keyboard.poll(keyDownBuffer); + if (native_mode) { + keyboard.poll(keyDownBuffer); + } + else { + keyboard_queue.poll(keyDownBuffer); + } } public void readKeyboard(ByteBuffer buffer) { - keyboard.copyEvents(buffer); + if (native_mode) { + keyboard.copyEvents(buffer); + } + else { + keyboard_queue.copyEvents(buffer); + } } /** Native cursor handles */ diff --git a/src/java/org/lwjgl/opengl/MacOSXMouseEventQueue.java b/src/java/org/lwjgl/opengl/MacOSXMouseEventQueue.java new file mode 100644 index 00000000..3104c18c --- /dev/null +++ b/src/java/org/lwjgl/opengl/MacOSXMouseEventQueue.java @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2002-2008 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; + +/** + * An AWT implementation of a LWJGL compatible Mouse event queue. + * @author elias_naur + */ + +import java.awt.Component; +import java.awt.Point; +import java.awt.Rectangle; +import java.nio.IntBuffer; + +import org.lwjgl.BufferUtils; + +final class MacOSXMouseEventQueue extends MouseEventQueue { + private final IntBuffer delta_buffer = BufferUtils.createIntBuffer(2); + + private boolean skip_event; + private static boolean is_grabbed; + + MacOSXMouseEventQueue(Component component) { + super(component); + } + + public void setGrabbed(boolean grab) { + if (is_grabbed != grab) { + super.setGrabbed(grab); + warpCursor(); + grabMouse(grab); + } + } + + private static synchronized void grabMouse(boolean grab) { + is_grabbed = grab; + if (!grab) + nGrabMouse(grab); + } + + protected void resetCursorToCenter() { + super.resetCursorToCenter(); + /* Clear accumulated deltas */ + getMouseDeltas(delta_buffer); + } + + protected void updateDeltas(long nanos) { + super.updateDeltas(nanos); + synchronized ( this ) { + getMouseDeltas(delta_buffer); + int dx = delta_buffer.get(0); + int dy = -delta_buffer.get(1); + if (skip_event) { + skip_event = false; + nGrabMouse(isGrabbed()); + return; + } + if ( dx != 0 || dy != 0 ) { + putMouseEventWithCoords((byte)-1, (byte)0, dx, dy, 0, nanos); + addDelta(dx, dy); + } + } + } + + void warpCursor() { + synchronized (this) { + // If we're going to warp the cursor position, we'll skip the next event to avoid bogus delta values + skip_event = isGrabbed(); + } + if (isGrabbed()) { + Rectangle bounds = getComponent().getBounds(); + Point location_on_screen = getComponent().getLocationOnScreen(); + int x = location_on_screen.x + bounds.width/2; + int y = location_on_screen.y + bounds.height/2; + nWarpCursor(x, y); + } + } + + private static native void getMouseDeltas(IntBuffer delta_buffer); + + private static native void nWarpCursor(int x, int y); + + static native void nGrabMouse(boolean grab); +} \ No newline at end of file diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 93eaadac..902ddbc9 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -495,7 +495,7 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE // Inform the view of its parent window info; [window_info->view setParent:window_info]; - if (peer_info->isCALayer) { + if (!fullscreen && peer_info->isCALayer) { // hidden window when using CALayer [window_info->window orderOut:nil]; } @@ -514,7 +514,7 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv *env, jobject this, jobject window_handle) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - + if (window_info->fullscreen) { [window_info->view exitFullScreenModeWithOptions: nil]; } @@ -534,6 +534,15 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv [pool drain]; } +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyCALayer(JNIEnv *env, jobject this, jobject peer_info_handle) { + MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); + if (peer_info->isCALayer) { + peer_info->isCALayer = false; + [peer_info->glLayer performSelectorOnMainThread:@selector(removeLayer) withObject:nil waitUntilDone:YES]; + [peer_info->glLayer release]; + } +} + JNIEXPORT jint JNICALL Java_org_lwjgl_DefaultSysImplementation_getJNIVersion(JNIEnv *env, jobject ignored) { return org_lwjgl_MacOSXSysImplementation_JNI_VERSION; } diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXAWTMouse.m b/src/native/macosx/org_lwjgl_opengl_MacOSXAWTMouse.m new file mode 100644 index 00000000..16122a82 --- /dev/null +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXAWTMouse.m @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2002-2008 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$ + * + * Mac OS X mouse handling. + * + * @author elias_naur + * @version $Revision$ + */ + +#include +#include +#include "org_lwjgl_opengl_MacOSXMouseEventQueue.h" +#include "common_tools.h" + +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXMouseEventQueue_nGrabMouse(JNIEnv *env, jclass unused, jboolean grab) { + CGAssociateMouseAndMouseCursorPosition(grab == JNI_TRUE ? FALSE : TRUE); + if (grab) + CGDisplayHideCursor(kCGDirectMainDisplay); + else + CGDisplayShowCursor(kCGDirectMainDisplay); +} + +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXMouseEventQueue_nWarpCursor(JNIEnv *env, jclass unused, jint x, jint y) { + CGPoint p; + p.x = x; + p.y = y; + CGWarpMouseCursorPosition(p); +} + +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXMouseEventQueue_getMouseDeltas(JNIEnv *env, jclass unused, jobject delta_buffer) { + CGMouseDelta dx, dy; + CGGetLastMouseDelta(&dx, &dy); + int buffer_length = (*env)->GetDirectBufferCapacity(env, delta_buffer); + if (buffer_length != 2) { + printfDebugJava(env, "Delta buffer not large enough!"); + return; + } + jint *buffer = (*env)->GetDirectBufferAddress(env, delta_buffer); + buffer[0] = dx; + buffer[1] = dy; +} \ No newline at end of file diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m index b4ad9350..6a8e7249 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m @@ -195,7 +195,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle GLint originalReadFBO; // get and save the current fbo values - glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING_EXT, &originalReadFBO); + //glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING_EXT, &originalReadFBO); // read the LWJGL FBO and blit it into this CALayers FBO glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, fboID); @@ -205,7 +205,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle GL_NEAREST); // restore original fbo read value - glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, originalReadFBO); + //glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, originalReadFBO); // call super to finalize the drawing - by default all it does is call glFlush() [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp]; diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m index ae6625d8..6fee4190 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m @@ -192,6 +192,13 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nDestro NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); + + if (context_info->peer_info->isCALayer) { + context_info->peer_info->isCALayer = false; + [context_info->peer_info->glLayer performSelectorOnMainThread:@selector(removeLayer) withObject:nil waitUntilDone:YES]; + [context_info->peer_info->glLayer release]; + } + // clearDrawable on main thread to ensure its not in use [context_info->context performSelectorOnMainThread:@selector(clearDrawable) withObject:nil waitUntilDone:YES]; [context_info->peer_info->window_info->view setOpenGLContext:nil]; From 02c0e80fb6e69081455602a7900369e819828112 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 15 Jan 2013 22:34:50 +0000 Subject: [PATCH 065/111] fix crash when switching to/from fullscreen when using CALayer mode. --- src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m index 6a8e7249..dac1dd08 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m @@ -93,6 +93,9 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle } - (void) removeLayer { + // finish any pending blits before destroying the offscreen window to prevent crashes + glFinish(); + // remove self from root layer id surfaceLayers = (id )macosx_dsi; surfaceLayers.layer = nil; From fc9e4f4bda0e86f04c71694e9f488119d80ba040 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 15 Jan 2013 23:09:03 +0000 Subject: [PATCH 066/111] Tweak CALayer mode to active correctly --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 17 +++++------------ src/native/macosx/org_lwjgl_opengl_Display.m | 10 ++++++++++ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index e6b0bde4..2df4d04e 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -110,6 +110,8 @@ final class MacOSXDisplay implements DisplayImplementation { private native int nGetWidth(ByteBuffer window_handle); private native int nGetHeight(ByteBuffer window_handle); + + private native boolean nIsNativeMode(ByteBuffer peer_info_handle); private static boolean isUndecorated() { return Display.getPrivilegedBoolean("org.lwjgl.opengl.Window.undecorated"); @@ -125,8 +127,6 @@ final class MacOSXDisplay implements DisplayImplementation { close_requested = false; - native_mode = isNativeMode(); - DrawableGL gl_drawable = (DrawableGL)Display.getDrawable(); PeerInfo peer_info = gl_drawable.peer_info; ByteBuffer peer_handle = peer_info.lockAndGetHandle(); @@ -145,6 +145,9 @@ final class MacOSXDisplay implements DisplayImplementation { current_viewport.put(3, mode.getHeight()); } } + + native_mode = nIsNativeMode(peer_handle); + } catch (LWJGLException e) { destroyWindow(); throw e; @@ -152,16 +155,6 @@ final class MacOSXDisplay implements DisplayImplementation { peer_info.unlock(); } } - - private boolean isNativeMode() { - //return true; - - if (Display.isFullscreen() || Display.getParent() == null) { - return true; - } - - return false; - } public void doHandleQuit() { synchronized (this) { diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 902ddbc9..0bc8b129 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -543,6 +543,16 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyCALayer(JNIEn } } +JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nIsNativeMode(JNIEnv *env, jobject this, jobject peer_info_handle) { + MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); + if (peer_info->isCALayer) { + return JNI_FALSE; + } + else { + return JNI_TRUE; + } +} + JNIEXPORT jint JNICALL Java_org_lwjgl_DefaultSysImplementation_getJNIVersion(JNIEnv *env, jobject ignored) { return org_lwjgl_MacOSXSysImplementation_JNI_VERSION; } From b8ba6ee6e55bde41283127136a7f1cf3d1b8e2c4 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Wed, 16 Jan 2013 18:39:19 +0000 Subject: [PATCH 067/111] Fix a small memory leak when using CALayer mode --- src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m index dac1dd08..59785621 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m @@ -58,6 +58,9 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle if(surface->awt.version & 0x80000000) { //JAWT_MACOSX_USE_CALAYER) { if (macosx_dsi != NULL) { + if (peer_info->isCALayer) { + [peer_info->glLayer release]; + } peer_info->glLayer = [GLLayer new]; peer_info->glLayer->macosx_dsi = macosx_dsi; peer_info->glLayer->window_info = peer_info->window_info; From fbb490bb3aee9489d01d7beded8ac7199ba6a721 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Wed, 16 Jan 2013 19:02:09 +0000 Subject: [PATCH 068/111] Reenable the AWT Robot for CALayer mode --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 2df4d04e..c024f236 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -67,7 +67,7 @@ final class MacOSXDisplay implements DisplayImplementation { private static final int PBUFFER_HANDLE_SIZE = 24; private static final int GAMMA_LENGTH = 256; - private MacOSXCanvasListener canvas_listener; + //private MacOSXCanvasListener canvas_listener; private Canvas canvas; private Robot robot; private MacOSXMouseEventQueue mouse_queue; @@ -148,6 +148,10 @@ final class MacOSXDisplay implements DisplayImplementation { native_mode = nIsNativeMode(peer_handle); + if (!native_mode) { + robot = AWTUtil.createRobot(canvas); + } + } catch (LWJGLException e) { destroyWindow(); throw e; @@ -175,6 +179,7 @@ final class MacOSXDisplay implements DisplayImplementation { ByteBuffer peer_handle = peer_info.getHandle(); nDestroyCALayer(peer_handle); } + robot = null; } nDestroyWindow(window); From 4735a6040fb2c528444cff5950ce39d969567b5f Mon Sep 17 00:00:00 2001 From: kappaOne Date: Wed, 16 Jan 2013 22:37:50 +0000 Subject: [PATCH 069/111] Fix crash when dragging mouse and switching from fullscreen to windowed mode --- src/native/macosx/org_lwjgl_opengl_Display.m | 1 + 1 file changed, 1 insertion(+) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 0bc8b129..5e2de0bd 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -516,6 +516,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); if (window_info->fullscreen) { + [window_info->view setParent:nil]; [window_info->view exitFullScreenModeWithOptions: nil]; } else { From 486ff0edeac0158202577f54ab846223768d0e91 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Fri, 25 Jan 2013 00:28:26 +0000 Subject: [PATCH 070/111] Added initial work towards getting native cursors working, not implemented yet --- src/java/org/lwjgl/input/Cursor.java | 6 ++- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 10 +++- .../org/lwjgl/opengl/MacOSXNativeMouse.java | 39 +++++++++++++-- src/native/macosx/context.h | 1 + src/native/macosx/org_lwjgl_opengl_Display.m | 29 +++++++++++ .../org_lwjgl_opengl_MacOSXNativeMouse.m | 50 +++++++++++++++++-- 6 files changed, 125 insertions(+), 10 deletions(-) diff --git a/src/java/org/lwjgl/input/Cursor.java b/src/java/org/lwjgl/input/Cursor.java index 6eec7d0b..315b2f61 100644 --- a/src/java/org/lwjgl/input/Cursor.java +++ b/src/java/org/lwjgl/input/Cursor.java @@ -173,7 +173,11 @@ public class Cursor { CursorElement[] cursors; switch (LWJGLUtil.getPlatform()) { case LWJGLUtil.PLATFORM_MACOSX: - /* Fall through */ + // create our cursor elements + Object handle_mac = Mouse.getImplementation().createCursor(width, height, xHotspot, yHotspot, numImages, images_copy, delays); + CursorElement cursor_element_mac = new CursorElement(handle_mac, -1, -1); + cursors = new CursorElement[]{cursor_element_mac}; + break; case LWJGLUtil.PLATFORM_WINDOWS: // create our cursor elements cursors = new CursorElement[numImages]; diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index c024f236..47ab8494 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -400,6 +400,9 @@ final class MacOSXDisplay implements DisplayImplementation { } public void setNativeCursor(Object handle) throws LWJGLException { + if (native_mode) { + MacOSXNativeMouse.setCursor(handle); + } } public int getMinCursorSize() { @@ -457,7 +460,12 @@ final class MacOSXDisplay implements DisplayImplementation { /** 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); + if (native_mode) { + return MacOSXNativeMouse.createCursor(width, height, xHotspot, yHotspot, numImages, images, delays); + } + else { + return AWTUtil.createCursor(width, height, xHotspot, yHotspot, numImages, images, delays); + } } public void destroyCursor(Object cursor_handle) { diff --git a/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java b/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java index 8736696b..4de64693 100644 --- a/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java +++ b/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java @@ -40,6 +40,7 @@ import java.nio.ByteBuffer; import java.nio.IntBuffer; import org.lwjgl.input.Mouse; +import org.lwjgl.LWJGLException; import java.lang.reflect.*; import java.lang.Integer; @@ -87,11 +88,41 @@ final class MacOSXNativeMouse extends EventQueue { private native void nRegisterMouseListener(ByteBuffer window_handle); private native void nUnregisterMouseListener(ByteBuffer window_handle); + + private static native Object nCreateCursor(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, int images_offset, IntBuffer delays, int delays_offset) throws LWJGLException; + + private static native void nDestroyCursor(Object handle) throws LWJGLException; + + private static native void nSetCursor(Object handle) throws LWJGLException; - public synchronized void register() { - nRegisterMouseListener(window_handle); - } - + public synchronized void register() { + nRegisterMouseListener(window_handle); + } + + public static Object createCursor(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, IntBuffer delays) throws LWJGLException { + try { + return nCreateCursor(width, height, xHotspot, yHotspot, numImages, images, images.position(), delays, delays != null ? delays.position() : -1); + } catch (LWJGLException e) { + throw e; + } + } + + public static void destroyCursor(Object handle) throws LWJGLException { + try { + nDestroyCursor(handle); + } catch (LWJGLException e) { + throw e; + } + } + + public static void setCursor(Object handle) throws LWJGLException { + try { + nSetCursor(handle); + } catch (LWJGLException e) { + throw e; + } + } + public synchronized void setCursorPosition(int x, int y) { nSetCursorPosition(window_handle, x, y); } diff --git a/src/native/macosx/context.h b/src/native/macosx/context.h index e274eb5a..881971e5 100644 --- a/src/native/macosx/context.h +++ b/src/native/macosx/context.h @@ -79,6 +79,7 @@ typedef struct { @private NSOpenGLContext* _openGLContext; NSOpenGLPixelFormat* _pixelFormat; + NSTrackingArea * _trackingArea; } + (NSOpenGLPixelFormat*)defaultPixelFormat; diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 5e2de0bd..75a65593 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -330,6 +330,35 @@ static NSAutoreleasePool *pool; } } +-(void)updateTrackingAreas { + if(_trackingArea != nil) { + [self removeTrackingArea:_trackingArea]; + [_trackingArea release]; + } + + int options = (NSTrackingMouseEnteredAndExited | NSTrackingCursorUpdate | NSTrackingActiveAlways); + _trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] + options:options + owner:self + userInfo:nil]; + [self addTrackingArea:_trackingArea]; +} + +-(void)mouseEntered:(NSEvent *)event { + // TODO + NSLog(@"MOUSE ENTERED"); +} + +-(void)mouseExited:(NSEvent *)event { + // TODO + NSLog(@"MOUSE EXITED"); +} + +-(void)cursorUpdate:(NSEvent *)event { + // TODO + NSLog(@"CURSOR UPDATE"); +} + - (void) drawRect:(NSRect)rect { // set black as the default background color // for the nsview to avoid white flash on fullscreen diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m index 37947810..036ecfdb 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m @@ -90,12 +90,54 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nSetCursorPositio JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nRegisterMouseListener(JNIEnv *env, jobject _this, jobject window_handle) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - [window_info->window setAcceptsMouseMovedEvents:YES]; - window_info->jmouse = (*env)->NewGlobalRef(env, _this); + [window_info->window setAcceptsMouseMovedEvents:YES]; + window_info->jmouse = (*env)->NewGlobalRef(env, _this); } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nUnregisterMouseListener(JNIEnv *env, jobject this, jobject window_handle) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - [window_info->window setAcceptsMouseMovedEvents:NO]; - window_info->jmouse = nil; + [window_info->window setAcceptsMouseMovedEvents:NO]; + window_info->jmouse = nil; +} + +JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nCreateCursor(JNIEnv *env, jobject _this, jint width, jint height, jint x_hotspot, jint y_hotspot, jint num_images, jobject image_buffer, jint images_offset, jobject delay_buffer, jint delays_offset) { + NSLog(@"nCreateCursor"); + + char *bytes = (char *)(*env)->GetDirectBufferAddress(env, image_buffer); + + NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] + initWithBitmapDataPlanes:(char *)&bytes + pixelsWide:width pixelsHigh:height + bitsPerSample:8 + samplesPerPixel:4 + hasAlpha:YES + isPlanar:NO + colorSpaceName:NSDeviceRGBColorSpace + bitmapFormat:0 + bytesPerRow:0 + bitsPerPixel:0]; + + NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(width, height)]; + + [image addRepresentation:bitmap]; + + + NSCursor *cursor = [[NSCursor alloc] initWithImage:image hotSpot:NSMakePoint(x_hotspot, y_hotspot)]; + + [cursor set]; // temporarily set the cursor here as returning the handle doesn't work yet + + return cursor; +} + +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nDestroyCursor(JNIEnv *env, jobject _this, jobject handle) { + // TODO +} + +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nSetCursor(JNIEnv *env, jobject _this, jobject handle) { + NSLog(@"nSetCursor"); + + // TODO - this method should get the cursor from the handle and set it + + //NSCursor *cursor = (NSCursor *)(*env)->GetDirectBufferAddress(env, handle); + //[cursor set]; } From fc2889cc27e0040ee0415d82f41775b53f0adf50 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Fri, 25 Jan 2013 01:03:13 +0000 Subject: [PATCH 071/111] Fix ARGB order on Native Cursors --- src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m index 036ecfdb..bc625600 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m @@ -113,9 +113,9 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nCreateCursor( hasAlpha:YES isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace - bitmapFormat:0 - bytesPerRow:0 - bitsPerPixel:0]; + bitmapFormat:NSAlphaNonpremultipliedBitmapFormat + bytesPerRow:width*4 + bitsPerPixel:32]; NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(width, height)]; From f0219aed1aa481fa05989b4ad935adff1745e796 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sat, 26 Jan 2013 22:47:36 +0000 Subject: [PATCH 072/111] Fix native cursor global handle passing --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 11 ++++++--- .../org/lwjgl/opengl/MacOSXNativeMouse.java | 12 +++++----- .../org_lwjgl_opengl_MacOSXNativeMouse.m | 24 +++++++------------ 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 47ab8494..832055f1 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -401,7 +401,7 @@ final class MacOSXDisplay implements DisplayImplementation { public void setNativeCursor(Object handle) throws LWJGLException { if (native_mode) { - MacOSXNativeMouse.setCursor(handle); + MacOSXNativeMouse.setCursor(getCursorHandle(handle)); } } @@ -457,11 +457,12 @@ final class MacOSXDisplay implements DisplayImplementation { keyboard_queue.copyEvents(buffer); } } - + /** Native cursor handles */ public Object createCursor(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, IntBuffer delays) throws LWJGLException { if (native_mode) { - return MacOSXNativeMouse.createCursor(width, height, xHotspot, yHotspot, numImages, images, delays); + long cursor = MacOSXNativeMouse.createCursor(width, height, xHotspot, yHotspot, numImages, images, delays); + return cursor; } else { return AWTUtil.createCursor(width, height, xHotspot, yHotspot, numImages, images, delays); @@ -471,6 +472,10 @@ final class MacOSXDisplay implements DisplayImplementation { public void destroyCursor(Object cursor_handle) { } + + private static long getCursorHandle(Object cursor_handle) { + return cursor_handle != null ? (Long)cursor_handle : 0; + } public int getPbufferCapabilities() { return Pbuffer.PBUFFER_SUPPORTED; diff --git a/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java b/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java index 4de64693..013fcd56 100644 --- a/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java +++ b/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java @@ -89,17 +89,17 @@ final class MacOSXNativeMouse extends EventQueue { private native void nUnregisterMouseListener(ByteBuffer window_handle); - private static native Object nCreateCursor(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, int images_offset, IntBuffer delays, int delays_offset) throws LWJGLException; + private static native long nCreateCursor(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, int images_offset, IntBuffer delays, int delays_offset) throws LWJGLException; - private static native void nDestroyCursor(Object handle) throws LWJGLException; + private static native void nDestroyCursor(long handle) throws LWJGLException; - private static native void nSetCursor(Object handle) throws LWJGLException; + private static native void nSetCursor(long handle) throws LWJGLException; public synchronized void register() { nRegisterMouseListener(window_handle); } - public static Object createCursor(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, IntBuffer delays) throws LWJGLException { + public static long createCursor(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, IntBuffer delays) throws LWJGLException { try { return nCreateCursor(width, height, xHotspot, yHotspot, numImages, images, images.position(), delays, delays != null ? delays.position() : -1); } catch (LWJGLException e) { @@ -107,7 +107,7 @@ final class MacOSXNativeMouse extends EventQueue { } } - public static void destroyCursor(Object handle) throws LWJGLException { + public static void destroyCursor(long handle) throws LWJGLException { try { nDestroyCursor(handle); } catch (LWJGLException e) { @@ -115,7 +115,7 @@ final class MacOSXNativeMouse extends EventQueue { } } - public static void setCursor(Object handle) throws LWJGLException { + public static void setCursor(long handle) throws LWJGLException { try { nSetCursor(handle); } catch (LWJGLException e) { diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m index bc625600..1a8e69a4 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m @@ -100,13 +100,11 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nUnregisterMouseL window_info->jmouse = nil; } -JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nCreateCursor(JNIEnv *env, jobject _this, jint width, jint height, jint x_hotspot, jint y_hotspot, jint num_images, jobject image_buffer, jint images_offset, jobject delay_buffer, jint delays_offset) { - NSLog(@"nCreateCursor"); - - char *bytes = (char *)(*env)->GetDirectBufferAddress(env, image_buffer); +JNIEXPORT jlong JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nCreateCursor(JNIEnv *env, jobject _this, jint width, jint height, jint x_hotspot, jint y_hotspot, jint num_images, jobject image_buffer, jint images_offset, jobject delay_buffer, jint delays_offset) { + jint *bytes = (jint *)(*env)->GetDirectBufferAddress(env, image_buffer); NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] - initWithBitmapDataPlanes:(char *)&bytes + initWithBitmapDataPlanes:(jint *)&bytes pixelsWide:width pixelsHigh:height bitsPerSample:8 samplesPerPixel:4 @@ -124,20 +122,14 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nCreateCursor( NSCursor *cursor = [[NSCursor alloc] initWithImage:image hotSpot:NSMakePoint(x_hotspot, y_hotspot)]; - [cursor set]; // temporarily set the cursor here as returning the handle doesn't work yet - - return cursor; + return (jlong)cursor; } -JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nDestroyCursor(JNIEnv *env, jobject _this, jobject handle) { +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nDestroyCursor(JNIEnv *env, jobject _this, jlong handle) { // TODO } -JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nSetCursor(JNIEnv *env, jobject _this, jobject handle) { - NSLog(@"nSetCursor"); - - // TODO - this method should get the cursor from the handle and set it - - //NSCursor *cursor = (NSCursor *)(*env)->GetDirectBufferAddress(env, handle); - //[cursor set]; +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nSetCursor(JNIEnv *env, jobject _this, jlong cursor_pointer) { + NSCursor *cursor = (NSCursor *)cursor_pointer; + [cursor set]; } From 6c090f1f61267769bf2751024449451980f295e5 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 27 Jan 2013 23:01:21 +0000 Subject: [PATCH 073/111] Implement Mouse.isInsideWindow() on OS X --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 16 +++++++++- .../org/lwjgl/opengl/MacOSXNativeMouse.java | 12 +++---- src/native/macosx/org_lwjgl_opengl_Display.m | 31 +++++++++++++------ 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 832055f1..71e5a4a0 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -83,6 +83,8 @@ final class MacOSXDisplay implements DisplayImplementation { private boolean skipViewportValue = false; private static final IntBuffer current_viewport = BufferUtils.createIntBuffer(16); + private boolean mouseInsideWindow; + private boolean close_requested; private boolean native_mode = true; @@ -165,6 +167,18 @@ final class MacOSXDisplay implements DisplayImplementation { close_requested = true; } } + + public void mouseInsideWindow() { + synchronized (this) { + mouseInsideWindow = true; + } + } + + public void mouseOutsideWindow() { + synchronized (this) { + mouseInsideWindow = false; + } + } public native void nDestroyCALayer(ByteBuffer peer_info_handle); @@ -564,7 +578,7 @@ final class MacOSXDisplay implements DisplayImplementation { } public boolean isInsideWindow() { - return true; + return mouseInsideWindow; } public void setResizable(boolean resizable) { diff --git a/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java b/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java index 013fcd56..ef204757 100644 --- a/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java +++ b/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java @@ -91,9 +91,9 @@ final class MacOSXNativeMouse extends EventQueue { private static native long nCreateCursor(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, int images_offset, IntBuffer delays, int delays_offset) throws LWJGLException; - private static native void nDestroyCursor(long handle) throws LWJGLException; + private static native void nDestroyCursor(long cursor_handle) throws LWJGLException; - private static native void nSetCursor(long handle) throws LWJGLException; + private static native void nSetCursor(long cursor_handle) throws LWJGLException; public synchronized void register() { nRegisterMouseListener(window_handle); @@ -107,17 +107,17 @@ final class MacOSXNativeMouse extends EventQueue { } } - public static void destroyCursor(long handle) throws LWJGLException { + public static void destroyCursor(long cursor_handle) throws LWJGLException { try { - nDestroyCursor(handle); + nDestroyCursor(cursor_handle); } catch (LWJGLException e) { throw e; } } - public static void setCursor(long handle) throws LWJGLException { + public static void setCursor(long cursor_handle) throws LWJGLException { try { - nSetCursor(handle); + nSetCursor(cursor_handle); } catch (LWJGLException e) { throw e; } diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 75a65593..308571d8 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -336,7 +336,7 @@ static NSAutoreleasePool *pool; [_trackingArea release]; } - int options = (NSTrackingMouseEnteredAndExited | NSTrackingCursorUpdate | NSTrackingActiveAlways); + int options = (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways); _trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] options:options owner:self @@ -345,18 +345,25 @@ static NSAutoreleasePool *pool; } -(void)mouseEntered:(NSEvent *)event { - // TODO - NSLog(@"MOUSE ENTERED"); + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil || _parent->jdisplay == nil) { + return; + } + + jclass display_class = (*env)->GetObjectClass(env, _parent->jdisplay); + jmethodID mouseInsideWindow_callback = (*env)->GetMethodID(env, display_class, "mouseInsideWindow", "()V"); + (*env)->CallVoidMethod(env, _parent->jdisplay, mouseInsideWindow_callback); } -(void)mouseExited:(NSEvent *)event { - // TODO - NSLog(@"MOUSE EXITED"); -} - --(void)cursorUpdate:(NSEvent *)event { - // TODO - NSLog(@"CURSOR UPDATE"); + JNIEnv *env = attachCurrentThread(); + if (env == nil || event == nil || _parent == nil || _parent->jdisplay == nil) { + return; + } + + jclass display_class = (*env)->GetObjectClass(env, _parent->jdisplay); + jmethodID mouseOutsideWindow_callback = (*env)->GetMethodID(env, display_class, "mouseOutsideWindow", "()V"); + (*env)->CallVoidMethod(env, _parent->jdisplay, mouseOutsideWindow_callback); } - (void) drawRect:(NSRect)rect { @@ -473,6 +480,10 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE window_info->window = [peer_info->parent window]; [peer_info->parent addSubview:window_info->view]; [window_info->view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; + + if (window_info->jdisplay == NULL) { + window_info->jdisplay = (*env)->NewGlobalRef(env, this); + } } } else { From 27cfdf646e1fd24fa8cae237ccaa4443e1293349 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 27 Jan 2013 23:45:08 +0000 Subject: [PATCH 074/111] Improve native cursor behaviour --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 25 ++++++++++++++++++- .../org_lwjgl_opengl_MacOSXNativeMouse.m | 10 ++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 71e5a4a0..7ecd404f 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -88,6 +88,10 @@ final class MacOSXDisplay implements DisplayImplementation { private boolean close_requested; private boolean native_mode = true; + + private boolean updateNativeCursor = false; + + private long currentNativeCursor = 0; MacOSXDisplay() { @@ -172,12 +176,15 @@ final class MacOSXDisplay implements DisplayImplementation { synchronized (this) { mouseInsideWindow = true; } + updateNativeCursor = true; } public void mouseOutsideWindow() { synchronized (this) { mouseInsideWindow = false; } + + updateNativeCursor = true; } public native void nDestroyCALayer(ByteBuffer peer_info_handle); @@ -327,6 +334,21 @@ final class MacOSXDisplay implements DisplayImplementation { else glGetInteger(GL_VIEWPORT, current_viewport); glViewport(current_viewport.get(0), current_viewport.get(1), current_viewport.get(2), current_viewport.get(3)); } + + if (native_mode && updateNativeCursor) { + updateNativeCursor = false; + try { + if (mouseInsideWindow) { + setNativeCursor(currentNativeCursor); + } + else { + // restore default cursor if outside Display + MacOSXNativeMouse.setCursor(0); + } + } catch (LWJGLException e) { + e.printStackTrace(); + } + } } public void reshape(int x, int y, int width, int height) { @@ -415,7 +437,8 @@ final class MacOSXDisplay implements DisplayImplementation { public void setNativeCursor(Object handle) throws LWJGLException { if (native_mode) { - MacOSXNativeMouse.setCursor(getCursorHandle(handle)); + currentNativeCursor = getCursorHandle(handle); + MacOSXNativeMouse.setCursor(currentNativeCursor); } } diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m index 1a8e69a4..c0a8ff03 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m @@ -130,6 +130,12 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nDestroyCursor(JN } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nSetCursor(JNIEnv *env, jobject _this, jlong cursor_pointer) { - NSCursor *cursor = (NSCursor *)cursor_pointer; - [cursor set]; + if (cursor_pointer == 0) { + // restore default cursor + [[NSCursor arrowCursor] set]; + } + else { + NSCursor *cursor = (NSCursor *)cursor_pointer; + [cursor set]; + } } From 4e023eec168517c7a9308decb8b4980e7439eb48 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 29 Jan 2013 22:59:40 +0000 Subject: [PATCH 075/111] Implement animated cursor support on OS X --- src/java/org/lwjgl/input/Cursor.java | 21 ++++++++++++------- .../org_lwjgl_opengl_MacOSXNativeMouse.m | 4 ++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/java/org/lwjgl/input/Cursor.java b/src/java/org/lwjgl/input/Cursor.java index 315b2f61..c38c10f3 100644 --- a/src/java/org/lwjgl/input/Cursor.java +++ b/src/java/org/lwjgl/input/Cursor.java @@ -163,20 +163,27 @@ public class Cursor { IntBuffer images_copy = BufferUtils.createIntBuffer(images.remaining()); flipImages(width, height, numImages, images, images_copy); - // Win32 doesn't (afaik) allow for animation based cursors, except when they're - // in the .ani format, which we don't support. + // Mac and Windows doesn't (afaik) allow for animation based cursors, except in the .ani + // format on Windows, which we don't support. // The cursor animation was therefor developed using java side time tracking. // unfortunately X flickers when changing cursor. We therefore check for either - // Win32 or X and do accordingly. This hasn't been implemented on Mac, but we - // might want to split it into a X/Win/Mac cursor if it gets too cluttered + // Windows, Mac or X and do accordingly. + // we might want to split it into a X/Win/Mac cursor if it gets too cluttered CursorElement[] cursors; switch (LWJGLUtil.getPlatform()) { case LWJGLUtil.PLATFORM_MACOSX: // create our cursor elements - Object handle_mac = Mouse.getImplementation().createCursor(width, height, xHotspot, yHotspot, numImages, images_copy, delays); - CursorElement cursor_element_mac = new CursorElement(handle_mac, -1, -1); - cursors = new CursorElement[]{cursor_element_mac}; + cursors = new CursorElement[numImages]; + for(int i=0; iGetDirectBufferAddress(env, image_buffer); + jlong *bytes = (jint *)(*env)->GetDirectBufferAddress(env, image_buffer) + images_offset; NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] - initWithBitmapDataPlanes:(jint *)&bytes + initWithBitmapDataPlanes:(jlong *)&bytes pixelsWide:width pixelsHigh:height bitsPerSample:8 samplesPerPixel:4 From 0d259140293aab208d45bab4eda33eb60d8e9263 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Thu, 31 Jan 2013 22:44:43 +0000 Subject: [PATCH 076/111] Fix colors on OS X native cursors --- src/java/org/lwjgl/input/Cursor.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/java/org/lwjgl/input/Cursor.java b/src/java/org/lwjgl/input/Cursor.java index c38c10f3..7f257f63 100644 --- a/src/java/org/lwjgl/input/Cursor.java +++ b/src/java/org/lwjgl/input/Cursor.java @@ -173,6 +173,10 @@ public class Cursor { CursorElement[] cursors; switch (LWJGLUtil.getPlatform()) { case LWJGLUtil.PLATFORM_MACOSX: + + // OS X requires the image format to be in ABGR format + convertARGBtoABGR(images_copy); + // create our cursor elements cursors = new CursorElement[numImages]; for(int i=0; i>> 24); + byte blue = (byte)(argbColor >>> 16); + byte green = (byte)(argbColor >>> 8); + byte red = (byte)argbColor; + + int abgrColor = ((alpha & 0xff) << 24 ) + ((red & 0xff) << 16 ) + ((green & 0xff) << 8 ) + ((blue & 0xff) ); + + imageBuffer.put(i, abgrColor); + } + } /** * Flips the images so they're oriented according to opengl From b27754089a46a8046f5c262772542bbf3ef7bf82 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Thu, 31 Jan 2013 23:01:19 +0000 Subject: [PATCH 077/111] Fix Display.setParent() on Java 7 by forcing CALayer usage as classic NSView method is no long available. --- .../org/lwjgl/opengl/MacOSXCanvasPeerInfo.java | 14 ++++++++++++-- .../macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m | 6 +++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXCanvasPeerInfo.java b/src/java/org/lwjgl/opengl/MacOSXCanvasPeerInfo.java index 684a7a5d..76d59954 100644 --- a/src/java/org/lwjgl/opengl/MacOSXCanvasPeerInfo.java +++ b/src/java/org/lwjgl/opengl/MacOSXCanvasPeerInfo.java @@ -51,9 +51,19 @@ abstract class MacOSXCanvasPeerInfo extends MacOSXPeerInfo { } protected void initHandle(Canvas component) throws LWJGLException { - nInitHandle(awt_surface.lockAndGetHandle(component), getHandle()); + boolean forceCALayer = true; + String javaVersion = System.getProperty("java.version"); + + if (javaVersion.startsWith("1.5") || javaVersion.startsWith("1.6")) { + // On Java 7 and newer CALayer mode is the only way to use OpenGL with AWT + // therefore force it on all JVM's except for the older Java 5 and Java 6 + // where the older cocoaViewRef NSView method maybe be available. + forceCALayer = false; + } + + nInitHandle(awt_surface.lockAndGetHandle(component), getHandle(), forceCALayer); } - private static native void nInitHandle(ByteBuffer surface_buffer, ByteBuffer peer_info_handle) throws LWJGLException; + private static native void nInitHandle(ByteBuffer surface_buffer, ByteBuffer peer_info_handle, boolean forceCALayer) throws LWJGLException; protected void doUnlock() throws LWJGLException { awt_surface.unlock(); diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m index 59785621..7e43de43 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m @@ -47,15 +47,15 @@ #include "common_tools.h" JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle -(JNIEnv *env, jclass clazz, jobject lock_buffer_handle, jobject peer_info_handle) { +(JNIEnv *env, jclass clazz, jobject lock_buffer_handle, jobject peer_info_handle, jboolean forceCALayer) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); AWTSurfaceLock *surface = (AWTSurfaceLock *)(*env)->GetDirectBufferAddress(env, lock_buffer_handle); JAWT_MacOSXDrawingSurfaceInfo *macosx_dsi = (JAWT_MacOSXDrawingSurfaceInfo *)surface->dsi->platformInfo; - // check for CALayer support - if(surface->awt.version & 0x80000000) { //JAWT_MACOSX_USE_CALAYER) { + // force CALayer usage or check if CALayer is supported (i.e. on Java 5 and Java 6) + if(forceCALayer || (surface->awt.version & 0x80000000)) { //JAWT_MACOSX_USE_CALAYER) { if (macosx_dsi != NULL) { if (peer_info->isCALayer) { From 549f07fd28a2f513992ee588346beaa58cb01b61 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Thu, 31 Jan 2013 23:47:50 +0000 Subject: [PATCH 078/111] Release NSView object on Display.destroy() --- src/native/macosx/org_lwjgl_opengl_Display.m | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 308571d8..76fdd237 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -565,10 +565,9 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv if ([window_info->window contentView] == window_info->view) { [window_info->window close]; } - else { - // the nsview has a parent, remove it from there - [window_info->view removeFromSuperviewWithoutNeedingDisplay]; - } + + // release the nsview and remove it from any parent nsview + [window_info->view removeFromSuperviewWithoutNeedingDisplay]; } } From abeb9f389cb6349b611fc25f7a97302863ff0d7d Mon Sep 17 00:00:00 2001 From: kappaOne Date: Fri, 1 Feb 2013 22:28:10 +0000 Subject: [PATCH 079/111] Fix Mouse.isInsideWindow() when starting in fullscreen mode --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 7ecd404f..dbe1d410 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -153,6 +153,7 @@ final class MacOSXDisplay implements DisplayImplementation { } native_mode = nIsNativeMode(peer_handle); + mouseInsideWindow = true; if (!native_mode) { robot = AWTUtil.createRobot(canvas); From 4e1d53909563ed278296e51035005942ac795e04 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sat, 2 Feb 2013 01:15:41 +0000 Subject: [PATCH 080/111] Further fix Native Mouse Cursor behaviour --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index dbe1d410..bf1f0ac8 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -39,7 +39,6 @@ package org.lwjgl.opengl; */ import java.awt.Canvas; -import java.awt.Cursor; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Robot; @@ -52,6 +51,7 @@ import java.security.PrivilegedExceptionAction; import java.util.ArrayList; import java.util.List; +import org.lwjgl.input.Cursor; import org.lwjgl.BufferUtils; import org.lwjgl.MemoryUtil; import org.lwjgl.LWJGLException; @@ -154,6 +154,7 @@ final class MacOSXDisplay implements DisplayImplementation { native_mode = nIsNativeMode(peer_handle); mouseInsideWindow = true; + updateNativeCursor = true; if (!native_mode) { robot = AWTUtil.createRobot(canvas); @@ -422,6 +423,10 @@ final class MacOSXDisplay implements DisplayImplementation { } public int getNativeCursorCapabilities() { + if (native_mode) { + return Cursor.CURSOR_ONE_BIT_TRANSPARENCY; + } + return AWTUtil.getNativeCursorCapabilities(); } @@ -439,7 +444,9 @@ final class MacOSXDisplay implements DisplayImplementation { public void setNativeCursor(Object handle) throws LWJGLException { if (native_mode) { currentNativeCursor = getCursorHandle(handle); - MacOSXNativeMouse.setCursor(currentNativeCursor); + if (Display.isCreated()) { + MacOSXNativeMouse.setCursor(currentNativeCursor); + } } } From a5c7ec0017521b5cb894481de4dbc14be1dc3989 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sat, 2 Feb 2013 19:58:18 +0000 Subject: [PATCH 081/111] Return correct OS X Cursor.getCapabilities() value --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index bf1f0ac8..ada859c6 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -424,7 +424,7 @@ final class MacOSXDisplay implements DisplayImplementation { public int getNativeCursorCapabilities() { if (native_mode) { - return Cursor.CURSOR_ONE_BIT_TRANSPARENCY; + return Cursor.CURSOR_ONE_BIT_TRANSPARENCY | Cursor.CURSOR_8_BIT_ALPHA | Cursor.CURSOR_ANIMATION; } return AWTUtil.getNativeCursorCapabilities(); From 2d43265d10131e57e7edbda3c3916a5278a5b559 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sat, 2 Feb 2013 20:30:12 +0000 Subject: [PATCH 082/111] Fix Display.getWidth()/getHeight() to return size of NSView instead of NSWindow, fix a crash on exit --- src/native/macosx/org_lwjgl_opengl_Display.m | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 76fdd237..0eac2227 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -325,7 +325,7 @@ static NSAutoreleasePool *pool; - (void)windowResized:(NSNotification *)notification; { if (_parent != nil) { - _parent->display_rect = [[self window] frame]; + _parent->display_rect = [self frame]; _parent->resized = JNI_TRUE; } } @@ -463,6 +463,8 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE NSRect view_rect = NSMakeRect(0.0, 0.0, width, height); window_info->view = [[MacOSXOpenGLView alloc] initWithFrame:view_rect pixelFormat:peer_info->pixel_format]; + [window_info->view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; + if (window_info->context != nil) { [window_info->view setOpenGLContext:window_info->context]; } @@ -479,7 +481,6 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE else { window_info->window = [peer_info->parent window]; [peer_info->parent addSubview:window_info->view]; - [window_info->view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; if (window_info->jdisplay == NULL) { window_info->jdisplay = (*env)->NewGlobalRef(env, this); @@ -555,8 +556,10 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv *env, jobject this, jobject window_handle) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + // remove parent to stop receiving input + [window_info->view setParent:nil]; + if (window_info->fullscreen) { - [window_info->view setParent:nil]; [window_info->view exitFullScreenModeWithOptions: nil]; } else { From 65beebb3795cf64b92a844467e3496f25c517883 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 3 Feb 2013 16:39:02 +0000 Subject: [PATCH 083/111] fix crash related to mouse being dragging whilst Display is destroyed. --- src/native/macosx/org_lwjgl_opengl_Display.m | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 0eac2227..2dc923d4 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -252,7 +252,7 @@ static NSAutoreleasePool *pool; - (void)mouseDragged:(NSEvent *)event { JNIEnv *env = attachCurrentThread(); - if (env == nil || event == nil || _parent == nil) { + if (env == nil || event == nil || _parent == nil || _parent->jmouse == nil) { return; } long time = [event timestamp] * 1000000000; @@ -264,7 +264,7 @@ static NSAutoreleasePool *pool; - (void)rightMouseDragged:(NSEvent *)event { JNIEnv *env = attachCurrentThread(); - if (env == nil || event == nil || _parent == nil) { + if (env == nil || event == nil || _parent == nil || _parent->jmouse == nil) { return; } long time = [event timestamp] * 1000000000; @@ -276,7 +276,7 @@ static NSAutoreleasePool *pool; - (void)otherMouseDragged:(NSEvent *)event { JNIEnv *env = attachCurrentThread(); - if (env == nil || event == nil || _parent == nil) { + if (env == nil || event == nil || _parent == nil || _parent->jmouse == nil) { return; } long time = [event timestamp] * 1000000000; @@ -556,9 +556,6 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv *env, jobject this, jobject window_handle) { MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - // remove parent to stop receiving input - [window_info->view setParent:nil]; - if (window_info->fullscreen) { [window_info->view exitFullScreenModeWithOptions: nil]; } From bf13ed9cc3216c434f3040dfaf9cfa18514a9a9c Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 3 Feb 2013 16:52:45 +0000 Subject: [PATCH 084/111] Restore default mouse cursor on mouse destroy --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index ada859c6..172f663c 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -381,6 +381,11 @@ final class MacOSXDisplay implements DisplayImplementation { public void destroyMouse() { if (native_mode) { + // restore default native cursor + try { + MacOSXNativeMouse.setCursor(0); + } catch (LWJGLException e) {}; + if (mouse != null) { mouse.unregister(); } From c328463776568825cd39dcfbd90b3d039d2f031f Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 3 Feb 2013 17:47:09 +0000 Subject: [PATCH 085/111] Implement Cursor.destroy and release memory used by native cursors --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 11 +++++++++-- .../org/lwjgl/opengl/MacOSXNativeMouse.java | 10 +++------- .../macosx/org_lwjgl_opengl_MacOSXNativeMouse.m | 17 ++++++++++++----- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 172f663c..c646a278 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -441,9 +441,9 @@ final class MacOSXDisplay implements DisplayImplementation { mouse.setCursorPosition(x, y); } } - else { + //else { //MacOSXMouseEventQueue.nWarpCursor(x, y); - } + //} } public void setNativeCursor(Object handle) throws LWJGLException { @@ -520,7 +520,14 @@ final class MacOSXDisplay implements DisplayImplementation { } public void destroyCursor(Object cursor_handle) { + long handle = getCursorHandle(cursor_handle); + // reset current cursor if same + if (currentNativeCursor == handle) { + currentNativeCursor = 0; + } + + MacOSXNativeMouse.destroyCursor(handle); } private static long getCursorHandle(Object cursor_handle) { diff --git a/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java b/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java index ef204757..6b1af8c3 100644 --- a/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java +++ b/src/java/org/lwjgl/opengl/MacOSXNativeMouse.java @@ -91,7 +91,7 @@ final class MacOSXNativeMouse extends EventQueue { private static native long nCreateCursor(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, int images_offset, IntBuffer delays, int delays_offset) throws LWJGLException; - private static native void nDestroyCursor(long cursor_handle) throws LWJGLException; + private static native void nDestroyCursor(long cursor_handle); private static native void nSetCursor(long cursor_handle) throws LWJGLException; @@ -107,12 +107,8 @@ final class MacOSXNativeMouse extends EventQueue { } } - public static void destroyCursor(long cursor_handle) throws LWJGLException { - try { - nDestroyCursor(cursor_handle); - } catch (LWJGLException e) { - throw e; - } + public static void destroyCursor(long cursor_handle) { + nDestroyCursor(cursor_handle); } public static void setCursor(long cursor_handle) throws LWJGLException { diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m index 9b919f9c..27f07a13 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m @@ -101,9 +101,11 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nUnregisterMouseL } JNIEXPORT jlong JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nCreateCursor(JNIEnv *env, jobject _this, jint width, jint height, jint x_hotspot, jint y_hotspot, jint num_images, jobject image_buffer, jint images_offset, jobject delay_buffer, jint delays_offset) { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + jlong *bytes = (jint *)(*env)->GetDirectBufferAddress(env, image_buffer) + images_offset; - NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] + NSBitmapImageRep *bitmap = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:(jlong *)&bytes pixelsWide:width pixelsHigh:height bitsPerSample:8 @@ -113,20 +115,25 @@ JNIEXPORT jlong JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nCreateCursor(JN colorSpaceName:NSDeviceRGBColorSpace bitmapFormat:NSAlphaNonpremultipliedBitmapFormat bytesPerRow:width*4 - bitsPerPixel:32]; + bitsPerPixel:32] autorelease]; - NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(width, height)]; + NSImage *image = [[[NSImage alloc] initWithSize:NSMakeSize(width, height)] autorelease]; [image addRepresentation:bitmap]; NSCursor *cursor = [[NSCursor alloc] initWithImage:image hotSpot:NSMakePoint(x_hotspot, y_hotspot)]; + [pool release]; + return (jlong)cursor; } -JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nDestroyCursor(JNIEnv *env, jobject _this, jlong handle) { - // TODO +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nDestroyCursor(JNIEnv *env, jobject _this, jlong cursor_pointer) { + if (cursor_pointer != 0) { + NSCursor *cursor = (NSCursor *)cursor_pointer; + [cursor release]; + } } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nSetCursor(JNIEnv *env, jobject _this, jlong cursor_pointer) { From f6a2bc832732bd2835f3f8b4d7ee4b0cfd8f8d6f Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 3 Feb 2013 18:58:00 +0000 Subject: [PATCH 086/111] Detect correctly if mouse is inside or outside the Display on creation and resize. --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 2 -- src/native/macosx/org_lwjgl_opengl_Display.m | 16 ++++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index c646a278..2f6251f9 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -153,8 +153,6 @@ final class MacOSXDisplay implements DisplayImplementation { } native_mode = nIsNativeMode(peer_handle); - mouseInsideWindow = true; - updateNativeCursor = true; if (!native_mode) { robot = AWTUtil.createRobot(canvas); diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 2dc923d4..b197af03 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -342,11 +342,23 @@ static NSAutoreleasePool *pool; owner:self userInfo:nil]; [self addTrackingArea:_trackingArea]; + + // since nstrackingarea's don't know if mouse is inside or outside on creation + // manually detect this and send a fake mouse entered/exited message + NSPoint mouseLocation = [[self window] mouseLocationOutsideOfEventStream]; + mouseLocation = [self convertPoint:mouseLocation fromView:nil]; + + if (NSPointInRect(mouseLocation, [self bounds])) { + [self mouseEntered: nil]; + } + else { + [self mouseExited: nil]; + } } -(void)mouseEntered:(NSEvent *)event { JNIEnv *env = attachCurrentThread(); - if (env == nil || event == nil || _parent == nil || _parent->jdisplay == nil) { + if (env == nil || _parent == nil || _parent->jdisplay == nil) { return; } @@ -357,7 +369,7 @@ static NSAutoreleasePool *pool; -(void)mouseExited:(NSEvent *)event { JNIEnv *env = attachCurrentThread(); - if (env == nil || event == nil || _parent == nil || _parent->jdisplay == nil) { + if (env == nil || _parent == nil || _parent->jdisplay == nil) { return; } From c57c27abaf0dc8d42314554e8478696f9f5e7ce7 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 3 Feb 2013 20:54:27 +0000 Subject: [PATCH 087/111] remove nsview on main thread to avoid thread issues --- src/native/macosx/org_lwjgl_opengl_Display.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index b197af03..c10b6fe2 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -349,10 +349,10 @@ static NSAutoreleasePool *pool; mouseLocation = [self convertPoint:mouseLocation fromView:nil]; if (NSPointInRect(mouseLocation, [self bounds])) { - [self mouseEntered: nil]; + [self mouseEntered:nil]; } else { - [self mouseExited: nil]; + [self mouseExited:nil]; } } @@ -578,8 +578,8 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv [window_info->window close]; } - // release the nsview and remove it from any parent nsview - [window_info->view removeFromSuperviewWithoutNeedingDisplay]; + // release the nsview and remove it from any parent nsview using main thread + [window_info->view performSelectorOnMainThread:@selector(removeFromSuperviewWithoutNeedingDisplay) withObject:window_info->view waitUntilDone:YES]; } } From e15d1720873d908131cdec1719eab22dcded539d Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 3 Feb 2013 20:55:33 +0000 Subject: [PATCH 088/111] report Mouse.getX()/getY() values correctly on initial display creation. --- .../org_lwjgl_opengl_MacOSXNativeMouse.m | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m index 27f07a13..208496d0 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m @@ -92,6 +92,24 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nRegisterMouseLis MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); [window_info->window setAcceptsMouseMovedEvents:YES]; window_info->jmouse = (*env)->NewGlobalRef(env, _this); + + // since initial mouse is reported until mouse is moved, manually + // get the mouse location and report it with a fake event + NSPoint mouseLocation = [window_info->window mouseLocationOutsideOfEventStream]; + mouseLocation = [window_info->view convertPoint:mouseLocation fromView:nil]; + + NSEvent *mouseLocationEvent = [NSEvent + mouseEventWithType:NSMouseMoved + location:mouseLocation + modifierFlags:NSMouseMovedMask + timestamp:0 + windowNumber:[window_info->window windowNumber] + context:nil + eventNumber:0 + clickCount:0 + pressure:0]; + + [window_info->view mouseMoved:mouseLocationEvent]; } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nUnregisterMouseListener(JNIEnv *env, jobject this, jobject window_handle) { From 93a169b27b9d0108c5f3a515c2965c26e5e90f66 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 5 Feb 2013 21:28:08 +0000 Subject: [PATCH 089/111] Catch Command+Q shortcut and get it to call Display.isCloseRequested() --- src/native/macosx/org_lwjgl_opengl_Display.m | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index c10b6fe2..e20e1057 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -178,6 +178,19 @@ static NSAutoreleasePool *pool; (*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, [event keyCode], charcode, time); } +- (BOOL)performKeyEquivalent:(NSEvent *)event { + // if command key down + if (([event modifierFlags] & NSDeviceIndependentModifierFlagsMask) == NSCommandKeyMask) { + // and if q key down + if ([[event charactersIgnoringModifiers] isEqualToString:@"q"]) { + [self windowShouldClose:nil]; + return YES; + } + } + + return NO; +} + - (void)flagsChanged:(NSEvent *)event { JNIEnv *env = attachCurrentThread(); if (env == nil || event == nil || _parent == nil || _parent->jkeyboard == nil) { @@ -529,6 +542,11 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE CGLDisable(cgcontext, kCGLCESurfaceBackingSize); } else { + // Cache the necessary info for window-close callbacks into the JVM + if (window_info->jdisplay == NULL) { + window_info->jdisplay = (*env)->NewGlobalRef(env, this); + } + // set a fixed backbuffer size for fullscreen CGLContextObj cgcontext = (CGLContextObj)[window_info->context CGLContextObj]; GLint dim[2] = {width, height}; From 409b3fea6d45ae6833c0526a70ec5cf61bd6efba Mon Sep 17 00:00:00 2001 From: kappaOne Date: Thu, 7 Feb 2013 21:55:53 +0000 Subject: [PATCH 090/111] Correctly catch quit events instead of manually catching OS X hotkeys --- src/native/macosx/org_lwjgl_opengl_Display.m | 21 ++++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index e20e1057..81c9b5fa 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -85,6 +85,11 @@ static NSAutoreleasePool *pool; return NO; } +- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender { + [self windowShouldClose:nil]; + return NSTerminateCancel; +} + - (id)initWithFrame:(NSRect)frameRect pixelFormat:(NSOpenGLPixelFormat*)format { self = [super initWithFrame:frameRect]; if (self != nil) { @@ -178,19 +183,6 @@ static NSAutoreleasePool *pool; (*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, [event keyCode], charcode, time); } -- (BOOL)performKeyEquivalent:(NSEvent *)event { - // if command key down - if (([event modifierFlags] & NSDeviceIndependentModifierFlagsMask) == NSCommandKeyMask) { - // and if q key down - if ([[event charactersIgnoringModifiers] isEqualToString:@"q"]) { - [self windowShouldClose:nil]; - return YES; - } - } - - return NO; -} - - (void)flagsChanged:(NSEvent *)event { JNIEnv *env = attachCurrentThread(); if (env == nil || event == nil || _parent == nil || _parent->jkeyboard == nil) { @@ -490,6 +482,9 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE window_info->view = [[MacOSXOpenGLView alloc] initWithFrame:view_rect pixelFormat:peer_info->pixel_format]; [window_info->view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; + // set nsapp delegate for catching app quit events + [NSApp setDelegate:window_info->view]; + if (window_info->context != nil) { [window_info->view setOpenGLContext:window_info->context]; } From db4eb9c5c81c77e49f6be48c857ca7bfe3b57b34 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Fri, 8 Feb 2013 23:29:15 +0000 Subject: [PATCH 091/111] Replace AWT code for getAvailableDisplayMode and other DisplayModes stuff to use pure native Cococa code. --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 68 ++++++++------------ src/native/macosx/context.h | 2 + src/native/macosx/org_lwjgl_opengl_Display.m | 59 +++++++++++++++++ 3 files changed, 88 insertions(+), 41 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 2f6251f9..3c2ceff9 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -39,8 +39,6 @@ package org.lwjgl.opengl; */ import java.awt.Canvas; -import java.awt.GraphicsDevice; -import java.awt.GraphicsEnvironment; import java.awt.Robot; import java.nio.ByteBuffer; import java.nio.FloatBuffer; @@ -57,10 +55,6 @@ import org.lwjgl.MemoryUtil; import org.lwjgl.LWJGLException; import org.lwjgl.LWJGLUtil; -import com.apple.eawt.Application; -import com.apple.eawt.ApplicationAdapter; -import com.apple.eawt.ApplicationEvent; - import static org.lwjgl.opengl.GL11.*; final class MacOSXDisplay implements DisplayImplementation { @@ -72,7 +66,7 @@ final class MacOSXDisplay implements DisplayImplementation { private Robot robot; private MacOSXMouseEventQueue mouse_queue; private KeyboardEventQueue keyboard_queue; - private java.awt.DisplayMode requested_mode; + private DisplayMode requested_mode; /* Members for native window use */ private MacOSXNativeMouse mouse; @@ -99,6 +93,10 @@ final class MacOSXDisplay implements DisplayImplementation { private native ByteBuffer nCreateWindow(int x, int y, int width, int height, boolean fullscreen, boolean undecorated, boolean resizable, boolean parented, ByteBuffer peer_info_handle, ByteBuffer window_handle) throws LWJGLException; + private native Object nGetCurrentDisplayMode(); + + private native void nGetDisplayModes(Object modesList); + private native boolean nIsMiniaturized(ByteBuffer window_handle); private native boolean nIsFocused(ByteBuffer window_handle); @@ -219,29 +217,25 @@ final class MacOSXDisplay implements DisplayImplementation { public String getVersion() { return null; } - - private static boolean equals(java.awt.DisplayMode awt_mode, DisplayMode mode) { - return awt_mode.getWidth() == mode.getWidth() && awt_mode.getHeight() == mode.getHeight() - && awt_mode.getBitDepth() == mode.getBitsPerPixel() && awt_mode.getRefreshRate() == mode.getFrequency(); + + private static boolean equals(DisplayMode mode1, DisplayMode mode2) { + return mode1.getWidth() == mode2.getWidth() && mode1.getHeight() == mode2.getHeight() + && mode1.getBitsPerPixel() == mode2.getBitsPerPixel() && mode1.getFrequency() == mode2.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; + DisplayMode[] modes = getAvailableDisplayModes(); + + for (DisplayMode available_mode : modes) { + if (equals(available_mode, mode)) { + requested_mode = available_mode; return; } } + throw new LWJGLException(mode + " is not supported"); } - private static GraphicsDevice getDevice() { - GraphicsEnvironment g_env = GraphicsEnvironment.getLocalGraphicsEnvironment(); - GraphicsDevice device = g_env.getDefaultScreenDevice(); - return device; - } - public void resetDisplayMode() { requested_mode = null; restoreGamma(); @@ -249,32 +243,24 @@ final class MacOSXDisplay implements DisplayImplementation { private native void restoreGamma(); - private static DisplayMode createLWJGLDisplayMode(java.awt.DisplayMode awt_mode) { - 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); + public Object createDisplayMode(int width, int height, int bitsPerPixel, int refreshRate) { + return new DisplayMode(width, height, bitsPerPixel, refreshRate); } - + public DisplayMode init() throws LWJGLException { - return createLWJGLDisplayMode(getDevice().getDisplayMode()); + java.awt.Toolkit.getDefaultToolkit(); // force start AWT Application loop + return (DisplayMode) nGetCurrentDisplayMode(); + } + + public void addDisplayMode(Object modesList, int width, int height, int bitsPerPixel, int refreshRate) { + List modes = (List) modesList; + DisplayMode displayMode = new DisplayMode(width, height, bitsPerPixel, refreshRate); + modes.add(displayMode); } public DisplayMode[] getAvailableDisplayModes() throws LWJGLException { - java.awt.DisplayMode[] awt_modes = getDevice().getDisplayModes(); List modes = new ArrayList(); - for ( java.awt.DisplayMode awt_mode : awt_modes ) - if ( awt_mode.getBitDepth() >= 16 ) - modes.add(createLWJGLDisplayMode(awt_mode)); + nGetDisplayModes(modes); // will populate the above list return modes.toArray(new DisplayMode[modes.size()]); } diff --git a/src/native/macosx/context.h b/src/native/macosx/context.h index 881971e5..ec0b7d73 100644 --- a/src/native/macosx/context.h +++ b/src/native/macosx/context.h @@ -83,6 +83,8 @@ typedef struct { } + (NSOpenGLPixelFormat*)defaultPixelFormat; +- (BOOL)windowShouldClose:(id)sender; +- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender; - (id)initWithFrame:(NSRect)frameRect pixelFormat:(NSOpenGLPixelFormat*)format; - (void)setOpenGLContext:(NSOpenGLContext*)context; - (NSOpenGLContext*)openGLContext; diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 81c9b5fa..a8c926d7 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -618,6 +618,65 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nIsNativeMode(JNI } } +JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nGetCurrentDisplayMode(JNIEnv *env, jobject this) { + + jclass displayClass = (*env)->GetObjectClass(env, this); + jmethodID createDisplayModeMethod = (*env)->GetMethodID(env, displayClass, "createDisplayMode", "(IIII)Ljava/lang/Object;"); + + CGDisplayModeRef mode = CGDisplayCopyDisplayMode(kCGDirectMainDisplay); + + int width = (int) CGDisplayModeGetWidth(mode); + int height = (int) CGDisplayModeGetHeight(mode); + int refreshRate = (int)CGDisplayModeGetRefreshRate(mode); + int bitsPerPixel; + + // get bitsPerPixel + CFStringRef pixelEncoding = CGDisplayModeCopyPixelEncoding(mode); + + if(CFStringCompare(pixelEncoding, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) { + bitsPerPixel = 16; + } + else { + bitsPerPixel = 32; + } + + jobject displayMode = (*env)->CallObjectMethod(env, this, createDisplayModeMethod, width, height, bitsPerPixel, refreshRate); + + return displayMode; +} + +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nGetDisplayModes(JNIEnv *env, jobject this, jobject modesList) { + CFArrayRef modes = CGDisplayCopyAllDisplayModes(kCGDirectMainDisplay, NULL); + + jclass displayClass = (*env)->GetObjectClass(env, this); + jmethodID addDisplayModeMethod = (*env)->GetMethodID(env, displayClass, "addDisplayMode", "(Ljava/lang/Object;IIII)V"); + + int i = 0; + + for (i = 0; i < CFArrayGetCount(modes); i++) { + CGDisplayModeRef mode = (CGDisplayModeRef)CFArrayGetValueAtIndex(modes, i); + + int width = (int) CGDisplayModeGetWidth(mode); + int height = (int) CGDisplayModeGetHeight(mode); + int refreshRate = (int)CGDisplayModeGetRefreshRate(mode); + int bitsPerPixel; + + // get bitsPerPixel + CFStringRef pixelEncoding = CGDisplayModeCopyPixelEncoding(mode); + if(CFStringCompare(pixelEncoding, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) { + bitsPerPixel = 32; + } + else if(CFStringCompare(pixelEncoding, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) { + bitsPerPixel = 16; + } + else { + continue; // ignore DisplayMode of other bitsPerPixel rates + } + + (*env)->CallVoidMethod(env, this, addDisplayModeMethod, modesList, width, height, bitsPerPixel, refreshRate); + } +} + JNIEXPORT jint JNICALL Java_org_lwjgl_DefaultSysImplementation_getJNIVersion(JNIEnv *env, jobject ignored) { return org_lwjgl_MacOSXSysImplementation_JNI_VERSION; } From 4e0c593644313bda07317dfdf735f006d18effff Mon Sep 17 00:00:00 2001 From: kappaOne Date: Fri, 8 Feb 2013 23:55:45 +0000 Subject: [PATCH 092/111] Tweak native cursor behaviour --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 3c2ceff9..27ac3461 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -324,13 +324,7 @@ final class MacOSXDisplay implements DisplayImplementation { if (native_mode && updateNativeCursor) { updateNativeCursor = false; try { - if (mouseInsideWindow) { - setNativeCursor(currentNativeCursor); - } - else { - // restore default cursor if outside Display - MacOSXNativeMouse.setCursor(0); - } + setNativeCursor(currentNativeCursor); } catch (LWJGLException e) { e.printStackTrace(); } @@ -434,7 +428,8 @@ final class MacOSXDisplay implements DisplayImplementation { if (native_mode) { currentNativeCursor = getCursorHandle(handle); if (Display.isCreated()) { - MacOSXNativeMouse.setCursor(currentNativeCursor); + if (mouseInsideWindow) MacOSXNativeMouse.setCursor(currentNativeCursor); + else MacOSXNativeMouse.setCursor(0); // restore default cursor if outside Display } } } From f6c0f17c79c62617104354558150bbda9e0c0c1b Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sat, 9 Feb 2013 00:59:11 +0000 Subject: [PATCH 093/111] Fix freeze/hang when calling AL.create() before Display.create() --- src/java/org/lwjgl/MacOSXSysImplementation.java | 5 +++++ src/java/org/lwjgl/opengl/MacOSXDisplay.java | 1 - 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/java/org/lwjgl/MacOSXSysImplementation.java b/src/java/org/lwjgl/MacOSXSysImplementation.java index 55fa6cef..e49c712c 100644 --- a/src/java/org/lwjgl/MacOSXSysImplementation.java +++ b/src/java/org/lwjgl/MacOSXSysImplementation.java @@ -42,6 +42,11 @@ import com.apple.eio.FileManager; final class MacOSXSysImplementation extends J2SESysImplementation { private static final int JNI_VERSION = 23; + static { + // Manually start the AWT Application Loop + java.awt.Toolkit.getDefaultToolkit(); + } + public int getRequiredJNIVersion() { return JNI_VERSION; } diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 27ac3461..1bc6b12b 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -248,7 +248,6 @@ final class MacOSXDisplay implements DisplayImplementation { } public DisplayMode init() throws LWJGLException { - java.awt.Toolkit.getDefaultToolkit(); // force start AWT Application loop return (DisplayMode) nGetCurrentDisplayMode(); } From 93bfa246c6dc1366449ba5d2303598e9de51600d Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 10 Feb 2013 17:57:52 +0000 Subject: [PATCH 094/111] reduce use of window specific code in the context implementation code --- .../opengl/MacOSXContextImplementation.java | 6 +-- ...lwjgl_opengl_MacOSXContextImplementation.m | 44 ++++++++++--------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXContextImplementation.java b/src/java/org/lwjgl/opengl/MacOSXContextImplementation.java index 5ea1e80f..c47d8c87 100644 --- a/src/java/org/lwjgl/opengl/MacOSXContextImplementation.java +++ b/src/java/org/lwjgl/opengl/MacOSXContextImplementation.java @@ -90,7 +90,7 @@ final class MacOSXContextImplementation implements ContextImplementation { try { synchronized ( context ) { clearDrawable(context.getHandle()); - setView(peer_handle); + setView(peer_handle, context.getHandle()); } } finally { peer_info.unlock(); @@ -100,14 +100,14 @@ final class MacOSXContextImplementation implements ContextImplementation { public void makeCurrent(PeerInfo peer_info, ByteBuffer handle) throws LWJGLException { ByteBuffer peer_handle = peer_info.lockAndGetHandle(); try { - setView(peer_handle); + setView(peer_handle, handle); nMakeCurrent(handle); } finally { peer_info.unlock(); } } - private static native void setView(ByteBuffer peer_handle) throws LWJGLException; + private static native void setView(ByteBuffer peer_handle, ByteBuffer context_handle) throws LWJGLException; private static native void nMakeCurrent(ByteBuffer context_handle) throws LWJGLException; diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m index 6fee4190..20df37c3 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m @@ -89,7 +89,7 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nCre CGLContextObj cgcontext = (CGLContextObj)[context CGLContextObj]; CGLDisable(cgcontext, kCGLCESurfaceBackingSize); } - + [peer_info->window_info->view setOpenGLContext:context]; peer_info->window_info->context = context; context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); @@ -103,8 +103,8 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nCre JNIEXPORT jlong JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_getCGLShareGroup (JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; - CGLContextObj cgl_context = [[peer_info->window_info->view openGLContext] CGLContextObj]; + MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); + CGLContextObj cgl_context = [context_info->context CGLContextObj]; CGLShareGroupObj share_group = CGLGetShareGroup(cgl_context); [pool release]; return (jlong)share_group; @@ -113,12 +113,12 @@ JNIEXPORT jlong JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_getCGL JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nSwapBuffers (JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; - [[peer_info->window_info->view openGLContext] flushBuffer]; + MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); + [context_info->context flushBuffer]; - if (peer_info->isCALayer) { + if (context_info->peer_info->isCALayer) { // blit the contents of buffer to CALayer - [peer_info->glLayer blitFrameBuffer]; + [context_info->peer_info->glLayer blitFrameBuffer]; } [pool release]; @@ -127,16 +127,16 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nSwapBu JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nUpdate (JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; - [[peer_info->window_info->view openGLContext] update]; - [pool release]; + MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); + [context_info->context update]; + [pool release]; } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_clearDrawable (JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; - [[peer_info->window_info->view openGLContext] clearDrawable]; + MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); + [context_info->context clearDrawable]; [pool release]; } @@ -148,32 +148,34 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nReleas } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_setView - (JNIEnv *env, jclass clazz, jobject peer_info_handle) { + //(JNIEnv *env, jclass clazz, jobject peer_info_handle) { + (JNIEnv *env, jclass clazz, jobject peer_info_handle, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); - [[peer_info->window_info->view openGLContext] setView: peer_info->window_info->view]; + [context_info->context setView: peer_info->window_info->view]; if (peer_info->isCALayer) { // if using a CALayer, attach it to AWT Canvas and create a shared opengl context with current context [peer_info->glLayer performSelectorOnMainThread:@selector(attachLayer) withObject:nil waitUntilDone:NO]; } - + [pool release]; } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nMakeCurrent (JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; - [[peer_info->window_info->view openGLContext] makeCurrentContext]; + MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); + [context_info->context makeCurrentContext]; [pool release]; } JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nIsCurrent (JNIEnv *env, jclass clazz, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; - bool result = [peer_info->window_info->view openGLContext] == [NSOpenGLContext currentContext]; + MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); + bool result = context_info->context == [NSOpenGLContext currentContext]; [pool release]; return result ? JNI_TRUE : JNI_FALSE; } @@ -181,9 +183,9 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nIs JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nSetSwapInterval (JNIEnv *env, jclass clazz, jobject context_handle, jint int_value) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info; + MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); GLint value = int_value; - [[peer_info->window_info->view openGLContext] setValues:&value forParameter:NSOpenGLCPSwapInterval]; + [context_info->context setValues:&value forParameter:NSOpenGLCPSwapInterval]; [pool release]; } From 9929d0fac9cd9afe7953c1230603b8ae4e32bf80 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 10 Feb 2013 20:32:07 +0000 Subject: [PATCH 095/111] release mouse grab when destroying the Mouse or Display --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 1bc6b12b..df620375 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -363,6 +363,9 @@ final class MacOSXDisplay implements DisplayImplementation { MacOSXNativeMouse.setCursor(0); } catch (LWJGLException e) {}; + // release any mouse grab + grabMouse(false); + if (mouse != null) { mouse.unregister(); } From e0a52b20c3a2fa71bcf018f34bac65be1f15fb53 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 10 Feb 2013 21:33:28 +0000 Subject: [PATCH 096/111] fix and reimplement pbuffer support --- ...lwjgl_opengl_MacOSXContextImplementation.m | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m index 20df37c3..ec5e2fe3 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m @@ -76,22 +76,25 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nCre return NULL; } - if (peer_info->window_info->fullscreen) { - // set a fixed backbuffer size for fullscreen - CGLContextObj cgcontext = (CGLContextObj)[context CGLContextObj]; - NSSize displaySize = peer_info->window_info->display_rect.size; - GLint dim[2] = {displaySize.width, displaySize.height}; - CGLSetParameter(cgcontext, kCGLCPSurfaceBackingSize, dim); - CGLEnable(cgcontext, kCGLCESurfaceBackingSize); - } - else { - // disable any fixed backbuffer size to allow resizing - CGLContextObj cgcontext = (CGLContextObj)[context CGLContextObj]; - CGLDisable(cgcontext, kCGLCESurfaceBackingSize); + if (peer_info->isWindowed) { + if (peer_info->window_info->fullscreen) { + // set a fixed backbuffer size for fullscreen + CGLContextObj cgcontext = (CGLContextObj)[context CGLContextObj]; + NSSize displaySize = peer_info->window_info->display_rect.size; + GLint dim[2] = {displaySize.width, displaySize.height}; + CGLSetParameter(cgcontext, kCGLCPSurfaceBackingSize, dim); + CGLEnable(cgcontext, kCGLCESurfaceBackingSize); + } + else { + // disable any fixed backbuffer size to allow resizing + CGLContextObj cgcontext = (CGLContextObj)[context CGLContextObj]; + CGLDisable(cgcontext, kCGLCESurfaceBackingSize); + } + + [peer_info->window_info->view setOpenGLContext:context]; + peer_info->window_info->context = context; } - [peer_info->window_info->view setOpenGLContext:context]; - peer_info->window_info->context = context; context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); context_info->context = context; context_info->peer_info = peer_info; @@ -153,7 +156,13 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_setView NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); - [context_info->context setView: peer_info->window_info->view]; + + if (peer_info->isWindowed) { + [context_info->context setView: peer_info->window_info->view]; + } + else { + [context_info->context setPixelBuffer:peer_info->pbuffer cubeMapFace:0 mipMapLevel:0 currentVirtualScreen:0]; + } if (peer_info->isCALayer) { // if using a CALayer, attach it to AWT Canvas and create a shared opengl context with current context @@ -200,11 +209,13 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nDestro [context_info->peer_info->glLayer performSelectorOnMainThread:@selector(removeLayer) withObject:nil waitUntilDone:YES]; [context_info->peer_info->glLayer release]; } - + // clearDrawable on main thread to ensure its not in use [context_info->context performSelectorOnMainThread:@selector(clearDrawable) withObject:nil waitUntilDone:YES]; - [context_info->peer_info->window_info->view setOpenGLContext:nil]; - [context_info->context release]; + + if (context_info->peer_info->isWindowed) { + [context_info->context release]; + } [pool release]; } From e28f8554f9db9f4d3381d377296c8404249f87a7 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 12 Feb 2013 21:40:48 +0000 Subject: [PATCH 097/111] tweak to pbuffer support --- src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m | 1 + 1 file changed, 1 insertion(+) diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m index ec5e2fe3..dcdde4eb 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m @@ -215,6 +215,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nDestro if (context_info->peer_info->isWindowed) { [context_info->context release]; + context_info->context = nil; } [pool release]; From e725ca7d3438e59082ad6814b3c446f48b1f8ca0 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 12 Feb 2013 21:41:47 +0000 Subject: [PATCH 098/111] further tweak to pbuffer support --- src/native/macosx/org_lwjgl_opengl_Display.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index a8c926d7..ea5fd2d3 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -575,6 +575,8 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE peer_info->window_info = window_info; + peer_info->isWindowed = true; + return window_handle; } From ed9ab9541910342376db1406357459f4b12b79d2 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 12 Feb 2013 21:53:49 +0000 Subject: [PATCH 099/111] fix crash on startup when creating a second window Display after a destroy in the same app --- src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m | 1 + 1 file changed, 1 insertion(+) diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m index dcdde4eb..0145f665 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m @@ -216,6 +216,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nDestro if (context_info->peer_info->isWindowed) { [context_info->context release]; context_info->context = nil; + context_info->peer_info->window_info->context = nil; } [pool release]; From 817cada066451cc9e73a94af1a18ab512a2ce2a1 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 12 Feb 2013 22:00:11 +0000 Subject: [PATCH 100/111] move nswindow close to main thread 0 to fix a potential crash on exit --- src/native/macosx/org_lwjgl_opengl_Display.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index ea5fd2d3..408bd96a 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -590,7 +590,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv if (window_info->window != nil) { // if the nsview has no parent then close window if ([window_info->window contentView] == window_info->view) { - [window_info->window close]; + [window_info->window performSelectorOnMainThread:@selector(close) withObject:nil waitUntilDone:YES]; } // release the nsview and remove it from any parent nsview using main thread From 1b48b704bc577e0776fcee1639488a586b68ff0c Mon Sep 17 00:00:00 2001 From: kappaOne Date: Fri, 15 Feb 2013 21:35:12 +0000 Subject: [PATCH 101/111] Move entire Display nCreate and nDestroy onto the Main Thread 0 --- src/native/macosx/context.h | 17 +- src/native/macosx/org_lwjgl_opengl_Display.m | 238 +++++++++--------- .../org_lwjgl_opengl_MacOSXNativeMouse.m | 4 +- 3 files changed, 140 insertions(+), 119 deletions(-) diff --git a/src/native/macosx/context.h b/src/native/macosx/context.h index ec0b7d73..2a3233a9 100644 --- a/src/native/macosx/context.h +++ b/src/native/macosx/context.h @@ -62,15 +62,16 @@ typedef struct { jobject jdisplay; jobject jmouse; jobject jkeyboard; + jboolean fullscreen; + jboolean undecorated; + jboolean resizable; + jboolean parented; + jboolean resized; + } MacOSXWindowInfo; -@interface MacOSXKeyableWindow : NSWindow - -- (BOOL)canBecomeKeyWindow; -@end - @interface MacOSXOpenGLView : NSView { @public @@ -128,5 +129,11 @@ typedef struct { GLLayer *glLayer; } MacOSXPeerInfo; +@interface MacOSXKeyableWindow : NSWindow ++ (void)createWindow; ++ (void)destroyWindow; +- (BOOL)canBecomeKeyWindow; +@end + NSOpenGLPixelFormat *choosePixelFormat(JNIEnv *env, jobject pixel_format, bool gl32, bool use_display_bpp, bool support_window, bool support_pbuffer, bool double_buffered); #endif diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 408bd96a..508ceaad 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -53,7 +53,115 @@ static NSOpenGLPixelFormat *default_format = nil; static NSAutoreleasePool *pool; +static MacOSXPeerInfo *peer_info; + @implementation MacOSXKeyableWindow + ++ (void) createWindow { + MacOSXWindowInfo *window_info = peer_info->window_info; + + int width = window_info->display_rect.size.width; + int height = window_info->display_rect.size.height; + + NSRect view_rect = NSMakeRect(0.0, 0.0, width, height); + window_info->view = [[MacOSXOpenGLView alloc] initWithFrame:view_rect pixelFormat:peer_info->pixel_format]; + [window_info->view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; + + // set nsapp delegate for catching app quit events + [NSApp setDelegate:window_info->view]; + + if (window_info->context != nil) { + [window_info->view setOpenGLContext:window_info->context]; + } + + if (!window_info->fullscreen) { + + if (window_info->parented) { + if (peer_info->isCALayer) { + window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO]; + [window_info->window setContentView:window_info->view]; + } + else { + window_info->window = [peer_info->parent window]; + [peer_info->parent addSubview:window_info->view]; + } + } + else { + + int default_window_mask = NSBorderlessWindowMask; // undecorated + + if (!window_info->undecorated) { + default_window_mask = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask; + } + + if (window_info->resizable) { + default_window_mask |= NSResizableWindowMask; + } + + window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:default_window_mask backing:NSBackingStoreBuffered defer:NO]; + + [window_info->window setContentView:window_info->view]; + [window_info->window setContentView:window_info->view]; // call twice to fix issue + + // set NSView as delegate of NSWindow to get windowShouldClose events + [window_info->window setDelegate:window_info->view]; + } + + // disable any fixed backbuffer size to allow resizing + CGLContextObj cgcontext = (CGLContextObj)[[window_info->view openGLContext] CGLContextObj]; + CGLDisable(cgcontext, kCGLCESurfaceBackingSize); + } + else { + // set a fixed backbuffer size for fullscreen + CGLContextObj cgcontext = (CGLContextObj)[window_info->context CGLContextObj]; + GLint dim[2] = {width, height}; + CGLSetParameter(cgcontext, kCGLCPSurfaceBackingSize, dim); + CGLEnable(cgcontext, kCGLCESurfaceBackingSize); + + // enter fullscreen mode + [window_info->view enterFullScreenMode: [NSScreen mainScreen] withOptions: nil ]; + window_info->window = [window_info->view window]; + + // adjust the NSView bounds to correct mouse coordinates in fullscreen + NSSize windowSize = [window_info->window frame].size; + NSSize newBounds = NSMakeSize(windowSize.width/width*windowSize.width, windowSize.height/height*windowSize.height); + [window_info->view setBoundsSize:newBounds]; + } + + // Inform the view of its parent window info; + [window_info->view setParent:window_info]; + + if (!window_info->fullscreen && peer_info->isCALayer) { + // hidden window when using CALayer + [window_info->window orderOut:nil]; + } + else { + [window_info->window makeFirstResponder:window_info->view]; + [window_info->window setInitialFirstResponder:window_info->view]; + [window_info->window makeKeyAndOrderFront:[NSApplication sharedApplication]]; + } +} + ++ (void) destroyWindow { + + MacOSXWindowInfo *window_info = peer_info->window_info; + + if (window_info->fullscreen) { + [window_info->view exitFullScreenModeWithOptions: nil]; + } + else { + if (window_info->window != nil) { + // if the nsview has no parent then close window + if ([window_info->window contentView] == window_info->view) { + [window_info->window close]; + } + + // release the nsview and remove it from any parent nsview + [window_info->view removeFromSuperviewWithoutNeedingDisplay]; + } + } +} + - (BOOL)canBecomeKeyWindow; { return YES; @@ -473,130 +581,36 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE } } - pool = [[NSAutoreleasePool alloc] init]; - + peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); - - NSRect view_rect = NSMakeRect(0.0, 0.0, width, height); - window_info->view = [[MacOSXOpenGLView alloc] initWithFrame:view_rect pixelFormat:peer_info->pixel_format]; - [window_info->view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; - - // set nsapp delegate for catching app quit events - [NSApp setDelegate:window_info->view]; - - if (window_info->context != nil) { - [window_info->view setOpenGLContext:window_info->context]; - } - - window_info->display_rect = NSMakeRect(x, y, width, height); - - if (!fullscreen) { - - if (parented) { - if (peer_info->isCALayer) { - window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO]; - [window_info->window setContentView:window_info->view]; - } - else { - window_info->window = [peer_info->parent window]; - [peer_info->parent addSubview:window_info->view]; - - if (window_info->jdisplay == NULL) { - window_info->jdisplay = (*env)->NewGlobalRef(env, this); - } - } - } - else { - - int default_window_mask = NSBorderlessWindowMask; // undecorated - - if (!undecorated) { - default_window_mask = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask; - } - - if (resizable) { - default_window_mask |= NSResizableWindowMask; - } - - window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:default_window_mask backing:NSBackingStoreBuffered defer:NO]; - - [window_info->window setContentView:window_info->view]; - - // Cache the necessary info for window-close callbacks into the JVM - if (window_info->jdisplay == NULL) { - window_info->jdisplay = (*env)->NewGlobalRef(env, this); - } - - // set NSView as delegate of NSWindow to get windowShouldClose events - [window_info->window setDelegate:window_info->view]; - } - - // disable any fixed backbuffer size to allow resizing - CGLContextObj cgcontext = (CGLContextObj)[[window_info->view openGLContext] CGLContextObj]; - CGLDisable(cgcontext, kCGLCESurfaceBackingSize); - } - else { - // Cache the necessary info for window-close callbacks into the JVM - if (window_info->jdisplay == NULL) { - window_info->jdisplay = (*env)->NewGlobalRef(env, this); - } - - // set a fixed backbuffer size for fullscreen - CGLContextObj cgcontext = (CGLContextObj)[window_info->context CGLContextObj]; - GLint dim[2] = {width, height}; - CGLSetParameter(cgcontext, kCGLCPSurfaceBackingSize, dim); - CGLEnable(cgcontext, kCGLCESurfaceBackingSize); - - // enter fullscreen mode - [window_info->view enterFullScreenMode: [NSScreen mainScreen] withOptions: nil ]; - window_info->window = [window_info->view window]; - - // adjust the NSView bounds to correct mouse coordinates in fullscreen - NSSize windowSize = [window_info->window frame].size; - NSSize newBounds = NSMakeSize(windowSize.width/width*windowSize.width, windowSize.height/height*windowSize.height); - [window_info->view setBoundsSize:newBounds]; - } - - // Inform the view of its parent window info; - [window_info->view setParent:window_info]; - - if (!fullscreen && peer_info->isCALayer) { - // hidden window when using CALayer - [window_info->window orderOut:nil]; - } - else { - [window_info->window performSelectorOnMainThread:@selector(makeFirstResponder:) withObject:window_info->view waitUntilDone:NO]; - [window_info->window performSelectorOnMainThread:@selector(setInitialFirstResponder:) withObject:window_info->view waitUntilDone:NO]; - [window_info->window performSelectorOnMainThread:@selector(makeKeyAndOrderFront:) withObject:[NSApplication sharedApplication] waitUntilDone:NO]; - } window_info->fullscreen = fullscreen; + window_info->undecorated = undecorated; + window_info->resizable = resizable; + window_info->parented = parented; peer_info->window_info = window_info; - peer_info->isWindowed = true; + window_info->display_rect = NSMakeRect(x, y, width, height); + + // Cache the necessary info for window-close callbacks into the JVM + if (!peer_info->isCALayer && window_info->jdisplay == NULL) { + window_info->jdisplay = (*env)->NewGlobalRef(env, this); + } + + pool = [[NSAutoreleasePool alloc] init]; + + // create window on main thread + [MacOSXKeyableWindow performSelectorOnMainThread:@selector(createWindow) withObject:nil waitUntilDone:YES]; + return window_handle; } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv *env, jobject this, jobject window_handle) { - MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); - if (window_info->fullscreen) { - [window_info->view exitFullScreenModeWithOptions: nil]; - } - else { - if (window_info->window != nil) { - // if the nsview has no parent then close window - if ([window_info->window contentView] == window_info->view) { - [window_info->window performSelectorOnMainThread:@selector(close) withObject:nil waitUntilDone:YES]; - } - - // release the nsview and remove it from any parent nsview using main thread - [window_info->view performSelectorOnMainThread:@selector(removeFromSuperviewWithoutNeedingDisplay) withObject:window_info->view waitUntilDone:YES]; - } - } + // destroy window on main thread + [MacOSXKeyableWindow performSelectorOnMainThread:@selector(destroyWindow) withObject:nil waitUntilDone:YES]; [pool drain]; } diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m index 208496d0..3139ecf5 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m @@ -157,10 +157,10 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nDestroyCursor(JN JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nSetCursor(JNIEnv *env, jobject _this, jlong cursor_pointer) { if (cursor_pointer == 0) { // restore default cursor - [[NSCursor arrowCursor] set]; + [[NSCursor arrowCursor] performSelectorOnMainThread:@selector(set) withObject:nil waitUntilDone:NO]; } else { NSCursor *cursor = (NSCursor *)cursor_pointer; - [cursor set]; + [cursor performSelectorOnMainThread:@selector(set) withObject:nil waitUntilDone:NO]; } } From 10b8d633d0a75f6a035d4d2fc39645d95f0c1a12 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Fri, 15 Feb 2013 21:40:33 +0000 Subject: [PATCH 102/111] push --- src/native/macosx/org_lwjgl_opengl_Display.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 508ceaad..8b5016af 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -59,7 +59,7 @@ static MacOSXPeerInfo *peer_info; + (void) createWindow { MacOSXWindowInfo *window_info = peer_info->window_info; - + // int width = window_info->display_rect.size.width; int height = window_info->display_rect.size.height; From e67d717c4ed4470901dba7430d5b12870b874e05 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Fri, 15 Feb 2013 21:49:19 +0000 Subject: [PATCH 103/111] simplify mouse code a little --- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 12 ++---------- src/native/macosx/org_lwjgl_opengl_Display.m | 10 +++++----- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index df620375..2cc0005b 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -170,20 +170,12 @@ final class MacOSXDisplay implements DisplayImplementation { } } - public void mouseInsideWindow() { + public void mouseInsideWindow(boolean inside) { synchronized (this) { - mouseInsideWindow = true; + mouseInsideWindow = inside; } updateNativeCursor = true; } - - public void mouseOutsideWindow() { - synchronized (this) { - mouseInsideWindow = false; - } - - updateNativeCursor = true; - } public native void nDestroyCALayer(ByteBuffer peer_info_handle); diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 8b5016af..7a3e5c7b 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -59,7 +59,7 @@ static MacOSXPeerInfo *peer_info; + (void) createWindow { MacOSXWindowInfo *window_info = peer_info->window_info; - // + int width = window_info->display_rect.size.width; int height = window_info->display_rect.size.height; @@ -476,8 +476,8 @@ static MacOSXPeerInfo *peer_info; } jclass display_class = (*env)->GetObjectClass(env, _parent->jdisplay); - jmethodID mouseInsideWindow_callback = (*env)->GetMethodID(env, display_class, "mouseInsideWindow", "()V"); - (*env)->CallVoidMethod(env, _parent->jdisplay, mouseInsideWindow_callback); + jmethodID mouseInsideWindow_callback = (*env)->GetMethodID(env, display_class, "mouseInsideWindow", "(Z)V"); + (*env)->CallVoidMethod(env, _parent->jdisplay, mouseInsideWindow_callback, JNI_TRUE); } -(void)mouseExited:(NSEvent *)event { @@ -487,8 +487,8 @@ static MacOSXPeerInfo *peer_info; } jclass display_class = (*env)->GetObjectClass(env, _parent->jdisplay); - jmethodID mouseOutsideWindow_callback = (*env)->GetMethodID(env, display_class, "mouseOutsideWindow", "()V"); - (*env)->CallVoidMethod(env, _parent->jdisplay, mouseOutsideWindow_callback); + jmethodID mouseInsideWindow_callback = (*env)->GetMethodID(env, display_class, "mouseInsideWindow", "(Z)V"); + (*env)->CallVoidMethod(env, _parent->jdisplay, mouseInsideWindow_callback, JNI_FALSE); } - (void) drawRect:(NSRect)rect { From d28381e1cdd4887ce16d11c2fb8689ff31221d45 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sat, 16 Feb 2013 00:31:10 +0000 Subject: [PATCH 104/111] minor tweak to comments --- src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m index 3139ecf5..1d625ee1 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXNativeMouse.m @@ -49,10 +49,12 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nGrabMouse(JNIEnv *env, jclass this, jboolean grab) { CGAssociateMouseAndMouseCursorPosition(grab == JNI_TRUE ? FALSE : TRUE); - if (grab) + if (grab) { CGDisplayHideCursor(kCGDirectMainDisplay); - else + } + else { CGDisplayShowCursor(kCGDirectMainDisplay); + } } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nSetCursorPosition(JNIEnv *env, jclass this, jobject window_handle, jint x, jint y) { @@ -93,8 +95,8 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nRegisterMouseLis [window_info->window setAcceptsMouseMovedEvents:YES]; window_info->jmouse = (*env)->NewGlobalRef(env, _this); - // since initial mouse is reported until mouse is moved, manually - // get the mouse location and report it with a fake event + // since initial mouse location is not reported until mouse is moved + // manually get the mouse location and report it with a fake event NSPoint mouseLocation = [window_info->window mouseLocationOutsideOfEventStream]; mouseLocation = [window_info->view convertPoint:mouseLocation fromView:nil]; From 6663ee037c6ba3f2d459bf2b19b12dc2edaed296 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sat, 16 Feb 2013 14:07:16 +0000 Subject: [PATCH 105/111] Attempt fix for no JAWT on OS X Java 7 issue --- src/native/common/org_lwjgl_opengl_AWTSurfaceLock.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/native/common/org_lwjgl_opengl_AWTSurfaceLock.c b/src/native/common/org_lwjgl_opengl_AWTSurfaceLock.c index d4e5f1c2..c5ded2e5 100644 --- a/src/native/common/org_lwjgl_opengl_AWTSurfaceLock.c +++ b/src/native/common/org_lwjgl_opengl_AWTSurfaceLock.c @@ -58,8 +58,12 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_AWTSurfaceLock_lockAndInitHandl jboolean result = JNI_FALSE; #ifdef __MACH__ - if (allowCALayer) { - //first try CALAYER + // try to get JAWT for Java 7+ + awt.version = 0x00010007;//JAWT_VERSION_1_7 + result = JAWT_GetAWT(env, &awt); + + if (result == JNI_FALSE && allowCALayer) { + // try with JAWT 1_4 WITH CALAYER Opt In awt.version = JAWT_VERSION_1_4 | 0x80000000;//JAWT_MACOSX_USE_CALAYER; result = JAWT_GetAWT(env, &awt); } From af1ae935d8dde11cd0e887715d381c9ff9aecc72 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 17 Feb 2013 15:09:31 +0000 Subject: [PATCH 106/111] fix CALayer/Display.setParent driver crash on resize --- src/native/macosx/org_lwjgl_opengl_Display.m | 2 +- src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 7a3e5c7b..3a526128 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -78,7 +78,7 @@ static MacOSXPeerInfo *peer_info; if (window_info->parented) { if (peer_info->isCALayer) { - window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO]; + window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame] styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO]; [window_info->window setContentView:window_info->view]; } else { diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m index 7e43de43..3056c1a5 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m @@ -159,7 +159,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle // set the size of the offscreen frame buffer window window_info->display_rect = NSMakeRect(0, 0, width, height); - [window_info->window setFrame:window_info->display_rect display:false]; + //[window_info->window setFrame:window_info->display_rect display:false]; // clean up the old fbo and renderBuffers glDeleteFramebuffersEXT(1, &oldFboID); From f381eb1f3536f3104693d28d3c71eb1f0d08fff1 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Sun, 17 Feb 2013 16:50:56 +0000 Subject: [PATCH 107/111] Simplify code in AWTSurfaceLock for CALayer support --- src/java/org/lwjgl/opengl/AWTSurfaceLock.java | 9 ++---- .../common/org_lwjgl_opengl_AWTSurfaceLock.c | 32 +++++++++---------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/java/org/lwjgl/opengl/AWTSurfaceLock.java b/src/java/org/lwjgl/opengl/AWTSurfaceLock.java index da1f6d56..b0935dbc 100644 --- a/src/java/org/lwjgl/opengl/AWTSurfaceLock.java +++ b/src/java/org/lwjgl/opengl/AWTSurfaceLock.java @@ -82,16 +82,13 @@ final class AWTSurfaceLock { // It is only needed on first call, so we avoid it on all subsequent calls // due to performance.. - // Allow the use of a Core Animation Layer only when using non fullscreen Display.setParent() or AWTGLCanvas and OS X 10.6+ - final boolean allowCALayer = ((Display.getParent() != null && !Display.isFullscreen()) || component instanceof AWTGLCanvas) && LWJGLUtil.isMacOSXEqualsOrBetterThan(10, 6); - if (firstLockSucceeded) - return lockAndInitHandle(lock_buffer, component, allowCALayer); + return lockAndInitHandle(lock_buffer, component); else try { firstLockSucceeded = AccessController.doPrivileged(new PrivilegedExceptionAction() { public Boolean run() throws LWJGLException { - return lockAndInitHandle(lock_buffer, component, allowCALayer); + return lockAndInitHandle(lock_buffer, component); } }); return firstLockSucceeded; @@ -100,7 +97,7 @@ final class AWTSurfaceLock { } } - private static native boolean lockAndInitHandle(ByteBuffer lock_buffer, Canvas component, boolean allowCALayer) throws LWJGLException; + private static native boolean lockAndInitHandle(ByteBuffer lock_buffer, Canvas component) throws LWJGLException; void unlock() throws LWJGLException { nUnlock(lock_buffer); diff --git a/src/native/common/org_lwjgl_opengl_AWTSurfaceLock.c b/src/native/common/org_lwjgl_opengl_AWTSurfaceLock.c index c5ded2e5..b31e03d1 100644 --- a/src/native/common/org_lwjgl_opengl_AWTSurfaceLock.c +++ b/src/native/common/org_lwjgl_opengl_AWTSurfaceLock.c @@ -38,7 +38,11 @@ */ #include +#ifdef __MACH__ +#include +#else #include +#endif #include "org_lwjgl_opengl_AWTSurfaceLock.h" #include "awt_tools.h" #include "common_tools.h" @@ -49,33 +53,27 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_AWTSurfaceLock_createHandle } JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_AWTSurfaceLock_lockAndInitHandle - (JNIEnv *env, jclass clazz, jobject lock_buffer_handle, jobject canvas, jboolean allowCALayer) { + (JNIEnv *env, jclass clazz, jobject lock_buffer_handle, jobject canvas) { JAWT awt; JAWT_DrawingSurface* ds; JAWT_DrawingSurfaceInfo *dsi; AWTSurfaceLock *awt_lock = (AWTSurfaceLock *)(*env)->GetDirectBufferAddress(env, lock_buffer_handle); - jboolean result = JNI_FALSE; + jboolean result = JNI_FALSE; #ifdef __MACH__ - // try to get JAWT for Java 7+ - awt.version = 0x00010007;//JAWT_VERSION_1_7 + // try get JAWT with JAWT_MACOSX_USE_CALAYER Opt In + awt.version = JAWT_VERSION_1_4 | 0x80000000;//JAWT_MACOSX_USE_CALAYER; result = JAWT_GetAWT(env, &awt); - - if (result == JNI_FALSE && allowCALayer) { - // try with JAWT 1_4 WITH CALAYER Opt In - awt.version = JAWT_VERSION_1_4 | 0x80000000;//JAWT_MACOSX_USE_CALAYER; - result = JAWT_GetAWT(env, &awt); - } #endif - + if (result == JNI_FALSE) { - // now try without CALAYER - awt.version = JAWT_VERSION_1_4; - if (JAWT_GetAWT(env, &awt) == JNI_FALSE) { - throwException(env, "Could not get the JAWT interface"); - return JNI_FALSE; - } + // now try without CALAYER + awt.version = JAWT_VERSION_1_4; + if (JAWT_GetAWT(env, &awt) == JNI_FALSE) { + throwException(env, "Could not get the JAWT interface"); + return JNI_FALSE; + } } ds = awt.GetDrawingSurface(env, canvas); From 6cd860525fa7390784ecfdd3085258c5cedd1939 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Tue, 19 Feb 2013 22:28:14 +0000 Subject: [PATCH 108/111] fix potential crash on exit when closing the Display window --- .../macosx/org_lwjgl_opengl_MacOSXContextImplementation.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m index 0145f665..875de517 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m @@ -214,6 +214,10 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nDestro [context_info->context performSelectorOnMainThread:@selector(clearDrawable) withObject:nil waitUntilDone:YES]; if (context_info->peer_info->isWindowed) { + if (context_info->peer_info->window_info->view != nil) { + [context_info->peer_info->window_info->view setOpenGLContext:nil]; + } + [context_info->context release]; context_info->context = nil; context_info->peer_info->window_info->context = nil; From 0cbe94d9e3896f1921342d9b3a95136a3c76282f Mon Sep 17 00:00:00 2001 From: kappaOne Date: Wed, 20 Feb 2013 22:20:23 +0000 Subject: [PATCH 109/111] Tweak to fix a crash on exit, attempt to fix missing jawt error on Java 7 --- src/java/org/lwjgl/MacOSXSysImplementation.java | 17 +++++++++++++++++ src/native/macosx/org_lwjgl_opengl_Display.m | 9 ++++++--- ...g_lwjgl_opengl_MacOSXContextImplementation.m | 5 +---- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/java/org/lwjgl/MacOSXSysImplementation.java b/src/java/org/lwjgl/MacOSXSysImplementation.java index e49c712c..fa1d56da 100644 --- a/src/java/org/lwjgl/MacOSXSysImplementation.java +++ b/src/java/org/lwjgl/MacOSXSysImplementation.java @@ -32,6 +32,10 @@ package org.lwjgl; import com.apple.eio.FileManager; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.security.PrivilegedExceptionAction; +import java.lang.UnsatisfiedLinkError; /** * @@ -45,6 +49,19 @@ final class MacOSXSysImplementation extends J2SESysImplementation { static { // Manually start the AWT Application Loop java.awt.Toolkit.getDefaultToolkit(); + + // manually load libjawt.dylib into vm, needed since Java 7 + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + try { + System.loadLibrary("jawt"); + } catch (UnsatisfiedLinkError e) { + // catch and ignore an already loaded in another classloader + // exception, as vm already has it loaded + } + return null; + } + }); } public int getRequiredJNIVersion() { diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 3a526128..136d97ec 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -153,11 +153,14 @@ static MacOSXPeerInfo *peer_info; if (window_info->window != nil) { // if the nsview has no parent then close window if ([window_info->window contentView] == window_info->view) { + // release the nsview and remove it from any parent nsview + [window_info->view removeFromSuperviewWithoutNeedingDisplay]; [window_info->window close]; } - - // release the nsview and remove it from any parent nsview - [window_info->view removeFromSuperviewWithoutNeedingDisplay]; + else { + // release the nsview and remove it from any parent nsview + [window_info->view removeFromSuperviewWithoutNeedingDisplay]; + } } } } diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m index 875de517..ff2ee12c 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m @@ -214,14 +214,11 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nDestro [context_info->context performSelectorOnMainThread:@selector(clearDrawable) withObject:nil waitUntilDone:YES]; if (context_info->peer_info->isWindowed) { - if (context_info->peer_info->window_info->view != nil) { - [context_info->peer_info->window_info->view setOpenGLContext:nil]; - } - [context_info->context release]; context_info->context = nil; context_info->peer_info->window_info->context = nil; } + else [context_info->context release]; [pool release]; } From d462208c4cfb3fdb5a857b048070e34bfe2303f3 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Wed, 13 Mar 2013 00:51:32 +0000 Subject: [PATCH 110/111] Implement AWTGLCanvas, fix initial glViewport size when using Display.setParent --- .../lwjgl/opengl/MacOSXCanvasPeerInfo.java | 5 +- src/java/org/lwjgl/opengl/MacOSXDisplay.java | 8 ++- src/native/macosx/context.h | 3 + src/native/macosx/org_lwjgl_opengl_Display.m | 49 +++++++------- .../org_lwjgl_opengl_MacOSXCanvasPeerInfo.m | 65 ++++++++++++++----- ...lwjgl_opengl_MacOSXContextImplementation.m | 14 ++-- 6 files changed, 95 insertions(+), 49 deletions(-) diff --git a/src/java/org/lwjgl/opengl/MacOSXCanvasPeerInfo.java b/src/java/org/lwjgl/opengl/MacOSXCanvasPeerInfo.java index 76d59954..d43e1d0d 100644 --- a/src/java/org/lwjgl/opengl/MacOSXCanvasPeerInfo.java +++ b/src/java/org/lwjgl/opengl/MacOSXCanvasPeerInfo.java @@ -45,6 +45,7 @@ import org.lwjgl.LWJGLUtil; */ abstract class MacOSXCanvasPeerInfo extends MacOSXPeerInfo { private final AWTSurfaceLock awt_surface = new AWTSurfaceLock(); + public ByteBuffer window_handle; protected MacOSXCanvasPeerInfo(PixelFormat pixel_format, ContextAttribs attribs, boolean support_pbuffer) throws LWJGLException { super(pixel_format, attribs, true, true, support_pbuffer, true); @@ -61,9 +62,9 @@ abstract class MacOSXCanvasPeerInfo extends MacOSXPeerInfo { forceCALayer = false; } - nInitHandle(awt_surface.lockAndGetHandle(component), getHandle(), forceCALayer); + window_handle = nInitHandle(awt_surface.lockAndGetHandle(component), getHandle(), window_handle, forceCALayer); } - private static native void nInitHandle(ByteBuffer surface_buffer, ByteBuffer peer_info_handle, boolean forceCALayer) throws LWJGLException; + private static native ByteBuffer nInitHandle(ByteBuffer surface_buffer, ByteBuffer peer_info_handle, ByteBuffer window_handle, boolean forceCALayer) throws LWJGLException; protected void doUnlock() throws LWJGLException { awt_surface.unlock(); diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 2cc0005b..b392cffd 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -134,11 +134,13 @@ final class MacOSXDisplay implements DisplayImplementation { DrawableGL gl_drawable = (DrawableGL)Display.getDrawable(); PeerInfo peer_info = gl_drawable.peer_info; ByteBuffer peer_handle = peer_info.lockAndGetHandle(); + ByteBuffer window_handle = parented ? ((MacOSXCanvasPeerInfo)peer_info).window_handle : window; + try { + window = nCreateWindow(x, y, mode.getWidth(), mode.getHeight(), - fullscreen, isUndecorated(), resizable, - parented, peer_handle, window); - + fullscreen, isUndecorated(), resizable, + parented, peer_handle, window_handle); if (fullscreen) { // when going to fullscreen viewport is set to screen size by Cocoa, ignore this value diff --git a/src/native/macosx/context.h b/src/native/macosx/context.h index 2a3233a9..40151e8d 100644 --- a/src/native/macosx/context.h +++ b/src/native/macosx/context.h @@ -103,6 +103,7 @@ typedef struct { @public JAWT_MacOSXDrawingSurfaceInfo *macosx_dsi; MacOSXWindowInfo *window_info; + bool setViewport; @private CGLContextObj contextObject; @@ -117,6 +118,8 @@ typedef struct { - (void) removeLayer; - (void) blitFrameBuffer; +- (int) getWidth; +- (int) getHeight; @end typedef struct { diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index 136d97ec..1c31d883 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -77,14 +77,8 @@ static MacOSXPeerInfo *peer_info; if (!window_info->fullscreen) { if (window_info->parented) { - if (peer_info->isCALayer) { - window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame] styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO]; - [window_info->window setContentView:window_info->view]; - } - else { - window_info->window = [peer_info->parent window]; - [peer_info->parent addSubview:window_info->view]; - } + window_info->window = [peer_info->parent window]; + [peer_info->parent addSubview:window_info->view]; } else { @@ -131,31 +125,30 @@ static MacOSXPeerInfo *peer_info; // Inform the view of its parent window info; [window_info->view setParent:window_info]; - if (!window_info->fullscreen && peer_info->isCALayer) { - // hidden window when using CALayer - [window_info->window orderOut:nil]; - } - else { - [window_info->window makeFirstResponder:window_info->view]; - [window_info->window setInitialFirstResponder:window_info->view]; - [window_info->window makeKeyAndOrderFront:[NSApplication sharedApplication]]; - } + [window_info->window makeFirstResponder:window_info->view]; + [window_info->window setInitialFirstResponder:window_info->view]; + [window_info->window makeKeyAndOrderFront:[NSApplication sharedApplication]]; } + (void) destroyWindow { - MacOSXWindowInfo *window_info = peer_info->window_info; if (window_info->fullscreen) { [window_info->view exitFullScreenModeWithOptions: nil]; + window_info->window = nil; } else { + if (peer_info->isCALayer) { + [peer_info->glLayer removeLayer]; + } + if (window_info->window != nil) { // if the nsview has no parent then close window if ([window_info->window contentView] == window_info->view) { // release the nsview and remove it from any parent nsview [window_info->view removeFromSuperviewWithoutNeedingDisplay]; [window_info->window close]; + window_info->window = nil; } else { // release the nsview and remove it from any parent nsview @@ -575,7 +568,20 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nSetTitle(JNIEnv *env } JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIEnv *env, jobject this, jint x, jint y, jint width, jint height, jboolean fullscreen, jboolean undecorated, jboolean resizable, jboolean parented, jobject peer_info_handle, jobject window_handle) { - + + pool = [[NSAutoreleasePool alloc] init]; + + peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); + + if (peer_info->isCALayer && !fullscreen) { + MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); + window_info->fullscreen = fullscreen; + window_info->undecorated = undecorated; + window_info->parented = parented; + + return window_handle; + } + if (window_handle == NULL) { window_handle = newJavaManagedByteBuffer(env, sizeof(MacOSXWindowInfo)); if (window_handle == NULL) { @@ -584,7 +590,6 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE } } - peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); window_info->fullscreen = fullscreen; @@ -598,12 +603,10 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE window_info->display_rect = NSMakeRect(x, y, width, height); // Cache the necessary info for window-close callbacks into the JVM - if (!peer_info->isCALayer && window_info->jdisplay == NULL) { + if (window_info->jdisplay == NULL) { window_info->jdisplay = (*env)->NewGlobalRef(env, this); } - pool = [[NSAutoreleasePool alloc] init]; - // create window on main thread [MacOSXKeyableWindow performSelectorOnMainThread:@selector(createWindow) withObject:nil waitUntilDone:YES]; diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m index 3056c1a5..5041a309 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m @@ -46,8 +46,8 @@ #include "context.h" #include "common_tools.h" -JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle -(JNIEnv *env, jclass clazz, jobject lock_buffer_handle, jobject peer_info_handle, jboolean forceCALayer) { +JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle +(JNIEnv *env, jclass clazz, jobject lock_buffer_handle, jobject peer_info_handle, jobject window_handle, jboolean forceCALayer) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); @@ -58,20 +58,35 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle if(forceCALayer || (surface->awt.version & 0x80000000)) { //JAWT_MACOSX_USE_CALAYER) { if (macosx_dsi != NULL) { + + if (window_handle == NULL) { + window_handle = newJavaManagedByteBuffer(env, sizeof(MacOSXWindowInfo)); + if (window_handle == NULL) { + throwException(env, "Could not create handle buffer"); + } + } else if (peer_info->window_info->window != nil) { + return window_handle; + } + if (peer_info->isCALayer) { [peer_info->glLayer release]; } + peer_info->glLayer = [GLLayer new]; + peer_info->glLayer->macosx_dsi = macosx_dsi; + peer_info->window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); peer_info->glLayer->window_info = peer_info->window_info; + + [peer_info->glLayer performSelectorOnMainThread:@selector(createWindow:) withObject:peer_info->pixel_format waitUntilDone:YES]; + + peer_info->isCALayer = true; + peer_info->isWindowed = true; + peer_info->parent = nil; + + [pool release]; + return window_handle; } - - peer_info->isCALayer = true; - peer_info->isWindowed = true; - peer_info->parent = nil; - - [pool release]; - return; } // no CALayer support, fallback to using legacy method of getting the NSView of an AWT Canvas @@ -80,6 +95,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle peer_info->isWindowed = true; [pool release]; + return NULL; } @implementation GLLayer @@ -104,6 +120,29 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle surfaceLayers.layer = nil; } +- (int) getWidth { + return self.bounds.size.width; +} + +- (int) getHeight { + return self.bounds.size.height; +} + +- (void) createWindow:(NSOpenGLPixelFormat*)pixel_format { + if (window_info->window != nil) { + [window_info->window close]; + } + + window_info->display_rect = [[NSScreen mainScreen] frame]; + + window_info->view = [[MacOSXOpenGLView alloc] initWithFrame:window_info->display_rect pixelFormat:pixel_format]; + + window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO]; + [window_info->window setContentView:window_info->view]; + + [window_info->window orderOut:nil]; +} + - (void) blitFrameBuffer { // get the size of the CALayer/AWT Canvas @@ -198,11 +237,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle glClear(GL_COLOR_BUFFER_BIT); } - GLint originalReadFBO; - - // get and save the current fbo values - //glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING_EXT, &originalReadFBO); - // read the LWJGL FBO and blit it into this CALayers FBO glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, fboID); glBlitFramebufferEXT(0, 0, width, height, @@ -210,9 +244,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST); - // restore original fbo read value - //glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, originalReadFBO); - // call super to finalize the drawing - by default all it does is call glFlush() [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp]; } diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m index ff2ee12c..2b2f02b7 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m @@ -151,8 +151,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nReleas } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_setView - //(JNIEnv *env, jclass clazz, jobject peer_info_handle) { - (JNIEnv *env, jclass clazz, jobject peer_info_handle, jobject context_handle) { + (JNIEnv *env, jclass clazz, jobject peer_info_handle, jobject context_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); @@ -165,7 +164,8 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_setView } if (peer_info->isCALayer) { - // if using a CALayer, attach it to AWT Canvas and create a shared opengl context with current context + peer_info->glLayer->setViewport = YES; + // if using a CALayer, attach it to AWT Canvas and create a shared opengl context with current context [peer_info->glLayer performSelectorOnMainThread:@selector(attachLayer) withObject:nil waitUntilDone:NO]; } @@ -177,6 +177,12 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nMakeCu NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle); [context_info->context makeCurrentContext]; + + if (context_info->peer_info->isCALayer && context_info->peer_info->glLayer->setViewport) { + context_info->peer_info->glLayer->setViewport = NO; + glViewport(0, 0, [context_info->peer_info->glLayer getWidth], [context_info->peer_info->glLayer getHeight]); + } + [pool release]; } @@ -208,6 +214,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nDestro context_info->peer_info->isCALayer = false; [context_info->peer_info->glLayer performSelectorOnMainThread:@selector(removeLayer) withObject:nil waitUntilDone:YES]; [context_info->peer_info->glLayer release]; + context_info->peer_info->glLayer = nil; } // clearDrawable on main thread to ensure its not in use @@ -218,7 +225,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nDestro context_info->context = nil; context_info->peer_info->window_info->context = nil; } - else [context_info->context release]; [pool release]; } From d8297e75837a92d07f6cb477ebcf061543f7b5a4 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Thu, 14 Mar 2013 02:58:14 +0000 Subject: [PATCH 111/111] set correct size on glViewport when using CALayer --- src/native/macosx/context.h | 1 + src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/native/macosx/context.h b/src/native/macosx/context.h index 40151e8d..91932a33 100644 --- a/src/native/macosx/context.h +++ b/src/native/macosx/context.h @@ -102,6 +102,7 @@ typedef struct { @interface GLLayer : CAOpenGLLayer { @public JAWT_MacOSXDrawingSurfaceInfo *macosx_dsi; + JAWT_Rectangle canvasBounds; MacOSXWindowInfo *window_info; bool setViewport; diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m index 5041a309..b217509c 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m @@ -75,6 +75,7 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle peer_info->glLayer = [GLLayer new]; peer_info->glLayer->macosx_dsi = macosx_dsi; + peer_info->glLayer->canvasBounds = (JAWT_Rectangle)surface->dsi->bounds; peer_info->window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle); peer_info->glLayer->window_info = peer_info->window_info; @@ -104,6 +105,7 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle self.asynchronous = YES; self.needsDisplayOnBoundsChange = YES; self.opaque = NO; + self.contentsGravity = kCAGravityTopLeft; self.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable; // get root layer of the AWT Canvas and add self to it @@ -121,11 +123,11 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle } - (int) getWidth { - return self.bounds.size.width; + return canvasBounds.width; } - (int) getHeight { - return self.bounds.size.height; + return canvasBounds.height; } - (void) createWindow:(NSOpenGLPixelFormat*)pixel_format { @@ -240,7 +242,7 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle // read the LWJGL FBO and blit it into this CALayers FBO glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, fboID); glBlitFramebufferEXT(0, 0, width, height, - 0, 0, width, height, + 0, height - fboHeight, width, height, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST);