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