-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi3-hotbar.go
153 lines (130 loc) · 2.89 KB
/
i3-hotbar.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
package main
import (
"flag"
"log"
"os/exec"
"sync"
"time"
"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/randr"
"github.com/BurntSushi/xgb/xproto"
"github.com/BurntSushi/xgbutil"
"github.com/BurntSushi/xgbutil/xinerama"
"github.com/BurntSushi/xgbutil/xwindow"
"github.com/go-vgo/robotgo"
)
var (
headLock sync.RWMutex
heads xinerama.Heads
)
func main() {
refreshPeriod := flag.Duration("refreshPeriod", 500*time.Millisecond, "Refresh period")
barHeight := flag.Int("barHeight", 30, "Bar height in pixels")
flag.Parse()
go updateScreens()
// Wait for first update
for {
headLock.RLock()
h := heads
headLock.RUnlock()
if h == nil {
time.Sleep(100 * time.Millisecond)
} else {
break
}
}
barHidden := true
for {
inBar := cursorInBar(*barHeight)
if barHidden && inBar {
barHidden = false
showBar()
} else if !barHidden && !inBar {
barHidden = true
hideBar()
}
time.Sleep(*refreshPeriod)
}
}
func updateScreens() {
// Connect to the X server using the DISPLAY environment variable.
X, err := xgb.NewConn()
if err != nil {
log.Fatal(err)
}
Xutil, err := xgbutil.NewConnXgb(X)
if err != nil {
log.Fatal(err)
}
// Every extension must be initialized before it can be used.
err = randr.Init(X)
if err != nil {
log.Fatal(err)
}
// Get the root window on the default screen.
root := xproto.Setup(X).DefaultScreen(X).Root
// Tell RandR to send us events. (I think these are all of them, as of
// 1.3.)
err = randr.SelectInputChecked(X, root,
randr.NotifyMaskScreenChange|
randr.NotifyMaskCrtcChange|
randr.NotifyMaskOutputChange|
randr.NotifyMaskOutputProperty).Check()
if err != nil {
log.Fatal(err)
}
// Wrap the root window in a nice Window type.
rootWin := xwindow.New(Xutil, root)
// Get the geometry of the root window.
rgeom, err := rootWin.Geometry()
if err != nil {
log.Fatal(err)
}
for {
// Get the rectangles for each of the active physical heads.
// These are returned sorted in order from left to right and
// then top to bottom. But first check if Xinerama is enabled.
// If not, use root geometry.
var h xinerama.Heads
if Xutil.ExtInitialized("XINERAMA") {
h, err = xinerama.PhysicalHeads(Xutil)
if err != nil {
log.Fatal(err)
}
} else {
h = xinerama.Heads{rgeom}
}
headLock.Lock()
heads = h
headLock.Unlock()
_, err := X.WaitForEvent()
if err != nil {
log.Fatal(err)
}
}
}
func cursorInBar(barHeight int) bool {
x, y := robotgo.GetMousePos()
headLock.RLock()
h := heads
headLock.RUnlock()
i := 1
for _, head := range h[1:] {
if x <= head.X() {
break
}
i++
}
if y > h[i-1].Height()-barHeight {
return true
}
return false
}
func showBar() {
showBar := exec.Command("i3-msg", "-q", "bar", "hidden_state", "show")
showBar.Start()
}
func hideBar() {
hideBar := exec.Command("i3-msg", "-q", "bar", "hidden_state", "hide")
hideBar.Start()
}