lwjgl/src/native/win32/org_lwjgl_Display.cpp

476 lines
14 KiB
C++
Raw Normal View History

2002-08-15 11:46:18 -04: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.
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
*
2002-08-15 11:46:18 -04:00
* * 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.
*/
/**
* $Id$
2002-08-11 07:49:32 -04:00
*
2002-08-15 11:46:18 -04:00
* Win32 specific library for display handdling.
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
*/
#include "Window.h"
2002-08-11 07:49:32 -04:00
#include "org_lwjgl_Display.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
2003-09-10 18:35:06 -04:00
jobjectArray GetAvailableDisplayModesEx(JNIEnv * env);
jobjectArray GetAvailableDisplayModes(JNIEnv * env);
bool modeSet = false; // Whether we've done a display mode change
2003-03-29 16:52:14 -05:00
WORD* originalGamma = new WORD[256 * 3]; // Original gamma settings
WORD* currentGamma = new WORD[256 * 3]; // Current gamma settings
DEVMODE devmode; // Now we'll remember this value for the future
2003-02-07 16:54:31 -05:00
2002-08-11 07:49:32 -04:00
/*
* Class: org_lwjgl_Display
2003-03-03 16:51:13 -05:00
* Method: nGetAvailableDisplayModes
2002-08-11 07:49:32 -04:00
* Signature: ()[Lorg/lwjgl/DisplayMode;
*/
JNIEXPORT jobjectArray JNICALL Java_org_lwjgl_Display_nGetAvailableDisplayModes
2002-08-11 07:49:32 -04:00
(JNIEnv * env, jclass clazz)
{
jobjectArray result = GetAvailableDisplayModesEx(env);
if (result == NULL) {
#ifdef _DEBUG
printf("Extended display mode selection failed, using fallback\n");
#endif
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)
*/
2003-09-10 18:35:06 -04:00
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) {
#ifdef _DEBUG
printf("Could not load user32.dll\n");
#endif
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) {
#ifdef _DEBUG
printf("Querying %s device\n", DisplayDevice.DeviceString);
#endif
2003-09-02 07:30:36 -04:00
j = 0;
while(EnumDisplaySettingsExA(DisplayDevice.DeviceName, j++, &DevMode, 0) != 0) {
2003-09-02 07:30:36 -04:00
#ifdef _DEBUG
printf("Checking setting #%d\n", j);
#endif
2003-03-19 07:41:28 -05:00
if (DevMode.dmBitsPerPel > 8) {
AvailableModes++;
}
}
}
#ifdef _DEBUG
2003-03-19 07:41:28 -05:00
printf("Found %d displaymodes\n", AvailableModes);
#endif
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
2002-08-24 17:19:06 -04:00
jclass displayModeClass = env->FindClass("org/lwjgl/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) {
2003-09-02 07:30:36 -04:00
j = 0;
while(EnumDisplaySettingsExA(DisplayDevice.DeviceName, j++, &DevMode, 0) != 0) {
2003-03-19 07:41:28 -05:00
// Filter out indexed modes
if (DevMode.dmBitsPerPel > 8) {
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)
*/
2003-09-10 18:35:06 -04:00
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) {
2003-03-19 07:41:28 -05:00
if (DevMode.dmBitsPerPel > 8) {
AvailableModes++;
}
}
#ifdef _DEBUG
2003-03-19 07:41:28 -05:00
printf("Found %d displaymodes\n", AvailableModes);
#endif
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
jclass displayModeClass = env->FindClass("org/lwjgl/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
if (DevMode.dmBitsPerPel > 8) {
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
}
2002-08-11 07:49:32 -04:00
/*
* Class: org_lwjgl_Display
* Method: getPlatform
* Signature: ()I
2002-08-11 07:49:32 -04:00
*/
JNIEXPORT jint JNICALL Java_org_lwjgl_Display_getPlatform
(JNIEnv * env, jclass clazz)
2002-08-11 07:49:32 -04:00
{
return org_lwjgl_Display_PLATFORM_WGL;
}
/*
* Class: org_lwjgl_Display
* Method: setDisplayMode
* Signature: (Lorg/lwjgl/DisplayMode;)V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_Display_setDisplayMode
(JNIEnv * env, jclass clazz, jobject mode)
{
2003-03-28 14:02:24 -05:00
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");
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;
2003-02-12 04:33:11 -05: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
#ifdef _DEBUG
printf("Failed to set display mode... assuming dual monitors\n");
#endif
devmode.dmPelsWidth = width * 2;
cdsret = ChangeDisplaySettings(&devmode, CDS_FULLSCREEN);
if (cdsret != DISP_CHANGE_SUCCESSFUL) {
#ifdef _DEBUG
printf("Failed to set display mode using dual monitors\n");
#endif
throwException(env, "Failed to set display mode.");
2003-03-28 14:02:24 -05:00
return;
}
2002-08-11 07:49:32 -04:00
}
// The change was successful but might not be the exact change we were expecting.
// Now we'll construct a new DisplayMode instance and stash it back in the Display
// class's mode instance variable.
// Get the screen
2003-03-29 16:52:14 -05:00
HDC screenDC = GetDC(NULL);
// Get the device caps
width = GetDeviceCaps(screenDC, HORZRES);
height = GetDeviceCaps(screenDC, VERTRES);
bpp = GetDeviceCaps(screenDC, COLORRES);
freq = GetDeviceCaps(screenDC, VREFRESH);
if (freq <= 1)
freq = 0; // Unknown
2003-03-29 16:52:14 -05:00
ReleaseDC(NULL, screenDC);
2003-03-28 14:02:24 -05:00
jmethodID ctor = env->GetMethodID(cls_displayMode, "<init>", "(IIII)V");
jobject newMode = env->NewObject(cls_displayMode, ctor, width, height, bpp, freq);
jfieldID fid_initialMode = env->GetStaticFieldID(clazz, "mode", "Lorg/lwjgl/DisplayMode;");
env->SetStaticObjectField(clazz, fid_initialMode, newMode);
env->DeleteLocalRef(newMode);
2002-08-17 10:13:12 -04:00
modeSet = true;
2002-08-11 07:49:32 -04:00
}
/*
* Class: org_lwjgl_Display
* Method: resetDisplayMode
2002-08-11 07:49:32 -04:00
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_Display_resetDisplayMode
2002-08-11 07:49:32 -04:00
(JNIEnv * env, jclass clazz)
{
2003-09-10 18:39:46 -04:00
2003-03-29 16:52:14 -05:00
// Return device gamma to normal
HDC screenDC = GetDC(NULL);
2003-08-07 17:53:06 -04:00
try {
if (!SetDeviceGammaRamp(screenDC, originalGamma)) {
#ifdef _DEBUG
printf("Could not reset device gamma\n");
#endif
}
} catch (...) {
printf("Exception occurred in SetDeviceGammaRamp\n");
}
2003-03-29 16:52:14 -05:00
ReleaseDC(NULL, screenDC);
2003-09-10 18:39:46 -04:00
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
Java_org_lwjgl_Display_init(env, clazz);
}
2002-08-11 07:49:32 -04:00
}
/*
* Temporarily reset display settings. This is called when the window is minimized.
*/
void tempResetDisplayMode() {
// Return device gamma to normal
HDC screenDC = GetDC(NULL);
2003-08-07 17:53:06 -04:00
try {
if (!SetDeviceGammaRamp(screenDC, originalGamma)) {
#ifdef _DEBUG
printf("Could not reset device gamma\n");
#endif
}
} catch (...) {
printf("Exception occurred in SetDeviceGammaRamp\n");
}
ReleaseDC(NULL, screenDC);
2003-09-10 18:39:46 -04:00
if (modeSet) {
#ifdef _DEBUG
printf("Attempting to temporarily reset the display mode\n");
#endif
modeSet = false;
// Under Win32, all we have to do is:
ChangeDisplaySettings(NULL, 0);
}
}
/*
* Put display settings back to what they were when the window is maximized.
*/
void tempRestoreDisplayMode() {
// Restore gamma
HDC screenDC = GetDC(NULL);
2003-08-07 17:53:06 -04:00
try {
if (!SetDeviceGammaRamp(screenDC, currentGamma)) {
#ifdef _DEBUG
printf("Could not restore device gamma\n");
#endif
}
} catch (...) {
printf("Exception occurred in SetDeviceGammaRamp\n");
}
ReleaseDC(NULL, screenDC);
2003-09-10 18:39:46 -04:00
if (!modeSet) {
#ifdef _DEBUG
printf("Attempting to restore the display mode\n");
#endif
modeSet = true;
LONG cdsret = ChangeDisplaySettings(&devmode, CDS_FULLSCREEN);
#ifdef _DEBUG
if (cdsret != DISP_CHANGE_SUCCESSFUL) {
printf("Failed to restore display mode\n");
}
#endif
}
}
2003-02-07 16:54:31 -05:00
/*
* Class: org_lwjgl_Display
* Method: getGammaRampLength
* Signature: ()I
2003-02-07 16:54:31 -05:00
*/
JNIEXPORT jint JNICALL Java_org_lwjgl_Display_getGammaRampLength
(JNIEnv *env, jclass clazz)
2003-02-07 16:54:31 -05:00
{
return 256;
}
2003-02-07 16:54:31 -05:00
/*
* Class: org_lwjgl_Display
* Method: setGammaRamp
* Signature: (I)Z
*/
2003-03-29 16:52:14 -05:00
JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_setGammaRamp
2003-06-24 08:24:55 -04:00
(JNIEnv * env, jclass clazz, 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
}
jboolean ret;
HDC screenDC = GetDC(NULL);
2003-08-07 17:53:06 -04:00
try {
if (SetDeviceGammaRamp(screenDC, currentGamma) == FALSE) {
#ifdef _DEBUG
printf("Failed to set device gamma\n");
#endif
ret = JNI_FALSE;
} else {
ret = JNI_TRUE;
}
} catch (...) {
printf("Exception occurred in SetDeviceGammaRamp\n");
2003-03-29 16:52:14 -05:00
ret = JNI_FALSE;
}
ReleaseDC(NULL, screenDC);
return ret;
2003-02-07 16:54:31 -05:00
}
2003-03-21 12:08:26 -05:00
/*
* Class: org_lwjgl_Display
* Method: init
* Signature: ()V
2003-03-21 12:08:26 -05:00
*/
JNIEXPORT void JNICALL Java_org_lwjgl_Display_init
2003-03-21 12:08:26 -05:00
(JNIEnv * env, jclass clazz)
{
// 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) {
printf("Couldn't get screen DC!\n");
return;
}
// Get the device caps
int width = GetDeviceCaps(screenDC, HORZRES);
int height = GetDeviceCaps(screenDC, VERTRES);
int bpp = GetDeviceCaps(screenDC, COLORRES);
int freq = GetDeviceCaps(screenDC, VREFRESH);
if (freq <= 1)
freq = 0; // Unknown
jclass jclass_DisplayMode = env->FindClass("org/lwjgl/DisplayMode");
jmethodID ctor = env->GetMethodID(jclass_DisplayMode, "<init>", "(IIII)V");
jobject newMode = env->NewObject(jclass_DisplayMode, ctor, width, height, bpp, freq);
2003-03-28 14:02:24 -05:00
jfieldID fid_initialMode = env->GetStaticFieldID(clazz, "mode", "Lorg/lwjgl/DisplayMode;");
env->SetStaticObjectField(clazz, fid_initialMode, newMode);
env->DeleteLocalRef(newMode);
2003-03-29 16:52:14 -05:00
// Get the default gamma ramp
2003-08-07 17:53:06 -04:00
try {
if (GetDeviceGammaRamp(screenDC, originalGamma) == FALSE) {
#ifdef _DEBUG
printf("Failed to get initial device gamma\n");
#endif
}
} catch (...) {
printf("Exception occurred in GetDeviceGammaRamp\n");
2003-03-29 16:52:14 -05:00
}
2003-09-10 18:39:46 -04:00
2003-03-29 16:52:14 -05:00
ReleaseDC(NULL, screenDC);
2003-03-21 12:08:26 -05:00
}