From b04b4f800d1594c3a6e90176f16fc18c04b8aa70 Mon Sep 17 00:00:00 2001 From: Erica Z Date: Sat, 2 Nov 2024 21:11:18 +0100 Subject: [PATCH] quit mpv on application drop --- src/application.rs | 8 ++++++++ src/mpv.rs | 21 ++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/application.rs b/src/application.rs index 4ec016d..1a376e5 100644 --- a/src/application.rs +++ b/src/application.rs @@ -6,6 +6,7 @@ mod imp { #[derive(Default)] pub struct Application { + // FIXME: move somewhere else mpv: Rc, } @@ -91,6 +92,13 @@ mod imp { impl AdwApplicationImpl for Application {} impl Application {} + + impl Drop for Application { + fn drop(&mut self) { + println!("dropping AudreyApplication"); + self.mpv.command(["quit"]).unwrap(); + } + } } use gtk::{gio, glib}; diff --git a/src/mpv.rs b/src/mpv.rs index 87ba7ee..0df45a9 100644 --- a/src/mpv.rs +++ b/src/mpv.rs @@ -1,6 +1,6 @@ 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")] extern "C" {} @@ -114,6 +114,25 @@ impl Handle { }) } + pub fn command<'a>(&self, args: impl IntoIterator) -> Result<(), Error> { + // add zero terminators to Everything + let args: Vec = 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? pub fn tick(&self) -> Option { // take listener before we drain the event queue, so we don't miss any notifications