cosmic-comp/src/utils/prelude.rs

142 lines
4.4 KiB
Rust
Raw Normal View History

2022-07-04 15:27:08 +02:00
use smithay::{
backend::drm::VrrSupport as Support,
2024-04-29 19:00:39 +02:00
output::{Output, WeakOutput},
2024-04-05 13:53:35 +02:00
utils::{Rectangle, Transform},
2022-07-04 15:27:08 +02:00
};
pub use super::geometry::*;
2024-11-18 19:47:59 +01:00
use crate::config::{AdaptiveSync, OutputConfig, OutputState};
2024-04-05 13:53:35 +02:00
pub use crate::shell::{SeatExt, Shell, Workspace};
2022-08-31 13:01:23 +02:00
pub use crate::state::{Common, State};
2022-07-05 18:46:38 +02:00
pub use crate::wayland::handlers::xdg_shell::popup::update_reactive_popups;
2022-07-04 15:27:08 +02:00
use std::{
cell::{Ref, RefCell, RefMut},
sync::{
2024-11-18 19:47:59 +01:00
atomic::{AtomicU8, Ordering},
Mutex,
},
2024-04-29 19:00:39 +02:00
};
2024-04-29 15:40:29 +02:00
2022-07-04 15:27:08 +02:00
pub trait OutputExt {
fn geometry(&self) -> Rectangle<i32, Global>;
2024-11-18 19:47:59 +01:00
fn adaptive_sync(&self) -> AdaptiveSync;
fn set_adaptive_sync(&self, vrr: AdaptiveSync);
fn adaptive_sync_support(&self) -> Option<Support>;
fn set_adaptive_sync_support(&self, vrr: Option<Support>);
2024-04-29 19:00:39 +02:00
fn mirroring(&self) -> Option<Output>;
fn set_mirroring(&self, output: Option<Output>);
fn is_enabled(&self) -> bool;
fn config(&self) -> Ref<'_, OutputConfig>;
fn config_mut(&self) -> RefMut<'_, OutputConfig>;
2022-07-04 15:27:08 +02:00
}
2024-11-18 19:47:59 +01:00
struct Vrr(AtomicU8);
struct VrrSupport(AtomicU8);
2024-04-29 19:00:39 +02:00
struct Mirroring(Mutex<Option<WeakOutput>>);
2022-07-04 15:27:08 +02:00
impl OutputExt for Output {
fn geometry(&self) -> Rectangle<i32, Global> {
2022-07-04 16:00:29 +02:00
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()
})
.as_global()
2022-07-04 15:27:08 +02:00
}
2024-04-29 15:40:29 +02:00
2024-11-18 19:47:59 +01:00
fn adaptive_sync(&self) -> AdaptiveSync {
2024-04-29 15:40:29 +02:00
self.user_data()
.get::<Vrr>()
2024-11-18 19:47:59 +01:00
.map(|vrr| match vrr.0.load(Ordering::SeqCst) {
2 => AdaptiveSync::Force,
1 => AdaptiveSync::Enabled,
_ => AdaptiveSync::Disabled,
})
.unwrap_or(AdaptiveSync::Disabled)
2024-04-29 15:40:29 +02:00
}
2024-11-18 19:47:59 +01:00
fn set_adaptive_sync(&self, vrr: AdaptiveSync) {
2024-04-29 15:40:29 +02:00
let user_data = self.user_data();
2024-11-18 19:47:59 +01:00
user_data.insert_if_missing_threadsafe(|| Vrr(AtomicU8::new(0)));
user_data.get::<Vrr>().unwrap().0.store(
match vrr {
AdaptiveSync::Disabled => 0,
AdaptiveSync::Enabled => 1,
AdaptiveSync::Force => 2,
},
Ordering::SeqCst,
);
2024-04-29 15:40:29 +02:00
}
2024-04-29 19:00:39 +02:00
fn adaptive_sync_support(&self) -> Option<Support> {
self.user_data()
.get::<VrrSupport>()
.map(|vrr| match vrr.0.load(Ordering::SeqCst) {
0 => None,
2 => Some(Support::RequiresModeset),
3 => Some(Support::Supported),
_ => Some(Support::NotSupported),
})
.flatten()
}
fn set_adaptive_sync_support(&self, vrr: Option<Support>) {
let user_data = self.user_data();
user_data.insert_if_missing_threadsafe(|| VrrSupport(AtomicU8::new(0)));
user_data.get::<VrrSupport>().unwrap().0.store(
match vrr {
None => 0,
Some(Support::NotSupported) => 1,
Some(Support::RequiresModeset) => 2,
Some(Support::Supported) => 3,
},
Ordering::SeqCst,
);
}
2024-04-29 19:00:39 +02:00
fn mirroring(&self) -> Option<Output> {
self.user_data().get::<Mirroring>().and_then(|mirroring| {
mirroring
.0
.lock()
.unwrap()
.clone()
.and_then(|o| o.upgrade())
})
}
fn set_mirroring(&self, output: Option<Output>) {
let user_data = self.user_data();
user_data.insert_if_missing_threadsafe(|| Mirroring(Mutex::new(None)));
*user_data.get::<Mirroring>().unwrap().0.lock().unwrap() =
output.map(|output| output.downgrade());
}
fn is_enabled(&self) -> bool {
self.user_data()
.get::<RefCell<OutputConfig>>()
.map(|conf| conf.borrow().enabled != OutputState::Disabled)
.unwrap_or(false)
}
fn config(&self) -> Ref<'_, OutputConfig> {
self.user_data()
.get::<RefCell<OutputConfig>>()
.unwrap()
.borrow()
}
fn config_mut(&self) -> RefMut<'_, OutputConfig> {
self.user_data()
.get::<RefCell<OutputConfig>>()
.unwrap()
.borrow_mut()
}
2022-07-04 15:27:08 +02:00
}