-
Notifications
You must be signed in to change notification settings - Fork 0
/
main _test.txt
190 lines (153 loc) · 5.53 KB
/
main _test.txt
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
package main
import (
"fmt"
"image"
"image/color"
"log"
// _ "image/png"
// "os"
"os/exec"
"regexp"
"runtime"
"time"
"github.com/kbinani/screenshot"
)
func getPercentLightness(img image.Image) int8 {
imgSize := img.Bounds().Size()
var sum uint
for x := 0; x < imgSize.X; x++ {
for y := 0; y < imgSize.Y; y++ {
sum += uint(color.GrayModel.Convert(img.At(x, y)).(color.Gray).Y)
}
}
sum = sum / uint(imgSize.X*imgSize.Y)
var percentage int8 = int8(100 * int(sum) / 255)
return percentage
}
// func setBrightnessAll(brightness []int16) {
// switch runtime.GOOS {
// case "linux":
// // fmt.Println("yoo deez linux")
// noOfDevicesRegex := regexp.MustCompile(" '(.*)' ")
// backlightDump, err := exec.Command("brightnessctl", "-lc", "backlight").Output()
// if err != nil {
// fmt.Println(err.Error())
// return
// }
// displays := noOfDevicesRegex.FindAll(backlightDump, -1)
// // fmt.Printf("the devices: %q\n", displays)
// for i := 0; i < len(displays); i++ {
// // out, err := exec.Command("brightnessctl", "-d", strings.ReplaceAll(string(displays[i]), "'", " "), "s", fmt.Sprintf("%d%%", brightness[i])).Output()
// out, err := exec.Command("brightnessctl", "-d", "intel_backlight", "s", fmt.Sprintf("%d%%", brightness[i])).Output()
// fmt.Println(out, err)
// fmt.Printf("%s %s %s %s %s\n", "brightnessctl", "-d", strings.ReplaceAll(string(displays[i]), " ", ""), "s", fmt.Sprintf("%d%%", brightness[i]))
// }
// case "windows":
// fmt.Printf("microshit windows detected")
// default:
// fmt.Println("tf is this OS")
// }
// }
func setBrightness(brightness int16, i int) {
switch runtime.GOOS {
case "linux":
// fmt.Println("yoo deez linux")
noOfDevicesRegex := regexp.MustCompile(" '(.*)' ")
backlightDump, err := exec.Command("brightnessctl", "-lc", "backlight").Output()
if err != nil {
fmt.Println(err.Error())
return
}
displays := noOfDevicesRegex.FindAll(backlightDump, -1)
// fmt.Printf("the devices: %q\n", displays)
exec.Command("brightnessctl", "-d", string(displays[i][2:(len(displays[i])-2)]), "s", fmt.Sprintf("%d%%", brightness)).Output()
// out, err := exec.Command("brightnessctl", "-d", "intel_backlight", "s", fmt.Sprintf("%d%%", brightness)).Output()
// fmt.Println(out, err)
// fmt.Printf("%s %s %s %s %s\n", "brightnessctl", "-d", strings.ReplaceAll(string(displays[i]), " ", ""), "s", fmt.Sprintf("%d%%", brightness))
// fmt.Printf("%s %s %s %s %s\n", "brightnessctl", "-d", string(displays[i][2:(len(displays[i])-2)]), "s", fmt.Sprintf("%d%%\n", brightness))
// for i := 0; i < len(displays); i++ {
// // out, err := exec.Command("brightnessctl", "-d", strings.ReplaceAll(string(displays[i]), "'", " "), "s", fmt.Sprintf("%d%%", brightness[i])).Output()
// out, err := exec.Command("brightnessctl", "-d", "intel_backlight", "s", fmt.Sprintf("%d%%", brightness)).Output()
// fmt.Println(out, err)
// fmt.Printf("%s %s %s %s %s\n", "brightnessctl", "-d", strings.ReplaceAll(string(displays[i]), " ", ""), "s", fmt.Sprintf("%d%%", brightness[i]))
// }
case "windows":
fmt.Println("oof microshit windows detected")
exec.Command("powershell", fmt.Sprintf("(Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(%d,%d)", i+1, brightness))
case "darwin":
fmt.Println("Sorry the changing brightness part is not supported for macOS")
default:
fmt.Println("Seriously tf is this OS")
}
}
func main() {
// setBrightness(30, 0)
n := screenshot.NumActiveDisplays()
refreshRate := 0.5
var normalBrightness int16 = 30
var maxDeviation int16 = 20
var threshold int8 = 3
prevLightness := make([]int8, n)
fmt.Println()
fmt.Printf("Detected %d displays connected\n", n)
// for i := 0; i < n; i++ {
// img, err := screenshot.CaptureDisplay(i)
// if err != nil {
// log.Fatalln(err)
// }
// prevLightness[i] = getPercentLightness(img)
// fmt.Printf("Display #%d\nLightness: %d%%\n\n", i, prevLightness[i])
// }
for range time.Tick(time.Millisecond * time.Duration(1000*refreshRate)) {
go func() {
unchanged := true
k := screenshot.NumActiveDisplays()
if n != k {
unchanged = false
n = k
}
// var lightness []uint8
lightness := make([]int8, n)
copy(lightness, prevLightness)
for i := 0; i < n; i++ {
img, err := screenshot.CaptureDisplay(i)
if err != nil {
log.Fatalln(err)
}
lig := getPercentLightness(img)
if unchanged && (prevLightness[i] != lig) {
unchanged = false
}
lightness[i] = lig
}
if !(unchanged) {
for i := 0; i < n; i++ {
delta := (lightness[i]) - (prevLightness[i])
// var delta int8 = 3
// fmt.Printf("There was a change: delta = %d %d\n", prevLightness[i], lightness[i])
if delta > threshold || delta < -threshold {
// var brightness int8 = normalBrightness + ((int8(lightness[i]) - 50) * maxDeviation / 50)
brightness := (normalBrightness) + (50-int16(lightness[i]))*(maxDeviation)/50
fmt.Printf("Changed Brightness for Screen #%d\nLightness: %d%%\nNewBrightness: %d%%\n\n", i, lightness[i], brightness)
setBrightness(brightness, i)
}
}
prevLightness = lightness
}
// else {
// fmt.Printf("No changes were found, %d\n", lightness[0])
// }
}()
}
}
// inputFile, err := os.Open("test_img/test_img_copy.png")
// if err != nil {
// log.Fatalln(err)
// }
// defer inputFile.Close()
// img, _, err := image.Decode(inputFile)
// if err != nil {
// log.Fatalln(err)
// }
// var percentage uint = getPercentLightness(img)
// fmt.Printf("Brightness: %d%%\n", percentage)