fix idle-active detection

This commit is contained in:
Erica Z 2024-11-19 21:12:55 +01:00
parent 378eb0761d
commit 20aaacd40e
3 changed files with 28 additions and 3 deletions

View file

@ -63,6 +63,7 @@ pub enum PropertyEventValue {
Int64(i64), Int64(i64),
Double(f64), Double(f64),
String(String), String(String),
Flag(bool),
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]

View file

@ -5,7 +5,7 @@ use super::event::{
use super::{ffi, Error, Event as MpvEvent, GetProperty, SetProperty}; use super::{ffi, Error, Event as MpvEvent, GetProperty, SetProperty};
use event_listener::{Event, EventListener, IntoNotification}; use event_listener::{Event, EventListener, IntoNotification};
use std::cell::{RefCell, RefMut}; 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::fmt;
use std::pin::Pin; use std::pin::Pin;
use std::ptr::NonNull; 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<u32, Error> { pub fn unobserve_property(&self, registered_reply_userdata: u64) -> Result<u32, Error> {
let rc = let rc =
unsafe { ffi::mpv_unobserve_property(self.inner.as_ptr(), registered_reply_userdata) }; 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(), 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!(), _ => todo!(),
}, },
})) }))

View file

@ -99,7 +99,7 @@ mod imp {
mpv.observe_property_string(0, "path").unwrap(); mpv.observe_property_string(0, "path").unwrap();
mpv.observe_property_int64(3, "playlist-pos").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(6, "playlist-count").unwrap();
mpv.observe_property_double(7, "duration").unwrap(); mpv.observe_property_double(7, "duration").unwrap();
@ -583,8 +583,13 @@ mod imp {
4 => { 4 => {
assert_eq!(event.name, "idle-active"); 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"); self.obj().notify("idle-active");
if self.obj().idle_active() { if value {
self.on_idle_active(); self.on_idle_active();
} }
} }