wip: New shell logic

This commit is contained in:
Victoria Brekenfeld 2022-09-28 12:01:29 +02:00
parent 146a4893ca
commit 00f1b029da
39 changed files with 3922 additions and 2503 deletions

631
src/shell/element/mod.rs Normal file
View file

@ -0,0 +1,631 @@
use crate::state::State;
use id_tree::NodeId;
use smithay::{
backend::{
input::KeyState,
renderer::{element::AsRenderElements, ImportAll, Renderer},
},
desktop::{space::SpaceElement, Kind, PopupManager, Window, WindowSurfaceType},
input::{
keyboard::{KeyboardTarget, KeysymHandle, ModifiersState},
pointer::{AxisFrame, ButtonEvent, MotionEvent, PointerTarget},
Seat,
},
output::Output,
reexports::{
wayland_protocols::xdg::shell::server::xdg_toplevel::State as XdgState,
wayland_server::{backend::ObjectId, protocol::wl_surface::WlSurface},
},
render_elements, space_elements,
utils::{IsAlive, Logical, Physical, Point, Rectangle, Scale, Serial, Size},
wayland::{
compositor::{with_states, with_surface_tree_downward, TraversalAction},
seat::WaylandFocus,
shell::xdg::XdgToplevelSurfaceRoleAttributes,
},
};
use std::{
hash::Hash,
sync::{Arc, Mutex},
};
pub mod stack;
pub use self::stack::CosmicStack;
pub mod window;
pub use self::window::CosmicWindow;
use super::focus::FocusDirection;
space_elements! {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
CosmicMappedInternal;
Window=CosmicWindow,
Stack=CosmicStack,
}
#[derive(Debug, Clone)]
pub struct CosmicMapped {
element: CosmicMappedInternal,
// associated data
//tiling
pub(super) tiling_node_id: Arc<Mutex<Option<NodeId>>>,
//floating
pub(super) last_geometry: Arc<Mutex<Option<Rectangle<i32, Logical>>>>,
}
impl PartialEq for CosmicMapped {
fn eq(&self, other: &Self) -> bool {
self.element == other.element
}
}
impl Eq for CosmicMapped {}
impl Hash for CosmicMapped {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.element.hash(state)
}
}
impl CosmicMapped {
pub fn windows(&self) -> impl Iterator<Item = (Window, Point<i32, Logical>)> + '_ {
match &self.element {
CosmicMappedInternal::Stack(stack) => Box::new(stack.windows().map(|w| {
(
w,
stack
.header
.lock()
.unwrap()
.as_ref()
.map(|header| Point::from((0, header.height() as i32)))
.unwrap_or(Point::from((0, 0))),
)
}))
as Box<dyn Iterator<Item = (Window, Point<i32, Logical>)>>,
CosmicMappedInternal::Window(window) => Box::new(std::iter::once((
window.window.clone(),
window
.header
.lock()
.unwrap()
.as_ref()
.map(|header| Point::from((0, header.height() as i32)))
.unwrap_or(Point::from((0, 0))),
))),
_ => Box::new(std::iter::empty()),
}
}
pub fn active_window(&self) -> Window {
match &self.element {
CosmicMappedInternal::Stack(stack) => stack.active(),
CosmicMappedInternal::Window(win) => win.window.clone(),
_ => unreachable!(),
}
}
pub fn focus_window(&self, window: &Window) {
match &self.element {
CosmicMappedInternal::Stack(stack) => stack.set_active(window),
_ => {}
}
}
pub fn has_surface(&self, surface: &WlSurface, surface_type: WindowSurfaceType) -> bool {
self.windows().any(|(w, _)| {
let toplevel = w.toplevel().wl_surface();
if surface_type.contains(WindowSurfaceType::TOPLEVEL) {
if toplevel == surface {
return true;
}
}
if surface_type.contains(WindowSurfaceType::SUBSURFACE) {
use std::sync::atomic::{AtomicBool, Ordering};
let found = AtomicBool::new(false);
with_surface_tree_downward(
toplevel,
surface,
|_, _, search| TraversalAction::DoChildren(search),
|s, _, search| {
found.fetch_or(s == *search, Ordering::SeqCst);
},
|_, _, _| !found.load(Ordering::SeqCst),
);
if found.load(Ordering::SeqCst) {
return true;
}
}
if surface_type.contains(WindowSurfaceType::POPUP) {
PopupManager::popups_for_surface(toplevel).any(|(p, _)| p.wl_surface() == surface)
} else {
false
}
})
}
pub fn handle_focus(&self, direction: FocusDirection) -> bool {
if let CosmicMappedInternal::Stack(stack) = &self.element {
//TODO: stack.handle_focus(direction)
false
} else {
false
}
}
pub fn set_tiled(&self, tiled: bool) {
for toplevel in match &self.element {
// we use the tiled state of stack windows anyway to get rid of decorations
CosmicMappedInternal::Stack(s) => None,
CosmicMappedInternal::Window(w) => Some(w.window.toplevel()),
_ => unreachable!(),
} {
match toplevel {
Kind::Xdg(xdg) => xdg.with_pending_state(|state| {
if tiled {
state.states.set(XdgState::TiledLeft);
state.states.set(XdgState::TiledRight);
state.states.set(XdgState::TiledTop);
state.states.set(XdgState::TiledBottom);
} else {
state.states.unset(XdgState::TiledLeft);
state.states.unset(XdgState::TiledRight);
state.states.unset(XdgState::TiledTop);
state.states.unset(XdgState::TiledBottom);
}
}),
// Kind::X11?
};
}
}
pub fn is_tiled(&self) -> bool {
let window = match &self.element {
CosmicMappedInternal::Stack(s) => s.active(),
CosmicMappedInternal::Window(w) => w.window.clone(),
_ => unreachable!(),
};
match window.toplevel() {
Kind::Xdg(xdg) => xdg.current_state().states.contains(XdgState::TiledLeft),
// Kind::X11?
}
}
pub fn set_fullscreen(&self, fullscreen: bool) {
for window in match &self.element {
CosmicMappedInternal::Stack(s) => {
Box::new(s.windows()) as Box<dyn Iterator<Item = Window>>
}
CosmicMappedInternal::Window(w) => Box::new(std::iter::once(w.window.clone())),
_ => unreachable!(),
} {
match window.toplevel() {
Kind::Xdg(xdg) => xdg.with_pending_state(|state| {
if fullscreen {
state.states.set(XdgState::Fullscreen);
} else {
state.states.unset(XdgState::Fullscreen);
}
}),
// Kind::X11?
};
}
}
pub fn is_fullscreen(&self) -> bool {
let window = match &self.element {
CosmicMappedInternal::Stack(s) => s.active(),
CosmicMappedInternal::Window(w) => w.window.clone(),
_ => unreachable!(),
};
match window.toplevel() {
Kind::Xdg(xdg) => xdg.current_state().states.contains(XdgState::Fullscreen),
// Kind::X11?
}
}
pub fn set_maximized(&self, maximized: bool) {
for window in match &self.element {
CosmicMappedInternal::Stack(s) => {
Box::new(s.windows()) as Box<dyn Iterator<Item = Window>>
}
CosmicMappedInternal::Window(w) => Box::new(std::iter::once(w.window.clone())),
_ => unreachable!(),
} {
match window.toplevel() {
Kind::Xdg(xdg) => xdg.with_pending_state(|state| {
if maximized {
state.states.set(XdgState::Maximized);
} else {
state.states.unset(XdgState::Maximized);
}
}),
// Kind::X11?
};
}
}
pub fn is_maximized(&self) -> bool {
let window = match &self.element {
CosmicMappedInternal::Stack(s) => s.active(),
CosmicMappedInternal::Window(w) => w.window.clone(),
_ => unreachable!(),
};
match window.toplevel() {
Kind::Xdg(xdg) => xdg.current_state().states.contains(XdgState::Maximized),
// Kind::X11?
}
}
pub fn set_activated(&self, activated: bool) {
for window in match &self.element {
CosmicMappedInternal::Stack(s) => {
Box::new(s.windows()) as Box<dyn Iterator<Item = Window>>
}
CosmicMappedInternal::Window(w) => Box::new(std::iter::once(w.window.clone())),
_ => unreachable!(),
} {
match window.toplevel() {
Kind::Xdg(xdg) => xdg.with_pending_state(|state| {
if activated {
state.states.set(XdgState::Activated);
} else {
state.states.unset(XdgState::Activated);
}
}),
// Kind::X11?
};
}
}
pub fn is_activated(&self) -> bool {
let window = match &self.element {
CosmicMappedInternal::Stack(s) => s.active(),
CosmicMappedInternal::Window(w) => w.window.clone(),
_ => unreachable!(),
};
match window.toplevel() {
Kind::Xdg(xdg) => xdg.current_state().states.contains(XdgState::Activated),
// Kind::X11?
}
}
pub fn set_size(&self, size: Size<i32, Logical>) {
match &self.element {
CosmicMappedInternal::Stack(s) => s.set_size(size),
CosmicMappedInternal::Window(w) => w.set_size(size),
_ => {}
}
}
pub fn min_size(&self) -> Size<i32, Logical> {
match &self.element {
CosmicMappedInternal::Stack(stack) => stack
.windows()
.fold(None, |min_size, window| {
let win_min_size = with_states(window.toplevel().wl_surface(), |states| {
let attrs = states
.data_map
.get::<Mutex<XdgToplevelSurfaceRoleAttributes>>()
.unwrap()
.lock()
.unwrap();
attrs.min_size
});
match (min_size, win_min_size) {
(None, x) => Some(x),
(Some(min1), min2) => Some((min1.w.max(min2.w), min1.h.max(min2.h)).into()),
}
})
.expect("Empty stack?"),
CosmicMappedInternal::Window(window) => {
with_states(window.window.toplevel().wl_surface(), |states| {
let attrs = states
.data_map
.get::<Mutex<XdgToplevelSurfaceRoleAttributes>>()
.unwrap()
.lock()
.unwrap();
attrs.min_size
})
}
_ => unreachable!(),
}
}
pub fn max_size(&self) -> Size<i32, Logical> {
match &self.element {
CosmicMappedInternal::Stack(stack) => {
let theoretical_max = stack.windows().fold(None, |max_size, window| {
let win_max_size = with_states(window.toplevel().wl_surface(), |states| {
let attrs = states
.data_map
.get::<Mutex<XdgToplevelSurfaceRoleAttributes>>()
.unwrap()
.lock()
.unwrap();
attrs.max_size
});
match (max_size, win_max_size) {
(None, x) => Some(x),
(Some(max1), max2) => Some(
(
if max1.w == 0 {
max2.w
} else if max2.w == 0 {
max1.w
} else {
max1.w.min(max2.w)
},
if max1.h == 0 {
max2.h
} else if max2.h == 0 {
max1.h
} else {
max1.h.min(max2.h)
},
)
.into(),
),
}
});
// The problem is, with accumulated sizes, the minimum size could be larger than our maximum...
let min_size = self.min_size();
match (theoretical_max, min_size) {
(None, _) => (0, 0).into(),
(Some(max), min) => (max.w.max(min.w), max.h.max(min.h)).into(),
}
}
CosmicMappedInternal::Window(window) => {
with_states(window.window.toplevel().wl_surface(), |states| {
let attrs = states
.data_map
.get::<Mutex<XdgToplevelSurfaceRoleAttributes>>()
.unwrap()
.lock()
.unwrap();
attrs.max_size
})
}
_ => unreachable!(),
}
}
pub fn configure(&self) {
for window in match &self.element {
CosmicMappedInternal::Stack(s) => {
Box::new(s.windows()) as Box<dyn Iterator<Item = Window>>
}
CosmicMappedInternal::Window(w) => Box::new(std::iter::once(w.window.clone())),
_ => unreachable!(),
} {
match window.toplevel() {
Kind::Xdg(xdg) => xdg.send_configure(),
// Kind::X11?
};
}
}
pub fn send_close(&self) {
let window = match &self.element {
CosmicMappedInternal::Stack(s) => s.active(),
CosmicMappedInternal::Window(w) => w.window.clone(),
_ => unreachable!(),
};
match window.toplevel() {
Kind::Xdg(xdg) => xdg.send_close(),
// Kind::X11?
};
}
}
impl IsAlive for CosmicMapped {
fn alive(&self) -> bool {
self.element.alive()
}
}
impl SpaceElement for CosmicMapped {
fn bbox(&self) -> Rectangle<i32, Logical> {
SpaceElement::bbox(&self.element)
}
fn is_in_input_region(&self, point: &Point<f64, Logical>) -> bool {
SpaceElement::is_in_input_region(&self.element, point)
}
fn set_activate(&self, activated: bool) {
SpaceElement::set_activate(&self.element, activated)
}
fn output_enter(&self, output: &Output, overlap: Rectangle<i32, Logical>) {
SpaceElement::output_enter(&self.element, output, overlap)
}
fn output_leave(&self, output: &Output) {
SpaceElement::output_leave(&self.element, output)
}
fn geometry(&self) -> Rectangle<i32, Logical> {
SpaceElement::geometry(&self.element)
}
fn z_index(&self) -> u8 {
SpaceElement::z_index(&self.element)
}
fn refresh(&self) {
SpaceElement::refresh(&self.element)
}
}
impl KeyboardTarget<State> for CosmicMapped {
fn enter(
&self,
seat: &Seat<State>,
data: &mut State,
keys: Vec<KeysymHandle<'_>>,
serial: Serial,
) {
match &self.element {
CosmicMappedInternal::Stack(s) => KeyboardTarget::enter(s, seat, data, keys, serial),
CosmicMappedInternal::Window(w) => KeyboardTarget::enter(w, seat, data, keys, serial),
_ => {}
}
}
fn leave(&self, seat: &Seat<State>, data: &mut State, serial: Serial) {
match &self.element {
CosmicMappedInternal::Stack(s) => KeyboardTarget::leave(s, seat, data, serial),
CosmicMappedInternal::Window(w) => KeyboardTarget::leave(w, seat, data, serial),
_ => {}
}
}
fn key(
&self,
seat: &Seat<State>,
data: &mut State,
key: KeysymHandle<'_>,
state: KeyState,
serial: Serial,
time: u32,
) {
match &self.element {
CosmicMappedInternal::Stack(s) => {
KeyboardTarget::key(s, seat, data, key, state, serial, time)
}
CosmicMappedInternal::Window(w) => {
KeyboardTarget::key(w, seat, data, key, state, serial, time)
}
_ => {}
}
}
fn modifiers(
&self,
seat: &Seat<State>,
data: &mut State,
modifiers: ModifiersState,
serial: Serial,
) {
match &self.element {
CosmicMappedInternal::Stack(s) => {
KeyboardTarget::modifiers(s, seat, data, modifiers, serial)
}
CosmicMappedInternal::Window(w) => {
KeyboardTarget::modifiers(w, seat, data, modifiers, serial)
}
_ => {}
}
}
}
impl PointerTarget<State> for CosmicMapped {
fn enter(&self, seat: &Seat<State>, data: &mut State, event: &MotionEvent) {
match &self.element {
CosmicMappedInternal::Stack(s) => PointerTarget::enter(s, seat, data, event),
CosmicMappedInternal::Window(w) => PointerTarget::enter(w, seat, data, event),
_ => {}
}
}
fn motion(&self, seat: &Seat<State>, data: &mut State, event: &MotionEvent) {
match &self.element {
CosmicMappedInternal::Stack(s) => PointerTarget::motion(s, seat, data, event),
CosmicMappedInternal::Window(w) => PointerTarget::motion(w, seat, data, event),
_ => {}
}
}
fn button(&self, seat: &Seat<State>, data: &mut State, event: &ButtonEvent) {
match &self.element {
CosmicMappedInternal::Stack(s) => PointerTarget::button(s, seat, data, event),
CosmicMappedInternal::Window(w) => PointerTarget::button(w, seat, data, event),
_ => {}
}
}
fn axis(&self, seat: &Seat<State>, data: &mut State, frame: AxisFrame) {
match &self.element {
CosmicMappedInternal::Stack(s) => PointerTarget::axis(s, seat, data, frame),
CosmicMappedInternal::Window(w) => PointerTarget::axis(w, seat, data, frame),
_ => {}
}
}
fn leave(&self, seat: &Seat<State>, data: &mut State, serial: Serial, time: u32) {
match &self.element {
CosmicMappedInternal::Stack(s) => PointerTarget::leave(s, seat, data, serial, time),
CosmicMappedInternal::Window(w) => PointerTarget::leave(w, seat, data, serial, time),
_ => {}
}
}
}
impl WaylandFocus for CosmicMapped {
fn wl_surface(&self) -> Option<WlSurface> {
match &self.element {
CosmicMappedInternal::Window(w) => w.window.wl_surface().clone(),
CosmicMappedInternal::Stack(s) => s.active().wl_surface().clone(),
_ => None,
}
}
fn same_client_as(&self, object_id: &ObjectId) -> bool {
match &self.element {
CosmicMappedInternal::Window(w) => w.window.same_client_as(object_id),
CosmicMappedInternal::Stack(s) => s.windows().any(|w| w.same_client_as(object_id)),
_ => false,
}
}
}
impl From<CosmicWindow> for CosmicMapped {
fn from(w: CosmicWindow) -> Self {
CosmicMapped {
element: CosmicMappedInternal::Window(w),
tiling_node_id: Arc::new(Mutex::new(None)),
last_geometry: Arc::new(Mutex::new(None)),
}
}
}
impl From<CosmicStack> for CosmicMapped {
fn from(s: CosmicStack) -> Self {
CosmicMapped {
element: CosmicMappedInternal::Stack(s),
tiling_node_id: Arc::new(Mutex::new(None)),
last_geometry: Arc::new(Mutex::new(None)),
}
}
}
render_elements! {
pub CosmicMappedRenderElement<R> where R: ImportAll;
Stack=self::stack::CosmicStackRenderElement<R>,
Window=self::window::CosmicWindowRenderElement<R>,
}
impl<R> AsRenderElements<R> for CosmicMapped
where
R: Renderer + ImportAll,
<R as Renderer>::TextureId: 'static,
{
type RenderElement = CosmicMappedRenderElement<R>;
fn render_elements<C: From<Self::RenderElement>>(
&self,
location: Point<i32, Physical>,
scale: Scale<f64>,
) -> Vec<C> {
match &self.element {
CosmicMappedInternal::Stack(s) => AsRenderElements::<R>::render_elements::<
CosmicMappedRenderElement<R>,
>(s, location, scale),
CosmicMappedInternal::Window(w) => AsRenderElements::<R>::render_elements::<
CosmicMappedRenderElement<R>,
>(w, location, scale),
_ => Vec::new(),
}
.into_iter()
.map(C::from)
.collect()
}
}

