-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
63 lines (51 loc) · 900 Bytes
/
server.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
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
)
type switchFunc func()
const socketTpl = "\x00i3-focus-last/%d"
func startServer(sf switchFunc) error {
l, err := net.ListenUnix("unix", &net.UnixAddr{
Name: fmt.Sprintf(socketTpl, os.Getuid()),
Net: "unix",
})
if err != nil {
return err
}
for {
func() {
conn, err := l.AcceptUnix()
if err != nil {
log.Println(err)
return
}
defer conn.Close()
b, err := ioutil.ReadAll(io.LimitReader(conn, 1))
if b[0] != 's' {
log.Println("invalid command")
return
}
sf()
}()
}
}
func remoteSwitch() error {
addr := net.UnixAddr{
Name: fmt.Sprintf(socketTpl, os.Getuid()),
Net: "unix",
}
conn, err := net.DialUnix("unix", nil, &addr)
if err != nil {
return err
}
defer conn.Close()
if _, err := conn.Write([]byte{'s'}); err != nil {
return err
}
return nil
}