utils: Adding additional helpers
This commit is contained in:
parent
06d5989223
commit
a088f7fd6e
4 changed files with 108 additions and 43 deletions
43
src/utils.rs
43
src/utils.rs
|
|
@ -1,43 +0,0 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
use smithay::reexports::wayland_server::{Global, Interface, Resource};
|
||||
use std::{
|
||||
convert::{AsRef, From},
|
||||
sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
},
|
||||
};
|
||||
|
||||
pub struct GlobalDrop<I: Interface + AsRef<Resource<I>> + From<Resource<I>>>(Option<Global<I>>);
|
||||
|
||||
impl<I: Interface + AsRef<Resource<I>> + From<Resource<I>>> From<Global<I>> for GlobalDrop<I> {
|
||||
fn from(g: Global<I>) -> GlobalDrop<I> {
|
||||
GlobalDrop(Some(g))
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: Interface + AsRef<Resource<I>> + From<Resource<I>>> Drop for GlobalDrop<I> {
|
||||
fn drop(&mut self) {
|
||||
if let Some(global) = self.0.take() {
|
||||
global.destroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This hack will hopefully will be superseeded by a better solution, when smithay transitions to wayland-rs 0.30.
|
||||
// But until then there is not really a better way to schedule a repaint on surface destruction
|
||||
#[derive(Debug)]
|
||||
pub struct SurfaceDropNotifier(Arc<AtomicBool>);
|
||||
|
||||
impl From<&crate::state::Common> for SurfaceDropNotifier {
|
||||
fn from(state: &crate::state::Common) -> Self {
|
||||
SurfaceDropNotifier(state.dirty_flag.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for SurfaceDropNotifier {
|
||||
fn drop(&mut self) {
|
||||
self.0.store(true, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
41
src/utils/ids.rs
Normal file
41
src/utils/ids.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
// TODO: Remove once desktop is back
|
||||
#![allow(unused)]
|
||||
|
||||
macro_rules! id_gen {
|
||||
($func_name:ident, $id_name:ident, $ids_name:ident) => {
|
||||
static $id_name: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||
lazy_static::lazy_static! {
|
||||
static ref $ids_name: std::sync::Mutex<std::collections::HashSet<usize>> =
|
||||
std::sync::Mutex::new(std::collections::HashSet::new());
|
||||
}
|
||||
|
||||
fn $func_name() -> usize {
|
||||
let mut ids = $ids_name.lock().unwrap();
|
||||
if ids.len() == usize::MAX {
|
||||
panic!("Out of ids");
|
||||
}
|
||||
|
||||
let id = loop {
|
||||
let new_id = $id_name.fetch_update(
|
||||
std::sync::atomic::Ordering::SeqCst,
|
||||
std::sync::atomic::Ordering::SeqCst,
|
||||
|mut id| {
|
||||
while ids.iter().any(|k| *k == id) {
|
||||
id += 1;
|
||||
}
|
||||
id += 1;
|
||||
Some(id)
|
||||
},
|
||||
);
|
||||
if let Ok(id) = new_id {
|
||||
break id;
|
||||
}
|
||||
};
|
||||
|
||||
ids.insert(id);
|
||||
id
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) use id_gen;
|
||||
6
src/utils/mod.rs
Normal file
6
src/utils/mod.rs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
mod ids;
|
||||
pub(crate) use self::ids::id_gen;
|
||||
|
||||
pub mod prelude;
|
||||
61
src/utils/prelude.rs
Normal file
61
src/utils/prelude.rs
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
use std::cell::RefCell;
|
||||
use smithay::{
|
||||
wayland::{
|
||||
output::Output,
|
||||
seat::Seat,
|
||||
},
|
||||
utils::{Rectangle, Transform, Logical},
|
||||
};
|
||||
use crate::{
|
||||
input::ActiveOutput,
|
||||
state::Common,
|
||||
};
|
||||
|
||||
pub use crate::{
|
||||
state::State,
|
||||
};
|
||||
|
||||
pub trait OutputExt {
|
||||
fn geometry(&self) -> Rectangle<i32, Logical>;
|
||||
}
|
||||
|
||||
impl OutputExt for Output {
|
||||
fn geometry(&self) -> Rectangle<i32, Logical> {
|
||||
Rectangle::from_loc_and_size(
|
||||
self.current_location(),
|
||||
{
|
||||
Transform::from(self.current_transform()).transform_size(
|
||||
self.current_mode().map(|m| m.size).unwrap_or_else(|| (0,0).into())
|
||||
).to_f64().to_logical(self.current_scale().fractional_scale()).to_i32_round()
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn active_output(seat: &Seat<State>, state: &Common) -> Output {
|
||||
seat.user_data()
|
||||
.get::<ActiveOutput>()
|
||||
.map(|x| x.0.borrow().clone())
|
||||
.unwrap_or_else(|| {
|
||||
state
|
||||
.shell
|
||||
.outputs()
|
||||
.next()
|
||||
.cloned()
|
||||
.expect("Backend has no outputs?")
|
||||
})
|
||||
}
|
||||
|
||||
pub fn set_active_output(seat: &Seat<State>, output: &Output) {
|
||||
if !seat
|
||||
.user_data()
|
||||
.insert_if_missing(|| ActiveOutput(RefCell::new(output.clone())))
|
||||
{
|
||||
*seat
|
||||
.user_data()
|
||||
.get::<ActiveOutput>()
|
||||
.unwrap()
|
||||
.0
|
||||
.borrow_mut() = output.clone();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue