-
Notifications
You must be signed in to change notification settings - Fork 0
/
xapp.go
97 lines (80 loc) · 2.07 KB
/
xapp.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
package taskbar
import (
"encoding/binary"
"github.com/jezek/xgb"
"github.com/jezek/xgb/xproto"
)
// Reference: https://github.com/linuxmint/xapp/blob/master/libxapp/xapp-gtk-window.c
type xappData struct {
connection *xgb.Conn
progressAtom xproto.Atom
pulseAtom xproto.Atom
window xproto.Window
}
func xappConnect(xid uint32) (*xappData, error) {
const progressName = "_NET_WM_XAPP_PROGRESS"
const pulseName = "_NET_WM_XAPP_PROGRESS_PULSE"
conn, err := xgb.NewConn()
if err != nil {
return nil, err
}
data := xappData{}
data.connection = conn
progressAtom, err := xproto.InternAtom(conn, false, uint16(len(progressName)), progressName).Reply()
if err != nil {
return nil, err
}
data.progressAtom = progressAtom.Atom
pulseAtom, err := xproto.InternAtom(conn, false, uint16(len(pulseName)), pulseName).Reply()
if err != nil {
return nil, err
}
data.pulseAtom = pulseAtom.Atom
data.window = xproto.Window(xid)
return &data, nil
}
func xappDisconnect(data *xappData) error {
if data == nil {
return nil
}
if err := data.update(0, false); err != nil {
return err
}
data.connection.Close()
return nil
}
func (data *xappData) update(progress uint64, pulse bool) error {
var err error
if progress > 0 {
err = data.xChangeProperty(data.progressAtom, progress)
} else {
err = data.xDeleteProperty(data.progressAtom)
}
if err != nil {
return err
}
if pulse {
err = data.xChangeProperty(data.pulseAtom, 1)
} else {
err = data.xDeleteProperty(data.pulseAtom)
}
if err != nil {
return err
}
return nil
}
func (data *xappData) xChangeProperty(atom xproto.Atom, value uint64) error {
bslice := make([]byte, 32)
binary.LittleEndian.PutUint64(bslice, value)
err := xproto.ChangePropertyChecked(data.connection, xproto.PropModeReplace, data.window, atom, xproto.AtomCardinal, 32, 8, bslice).Check()
if err != nil {
return err
}
return nil
}
func (data *xappData) xDeleteProperty(atom xproto.Atom) error {
if err := xproto.DeletePropertyChecked(data.connection, data.window, atom).Check(); err != nil {
return err
}
return nil
}