lwjgl/src/native/win32/display.cpp

439 lines
14 KiB
C++
Raw Normal View History

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.
*/
/**
* $Id$
2002-08-11 07:49:32 -04:00
*
2004-07-03 06:16:28 -04:00
* Win32 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>
#include <jni.h>
#include "display.h"
2003-10-11 12:29:40 -04:00
#include "common_tools.h"
2002-08-15 10:53:18 -04:00
2002-08-17 10:13:12 -04:00
#define WINDOWCLASSNAME "LWJGLWINDOW"
2002-08-11 07:49:32 -04:00
2004-11-09 06:00:49 -05:00
#define GAMMA_SIZE (3*256)
static jobjectArray GetAvailableDisplayModesEx(JNIEnv * env);
static jobjectArray GetAvailableDisplayModes(JNIEnv * env);
2004-05-23 09:54:55 -04:00
static char * getDriver();
2004-11-09 06:00:49 -05:00
static bool modeSet = false; // Whether we've done a display mode change
2004-11-13 17:27:19 -05:00
static WORD originalGamma[GAMMA_SIZE]; // Original gamma settings
static WORD currentGamma[GAMMA_SIZE]; // Current gamma settings
2004-11-09 06:00:49 -05:00
static DEVMODE devmode; // Now we'll remember this value for the future
extern HWND display_hwnd; // Handle to the window
2004-09-15 13:07:06 -04:00
extern RECT clientSize;
2004-11-09 06:00:49 -05:00
static char * driver = getDriver();
2003-02-07 16:54:31 -05:00
2004-07-03 06:16:28 -04:00
jobjectArray getAvailableDisplayModes(JNIEnv *env)
2002-08-11 07:49:32 -04:00
{
jobjectArray result = GetAvailableDisplayModesEx(env);
if (result == NULL) {
2003-12-20 17:03:25 -05:00
printfDebug("Extended display mode selection failed, using fallback\n");
result = GetAvailableDisplayModes(env);
2003-09-10 18:35:06 -04:00
}
return result;
}
/**
2003-09-10 18:35:06 -04:00
* Choose displaymodes using extended codepath (multiple displaydevices)
*/
static jobjectArray GetAvailableDisplayModesEx(JNIEnv * env) {
typedef BOOL (WINAPI * EnumDisplayDevicesAPROC)(IN LPCSTR lpDevice, IN DWORD iDevNum, OUT PDISPLAY_DEVICEA lpDisplayDevice, IN DWORD dwFlags);
typedef BOOL (WINAPI * EnumDisplaySettingsExAPROC)(IN LPCSTR lpszDeviceName, IN DWORD iModeNum, OUT LPDEVMODEA lpDevMode, IN DWORD dwFlags);
EnumDisplayDevicesAPROC EnumDisplayDevicesA;
EnumDisplaySettingsExAPROC EnumDisplaySettingsExA;
HMODULE lib_handle = LoadLibrary("user32.dll");
if (lib_handle == NULL) {
2003-12-20 17:03:25 -05:00
printfDebug("Could not load user32.dll\n");
return NULL;
}
EnumDisplayDevicesA = (EnumDisplayDevicesAPROC)GetProcAddress(lib_handle, "EnumDisplayDevicesA");
if (EnumDisplayDevicesA == NULL)
return NULL;
EnumDisplaySettingsExA = (EnumDisplaySettingsExAPROC)GetProcAddress(lib_handle, "EnumDisplaySettingsExA");
if (EnumDisplaySettingsExA == NULL)
return NULL;
2003-03-19 07:41:28 -05:00
int i = 0, j = 0, n = 0;
int AvailableModes = 0;
2003-03-19 07:41:28 -05:00
DISPLAY_DEVICE DisplayDevice;
DEVMODE DevMode;
2003-09-02 07:30:36 -04:00
ZeroMemory(&DevMode, sizeof(DEVMODE));
ZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICE));
2003-03-19 07:41:28 -05:00
DevMode.dmSize = sizeof(DEVMODE);
DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
//enumerate all displays, and all of their displaymodes
while(EnumDisplayDevicesA(NULL, i++, &DisplayDevice, 0) != 0) {
2004-03-11 14:50:42 -05:00
// continue if mirroring device
if((DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER) != 0) {
continue;
}
2004-03-10 11:47:04 -05:00
// go ahead
2003-12-20 17:03:25 -05:00
printfDebug("Querying %s device\n", DisplayDevice.DeviceString);
2003-09-02 07:30:36 -04:00
j = 0;
while(EnumDisplaySettingsExA((const char *) DisplayDevice.DeviceName, j++, &DevMode, 0) != 0) {
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
AvailableModes++;
}
}
}
2003-12-20 17:03:25 -05:00
printfDebug("Found %d displaymodes\n", AvailableModes);
2003-03-19 07:41:28 -05:00
// now that we have the count create the classes, and add 'em all - we'll remove dups in Java
// Allocate an array of DisplayModes big enough
2004-07-03 06:16:28 -04:00
jclass displayModeClass = env->FindClass("org/lwjgl/opengl/DisplayMode");
2002-12-22 14:52:44 -05:00
2003-03-19 07:41:28 -05:00
jobjectArray ret = env->NewObjectArray(AvailableModes, displayModeClass, NULL);
jmethodID displayModeConstructor = env->GetMethodID(displayModeClass, "<init>", "(IIII)V");
2003-09-02 07:30:36 -04:00
i = 0, n = 0;
while(EnumDisplayDevicesA(NULL, i++, &DisplayDevice, 0) != 0) {
2004-03-10 11:47:04 -05:00
// continue if mirroring device
2004-03-11 14:50:42 -05:00
if((DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER) != 0) {
continue;
}
2004-03-10 11:47:04 -05:00
2003-09-02 07:30:36 -04:00
j = 0;
while(EnumDisplaySettingsExA((const char *) DisplayDevice.DeviceName, j++, &DevMode, 0) != 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;
displayMode = env->NewObject(displayModeClass, displayModeConstructor,
DevMode.dmPelsWidth, DevMode.dmPelsHeight,
DevMode.dmBitsPerPel, DevMode.dmDisplayFrequency);
env->SetObjectArrayElement(ret, n++, displayMode);
}
2003-03-19 07:41:28 -05:00
}
}
FreeLibrary(lib_handle);
2003-03-19 07:41:28 -05:00
return ret;
}
2002-12-22 14:52:44 -05:00
/**
2003-09-10 18:35:06 -04:00
* Choose displaymodes using standard codepath (single displaydevice)
*/
static jobjectArray GetAvailableDisplayModes(JNIEnv * env) {
2003-03-19 07:41:28 -05:00
int i = 0, j = 0, n = 0;
int AvailableModes = 0;
2003-03-19 07:41:28 -05:00
DEVMODE DevMode;
2003-09-02 07:30:36 -04:00
ZeroMemory(&DevMode, sizeof(DEVMODE));
2003-03-19 07:41:28 -05:00
DevMode.dmSize = sizeof(DEVMODE);
2003-03-19 07:41:28 -05:00
//enumerate all displaymodes
2003-09-10 18:55:25 -04:00
while(EnumDisplaySettings(NULL, j++, &DevMode) != 0) {
2004-03-10 11:52:31 -05:00
if (DevMode.dmBitsPerPel > 8 && ChangeDisplaySettings(&DevMode, CDS_FULLSCREEN | CDS_TEST) == DISP_CHANGE_SUCCESSFUL) {
2003-03-19 07:41:28 -05:00
AvailableModes++;
}
}
2003-12-20 17:03:25 -05:00
printfDebug("Found %d displaymodes\n", AvailableModes);
2003-03-19 07:41:28 -05:00
// now that we have the count create the classes, and add 'em all - we'll remove dups in Java
// Allocate an array of DisplayModes big enough
2004-07-03 06:16:28 -04:00
jclass displayModeClass = env->FindClass("org/lwjgl/opengl/DisplayMode");
2002-12-22 14:52:44 -05:00
jobjectArray ret = env->NewObjectArray(AvailableModes, displayModeClass, NULL);
2003-03-19 07:41:28 -05:00
jmethodID displayModeConstructor = env->GetMethodID(displayModeClass, "<init>", "(IIII)V");
2003-03-19 07:41:28 -05:00
i = 0, j = 0, n = 0;
2003-09-10 18:55:25 -04:00
while(EnumDisplaySettings(NULL, j++, &DevMode) != 0) {
2003-03-19 07:41:28 -05:00
// Filter out indexed modes
2004-03-10 11:52:31 -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;
displayMode = env->NewObject(displayModeClass, displayModeConstructor,
DevMode.dmPelsWidth, DevMode.dmPelsHeight,
DevMode.dmBitsPerPel, DevMode.dmDisplayFrequency);
env->SetObjectArrayElement(ret, n++, displayMode);
}
}
return ret;
2002-08-11 07:49:32 -04:00
}
2004-07-03 06:16:28 -04:00
void switchDisplayMode(JNIEnv * env, jobject mode)
{
2004-07-03 06:16:28 -04:00
jclass cls_displayMode = env->GetObjectClass(mode);
2003-03-28 14:02:24 -05:00
jfieldID fid_width = env->GetFieldID(cls_displayMode, "width", "I");
jfieldID fid_height = env->GetFieldID(cls_displayMode, "height", "I");
jfieldID fid_bpp = env->GetFieldID(cls_displayMode, "bpp", "I");
jfieldID fid_freq = env->GetFieldID(cls_displayMode, "freq", "I");
2003-01-31 14:04:45 -05:00
int width = env->GetIntField(mode, fid_width);
int height = env->GetIntField(mode, fid_height);
int bpp = env->GetIntField(mode, fid_bpp);
int freq = env->GetIntField(mode, fid_freq);
devmode.dmSize = sizeof(DEVMODE);
devmode.dmBitsPerPel = bpp;
devmode.dmPelsWidth = width;
devmode.dmPelsHeight = height;
devmode.dmDisplayFlags = 0;
devmode.dmDisplayFrequency = freq;
devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFLAGS;
if (freq != 0)
devmode.dmFields |= DM_DISPLAYFREQUENCY;
2004-09-15 13:07:06 -04:00
LONG cdsret = ChangeDisplaySettings(&devmode, CDS_FULLSCREEN);
if (cdsret != DISP_CHANGE_SUCCESSFUL) {
// Failed: so let's check to see if it's a wierd dual screen display
2003-12-20 17:03:25 -05:00
printfDebug("Failed to set display mode... assuming dual monitors\n");
devmode.dmPelsWidth = width * 2;
cdsret = ChangeDisplaySettings(&devmode, CDS_FULLSCREEN);
if (cdsret != DISP_CHANGE_SUCCESSFUL) {
2003-12-20 17:03:25 -05:00
printfDebug("Failed to set display mode using dual monitors\n");
throwException(env, "Failed to set display mode.");
2003-03-28 14:02:24 -05:00
return;
}
2002-08-11 07:49:32 -04:00
}
modeSet = true;
2002-08-11 07:49:32 -04:00
}
2004-07-03 06:16:28 -04:00
int getGammaRampLength(void)
2003-02-07 16:54:31 -05:00
{
return 256;
}
2003-02-07 16:54:31 -05:00
2004-07-03 06:16:28 -04:00
void setGammaRamp(JNIEnv * env, jobject gammaRampBuffer)
{
2003-06-24 08:24:55 -04:00
const float *gammaRamp = (const float *)env->GetDirectBufferAddress(gammaRampBuffer);
// Turn array of floats into array of RGB WORDs
2003-03-29 16:52:14 -05:00
for (int i = 0; i < 256; i ++) {
float scaledRampEntry = gammaRamp[i]*0xffff;
WORD rampEntry = (WORD)scaledRampEntry;
currentGamma[i] = rampEntry;
currentGamma[i + 256] = rampEntry;
currentGamma[i + 512] = rampEntry;
2003-03-29 16:52:14 -05:00
}
HDC screenDC = GetDC(NULL);
2003-10-23 03:18:07 -04:00
if (SetDeviceGammaRamp(screenDC, currentGamma) == FALSE) {
throwException(env, "Failed to set device gamma.");
2003-03-29 16:52:14 -05:00
}
ReleaseDC(NULL, screenDC);
2003-02-07 16:54:31 -05:00
}
2003-03-21 12:08:26 -05:00
2004-07-03 06:16:28 -04:00
jobject initDisplay(JNIEnv * env)
2003-03-21 12:08:26 -05:00
{
// Determine the current screen resolution
// Get the screen
2003-03-29 16:52:14 -05:00
HDC screenDC = GetDC(NULL);
2003-03-28 14:02:24 -05:00
if (!screenDC) {
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
}
// Get the device caps
int width = GetDeviceCaps(screenDC, HORZRES);
int height = GetDeviceCaps(screenDC, VERTRES);
int bpp = GetDeviceCaps(screenDC, BITSPIXEL);
int freq = GetDeviceCaps(screenDC, VREFRESH);
if (freq <= 1)
freq = 0; // Unknown
2004-07-03 06:16:28 -04:00
jclass jclass_DisplayMode = env->FindClass("org/lwjgl/opengl/DisplayMode");
jmethodID ctor = env->GetMethodID(jclass_DisplayMode, "<init>", "(IIII)V");
jobject newMode = env->NewObject(jclass_DisplayMode, ctor, width, height, bpp, freq);
2003-03-29 16:52:14 -05:00
// Get the default gamma ramp
2004-05-10 04:55:09 -04:00
if (GetDeviceGammaRamp(screenDC, originalGamma) == FALSE) {
printfDebug("Failed to get initial device gamma\n");
2003-03-29 16:52:14 -05:00
}
2004-11-09 06:00:49 -05:00
memcpy(currentGamma, originalGamma, sizeof(WORD)*GAMMA_SIZE);
2003-03-29 16:52:14 -05:00
ReleaseDC(NULL, screenDC);
2004-07-03 06:16:28 -04:00
return newMode;
}
void resetDisplayMode(JNIEnv * env) {
2004-07-03 06:16:28 -04:00
// Return device gamma to normal
HDC screenDC = GetDC(NULL);
if (!SetDeviceGammaRamp(screenDC, originalGamma)) {
printfDebug("Could not reset device gamma\n");
}
ReleaseDC(NULL, screenDC);
if (modeSet) {
modeSet = false;
// Under Win32, all we have to do is:
ChangeDisplaySettings(NULL, 0);
// And we'll call init() again to put the correct mode back in Display
if (env != NULL)
initDisplay(env);
}
}
/*
* Put display settings back to what they were when the window is maximized.
*/
void restoreDisplayMode(void) {
// Restore gamma
HDC screenDC = GetDC(NULL);
if (!SetDeviceGammaRamp(screenDC, currentGamma)) {
printfDebug("Could not restore device gamma\n");
}
ReleaseDC(NULL, screenDC);
if (!modeSet) {
printfDebug("Attempting to restore the display mode\n");
modeSet = true;
LONG cdsret = ChangeDisplaySettings(&devmode, CDS_FULLSCREEN);
if (cdsret != DISP_CHANGE_SUCCESSFUL) {
printfDebug("Failed to restore display mode\n");
}
2004-07-03 06:16:28 -04:00
}
2003-09-28 02:55:01 -04:00
}
static char * getDriver() {
#define MY_BUFSIZE 256
2003-09-28 02:55:01 -04:00
HKEY hKey;
static TCHAR szAdapterKey[MY_BUFSIZE], szDriverValue[MY_BUFSIZE];
DWORD dwBufLen = MY_BUFSIZE;
LONG lRet;
2003-09-28 02:55:01 -04:00
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
TEXT("HARDWARE\\DeviceMap\\Video"),
0,
KEY_QUERY_VALUE,
&hKey) != ERROR_SUCCESS) return NULL;
2003-09-28 02:55:01 -04:00
lRet = RegQueryValueEx(hKey,
TEXT("\\Device\\Video0"),
NULL,
NULL,
(LPBYTE)szAdapterKey,
&dwBufLen);
2003-09-28 02:55:01 -04:00
RegCloseKey(hKey);
2003-09-28 02:55:01 -04:00
if(lRet != ERROR_SUCCESS) return NULL;
2003-09-28 02:55:01 -04:00
2003-12-20 17:03:25 -05:00
printfDebug("Adapter key: %s\n", szAdapterKey);
2003-09-28 02:55:01 -04:00
// szAdapterKey now contains something like \Registry\Machine\System\CurrentControlSet\Control\Video\{B70DBD2A-90C4-41CF-A58E-F3BA69F1A6BC}\0000
// We'll check for the first chunk:
if (strnicmp("\\Registry\\Machine", szAdapterKey, 17) == 0) {
2003-09-28 02:55:01 -04:00
// Yes, it's right, so let's look for that key now
TCHAR szDriverKey[MY_BUFSIZE];
strcpy(szDriverKey, &szAdapterKey[18]);
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
TEXT(szDriverKey),
0,
KEY_QUERY_VALUE,
&hKey) != ERROR_SUCCESS) return NULL;
lRet = RegQueryValueEx(hKey,
TEXT("InstalledDisplayDrivers"),
NULL,
NULL,
(LPBYTE)szDriverValue,
&dwBufLen);
RegCloseKey(hKey);
}
if(lRet != ERROR_SUCCESS) return NULL;
return szDriverValue;
}
2004-07-03 06:16:28 -04:00
jstring getAdapter(JNIEnv * env)
2003-09-28 02:55:01 -04:00
{
jstring ret = NULL;
if (driver == NULL) {
return NULL;
}
ret = NewStringNative(env, driver);
2003-09-28 02:55:01 -04:00
return ret;
}
2004-07-03 06:16:28 -04:00
jstring getVersion(JNIEnv * env)
2003-09-28 02:55:01 -04:00
{
jstring ret = NULL;
TCHAR driverDLL[256] = "\0";
if (driver == NULL) {
return NULL;
}
strcat(driverDLL, driver);
strcat(driverDLL, ".dll");
DWORD var = 0;
DWORD dwInfoSize = GetFileVersionInfoSize(driverDLL, &var);
LPVOID lpInfoBuff = new unsigned char[dwInfoSize];
BOOL bRetval = GetFileVersionInfo(driverDLL, NULL, dwInfoSize, lpInfoBuff);
if (bRetval == 0) {
} else {
VS_FIXEDFILEINFO * fxdFileInfo;
UINT uiLen = 0;
bRetval = VerQueryValue(lpInfoBuff, TEXT("\\"), (void **) &fxdFileInfo, &uiLen);
if (bRetval != 0) {
2003-09-28 02:55:01 -04:00
TCHAR version[256];
TCHAR ms[10], ls[10];
sprintf(ms, "%d.%d\0", fxdFileInfo->dwProductVersionMS >> 16, fxdFileInfo->dwProductVersionMS & 0xFFFF);
sprintf(ls, "%d.%d\0", fxdFileInfo->dwProductVersionLS >> 16, fxdFileInfo->dwProductVersionLS & 0xFFFF);
sprintf(version, "%s.%s\0", ms, ls);
ret = NewStringNative(env, version);
2003-09-28 02:55:01 -04:00
}
}
delete lpInfoBuff;
return ret;
2003-03-21 12:08:26 -05:00
}