293
src/shell/element/stack.rs Normal file
View file

@ -0,0 +1,293 @@
use crate::state::State;
use smithay::{
backend::{
input::KeyState,
renderer::{
element::{surface::WaylandSurfaceRenderElement, AsRenderElements},
ImportAll, Renderer,
},
},
desktop::{space::SpaceElement, Kind, Window},
input::{
keyboard::{KeyboardTarget, KeysymHandle, ModifiersState},
pointer::{AxisFrame, ButtonEvent, MotionEvent, PointerTarget},
Seat,
},
output::Output,
render_elements,
utils::{IsAlive, Logical, Physical, Point, Rectangle, Scale, Serial, Size},
};
use std::{
hash::Hash,
sync::{
atomic::{AtomicUsize, Ordering},
Arc, Mutex,
},
};
#[derive(Debug, Clone)]
pub struct CosmicStack {
windows: Arc<Mutex<Vec<Window>>>,
active: Arc<AtomicUsize>,
pub(super) header: Arc<Mutex<Option<HeaderBar>>>,
}
impl PartialEq for CosmicStack {
fn eq(&self, other: &Self) -> bool {
*self.windows.lock().unwrap() == *other.windows.lock().unwrap()
}
}
impl Eq for CosmicStack {}
impl Hash for CosmicStack {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
Arc::as_ptr(&self.windows).hash(state)
}
}
#[derive(Debug)]
pub struct HeaderBar {}
impl HeaderBar {
pub fn height(&self) -> i32 {
0
}
}
impl CosmicStack {
pub fn active(&self) -> Window {
self.windows.lock().unwrap()[self.active.load(Ordering::SeqCst)].clone()
}
pub fn set_active(&self, window: &Window) {
if let Some(val) = self
.windows
.lock()
.unwrap()
.iter()
.position(|w| w == window)
{
self.active.store(val, Ordering::SeqCst)
}
}
pub fn set_size(&self, size: Size<i32, Logical>) {
let surface_size = (
size.w,
size.h
- self
.header
.lock()
.unwrap()
.as_ref()
.map(|h| h.height())
.unwrap_or(0),
)
.into();
for window in self.windows.lock().unwrap().iter() {
match window.toplevel() {
Kind::Xdg(xdg) => xdg.with_pending_state(|state| state.size = Some(surface_size)),
};
}
}
}
impl IsAlive for CosmicStack {
fn alive(&self) -> bool {
self.windows.lock().unwrap().iter().any(IsAlive::alive)
}
}
impl SpaceElement for CosmicStack {
fn bbox(&self) -> Rectangle<i32, Logical> {
SpaceElement::bbox(&self.windows.lock().unwrap()[self.active.load(Ordering::SeqCst)])
}
fn is_in_input_region(&self, point: &Point<f64, Logical>) -> bool {
SpaceElement::is_in_input_region(
&self.windows.lock().unwrap()[self.active.load(Ordering::SeqCst)],
point,
)
}
fn set_activate(&self, activated: bool) {
self.windows
.lock()
.unwrap()
.iter()
.for_each(|w| SpaceElement::set_activate(w, activated))
}
fn output_enter(&self, output: &Output, overlap: Rectangle<i32, Logical>) {
self.windows
.lock()
.unwrap()
.iter()
.for_each(|w| SpaceElement::output_enter(w, output, overlap))
}
fn output_leave(&self, output: &Output) {
self.windows
.lock()
.unwrap()
.iter()
.for_each(|w| SpaceElement::output_leave(w, output))
}
fn geometry(&self) -> Rectangle<i32, Logical> {
SpaceElement::geometry(&self.windows.lock().unwrap()[self.active.load(Ordering::SeqCst)])
}
fn z_index(&self) -> u8 {
SpaceElement::z_index(&self.windows.lock().unwrap()[self.active.load(Ordering::SeqCst)])
}
fn refresh(&self) {
let mut windows = self.windows.lock().unwrap();
windows.retain(IsAlive::alive);
let len = windows.len();
let _ = self
.active
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |active| {
(active > len).then_some(len - 1)
});
windows.iter().for_each(|w| SpaceElement::refresh(w))
}
}
impl KeyboardTarget<State> for CosmicStack {
fn enter(
&self,
seat: &Seat<State>,
data: &mut State,
keys: Vec<KeysymHandle<'_>>,
serial: Serial,
) {
KeyboardTarget::enter(
&self.windows.lock().unwrap()[self.active.load(Ordering::SeqCst)],
seat,
data,
keys,
serial,
)
}
fn leave(&self, seat: &Seat<State>, data: &mut State, serial: Serial) {
KeyboardTarget::leave(
&self.windows.lock().unwrap()[self.active.load(Ordering::SeqCst)],
seat,
data,
serial,
)
}
fn key(
&self,
seat: &Seat<State>,
data: &mut State,
key: KeysymHandle<'_>,
state: KeyState,
serial: Serial,
time: u32,
) {
KeyboardTarget::key(
&self.windows.lock().unwrap()[self.active.load(Ordering::SeqCst)],
seat,
data,
key,
state,
serial,
time,
)
}
fn modifiers(
&self,
seat: &Seat<State>,
data: &mut State,
modifiers: ModifiersState,
serial: Serial,
) {
KeyboardTarget::modifiers(
&self.windows.lock().unwrap()[self.active.load(Ordering::SeqCst)],
seat,
data,
modifiers,
serial,
)
}
}
impl PointerTarget<State> for CosmicStack {
fn enter(&self, seat: &Seat<State>, data: &mut State, event: &MotionEvent) {
PointerTarget::enter(
&self.windows.lock().unwrap()[self.active.load(Ordering::SeqCst)],
seat,
data,
event,
)
}
fn motion(&self, seat: &Seat<State>, data: &mut State, event: &MotionEvent) {
PointerTarget::motion(
&self.windows.lock().unwrap()[self.active.load(Ordering::SeqCst)],
seat,
data,
event,
)
}
fn button(&self, seat: &Seat<State>, data: &mut State, event: &ButtonEvent) {
PointerTarget::button(
&self.windows.lock().unwrap()[self.active.load(Ordering::SeqCst)],
seat,
data,
event,
)
}
fn axis(&self, seat: &Seat<State>, data: &mut State, frame: AxisFrame) {
PointerTarget::axis(
&self.windows.lock().unwrap()[self.active.load(Ordering::SeqCst)],
seat,
data,
frame,
)
}
fn leave(&self, seat: &Seat<State>, data: &mut State, serial: Serial, time: u32) {
PointerTarget::leave(
&self.windows.lock().unwrap()[self.active.load(Ordering::SeqCst)],
seat,
data,
serial,
time,
)
}
}
render_elements! {
pub CosmicStackRenderElement<R> where R: ImportAll;
Window=WaylandSurfaceRenderElement,
}
impl<R> AsRenderElements<R> for CosmicStack
where
R: Renderer + ImportAll,
<R as Renderer>::TextureId: 'static,
{
type RenderElement = CosmicStackRenderElement<R>;
fn render_elements<C: From<Self::RenderElement>>(
&self,
location: Point<i32, Physical>,
scale: Scale<f64>,
) -> Vec<C> {
AsRenderElements::<R>::render_elements::<CosmicStackRenderElement<R>>(
&self.windows.lock().unwrap()[self.active.load(Ordering::SeqCst)],
location,
scale,
)
.into_iter()
.map(C::from)
.collect()
}
}
impl CosmicStack {
pub fn windows(&self) -> impl Iterator<Item = Window> {
self.windows
.lock()
.unwrap()
.iter()
.cloned()
.collect::<Vec<_>>()
.into_iter()
}
}

