-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctempd.c
237 lines (197 loc) · 4.23 KB
/
ctempd.c
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* The code to set color temperature (set_color() method below) was written
* by Ted Unangst as is licensed as public domain, do as you wish
* https://flak.tedunangst.com/post/sct-set-color-temperature
*
* The rest of the code was written by Matthias Schmidt <[email protected]>
* and is also public domain.
*/
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xrandr.h>
#include <sys/wait.h>
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include "ctempd.h"
static int fg = 0;
static int verbose = 0;
static Display *dpy;
void
show_usage(void)
{
fprintf(stderr, "ctempd [-fhsv] [temp]\n");
exit(1);
}
void
set_color(int temp)
{
int screen = DefaultScreen(dpy);
Window root = RootWindow(dpy, screen);
XRRScreenResources *res = XRRGetScreenResourcesCurrent(dpy, root);
double ratio = temp % 500 / 500.0;
double gammar = AVG(r);
double gammag = AVG(g);
double gammab = AVG(b);
for (int c = 0; c < res->ncrtc; c++) {
int crtcxid = res->crtcs[c];
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);
}
}
void
daemonize(int default_temp)
{
struct tm tm;
time_t t;
int hour = 0;
int temp = default_temp;
int div = default_temp / 24;
int old = 0;
int diff;
while (1) {
t = time(NULL);
tm = *localtime(&t);
hour = tm.tm_hour;
/* Color temperature before midday ... */
if (hour < 12) {
diff = 12 - hour;
temp = default_temp - (diff * div);
/* ... and after midday */
} else {
diff = hour - 12;
temp = default_temp - (diff * div);
}
if (old != temp) {
if (fg)
fprintf(stderr, "Current time: %d. Set color temperature to %d\n", hour, temp);
else
syslog(LOG_INFO, "Set color temperature to %dK", temp);
set_color(temp);
}
old = temp;
sleep(SLEEP);
}
}
int
main(int argc, char **argv)
{
extern char *__progname;
char *ep;
unsigned long ulval;
pid_t pid;
int temp = TEMP_DEFAULT;
int ch;
int settemp = 0;
while ((ch = getopt(argc, argv, "fhsv")) != -1) {
switch (ch) {
case 'f':
fg = 1;
break;
case 'h':
show_usage();
break;
case 's':
settemp = 1;
break;
case 'v':
verbose = 1;
break;
default:
show_usage();
break;
}
}
argc -= optind;
argv += optind;
if ((dpy = XOpenDisplay(NULL)) == NULL) {
fprintf(stderr, "Cannot connect to X server\n");
return 1;
}
if (sandbox() == -1) {
fprintf(stderr, "Cannot sandbox programm\n");
return 1;
}
if (!verbose)
openlog(__progname, LOG_PID, LOG_USER);
if (argc >= 1) {
errno = 0;
ulval = strtol(argv[0], &ep, 10);
if (argv[0][0] == '\0' || *ep != '\0') {
fprintf(stderr, "Not a valid number\n");
return 1;
}
if (errno == ERANGE && ulval == ULONG_MAX) {
fprintf(stderr, "Out of range\n");
return 1;
}
temp = ulval;
if (temp < 1000 || temp > 10000) {
temp = TEMP_DEFAULT;
}
if (verbose)
fprintf(stderr, "Set color temperature to %d\n", temp);
if (settemp) {
set_color(temp);
return 0;
}
}
pid = fork();
switch (pid) {
case -1:
fprintf(stderr, "Cannot fork\n");
return 1;
case 0:
if (!fg) {
if (daemon(0, 0) == -1) {
fprintf(stderr, "Error calling daemon\n");
return 1;
}
syslog(LOG_INFO, "Startup temperature daemon");
}
daemonize(temp);
}
wait(NULL);
if (!fg)
closelog();
return 0;
}
int
sandbox(void)
{
#ifdef __OpenBSD__
char hf[PATH_MAX], *env;
if (unveil("/usr/X11R6/", "r") == -1)
return -1;
if (unveil("/tmp/.X11-unix/", "rw") == -1)
return -1;
if (unveil("/etc/hosts", "r") == -1)
return -1;
if (unveil("/etc/resolv.conf", "r") == -1)
return -1;
if ((env = getenv("HOME"))) {
if (snprintf(hf, sizeof hf, "%s/.Xauthority", env) <= (int)sizeof(hf)) {
if (unveil(hf, "r") == -1)
return -1;
}
}
if (unveil(NULL, NULL) == -1)
return -1;
if (pledge("stdio rpath proc unix", NULL) == -1)
return -1;
#endif /* __OpenBSD__ */
return 0;
}