Linux: More native code refactoring

This commit is contained in:
Elias Naur 2005-11-22 13:53:13 +00:00
parent 7469d59dbf
commit 2bc0988c98
4 changed files with 95 additions and 68 deletions

View File

@ -95,7 +95,7 @@ final class LinuxDisplay implements DisplayImplementation {
if (isXF86VidModeSupported())
return nGetCurrentGammaRamp();
else
return BufferUtils.createByteBuffer(0);
return null;
} finally {
decDisplay();
}
@ -105,29 +105,58 @@ final class LinuxDisplay implements DisplayImplementation {
}
private static native ByteBuffer nGetCurrentGammaRamp() throws LWJGLException;
private static int getBestDisplayModeExtension() throws LWJGLException {
private static int getBestDisplayModeExtension() {
int result;
if (isXrandrSupported()) {
LWJGLUtil.log("Using Xrandr for display mode switching");
result = XRANDR;
} else if (isXF86VidModeSupported()) {
LWJGLUtil.log("Using XF86VidMode for display mode switching");
result = XF86VIDMODE;
} else {
LWJGLUtil.log("No display mode extensions available");
result = NONE;
}
return result;
}
private static boolean isXrandrSupported() {
if (System.getenv("LWJGL_DISABLE_XRANDR") != null)
return false;
lockAWT();
try {
incDisplay();
int result;
if (System.getenv("LWJGL_DISABLE_XRANDR") == null && isXrandrSupported()) {
LWJGLUtil.log("Using Xrandr for display mode switching");
result = XRANDR;
} else if (isXF86VidModeSupported()) {
LWJGLUtil.log("Using XF86VidMode for display mode switching");
result = XF86VIDMODE;
} else {
LWJGLUtil.log("No display mode extensions available");
result = NONE;
try {
return nIsXrandrSupported();
} finally {
decDisplay();
}
decDisplay();
return result;
} catch (LWJGLException e) {
LWJGLUtil.log("Got exception while querying Xrandr support: " + e);
return false;
} finally {
unlockAWT();
}
}
private static native boolean isXrandrSupported();
private static native boolean isXF86VidModeSupported();
private static native boolean nIsXrandrSupported() throws LWJGLException;
private static boolean isXF86VidModeSupported() {
lockAWT();
try {
incDisplay();
try {
return nIsXF86VidModeSupported();
} finally {
decDisplay();
}
} catch (LWJGLException e) {
LWJGLUtil.log("Got exception while querying XF86VM support: " + e);
return false;
} finally {
unlockAWT();
}
}
private static native boolean nIsXF86VidModeSupported() throws LWJGLException;
private static boolean isNetWMFullscreenSupported() throws LWJGLException {
if (System.getenv("LWJGL_DISABLE_NETWM") != null)
@ -135,14 +164,19 @@ final class LinuxDisplay implements DisplayImplementation {
lockAWT();
try {
incDisplay();
boolean supported = nIsNetWMFullscreenSupported();
decDisplay();
return supported;
try {
return nIsNetWMFullscreenSupported();
} finally {
decDisplay();
}
} catch (LWJGLException e) {
LWJGLUtil.log("Got exception while querying NetWM support: " + e);
return false;
} finally {
unlockAWT();
}
}
private static native boolean nIsNetWMFullscreenSupported();
private static native boolean nIsNetWMFullscreenSupported() throws LWJGLException;
/* Since Xlib is not guaranteed to be thread safe, we need a way to synchronize LWJGL
* Xlib calls with AWT Xlib calls. Fortunately, JAWT implements Lock()/Unlock() to
@ -250,8 +284,8 @@ final class LinuxDisplay implements DisplayImplementation {
public void switchDisplayMode(DisplayMode mode) throws LWJGLException {
lockAWT();
try {
nSwitchDisplayMode(current_displaymode_extension, mode);
current_mode = mode;
nSwitchDisplayMode(current_displaymode_extension, current_mode);
} finally {
unlockAWT();
}
@ -261,27 +295,31 @@ final class LinuxDisplay implements DisplayImplementation {
public void resetDisplayMode() {
lockAWT();
try {
nResetDisplayMode(current_displaymode_extension, saved_gamma, saved_mode);
switchDisplayMode(saved_mode);
if (isXF86VidModeSupported())
doSetGamma(saved_gamma);
} catch (LWJGLException e) {
LWJGLUtil.log("Caught exception while resetting mode: " + e);
} finally {
unlockAWT();
}
}
private static native void nResetDisplayMode(int extension, ByteBuffer gamma_ramp, DisplayMode saved_mode) throws LWJGLException;
public int getGammaRampLength() {
if (!isXF86VidModeSupported())
return 0;
lockAWT();
try {
try {
incDisplay();
int length;
if (isXF86VidModeSupported()) {
length = nGetGammaRampLength();
} else
length = 0;
decDisplay();
return length;
try {
return nGetGammaRampLength();
} catch (LWJGLException e) {
LWJGLUtil.log("Got exception while querying gamma length: " + e);
return 0;
} finally {
decDisplay();
}
} catch (LWJGLException e) {
LWJGLUtil.log("Failed to get gamma ramp length: " + e);
return 0;
@ -290,18 +328,19 @@ final class LinuxDisplay implements DisplayImplementation {
unlockAWT();
}
}
private static native int nGetGammaRampLength();
private static native int nGetGammaRampLength() throws LWJGLException;
public void setGammaRamp(FloatBuffer gammaRamp) throws LWJGLException {
if (!isXF86VidModeSupported())
throw new LWJGLException("No gamma ramp support (Missing XF86VM extension)");
doSetGamma(convertToNativeRamp(gammaRamp));
}
private static void doSetGamma(ByteBuffer native_gamma) throws LWJGLException {
lockAWT();
try {
incDisplay();
boolean xf86_support = isXF86VidModeSupported();
decDisplay();
if (!xf86_support)
throw new LWJGLException("No gamma ramp support (Missing XF86VM extension)");
current_gamma = convertToNativeRamp(gammaRamp);
nSetGammaRamp(current_gamma);
nSetGammaRamp(native_gamma);
current_gamma = native_gamma;
} finally {
unlockAWT();
}

View File

@ -68,11 +68,11 @@ static bool getXF86VidModeVersion(JNIEnv *env, Display *disp, int *major, int *m
int event_base, error_base;
if (!XF86VidModeQueryExtension(disp, &event_base, &error_base)) {
printfDebugJava(env, "XF86VidMode extension not available");
throwException(env, "XF86VidMode extension not available");
return false;
}
if (!XF86VidModeQueryVersion(disp, major, minor)) {
printfDebugJava(env, "Could not query XF86VidMode version");
throwException(env, "Could not query XF86VidMode version");
return false;
}
printfDebugJava(env, "XF86VidMode extension version %i.%i", *major, *minor);
@ -83,11 +83,11 @@ static bool getXrandrVersion(JNIEnv *env, Display *disp, int *major, int *minor)
int event_base, error_base;
if (!XRRQueryExtension(disp, &event_base, &error_base)) {
printfDebugJava(env, "Xrandr extension not available");
throwException(env, "Xrandr extension not available");
return false;
}
if (!XRRQueryVersion(disp, major, minor)) {
printfDebugJava(env, "Could not query Xrandr version");
throwException(env, "Could not query Xrandr version");
return false;
}
printfDebugJava(env, "Xrandr extension version %i.%i", *major, *minor);
@ -108,12 +108,12 @@ static bool isXF86VidModeSupported(JNIEnv *env, Display *disp) {
return major_ver >= 2;
}
JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_LinuxDisplay_isXrandrSupported(JNIEnv *env, jclass unused) {
JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nIsXrandrSupported(JNIEnv *env, jclass unused) {
jboolean result = isXrandrSupported(env, getDisplay()) ? JNI_TRUE : JNI_FALSE;
return result;
}
JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_LinuxDisplay_isXF86VidModeSupported(JNIEnv *env, jclass unused) {
JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nIsXF86VidModeSupported(JNIEnv *env, jclass unused) {
jboolean result = isXF86VidModeSupported(env, getDisplay()) ? JNI_TRUE : JNI_FALSE;
return result;
}
@ -262,7 +262,7 @@ static bool setMode(JNIEnv *env, Display *disp, int screen, jint extension, int
static int getGammaRampLengthOfDisplay(JNIEnv *env, Display *disp, int screen) {
int ramp_size;
if (XF86VidModeGetGammaRampSize(disp, screen, &ramp_size) == False) {
printfDebugJava(env, "XF86VidModeGetGammaRampSize call failed");
throwException(env, "XF86VidModeGetGammaRampSize call failed");
return 0;
}
return ramp_size;
@ -304,6 +304,8 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nGetCurrentGammaRam
}
static void setGamma(JNIEnv *env, Display *disp, int screen, jobject ramp_buffer) {
if (ramp_buffer == NULL)
return;
unsigned short *ramp_ptr = (unsigned short *)(*env)->GetDirectBufferAddress(env, ramp_buffer);
jlong capacity = (*env)->GetDirectBufferCapacity(env, ramp_buffer);
int size = capacity/(sizeof(unsigned short)*3);
@ -314,7 +316,7 @@ static void setGamma(JNIEnv *env, Display *disp, int screen, jobject ramp_buffer
}
}
static void setGammaRamp(JNIEnv *env, int screen, jobject gamma_ramp_buffer) {
void setGammaRamp(JNIEnv *env, int screen, jobject gamma_ramp_buffer) {
Display * disp = XOpenDisplay(NULL);
if (disp == NULL) {
throwException(env, "Could not open display");
@ -324,7 +326,7 @@ static void setGammaRamp(JNIEnv *env, int screen, jobject gamma_ramp_buffer) {
XCloseDisplay(disp);
}
static bool switchDisplayMode(JNIEnv * env, int screen, jint extension, jobject mode) {
bool switchDisplayMode(JNIEnv * env, int screen, jint extension, jobject mode) {
if (mode == NULL) {
throwException(env, "mode must be non-null");
return false;
@ -350,18 +352,6 @@ static bool switchDisplayMode(JNIEnv * env, int screen, jint extension, jobject
return true;
}
void temporaryRestoreMode(JNIEnv *env, int screen, jint extension, jobject current_gamma_ramp, jobject current_mode) {
switchDisplayMode(env, screen, extension, current_mode);
// Don't propagate error to caller
setGammaRamp(env, screen, current_gamma_ramp);
}
void resetDisplayMode(JNIEnv *env, int screen, jint extension, jobject saved_gamma_ramp, jobject saved_mode) {
if (!switchDisplayMode(env, screen, extension, saved_mode))
return;
setGammaRamp(env, screen, saved_gamma_ramp);
}
static jobjectArray getAvailableDisplayModes(JNIEnv * env, Display *disp, int screen, jint extension) {
int num_modes, i;
mode_info *avail_modes;
@ -393,10 +383,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nSwitchDisplayMode(JNI
switchDisplayMode(env, getCurrentScreen(), extension, mode);
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nResetDisplayMode(JNIEnv *env, jclass clazz, jint extension, jobject gamma_ramp, jobject saved_mode) {
resetDisplayMode(env, getCurrentScreen(), extension, gamma_ramp, saved_mode);
}
JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nGetGammaRampLength(JNIEnv *env, jclass clazz) {
return (jint)getGammaRampLengthOfDisplay(env, getDisplay(), getCurrentScreen());
}

View File

@ -45,7 +45,7 @@
#include <jni.h>
#include "common_tools.h"
extern void resetDisplayMode(JNIEnv *env, int screen, jint extension, jobject gamma_ramp, jobject saved_mode);
extern void temporaryRestoreMode(JNIEnv *env, int screen, jint extension, jobject gamma_ramp, jobject saved_mode);
extern bool switchDisplayMode(JNIEnv * env, int screen, jint extension, jobject mode);
extern void setGammaRamp(JNIEnv *env, int screen, jobject gamma_ramp_buffer);
#endif

View File

@ -190,7 +190,8 @@ static bool releaseInput(JNIEnv *env, jint extension, jint window_mode, jobject
updateInputGrab(window_mode);
if (window_mode == org_lwjgl_opengl_LinuxDisplay_FULLSCREEN_NETWM) {
XIconifyWindow(getDisplay(), getCurrentWindow(), getCurrentScreen());
resetDisplayMode(env, getCurrentScreen(), extension, gamma_ramp, saved_mode);
switchDisplayMode(env, getCurrentScreen(), extension, saved_mode);
setGammaRamp(env, getCurrentScreen(), gamma_ramp);
}
return true;
}
@ -202,7 +203,8 @@ static void acquireInput(JNIEnv *env, jint extension, jint window_mode, jobject
setRepeatMode(env, AutoRepeatModeOff);
updateInputGrab(window_mode);
if (window_mode == org_lwjgl_opengl_LinuxDisplay_FULLSCREEN_NETWM) {
temporaryRestoreMode(env, getCurrentScreen(), extension, gamma_ramp, mode);
switchDisplayMode(env, getCurrentScreen(), extension, mode);
setGammaRamp(env, getCurrentScreen(), gamma_ramp);
}
}
@ -345,7 +347,7 @@ static bool isNetWMFullscreenSupported(JNIEnv *env) {
Atom netwm_supported_atom = XInternAtom(getDisplay(), "_NET_SUPPORTED", False);
int result = XGetWindowProperty(getDisplay(), RootWindow(getDisplay(), getCurrentScreen()), netwm_supported_atom, 0, 10000, False, AnyPropertyType, &actual_type, &actual_format, &nitems, &bytes_after, (void *)&supported_list);
if (result != Success) {
printfDebugJava(env, "Unable to query _NET_SUPPORTED window property");
throwException(env, "Unable to query _NET_SUPPORTED window property");
return false;
}
Atom fullscreen_atom = XInternAtom(getDisplay(), "_NET_WM_STATE_FULLSCREEN", False);