/*
* LICENCE TYPE : BSD
*
* Copyright 2012 Franck Porcher, Ph.D <franck.porcher@franckys.com>
* Inspired by x11/xbrightness (see x11/xbrightness/work/AUTHORS)
*/
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/xf86vmode.h>
#include <math.h>
#define BRIGHTNESS_MAX 65535
#define BRIGHTNESS_MIN 0
#define BRIGHTNESS_DEFAULT 65535
#define STEPS_DEFAULT 5000
#define DISPLAY_DEFAULT ":0"
/*
* Usage
*/
static void usage(char *arg) {
fprintf (stderr, "%s builds a brightness ramp for X11.\n\n", arg);
fprintf (stderr, "Usage: %s [-h] [BRIGHTNESS] [STEP] [DISPLAY] \n", arg);
fprintf (stderr, "Where :\n");
fprintf (stderr, " - BRIGHTNESS is the requested brightness. It can be a number from 0 to 65535, the word 'max' or the word 'min' (de
faults to max)\n");
fprintf (stderr, " - STEPS is the number of steps (>= 1) to step-by-step progressively adjust the screen to the requested bright
ness (defaults to 5000)\n");
fprintf (stderr, " - DISPLAY is the name of the X11 display to use (defaults to :0)\n\n");
fprintf (stderr, "Copyright(C) Franck Porcher, Ph.D <franck.porcher@franckys.com>.\n");
exit(0);
}
/*
* Main
*/
int main(int argc, char *argv[]) {
Display *dpy;
int i,j;
int screen, ramp_size;
unsigned short *ramp;
double level, delta_level;
double brightness, delta_step;
/**** Defaults ****/
unsigned short brightness_requested = BRIGHTNESS_MAX;
unsigned short steps = STEPS_DEFAULT;
char *display = DISPLAY_DEFAULT;
/**** Parameters ****/
if (argc >= 2) {
if (! strcmp(argv[1], "-h")) {
usage(argv[0]);
}
if (! strcmp(argv[1], "max")) {
brightness_requested = BRIGHTNESS_MAX;
}
else if (! strcmp(argv[1], "min")) {
brightness_requested = BRIGHTNESS_MIN;
}
else {
brightness_requested = atoi(argv[1]);
}
}
if (argc >= 3) {
steps = atoi(argv[2]);
}
if (argc >= 4) {
display = argv[3];
}
/**** Setup ****/
if ( (dpy = XOpenDisplay(display)) == NULL) {
fprintf (stderr, "%s: unable to open default display '%s'.\n", argv[0], display);
exit(1);
}
screen = DefaultScreen(dpy);
/**** main portion ****/
if ( XF86VidModeGetGammaRampSize(dpy, screen, &ramp_size )
&& (ramp = (unsigned short *) malloc(ramp_size * sizeof(unsigned short)))
&& XF86VidModeGetGammaRamp(dpy, screen, ramp_size, ramp, ramp, ramp)
) {
/* "steps" steps by step progressive monitor's screen brightness adjustment.
* from "actual brightness", given by ramp[ramp_size-1],
* to "requested brightness", given by 1st argument
*/
brightness = (double)ramp[ramp_size-1];
delta_step = ((double)brightness_requested - brightness)/steps;
for (j=0; j<steps; j++) {
brightness += delta_step; /* progressive goal */
delta_level = brightness/(ramp_size-1);
level = 0.0;
/* ramp[0] is always 0 whatever brightness,
* so we start modifying ramp at index 1,
* saving tiny fractions of precious time
*/
for (i=1; i<ramp_size; i++) {
level += delta_level;
ramp[i] = (unsigned short)level;
}
XF86VidModeSetGammaRamp(dpy, screen, ramp_size, ramp, ramp, ramp);
}
}
XCloseDisplay(dpy);
exit(0);
}