-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (92 loc) · 2.16 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/urfave/cli/v2"
"github.com/vampy/LinuxMonitorControl/pkg/build"
"github.com/vampy/LinuxMonitorControl/pkg/ddc"
"github.com/vampy/LinuxMonitorControl/pkg/xorg"
"golang.org/x/sync/errgroup"
)
// TODO systray https://www.reddit.com/r/golang/comments/bh0p2h/go_and_the_linux_system_tray/
func main() {
app := cli.NewApp()
app.Version = build.Version()
app.Flags = []cli.Flag{
&cli.IntFlag{
Name: "brightness",
Value: -1,
Usage: "brightness value 0-100",
Aliases: []string{"b"},
},
&cli.IntFlag{
Name: "display",
Value: -1,
Usage: "display index to control. -1 means all displays",
Aliases: []string{"d"},
},
&cli.IntFlag{
Name: "contrast",
Value: -1,
Usage: "contrast value 0-100",
Aliases: []string{"c"},
},
}
app.Before = func(c *cli.Context) error {
numDisplays := xorg.NumActiveDisplays()
fmt.Fprintf(c.App.Writer, "NumDisplays = %d\n\n", numDisplays)
return nil
}
app.Action = func(c *cli.Context) error {
ddc, err := ddc.New()
if err != nil {
return err
}
fmt.Printf("%v\n\n", ddc)
brightness := c.Int("brightness")
contrast := c.Int("contrast")
display := c.Int("display")
if brightness >= 0 && brightness <= 100 {
fmt.Printf("Using brightness = %d\n\n", brightness)
if display == -1 {
// All
numDisplays := xorg.NumActiveDisplays()
errs, _ := errgroup.WithContext(context.Background())
for i := 0; i < numDisplays; i++ {
i := i
errs.Go(func() error {
return ddc.SetBrightness(i, brightness)
})
}
err := errs.Wait()
if err != nil {
return err
}
} else {
// One Display
fmt.Printf("For Display = %d\n", display)
err := ddc.SetBrightness(display, brightness)
if err != nil {
return err
}
}
}
if contrast >= 0 && contrast <= 100 {
fmt.Printf("Using contrast = %d\n\n", contrast)
if display != -1 {
fmt.Printf("For Display = %d\n", display)
err := ddc.SetContrast(display, contrast)
if err != nil {
return err
}
}
}
return nil
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}