-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbus.go
80 lines (68 loc) · 1.46 KB
/
dbus.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
package blocks
import (
"context"
"github.com/godbus/dbus/v5"
. "github.com/kraftwerk28/gost/core"
)
type DbusConfig struct {
ObjectPath string `yaml:"object_path"`
InitialText string `yaml:"initial_text"`
}
const dbusGetProperty = "org.freedesktop.DBus.Properties.Get"
const dbusPropertiesIface = "org.freedesktop.DBus.Properties"
const dbusCustomInterface = "com.kraftwerk28.gost"
const dbusObjectPath dbus.ObjectPath = "/org/freedesktop/DBus"
type DbusBlock struct {
DbusConfig
text string
}
func NewDbusBlock() I3barBlocklet {
return &DbusBlock{}
}
type busObject struct {
b *DbusBlock
ch UpdateChan
}
func (o *busObject) SetStatus(text string) *dbus.Error {
o.b.text = text
o.ch.SendUpdate()
return nil
}
func (b *DbusBlock) Run(ch UpdateChan, ctx context.Context) {
b.text = b.InitialText
ch.SendUpdate()
conn, err := dbus.SessionBus()
if err != nil {
Log.Println(err)
return
}
defer conn.Close()
dbusObject := &busObject{b, ch}
if err := conn.Export(
dbusObject,
dbus.ObjectPath(b.ObjectPath),
dbusCustomInterface,
); err != nil {
Log.Print(err)
return
}
if _, err := conn.RequestName(
dbusCustomInterface,
dbus.NameFlagAllowReplacement,
); err != nil {
Log.Print(err)
return
}
<-ctx.Done()
}
func (b *DbusBlock) GetConfig() interface{} {
return &b.DbusConfig
}
func (b *DbusBlock) Render(cfg *AppConfig) []I3barBlock {
return []I3barBlock{{
FullText: b.text,
}}
}
func init() {
RegisterBlocklet("dbus", NewDbusBlock)
}