rustsni 0.2.2

A poll-friendly StatusNotifierItem host library built on rustbus
Documentation
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! StatusNotifierWatcher implementation.
//!
//! This module implements the `org.kde.StatusNotifierWatcher` D-Bus interface.
//! The watcher is the central registry on the session bus:
//!
//! - Tray items call `RegisterStatusNotifierItem` to announce themselves.
//! - The host calls `RegisterStatusNotifierHost` to announce itself.
//! - The watcher emits `StatusNotifierItemRegistered` /
//!   `StatusNotifierItemUnregistered` signals so the host knows when to
//!   update its tray representation.
//!
//! The watcher emits `StatusNotifierHostRegistered` at startup so that
//! already-running items may re-register via `RegisterStatusNotifierItem`.
//!
//! Additionally it subscribes to:
//! - `NameOwnerChanged` to detect items vanishing from the bus.
//! - `com.canonical.dbusmenu` signals for menu layout updates.

use std::collections::HashMap;

use rustbus::connection::Timeout;
use rustbus::connection::ll_conn::DuplexConn;
use rustbus::message_builder::MarshalledMessage;
use rustbus::standard_messages;

use crate::item::{ItemId, TrayItem};
use crate::{Result, TrayEvent};

pub(crate) const WATCHER_INTERFACE: &str = "org.kde.StatusNotifierWatcher";
pub(crate) const WATCHER_PATH: &str = "/StatusNotifierWatcher";

const WATCHER_INTROSPECT_XML: &str = include_str!("../protocols/org.kde.StatusNotifierWatcher.xml");

/// Request the watcher bus names and set up signal subscriptions.
pub fn register(conn: &mut DuplexConn) -> Result<()> {
    // Request both the freedesktop and KDE well-known names
    for name in [
        "org.freedesktop.StatusNotifierWatcher",
        "org.kde.StatusNotifierWatcher",
    ] {
        let msg = standard_messages::request_name(name, 0);
        let serial = conn.send.send_message_write_all(&msg)?;
        // Wait for the reply matching our serial; skip unrelated signals.
        loop {
            let resp = conn.recv.get_next_message(Timeout::Infinite)?;
            if resp.dynheader.response_serial == Some(serial) {
                match resp.typ {
                    rustbus::message_builder::MessageType::Reply => {
                        // Check the reply body: 1 = PRIMARY_OWNER, 4 = ALREADY_OWNER.
                        // 2 = IN_QUEUE and 3 = EXISTS mean another watcher is active.
                        let result: u32 = resp.body.parser().get().unwrap_or(0);
                        if result != 1 && result != 4 {
                            return Err(crate::Error::WatcherAlreadyRunning);
                        }
                        break;
                    }
                    _ => return Err(crate::Error::WatcherAlreadyRunning),
                }
            }
            if !matches!(resp.typ, rustbus::message_builder::MessageType::Signal) {
                break; // unexpected — abort spin
            }
        }
    }

    // Subscribe to NameOwnerChanged so we know when items vanish
    let add_match = standard_messages::add_match(
        "type='signal',interface='org.freedesktop.DBus',member='NameOwnerChanged'",
    );
    conn.send.send_message_write_all(&add_match)?;

    // Subscribe to dbusmenu signals (LayoutUpdated, ItemsPropertiesUpdated)
    let add_match =
        standard_messages::add_match("type='signal',interface='com.canonical.dbusmenu'");
    conn.send.send_message_write_all(&add_match)?;

    Ok(())
}

/// Handle an incoming method call.
pub fn handle_call(
    conn: &mut DuplexConn,
    msg: &MarshalledMessage,
    items: &mut HashMap<ItemId, TrayItem>,
    events: &mut Vec<TrayEvent>,
    pending: &mut std::collections::VecDeque<rustbus::message_builder::MarshalledMessage>,
) -> Result<()> {
    let iface = msg.dynheader.interface.as_deref().unwrap_or("");
    let member = msg.dynheader.member.as_deref().unwrap_or("");
    let object = msg.dynheader.object.as_deref().unwrap_or("");

    // Handle Introspectable on any object path (D-Bus spec requirement).
    // The dbus-daemon and bus clients often introspect "/" to discover
    // available object trees.
    if iface == "org.freedesktop.DBus.Introspectable" && member == "Introspect" {
        let xml = if object == WATCHER_PATH {
            WATCHER_INTROSPECT_XML
        } else {
            r#"<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
 "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
  <node name="StatusNotifierWatcher"/>
</node>"#
        };
        let mut reply = msg.dynheader.make_response();
        reply.body.push_param(xml).unwrap();
        conn.send.send_message_write_all(&reply)?;
        return Ok(());
    }

    if object != WATCHER_PATH {
        let err = msg.dynheader.make_error_response(
            "org.freedesktop.DBus.Error.UnknownObject".to_owned(),
            Some(format!(
                "This service does not implement the object path: {object}"
            )),
        );
        conn.send.send_message_write_all(&err)?;
        return Ok(());
    }

    match iface {
        WATCHER_INTERFACE => handle_watcher_call(conn, msg, member, items, events, pending),
        "org.freedesktop.DBus.Properties" => handle_properties_call(conn, msg, member, items),
        _ => {
            let err = standard_messages::unknown_method(&msg.dynheader);
            conn.send.send_message_write_all(&err)?;
            Ok(())
        }
    }
}

