-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
83 lines (73 loc) · 1.61 KB
/
events.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
)
type ClickMessage struct {
Name string `json:"name"`
Button int `json:"button"`
Event int `json:"event"`
X int `json:"x"`
Y int `json:"y"`
RelativeX int `json:"relative_x"`
RelativeY int `json:"relative_y"`
Width int `json:"width"`
Height int `json:"height"`
Scale int `json:"scale"`
}
const SIGRTMIN = 34
func ReadInput() {
reader := bufio.NewReader(os.Stdin)
for {
line, err := reader.ReadString('\n')
if err != nil {
return
}
// decode message
line = strings.Trim(line, "[], \n")
clickMsg := ClickMessage{}
err = json.Unmarshal([]byte(line), &clickMsg)
if err != nil {
continue
}
// update clicked field and re-draw
mu.Lock()
UpdateModuleByName(
clickMsg.Name,
0,
[]string{fmt.Sprintf("BLOCK_BUTTON=%d", clickMsg.Button)})
draw()
mu.Unlock()
}
}
func ListenFor(signalNumber int, blockName string) {
channel := make(chan os.Signal, 1)
signal.Notify(channel, syscall.Signal(SIGRTMIN+signalNumber))
<-channel
mu.Lock()
UpdateModuleByName(blockName, 0, []string{})
draw()
mu.Unlock()
ListenFor(signalNumber, blockName)
}
func ListenToReloadConfig() {
channel := make(chan os.Signal, 1)
signal.Notify(channel, syscall.Signal(syscall.SIGUSR1))
<-channel
mu.Lock()
fmt.Printf(
`[{"full_text": "reloading config...", "color": "%s"}],`,
config.Colors["WHITE"])
config = LoadConfig()
for i := 0; i < len(config.Modules); i++ {
UpdateModule(i, 0, []string{})
}
draw()
mu.Unlock()
ListenToReloadConfig()
}