diff --git a/CMakeLists.txt b/CMakeLists.txt index b2313305..ba17da7e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ set(common_SOURCES ${GLFW_SOURCE_DIR}/src/enable.c ${GLFW_SOURCE_DIR}/src/error.c ${GLFW_SOURCE_DIR}/src/fullscreen.c + ${GLFW_SOURCE_DIR}/src/gamma.c ${GLFW_SOURCE_DIR}/src/glext.c ${GLFW_SOURCE_DIR}/src/init.c ${GLFW_SOURCE_DIR}/src/input.c @@ -67,14 +68,12 @@ if (UNIX AND NOT APPLE AND NOT CYGWIN) endif(X11_XRANDR_FOUND) # Check for Xf86VidMode (fallback legacy resolution switching extension) - if (NOT X11_XRANDR_FOUND) - CHECK_X11_XF86VIDMODE() - if (X11_XF86VIDMODE_FOUND) - set(_GLFW_HAS_XF86VIDMODE 1) - list(APPEND GLFW_INCLUDE_DIR ${X11_XF86VIDMODE_INCLUDE_DIR}) - list(APPEND GLFW_LIBRARIES ${X11_XF86VIDMODE_LIBRARIES}) - endif(X11_XF86VIDMODE_FOUND) - endif (NOT X11_XRANDR_FOUND) + CHECK_X11_XF86VIDMODE() + if (X11_XF86VIDMODE_FOUND) + set(_GLFW_HAS_XF86VIDMODE 1) + list(APPEND GLFW_INCLUDE_DIR ${X11_XF86VIDMODE_INCLUDE_DIR}) + list(APPEND GLFW_LIBRARIES ${X11_XF86VIDMODE_LIBRARIES}) + endif(X11_XF86VIDMODE_FOUND) CHECK_FUNCTION_EXISTS(glXGetProcAddress _GLFW_HAS_GLXGETPROCADDRESS) diff --git a/include/GL/glfw3.h b/include/GL/glfw3.h index 138c7c50..08e0d006 100644 --- a/include/GL/glfw3.h +++ b/include/GL/glfw3.h @@ -361,6 +361,8 @@ extern "C" { #define GLFW_VERSION_UNAVAILABLE 0x00070007 #define GLFW_PLATFORM_ERROR 0x00070008 +/* Gamma ramps */ +#define GLFW_GAMMA_RAMP_SIZE 256 /************************************************************************* * Typedefs @@ -379,6 +381,14 @@ typedef struct int greenBits; } GLFWvidmode; +/* Gamma ramp */ +typedef struct +{ + unsigned short red[GLFW_GAMMA_RAMP_SIZE]; + unsigned short green[GLFW_GAMMA_RAMP_SIZE]; + unsigned short blue[GLFW_GAMMA_RAMP_SIZE]; +} GLFWgammaramp; + /* Function pointer types */ typedef void (* GLFWwindowsizefun)(GLFWwindow,int,int); typedef int (* GLFWwindowclosefun)(GLFWwindow); @@ -410,6 +420,11 @@ GLFWAPI const char* glfwErrorString(int error); GLFWAPI int glfwGetVideoModes(GLFWvidmode* list, int maxcount); GLFWAPI void glfwGetDesktopMode(GLFWvidmode* mode); +/* Gamma ramp functions */ +GLFWAPI void glfwSetGammaFormula(float gamma, float blacklevel, float gain); +GLFWAPI void glfwGetGammaRamp(GLFWgammaramp* ramp); +GLFWAPI void glfwSetGammaRamp(const GLFWgammaramp* ramp); + /* Window handling */ GLFWAPI GLFWwindow glfwOpenWindow(int width, int height, int mode, const char* title, GLFWwindow share); GLFWAPI void glfwOpenWindowHint(int target, int hint); diff --git a/readme.html b/readme.html index a11b614d..1482a13d 100644 --- a/readme.html +++ b/readme.html @@ -802,6 +802,9 @@ their skills. Special thanks go out to:

  • Jonathan Dummer, for submitting a patch fixing an input bug on Win32 and adding logic for the GLFW_ICON resource
  • +
  • Ralph Eastwood, for the initial design of the gamma correction API and + the Win32 and X11 implementations of it
  • +
  • Gerald Franz, who made GLFW compile under IRIX, and supplied patches for the X11 keyboard translation routine
  • diff --git a/src/gamma.c b/src/gamma.c new file mode 100644 index 00000000..ff97d600 --- /dev/null +++ b/src/gamma.c @@ -0,0 +1,115 @@ +//======================================================================== +// GLFW - An OpenGL framework +// Platform: Any +// API version: 3.0 +// WWW: http://www.glfw.org/ +//------------------------------------------------------------------------ +// Copyright (c) 2010 Camilla Berglund +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would +// be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not +// be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source +// distribution. +// +//======================================================================== + +#include "internal.h" + +#include +#include + + +////////////////////////////////////////////////////////////////////////// +////// GLFW public API ////// +////////////////////////////////////////////////////////////////////////// + +//======================================================================== +// Calculate the gamma ramp table from specified values +//======================================================================== + +GLFWAPI void glfwSetGammaFormula(float gamma, float blacklevel, float gain) +{ + int i, size = 256; + GLFWgammaramp ramp; + + if (!_glfwInitialized) + { + _glfwSetError(GLFW_NOT_INITIALIZED); + return; + } + + for (i = 0; i < size; i++) + { + float value = (float) i / ((float) (size - 1)); + + // Apply gamma + value = pow(value, gamma) * 65535.f + 0.5f; + + // Apply gain + value = gain * (value - 32767.5f) + 32767.5f; + + // Apply black-level + value += blacklevel * 65535.f; + + // Clamp values + if (value < 0.f) + value = 0.f; + else if (value > 65535.f) + value = 65535.f; + + // Set the gamma ramp values + ramp.red[i] = (unsigned short) value; + ramp.green[i] = (unsigned short) value; + ramp.blue[i] = (unsigned short) value; + } + + glfwSetGammaRamp(&ramp); +} + + +//======================================================================== +// Return the currently set gamma ramp +//======================================================================== + +GLFWAPI void glfwGetGammaRamp(GLFWgammaramp* ramp) +{ + if (!_glfwInitialized) + { + _glfwSetError(GLFW_NOT_INITIALIZED); + return; + } + + *ramp = _glfwLibrary.currentRamp; +} + + +//======================================================================== +// Make the specified gamma ramp current +//======================================================================== + +GLFWAPI void glfwSetGammaRamp(const GLFWgammaramp* ramp) +{ + if (!_glfwInitialized) + { + _glfwSetError(GLFW_NOT_INITIALIZED); + return; + } + + _glfwPlatformSetGammaRamp(ramp); + _glfwLibrary.currentRamp = *ramp; +} + diff --git a/src/internal.h b/src/internal.h index d073d44b..fb7fef90 100644 --- a/src/internal.h +++ b/src/internal.h @@ -215,12 +215,16 @@ struct _GLFWwindow //------------------------------------------------------------------------ struct _GLFWlibrary { - _GLFWhints hints; + _GLFWhints hints; - _GLFWwindow* windowListHead; - _GLFWwindow* currentWindow; - _GLFWwindow* activeWindow; - _GLFWwindow* cursorLockWindow; + _GLFWwindow* windowListHead; + _GLFWwindow* currentWindow; + _GLFWwindow* activeWindow; + _GLFWwindow* cursorLockWindow; + + GLFWgammaramp currentRamp; + GLFWgammaramp originalRamp; + int originalRampSize; _GLFW_PLATFORM_LIBRARY_STATE; }; @@ -257,6 +261,10 @@ void _glfwPlatformDisableSystemKeys(_GLFWwindow* window); int _glfwPlatformGetVideoModes(GLFWvidmode* list, int maxcount); void _glfwPlatformGetDesktopMode(GLFWvidmode* mode); +// Gamma ramp +void _glfwPlatformSaveGammaRamp(void); +void _glfwPlatformSetGammaRamp(const GLFWgammaramp* ramp); + // Joystick int _glfwPlatformGetJoystickParam(int joy, int param); int _glfwPlatformGetJoystickPos(int joy, float* pos, int numaxes); diff --git a/src/win32/CMakeLists.txt b/src/win32/CMakeLists.txt index 50b160d4..f7df1ff7 100644 --- a/src/win32/CMakeLists.txt +++ b/src/win32/CMakeLists.txt @@ -23,6 +23,7 @@ set(libglfw_SOURCES ${common_SOURCES} win32_enable.c win32_fullscreen.c + win32_gamma.c win32_glext.c win32_init.c win32_joystick.c diff --git a/src/win32/platform.h b/src/win32/platform.h index 8a2a2ffa..fb9107b8 100644 --- a/src/win32/platform.h +++ b/src/win32/platform.h @@ -156,6 +156,8 @@ typedef int (WINAPI * DESCRIBEPIXELFORMAT_T) (HDC,int,UINT,LPPIXELFORMATDESCRIP typedef int (WINAPI * GETPIXELFORMAT_T) (HDC); typedef BOOL (WINAPI * SETPIXELFORMAT_T) (HDC,int,const PIXELFORMATDESCRIPTOR*); typedef BOOL (WINAPI * SWAPBUFFERS_T) (HDC); +typedef BOOL (WINAPI * GETDEVICEGAMMARAMP_T) (HDC,PVOID); +typedef BOOL (WINAPI * SETDEVICEGAMMARAMP_T) (HDC,PVOID); #endif // _GLFW_NO_DLOAD_GDI32 // winmm.dll function pointer typedefs @@ -174,12 +176,16 @@ typedef DWORD (WINAPI * TIMEGETTIME_T) (void); #define _glfw_GetPixelFormat _glfwLibrary.Win32.gdi.GetPixelFormat #define _glfw_SetPixelFormat _glfwLibrary.Win32.gdi.SetPixelFormat #define _glfw_SwapBuffers _glfwLibrary.Win32.gdi.SwapBuffers +#define _glfw_GetDeviceGammaRamp _glfwLibrary.Win32.gdi.GetDeviceGammaRamp +#define _glfw_SetDeviceGammaRamp _glfwLibrary.Win32.gdi.SetDeviceGammaRamp #else #define _glfw_ChoosePixelFormat ChoosePixelFormat #define _glfw_DescribePixelFormat DescribePixelFormat #define _glfw_GetPixelFormat GetPixelFormat #define _glfw_SetPixelFormat SetPixelFormat #define _glfw_SwapBuffers SwapBuffers +#define _glfw_GetDeviceGammaRamp GetDeviceGammaRamp +#define _glfw_SetDeviceGammaRamp SetDeviceGammaRamp #endif // _GLFW_NO_DLOAD_GDI32 // winmm.dll shortcuts @@ -264,6 +270,7 @@ typedef struct _GLFWlibraryWin32 ATOM classAtom; // Window class atom HHOOK keyboardHook; // Keyboard hook handle DWORD foregroundLockTimeout; + HDC desktopDC; // Default monitor struct { @@ -291,6 +298,8 @@ typedef struct _GLFWlibraryWin32 GETPIXELFORMAT_T GetPixelFormat; SETPIXELFORMAT_T SetPixelFormat; SWAPBUFFERS_T SwapBuffers; + GETDEVICEGAMMARAMP_T GetDeviceGammaRamp; + SETDEVICEGAMMARAMP_T SetDeviceGammaRamp; } gdi; #endif // _GLFW_NO_DLOAD_GDI32 diff --git a/src/win32/win32_gamma.c b/src/win32/win32_gamma.c new file mode 100644 index 00000000..71d2645f --- /dev/null +++ b/src/win32/win32_gamma.c @@ -0,0 +1,67 @@ +//======================================================================== +// GLFW - An OpenGL framework +// Platform: Win32/WGL +// API version: 2.7 +// WWW: http://www.glfw.org/ +//------------------------------------------------------------------------ +// Copyright (c) 2002-2006 Marcus Geelnard +// Copyright (c) 2006-2010 Camilla Berglund +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would +// be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not +// be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source +// distribution. +// +//======================================================================== + +#include "internal.h" + +#include + + +//************************************************************************ +//**** GLFW internal functions **** +//************************************************************************ + +//======================================================================== +// Save the gamma ramp to our internal copy +//======================================================================== + +void _glfwPlatformSaveGammaRamp(int ramp) +{ + if (!_glfwLibrary.gammaSize) + { + return; + } + _glfw_GetDeviceGammaRamp(_glfwLibrary.Win32.desktopDC, + _glfwLibrary.gammaRamp[ramp]); +} + + +//======================================================================== +// Restore the gamma ramp to our internal copy of the gamma ramp +//======================================================================== + +void _glfwPlatformRestoreGammaRamp(int ramp) +{ + if (!_glfwLibrary.gammaSize) + { + return; + } + _glfw_SetDeviceGammaRamp(_glfwLibrary.Win32.desktopDC, + _glfwLibrary.gammaRamp[ramp]); +} diff --git a/src/win32/win32_init.c b/src/win32/win32_init.c index 3e71ad6e..54939cc3 100644 --- a/src/win32/win32_init.c +++ b/src/win32/win32_init.c @@ -30,6 +30,8 @@ #include "internal.h" +#include + #ifdef __BORLANDC__ // With the Borland C++ compiler, we want to disable FPU exceptions #include @@ -59,12 +61,18 @@ static GLboolean initLibraries(void) GetProcAddress(_glfwLibrary.Win32.gdi.instance, "SetPixelFormat"); _glfwLibrary.Win32.gdi.SwapBuffers = (SWAPBUFFERS_T) GetProcAddress(_glfwLibrary.Win32.gdi.instance, "SwapBuffers"); + _glfwLibrary.Win32.gdi.GetDeviceGammaRamp = (GETDEVICEGAMMARAMP_T) + GetProcAddress(_glfwLibrary.Win32.gdi.instance, "GetDeviceGammaRamp"); + _glfwLibrary.Win32.gdi.SetDeviceGammaRamp = (SETDEVICEGAMMARAMP_T) + GetProcAddress(_glfwLibrary.Win32.gdi.instance, "SetDeviceGammaRamp"); if (!_glfwLibrary.Win32.gdi.ChoosePixelFormat || !_glfwLibrary.Win32.gdi.DescribePixelFormat || !_glfwLibrary.Win32.gdi.GetPixelFormat || !_glfwLibrary.Win32.gdi.SetPixelFormat || - !_glfwLibrary.Win32.gdi.SwapBuffers) + !_glfwLibrary.Win32.gdi.SwapBuffers || + !_glfwLibrary.Win32.gdi.GetDeviceGammaRamp || + !_glfwLibrary.Win32.gdi.SetDeviceGammaRamp) { return GL_FALSE; } @@ -152,6 +160,19 @@ int _glfwPlatformInit(void) _glfwLibrary.Win32.instance = GetModuleHandle(NULL); + // Initialise the internal gamma ramp + _glfwLibrary.gammaSize = 256; + _glfwLibrary.gammaRamp[GLFW_GAMMA_ORIG] = + malloc(256 * sizeof(unsigned short) * 3); + _glfwLibrary.gammaRamp[GLFW_GAMMA_CURR] = + malloc(256 * sizeof(unsigned short) * 3); + + // Get the desktop DC + _glfwLibrary.Win32.desktopDC = GetDC(GetDesktopWindow()); + + // Save the original gamma ramp + _glfwPlatformSaveGammaRamp(GLFW_GAMMA_ORIG); + _glfwInitTimer(); return GL_TRUE; @@ -164,6 +185,13 @@ int _glfwPlatformInit(void) int _glfwPlatformTerminate(void) { + // Restore the original gamma ramp + _glfwPlatformRestoreGammaRamp(GLFW_GAMMA_ORIG); + + // Free the gamma ramps + free(_glfwLibrary.gammaRamp[GLFW_GAMMA_ORIG]); + free(_glfwLibrary.gammaRamp[GLFW_GAMMA_CURR]); + if (_glfwLibrary.Win32.classAtom) { UnregisterClass(_GLFW_WNDCLASSNAME, _glfwLibrary.Win32.instance); diff --git a/src/x11/CMakeLists.txt b/src/x11/CMakeLists.txt index 2e20dadc..a75adf7b 100644 --- a/src/x11/CMakeLists.txt +++ b/src/x11/CMakeLists.txt @@ -12,6 +12,7 @@ set(libglfw_SOURCES ${common_SOURCES} x11_enable.c x11_fullscreen.c + x11_gamma.c x11_glext.c x11_init.c x11_joystick.c diff --git a/src/x11/platform.h b/src/x11/platform.h index 1e3668be..5c497777 100644 --- a/src/x11/platform.h +++ b/src/x11/platform.h @@ -52,10 +52,6 @@ #error "GLX header version 1.3 or above is required" #endif -#if defined(_GLFW_HAS_XF86VIDMODE) && defined(_GLFW_HAS_XRANDR) - #error "Xf86VidMode and RandR extensions cannot both be enabled" -#endif - // With XFree86, we can use the XF86VidMode extension #if defined(_GLFW_HAS_XF86VIDMODE) #include @@ -156,15 +152,18 @@ typedef struct _GLFWlibraryX11 int glxMajor, glxMinor; struct { - int available; + GLboolean available; int eventBase; int errorBase; } XF86VidMode; struct { - int available; + GLboolean available; int eventBase; int errorBase; + int majorVersion; + int minorVersion; + GLboolean gammaBroken; } XRandR; // Screensaver data @@ -179,14 +178,13 @@ typedef struct _GLFWlibraryX11 // Fullscreen data struct { int modeChanged; -#if defined(_GLFW_HAS_XF86VIDMODE) - XF86VidModeModeInfo oldMode; -#endif #if defined(_GLFW_HAS_XRANDR) SizeID oldSizeID; int oldWidth; int oldHeight; Rotation oldRotation; +#elif defined(_GLFW_HAS_XF86VIDMODE) + XF86VidModeModeInfo oldMode; #endif } FS; diff --git a/src/x11/x11_fullscreen.c b/src/x11/x11_fullscreen.c index a80ed918..17ce6584 100644 --- a/src/x11/x11_fullscreen.c +++ b/src/x11/x11_fullscreen.c @@ -480,7 +480,7 @@ void _glfwPlatformGetDesktopMode(GLFWvidmode* mode) { Display* dpy; int bpp, screen; -#if defined(_GLFW_HAS_XF86VIDMODE) +#if !defined(_GLFW_HAS_XRANDR) && defined(_GLFW_HAS_XF86VIDMODE) XF86VidModeModeInfo** modelist; int modecount; #endif diff --git a/src/x11/x11_gamma.c b/src/x11/x11_gamma.c new file mode 100644 index 00000000..779859f3 --- /dev/null +++ b/src/x11/x11_gamma.c @@ -0,0 +1,124 @@ +//======================================================================== +// GLFW - An OpenGL framework +// Platform: X11/GLX +// API version: 3.0 +// WWW: http://www.glfw.org/ +//------------------------------------------------------------------------ +// Copyright (c) 2010 Camilla Berglund +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would +// be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not +// be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source +// distribution. +// +//======================================================================== + +#include "internal.h" + +#include +#include + + +//************************************************************************ +//**** GLFW internal functions **** +//************************************************************************ + +//======================================================================== +// Save the original gamma ramp so that we can restore it later +//======================================================================== + +void _glfwPlatformSaveGammaRamp(void) +{ + if (_glfwLibrary.X11.XRandR.available && + !_glfwLibrary.X11.XRandR.gammaBroken) + { +#if defined (_GLFW_HAS_XRANDR) + size_t size = GLFW_GAMMA_RAMP_SIZE * sizeof(unsigned short); + + XRRScreenResources* rr = XRRGetScreenResources(_glfwLibrary.X11.display, + _glfwLibrary.X11.root); + + XRRCrtcGamma* gamma = XRRGetCrtcGamma(_glfwLibrary.X11.display, + rr->crtcs[0]); + + memcpy(_glfwLibrary.originalRamp.red, gamma->red, size); + memcpy(_glfwLibrary.originalRamp.green, gamma->green, size); + memcpy(_glfwLibrary.originalRamp.blue, gamma->blue, size); + + XRRFreeGamma(gamma); + XRRFreeScreenResources(rr); + } +#endif + else if (_glfwLibrary.X11.XF86VidMode.available) + { +#if defined (_GLFW_HAS_XF86VIDMODE) + XF86VidModeGetGammaRamp(_glfwLibrary.X11.display, + _glfwLibrary.X11.screen, + GLFW_GAMMA_RAMP_SIZE, + _glfwLibrary.originalRamp.red, + _glfwLibrary.originalRamp.green, + _glfwLibrary.originalRamp.blue); +#endif + } +} + + +//======================================================================== +// Make the specified gamma ramp current +//======================================================================== + +void _glfwPlatformSetGammaRamp(const GLFWgammaramp* ramp) +{ + if (_glfwLibrary.X11.XRandR.available && + !_glfwLibrary.X11.XRandR.gammaBroken) + { +#if defined (_GLFW_HAS_XRANDR) + int i; + size_t size = GLFW_GAMMA_RAMP_SIZE * sizeof(unsigned short); + + XRRScreenResources* rr = XRRGetScreenResources(_glfwLibrary.X11.display, + _glfwLibrary.X11.root); + + // Update gamma per monitor + for (i = 0; i < rr->ncrtc; i++) + { + XRRCrtcGamma* gamma = XRRAllocGamma(GLFW_GAMMA_RAMP_SIZE); + + memcpy(gamma->red, ramp->red, size); + memcpy(gamma->green, ramp->green, size); + memcpy(gamma->blue, ramp->blue, size); + + XRRSetCrtcGamma(_glfwLibrary.X11.display, rr->crtcs[i], gamma); + XRRFreeGamma(gamma); + } + + XRRFreeScreenResources(rr); + } +#endif + else if (_glfwLibrary.X11.XF86VidMode.available) + { +#if defined (_GLFW_HAS_XF86VIDMODE) + XF86VidModeSetGammaRamp(_glfwLibrary.X11.display, + _glfwLibrary.X11.screen, + GLFW_GAMMA_RAMP_SIZE, + (unsigned short*) ramp->red, + (unsigned short*) ramp->green, + (unsigned short*) ramp->blue); +#endif + } +} + diff --git a/src/x11/x11_init.c b/src/x11/x11_init.c index 6eab3c5f..b6529168 100644 --- a/src/x11/x11_init.c +++ b/src/x11/x11_init.c @@ -31,6 +31,7 @@ #include "internal.h" #include +#include //======================================================================== @@ -62,7 +63,7 @@ static void initLibraries(void) //======================================================================== -// Initialize X11 display +// Initialize X11 display and look for supported X11 extensions //======================================================================== static GLboolean initDisplay(void) @@ -88,7 +89,7 @@ static GLboolean initDisplay(void) &_glfwLibrary.X11.XF86VidMode.eventBase, &_glfwLibrary.X11.XF86VidMode.errorBase); #else - _glfwLibrary.X11.XF86VidMode.available = 0; + _glfwLibrary.X11.XF86VidMode.available = GL_FALSE; #endif // Check for XRandR extension @@ -97,11 +98,17 @@ static GLboolean initDisplay(void) XRRQueryExtension(_glfwLibrary.X11.display, &_glfwLibrary.X11.XRandR.eventBase, &_glfwLibrary.X11.XRandR.errorBase); + + if (!XRRQueryVersion(_glfwLibrary.X11.display, + &_glfwLibrary.X11.XRandR.majorVersion, + &_glfwLibrary.X11.XRandR.minorVersion)) + { + fprintf(stderr, "Unable to query RandR version number\n"); + } #else - _glfwLibrary.X11.XRandR.available = 0; + _glfwLibrary.X11.XRandR.available = GL_FALSE; #endif - // Fullscreen & screen saver settings // Check if GLX is supported on this display if (!glXQueryExtension(_glfwLibrary.X11.display, NULL, NULL)) { @@ -123,6 +130,58 @@ static GLboolean initDisplay(void) } +//======================================================================== +// Detect gamma ramp support and save original gamma ramp, if available +//======================================================================== + +static void initGammaRamp(void) +{ +#ifdef _GLFW_HAS_XRANDR + // RandR gamma support is only available with version 1.2 and above + if (_glfwLibrary.X11.XRandR.available && + (_glfwLibrary.X11.XRandR.majorVersion > 1 || + _glfwLibrary.X11.XRandR.majorVersion == 1 && + _glfwLibrary.X11.XRandR.minorVersion >= 2)) + { + // FIXME: Assumes that all monitors have the same size gamma tables + // This is reasonable as I suspect the that if they did differ, it + // would imply that setting the gamma size to an arbitary size is + // possible as well. + XRRScreenResources* rr = XRRGetScreenResources(_glfwLibrary.X11.display, + _glfwLibrary.X11.root); + + _glfwLibrary.originalRampSize = XRRGetCrtcGammaSize(_glfwLibrary.X11.display, + rr->crtcs[0]); + if (!_glfwLibrary.originalRampSize) + { + // This is probably Nvidia RandR with broken gamma support + // Flag it as useless and try Xf86VidMode below, if available + _glfwLibrary.X11.XRandR.gammaBroken = GL_TRUE; + } + + XRRFreeScreenResources(rr); + } +#endif + +#if defined(_GLFW_HAS_XF86VIDMODE) + if (_glfwLibrary.X11.XF86VidMode.available && + !_glfwLibrary.originalRampSize) + { + // Get the gamma size using XF86VidMode + XF86VidModeGetGammaRampSize(_glfwLibrary.X11.display, + _glfwLibrary.X11.screen, + &_glfwLibrary.originalRampSize); + } +#endif + + if (!_glfwLibrary.originalRampSize) + fprintf(stderr, "Gamma ramp setting unsupported\n"); + + // Save the original gamma ramp + _glfwPlatformSaveGammaRamp(); +} + + //======================================================================== // Create a blank cursor (for locked mouse mode) //======================================================================== @@ -159,8 +218,15 @@ static Cursor createNULLCursor(void) static void terminateDisplay(void) { + if (_glfwLibrary.originalRampSize) + { + _glfwPlatformSetGammaRamp(&_glfwLibrary.originalRamp); + } + if (_glfwLibrary.X11.display) { + _glfwPlatformSetGammaRamp(&_glfwLibrary.originalRamp); + XCloseDisplay(_glfwLibrary.X11.display); _glfwLibrary.X11.display = NULL; } @@ -180,6 +246,8 @@ int _glfwPlatformInit(void) if (!initDisplay()) return GL_FALSE; + initGammaRamp(); + _glfwLibrary.X11.cursor = createNULLCursor(); // Try to load libGL.so if necessary @@ -232,7 +300,8 @@ const char* _glfwPlatformGetVersionString(void) const char* version = "GLFW " _GLFW_VERSION_FULL #if defined(_GLFW_HAS_XRANDR) " XRandR" -#elif defined(_GLFW_HAS_XF86VIDMODE) +#endif +#if defined(_GLFW_HAS_XF86VIDMODE) " Xf86VidMode" #else " (no mode switching support)" diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f55a1900..9d53b9f2 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,6 +6,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include ${OPENGL_INCLUDE_DIR} add_executable(defaults defaults.c) add_executable(events events.c) add_executable(fsfocus fsfocus.c) +add_executable(gamma gamma.c getopt.c) add_executable(iconify iconify.c getopt.c) add_executable(joysticks joysticks.c) add_executable(listmodes listmodes.c) @@ -30,8 +31,8 @@ else() endif(APPLE) set(WINDOWS_BINARIES accuracy sharing tearing windows) -set(CONSOLE_BINARIES defaults events fsaa fsfocus iconify joysticks listmodes - peter reopen version) +set(CONSOLE_BINARIES defaults events fsaa fsfocus gamma iconify joysticks + listmodes peter reopen version) if(MSVC) # Tell MSVC to use main instead of WinMain for Windows subsystem executables diff --git a/tests/gamma.c b/tests/gamma.c new file mode 100644 index 00000000..e918a156 --- /dev/null +++ b/tests/gamma.c @@ -0,0 +1,178 @@ +//======================================================================== +// Gamma correction test program +// Copyright (c) Camilla Berglund +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would +// be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not +// be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source +// distribution. +// +//======================================================================== +// +// This program is used to test the gamma correction functionality for +// both fullscreen and windowed mode windows +// +//======================================================================== + +#include + +#include +#include + +#include "getopt.h" + +static GLfloat ggamma = 1.0f; +static GLfloat ggain = 1.0f; +static GLfloat gblacklevel = 0.0f; + +static void usage(void) +{ + printf("Usage: gammatest [-h] [-f]\n"); +} + +static void key_callback(GLFWwindow window, int key, int action) +{ + if (action != GLFW_PRESS) + return; + + switch (key) + { + case GLFW_KEY_ESC: + glfwCloseWindow(window); + break; + case 'Q': + ggamma += 0.1f; + printf("Gamma: %f\n", ggamma); + glfwSetGammaFormula( ggamma, gblacklevel, ggain ); + break; + case 'W': + ggamma -= 0.1f; + printf("Gamma: %f\n", ggamma); + glfwSetGammaFormula( ggamma, gblacklevel, ggain ); + break; + case 'A': + ggain += 0.1f; + printf("Gain: %f\n", ggain); + glfwSetGammaFormula( ggamma, gblacklevel, ggain ); + break; + case 'S': + ggain -= 0.1f; + printf("Gain: %f\n", ggain); + glfwSetGammaFormula( ggamma, gblacklevel, ggain ); + break; + case 'Z': + gblacklevel += 0.1f; + printf("Black Level: %f\n", gblacklevel); + glfwSetGammaFormula( ggamma, gblacklevel, ggain ); + break; + case 'X': + gblacklevel -= 0.1f; + printf("Black Level: %f\n", gblacklevel); + glfwSetGammaFormula( ggamma, gblacklevel, ggain ); + break; + + } +} + +static void size_callback(GLFWwindow window, int width, int height) +{ + glViewport(0, 0, width, height); +} + +int main(int argc, char** argv) +{ + int width, height, ch; + int mode = GLFW_WINDOWED; + GLFWwindow window; + + while ((ch = getopt(argc, argv, "fh")) != -1) + { + switch (ch) + { + case 'h': + usage(); + exit(EXIT_SUCCESS); + + case 'f': + mode = GLFW_FULLSCREEN; + break; + + default: + usage(); + exit(EXIT_FAILURE); + } + } + + if (!glfwInit()) + { + fprintf(stderr, "Failed to initialize GLFW: %s\n", glfwErrorString(glfwGetError())); + exit(EXIT_FAILURE); + } + + if (mode == GLFW_FULLSCREEN) + { + GLFWvidmode mode; + glfwGetDesktopMode(&mode); + width = mode.width; + height = mode.height; + } + else + { + width = 0; + height = 0; + } + + window = glfwOpenWindow(width, height, mode, "Gamma Test", NULL); + if (!window) + { + glfwTerminate(); + + fprintf(stderr, "Failed to open GLFW window: %s\n", glfwErrorString(glfwGetError())); + exit(EXIT_FAILURE); + } + + printf("Gamma: %f\nGain: %f\nBlack Level: %f\n", + ggamma, ggain, gblacklevel); + + glfwSwapInterval(1); + glfwSetKeyCallback(window, key_callback); + glfwSetWindowSizeCallback(window, size_callback); + + glEnable(GL_SCISSOR_TEST); + + while (glfwIsWindow(window)) + { + int width, height; + + glfwGetWindowSize(window, &width, &height); + + glScissor(0, 0, width, height); + glClearColor(0.5f, 0.5f, 0.5f, 0); + glClear(GL_COLOR_BUFFER_BIT); + + glScissor(0, 0, 640, 480); + glClearColor(0.8f, 0.2f, 0.4f, 0); + glClear(GL_COLOR_BUFFER_BIT); + + glfwSwapBuffers(); + glfwPollEvents(); + } + + glfwTerminate(); + exit(EXIT_SUCCESS); +} +