fn handle_watcher_call(
    conn: &mut DuplexConn,
    msg: &MarshalledMessage,
    member: &str,
    items: &mut HashMap<ItemId, TrayItem>,
    events: &mut Vec<TrayEvent>,
    pending: &mut std::collections::VecDeque<rustbus::message_builder::MarshalledMessage>,
) -> Result<()> {
    match member {
        "RegisterStatusNotifierItem" => {
            let service: String = msg.body.parser().get()?;
            let sender = msg.dynheader.sender.as_deref().unwrap_or("");
            let (bus_name, object_path) = parse_service(sender, &service);

            // Build service_id: sender+path if only a path was given, else the raw service
            let service_id = if service.starts_with('/') {
                format!("{sender}{service}")
            } else {
                service.clone()
            };

            // Read item properties from the bus
            match TrayItem::from_bus_get_all(conn, &service_id, &bus_name, &object_path, pending) {
                Ok(item) => {
                    // Deduplicate: if this bus_name is already known (e.g. from step 3
                    // unique-name probe), skip the insert but still emit ItemChanged.
                    let is_new = !items.contains_key(&ItemId(service_id.clone()))
                        && !items.values().any(|existing| existing.bus_name == bus_name);
                    if is_new {
                        let id = item.id.clone();
                        items.insert(id.clone(), item);
                        events.push(TrayEvent::ItemAdded(id));
                    }

                    // Emit StatusNotifierItemRegistered signal
                    let mut sig = rustbus::MessageBuilder::new()
                        .signal(
                            WATCHER_INTERFACE,
                            "StatusNotifierItemRegistered",
                            WATCHER_PATH,
                        )
                        .build();
                    sig.body.push_param(&service_id as &str).unwrap();
                    conn.send.send_message_write_all(&sig)?;
                }
                Err(_e) => {}
            }

            // Send empty reply
            let reply = msg.dynheader.make_response();
            conn.send.send_message_write_all(&reply)?;
        }
        "RegisterStatusNotifierHost" => {
            let reply = msg.dynheader.make_response();
            conn.send.send_message_write_all(&reply)?;
        }
        "GetHostServiceName" => {
            let mut reply = msg.dynheader.make_response();
            let host_name = format!("org.freedesktop.StatusNotifierHost-{}", std::process::id());
            reply.body.push_param(host_name.as_str()).unwrap();
            conn.send.send_message_write_all(&reply)?;
        }
        _ => {
            let err = standard_messages::unknown_method(&msg.dynheader);
            conn.send.send_message_write_all(&err)?;
        }
    }
    Ok(())
}

