pinnacle/src/window.rs

495 lines
16 KiB
Rust
Raw Normal View History

2023-08-01 11:06:35 -05:00
// SPDX-License-Identifier: GPL-3.0-or-later
2023-06-25 17:18:50 -05:00
use std::{cell::RefCell, sync::atomic::AtomicU32, time::Duration};
2023-06-18 19:30:52 -05:00
use smithay::{
2023-07-24 12:50:24 -05:00
backend::input::KeyState,
desktop::{
2023-07-24 12:50:24 -05:00
space::SpaceElement,
utils::{
send_dmabuf_feedback_surface_tree, send_frames_surface_tree,
take_presentation_feedback_surface_tree, with_surfaces_surface_tree,
OutputPresentationFeedback,
},
Window, WindowSurfaceType,
},
2023-07-24 12:50:24 -05:00
input::{
keyboard::{KeyboardTarget, KeysymHandle, ModifiersState},
pointer::{AxisFrame, MotionEvent, PointerTarget},
Seat,
},
output::Output,
2023-07-12 18:50:41 -05:00
reexports::{
wayland_protocols::{
wp::presentation_time::server::wp_presentation_feedback,
xdg::shell::server::xdg_toplevel,
},
2023-07-12 18:50:41 -05:00
wayland_server::protocol::wl_surface::WlSurface,
},
2023-07-24 12:50:24 -05:00
utils::{user_data::UserDataMap, IsAlive, Logical, Point, Rectangle, Serial},
wayland::{
compositor::{Blocker, BlockerState, SurfaceData},
dmabuf::DmabufFeedback,
seat::WaylandFocus,
},
xwayland::X11Surface,
2023-06-18 19:30:52 -05:00
};
2023-05-28 19:14:30 -05:00
use crate::{
backend::Backend,
state::{State, WithState},
};
2023-05-28 19:14:30 -05:00
use self::window_state::{Float, WindowState};
2023-05-28 19:14:30 -05:00
pub mod window_state;
#[derive(Debug, Clone, PartialEq)]
pub enum WindowElement {
Wayland(Window),
X11(X11Surface),
}
impl WindowElement {
pub fn surface_under(
&self,
location: Point<i32, Logical>,
window_type: WindowSurfaceType,
) -> Option<(WlSurface, Point<i32, Logical>)> {
todo!()
}
pub fn with_surfaces<F>(&self, processor: F)
where
F: FnMut(&WlSurface, &SurfaceData) + Copy,
{
match self {
WindowElement::Wayland(window) => window.with_surfaces(processor),
WindowElement::X11(surface) => {
if let Some(surface) = surface.wl_surface() {
with_surfaces_surface_tree(&surface, processor);
}
}
}
}
pub fn send_frame<T, F>(
&self,
output: &Output,
time: T,
throttle: Option<Duration>,
primary_scan_out_output: F,
) where
T: Into<Duration>,
F: FnMut(&WlSurface, &SurfaceData) -> Option<Output> + Copy,
{
match self {
WindowElement::Wayland(window) => {
window.send_frame(output, time, throttle, primary_scan_out_output)
}
WindowElement::X11(surface) => {
if let Some(surface) = surface.wl_surface() {
send_frames_surface_tree(
&surface,
output,
time,
throttle,
primary_scan_out_output,
);
}
}
}
}
pub fn send_dmabuf_feedback<'a, P, F>(
&self,
output: &Output,
primary_scan_out_output: P,
select_dmabuf_feedback: F,
) where
P: FnMut(&WlSurface, &SurfaceData) -> Option<Output> + Copy,
F: Fn(&WlSurface, &SurfaceData) -> &'a DmabufFeedback + Copy,
{
match self {
WindowElement::Wayland(window) => {
window.send_dmabuf_feedback(
output,
primary_scan_out_output,
select_dmabuf_feedback,
);
}
WindowElement::X11(surface) => {
if let Some(surface) = surface.wl_surface() {
send_dmabuf_feedback_surface_tree(
&surface,
output,
primary_scan_out_output,
select_dmabuf_feedback,
);
}
}
}
}
pub fn take_presentation_feedback<F1, F2>(
&self,
output_feedback: &mut OutputPresentationFeedback,
primary_scan_out_output: F1,
presentation_feedback_flags: F2,
) where
F1: FnMut(&WlSurface, &SurfaceData) -> Option<Output> + Copy,
F2: FnMut(&WlSurface, &SurfaceData) -> wp_presentation_feedback::Kind + Copy,
{
match self {
WindowElement::Wayland(window) => {
window.take_presentation_feedback(
output_feedback,
primary_scan_out_output,
presentation_feedback_flags,
);
}
WindowElement::X11(surface) => {
if let Some(surface) = surface.wl_surface() {
take_presentation_feedback_surface_tree(
&surface,
output_feedback,
primary_scan_out_output,
presentation_feedback_flags,
);
}
}
}
}
pub fn wl_surface(&self) -> Option<WlSurface> {
match self {
WindowElement::Wayland(window) => window.wl_surface(),
WindowElement::X11(surface) => surface.wl_surface(),
}
}
pub fn user_data(&self) -> &UserDataMap {
match self {
WindowElement::Wayland(window) => window.user_data(),
WindowElement::X11(surface) => surface.user_data(),
}
}
}
impl IsAlive for WindowElement {
fn alive(&self) -> bool {
match self {
WindowElement::Wayland(window) => window.alive(),
WindowElement::X11(surface) => surface.alive(),
}
}
}
2023-07-24 12:50:24 -05:00
impl<B: Backend> PointerTarget<State<B>> for WindowElement {
fn enter(&self, seat: &Seat<State<B>>, data: &mut State<B>, event: &MotionEvent) {
// TODO: ssd
match self {
WindowElement::Wayland(window) => PointerTarget::enter(window, seat, data, event),
WindowElement::X11(surface) => PointerTarget::enter(surface, seat, data, event),
}
}
fn motion(&self, seat: &Seat<State<B>>, data: &mut State<B>, event: &MotionEvent) {
// TODO: ssd
match self {
WindowElement::Wayland(window) => PointerTarget::motion(window, seat, data, event),
WindowElement::X11(surface) => PointerTarget::motion(surface, seat, data, event),
}
}
fn relative_motion(
&self,
seat: &Seat<State<B>>,
data: &mut State<B>,
event: &smithay::input::pointer::RelativeMotionEvent,
) {
// TODO: ssd
match self {
WindowElement::Wayland(window) => {
PointerTarget::relative_motion(window, seat, data, event);
}
WindowElement::X11(surface) => {
PointerTarget::relative_motion(surface, seat, data, event);
}
}
}
fn button(
&self,
seat: &Seat<State<B>>,
data: &mut State<B>,
event: &smithay::input::pointer::ButtonEvent,
) {
// TODO: ssd
match self {
WindowElement::Wayland(window) => PointerTarget::button(window, seat, data, event),
WindowElement::X11(surface) => PointerTarget::button(surface, seat, data, event),
}
}
fn axis(&self, seat: &Seat<State<B>>, data: &mut State<B>, frame: AxisFrame) {
// TODO: ssd
match self {
WindowElement::Wayland(window) => PointerTarget::axis(window, seat, data, frame),
WindowElement::X11(surface) => PointerTarget::axis(surface, seat, data, frame),
}
}
fn leave(&self, seat: &Seat<State<B>>, data: &mut State<B>, serial: Serial, time: u32) {
// TODO: ssd
match self {
WindowElement::Wayland(window) => {
PointerTarget::leave(window, seat, data, serial, time);
}
WindowElement::X11(surface) => PointerTarget::leave(surface, seat, data, serial, time),
}
}
}
impl<B: Backend> KeyboardTarget<State<B>> for WindowElement {
fn enter(
&self,
seat: &Seat<State<B>>,
data: &mut State<B>,
keys: Vec<KeysymHandle<'_>>,
serial: Serial,
) {
match self {
WindowElement::Wayland(window) => {
KeyboardTarget::enter(window, seat, data, keys, serial);
}
WindowElement::X11(surface) => KeyboardTarget::enter(surface, seat, data, keys, serial),
}
}
fn leave(&self, seat: &Seat<State<B>>, data: &mut State<B>, serial: Serial) {
match self {
WindowElement::Wayland(window) => KeyboardTarget::leave(window, seat, data, serial),
WindowElement::X11(surface) => KeyboardTarget::leave(surface, seat, data, serial),
}
}
fn key(
&self,
seat: &Seat<State<B>>,
data: &mut State<B>,
key: KeysymHandle<'_>,
state: KeyState,
serial: Serial,
time: u32,
) {
match self {
WindowElement::Wayland(window) => {
KeyboardTarget::key(window, seat, data, key, state, serial, time);
}
WindowElement::X11(surface) => {
KeyboardTarget::key(surface, seat, data, key, state, serial, time);
}
}
}
fn modifiers(
&self,
seat: &Seat<State<B>>,
data: &mut State<B>,
modifiers: ModifiersState,
serial: Serial,
) {
match self {
WindowElement::Wayland(window) => {
KeyboardTarget::modifiers(window, seat, data, modifiers, serial);
}
WindowElement::X11(surface) => {
KeyboardTarget::modifiers(surface, seat, data, modifiers, serial);
}
}
}
}
impl SpaceElement for WindowElement {
fn geometry(&self) -> Rectangle<i32, Logical> {
// TODO: ssd
match self {
WindowElement::Wayland(window) => SpaceElement::geometry(window),
WindowElement::X11(surface) => SpaceElement::geometry(surface),
}
}
fn bbox(&self) -> Rectangle<i32, Logical> {
// TODO: ssd
match self {
WindowElement::Wayland(window) => SpaceElement::bbox(window),
WindowElement::X11(surface) => SpaceElement::bbox(surface),
}
}
fn is_in_input_region(&self, point: &Point<f64, Logical>) -> bool {
// TODO: ssd
match self {
WindowElement::Wayland(window) => SpaceElement::is_in_input_region(window, point),
WindowElement::X11(surface) => SpaceElement::is_in_input_region(surface, point),
}
}
fn z_index(&self) -> u8 {
match self {
WindowElement::Wayland(window) => SpaceElement::z_index(window),
WindowElement::X11(surface) => SpaceElement::z_index(surface),
}
}
fn set_activate(&self, activated: bool) {
match self {
WindowElement::Wayland(window) => SpaceElement::set_activate(window, activated),
WindowElement::X11(surface) => SpaceElement::set_activate(surface, activated),
}
}
fn output_enter(&self, output: &Output, overlap: Rectangle<i32, Logical>) {
match self {
WindowElement::Wayland(window) => SpaceElement::output_enter(window, output, overlap),
WindowElement::X11(surface) => SpaceElement::output_enter(surface, output, overlap),
}
}
fn output_leave(&self, output: &Output) {
match self {
WindowElement::Wayland(window) => SpaceElement::output_leave(window, output),
WindowElement::X11(surface) => SpaceElement::output_leave(surface, output),
}
}
fn refresh(&self) {
match self {
WindowElement::Wayland(window) => SpaceElement::refresh(window),
WindowElement::X11(surface) => SpaceElement::refresh(surface),
}
}
}
impl WithState for WindowElement {
type State = WindowState;
fn with_state<F, T>(&self, mut func: F) -> T
where
F: FnMut(&mut Self::State) -> T,
{
self.user_data()
.insert_if_missing(RefCell::<Self::State>::default);
let state = self
.user_data()
.get::<RefCell<Self::State>>()
.expect("RefCell not in data map");
func(&mut state.borrow_mut())
}
}
impl<B: Backend> State<B> {
/// Returns the [Window] associated with a given [WlSurface].
pub fn window_for_surface(&self, surface: &WlSurface) -> Option<Window> {
self.space
.elements()
.find(|window| window.wl_surface().map(|s| s == *surface).unwrap_or(false))
.cloned()
2023-06-30 21:34:07 -05:00
.or_else(|| {
self.windows
.iter()
.find(|&win| win.toplevel().wl_surface() == surface)
.cloned()
})
}
}
/// Toggle a window's floating status.
2023-06-18 19:30:52 -05:00
pub fn toggle_floating<B: Backend>(state: &mut State<B>, window: &Window) {
window.with_state(|window_state| {
2023-05-28 19:14:30 -05:00
match window_state.floating {
2023-06-18 19:30:52 -05:00
Float::Tiled(prev_loc_and_size) => {
2023-05-28 19:14:30 -05:00
if let Some((prev_loc, prev_size)) = prev_loc_and_size {
window.toplevel().with_pending_state(|state| {
state.size = Some(prev_size);
});
window.toplevel().send_pending_configure();
2023-07-12 18:50:41 -05:00
state.space.map_element(window.clone(), prev_loc, false);
// TODO: should it activate?
2023-05-28 19:14:30 -05:00
}
2023-06-18 19:30:52 -05:00
window_state.floating = Float::Floating;
2023-07-12 18:50:41 -05:00
window.toplevel().with_pending_state(|tl_state| {
tl_state.states.unset(xdg_toplevel::State::TiledTop);
tl_state.states.unset(xdg_toplevel::State::TiledBottom);
tl_state.states.unset(xdg_toplevel::State::TiledLeft);
tl_state.states.unset(xdg_toplevel::State::TiledRight);
});
2023-05-28 19:14:30 -05:00
}
Float::Floating => {
2023-06-18 19:30:52 -05:00
window_state.floating = Float::Tiled(Some((
2023-06-19 12:42:49 -05:00
// We get the location this way because window.geometry().loc
// doesn't seem to be the actual location
state.space.element_location(window).unwrap(),
2023-05-28 19:14:30 -05:00
window.geometry().size,
)));
2023-07-12 18:50:41 -05:00
window.toplevel().with_pending_state(|tl_state| {
tl_state.states.set(xdg_toplevel::State::TiledTop);
tl_state.states.set(xdg_toplevel::State::TiledBottom);
tl_state.states.set(xdg_toplevel::State::TiledLeft);
tl_state.states.set(xdg_toplevel::State::TiledRight);
});
2023-05-28 19:14:30 -05:00
}
}
2023-06-18 19:30:52 -05:00
});
let output = state.focus_state.focused_output.clone().unwrap();
state.re_layout(&output);
2023-07-09 17:48:46 -05:00
let render = output.with_state(|op_state| {
state
.windows
.iter()
.cloned()
.filter(|win| {
win.with_state(|win_state| {
if win_state.floating.is_floating() {
return true;
}
for tag in win_state.tags.iter() {
if op_state.focused_tags().any(|tg| tg == tag) {
return true;
}
}
false
})
})
.collect::<Vec<_>>()
});
let clone = window.clone();
state.loop_handle.insert_idle(move |data| {
crate::state::schedule_on_commit(data, render, move |dt| {
dt.state.space.raise_element(&clone, true);
});
2023-07-09 17:48:46 -05:00
});
2023-05-28 19:14:30 -05:00
}
pub struct WindowBlocker;
pub static BLOCKER_COUNTER: AtomicU32 = AtomicU32::new(0);
impl Blocker for WindowBlocker {
fn state(&self) -> BlockerState {
if BLOCKER_COUNTER.load(std::sync::atomic::Ordering::SeqCst) > 0 {
BlockerState::Pending
} else {
BlockerState::Released
}
}
}