aboutsummaryrefslogtreecommitdiff
path: root/src/sources/spotify.rs
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2024-03-15 22:10:06 +0100
committerRuben Beltran del Rio <git@r.bdr.sh>2024-03-15 22:10:06 +0100
commit44e7b4de4073e6dc25681bb2fa6977bf5869689a (patch)
tree164c8d8d980732e64d96fe6e788373108144d085 /src/sources/spotify.rs
parentb2096fb3958d9e9cdcca77931aadfb947cab24ee (diff)
Add macos sources
Diffstat (limited to 'src/sources/spotify.rs')
-rw-r--r--src/sources/spotify.rs69
1 files changed, 69 insertions, 0 deletions
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(())
+ }
+}