public void destroy() { synchronized ( GlobalLock.lock ) { if ( context == null ) return; try { releaseContext(); context.forceDestroy(); context = null; if ( peer_info != null ) { peer_info.destroy(); peer_info = null; } } catch (LWJGLException e) { LWJGLUtil.log("Exception occurred while destroying Drawable: " + e); } } }
public static int getNativeCursorCapabilities() { if (LWJGLUtil.getPlatform() != LWJGLUtil.PLATFORM_MACOSX || LWJGLUtil.isMacOSXEqualsOrBetterThan(10, 4)) { int cursor_colors = Toolkit.getDefaultToolkit().getMaximumCursorColors(); boolean supported = cursor_colors >= Short.MAX_VALUE && getMaxCursorSize() > 0; int caps = supported ? org.lwjgl.input.Cursor.CURSOR_8_BIT_ALPHA | org.lwjgl.input.Cursor.CURSOR_ONE_BIT_TRANSPARENCY: 0 | org.lwjgl.input.Cursor.CURSOR_ANIMATION; return caps; } else { /* Return no capability in Mac OS X 10.3 and earlier , as there are two unsolved bugs (both reported to apple along with minimal test case): 1. When a custom cursor (or some standard) java.awt.Cursor is assigned to a Componennt, it is reset to the default pointer cursor when the window is de- activated and the re-activated. The Cursor can not be reset to the custom cursor, with another setCursor. 2. When the cursor is moving in the top pixel row (y = 0 in AWT coordinates) in fullscreen mode, no mouse moved events are reported, even though mouse pressed/released and dragged events are reported */ return 0; } }
/** * Use reflection to access the JDK 1.5 pointer location, if possible and * only if the given component is on the same screen as the cursor. Return * null otherwise. */ private static Point getPointerLocation(final Component component) { try { final GraphicsConfiguration config = component.getGraphicsConfiguration(); if (config != null) { PointerInfo pointer_info = AccessController.doPrivileged(new PrivilegedExceptionAction<PointerInfo>() { public PointerInfo run() throws Exception { return MouseInfo.getPointerInfo(); } }); GraphicsDevice device = pointer_info.getDevice(); if (device == config.getDevice()) { return pointer_info.getLocation(); } return null; } } catch (Exception e) { LWJGLUtil.log("Failed to query pointer location: " + e.getCause()); } return null; }
public final synchronized ByteBuffer lockAndGetHandle() throws LWJGLException { Thread this_thread = Thread.currentThread(); while (locking_thread != null && locking_thread != this_thread) { try { wait(); } catch (InterruptedException e) { LWJGLUtil.log("Interrupted while waiting for PeerInfo lock: " + e); } } if (lock_count == 0) { locking_thread = this_thread; doLockAndInitHandle(); } lock_count++; return getHandle(); }
private static boolean isXrandrSupported() { if (Display.getPrivilegedBoolean("LWJGL_DISABLE_XRANDR")) return false; lockAWT(); try { incDisplay(); try { return nIsXrandrSupported(getDisplay()); } finally { decDisplay(); } } catch (LWJGLException e) { LWJGLUtil.log("Got exception while querying Xrandr support: " + e); return false; } finally { unlockAWT(); } }
private static boolean isXF86VidModeSupported() { lockAWT(); try { incDisplay(); try { return nIsXF86VidModeSupported(getDisplay()); } finally { decDisplay(); } } catch (LWJGLException e) { LWJGLUtil.log("Got exception while querying XF86VM support: " + e); return false; } finally { unlockAWT(); } }
private static boolean isNetWMFullscreenSupported() throws LWJGLException { if (Display.getPrivilegedBoolean("LWJGL_DISABLE_NETWM")) return false; lockAWT(); try { incDisplay(); try { return nIsNetWMFullscreenSupported(getDisplay(), getDefaultScreen()); } finally { decDisplay(); } } catch (LWJGLException e) { LWJGLUtil.log("Got exception while querying NetWM support: " + e); return false; } finally { unlockAWT(); } }
public void destroyWindow() { lockAWT(); try { if (parent != null) { parent.removeFocusListener(focus_listener); } try { setNativeCursor(null); } catch (LWJGLException e) { LWJGLUtil.log("Failed to reset cursor: " + e.getMessage()); } nDestroyCursor(getDisplay(), blank_cursor); blank_cursor = None; ungrabKeyboard(); nDestroyWindow(getDisplay(), getWindow()); decDisplay(); if ( current_window_mode != WINDOWED ) Compiz.setLegacyFullscreenSupport(false); } finally { unlockAWT(); } }
public void resetDisplayMode() { lockAWT(); try { if( current_displaymode_extension == XRANDR && savedXrandrConfig.length > 0 ) { AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { XRandR.setConfiguration( savedXrandrConfig ); return null; } }); } else { switchDisplayMode(saved_mode); } if (isXF86VidModeSupported()) doSetGamma(saved_gamma); Compiz.setLegacyFullscreenSupport(false); } catch (LWJGLException e) { LWJGLUtil.log("Caught exception while resetting mode: " + e); } finally { unlockAWT(); } }
public int getMinCursorSize() { lockAWT(); try { incDisplay(); try { return nGetMinCursorSize(getDisplay(), getWindow()); } finally { decDisplay(); } } catch (LWJGLException e) { LWJGLUtil.log("Exception occurred in getMinCursorSize: " + e); return 0; } finally { unlockAWT(); } }
public int getMaxCursorSize() { lockAWT(); try { incDisplay(); try { return nGetMaxCursorSize(getDisplay(), getWindow()); } finally { decDisplay(); } } catch (LWJGLException e) { LWJGLUtil.log("Exception occurred in getMaxCursorSize: " + e); return 0; } finally { unlockAWT(); } }
public int getPbufferCapabilities() { lockAWT(); try { incDisplay(); try { return nGetPbufferCapabilities(getDisplay(), getDefaultScreen()); } finally { decDisplay(); } } catch (LWJGLException e) { LWJGLUtil.log("Exception occurred in getPbufferCapabilities: " + e); return 0; } finally { unlockAWT(); } }
/** * Helper method to ContextCapabilities. It will try to initialize the native stubs, * and remove the given extension name from the extension set if the initialization fails. */ static void initNativeStubs(final Class<?> extension_class, Set supported_extensions, String ext_name) { resetNativeStubs(extension_class); if ( supported_extensions.contains(ext_name) ) { try { AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { public Object run() throws Exception { Method init_stubs_method = extension_class.getDeclaredMethod("initNativeStubs"); init_stubs_method.invoke(null); return null; } }); } catch (Exception e) { LWJGLUtil.log("Failed to initialize extension " + extension_class + " - exception: " + e); supported_extensions.remove(ext_name); } } }
/** * Returns the entire list of possible fullscreen display modes as an array, in no * particular order. Although best attempts to filter out invalid modes are done, any * given mode is not guaranteed to be available nor is it guaranteed to be within the * current monitor specs (this is especially a problem with the frequency parameter). * Furthermore, it is not guaranteed that create() will detect an illegal display mode. * <p/> * The only certain way to check * is to call create() and make sure it works. * Only non-palette-indexed modes are returned (ie. bpp will be 16, 24, or 32). * Only DisplayModes from this call can be used when the Display is in fullscreen * mode. * * @return an array of all display modes the system reckons it can handle. */ public static DisplayMode[] getAvailableDisplayModes() throws LWJGLException { synchronized ( GlobalLock.lock ) { DisplayMode[] unfilteredModes = display_impl.getAvailableDisplayModes(); if ( unfilteredModes == null ) { return new DisplayMode[0]; } // We'll use a HashSet to filter out the duplicated modes HashSet<DisplayMode> modes = new HashSet<DisplayMode>(unfilteredModes.length); modes.addAll(Arrays.asList(unfilteredModes)); DisplayMode[] filteredModes = new DisplayMode[modes.size()]; modes.toArray(filteredModes); LWJGLUtil.log("Removed " + (unfilteredModes.length - filteredModes.length) + " duplicate displaymodes"); return filteredModes; } }
/** * Find a proper GraphicsConfiguration from the given GraphicsDevice and PixelFormat. * * @return The GraphicsConfiguration corresponding to a visual that matches the pixel format. */ public GraphicsConfiguration findConfiguration(GraphicsDevice device, PixelFormat pixel_format) throws LWJGLException { try { int screen = getScreenFromDevice(device); int visual_id_matching_format = findVisualIDFromFormat(screen, pixel_format); GraphicsConfiguration[] configurations = device.getConfigurations(); for ( GraphicsConfiguration configuration : configurations ) { int visual_id = getVisualIDFromConfiguration(configuration); if ( visual_id == visual_id_matching_format ) return configuration; } } catch (LWJGLException e) { LWJGLUtil.log("Got exception while trying to determine configuration: " + e); } return null; // In case we failed to locate the visual, or if we're not on a SUN JDK }
public void update() { nUpdate(); if ( !isFocused && parent != null && parent.isFocusOwner() ) { KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner(); setFocus(getHwnd()); } if (redoMakeContextCurrent) { redoMakeContextCurrent = false; /** * WORKAROUND: * Making the context current (redundantly) when the window * is maximized helps some gfx cards recover from fullscreen */ try { Context context = ((DrawableLWJGL)Display.getDrawable()).getContext(); if (context != null && context.isCurrent()) context.makeCurrent(); } catch (LWJGLException e) { LWJGLUtil.log("Exception occurred while trying to make context current: " + e); } } }
/** * Initializes ALC11, including any extensions * @return true if initialization was successfull */ static boolean initialize() { try { IntBuffer ib = BufferUtils.createIntBuffer(2); ALC10.alcGetInteger(AL.getDevice(), ALC10.ALC_MAJOR_VERSION, ib); ib.position(1); ALC10.alcGetInteger(AL.getDevice(), ALC10.ALC_MINOR_VERSION, ib); int major = ib.get(0); int minor = ib.get(1); // checking for version 1.x+ if(major >= 1) { // checking for version 1.1+ if(major > 1 || minor >= 1) { ALC11.initNativeStubs(); AL11.initNativeStubs(); } } } catch (LWJGLException le) { LWJGLUtil.log("failed to initialize ALC11: " + le); return false; } return true; }
/** * Calculates the number of bytes in the specified cl_mem image region. * This implementation assumes 1 byte per element, because we cannot the * image type. * * @param region the image region * @param row_pitch the row pitch * @param slice_pitch the slice pitch * * @return the region size in bytes */ static int calculateImageSize(final PointerBuffer region, long row_pitch, long slice_pitch) { if ( !LWJGLUtil.CHECKS ) return 0; final long w = region.get(0); final long h = region.get(1); final long d = region.get(2); if ( LWJGLUtil.DEBUG && (w < 1 || h < 1 || d < 1) ) throw new IllegalArgumentException("Invalid cl_mem image region dimensions: " + w + " x " + h + " x " + d); if ( row_pitch == 0 ) row_pitch = w; else if ( LWJGLUtil.DEBUG && row_pitch < w ) throw new IllegalArgumentException("Invalid row pitch specified: " + row_pitch); if ( slice_pitch == 0 ) slice_pitch = row_pitch * h; else if ( LWJGLUtil.DEBUG && slice_pitch < (row_pitch * h) ) throw new IllegalArgumentException("Invalid slice pitch specified: " + slice_pitch); return (int)(slice_pitch * d); }
/** * Calculates the number of bytes in the specified 2D image. * * @param format the cl_image_format struct * @param w the image width * @param h the image height * @param row_pitch the image row pitch * * @return the 2D image size in bytes */ static int calculateImage2DSize(final ByteBuffer format, final long w, final long h, long row_pitch) { if ( !LWJGLUtil.CHECKS ) return 0; if ( LWJGLUtil.DEBUG && (w < 1 || h < 1) ) throw new IllegalArgumentException("Invalid 2D image dimensions: " + w + " x " + h); final int elementSize = getElementSize(format); if ( row_pitch == 0 ) row_pitch = w * elementSize; else if ( LWJGLUtil.DEBUG && ((row_pitch < w * elementSize) || (row_pitch % elementSize != 0)) ) throw new IllegalArgumentException("Invalid image_row_pitch specified: " + row_pitch); return (int)(row_pitch * h); }
/** * Calculates the number of bytes in the specified 3D image. * * @param format the cl_image_format struct * @param w the image width * @param h the image height * @param d the image depth * @param row_pitch the image row pitch * @param slice_pitch the image slice pitch * * @return the 3D image size in bytes */ static int calculateImage3DSize(final ByteBuffer format, final long w, final long h, final long d, long row_pitch, long slice_pitch) { if ( !LWJGLUtil.CHECKS ) return 0; if ( LWJGLUtil.DEBUG && (w < 1 || h < 1 || d < 2) ) throw new IllegalArgumentException("Invalid 3D image dimensions: " + w + " x " + h + " x " + d); final int elementSize = getElementSize(format); if ( row_pitch == 0 ) row_pitch = w * elementSize; else if ( LWJGLUtil.DEBUG && ((row_pitch < w * elementSize) || (row_pitch % elementSize != 0)) ) throw new IllegalArgumentException("Invalid image_row_pitch specified: " + row_pitch); if ( slice_pitch == 0 ) slice_pitch = row_pitch * h; else if ( LWJGLUtil.DEBUG && ((row_pitch < row_pitch * h) || (slice_pitch % row_pitch != 0)) ) throw new IllegalArgumentException("Invalid image_slice_pitch specified: " + row_pitch); return (int)(slice_pitch * d); }
/** * Returns the number of channels in the specified cl_channel_order. * * @param channelOrder the cl_channel_order * * @return the number of channels */ private static int getChannelCount(final int channelOrder) { switch ( channelOrder ) { case CL_R: case CL_A: case CL_INTENSITY: case CL_LUMINANCE: case CL_Rx: return 1; case CL_RG: case CL_RA: case CL_RGx: return 2; case CL_RGB: case CL_RGBx: return 3; case CL_RGBA: case CL_BGRA: case CL_ARGB: return 4; default: throw new IllegalArgumentException("Invalid cl_channel_order specified: " + LWJGLUtil.toHexString(channelOrder)); } }
/** * Returns the number of bytes in the specified cl_channel_type. * * @param channelType the cl_channel_type * * @return the number of bytes */ private static int getChannelSize(final int channelType) { switch ( channelType ) { case CL_SNORM_INT8: case CL_UNORM_INT8: case CL_SIGNED_INT8: case CL_UNSIGNED_INT8: return 1; case CL_SNORM_INT16: case CL_UNORM_INT16: case CL_UNORM_SHORT_565: case CL_UNORM_SHORT_555: case CL_SIGNED_INT16: case CL_UNSIGNED_INT16: case CL_HALF_FLOAT: return 2; case CL_UNORM_INT_101010: case CL_SIGNED_INT32: case CL_UNSIGNED_INT32: case CL_FLOAT: return 4; default: throw new IllegalArgumentException("Invalid cl_channel_type specified: " + LWJGLUtil.toHexString(channelType)); } }
/** * Tests debug mode */ private void testDebug() { System.out.println("==== Test Debug ===="); if (LWJGLUtil.DEBUG) { LWJGLUtil.log("Debug is enabled, you should now see output from LWJGL during the following tests."); } else { System.out.println("Debug is not enabled. Please set the org.lwjgl.Sys.debug property to true to enable debugging"); System.out.println("Example:\n java -Dorg.lwjgl.util.Debug=true ..."); System.out.println("You will not see any debug output in the following tests."); } // get some display modes, to force some debug info try { Display.getAvailableDisplayModes(); } catch (LWJGLException e) { throw new RuntimeException(e); } System.out.println("---- Test Debug ----\n"); }
private void saveWindowParams() { int width = Display.getWidth(); int height = Display.getHeight(); int x = Display.getX(); int y = Display.getY(); //FIXME by some reason actual window position shifted by 6 pixels on Windows (by 12 at y when maximized) if (LWJGLUtil.getPlatform() == LWJGLUtil.PLATFORM_WINDOWS) { x += 6; y += 6; } Preferences prefs = Gdx.app.getPreferences("window_params.xml"); prefs.putInteger("x", x); prefs.putInteger("y", y); prefs.putInteger("width", width); prefs.putInteger("height", height); prefs.flush(); }
public static void setup(MappedObject mo, ByteBuffer buffer, int align, int sizeof) { if ( LWJGLUtil.CHECKS && mo.baseAddress != 0L ) throw new IllegalStateException("this method should not be called by user-code"); if ( LWJGLUtil.CHECKS && !buffer.isDirect() ) throw new IllegalArgumentException("bytebuffer must be direct"); mo.preventGC = buffer; if ( LWJGLUtil.CHECKS && align <= 0 ) throw new IllegalArgumentException("invalid alignment"); if ( LWJGLUtil.CHECKS && (sizeof <= 0 || sizeof % align != 0) ) throw new IllegalStateException("sizeof not a multiple of alignment"); long addr = MemoryUtil.getAddress(buffer); if ( LWJGLUtil.CHECKS && addr % align != 0 ) throw new IllegalStateException("buffer address not aligned on " + align + " bytes"); mo.baseAddress = mo.viewAddress = addr; }
public void update() { nUpdate(); if ( !isFocused && parent != null && parent_focused.compareAndSet(true, false) ) { setFocus(getHwnd()); } if (redoMakeContextCurrent) { redoMakeContextCurrent = false; /** * WORKAROUND: * Making the context current (redundantly) when the window * is maximized helps some gfx cards recover from fullscreen */ try { Context context = ((DrawableLWJGL)Display.getDrawable()).getContext(); if (context != null && context.isCurrent()) context.makeCurrent(); } catch (LWJGLException e) { LWJGLUtil.log("Exception occurred while trying to make context current: " + e); } } }