-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (80 loc) · 1.75 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
package main
import (
"bytes"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/jezek/xgb"
"github.com/jezek/xgb/xproto"
"github.com/lordrusk/zara/modules"
)
var update = make(chan struct{})
var bar = make([]string, len(mods))
/*
wrapper to handle output and error
of module.Moudle.Run()
*/
func run(m modules.Module) {
out, err := m.Run()
if err != nil {
fmt.Printf("Failed to run '%s': %s\n", m.Name(), err)
return
}
bar[m.Pos()] = strings.Split(out, "\n")[0]
update <- struct{}{}
}
func main() {
x, err := xgb.NewConn() /* connect to X */
if err != nil {
fmt.Printf("Cannot connect to X: %s\n", err)
return
}
defer x.Close()
root := xproto.Setup(x).DefaultScreen(x).Root
go func() {
var buf bytes.Buffer
for range update {
for i := 0; i < len(mods); i++ {
if bar[i] != "" {
buf.WriteString(delim)
buf.WriteString(bar[i])
}
}
if buf.Len() > 0 {
xproto.ChangeProperty(x, xproto.PropModeReplace, root, xproto.AtomWmName, xproto.AtomString, 8, uint32(buf.Len()-len(delim)), buf.Bytes()[len(delim):]) /* set the root window name */
buf.Reset()
}
}
}()
sigs := make(chan os.Signal, 1024)
sigMap := make(map[os.Signal][]modules.Module)
for i := 0; i < len(mods); i++ {
go func(i int) {
mods[i].SetPos(i)
if mods[i].Sig() != 0 {
sig := syscall.Signal(34 + mods[i].Sig())
signal.Notify(sigs, sig)
sigMap[sig] = append(sigMap[sig], mods[i])
}
run(mods[i]) /* build bar */
if mods[i].Int() != 0 {
interval := time.Duration(mods[i].Int()) * time.Second
for {
time.Sleep(interval)
run(mods[i])
}
}
}(i)
}
for sig := range sigs {
go func(sig *os.Signal) {
ms := sigMap[*sig]
for _, m := range ms {
go run(m)
}
}(&sig)
}
}