2002-11-15 05:58:57 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2002 Light Weight Java Game Library Project
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * 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.
|
|
|
|
*
|
|
|
|
* * Neither the name of 'Light Weight Java Game Library' nor the names of
|
|
|
|
* its contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "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 THE COPYRIGHT OWNER 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2002-11-15 06:00:19 -05:00
|
|
|
* $Id$
|
2002-11-15 05:58:57 -05:00
|
|
|
*
|
2002-11-15 06:10:32 -05:00
|
|
|
* Linux specific library for display handling.
|
2002-11-15 05:58:57 -05:00
|
|
|
*
|
|
|
|
* @author elias_naur <elias_naur@users.sourceforge.net>
|
2002-11-15 06:00:19 -05:00
|
|
|
* @version $Revision$
|
2002-11-15 05:58:57 -05:00
|
|
|
*/
|
|
|
|
|
2002-11-15 05:40:55 -05:00
|
|
|
#include <X11/X.h>
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/extensions/xf86vmode.h>
|
|
|
|
#include <X11/Xutil.h>
|
|
|
|
#include <stdio.h>
|
2002-12-22 15:58:25 -05:00
|
|
|
#include <stdlib.h>
|
2003-05-27 10:12:10 -04:00
|
|
|
#include <math.h>
|
2002-11-15 05:40:55 -05:00
|
|
|
#include <jni.h>
|
|
|
|
#include "org_lwjgl_Display.h"
|
2003-10-23 03:18:07 -04:00
|
|
|
#include "common_tools.h"
|
2002-11-15 05:40:55 -05:00
|
|
|
|
2003-03-30 14:26:39 -05:00
|
|
|
static int saved_width;
|
|
|
|
static int saved_height;
|
2003-05-27 10:12:10 -04:00
|
|
|
static int gamma_ramp_length = 0;
|
|
|
|
static unsigned short *r_ramp;
|
|
|
|
static unsigned short *g_ramp;
|
|
|
|
static unsigned short *b_ramp;
|
2002-12-28 05:41:17 -05:00
|
|
|
|
2003-05-27 10:12:10 -04:00
|
|
|
static bool getVidModeExtensionVersion(Display *disp, int screen, int *major, int *minor) {
|
|
|
|
int event_base, error_base;
|
2002-11-15 05:40:55 -05:00
|
|
|
|
|
|
|
if (!XF86VidModeQueryExtension(disp, &event_base, &error_base)) {
|
|
|
|
#ifdef _DEBUG
|
2002-11-20 05:33:37 -05:00
|
|
|
printf("XF86VidMode extension not available\n");
|
2002-11-15 05:40:55 -05:00
|
|
|
#endif
|
2003-05-27 10:12:10 -04:00
|
|
|
return false;
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
2003-05-30 10:44:45 -04:00
|
|
|
if (!XF86VidModeQueryVersion(disp, major, minor)) {
|
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Could not determine XF86VidMode version\n");
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
2002-11-15 05:40:55 -05:00
|
|
|
#ifdef _DEBUG
|
2003-06-01 13:20:03 -04:00
|
|
|
printf("XF86VidMode extension version %i.%i\n", *major, *minor);
|
2002-11-15 05:40:55 -05:00
|
|
|
#endif
|
2003-05-27 10:12:10 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool getDisplayModes(Display *disp, int screen, int *num_modes, XF86VidModeModeInfo ***avail_modes) {
|
|
|
|
int minor_ver, major_ver;
|
|
|
|
if (!getVidModeExtensionVersion(disp, screen, &major_ver, &minor_ver))
|
|
|
|
return false;
|
2002-11-15 05:40:55 -05:00
|
|
|
XF86VidModeGetAllModeLines(disp, screen, num_modes, avail_modes);
|
2003-05-27 10:12:10 -04:00
|
|
|
return true;
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
|
|
|
|
2003-05-27 10:12:10 -04:00
|
|
|
static bool setMode(Display *disp, int screen, int width, int height, bool lock_mode) {
|
2002-11-25 22:30:41 -05:00
|
|
|
int num_modes, i;
|
2003-03-30 14:26:39 -05:00
|
|
|
XF86VidModeModeInfo **avail_modes;
|
2002-12-23 06:31:32 -05:00
|
|
|
if (!getDisplayModes(disp, screen, &num_modes, &avail_modes)) {
|
2002-12-11 02:26:49 -05:00
|
|
|
#ifdef _DEBUG
|
2002-12-23 06:31:32 -05:00
|
|
|
printf("Could not get display modes\n");
|
2002-12-11 02:26:49 -05:00
|
|
|
#endif
|
2003-03-30 14:26:39 -05:00
|
|
|
return false;
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
2003-03-30 14:26:39 -05:00
|
|
|
XF86VidModeLockModeSwitch(disp, screen, 0);
|
|
|
|
for ( i = 0; i < num_modes; ++i ) {
|
2002-11-15 05:40:55 -05:00
|
|
|
#ifdef _DEBUG
|
2003-03-30 14:26:39 -05:00
|
|
|
printf("Mode %d: %dx%d\n", i, avail_modes[i]->hdisplay, avail_modes[i]->vdisplay);
|
2002-11-15 05:40:55 -05:00
|
|
|
#endif
|
2003-03-30 14:26:39 -05:00
|
|
|
if (avail_modes[i]->hdisplay == width && avail_modes[i]->vdisplay == height) {
|
|
|
|
if (!XF86VidModeSwitchToMode(disp, screen, avail_modes[i])) {
|
2002-11-15 05:40:55 -05:00
|
|
|
#ifdef _DEBUG
|
2003-03-30 14:26:39 -05:00
|
|
|
printf("Could not switch mode\n");
|
2002-11-15 05:40:55 -05:00
|
|
|
#endif
|
2003-03-27 10:16:35 -05:00
|
|
|
break;
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
2003-03-30 14:26:39 -05:00
|
|
|
// XF86VidModeSetViewPort(disp, screen, 0, 0);
|
|
|
|
if (lock_mode)
|
|
|
|
XF86VidModeLockModeSwitch(disp, screen, 1);
|
|
|
|
XFree(avail_modes);
|
|
|
|
return true;
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
|
|
|
}
|
2003-03-30 14:26:39 -05:00
|
|
|
XFree(avail_modes);
|
|
|
|
return false;
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
|
|
|
|
2003-05-27 10:12:10 -04:00
|
|
|
static void freeSavedGammaRamps() {
|
|
|
|
free(r_ramp);
|
|
|
|
free(g_ramp);
|
|
|
|
free(b_ramp);
|
|
|
|
r_ramp = NULL;
|
|
|
|
g_ramp = NULL;
|
|
|
|
b_ramp = NULL;
|
|
|
|
gamma_ramp_length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getGammaRampLength(Display *disp, int screen) {
|
|
|
|
int minor_ver, major_ver, ramp_size;
|
|
|
|
if (!getVidModeExtensionVersion(disp, screen, &major_ver, &minor_ver) || major_ver < 2) {
|
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("XF86VidMode extension version >= 2 not found\n");
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (XF86VidModeGetGammaRampSize(disp, screen, &ramp_size) == False) {
|
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("XF86VidModeGetGammaRampSize call failed\n");
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ramp_size;
|
|
|
|
}
|
|
|
|
|
2003-03-30 14:26:39 -05:00
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_Display_init
|
|
|
|
(JNIEnv * env, jclass clazz)
|
|
|
|
{
|
|
|
|
int num_modes;
|
|
|
|
XF86VidModeModeInfo **avail_modes;
|
|
|
|
int screen;
|
|
|
|
Display *disp = XOpenDisplay(NULL);
|
|
|
|
if (disp == NULL) {
|
2002-11-15 05:40:55 -05:00
|
|
|
#ifdef _DEBUG
|
2003-03-30 14:26:39 -05:00
|
|
|
printf("Could not open X connection\n");
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
screen = DefaultScreen(disp);
|
|
|
|
|
|
|
|
if (!getDisplayModes(disp, screen, &num_modes, &avail_modes)) {
|
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Could not get display modes\n");
|
2002-11-15 05:40:55 -05:00
|
|
|
#endif
|
|
|
|
}
|
2003-03-30 14:26:39 -05:00
|
|
|
saved_width = avail_modes[0]->hdisplay;
|
|
|
|
saved_height = avail_modes[0]->vdisplay;
|
|
|
|
int bpp = XDefaultDepth(disp, screen);
|
2002-11-15 05:40:55 -05:00
|
|
|
#ifdef _DEBUG
|
2003-03-30 14:26:39 -05:00
|
|
|
printf("Saved width, height %d, %d\n", saved_width, saved_height);
|
2002-11-15 05:40:55 -05:00
|
|
|
#endif
|
2003-03-30 14:26:39 -05:00
|
|
|
jclass jclass_DisplayMode = env->FindClass("org/lwjgl/DisplayMode");
|
|
|
|
jmethodID ctor = env->GetMethodID(jclass_DisplayMode, "<init>", "(IIII)V");
|
|
|
|
jobject newMode = env->NewObject(jclass_DisplayMode, ctor, saved_width, saved_height, bpp, 0);
|
|
|
|
jfieldID fid_initialMode = env->GetStaticFieldID(clazz, "mode", "Lorg/lwjgl/DisplayMode;");
|
|
|
|
env->SetStaticObjectField(clazz, fid_initialMode, newMode);
|
|
|
|
|
|
|
|
XFree(avail_modes);
|
2003-05-27 10:12:10 -04:00
|
|
|
|
|
|
|
/* Fetch the current gamma ramp */
|
|
|
|
gamma_ramp_length = getGammaRampLength(disp, screen);
|
|
|
|
if (gamma_ramp_length > 0) {
|
|
|
|
r_ramp = (unsigned short *)malloc(sizeof(unsigned short)*gamma_ramp_length);
|
|
|
|
g_ramp = (unsigned short *)malloc(sizeof(unsigned short)*gamma_ramp_length);
|
|
|
|
b_ramp = (unsigned short *)malloc(sizeof(unsigned short)*gamma_ramp_length);
|
|
|
|
if (!XF86VidModeGetGammaRamp(disp, screen, gamma_ramp_length, r_ramp, g_ramp, b_ramp))
|
|
|
|
freeSavedGammaRamps();
|
|
|
|
}
|
2003-03-30 14:26:39 -05:00
|
|
|
XCloseDisplay(disp);
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_Display_setDisplayMode(JNIEnv * env, jclass clazz, jobject mode) {
|
|
|
|
jclass cls_displayMode = env->FindClass("org/lwjgl/DisplayMode");
|
|
|
|
jfieldID fid_width = env->GetFieldID(cls_displayMode, "width", "I");
|
|
|
|
jfieldID fid_height = env->GetFieldID(cls_displayMode, "height", "I");
|
|
|
|
int width = env->GetIntField(mode, fid_width);
|
|
|
|
int height = env->GetIntField(mode, fid_height);
|
2003-05-27 10:12:10 -04:00
|
|
|
int screen;
|
|
|
|
Display *disp = XOpenDisplay(NULL);
|
|
|
|
|
|
|
|
if (disp == NULL) {
|
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Could not open X connection\n");
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
screen = DefaultScreen(disp);
|
|
|
|
if (setMode(disp, screen, width, height, true)) {
|
2003-03-30 14:26:39 -05:00
|
|
|
jfieldID fid_initialMode = env->GetStaticFieldID(clazz, "mode", "Lorg/lwjgl/DisplayMode;");
|
|
|
|
env->SetStaticObjectField(clazz, fid_initialMode, mode);
|
|
|
|
}
|
2003-05-27 10:12:10 -04:00
|
|
|
XCloseDisplay(disp);
|
2003-03-30 14:26:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_Display_resetDisplayMode(JNIEnv * env, jclass clazz) {
|
2003-05-27 10:12:10 -04:00
|
|
|
int screen;
|
|
|
|
Display *disp = XOpenDisplay(NULL);
|
|
|
|
|
|
|
|
if (disp == NULL) {
|
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Could not open X connection\n");
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
screen = DefaultScreen(disp);
|
|
|
|
setMode(disp, screen, saved_width, saved_height, false);
|
|
|
|
if (gamma_ramp_length > 0) {
|
|
|
|
XF86VidModeSetGammaRamp(disp, screen, gamma_ramp_length, r_ramp, g_ramp, b_ramp);
|
|
|
|
freeSavedGammaRamps();
|
|
|
|
}
|
|
|
|
XCloseDisplay(disp);
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_lwjgl_Display
|
2003-03-03 16:49:46 -05:00
|
|
|
* Method: nGetAvailableDisplayModes
|
2002-11-15 05:40:55 -05:00
|
|
|
* Signature: ()[Lorg/lwjgl/DisplayMode;
|
|
|
|
*/
|
2003-03-03 16:49:46 -05:00
|
|
|
JNIEXPORT jobjectArray JNICALL Java_org_lwjgl_Display_nGetAvailableDisplayModes
|
2002-11-15 05:40:55 -05:00
|
|
|
(JNIEnv * env, jclass clazz)
|
|
|
|
{
|
|
|
|
int num_modes, i;
|
|
|
|
Display *disp = XOpenDisplay(NULL);
|
2003-02-20 08:20:54 -05:00
|
|
|
int screen;
|
2002-11-15 05:40:55 -05:00
|
|
|
XF86VidModeModeInfo **avail_modes;
|
|
|
|
|
|
|
|
if (disp == NULL) {
|
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Could not open X connection\n");
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-02-20 08:20:54 -05:00
|
|
|
|
2003-02-20 08:27:20 -05:00
|
|
|
screen = DefaultScreen(disp);
|
2003-03-19 07:41:28 -05:00
|
|
|
int bpp = XDefaultDepth(disp, screen);
|
2003-02-20 08:20:54 -05:00
|
|
|
|
2002-11-15 05:40:55 -05:00
|
|
|
if (!getDisplayModes(disp, screen, &num_modes, &avail_modes)) {
|
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Could not get display modes\n");
|
|
|
|
#endif
|
2002-12-23 06:31:32 -05:00
|
|
|
XCloseDisplay(disp);
|
2002-11-15 05:40:55 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// Allocate an array of DisplayModes big enough
|
2002-11-19 03:50:57 -05:00
|
|
|
jclass displayModeClass = env->FindClass("org/lwjgl/DisplayMode");
|
2003-03-27 10:16:35 -05:00
|
|
|
jobjectArray ret = env->NewObjectArray(num_modes, displayModeClass, NULL);
|
2003-03-19 07:41:28 -05:00
|
|
|
jmethodID displayModeConstructor = env->GetMethodID(displayModeClass, "<init>", "(IIII)V");
|
2002-11-15 05:40:55 -05:00
|
|
|
|
|
|
|
for (i = 0; i < num_modes; i++) {
|
2003-03-19 07:41:28 -05:00
|
|
|
jobject displayMode = env->NewObject(displayModeClass, displayModeConstructor, avail_modes[i]->hdisplay, avail_modes[i]->vdisplay, bpp, 0);
|
|
|
|
env->SetObjectArrayElement(ret, i, displayMode);
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
|
|
|
XFree(avail_modes);
|
|
|
|
XCloseDisplay(disp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-10-22 14:34:42 -04:00
|
|
|
JNIEXPORT jint JNICALL Java_org_lwjgl_Display_getPlatform(JNIEnv * env, jclass clazz) {
|
2003-03-21 12:37:58 -05:00
|
|
|
return org_lwjgl_Display_PLATFORM_GLX;
|
|
|
|
}
|
2003-04-07 13:04:24 -04:00
|
|
|
|
2003-10-22 14:34:42 -04:00
|
|
|
JNIEXPORT jint JNICALL Java_org_lwjgl_Display_getGammaRampLength(JNIEnv *env, jclass clazz) {
|
2003-05-27 10:12:10 -04:00
|
|
|
return gamma_ramp_length;
|
2003-04-07 13:04:24 -04:00
|
|
|
}
|
|
|
|
|
2003-10-22 14:34:42 -04:00
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_Display_setGammaRamp(JNIEnv *env, jclass clazz, jobject gamma_ramp_buffer) {
|
|
|
|
if (gamma_ramp_length == 0) {
|
|
|
|
throwException(env, "gamma ramp length == 0.");
|
|
|
|
return;
|
|
|
|
}
|
2003-05-27 10:12:10 -04:00
|
|
|
Display * disp = XOpenDisplay(NULL);
|
|
|
|
if (disp == NULL) {
|
2003-10-22 14:34:42 -04:00
|
|
|
throwException(env, "Could not open X connection.");
|
|
|
|
return;
|
2003-05-27 10:12:10 -04:00
|
|
|
}
|
|
|
|
int screen = DefaultScreen(disp);
|
2003-06-24 08:24:55 -04:00
|
|
|
const float *gamma_ramp = (const float *)env->GetDirectBufferAddress(gamma_ramp_buffer);
|
2003-05-27 10:12:10 -04:00
|
|
|
unsigned short *ramp;
|
|
|
|
ramp = (unsigned short *)malloc(sizeof(unsigned short)*gamma_ramp_length);
|
|
|
|
for (int i = 0; i < gamma_ramp_length; i++) {
|
|
|
|
float scaled_gamma = gamma_ramp[i]*0xffff;
|
|
|
|
ramp[i] = (unsigned short)round(scaled_gamma);
|
|
|
|
}
|
|
|
|
if (XF86VidModeSetGammaRamp(disp, screen, gamma_ramp_length, ramp, ramp, ramp) == False) {
|
2003-10-22 14:34:42 -04:00
|
|
|
throwException(env, "Could not set gamma ramp.");
|
2003-05-27 10:12:10 -04:00
|
|
|
}
|
|
|
|
XCloseDisplay(disp);
|
2003-04-07 13:04:24 -04:00
|
|
|
}
|
2003-05-27 10:12:10 -04:00
|
|
|
|
2003-09-28 03:02:23 -04:00
|
|
|
JNIEXPORT jstring JNICALL Java_org_lwjgl_Display_getAdapter
|
|
|
|
(JNIEnv * env, jclass clazz)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jstring JNICALL Java_org_lwjgl_Display_getVersion
|
|
|
|
(JNIEnv * env, jclass clazz)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|