2021-06-23 07:43:53 +00:00
|
|
|
use crate::wayland::Serial;
|
|
|
|
|
|
|
|
use super::{
|
|
|
|
cache::MultiCache, get_children, handlers::is_effectively_sync, transaction::PendingTransaction,
|
|
|
|
SurfaceData,
|
|
|
|
};
|
|
|
|
use std::sync::{atomic::Ordering, Mutex};
|
2019-02-22 20:46:26 +00:00
|
|
|
use wayland_server::protocol::wl_surface::WlSurface;
|
2017-06-04 15:47:37 +00:00
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
pub(crate) static SUBSURFACE_ROLE: &str = "subsurface";
|
|
|
|
|
2017-06-04 15:47:37 +00:00
|
|
|
/// Node of a subsurface tree, holding some user specified data type U
|
|
|
|
/// at each node
|
|
|
|
///
|
|
|
|
/// This type is internal to Smithay, and should not appear in the
|
|
|
|
/// public API
|
|
|
|
///
|
2018-10-30 12:56:30 +00:00
|
|
|
/// It is a bidirectional tree, meaning we can move along it in both
|
2017-06-04 15:47:37 +00:00
|
|
|
/// direction (top-bottom or bottom-up). We are taking advantage of the
|
2018-10-30 12:56:30 +00:00
|
|
|
/// fact that lifetime of objects are decided by Wayland-server to ensure
|
2017-06-04 15:47:37 +00:00
|
|
|
/// the cleanup will be done properly, and we won't leak anything.
|
|
|
|
///
|
2019-04-10 13:22:06 +00:00
|
|
|
/// Each node also appears within its children list, to allow relative placement
|
|
|
|
/// between them.
|
2021-06-23 07:43:53 +00:00
|
|
|
pub struct PrivateSurfaceData {
|
2019-02-22 16:55:00 +00:00
|
|
|
parent: Option<WlSurface>,
|
|
|
|
children: Vec<WlSurface>,
|
2021-06-23 07:43:53 +00:00
|
|
|
public_data: SurfaceData,
|
|
|
|
pending_transaction: PendingTransaction,
|
|
|
|
current_txid: Serial,
|
|
|
|
commit_hooks: Vec<fn(&WlSurface)>,
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
/// An error type signifying that the surface already has a role and
|
|
|
|
/// cannot be assigned an other
|
|
|
|
///
|
|
|
|
/// Generated if you attempt a role operation on a surface that does
|
|
|
|
/// not have the role you asked for.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct AlreadyHasRole;
|
|
|
|
|
|
|
|
impl std::fmt::Display for AlreadyHasRole {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.write_str("Surface already has a role.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for AlreadyHasRole {}
|
|
|
|
|
2017-06-04 20:41:42 +00:00
|
|
|
pub enum Location {
|
|
|
|
Before,
|
|
|
|
After,
|
|
|
|
}
|
|
|
|
|
2021-07-24 23:31:25 +00:00
|
|
|
/// Possible actions to do after handling a node during tree traversal
|
2021-02-12 21:13:38 +00:00
|
|
|
#[derive(Debug)]
|
2017-06-13 15:38:25 +00:00
|
|
|
pub enum TraversalAction<T> {
|
|
|
|
/// Traverse its children as well, providing them the data T
|
|
|
|
DoChildren(T),
|
|
|
|
/// Skip its children
|
|
|
|
SkipChildren,
|
|
|
|
/// Stop traversal completely
|
|
|
|
Break,
|
|
|
|
}
|
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
impl PrivateSurfaceData {
|
|
|
|
pub fn new() -> Mutex<PrivateSurfaceData> {
|
|
|
|
Mutex::new(PrivateSurfaceData {
|
2017-06-04 15:47:37 +00:00
|
|
|
parent: None,
|
2019-04-10 13:22:06 +00:00
|
|
|
children: vec![],
|
2021-06-23 07:43:53 +00:00
|
|
|
public_data: SurfaceData {
|
|
|
|
role: Default::default(),
|
|
|
|
data_map: Default::default(),
|
|
|
|
cached_state: MultiCache::new(),
|
|
|
|
},
|
|
|
|
pending_transaction: Default::default(),
|
|
|
|
current_txid: Serial(0),
|
|
|
|
commit_hooks: Vec::new(),
|
2018-09-24 22:30:39 +00:00
|
|
|
})
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 13:22:06 +00:00
|
|
|
/// Initializes the surface, must be called at creation for state coherence
|
|
|
|
pub fn init(surface: &WlSurface) {
|
2020-04-05 17:01:08 +00:00
|
|
|
let my_data_mutex = surface
|
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
2021-06-23 07:43:53 +00:00
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
2020-04-05 17:01:08 +00:00
|
|
|
.unwrap();
|
2019-04-10 13:22:06 +00:00
|
|
|
let mut my_data = my_data_mutex.lock().unwrap();
|
2019-10-17 07:14:56 +00:00
|
|
|
debug_assert!(my_data.children.is_empty());
|
2019-04-10 13:22:06 +00:00
|
|
|
my_data.children.push(surface.clone());
|
|
|
|
}
|
|
|
|
|
2019-02-22 16:55:00 +00:00
|
|
|
/// Cleans the `as_ref().user_data` of that surface, must be called when it is destroyed
|
|
|
|
pub fn cleanup(surface: &WlSurface) {
|
2020-04-05 17:01:08 +00:00
|
|
|
let my_data_mutex = surface
|
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
2021-06-23 07:43:53 +00:00
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
2020-04-05 17:01:08 +00:00
|
|
|
.unwrap();
|
2018-09-24 22:30:39 +00:00
|
|
|
let mut my_data = my_data_mutex.lock().unwrap();
|
2017-06-04 15:47:37 +00:00
|
|
|
if let Some(old_parent) = my_data.parent.take() {
|
2019-04-10 13:22:06 +00:00
|
|
|
// We had a parent, lets unregister ourselves from it
|
2020-04-05 17:01:08 +00:00
|
|
|
let old_parent_mutex = old_parent
|
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
2021-06-23 07:43:53 +00:00
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
2020-04-05 17:01:08 +00:00
|
|
|
.unwrap();
|
2019-04-10 13:22:06 +00:00
|
|
|
let mut old_parent_guard = old_parent_mutex.lock().unwrap();
|
|
|
|
old_parent_guard
|
|
|
|
.children
|
|
|
|
.retain(|c| !c.as_ref().equals(surface.as_ref()));
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
// orphan all our children
|
2021-09-28 14:02:58 +00:00
|
|
|
for child in my_data.children.drain(..) {
|
2021-06-23 07:43:53 +00:00
|
|
|
let child_mutex = child
|
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
|
|
|
.unwrap();
|
2020-02-16 15:38:07 +00:00
|
|
|
if std::ptr::eq(child_mutex, my_data_mutex) {
|
|
|
|
// This child is ourselves, don't do anything.
|
2017-06-04 15:47:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-02-16 15:38:07 +00:00
|
|
|
|
2017-06-04 15:47:37 +00:00
|
|
|
let mut child_guard = child_mutex.lock().unwrap();
|
|
|
|
child_guard.parent = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
pub fn set_role(surface: &WlSurface, role: &'static str) -> Result<(), AlreadyHasRole> {
|
|
|
|
let my_data_mutex = surface
|
2020-04-05 17:01:08 +00:00
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
2021-06-23 07:43:53 +00:00
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
2020-04-05 17:01:08 +00:00
|
|
|
.unwrap();
|
2021-06-23 07:43:53 +00:00
|
|
|
let mut my_data = my_data_mutex.lock().unwrap();
|
|
|
|
if my_data.public_data.role.is_some() {
|
|
|
|
return Err(AlreadyHasRole);
|
|
|
|
}
|
|
|
|
my_data.public_data.role = Some(role);
|
|
|
|
Ok(())
|
2017-09-03 17:53:29 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
pub fn get_role(surface: &WlSurface) -> Option<&'static str> {
|
|
|
|
let my_data_mutex = surface
|
2020-04-05 17:01:08 +00:00
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
2021-06-23 07:43:53 +00:00
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
2020-04-05 17:01:08 +00:00
|
|
|
.unwrap();
|
2021-06-23 07:43:53 +00:00
|
|
|
let my_data = my_data_mutex.lock().unwrap();
|
|
|
|
my_data.public_data.role
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
pub fn with_states<T, F: FnOnce(&SurfaceData) -> T>(surface: &WlSurface, f: F) -> T {
|
|
|
|
let my_data_mutex = surface
|
2020-04-05 17:01:08 +00:00
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
2021-06-23 07:43:53 +00:00
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
2020-04-05 17:01:08 +00:00
|
|
|
.unwrap();
|
2021-06-23 07:43:53 +00:00
|
|
|
let my_data = my_data_mutex.lock().unwrap();
|
|
|
|
f(&my_data.public_data)
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
pub fn add_commit_hook(surface: &WlSurface, hook: fn(&WlSurface)) {
|
|
|
|
let my_data_mutex = surface
|
2020-04-05 17:01:08 +00:00
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
2021-06-23 07:43:53 +00:00
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
2020-04-05 17:01:08 +00:00
|
|
|
.unwrap();
|
2021-06-23 07:43:53 +00:00
|
|
|
let mut my_data = my_data_mutex.lock().unwrap();
|
|
|
|
my_data.commit_hooks.push(hook);
|
2017-09-03 17:53:29 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
pub fn invoke_commit_hooks(surface: &WlSurface) {
|
|
|
|
// don't hold the mutex while the hooks are invoked
|
|
|
|
let hooks = {
|
|
|
|
let my_data_mutex = surface
|
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
|
|
|
.unwrap();
|
|
|
|
let my_data = my_data_mutex.lock().unwrap();
|
|
|
|
my_data.commit_hooks.clone()
|
|
|
|
};
|
|
|
|
for hook in hooks {
|
|
|
|
hook(surface);
|
|
|
|
}
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
pub fn commit(surface: &WlSurface) {
|
|
|
|
let is_sync = is_effectively_sync(surface);
|
|
|
|
let children = get_children(surface);
|
|
|
|
let my_data_mutex = surface
|
2020-04-05 17:01:08 +00:00
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
2021-06-23 07:43:53 +00:00
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
2020-04-05 17:01:08 +00:00
|
|
|
.unwrap();
|
2021-06-23 07:43:53 +00:00
|
|
|
let mut my_data = my_data_mutex.lock().unwrap();
|
|
|
|
// commit our state
|
|
|
|
let current_txid = my_data.current_txid;
|
|
|
|
my_data.public_data.cached_state.commit(Some(current_txid));
|
|
|
|
// take all our children state into our pending transaction
|
|
|
|
for child in children {
|
|
|
|
let child_data_mutex = child
|
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
|
|
|
.unwrap();
|
|
|
|
// if the child is effectively sync, take its state
|
|
|
|
// this is the case if either we are effectively sync, or the child is explicitly sync
|
|
|
|
let mut child_data = child_data_mutex.lock().unwrap();
|
|
|
|
let is_child_sync = || {
|
|
|
|
child_data
|
|
|
|
.public_data
|
|
|
|
.data_map
|
|
|
|
.get::<super::handlers::SubsurfaceState>()
|
|
|
|
.map(|s| s.sync.load(Ordering::Acquire))
|
|
|
|
.unwrap_or(false)
|
|
|
|
};
|
|
|
|
if is_sync || is_child_sync() {
|
|
|
|
let child_tx = std::mem::take(&mut child_data.pending_transaction);
|
|
|
|
child_tx.merge_into(&my_data.pending_transaction);
|
|
|
|
child_data.current_txid.0 = child_data.current_txid.0.wrapping_add(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
my_data
|
|
|
|
.pending_transaction
|
|
|
|
.insert_state(surface.clone(), my_data.current_txid);
|
|
|
|
if !is_sync {
|
|
|
|
// if we are not sync, apply the transaction immediately
|
|
|
|
let tx = std::mem::take(&mut my_data.pending_transaction);
|
|
|
|
// release the mutex, as applying the transaction will try to lock it
|
|
|
|
std::mem::drop(my_data);
|
|
|
|
// apply the transaction
|
|
|
|
tx.finalize().apply();
|
|
|
|
}
|
2017-09-03 17:53:29 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 13:22:06 +00:00
|
|
|
/// Checks if the first surface is an ancestor of the second
|
|
|
|
pub fn is_ancestor(a: &WlSurface, b: &WlSurface) -> bool {
|
2021-06-23 07:43:53 +00:00
|
|
|
let b_mutex = b.as_ref().user_data().get::<Mutex<PrivateSurfaceData>>().unwrap();
|
2019-04-10 13:22:06 +00:00
|
|
|
let b_guard = b_mutex.lock().unwrap();
|
|
|
|
if let Some(ref parent) = b_guard.parent {
|
|
|
|
if parent.as_ref().equals(a.as_ref()) {
|
2019-10-17 07:14:56 +00:00
|
|
|
true
|
2019-04-10 13:22:06 +00:00
|
|
|
} else {
|
2019-10-17 07:14:56 +00:00
|
|
|
Self::is_ancestor(a, parent)
|
2019-04-10 13:22:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-10-17 07:14:56 +00:00
|
|
|
false
|
2019-04-10 13:22:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-04 15:47:37 +00:00
|
|
|
/// Sets the parent of a surface
|
2017-09-03 17:53:29 +00:00
|
|
|
///
|
2017-06-04 15:47:37 +00:00
|
|
|
/// if this surface already has a role, does nothing and fails, otherwise
|
|
|
|
/// its role is now to be a subsurface
|
2021-06-09 20:46:09 +00:00
|
|
|
pub fn set_parent(child: &WlSurface, parent: &WlSurface) -> Result<(), AlreadyHasRole> {
|
2019-02-22 16:55:00 +00:00
|
|
|
debug_assert!(child.as_ref().is_alive());
|
|
|
|
debug_assert!(parent.as_ref().is_alive());
|
2019-04-10 13:22:06 +00:00
|
|
|
// ensure the child is not already a parent of the parent
|
|
|
|
if Self::is_ancestor(child, parent) {
|
2021-06-09 20:46:09 +00:00
|
|
|
return Err(AlreadyHasRole);
|
2019-04-10 13:22:06 +00:00
|
|
|
}
|
2017-06-04 15:47:37 +00:00
|
|
|
|
|
|
|
// change child's parent
|
|
|
|
{
|
2021-06-23 07:43:53 +00:00
|
|
|
let child_mutex = child
|
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
|
|
|
.unwrap();
|
2017-06-04 15:47:37 +00:00
|
|
|
let mut child_guard = child_mutex.lock().unwrap();
|
2017-09-03 17:53:29 +00:00
|
|
|
// if surface already has a role, it cannot become a subsurface
|
2021-06-23 07:43:53 +00:00
|
|
|
if child_guard.public_data.role.is_some() && child_guard.public_data.role != Some(SUBSURFACE_ROLE)
|
|
|
|
{
|
|
|
|
return Err(AlreadyHasRole);
|
|
|
|
}
|
|
|
|
child_guard.public_data.role = Some(SUBSURFACE_ROLE);
|
2017-06-04 15:47:37 +00:00
|
|
|
debug_assert!(child_guard.parent.is_none());
|
2018-04-17 09:03:42 +00:00
|
|
|
child_guard.parent = Some(parent.clone());
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
// register child to new parent
|
|
|
|
{
|
2020-04-05 17:01:08 +00:00
|
|
|
let parent_mutex = parent
|
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
2021-06-23 07:43:53 +00:00
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
2020-04-05 17:01:08 +00:00
|
|
|
.unwrap();
|
2017-06-04 15:47:37 +00:00
|
|
|
let mut parent_guard = parent_mutex.lock().unwrap();
|
2018-04-17 09:03:42 +00:00
|
|
|
parent_guard.children.push(child.clone())
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove a pre-existing parent of this child
|
|
|
|
///
|
|
|
|
/// Does nothing if it has no parent
|
2019-02-22 16:55:00 +00:00
|
|
|
pub fn unset_parent(child: &WlSurface) {
|
|
|
|
debug_assert!(child.as_ref().is_alive());
|
2017-06-04 15:47:37 +00:00
|
|
|
let old_parent = {
|
2021-06-23 07:43:53 +00:00
|
|
|
let child_mutex = child
|
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
|
|
|
.unwrap();
|
2017-06-04 15:47:37 +00:00
|
|
|
let mut child_guard = child_mutex.lock().unwrap();
|
2021-06-23 07:43:53 +00:00
|
|
|
child_guard.parent.take()
|
2017-06-04 15:47:37 +00:00
|
|
|
};
|
|
|
|
// unregister from our parent
|
|
|
|
if let Some(old_parent) = old_parent {
|
2020-04-05 17:01:08 +00:00
|
|
|
let parent_mutex = old_parent
|
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
2021-06-23 07:43:53 +00:00
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
2020-04-05 17:01:08 +00:00
|
|
|
.unwrap();
|
2017-06-04 15:47:37 +00:00
|
|
|
let mut parent_guard = parent_mutex.lock().unwrap();
|
2019-02-22 16:55:00 +00:00
|
|
|
parent_guard
|
|
|
|
.children
|
|
|
|
.retain(|c| !c.as_ref().equals(child.as_ref()));
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve the parent surface (if any) of this surface
|
2019-02-22 16:55:00 +00:00
|
|
|
pub fn get_parent(child: &WlSurface) -> Option<WlSurface> {
|
2021-06-23 07:43:53 +00:00
|
|
|
let child_mutex = child
|
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
|
|
|
.unwrap();
|
2017-06-04 15:47:37 +00:00
|
|
|
let child_guard = child_mutex.lock().unwrap();
|
2018-06-28 09:33:49 +00:00
|
|
|
child_guard.parent.as_ref().cloned()
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 13:22:06 +00:00
|
|
|
/// Retrieve the children surface (if any) of this surface
|
|
|
|
pub fn get_children(parent: &WlSurface) -> Vec<WlSurface> {
|
2020-04-05 17:01:08 +00:00
|
|
|
let parent_mutex = parent
|
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
2021-06-23 07:43:53 +00:00
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
2020-04-05 17:01:08 +00:00
|
|
|
.unwrap();
|
2019-04-10 13:22:06 +00:00
|
|
|
let parent_guard = parent_mutex.lock().unwrap();
|
|
|
|
parent_guard
|
|
|
|
.children
|
|
|
|
.iter()
|
|
|
|
.filter(|s| !s.as_ref().equals(parent.as_ref()))
|
|
|
|
.cloned()
|
|
|
|
.collect()
|
2017-06-11 12:31:42 +00:00
|
|
|
}
|
|
|
|
|
2017-06-04 20:41:42 +00:00
|
|
|
/// Reorders a surface relative to one of its sibling
|
|
|
|
///
|
|
|
|
/// Fails if `relative_to` is not a sibling or parent of `surface`.
|
2019-02-22 16:55:00 +00:00
|
|
|
pub fn reorder(surface: &WlSurface, to: Location, relative_to: &WlSurface) -> Result<(), ()> {
|
2017-06-04 20:41:42 +00:00
|
|
|
let parent = {
|
2020-04-05 17:01:08 +00:00
|
|
|
let data_mutex = surface
|
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
2021-06-23 07:43:53 +00:00
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
2020-04-05 17:01:08 +00:00
|
|
|
.unwrap();
|
2017-06-04 20:41:42 +00:00
|
|
|
let data_guard = data_mutex.lock().unwrap();
|
2018-06-28 09:33:49 +00:00
|
|
|
data_guard.parent.as_ref().cloned().unwrap()
|
2017-06-04 20:41:42 +00:00
|
|
|
};
|
|
|
|
|
2019-02-22 16:55:00 +00:00
|
|
|
fn index_of(surface: &WlSurface, slice: &[WlSurface]) -> Option<usize> {
|
2017-06-04 20:41:42 +00:00
|
|
|
for (i, s) in slice.iter().enumerate() {
|
2019-02-22 16:55:00 +00:00
|
|
|
if s.as_ref().equals(surface.as_ref()) {
|
2017-06-04 20:41:42 +00:00
|
|
|
return Some(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2020-04-05 17:01:08 +00:00
|
|
|
let parent_mutex = parent
|
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
2021-06-23 07:43:53 +00:00
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
2020-04-05 17:01:08 +00:00
|
|
|
.unwrap();
|
2017-06-04 20:41:42 +00:00
|
|
|
let mut parent_guard = parent_mutex.lock().unwrap();
|
|
|
|
let my_index = index_of(surface, &parent_guard.children).unwrap();
|
2019-04-10 13:22:06 +00:00
|
|
|
let mut other_index = match index_of(relative_to, &parent_guard.children) {
|
2017-06-04 20:41:42 +00:00
|
|
|
Some(idx) => idx,
|
|
|
|
None => return Err(()),
|
|
|
|
};
|
|
|
|
let me = parent_guard.children.remove(my_index);
|
|
|
|
if my_index < other_index {
|
|
|
|
other_index -= 1;
|
|
|
|
}
|
|
|
|
let new_index = match to {
|
|
|
|
Location::Before => other_index,
|
|
|
|
Location::After => other_index + 1,
|
|
|
|
};
|
|
|
|
parent_guard.children.insert(new_index, me);
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-09-03 17:53:29 +00:00
|
|
|
}
|
2017-06-04 20:41:42 +00:00
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
impl PrivateSurfaceData {
|
2017-06-04 15:47:37 +00:00
|
|
|
/// Access sequentially the attributes associated with a surface tree,
|
2017-09-22 09:12:49 +00:00
|
|
|
/// in a depth-first order.
|
2017-06-04 15:47:37 +00:00
|
|
|
///
|
|
|
|
/// Note that an internal lock is taken during access of this data,
|
|
|
|
/// so the tree cannot be manipulated at the same time.
|
|
|
|
///
|
2019-04-10 13:22:06 +00:00
|
|
|
/// The first callback determines if this node children should be processed or not.
|
|
|
|
///
|
|
|
|
/// The second actually does the processing, being called on children in display depth
|
|
|
|
/// order.
|
|
|
|
///
|
|
|
|
/// The third is called once all the children of a node has been processed (including itself), only if the first
|
|
|
|
/// returned `DoChildren`, and gives an opportunity to early stop
|
|
|
|
pub fn map_tree<F1, F2, F3, T>(
|
|
|
|
surface: &WlSurface,
|
|
|
|
initial: &T,
|
|
|
|
mut filter: F1,
|
|
|
|
mut processor: F2,
|
|
|
|
mut post_filter: F3,
|
|
|
|
reverse: bool,
|
|
|
|
) where
|
2021-06-23 07:43:53 +00:00
|
|
|
F1: FnMut(&WlSurface, &SurfaceData, &T) -> TraversalAction<T>,
|
|
|
|
F2: FnMut(&WlSurface, &SurfaceData, &T),
|
|
|
|
F3: FnMut(&WlSurface, &SurfaceData, &T) -> bool,
|
2017-06-04 15:47:37 +00:00
|
|
|
{
|
2019-04-10 13:22:06 +00:00
|
|
|
Self::map(
|
|
|
|
surface,
|
|
|
|
initial,
|
|
|
|
&mut filter,
|
|
|
|
&mut processor,
|
|
|
|
&mut post_filter,
|
|
|
|
reverse,
|
|
|
|
);
|
|
|
|
}
|
2017-06-04 15:47:37 +00:00
|
|
|
|
2019-04-10 13:22:06 +00:00
|
|
|
// helper function for map_tree
|
|
|
|
fn map<F1, F2, F3, T>(
|
|
|
|
surface: &WlSurface,
|
|
|
|
initial: &T,
|
|
|
|
filter: &mut F1,
|
|
|
|
processor: &mut F2,
|
|
|
|
post_filter: &mut F3,
|
|
|
|
reverse: bool,
|
|
|
|
) -> bool
|
|
|
|
where
|
2021-06-23 07:43:53 +00:00
|
|
|
F1: FnMut(&WlSurface, &SurfaceData, &T) -> TraversalAction<T>,
|
|
|
|
F2: FnMut(&WlSurface, &SurfaceData, &T),
|
|
|
|
F3: FnMut(&WlSurface, &SurfaceData, &T) -> bool,
|
2019-04-10 13:22:06 +00:00
|
|
|
{
|
2020-04-05 17:01:08 +00:00
|
|
|
let data_mutex = surface
|
|
|
|
.as_ref()
|
|
|
|
.user_data()
|
2021-06-23 07:43:53 +00:00
|
|
|
.get::<Mutex<PrivateSurfaceData>>()
|
2020-04-05 17:01:08 +00:00
|
|
|
.unwrap();
|
2017-06-04 15:47:37 +00:00
|
|
|
let mut data_guard = data_mutex.lock().unwrap();
|
2017-09-03 17:53:29 +00:00
|
|
|
let data_guard = &mut *data_guard;
|
2019-04-10 13:22:06 +00:00
|
|
|
// call the filter on ourselves
|
2021-06-23 07:43:53 +00:00
|
|
|
match filter(surface, &data_guard.public_data, initial) {
|
2019-04-10 13:22:06 +00:00
|
|
|
TraversalAction::DoChildren(t) => {
|
|
|
|
// loop over children
|
|
|
|
if reverse {
|
|
|
|
for c in data_guard.children.iter().rev() {
|
|
|
|
if c.as_ref().equals(surface.as_ref()) {
|
2021-06-23 07:43:53 +00:00
|
|
|
processor(surface, &data_guard.public_data, initial);
|
2019-04-10 13:22:06 +00:00
|
|
|
} else if !Self::map(c, &t, filter, processor, post_filter, true) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-22 09:12:49 +00:00
|
|
|
}
|
2019-04-10 13:22:06 +00:00
|
|
|
} else {
|
|
|
|
for c in &data_guard.children {
|
|
|
|
if c.as_ref().equals(surface.as_ref()) {
|
2021-06-23 07:43:53 +00:00
|
|
|
processor(surface, &data_guard.public_data, initial);
|
2019-04-10 13:22:06 +00:00
|
|
|
} else if !Self::map(c, &t, filter, processor, post_filter, false) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-13 15:38:25 +00:00
|
|
|
}
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
2021-06-23 07:43:53 +00:00
|
|
|
post_filter(surface, &data_guard.public_data, initial)
|
2019-04-10 13:22:06 +00:00
|
|
|
}
|
|
|
|
TraversalAction::SkipChildren => {
|
|
|
|
// still process ourselves
|
2021-06-23 07:43:53 +00:00
|
|
|
processor(surface, &data_guard.public_data, initial);
|
2019-04-10 13:22:06 +00:00
|
|
|
true
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
2019-04-10 13:22:06 +00:00
|
|
|
TraversalAction::Break => false,
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|