2017-10-27 21:03:45 +00:00
|
|
|
// Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use glib;
|
2019-01-16 11:32:58 +00:00
|
|
|
use glib::object::{Cast, ObjectExt};
|
2018-04-01 08:30:03 +00:00
|
|
|
use glib::signal::SignalHandlerId;
|
2019-05-11 10:13:33 +00:00
|
|
|
use glib::translate::*;
|
2018-04-25 08:10:06 +00:00
|
|
|
use glib::IsA;
|
2017-10-27 21:03:45 +00:00
|
|
|
|
2019-03-19 07:58:20 +00:00
|
|
|
use gobject_sys;
|
2017-10-27 21:03:45 +00:00
|
|
|
|
2019-05-11 10:13:33 +00:00
|
|
|
use ObjectFlags;
|
|
|
|
|
2018-12-08 09:22:42 +00:00
|
|
|
pub trait GstObjectExtManual: 'static {
|
2019-05-23 18:19:24 +00:00
|
|
|
fn connect_deep_notify<F: Fn(&Self, &::Object, &glib::ParamSpec) + Send + Sync + 'static>(
|
2017-10-27 21:03:45 +00:00
|
|
|
&self,
|
2019-05-23 18:19:24 +00:00
|
|
|
name: Option<&str>,
|
2017-10-27 21:03:45 +00:00
|
|
|
f: F,
|
|
|
|
) -> SignalHandlerId;
|
2019-05-11 10:13:33 +00:00
|
|
|
|
|
|
|
fn set_object_flags(&self, flags: ObjectFlags);
|
|
|
|
|
2019-05-11 12:51:33 +00:00
|
|
|
fn unset_object_flags(&self, flags: ObjectFlags);
|
|
|
|
|
2019-05-11 10:13:33 +00:00
|
|
|
fn get_object_flags(&self) -> ObjectFlags;
|
2017-10-27 21:03:45 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 09:22:42 +00:00
|
|
|
impl<O: IsA<::Object>> GstObjectExtManual for O {
|
2019-05-23 18:19:24 +00:00
|
|
|
fn connect_deep_notify<F: Fn(&Self, &::Object, &glib::ParamSpec) + Send + Sync + 'static>(
|
2017-10-27 21:03:45 +00:00
|
|
|
&self,
|
2019-05-23 18:19:24 +00:00
|
|
|
name: Option<&str>,
|
2017-10-27 21:03:45 +00:00
|
|
|
f: F,
|
|
|
|
) -> SignalHandlerId {
|
2018-07-09 17:47:15 +00:00
|
|
|
let signal_name = if let Some(name) = name {
|
|
|
|
format!("deep-notify::{}", name)
|
|
|
|
} else {
|
|
|
|
"deep-notify".into()
|
|
|
|
};
|
|
|
|
|
2020-04-05 14:52:56 +00:00
|
|
|
let obj: Borrowed<glib::Object> =
|
2019-03-19 07:58:20 +00:00
|
|
|
unsafe { from_glib_borrow(self.as_ptr() as *mut gobject_sys::GObject) };
|
2018-12-08 09:22:42 +00:00
|
|
|
|
|
|
|
obj.connect(signal_name.as_str(), false, move |values| {
|
2019-08-11 07:33:34 +00:00
|
|
|
// It would be nice to display the actual signal name in the panic messages below,
|
|
|
|
// but that would require to copy `signal_name` so as to move it into the closure
|
|
|
|
// which seems too much for the messages of development errors
|
|
|
|
let obj: O = unsafe {
|
|
|
|
values[0]
|
|
|
|
.get::<::Object>()
|
|
|
|
.unwrap_or_else(|err| {
|
|
|
|
panic!("Object signal \"deep-notify\": values[0]: {}", err)
|
|
|
|
})
|
|
|
|
.expect("Object signal \"deep-notify\": values[0] not defined")
|
|
|
|
.unsafe_cast()
|
|
|
|
};
|
|
|
|
let prop_obj: ::Object = values[1]
|
|
|
|
.get()
|
|
|
|
.unwrap_or_else(|err| panic!("Object signal \"deep-notify\": values[1]: {}", err))
|
|
|
|
.expect("Object signal \"deep-notify\": values[1] not defined");
|
2017-10-27 21:03:45 +00:00
|
|
|
|
2018-07-09 17:47:15 +00:00
|
|
|
let pspec = unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
let pspec = gobject_sys::g_value_get_param(values[2].to_glib_none().0);
|
2018-07-09 17:47:15 +00:00
|
|
|
from_glib_none(pspec)
|
2017-10-27 21:03:45 +00:00
|
|
|
};
|
|
|
|
|
2018-07-09 17:47:15 +00:00
|
|
|
f(&obj, &prop_obj, &pspec);
|
2017-10-27 21:03:45 +00:00
|
|
|
|
|
|
|
None
|
2018-10-08 12:02:23 +00:00
|
|
|
})
|
|
|
|
.unwrap()
|
2017-10-27 21:03:45 +00:00
|
|
|
}
|
2019-05-11 10:13:33 +00:00
|
|
|
|
|
|
|
fn set_object_flags(&self, flags: ObjectFlags) {
|
|
|
|
unsafe {
|
|
|
|
let ptr: *mut gst_sys::GstObject = self.as_ptr() as *mut _;
|
|
|
|
let _guard = ::utils::MutexGuard::lock(&(*ptr).lock);
|
|
|
|
(*ptr).flags |= flags.to_glib();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 12:51:33 +00:00
|
|
|
fn unset_object_flags(&self, flags: ObjectFlags) {
|
|
|
|
unsafe {
|
|
|
|
let ptr: *mut gst_sys::GstObject = self.as_ptr() as *mut _;
|
|
|
|
let _guard = ::utils::MutexGuard::lock(&(*ptr).lock);
|
|
|
|
(*ptr).flags &= !flags.to_glib();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 10:13:33 +00:00
|
|
|
fn get_object_flags(&self) -> ObjectFlags {
|
|
|
|
unsafe {
|
|
|
|
let ptr: *mut gst_sys::GstObject = self.as_ptr() as *mut _;
|
|
|
|
let _guard = ::utils::MutexGuard::lock(&(*ptr).lock);
|
|
|
|
from_glib((*ptr).flags)
|
|
|
|
}
|
|
|
|
}
|
2017-10-27 21:03:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use prelude::*;
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_deep_notify() {
|
|
|
|
::init().unwrap();
|
|
|
|
|
|
|
|
let bin = ::Bin::new(None);
|
2019-03-19 07:58:20 +00:00
|
|
|
let identity = ::ElementFactory::make("identity", Some("id")).unwrap();
|
2017-10-27 21:03:45 +00:00
|
|
|
bin.add(&identity).unwrap();
|
|
|
|
|
|
|
|
let notify = Arc::new(Mutex::new(None));
|
|
|
|
let notify_clone = notify.clone();
|
2018-07-09 17:47:15 +00:00
|
|
|
bin.connect_deep_notify(None, move |_, id, prop| {
|
2019-12-22 07:59:23 +00:00
|
|
|
*notify_clone.lock().unwrap() = Some((id.clone(), prop.get_name()));
|
2017-10-27 21:03:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
identity.set_property("silent", &false).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
*notify.lock().unwrap(),
|
2020-09-08 13:11:39 +00:00
|
|
|
Some((identity.upcast::<::Object>(), "silent"))
|
2017-10-27 21:03:45 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|