@@ -13,6 +13,8 @@ type BusObject interface {
13
13
CallWithContext (ctx context.Context , method string , flags Flags , args ... interface {}) * Call
14
14
Go (method string , flags Flags , ch chan * Call , args ... interface {}) * Call
15
15
GoWithContext (ctx context.Context , method string , flags Flags , ch chan * Call , args ... interface {}) * Call
16
+ AddMatchSignal (iface , member string , options ... MatchOption ) * Call
17
+ RemoveMatchSignal (iface , member string , options ... MatchOption ) * Call
16
18
GetProperty (p string ) (Variant , error )
17
19
Destination () string
18
20
Path () ObjectPath
@@ -35,23 +37,65 @@ func (o *Object) CallWithContext(ctx context.Context, method string, flags Flags
35
37
return <- o .createCall (ctx , method , flags , make (chan * Call , 1 ), args ... ).Done
36
38
}
37
39
38
- // AddMatchSignal subscribes BusObject to signals from specified interface and
39
- // method (member).
40
- func (o * Object ) AddMatchSignal (iface , member string ) * Call {
41
- return o .Call (
40
+ // MatchOption specifies option for dbus routing match rule. Options can be constructed with WithMatch* helpers.
41
+ // For full list of available options consult
42
+ // https://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing-match-rules
43
+ type MatchOption struct {
44
+ key string
45
+ value string
46
+ }
47
+
48
+ // WithMatchOption creates match option with given key and value
49
+ func WithMatchOption (key , value string ) MatchOption {
50
+ return MatchOption {key , value }
51
+ }
52
+
53
+ // WithMatchObjectPath creates match option that filters events based on given path
54
+ func WithMatchObjectPath (path ObjectPath ) MatchOption {
55
+ return MatchOption {"path" , string (path )}
56
+ }
57
+
58
+ func formatMatchOptions (options []MatchOption ) string {
59
+ items := make ([]string , 0 , len (options ))
60
+ for _ , option := range options {
61
+ items = append (items , option .key + "='" + option .value + "'" )
62
+ }
63
+
64
+ return strings .Join (items , "," )
65
+ }
66
+
67
+ // AddMatchSignal subscribes BusObject to signals from specified interface,
68
+ // method (member). Additional filter rules can be added via WithMatch* option constructors.
69
+ // Note: To filter events by object path you have to specify this path via an option.
70
+ func (o * Object ) AddMatchSignal (iface , member string , options ... MatchOption ) * Call {
71
+ base := []MatchOption {
72
+ {"type" , "signal" },
73
+ {"interface" , iface },
74
+ {"member" , member },
75
+ }
76
+
77
+ options = append (base , options ... )
78
+ return o .conn .BusObject ().Call (
42
79
"org.freedesktop.DBus.AddMatch" ,
43
80
0 ,
44
- "type='signal',interface='" + iface + "',member='" + member + "'" ,
81
+ formatMatchOptions ( options ) ,
45
82
)
46
83
}
47
84
48
- // RemoveMatchSignal unsubscribes BusObject to signals from specified interface and
49
- // method (member).
50
- func (o * Object ) RemoveMatchSignal (iface , member string ) * Call {
51
- return o .Call (
85
+ // RemoveMatchSignal unsubscribes BusObject from signals from specified interface,
86
+ // method (member). Additional filter rules can be added via WithMatch* option constructors
87
+ func (o * Object ) RemoveMatchSignal (iface , member string , options ... MatchOption ) * Call {
88
+ base := []MatchOption {
89
+ {"type" , "signal" },
90
+ {"interface" , iface },
91
+ {"member" , member },
92
+ }
93
+
94
+ options = append (base , options ... )
95
+ return o .conn .BusObject ().Call (
52
96
"org.freedesktop.DBus.RemoveMatch" ,
53
97
0 ,
54
- "type='signal',interface='" + iface + "',member='" + member + "'" ,
98
+ formatMatchOptions ( options ) ,
55
99
)
56
100
}
57
101
0 commit comments