-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
81 lines (66 loc) · 1.62 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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"github.com/jezek/xgb"
"github.com/jezek/xgb/randr"
"github.com/jezek/xgb/xproto"
)
const defaultAutorandrPath = "/usr/bin/autorandr"
func main() {
var autorandr string
flag.StringVar(&autorandr, "path", defaultAutorandrPath, "Path to the autorandr binary")
flag.StringVar(&autorandr, "p", defaultAutorandrPath, "Path to the autorandr binary")
flag.Parse()
stat, err := os.Stat(autorandr)
if err != nil {
log.Fatal(err)
}
if (stat.Mode().Perm() & 0111) == 0 {
log.Fatal(fmt.Errorf("%s is not executable", autorandr))
} else if stat.IsDir() {
log.Fatal(fmt.Errorf("%s is a directory", autorandr))
}
X, err := xgb.NewConn()
if err != nil {
log.Fatal(err)
}
err = randr.Init(X)
if err != nil {
log.Fatal(err)
}
root := xproto.Setup(X).DefaultScreen(X).Root
err = randr.SelectInputChecked(X, root, randr.NotifyMaskScreenChange).Check()
if err != nil {
log.Fatal(err)
}
log.Println("Autorandr launcher started, waiting for events...")
for {
ev, err := X.WaitForEvent()
if err != nil {
log.Fatal(err)
}
switch eventType := ev.(type) {
case randr.ScreenChangeNotifyEvent:
output, err := runAutorandr(autorandr)
if err != nil {
log.Printf("Error calling autorandr %s", err)
continue
}
log.Println(output)
default:
log.Printf("Unsupported event type: %v\n", eventType)
}
}
}
func runAutorandr(autorandr string) (string, error) {
cmd := []string{autorandr, "--change", "--default", "default"}
output, err := exec.Command(cmd[0], cmd[1:]...).Output()
if err != nil {
return "", err
}
return string(output), nil
}