audrey/src/ui/window.rs
2024-11-05 10:22:50 +01:00

489 lines
17 KiB
Rust

mod imp {
use crate::mpv;
use crate::PlaybinSong;
use adw::prelude::*;
use adw::subclass::prelude::*;
use glib::subclass::InitializingObject;
use gtk::{gdk, gio, glib};
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use tracing::{event, span, Level};
#[derive(gtk::CompositeTemplate, glib::Properties)]
#[template(resource = "/eu/callcc/audrey/window.ui")]
#[properties(wrapper_type = super::Window)]
pub struct Window {
#[template_child]
pub(super) playbar: TemplateChild<crate::ui::Playbar>,
#[template_child]
pub(super) play_queue: TemplateChild<crate::ui::PlayQueue>,
#[property(get, set, default = false)]
can_click_shuffle_all: Cell<bool>,
#[property(get, set, nullable)]
playing_cover_art: RefCell<Option<gdk::Paintable>>,
#[property(type = Option<PlaybinSong>, get = Self::song, nullable)]
_song: (),
pub(super) setup: crate::ui::Setup,
pub(super) api: RefCell<Option<Rc<crate::subsonic::Client>>>,
pub(super) mpv: mpv::Handle,
#[property(get)]
playlist_model: gio::ListStore,
#[property(type = u32, get = Self::volume, set = Self::set_volume)]
_volume: (),
#[property(type = bool, get = Self::mute, set = Self::set_mute)]
_mute: (),
#[property(type = bool, get = Self::pause, set = Self::set_pause)]
_pause: (),
#[property(type = i32, get = Self::playlist_pos)]
_playlist_pos: (),
#[property(type = f64, get = Self::time_pos)]
_time_pos: (),
#[property(type = f64, get = Self::duration)]
_duration: (), // as reported by mpv, compare with song.duration
#[property(type = bool, get = Self::idle_active)]
_idle_active: (),
#[property(type = u32, get = Self::playlist_count)]
_playlist_count: (),
}
impl Default for Window {
fn default() -> Self {
let mpv = mpv::Handle::new();
mpv.set_property("audio-client-name", "audrey").unwrap();
mpv.set_property("user-agent", crate::USER_AGENT).unwrap();
mpv.set_property("video", false).unwrap();
mpv.set_property("prefetch-playlist", true).unwrap();
mpv.set_property("gapless-audio", true).unwrap();
mpv.observe_property(0, "volume").unwrap();
mpv.observe_property(1, "mute").unwrap();
mpv.observe_property(2, "pause").unwrap();
mpv.observe_property(3, "playlist-pos").unwrap();
mpv.observe_property(4, "idle-active").unwrap();
mpv.observe_property(5, "time-pos").unwrap();
mpv.observe_property(6, "playlist-count").unwrap();
mpv.observe_property(7, "duration").unwrap();
mpv.observe_property(8, "path").unwrap();
// "Useful to drain property changes before a new file is loaded."
mpv.add_hook(0, "on_before_start_file", 0).unwrap();
Self {
playbar: Default::default(),
play_queue: Default::default(),
can_click_shuffle_all: Cell::new(false),
playing_cover_art: Default::default(),
_song: (),
setup: Default::default(),
api: Default::default(),
mpv,
playlist_model: gio::ListStore::new::<PlaybinSong>(),
_volume: (),
_mute: (),
_pause: (),
_playlist_pos: (),
_time_pos: (),
_duration: (),
_idle_active: (),
_playlist_count: (),
}
}
}
#[glib::object_subclass]
impl ObjectSubclass for Window {
const NAME: &'static str = "AudreyUiWindow";
type Type = super::Window;
type ParentType = adw::ApplicationWindow;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
klass.bind_template_callbacks();
}
fn instance_init(obj: &InitializingObject<Self>) {
obj.init_template();
}
}
#[glib::derived_properties]
impl ObjectImpl for Window {
fn constructed(&self) {
self.parent_constructed();
let window = self.obj().downgrade();
glib::spawn_future_local(async move {
while let Some(window) = window.upgrade() {
let listener = window.imp().mpv.wakeup_listener();
while let Some(event) = window.imp().mpv.wait_event(0.0) {
use crate::mpv::Event;
let span = span!(Level::DEBUG, "mpv_wait_event");
let _guard = span.enter();
match event {
Event::PropertyChange(event) => match event.reply_userdata {
0 => {
assert_eq!(event.name, "volume");
window.notify("volume");
}
1 => {
assert_eq!(event.name, "mute");
window.notify("mute");
}
2 => {
assert_eq!(event.name, "pause");
window.notify("pause");
}
3 => {
assert_eq!(event.name, "playlist-pos");
window.notify("playlist-pos");
}
4 => {
assert_eq!(event.name, "idle-active");
window.notify("idle-active");
}
5 => {
assert_eq!(event.name, "time-pos");
window.notify("time-pos");
}
6 => {
assert_eq!(event.name, "playlist-count");
window.notify("playlist-count");
}
7 => {
assert_eq!(event.name, "duration");
window.notify("duration");
}
8 => {
assert_eq!(event.name, "path");
// sanity check
match window.imp().mpv.get_property::<String>("path") {
Ok(path) => {
assert_eq!(path, window.song().unwrap().stream_url())
}
Err(err) if err.is_property_unavailable() => {}
Err(err) => Err(err).unwrap(),
}
}
_ => unreachable!(),
},
Event::StartFile(_) => {
window.notify("song");
// TODO: load cover art
}
Event::Hook(event) => match event.reply_userdata {
0 => {
assert_eq!(&event.name, "on_before_start_file");
event!(Level::DEBUG, "on_before_start_file triggered");
// just use this as a barrier
window.imp().mpv.continue_hook(event.id).unwrap();
}
_ => unreachable!(),
},
Event::LogMessage(event) => {
match event.log_level {
// level has to be 'static so this sux
l if l <= 20 => {
event!(target: "mpv", Level::ERROR, prefix = event.prefix, event = event.text.trim())
}
l if l <= 30 => {
event!(target: "mpv", Level::WARN, prefix = event.prefix, event = event.text.trim())
}
l if l <= 40 => {
event!(target: "mpv", Level::INFO, prefix = event.prefix, event = event.text.trim())
}
l if l <= 60 => {
event!(target: "mpv", Level::DEBUG, prefix = event.prefix, event = event.text.trim())
}
l if l <= 70 => {
event!(target: "mpv", Level::TRACE, prefix = event.prefix, event = event.text.trim())
}
// should be unused
_ => event!(
target: "mpv",
Level::DEBUG,
prefix = event.prefix,
event = event.text.trim(),
"unknown mpv level: {}",
event.log_level,
),
};
}
Event::Unknown(_) => {
// either deprecated or future, ignore
}
_ => event!(Level::DEBUG, "unhandled {event:?}"),
}
}
drop(window);
listener.await;
}
});
// set up mpris
let window = self.obj().clone();
glib::spawn_future_local(async move {
let conn = zbus::connection::Builder::session()
.expect("could not connect to the session bus")
.internal_executor(false)
.build()
.await
.expect("could not build connection to the session bus");
// run this in glib's main loop
glib::spawn_future_local(glib::clone!(
#[strong]
conn,
async move {
loop {
conn.executor().tick().await;
}
}
));
crate::Mpris::setup(conn.object_server(), &window)
.await
.expect("could not serve mpris");
/*
crate::mpris::Player::setup(conn.object_server(), &window.imp().playbin)
.await
.expect("could not serve mpris player");
FIXME */
drop(window); // don't keep this alive
// always set up handlers before requesting service name
conn.request_name("org.mpris.MediaPlayer2.audrey")
.await
.expect("could not register name in session bus");
});
}
}
impl WidgetImpl for Window {}
impl WindowImpl for Window {}
impl ApplicationWindowImpl for Window {}
impl AdwApplicationWindowImpl for Window {}
#[gtk::template_callbacks]
impl Window {
#[template_callback]
fn show_playbar_cover_art(&self, stack_child: Option<&str>) -> bool {
stack_child != Some("play-queue")
}
#[template_callback]
async fn shuffle_all(&self) {
self.obj().set_can_click_shuffle_all(false);
self.mpv.command(["stop"]).unwrap();
self.playlist_model.remove_all();
self.set_pause(false);
let api = {
let api = self.api.borrow();
Rc::clone(api.as_ref().unwrap())
};
for song in api.get_random_songs(10).await.unwrap().into_iter() {
let song = PlaybinSong::from_child(&api, &song);
self.mpv
.command(["loadfile", &song.stream_url(), "append-play"])
.unwrap();
self.playlist_model.append(&song);
}
self.obj().set_can_click_shuffle_all(true);
}
#[template_callback]
fn show_setup_dialog(&self) {
self.setup.present(Some(self.obj().as_ref()));
}
fn volume(&self) -> u32 {
self.mpv
.get_property::<i64>("volume")
.unwrap()
.try_into()
.unwrap()
}
fn set_volume(&self, volume: u32) {
self.mpv.set_property("volume", volume as i64).unwrap();
}
fn mute(&self) -> bool {
self.mpv.get_property("mute").unwrap()
}
fn set_mute(&self, mute: bool) {
self.mpv.set_property("mute", mute).unwrap();
}
fn pause(&self) -> bool {
self.mpv.get_property("pause").unwrap()
}
fn set_pause(&self, pause: bool) {
self.mpv.set_property("pause", pause).unwrap();
}
fn playlist_pos(&self) -> i32 {
self.mpv
.get_property::<i64>("playlist-pos")
.unwrap()
.try_into()
.unwrap()
}
fn time_pos(&self) -> f64 {
match self.mpv.get_property("time-pos") {
Ok(time_pos) => Ok(time_pos),
Err(err) if err.is_property_unavailable() => Ok(0.0), //placeholder
Err(err) => Err(err),
}
.unwrap()
}
fn duration(&self) -> f64 {
let duration = match self.mpv.get_property::<f64>("duration") {
Ok(duration) => Ok(Some(duration)),
Err(err) if err.is_property_unavailable() => {
Ok(self.song().as_ref().map(|song| song.duration() as f64))
}
Err(err) => Err(err),
}
.unwrap();
assert_eq!(
duration.map(|f| f as i64),
self.song().as_ref().map(crate::PlaybinSong::duration),
"mpv duration doesn not match subsonic duration (this should probably be a warn)"
);
duration.unwrap_or(0.0) // placeholder
}
fn idle_active(&self) -> bool {
self.mpv.get_property("idle-active").unwrap()
}
fn playlist_count(&self) -> u32 {
self.mpv
.get_property::<i64>("playlist-count")
.unwrap()
.try_into()
.unwrap()
}
fn song(&self) -> Option<PlaybinSong> {
if self.obj().playlist_pos() < 0 {
None
} else {
let song: PlaybinSong = self
.obj()
.playlist_model()
.item(self.obj().playlist_pos() as u32)
.unwrap()
.dynamic_cast()
.unwrap();
Some(song)
}
}
}
impl Drop for Window {
fn drop(&mut self) {
event!(Level::DEBUG, "dropping AudreyUiWindow");
}
}
}
use adw::prelude::*;
use adw::subclass::prelude::*;
use gtk::{gio, glib};
use std::rc::Rc;
glib::wrapper! {
pub struct Window(ObjectSubclass<imp::Window>)
@extends adw::ApplicationWindow, gtk::ApplicationWindow, gtk::Window, gtk::Widget,
@implements gio::ActionGroup, gio::ActionMap, gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager;
}
impl Window {
pub fn new(app: &impl IsA<gtk::Application>) -> Self {
let window: Self = glib::Object::builder().property("application", app).build();
window.imp().setup.set_window(&window);
window.imp().setup.load();
window
}
pub fn playbin(&self) -> ! {
todo!()
}
pub fn playlist_play_index(&self, index: i64) {
self.imp()
.mpv
.command(["playlist-play-index", &index.to_string()])
.unwrap();
}
pub fn playlist_remove(&self, index: i64) {
self.imp()
.mpv
.command(["playlist-remove", &index.to_string()])
.unwrap();
self.playlist_model().remove(index as u32);
}
pub fn setup_connected(&self, api: crate::subsonic::Client) {
self.imp().api.replace(Some(Rc::new(api)));
self.set_can_click_shuffle_all(true);
}
pub fn playlist_next(&self) {
self.imp().mpv.command(["playlist-next"]).unwrap();
}
pub fn playlist_prev(&self) {
self.imp().mpv.command(["playlist-prev"]).unwrap();
}
pub fn seek(&self, new_position: f64) {
self.imp()
.mpv
.command(["seek", &new_position.to_string(), "absolute", "exact"])
.unwrap();
}
}