src/video/x11/SDL_x11gamma.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 22 Mar 2006 05:00:59 +0000
changeset 1575 3ba88cb7eb1b
parent 1402 d910939febfa
child 1662 782fd950bd46
child 1895 c121d94672cb
child 4159 a1b03ba2fcd0
permissions -rw-r--r--
Updated dynamic X11 code. See details in Bugzilla #170.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 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@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
#include "SDL.h"
slouken@0
    25
#include "SDL_events.h"
slouken@1361
    26
#include "../../events/SDL_events_c.h"
slouken@0
    27
#include "SDL_x11video.h"
slouken@0
    28
slouken@0
    29
/* From the X server sources... */
slouken@0
    30
#define MAX_GAMMA 10.0
slouken@0
    31
#define MIN_GAMMA (1.0/MAX_GAMMA)
slouken@0
    32
slouken@0
    33
static int X11_SetGammaNoLock(_THIS, float red, float green, float blue)
slouken@0
    34
{
slouken@1361
    35
#if SDL_VIDEO_DRIVER_X11_VIDMODE
slouken@100
    36
    if (use_vidmode >= 200) {
slouken@292
    37
        SDL_NAME(XF86VidModeGamma) gamma;
slouken@0
    38
        Bool succeeded;
slouken@0
    39
slouken@0
    40
	/* Clamp the gamma values */
slouken@0
    41
	if ( red < MIN_GAMMA ) {
slouken@0
    42
		gamma.red = MIN_GAMMA;
slouken@0
    43
	} else
slouken@0
    44
	if ( red > MAX_GAMMA ) {
slouken@0
    45
		gamma.red = MAX_GAMMA;
slouken@0
    46
	} else {
slouken@0
    47
        	gamma.red = red;
slouken@0
    48
	}
slouken@0
    49
	if ( green < MIN_GAMMA ) {
slouken@0
    50
		gamma.green = MIN_GAMMA;
slouken@0
    51
	} else
slouken@0
    52
	if ( green > MAX_GAMMA ) {
slouken@0
    53
		gamma.green = MAX_GAMMA;
slouken@0
    54
	} else {
slouken@0
    55
        	gamma.green = green;
slouken@0
    56
	}
slouken@0
    57
	if ( blue < MIN_GAMMA ) {
slouken@0
    58
		gamma.blue = MIN_GAMMA;
slouken@0
    59
	} else
slouken@0
    60
	if ( blue > MAX_GAMMA ) {
slouken@0
    61
		gamma.blue = MAX_GAMMA;
slouken@0
    62
	} else {
slouken@0
    63
        	gamma.blue = blue;
slouken@0
    64
	}
slouken@0
    65
        if ( SDL_GetAppState() & SDL_APPACTIVE ) {
slouken@292
    66
            succeeded = SDL_NAME(XF86VidModeSetGamma)(SDL_Display, SDL_Screen, &gamma);
icculus@1575
    67
            XSync(SDL_Display, False);
slouken@0
    68
        } else {
slouken@0
    69
            gamma_saved[0] = gamma.red;
slouken@0
    70
            gamma_saved[1] = gamma.green;
slouken@0
    71
            gamma_saved[2] = gamma.blue;
slouken@0
    72
            succeeded = True;
slouken@0
    73
        }
slouken@0
    74
        if ( succeeded ) {
slouken@0
    75
            ++gamma_changed;
slouken@0
    76
        }
slouken@0
    77
        return succeeded ? 0 : -1;
slouken@0
    78
    }
slouken@0
    79
#endif
slouken@0
    80
    SDL_SetError("Gamma correction not supported");
slouken@0
    81
    return -1;
slouken@0
    82
}
slouken@0
    83
int X11_SetVidModeGamma(_THIS, float red, float green, float blue)
slouken@0
    84
{
slouken@0
    85
    int result;
slouken@0
    86
slouken@0
    87
    SDL_Lock_EventThread();
slouken@0
    88
    result = X11_SetGammaNoLock(this, red, green, blue);
slouken@0
    89
    SDL_Unlock_EventThread();
slouken@0
    90
slouken@0
    91
    return(result);
slouken@0
    92
}
slouken@0
    93
slouken@0
    94
static int X11_GetGammaNoLock(_THIS, float *red, float *green, float *blue)
slouken@0
    95
{
slouken@1361
    96
#if SDL_VIDEO_DRIVER_X11_VIDMODE
slouken@445
    97
    if (use_vidmode >= 200) {
slouken@292
    98
        SDL_NAME(XF86VidModeGamma) gamma;
slouken@292
    99
        if (SDL_NAME(XF86VidModeGetGamma)(SDL_Display, SDL_Screen, &gamma)) {
slouken@0
   100
            *red   = gamma.red;
slouken@0
   101
            *green = gamma.green;
slouken@0
   102
            *blue  = gamma.blue;
slouken@0
   103
            return 0;
slouken@0
   104
        }
slouken@0
   105
        return -1;
slouken@0
   106
    }
slouken@0
   107
#endif
slouken@0
   108
    return -1;
slouken@0
   109
}
slouken@0
   110
int X11_GetVidModeGamma(_THIS, float *red, float *green, float *blue)
slouken@0
   111
{
slouken@0
   112
    int result;
slouken@0
   113
slouken@0
   114
    SDL_Lock_EventThread();
slouken@0
   115
    result = X11_GetGammaNoLock(this, red, green, blue);
slouken@0
   116
    SDL_Unlock_EventThread();
slouken@0
   117
slouken@0
   118
    return(result);
slouken@0
   119
}
slouken@0
   120
slouken@0
   121
void X11_SaveVidModeGamma(_THIS)
slouken@0
   122
{
slouken@0
   123
    /* Try to save the current gamma, otherwise disable gamma control */
slouken@0
   124
    if ( X11_GetGammaNoLock(this,
slouken@0
   125
              &gamma_saved[0], &gamma_saved[1], &gamma_saved[2]) < 0 ) {
slouken@0
   126
        this->SetGamma = 0;
slouken@0
   127
        this->GetGamma = 0;
slouken@0
   128
    }
slouken@0
   129
    gamma_changed = 0;
slouken@0
   130
}
slouken@0
   131
void X11_SwapVidModeGamma(_THIS)
slouken@0
   132
{
slouken@0
   133
    float new_gamma[3];
slouken@0
   134
slouken@0
   135
    if ( gamma_changed ) {
slouken@0
   136
        new_gamma[0] = gamma_saved[0];
slouken@0
   137
        new_gamma[1] = gamma_saved[1];
slouken@0
   138
        new_gamma[2] = gamma_saved[2];
slouken@0
   139
        X11_GetGammaNoLock(this, &gamma_saved[0], &gamma_saved[1], &gamma_saved[2]);
slouken@0
   140
        X11_SetGammaNoLock(this, new_gamma[0], new_gamma[1], new_gamma[2]);
slouken@0
   141
    }
slouken@0
   142
}