#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/types.h>
#include <time.h>
#include <string.h>
// X11 stuff
#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xlibint.h>
#include <X11/Xproto.h>
#include <X11/Xatom.h>
#include <X11/extensions/xf86vmode.h>
#include <X11/extensions/dpms.h>
#include <X11/extensions/Xinerama.h>
#include "gamma_ramp.h"
gamma_ramp::gamma_ramp(int Screen)
{
screen = Screen;
size = getGammaRampSize(screen);
// now allocate the arrays
red = new unsigned short[size];
green = new unsigned short[size];
blue = new unsigned short[size];
}
gamma_ramp::~gamma_ramp()
{
delete[] red;
delete[] green;
delete[] blue;
}
int gamma_ramp::getGammaRampSize(int Screen)
{
int size = 0;
int event_base;
int error_base;
Display *dpy;
if ((dpy = XOpenDisplay(":0.0")) == NULL)
{
printf("failed to open display :0.0\n");
}
else
{
if (XF86VidModeQueryExtension(dpy, &event_base, &error_base) ==0)
{
printf("XF86VidModeQueryExtension failed\n");
}
else
{
if (!XF86VidModeGetGammaRampSize(dpy, Screen, &size))
{
printf("failed to get XF86VidModeGetGammaRampSize\n");
}
}
}
return size; // will be 0 if failed
}
void gamma_ramp::getGammaRamp()
{
int event_base;
int error_base;
Display *dpy;
if ((dpy = XOpenDisplay(":0.0")) == NULL)
{
printf("failed to open display :0.0\n");
}
else
{
if (XF86VidModeQueryExtension(dpy, &event_base, &error_base) ==0)
{
printf("XF86VidModeQueryExtension failed\n");
}
else
{
XF86VidModeGetGammaRamp(dpy, screen, size,
red, green, blue);
}
}
}
void gamma_ramp::setGammaRamp()
{
int event_base;
int error_base;
Display *dpy;
if ((dpy = XOpenDisplay(":0.0")) == NULL)
{
printf("failed to open display :0.0\n");
}
else
{
if (XF86VidModeQueryExtension(dpy, &event_base, &error_base) ==0)
{
printf("XF86VidModeQueryExtension failed\n");
}
else
{
XF86VidModeSetGammaRamp(dpy, screen, size,
red, green, blue);
}
}
}
void gamma_ramp::setLinearGammaRamp()
{
int event_base;
int error_base;
Display *dpy;
gamma_ramp* temp;
temp = new gamma_ramp(screen);
for (int i=0; i< temp -> size; i++)
{
temp -> red[i] = temp -> green[i] = temp -> blue[i] = i * 256;
}
if ((dpy = XOpenDisplay(":0.0")) == NULL)
{
printf("failed to open display :0.0\n");
}
else
{
if (XF86VidModeQueryExtension(dpy, &event_base, &error_base) ==0)
{
printf("XF86VidModeQueryExtension failed\n");
}
else
{
temp -> setGammaRamp();
}
}
delete temp;
}