19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL.h"
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_events.h"
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11video.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* From the X server sources... */
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MAX_GAMMA 10.0
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MIN_GAMMA (1.0/MAX_GAMMA)
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int X11_SetGammaNoLock(_THIS, float red, float green, float blue)
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_VIDMODE
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (use_vidmode >= 200) {
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_NAME(XF86VidModeGamma) gamma;
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Bool succeeded;
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Clamp the gamma values */
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( red < MIN_GAMMA ) {
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gamma.red = MIN_GAMMA;
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( red > MAX_GAMMA ) {
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gamma.red = MAX_GAMMA;
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	gamma.red = red;
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( green < MIN_GAMMA ) {
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gamma.green = MIN_GAMMA;
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( green > MAX_GAMMA ) {
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gamma.green = MAX_GAMMA;
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	gamma.green = green;
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( blue < MIN_GAMMA ) {
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gamma.blue = MIN_GAMMA;
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( blue > MAX_GAMMA ) {
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gamma.blue = MAX_GAMMA;
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	gamma.blue = blue;
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( SDL_GetAppState() & SDL_APPACTIVE ) {
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            succeeded = SDL_NAME(XF86VidModeSetGamma)(SDL_Display, SDL_Screen, &gamma);
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            XSync(SDL_Display, False);
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            gamma_saved[0] = gamma.red;
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            gamma_saved[1] = gamma.green;
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            gamma_saved[2] = gamma.blue;
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            succeeded = True;
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( succeeded ) {
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            ++gamma_changed;
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return succeeded ? 0 : -1;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_SetError("Gamma correction not supported");
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return -1;
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint X11_SetVidModeGamma(_THIS, float red, float green, float blue)
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int result;
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_Lock_EventThread();
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    result = X11_SetGammaNoLock(this, red, green, blue);
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_Unlock_EventThread();
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(result);
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int X11_GetGammaNoLock(_THIS, float *red, float *green, float *blue)
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_VIDMODE
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (use_vidmode >= 200) {
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_NAME(XF86VidModeGamma) gamma;
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (SDL_NAME(XF86VidModeGetGamma)(SDL_Display, SDL_Screen, &gamma)) {
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *red   = gamma.red;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *green = gamma.green;
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *blue  = gamma.blue;
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return 0;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -1;
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return -1;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint X11_GetVidModeGamma(_THIS, float *red, float *green, float *blue)
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int result;
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_Lock_EventThread();
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    result = X11_GetGammaNoLock(this, red, green, blue);
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_Unlock_EventThread();
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(result);
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid X11_SaveVidModeGamma(_THIS)
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Try to save the current gamma, otherwise disable gamma control */
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( X11_GetGammaNoLock(this,
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              &gamma_saved[0], &gamma_saved[1], &gamma_saved[2]) < 0 ) {
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        this->SetGamma = 0;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        this->GetGamma = 0;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    gamma_changed = 0;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid X11_SwapVidModeGamma(_THIS)
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    float new_gamma[3];
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( gamma_changed ) {
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        new_gamma[0] = gamma_saved[0];
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        new_gamma[1] = gamma_saved[1];
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        new_gamma[2] = gamma_saved[2];
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        X11_GetGammaNoLock(this, &gamma_saved[0], &gamma_saved[1], &gamma_saved[2]);
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        X11_SetGammaNoLock(this, new_gamma[0], new_gamma[1], new_gamma[2]);
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
143