fn handle_properties_call(
    conn: &mut DuplexConn,
    msg: &MarshalledMessage,
    member: &str,
    items: &mut HashMap<ItemId, TrayItem>,
) -> Result<()> {
    match member {
        "Get" => {
            let mut parser = msg.body.parser();
            let iface: &str = parser.get()?;
            let prop: &str = parser.get()?;

            // Only handle properties for our watcher interface
            if iface != WATCHER_INTERFACE {
                let err = msg.dynheader.make_error_response(
                    "org.freedesktop.DBus.Error.UnknownProperty".to_owned(),
                    Some(format!("Unknown interface: {iface}")),
                );
                conn.send.send_message_write_all(&err)?;
                return Ok(());
            }

            let mut reply = msg.dynheader.make_response();
            match prop {
                "RegisteredStatusNotifierItems" => {
                    let names: Vec<&str> = items.keys().map(|id| id.0.as_str()).collect();
                    reply.body.push_variant(names).unwrap();
                }
                "IsStatusNotifierHostRegistered" => {
                    reply.body.push_variant(true).unwrap();
                }
                "ProtocolVersion" => {
                    reply.body.push_variant(0i32).unwrap();
                }
                _ => {
                    let err = msg.dynheader.make_error_response(
                        "org.freedesktop.DBus.Error.UnknownProperty".to_owned(),
                        Some(format!("Unknown property: {prop}")),
                    );
                    conn.send.send_message_write_all(&err)?;
                    return Ok(());
                }
            }
            conn.send.send_message_write_all(&reply)?;
        }
        "GetAll" => {
            let mut reply = msg.dynheader.make_response();
            let names: Vec<&str> = items.keys().map(|id| id.0.as_str()).collect();
            // Build a dict of all watcher properties: a{sv}
            use rustbus::params::{Base as PBase, Container, Dict, DictMap, Param};
            use rustbus::signature::{Base as SBase, Container as SContainer, Type};
            let mut dict = DictMap::new();
            dict.insert(
                PBase::StringRef("RegisteredStatusNotifierItems"),
                Param::Container(Container::Variant(Box::new(rustbus::params::Variant {
                    sig: Type::Container(SContainer::Array(Box::new(Type::Base(SBase::String)))),
                    value: Param::Container(Container::Array(rustbus::params::Array {
                        element_sig: Type::Base(SBase::String),
                        values: names
                            .into_iter()
                            .map(|n| Param::Base(PBase::StringRef(n)))
                            .collect(),
                    })),
                }))),
            );
            dict.insert(
                PBase::StringRef("IsStatusNotifierHostRegistered"),
                Param::Container(Container::Variant(Box::new(rustbus::params::Variant {
                    sig: Type::Base(SBase::Boolean),
                    value: Param::Base(PBase::Boolean(true)),
                }))),
            );
            dict.insert(
                PBase::StringRef("ProtocolVersion"),
                Param::Container(Container::Variant(Box::new(rustbus::params::Variant {
                    sig: Type::Base(SBase::Int32),
                    value: Param::Base(PBase::Int32(0)),
                }))),
            );
            reply
                .body
                .push_old_param(&Param::Container(Container::Dict(Dict {
                    key_sig: SBase::String,
                    value_sig: Type::Container(SContainer::Variant),
                    map: dict,
                })))
                .unwrap();
            conn.send.send_message_write_all(&reply)?;
        }
        _ => {
            let err = standard_messages::unknown_method(&msg.dynheader);
            conn.send.send_message_write_all(&err)?;
        }
    }
    Ok(())
}

/// Handle incoming signals (NameOwnerChanged, item property-change signals).
pub fn handle_signal(
    conn: &mut DuplexConn,
    msg: &MarshalledMessage,
    items: &mut HashMap<ItemId, TrayItem>,
    events: &mut Vec<TrayEvent>,
    pending: &mut std::collections::VecDeque<rustbus::message_builder::MarshalledMessage>,
) -> Result<()> {
    let iface = msg.dynheader.interface.as_deref().unwrap_or("");
    let member = msg.dynheader.member.as_deref().unwrap_or("");
    if iface == "org.freedesktop.DBus" && member == "NameOwnerChanged" {
        let mut parser = msg.body.parser();
        let name: String = parser.get()?;
        let _old_owner: String = parser.get()?;
        let new_owner: String = parser.get()?;

        // ── Name disappeared from the bus ───────────────────────
        if new_owner.is_empty() {
            let ids_to_remove: Vec<ItemId> = items
                .values()
                .filter(|item| item.bus_name == name)
                .map(|item| item.id.clone())
                .collect();
            for id in ids_to_remove {
                let service_id = id.0.clone();
                items.remove(&id);
                events.push(TrayEvent::ItemRemoved(id));

                // Emit StatusNotifierItemUnregistered signal
                let mut sig = rustbus::MessageBuilder::new()
                    .signal(
                        WATCHER_INTERFACE,
                        "StatusNotifierItemUnregistered",
                        WATCHER_PATH,
                    )
                    .build();
                sig.body.push_param(service_id.as_str()).unwrap();
                conn.send.send_message_write_all(&sig)?;
            }
        }
    } else if iface == "org.kde.StatusNotifierItem" {
        let sender = msg.dynheader.sender.as_deref().unwrap_or("");
        if !sender.is_empty() {
            // Find the existing item to preserve its service_id and object_path
            let existing = items.values().find(|item| item.bus_name == sender);
            if let Some(old) = existing {
                let service_id = old.id.0.clone();
                let object_path = old.object_path.clone();
                match TrayItem::from_bus_get_all(conn, &service_id, sender, &object_path, pending) {
                    Ok(item) => {
                        let id = item.id.clone();
                        items.insert(id.clone(), item);
                        events.push(TrayEvent::ItemChanged(id));
                    }
                    Err(_e) => {}
                }
            }
        }
    } else if iface == "com.canonical.dbusmenu"
        && (member == "LayoutUpdated" || member == "ItemsPropertiesUpdated")
    {
        // Match signal sender to an item's bus_name to emit MenuChanged
        let sender = msg.dynheader.sender.as_deref().unwrap_or("");
        if !sender.is_empty() {
            let ids: Vec<ItemId> = items
                .values()
                .filter(|item| item.bus_name == sender)
                .map(|item| item.id.clone())
                .collect();
            for id in ids {
                events.push(TrayEvent::MenuChanged(id));
            }
        }
    } else if iface == "com.canonical.dbusmenu" && member == "ItemActivationRequested" {
        let sender = msg.dynheader.sender.as_deref().unwrap_or("");
        if !sender.is_empty() {
            let ids: Vec<ItemId> = items
                .values()
                .filter(|item| item.bus_name == sender)
                .map(|item| item.id.clone())
                .collect();
            for id in ids {
                events.push(TrayEvent::MenuActivationRequested(id));
            }
        }
    }

    Ok(())
}

