mirror of
https://github.com/glfw/glfw.git
synced 2026-09-22 00:49:48 +00:00
Initial implementation of experimental gamma ramp API.
This commit is contained in:
@@ -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
|
||||
|
||||
+7
-9
@@ -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 <X11/extensions/xf86vmode.h>
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 <elmindreda@elmindreda.org>
|
||||
//
|
||||
// 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 <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
//************************************************************************
|
||||
//**** 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
|
||||
}
|
||||
}
|
||||
|
||||
+74
-5
@@ -31,6 +31,7 @@
|
||||
#include "internal.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
//========================================================================
|
||||
@@ -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)"
|
||||
|
||||
Reference in New Issue
Block a user