2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
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.
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.
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
22 #include "SDL_config.h"
25 #include "SDL_events.h"
26 #include "../../events/SDL_events_c.h"
27 #include "SDL_x11video.h"
29 /* From the X server sources... */
30 #define MAX_GAMMA 10.0
31 #define MIN_GAMMA (1.0/MAX_GAMMA)
33 static int X11_SetGammaNoLock(_THIS, float red, float green, float blue)
35 #if SDL_VIDEO_DRIVER_X11_VIDMODE
36 if (use_vidmode >= 200) {
37 SDL_NAME(XF86VidModeGamma) gamma;
40 /* Clamp the gamma values */
41 if ( red < MIN_GAMMA ) {
42 gamma.red = MIN_GAMMA;
44 if ( red > MAX_GAMMA ) {
45 gamma.red = MAX_GAMMA;
49 if ( green < MIN_GAMMA ) {
50 gamma.green = MIN_GAMMA;
52 if ( green > MAX_GAMMA ) {
53 gamma.green = MAX_GAMMA;
57 if ( blue < MIN_GAMMA ) {
58 gamma.blue = MIN_GAMMA;
60 if ( blue > MAX_GAMMA ) {
61 gamma.blue = MAX_GAMMA;
65 if ( SDL_GetAppState() & SDL_APPACTIVE ) {
66 succeeded = SDL_NAME(XF86VidModeSetGamma)(SDL_Display, SDL_Screen, &gamma);
67 XSync(SDL_Display, False);
69 gamma_saved[0] = gamma.red;
70 gamma_saved[1] = gamma.green;
71 gamma_saved[2] = gamma.blue;
77 return succeeded ? 0 : -1;
80 SDL_SetError("Gamma correction not supported");
83 int X11_SetVidModeGamma(_THIS, float red, float green, float blue)
87 SDL_Lock_EventThread();
88 result = X11_SetGammaNoLock(this, red, green, blue);
89 SDL_Unlock_EventThread();
94 static int X11_GetGammaNoLock(_THIS, float *red, float *green, float *blue)
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)) {
101 *green = gamma.green;
110 int X11_GetVidModeGamma(_THIS, float *red, float *green, float *blue)
114 SDL_Lock_EventThread();
115 result = X11_GetGammaNoLock(this, red, green, blue);
116 SDL_Unlock_EventThread();
121 void X11_SaveVidModeGamma(_THIS)
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 ) {
131 void X11_SwapVidModeGamma(_THIS)
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]);