-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_posterity-physical-monitor-api.go
91 lines (77 loc) · 2.63 KB
/
_posterity-physical-monitor-api.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
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
package main
import (
"fmt"
"syscall"
"unsafe"
)
var (
user32 = syscall.NewLazyDLL("user32.dll")
dxva2 = syscall.NewLazyDLL("dxva2.dll")
enumDisplayMonitors = user32.NewProc("EnumDisplayMonitors")
getNumberOfPhysicalMonitorsFromHMONITOR = dxva2.NewProc("GetNumberOfPhysicalMonitorsFromHMONITOR")
getPhysicalMonitorsFromHMONITOR = dxva2.NewProc("GetPhysicalMonitorsFromHMONITOR")
getMonitorBrightness = dxva2.NewProc("GetMonitorBrightness")
setMonitorBrightness = dxva2.NewProc("SetMonitorBrightness")
)
type RECT struct {
Left, Top, Right, Bottom int32
}
type PHYSICAL_MONITOR struct {
hPhysicalMonitor syscall.Handle
szPhysicalMonitorDescription [128]uint16
}
func main() {
err := enumDisplayMonitors.Find()
if err != nil {
fmt.Printf("Failed to find EnumDisplayMonitors: %v\n", err)
return
}
err = enumDisplayMonitors.Call(0, 0, syscall.NewCallback(monitorEnumProc), 0)
if err != nil && err != syscall.Errno(0) {
fmt.Printf("EnumDisplayMonitors failed: %v\n", err)
}
}
func monitorEnumProc(hMonitor syscall.Handle, hdcMonitor syscall.Handle, lprcMonitor *RECT, dwData uintptr) uintptr {
var numPhysicalMonitors uint32
ret, _, err := getNumberOfPhysicalMonitorsFromHMONITOR.Call(uintptr(hMonitor), uintptr(unsafe.Pointer(&numPhysicalMonitors)))
if ret == 0 {
fmt.Printf("GetNumberOfPhysicalMonitorsFromHMONITOR failed: %v\n", err)
return 1
}
physicalMonitors := make([]PHYSICAL_MONITOR, numPhysicalMonitors)
ret, _, err = getPhysicalMonitorsFromHMONITOR.Call(
uintptr(hMonitor),
uintptr(numPhysicalMonitors),
uintptr(unsafe.Pointer(&physicalMonitors[0])),
)
if ret == 0 {
fmt.Printf("GetPhysicalMonitorsFromHMONITOR failed: %v\n", err)
return 1
}
for _, monitor := range physicalMonitors {
var minimumBrightness, currentBrightness, maximumBrightness uint32
ret, _, err = getMonitorBrightness.Call(
uintptr(monitor.hPhysicalMonitor),
uintptr(unsafe.Pointer(&minimumBrightness)),
uintptr(unsafe.Pointer(¤tBrightness)),
uintptr(unsafe.Pointer(&maximumBrightness)),
)
if ret == 0 {
fmt.Printf("GetMonitorBrightness failed: %v\n", err)
continue
}
fmt.Printf("Current brightness: %d\n", currentBrightness)
// Increase brightness by 10%
newBrightness := currentBrightness + 10
if newBrightness > maximumBrightness {
newBrightness = maximumBrightness
}
ret, _, err = setMonitorBrightness.Call(uintptr(monitor.hPhysicalMonitor), uintptr(newBrightness))
if ret == 0 {
fmt.Printf("SetMonitorBrightness failed: %v\n", err)
} else {
fmt.Printf("New brightness: %d\n", newBrightness)
}
}
return 1
}