/// Split a raw SNI service registration string into `(bus_name, object_path)`.
///
/// The SNI spec allows three forms:
/// - `"com.example.App"` → bus_name, default path `/StatusNotifierItem`
/// - `"/StatusNotifierItem"` → sender's bus name, given path
/// - `"com.example.App/SomePath"` → bus_name, given path
fn parse_service(sender: &str, service: &str) -> (String, String) {
    if service.starts_with('/') {
        (sender.to_owned(), service.to_owned())
    } else if let Some(slash) = service.find('/') {
        (service[..slash].to_owned(), service[slash..].to_owned())
    } else {
        (
            normalize_item_name(service),
            "/StatusNotifierItem".to_owned(),
        )
    }
}

/// Normalize the service name passed to RegisterStatusNotifierItem.
///
/// Accepts:
/// - Well-known names with suffix: `org.freedesktop.StatusNotifierItem-PID-ID`
/// - KDE variant: `org.kde.StatusNotifierItem-PID-ID`
/// - Unique bus names: `:1.42`
/// - Bare PID-ID: `4077-1`
fn normalize_item_name(name: &str) -> String {
    if name.starts_with("org.freedesktop.StatusNotifierItem")
        || name.starts_with("org.kde.StatusNotifierItem")
        || name.starts_with(':')
    {
        return name.to_owned();
    }
    format!("org.freedesktop.StatusNotifierItem-{name}")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn normalize_full_name() {
        let input = "org.freedesktop.StatusNotifierItem-4077-1";
        assert_eq!(normalize_item_name(input), input);
    }

    #[test]
    fn normalize_unique_name() {
        assert_eq!(normalize_item_name(":1.42"), ":1.42");
    }

    #[test]
    fn normalize_pid_id() {
        assert_eq!(
            normalize_item_name("4077-1"),
            "org.freedesktop.StatusNotifierItem-4077-1"
        );
    }

    #[test]
    fn parse_service_uses_sender_for_path_only() {
        let (bus, path) = parse_service(":1.42", "/StatusNotifierItem");
        assert_eq!(bus, ":1.42");
        assert_eq!(path, "/StatusNotifierItem");
    }

    #[test]
    fn parse_service_bus_name_and_path() {
        let (bus, path) = parse_service(":1.42", "com.example.App/SomePath");
        assert_eq!(bus, "com.example.App");
        assert_eq!(path, "/SomePath");
    }

    #[test]
    fn parse_service_just_bus_name() {
        let (bus, path) = parse_service(":1.42", "org.freedesktop.StatusNotifierItem-4077-1");
        assert_eq!(bus, "org.freedesktop.StatusNotifierItem-4077-1");
        assert_eq!(path, "/StatusNotifierItem");
    }

    #[test]
    fn service_id_preserved_on_path_only_registration() {
        // When a path-only registration comes from sender :1.42,
        // the service_id should be sender + path
        let sender = ":1.42";
        let service = "/StatusNotifierItem";
        let service_id = if service.starts_with('/') {
            format!("{sender}{service}")
        } else {
            service.to_owned()
        };
        assert_eq!(service_id, ":1.42/StatusNotifierItem");
    }

    #[test]
    fn normalize_freedesktop_exact() {
        assert_eq!(
            normalize_item_name("org.freedesktop.StatusNotifierItem"),
            "org.freedesktop.StatusNotifierItem"
        );
    }

    #[test]
    fn normalize_kde_exact() {
        assert_eq!(
            normalize_item_name("org.kde.StatusNotifierItem"),
            "org.kde.StatusNotifierItem"
        );
    }
}