231
src/shell/element/window.rs Normal file
View file

@ -0,0 +1,231 @@
use crate::state::State;
use smithay::{
backend::{
input::KeyState,
renderer::{
element::{surface::WaylandSurfaceRenderElement, AsRenderElements},
ImportAll, Renderer,
},
},
desktop::{space::SpaceElement, Kind, Window},
input::{
keyboard::{KeyboardTarget, KeysymHandle, ModifiersState},
pointer::{AxisFrame, ButtonEvent, MotionEvent, PointerTarget},
Seat,
},
output::Output,
reexports::wayland_protocols::xdg::decoration::zv1::server::zxdg_toplevel_decoration_v1::Mode as DecorationMode,
render_elements,
utils::{IsAlive, Logical, Physical, Point, Rectangle, Scale, Serial, Size},
wayland::shell::xdg::ToplevelSurface,
};
use std::{
hash::Hash,
sync::{Arc, Mutex},
};
#[derive(Debug, Clone)]
pub struct CosmicWindow {
pub(super) window: Window,
pub(super) header: Arc<Mutex<Option<HeaderBar>>>,
}
impl PartialEq<Window> for CosmicWindow {
fn eq(&self, other: &Window) -> bool {
&self.window == other
}
}
impl PartialEq<CosmicWindow> for Window {
fn eq(&self, other: &CosmicWindow) -> bool {
self == &other.window
}
}
impl PartialEq for CosmicWindow {
fn eq(&self, other: &Self) -> bool {
self.window == other.window
}
}
impl Eq for CosmicWindow {}
impl Hash for CosmicWindow {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.window.hash(state)
}
}
impl CosmicWindow {
pub fn set_size(&self, size: Size<i32, Logical>) {
let surface_size = (
size.w,
size.h
- self
.header
.lock()
.unwrap()
.as_ref()
.map(|h| h.height())
.unwrap_or(0),
)
.into();
match self.window.toplevel() {
Kind::Xdg(xdg) => xdg.with_pending_state(|state| state.size = Some(surface_size)),
};
}
}
impl From<Window> for CosmicWindow {
fn from(window: Window) -> Self {
let is_ssd = matches!(
match window.toplevel() {
Kind::Xdg(xdg) => xdg.current_state().decoration_mode,
},
Some(DecorationMode::ServerSide)
);
CosmicWindow {
window,
header: Arc::new(Mutex::new(is_ssd.then_some(HeaderBar::default()))),
}
}
}
impl From<ToplevelSurface> for CosmicWindow {
fn from(surf: ToplevelSurface) -> Self {
let is_ssd = matches!(
surf.current_state().decoration_mode,
Some(DecorationMode::ServerSide)
);
CosmicWindow {
window: Window::new(Kind::Xdg(surf)),
header: Arc::new(Mutex::new(is_ssd.then_some(HeaderBar::default()))),
}
}
}
#[derive(Debug, Default, PartialEq)]
pub(super) struct HeaderBar {
pointer_loc: Option<Point<f64, Logical>>,
close_button_hover: bool,
maximize_button_hover: bool,
}
impl HeaderBar {
pub fn height(&self) -> i32 {
0
}
}
impl IsAlive for CosmicWindow {
fn alive(&self) -> bool {
self.window.alive()
}
}
impl SpaceElement for CosmicWindow {
fn bbox(&self) -> Rectangle<i32, Logical> {
SpaceElement::bbox(&self.window)
}
fn is_in_input_region(&self, point: &Point<f64, Logical>) -> bool {
SpaceElement::is_in_input_region(&self.window, point)
}
fn set_activate(&self, activated: bool) {
SpaceElement::set_activate(&self.window, activated)
}
fn output_enter(&self, output: &Output, overlap: Rectangle<i32, Logical>) {
SpaceElement::output_enter(&self.window, output, overlap)
}
fn output_leave(&self, output: &Output) {
SpaceElement::output_leave(&self.window, output)
}
fn geometry(&self) -> Rectangle<i32, Logical> {
SpaceElement::geometry(&self.window)
}
fn z_index(&self) -> u8 {
SpaceElement::z_index(&self.window)
}
fn refresh(&self) {
SpaceElement::refresh(&self.window)
}
}
impl KeyboardTarget<State> for CosmicWindow {
fn enter(
&self,
seat: &Seat<State>,
data: &mut State,
keys: Vec<KeysymHandle<'_>>,
serial: Serial,
) {
KeyboardTarget::enter(&self.window, seat, data, keys, serial)
}
fn leave(&self, seat: &Seat<State>, data: &mut State, serial: Serial) {
KeyboardTarget::leave(&self.window, seat, data, serial)
}
fn key(
&self,
seat: &Seat<State>,
data: &mut State,
key: KeysymHandle<'_>,
state: KeyState,
serial: Serial,
time: u32,
) {
KeyboardTarget::key(&self.window, seat, data, key, state, serial, time)
}
fn modifiers(
&self,
seat: &Seat<State>,
data: &mut State,
modifiers: ModifiersState,
serial: Serial,
) {
KeyboardTarget::modifiers(&self.window, seat, data, modifiers, serial)
}
}
impl PointerTarget<State> for CosmicWindow {
fn enter(&self, seat: &Seat<State>, data: &mut State, event: &MotionEvent) {
PointerTarget::enter(&self.window, seat, data, event)
}
fn motion(&self, seat: &Seat<State>, data: &mut State, event: &MotionEvent) {
PointerTarget::motion(&self.window, seat, data, event)
}
fn button(&self, seat: &Seat<State>, data: &mut State, event: &ButtonEvent) {
PointerTarget::button(&self.window, seat, data, event)
}
fn axis(&self, seat: &Seat<State>, data: &mut State, frame: AxisFrame) {
PointerTarget::axis(&self.window, seat, data, frame)
}
fn leave(&self, seat: &Seat<State>, data: &mut State, serial: Serial, time: u32) {
PointerTarget::leave(&self.window, seat, data, serial, time)
}
}
render_elements! {
pub CosmicWindowRenderElement<R> where R: ImportAll;
Window=WaylandSurfaceRenderElement,
}
impl<R> AsRenderElements<R> for CosmicWindow
where
R: Renderer + ImportAll,
<R as Renderer>::TextureId: 'static,
{
type RenderElement = CosmicWindowRenderElement<R>;
fn render_elements<C: From<Self::RenderElement>>(
&self,
location: Point<i32, Physical>,
scale: Scale<f64>,
) -> Vec<C> {
AsRenderElements::<R>::render_elements::<CosmicWindowRenderElement<R>>(
&self.window,
location - self.window.geometry().loc.to_physical_precise_round(scale),
scale,
)
.into_iter()
.map(C::from)
.collect()
}
}

View file

