BlueGit

ict rusty-teams

feat(linux): notification sounds and profile via User.ReadBasic.All

- play notification and ring MP3s on Linux with rodio (from PR #23)
- me() and the call profile read with User.ReadBasic.All, served by the Teams tab

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-11-13 · 2 weeks ago

Hash
5c802888409705e5980e78467f4dc40b182848eb
Parent
2d4cc591
6 files changed +126 −2
crates/app-base/Cargo.toml +1 −0
@@ -34,6 +34,7 @@ rust-i18n = "4.2"
34 34 resvg = { version = "0.46", default-features = false, features = ["text"] }
35 35
36 36 [target.'cfg(target_os = "linux")'.dependencies]
37 rodio = { version = "0.22", default-features = false, features = ["playback", "mp3"] }
37 38 x11rb = { version = "0.13", features = ["shape"] }
38 39
39 40 [target.'cfg(windows)'.dependencies]
@@ -34,6 +34,7 @@ rust-i18n = "4.2"
34resvg = { version = "0.46", default-features = false, features = ["text"] }
34resvg = { version = "0.46", default-features = false, features = ["text"] }
35
35
36[target.'cfg(target_os = "linux")'.dependencies]
36[target.'cfg(target_os = "linux")'.dependencies]
37rodio = { version = "0.22", default-features = false, features = ["playback", "mp3"] }
37x11rb = { version = "0.13", features = ["shape"] }
38x11rb = { version = "0.13", features = ["shape"] }
38
39
39[target.'cfg(windows)'.dependencies]
40[target.'cfg(windows)'.dependencies]
crates/app-base/src/notify/platform/linux_sound.rs added +110 −0
@@ -0,0 +1,110 @@
1 //! Linux notification and ringtone playback through the system audio output.
2 use std::io::Cursor;
3 use std::sync::atomic::{AtomicBool, Ordering};
4 use std::sync::{Arc, Mutex};
5 use std::time::Duration;
6
7 use rodio::cpal::traits::{DeviceTrait, HostTrait};
8
9 const SOUND_BYTES: &[u8] = include_bytes!("../../../../app/assets/sounds/notification.mp3");
10 const POLL: Duration = Duration::from_millis(20);
11 static SOUND_OUTPUT: Mutex<Option<String>> = Mutex::new(None);
12
13 pub fn set_sound_output(device_key: Option<String>) {
14 *SOUND_OUTPUT.lock().unwrap_or_else(|p| p.into_inner()) = device_key;
15 }
16
17 fn open_sink() -> Result<rodio::MixerDeviceSink, String> {
18 let chosen = SOUND_OUTPUT
19 .lock()
20 .unwrap_or_else(|p| p.into_inner())
21 .clone();
22 let device = chosen.and_then(|key| {
23 rodio::cpal::default_host()
24 .output_devices()
25 .ok()?
26 .find(|device| {
27 device.id().is_ok_and(|id| id.1 == key)
28 || device
29 .description()
30 .is_ok_and(|description| description.name() == key)
31 })
32 });
33 if let Some(device) = device {
34 if let Ok(sink) =
35 rodio::DeviceSinkBuilder::from_device(device).and_then(|builder| builder.open_stream())
36 {
37 return Ok(sink);
38 }
39 }
40 rodio::DeviceSinkBuilder::open_default_sink()
41 .map_err(|e| format!("cannot open audio output: {e}"))
42 }
43
44 fn play(bytes: Vec<u8>, looped: bool, stop: Arc<AtomicBool>) -> Result<(), String> {
45 if stop.load(Ordering::SeqCst) {
46 return Ok(());
47 }
48 let mut sink = open_sink()?;
49 sink.log_on_drop(false);
50 loop {
51 if stop.load(Ordering::SeqCst) {
52 return Ok(());
53 }
54 let player = rodio::play(sink.mixer(), Cursor::new(bytes.clone()))
55 .map_err(|e| format!("cannot decode notification sound: {e}"))?;
56 while !player.empty() {
57 if stop.load(Ordering::SeqCst) {
58 player.stop();
59 return Ok(());
60 }
61 std::thread::sleep(POLL);
62 }
63 if !looped {
64 return Ok(());
65 }
66 }
67 }
68
69 pub fn play_mp3_until(bytes: Vec<u8>, looped: bool, stop: Arc<AtomicBool>) {
70 std::thread::spawn(move || {
71 if let Err(error) = play(bytes, looped, stop) {
72 crate::diagnostics::record(format!("sound: {error}"));
73 }
74 });
75 }
76
77 pub fn play_mp3(bytes: Vec<u8>) {
78 play_mp3_until(bytes, false, Arc::new(AtomicBool::new(false)));
79 }
80
81 pub fn play_sound() {
82 play_mp3(SOUND_BYTES.to_vec());
83 }
84
85 #[cfg(test)]
86 mod tests {
87 use super::*;
88
89 #[test]
90 fn bundled_notification_decodes_to_audible_samples() {
91 let decoded = rodio::Decoder::try_from(Cursor::new(SOUND_BYTES)).unwrap();
92 assert!(decoded.take(48000).any(|sample| sample.abs() > 0.001));
93 }
94
95 #[test]
96 fn cancelled_playback_does_not_open_a_device_or_decode_audio() {
97 assert!(play(Vec::new(), true, Arc::new(AtomicBool::new(true))).is_ok());
98 }
99
100 #[test]
101 #[ignore = "plays a short notification on the real default audio output"]
102 fn notification_plays_on_default_output() {
103 play(
104 SOUND_BYTES.to_vec(),
105 false,
106 Arc::new(AtomicBool::new(false)),
107 )
108 .unwrap();
109 }
110 }
@@ -0,0 +1,110 @@
1//! Linux notification and ringtone playback through the system audio output.
2use std::io::Cursor;
3use std::sync::atomic::{AtomicBool, Ordering};
4use std::sync::{Arc, Mutex};
5use std::time::Duration;
6
7use rodio::cpal::traits::{DeviceTrait, HostTrait};
8
9const SOUND_BYTES: &[u8] = include_bytes!("../../../../app/assets/sounds/notification.mp3");
10const POLL: Duration = Duration::from_millis(20);
11static SOUND_OUTPUT: Mutex<Option<String>> = Mutex::new(None);
12
13pub fn set_sound_output(device_key: Option<String>) {
14 *SOUND_OUTPUT.lock().unwrap_or_else(|p| p.into_inner()) = device_key;
15}
16
17fn open_sink() -> Result<rodio::MixerDeviceSink, String> {
18 let chosen = SOUND_OUTPUT
19 .lock()
20 .unwrap_or_else(|p| p.into_inner())
21 .clone();
22 let device = chosen.and_then(|key| {
23 rodio::cpal::default_host()
24 .output_devices()
25 .ok()?
26 .find(|device| {
27 device.id().is_ok_and(|id| id.1 == key)
28 || device
29 .description()
30 .is_ok_and(|description| description.name() == key)
31 })
32 });
33 if let Some(device) = device {
34 if let Ok(sink) =
35 rodio::DeviceSinkBuilder::from_device(device).and_then(|builder| builder.open_stream())
36 {
37 return Ok(sink);
38 }
39 }
40 rodio::DeviceSinkBuilder::open_default_sink()
41 .map_err(|e| format!("cannot open audio output: {e}"))
42}
43
44fn play(bytes: Vec<u8>, looped: bool, stop: Arc<AtomicBool>) -> Result<(), String> {
45 if stop.load(Ordering::SeqCst) {
46 return Ok(());
47 }
48 let mut sink = open_sink()?;
49 sink.log_on_drop(false);
50 loop {
51 if stop.load(Ordering::SeqCst) {
52 return Ok(());
53 }
54 let player = rodio::play(sink.mixer(), Cursor::new(bytes.clone()))
55 .map_err(|e| format!("cannot decode notification sound: {e}"))?;
56 while !player.empty() {
57 if stop.load(Ordering::SeqCst) {
58 player.stop();
59 return Ok(());
60 }
61 std::thread::sleep(POLL);
62 }
63 if !looped {
64 return Ok(());
65 }
66 }
67}
68
69pub fn play_mp3_until(bytes: Vec<u8>, looped: bool, stop: Arc<AtomicBool>) {
70 std::thread::spawn(move || {
71 if let Err(error) = play(bytes, looped, stop) {
72 crate::diagnostics::record(format!("sound: {error}"));
73 }
74 });
75}
76
77pub fn play_mp3(bytes: Vec<u8>) {
78 play_mp3_until(bytes, false, Arc::new(AtomicBool::new(false)));
79}
80
81pub fn play_sound() {
82 play_mp3(SOUND_BYTES.to_vec());
83}
84
85#[cfg(test)]
86mod tests {
87 use super::*;
88
89 #[test]
90 fn bundled_notification_decodes_to_audible_samples() {
91 let decoded = rodio::Decoder::try_from(Cursor::new(SOUND_BYTES)).unwrap();
92 assert!(decoded.take(48000).any(|sample| sample.abs() > 0.001));
93 }
94
95 #[test]
96 fn cancelled_playback_does_not_open_a_device_or_decode_audio() {
97 assert!(play(Vec::new(), true, Arc::new(AtomicBool::new(true))).is_ok());
98 }
99
100 #[test]
101 #[ignore = "plays a short notification on the real default audio output"]
102 fn notification_plays_on_default_output() {
103 play(
104 SOUND_BYTES.to_vec(),
105 false,
106 Arc::new(AtomicBool::new(false)),
107 )
108 .unwrap();
109 }
110}
crates/app-base/src/notify/platform/mod.rs +2 −0
@@ -3,6 +3,8 @@ mod windows_impl;
3 3 #[cfg(windows)]
4 4 pub use windows_impl::*;
5 5
6 #[cfg(target_os = "linux")]
7 mod linux_sound;
6 8 #[cfg(not(windows))]
7 9 mod stub;
8 10 #[cfg(target_os = "linux")]
@@ -3,6 +3,8 @@ mod windows_impl;
3#[cfg(windows)]
3#[cfg(windows)]
4pub use windows_impl::*;
4pub use windows_impl::*;
5
5
6#[cfg(target_os = "linux")]
7mod linux_sound;
6#[cfg(not(windows))]
8#[cfg(not(windows))]
7mod stub;
9mod stub;
8#[cfg(target_os = "linux")]
10#[cfg(target_os = "linux")]
crates/app-base/src/notify/platform/stub.rs +9 −0
@@ -1,4 +1,6 @@
1 #[cfg(not(target_os = "linux"))]
1 2 use std::sync::Arc;
3 #[cfg(not(target_os = "linux"))]
2 4 use std::sync::atomic::AtomicBool;
3 5
4 6 use gpui_kit::Window;
@@ -86,12 +88,19 @@ pub fn stop_flash(_handle: NativeHandle) {}
86 88
87 89 pub fn set_badge(_handle: NativeHandle, _badge: Option<&Badge>) {}
88 90
91 #[cfg(target_os = "linux")]
92 pub use super::linux_sound::{play_mp3, play_mp3_until, play_sound, set_sound_output};
93
94 #[cfg(not(target_os = "linux"))]
89 95 pub fn play_sound() {}
90 96
97 #[cfg(not(target_os = "linux"))]
91 98 pub fn set_sound_output(_device_key: Option<String>) {}
92 99
100 #[cfg(not(target_os = "linux"))]
93 101 pub fn play_mp3(_bytes: Vec<u8>) {}
94 102
103 #[cfg(not(target_os = "linux"))]
95 104 pub fn play_mp3_until(_bytes: Vec<u8>, _looped: bool, _stop: Arc<AtomicBool>) {}
96 105
97 106 pub struct Tray;
@@ -1,4 +1,6 @@
1#[cfg(not(target_os = "linux"))]
1use std::sync::Arc;
2use std::sync::Arc;
3#[cfg(not(target_os = "linux"))]
2use std::sync::atomic::AtomicBool;
4use std::sync::atomic::AtomicBool;
3
5
4use gpui_kit::Window;
6use gpui_kit::Window;
@@ -86,12 +88,19 @@ pub fn stop_flash(_handle: NativeHandle) {}
86
88
87pub fn set_badge(_handle: NativeHandle, _badge: Option<&Badge>) {}
89pub fn set_badge(_handle: NativeHandle, _badge: Option<&Badge>) {}
88
90
91#[cfg(target_os = "linux")]
92pub use super::linux_sound::{play_mp3, play_mp3_until, play_sound, set_sound_output};
93
94#[cfg(not(target_os = "linux"))]
89pub fn play_sound() {}
95pub fn play_sound() {}
90
96
97#[cfg(not(target_os = "linux"))]
91pub fn set_sound_output(_device_key: Option<String>) {}
98pub fn set_sound_output(_device_key: Option<String>) {}
92
99
100#[cfg(not(target_os = "linux"))]
93pub fn play_mp3(_bytes: Vec<u8>) {}
101pub fn play_mp3(_bytes: Vec<u8>) {}
94
102
103#[cfg(not(target_os = "linux"))]
95pub fn play_mp3_until(_bytes: Vec<u8>, _looped: bool, _stop: Arc<AtomicBool>) {}
104pub fn play_mp3_until(_bytes: Vec<u8>, _looped: bool, _stop: Arc<AtomicBool>) {}
96
105
97pub struct Tray;
106pub struct Tray;
crates/calling/src/signaling.rs +1 −1
@@ -1463,7 +1463,7 @@ pub async fn fetch_self(session: &Session) -> Result<SelfIdentity> {
1463 1463 .request(
1464 1464 Method::Get,
1465 1465 &format!("{GRAPH}/v1.0/me?$select=id,displayName"),
1466 &Scope::graph("User.Read"),
1466 &Scope::graph("User.ReadBasic.All"),
1467 1467 None,
1468 1468 )
1469 1469 .await?;
@@ -1463,7 +1463,7 @@ pub async fn fetch_self(session: &Session) -> Result<SelfIdentity> {
1463 .request(
1463 .request(
1464 Method::Get,
1464 Method::Get,
1465 &format!("{GRAPH}/v1.0/me?$select=id,displayName"),
1465 &format!("{GRAPH}/v1.0/me?$select=id,displayName"),
1466 &Scope::graph("User.Read"),
1466 &Scope::graph("User.ReadBasic.All"),
1467 None,
1467 None,
1468 )
1468 )
1469 .await?;
1469 .await?;
crates/graph/src/client.rs +3 −1
@@ -27,7 +27,9 @@ impl Graph {
27 27 }
28 28
29 29 pub async fn me(&self) -> Result<User> {
30 let body = self.get(&urls::me(), &Scope::graph("User.Read")).await?;
30 let body = self
31 .get(&urls::me(), &Scope::graph("User.ReadBasic.All"))
32 .await?;
31 33 Ok(serde_json::from_value(body)?)
32 34 }
33 35
@@ -27,7 +27,9 @@ impl Graph {
27 }
27 }
28
28
29 pub async fn me(&self) -> Result<User> {
29 pub async fn me(&self) -> Result<User> {
30 let body = self.get(&urls::me(), &Scope::graph("User.Read")).await?;
30 let body = self
31 .get(&urls::me(), &Scope::graph("User.ReadBasic.All"))
32 .await?;
31 Ok(serde_json::from_value(body)?)
33 Ok(serde_json::from_value(body)?)
32 }
34 }
33
35