2002-08-15 11:46:18 -04:00
|
|
|
/*
|
2004-06-12 16:28:34 -04:00
|
|
|
* Copyright (c) 2002-2004 LWJGL Project
|
2002-08-15 11:46:18 -04:00
|
|
|
* 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.
|
2002-08-11 07:49:32 -04:00
|
|
|
*
|
2002-08-15 11:46:18 -04:00
|
|
|
* * 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.
|
2002-08-11 07:49:32 -04:00
|
|
|
*
|
2004-06-12 16:28:34 -04:00
|
|
|
* * Neither the name of 'LWJGL' nor the names of
|
2002-08-15 11:46:18 -04:00
|
|
|
* 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.
|
|
|
|
*/
|
2005-12-26 10:06:45 -05:00
|
|
|
|
2002-08-15 11:46:18 -04:00
|
|
|
/**
|
|
|
|
* $Id$
|
2002-08-11 07:49:32 -04:00
|
|
|
*
|
2006-07-12 14:32:58 -04:00
|
|
|
* Windows specific library for display handling.
|
2002-08-11 07:49:32 -04:00
|
|
|
*
|
2002-08-15 11:46:18 -04:00
|
|
|
* @author cix_foo <cix_foo@users.sourceforge.net>
|
|
|
|
* @version $Revision$
|
2002-08-11 07:49:32 -04:00
|
|
|
*/
|
|
|
|
|
2004-07-03 06:16:28 -04:00
|
|
|
#include <windows.h>
|
2005-02-21 09:46:47 -05:00
|
|
|
// Multimon.h enables multi monitor emulation on win95 and winnt4
|
|
|
|
// So we only need the extended, multi-monitor aware path
|
2006-07-08 17:57:20 -04:00
|
|
|
//#define COMPILE_MULTIMON_STUBS
|
|
|
|
//#include <Multimon.h>
|
2004-07-03 06:16:28 -04:00
|
|
|
#include <jni.h>
|
2006-07-12 14:32:58 -04:00
|
|
|
#include "org_lwjgl_opengl_WindowsDisplay.h"
|
2004-07-03 06:16:28 -04:00
|
|
|
#include "display.h"
|
2003-10-11 12:29:40 -04:00
|
|
|
#include "common_tools.h"
|
2002-08-15 10:53:18 -04:00
|
|
|
|
2005-12-27 06:12:53 -05:00
|
|
|
static jobject createDisplayMode(JNIEnv *env, DEVMODE *devmode) {
|
|
|
|
jclass displayModeClass;
|
|
|
|
|
|
|
|
jmethodID displayModeConstructor;
|
|
|
|
|
|
|
|
displayModeClass = (*env)->FindClass(env, "org/lwjgl/opengl/DisplayMode");
|
|
|
|
if (displayModeClass == NULL)
|
|
|
|
return NULL;
|
|
|
|
displayModeConstructor = (*env)->GetMethodID(env, displayModeClass, "<init>", "(IIII)V");
|
|
|
|
if (displayModeConstructor == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return (*env)->NewObject(env, displayModeClass, displayModeConstructor,
|
|
|
|
devmode->dmPelsWidth, devmode->dmPelsHeight,
|
|
|
|
devmode->dmBitsPerPel, devmode->dmDisplayFrequency);
|
|
|
|
}
|
|
|
|
|
2003-03-03 16:49:46 -05:00
|
|
|
/**
|
2003-09-10 18:35:06 -04:00
|
|
|
* Choose displaymodes using extended codepath (multiple displaydevices)
|
2003-03-03 16:49:46 -05:00
|
|
|
*/
|
2005-12-27 06:03:33 -05:00
|
|
|
jobjectArray getAvailableDisplayModes(JNIEnv * env) {
|
2003-09-16 04:29:16 -04:00
|
|
|
|
2005-12-26 10:06:45 -05:00
|
|
|
int i = 0, j = 0, n = 0;
|
2004-12-09 10:36:13 -05:00
|
|
|
|
2005-12-27 06:12:53 -05:00
|
|
|
// DISPLAY_DEVICE DisplayDevice;
|
2004-12-09 10:36:13 -05:00
|
|
|
DEVMODE DevMode;
|
2005-01-16 06:12:57 -05:00
|
|
|
jobject *display_mode_objects = NULL;
|
|
|
|
int list_size = 0;
|
2004-12-09 10:36:13 -05:00
|
|
|
|
2005-12-26 10:06:45 -05:00
|
|
|
jclass displayModeClass;
|
|
|
|
jobjectArray ret;
|
2005-12-27 06:12:53 -05:00
|
|
|
displayModeClass = (*env)->FindClass(env, "org/lwjgl/opengl/DisplayMode");
|
2004-12-09 10:36:13 -05:00
|
|
|
|
2003-09-02 07:30:36 -04:00
|
|
|
ZeroMemory(&DevMode, sizeof(DEVMODE));
|
2005-12-27 06:12:53 -05:00
|
|
|
// ZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICE));
|
2003-09-02 07:30:36 -04:00
|
|
|
|
2003-03-19 07:41:28 -05:00
|
|
|
DevMode.dmSize = sizeof(DEVMODE);
|
2005-12-27 06:12:53 -05:00
|
|
|
// DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
|
2005-12-26 10:06:45 -05:00
|
|
|
|
2005-12-26 16:52:12 -05:00
|
|
|
/* Multi-monitor stuff commented out since we're only really interested in the primary monitor */
|
|
|
|
/* while(EnumDisplayDevices(NULL, i++, &DisplayDevice, 0) != 0) {
|
2005-12-26 10:06:45 -05:00
|
|
|
// continue if mirroring device
|
2004-03-11 14:50:42 -05:00
|
|
|
if((DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2005-12-26 10:06:45 -05:00
|
|
|
|
2003-09-02 07:30:36 -04:00
|
|
|
j = 0;
|
2005-12-26 16:52:12 -05:00
|
|
|
while(EnumDisplaySettings((const char *) DisplayDevice.DeviceName, j++, &DevMode) != 0) {*/
|
|
|
|
while(EnumDisplaySettings(NULL, j++, &DevMode) != 0) {
|
2003-03-19 07:41:28 -05:00
|
|
|
// Filter out indexed modes
|
2004-03-10 11:47:04 -05:00
|
|
|
if (DevMode.dmBitsPerPel > 8 && ChangeDisplaySettings(&DevMode, CDS_FULLSCREEN | CDS_TEST) == DISP_CHANGE_SUCCESSFUL) {
|
2003-03-19 07:41:28 -05:00
|
|
|
jobject displayMode;
|
2005-01-16 06:12:57 -05:00
|
|
|
if (list_size <= n) {
|
|
|
|
list_size += 1;
|
|
|
|
display_mode_objects = (jobject *)realloc(display_mode_objects, sizeof(jobject)*list_size);
|
|
|
|
if (display_mode_objects == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-12-27 06:12:53 -05:00
|
|
|
displayMode = createDisplayMode(env, &DevMode);
|
2005-01-16 06:12:57 -05:00
|
|
|
display_mode_objects[n++] = displayMode;
|
2003-03-03 16:49:46 -05:00
|
|
|
}
|
2003-03-19 07:41:28 -05:00
|
|
|
}
|
2005-12-26 16:52:12 -05:00
|
|
|
// }
|
2005-05-12 03:47:07 -04:00
|
|
|
printfDebugJava(env, "Found %d displaymodes", n);
|
2005-12-26 10:06:45 -05:00
|
|
|
|
|
|
|
ret = (*env)->NewObjectArray(env, n, displayModeClass, NULL);
|
2005-01-16 06:12:57 -05:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
(*env)->SetObjectArrayElement(env, ret, i, display_mode_objects[i]);
|
|
|
|
}
|
|
|
|
free(display_mode_objects);
|
2003-03-19 07:41:28 -05:00
|
|
|
return ret;
|
2002-08-11 07:49:32 -04:00
|
|
|
}
|
|
|
|
|
2006-07-08 17:57:20 -04:00
|
|
|
void switchDisplayMode(JNIEnv * env, jobject mode) {
|
|
|
|
DEVMODE devmode;
|
|
|
|
|
2005-12-26 10:06:45 -05: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_bpp = (*env)->GetFieldID(env, cls_displayMode, "bpp", "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 bpp = (*env)->GetIntField(env, mode, fid_bpp);
|
|
|
|
int freq = (*env)->GetIntField(env, mode, fid_freq);
|
|
|
|
LONG cdsret;
|
2002-12-19 11:35:35 -05:00
|
|
|
|
2005-12-27 03:54:56 -05:00
|
|
|
ZeroMemory(&devmode, sizeof(DEVMODE));
|
2003-03-27 20:28:04 -05:00
|
|
|
devmode.dmSize = sizeof(DEVMODE);
|
|
|
|
devmode.dmBitsPerPel = bpp;
|
|
|
|
devmode.dmPelsWidth = width;
|
|
|
|
devmode.dmPelsHeight = height;
|
|
|
|
devmode.dmDisplayFrequency = freq;
|
2005-12-26 10:06:45 -05:00
|
|
|
devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
|
2003-03-27 20:28:04 -05:00
|
|
|
if (freq != 0)
|
|
|
|
devmode.dmFields |= DM_DISPLAYFREQUENCY;
|
2005-12-26 10:06:45 -05:00
|
|
|
cdsret = ChangeDisplaySettings(&devmode, CDS_FULLSCREEN);
|
2002-12-19 11:35:35 -05:00
|
|
|
|
2003-03-27 20:28:04 -05:00
|
|
|
if (cdsret != DISP_CHANGE_SUCCESSFUL) {
|
2005-12-26 16:52:12 -05:00
|
|
|
/* What's the proper way to do this multiply with 2 thing, if at all necessary? */
|
|
|
|
/* // Failed: so let's check to see if it's a wierd dual screen display
|
2005-12-26 10:06:45 -05:00
|
|
|
printfDebugJava(env, "Failed to set display mode (%ld) ... assuming dual monitors", cdsret);
|
2003-03-27 20:28:04 -05:00
|
|
|
devmode.dmPelsWidth = width * 2;
|
|
|
|
cdsret = ChangeDisplaySettings(&devmode, CDS_FULLSCREEN);
|
2002-12-19 11:35:35 -05:00
|
|
|
|
2003-03-27 20:28:04 -05:00
|
|
|
if (cdsret != DISP_CHANGE_SUCCESSFUL) {
|
2005-12-26 16:52:12 -05:00
|
|
|
printfDebugJava(env, "Failed to set display mode using dual monitors (%ld)", cdsret);*/
|
2005-12-26 10:06:45 -05:00
|
|
|
throwFormattedException(env, "Failed to set display mode (%ld).", cdsret);
|
2003-03-28 14:02:24 -05:00
|
|
|
return;
|
2005-12-26 16:52:12 -05:00
|
|
|
// }
|
2002-08-11 07:49:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-08 17:57:20 -04:00
|
|
|
static jobject createNativeGammaBuffer(JNIEnv *env) {
|
2006-07-12 14:32:58 -04:00
|
|
|
return newJavaManagedByteBuffer(env, sizeof(WORD)*3*org_lwjgl_opengl_WindowsDisplay_GAMMA_LENGTH);
|
2003-02-07 16:54:31 -05:00
|
|
|
}
|
2003-03-21 12:08:26 -05:00
|
|
|
|
2006-07-08 17:57:20 -04:00
|
|
|
jobject getCurrentGammaRamp(JNIEnv *env) {
|
|
|
|
jobject gamma_buffer;
|
|
|
|
WORD *gamma;
|
|
|
|
HDC screenDC;
|
2003-03-21 12:08:26 -05:00
|
|
|
|
2006-07-08 17:57:20 -04:00
|
|
|
gamma_buffer = createNativeGammaBuffer(env);
|
|
|
|
if (gamma_buffer == NULL)
|
|
|
|
return NULL;
|
|
|
|
gamma = (WORD *)(*env)->GetDirectBufferAddress(env, gamma_buffer);
|
2005-12-27 06:21:46 -05:00
|
|
|
|
2003-03-27 20:28:04 -05:00
|
|
|
// Get the screen
|
2006-07-08 17:57:20 -04:00
|
|
|
screenDC = GetDC(NULL);
|
|
|
|
if (screenDC == NULL) {
|
2004-07-03 06:16:28 -04:00
|
|
|
throwException(env, "Couldn't get screen DC!");
|
|
|
|
return NULL;
|
2003-03-28 14:02:24 -05:00
|
|
|
}
|
2003-03-29 16:52:14 -05:00
|
|
|
// Get the default gamma ramp
|
2006-07-08 17:57:20 -04:00
|
|
|
if (GetDeviceGammaRamp(screenDC, gamma) == FALSE) {
|
2005-05-12 03:47:07 -04:00
|
|
|
printfDebugJava(env, "Failed to get initial device gamma");
|
2003-03-29 16:52:14 -05:00
|
|
|
}
|
|
|
|
ReleaseDC(NULL, screenDC);
|
2006-07-08 17:57:20 -04:00
|
|
|
return gamma_buffer;
|
|
|
|
}
|
2005-12-27 06:21:46 -05:00
|
|
|
|
2006-07-08 17:57:20 -04:00
|
|
|
void setGammaRamp(JNIEnv * env, jobject gammaRampBuffer) {
|
|
|
|
HDC screenDC;
|
|
|
|
WORD *gammaRamp = (WORD *)(*env)->GetDirectBufferAddress(env, gammaRampBuffer);
|
|
|
|
|
|
|
|
screenDC = GetDC(NULL);
|
|
|
|
if (SetDeviceGammaRamp(screenDC, gammaRamp) == FALSE) {
|
|
|
|
throwException(env, "Failed to set device gamma.");
|
2005-12-27 06:21:46 -05:00
|
|
|
}
|
2006-07-08 17:57:20 -04:00
|
|
|
ReleaseDC(NULL, screenDC);
|
2004-07-03 06:16:28 -04:00
|
|
|
}
|
|
|
|
|
2006-07-08 17:57:20 -04:00
|
|
|
jobject convertToNativeRamp(JNIEnv *env, jobject float_gamma_obj) {
|
|
|
|
int i;
|
|
|
|
float scaledRampEntry;
|
|
|
|
WORD rampEntry;
|
|
|
|
const float *gammaRamp = (const float *)(*env)->GetDirectBufferAddress(env, float_gamma_obj);
|
|
|
|
jint gamma_ramp_length = (*env)->GetDirectBufferCapacity(env, float_gamma_obj);
|
|
|
|
jobject native_ramp;
|
|
|
|
WORD *native_ramp_buffer;
|
2004-07-03 06:16:28 -04:00
|
|
|
|
2006-07-08 17:57:20 -04:00
|
|
|
native_ramp = createNativeGammaBuffer(env);
|
|
|
|
if (native_ramp == NULL)
|
|
|
|
return NULL;
|
|
|
|
native_ramp_buffer = (WORD *)(*env)->GetDirectBufferAddress(env, native_ramp);
|
|
|
|
// Turn array of floats into array of RGB WORDs
|
2004-07-03 06:16:28 -04:00
|
|
|
|
2006-07-08 17:57:20 -04:00
|
|
|
for (i = 0; i < gamma_ramp_length; i++) {
|
|
|
|
scaledRampEntry = gammaRamp[i]*0xffff;
|
|
|
|
rampEntry = (WORD)scaledRampEntry;
|
|
|
|
native_ramp_buffer[i] = rampEntry;
|
2006-07-12 14:32:58 -04:00
|
|
|
native_ramp_buffer[i + org_lwjgl_opengl_WindowsDisplay_GAMMA_LENGTH] = rampEntry;
|
|
|
|
native_ramp_buffer[i + 2*org_lwjgl_opengl_WindowsDisplay_GAMMA_LENGTH] = rampEntry;
|
2004-09-22 14:21:28 -04:00
|
|
|
}
|
2006-07-08 17:57:20 -04:00
|
|
|
return native_ramp;
|
2004-09-22 14:21:28 -04:00
|
|
|
}
|
|
|
|
|
2006-07-08 17:57:20 -04:00
|
|
|
jobject getCurrentDisplayMode(JNIEnv * env) {
|
|
|
|
DEVMODE devmode;
|
|
|
|
if (!EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devmode)) {
|
|
|
|
throwFormattedException(env, "Couldn't get current display settings (%ld)", GetLastError());
|
|
|
|
return NULL;
|
2004-09-22 14:21:28 -04:00
|
|
|
}
|
2006-07-08 17:57:20 -04:00
|
|
|
return createDisplayMode(env, &devmode);
|
|
|
|
}
|
2004-09-22 14:21:28 -04:00
|
|
|
|
2006-07-08 17:57:20 -04:00
|
|
|
void resetDisplayMode(JNIEnv * env) {
|
2006-07-12 14:32:58 -04:00
|
|
|
// Under Windows, all we have to do is:
|
2006-07-08 17:57:20 -04:00
|
|
|
ChangeDisplaySettings(NULL, 0);
|
2003-09-28 02:55:01 -04:00
|
|
|
}
|
|
|
|
|
2005-09-23 06:39:56 -04:00
|
|
|
jstring getVersion(JNIEnv * env, char *driver)
|
2003-09-28 02:55:01 -04:00
|
|
|
{
|
2006-05-18 08:11:37 -04:00
|
|
|
#define BUFFER_SIZE 1024
|
2003-09-28 02:55:01 -04:00
|
|
|
jstring ret = NULL;
|
|
|
|
|
2006-05-18 08:11:37 -04:00
|
|
|
const char *dll_ext = ".dll";
|
2004-12-09 10:36:13 -05:00
|
|
|
DWORD var = 0;
|
2005-12-26 10:06:45 -05:00
|
|
|
DWORD dwInfoSize;
|
|
|
|
LPVOID lpInfoBuff;
|
|
|
|
BOOL bRetval;
|
2003-09-28 02:55:01 -04:00
|
|
|
|
2006-07-15 14:43:04 -04:00
|
|
|
dwInfoSize = GetFileVersionInfoSize(driver, &var);
|
2005-12-26 10:06:45 -05:00
|
|
|
lpInfoBuff = malloc(dwInfoSize);
|
2006-07-15 14:43:04 -04:00
|
|
|
bRetval = GetFileVersionInfo(driver, 0, dwInfoSize, lpInfoBuff);
|
2003-09-28 02:55:01 -04:00
|
|
|
if (bRetval == 0) {
|
|
|
|
} else {
|
|
|
|
VS_FIXEDFILEINFO * fxdFileInfo;
|
|
|
|
|
|
|
|
UINT uiLen = 0;
|
2006-07-15 14:43:04 -04:00
|
|
|
bRetval = VerQueryValue(lpInfoBuff, TEXT("\\"), (void *)&fxdFileInfo, &uiLen);
|
2003-12-15 06:49:17 -05:00
|
|
|
if (bRetval != 0) {
|
2006-05-18 08:11:37 -04:00
|
|
|
TCHAR version[BUFFER_SIZE];
|
|
|
|
TCHAR ms[BUFFER_SIZE], ls[BUFFER_SIZE];
|
|
|
|
_snprintf_s(ms, BUFFER_SIZE, _TRUNCATE, "%d.%d\0", fxdFileInfo->dwProductVersionMS >> 16, fxdFileInfo->dwProductVersionMS & 0xFFFF);
|
|
|
|
_snprintf_s(ls, BUFFER_SIZE, _TRUNCATE, "%d.%d\0", fxdFileInfo->dwProductVersionLS >> 16, fxdFileInfo->dwProductVersionLS & 0xFFFF);
|
|
|
|
_snprintf_s(version, BUFFER_SIZE, _TRUNCATE, "%s.%s\0", ms, ls);
|
2004-11-14 05:42:19 -05:00
|
|
|
ret = NewStringNative(env, version);
|
2003-09-28 02:55:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-26 10:06:45 -05:00
|
|
|
free(lpInfoBuff);
|
2003-09-28 02:55:01 -04:00
|
|
|
|
|
|
|
return ret;
|
2003-03-21 12:08:26 -05:00
|
|
|
}
|
|
|
|
|