-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcfg_utils.go
410 lines (370 loc) · 7.58 KB
/
cfg_utils.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/ziutek/mdtwm/xgb_patched"
)
func rgbColor(r, g, b uint16) uint32 {
c, err := conn.AllocColor(screen.DefaultColormap, r, g, b)
if err != nil {
l.Fatalf("Cannot allocate a color (%x,%x,%x): %s", r, g, b, err)
}
return c.Pixel
}
func namedColor(name string) uint32 {
c, err := conn.AllocNamedColor(screen.DefaultColormap, name)
if err != nil {
l.Fatalf("Cannot allocate a color by name '%s': %s", name, err)
}
return c.Pixel
}
type TextList []string
func (l TextList) Contains(e string) bool {
for _, v := range l {
if v == e {
return true
}
}
return false
}
type Cmd struct {
Func func(interface{}) error
Param interface{}
}
func (c *Cmd) Run() error {
return c.Func(c.Param)
}
func spawn(cmd interface{}) error {
// TODO: check what filedescriptors are inherited from WM by cmd when
// exec.Command is used
args := strings.Split(cmd.(string), " ")
return exec.Command(args[0], args[1:]...).Start()
}
func closeCurrentWindow(cmd interface{}) error {
if currentBox == nil {
return nil
}
if b, ok := currentBox.(*BoxedWindow); ok {
if b.Protocols().Contains(AtomWmDeleteWindow) {
b.SendMessage(AtomWmDeleteWindow, b.Window())
} else {
b.Window().Destroy()
}
}
return nil
}
func exit(retval interface{}) error {
os.Exit(retval.(int))
return nil
}
func chDesk(deskNum interface{}) error {
setCurrentDesk(deskNum.(int))
return nil
}
func nextDesk(interface{}) error {
setNextDesk()
return nil
}
func prevDesk(interface{}) error {
setPrevDesk()
return nil
}
// Keycodes
const (
KeyA = 0x0061
KeyB = 0x0062
KeyC = 0x0063
KeyD = 0x0064
KeyE = 0x0065
KeyF = 0x0066
KeyG = 0x0067
KeyH = 0x0068
KeyI = 0x0069
KeyJ = 0x006a
KeyK = 0x006b
KeyL = 0x006c
KeyM = 0x006d
KeyN = 0x006e
KeyO = 0x006f
KeyP = 0x0070
KeyQ = 0x0071
KeyR = 0x0072
KeyS = 0x0073
KeyT = 0x0074
KeyU = 0x0075
KeyV = 0x0076
KeyW = 0x0077
KeyX = 0x0078
KeyY = 0x0079
KeyZ = 0x007a
Key0 = 0x0030
Key1 = 0x0031
Key2 = 0x0032
Key3 = 0x0033
Key4 = 0x0034
Key5 = 0x0035
Key6 = 0x0036
Key7 = 0x0037
Key8 = 0x0038
Key9 = 0x0039
KeyMinus = 0x002d
KeyEqual = 0x003d
KeySemicolon = 0x003b
KeyComma = 0x002c
KeyPeriod = 0x002e
KeySlash = 0x002f
KeyApostrophe = 0x0027
KeyBackslash = 0x005c
KeyBracketRight = 0x005d
KeyBracketLeft = 0x005b
KeySpace = 0x0020
KeyBackSpace = 0xff08
KeyTab = 0xff09
KeyReturn = 0xff0d
KeyPause = 0xff13
KeyScrollLock = 0xff14
KeySysReq = 0xff15
KeyEscape = 0xff1b
KeyInsert = 0xff63 // ?
KeyDelete = 0xffff
KeyHome = 0xff50
KeyPageDown = 0xff56
KeyPageUp = 0xff55
KeyEnd = 0xff57
KeyLeft = 0xff51
KeyUp = 0xff52
KeyRight = 0xff53
KeyDown = 0xff54
KeyF1 = 0xffbe
KeyF2 = 0xffbf
KeyF3 = 0xffc0
KeyF4 = 0xffc1
KeyF5 = 0xffc2
KeyF6 = 0xffc3
KeyF7 = 0xffc4
KeyF8 = 0xffc5
KeyF9 = 0xffc6
KeyF10 = 0xffc7
KeyF11 = 0xffc8
KeyF12 = 0xffc9
)
var stdCursorFont xgb.Id
func stdCursor(id uint16) xgb.Id {
if stdCursorFont == 0 {
stdCursorFont = conn.NewId()
conn.OpenFont(stdCursorFont, "cursor")
}
cursor := conn.NewId()
conn.CreateGlyphCursor(cursor, stdCursorFont, stdCursorFont, id, id+1,
0, 0, 0, 0xffff, 0xffff, 0xffff)
return cursor
}
type Status struct {
curDesk, desks int
title string
}
type StatusLogger interface {
Log(s Status)
Start()
}
type sysStat struct {
act, sum uint64
}
type Dzen2Logger struct {
io.Writer
FgColor string
BgColor string
BatPath string
TimeFormat string
InfoPos int16
ch chan *Status
i [9]byte
c [4]byte
s [1]byte
stat []sysStat
}
func (d *Dzen2Logger) invColors() {
fmt.Fprintf(d.Writer, "^bg(%s)^fg(%s)", d.FgColor, d.BgColor)
}
func (d *Dzen2Logger) nrmColors() {
fmt.Fprintf(d.Writer, "^bg(%s)^fg(%s)", d.BgColor, d.FgColor)
}
func (d *Dzen2Logger) Start() {
if d.InfoPos < 0 {
_, _, width, _ := root.PosSize()
d.InfoPos += width
}
d.ch = make(chan *Status)
if f, err := os.Open("/proc/stat"); err == nil {
sc := bufio.NewScanner(f)
n := 0
for sc.Scan() {
line := sc.Text()
if !strings.HasPrefix(line, "cpu") {
break
}
n++
}
f.Close()
if sc.Err() == nil {
d.stat = make([]sysStat, n-1)
}
}
go d.thr()
}
func (d *Dzen2Logger) Log(s Status) {
d.ch <- &s
}
func readFull(fname string, buf []byte) (int, error) {
f, err := os.Open(fname)
if err != nil {
return 0, err
}
defer f.Close()
return io.ReadFull(f, buf)
}
func (d *Dzen2Logger) batInfo() string {
if d.BatPath == "" {
return ""
}
c := d.c[:]
n, err := readFull(filepath.Join(d.BatPath, "capacity"), c)
if err != nil && err != io.ErrUnexpectedEOF {
return ""
}
if n > 0 {
c = c[:n-1]
} else {
c = nil
}
i := d.i[:]
i[0] = ' '
n, err = readFull(filepath.Join(d.BatPath, "current_now"), i[1:])
if err != nil && err != io.ErrUnexpectedEOF {
return ""
}
if n > 0 {
if n > 3 {
i = i[:n-3]
} else {
i = i[:2]
i[1] = '0'
}
} else {
i = i[:1]
}
_, err = readFull(filepath.Join(d.BatPath, "status"), d.s[:])
if err == nil && d.s[0] == 'C' {
i[0] = '~'
}
return fmt.Sprintf("[%s%%%5smA]", c, i)
}
func (d *Dzen2Logger) cpuLoad() string {
if d.stat == nil {
return ""
}
f, err := os.Open("/proc/stat")
if err != nil {
return ""
}
defer f.Close()
sc := bufio.NewScanner(f)
load := make([]string, len(d.stat))
i := 0
for sc.Scan() {
line := sc.Text()
if !strings.HasPrefix(line, "cpu") {
break
}
if strings.HasPrefix(line, "cpu ") {
continue
}
a := strings.Fields(line)
user, _ := strconv.ParseUint(a[1], 10, 64)
nice, _ := strconv.ParseUint(a[2], 10, 64)
system, _ := strconv.ParseUint(a[3], 10, 64)
idle, _ := strconv.ParseUint(a[4], 10, 64)
iowait, _ := strconv.ParseUint(a[5], 10, 64)
irq, _ := strconv.ParseUint(a[6], 10, 64)
soft, _ := strconv.ParseUint(a[7], 10, 64)
act := user + nice + system + iowait + irq + soft
sum := act + idle
dact := act - d.stat[i].act + 1
dsum := sum - d.stat[i].sum + 1
percent := strconv.FormatUint((dact*100+dsum/2)/dsum, 10)
switch len(percent) {
case 1:
load[i] = " " + percent
case 2:
load[i] = " " + percent
default:
load[i] = " ^^^^"
}
d.stat[i].act = act
d.stat[i].sum = sum
i++
}
return "cpu:" + strings.Join(load, "")
}
func (d *Dzen2Logger) thr() {
var s *Status
tick := time.Tick(time.Second)
for {
select {
case <-tick:
case s = <-d.ch:
s.curDesk++ // Printed desk names starts from 1
}
if s == nil {
continue
}
d.nrmColors()
for i := 1; i <= s.desks; i++ {
if i == s.curDesk {
d.invColors()
}
fmt.Fprintf(d.Writer, " %d ", i)
if i == s.curDesk {
d.nrmColors()
}
}
t := time.Now()
fmt.Fprintf(
d.Writer, " %s^pa(%d)%s %13s %s\n",
s.title, d.InfoPos, d.cpuLoad(), d.batInfo(),
t.Format(d.TimeFormat),
)
}
}
func (c *Config) Load(fname string) {
f, err := os.Open(fname)
if err != nil {
if e, ok := err.(*os.PathError); !ok || e.Err != syscall.ENOENT {
l.Fatalf("Can't open a configuration file: %s", err)
}
// Configuration file doesn't exists: create default
if f, err = os.Create(fname); err != nil {
l.Fatal("Can't create a configuration file: ", err)
}
buf, err := json.MarshalIndent(c, "", "\t")
if err != nil {
l.Fatal("Can't encode a configuration: ", err)
}
if _, err = f.Write(buf); err != nil {
l.Fatal("Can't write a configuration file: ", err)
}
} else {
if err = json.NewDecoder(f).Decode(c); err != nil {
l.Fatal("Can't decode a configuration file: ", err)
}
}
}