-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
274 lines (234 loc) · 4.83 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package main
import (
"bufio"
"bytes"
"errors"
"flag"
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"time"
)
var version = "0.1-untracked-dev"
func main() {
validateDeps()
eventLoop()
}
var (
versonFlag bool
interval = 2
)
func init() {
flag.BoolVar(&versonFlag, "v", versonFlag, "show version")
flag.IntVar(&interval, "interval", interval, "mouse poll interval in secs")
flag.Parse()
if versonFlag {
fmt.Println("autoplank v" + version)
os.Exit(0)
}
}
type axis struct {
x, y int
}
type display struct {
axis axis
offset axis
name string
primary bool
}
func (d display) Within(x, y int) bool {
return x > d.offset.x &&
x < d.offset.x+d.axis.x &&
y > d.offset.y &&
y < d.offset.y+d.axis.y
}
func (d display) IsBottom(y int) bool {
return y < d.offset.y+d.axis.y && y > d.offset.y+d.axis.y-20
}
func pollMouse() <-chan axis {
aChan := make(chan axis)
go func() {
for range time.Tick(time.Second * time.Duration(interval)) {
pos, err := getMouseLocation()
if err != nil {
log.Println(err)
continue
}
aChan <- pos
}
}()
return aChan
}
func getMouseLocation() (a axis, err error) {
cmd := exec.Command("xdotool", "getmouselocation")
out, err := cmd.Output()
if err != nil {
return a, err
}
cols := strings.Fields(string(out))
if len(cols) < 4 {
return a, errors.New("unexpected output from xdotool")
}
a.x, err = strconv.Atoi(cols[0][2:])
if err != nil {
return a, err
}
a.y, err = strconv.Atoi(cols[1][2:])
if err != nil {
return a, err
}
return a, nil
}
var dLock sync.RWMutex
var displaysFound []display
func watchDisplays() {
var err error
displaysFound, err = fetchDisplays()
if err != nil {
log.Println(err)
}
for range time.Tick(time.Second * 5) {
dLock.Lock()
displaysFound, err = fetchDisplays()
if err != nil {
log.Println(err)
}
dLock.Unlock()
}
}
func getDisplays(lastUpdate time.Time) ([]display, bool) {
dLock.RLock()
defer dLock.RUnlock()
if !displaysUpdateTime.After(lastUpdate) {
return nil, false
}
if len(displaysFound) == 0 {
// this is rare and should never happen
// may be a one off and can be fixed at the next
// poll.
// let's simply log
log.Println("Error: no displays are found")
}
// create a copy to not worry about
// race conditions outside this
displaysCopy := make([]display, len(displaysFound))
copy(displaysCopy, displaysFound)
return displaysCopy, true
}
// keep track of previous displays state
var (
displaysConf string
displaysUpdateTime time.Time
)
func fetchDisplays() ([]display, error) {
cmd := exec.Command("xrandr")
out, err := cmd.Output()
if err != nil {
return nil, err
}
if string(out) == displaysConf {
// ignore
return displaysFound, nil
}
displaysConf = string(out)
displaysUpdateTime = time.Now()
var displays []display
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
line := scanner.Text()
if !strings.Contains(line, " connected") {
continue
}
cols := strings.Fields(line)
if len(cols) < 3 {
return nil, errors.New("unexpected response from xrandr")
}
var m display
m.name = cols[0]
xy := cols[2]
if xy == "primary" {
m.primary = true
xy = cols[3]
}
vals := strings.FieldsFunc(xy, func(c rune) bool {
switch c {
case 'x', '+':
return true
}
return false
})
m.axis.x, _ = strconv.Atoi(vals[0])
m.axis.y, _ = strconv.Atoi(vals[1])
m.offset.x, _ = strconv.Atoi(vals[2])
m.offset.y, _ = strconv.Atoi(vals[3])
displays = append(displays, m)
}
return displays, nil
}
const dconfPlank = "/net/launchpad/plank/docks/dock1/monitor"
func movePlankTo(d display) error {
value := fmt.Sprintf(`'%s'`, d.name)
if d.primary {
value = `''`
}
cmd := exec.Command("dconf", "read", dconfPlank)
out, err := cmd.Output()
if err != nil {
return err
}
// no need to set if its same value
if value == strings.TrimSpace(string(out)) {
return nil
}
var buf bytes.Buffer
fmt.Fprint(&buf, "attempting to move plank to "+d.name)
if d.primary {
fmt.Fprintf(&buf, " - primary")
}
log.Println(buf.String())
return exec.Command("dconf", "write", dconfPlank, value).
Run()
}
var requiredCommands = []string{
"xrandr",
"xdotool",
"dconf",
}
func validateDeps() {
errCount := 0
for _, c := range requiredCommands {
_, err := exec.LookPath(c)
if err != nil {
fmt.Fprintln(os.Stderr, c+" not found in PATH")
errCount++
}
}
if errCount > 0 {
os.Exit(1)
}
}
func eventLoop() {
var displays []display
var lastRequestTime time.Time
go watchDisplays()
var pos axis
for pos = range pollMouse() {
if ds, ok := getDisplays(lastRequestTime); ok {
lastRequestTime = time.Now()
displays = ds
}
for _, d := range displays {
if d.Within(pos.x, pos.y) && d.IsBottom(pos.y) {
err := movePlankTo(d)
if err != nil {
log.Println(err)
}
break
}
}
}
}