-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathdbus.go
More file actions
59 lines (49 loc) · 1.51 KB
/
Copy pathdbus.go
File metadata and controls
59 lines (49 loc) · 1.51 KB
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
package main
import (
"github.com/godbus/dbus/v5"
"github.com/godbus/dbus/v5/introspect"
)
const (
dbusInterface = "io.github.muesli.DeckMaster"
dbusMonitorPath = "/Monitor"
introInterface = "org.freedesktop.DBus.Introspectable"
intro = `<node>
<interface name="` + dbusInterface + `">
<method name="ActiveWindowChanged">
<arg direction="in" type="s" />
<arg direction="in" type="s" />
</method>
</interface>` + introspect.IntrospectDataString + "<node>"
)
type ActiveWindow struct {
resource string
title string
id string
}
type WindowChannel struct {
channel chan ActiveWindow
}
func (w *WindowChannel) ActiveWindowChanged(class, title, id string) *dbus.Error {
w.channel <- ActiveWindow{class, title, id}
return nil
}
func MonitorActiveWindowChanged() <-chan ActiveWindow {
w := WindowChannel{make(chan ActiveWindow)}
cnn, _ := dbus.SessionBus()
_ = cnn.Export(&w, dbusMonitorPath, dbusInterface)
introspectable := introspect.Introspectable(intro)
_ = cnn.Export(introspectable, dbusMonitorPath, introInterface)
reply, e := cnn.RequestName(dbusInterface, dbus.NameFlagDoNotQueue)
if e != nil {
errorLog(e, "failed to request name on active window changed")
}
if reply != dbus.RequestNameReplyPrimaryOwner {
errorLogF("service '%s' already running", dbusInterface)
}
return w.channel
}
func CallDBus(object, path, method string, args ...interface{}) error {
cnn, _ := dbus.SessionBus()
o := cnn.Object(object, dbus.ObjectPath(path))
return o.Call(method, 0, args...).Err
}