From db4eb9c5c81c77e49f6be48c857ca7bfe3b57b34 Mon Sep 17 00:00:00 2001 From: kappaOne Date: Fri, 8 Feb 2013 23:29:15 +0000 Subject: [PATCH] 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; }