Linux: Moved saved display mode from C to LinuxDisplay.java

This commit is contained in:
Elias Naur 2005-11-21 21:59:11 +00:00
parent b3fd19cf01
commit b6dd18fc7d
4 changed files with 68 additions and 89 deletions

View File

@ -83,6 +83,9 @@ final class LinuxDisplay implements DisplayImplementation {
private static ByteBuffer saved_gamma; private static ByteBuffer saved_gamma;
private static ByteBuffer current_gamma; private static ByteBuffer current_gamma;
/** Saved mode to restore with */
private static DisplayMode saved_mode;
private static ByteBuffer getCurrentGammaRamp() throws LWJGLException { private static ByteBuffer getCurrentGammaRamp() throws LWJGLException {
lockAWT(); lockAWT();
try { try {
@ -256,12 +259,12 @@ final class LinuxDisplay implements DisplayImplementation {
public void resetDisplayMode() { public void resetDisplayMode() {
lockAWT(); lockAWT();
try { try {
nResetDisplayMode(current_displaymode_extension, saved_gamma); nResetDisplayMode(current_displaymode_extension, saved_gamma, saved_mode);
} finally { } finally {
unlockAWT(); unlockAWT();
} }
} }
private static native void nResetDisplayMode(int extension, ByteBuffer gamma_ramp); private static native void nResetDisplayMode(int extension, ByteBuffer gamma_ramp, DisplayMode saved_mode);
public int getGammaRampLength() { public int getGammaRampLength() {
lockAWT(); lockAWT();
@ -320,10 +323,13 @@ final class LinuxDisplay implements DisplayImplementation {
current_displaymode_extension = getBestDisplayModeExtension(); current_displaymode_extension = getBestDisplayModeExtension();
if (current_displaymode_extension == NONE) if (current_displaymode_extension == NONE)
throw new LWJGLException("No display mode extension is available"); throw new LWJGLException("No display mode extension is available");
DisplayMode mode = nInit(current_displaymode_extension); DisplayMode[] modes = getAvailableDisplayModes();
if (modes == null || modes.length == 0)
throw new LWJGLException("No modes available");
saved_mode = modes[0];
saved_gamma = getCurrentGammaRamp(); saved_gamma = getCurrentGammaRamp();
current_gamma = saved_gamma; current_gamma = saved_gamma;
return mode; return saved_mode;
} finally { } finally {
unlockAWT(); unlockAWT();
} }
@ -377,10 +383,10 @@ final class LinuxDisplay implements DisplayImplementation {
public void update() { public void update() {
lockAWT(); lockAWT();
nUpdate(current_displaymode_extension, current_window_mode, saved_gamma, current_gamma); nUpdate(current_displaymode_extension, current_window_mode, saved_gamma, current_gamma, saved_mode);
unlockAWT(); unlockAWT();
} }
private static native void nUpdate(int extension, int current_window_mode, ByteBuffer saved_gamma, ByteBuffer current_gamma); private static native void nUpdate(int extension, int current_window_mode, ByteBuffer saved_gamma, ByteBuffer current_gamma, DisplayMode saved_mode);
public void reshape(int x, int y, int width, int height) { public void reshape(int x, int y, int width, int height) {
lockAWT(); lockAWT();

View File

@ -64,9 +64,6 @@ typedef struct {
} mode_data; } mode_data;
} mode_info; } mode_info;
static int saved_width;
static int saved_height;
static int saved_freq;
static int current_width; static int current_width;
static int current_height; static int current_height;
static int current_freq; static int current_freq;
@ -208,7 +205,7 @@ static bool setXF86VidModeMode(Display *disp, int screen, mode_info *mode) {
static Status trySetXrandrMode(Display *disp, int screen, mode_info *mode, Time *timestamp) { static Status trySetXrandrMode(Display *disp, int screen, mode_info *mode, Time *timestamp) {
Status status; Status status;
Drawable root_window = RootWindow(disp, screen); Drawable root_window = RootWindow(disp, screen);
XRRScreenConfiguration *screen_configuration = XRRGetScreenInfo (disp, root_window); XRRScreenConfiguration *screen_configuration = XRRGetScreenInfo(disp, root_window);
XRRConfigTimes(screen_configuration, timestamp); XRRConfigTimes(screen_configuration, timestamp);
Rotation current_rotation; Rotation current_rotation;
XRRConfigRotations(screen_configuration, &current_rotation); XRRConfigRotations(screen_configuration, &current_rotation);
@ -228,8 +225,9 @@ static bool setXrandrMode(Display *disp, int screen, mode_info *mode) {
status = trySetXrandrMode(disp, screen, mode, &new_timestamp); status = trySetXrandrMode(disp, screen, mode, &new_timestamp);
if (status == 0) if (status == 0)
return true; // Success return true; // Success
if (new_timestamp == timestamp) if (new_timestamp == timestamp) {
return false; // Failure, and the stamps are equal meaning that the failure is not merely transient return false; // Failure, and the stamps are equal meaning that the failure is not merely transient
}
timestamp = new_timestamp; timestamp = new_timestamp;
} }
return false; return false;
@ -277,7 +275,7 @@ static bool setMode(JNIEnv *env, Display *disp, int screen, jint extension, int
return result; return result;
} }
int getGammaRampLengthOfDisplay(JNIEnv *env, Display *disp, int screen) { static int getGammaRampLengthOfDisplay(JNIEnv *env, Display *disp, int screen) {
int ramp_size; int ramp_size;
if (XF86VidModeGetGammaRampSize(disp, screen, &ramp_size) == False) { if (XF86VidModeGetGammaRampSize(disp, screen, &ramp_size) == False) {
printfDebugJava(env, "XF86VidModeGetGammaRampSize call failed"); printfDebugJava(env, "XF86VidModeGetGammaRampSize call failed");
@ -305,36 +303,6 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nConvertToNativeRam
return native_ramp; return native_ramp;
} }
jobject initDisplay(JNIEnv *env, int screen, jint extension) {
int num_modes;
mode_info *avail_modes;
Display *disp = XOpenDisplay(NULL);
if (disp == NULL) {
throwException(env, "Could not open display");
return NULL;
}
avail_modes = getDisplayModes(disp, screen, extension, &num_modes);
if (avail_modes == NULL || num_modes == 0) {
throwException(env, "Could not get display modes");
XCloseDisplay(disp);
return NULL;
}
saved_width = current_width = avail_modes[0].width;
saved_height = current_height = avail_modes[0].height;
saved_freq = current_freq = avail_modes[0].freq;
int bpp = XDefaultDepth(disp, screen);
printfDebugJava(env, "Original display dimensions: width %d, height %d freq %d", saved_width, saved_height, saved_freq);
jclass jclass_DisplayMode = (*env)->FindClass(env, "org/lwjgl/opengl/DisplayMode");
jmethodID ctor = (*env)->GetMethodID(env, jclass_DisplayMode, "<init>", "(IIII)V");
jobject newMode = (*env)->NewObject(env, jclass_DisplayMode, ctor, saved_width, saved_height, bpp, saved_freq);
free(avail_modes);
XCloseDisplay(disp);
return newMode;
}
JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nGetCurrentGammaRamp(JNIEnv *env, jclass unused) { JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nGetCurrentGammaRamp(JNIEnv *env, jclass unused) {
int ramp_size = getGammaRampLengthOfDisplay(env, getDisplay(), getCurrentScreen()); int ramp_size = getGammaRampLengthOfDisplay(env, getDisplay(), getCurrentScreen());
jobject ramp_buffer = newJavaManagedByteBuffer(env, sizeof(unsigned short)*3*ramp_size); jobject ramp_buffer = newJavaManagedByteBuffer(env, sizeof(unsigned short)*3*ramp_size);
@ -365,6 +333,19 @@ static void setGamma(JNIEnv *env, Display *disp, int screen, jobject ramp_buffer
} }
} }
static void setGammaRamp(JNIEnv *env, jobject gamma_ramp_buffer, int screen, bool throw_on_error) {
Display * disp = XOpenDisplay(NULL);
if (disp == NULL) {
if (throw_on_error)
throwException(env, "Could not open display");
else
printfDebugJava(env, "Could not open display");
return;
}
setGamma(env, disp, screen, gamma_ramp_buffer, throw_on_error);
XCloseDisplay(disp);
}
void temporaryRestoreMode(JNIEnv *env, int screen, jint extension, jobject saved_gamma_ramp) { void temporaryRestoreMode(JNIEnv *env, int screen, jint extension, jobject saved_gamma_ramp) {
Display *disp = XOpenDisplay(NULL); Display *disp = XOpenDisplay(NULL);
if (disp == NULL) { if (disp == NULL) {
@ -373,15 +354,18 @@ void temporaryRestoreMode(JNIEnv *env, int screen, jint extension, jobject saved
} }
if (!setMode(env, disp, screen, extension, current_width, current_height, current_freq, false)) if (!setMode(env, disp, screen, extension, current_width, current_height, current_freq, false))
printfDebugJava(env, "Could not restore mode"); printfDebugJava(env, "Could not restore mode");
// Don't propagate error to caller
setGamma(env, disp, screen, saved_gamma_ramp, false);
XCloseDisplay(disp); XCloseDisplay(disp);
// Don't propagate error to caller
setGammaRamp(env, saved_gamma_ramp, screen, false);
} }
void switchDisplayMode(JNIEnv * env, jobject mode, int screen, jint extension) { static bool switchDisplayMode(JNIEnv * env, int screen, jint extension, jobject mode, bool temporary, bool throw_on_error) {
if (mode == NULL) { if (mode == NULL) {
throwException(env, "mode must be non-null"); if (throw_on_error)
return; throwException(env, "mode must be non-null");
else
printfDebugJava(env, "Ignored null mode");
return false;
} }
jclass cls_displayMode = (*env)->GetObjectClass(env, mode); jclass cls_displayMode = (*env)->GetObjectClass(env, mode);
jfieldID fid_width = (*env)->GetFieldID(env, cls_displayMode, "width", "I"); jfieldID fid_width = (*env)->GetFieldID(env, cls_displayMode, "width", "I");
@ -392,28 +376,31 @@ void switchDisplayMode(JNIEnv * env, jobject mode, int screen, jint extension) {
int freq = (*env)->GetIntField(env, mode, fid_freq); int freq = (*env)->GetIntField(env, mode, fid_freq);
Display *disp = XOpenDisplay(NULL); Display *disp = XOpenDisplay(NULL);
if (disp == NULL) { if (disp == NULL) {
throwException(env, "Could not open display"); if (throw_on_error)
return; throwException(env, "Could not open display");
else
printfDebugJava(env, "Could not open display");
return false;
}
if (!setMode(env, disp, screen, extension, width, height, freq, temporary)) {
XCloseDisplay(disp);
if (throw_on_error)
throwException(env, "Could not switch mode.");
else
printfDebugJava(env, "Could not switch mode");
return false;
} }
if (!setMode(env, disp, screen, extension, width, height, freq, false))
throwException(env, "Could not switch mode.");
XCloseDisplay(disp); XCloseDisplay(disp);
return true;
} }
void resetDisplayMode(JNIEnv *env, int screen, jint extension, jobject gamma_ramp, bool temporary) { void resetDisplayMode(JNIEnv *env, int screen, jint extension, jobject gamma_ramp, jobject saved_mode, bool temporary) {
Display *disp = XOpenDisplay(NULL); if (!switchDisplayMode(env, screen, extension, saved_mode, temporary, false))
if (disp == NULL) {
printfDebugJava(env, "Failed to contact X Server");
return; return;
} setGammaRamp(env, gamma_ramp, screen, false);
if (!setMode(env, disp, screen, extension, saved_width, saved_height, saved_freq, temporary)) {
printfDebugJava(env, "Failed to reset mode");
}
setGamma(env, disp, screen, gamma_ramp, false);
XCloseDisplay(disp);
} }
jobjectArray getAvailableDisplayModes(JNIEnv * env, Display *disp, int screen, jint extension) { static jobjectArray getAvailableDisplayModes(JNIEnv * env, Display *disp, int screen, jint extension) {
int num_modes, i; int num_modes, i;
mode_info *avail_modes; mode_info *avail_modes;
int bpp = XDefaultDepth(disp, screen); int bpp = XDefaultDepth(disp, screen);
@ -436,26 +423,16 @@ jobjectArray getAvailableDisplayModes(JNIEnv * env, Display *disp, int screen, j
return ret; return ret;
} }
void setGammaRamp(JNIEnv *env, jobject gamma_ramp_buffer, int screen) {
Display * disp = XOpenDisplay(NULL);
if (disp == NULL) {
throwException(env, "Could not open display");
return;
}
setGamma(env, disp, screen, gamma_ramp_buffer, true);
XCloseDisplay(disp);
}
JNIEXPORT jobjectArray JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nGetAvailableDisplayModes(JNIEnv *env, jclass clazz, jint extension) { JNIEXPORT jobjectArray JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nGetAvailableDisplayModes(JNIEnv *env, jclass clazz, jint extension) {
return getAvailableDisplayModes(env, getDisplay(), getCurrentScreen(), extension); return getAvailableDisplayModes(env, getDisplay(), getCurrentScreen(), extension);
} }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nSwitchDisplayMode(JNIEnv *env, jclass clazz, jint extension, jobject mode) { JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nSwitchDisplayMode(JNIEnv *env, jclass clazz, jint extension, jobject mode) {
switchDisplayMode(env, mode, getCurrentScreen(), extension); switchDisplayMode(env, getCurrentScreen(), extension, mode, false, true);
} }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nResetDisplayMode(JNIEnv *env, jclass clazz, jint extension, jobject gamma_ramp) { 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, false); resetDisplayMode(env, getCurrentScreen(), extension, gamma_ramp, saved_mode, false);
} }
JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nGetGammaRampLength(JNIEnv *env, jclass clazz) { JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nGetGammaRampLength(JNIEnv *env, jclass clazz) {
@ -463,10 +440,6 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nGetGammaRampLength(JN
} }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nSetGammaRamp(JNIEnv *env, jclass clazz, jobject gamma_buffer) { JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nSetGammaRamp(JNIEnv *env, jclass clazz, jobject gamma_buffer) {
setGammaRamp(env, gamma_buffer, getCurrentScreen()); setGammaRamp(env, gamma_buffer, getCurrentScreen(), true);
}
JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nInit(JNIEnv *env, jclass clazz, jint extension) {
return initDisplay(env, getCurrentScreen(), extension);
} }

View File

@ -47,7 +47,7 @@
extern int getScreenModeWidth(void); extern int getScreenModeWidth(void);
extern int getScreenModeHeight(void); extern int getScreenModeHeight(void);
extern void resetDisplayMode(JNIEnv *env, int screen, jint extension, jobject gamma_ramp, bool temporary); extern void resetDisplayMode(JNIEnv *env, int screen, jint extension, jobject gamma_ramp, jobject saved_mode, bool temporary);
extern void temporaryRestoreMode(JNIEnv *env, int screen, jint extension, jobject gamma_ramp); extern void temporaryRestoreMode(JNIEnv *env, int screen, jint extension, jobject gamma_ramp);
#endif #endif

View File

@ -182,7 +182,7 @@ static void setDecorations(int dec) {
XChangeProperty (getDisplay(), getCurrentWindow(), motif_hints_atom, motif_hints_atom, 32, PropModeReplace, (unsigned char *)&motif_hints, sizeof(MotifWmHints)/sizeof(long)); XChangeProperty (getDisplay(), getCurrentWindow(), motif_hints_atom, motif_hints_atom, 32, PropModeReplace, (unsigned char *)&motif_hints, sizeof(MotifWmHints)/sizeof(long));
} }
static bool releaseInput(JNIEnv *env, jint extension, jint window_mode, jobject gamma_ramp) { static bool releaseInput(JNIEnv *env, jint extension, jint window_mode, jobject gamma_ramp, jobject saved_mode) {
if (isLegacyFullscreen(window_mode) || input_released) if (isLegacyFullscreen(window_mode) || input_released)
return false; return false;
input_released = true; input_released = true;
@ -190,7 +190,7 @@ static bool releaseInput(JNIEnv *env, jint extension, jint window_mode, jobject
updateInputGrab(window_mode); updateInputGrab(window_mode);
if (window_mode == org_lwjgl_opengl_LinuxDisplay_FULLSCREEN_NETWM) { if (window_mode == org_lwjgl_opengl_LinuxDisplay_FULLSCREEN_NETWM) {
XIconifyWindow(getDisplay(), getCurrentWindow(), getCurrentScreen()); XIconifyWindow(getDisplay(), getCurrentWindow(), getCurrentScreen());
resetDisplayMode(env, getCurrentScreen(), extension, gamma_ramp, true); resetDisplayMode(env, getCurrentScreen(), extension, gamma_ramp, saved_mode, true);
} }
return true; return true;
} }
@ -229,7 +229,7 @@ void setGrab(jint window_mode, bool new_grab) {
} }
} }
static void checkInput(JNIEnv *env, jint extension, jint window_mode, jobject saved_gamma, jobject current_gamma) { static void checkInput(JNIEnv *env, jint extension, jint window_mode, jobject saved_gamma, jobject current_gamma, jobject saved_mode) {
Window win; Window win;
int revert_mode; int revert_mode;
XGetInputFocus(getDisplay(), &win, &revert_mode); XGetInputFocus(getDisplay(), &win, &revert_mode);
@ -237,12 +237,12 @@ static void checkInput(JNIEnv *env, jint extension, jint window_mode, jobject sa
acquireInput(env, extension, window_mode, current_gamma); acquireInput(env, extension, window_mode, current_gamma);
focused = true; focused = true;
} else { } else {
releaseInput(env, extension, window_mode, saved_gamma); releaseInput(env, extension, window_mode, saved_gamma, saved_mode);
focused = false; focused = false;
} }
} }
void handleMessages(JNIEnv *env, jint extension, jint window_mode, jobject saved_gamma, jobject current_gamma) { void handleMessages(JNIEnv *env, jint extension, jint window_mode, jobject saved_gamma, jobject current_gamma, jobject saved_mode) {
XEvent event; XEvent event;
/* Window win; /* Window win;
int revert_mode;*/ int revert_mode;*/
@ -293,7 +293,7 @@ void handleMessages(JNIEnv *env, jint extension, jint window_mode, jobject saved
break; break;
} }
} }
checkInput(env, extension, window_mode, saved_gamma, current_gamma); checkInput(env, extension, window_mode, saved_gamma, current_gamma, saved_mode);
} }
static void setWindowTitle(const char *title) { static void setWindowTitle(const char *title) {
@ -454,9 +454,9 @@ int getWindowHeight(void) {
} }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nUpdate JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nUpdate
(JNIEnv *env, jclass clazz, jint extension, jint window_mode, jobject saved_gamma, jobject current_gamma) (JNIEnv *env, jclass clazz, jint extension, jint window_mode, jobject saved_gamma, jobject current_gamma, jobject saved_mode)
{ {
handleMessages(env, extension, window_mode, saved_gamma, current_gamma); handleMessages(env, extension, window_mode, saved_gamma, current_gamma, saved_mode);
} }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nCreateWindow(JNIEnv *env, jclass clazz, jobject peer_info_handle, jobject mode, jint window_mode, jint x, jint y) { JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nCreateWindow(JNIEnv *env, jclass clazz, jobject peer_info_handle, jobject mode, jint window_mode, jint x, jint y) {