Add setting gamma ramps with the VidMode extension.

Allow selection of gamma setting method with program parameter (-m).
Fallback to VidMode if RANDR doesn't work.
This commit is contained in:
Jon Lund Steffensen
2009-12-23 18:25:56 +01:00
parent 03dcd5d78a
commit cfeb79731a
7 changed files with 194 additions and 16 deletions
+6 -2
View File
@@ -6,7 +6,11 @@ redshift_SOURCES = \
src/redshift.c \
src/colorramp.c src/colorramp.h \
src/randr.c src/randr.h \
src/vidmode.c src/vidmode.h \
src/solar.c src/solar.h
AM_CFLAGS = $(XCB_CFLAGS)
redshift_LDADD = -lm $(XCB_LIBS) $(XCB_CFLAGS)
AM_CFLAGS = $(X11_CFLAGS) $(XF86VM_CFLAGS) $(XCB_CFLAGS)
redshift_LDADD = -lm \
$(X11_LIBS) $(X11_CFLAGS) \
$(XF86VM_LIBS) $(XF86VM_CFLAGS) \
$(XCB_LIBS) $(XCB_CFLAGS)
+6 -1
View File
@@ -1,8 +1,13 @@
sources = ['redshift.c', 'solar.c', 'colortemp.c']
sources = ['src/redshift.c',
'src/solar.c',
'src/colorramp.c',
'src/randr.c',
'src/vidmode.c']
env = Environment()
env.ParseConfig('pkg-config --cflags --libs xcb xcb-randr')
env.ParseConfig('pkg-config --cflags --libs x11 xxf86vm')
env.Program('redshift', sources,
CFLAGS='-std=gnu99 -O2 -Wall',
LINKFLAGS='-lm')
+2
View File
@@ -11,6 +11,8 @@ AM_INIT_AUTOMAKE([dist-bzip2])
AC_PROG_CC_C99
# Checks for libraries.
PKG_CHECK_MODULES([X11], [x11])
PKG_CHECK_MODULES([XF86VM], [xxf86vm])
PKG_CHECK_MODULES([XCB], [xcb xcb-randr])
# Checks for header files.
+1 -1
View File
@@ -115,7 +115,7 @@ randr_set_temperature(int temp, float gamma[3])
free(gamma_size_reply);
if (gamma_ramp_size == 0) {
fprintf(stderr, "Error: Gamma ramp size too small, %i\n",
fprintf(stderr, "Error: Gamma ramp size too small: %i\n",
gamma_ramp_size);
xcb_disconnect(conn);
return -1;
+44 -12
View File
@@ -27,6 +27,7 @@
#include "solar.h"
#include "randr.h"
#include "vidmode.h"
/* Bounds for parameters. */
@@ -60,6 +61,7 @@
" -g R:G:B\tAdditional gamma correction to apply\n" \
" -h\t\tDisplay this help message\n" \
" -l LAT:LON\tYour current location\n" \
" -m METHOD\tMethod to use to set color temperature (randr or vidmode)\n" \
" -t DAY:NIGHT\tColor temperature to set at daytime/night\n" \
" -v\t\tVerbose output\n"
@@ -70,13 +72,7 @@
int
main(int argc, char *argv[])
{
/* Check extensions needed for color temperature adjustment. */
int r = randr_check_extension();
if (r < 0) {
fprintf(stderr, "Unable to set color temperature:"
" Needed extension is missing.\n");
exit(EXIT_FAILURE);
}
int r;
/* Init locale for degree symbol. */
char *loc = setlocale(LC_CTYPE, "");
@@ -91,11 +87,12 @@ main(int argc, char *argv[])
int temp_day = DEFAULT_DAY_TEMP;
int temp_night = DEFAULT_NIGHT_TEMP;
float gamma[3] = { DEFAULT_GAMMA, DEFAULT_GAMMA, DEFAULT_GAMMA };
int use_randr = 1;
int verbose = 0;
char *s;
int opt;
while ((opt = getopt(argc, argv, "g:hl:t:v")) != -1) {
while ((opt = getopt(argc, argv, "g:hl:m:t:v")) != -1) {
switch (opt) {
case 'g':
s = strchr(optarg, ':');
@@ -133,6 +130,16 @@ main(int argc, char *argv[])
lat = atof(optarg);
lon = atof(s);
break;
case 'm':
if (strcmp(optarg, "randr") == 0) {
use_randr = 1;
} else if (strcmp(optarg, "vidmode") == 0) {
use_randr = 0;
} else {
fprintf(stderr, "Unknown method `%s'.\n", optarg);
exit(EXIT_FAILURE);
}
break;
case 't':
s = strchr(optarg, ':');
if (s == NULL) {
@@ -236,10 +243,35 @@ main(int argc, char *argv[])
}
/* Set color temperature */
r = randr_set_temperature(temp, gamma);
if (r < 0) {
fprintf(stderr, "Unable to set color temperature.\n");
exit(EXIT_FAILURE);
if (use_randr) {
/* Check RANDR extension. */
int r = randr_check_extension();
if (r < 0) {
use_randr = 0;
} else {
r = randr_set_temperature(temp, gamma);
if (r < 0) {
fprintf(stderr, "Unable to set color temperature with RANDR,"
" trying VidMode...\n");
use_randr = 0;
}
}
}
if (!use_randr) {
/* Check VidMode extension */
r = vidmode_check_extension();
if (r < 0) {
fprintf(stderr, "Missing needed extension to set gamma ramp"
"(RANDR or VidMode).\n");
exit(EXIT_FAILURE);
}
r = vidmode_set_temperature(temp, gamma);
if (r < 0) {
fprintf(stderr, "Unable to set color temperature with VidMode.\n");
exit(EXIT_FAILURE);
}
}
return EXIT_SUCCESS;
+109
View File
@@ -0,0 +1,109 @@
/* vidmode.c -- X VidMode gamma adjustment source
This file is part of Redshift.
Redshift is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Redshift is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Redshift. If not, see <http://www.gnu.org/licenses/>.
Copyright (c) 2009 Jon Lund Steffensen <jonlst@gmail.com>
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <X11/Xlib.h>
#include <X11/extensions/xf86vmode.h>
#include "colorramp.h"
int
vidmode_check_extension()
{
int r;
/* Open display */
Display *dpy = XOpenDisplay(NULL);
if (dpy == NULL) {
fprintf(stderr, "XOpenDisplay failed.\n");
return -1;
}
/* Query extension version */
int major, minor;
r = XF86VidModeQueryVersion(dpy, &major, &minor);
if (!r) {
fprintf(stderr, "XF86VidModeQueryVersion failed.\n");
return -1;
}
/* TODO Check major/minor */
/* Close display */
XCloseDisplay(dpy);
return 0;
}
int
vidmode_set_temperature(int temp, float gamma[3])
{
int r;
/* Open display */
Display *dpy = XOpenDisplay(NULL);
if (dpy == NULL) {
fprintf(stderr, "XOpenDisplay failed.\n");
return -1;
}
/* Request size of gamma ramps for screen 0 */
int gamma_ramp_size;
r = XF86VidModeGetGammaRampSize(dpy, 0, &gamma_ramp_size);
if (!r) {
fprintf(stderr, "XF86VidModeGetGammaRampSize failed.\n");
XCloseDisplay(dpy);
return -1;
}
if (gamma_ramp_size == 0) {
fprintf(stderr, "Error: Gamma ramp size too small: %i\n",
gamma_ramp_size);
XCloseDisplay(dpy);
return -1;
}
/* Create new gamma ramps */
uint16_t *gamma_ramps = malloc(3*gamma_ramp_size*sizeof(uint16_t));
if (gamma_ramps == NULL) abort();
uint16_t *gamma_r = &gamma_ramps[0*gamma_ramp_size];
uint16_t *gamma_g = &gamma_ramps[1*gamma_ramp_size];
uint16_t *gamma_b = &gamma_ramps[2*gamma_ramp_size];
colorramp_fill(gamma_r, gamma_g, gamma_b, gamma_ramp_size,
temp, gamma);
/* Set new gamma ramps */
r = XF86VidModeSetGammaRamp(dpy, 0, gamma_ramp_size, gamma_r, gamma_g, gamma_b);
if (!r) {
fprintf(stderr, "XF86VidModeSetGammaRamp failed.\n");
XCloseDisplay(dpy);
return -1;
}
/* Close display */
XCloseDisplay(dpy);
return 0;
}
+26
View File
@@ -0,0 +1,26 @@
/* vidmode.h -- X VidMode gamma adjustment header
This file is part of Redshift.
Redshift is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Redshift is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Redshift. If not, see <http://www.gnu.org/licenses/>.
Copyright (c) 2009 Jon Lund Steffensen <jonlst@gmail.com>
*/
#ifndef _REDSHIFT_VIDMODE_H
#define _REDSHIFT_VIDMODE_H
int vidmode_check_extension();
int vidmode_set_temperature(int temp, float gamma[3]);
#endif /* ! _REDSHIFT_VIDMODE_H */