1/*
2    SDL - Simple DirectMedia Layer
3    Copyright (C) 1997-2006 Sam Lantinga
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19    Sam Lantinga
20    slouken@libsdl.org
21*/
22#include "SDL_config.h"
23
24#include "SDL.h"
25#include "SDL_events.h"
26#include "../../events/SDL_events_c.h"
27#include "SDL_x11video.h"
28
29/* From the X server sources... */
30#define MAX_GAMMA 10.0
31#define MIN_GAMMA (1.0/MAX_GAMMA)
32
33static int X11_SetGammaNoLock(_THIS, float red, float green, float blue)
34{
35#if SDL_VIDEO_DRIVER_X11_VIDMODE
36    if (use_vidmode >= 200) {
37        SDL_NAME(XF86VidModeGamma) gamma;
38        Bool succeeded;
39
40	/* Clamp the gamma values */
41	if ( red < MIN_GAMMA ) {
42		gamma.red = MIN_GAMMA;
43	} else
44	if ( red > MAX_GAMMA ) {
45		gamma.red = MAX_GAMMA;
46	} else {
47        	gamma.red = red;
48	}
49	if ( green < MIN_GAMMA ) {
50		gamma.green = MIN_GAMMA;
51	} else
52	if ( green > MAX_GAMMA ) {
53		gamma.green = MAX_GAMMA;
54	} else {
55        	gamma.green = green;
56	}
57	if ( blue < MIN_GAMMA ) {
58		gamma.blue = MIN_GAMMA;
59	} else
60	if ( blue > MAX_GAMMA ) {
61		gamma.blue = MAX_GAMMA;
62	} else {
63        	gamma.blue = blue;
64	}
65        if ( SDL_GetAppState() & SDL_APPACTIVE ) {
66            succeeded = SDL_NAME(XF86VidModeSetGamma)(SDL_Display, SDL_Screen, &gamma);
67            XSync(SDL_Display, False);
68        } else {
69            gamma_saved[0] = gamma.red;
70            gamma_saved[1] = gamma.green;
71            gamma_saved[2] = gamma.blue;
72            succeeded = True;
73        }
74        if ( succeeded ) {
75            ++gamma_changed;
76        }
77        return succeeded ? 0 : -1;
78    }
79#endif
80    SDL_SetError("Gamma correction not supported");
81    return -1;
82}
83int X11_SetVidModeGamma(_THIS, float red, float green, float blue)
84{
85    int result;
86
87    SDL_Lock_EventThread();
88    result = X11_SetGammaNoLock(this, red, green, blue);
89    SDL_Unlock_EventThread();
90
91    return(result);
92}
93
94static int X11_GetGammaNoLock(_THIS, float *red, float *green, float *blue)
95{
96#if SDL_VIDEO_DRIVER_X11_VIDMODE
97    if (use_vidmode >= 200) {
98        SDL_NAME(XF86VidModeGamma) gamma;
99        if (SDL_NAME(XF86VidModeGetGamma)(SDL_Display, SDL_Screen, &gamma)) {
100            *red   = gamma.red;
101            *green = gamma.green;
102            *blue  = gamma.blue;
103            return 0;
104        }
105        return -1;
106    }
107#endif
108    return -1;
109}
110int X11_GetVidModeGamma(_THIS, float *red, float *green, float *blue)
111{
112    int result;
113
114    SDL_Lock_EventThread();
115    result = X11_GetGammaNoLock(this, red, green, blue);
116    SDL_Unlock_EventThread();
117
118    return(result);
119}
120
121void X11_SaveVidModeGamma(_THIS)
122{
123    /* Try to save the current gamma, otherwise disable gamma control */
124    if ( X11_GetGammaNoLock(this,
125              &gamma_saved[0], &gamma_saved[1], &gamma_saved[2]) < 0 ) {
126        this->SetGamma = 0;
127        this->GetGamma = 0;
128    }
129    gamma_changed = 0;
130}
131void X11_SwapVidModeGamma(_THIS)
132{
133    float new_gamma[3];
134
135    if ( gamma_changed ) {
136        new_gamma[0] = gamma_saved[0];
137        new_gamma[1] = gamma_saved[1];
138        new_gamma[2] = gamma_saved[2];
139        X11_GetGammaNoLock(this, &gamma_saved[0], &gamma_saved[1], &gamma_saved[2]);
140        X11_SetGammaNoLock(this, new_gamma[0], new_gamma[1], new_gamma[2]);
141    }
142}
143