-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluez.go
180 lines (166 loc) · 4.14 KB
/
bluez.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
package blocks
import (
"context"
"strings"
"github.com/godbus/dbus/v5"
. "github.com/kraftwerk28/gost/core"
"github.com/kraftwerk28/gost/core/formatting"
)
type bluezObjectManagerOutput map[dbus.ObjectPath](map[string](map[string]dbus.Variant))
// Displays connected bluetooth devices
type BluezBlockConfig struct {
BaseBlockletConfig `yaml:",inline"`
// Mac address of the device
Device string `yaml:"mac"`
Format *ConfigFormat `yaml:"format"`
// Device format
DeviceFormat *ConfigFormat `yaml:"device_format"`
Icons map[string]string `yaml:"icons"`
ExcludeMac []string `yaml:"exclude"`
}
type bluezDevice struct {
path dbus.ObjectPath
connected bool
name, alias, icon string
}
type BluezBlock struct {
BluezBlockConfig
dbus *dbus.Conn
devices map[dbus.ObjectPath]*bluezDevice
}
func NewBluezBlock() I3barBlocklet {
b := BluezBlock{}
b.DeviceFormat = NewConfigFormatFromString("{icon}")
return &b
}
func (t *BluezBlock) GetConfig() interface{} {
return &t.BluezBlockConfig
}
func (b *BluezBlock) isExcluded(addr string) bool {
for _, p := range b.ExcludeMac {
if p == addr {
return true
}
}
return false
}
func (b *BluezBlock) loadDevices() (err error) {
var bluezObjects bluezObjectManagerOutput
if err = b.dbus.Object("org.bluez", "/").Call(
"org.freedesktop.DBus.ObjectManager.GetManagedObjects", 0,
).Store(&bluezObjects); err != nil {
return
}
b.devices = make(map[dbus.ObjectPath]*bluezDevice)
for path, v := range bluezObjects {
if info, ok := v["org.bluez.Device1"]; ok {
var addr, name, alias, icon string
var connected bool
info["Address"].Store(&addr)
if b.isExcluded(addr) {
continue
}
if i, ok := info["Icon"]; ok {
i.Store(&icon)
}
info["Name"].Store(&name)
info["Alias"].Store(&alias)
info["Connected"].Store(&connected)
b.devices[path] = &bluezDevice{path, connected, name, alias, icon}
}
}
return
}
func (t *BluezBlock) addSignals() (err error) {
b := t.dbus
if err = b.AddMatchSignal(
dbus.WithMatchPathNamespace("/org/bluez"),
dbus.WithMatchInterface("org.freedesktop.DBus.Properties"),
dbus.WithMatchMember("PropertiesChanged"),
dbus.WithMatchArg(0, "org.bluez.Device1"),
); err != nil {
return
}
if err = b.AddMatchSignal(
dbus.WithMatchObjectPath("/"),
dbus.WithMatchInterface("org.freedesktop.DBus.ObjectManager"),
dbus.WithMatchMember("InterfaceAdded"),
); err != nil {
return
}
if err = b.AddMatchSignal(
dbus.WithMatchObjectPath("/"),
dbus.WithMatchInterface("org.freedesktop.DBus.ObjectManager"),
dbus.WithMatchMember("InterfaceRemoved"),
); err != nil {
return
}
return
}
func (t *BluezBlock) Run(ch UpdateChan, ctx context.Context) {
b, err := dbus.ConnectSystemBus()
if err != nil {
Log.Print(err)
return
}
defer b.Close()
t.dbus = b
if err := t.addSignals(); err != nil {
Log.Print(err)
return
}
if err := t.loadDevices(); err != nil {
Log.Print(err)
} else {
ch.SendUpdate()
}
c := make(chan *dbus.Signal)
b.Signal(c)
for {
select {
case <-ctx.Done():
return
case s := <-c:
switch s.Name {
case "org.freedesktop.DBus.ObjectManager.InterfaceAdded",
"org.freedesktop.DBus.ObjectManager.InterfaceRemoved":
t.loadDevices()
ch.SendUpdate()
case "org.freedesktop.DBus.Properties.PropertiesChanged":
if t.devices[s.Path] == nil {
t.loadDevices()
ch.SendUpdate()
break
}
props := s.Body[1].(map[string]dbus.Variant)
if conn, ok := props["Connected"]; ok {
var connected bool
conn.Store(&connected)
t.devices[s.Path].connected = connected
ch.SendUpdate()
}
}
}
}
}
func (b *BluezBlock) Render(cfg *AppConfig) []I3barBlock {
labels := []string{}
for _, d := range b.devices {
if d.connected {
args := formatting.NamedArgs{
"icon": b.Icons[d.icon],
"name": d.name,
"alias": d.alias,
}
labels = append(labels, b.DeviceFormat.Expand(args))
}
}
return []I3barBlock{{
FullText: b.Format.Expand(formatting.NamedArgs{
"devices": strings.Join(labels, " "),
}),
}}
}
func init() {
RegisterBlocklet("bluez", NewBluezBlock)
}