]> git.r.bdr.sh - rbdr/lyricli/blob - src/sources/apple_music.rs
Add macos sources
[rbdr/lyricli] / src / sources / apple_music.rs
1 use std::ffi::CStr;
2 use std::io::Result;
3
4 use cocoa::{base::nil, foundation::NSString};
5 use objc::{class, msg_send, sel, sel_impl, runtime::Object};
6 use objc_id::Id;
7
8 use crate::Track;
9
10 use super::LyricsSource;
11
12 pub struct AppleMusic;
13
14 impl AppleMusic {
15 pub fn new() -> Self {
16 AppleMusic
17 }
18 }
19
20 impl LyricsSource for AppleMusic {
21
22 fn name(&self) -> String {
23 "apple_music".to_string()
24 }
25
26 fn current_track(&self) -> Option<Track> {
27 unsafe {
28 let app: Id<Object> = {
29 let cls = class!(SBApplication);
30 let bundle_identifier = NSString::alloc(nil).init_str("com.apple.Music");
31 let app: *mut Object = msg_send![cls, applicationWithBundleIdentifier:bundle_identifier];
32 Id::from_ptr(app)
33 };
34
35 if msg_send![app, isRunning] {
36 let current_track: *mut Object = msg_send![app, currentTrack];
37 if !current_track.is_null() {
38 let name_raw: *mut Object = msg_send![current_track, name];
39 let artist_raw: *mut Object = msg_send![current_track, artist];
40
41 let name_ptr: *const i8 = msg_send![name_raw, UTF8String];
42 let artist_ptr: *const i8 = msg_send![artist_raw, UTF8String];
43
44 let name = CStr::from_ptr(name_ptr).to_string_lossy().into_owned();
45 let artist = CStr::from_ptr(artist_ptr).to_string_lossy().into_owned();
46
47
48 return Some(Track {
49 name,
50 artist
51 })
52 }
53 }
54 }
55 None
56 }
57
58 fn disable(&self) -> Result<()> {
59 Ok(())
60 }
61
62 fn enable(&self) -> Result<()> {
63 Ok(())
64 }
65
66 fn reset(&self) -> Result<()> {
67 Ok(())
68 }
69 }
70
71