1#[cfg(feature = "data")]
2use crate::data::apply_menu_diffs;
3use crate::data::TrayItemMap;
4use crate::dbus::dbus_menu_proxy::{DBusMenuProxy, PropertiesUpdate};
5use crate::dbus::notifier_item_proxy::StatusNotifierItemProxy;
6use crate::dbus::notifier_watcher_proxy::StatusNotifierWatcherProxy;
7use crate::dbus::status_notifier_watcher::StatusNotifierWatcher;
8use crate::dbus::{self, OwnedValueExt};
9use crate::error::{Error, Result};
10use crate::item::{self, IconPixmap, Status, StatusNotifierItem, Tooltip};
11use crate::menu::{MenuDiff, TrayMenu};
12use crate::names;
13use dbus::DBusProps;
14use futures_lite::StreamExt;
15use std::sync::{Arc, Mutex};
16use std::time::{Duration, SystemTime, UNIX_EPOCH};
17use tokio::spawn;
18use tokio::sync::{broadcast, mpsc};
19use tokio::time::{sleep, timeout, Instant};
20use tracing::{debug, error, trace, warn};
21use zbus::fdo::{DBusProxy, PropertiesProxy};
22use zbus::names::InterfaceName;
23use zbus::zvariant::{Array, Structure, Value};
24use zbus::{Connection, Message};
25
26use self::names::ITEM_OBJECT;
27
28#[derive(Debug, Clone)]
32pub enum Event {
33 Add(String, Box<StatusNotifierItem>),
35 Update(String, UpdateEvent),
39 Remove(String),
41}
42
43#[derive(Debug, Clone)]
45pub enum UpdateEvent {
46 AttentionIcon(Option<String>),
47 Icon {
48 icon_name: Option<String>,
49 icon_pixmap: Option<Vec<IconPixmap>>,
50 },
51 OverlayIcon(Option<String>),
52 Status(Status),
53 Title(Option<String>),
54 Tooltip(Option<Tooltip>),
55 Menu(TrayMenu),
58 MenuDiff(Vec<MenuDiff>),
61 MenuConnect(String),
64}
65
66#[derive(Debug, Clone, Eq, PartialEq)]
69pub enum ActivateRequest {
70 MenuItem {
72 address: String,
73 menu_path: String,
74 submenu_id: i32,
75 },
76 Default { address: String, x: i32, y: i32 },
79 Secondary { address: String, x: i32, y: i32 },
82}
83
84const PROPERTIES_INTERFACE: &str = "org.kde.StatusNotifierItem";
85
86#[derive(Debug)]
88pub struct Client {
89 tx: broadcast::Sender<Event>,
90 _rx: broadcast::Receiver<Event>,
91 connection: Connection,
92
93 #[cfg(feature = "data")]
94 items: TrayItemMap,
95}
96
97impl Client {
98 pub async fn new() -> Result<Self> {
121 let connection = Connection::session().await?;
122 let (tx, rx) = broadcast::channel(32);
123
124 StatusNotifierWatcher::new().attach_to(&connection).await?;
126
127 let watcher_proxy = StatusNotifierWatcherProxy::new(&connection).await?;
129
130 let pid = std::process::id();
133 let mut i = 0;
134 let wellknown = loop {
135 use zbus::fdo::RequestNameReply::{AlreadyOwner, Exists, InQueue, PrimaryOwner};
136
137 i += 1;
138 let wellknown = format!("org.freedesktop.StatusNotifierHost-{pid}-{i}");
139 let wellknown: zbus::names::WellKnownName = wellknown
140 .try_into()
141 .expect("generated well-known name is invalid");
142
143 let flags = [zbus::fdo::RequestNameFlags::DoNotQueue];
144 match connection
145 .request_name_with_flags(&wellknown, flags.into_iter().collect())
146 .await?
147 {
148 PrimaryOwner => break wellknown,
149 Exists | AlreadyOwner => {}
150 InQueue => unreachable!(
151 "request_name_with_flags returned InQueue even though we specified DoNotQueue"
152 ),
153 };
154 };
155
156 debug!("wellknown: {wellknown}");
157 watcher_proxy
158 .register_status_notifier_host(&wellknown)
159 .await?;
160 let items = TrayItemMap::new();
161
162 {
164 let connection = connection.clone();
165 let tx = tx.clone();
166 let items = items.clone();
167
168 let mut stream = watcher_proxy
169 .receive_status_notifier_item_registered()
170 .await?;
171
172 spawn(async move {
173 while let Some(item) = stream.next().await {
174 let address = item.args().map(|args| args.service);
175
176 if let Ok(address) = address {
177 debug!("received new item: {address}");
178 if let Err(err) = Self::handle_item(
179 address,
180 connection.clone(),
181 tx.clone(),
182 items.clone(),
183 )
184 .await
185 {
186 error!("{err}");
187 break;
188 }
189 }
190 }
191
192 Ok::<(), Error>(())
193 });
194 }
195
196 {
200 let connection = connection.clone();
201 let tx = tx.clone();
202 let items = items.clone();
203
204 spawn(async move {
205 let initial_items = watcher_proxy.registered_status_notifier_items().await?;
206 debug!("initial items: {initial_items:?}");
207
208 for item in initial_items {
209 if let Err(err) =
210 Self::handle_item(&item, connection.clone(), tx.clone(), items.clone())
211 .await
212 {
213 error!("{err}");
214 }
215 }
216
217 Ok::<(), Error>(())
218 });
219 }
220
221 {
224 let tx = tx.clone();
225 let items = items.clone();
226
227 let dbus_proxy = DBusProxy::new(&connection).await?;
228
229 let mut stream = dbus_proxy.receive_name_acquired().await?;
230
231 spawn(async move {
232 while let Some(thing) = stream.next().await {
233 let body = thing.args()?;
234 if body.name == names::WATCHER_BUS {
235 for dest in items.clear_items() {
236 tx.send(Event::Remove(dest))?;
237 }
238 }
239 }
240
241 Ok::<(), Error>(())
242 });
243 }
244
245 debug!("tray client initialized");
246
247 Ok(Self {
248 connection,
249 tx,
250 _rx: rx,
251 #[cfg(feature = "data")]
252 items,
253 })
254 }
255
256 async fn handle_item(
259 address: &str,
260 connection: Connection,
261 tx: broadcast::Sender<Event>,
262 items: TrayItemMap,
263 ) -> Result<()> {
264 let (destination, path) = parse_address(address);
265
266 let properties_proxy = PropertiesProxy::builder(&connection)
267 .destination(destination.to_string())?
268 .path(path.clone())?
269 .build()
270 .await?;
271
272 let properties = Self::get_item_properties(destination, &path, &properties_proxy).await?;
273
274 items.new_item(destination.into(), &properties);
275
276 tx.send(Event::Add(
277 destination.to_string(),
278 properties.clone().into(),
279 ))?;
280
281 {
282 let connection = connection.clone();
283 let destination = destination.to_string();
284 let items = items.clone();
285 let tx = tx.clone();
286
287 spawn(async move {
288 Self::watch_item_properties(
289 &destination,
290 &path,
291 &connection,
292 properties_proxy,
293 tx,
294 items,
295 )
296 .await?;
297
298 debug!("Stopped watching {destination}{path}");
299 Ok::<(), Error>(())
300 });
301 }
302
303 if let Some(menu) = properties.menu {
304 let destination = destination.to_string();
305
306 tx.send(Event::Update(
307 destination.clone(),
308 UpdateEvent::MenuConnect(menu.clone()),
309 ))?;
310
311 spawn(async move {
312 Self::watch_menu(destination, &menu, &connection, tx, items).await?;
313 Ok::<(), Error>(())
314 });
315 }
316
317 Ok(())
318 }
319
320 async fn get_item_properties(
322 destination: &str,
323 path: &str,
324 properties_proxy: &PropertiesProxy<'_>,
325 ) -> Result<StatusNotifierItem> {
326 let properties = properties_proxy
327 .get_all(
328 InterfaceName::from_static_str(PROPERTIES_INTERFACE)
329 .expect("to be valid interface name"),
330 )
331 .await;
332
333 let properties = match properties {
334 Ok(properties) => properties,
335 Err(err) => {
336 error!("Error fetching properties from {destination}{path}: {err:?}");
337 return Err(err.into());
338 }
339 };
340
341 StatusNotifierItem::try_from(DBusProps(properties))
342 }
343
344 async fn watch_item_properties(
347 destination: &str,
348 path: &str,
349 connection: &Connection,
350 properties_proxy: PropertiesProxy<'_>,
351 tx: broadcast::Sender<Event>,
352 items: TrayItemMap,
353 ) -> Result<()> {
354 let notifier_item_proxy = StatusNotifierItemProxy::builder(connection)
355 .destination(destination)?
356 .path(path)?
357 .build()
358 .await?;
359
360 let dbus_proxy = DBusProxy::new(connection).await?;
361
362 let mut disconnect_stream = dbus_proxy.receive_name_owner_changed().await?;
363 let mut props_changed = notifier_item_proxy.inner().receive_all_signals().await?;
364
365 loop {
366 tokio::select! {
367 Some(change) = props_changed.next() => {
368 match Self::get_update_event(change, &properties_proxy).await {
369 Ok(Some(event)) => {
370 cfg_if::cfg_if! {
371 if #[cfg(feature = "data")] {
372 items.apply_update_event(destination, &event);
373 }
374 }
375 debug!("[{destination}{path}] received property change: {event:?}");
376 tx.send(Event::Update(destination.to_string(), event))?;
377 }
378 Err(e) => {
379 error!("Error parsing update properties from {destination}{path}: {e:?}");
380 }
381 _ => {}
382 }
383 }
384 Some(signal) = disconnect_stream.next() => {
385 let args = signal.args()?;
386 let old = args.old_owner();
387 let new = args.new_owner();
388
389 if let (Some(old), None) = (old.as_ref(), new.as_ref()) {
390 if old == destination {
391 debug!("[{destination}{path}] disconnected");
392
393 let watcher_proxy = StatusNotifierWatcherProxy::new(connection)
394 .await
395 .expect("Failed to open StatusNotifierWatcherProxy");
396
397 if let Err(error) = watcher_proxy.unregister_status_notifier_item(old).await {
398 error!("{error:?}");
399 }
400
401
402 items.remove_item(destination);
403
404 tx.send(Event::Remove(destination.to_string()))?;
405 break Ok(());
406 }
407 }
408 }
409 }
410 }
411 }
412
413 async fn get_update_event(
415 change: Message,
416 properties_proxy: &PropertiesProxy<'_>,
417 ) -> Result<Option<UpdateEvent>> {
418 use UpdateEvent::{AttentionIcon, Icon, OverlayIcon, Status, Title, Tooltip};
419
420 let header = change.header();
421 let member = header
422 .member()
423 .ok_or(Error::InvalidData("Update message header missing `member`"))?;
424
425 macro_rules! get_property {
426 ($name:expr) => {
427 match properties_proxy
428 .get(
429 InterfaceName::from_static_str(PROPERTIES_INTERFACE)
430 .expect("to be valid interface name"),
431 $name,
432 )
433 .await
434 {
435 Ok(v) => Ok(Some(v)),
436 Err(e) => match e {
437 zbus::fdo::Error::InvalidArgs(_) => {
439 warn!("{e}");
440 Ok(None)
441 }
442 _ => Err(Into::<Error>::into(e)),
443 },
444 }
445 };
446 }
447
448 let property = match member.as_str() {
449 "NewAttentionIcon" => Some(AttentionIcon(
450 get_property!("AttentionIconName")?
451 .as_ref()
452 .map(OwnedValueExt::to_string)
453 .transpose()?,
454 )),
455 "NewIcon" => {
456 let icon_name = match get_property!("IconName") {
457 Ok(name) => name,
458 Err(e) => {
459 warn!("Error getting IconName: {e:?}");
460 None
461 }
462 }
463 .as_ref()
464 .map(OwnedValueExt::to_string)
465 .transpose()
466 .ok()
467 .flatten();
468
469 let icon_pixmap = match get_property!("IconPixmap") {
470 Ok(pixmap) => pixmap,
471 Err(e) => {
472 warn!("Error getting IconPixmap: {e:?}");
473 None
474 }
475 }
476 .as_deref()
477 .map(Value::downcast_ref::<&Array>)
478 .transpose()?
479 .map(IconPixmap::from_array)
480 .transpose()?;
481
482 Some(Icon {
483 icon_name,
484 icon_pixmap,
485 })
486 }
487 "NewOverlayIcon" => Some(OverlayIcon(
488 get_property!("OverlayIconName")?
489 .as_ref()
490 .map(OwnedValueExt::to_string)
491 .transpose()?,
492 )),
493 "NewStatus" => Some(Status(
494 get_property!("Status")?
495 .as_deref()
496 .map(Value::downcast_ref::<&str>)
497 .transpose()?
498 .map(item::Status::from)
499 .unwrap_or_default(), )),
501 "NewTitle" => Some(Title(
502 get_property!("Title")?
503 .as_ref()
504 .map(OwnedValueExt::to_string)
505 .transpose()?,
506 )),
507 "NewToolTip" => Some(Tooltip(
508 get_property!("ToolTip")?
509 .as_deref()
510 .map(Value::downcast_ref::<&Structure>)
511 .transpose()?
512 .map(crate::item::Tooltip::try_from)
513 .transpose()?,
514 )),
515 _ => {
516 warn!("received unhandled update event: {member}");
517 None
518 }
519 };
520
521 debug!("received tray item update: {member} -> {property:?}");
522
523 Ok(property)
524 }
525
526 async fn watch_menu(
532 destination: String,
533 menu_path: &str,
534 connection: &Connection,
535 tx: broadcast::Sender<Event>,
536 items: TrayItemMap,
537 ) -> Result<()> {
538 const LAYOUT_UPDATE_INTERVAL_MS: Duration = Duration::from_millis(50);
539
540 let dbus_menu_proxy = DBusMenuProxy::builder(connection)
541 .destination(destination.as_str())?
542 .path(menu_path)?
543 .build()
544 .await?;
545
546 debug!("[{destination}{menu_path}] getting initial menu");
547 let menu = dbus_menu_proxy.get_layout(0, -1, &[]).await?;
548 let menu = TrayMenu::try_from(menu)?;
549
550 items.update_menu(&destination, &menu);
551
552 tx.send(Event::Update(destination.clone(), UpdateEvent::Menu(menu)))?;
553
554 let mut layout_updated = dbus_menu_proxy.receive_layout_updated().await?;
555 let mut properties_updated = dbus_menu_proxy.receive_items_properties_updated().await?;
556
557 let last_layout_update = Arc::new(Mutex::new(Instant::now()));
558 let (layout_tx, mut layout_rx) = mpsc::channel(4);
559
560 loop {
561 tokio::select!(
562 Some(ev) = layout_updated.next() => {
563 trace!("received layout update");
564
565 let now = Instant::now();
566 *last_layout_update.lock().expect("should get lock") = now;
567
568 let args = ev.args()?;
569
570 let last_layout_update = last_layout_update.clone();
571 let layout_tx = layout_tx.clone();
572 spawn(async move {
573 sleep(LAYOUT_UPDATE_INTERVAL_MS).await;
574 if *last_layout_update.lock().expect("should get lock") == now {
575 trace!("dispatching layout update");
576 layout_tx.send(args.parent).await.expect("should send");
577 }
578 });
579 }
580 Some(layout_parent) = layout_rx.recv() => {
581 debug!("[{destination}{menu_path}] layout update");
582
583 let get_layout = dbus_menu_proxy.get_layout(layout_parent, -1, &[]);
584
585 let menu = match timeout(Duration::from_secs(1), get_layout).await {
586 Ok(Ok(menu)) => {
587 debug!("got new menu layout");
588 menu
589 }
590 Ok(Err(err)) => {
591 error!("error fetching layout: {err:?}");
592 break;
593 }
594 Err(_) => {
595 error!("Timeout getting layout");
596 break;
597 }
598 };
599
600 let menu = TrayMenu::try_from(menu)?;
601
602 items.update_menu(&destination, &menu);
603
604 debug!("sending new menu for '{destination}'");
605 trace!("new menu for '{destination}': {menu:?}");
606 tx.send(Event::Update(
607 destination.clone(),
608 UpdateEvent::Menu(menu),
609 ))?;
610 }
611 Some(change) = properties_updated.next() => {
612 let body = change.message().body();
613 let update: PropertiesUpdate= body.deserialize::<PropertiesUpdate>()?;
614 let diffs = Vec::try_from(update)?;
615
616 #[cfg(feature = "data")]
617 if let Some((_, Some(menu))) = items
618 .get_map()
619 .lock()
620 .expect("mutex lock should succeed")
621 .get_mut(&destination)
622 {
623 apply_menu_diffs(menu, &diffs);
624 } else {
625 error!("could not find item in state");
626 }
627
628 tx.send(Event::Update(
629 destination.clone(),
630 UpdateEvent::MenuDiff(diffs),
631 ))?;
632
633 }
635 );
636 }
637
638 Ok(())
639 }
640
641 async fn get_notifier_item_proxy(
642 &self,
643 address: String,
644 ) -> Result<StatusNotifierItemProxy<'_>> {
645 let proxy = StatusNotifierItemProxy::builder(&self.connection)
646 .destination(address)?
647 .path(ITEM_OBJECT)?
648 .build()
649 .await?;
650 Ok(proxy)
651 }
652
653 async fn get_menu_proxy(
654 &self,
655 address: String,
656 menu_path: String,
657 ) -> Result<DBusMenuProxy<'_>> {
658 let proxy = DBusMenuProxy::builder(&self.connection)
659 .destination(address)?
660 .path(menu_path)?
661 .build()
662 .await?;
663
664 Ok(proxy)
665 }
666
667 #[must_use]
672 pub fn subscribe(&self) -> broadcast::Receiver<Event> {
673 self.tx.subscribe()
674 }
675
676 #[cfg(feature = "data")]
678 #[must_use]
679 pub fn items(&self) -> std::sync::Arc<std::sync::Mutex<crate::data::BaseMap>> {
680 self.items.get_map()
681 }
682
683 pub async fn about_to_show_menuitem(
692 &self,
693 address: String,
694 menu_path: String,
695 id: i32,
696 ) -> Result<bool> {
697 let proxy = self.get_menu_proxy(address, menu_path).await?;
698 Ok(proxy.about_to_show(id).await?)
699 }
700
701 pub async fn activate(&self, req: ActivateRequest) -> Result<()> {
712 macro_rules! timeout_event {
713 ($event:expr) => {
714 if timeout(Duration::from_secs(1), $event).await.is_err() {
715 error!("Timed out sending activate event");
716 }
717 };
718 }
719 match req {
720 ActivateRequest::MenuItem {
721 address,
722 menu_path,
723 submenu_id,
724 } => {
725 let proxy = self.get_menu_proxy(address, menu_path).await?;
726 let timestamp = SystemTime::now()
727 .duration_since(UNIX_EPOCH)
728 .expect("time should flow forwards");
729
730 let event = proxy.event(
731 submenu_id,
732 "clicked",
733 &Value::I32(0),
734 timestamp.as_secs() as u32,
735 );
736
737 timeout_event!(event);
738 }
739 ActivateRequest::Default { address, x, y } => {
740 let proxy = self.get_notifier_item_proxy(address).await?;
741 let event = proxy.activate(x, y);
742
743 timeout_event!(event);
744 }
745 ActivateRequest::Secondary { address, x, y } => {
746 let proxy = self.get_notifier_item_proxy(address).await?;
747 let event = proxy.secondary_activate(x, y);
748
749 timeout_event!(event);
750 }
751 }
752
753 Ok(())
754 }
755}
756
757fn parse_address(address: &str) -> (&str, String) {
758 address
759 .split_once('/')
760 .map_or((address, String::from("/StatusNotifierItem")), |(d, p)| {
761 (d, format!("/{p}"))
762 })
763}
764
765#[cfg(test)]
766mod tests {
767 use super::*;
768
769 #[test]
770 fn parse_unnamed() {
771 let address = ":1.58/StatusNotifierItem";
772 let (destination, path) = parse_address(address);
773
774 assert_eq!(":1.58", destination);
775 assert_eq!("/StatusNotifierItem", path);
776 }
777
778 #[test]
779 fn parse_named() {
780 let address = ":1.72/org/ayatana/NotificationItem/dropbox_client_1398";
781 let (destination, path) = parse_address(address);
782
783 assert_eq!(":1.72", destination);
784 assert_eq!("/org/ayatana/NotificationItem/dropbox_client_1398", path);
785 }
786}