sct.c (2533B)
1 /* cc -std=c99 -O2 -I /usr/X11R6/include -o sct sct.c -L /usr/X11R6/lib -lm -lX11 -lXrandr */ 2 #include <stdio.h> 3 #include <X11/Xlib.h> 4 #include <X11/Xlibint.h> 5 #include <X11/Xproto.h> 6 #include <X11/Xatom.h> 7 #include <X11/extensions/Xrandr.h> 8 #include <X11/extensions/Xrender.h> 9 #include <strings.h> 10 #include <string.h> 11 #include <stdlib.h> 12 #include <stdint.h> 13 #include <inttypes.h> 14 #include <stdarg.h> 15 #include <math.h> 16 17 18 /* cribbed from redshift, but truncated with 500K steps */ 19 static const struct { float r; float g; float b; } whitepoints[] = { 20 { 1.00000000, 0.18172716, 0.00000000, }, /* 1000K */ 21 { 1.00000000, 0.42322816, 0.00000000, }, 22 { 1.00000000, 0.54360078, 0.08679949, }, 23 { 1.00000000, 0.64373109, 0.28819679, }, 24 { 1.00000000, 0.71976951, 0.42860152, }, 25 { 1.00000000, 0.77987699, 0.54642268, }, 26 { 1.00000000, 0.82854786, 0.64816570, }, 27 { 1.00000000, 0.86860704, 0.73688797, }, 28 { 1.00000000, 0.90198230, 0.81465502, }, 29 { 1.00000000, 0.93853986, 0.88130458, }, 30 { 1.00000000, 0.97107439, 0.94305985, }, 31 { 1.00000000, 1.00000000, 1.00000000, }, /* 6500K */ 32 { 0.95160805, 0.96983355, 1.00000000, }, 33 { 0.91194747, 0.94470005, 1.00000000, }, 34 { 0.87906581, 0.92357340, 1.00000000, }, 35 { 0.85139976, 0.90559011, 1.00000000, }, 36 { 0.82782969, 0.89011714, 1.00000000, }, 37 { 0.80753191, 0.87667891, 1.00000000, }, 38 { 0.78988728, 0.86491137, 1.00000000, }, /* 10000K */ 39 { 0.77442176, 0.85453121, 1.00000000, }, 40 }; 41 42 int 43 main(int argc, char **argv) 44 { 45 Display *dpy = XOpenDisplay(NULL); 46 int screen = DefaultScreen(dpy); 47 Window root = RootWindow(dpy, screen); 48 49 XRRScreenResources *res = XRRGetScreenResourcesCurrent(dpy, root); 50 51 int temp = 6500; 52 if (argc > 1) 53 temp = atoi(argv[1]); 54 if (temp < 1000 || temp > 10000) 55 temp = 6500; 56 57 temp -= 1000; 58 double ratio = temp % 500 / 500.0; 59 #define AVG(c) whitepoints[temp / 500].c * (1 - ratio) + whitepoints[temp / 500 + 1].c * ratio 60 double gammar = AVG(r); 61 double gammag = AVG(g); 62 double gammab = AVG(b); 63 64 int num_crtcs = res->ncrtc; 65 for (int c = 0; c < res->ncrtc; c++) { 66 int crtcxid = res->crtcs[c]; 67 XRRCrtcInfo *crtc_info = XRRGetCrtcInfo(dpy, res, crtcxid); 68 69 int size = XRRGetCrtcGammaSize(dpy, crtcxid); 70 71 XRRCrtcGamma *crtc_gamma = XRRAllocGamma(size); 72 73 for (int i = 0; i < size; i++) { 74 double g = 65535.0 * i / size; 75 crtc_gamma->red[i] = g * gammar; 76 crtc_gamma->green[i] = g * gammag; 77 crtc_gamma->blue[i] = g * gammab; 78 } 79 XRRSetCrtcGamma(dpy, crtcxid, crtc_gamma); 80 81 XFree(crtc_gamma); 82 } 83 } 84