2004-06-12 16:28:34 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2002-2004 LWJGL Project
|
2002-11-15 05:58:57 -05:00
|
|
|
* All rights reserved.
|
2004-06-12 16:28:34 -04:00
|
|
|
*
|
2002-11-15 05:58:57 -05:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
2004-06-12 16:28:34 -04:00
|
|
|
* modification, are permitted provided that the following conditions are
|
2002-11-15 05:58:57 -05:00
|
|
|
* met:
|
2004-06-12 16:28:34 -04:00
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
2002-11-15 05:58:57 -05:00
|
|
|
* 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.
|
|
|
|
*
|
2004-06-12 16:28:34 -04:00
|
|
|
* * Neither the name of 'LWJGL' nor the names of
|
|
|
|
* its contributors may be used to endorse or promote products derived
|
2002-11-15 05:58:57 -05:00
|
|
|
* from this software without specific prior written permission.
|
2004-06-12 16:28:34 -04:00
|
|
|
*
|
2002-11-15 05:58:57 -05:00
|
|
|
* 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
|
2004-06-12 16:28:34 -04:00
|
|
|
* 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
|
2002-11-15 05:58:57 -05:00
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
2004-06-12 16:28:34 -04:00
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
2002-11-15 05:58:57 -05:00
|
|
|
* 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>
|
2004-08-21 16:31:24 -04:00
|
|
|
#include <X11/extensions/Xrandr.h>
|
2002-11-15 05:40:55 -05:00
|
|
|
#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>
|
2004-07-02 15:02:00 -04:00
|
|
|
#include "display.h"
|
2003-10-23 03:18:07 -04:00
|
|
|
#include "common_tools.h"
|
2004-04-07 03:46:03 -04:00
|
|
|
#include "Window.h"
|
2002-11-15 05:40:55 -05:00
|
|
|
|
2004-08-21 16:31:24 -04:00
|
|
|
typedef struct {
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
int freq;
|
|
|
|
union {
|
2004-08-23 04:46:35 -04:00
|
|
|
int size_index; // Data for Xrandr extension
|
|
|
|
XF86VidModeModeInfo xf86vm_modeinfo; // Data for XF86VidMode extension
|
2004-08-21 16:31:24 -04:00
|
|
|
} mode_data;
|
|
|
|
} mode_info;
|
|
|
|
|
2003-03-30 14:26:39 -05:00
|
|
|
static int saved_width;
|
|
|
|
static int saved_height;
|
2004-08-21 16:31:24 -04:00
|
|
|
static int saved_freq;
|
2004-08-23 04:46:35 -04:00
|
|
|
static int current_width;
|
|
|
|
static int current_height;
|
|
|
|
static int current_freq;
|
|
|
|
static int saved_gamma_ramp_length = 0;
|
2004-09-22 15:25:16 -04:00
|
|
|
static unsigned short *r_ramp = NULL;
|
|
|
|
static unsigned short *g_ramp = NULL;
|
|
|
|
static unsigned short *b_ramp = NULL;
|
|
|
|
static unsigned short *current_ramp = NULL;
|
|
|
|
static int current_gamma_ramp_length = 0;
|
2004-08-21 16:31:24 -04:00
|
|
|
static extension current_extension = NONE;
|
2002-12-28 05:41:17 -05:00
|
|
|
|
2004-09-08 16:15:55 -04:00
|
|
|
int getScreenModeWidth(void) {
|
|
|
|
return current_width;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getScreenModeHeight(void) {
|
|
|
|
return current_height;
|
|
|
|
}
|
|
|
|
|
2004-08-23 04:46:35 -04:00
|
|
|
extension getCurrentDisplayModeExtension(void) {
|
|
|
|
return current_extension;
|
|
|
|
}
|
|
|
|
|
2004-12-11 15:09:04 -05:00
|
|
|
static bool getXF86VidModeVersion(JNIEnv *env, Display *disp, int *major, int *minor) {
|
2003-05-27 10:12:10 -04:00
|
|
|
int event_base, error_base;
|
2004-05-05 10:28:40 -04:00
|
|
|
|
2002-11-15 05:40:55 -05:00
|
|
|
if (!XF86VidModeQueryExtension(disp, &event_base, &error_base)) {
|
2004-12-11 15:09:04 -05:00
|
|
|
printfDebugJava(env, "XF86VidMode extension not available");
|
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)) {
|
2004-12-11 15:09:04 -05:00
|
|
|
printfDebugJava(env, "Could not query XF86VidMode version");
|
2003-05-30 10:44:45 -04:00
|
|
|
return false;
|
|
|
|
}
|
2004-12-11 15:09:04 -05:00
|
|
|
printfDebugJava(env, "XF86VidMode extension version %i.%i", *major, *minor);
|
2003-05-27 10:12:10 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-12-11 15:09:04 -05:00
|
|
|
static bool getXrandrVersion(JNIEnv *env, Display *disp, int *major, int *minor) {
|
2004-08-21 16:31:24 -04:00
|
|
|
int event_base, error_base;
|
|
|
|
|
|
|
|
if (!XRRQueryExtension(disp, &event_base, &error_base)) {
|
2004-12-11 15:09:04 -05:00
|
|
|
printfDebugJava(env, "Xrandr extension not available");
|
2004-08-21 16:31:24 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!XRRQueryVersion(disp, major, minor)) {
|
2004-12-11 15:09:04 -05:00
|
|
|
printfDebugJava(env, "Could not query Xrandr version");
|
2004-08-21 16:31:24 -04:00
|
|
|
return false;
|
|
|
|
}
|
2004-12-11 15:09:04 -05:00
|
|
|
printfDebugJava(env, "Xrandr extension version %i.%i", *major, *minor);
|
2004-08-21 16:31:24 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-12-11 15:09:04 -05:00
|
|
|
static bool isXrandrSupported(JNIEnv *env, Display *disp) {
|
2004-08-21 16:31:24 -04:00
|
|
|
int major, minor;
|
2004-12-11 15:09:04 -05:00
|
|
|
if (!getXrandrVersion(env, disp, &major, &minor))
|
2004-08-21 16:31:24 -04:00
|
|
|
return false;
|
|
|
|
return major >= 1;
|
|
|
|
}
|
|
|
|
|
2004-12-11 15:09:04 -05:00
|
|
|
static bool isXF86VidModeSupported(JNIEnv *env, Display *disp) {
|
2003-05-27 10:12:10 -04:00
|
|
|
int minor_ver, major_ver;
|
2004-12-11 15:09:04 -05:00
|
|
|
if (!getXF86VidModeVersion(env, disp, &major_ver, &minor_ver))
|
2003-05-27 10:12:10 -04:00
|
|
|
return false;
|
2004-08-21 16:31:24 -04:00
|
|
|
return major_ver >= 2;
|
|
|
|
}
|
|
|
|
|
2004-12-11 15:09:04 -05:00
|
|
|
static extension getBestDisplayModeExtension(JNIEnv *env, Display *disp) {
|
2004-12-12 12:07:45 -05:00
|
|
|
if (isXrandrSupported(env, disp)) {
|
|
|
|
printfDebugJava(env, "Using Xrandr for display mode switching");
|
2004-08-21 16:31:24 -04:00
|
|
|
return XRANDR;
|
2004-12-12 12:07:45 -05:00
|
|
|
} else if (isXF86VidModeSupported(env, disp)) {
|
|
|
|
printfDebugJava(env, "Using XF86VidMode for display mode switching");
|
2004-08-21 16:31:24 -04:00
|
|
|
return XF86VIDMODE;
|
2004-12-12 12:07:45 -05:00
|
|
|
} else {
|
|
|
|
printfDebugJava(env, "No display mode extensions available");
|
2004-08-21 16:31:24 -04:00
|
|
|
return NONE;
|
2004-12-12 12:07:45 -05:00
|
|
|
}
|
2004-08-21 16:31:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static mode_info *getXrandrDisplayModes(Display *disp, int screen, int *num_modes) {
|
|
|
|
int num_randr_sizes;
|
|
|
|
XRRScreenSize *sizes = XRRSizes(disp, screen, &num_randr_sizes);
|
2005-01-16 05:43:30 -05:00
|
|
|
mode_info *avail_modes = NULL;
|
|
|
|
int list_size = 0;
|
2004-08-21 16:31:24 -04:00
|
|
|
/* Count number of modes */
|
2004-09-10 04:13:56 -04:00
|
|
|
int i;
|
2005-01-16 05:43:30 -05:00
|
|
|
int mode_index = 0;
|
2004-09-10 04:13:56 -04:00
|
|
|
for (i = 0; i < num_randr_sizes; i++) {
|
2004-08-21 16:31:24 -04:00
|
|
|
int num_randr_rates;
|
|
|
|
short *freqs = XRRRates(disp, screen, i, &num_randr_rates);
|
2004-09-10 04:13:56 -04:00
|
|
|
int j;
|
|
|
|
for (j = 0; j < num_randr_rates; j++) {
|
2005-01-16 05:43:30 -05:00
|
|
|
if (list_size <= mode_index) {
|
|
|
|
list_size += 1;
|
|
|
|
avail_modes = (mode_info *)realloc(avail_modes, sizeof(mode_info)*list_size);
|
|
|
|
if (avail_modes == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
avail_modes[mode_index].width = sizes[i].width;
|
|
|
|
avail_modes[mode_index].height = sizes[i].height;
|
|
|
|
avail_modes[mode_index].freq = freqs[j];
|
|
|
|
avail_modes[mode_index].mode_data.size_index = i;
|
|
|
|
mode_index++;
|
2004-08-21 16:31:24 -04:00
|
|
|
}
|
|
|
|
}
|
2005-01-16 05:43:30 -05:00
|
|
|
*num_modes = mode_index;
|
2004-08-21 16:31:24 -04:00
|
|
|
return avail_modes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static mode_info *getXF86VidModeDisplayModes(Display *disp, int screen, int *num_modes) {
|
|
|
|
int num_xf86vm_modes;
|
|
|
|
XF86VidModeModeInfo **avail_xf86vm_modes;
|
|
|
|
XF86VidModeGetAllModeLines(disp, screen, &num_xf86vm_modes, &avail_xf86vm_modes);
|
|
|
|
mode_info *avail_modes = (mode_info *)malloc(sizeof(mode_info)*num_xf86vm_modes);
|
|
|
|
if (avail_modes == NULL) {
|
|
|
|
XFree(avail_xf86vm_modes);
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-09-10 04:13:56 -04:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < num_xf86vm_modes; i++) {
|
2004-08-21 16:31:24 -04:00
|
|
|
avail_modes[i].width = avail_xf86vm_modes[i]->hdisplay;
|
|
|
|
avail_modes[i].height = avail_xf86vm_modes[i]->vdisplay;
|
|
|
|
avail_modes[i].freq = 0; // No frequency support in XF86VidMode
|
|
|
|
avail_modes[i].mode_data.xf86vm_modeinfo = *avail_xf86vm_modes[i];
|
|
|
|
}
|
|
|
|
XFree(avail_xf86vm_modes);
|
|
|
|
*num_modes = num_xf86vm_modes;
|
|
|
|
return avail_modes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static mode_info *getDisplayModes(Display *disp, int screen, int *num_modes) {
|
|
|
|
switch (current_extension) {
|
|
|
|
case XF86VIDMODE:
|
|
|
|
return getXF86VidModeDisplayModes(disp, screen, num_modes);
|
|
|
|
case XRANDR:
|
|
|
|
return getXrandrDisplayModes(disp, screen, num_modes);
|
|
|
|
case NONE:
|
|
|
|
// fall through
|
|
|
|
default:
|
|
|
|
// Should never happen
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool setXF86VidModeMode(Display *disp, int screen, mode_info *mode) {
|
|
|
|
return True == XF86VidModeSwitchToMode(disp, screen, &mode->mode_data.xf86vm_modeinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool setXrandrMode(Display *disp, int screen, mode_info *mode) {
|
|
|
|
Status success;
|
|
|
|
do {
|
|
|
|
Time config_time;
|
|
|
|
Drawable root_window = RootWindow(disp, screen);
|
|
|
|
XRRScreenConfiguration *screen_configuration = XRRGetScreenInfo (disp, root_window);
|
|
|
|
XRRConfigTimes(screen_configuration, &config_time);
|
|
|
|
Rotation current_rotation;
|
|
|
|
XRRConfigRotations(screen_configuration, ¤t_rotation);
|
|
|
|
success = XRRSetScreenConfigAndRate(disp, screen_configuration, root_window, mode->mode_data.size_index, current_rotation, mode->freq, config_time);
|
|
|
|
XRRFreeScreenConfigInfo(screen_configuration);
|
|
|
|
} while (success != 0);
|
2003-05-27 10:12:10 -04:00
|
|
|
return true;
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
|
|
|
|
2004-12-11 15:09:04 -05:00
|
|
|
static bool setMode(JNIEnv *env, Display *disp, int screen, int width, int height, int freq, bool temporary) {
|
2004-08-21 16:31:24 -04:00
|
|
|
if (current_extension == NONE)
|
|
|
|
return false;
|
2004-07-22 04:52:25 -04:00
|
|
|
int num_modes, i;
|
2004-08-21 16:31:24 -04:00
|
|
|
mode_info *avail_modes = getDisplayModes(disp, screen, &num_modes);
|
2004-12-12 11:55:38 -05:00
|
|
|
if (avail_modes == NULL || num_modes == 0) {
|
2004-12-11 15:09:04 -05:00
|
|
|
printfDebugJava(env, "Could not get display modes");
|
2003-03-30 14:26:39 -05:00
|
|
|
return false;
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
2004-08-21 16:31:24 -04:00
|
|
|
bool result = false;
|
2003-03-30 14:26:39 -05:00
|
|
|
for ( i = 0; i < num_modes; ++i ) {
|
2004-12-11 15:09:04 -05:00
|
|
|
printfDebugJava(env, "Mode %d: %dx%d @%d", i, avail_modes[i].width, avail_modes[i].height, avail_modes[i].freq);
|
2004-08-21 16:31:24 -04:00
|
|
|
if (avail_modes[i].width == width && avail_modes[i].height == height && avail_modes[i].freq == freq) {
|
|
|
|
switch (current_extension) {
|
|
|
|
case XF86VIDMODE:
|
|
|
|
if (!setXF86VidModeMode(disp, screen, &avail_modes[i])) {
|
2004-12-11 15:09:04 -05:00
|
|
|
printfDebugJava(env, "Could not switch mode");
|
2004-08-21 16:31:24 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case XRANDR:
|
|
|
|
if (!setXrandrMode(disp, screen, &avail_modes[i])) {
|
2004-12-11 15:09:04 -05:00
|
|
|
printfDebugJava(env, "Could not switch mode");
|
2004-08-21 16:31:24 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NONE: // Should never happen
|
|
|
|
default: // Should never happen
|
|
|
|
continue;
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
2004-08-21 16:31:24 -04:00
|
|
|
result = true;
|
2004-09-22 15:56:02 -04:00
|
|
|
if (!temporary) {
|
|
|
|
current_width = width;
|
|
|
|
current_height = height;
|
|
|
|
current_freq = freq;
|
|
|
|
}
|
2004-08-21 16:31:24 -04:00
|
|
|
break;
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
|
|
|
}
|
2004-08-21 16:31:24 -04:00
|
|
|
free(avail_modes);
|
2004-07-21 17:51:37 -04:00
|
|
|
XFlush(disp);
|
2004-08-21 16:31:24 -04:00
|
|
|
return result;
|
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;
|
2004-08-23 04:46:35 -04:00
|
|
|
saved_gamma_ramp_length = 0;
|
2003-05-27 10:12:10 -04:00
|
|
|
}
|
|
|
|
|
2004-12-11 15:09:04 -05:00
|
|
|
static int getGammaRampLengthOfDisplay(JNIEnv *env, Display *disp, int screen) {
|
2004-08-21 16:31:24 -04:00
|
|
|
int ramp_size;
|
2004-12-11 15:09:04 -05:00
|
|
|
if (!isXF86VidModeSupported(env, disp)) {
|
|
|
|
printfDebugJava(env, "XF86VidMode extension version >= 2 not found");
|
2003-05-27 10:12:10 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (XF86VidModeGetGammaRampSize(disp, screen, &ramp_size) == False) {
|
2004-12-11 15:09:04 -05:00
|
|
|
printfDebugJava(env, "XF86VidModeGetGammaRampSize call failed");
|
2003-05-27 10:12:10 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ramp_size;
|
|
|
|
}
|
|
|
|
|
2004-12-11 15:09:04 -05:00
|
|
|
int getGammaRampLength(JNIEnv *env, int screen) {
|
2004-08-23 04:46:35 -04:00
|
|
|
Display *disp = XOpenDisplay(NULL);
|
|
|
|
if (disp == NULL) {
|
2004-12-11 15:09:04 -05:00
|
|
|
printfDebugJava(env, "Could not open display");
|
2004-08-23 04:46:35 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2004-12-11 15:09:04 -05:00
|
|
|
int length = getGammaRampLengthOfDisplay(env, disp, screen);
|
2004-08-23 04:46:35 -04:00
|
|
|
XCloseDisplay(disp);
|
2004-09-10 04:13:56 -04:00
|
|
|
return length;
|
2004-08-23 04:46:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
jobject initDisplay(JNIEnv *env, int screen) {
|
2004-08-21 13:42:23 -04:00
|
|
|
int num_modes;
|
2004-08-21 16:31:24 -04:00
|
|
|
mode_info *avail_modes;
|
2004-07-21 17:51:37 -04:00
|
|
|
Display *disp = XOpenDisplay(NULL);
|
|
|
|
if (disp == NULL) {
|
|
|
|
throwException(env, "Could not open display");
|
2004-07-02 15:02:00 -04:00
|
|
|
return NULL;
|
2004-07-21 17:51:37 -04:00
|
|
|
}
|
2004-05-05 10:28:40 -04:00
|
|
|
|
2004-12-11 15:09:04 -05:00
|
|
|
current_extension = getBestDisplayModeExtension(env, disp);
|
2004-08-21 16:31:24 -04:00
|
|
|
if (current_extension == NONE) {
|
|
|
|
throwException(env, "No display mode extension is available");
|
|
|
|
XCloseDisplay(disp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
avail_modes = getDisplayModes(disp, screen, &num_modes);
|
|
|
|
if (avail_modes == NULL) {
|
2004-08-21 13:42:23 -04:00
|
|
|
throwException(env, "Could not get display modes");
|
2004-08-21 16:31:24 -04:00
|
|
|
XCloseDisplay(disp);
|
2004-08-21 13:42:23 -04:00
|
|
|
return NULL;
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
2004-08-23 04:46:35 -04:00
|
|
|
saved_width = current_width = avail_modes[0].width;
|
|
|
|
saved_height = current_height = avail_modes[0].height;
|
|
|
|
saved_freq = current_freq = avail_modes[0].freq;
|
2003-03-30 14:26:39 -05:00
|
|
|
int bpp = XDefaultDepth(disp, screen);
|
2004-12-11 15:09:04 -05:00
|
|
|
printfDebugJava(env, "Original display dimensions: width %d, height %d freq %d", saved_width, saved_height, saved_freq);
|
2004-09-10 04:13:56 -04:00
|
|
|
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);
|
2003-03-30 14:26:39 -05:00
|
|
|
|
2004-08-21 16:31:24 -04:00
|
|
|
free(avail_modes);
|
2003-05-27 10:12:10 -04:00
|
|
|
|
|
|
|
/* Fetch the current gamma ramp */
|
2004-12-11 15:09:04 -05:00
|
|
|
saved_gamma_ramp_length = getGammaRampLengthOfDisplay(env, disp, screen);
|
2004-08-23 04:46:35 -04:00
|
|
|
if (saved_gamma_ramp_length > 0) {
|
|
|
|
r_ramp = (unsigned short *)malloc(sizeof(unsigned short)*saved_gamma_ramp_length);
|
|
|
|
g_ramp = (unsigned short *)malloc(sizeof(unsigned short)*saved_gamma_ramp_length);
|
|
|
|
b_ramp = (unsigned short *)malloc(sizeof(unsigned short)*saved_gamma_ramp_length);
|
|
|
|
if (!XF86VidModeGetGammaRamp(disp, screen, saved_gamma_ramp_length, r_ramp, g_ramp, b_ramp))
|
2003-05-27 10:12:10 -04:00
|
|
|
freeSavedGammaRamps();
|
|
|
|
}
|
2004-07-21 17:51:37 -04:00
|
|
|
XCloseDisplay(disp);
|
2004-07-02 15:02:00 -04:00
|
|
|
return newMode;
|
2003-03-30 14:26:39 -05:00
|
|
|
}
|
|
|
|
|
2004-09-22 15:25:16 -04:00
|
|
|
static void freeCurrentGamma(void) {
|
|
|
|
if (current_ramp != NULL) {
|
|
|
|
free(current_ramp);
|
|
|
|
current_ramp = NULL;
|
|
|
|
current_gamma_ramp_length = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setCurrentGamma(Display *disp, int screen, JNIEnv *env) {
|
|
|
|
if (current_gamma_ramp_length == 0)
|
2004-08-23 04:46:35 -04:00
|
|
|
return;
|
2004-09-22 15:25:16 -04:00
|
|
|
if (XF86VidModeSetGammaRamp(disp, screen, current_gamma_ramp_length, current_ramp, current_ramp, current_ramp) == False) {
|
|
|
|
if (env != NULL)
|
|
|
|
throwException(env, "Could not set gamma ramp.");
|
|
|
|
else
|
2004-12-11 15:09:04 -05:00
|
|
|
printfDebugJava(env, "Could not set gamma ramp");
|
2004-08-23 04:46:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-11 15:09:04 -05:00
|
|
|
void temporaryRestoreMode(JNIEnv *env, int screen) {
|
2004-08-23 04:46:35 -04:00
|
|
|
Display *disp = XOpenDisplay(NULL);
|
|
|
|
if (disp == NULL) {
|
2004-12-11 15:09:04 -05:00
|
|
|
printfDebugJava(env, "Could not open display");
|
2004-08-23 04:46:35 -04:00
|
|
|
return;
|
|
|
|
}
|
2004-12-11 15:09:04 -05:00
|
|
|
if (!setMode(env, disp, screen, current_width, current_height, current_freq, false))
|
|
|
|
printfDebugJava(env, "Could not restore mode");
|
2004-09-22 15:25:16 -04:00
|
|
|
setCurrentGamma(disp, screen, NULL);
|
2004-08-23 04:46:35 -04:00
|
|
|
XCloseDisplay(disp);
|
|
|
|
// Don't propagate error to caller
|
|
|
|
}
|
|
|
|
|
|
|
|
void switchDisplayMode(JNIEnv * env, jobject mode, int screen) {
|
2004-02-11 06:25:19 -05:00
|
|
|
if (mode == NULL) {
|
|
|
|
throwException(env, "mode must be non-null");
|
|
|
|
return;
|
|
|
|
}
|
2004-09-10 04:13:56 -04:00
|
|
|
jclass cls_displayMode = (*env)->GetObjectClass(env, mode);
|
|
|
|
jfieldID fid_width = (*env)->GetFieldID(env, cls_displayMode, "width", "I");
|
|
|
|
jfieldID fid_height = (*env)->GetFieldID(env, cls_displayMode, "height", "I");
|
|
|
|
jfieldID fid_freq = (*env)->GetFieldID(env, cls_displayMode, "freq", "I");
|
|
|
|
int width = (*env)->GetIntField(env, mode, fid_width);
|
|
|
|
int height = (*env)->GetIntField(env, mode, fid_height);
|
|
|
|
int freq = (*env)->GetIntField(env, mode, fid_freq);
|
2004-07-21 17:51:37 -04:00
|
|
|
Display *disp = XOpenDisplay(NULL);
|
|
|
|
if (disp == NULL) {
|
|
|
|
throwException(env, "Could not open display");
|
2003-05-27 10:12:10 -04:00
|
|
|
return;
|
2004-07-21 17:51:37 -04:00
|
|
|
}
|
2004-12-11 15:09:04 -05:00
|
|
|
if (!setMode(env, disp, screen, width, height, freq, false))
|
2003-10-24 01:51:50 -04:00
|
|
|
throwException(env, "Could not switch mode.");
|
2004-07-21 17:51:37 -04:00
|
|
|
XCloseDisplay(disp);
|
2003-03-30 14:26:39 -05:00
|
|
|
}
|
|
|
|
|
2004-12-11 15:09:04 -05:00
|
|
|
void resetDisplayMode(JNIEnv *env, int screen, bool temporary) {
|
2004-07-21 17:51:37 -04:00
|
|
|
Display *disp = XOpenDisplay(NULL);
|
2004-12-12 11:55:38 -05:00
|
|
|
if (disp == NULL) {
|
|
|
|
printfDebugJava(env, "Failed to contact X Server");
|
2003-05-27 10:12:10 -04:00
|
|
|
return;
|
2004-12-12 11:55:38 -05:00
|
|
|
}
|
2004-12-11 15:09:04 -05:00
|
|
|
if (!setMode(env, disp, screen, saved_width, saved_height, saved_freq, temporary)) {
|
|
|
|
printfDebugJava(env, "Failed to reset mode");
|
2004-07-21 17:51:37 -04:00
|
|
|
}
|
2004-08-23 04:46:35 -04:00
|
|
|
if (saved_gamma_ramp_length > 0) {
|
|
|
|
XF86VidModeSetGammaRamp(disp, screen, saved_gamma_ramp_length, r_ramp, g_ramp, b_ramp);
|
2003-05-27 10:12:10 -04:00
|
|
|
}
|
2004-07-22 04:52:25 -04:00
|
|
|
// decDisplay();
|
2004-07-21 17:51:37 -04:00
|
|
|
XCloseDisplay(disp);
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
|
|
|
|
2004-08-23 04:46:35 -04:00
|
|
|
jobjectArray getAvailableDisplayModes(JNIEnv * env, int screen) {
|
2002-11-15 05:40:55 -05:00
|
|
|
int num_modes, i;
|
2004-08-21 16:31:24 -04:00
|
|
|
mode_info *avail_modes;
|
2004-07-21 17:51:37 -04:00
|
|
|
Display *disp = XOpenDisplay(NULL);
|
|
|
|
if (disp == NULL) {
|
|
|
|
throwException(env, "Could not open display");
|
2002-11-15 05:40:55 -05:00
|
|
|
return NULL;
|
2004-07-21 17:51:37 -04:00
|
|
|
}
|
2004-05-05 10:28:40 -04:00
|
|
|
|
2003-03-19 07:41:28 -05:00
|
|
|
int bpp = XDefaultDepth(disp, screen);
|
2004-08-21 16:31:24 -04:00
|
|
|
avail_modes = getDisplayModes(disp, screen, &num_modes);
|
|
|
|
if (avail_modes == NULL) {
|
2004-12-11 15:09:04 -05:00
|
|
|
printfDebugJava(env, "Could not get display modes");
|
2004-07-21 17:51:37 -04:00
|
|
|
XCloseDisplay(disp);
|
2002-11-15 05:40:55 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// Allocate an array of DisplayModes big enough
|
2004-09-10 04:13:56 -04:00
|
|
|
jclass displayModeClass = (*env)->FindClass(env, "org/lwjgl/opengl/DisplayMode");
|
|
|
|
jobjectArray ret = (*env)->NewObjectArray(env, num_modes, displayModeClass, NULL);
|
|
|
|
jmethodID displayModeConstructor = (*env)->GetMethodID(env, displayModeClass, "<init>", "(IIII)V");
|
2004-05-05 10:28:40 -04:00
|
|
|
|
2002-11-15 05:40:55 -05:00
|
|
|
for (i = 0; i < num_modes; i++) {
|
2004-09-10 04:13:56 -04:00
|
|
|
jobject displayMode = (*env)->NewObject(env, displayModeClass, displayModeConstructor, avail_modes[i].width, avail_modes[i].height, bpp, avail_modes[i].freq);
|
|
|
|
(*env)->SetObjectArrayElement(env, ret, i, displayMode);
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
2004-08-21 16:31:24 -04:00
|
|
|
free(avail_modes);
|
2004-07-21 17:51:37 -04:00
|
|
|
XCloseDisplay(disp);
|
2002-11-15 05:40:55 -05:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-08-23 04:46:35 -04:00
|
|
|
void setGammaRamp(JNIEnv *env, jobject gamma_ramp_buffer, int screen) {
|
2004-07-21 17:51:37 -04:00
|
|
|
Display * disp = XOpenDisplay(NULL);
|
|
|
|
if (disp == NULL) {
|
|
|
|
throwException(env, "Could not open display");
|
2003-10-22 14:34:42 -04:00
|
|
|
return;
|
2004-07-21 17:51:37 -04:00
|
|
|
}
|
2004-09-22 15:25:16 -04:00
|
|
|
freeCurrentGamma();
|
2004-12-11 15:09:04 -05:00
|
|
|
current_gamma_ramp_length = getGammaRampLengthOfDisplay(env, disp, screen);
|
2004-09-22 15:25:16 -04:00
|
|
|
if (current_gamma_ramp_length == 0) {
|
2004-08-23 04:46:35 -04:00
|
|
|
throwException(env, "Gamma ramp not supported");
|
|
|
|
return;
|
|
|
|
}
|
2004-09-10 04:13:56 -04:00
|
|
|
const float *gamma_ramp = (const float *)(*env)->GetDirectBufferAddress(env, gamma_ramp_buffer);
|
2004-09-22 15:25:16 -04:00
|
|
|
current_ramp = (unsigned short *)malloc(sizeof(unsigned short)*current_gamma_ramp_length);
|
2004-09-10 04:13:56 -04:00
|
|
|
int i;
|
2004-09-22 15:25:16 -04:00
|
|
|
for (i = 0; i < current_gamma_ramp_length; i++) {
|
2003-05-27 10:12:10 -04:00
|
|
|
float scaled_gamma = gamma_ramp[i]*0xffff;
|
2004-09-22 15:25:16 -04:00
|
|
|
current_ramp[i] = (unsigned short)round(scaled_gamma);
|
2003-05-27 10:12:10 -04:00
|
|
|
}
|
2004-09-22 15:25:16 -04:00
|
|
|
setCurrentGamma(disp, screen, env);
|
2004-07-21 17:51:37 -04:00
|
|
|
XCloseDisplay(disp);
|
2003-04-07 13:04:24 -04:00
|
|
|
}
|