1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
use crate::PlayerTrait;
use termusiclib::track::Track;
// use crate::souvlaki::{
// MediaControlEvent, MediaControls, MediaMetadata, MediaPlayback, PlatformConfig,
// };
use crate::GeneralPlayer;
use souvlaki::{MediaControlEvent, MediaControls, MediaMetadata, MediaPlayback, PlatformConfig};
// use std::str::FromStr;
use std::sync::mpsc::{self, Receiver};
// use std::sync::{mpsc, Arc, Mutex};
// use std::thread::{self, JoinHandle};
pub struct Mpris {
controls: MediaControls,
pub rx: Receiver<MediaControlEvent>,
}
impl Default for Mpris {
fn default() -> Self {
// #[cfg(not(target_os = "windows"))]
let hwnd = None;
// #[cfg(target_os = "windows")]
// let hwnd = {
// use raw_window_handle::windows::WindowsHandle;
// let handle: WindowsHandle = unimplemented!();
// Some(handle.hwnd)
// };
let config = PlatformConfig {
dbus_name: "termusic",
display_name: "Termuisc in Rust",
hwnd,
};
let mut controls = MediaControls::new(config).unwrap();
let (tx, rx) = mpsc::sync_channel(32);
// The closure must be Send and have a static lifetime.
controls
.attach(move |event: MediaControlEvent| {
tx.send(event).ok();
})
.ok();
Self { controls, rx }
}
}
impl Mpris {
pub fn add_and_play(&mut self, track: &Track) {
// This is to fix a bug that the first track is not updated
std::thread::sleep(std::time::Duration::from_millis(100));
self.controls
.set_playback(MediaPlayback::Playing { progress: None })
.ok();
self.controls
.set_metadata(MediaMetadata {
title: Some(track.title().unwrap_or("Unknown Title")),
artist: Some(track.artist().unwrap_or("Unknown Artist")),
album: Some(track.album().unwrap_or("")),
..MediaMetadata::default()
})
.ok();
}
pub fn pause(&mut self) {
self.controls
.set_playback(MediaPlayback::Paused { progress: None })
.ok();
}
pub fn resume(&mut self) {
self.controls
.set_playback(MediaPlayback::Playing { progress: None })
.ok();
}
}
impl GeneralPlayer {
pub fn mpris_handler(&mut self, e: MediaControlEvent) {
match e {
MediaControlEvent::Next => {
self.next();
}
MediaControlEvent::Previous => {
self.previous();
}
MediaControlEvent::Pause => {
self.backend.pause();
}
MediaControlEvent::Toggle => {
self.toggle_pause();
}
MediaControlEvent::Play => {
self.backend.resume();
}
// MediaControlEvent::Seek(x) => match x {
// SeekDirection::Forward => activity.player.seek(5).ok(),
// SeekDirection::Backward => activity.player.seek(-5).ok(),
// },
// MediaControlEvent::SetPosition(position) => {
// let _position = position. / 1000;
// }
MediaControlEvent::OpenUri(_uri) => {
// let wait = async {
// self.player.add_and_play(&uri).await;
// };
// let rt = tokio::runtime::Runtime::new().expect("failed to create runtime");
// rt.block_on(wait);
}
_ => {}
}
}
pub fn update_mpris(&mut self) {
if let Ok(m) = self.mpris.rx.try_recv() {
self.mpris_handler(m);
}
}
}