diff options
21 files changed, 614 insertions, 302 deletions
diff --git a/make/build-nativewindow.xml b/make/build-nativewindow.xml index 432646841..c7e04dbbb 100644 --- a/make/build-nativewindow.xml +++ b/make/build-nativewindow.xml @@ -560,14 +560,16 @@ </patternset> <patternset id="c.src.files.x11"> - <include name="${rootrel.generated.c}/X11/X11*.c" if="isX11"/> - <include name="${rootrel.src.c}/x11/Xmisc.c" if="isX11"/> - <include name="${rootrel.src.c}/x11/XineramaHelper.c" if="isX11"/> + <include name="${rootrel.generated.c}/X11/X11*.c"/> + <include name="${rootrel.src.c}/x11/Xmisc.c"/> + <include name="${rootrel.src.c}/x11/XineramaHelper.c"/> + <include name="${rootrel.src.c}/NativewindowCommon.c"/> </patternset> <patternset id="c.src.files.windows"> - <include name="${rootrel.generated.c}/Windows/GDI*.c" if="isWindows"/> - <include name="${rootrel.src.c}/windows/GDImisc.c" if="isWindows"/> + <include name="${rootrel.generated.c}/Windows/GDI*.c"/> + <include name="${rootrel.src.c}/windows/GDImisc.c"/> + <include name="${rootrel.src.c}/NativewindowCommon.c"/> </patternset> <echo message="Compiling @{output.lib.name}" /> @@ -591,6 +593,7 @@ <includepath path="${src.generated.c}/X11" if="isX11"/> <includepath path="${src.generated.c}/MacOSX" if="isOSX"/> <includepath path="${src.generated.c}/Windows" if="isWindows"/> + <includepath path="${src.c}"/> <!-- This must come last to not override real include paths --> <!-- includepath path="stub_includes/macosx" if="isOSX" / --> diff --git a/make/config/nativewindow/win32-CustomJavaCode.java b/make/config/nativewindow/win32-CustomJavaCode.java index 0c1a3a402..5d0c82998 100644 --- a/make/config/nativewindow/win32-CustomJavaCode.java +++ b/make/config/nativewindow/win32-CustomJavaCode.java @@ -1,22 +1,50 @@ - static { - NWJNILibLoader.loadNativeWindow("win32"); - - if( !initIDs0() ) { - throw new NativeWindowException("GDI: Could not initialized native stub"); - } - } + private static final boolean DEBUG = Debug.debug("GDI"); + + private static final String dummyWindowClassNameBase = "_dummyWindow_clazz" ; + private static RegisteredClassFactory dummyWindowClassFactory; + private static boolean isInit = false; - public static synchronized void initSingleton() { - } private static native boolean initIDs0(); + private static native long getDummyWndProc0(); - private static Object createDummyWindowSync = new Object(); + public static synchronized void initSingleton(boolean firstX11ActionOnProcess) { + if(!isInit) { + NWJNILibLoader.loadNativeWindow("win32"); + + if( !initIDs0() ) { + throw new NativeWindowException("GDI: Could not initialized native stub"); + } + + if(DEBUG) { + System.out.println("GDI.isFirstX11ActionOnProcess: "+firstX11ActionOnProcess); + } + + dummyWindowClassFactory = new RegisteredClassFactory(dummyWindowClassNameBase, getDummyWndProc0()); + isInit = true; + } + } + + private static RegisteredClass dummyWindowClass = null; + private static Object dummyWindowSync = new Object(); public static long CreateDummyWindow(int x, int y, int width, int height) { - synchronized(createDummyWindowSync) { - return CreateDummyWindow0(x, y, width, height); + synchronized(dummyWindowSync) { + dummyWindowClass = dummyWindowClassFactory.getSharedClass(); + return CreateDummyWindow0(dummyWindowClass.getHandle(), dummyWindowClass.getName(), dummyWindowClass.getName(), x, y, width, height); + } + } + + public static boolean DestroyDummyWindow(long hwnd) { + boolean res; + synchronized(dummyWindowSync) { + if( null == dummyWindowClass ) { + throw new InternalError("GDI Error ("+dummyWindowClassFactory.getSharedRefCount()+"): SharedClass is null"); + } + res = DestroyWindow(hwnd); + dummyWindowClassFactory.releaseSharedClass(); } + return res; } public static Point GetRelativeLocation(long src_win, long dest_win, int src_x, int src_y) { @@ -24,3 +52,7 @@ } private static native Object GetRelativeLocation0(long src_win, long dest_win, int src_x, int src_y); + public static native boolean CreateWindowClass(long hInstance, String clazzName, long wndProc); + public static native boolean DestroyWindowClass(long hInstance, String className); + static native long CreateDummyWindow0(long hInstance, String className, String windowName, int x, int y, int width, int height); + diff --git a/make/config/nativewindow/win32-lib.cfg b/make/config/nativewindow/win32-lib.cfg index e140ed3aa..0ba7d63bc 100644 --- a/make/config/nativewindow/win32-lib.cfg +++ b/make/config/nativewindow/win32-lib.cfg @@ -22,7 +22,10 @@ Opaque long void ** Import javax.media.nativewindow.util.Point Import javax.media.nativewindow.NativeWindowException +Import javax.media.nativewindow.windows.RegisteredClass +Import javax.media.nativewindow.windows.RegisteredClassFactory Import com.jogamp.nativewindow.impl.NWJNILibLoader +import com.jogamp.nativewindow.impl.Debug CustomCCode #define WIN32_LEAN_AND_MEAN CustomCCode #include <windows.h> @@ -33,7 +36,7 @@ CustomCCode #include <stddef.h> Include ../intptr.cfg -CustomCCode extern HWND CreateDummyWindow0( int x, int y, int width, int height ) ; +CustomCCode extern HINSTANCE GetApplicationHandle(); IncludeAs CustomJavaCode GDI win32-CustomJavaCode.java diff --git a/make/stub_includes/win32/windows.h b/make/stub_includes/win32/windows.h index d9f9e692a..382ab8c81 100644 --- a/make/stub_includes/win32/windows.h +++ b/make/stub_includes/win32/windows.h @@ -34,6 +34,7 @@ typedef unsigned int* PUINT; typedef unsigned int UINT; typedef unsigned short USHORT; typedef unsigned short WORD; +typedef unsigned short ATOM; /* Necessary handle typedefs for parsing wglext.h */ diff --git a/make/stub_includes/win32/wingdi.h b/make/stub_includes/win32/wingdi.h index 73f6276fd..7862b5533 100644 --- a/make/stub_includes/win32/wingdi.h +++ b/make/stub_includes/win32/wingdi.h @@ -187,7 +187,7 @@ WINGDIAPI HGDIOBJ WINAPI SelectObject(HDC, HGDIOBJ); // Routines for creation of a dummy window, device context and OpenGL // context for the purposes of getting wglChoosePixelFormatARB and // associated routines - HWND CreateDummyWindow0( int x, int y, int width, int height ) ; + HINSTANCE GetApplicationHandle(); WINUSERAPI BOOL WINAPI ShowWindow(HWND hWnd, int nCmdShow); WINUSERAPI HDC WINAPI GetDC(HWND); WINUSERAPI int WINAPI ReleaseDC(HWND hWnd, HDC hDC); diff --git a/src/jogl/classes/com/jogamp/opengl/impl/windows/wgl/WindowsDummyWGLDrawable.java b/src/jogl/classes/com/jogamp/opengl/impl/windows/wgl/WindowsDummyWGLDrawable.java index 920000bf1..9a93f6b33 100644 --- a/src/jogl/classes/com/jogamp/opengl/impl/windows/wgl/WindowsDummyWGLDrawable.java +++ b/src/jogl/classes/com/jogamp/opengl/impl/windows/wgl/WindowsDummyWGLDrawable.java @@ -65,32 +65,21 @@ public class WindowsDummyWGLDrawable extends WindowsWGLDrawable { } ProxySurface ns = (ProxySurface) getNativeSurface(); ns.setSize(f_dim, f_dim); - - WindowsWGLGraphicsConfiguration config = (WindowsWGLGraphicsConfiguration)ns.getGraphicsConfiguration().getNativeGraphicsConfiguration(); - AbstractGraphicsDevice adevice = config.getScreen().getDevice(); - adevice.lock(); + + if(NativeSurface.LOCK_SURFACE_NOT_READY >= lockSurface()) { + throw new GLException("WindowsDummyWGLDrawable: surface not ready (lockSurface)"); + } try { - if(NativeSurface.LOCK_SURFACE_NOT_READY >= ns.lockSurface()) { - throw new GLException("WindowsDummyWGLDrawable: surface not ready (lockSurface)"); - } - try { - hdc = GDI.GetDC(hwnd); - if(0 == hdc) { - throw new GLException("Error hdc 0, werr: "+GDI.GetLastError()); - } - ns.setSurfaceHandle(hdc); - config.updateGraphicsConfiguration(factory, ns, null); - if (DEBUG) { - System.err.println("!!! WindowsDummyWGLDrawable: hdc "+toHexString(hdc)+", "+config); - } - } catch (Throwable t) { - destroyImpl(); - throw new GLException(t); - } finally { - ns.unlockSurface(); + WindowsWGLGraphicsConfiguration config = (WindowsWGLGraphicsConfiguration)ns.getGraphicsConfiguration().getNativeGraphicsConfiguration(); + config.updateGraphicsConfiguration(factory, ns, null); + if (DEBUG) { + System.err.println("!!! WindowsDummyWGLDrawable: "+config); } + } catch (Throwable t) { + destroyImpl(); + throw new GLException(t); } finally { - adevice.unlock(); + unlockSurface(); } } @@ -103,6 +92,53 @@ public class WindowsDummyWGLDrawable extends WindowsWGLDrawable { return new WindowsDummyWGLDrawable(factory, caps, absScreen); } + public int lockSurface() throws GLException { + int res = NativeSurface.LOCK_SURFACE_NOT_READY; + ProxySurface ns = (ProxySurface) getNativeSurface(); + AbstractGraphicsDevice adevice = ns.getGraphicsConfiguration().getNativeGraphicsConfiguration().getScreen().getDevice(); + adevice.lock(); + try { + res = ns.lockSurface(); + if(NativeSurface.LOCK_SUCCESS == res) { + if(0 == hdc) { + hdc = GDI.GetDC(hwnd); + ns.setSurfaceHandle(hdc); + if(0 == hdc) { + res = NativeSurface.LOCK_SURFACE_NOT_READY; + ns.unlockSurface(); + throw new GLException("Error hdc 0, werr: "+GDI.GetLastError()); + // finally will unlock adevice + } + } + } else { + Throwable t = new Throwable("Error lock failed - res "+res+", hwnd "+toHexString(hwnd)+", hdc "+toHexString(hdc)); + t.printStackTrace(); + } + } finally { + if( NativeSurface.LOCK_SURFACE_NOT_READY == res ) { + adevice.unlock(); + } + } + return res; + } + + public void unlockSurface() { + ProxySurface ns = (ProxySurface) getNativeSurface(); + ns.validateSurfaceLocked(); + AbstractGraphicsDevice adevice = ns.getGraphicsConfiguration().getNativeGraphicsConfiguration().getScreen().getDevice(); + + try { + if ( 0 != hdc && 0 != hwnd && ns.getSurfaceRecursionCount() == 0) { + GDI.ReleaseDC(hwnd, hdc); + hdc=0; + ns.setSurfaceHandle(hdc); + } + surface.unlockSurface(); + } finally { + adevice.unlock(); + } + } + public void setSize(int width, int height) { } @@ -128,7 +164,7 @@ public class WindowsDummyWGLDrawable extends WindowsWGLDrawable { } if (hwnd != 0) { GDI.ShowWindow(hwnd, GDI.SW_HIDE); - GDI.DestroyWindow(hwnd); + GDI.DestroyDummyWindow(hwnd); hwnd = 0; } } diff --git a/src/jogl/classes/com/jogamp/opengl/impl/windows/wgl/WindowsWGLDrawableFactory.java b/src/jogl/classes/com/jogamp/opengl/impl/windows/wgl/WindowsWGLDrawableFactory.java index 4d687a00c..eb6db0a50 100644 --- a/src/jogl/classes/com/jogamp/opengl/impl/windows/wgl/WindowsWGLDrawableFactory.java +++ b/src/jogl/classes/com/jogamp/opengl/impl/windows/wgl/WindowsWGLDrawableFactory.java @@ -45,8 +45,6 @@ import java.nio.ShortBuffer; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; import java.util.List; import javax.media.nativewindow.AbstractGraphicsDevice; @@ -72,6 +70,7 @@ import com.jogamp.opengl.impl.GLDrawableImpl; import com.jogamp.opengl.impl.GLDynamicLookupHelper; import com.jogamp.opengl.impl.SharedResourceRunner; import javax.media.nativewindow.AbstractGraphicsConfiguration; +import javax.media.nativewindow.windows.RegisteredClassFactory; import javax.media.opengl.GLCapabilitiesImmutable; public class WindowsWGLDrawableFactory extends GLDrawableFactoryImpl { @@ -195,11 +194,16 @@ public class WindowsWGLDrawableFactory extends GLDrawableFactoryImpl { throw new GLException("Couldn't create shared context for drawable: "+sharedDrawable); } sharedContext.setSynchronized(true); + boolean canCreateGLPbuffer; + boolean readDrawableAvailable; sharedContext.makeCurrent(); - boolean canCreateGLPbuffer = sharedContext.getGL().isExtensionAvailable(GL_ARB_pbuffer); - boolean readDrawableAvailable = sharedContext.isExtensionAvailable(WGL_ARB_make_current_read) && - sharedContext.isFunctionAvailable(wglMakeContextCurrent); - sharedContext.release(); + try { + canCreateGLPbuffer = sharedContext.getGL().isExtensionAvailable(GL_ARB_pbuffer); + readDrawableAvailable = sharedContext.isExtensionAvailable(WGL_ARB_make_current_read) && + sharedContext.isFunctionAvailable(wglMakeContextCurrent); + } finally { + sharedContext.release(); + } if (DEBUG) { System.err.println("!!! SharedDevice: " + sharedDevice); System.err.println("!!! SharedScreen: " + absScreen); @@ -231,7 +235,7 @@ public class WindowsWGLDrawableFactory extends GLDrawableFactoryImpl { } if (null != sr.drawable) { - // may cause JVM SIGSEGV: sharedDrawable.destroy(); + sr.drawable.destroy(); sr.drawable = null; } @@ -287,6 +291,7 @@ public class WindowsWGLDrawableFactory extends GLDrawableFactoryImpl { protected final void shutdownInstance() { sharedResourceRunner.releaseAndWait(); + RegisteredClassFactory.shutdownSharedClasses(); } protected final GLDrawableImpl createOnscreenDrawableImpl(NativeSurface target) { diff --git a/src/jogl/classes/javax/media/opengl/awt/AWTGLAutoDrawable.java b/src/jogl/classes/javax/media/opengl/awt/AWTGLAutoDrawable.java index d92cec389..d1e725b00 100644 --- a/src/jogl/classes/javax/media/opengl/awt/AWTGLAutoDrawable.java +++ b/src/jogl/classes/javax/media/opengl/awt/AWTGLAutoDrawable.java @@ -40,7 +40,6 @@ package javax.media.opengl.awt; import javax.media.opengl.*; -import javax.media.opengl.glu.*; public interface AWTGLAutoDrawable extends GLAutoDrawable, ComponentEvents { /** Requests a new width and height for this AWTGLAutoDrawable. */ diff --git a/src/junit/com/jogamp/test/junit/util/QuitAdapter.java b/src/junit/com/jogamp/test/junit/util/QuitAdapter.java index 258132582..174412295 100644 --- a/src/junit/com/jogamp/test/junit/util/QuitAdapter.java +++ b/src/junit/com/jogamp/test/junit/util/QuitAdapter.java @@ -28,7 +28,6 @@ package com.jogamp.test.junit.util; -import com.jogamp.opengl.util.Animator; import com.jogamp.newt.event.*; public class QuitAdapter extends WindowAdapter implements WindowListener, KeyListener { diff --git a/src/nativewindow/classes/com/jogamp/nativewindow/impl/x11/X11Util.java b/src/nativewindow/classes/com/jogamp/nativewindow/impl/x11/X11Util.java index f3b7f4a4e..c56f632d8 100644 --- a/src/nativewindow/classes/com/jogamp/nativewindow/impl/x11/X11Util.java +++ b/src/nativewindow/classes/com/jogamp/nativewindow/impl/x11/X11Util.java @@ -34,10 +34,11 @@ package com.jogamp.nativewindow.impl.x11; import com.jogamp.common.util.LongObjectHashMap; +import com.jogamp.nativewindow.impl.Debug; +import com.jogamp.nativewindow.impl.NWJNILibLoader; import javax.media.nativewindow.*; -import com.jogamp.nativewindow.impl.*; import java.nio.Buffer; import java.nio.IntBuffer; import java.nio.ShortBuffer; diff --git a/src/nativewindow/classes/javax/media/nativewindow/NativeWindowFactory.java b/src/nativewindow/classes/javax/media/nativewindow/NativeWindowFactory.java index a36440873..9e0aa28fa 100644 --- a/src/nativewindow/classes/javax/media/nativewindow/NativeWindowFactory.java +++ b/src/nativewindow/classes/javax/media/nativewindow/NativeWindowFactory.java @@ -80,6 +80,7 @@ public abstract class NativeWindowFactory { public static final String AWTComponentClassName = "java.awt.Component" ; public static final String JAWTUtilClassName = "com.jogamp.nativewindow.impl.jawt.JAWTUtil" ; public static final String X11UtilClassName = "com.jogamp.nativewindow.impl.x11.X11Util"; + public static final String GDIClassName = "com.jogamp.nativewindow.impl.windows.GDI"; public static final String X11JAWTToolkitLockClassName = "com.jogamp.nativewindow.impl.jawt.x11.X11JAWTToolkitLock" ; public static final String X11ToolkitLockClassName = "com.jogamp.nativewindow.impl.x11.X11ToolkitLock" ; private static Class jawtUtilClass; @@ -117,6 +118,20 @@ public abstract class NativeWindowFactory { static boolean initialized = false; + private static void initNativeImpl(final boolean firstUIActionOnProcess, final ClassLoader cl) { + String clazzName = null; + if( TYPE_X11.equals(nativeWindowingTypePure) ) { + clazzName = X11UtilClassName; + } else if( TYPE_WINDOWS.equals(nativeWindowingTypePure) ) { + clazzName = GDIClassName; + } + if( null != clazzName ) { + ReflectionUtil.callStaticMethod(clazzName, "initSingleton", + new Class[] { boolean.class }, + new Object[] { new Boolean(firstUIActionOnProcess) }, cl ); + } + } + /** * Static one time initialization of this factory.<br> * This initialization method <b>must be called</b> once by the program or utilizing modules!<br> @@ -144,15 +159,14 @@ public abstract class NativeWindowFactory { nativeWindowingTypeCustom = nativeOSNameCustom; } - ClassLoader cl = NativeWindowFactory.class.getClassLoader(); + final ClassLoader cl = NativeWindowFactory.class.getClassLoader(); - if( TYPE_X11.equals(nativeWindowingTypePure) ) { - // explicit initialization of X11Util - ReflectionUtil.callStaticMethod(X11UtilClassName, "initSingleton", - new Class[] { boolean.class }, - new Object[] { new Boolean(firstUIActionOnProcess) }, cl ); + if(firstUIActionOnProcess) { + // X11 initialization before possible AWT initialization + initNativeImpl(firstUIActionOnProcess, cl); } isFirstUIActionOnProcess = firstUIActionOnProcess; + isAWTAvailable = false; // may be set to true below if( !Debug.getBooleanProperty("java.awt.headless", true, acc) && ReflectionUtil.isClassAvailable(AWTComponentClassName, cl) && @@ -180,16 +194,13 @@ public abstract class NativeWindowFactory { // AWT is only available in case all above classes are available // and AWT is not int headless mode isAWTAvailable = ((Boolean)resO).equals(Boolean.FALSE); - } else { - isAWTAvailable = false; } - } else { - isAWTAvailable = false; } - } else { - isAWTAvailable = false; } - + if(!firstUIActionOnProcess) { + // X11 initialization after possible AWT initialization + initNativeImpl(firstUIActionOnProcess, cl); + } registeredFactories = Collections.synchronizedMap(new HashMap()); // register our default factory -> NativeWindow diff --git a/src/nativewindow/classes/javax/media/nativewindow/windows/RegisteredClass.java b/src/nativewindow/classes/javax/media/nativewindow/windows/RegisteredClass.java new file mode 100644 index 000000000..f42c2cbc0 --- /dev/null +++ b/src/nativewindow/classes/javax/media/nativewindow/windows/RegisteredClass.java @@ -0,0 +1,44 @@ +/** + * Copyright 2010 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. 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. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``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 JogAmp Community 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. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package javax.media.nativewindow.windows; + +public class RegisteredClass { + long hInstance; + String className; + + RegisteredClass(long hInst, String name) { + hInstance = hInst; + className = name; + } + + public final long getHandle() { return hInstance; } + public final String getName() { return className; } + + public final String toString() { return "RegisteredClass[handle 0x"+Long.toHexString(hInstance)+", "+className+"]"; } +} diff --git a/src/nativewindow/classes/javax/media/nativewindow/windows/RegisteredClassFactory.java b/src/nativewindow/classes/javax/media/nativewindow/windows/RegisteredClassFactory.java new file mode 100644 index 000000000..2378bb9a8 --- /dev/null +++ b/src/nativewindow/classes/javax/media/nativewindow/windows/RegisteredClassFactory.java @@ -0,0 +1,134 @@ +/** + * Copyright 2010 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. 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. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``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 JogAmp Community 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. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package javax.media.nativewindow.windows; + +import com.jogamp.nativewindow.impl.Debug; +import com.jogamp.nativewindow.impl.windows.GDI; +import java.util.ArrayList; +import javax.media.nativewindow.NativeWindowException; + +public class RegisteredClassFactory { + static final boolean DEBUG = Debug.debug("RegisteredClass"); + private static ArrayList sharedClasses = new ArrayList(); + private String classBaseName; + long wndProc; + + private RegisteredClass sharedClass = null; + private int classIter = 0; + private int sharedRefCount = 0; + private Object sync = new Object(); + + /** + * Intended for a JVM shutdown hook, hence little synchronization + */ + public static void shutdownSharedClasses() { + synchronized(sharedClasses) { + for(int i=0; i<sharedClasses.size(); i++) { + RegisteredClass sc = (RegisteredClass) sharedClasses.get(i); + GDI.DestroyWindowClass(sc.getHandle(), sc.getName()); + if(DEBUG) { + System.err.println("RegisteredClassFactory shutdownSharedClasses "+i+"/"+sharedClasses.size()+": "+sc); + } + } + sharedClasses.clear(); + } + } + + public RegisteredClassFactory(String classBaseName, long wndProc) { + this.classBaseName = classBaseName; + this.wndProc = wndProc; + } + + public RegisteredClass getSharedClass() throws NativeWindowException { + synchronized(sync) { + if( 0 == sharedRefCount ) { + if( null != sharedClass ) { + throw new InternalError("Error ("+sharedRefCount+"): SharedClass not null: "+sharedClass); + } + long hInstance = GDI.GetApplicationHandle(); + if( 0 == hInstance ) { + throw new NativeWindowException("Error: Null ModuleHandle for Application"); + } + String clazzName = null; + boolean registered = false; + while ( !registered && Integer.MAX_VALUE >= classIter ) { + // Retry with next clazz name, this could happen if more than one JVM is running + clazzName = classBaseName + classIter; + classIter++; + registered = GDI.CreateWindowClass(hInstance, clazzName, wndProc); + } + if( !registered ) { + throw new NativeWindowException("Error: Could not create WindowClass: "+clazzName); + } + sharedClass = new RegisteredClass(hInstance, clazzName); + synchronized(sharedClasses) { + sharedClasses.add(sharedClass); + } + if(DEBUG) { + System.err.println("RegisteredClassFactory getSharedClass ("+sharedRefCount+") initialized: "+sharedClass); + } + } else if ( null == sharedClass ) { + throw new InternalError("Error ("+sharedRefCount+"): SharedClass is null"); + } + sharedRefCount++; + } + return sharedClass; + } + + public void releaseSharedClass() { + synchronized(sync) { + if( 0 == sharedRefCount ) { + if( null != sharedClass ) { + throw new InternalError("Error ("+sharedRefCount+"): SharedClass not null: "+sharedClass); + } + return; + } + sharedRefCount--; + if( null == sharedClass ) { + throw new InternalError("Error ("+sharedRefCount+"): SharedClass is null"); + } + if( 0 == sharedRefCount ) { + GDI.DestroyWindowClass(sharedClass.getHandle(), sharedClass.getName()); + synchronized(sharedClasses) { + sharedClasses.remove(sharedClass); + } + if(DEBUG) { + System.err.println("RegisteredClassFactory releaseSharedClass ("+sharedRefCount+") released: "+sharedClass); + } + sharedClass = null; + sharedRefCount = 0; + classIter = 0; + } + } + } + + public int getSharedRefCount() { + return sharedRefCount; + } +} diff --git a/src/nativewindow/native/NativewindowCommon.c b/src/nativewindow/native/NativewindowCommon.c new file mode 100644 index 000000000..e357045d6 --- /dev/null +++ b/src/nativewindow/native/NativewindowCommon.c @@ -0,0 +1,57 @@ + +#include "NativewindowCommon.h" + +static const char * const ClazzNameRuntimeException = "java/lang/RuntimeException"; +static jclass runtimeExceptionClz=NULL; + +void NativewindowCommon_FatalError(JNIEnv *env, const char* msg, ...) +{ + char buffer[512]; + va_list ap; + + va_start(ap, msg); + vsnprintf(buffer, sizeof(buffer), msg, ap); + va_end(ap); + + fprintf(stderr, "%s\n", buffer); + (*env)->FatalError(env, buffer); +} + +void NativewindowCommon_throwNewRuntimeException(JNIEnv *env, const char* msg, ...) +{ + char buffer[512]; + va_list ap; + + va_start(ap, msg); + vsnprintf(buffer, sizeof(buffer), msg, ap); + va_end(ap); + + (*env)->ThrowNew(env, runtimeExceptionClz, buffer); +} + +int NativewindowCommon_init(JNIEnv *env) { + if(NULL==runtimeExceptionClz) { + jclass c = (*env)->FindClass(env, ClazzNameRuntimeException); + if(NULL==c) { + NativewindowCommon_FatalError(env, "Nativewindow: can't find %s", ClazzNameRuntimeException); + } + runtimeExceptionClz = (jclass)(*env)->NewGlobalRef(env, c); + (*env)->DeleteLocalRef(env, c); + if(NULL==runtimeExceptionClz) { + NativewindowCommon_FatalError(env, "Nativewindow: can't use %s", ClazzNameRuntimeException); + } + return 1; + } + return 0; +} + +jchar* NativewindowCommon_GetNullTerminatedStringChars(JNIEnv* env, jstring str) +{ + jchar* strChars = NULL; + strChars = calloc((*env)->GetStringLength(env, str) + 1, sizeof(jchar)); + if (strChars != NULL) { + (*env)->GetStringRegion(env, str, 0, (*env)->GetStringLength(env, str), strChars); + } + return strChars; +} + diff --git a/src/nativewindow/native/NativewindowCommon.h b/src/nativewindow/native/NativewindowCommon.h new file mode 100644 index 000000000..5dc5debef --- /dev/null +++ b/src/nativewindow/native/NativewindowCommon.h @@ -0,0 +1,15 @@ + +#ifndef NATIVEWINDOW_COMMON_H +#define NATIVEWINDOW_COMMON_H 1 + +#include <jni.h> +#include <stdlib.h> + +int NativewindowCommon_init(JNIEnv *env); + +jchar* NativewindowCommon_GetNullTerminatedStringChars(JNIEnv* env, jstring str); + +void NativewindowCommon_FatalError(JNIEnv *env, const char* msg, ...); +void NativewindowCommon_throwNewRuntimeException(JNIEnv *env, const char* msg, ...); + +#endif diff --git a/src/nativewindow/native/windows/GDImisc.c b/src/nativewindow/native/windows/GDImisc.c index 0de40cf50..798edcbbf 100644 --- a/src/nativewindow/native/windows/GDImisc.c +++ b/src/nativewindow/native/windows/GDImisc.c @@ -29,6 +29,7 @@ #include <stdio.h> +#include "NativewindowCommon.h" #include "com_jogamp_nativewindow_impl_windows_GDI.h" // #define VERBOSE_ON 1 @@ -39,19 +40,6 @@ #define DBG_PRINT(args...) #endif -static void _FatalError(JNIEnv *env, const char* msg, ...) -{ - char buffer[512]; - va_list ap; - - va_start(ap, msg); - vsnprintf(buffer, sizeof(buffer), msg, ap); - va_end(ap); - - fprintf(stderr, "%s\n", buffer); - (*env)->FatalError(env, buffer); -} - static const char * const ClazzNamePoint = "javax/media/nativewindow/util/Point"; static const char * const ClazzAnyCstrName = "<init>"; static const char * const ClazzNamePointCstrSignature = "(II)V"; @@ -59,52 +47,136 @@ static const char * const ClazzNamePointCstrSignature = "(II)V"; static jclass pointClz = NULL; static jmethodID pointCstr = NULL; -#define NATIVEWINDOW_DUMMY_WINDOW_NAME "__nativewindow_dummy_window" -static ATOM nativewindowClass = 0; -static HINSTANCE nativeHInstance = NULL; - -LRESULT CALLBACK DummyWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { - return DefWindowProc(hWnd,uMsg,wParam,lParam); +HINSTANCE GetApplicationHandle() { + return GetModuleHandle(NULL); } -HWND CreateDummyWindow0(int x, int y, int width, int height ) { - DWORD dwExStyle; - DWORD dwStyle; - HWND hWnd; - HINSTANCE hInstance = GetModuleHandle(NULL); - if( nativeHInstance != hInstance || 0 == nativewindowClass ) { - nativeHInstance=hInstance; - WNDCLASS wc; +/* Java->C glue code: + * Java package: com.jogamp.nativewindow.impl.windows.GDI + * Java method: boolean CreateWindowClass(long hInstance, java.lang.String clazzName, long wndProc) + * C function: BOOL CreateWindowClass(HANDLE hInstance, LPCSTR clazzName, HANDLE wndProc); + */ +JNIEXPORT jboolean JNICALL +Java_com_jogamp_nativewindow_impl_windows_GDI_CreateWindowClass + (JNIEnv *env, jclass _unused, jlong jHInstance, jstring jClazzName, jlong wndProc) +{ + HINSTANCE hInstance = (HINSTANCE) (intptr_t) jHInstance; + const TCHAR* clazzName = NULL; + WNDCLASS wc; + jboolean res; + +#ifdef UNICODE + clazzName = NewtCommon_GetNullTerminatedStringChars(env, jClazzName); +#else + clazzName = (*env)->GetStringUTFChars(env, jClazzName, NULL); +#endif + + ZeroMemory( &wc, sizeof( wc ) ); + if( GetClassInfo( hInstance, clazzName, &wc ) ) { + // registered already + res = JNI_TRUE; + } else { + // register now ZeroMemory( &wc, sizeof( wc ) ); - wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; - wc.lpfnWndProc = (WNDPROC) DummyWndProc; + wc.style = CS_HREDRAW | CS_VREDRAW ; + wc.lpfnWndProc = (WNDPROC) (intptr_t) wndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = NULL; - wc.hCursor = NULL; - wc.hbrBackground = NULL; + wc.hCursor = LoadCursor( NULL, IDC_ARROW); + wc.hbrBackground = NULL; // no background paint - GetStockObject(BLACK_BRUSH); wc.lpszMenuName = NULL; - wc.lpszClassName = NATIVEWINDOW_DUMMY_WINDOW_NAME; - if( !(nativewindowClass = RegisterClass( &wc )) ) { - fprintf(stderr, "FatalError com_jogamp_nativewindow_impl_windows_GDI: RegisterClass Failed: %d\n", GetLastError() ); - return( 0 ); - } + wc.lpszClassName = clazzName; + res = ( 0 != RegisterClass( &wc ) ) ? JNI_TRUE : JNI_FALSE ; } +#ifdef UNICODE + free((void*) clazzName); +#else + (*env)->ReleaseStringUTFChars(env, jClazzName, clazzName); +#endif + + return res; +} + +/* Java->C glue code: + * Java package: com.jogamp.nativewindow.impl.windows.GDI + * Java method: boolean DestroyWindowClass(long hInstance, java.lang.String className) + * C function: BOOL DestroyWindowClass(HANDLE hInstance, LPCSTR className); + */ +JNIEXPORT jboolean JNICALL +Java_com_jogamp_nativewindow_impl_windows_GDI_DestroyWindowClass + (JNIEnv *env, jclass _unused, jlong jHInstance, jstring jClazzName) +{ + HINSTANCE hInstance = (HINSTANCE) (intptr_t) jHInstance; + const TCHAR* clazzName = NULL; + jboolean res; + +#ifdef UNICODE + clazzName = NewtCommon_GetNullTerminatedStringChars(env, jClazzName); +#else + clazzName = (*env)->GetStringUTFChars(env, jClazzName, NULL); +#endif + + res = ( 0 != UnregisterClass( clazzName, hInstance ) ) ? JNI_TRUE : JNI_FALSE ; + +#ifdef UNICODE + free((void*) clazzName); +#else + (*env)->ReleaseStringUTFChars(env, jClazzName, clazzName); +#endif + + return res; +} + + +/* Java->C glue code: + * Java package: com.jogamp.nativewindow.impl.windows.GDI + * Java method: long CreateDummyWindow0(long hInstance, java.lang.String className, java.lang.String windowName, int x, int y, int width, int height) + * C function: HANDLE CreateDummyWindow0(HANDLE hInstance, LPCSTR className, LPCSTR windowName, int x, int y, int width, int height); + */ +JNIEXPORT jlong JNICALL +Java_com_jogamp_nativewindow_impl_windows_GDI_CreateDummyWindow0 + (JNIEnv *env, jclass _unused, jlong jHInstance, jstring jWndClassName, jstring jWndName, jint x, jint y, jint width, jint height) +{ + HINSTANCE hInstance = (HINSTANCE) (intptr_t) jHInstance; + const TCHAR* wndClassName = NULL; + const TCHAR* wndName = NULL; + DWORD dwExStyle; + DWORD dwStyle; + HWND hWnd; + +#ifdef UNICODE + wndClassName = NewtCommon_GetNullTerminatedStringChars(env, jWndClassName); + wndName = NewtCommon_GetNullTerminatedStringChars(env, jWndName); +#else + wndClassName = (*env)->GetStringUTFChars(env, jWndClassName, NULL); + wndName = (*env)->GetStringUTFChars(env, jWndName, NULL); +#endif + dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; dwStyle = WS_OVERLAPPEDWINDOW; - if( !(hWnd=CreateWindowEx( dwExStyle, - NATIVEWINDOW_DUMMY_WINDOW_NAME, - NATIVEWINDOW_DUMMY_WINDOW_NAME, - dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, - x, y, width, height, - NULL, NULL, hInstance, NULL ) ) ) { - return( 0 ); - } - return( hWnd ); + + hWnd = CreateWindowEx( dwExStyle, + wndClassName, + wndName, + dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, + x, y, width, height, + NULL, NULL, hInstance, NULL ); + +#ifdef UNICODE + free((void*) wndClassName); + free((void*) wndName); +#else + (*env)->ReleaseStringUTFChars(env, jWndClassName, wndClassName); + (*env)->ReleaseStringUTFChars(env, jWndName, wndName); +#endif + + return (jlong) (intptr_t) hWnd; } + /* * Class: com_jogamp_nativewindow_impl_windows_GDI * Method: initIDs0 @@ -113,25 +185,40 @@ HWND CreateDummyWindow0(int x, int y, int width, int height ) { JNIEXPORT jboolean JNICALL Java_com_jogamp_nativewindow_impl_windows_GDI_initIDs0 (JNIEnv *env, jclass clazz) { - if(NULL==pointClz) { + if(NativewindowCommon_init(env)) { jclass c = (*env)->FindClass(env, ClazzNamePoint); if(NULL==c) { - _FatalError(env, "FatalError com_jogamp_nativewindow_impl_windows_GDI: can't find %s", ClazzNamePoint); + NativewindowCommon_FatalError(env, "FatalError com_jogamp_nativewindow_impl_windows_GDI: can't find %s", ClazzNamePoint); } pointClz = (jclass)(*env)->NewGlobalRef(env, c); (*env)->DeleteLocalRef(env, c); if(NULL==pointClz) { - _FatalError(env, "FatalError com_jogamp_nativewindow_impl_windows_GDI: can't use %s", ClazzNamePoint); + NativewindowCommon_FatalError(env, "FatalError com_jogamp_nativewindow_impl_windows_GDI: can't use %s", ClazzNamePoint); } pointCstr = (*env)->GetMethodID(env, pointClz, ClazzAnyCstrName, ClazzNamePointCstrSignature); if(NULL==pointCstr) { - _FatalError(env, "FatalError com_jogamp_nativewindow_impl_windows_GDI: can't fetch %s.%s %s", + NativewindowCommon_FatalError(env, "FatalError com_jogamp_nativewindow_impl_windows_GDI: can't fetch %s.%s %s", ClazzNamePoint, ClazzAnyCstrName, ClazzNamePointCstrSignature); } } return JNI_TRUE; } +LRESULT CALLBACK DummyWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { + return DefWindowProc(hWnd,uMsg,wParam,lParam); +} + +/* + * Class: com_jogamp_nativewindow_impl_windows_GDI + * Method: getDummyWndProc0 + * Signature: ()J + */ +JNIEXPORT jlong JNICALL Java_com_jogamp_nativewindow_impl_windows_GDI_getDummyWndProc0 + (JNIEnv *env, jclass clazz) +{ + return (jlong) (intptr_t) DummyWndProc; +} + /* * Class: com_jogamp_nativewindow_impl_windows_GDI * Method: GetRelativeLocation0 diff --git a/src/nativewindow/native/x11/Xmisc.c b/src/nativewindow/native/x11/Xmisc.c index efb20c54e..0d92880b3 100644 --- a/src/nativewindow/native/x11/Xmisc.c +++ b/src/nativewindow/native/x11/Xmisc.c @@ -80,6 +80,7 @@ Bool XF86VidModeSetGammaRamp( #define RTLD_DEFAULT NULL #endif +#include "NativewindowCommon.h" #include "com_jogamp_nativewindow_impl_x11_X11Lib.h" // #define VERBOSE_ON 1 @@ -93,107 +94,66 @@ Bool XF86VidModeSetGammaRamp( /* Need to pull this in as we don't have a stub header for it */ extern Bool XineramaEnabled(Display* display); -static void _FatalError(JNIEnv *env, const char* msg, ...) -{ - char buffer[512]; - va_list ap; - - va_start(ap, msg); - vsnprintf(buffer, sizeof(buffer), msg, ap); - va_end(ap); - - fprintf(stderr, "%s\n", buffer); - (*env)->FatalError(env, buffer); -} - static const char * const ClazzNameBuffers = "com/jogamp/common/nio/Buffers"; static const char * const ClazzNameBuffersStaticCstrName = "copyByteBuffer"; static const char * const ClazzNameBuffersStaticCstrSignature = "(Ljava/nio/ByteBuffer;)Ljava/nio/ByteBuffer;"; static const char * const ClazzNameByteBuffer = "java/nio/ByteBuffer"; -static const char * const ClazzNameRuntimeException = "java/lang/RuntimeException"; static const char * const ClazzNamePoint = "javax/media/nativewindow/util/Point"; static const char * const ClazzAnyCstrName = "<init>"; static const char * const ClazzNamePointCstrSignature = "(II)V"; static jclass clazzBuffers = NULL; static jmethodID cstrBuffers = NULL; static jclass clazzByteBuffer = NULL; -static jclass clazzRuntimeException=NULL; static jclass pointClz = NULL; static jmethodID pointCstr = NULL; static void _initClazzAccess(JNIEnv *env) { jclass c; - if(NULL!=clazzRuntimeException) return ; - - c = (*env)->FindClass(env, ClazzNameRuntimeException); - if(NULL==c) { - _FatalError(env, "FatalError Java_com_jogamp_nativewindow_impl_x11_X11Lib: can't find %s", ClazzNameRuntimeException); - } - clazzRuntimeException = (jclass)(*env)->NewGlobalRef(env, c); - (*env)->DeleteLocalRef(env, c); - if(NULL==clazzRuntimeException) { - _FatalError(env, "FatalError Java_com_jogamp_nativewindow_impl_x11_X11Lib: can't use %s", ClazzNameRuntimeException); - } + if(!NativewindowCommon_init(env)) return; c = (*env)->FindClass(env, ClazzNameBuffers); if(NULL==c) { - _FatalError(env, "FatalError Java_com_jogamp_nativewindow_impl_x11_X11Lib: can't find %s", ClazzNameBuffers); + NativewindowCommon_FatalError(env, "FatalError Java_com_jogamp_nativewindow_impl_x11_X11Lib: can't find %s", ClazzNameBuffers); } clazzBuffers = (jclass)(*env)->NewGlobalRef(env, c); (*env)->DeleteLocalRef(env, c); if(NULL==clazzBuffers) { - _FatalError(env, "FatalError Java_com_jogamp_nativewindow_impl_x11_X11Lib: can't use %s", ClazzNameBuffers); + NativewindowCommon_FatalError(env, "FatalError Java_com_jogamp_nativewindow_impl_x11_X11Lib: can't use %s", ClazzNameBuffers); } c = (*env)->FindClass(env, ClazzNameByteBuffer); if(NULL==c) { - _FatalError(env, "FatalError Java_com_jogamp_nativewindow_impl_x11_X11Lib: can't find %s", ClazzNameByteBuffer); + NativewindowCommon_FatalError(env, "FatalError Java_com_jogamp_nativewindow_impl_x11_X11Lib: can't find %s", ClazzNameByteBuffer); } clazzByteBuffer = (jclass)(*env)->NewGlobalRef(env, c); (*env)->DeleteLocalRef(env, c); if(NULL==c) { - _FatalError(env, "FatalError Java_com_jogamp_nativewindow_impl_x11_X11Lib: can't use %s", ClazzNameByteBuffer); + NativewindowCommon_FatalError(env, "FatalError Java_com_jogamp_nativewindow_impl_x11_X11Lib: can't use %s", ClazzNameByteBuffer); } cstrBuffers = (*env)->GetStaticMethodID(env, clazzBuffers, ClazzNameBuffersStaticCstrName, ClazzNameBuffersStaticCstrSignature); if(NULL==cstrBuffers) { - _FatalError(env, "FatalError Java_com_jogamp_nativewindow_impl_x11_X11Lib: can't create %s.%s %s", + NativewindowCommon_FatalError(env, "FatalError Java_com_jogamp_nativewindow_impl_x11_X11Lib: can't create %s.%s %s", ClazzNameBuffers, ClazzNameBuffersStaticCstrName, ClazzNameBuffersStaticCstrSignature); } c = (*env)->FindClass(env, ClazzNamePoint); if(NULL==c) { - _FatalError(env, "FatalError Java_com_jogamp_nativewindow_impl_x11_X11Lib: can't find %s", ClazzNamePoint); + NativewindowCommon_FatalError(env, "FatalError Java_com_jogamp_nativewindow_impl_x11_X11Lib: can't find %s", ClazzNamePoint); } pointClz = (jclass)(*env)->NewGlobalRef(env, c); (*env)->DeleteLocalRef(env, c); if(NULL==pointClz) { - _FatalError(env, "FatalError Java_com_jogamp_nativewindow_impl_x11_X11Lib: can't use %s", ClazzNamePoint); + NativewindowCommon_FatalError(env, "FatalError Java_com_jogamp_nativewindow_impl_x11_X11Lib: can't use %s", ClazzNamePoint); } pointCstr = (*env)->GetMethodID(env, pointClz, ClazzAnyCstrName, ClazzNamePointCstrSignature); if(NULL==pointCstr) { - _FatalError(env, "FatalError Java_com_jogamp_nativewindow_impl_x11_X11Lib: can't fetch %s.%s %s", + NativewindowCommon_FatalError(env, "FatalError Java_com_jogamp_nativewindow_impl_x11_X11Lib: can't fetch %s.%s %s", ClazzNamePoint, ClazzAnyCstrName, ClazzNamePointCstrSignature); } } -static void _throwNewRuntimeException(Display * unlockDisplay, JNIEnv *env, const char* msg, ...) -{ - char buffer[512]; - va_list ap; - - if(NULL!=unlockDisplay) { - XUnlockDisplay(unlockDisplay); - } - - va_start(ap, msg); - vsnprintf(buffer, sizeof(buffer), msg, ap); - va_end(ap); - - (*env)->ThrowNew(env, clazzRuntimeException, buffer); -} - static JNIEnv * x11ErrorHandlerJNIEnv = NULL; static XErrorHandler origErrorHandler = NULL ; static int errorHandlerBlocked = 0 ; @@ -208,7 +168,7 @@ static int x11ErrorHandler(Display *dpy, XErrorEvent *e) // Since the X11 Error may happen anytime, a exception could mess up the JVM completely. // Experienced this for remote displays issuing non supported commands, eg. glXCreateContextAttribsARB(..) // - _throwNewRuntimeException(NULL, x11ErrorHandlerJNIEnv, "Info: Nativewindow X11 Error: Display %p, Code 0x%X, errno %s", + NativewindowCommon_throwNewRuntimeException(x11ErrorHandlerJNIEnv, "Info: Nativewindow X11 Error: Display %p, Code 0x%X, errno %s", dpy, e->error_code, strerror(errno)); #endif @@ -256,7 +216,7 @@ static XIOErrorHandler origIOErrorHandler = NULL; static int x11IOErrorHandler(Display *dpy) { fprintf(stderr, "Nativewindow X11 IOError: Display %p (%s): %s\n", dpy, XDisplayName(NULL), strerror(errno)); - // _FatalError(x11ErrorHandlerJNIEnv, "Nativewindow X11 IOError: Display %p (%s): %s", dpy, XDisplayName(NULL), strerror(errno)); + // NativewindowCommon_FatalError(x11ErrorHandlerJNIEnv, "Nativewindow X11 IOError: Display %p (%s): %s", dpy, XDisplayName(NULL), strerror(errno)); if(NULL!=origIOErrorHandler) { origIOErrorHandler(dpy); } @@ -315,7 +275,7 @@ Java_com_jogamp_nativewindow_impl_x11_X11Lib_XGetVisualInfo1__JJLjava_nio_ByteBu jobject jbyteSource; jobject jbyteCopy; if(0==arg0) { - _FatalError(env, "invalid display connection.."); + NativewindowCommon_FatalError(env, "invalid display connection.."); } if (arg2 != NULL) { _ptr2 = (XVisualInfo *) (((char*) (*env)->GetDirectBufferAddress(env, arg2)) + 0); @@ -344,7 +304,7 @@ JNIEXPORT jlong JNICALL Java_com_jogamp_nativewindow_impl_x11_X11Lib_DefaultVisualID(JNIEnv *env, jclass _unused, jlong display, jint screen) { jlong r; if(0==display) { - _FatalError(env, "invalid display connection.."); + NativewindowCommon_FatalError(env, "invalid display connection.."); } x11ErrorHandlerEnable((Display *) (intptr_t) display, 1, env); r = (jlong) XVisualIDFromVisual( DefaultVisual( (Display*) (intptr_t) display, screen ) ); @@ -360,7 +320,7 @@ Java_com_jogamp_nativewindow_impl_x11_X11Lib_DefaultVisualID(JNIEnv *env, jclass JNIEXPORT void JNICALL Java_com_jogamp_nativewindow_impl_x11_X11Lib_XLockDisplay__J(JNIEnv *env, jclass _unused, jlong display) { if(0==display) { - _FatalError(env, "invalid display connection.."); + NativewindowCommon_FatalError(env, "invalid display connection.."); } XLockDisplay((Display *) (intptr_t) display); } @@ -373,7 +333,7 @@ Java_com_jogamp_nativewindow_impl_x11_X11Lib_XLockDisplay__J(JNIEnv *env, jclass JNIEXPORT void JNICALL Java_com_jogamp_nativewindow_impl_x11_X11Lib_XUnlockDisplay__J(JNIEnv *env, jclass _unused, jlong display) { if(0==display) { - _FatalError(env, "invalid display connection.."); + NativewindowCommon_FatalError(env, "invalid display connection.."); } XUnlockDisplay((Display *) (intptr_t) display); } @@ -387,7 +347,7 @@ JNIEXPORT jint JNICALL Java_com_jogamp_nativewindow_impl_x11_X11Lib_XCloseDisplay__J(JNIEnv *env, jclass _unused, jlong display) { int _res; if(0==display) { - _FatalError(env, "invalid display connection.."); + NativewindowCommon_FatalError(env, "invalid display connection.."); } x11ErrorHandlerEnable((Display *) (intptr_t) display, 1, env); _res = XCloseDisplay((Display *) (intptr_t) display); @@ -420,12 +380,12 @@ JNIEXPORT jlong JNICALL Java_com_jogamp_nativewindow_impl_x11_X11Lib_CreateDummy Screen* scrn; if(NULL==dpy) { - _FatalError(env, "invalid display connection.."); + NativewindowCommon_FatalError(env, "invalid display connection.."); return 0; } if(visualID<0) { - _throwNewRuntimeException(NULL, env, "invalid VisualID .."); + NativewindowCommon_throwNewRuntimeException(env, "invalid VisualID .."); return 0; } @@ -450,7 +410,7 @@ JNIEXPORT jlong JNICALL Java_com_jogamp_nativewindow_impl_x11_X11Lib_CreateDummy if (visual==NULL) { x11ErrorHandlerEnable(dpy, 0, env); - _throwNewRuntimeException(dpy, env, "could not query Visual by given VisualID, bail out!"); + NativewindowCommon_throwNewRuntimeException(env, "could not query Visual by given VisualID, bail out!"); return 0; } @@ -515,7 +475,7 @@ JNIEXPORT void JNICALL Java_com_jogamp_nativewindow_impl_x11_X11Lib_DestroyDummy Window w = (Window) window; if(NULL==dpy) { - _throwNewRuntimeException(NULL, env, "invalid display connection.."); + NativewindowCommon_throwNewRuntimeException(env, "invalid display connection.."); return; } diff --git a/src/newt/classes/com/jogamp/newt/impl/windows/WindowsDisplay.java b/src/newt/classes/com/jogamp/newt/impl/windows/WindowsDisplay.java index 372859208..0556fe792 100644 --- a/src/newt/classes/com/jogamp/newt/impl/windows/WindowsDisplay.java +++ b/src/newt/classes/com/jogamp/newt/impl/windows/WindowsDisplay.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved. + * Copyright (c) 2010 JogAmp Community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -33,73 +34,61 @@ package com.jogamp.newt.impl.windows; -import javax.media.nativewindow.*; -import javax.media.nativewindow.windows.*; -import com.jogamp.newt.*; -import com.jogamp.newt.impl.*; +import com.jogamp.newt.impl.DisplayImpl; +import com.jogamp.newt.impl.NEWTJNILibLoader; +import javax.media.nativewindow.AbstractGraphicsDevice; +import javax.media.nativewindow.NativeWindowException; +import javax.media.nativewindow.windows.RegisteredClassFactory; +import javax.media.nativewindow.windows.RegisteredClass; +import javax.media.nativewindow.windows.WindowsGraphicsDevice; public class WindowsDisplay extends DisplayImpl { - protected static final String WINDOW_CLASS_NAME = "NewtWindowClass"; - private static int windowClassAtom; - private static long hInstance; + private static final String newtClassBaseName = "_newt_clazz" ; + private static RegisteredClassFactory sharedClassFactory; static { NEWTJNILibLoader.loadNEWT(); if (!WindowsWindow.initIDs0()) { throw new NativeWindowException("Failed to initialize WindowsWindow jmethodIDs"); - } + } + sharedClassFactory = new RegisteredClassFactory(newtClassBaseName, WindowsWindow.getNewtWndProc0()); } public static void initSingleton() { // just exist to ensure static init has been run } + private RegisteredClass sharedClass; public WindowsDisplay() { } protected void createNativeImpl() { + sharedClass = sharedClassFactory.getSharedClass(); aDevice = new WindowsGraphicsDevice(AbstractGraphicsDevice.DEFAULT_UNIT); } protected void closeNativeImpl() { - // Can't do .. only at application shutdown - // UnregisterWindowClass0(getWindowClassAtom(), getHInstance()); + sharedClassFactory.releaseSharedClass(); } protected void dispatchMessagesNative() { DispatchMessages0(); } - protected static synchronized int getWindowClassAtom() { - if(0 == windowClassAtom) { - windowClassAtom = RegisterWindowClass0(WINDOW_CLASS_NAME, getHInstance()); - if (0 == windowClassAtom) { - throw new NativeWindowException("Error while registering window class"); - } - } - return windowClassAtom; + protected long getHInstance() { + return sharedClass.getHandle(); } - protected static synchronized long getHInstance() { - if(0 == hInstance) { - hInstance = LoadLibraryW0("newt"); - if (0 == hInstance) { - throw new NativeWindowException("Error finding HINSTANCE for \"newt\""); - } - } - return hInstance; + protected String getWindowClassName() { + return sharedClass.getName(); } //---------------------------------------------------------------------- // Internals only // - private static native long LoadLibraryW0(String libraryName); - private static native int RegisterWindowClass0(String windowClassName, long hInstance); - private static native void UnregisterWindowClass0(int wndClassAtom, long hInstance); - private static native void DispatchMessages0(); } diff --git a/src/newt/classes/com/jogamp/newt/impl/windows/WindowsWindow.java b/src/newt/classes/com/jogamp/newt/impl/windows/WindowsWindow.java index d26f4dc82..eb6539c06 100644 --- a/src/newt/classes/com/jogamp/newt/impl/windows/WindowsWindow.java +++ b/src/newt/classes/com/jogamp/newt/impl/windows/WindowsWindow.java @@ -97,9 +97,8 @@ public class WindowsWindow extends WindowImpl { if (config == null) { throw new NativeWindowException("Error choosing GraphicsConfiguration creating window: "+this); } - setWindowHandle(CreateWindow0(getParentWindowHandle(), - display.getWindowClassAtom(), display.WINDOW_CLASS_NAME, display.getHInstance(), - 0, undecorated, x, y, width, height)); + setWindowHandle(CreateWindow0(display.getHInstance(), display.getWindowClassName(), display.getWindowClassName(), + getParentWindowHandle(), 0, undecorated, x, y, width, height)); if (getWindowHandle() == 0) { throw new NativeWindowException("Error creating window"); } @@ -128,7 +127,7 @@ public class WindowsWindow extends WindowImpl { } if(windowHandleClose != 0) { try { - DestroyWindow0(windowHandleClose); + GDI.DestroyWindow(windowHandleClose); } catch (Throwable t) { if(DEBUG_IMPLEMENTATION) { Exception e = new Exception("Warning: closeNativeImpl failed - "+Thread.currentThread().getName(), t); @@ -177,12 +176,11 @@ public class WindowsWindow extends WindowImpl { // Internals only // protected static native boolean initIDs0(); - private native long CreateWindow0(long parentWindowHandle, - int wndClassAtom, String wndName, - long hInstance, long visualID, - boolean isUndecorated, - int x, int y, int width, int height); - private native void DestroyWindow0(long windowHandle); + protected static native long getNewtWndProc0(); + + private native long CreateWindow0(long hInstance, String wndClassName, String wndName, + long parentWindowHandle, long visualID, boolean isUndecorated, + int x, int y, int width, int height); private native long MonitorFromWindow0(long windowHandle); private native void setVisible0(long windowHandle, boolean visible, boolean top, int x, int y, int width, int height); private native void reconfigureWindow0(long parentWindowHandle, long windowHandle, diff --git a/src/newt/classes/com/jogamp/newt/impl/x11/X11Display.java b/src/newt/classes/com/jogamp/newt/impl/x11/X11Display.java index 1aaffacb5..af05c0356 100644 --- a/src/newt/classes/com/jogamp/newt/impl/x11/X11Display.java +++ b/src/newt/classes/com/jogamp/newt/impl/x11/X11Display.java @@ -76,9 +76,8 @@ public class X11Display extends DisplayImpl { throw e; } aDevice = new X11GraphicsDevice(handle, AbstractGraphicsDevice.DEFAULT_UNIT, NativeWindowFactory.getNullToolkitLock()); - // aDevice = new X11GraphicsDevice(handle, NativeWindowFactory.createDefaultToolkitLockNoAWT(NativeWindowFactory.TYPE_X11, handle)); - // aDevice = new X11GraphicsDevice(handle); - + // aDevice = new X11GraphicsDevice(handle, AbstractGraphicsDevice.DEFAULT_UNIT, NativeWindowFactory.createDefaultToolkitLockNoAWT(NativeWindowFactory.TYPE_X11, handle)); + // aDevice = new X11GraphicsDevice(handle, AbstractGraphicsDevice.DEFAULT_UNIT); } protected void closeNativeImpl() { diff --git a/src/newt/native/WindowsWindow.c b/src/newt/native/WindowsWindow.c index 0a675157d..cafdaeef8 100644 --- a/src/newt/native/WindowsWindow.c +++ b/src/newt/native/WindowsWindow.c @@ -1047,71 +1047,6 @@ JNIEXPORT void JNICALL Java_com_jogamp_newt_impl_windows_WindowsDisplay_Dispatch } /* - * Class: com_jogamp_newt_impl_windows_WindowsDisplay - * Method: LoadLibraryW - * Signature: (Ljava/lang/String;)J - */ -JNIEXPORT jlong JNICALL Java_com_jogamp_newt_impl_windows_WindowsDisplay_LoadLibraryW0 - (JNIEnv *env, jclass clazz, jstring dllName) -{ - jchar* _dllName = NewtCommon_GetNullTerminatedStringChars(env, dllName); - HMODULE lib = LoadLibraryW(_dllName); - free(_dllName); - return (jlong) (intptr_t) lib; -} - -/* - * Class: com_jogamp_newt_impl_windows_WindowsDisplay - * Method: RegisterWindowClass - * Signature: (Ljava/lang/String;J)I - */ -JNIEXPORT jint JNICALL Java_com_jogamp_newt_impl_windows_WindowsDisplay_RegisterWindowClass0 - (JNIEnv *env, jclass clazz, jstring wndClassName, jlong hInstance) -{ - ATOM res; - WNDCLASS wc; -#ifndef UNICODE - const char* _wndClassName = NULL; -#endif - - /* register class */ - wc.style = CS_HREDRAW | CS_VREDRAW; - wc.lpfnWndProc = (WNDPROC)wndProc; - wc.cbClsExtra = 0; - wc.cbWndExtra = 0; - /* This cast is legal because the HMODULE for a DLL is the same as - its HINSTANCE -- see MSDN docs for DllMain */ - wc.hInstance = (HINSTANCE) (intptr_t) hInstance; - wc.hIcon = NULL; - wc.hCursor = LoadCursor( NULL, IDC_ARROW); - wc.hbrBackground = GetStockObject(BLACK_BRUSH); - wc.lpszMenuName = NULL; -#ifdef UNICODE - wc.lpszClassName = NewtCommon_GetNullTerminatedStringChars(env, wndClassName); -#else - _wndClassName = (*env)->GetStringUTFChars(env, wndClassName, NULL); - wc.lpszClassName = strdup(_wndClassName); - (*env)->ReleaseStringUTFChars(env, wndClassName, _wndClassName); -#endif - res = RegisterClass(&wc); - - free((void *)wc.lpszClassName); - - return (jint)res; -} - -/* - * Class: com_jogamp_newt_impl_windows_WindowsDisplay - * Method: CleanupWindowResources - * Signature: (java/lang/String;J)V - */ -JNIEXPORT void JNICALL Java_com_jogamp_newt_impl_windows_WindowsDisplay_UnregisterWindowClass0 - (JNIEnv *env, jclass clazz, jint wndClassAtom, jlong hInstance) -{ - UnregisterClass(MAKEINTATOM(wndClassAtom), (HINSTANCE) (intptr_t) hInstance); -} - -/* * Class: com_jogamp_newt_impl_windows_WindowsScreen * Method: getWidthImpl * Signature: (I)I @@ -1363,15 +1298,27 @@ JNIEXPORT jboolean JNICALL Java_com_jogamp_newt_impl_windows_WindowsWindow_initI /* * Class: com_jogamp_newt_impl_windows_WindowsWindow + * Method: getNewtWndProc0 + * Signature: ()J + */ +JNIEXPORT jlong JNICALL Java_com_jogamp_newt_impl_windows_WindowsWindow_getNewtWndProc0 + (JNIEnv *env, jclass clazz) +{ + return (jlong) (intptr_t) wndProc; +} + +/* + * Class: com_jogamp_newt_impl_windows_WindowsWindow * Method: CreateWindow - * Signature: (JILjava/lang/String;JJZIIII)J */ JNIEXPORT jlong JNICALL Java_com_jogamp_newt_impl_windows_WindowsWindow_CreateWindow0 - (JNIEnv *env, jobject obj, jlong parent, jint wndClassAtom, jstring jWndName, jlong hInstance, jlong visualID, - jboolean bIsUndecorated, - jint jx, jint jy, jint defaultWidth, jint defaultHeight) + (JNIEnv *env, jobject obj, + jlong hInstance, jstring jWndClassName, jstring jWndName, + jlong parent, jlong visualID, jboolean bIsUndecorated, + jint jx, jint jy, jint defaultWidth, jint defaultHeight) { HWND parentWindow = (HWND) (intptr_t) parent; + const TCHAR* wndClassName = NULL; const TCHAR* wndName = NULL; DWORD windowStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE | WS_TABSTOP; int x=(int)jx, y=(int)jy; @@ -1379,8 +1326,10 @@ JNIEXPORT jlong JNICALL Java_com_jogamp_newt_impl_windows_WindowsWindow_CreateWi HWND window = NULL; #ifdef UNICODE + wndClassName = NewtCommon_GetNullTerminatedStringChars(env, jWndClassName); wndName = NewtCommon_GetNullTerminatedStringChars(env, jWndName); #else + wndClassName = (*env)->GetStringUTFChars(env, jWndClassName, NULL); wndName = (*env)->GetStringUTFChars(env, jWndName, NULL); #endif @@ -1400,13 +1349,13 @@ JNIEXPORT jlong JNICALL Java_com_jogamp_newt_impl_windows_WindowsWindow_CreateWi (void) visualID; // FIXME: use the visualID .. - window = CreateWindow(MAKEINTATOM(wndClassAtom), wndName, windowStyle, + window = CreateWindow(wndClassName, wndName, windowStyle, x, y, width, height, parentWindow, NULL, (HINSTANCE) (intptr_t) hInstance, NULL); - DBG_PRINT("*** WindowsWindow: CreateWindow thread 0xX, parent %p, window %p, %d/%d %dx%d\n", + DBG_PRINT("*** WindowsWindow: CreateWindow thread 0x%X, parent %p, window %p, %d/%d %dx%d\n", (int)GetCurrentThreadId(), parentWindow, window, x, y, width, height); if (NULL == window) { @@ -1427,8 +1376,10 @@ JNIEXPORT jlong JNICALL Java_com_jogamp_newt_impl_windows_WindowsWindow_CreateWi } #ifdef UNICODE + free((void*) wndClassName); free((void*) wndName); #else + (*env)->ReleaseStringUTFChars(env, jWndClassName, wndClassName); (*env)->ReleaseStringUTFChars(env, jWndName, wndName); #endif @@ -1437,18 +1388,6 @@ JNIEXPORT jlong JNICALL Java_com_jogamp_newt_impl_windows_WindowsWindow_CreateWi /* * Class: com_jogamp_newt_impl_windows_WindowsWindow - * Method: DestroyWindow - * Signature: (J)V - */ -JNIEXPORT void JNICALL Java_com_jogamp_newt_impl_windows_WindowsWindow_DestroyWindow0 - (JNIEnv *env, jobject obj, jlong window) -{ - DBG_PRINT("*** WindowsWindow: DestroyWindow thread 0x%X, window %p\n", (int)GetCurrentThreadId(), window); - DestroyWindow((HWND) (intptr_t) window); -} - -/* - * Class: com_jogamp_newt_impl_windows_WindowsWindow * Method: MonitorFromWindow * Signature: (J)J */ |