diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2024-03-15 22:10:06 +0100 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2024-03-15 22:10:06 +0100 |
| commit | 44e7b4de4073e6dc25681bb2fa6977bf5869689a (patch) | |
| tree | 164c8d8d980732e64d96fe6e788373108144d085 /src/sources | |
| parent | b2096fb3958d9e9cdcca77931aadfb947cab24ee (diff) | |
Add macos sources
Diffstat (limited to 'src/sources')
| -rw-r--r-- | src/sources/apple_music.rs | 71 | ||||
| -rw-r--r-- | src/sources/mod.rs | 66 | ||||
| -rw-r--r-- | src/sources/spotify.rs | 69 |
3 files changed, 184 insertions, 22 deletions
diff --git a/src/sources/apple_music.rs b/src/sources/apple_music.rs new file mode 100644 index 0000000..cb36f5c --- /dev/null +++ b/src/sources/apple_music.rs @@ -0,0 +1,71 @@ +use std::ffi::CStr; +use std::io::Result; + +use cocoa::{base::nil, foundation::NSString}; +use objc::{class, msg_send, sel, sel_impl, runtime::Object}; +use objc_id::Id; + +use crate::Track; + +use super::LyricsSource; + +pub struct AppleMusic; + +impl AppleMusic { + pub fn new() -> Self { + AppleMusic + } +} + +impl LyricsSource for AppleMusic { + + fn name(&self) -> String { + "apple_music".to_string() + } + + fn current_track(&self) -> Option<Track> { + unsafe { + let app: Id<Object> = { + let cls = class!(SBApplication); + let bundle_identifier = NSString::alloc(nil).init_str("com.apple.Music"); + let app: *mut Object = msg_send![cls, applicationWithBundleIdentifier:bundle_identifier]; + Id::from_ptr(app) + }; + + if msg_send![app, isRunning] { + let current_track: *mut Object = msg_send![app, currentTrack]; + if !current_track.is_null() { + let name_raw: *mut Object = msg_send![current_track, name]; + let artist_raw: *mut Object = msg_send![current_track, artist]; + + let name_ptr: *const i8 = msg_send![name_raw, UTF8String]; + let artist_ptr: *const i8 = msg_send![artist_raw, UTF8String]; + + let name = CStr::from_ptr(name_ptr).to_string_lossy().into_owned(); + let artist = CStr::from_ptr(artist_ptr).to_string_lossy().into_owned(); + + + return Some(Track { + name, + artist + }) + } + } + } + None + } + + fn disable(&self) -> Result<()> { + Ok(()) + } + + fn enable(&self) -> Result<()> { + Ok(()) + } + + fn reset(&self) -> Result<()> { + Ok(()) + } +} + + diff --git a/src/sources/mod.rs b/src/sources/mod.rs index fb7af8f..78f7499 100644 --- a/src/sources/mod.rs +++ b/src/sources/mod.rs @@ -1,9 +1,10 @@ -use std::io::Result; +use std::io::{Result, Error, ErrorKind::Other}; + +#[cfg(target_os = "macos")] +mod apple_music; +#[cfg(target_os = "macos")] +mod spotify; -// #[cfg(target_os = "macos")] -// mod applie_music; -// #[cfg(target_os = "macos")] -// mod spotify; // #[cfg(not(target_os = "macos"))] // mod rhythmbox; // #[cfg(not(target_os = "macos"))] @@ -13,10 +14,10 @@ use std::io::Result; // #[cfg(not(target_os = "macos"))] // mod tauon; -// #[cfg(target_os = "macos")] -// use apple_music::AppleMusic; -// #[cfg(target_os = "macos")] -// use spotify::Spotify; +#[cfg(target_os = "macos")] +use apple_music::AppleMusic; +#[cfg(target_os = "macos")] +use spotify::Spotify; // #[cfg(not(target_os = "macos"))] // use rhythmbox::Rhythmbox; @@ -39,26 +40,47 @@ pub trait LyricsSource { fn reset(&self) -> Result<()>; } -pub fn list() -> Result<()> { - Ok(()) +pub fn list() -> Vec<String> { + available_sources().into_iter().map(|source| source.name()).collect() } -pub fn enable(source_name: String) -> Result<()> { - println!("Enabling {}", source_name); - Ok(()) +pub fn enable(source_name: &String) -> Result<()> { + let sources = available_sources(); + for source in sources { + if &source.name() == source_name { + return source.enable() + } + } + Err(Error::new(Other, "No such source was available.")) } -pub fn disable(source_name: String) -> Result<()> { - println!("Disabling {}", source_name); - Ok(()) +pub fn disable(source_name: &String) -> Result<()> { + let sources = available_sources(); + for source in sources { + if &source.name() == source_name { + return source.disable() + } + } + Err(Error::new(Other, "No such source was available.")) } -pub fn reset(source_name: String) -> Result<()> { - println!("Reset {}", source_name); - Ok(()) +pub fn reset(source_name: &String) -> Result<()> { + let sources = available_sources(); + for source in sources { + if &source.name() == source_name { + return source.reset() + } + } + Err(Error::new(Other, "No such source was available.")) } pub fn get_track() -> Option<Track> { + let sources = available_sources(); + for source in sources { + if let Some(track) = source.current_track() { + return Some(track); + } + } return None } @@ -66,8 +88,8 @@ pub fn available_sources() -> Vec<Box<dyn LyricsSource>> { let mut sources: Vec<Box<dyn LyricsSource>> = Vec::new(); #[cfg(target_os = "macos")] { - // sources.push(Box::new(AppleMusic::new())); - // sources.push(Box::new(Spotify::new())); + sources.push(Box::new(AppleMusic::new())); + sources.push(Box::new(Spotify::new())); } #[cfg(not(target_os = "macos"))] diff --git a/src/sources/spotify.rs b/src/sources/spotify.rs new file mode 100644 index 0000000..b23ea73 --- /dev/null +++ b/src/sources/spotify.rs @@ -0,0 +1,69 @@ +use std::ffi::CStr; +use std::io::Result; + +use cocoa::{base::nil, foundation::NSString}; +use objc::{class, msg_send, sel, sel_impl, runtime::Object}; +use objc_id::Id; + +use crate::Track; + +use super::LyricsSource; + +pub struct Spotify; + +impl Spotify { + pub fn new() -> Self { + Spotify + } +} + +impl LyricsSource for Spotify { + + fn name(&self) -> String { + "spotify".to_string() + } + + fn current_track(&self) -> Option<Track> { + unsafe { + let app: Id<Object> = { + let cls = class!(SBApplication); + let bundle_identifier = NSString::alloc(nil).init_str("com.spotify.Client"); + let app: *mut Object = msg_send![cls, applicationWithBundleIdentifier:bundle_identifier]; + Id::from_ptr(app) + }; + + if msg_send![app, isRunning] { + let current_track: *mut Object = msg_send![app, currentTrack]; + if !current_track.is_null() { + let name_raw: *mut Object = msg_send![current_track, name]; + let artist_raw: *mut Object = msg_send![current_track, artist]; + + let name_ptr: *const i8 = msg_send![name_raw, UTF8String]; + let artist_ptr: *const i8 = msg_send![artist_raw, UTF8String]; + + let name = CStr::from_ptr(name_ptr).to_string_lossy().into_owned(); + let artist = CStr::from_ptr(artist_ptr).to_string_lossy().into_owned(); + + + return Some(Track { + name, + artist + }) + } + } + } + None + } + + fn disable(&self) -> Result<()> { + Ok(()) + } + + fn enable(&self) -> Result<()> { + Ok(()) + } + + fn reset(&self) -> Result<()> { + Ok(()) + } +} |