quit mpv on application drop

This commit is contained in:
Erica Z 2024-11-02 21:11:18 +01:00
parent 859e1ca527
commit b04b4f800d
2 changed files with 28 additions and 1 deletions

View file

@ -6,6 +6,7 @@ mod imp {
#[derive(Default)] #[derive(Default)]
pub struct Application { pub struct Application {
// FIXME: move somewhere else
mpv: Rc<mpv::Handle>, mpv: Rc<mpv::Handle>,
} }
@ -91,6 +92,13 @@ mod imp {
impl AdwApplicationImpl for Application {} impl AdwApplicationImpl for Application {}
impl Application {} impl Application {}
impl Drop for Application {
fn drop(&mut self) {
println!("dropping AudreyApplication");
self.mpv.command(["quit"]).unwrap();
}
}
} }
use gtk::{gio, glib}; use gtk::{gio, glib};

View file

@ -1,6 +1,6 @@
mod ffi; mod ffi;
use std::ffi::{c_int, c_void, CStr, CString}; use std::ffi::{c_char, c_int, c_void, CStr, CString};
#[link(name = "mpv")] #[link(name = "mpv")]
extern "C" {} extern "C" {}
@ -114,6 +114,25 @@ impl Handle {
}) })
} }
pub fn command<'a>(&self, args: impl IntoIterator<Item = &'a str>) -> Result<(), Error> {
// add zero terminators to Everything
let args: Vec<CString> = args
.into_iter()
.map(CString::new)
.map(Result::unwrap)
.collect();
let mut args: Vec<*const c_char> = args
.iter()
.map(CString::as_c_str)
.map(CStr::as_ptr)
.collect();
// must be null terminated
args.push(std::ptr::null_mut());
let args: *mut *const c_char = args.as_mut_ptr();
Error::from_return_code(unsafe { ffi::mpv_command(self.inner.as_ptr(), args) })
}
// TODO: return None on SHUTDOWN possibly? // TODO: return None on SHUTDOWN possibly?
pub fn tick(&self) -> Option<EventListener> { pub fn tick(&self) -> Option<EventListener> {
// take listener before we drain the event queue, so we don't miss any notifications // take listener before we drain the event queue, so we don't miss any notifications