-
Notifications
You must be signed in to change notification settings - Fork 2
/
sct_nix.go
66 lines (59 loc) · 1.91 KB
/
sct_nix.go
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
// +build linux freebsd openbsd
package sct
// #cgo CFLAGS: -I/usr/X11R6/include -I/usr/local/include
// #cgo LDFLAGS: -L/usr/X11R6/lib -L/usr/local/lib -lX11 -lXrandr
// #include <stdio.h>
// #include <strings.h>
// #include <string.h>
// #include <stdlib.h>
// #include <stdint.h>
// #include <inttypes.h>
// #include <stdarg.h>
// #include <math.h>
// #include <X11/Xlib.h>
// #include <X11/Xlibint.h>
// #include <X11/Xproto.h>
// #include <X11/Xatom.h>
// #include <X11/extensions/Xrandr.h>
// #include <X11/extensions/Xrender.h>
// Window RootWindowMacro(Display * dpy, int scr) {
// return RootWindow(dpy, scr);
// }
// RRCrtc crtcxid(RRCrtc * crtcs, int i) {
// return crtcs[i];
// }
// void ushortSet(ushort * s, int k, double v) {
// s[k] = (ushort)v;
// }
// ushort* ushortCast(void* s) {
// return (ushort*)s;
// }
// int screenCount(Display * dpy) {
// return XScreenCount(dpy);
// }
import "C"
import "unsafe"
// setColorGamma changes the Xrandr colors to reflect the specified color temperature.
func setColorGamma(gammar, gammag, gammab float64) {
dpy := C.XOpenDisplay(nil)
screenCount := C.screenCount(dpy)
for screen := C.int(0); screen < screenCount; screen++ {
root := C.RootWindowMacro(dpy, screen)
res := C.XRRGetScreenResourcesCurrent(dpy, root)
for c := C.int(0); c < res.ncrtc; c++ {
crtcxid := C.crtcxid(res.crtcs, c)
size := C.XRRGetCrtcGammaSize(dpy, crtcxid)
crtc_gamma := C.XRRAllocGamma(size)
for i := C.int(0); i < size; i++ {
g := 65535.0 * float64(i) / float64(size)
C.ushortSet(C.ushortCast(unsafe.Pointer(crtc_gamma.red)), i, C.double(g*gammar))
C.ushortSet(C.ushortCast(unsafe.Pointer(crtc_gamma.green)), i, C.double(g*gammag))
C.ushortSet(C.ushortCast(unsafe.Pointer(crtc_gamma.blue)), i, C.double(g*gammab))
}
C.XRRSetCrtcGamma(dpy, crtcxid, crtc_gamma)
C.XFree(unsafe.Pointer(crtc_gamma))
}
C.XFree(unsafe.Pointer(res))
}
C.XCloseDisplay(dpy)
}