From 20aaacd40e70b06327a6f36e2403a98d9d51b5dd Mon Sep 17 00:00:00 2001 From: Erica Z Date: Tue, 19 Nov 2024 21:12:55 +0100 Subject: [PATCH] fix idle-active detection --- src/mpv/event.rs | 1 + src/mpv/handle.rs | 21 ++++++++++++++++++++- src/ui/window.rs | 9 +++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/mpv/event.rs b/src/mpv/event.rs index cebe92f..a559ae3 100644 --- a/src/mpv/event.rs +++ b/src/mpv/event.rs @@ -63,6 +63,7 @@ pub enum PropertyEventValue { Int64(i64), Double(f64), String(String), + Flag(bool), } #[derive(Clone, Debug)] diff --git a/src/mpv/handle.rs b/src/mpv/handle.rs index aada46d..1fabf88 100644 --- a/src/mpv/handle.rs +++ b/src/mpv/handle.rs @@ -5,7 +5,7 @@ use super::event::{ use super::{ffi, Error, Event as MpvEvent, GetProperty, SetProperty}; use event_listener::{Event, EventListener, IntoNotification}; use std::cell::{RefCell, RefMut}; -use std::ffi::{c_char, c_void, CStr, CString}; +use std::ffi::{c_char, c_int, c_void, CStr, CString}; use std::fmt; use std::pin::Pin; use std::ptr::NonNull; @@ -177,6 +177,18 @@ impl Handle { }) } + pub fn observe_property_flag(&self, reply_userdata: u64, name: &str) -> Result<(), Error> { + let name = CString::new(name).expect("null bytes in property name"); + Error::from_return_code(unsafe { + ffi::mpv_observe_property( + self.inner.as_ptr(), + reply_userdata, + name.as_ptr(), + ffi::mpv_format_MPV_FORMAT_FLAG, + ) + }) + } + pub fn unobserve_property(&self, registered_reply_userdata: u64) -> Result { let rc = unsafe { ffi::mpv_unobserve_property(self.inner.as_ptr(), registered_reply_userdata) }; @@ -299,6 +311,13 @@ impl Handle { String::from_utf8_lossy(value.to_bytes()).into_owned(), )) } + ffi::mpv_format_MPV_FORMAT_FLAG => Some(PropertyEventValue::Flag(unsafe { + match *(data.data as *mut c_int) { + 0 => false, + 1 => true, + other => unreachable!("bad mpv flag value {other}"), + } + })), _ => todo!(), }, })) diff --git a/src/ui/window.rs b/src/ui/window.rs index 875bb7a..b12670a 100644 --- a/src/ui/window.rs +++ b/src/ui/window.rs @@ -99,7 +99,7 @@ mod imp { mpv.observe_property_string(0, "path").unwrap(); mpv.observe_property_int64(3, "playlist-pos").unwrap(); - mpv.observe_property(4, "idle-active").unwrap(); + mpv.observe_property_flag(4, "idle-active").unwrap(); mpv.observe_property(6, "playlist-count").unwrap(); mpv.observe_property_double(7, "duration").unwrap(); @@ -583,8 +583,13 @@ mod imp { 4 => { assert_eq!(event.name, "idle-active"); + let value = match event.value { + Some(PropertyEventValue::Flag(b)) => b, + _ => unreachable!(), + }; + event!(Level::TRACE, "idle-active is now {value}"); self.obj().notify("idle-active"); - if self.obj().idle_active() { + if value { self.on_idle_active(); } }