/
notifier.go
149 lines (133 loc) · 3.49 KB
/
notifier.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
package main
import (
"fmt"
"strings"
"strconv"
"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/xproto"
"github.com/lucas8/notifier/lib/config"
"github.com/lucas8/notifier/lib/screens"
"github.com/lucas8/notifier/lib/window"
"github.com/lucas8/notifier/lib/fifo"
"github.com/lucas8/notifier/lib/queue"
"github.com/lucas8/notifier/lib/types"
)
type KillCommand struct {}
func (c *KillCommand) Validate(str string) bool {
return str == "kill" || str == "end"
}
func (c *KillCommand) Get() types.Order {
return types.KillOrder {}
}
type RedrawCommand struct {}
func (c *RedrawCommand) Validate(str string) bool {
return str == "redraw"
}
func (c *RedrawCommand) Get() types.Order {
return types.RedrawOrder {}
}
type CloseCommand types.CloseOrder
func (c *CloseCommand) Validate(str string) bool {
if str == "close" {
c.All = false
c.Top = true
return true
} else if str == "close_all" {
c.All = true
c.Top = false
return true
}
return false
}
func (c *CloseCommand) Get() types.Order {
return types.CloseOrder(*c)
}
type NotifCommand types.NotifOrder
func (c *NotifCommand) Validate(str string) bool {
parts := strings.Fields(str)
if len(parts) != 4 || parts[0] != "notif" {
return false
}
t, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return false
}
c.Time = uint32(t)
c.Level = parts[2]
c.Text = parts[3]
return true
}
func (c *NotifCommand) Get() types.Order {
return types.NotifOrder(*c)
}
func main() {
/* Loading config */
if err := config.Load(config.ConfigPath()); err != nil {
fmt.Printf("Error when loading config : %v\n", err)
return
}
/* Opening the connection */
var conn *xgb.Conn
if c, err := xgb.NewConn(); err != nil {
fmt.Printf("Error when connecting to x11 server : %v\n", err)
return
} else {
conn = c
}
defer conn.Close()
/* Loading screens configuration */
if err := screens.Load(conn); err != nil {
fmt.Printf("Error while getting screens configuration : %v\n", err)
return
}
/* Loading window manager */
if err := window.Load(conn); err != nil {
fmt.Printf("Error while loading window manager : %v\n", err)
return
}
/* Opening the fifo */
var pipe *fifo.Fifo
if p, err := fifo.Open(); err != nil {
fmt.Printf("Error while opening the fifo : %s\n", err)
return
} else {
pipe = p
}
defer pipe.Close()
cmds := [...]fifo.Command {
&KillCommand {},
&RedrawCommand {},
&CloseCommand {false, false, 0},
&NotifCommand {0, "", ""},
}
for _, cmd := range cmds {
pipe.AddCmd(cmd)
}
/* Opening the queue */
var notifs *queue.Queue
if q, err := queue.Open(conn); err != nil {
fmt.Printf("Error while opening the queue : %s\n", err)
return
} else {
notifs = q
}
/* Main loop */
orders := make(chan types.Order, 10)
go xloop(conn, orders)
go pipe.ReadOrders(orders)
notifs.Run(orders)
}
func xloop(conn *xgb.Conn, c chan types.Order) {
for {
ev, xerr := conn.WaitForEvent()
if ev == nil && xerr == nil {
c <- types.KillOrder {}
}
if ev != nil {
switch ev.(type) {
case xproto.ExposeEvent:
c <- types.RedrawOrder {}
}
}
}
}