@ -1,24 +1,18 @@
use crate::{
shell::{OutputBoundState, Shell, Workspace, WorkspaceMode},
shell::{element::CosmicMapped, Shell, Workspace},
state::Common,
utils::prelude::*,
wayland::handlers::xdg_shell::PopupGrabData,
};
use indexmap::IndexSet;
use smithay::{
desktop::{layer_map_for_output, PopupUngrabStrategy, Window, WindowSurfaceType},
desktop::{layer_map_for_output, PopupUngrabStrategy},
input::Seat,
reexports::wayland_server::protocol::wl_surface::WlSurface,
utils::{IsAlive, Serial, SERIAL_COUNTER},
wayland::{
compositor::get_role,
shell::{wlr_layer::LAYER_SURFACE_ROLE, xdg::XDG_TOPLEVEL_ROLE},
},
};
use std::{
cell::{Ref, RefCell, RefMut},
collections::HashMap,
};
use std::cell::RefCell;
use self::target::{KeyboardFocusTarget, WindowGroup};
pub mod target;
@ -32,80 +26,59 @@ pub enum FocusDirection {
Out,
}
pub struct FocusStack<'a>(Ref<'a, IndexSet<Window>>);
pub struct FocusStackMut<'a>(RefMut<'a, IndexSet<Window>>);
pub struct FocusStack<'a>(pub(super) Option<&'a IndexSet<CosmicMapped>>);
pub struct FocusStackMut<'a>(pub(super) &'a mut IndexSet<CosmicMapped>);
impl<'a> FocusStack<'a> {
pub fn last(&self) -> Option<Window> {
self.0.iter().rev().find(|w| w.toplevel().alive()).cloned()
pub fn last(&self) -> Option<&CosmicMapped> {
self.0
.as_ref()
.and_then(|set| set.iter().rev().find(|w| w.alive()))
}
pub fn iter(&self) -> impl Iterator<Item = &'_ Window> {
self.0.iter().rev().filter(|w| w.toplevel().alive())
pub fn iter(&self) -> impl Iterator<Item = &'_ CosmicMapped> {
self.0
.iter()
.flat_map(|set| set.iter().rev().filter(|w| w.alive()))
}
}
impl<'a> FocusStackMut<'a> {
pub fn append(&mut self, window: &Window) {
self.0.retain(|w| w.toplevel().alive());
pub fn append(&mut self, window: &CosmicMapped) {
self.0.retain(|w| w.alive());
self.0.shift_remove(window);
self.0.insert(window.clone());
}
pub fn last(&self) -> Option<Window> {
self.0.iter().rev().find(|w| w.toplevel().alive()).cloned()
pub fn last(&self) -> Option<&CosmicMapped> {
self.0.iter().rev().find(|w| w.alive())
}
pub fn iter(&self) -> impl Iterator<Item = &'_ Window> {
self.0.iter().rev().filter(|w| w.toplevel().alive())
pub fn iter(&self) -> impl Iterator<Item = &'_ CosmicMapped> {
self.0.iter().rev().filter(|w| w.alive())
}
}
type FocusStackData = RefCell<(HashMap<u8, IndexSet<Window>>, IndexSet<Window>)>;
impl Workspace {}
impl Workspace {
pub fn focus_stack<'a, 'b>(&'b self, seat: &'a Seat<State>) -> FocusStack<'a> {
seat.user_data()
.insert_if_missing(|| FocusStackData::new((HashMap::new(), IndexSet::new())));
let idx = self.idx;
FocusStack(Ref::map(
seat.user_data().get::<FocusStackData>().unwrap().borrow(),
|map| map.0.get(&idx).unwrap_or(&map.1), //TODO: workaround until Ref::filter_map goes stable
))
}
pub fn focus_stack_mut<'a, 'b>(&'b self, seat: &'a Seat<State>) -> FocusStackMut<'a> {
seat.user_data()
.insert_if_missing(|| FocusStackData::new((HashMap::new(), IndexSet::new())));
let idx = self.idx;
FocusStackMut(RefMut::map(
seat.user_data()
.get::<FocusStackData>()
.unwrap()
.borrow_mut(),
|map| map.0.entry(idx).or_insert_with(|| IndexSet::new()),
))
}
}
pub struct ActiveFocus(RefCell<Option<WlSurface>>);
pub struct ActiveFocus(RefCell<Option<KeyboardFocusTarget>>);
impl ActiveFocus {
fn set(seat: &Seat<State>, surface: Option<WlSurface>) {
fn set(seat: &Seat<State>, target: Option<KeyboardFocusTarget>) {
if !seat
.user_data()
.insert_if_missing(|| ActiveFocus(RefCell::new(surface.clone())))
.insert_if_missing(|| ActiveFocus(RefCell::new(target.clone())))
{
*seat
.user_data()
.get::<ActiveFocus>()
.unwrap()
.0
.borrow_mut() = surface;
.borrow_mut() = target;
}
}
fn get(seat: &Seat<State>) -> Option<WlSurface> {
fn get(seat: &Seat<State>) -> Option<KeyboardFocusTarget> {
seat.user_data()
.get::<ActiveFocus>()
.and_then(|a| a.0.borrow().clone())
@ -115,30 +88,25 @@ impl ActiveFocus {
impl Shell {
pub fn set_focus<'a>(
state: &mut State,
surface: Option<&WlSurface>,
target: Option<&KeyboardFocusTarget>,
active_seat: &Seat<State>,
serial: Option<Serial>,
) {
// update FocusStack and notify layouts about new focus (if any window)
if let Some(surface) = surface {
if let Some(workspace) = state.common.shell.space_for_window_mut(surface) {
if let Some(window) = workspace
.space
.window_for_surface(surface, WindowSurfaceType::ALL)
{
let mut focus_stack = workspace.focus_stack_mut(active_seat);
if Some(window) != focus_stack.last().as_ref() {
slog_scope::debug!("Focusing window: {:?}", window);
focus_stack.append(window);
// also remove popup grabs, if we are switching focus
if let Some(mut popup_grab) = active_seat
.user_data()
.get::<PopupGrabData>()
.and_then(|x| x.take())
{
if !popup_grab.has_ended() {
popup_grab.ungrab(PopupUngrabStrategy::All);
}
if let Some(KeyboardFocusTarget::Element(mapped)) = target {
if let Some(workspace) = state.common.shell.space_for_mut(mapped) {
let mut focus_stack = workspace.focus_stack.get_mut(active_seat);
if Some(mapped) != focus_stack.last() {
slog_scope::debug!("Focusing window: {:?}", mapped);
focus_stack.append(mapped);
// also remove popup grabs, if we are switching focus
if let Some(mut popup_grab) = active_seat
.user_data()
.get::<PopupGrabData>()
.and_then(|x| x.take())
{
if !popup_grab.has_ended() {
popup_grab.ungrab(PopupUngrabStrategy::All);
}
}
}
@ -147,10 +115,10 @@ impl Shell {
// update keyboard focus
if let Some(keyboard) = active_seat.get_keyboard() {
ActiveFocus::set(active_seat, surface.cloned());
ActiveFocus::set(active_seat, target.cloned());
keyboard.set_focus(
state,
surface.cloned(),
target.cloned(),
serial.unwrap_or_else(|| SERIAL_COUNTER.next_serial()),
);
}
@ -160,30 +128,23 @@ impl Shell {
// update activate status
let focused_windows = seats
.flat_map(|seat| {
self.outputs
.iter()
.flat_map(|o| self.active_space(o).focus_stack(seat).last().clone())
self.outputs.iter().flat_map(|o| {
let space = self.active_space(o);
let stack = space.focus_stack.get(seat);
stack.last().cloned()
})
})
.collect::<Vec<_>>();
for output in self.outputs.iter() {
let workspace = match &self.workspace_mode {
WorkspaceMode::OutputBound => {
let active = output
.user_data()
.get::<OutputBoundState>()
.unwrap()
.active
.get();
&mut self.spaces[active]
}
WorkspaceMode::Global { active, .. } => &mut self.spaces[*active],
};
let workspace = self.workspaces.active_mut(output);
for focused in focused_windows.iter() {
workspace.space.raise_window(focused, true);
if workspace.floating_layer.mapped().any(|m| m == focused) {
workspace.floating_layer.space.raise_element(focused, true);
}
}
for window in workspace.space.windows() {
window.set_activated(focused_windows.contains(window));
for window in workspace.mapped() {
window.set_activated(focused_windows.contains(&window));
window.configure();
}
}
@ -193,55 +154,50 @@ impl Shell {
impl Common {
pub fn set_focus(
state: &mut State,
surface: Option<&WlSurface>,
target: Option<&KeyboardFocusTarget>,
active_seat: &Seat<State>,
serial: Option<Serial>,
) {
Shell::set_focus(state, surface, active_seat, serial);
state.common.shell.update_active(state.common.seats.iter());
Shell::set_focus(state, target, active_seat, serial);
let seats = state.common.seats().cloned().collect::<Vec<_>>();
state.common.shell.update_active(seats.iter());
}
pub fn refresh_focus(state: &mut State) {
let seats = state.common.seats.clone();
let seats = state.common.seats().cloned().collect::<Vec<_>>();
for seat in seats {
let output = active_output(&seat, &state.common);
let output = seat.active_output();
let last_known_focus = ActiveFocus::get(&seat);
if let Some(surface) = last_known_focus {
if surface.alive() {
let is_toplevel = matches!(get_role(&surface), Some(XDG_TOPLEVEL_ROLE));
let is_layer = matches!(get_role(&surface), Some(LAYER_SURFACE_ROLE));
if let Some(popup) = state.common.shell.popups.find_popup(&surface) {
if popup.alive() {
continue;
}
} else if is_layer {
if layer_map_for_output(&output)
.layer_for_surface(&surface, WindowSurfaceType::ALL)
.is_some()
{
continue; // Focus is valid
}
} else if is_toplevel {
let workspace = state.common.shell.active_space(&output);
if let Some(window) = workspace
.space
.window_for_surface(&surface, WindowSurfaceType::ALL)
{
let focus_stack = workspace.focus_stack(&seat);
if !focus_stack.last().map(|w| &w != window).unwrap_or(true) {
if let Some(target) = last_known_focus {
if target.alive() {
match target {
KeyboardFocusTarget::Element(mapped) => {
let workspace = state.common.shell.active_space(&output);
let focus_stack = workspace.focus_stack.get(&seat);
if focus_stack.last().map(|m| m == &mapped).unwrap_or(false) {
continue; // Focus is valid
} else {
slog_scope::debug!("Wrong Window, focus fixup");
}
} else {
slog_scope::debug!("Different workspaces Window, focus fixup");
}
} else {
// unknown surface type, fixup
slog_scope::debug!("Surface unmapped, focus fixup");
}
KeyboardFocusTarget::LayerSurface(layer) => {
if layer_map_for_output(&output).layers().any(|l| l == &layer) {
continue; // Focus is valid
}
}
KeyboardFocusTarget::Group(WindowGroup {
output: weak_output,
..
}) => {
if weak_output == output {
continue; // Focus is valid,
}
}
KeyboardFocusTarget::Popup(_) | KeyboardFocusTarget::Fullscreen(_) => {
continue;
} // Focus is valid
};
} else {
slog_scope::debug!("Surface dead, focus fixup");
}
@ -263,21 +219,24 @@ impl Common {
}
// update keyboard focus
let surface = state
let target = state
.common
.shell
.active_space(&output)
.focus_stack(&seat)
.focus_stack
.get(&seat)
.last()
.map(|w| w.toplevel().wl_surface().clone());
.cloned()
.map(KeyboardFocusTarget::from);
if let Some(keyboard) = seat.get_keyboard() {
slog_scope::info!("restoring focus to: {:?}", surface.as_ref());
keyboard.set_focus(state, surface.clone(), SERIAL_COUNTER.next_serial());
ActiveFocus::set(&seat, surface);
slog_scope::info!("restoring focus to: {:?}", target.as_ref());
keyboard.set_focus(state, target.clone(), SERIAL_COUNTER.next_serial());
ActiveFocus::set(&seat, target);
}
}
}
state.common.shell.update_active(state.common.seats.iter())
let seats = state.common.seats().cloned().collect::<Vec<_>>();
state.common.shell.update_active(seats.iter())
}
}

View file

@ -1,5 +1,8 @@
use crate::utils::prelude::*;
pub use smithay::{
use std::sync::Weak;
use crate::{shell::element::CosmicMapped, utils::prelude::*};
use id_tree::NodeId;
use smithay::{
backend::input::KeyState,
desktop::{LayerSurface, PopupKind, Window},
input::{
@ -7,83 +10,139 @@ pub use smithay::{
pointer::{AxisFrame, ButtonEvent, MotionEvent, PointerTarget},
Seat,
},
output::WeakOutput,
reexports::wayland_server::{backend::ObjectId, protocol::wl_surface::WlSurface, Resource},
utils::{IsAlive, Serial},
wayland::seat::WaylandFocus,
};
#[derive(Debug, Clone, PartialEq)]
pub enum FocusTarget {
Window(Window),
pub enum PointerFocusTarget {
Element(CosmicMapped),
Fullscreen(Window),
LayerSurface(LayerSurface),
Popup(PopupKind),
}
impl IsAlive for FocusTarget {
fn alive(&self) -> bool {
match self {
FocusTarget::Window(w) => w.alive(),
FocusTarget::LayerSurface(l) => l.alive(),
FocusTarget::Popup(p) => p.alive(),
#[derive(Debug, Clone, PartialEq)]
pub enum KeyboardFocusTarget {
Element(CosmicMapped),
Fullscreen(Window),
Group(WindowGroup),
LayerSurface(LayerSurface),
Popup(PopupKind),
}
impl From<KeyboardFocusTarget> for PointerFocusTarget {
fn from(target: KeyboardFocusTarget) -> Self {
match target {
KeyboardFocusTarget::Element(elem) => PointerFocusTarget::Element(elem),
KeyboardFocusTarget::Fullscreen(elem) => PointerFocusTarget::Fullscreen(elem),
KeyboardFocusTarget::LayerSurface(layer) => PointerFocusTarget::LayerSurface(layer),
KeyboardFocusTarget::Popup(popup) => PointerFocusTarget::Popup(popup),
_ => unreachable!("A window grab cannot start a popup grab"),
}
}
}
impl PointerTarget<State> for FocusTarget {
#[derive(Debug, Clone)]
pub struct WindowGroup {
pub(in crate::shell) node: NodeId,
pub(in crate::shell) output: WeakOutput,
pub(in crate::shell) alive: Weak<()>,
}
impl PartialEq for WindowGroup {
fn eq(&self, other: &Self) -> bool {
self.node == other.node
&& self.output == other.output
&& Weak::ptr_eq(&self.alive, &other.alive)
}
}
impl IsAlive for PointerFocusTarget {
fn alive(&self) -> bool {
match self {
PointerFocusTarget::Element(e) => e.alive(),
PointerFocusTarget::Fullscreen(f) => f.alive(),
PointerFocusTarget::LayerSurface(l) => l.alive(),
PointerFocusTarget::Popup(p) => p.alive(),
}
}
}
impl IsAlive for KeyboardFocusTarget {
fn alive(&self) -> bool {
match self {
KeyboardFocusTarget::Element(e) => e.alive(),
KeyboardFocusTarget::Fullscreen(f) => f.alive(),
KeyboardFocusTarget::Group(g) => g.alive.upgrade().is_some(),
KeyboardFocusTarget::LayerSurface(l) => l.alive(),
KeyboardFocusTarget::Popup(p) => p.alive(),
}
}
}
impl PointerTarget<State> for PointerFocusTarget {
fn enter(&self, seat: &Seat<State>, data: &mut State, event: &MotionEvent) {
match self {
FocusTarget::Window(w) => {
PointerTarget::enter(w.toplevel().wl_surface(), seat, data, event)
PointerFocusTarget::Element(w) => PointerTarget::enter(w, seat, data, event),
PointerFocusTarget::Fullscreen(w) => PointerTarget::enter(w, seat, data, event),
PointerFocusTarget::LayerSurface(l) => {
PointerTarget::enter(l.wl_surface(), seat, data, event)
}
FocusTarget::LayerSurface(l) => PointerTarget::enter(l.wl_surface(), seat, data, event),
FocusTarget::Popup(p) => PointerTarget::enter(p.wl_surface(), seat, data, event),
PointerFocusTarget::Popup(p) => PointerTarget::enter(p.wl_surface(), seat, data, event),
}
}
fn motion(&self, seat: &Seat<State>, data: &mut State, event: &MotionEvent) {
match self {
FocusTarget::Window(w) => {
PointerTarget::motion(w.toplevel().wl_surface(), seat, data, event)
}
FocusTarget::LayerSurface(l) => {
PointerFocusTarget::Element(w) => PointerTarget::motion(w, seat, data, event),
PointerFocusTarget::Fullscreen(w) => PointerTarget::motion(w, seat, data, event),
PointerFocusTarget::LayerSurface(l) => {
PointerTarget::motion(l.wl_surface(), seat, data, event)
}
FocusTarget::Popup(p) => PointerTarget::motion(p.wl_surface(), seat, data, event),
PointerFocusTarget::Popup(p) => {
PointerTarget::motion(p.wl_surface(), seat, data, event)
}
}
}
fn button(&self, seat: &Seat<State>, data: &mut State, event: &ButtonEvent) {
match self {
FocusTarget::Window(w) => {
PointerTarget::button(w.toplevel().wl_surface(), seat, data, event)
}
FocusTarget::LayerSurface(l) => {
PointerFocusTarget::Element(w) => PointerTarget::button(w, seat, data, event),
PointerFocusTarget::Fullscreen(w) => PointerTarget::button(w, seat, data, event),
PointerFocusTarget::LayerSurface(l) => {
PointerTarget::button(l.wl_surface(), seat, data, event)
}
FocusTarget::Popup(p) => PointerTarget::button(p.wl_surface(), seat, data, event),
PointerFocusTarget::Popup(p) => {
PointerTarget::button(p.wl_surface(), seat, data, event)
}
}
}
fn axis(&self, seat: &Seat<State>, data: &mut State, frame: AxisFrame) {
match self {
FocusTarget::Window(w) => {
PointerTarget::axis(w.toplevel().wl_surface(), seat, data, frame)
PointerFocusTarget::Element(w) => PointerTarget::axis(w, seat, data, frame),
PointerFocusTarget::Fullscreen(w) => PointerTarget::axis(w, seat, data, frame),
PointerFocusTarget::LayerSurface(l) => {
PointerTarget::axis(l.wl_surface(), seat, data, frame)
}
FocusTarget::LayerSurface(l) => PointerTarget::axis(l.wl_surface(), seat, data, frame),
FocusTarget::Popup(p) => PointerTarget::axis(p.wl_surface(), seat, data, frame),
PointerFocusTarget::Popup(p) => PointerTarget::axis(p.wl_surface(), seat, data, frame),
}
}
fn leave(&self, seat: &Seat<State>, data: &mut State, serial: Serial, time: u32) {
match self {
FocusTarget::Window(w) => {
PointerTarget::leave(w.toplevel().wl_surface(), seat, data, serial, time)
}
FocusTarget::LayerSurface(l) => {
PointerFocusTarget::Element(w) => PointerTarget::leave(w, seat, data, serial, time),
PointerFocusTarget::Fullscreen(w) => PointerTarget::leave(w, seat, data, serial, time),
PointerFocusTarget::LayerSurface(l) => {
PointerTarget::leave(l.wl_surface(), seat, data, serial, time)
}
FocusTarget::Popup(p) => PointerTarget::leave(p.wl_surface(), seat, data, serial, time),
PointerFocusTarget::Popup(p) => {
PointerTarget::leave(p.wl_surface(), seat, data, serial, time)
}
}
}
}
impl KeyboardTarget<State> for FocusTarget {
impl KeyboardTarget<State> for KeyboardFocusTarget {
fn enter(
&self,
seat: &Seat<State>,
@ -92,26 +151,30 @@ impl KeyboardTarget<State> for FocusTarget {
serial: Serial,
) {
match self {
FocusTarget::Window(w) => {
KeyboardTarget::enter(w.toplevel().wl_surface(), seat, data, keys, serial)
KeyboardFocusTarget::Element(w) => KeyboardTarget::enter(w, seat, data, keys, serial),
KeyboardFocusTarget::Fullscreen(w) => {
KeyboardTarget::enter(w, seat, data, keys, serial)
}
FocusTarget::LayerSurface(l) => {
KeyboardFocusTarget::Group(_) => {}
KeyboardFocusTarget::LayerSurface(l) => {
KeyboardTarget::enter(l.wl_surface(), seat, data, keys, serial)
}
FocusTarget::Popup(p) => {
KeyboardFocusTarget::Popup(p) => {
KeyboardTarget::enter(p.wl_surface(), seat, data, keys, serial)
}
}
}
fn leave(&self, seat: &Seat<State>, data: &mut State, serial: Serial) {
match self {
FocusTarget::Window(w) => {
KeyboardTarget::leave(w.toplevel().wl_surface(), seat, data, serial)
}
FocusTarget::LayerSurface(l) => {
KeyboardFocusTarget::Element(w) => KeyboardTarget::leave(w, seat, data, serial),
KeyboardFocusTarget::Fullscreen(w) => KeyboardTarget::leave(w, seat, data, serial),
KeyboardFocusTarget::Group(_) => {}
KeyboardFocusTarget::LayerSurface(l) => {
KeyboardTarget::leave(l.wl_surface(), seat, data, serial)
}
FocusTarget::Popup(p) => KeyboardTarget::leave(p.wl_surface(), seat, data, serial),
KeyboardFocusTarget::Popup(p) => {
KeyboardTarget::leave(p.wl_surface(), seat, data, serial)
}
}
}
fn key(
@ -124,19 +187,17 @@ impl KeyboardTarget<State> for FocusTarget {
time: u32,
) {
match self {
FocusTarget::Window(w) => KeyboardTarget::key(
w.toplevel().wl_surface(),
seat,
data,
key,
state,
serial,
time,
),
FocusTarget::LayerSurface(l) => {
KeyboardFocusTarget::Element(w) => {
KeyboardTarget::key(w, seat, data, key, state, serial, time)
}
KeyboardFocusTarget::Fullscreen(w) => {
KeyboardTarget::key(w, seat, data, key, state, serial, time)
}
KeyboardFocusTarget::Group(_) => {}
KeyboardFocusTarget::LayerSurface(l) => {
KeyboardTarget::key(l.wl_surface(), seat, data, key, state, serial, time)
}
FocusTarget::Popup(p) => {
KeyboardFocusTarget::Popup(p) => {
KeyboardTarget::key(p.wl_surface(), seat, data, key, state, serial, time)
}
}
@ -149,50 +210,113 @@ impl KeyboardTarget<State> for FocusTarget {
serial: Serial,
) {
match self {
FocusTarget::Window(w) => {
KeyboardTarget::modifiers(w.toplevel().wl_surface(), seat, data, modifiers, serial)
KeyboardFocusTarget::Element(w) => {
KeyboardTarget::modifiers(w, seat, data, modifiers, serial)
}
FocusTarget::LayerSurface(l) => {
KeyboardFocusTarget::Fullscreen(w) => {
KeyboardTarget::modifiers(w, seat, data, modifiers, serial)
}
KeyboardFocusTarget::Group(_) => {}
KeyboardFocusTarget::LayerSurface(l) => {
KeyboardTarget::modifiers(l.wl_surface(), seat, data, modifiers, serial)
}
FocusTarget::Popup(p) => {
KeyboardFocusTarget::Popup(p) => {
KeyboardTarget::modifiers(p.wl_surface(), seat, data, modifiers, serial)
}
}
}
}
impl WaylandFocus for FocusTarget {
fn wl_surface(&self) -> Option<&WlSurface> {
Some(match self {
FocusTarget::Window(w) => w.toplevel().wl_surface(),
FocusTarget::LayerSurface(l) => l.wl_surface(),
FocusTarget::Popup(p) => p.wl_surface(),
})
impl WaylandFocus for KeyboardFocusTarget {
fn wl_surface(&self) -> Option<WlSurface> {
match self {
KeyboardFocusTarget::Element(w) => WaylandFocus::wl_surface(w),
KeyboardFocusTarget::Fullscreen(w) => WaylandFocus::wl_surface(w),
KeyboardFocusTarget::Group(_) => None,
KeyboardFocusTarget::LayerSurface(l) => Some(l.wl_surface().clone()),
KeyboardFocusTarget::Popup(p) => Some(p.wl_surface().clone()),
}
}
fn same_client_as(&self, object_id: &ObjectId) -> bool {
match self {
FocusTarget::Window(w) => w.toplevel().wl_surface().id().same_client_as(object_id),
FocusTarget::LayerSurface(l) => l.wl_surface().id().same_client_as(object_id),
FocusTarget::Popup(p) => p.wl_surface().id().same_client_as(object_id),
KeyboardFocusTarget::Element(w) => WaylandFocus::same_client_as(w, object_id),
KeyboardFocusTarget::Fullscreen(w) => WaylandFocus::same_client_as(w, object_id),
KeyboardFocusTarget::Group(_) => false,
KeyboardFocusTarget::LayerSurface(l) => l.wl_surface().id().same_client_as(object_id),
KeyboardFocusTarget::Popup(p) => p.wl_surface().id().same_client_as(object_id),
}
}
}
impl From<Window> for FocusTarget {
impl WaylandFocus for PointerFocusTarget {
fn wl_surface(&self) -> Option<WlSurface> {
Some(match self {
PointerFocusTarget::Element(w) => WaylandFocus::wl_surface(w)?,
PointerFocusTarget::Fullscreen(w) => WaylandFocus::wl_surface(w)?,
PointerFocusTarget::LayerSurface(l) => l.wl_surface().clone(),
PointerFocusTarget::Popup(p) => p.wl_surface().clone(),
})
}
fn same_client_as(&self, object_id: &ObjectId) -> bool {
match self {
PointerFocusTarget::Element(w) => WaylandFocus::same_client_as(w, object_id),
PointerFocusTarget::Fullscreen(w) => WaylandFocus::same_client_as(w, object_id),
PointerFocusTarget::LayerSurface(l) => l.wl_surface().id().same_client_as(object_id),
PointerFocusTarget::Popup(p) => p.wl_surface().id().same_client_as(object_id),
}
}
}
impl From<CosmicMapped> for PointerFocusTarget {
fn from(w: CosmicMapped) -> Self {
PointerFocusTarget::Element(w)
}
}
impl From<Window> for PointerFocusTarget {
fn from(w: Window) -> Self {
FocusTarget::Window(w)
PointerFocusTarget::Fullscreen(w)
}
}
impl From<LayerSurface> for FocusTarget {
impl From<LayerSurface> for PointerFocusTarget {
fn from(l: LayerSurface) -> Self {
FocusTarget::LayerSurface(l)
PointerFocusTarget::LayerSurface(l)
}
}
impl From<PopupKind> for FocusTarget {
impl From<PopupKind> for PointerFocusTarget {
fn from(p: PopupKind) -> Self {
FocusTarget::Popup(p)
PointerFocusTarget::Popup(p)
}
}
impl From<CosmicMapped> for KeyboardFocusTarget {
fn from(w: CosmicMapped) -> Self {
KeyboardFocusTarget::Element(w)
}
}
impl From<Window> for KeyboardFocusTarget {
fn from(w: Window) -> Self {
KeyboardFocusTarget::Fullscreen(w)
}
}
impl From<WindowGroup> for KeyboardFocusTarget {
fn from(w: WindowGroup) -> Self {
KeyboardFocusTarget::Group(w)
}
}
impl From<LayerSurface> for KeyboardFocusTarget {
fn from(l: LayerSurface) -> Self {
KeyboardFocusTarget::LayerSurface(l)
}
}
impl From<PopupKind> for KeyboardFocusTarget {
fn from(p: PopupKind) -> Self {
KeyboardFocusTarget::Popup(p)
}
}

View file

@ -51,10 +51,11 @@ impl Shell {
.space
.outputs_for_window(&window)
.into_iter()
.find(|o| o.geometry().contains(pos.to_i32_round())) {
Some(o) => o,
None => return,
};
.find(|o| o.geometry().contains(pos.to_i32_round()))
{
Some(o) => o,
None => return,
};
let mut initial_window_location = workspace.space.window_location(&window).unwrap();
match &window.toplevel() {
@ -356,7 +357,7 @@ impl MoveSurfaceGrab {
time: u32,
) {
// No more buttons are pressed, release the grab.
let output = active_output(&self.seat, &state.common);
let output = self.seat.active_output();
let seat = self.seat.clone();
state.common.event_loop_handle.insert_idle(move |data| {

View file

@ -1,16 +1,13 @@
// SPDX-License-Identifier: GPL-3.0-only
use crate::utils::prelude::*;
use crate::{shell::focus::target::PointerFocusTarget, utils::prelude::*};
use smithay::{
desktop::{Kind, Window},
input::pointer::{
AxisFrame, ButtonEvent, GrabStartData as PointerGrabStartData, MotionEvent, PointerGrab,
PointerInnerHandle,
},
reexports::{
wayland_protocols::xdg::shell::server::xdg_toplevel,
wayland_server::protocol::wl_surface::WlSurface,
},
reexports::wayland_protocols::xdg::shell::server::xdg_toplevel,
utils::{IsAlive, Logical, Point, Serial, Size},
wayland::{
compositor::with_states,
@ -90,7 +87,7 @@ impl PointerGrab<State> for ResizeSurfaceGrab {
&mut self,
data: &mut State,
handle: &mut PointerInnerHandle<'_, State>,
_focus: Option<(WlSurface, Point<i32, Logical>)>,
_focus: Option<(PointerFocusTarget, Point<i32, Logical>)>,
event: &MotionEvent,
) {
// While the grab is active, no client has pointer focus

View file

@ -1,99 +1,86 @@
// SPDX-License-Identifier: GPL-3.0-only
use smithay::{
desktop::{layer_map_for_output, space::RenderZindex, Kind, Space, Window},
input::{
pointer::{Focus, GrabStartData as PointerGrabStartData},
Seat,
},
backend::renderer::{ImportAll, Renderer},
desktop::{layer_map_for_output, space::SpaceElement, Space, Window},
input::Seat,
output::Output,
reexports::wayland_protocols::xdg::shell::server::xdg_toplevel::{
ResizeEdge, State as XdgState,
},
utils::{IsAlive, Logical, Point, Rectangle, Serial},
wayland::{compositor::with_states, shell::xdg::XdgToplevelSurfaceRoleAttributes},
render_elements,
utils::{Logical, Point, Rectangle},
};
use std::{collections::HashSet, sync::Mutex};
use std::collections::HashMap;
use crate::state::State;
use crate::{
shell::{
element::{CosmicMapped, CosmicMappedRenderElement},
OutputNotMapped,
},
state::State,
utils::prelude::*,
};
mod grabs;
pub use self::grabs::*;
pub const FLOATING_INDEX: u8 = RenderZindex::Shell as u8 + 1;
#[derive(Debug, Default)]
#[derive(Debug)]
pub struct FloatingLayout {
pending_windows: Vec<Window>,
pub windows: HashSet<Window>,
pub(in crate::shell) space: Space<CosmicMapped>,
}
#[derive(Default)]
pub struct WindowUserDataInner {
last_geometry: Rectangle<i32, Logical>,
impl Default for FloatingLayout {
fn default() -> Self {
FloatingLayout {
space: Space::new(None),
}
}
}
pub type WindowUserData = Mutex<WindowUserDataInner>;
impl FloatingLayout {
pub fn new() -> FloatingLayout {
Default::default()
}
pub fn map_window(
pub fn map_output(&mut self, output: &Output, location: Point<i32, Logical>) {
self.space.map_output(output, location)
}
pub fn unmap_output(&mut self, output: &Output) {
self.space.unmap_output(output);
self.refresh();
}
pub fn map(
&mut self,
space: &mut Space,
window: Window,
mapped: impl Into<CosmicMapped>,
seat: &Seat<State>,
position: impl Into<Option<Point<i32, Logical>>>,
) {
if let Some(output) = super::output_from_seat(Some(seat), space) {
self.map_window_internal(space, window, &output, position.into());
} else {
self.pending_windows.push(window);
}
let mapped = mapped.into();
let output = seat.active_output();
let position = position.into();
self.map_internal(mapped, &output, position)
}
pub fn refresh(&mut self, space: &mut Space) {
self.pending_windows.retain(|w| w.toplevel().alive());
if let Some(output) = super::output_from_seat(None, space) {
for window in std::mem::take(&mut self.pending_windows).into_iter() {
self.map_window_internal(space, window, &output, None);
}
}
// TODO make sure all windows are still visible on any output or move them
}
fn map_window_internal(
pub(in crate::shell) fn map_internal(
&mut self,
space: &mut Space,
window: Window,
mapped: CosmicMapped,
output: &Output,
position: Option<Point<i32, Logical>>,
) {
let last_geometry = window
.user_data()
.get::<WindowUserData>()
.map(|u| u.lock().unwrap().last_geometry);
let mut win_geo = window.geometry();
let mut win_geo = mapped.geometry();
let layers = layer_map_for_output(&output);
let geometry = layers.non_exclusive_zone();
let last_geometry = mapped.last_geometry.lock().unwrap().clone();
let mut geo_updated = false;
if let Some(size) = last_geometry.clone().map(|g| g.size) {
if let Some(size) = last_geometry.map(|g| g.size) {
geo_updated = win_geo.size == size;
win_geo.size = size;
}
{
let (min_size, max_size) = with_states(window.toplevel().wl_surface(), |states| {
let attrs = states
.data_map
.get::<Mutex<XdgToplevelSurfaceRoleAttributes>>()
.unwrap()
.lock()
.unwrap();
(attrs.min_size, attrs.max_size)
});
let (min_size, max_size) = (mapped.min_size(), mapped.max_size());
if win_geo.size.w > geometry.size.w / 3 * 2 {
// try a more reasonable size
let mut width = geometry.size.w / 3 * 2;
@ -136,103 +123,67 @@ impl FloatingLayout {
.into()
});
#[allow(irrefutable_let_patterns)]
if let Kind::Xdg(xdg) = &window.toplevel() {
xdg.with_pending_state(|state| {
state.states.unset(XdgState::TiledLeft);
state.states.unset(XdgState::TiledRight);
state.states.unset(XdgState::TiledTop);
state.states.unset(XdgState::TiledBottom);
if geo_updated {
state.size = Some(win_geo.size);
}
});
xdg.send_configure();
// TODO: Move this into CosmicMapped, this needs to differciate between stacks and windows
mapped.set_tiled(false);
if geo_updated {
mapped.set_size(win_geo.size);
}
mapped.configure();
space.map_window(&window, position, FLOATING_INDEX, false);
self.windows.insert(window);
self.space.map_element(mapped, position, false);
}
pub fn unmap_window(&mut self, space: &mut Space, window: &Window) {
pub fn unmap(&mut self, window: &CosmicMapped) -> bool {
#[allow(irrefutable_let_patterns)]
let is_maximized = match &window.toplevel() {
Kind::Xdg(surface) => {
surface.with_pending_state(|state| state.states.contains(XdgState::Maximized))
}
};
let is_maximized = window.is_maximized();
if !is_maximized {
if let Some(location) = space.window_location(window) {
let user_data = window.user_data();
user_data.insert_if_missing(|| WindowUserData::default());
user_data
.get::<WindowUserData>()
.unwrap()
.lock()
.unwrap()
.last_geometry = Rectangle::from_loc_and_size(location, window.geometry().size);
if let Some(location) = self.space.element_location(window) {
*window.last_geometry.lock().unwrap() = Some(Rectangle::from_loc_and_size(
location,
window.geometry().size,
));
}
}
space.unmap_window(window);
self.pending_windows.retain(|w| w != window);
self.windows.remove(window);
let was_unmaped = self.space.elements().any(|e| e == window);
self.space.unmap_elem(&window);
was_unmaped
}
pub fn maximize_request(&mut self, space: &mut Space, window: &Window, output: &Output) {
pub fn element_geometry(&self, elem: &CosmicMapped) -> Option<Rectangle<i32, Logical>> {
self.space.element_geometry(elem)
}
pub fn maximize_request(&mut self, window: &CosmicMapped, output: &Output) {
let layers = layer_map_for_output(&output);
let geometry = layers.non_exclusive_zone();
if let Some(location) = space.window_location(window) {
let user_data = window.user_data();
user_data.insert_if_missing(|| WindowUserData::default());
user_data
.get::<WindowUserData>()
.unwrap()
.lock()
.unwrap()
.last_geometry = Rectangle::from_loc_and_size(location, window.geometry().size);
if let Some(location) = self.space.element_location(window) {
*window.last_geometry.lock().unwrap() = Some(Rectangle::from_loc_and_size(
location,
window.geometry().size,
));
}
space.map_window(
&window,
(geometry.loc.x, geometry.loc.y),
FLOATING_INDEX,
true,
);
#[allow(irrefutable_let_patterns)]
if let Kind::Xdg(surface) = &window.toplevel() {
surface.with_pending_state(|state| {
state.states.set(XdgState::Maximized);
state.size = Some(geometry.size);
});
window.configure();
}
self.space.map_element(window.clone(), geometry.loc, true);
window.set_maximized(true);
window.set_size(geometry.size);
window.configure();
}
pub fn unmaximize_request(&mut self, space: &mut Space, window: &Window) {
let last_geometry = window
.user_data()
.get::<WindowUserData>()
.map(|u| u.lock().unwrap().last_geometry);
match window.toplevel() {
Kind::Xdg(toplevel) => {
toplevel.with_pending_state(|state| {
state.states.unset(XdgState::Maximized);
state.size = last_geometry.map(|g| g.size);
});
toplevel.send_configure();
}
}
if let Some(last_location) = last_geometry.map(|g| g.loc) {
space.map_window(&window, last_location, FLOATING_INDEX, true);
}
pub fn unmaximize_request(&mut self, window: &CosmicMapped) {
let last_geometry = window.last_geometry.lock().unwrap().clone();
window.set_maximized(false);
window.set_size(last_geometry.map(|g| g.size).expect("No previous size?"));
window.configure();
let last_location = last_geometry.map(|g| g.loc).expect("No previous location?");
self.space.map_element(window.clone(), last_location, true);
}
/*
pub fn resize_request(
state: &mut State,
window: &Window,
window: &CosmicWindow,
seat: &Seat<State>,
serial: Serial,
start_data: PointerGrabStartData<State>,
@ -256,4 +207,111 @@ impl FloatingLayout {
pointer.set_grab(state, grab, serial, Focus::Clear);
}
}
*/
pub fn mapped(&self) -> impl Iterator<Item = &CosmicMapped> {
self.space.elements()
}
pub fn windows(&self) -> impl Iterator<Item = Window> + '_ {
self.mapped().flat_map(|e| e.windows().map(|(w, _)| w))
}
pub fn refresh(&mut self) {
for element in self
.space
.elements()
.filter(|e| self.space.outputs_for_element(e).is_empty())
.cloned()
.collect::<Vec<_>>()
.into_iter()
{
// TODO what about windows leaving to the top with no headerbar to drag? can that happen? (Probably if the user is moving outputs down)
*element.last_geometry.lock().unwrap() = None;
let output = self.space.outputs().next().unwrap().clone();
self.map_internal(element, &output, None);
}
self.space.refresh()
}
pub fn most_overlapped_output_for_element(&self, elem: &CosmicMapped) -> Option<Output> {
let elem_geo = self.space.element_geometry(elem)?;
if self.space.outputs().nth(1).is_none() {
return self.space.outputs().next().cloned();
}
Some(
self.space
.outputs_for_element(elem)
.into_iter()
.max_by_key(|o| {
let output_geo = self.space.output_geometry(o).unwrap();
if let Some(intersection) = output_geo.intersection(elem_geo) {
intersection.size.w * intersection.size.h
} else {
0
}
})
.unwrap_or(self.space.outputs().next().unwrap().clone()),
)
}
pub fn merge(&mut self, other: FloatingLayout) {
let mut output_pos_map = HashMap::new();
for output in self.space.outputs() {
output_pos_map.insert(
output.clone(),
self.space.output_geometry(output).unwrap().loc
- other
.space
.output_geometry(output)
.map(|geo| geo.loc)
.unwrap_or_else(|| (0, 0).into()),
);
}
for element in other.space.elements() {
let mut elem_geo = other.space.element_geometry(element).unwrap();
let output = other
.space
.outputs_for_element(element)
.into_iter()
.filter(|o| self.space.outputs().any(|o2| o == o2))
.max_by_key(|o| {
let output_geo = other.space.output_geometry(o).unwrap();
let intersection = output_geo.intersection(elem_geo).unwrap();
intersection.size.w * intersection.size.h
})
.unwrap_or(self.space.outputs().next().unwrap().clone());
elem_geo.loc += output_pos_map
.get(&output)
.copied()
.unwrap_or_else(|| (0, 0).into());
self.space.map_element(element.clone(), elem_geo.loc, false);
}
self.refresh(); //fixup any out of bounds elements
}
pub fn render_output<R>(
&self,
output: &Output,
) -> Result<Vec<FloatingRenderElement<R>>, OutputNotMapped>
where
R: Renderer + ImportAll,
<R as Renderer>::TextureId: 'static,
{
let output_scale = output.current_scale().fractional_scale();
let output_geo = self.space.output_geometry(output).ok_or(OutputNotMapped)?;
Ok(self
.space
.render_elements_for_region::<R, _>(&output_geo, output_scale)
.into_iter()
.map(FloatingRenderElement::from)
.collect())
}
}
render_elements! {
pub FloatingRenderElement<R> where R: ImportAll;
Window=CosmicMappedRenderElement<R>,
}

View file

@ -1,11 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-only
use crate::{input::ActiveOutput, state::State};
use regex::RegexSet;
use smithay::{
desktop::{Space, Window},
input::Seat,
output::Output,
desktop::Window,
wayland::{compositor::with_states, shell::xdg::XdgToplevelSurfaceRoleAttributes},
};
use std::sync::Mutex;
@ -118,17 +115,3 @@ pub fn should_be_floating(window: &Window) -> bool {
false
})
}
fn output_from_seat(seat: Option<&Seat<State>>, space: &Space) -> Option<Output> {
seat.and_then(|seat| {
seat.user_data()
.get::<ActiveOutput>()
.map(|active| active.0.borrow().clone())
.or_else(|| {
seat.get_pointer()
.map(|ptr| space.output_under(ptr.current_location()).next().unwrap())
.cloned()
})
})
.or_else(|| space.outputs().next().cloned())
}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -1,79 +1,179 @@
use crate::{
shell::layout::{floating::FloatingLayout, tiling::TilingLayout},
shell::{
element::CosmicWindow,
layout::{floating::FloatingLayout, tiling::TilingLayout},
},
state::State,
utils::prelude::*,
wayland::protocols::workspace::WorkspaceHandle,
};
use indexmap::IndexSet;
use smithay::{
desktop::{Kind, Space, Window, WindowSurfaceType},
backend::renderer::{
element::{surface::WaylandSurfaceRenderElement, AsRenderElements},
ImportAll, Renderer,
},
desktop::{
layer_map_for_output, space::SpaceElement, Kind, LayerSurface, Space, Window,
WindowSurfaceType,
},
input::{pointer::GrabStartData as PointerGrabStartData, Seat},
output::Output,
output::{Output, WeakOutput},
reexports::{
wayland_protocols::xdg::shell::server::xdg_toplevel::{self, ResizeEdge},
wayland_server::{protocol::wl_surface::WlSurface, DisplayHandle},
},
utils::{IsAlive, Serial},
render_elements,
utils::{IsAlive, Logical, Point, Rectangle, Scale, Serial},
wayland::shell::wlr_layer::Layer,
};
use std::collections::HashMap;
use std::{collections::HashMap, time::Duration};
use super::{
element::CosmicMapped,
focus::{FocusStack, FocusStackMut},
layout::{floating::FloatingRenderElement, tiling::TilingRenderElement},
};
#[derive(Debug)]
pub struct Workspace {
pub idx: u8,
pub space: Space,
pub tiling_layer: TilingLayout,
pub floating_layer: FloatingLayout,
tiling_enabled: bool,
pub fullscreen: HashMap<String, Window>,
pub tiling_enabled: bool,
pub fullscreen: HashMap<Output, Window>,
pub handle: WorkspaceHandle,
pub focus_stack: FocusStacks,
}
#[derive(Debug, Default)]
pub struct FocusStacks(HashMap<Seat<State>, IndexSet<CosmicMapped>>);
impl Workspace {
pub fn new(idx: u8, handle: WorkspaceHandle) -> Workspace {
pub fn new(handle: WorkspaceHandle) -> Workspace {
Workspace {
idx,
space: Space::new(None),
tiling_layer: TilingLayout::new(),
floating_layer: FloatingLayout::new(),
tiling_enabled: true,
fullscreen: HashMap::new(),
handle,
focus_stack: FocusStacks::default(),
}
}
pub fn refresh(&mut self, dh: &DisplayHandle) {
let outputs = self.space.outputs().collect::<Vec<_>>();
let dead_output_windows = self
.fullscreen
.iter()
.filter(|(name, _)| !outputs.iter().any(|o| o.name() == **name))
.map(|(_, w)| w)
.cloned()
.collect::<Vec<_>>();
for window in dead_output_windows {
self.unfullscreen_request(&window);
}
pub fn refresh(&mut self) {
self.fullscreen.retain(|_, w| w.alive());
self.floating_layer.refresh(&mut self.space);
self.tiling_layer.refresh(&mut self.space);
self.space.refresh(dh);
self.floating_layer.refresh();
self.tiling_layer.refresh();
}
pub fn maximize_request(&mut self, window: &Window, output: &Output) {
pub fn commit(&mut self, surface: &WlSurface) {
if let Some(mapped) = self.element_for_surface(surface) {
mapped
.windows()
.find(|(w, _)| w.toplevel().wl_surface() == surface)
.unwrap()
.0
.on_commit();
}
}
pub fn map_output(&mut self, output: &Output, position: Point<i32, Logical>) {
self.tiling_layer.map_output(output, position);
self.floating_layer.map_output(output, position);
}
pub fn unmap_output(&mut self, output: &Output) {
if let Some(dead_output_window) = self.fullscreen.remove(output) {
self.unfullscreen_request(&dead_output_window);
}
self.tiling_layer.unmap_output(output);
self.floating_layer.unmap_output(output);
self.refresh();
}
pub fn element_for_surface(&self, surface: &WlSurface) -> Option<&CosmicMapped> {
self.floating_layer
.mapped()
.chain(self.tiling_layer.mapped().map(|(_, w, _)| w))
.find(|e| {
e.windows()
.any(|(w, _)| w.toplevel().wl_surface() == surface)
})
}
pub fn outputs_for_element(&self, elem: &CosmicMapped) -> impl Iterator<Item = Output> {
self.floating_layer
.space
.outputs_for_element(elem)
.into_iter()
.chain(self.tiling_layer.output_for_element(elem).cloned())
}
pub fn output_under(&self, point: Point<i32, Logical>) -> Option<&Output> {
let space = &self.floating_layer.space;
space.outputs().find(|o| {
let internal_output_geo = space.output_geometry(o).unwrap();
let external_output_geo = o.geometry();
internal_output_geo.contains(point - external_output_geo.loc + internal_output_geo.loc)
})
}
pub fn element_under(
&self,
location: Point<f64, Logical>,
) -> Option<(&CosmicMapped, Point<i32, Logical>)> {
self.floating_layer
.space
.element_under(location)
.or_else(|| {
self.tiling_layer.mapped().find_map(|(_, mapped, loc)| {
let test_point = location - loc.to_f64();
mapped
.is_in_input_region(&test_point)
.then_some((mapped, loc))
})
})
}
pub fn element_geometry(&self, elem: &CosmicMapped) -> Option<Rectangle<i32, Logical>> {
let space = &self.floating_layer.space;
let outputs = space.outputs().collect::<Vec<_>>();
let offset = if outputs.len() == 1
&& space.output_geometry(&outputs[0]).unwrap().loc == Point::from((0, 0))
{
outputs[0].geometry().loc
} else {
(0, 0).into()
};
self.floating_layer
.space
.element_geometry(elem)
.or_else(|| self.tiling_layer.element_geometry(elem))
.map(|mut geo| {
geo.loc += offset;
geo
})
}
/*
pub fn maximize_request(&mut self, window: &CosmicWindow, output: &Output) {
if self.fullscreen.values().any(|w| w == window) {
return;
}
if self.floating_layer.windows.contains(window) {
self.floating_layer
.maximize_request(&mut self.space, window, output);
.maximize_request(, window, output);
}
}
pub fn unmaximize_request(&mut self, window: &Window) {
pub fn unmaximize_request(&mut self, window: &CosmicMapped) {
if self.fullscreen.values().any(|w| w == window) {
return self.unfullscreen_request(window);
}
if self.floating_layer.windows.contains(window) {
self.floating_layer
.unmaximize_request(&mut self.space, window);
.unmaximize_request(window);
}
}
@ -101,9 +201,10 @@ impl Workspace {
TilingLayout::resize_request(state, &window, seat, serial, start_data, edges)
}
}
*/
pub fn fullscreen_request(&mut self, window: &Window, output: &Output) {
if self.fullscreen.contains_key(&output.name()) {
if self.fullscreen.contains_key(output) {
return;
}
@ -123,7 +224,7 @@ impl Workspace {
});
xdg.send_configure();
self.fullscreen.insert(output.name(), window.clone());
self.fullscreen.insert(output.clone(), window.clone());
}
}
@ -135,8 +236,8 @@ impl Workspace {
state.states.unset(xdg_toplevel::State::Fullscreen);
state.size = None;
});
self.floating_layer.refresh(&mut self.space);
self.tiling_layer.refresh(&mut self.space);
self.floating_layer.refresh();
self.tiling_layer.refresh();
xdg.send_configure();
}
self.fullscreen.retain(|_, w| w != window);
@ -144,7 +245,7 @@ impl Workspace {
}
pub fn fullscreen_toggle(&mut self, window: &Window, output: &Output) {
if self.fullscreen.contains_key(&output.name()) {
if self.fullscreen.contains_key(output) {
self.unfullscreen_request(window)
} else {
self.fullscreen_request(window, output)
@ -152,26 +253,33 @@ impl Workspace {
}
pub fn get_fullscreen(&self, output: &Output) -> Option<&Window> {
if !self.space.outputs().any(|o| o == output) {
return None;
}
self.fullscreen.get(&output.name()).filter(|w| w.alive())
self.fullscreen.get(output).filter(|w| w.alive())
}
pub fn toggle_tiling(&mut self, seat: &Seat<State>) {
if self.tiling_enabled {
for window in self.tiling_layer.windows.clone().into_iter() {
self.tiling_layer.unmap_window(&mut self.space, &window);
self.floating_layer
.map_window(&mut self.space, window, seat, None);
for window in self
.tiling_layer
.mapped()
.map(|(_, m, _)| m.clone())
.collect::<Vec<_>>()
.into_iter()
{
self.tiling_layer.unmap(&window);
self.floating_layer.map(window, seat, None);
}
self.tiling_enabled = false;
} else {
let focus_stack = self.focus_stack(seat);
for window in self.floating_layer.windows.clone().into_iter() {
self.floating_layer.unmap_window(&mut self.space, &window);
self.tiling_layer
.map_window(&mut self.space, window, seat, focus_stack.iter())
let focus_stack = self.focus_stack.get(seat);
for window in self
.floating_layer
.mapped()
.cloned()
.collect::<Vec<_>>()
.into_iter()
{
self.floating_layer.unmap(&window);
self.tiling_layer.map(window, seat, focus_stack.iter())
}
self.tiling_enabled = true;
}
@ -179,18 +287,157 @@ impl Workspace {
pub fn toggle_floating_window(&mut self, seat: &Seat<State>) {
if self.tiling_enabled {
if let Some(window) = self.focus_stack(seat).iter().next().cloned() {
if self.tiling_layer.windows.contains(&window) {
self.tiling_layer.unmap_window(&mut self.space, &window);
self.floating_layer
.map_window(&mut self.space, window, seat, None);
} else if self.floating_layer.windows.contains(&window) {
let focus_stack = self.focus_stack(seat);
self.floating_layer.unmap_window(&mut self.space, &window);
self.tiling_layer
.map_window(&mut self.space, window, seat, focus_stack.iter())
if let Some(window) = self.focus_stack.get(seat).iter().next().cloned() {
if self.tiling_layer.mapped().any(|(_, m, _)| m == &window) {
self.tiling_layer.unmap(&window);
self.floating_layer.map(window, seat, None);
} else if self.floating_layer.mapped().any(|w| w == &window) {
let focus_stack = self.focus_stack.get(seat);
self.floating_layer.unmap(&window);
self.tiling_layer.map(window, seat, focus_stack.iter())
}
}
}
}
pub fn mapped(&self) -> impl Iterator<Item = &CosmicMapped> {
self.floating_layer
.mapped()
.chain(self.tiling_layer.mapped().map(|(_, w, _)| w))
}
pub fn windows(&self) -> impl Iterator<Item = Window> + '_ {
self.floating_layer
.windows()
.chain(self.tiling_layer.windows().map(|(_, w, _)| w))
}
pub fn render_output<R>(
&self,
output: &Output,
) -> Result<Vec<WorkspaceRenderElement<R>>, OutputNotMapped>
where
R: Renderer + ImportAll,
<R as Renderer>::TextureId: 'static,
{
let mut render_elements = Vec::new();
let output_scale = output.current_scale().fractional_scale();
let layer_map = layer_map_for_output(output);
if let Some(fullscreen) = self.fullscreen.get(output) {
// overlay layer surfaces
render_elements.extend(
layer_map
.layers()
.rev()
.filter(|s| s.layer() == Layer::Overlay)
.filter_map(|surface| {
layer_map
.layer_geometry(surface)
.map(|geo| (geo.loc, surface))
})
.flat_map(|(loc, surface)| {
AsRenderElements::<R>::render_elements::<WorkspaceRenderElement<R>>(
surface,
loc.to_physical_precise_round(output_scale),
Scale::from(output_scale),
)
}),
);
// fullscreen window
render_elements.extend(AsRenderElements::<R>::render_elements::<
WorkspaceRenderElement<R>,
>(fullscreen, (0, 0).into(), output_scale.into()));
} else {
// TODO: Handle modes like
// - keyboard window swapping
// - resizing / moving in tiling
// overlay and top layer surfaces
let lower = {
let (upper, lower): (Vec<&LayerSurface>, Vec<&LayerSurface>) = layer_map
.layers()
.rev()
.partition(|s| matches!(s.layer(), Layer::Background | Layer::Bottom));
render_elements.extend(
upper
.into_iter()
.filter_map(|surface| {
layer_map
.layer_geometry(surface)
.map(|geo| (geo.loc, surface))
})
.flat_map(|(loc, surface)| {
AsRenderElements::<R>::render_elements::<WorkspaceRenderElement<R>>(
surface,
loc.to_physical_precise_round(output_scale),
Scale::from(output_scale),
)
}),
);
lower
};
// floating surfaces
render_elements.extend(
self.floating_layer
.render_output::<R>(output)?
.into_iter()
.map(WorkspaceRenderElement::from),
);
//tiling surfaces
render_elements.extend(
self.tiling_layer
.render_output::<R>(output)?
.into_iter()
.map(WorkspaceRenderElement::from),
);
// bottom and background layer surfaces
{
render_elements.extend(
lower
.into_iter()
.filter_map(|surface| {
layer_map
.layer_geometry(surface)
.map(|geo| (geo.loc, surface))
})
.flat_map(|(loc, surface)| {
AsRenderElements::<R>::render_elements::<WorkspaceRenderElement<R>>(
surface,
loc.to_physical_precise_round(output_scale),
Scale::from(output_scale),
)
}),
);
}
}
Ok(render_elements)
}
}
impl FocusStacks {
pub fn get<'a>(&'a self, seat: &Seat<State>) -> FocusStack<'a> {
FocusStack(self.0.get(seat))
}
pub fn get_mut<'a>(&'a mut self, seat: &Seat<State>) -> FocusStackMut<'a> {
FocusStackMut(self.0.entry(seat.clone()).or_default())
}
}
pub struct OutputNotMapped;
render_elements! {
pub WorkspaceRenderElement<R> where R: ImportAll;
Wayland=WaylandSurfaceRenderElement,
Floating=FloatingRenderElement<R>,
Tiling=TilingRenderElement<R>,
}