diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-07-17 15:31:38 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-07-17 15:31:38 +0200 |
| commit | ee8ac99d9fa1f348065108b73297eebc8033e57e (patch) | |
| tree | ffccfd2492503c667d9d7fe67361294fef54d130 /src/sources | |
| parent | b3964aefbcab8ff098afabf64befa04a4eebd993 (diff) | |
Use objc2, rename dbus to mpris, add swinsian4.0.0
Diffstat (limited to 'src/sources')
| -rw-r--r-- | src/sources/apple_music.rs | 31 | ||||
| -rw-r--r-- | src/sources/mod.rs | 18 | ||||
| -rw-r--r-- | src/sources/mpris.rs (renamed from src/sources/dbus.rs) | 10 | ||||
| -rw-r--r-- | src/sources/spotify.rs | 31 | ||||
| -rw-r--r-- | src/sources/swinsian.rs | 62 |
5 files changed, 110 insertions, 42 deletions
diff --git a/src/sources/apple_music.rs b/src/sources/apple_music.rs index 572fe6b..af61b61 100644 --- a/src/sources/apple_music.rs +++ b/src/sources/apple_music.rs @@ -1,10 +1,6 @@ use std::io::Result; -use objc2::{ - msg_send, - rc::{Retained, autoreleasepool}, - runtime::AnyObject, -}; +use objc2::{msg_send, rc::Retained, runtime::AnyObject}; use objc2_foundation::NSString; use objc2_scripting_bridge::SBApplication; @@ -28,19 +24,22 @@ impl LyricsSource for AppleMusic { fn current_track(&self) -> Option<Track> { unsafe { let bundle_identifier = NSString::from_str("com.apple.Music"); - let app = SBApplication::applicationWithBundleIdentifier(&bundle_identifier); - if let Some(app) = app - && app.isRunning() - { - let current_track: Option<Retained<AnyObject>> = msg_send![&app, currentTrack]; - if let Some(current_track) = current_track { - let name_raw: Option<Retained<NSString>> = msg_send![¤t_track, name]; - let artist_raw: Option<Retained<NSString>> = msg_send![¤t_track, artist]; + if let Some(app) = SBApplication::applicationWithBundleIdentifier(&bundle_identifier) { + let key = NSString::from_str("currentTrack"); + let current_track: Option<Retained<AnyObject>> = msg_send![&app, valueForKey:&*key]; - if let (Some(name_raw), Some(artist_raw)) = (name_raw, artist_raw) { - let name = autoreleasepool(|pool| name_raw.to_str(pool).to_string()); - let artist = autoreleasepool(|pool| artist_raw.to_str(pool).to_string()); + if let Some(track) = current_track { + let name_key = NSString::from_str("name"); + let artist_key = NSString::from_str("artist"); + let name: Option<Retained<NSString>> = + msg_send![&track, valueForKey:&*name_key]; + let artist: Option<Retained<NSString>> = + msg_send![&track, valueForKey:&*artist_key]; + + if let Some(name) = name.map(|s| s.to_string()) + && let Some(artist) = artist.map(|s| s.to_string()) + { return Some(Track { name, artist }); } } diff --git a/src/sources/mod.rs b/src/sources/mod.rs index bf68ec3..cac5833 100644 --- a/src/sources/mod.rs +++ b/src/sources/mod.rs @@ -1,20 +1,25 @@ +use crate::configuration::Configuration; use std::io::{Error, Result}; #[cfg(target_os = "macos")] mod apple_music; #[cfg(target_os = "macos")] mod spotify; +#[cfg(target_os = "macos")] +mod swinsian; #[cfg(target_os = "linux")] -mod dbus; +mod mpris; #[cfg(target_os = "macos")] use apple_music::AppleMusic; #[cfg(target_os = "macos")] use spotify::Spotify; +#[cfg(target_os = "macos")] +use swinsian::Swinsian; #[cfg(target_os = "linux")] -use dbus::Dbus; +use mpris::Mpris; use crate::Track; @@ -65,10 +70,12 @@ pub fn reset(source_name: &String) -> Result<()> { Err(Error::other("No such source was available.")) } -pub fn get_track() -> Option<Track> { +pub fn get_track(configuration: &Configuration) -> Option<Track> { let sources = available_sources(); for source in sources { - if let Some(track) = source.current_track() { + if configuration.is_enabled(&source.name()) + && let Some(track) = source.current_track() + { return Some(track); } } @@ -81,11 +88,12 @@ pub fn available_sources() -> Vec<Box<dyn LyricsSource>> { { sources.push(Box::new(AppleMusic::new())); sources.push(Box::new(Spotify::new())); + sources.push(Box::new(Swinsian::new())); } #[cfg(not(target_os = "macos"))] { - sources.push(Box::new(Dbus::new())); + sources.push(Box::new(Mpris::new())); } sources diff --git a/src/sources/dbus.rs b/src/sources/mpris.rs index 2f6bccd..69eb862 100644 --- a/src/sources/dbus.rs +++ b/src/sources/mpris.rs @@ -5,17 +5,17 @@ use crate::Track; use super::LyricsSource; -pub struct Dbus; +pub struct Mpris; -impl Dbus { +impl Mpris { pub fn new() -> Self { - Dbus + Mpris } } -impl LyricsSource for Dbus { +impl LyricsSource for Mpris { fn name(&self) -> String { - "dbus".to_string() + "mpris".to_string() } fn current_track(&self) -> Option<Track> { diff --git a/src/sources/spotify.rs b/src/sources/spotify.rs index cd37491..8010ad1 100644 --- a/src/sources/spotify.rs +++ b/src/sources/spotify.rs @@ -1,10 +1,6 @@ use std::io::Result; -use objc2::{ - msg_send, - rc::{Retained, autoreleasepool}, - runtime::AnyObject, -}; +use objc2::{msg_send, rc::Retained, runtime::AnyObject}; use objc2_foundation::NSString; use objc2_scripting_bridge::SBApplication; @@ -28,19 +24,22 @@ impl LyricsSource for Spotify { fn current_track(&self) -> Option<Track> { unsafe { let bundle_identifier = NSString::from_str("com.spotify.Client"); - let app = SBApplication::applicationWithBundleIdentifier(&bundle_identifier); - if let Some(app) = app - && app.isRunning() - { - let current_track: Option<Retained<AnyObject>> = msg_send![&app, currentTrack]; - if let Some(current_track) = current_track { - let name_raw: Option<Retained<NSString>> = msg_send![¤t_track, name]; - let artist_raw: Option<Retained<NSString>> = msg_send![¤t_track, artist]; + if let Some(app) = SBApplication::applicationWithBundleIdentifier(&bundle_identifier) { + let key = NSString::from_str("currentTrack"); + let current_track: Option<Retained<AnyObject>> = msg_send![&app, valueForKey:&*key]; - if let (Some(name_raw), Some(artist_raw)) = (name_raw, artist_raw) { - let name = autoreleasepool(|pool| name_raw.to_str(pool).to_string()); - let artist = autoreleasepool(|pool| artist_raw.to_str(pool).to_string()); + if let Some(track) = current_track { + let name_key = NSString::from_str("name"); + let artist_key = NSString::from_str("artist"); + let name: Option<Retained<NSString>> = + msg_send![&track, valueForKey:&*name_key]; + let artist: Option<Retained<NSString>> = + msg_send![&track, valueForKey:&*artist_key]; + + if let Some(name) = name.map(|s| s.to_string()) + && let Some(artist) = artist.map(|s| s.to_string()) + { return Some(Track { name, artist }); } } diff --git a/src/sources/swinsian.rs b/src/sources/swinsian.rs new file mode 100644 index 0000000..641af6f --- /dev/null +++ b/src/sources/swinsian.rs @@ -0,0 +1,62 @@ +use std::io::Result; + +use objc2::{msg_send, rc::Retained, runtime::AnyObject}; +use objc2_foundation::NSString; +use objc2_scripting_bridge::SBApplication; + +use crate::Track; + +use super::LyricsSource; + +pub struct Swinsian; + +impl Swinsian { + pub fn new() -> Self { + Swinsian + } +} + +impl LyricsSource for Swinsian { + fn name(&self) -> String { + "swinsian".to_string() + } + + fn current_track(&self) -> Option<Track> { + unsafe { + let bundle_identifier = NSString::from_str("com.swinsian.Swinsian"); + if let Some(app) = SBApplication::applicationWithBundleIdentifier(&bundle_identifier) { + let key = NSString::from_str("currentTrack"); + let current_track: Option<Retained<AnyObject>> = msg_send![&app, valueForKey:&*key]; + + if let Some(track) = current_track { + let name_key = NSString::from_str("name"); + let artist_key = NSString::from_str("artist"); + + let name: Option<Retained<NSString>> = + msg_send![&track, valueForKey:&*name_key]; + let artist: Option<Retained<NSString>> = + msg_send![&track, valueForKey:&*artist_key]; + + if let Some(name) = name.map(|s| s.to_string()) + && let Some(artist) = artist.map(|s| s.to_string()) + { + return Some(Track { name, artist }); + } + } + } + } + None + } + + fn disable(&self) -> Result<()> { + Ok(()) + } + + fn enable(&self) -> Result<()> { + Ok(()) + } + + fn reset(&self) -> Result<()> { + Ok(()) + } +} |