-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxscreenfilter.c
More file actions
126 lines (103 loc) · 3.82 KB
/
xscreenfilter.c
File metadata and controls
126 lines (103 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
xscreenfilter. A command line tool for setting window birghtness and temperature in linux systems.
Compilation dependencies: sudo apt install libxrandr-dev libx11-dev
Compilation instructions: gcc -I/usr/local/include -L/usr/local/lib xscreenfilter.c -o xscreenfilter -lXrandr -lX11
Assign to hotkeys for best experience.
Author:
- Carlos Pinzón, caph1993@gmail.com
Credits to:
- https://flak.tedunangst.com/post/sct-set-color-temperature
- https://github.com/mgudemann/sct
*/
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xrandr.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* cribbed from redshift, but truncated with 500K steps */
static const struct { float r; float g; float b; } whitepoints[] = {
{ 1.00000000, 0.18172716, 0.00000000, }, /* 1000K */
{ 1.00000000, 0.42322816, 0.00000000, },
{ 1.00000000, 0.54360078, 0.08679949, },
{ 1.00000000, 0.64373109, 0.28819679, },
{ 1.00000000, 0.71976951, 0.42860152, },
{ 1.00000000, 0.77987699, 0.54642268, },
{ 1.00000000, 0.82854786, 0.64816570, },
{ 1.00000000, 0.86860704, 0.73688797, },
{ 1.00000000, 0.90198230, 0.81465502, },
{ 1.00000000, 0.93853986, 0.88130458, },
{ 1.00000000, 0.97107439, 0.94305985, },
{ 1.00000000, 1.00000000, 1.00000000, }, /* 6500K */
{ 0.95160805, 0.96983355, 1.00000000, },
{ 0.91194747, 0.94470005, 1.00000000, },
{ 0.87906581, 0.92357340, 1.00000000, },
{ 0.85139976, 0.90559011, 1.00000000, },
{ 0.82782969, 0.89011714, 1.00000000, },
{ 0.80753191, 0.87667891, 1.00000000, },
{ 0.78988728, 0.86491137, 1.00000000, }, /* 10000K */
{ 0.77442176, 0.85453121, 1.00000000, },
};
int
main(int argc, char **argv)
{
Display *dpy = XOpenDisplay(NULL);
int screen = DefaultScreen(dpy);
Window root = RootWindow(dpy, screen);
XRRScreenResources *res = XRRGetScreenResourcesCurrent(dpy, root);
char *filename = "/.xscreenfilter";
char *home_dir = getenv("HOME");
char *filepath = malloc(strlen(home_dir) + strlen(filename) + 1);
strncpy(filepath, home_dir, strlen(home_dir) + 1);
strncat(filepath, filename, strlen(filename) + 1);
//printf("%s\n", filepath);
int temp = 6500;
double brightness = 1.0;
FILE *fpRead;
fpRead = fopen(filepath, "r");
if(fpRead){
fscanf(fpRead, "%d %lf", &temp, &brightness);
}
if (argc > 1){
int tempArg = atoi(argv[1]);
if(argv[1][0]=='+' || argv[1][0]=='-') temp+=tempArg;
else temp = tempArg;
} else temp = 6500;
if (argc > 2){
double brightnessArg = atof(argv[2]);
if(argv[1][0]=='+' || argv[1][0]=='-') brightness+=brightnessArg;
else brightness = brightnessArg;
} else brightness = 1.0;
if (temp < 1000) temp = 1000;
if (temp > 6500) temp = 6500;
if (brightness < 0.1) brightness = 0.1;
if (brightness > 1.0) brightness = 1.0;
FILE *fpWrite;
fpWrite = fopen(filepath, "w");
//printf("%d\n", !!fpWrite);
if(fpWrite){
fprintf(fpWrite, "%d %lf\n", temp, brightness);
}
temp -= 1000;
double ratio = temp % 500 / 500.0;
#define AVG(c) whitepoints[temp / 500].c * (1 - ratio) + whitepoints[temp / 500 + 1].c * ratio
double gammar = brightness * (AVG(r));
double gammag = brightness * (AVG(g));
double gammab = brightness * (AVG(b));
int num_crtcs = res->ncrtc;
for (int c = 0; c < res->ncrtc; c++) {
int crtcxid = res->crtcs[c];
XRRCrtcInfo *crtc_info = XRRGetCrtcInfo(dpy, res, crtcxid);
int size = XRRGetCrtcGammaSize(dpy, crtcxid);
XRRCrtcGamma *crtc_gamma = XRRAllocGamma(size);
for (int i = 0; i < size; i++) {
double g = 65535.0 * i / size;
crtc_gamma->red[i] = g * gammar;
crtc_gamma->green[i] = g * gammag;
crtc_gamma->blue[i] = g * gammab;
}
XRRSetCrtcGamma(dpy, crtcxid, crtc_gamma);
XFree(crtc_gamma);
}
}