-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.go
181 lines (159 loc) · 4.03 KB
/
demo.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
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
package main
import (
"bufio"
"fmt"
"github.com/gek64/displayController"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"syscall"
)
var (
LANGZH = []string{
"找不到任何物理显示器",
"找到 %d 个显示器\n",
"显示器 handle 编号: %d\n",
"显示器描述: %s\n",
"显示器当前亮度: %d, 最大亮度: %d\n",
"输入你需要控制的显示器 handle 编号,直接回车默认选择所有显示器",
"选择显示器错误,请重新输入你需要控制的显示器",
"输入设置的新的亮度",
}
LANGEN = []string{
"can't find any physical display",
"found %d display monitors\n",
"display monitor handle No: %d\n",
"display monitor description: %s\n",
"display monitor current brightness value: %d, maximum brightness value: %d\n",
"enter the display handle No. you need to control, or select all the display by press ENTER",
"select the display error, please re-enter the display you need to control",
"enter the new brightness of the display",
}
LANG []string
)
func runDemo() (err error) {
if lang, _ := getLocale(); lang == "zh" {
LANG = LANGZH
} else {
LANG = LANGEN
}
err = showBasicInfo()
if err != nil {
return err
}
useAll, handle, err := selectMonitor()
if err != nil {
return err
}
err = setMonitor(useAll, handle)
if err != nil {
return err
}
return nil
}
func showBasicInfo() (err error) {
if len(monitors) == 0 {
return fmt.Errorf(LANG[0])
}
fmt.Printf(LANG[1], len(monitors))
for _, monitor := range monitors {
fmt.Printf(LANG[2], monitor.PhysicalInfo.Handle)
fmt.Printf(LANG[3], monitor.PhysicalInfo.Description)
current, max, err := displayController.GetVCPFeatureAndVCPFeatureReply(monitor.PhysicalInfo.Handle, displayController.Brightness)
if err != nil {
return err
}
fmt.Printf(LANG[4], current, max)
fmt.Println()
}
return nil
}
func selectMonitor() (useAll bool, handle int, err error) {
buf := bufio.NewReader(os.Stdin)
fmt.Println(LANG[5])
fmt.Printf("> ")
line, _, err := buf.ReadLine()
if err != nil {
return false, -1, err
}
if string(line) == "" {
return true, -1, nil
}
handle, err = strconv.Atoi(string(line))
if err != nil {
return false, -1, fmt.Errorf(LANG[6])
}
return false, handle, nil
}
func setMonitor(useAll bool, handle int) (err error) {
buf := bufio.NewReader(os.Stdin)
fmt.Println(LANG[7])
fmt.Printf("> ")
line, _, err := buf.ReadLine()
if err != nil {
return err
}
newValue, err := strconv.Atoi(string(line))
if err != nil {
return err
}
if useAll {
for _, monitor := range monitors {
err := displayController.SetVCPFeature(monitor.PhysicalInfo.Handle, displayController.Brightness, newValue)
if err != nil {
return err
}
}
}
if handle != -1 {
err := displayController.SetVCPFeature(syscall.Handle(handle), displayController.Brightness, newValue)
if err != nil {
return err
}
}
return nil
}
// get system language and location
// https://stackoverflow.com/a/64560642
func getLocale() (string, string) {
osHost := runtime.GOOS
defaultLang := "en"
defaultLoc := "US"
switch osHost {
case "windows":
// Exec powershell Get-Culture on Windows.
cmd := exec.Command("powershell", "Get-Culture | select -exp Name")
output, err := cmd.Output()
if err == nil {
langLocRaw := strings.TrimSpace(string(output))
langLoc := strings.Split(langLocRaw, "-")
lang := langLoc[0]
loc := langLoc[1]
return lang, loc
}
case "darwin":
// Exec powershell Get-Culture on macOS.
cmd := exec.Command("sh", "osascript -e 'user locale of (get system info)'")
output, err := cmd.Output()
if err == nil {
langLocRaw := strings.TrimSpace(string(output))
langLoc := strings.Split(langLocRaw, "_")
lang := langLoc[0]
loc := langLoc[1]
return lang, loc
}
case "linux":
envLang, ok := os.LookupEnv("LANG")
if ok {
langLocRaw := strings.TrimSpace(envLang)
langLocRaw = strings.Split(envLang, ".")[0]
langLoc := strings.Split(langLocRaw, "_")
lang := langLoc[0]
loc := langLoc[1]
return lang, loc
}
}
return defaultLang, defaultLoc
}