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

7
Cargo.lock generated
View file

@ -83,12 +83,6 @@ version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "983cd8b9d4b02a6dc6ffa557262eb5858a27a0038ffffe21a0f133eaa819a164" checksum = "983cd8b9d4b02a6dc6ffa557262eb5858a27a0038ffffe21a0f133eaa819a164"
[[package]]
name = "atomic_float"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62af46d040ba9df09edc6528dae9d8e49f5f3e82f55b7d2ec31a733c38dbc49d"
[[package]] [[package]]
name = "atomic_refcell" name = "atomic_refcell"
version = "0.1.8" version = "0.1.8"
@ -292,7 +286,6 @@ name = "cosmic-comp"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"atomic_float",
"bitflags", "bitflags",
"cosmic-protocols", "cosmic-protocols",
"edid-rs", "edid-rs",

View file

@ -28,7 +28,6 @@ xkbcommon = "0.4"
indexmap = "1.8.0" indexmap = "1.8.0"
xdg = "^2.1" xdg = "^2.1"
ron = "0.7" ron = "0.7"
atomic_float = "0.1"
libsystemd = "0.5" libsystemd = "0.5"
wayland-backend = "=0.1.0-beta.10" wayland-backend = "=0.1.0-beta.10"
wayland-scanner = "=0.30.0-beta.10" wayland-scanner = "=0.30.0-beta.10"

View file

@ -37,7 +37,7 @@
(modifiers: [Logo], key: "y"): ToggleTiling, (modifiers: [Logo], key: "y"): ToggleTiling,
(modifiers: [Logo], key: "g"): ToggleWindowFloating, (modifiers: [Logo], key: "g"): ToggleWindowFloating,
(modifiers: [Logo, Shift], key: "f"): Fullscreen, (modifiers: [Logo, Shift], key: "f"): Fullscreen,
(modifiers: [Logo, Shift], key: "s"): Screenshot, //(modifiers: [Logo, Shift], key: "s"): Screenshot,
//TODO: ability to select default web browser //TODO: ability to select default web browser
(modifiers: [Logo], key: "b"): Spawn("firefox"), (modifiers: [Logo], key: "b"): Spawn("firefox"),
//TODO: ability to select default file browser //TODO: ability to select default file browser
@ -53,5 +53,6 @@
(modifiers: [], key: "XF86MonBrightnessDown"): Spawn("busctl --user call com.system76.CosmicSettingsDaemon /com/system76/CosmicSettingsDaemon com.system76.CosmicSettingsDaemon DecreaseDisplayBrightness"), (modifiers: [], key: "XF86MonBrightnessDown"): Spawn("busctl --user call com.system76.CosmicSettingsDaemon /com/system76/CosmicSettingsDaemon com.system76.CosmicSettingsDaemon DecreaseDisplayBrightness"),
}, },
workspace_mode: OutputBound, workspace_mode: OutputBound,
workspace_amount: Dynamic,
floating_default: false, floating_default: false,
) )

View file

@ -20,7 +20,8 @@ use smithay::{
input::InputEvent, input::InputEvent,
libinput::{LibinputInputBackend, LibinputSessionInterface}, libinput::{LibinputInputBackend, LibinputSessionInterface},
renderer::{ renderer::{
gles2::Gles2Renderbuffer, damage::DamageTrackedRenderer,
gles2::{Gles2Renderbuffer, Gles2Renderer},
multigpu::{egl::EglGlesBackend, GpuManager}, multigpu::{egl::EglGlesBackend, GpuManager},
Bind, Bind,
}, },
@ -51,7 +52,7 @@ use std::{
os::unix::io::{FromRawFd, OwnedFd}, os::unix::io::{FromRawFd, OwnedFd},
path::PathBuf, path::PathBuf,
rc::Rc, rc::Rc,
time::{Duration, Instant}, time::Duration,
}; };
mod drm_helpers; mod drm_helpers;
@ -60,9 +61,11 @@ mod socket;
use session_fd::*; use session_fd::*;
use socket::*; use socket::*;
use super::render::GlMultiRenderer;
pub struct KmsState { pub struct KmsState {
devices: HashMap<DrmNode, Device>, devices: HashMap<DrmNode, Device>,
pub api: GpuManager<EglGlesBackend>, pub api: GpuManager<EglGlesBackend<Gles2Renderer>>,
pub primary: DrmNode, pub primary: DrmNode,
session: AutoSession, session: AutoSession,
signaler: Signaler<Signal>, signaler: Signaler<Signal>,
@ -83,9 +86,9 @@ pub struct Device {
pub struct Surface { pub struct Surface {
surface: Option<GbmBufferedSurface<Rc<RefCell<GbmDevice<SessionFd>>>, SessionFd>>, surface: Option<GbmBufferedSurface<Rc<RefCell<GbmDevice<SessionFd>>>, SessionFd>>,
damage_tracker: DamageTrackedRenderer,
connector: connector::Handle, connector: connector::Handle,
output: Output, output: Output,
last_render: Option<(Dmabuf, Instant)>,
last_submit: Option<DrmEventTime>, last_submit: Option<DrmEventTime>,
refresh_rate: u32, refresh_rate: u32,
vrr: bool, vrr: bool,
@ -142,7 +145,8 @@ pub fn init_backend(
.map_err(|err| err.error) .map_err(|err| err.error)
.context("Failed to initialize session event source")?; .context("Failed to initialize session event source")?;
let api = GpuManager::new(EglGlesBackend, None).context("Failed to initialize renderers")?; let api = GpuManager::new(EglGlesBackend::<Gles2Renderer>::default(), None)
.context("Failed to initialize renderers")?;
// TODO get this info from system76-power, if available and setup a watcher // TODO get this info from system76-power, if available and setup a watcher
let primary = if let Some(path) = std::env::var("COSMIC_RENDER_DEVICE") let primary = if let Some(path) = std::env::var("COSMIC_RENDER_DEVICE")
@ -370,15 +374,7 @@ impl State {
Some(Ok(_)) => { Some(Ok(_)) => {
surface.last_submit = metadata.take().map(|data| data.time); surface.last_submit = metadata.take().map(|data| data.time);
surface.pending = false; surface.pending = false;
data.state data.state.common.send_frames(&surface.output);
.common
.shell
.active_space_mut(&surface.output)
.space
.send_frames(
data.state.common.start_time.elapsed().as_millis()
as u32,
);
} }
Some(Err(err)) => { Some(Err(err)) => {
slog_scope::warn!("Failed to submit frame: {}", err) slog_scope::warn!("Failed to submit frame: {}", err)
@ -689,12 +685,12 @@ impl Device {
let data = Surface { let data = Surface {
output: output.clone(), output: output.clone(),
damage_tracker: DamageTrackedRenderer::from_output(&output),
surface: None, surface: None,
connector: conn, connector: conn,
vrr, vrr,
refresh_rate, refresh_rate,
last_submit: None, last_submit: None,
last_render: None,
pending: false, pending: false,
render_timer_token: None, render_timer_token: None,
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
@ -717,8 +713,8 @@ fn render_node_for_output(
let workspace = shell.active_space(output); let workspace = shell.active_space(output);
let nodes = workspace let nodes = workspace
.get_fullscreen(output) .get_fullscreen(output)
.map(|w| vec![w]) .map(|w| vec![w.clone()])
.unwrap_or_else(|| workspace.space.windows().collect::<Vec<_>>()) .unwrap_or_else(|| workspace.windows().collect::<Vec<_>>())
.into_iter() .into_iter()
.flat_map(|w| { .flat_map(|w| {
dh.get_client(w.toplevel().wl_surface().id()) dh.get_client(w.toplevel().wl_surface().id())
@ -750,7 +746,7 @@ impl Surface {
pub fn render_output( pub fn render_output(
&mut self, &mut self,
dh: &DisplayHandle, dh: &DisplayHandle,
api: &mut GpuManager<EglGlesBackend>, api: &mut GpuManager<EglGlesBackend<Gles2Renderer>>,
target_node: &DrmNode, target_node: &DrmNode,
state: &mut Common, state: &mut Common,
) -> Result<()> { ) -> Result<()> {
@ -758,12 +754,8 @@ impl Surface {
return Ok(()); return Ok(());
} }
if render::needs_buffer_reset(&self.output, state) {
self.surface.as_mut().unwrap().reset_buffers();
}
let render_node = render_node_for_output(dh, &self.output, *target_node, &state.shell); let render_node = render_node_for_output(dh, &self.output, *target_node, &state.shell);
let mut renderer = api.renderer(&render_node, &target_node).unwrap(); let mut renderer: GlMultiRenderer = api.renderer(&render_node, &target_node).unwrap();
let surface = self.surface.as_mut().unwrap(); let surface = self.surface.as_mut().unwrap();
let (buffer, age) = surface let (buffer, age) = surface
@ -777,7 +769,8 @@ impl Surface {
match render::render_output( match render::render_output(
Some(&render_node), Some(&render_node),
&mut renderer, &mut renderer,
age, &mut self.damage_tracker,
age as usize,
state, state,
&self.output, &self.output,
false, false,
@ -785,7 +778,6 @@ impl Surface {
Some(&mut self.fps), Some(&mut self.fps),
) { ) {
Ok(_) => { Ok(_) => {
self.last_render = Some((buffer, Instant::now()));
surface surface
.queue_buffer() .queue_buffer()
.with_context(|| "Failed to submit buffer for display")?; .with_context(|| "Failed to submit buffer for display")?;
@ -1026,17 +1018,4 @@ impl KmsState {
} }
Ok(()) Ok(())
} }
pub fn capture_output(&self, output: &Output) -> Option<(DrmNode, Dmabuf, Instant)> {
self.devices.values().find_map(|dev| {
dev.surfaces
.values()
.find(|s| &s.output == output)
.and_then(|s| {
s.last_render
.clone()
.map(|(buf, time)| (dev.render_node.clone(), buf, time))
})
})
}
} }

View file

@ -39,18 +39,22 @@ pub fn init_backend_auto(
} }
} }
}; };
if res.is_ok() { if res.is_ok() {
for seat in &state.common.seats { let output = state
let output = state .common
.common .shell
.shell .outputs()
.outputs() .next()
.next() .with_context(|| "Backend initialized without output")?;
.with_context(|| "Backend initialized without output")? let initial_seat = crate::input::add_seat(
.clone(); dh,
seat.user_data() &mut state.common.seat_state,
.insert_if_missing(|| crate::input::ActiveOutput(std::cell::RefCell::new(output))); output,
} &state.common.config,
"seat-0".into(),
);
state.common.add_seat(initial_seat);
} }
res res
} }

View file

@ -2,14 +2,20 @@
use crate::utils::prelude::*; use crate::utils::prelude::*;
use smithay::{ use smithay::{
backend::renderer::{Frame, ImportAll, ImportMem, Renderer, Texture}, backend::renderer::{
desktop::space::{RenderElement, SpaceOutputTuple, SurfaceTree}, element::{
surface::{render_elements_from_surface_tree, WaylandSurfaceRenderElement},
texture::{TextureBuffer, TextureRenderElement},
},
ImportAll, ImportMem, Renderer,
},
input::{ input::{
pointer::{CursorImageAttributes, CursorImageStatus}, pointer::{CursorImageAttributes, CursorImageStatus},
Seat, Seat,
}, },
reexports::wayland_server::protocol::wl_surface, reexports::wayland_server::protocol::wl_surface,
utils::{IsAlive, Logical, Physical, Point, Rectangle, Scale, Size, Transform}, render_elements,
utils::{IsAlive, Logical, Point, Scale, Transform},
wayland::compositor::{get_role, with_states}, wayland::compositor::{get_role, with_states},
}; };
use std::{ use std::{
@ -119,13 +125,21 @@ fn load_icon(theme: &CursorTheme) -> Result<Vec<Image>, Error> {
parse_xcursor(&cursor_data).ok_or(Error::Parse) parse_xcursor(&cursor_data).ok_or(Error::Parse)
} }
pub fn draw_surface_cursor( render_elements! {
surface: wl_surface::WlSurface, pub CursorRenderElement<R> where R: ImportAll;
Static=TextureRenderElement<<R as Renderer>::TextureId>,
Surface=WaylandSurfaceRenderElement,
}
pub fn draw_surface_cursor<R: Renderer + ImportAll>(
surface: &wl_surface::WlSurface,
location: impl Into<Point<i32, Logical>>, location: impl Into<Point<i32, Logical>>,
) -> SurfaceTree scale: impl Into<Scale<f64>>,
) -> Vec<CursorRenderElement<R>>
where where
{ {
let mut position = location.into(); let mut position = location.into();
let scale = scale.into();
let h = with_states(&surface, |states| { let h = with_states(&surface, |states| {
states states
.data_map .data_map
@ -136,125 +150,26 @@ where
.hotspot .hotspot
}); });
position -= h; position -= h;
SurfaceTree {
surface, render_elements_from_surface_tree(surface, position.to_physical_precise_round(scale), scale)
position,
z_index: 100,
}
} }
pub fn draw_dnd_icon( pub fn draw_dnd_icon<R: Renderer + ImportAll>(
surface: wl_surface::WlSurface, surface: &wl_surface::WlSurface,
location: impl Into<Point<i32, Logical>>, location: impl Into<Point<i32, Logical>>,
) -> SurfaceTree { scale: impl Into<Scale<f64>>,
) -> Vec<CursorRenderElement<R>> {
if get_role(&surface) != Some("dnd_icon") { if get_role(&surface) != Some("dnd_icon") {
slog_scope::warn!( slog_scope::warn!(
"Trying to display as a dnd icon a surface that does not have the DndIcon role." "Trying to display as a dnd icon a surface that does not have the DndIcon role."
); );
} }
SurfaceTree { let scale = scale.into();
render_elements_from_surface_tree(
surface, surface,
position: location.into(), location.into().to_physical_precise_round(scale),
z_index: 100, scale,
} )
}
pub struct PointerElement<T: Texture> {
seat_id: usize,
texture: T,
position: Point<f64, Logical>,
size: Size<i32, Logical>,
new_frame: bool,
}
impl<T: Texture> PointerElement<T> {
pub fn new(
seat: &Seat<State>,
texture: T,
relative_pointer_pos: Point<f64, Logical>,
new_frame: bool,
) -> PointerElement<T> {
let size = texture.size().to_logical(1, Transform::Normal);
PointerElement {
seat_id: seat.id(),
texture,
position: relative_pointer_pos,
size,
new_frame,
}
}
}
impl<R> RenderElement<R> for PointerElement<<R as Renderer>::TextureId>
where
R: Renderer + ImportAll,
<R as Renderer>::TextureId: 'static,
{
fn id(&self) -> usize {
self.seat_id
}
fn location(&self, scale: impl Into<Scale<f64>>) -> Point<f64, Physical> {
self.position.to_physical(scale)
}
fn geometry(&self, scale: impl Into<Scale<f64>>) -> Rectangle<i32, Physical> {
Rectangle::from_loc_and_size(self.position, self.size.to_f64())
.to_physical(scale)
.to_i32_round()
}
fn accumulated_damage(
&self,
scale: impl Into<Scale<f64>>,
_: Option<SpaceOutputTuple<'_, '_>>,
) -> Vec<Rectangle<i32, Physical>> {
if self.new_frame {
let scale = scale.into();
vec![Rectangle::from_loc_and_size(
self.position.to_physical(scale).to_i32_round(),
self.size.to_physical_precise_round(scale),
)]
} else {
vec![]
}
}
fn opaque_regions(
&self,
_scale: impl Into<Scale<f64>>,
) -> Option<Vec<Rectangle<i32, Physical>>> {
None
}
fn draw(
&self,
_renderer: &mut R,
frame: &mut <R as Renderer>::Frame,
scale: impl Into<Scale<f64>>,
position: Point<f64, Physical>,
damage: &[Rectangle<i32, Physical>],
_log: &slog::Logger,
) -> Result<(), <R as Renderer>::Error> {
let scale = scale.into();
frame.render_texture_at(
&self.texture,
position.to_i32_round(),
1,
scale,
Transform::Normal,
&damage
.iter()
.copied()
.map(|mut rect| {
rect.loc -= self.position.to_physical(scale).to_i32_round();
rect
})
.collect::<Vec<_>>(),
1.0,
)?;
Ok(())
}
} }
struct CursorState { struct CursorState {
@ -273,15 +188,15 @@ impl Default for CursorState {
} }
} }
pub fn draw_cursor<R, I>( pub fn draw_cursor<R>(
renderer: &mut R, renderer: &mut R,
seat: &Seat<State>, seat: &Seat<State>,
location: Point<f64, Logical>, location: Point<f64, Logical>,
scale: Scale<f64>,
start_time: &std::time::Instant, start_time: &std::time::Instant,
draw_default: bool, draw_default: bool,
) -> Option<I> ) -> Vec<CursorRenderElement<R>>
where where
I: From<SurfaceTree> + From<PointerElement<<R as Renderer>::TextureId>>,
R: Renderer + ImportAll + ImportMem, R: Renderer + ImportAll + ImportMem,
<R as Renderer>::TextureId: Clone + 'static, <R as Renderer>::TextureId: Clone + 'static,
{ {
@ -302,50 +217,69 @@ where
}) })
.unwrap_or(CursorImageStatus::Default); .unwrap_or(CursorImageStatus::Default);
if let CursorImageStatus::Surface(wl_surface) = cursor_status { if let CursorImageStatus::Surface(ref wl_surface) = cursor_status {
Some(draw_surface_cursor(wl_surface.clone(), location.to_i32_round()).into()) return draw_surface_cursor(wl_surface, location.to_i32_round(), scale);
} else if draw_default { } else if draw_default {
let integer_scale = scale.x.max(scale.y).ceil() as u32;
let seat_userdata = seat.user_data(); let seat_userdata = seat.user_data();
seat_userdata.insert_if_missing(CursorState::default); seat_userdata.insert_if_missing(CursorState::default);
let state = seat_userdata.get::<CursorState>().unwrap(); let state = seat_userdata.get::<CursorState>().unwrap();
let frame = state let frame = state
.cursor .cursor
.get_image(1, start_time.elapsed().as_millis() as u32); .get_image(integer_scale, start_time.elapsed().as_millis() as u32);
let new_frame = state.current_image.borrow().as_ref() != Some(&frame);
let mut cache = state.image_cache.borrow_mut(); let mut cache = state.image_cache.borrow_mut();
let pointer_images = cache let pointer_images = cache
.entry((TypeId::of::<<R as Renderer>::TextureId>(), renderer.id())) .entry((
TypeId::of::<TextureBuffer<<R as Renderer>::TextureId>>(),
renderer.id(),
))
.or_default(); .or_default();
let pointer_image = pointer_images
let maybe_image = pointer_images
.iter() .iter()
.find_map(|(image, texture)| if image == &frame { Some(texture) } else { None }) .find_map(|(image, texture)| if image == &frame { Some(texture) } else { None })
.and_then(|texture| { .and_then(|texture| {
texture texture.downcast_ref::<TextureBuffer<<R as Renderer>::TextureId>>()
.downcast_ref::<<R as Renderer>::TextureId>()
.cloned()
})
.unwrap_or_else(|| {
let texture = renderer
.import_memory(
&frame.pixels_rgba,
(frame.width as i32, frame.height as i32).into(),
false,
)
.expect("Failed to import cursor bitmap");
pointer_images.push((frame.clone(), Box::new(texture.clone())));
texture
}); });
let pointer_image = match maybe_image {
Some(image) => image,
None => {
let texture = TextureBuffer::from_memory(
renderer,
&frame.pixels_rgba,
(frame.width as i32, frame.height as i32),
false,
integer_scale as i32,
Transform::Normal,
None,
)
.expect("Failed to import cursor bitmap");
pointer_images.push((frame.clone(), Box::new(texture.clone())));
pointer_images
.last()
.and_then(|(_, i)| {
i.downcast_ref::<TextureBuffer<<R as Renderer>::TextureId>>()
})
.unwrap()
}
};
let hotspot = let hotspot =
Point::<i32, Logical>::from((frame.xhot as i32, frame.yhot as i32)).to_f64(); Point::<i32, Logical>::from((frame.xhot as i32, frame.yhot as i32)).to_f64();
*state.current_image.borrow_mut() = Some(frame); *state.current_image.borrow_mut() = Some(frame);
Some( return vec![CursorRenderElement::Static(
PointerElement::new(seat, pointer_image.clone(), location - hotspot, new_frame) TextureRenderElement::from_texture_buffer(
.into(), (location - hotspot).to_physical(scale),
) pointer_image,
None,
None,
),
)];
} else { } else {
None Vec::new()
} }
} }
} }

View file

@ -6,162 +6,74 @@ use crate::{
state::Fps, state::Fps,
utils::prelude::*, utils::prelude::*,
}; };
//grabs::{MoveGrabRenderElement, SeatMoveGrabState},
use crate::{ use crate::{
shell::grabs::{MoveGrabRenderElement, SeatMoveGrabState}, shell::WorkspaceRenderElement, state::Common, wayland::handlers::data_device::get_dnd_icon,
state::Common,
wayland::handlers::data_device::get_dnd_icon,
}; };
use slog::Logger;
use smithay::{ use smithay::{
backend::{ backend::{
drm::DrmNode, drm::DrmNode,
renderer::{ renderer::{
gles2::{Gles2Renderbuffer, Gles2Renderer, Gles2Texture}, damage::{
multigpu::{egl::EglGlesBackend, Error as MultiError, MultiFrame, MultiRenderer}, DamageTrackedRenderer, DamageTrackedRendererError as RenderError, OutputNoMode,
Frame, ImportAll, Renderer, },
gles2::{Gles2Renderbuffer, Gles2Renderer},
multigpu::{egl::EglGlesBackend, MultiFrame, MultiRenderer},
ImportAll, ImportMem, Renderer,
}, },
}, },
desktop::{
draw_layer_popups, draw_layer_surface, draw_window, draw_window_popups,
layer_map_for_output,
space::{RenderElement, RenderError, SpaceOutputTuple, SurfaceTree},
utils::damage_from_surface_tree,
Window,
},
output::Output, output::Output,
utils::{Physical, Point, Rectangle, Scale, Transform}, utils::{Physical, Rectangle},
wayland::shell::wlr_layer::Layer as WlrLayer,
}; };
pub mod cursor; pub mod cursor;
use self::cursor::PointerElement; use self::cursor::CursorRenderElement;
pub type GlMultiRenderer<'a> = pub type GlMultiRenderer<'a> = MultiRenderer<
MultiRenderer<'a, 'a, EglGlesBackend, EglGlesBackend, Gles2Renderbuffer>; 'a,
pub type GlMultiFrame = MultiFrame<EglGlesBackend, EglGlesBackend>; 'a,
EglGlesBackend<Gles2Renderer>,
EglGlesBackend<Gles2Renderer>,
Gles2Renderbuffer,
>;
pub type GlMultiFrame = MultiFrame<EglGlesBackend<Gles2Renderer>, EglGlesBackend<Gles2Renderer>>;
static CLEAR_COLOR: [f32; 4] = [0.153, 0.161, 0.165, 1.0]; static CLEAR_COLOR: [f32; 4] = [0.153, 0.161, 0.165, 1.0];
smithay::custom_elements! { smithay::render_elements! {
pub CustomElem<=Gles2Renderer>; pub CosmicElement<R> where R: ImportAll;
SurfaceTree=SurfaceTree, WorkspaceElement=WorkspaceRenderElement<R>,
PointerElement=PointerElement::<Gles2Texture>, CursorElement=CursorRenderElement<R>,
MoveGrabRenderElement=MoveGrabRenderElement, //MoveGrabRenderElement=MoveGrabRenderElement,
#[cfg(feature = "debug")] //#[cfg(feature = "debug")]
EguiFrame=EguiFrame, //EguiFrame=EguiFrame,
} }
// TODO: due to the lifetime of MultiRenderer, we cannot be generic over CustomElem's renderer pub fn cursor_elements<E, R>(
// util after GATs land. So we generate with the macro for Gles2 and then
// do a manual impl for MultiRenderer.
impl RenderElement<GlMultiRenderer<'_>> for CustomElem {
fn id(&self) -> usize {
RenderElement::<Gles2Renderer>::id(self)
}
fn location(&self, scale: impl Into<Scale<f64>>) -> Point<f64, Physical> {
RenderElement::<Gles2Renderer>::location(self, scale)
}
fn geometry(&self, scale: impl Into<Scale<f64>>) -> Rectangle<i32, Physical> {
RenderElement::<Gles2Renderer>::geometry(self, scale)
}
fn accumulated_damage(
&self,
scale: impl Into<Scale<f64>>,
for_values: Option<SpaceOutputTuple<'_, '_>>,
) -> Vec<Rectangle<i32, Physical>> {
RenderElement::<Gles2Renderer>::accumulated_damage(self, scale, for_values)
}
fn opaque_regions(
&self,
scale: impl Into<Scale<f64>>,
) -> Option<Vec<Rectangle<i32, Physical>>> {
RenderElement::<Gles2Renderer>::opaque_regions(self, scale)
}
fn draw(
&self,
renderer: &mut GlMultiRenderer<'_>,
frame: &mut GlMultiFrame,
scale: impl Into<Scale<f64>>,
location: Point<f64, Physical>,
damage: &[Rectangle<i32, Physical>],
log: &Logger,
) -> Result<(), MultiError<EglGlesBackend, EglGlesBackend>> {
RenderElement::<Gles2Renderer>::draw(
self,
renderer.as_mut(),
frame.as_mut(),
scale,
location,
damage,
log,
)
.map_err(MultiError::Render)
}
fn z_index(&self) -> u8 {
RenderElement::<Gles2Renderer>::z_index(self)
}
}
pub trait AsGles2Renderer {
fn as_gles2(&mut self) -> &mut Gles2Renderer;
}
impl AsGles2Renderer for Gles2Renderer {
fn as_gles2(&mut self) -> &mut Gles2Renderer {
self
}
}
impl AsGles2Renderer for GlMultiRenderer<'_> {
fn as_gles2(&mut self) -> &mut Gles2Renderer {
self.as_mut()
}
}
pub fn needs_buffer_reset(output: &Output, state: &Common) -> bool {
use std::sync::atomic::{AtomicBool, Ordering};
struct DidCustomRendering(AtomicBool);
let will_render_custom = {
let workspace = state.shell.active_space(output);
workspace.get_fullscreen(output).is_some()
};
let userdata = output.user_data();
userdata.insert_if_missing(|| DidCustomRendering(AtomicBool::new(false)));
userdata
.get::<DidCustomRendering>()
.unwrap()
.0
.swap(will_render_custom, Ordering::AcqRel)
!= will_render_custom
}
pub fn cursor_custom_elements<R>(
renderer: &mut R, renderer: &mut R,
state: &Common, state: &Common,
output: &Output, output: &Output,
hardware_cursor: bool, hardware_cursor: bool,
) -> Vec<CustomElem> ) -> Vec<E>
where where
R: AsGles2Renderer, R: Renderer + ImportAll + ImportMem,
<R as Renderer>::TextureId: Clone + 'static,
E: From<CursorRenderElement<R>>,
{ {
let mut custom_elements = Vec::new(); let scale = output.current_scale().fractional_scale();
let mut elements = Vec::new();
for seat in &state.seats { for seat in state.seats() {
let pointer = match seat.get_pointer() { let pointer = match seat.get_pointer() {
Some(ptr) => ptr, Some(ptr) => ptr,
None => continue, None => continue,
}; };
let location = state let location = state
.shell .shell
.space_relative_output_geometry(pointer.current_location().to_i32_round(), output); .map_global_to_space(pointer.current_location().to_i32_round(), output);
/*
if let Some(grab) = seat if let Some(grab) = seat
.user_data() .user_data()
.get::<SeatMoveGrabState>() .get::<SeatMoveGrabState>()
@ -172,147 +84,92 @@ where
{ {
custom_elements.push(grab); custom_elements.push(grab);
} }
*/
if let Some(wl_surface) = get_dnd_icon(seat) { if let Some(wl_surface) = get_dnd_icon(seat) {
custom_elements.push(cursor::draw_dnd_icon(wl_surface, location.to_i32_round()).into()); elements.extend(
cursor::draw_dnd_icon(&wl_surface, location.to_i32_round(), scale)
.into_iter()
.map(E::from),
);
} }
if let Some(cursor) = cursor::draw_cursor( elements.extend(
renderer.as_gles2(), cursor::draw_cursor(
seat, renderer,
location, seat,
&state.start_time, location,
!hardware_cursor, scale.into(),
) { &state.start_time,
custom_elements.push(cursor) !hardware_cursor,
} )
.into_iter()
.map(E::from),
);
} }
custom_elements elements
} }
pub fn render_output<R>( pub fn render_output<R>(
gpu: Option<&DrmNode>, gpu: Option<&DrmNode>,
renderer: &mut R, renderer: &mut R,
age: u8, damage_tracker: &mut DamageTrackedRenderer,
age: usize,
state: &mut Common, state: &mut Common,
output: &Output, output: &Output,
hardware_cursor: bool, hardware_cursor: bool,
#[cfg(feature = "debug")] mut fps: Option<&mut Fps>, #[cfg(feature = "debug")] mut fps: Option<&mut Fps>,
) -> Result<Option<Vec<Rectangle<i32, Physical>>>, RenderError<R>> ) -> Result<Option<Vec<Rectangle<i32, Physical>>>, RenderError<R>>
where where
R: Renderer + ImportAll + AsGles2Renderer, R: Renderer + ImportAll + ImportMem,
<R as Renderer>::TextureId: Clone + 'static, <R as Renderer>::TextureId: Clone + 'static,
CustomElem: RenderElement<R>,
{ {
let workspace = state.shell.active_space(output).idx; let idx = state.shell.workspaces.active_num(output);
render_workspace( render_workspace(
gpu, gpu,
renderer, renderer,
damage_tracker,
age, age,
state, state,
workspace,
output, output,
idx,
hardware_cursor, hardware_cursor,
) )
} }
pub fn render_workspace<R>( pub fn render_workspace<R>(
gpu: Option<&DrmNode>, _gpu: Option<&DrmNode>,
renderer: &mut R, renderer: &mut R,
age: u8, damage_tracker: &mut DamageTrackedRenderer,
age: usize,
state: &mut Common, state: &mut Common,
space_idx: u8,
output: &Output, output: &Output,
idx: usize,
hardware_cursor: bool, hardware_cursor: bool,
#[cfg(feature = "debug")] mut fps: Option<&mut Fps>, #[cfg(feature = "debug")] mut fps: Option<&mut Fps>,
) -> Result<Option<Vec<Rectangle<i32, Physical>>>, RenderError<R>> ) -> Result<Option<Vec<Rectangle<i32, Physical>>>, RenderError<R>>
where where
R: Renderer + ImportAll + AsGles2Renderer, R: Renderer + ImportAll + ImportMem,
<R as Renderer>::TextureId: Clone + 'static, <R as Renderer>::TextureId: Clone + 'static,
CustomElem: RenderElement<R>,
{ {
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
if let Some(ref mut fps) = fps { if let Some(ref mut fps) = fps {
fps.start(); fps.start();
} }
let space_idx = space_idx as usize; let workspace = &state
let workspace = &mut state.shell.spaces[space_idx]; .shell
let maybe_fullscreen_window = workspace.get_fullscreen(output).cloned(); .workspaces
.get(idx, output)
.ok_or(OutputNoMode)?;
let res = if let Some(window) = maybe_fullscreen_window { let mut elements: Vec<CosmicElement<R>> =
#[cfg(not(feature = "debug"))] cursor_elements(renderer, state, output, hardware_cursor);
{
render_fullscreen(gpu, renderer, window, state, output, hardware_cursor)
}
#[cfg(feature = "debug")]
{
render_fullscreen(
gpu,
renderer,
window,
state,
output,
hardware_cursor,
fps.as_deref_mut(),
)
}
} else {
#[cfg(not(feature = "debug"))]
{
render_desktop(
gpu,
renderer,
age,
state,
space_idx,
output,
hardware_cursor,
)
}
#[cfg(feature = "debug")]
{
render_desktop(
gpu,
renderer,
age,
state,
space_idx,
output,
hardware_cursor,
fps.as_deref_mut(),
)
}
};
#[cfg(feature = "debug")]
if let Some(ref mut fps) = fps {
fps.end();
}
res
}
fn render_desktop<R>(
_gpu: Option<&DrmNode>,
renderer: &mut R,
age: u8,
state: &mut Common,
space_idx: usize,
output: &Output,
hardware_cursor: bool,
#[cfg(feature = "debug")] fps: Option<&mut Fps>,
) -> Result<Option<Vec<Rectangle<i32, Physical>>>, RenderError<R>>
where
R: Renderer + ImportAll + AsGles2Renderer,
<R as Renderer>::TextureId: Clone + 'static,
CustomElem: RenderElement<R>,
{
let mut custom_elements = Vec::<CustomElem>::new();
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
{ {
// TODO add debug elements
let workspace = &state.shell.spaces[space_idx]; let workspace = &state.shell.spaces[space_idx];
let output_geo = workspace let output_geo = workspace
.space .space
@ -346,132 +203,20 @@ where
} }
} }
custom_elements.extend(cursor_custom_elements( elements.extend(
renderer, workspace
state, .render_output(output)
output, .map_err(|_| OutputNoMode)?
hardware_cursor, .into_iter()
)); .map(Into::into),
);
state.shell.spaces[space_idx].space.render_output( let res = damage_tracker.render_output(renderer, age, &elements, CLEAR_COLOR, None);
renderer,
&output,
age as usize,
CLEAR_COLOR,
&*custom_elements,
)
}
fn render_fullscreen<R>(
_gpu: Option<&DrmNode>,
renderer: &mut R,
window: Window,
state: &mut Common,
output: &Output,
hardware_cursor: bool,
#[cfg(feature = "debug")] fps: Option<&mut Fps>,
) -> Result<Option<Vec<Rectangle<i32, Physical>>>, RenderError<R>>
where
R: Renderer + ImportAll + AsGles2Renderer,
<R as Renderer>::TextureId: Clone + 'static,
CustomElem: RenderElement<R>,
{
let transform = Transform::from(output.current_transform());
let mode = output.current_mode().unwrap();
let scale = output.current_scale().fractional_scale();
let mut custom_elements = Vec::<CustomElem>::new();
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
if let Some(fps) = fps { if let Some(ref mut fps) = fps {
let output_geo = output.geometry(); fps.end();
let fps_overlay = fps_ui(
_gpu,
state,
fps,
Rectangle::from_loc_and_size((0, 0), output_geo.size)
.to_f64()
.to_physical(scale),
scale,
);
custom_elements.push(fps_overlay.into());
} }
custom_elements.extend(cursor_custom_elements( res
renderer,
state,
output,
hardware_cursor,
));
renderer
.render(mode.size, transform, |renderer, frame| {
let mut damage = window.accumulated_damage((0.0, 0.0), scale, None);
frame.clear(
CLEAR_COLOR,
&[Rectangle::from_loc_and_size((0, 0), mode.size)],
)?;
draw_window(
renderer,
frame,
&window,
scale,
(0.0, 0.0),
&[Rectangle::from_loc_and_size((0, 0), mode.size)],
&slog_scope::logger(),
)?;
draw_window_popups(
renderer,
frame,
&window,
scale,
(0.0, 0.0),
&[Rectangle::from_loc_and_size((0, 0), mode.size)],
&slog_scope::logger(),
)?;
let layer_map = layer_map_for_output(output);
for layer_surface in layer_map.layers_on(WlrLayer::Overlay) {
let geo = layer_map.layer_geometry(&layer_surface).unwrap();
draw_layer_surface(
renderer,
frame,
layer_surface,
scale,
geo.loc.to_f64().to_physical(scale),
&[Rectangle::from_loc_and_size(
(0, 0),
geo.size.to_physical_precise_round(scale),
)],
&slog_scope::logger(),
)?;
draw_layer_popups(
renderer,
frame,
layer_surface,
scale,
geo.loc.to_f64().to_physical(scale),
&[Rectangle::from_loc_and_size(
(0, 0),
geo.size.to_physical_precise_round(scale),
)],
&slog_scope::logger(),
)?;
damage.extend(damage_from_surface_tree(
layer_surface.wl_surface(),
geo.loc.to_f64().to_physical(scale),
scale,
None,
));
}
for elem in custom_elements {
let loc = elem.location(scale);
let geo = elem.geometry(scale);
let elem_damage = elem.accumulated_damage(scale, None);
elem.draw(renderer, frame, scale, loc, &[geo], &slog_scope::logger())?;
damage.extend(elem_damage)
}
Ok(Some(damage))
})
.and_then(std::convert::identity)
.map_err(RenderError::<R>::Rendering)
} }

View file

@ -10,7 +10,7 @@ use crate::{
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use smithay::{ use smithay::{
backend::{ backend::{
renderer::{ImportDma, ImportEgl}, renderer::{damage::DamageTrackedRenderer, ImportDma, ImportEgl},
winit::{self, WinitEvent, WinitGraphicsBackend, WinitVirtualDevice}, winit::{self, WinitEvent, WinitGraphicsBackend, WinitVirtualDevice},
}, },
desktop::layer_map_for_output, desktop::layer_map_for_output,
@ -30,31 +30,23 @@ pub struct WinitState {
// The winit backend currently has no notion of multiple windows // The winit backend currently has no notion of multiple windows
pub backend: WinitGraphicsBackend, pub backend: WinitGraphicsBackend,
output: Output, output: Output,
age_reset: u8, damage_tracker: DamageTrackedRenderer,
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
fps: Fps, fps: Fps,
} }
impl WinitState { impl WinitState {
pub fn render_output(&mut self, state: &mut Common) -> Result<()> { pub fn render_output(&mut self, state: &mut Common) -> Result<()> {
if render::needs_buffer_reset(&self.output, state) {
self.reset_buffers();
}
self.backend self.backend
.bind() .bind()
.with_context(|| "Failed to bind buffer")?; .with_context(|| "Failed to bind buffer")?;
let age = if self.age_reset > 0 { let age = self.backend.buffer_age().unwrap_or(0);
self.age_reset -= 1;
0
} else {
self.backend.buffer_age().unwrap_or(0)
};
match render::render_output( match render::render_output(
None, None,
self.backend.renderer(), self.backend.renderer(),
age as u8, &mut self.damage_tracker,
age,
state, state,
&self.output, &self.output,
true, true,
@ -62,11 +54,7 @@ impl WinitState {
Some(&mut self.fps), Some(&mut self.fps),
) { ) {
Ok(damage) => { Ok(damage) => {
state state.send_frames(&self.output);
.shell
.active_space_mut(&self.output)
.space
.send_frames(state.start_time.elapsed().as_millis() as u32);
self.backend self.backend
.submit(damage.as_ref().map(|x| &**x)) .submit(damage.as_ref().map(|x| &**x))
.with_context(|| "Failed to submit buffer for display")?; .with_context(|| "Failed to submit buffer for display")?;
@ -104,10 +92,6 @@ impl WinitState {
Ok(()) Ok(())
} }
} }
pub fn reset_buffers(&mut self) {
self.age_reset = 3;
}
} }
pub fn init_backend( pub fn init_backend(
@ -180,8 +164,7 @@ pub fn init_backend(
.handle() .handle()
.insert_source(event_source, move |_, _, data| { .insert_source(event_source, move |_, _, data| {
match input.dispatch_new_events(|event| { match input.dispatch_new_events(|event| {
data.state data.state.process_winit_event(event, &render_ping_handle)
.process_winit_event(&data.display.handle(), event, &render_ping_handle)
}) { }) {
Ok(_) => { Ok(_) => {
event_ping_handle.ping(); event_ping_handle.ping();
@ -202,9 +185,9 @@ pub fn init_backend(
state.backend = BackendData::Winit(WinitState { state.backend = BackendData::Winit(WinitState {
backend, backend,
output: output.clone(), output: output.clone(),
damage_tracker: DamageTrackedRenderer::from_output(&output),
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
fps: Fps::default(), fps: Fps::default(),
age_reset: 0,
}); });
state state
.common .common
@ -250,19 +233,14 @@ fn init_egl_client_side(
} }
impl State { impl State {
pub fn process_winit_event( pub fn process_winit_event(&mut self, event: WinitEvent, render_ping: &ping::Ping) {
&mut self,
dh: &DisplayHandle,
event: WinitEvent,
render_ping: &ping::Ping,
) {
// here we can handle special cases for winit inputs // here we can handle special cases for winit inputs
match event { match event {
WinitEvent::Focus(true) => { WinitEvent::Focus(true) => {
for seat in self.common.seats.clone().iter() { for seat in self.common.seats().cloned().collect::<Vec<_>>().iter() {
let devices = seat.user_data().get::<Devices>().unwrap(); let devices = seat.user_data().get::<Devices>().unwrap();
if devices.has_device(&WinitVirtualDevice) { if devices.has_device(&WinitVirtualDevice) {
set_active_output(seat, &self.backend.winit().output); seat.set_active_output(&self.backend.winit().output);
break; break;
} }
} }
@ -286,7 +264,7 @@ impl State {
output.delete_mode(output.current_mode().unwrap()); output.delete_mode(output.current_mode().unwrap());
output.set_preferred(mode); output.set_preferred(mode);
output.change_current_state(Some(mode), None, None, None); output.change_current_state(Some(mode), None, None, None);
layer_map_for_output(output).arrange(dh); layer_map_for_output(output).arrange();
self.common.output_configuration_state.update(); self.common.output_configuration_state.update();
self.common.shell.refresh_outputs(); self.common.shell.refresh_outputs();
render_ping.ping(); render_ping.ping();

View file

@ -13,7 +13,9 @@ use smithay::{
allocator::dmabuf::Dmabuf, allocator::dmabuf::Dmabuf,
egl::{EGLContext, EGLDisplay}, egl::{EGLContext, EGLDisplay},
input::{Event, InputEvent}, input::{Event, InputEvent},
renderer::{gles2::Gles2Renderer, Bind, ImportDma, ImportEgl}, renderer::{
damage::DamageTrackedRenderer, gles2::Gles2Renderer, Bind, ImportDma, ImportEgl,
},
x11::{Window, WindowBuilder, X11Backend, X11Event, X11Handle, X11Input, X11Surface}, x11::{Window, WindowBuilder, X11Backend, X11Event, X11Handle, X11Input, X11Surface},
}, },
desktop::layer_map_for_output, desktop::layer_map_for_output,
@ -114,6 +116,7 @@ impl X11State {
self.surfaces.push(Surface { self.surfaces.push(Surface {
window, window,
surface, surface,
damage_tracker: DamageTrackedRenderer::from_output(&output),
output: output.clone(), output: output.clone(),
render: ping.clone(), render: ping.clone(),
dirty: false, dirty: false,
@ -168,6 +171,7 @@ impl X11State {
pub struct Surface { pub struct Surface {
window: Window, window: Window,
damage_tracker: DamageTrackedRenderer,
surface: X11Surface, surface: X11Surface,
output: Output, output: Output,
render: ping::Ping, render: ping::Ping,
@ -183,10 +187,6 @@ impl Surface {
renderer: &mut Gles2Renderer, renderer: &mut Gles2Renderer,
state: &mut Common, state: &mut Common,
) -> Result<()> { ) -> Result<()> {
if render::needs_buffer_reset(&self.output, state) {
self.surface.reset_buffers();
}
let (buffer, age) = self let (buffer, age) = self
.surface .surface
.buffer() .buffer()
@ -198,7 +198,8 @@ impl Surface {
match render::render_output( match render::render_output(
None, None,
renderer, renderer,
age as u8, &mut self.damage_tracker,
age as usize,
state, state,
&self.output, &self.output,
true, true,
@ -206,11 +207,7 @@ impl Surface {
Some(&mut self.fps), Some(&mut self.fps),
) { ) {
Ok(_) => { Ok(_) => {
state state.send_frames(&self.output);
.shell
.active_space_mut(&self.output)
.space
.send_frames(state.start_time.elapsed().as_millis() as u32);
self.surface self.surface
.submit() .submit()
.with_context(|| "Failed to submit buffer for display")?; .with_context(|| "Failed to submit buffer for display")?;
@ -336,7 +333,7 @@ pub fn init_backend(
output.delete_mode(output.current_mode().unwrap()); output.delete_mode(output.current_mode().unwrap());
output.change_current_state(Some(mode), None, None, None); output.change_current_state(Some(mode), None, None, None);
output.set_preferred(mode); output.set_preferred(mode);
layer_map_for_output(output).arrange(&data.display.handle()); layer_map_for_output(output).arrange();
data.state.common.output_configuration_state.update(); data.state.common.output_configuration_state.update();
data.state.common.shell.refresh_outputs(); data.state.common.shell.refresh_outputs();
surface.dirty = true; surface.dirty = true;
@ -405,10 +402,10 @@ impl State {
.unwrap(); .unwrap();
let device = event.device(); let device = event.device();
for seat in self.common.seats.clone().iter() { for seat in self.common.seats().cloned().collect::<Vec<_>>().iter() {
let devices = seat.user_data().get::<Devices>().unwrap(); let devices = seat.user_data().get::<Devices>().unwrap();
if devices.has_device(&device) { if devices.has_device(&device) {
set_active_output(seat, &output); seat.set_active_output(&output);
break; break;
} }
} }

View file

@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only // SPDX-License-Identifier: GPL-3.0-only
use crate::{ use crate::{
shell::{focus::FocusDirection, Shell}, shell::{focus::FocusDirection, Shell, WorkspaceAmount},
state::{BackendData, Data}, state::{BackendData, Data},
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -32,6 +32,7 @@ pub struct Config {
pub struct StaticConfig { pub struct StaticConfig {
pub key_bindings: HashMap<KeyPattern, Action>, pub key_bindings: HashMap<KeyPattern, Action>,
pub workspace_mode: WorkspaceMode, pub workspace_mode: WorkspaceMode,
pub workspace_amount: WorkspaceAmount,
pub floating_default: bool, pub floating_default: bool,
} }
@ -215,6 +216,7 @@ impl Config {
StaticConfig { StaticConfig {
key_bindings: HashMap::new(), key_bindings: HashMap::new(),
workspace_mode: WorkspaceMode::Global, workspace_mode: WorkspaceMode::Global,
workspace_amount: WorkspaceAmount::Dynamic,
floating_default: false, floating_default: false,
} }
} }
@ -787,6 +789,6 @@ pub enum Action {
ToggleTiling, ToggleTiling,
ToggleWindowFloating, ToggleWindowFloating,
Fullscreen, Fullscreen,
Screenshot, //Screenshot,
Spawn(String), Spawn(String),
} }

View file

@ -2,7 +2,7 @@
use crate::{ use crate::{
config::{Action, Config}, config::{Action, Config},
shell::{grabs::SeatMoveGrabState, Workspace}, shell::{focus::target::PointerFocusTarget, Workspace}, // shell::grabs::SeatMoveGrabState
state::Common, state::Common,
utils::prelude::*, utils::prelude::*,
}; };
@ -11,17 +11,17 @@ use smithay::{
AbsolutePositionEvent, Axis, AxisSource, Device, DeviceCapability, InputBackend, AbsolutePositionEvent, Axis, AxisSource, Device, DeviceCapability, InputBackend,
InputEvent, KeyState, PointerAxisEvent, InputEvent, KeyState, PointerAxisEvent,
}, },
desktop::{layer_map_for_output, Kind, WindowSurfaceType}, desktop::layer_map_for_output,
input::{ input::{
keyboard::{keysyms, FilterResult, KeysymHandle, XkbConfig}, keyboard::{keysyms, FilterResult, KeysymHandle, XkbConfig},
pointer::{AxisFrame, ButtonEvent, CursorImageStatus, MotionEvent}, pointer::{AxisFrame, ButtonEvent, CursorImageStatus, MotionEvent},
Seat, SeatState, Seat, SeatState,
}, },
output::Output, output::Output,
reexports::wayland_server::{protocol::wl_surface::WlSurface, DisplayHandle}, reexports::wayland_server::DisplayHandle,
utils::{Buffer, Logical, Point, Rectangle, Size, SERIAL_COUNTER}, utils::{Logical, Point, Rectangle, SERIAL_COUNTER},
wayland::{ wayland::{
keyboard_shortcuts_inhibit::KeyboardShortcutsInhibitorSeat, keyboard_shortcuts_inhibit::KeyboardShortcutsInhibitorSeat, seat::WaylandFocus,
shell::wlr_layer::Layer as WlrLayer, shell::wlr_layer::Layer as WlrLayer,
}, },
}; };
@ -103,6 +103,7 @@ impl Devices {
pub fn add_seat( pub fn add_seat(
dh: &DisplayHandle, dh: &DisplayHandle,
seat_state: &mut SeatState<State>, seat_state: &mut SeatState<State>,
output: &Output,
config: &Config, config: &Config,
name: String, name: String,
) -> Seat<State> { ) -> Seat<State> {
@ -111,7 +112,8 @@ pub fn add_seat(
userdata.insert_if_missing(SeatId::default); userdata.insert_if_missing(SeatId::default);
userdata.insert_if_missing(Devices::default); userdata.insert_if_missing(Devices::default);
userdata.insert_if_missing(SupressedKeys::default); userdata.insert_if_missing(SupressedKeys::default);
userdata.insert_if_missing(SeatMoveGrabState::default); //userdata.insert_if_missing(SeatMoveGrabState::default);
userdata.insert_if_missing(|| ActiveOutput(RefCell::new(output.clone())));
userdata.insert_if_missing(|| RefCell::new(CursorImageStatus::Default)); userdata.insert_if_missing(|| RefCell::new(CursorImageStatus::Default));
// A lot of clients bind keyboard and pointer unconditionally once on launch.. // A lot of clients bind keyboard and pointer unconditionally once on launch..
@ -143,7 +145,7 @@ impl State {
match event { match event {
InputEvent::DeviceAdded { device } => { InputEvent::DeviceAdded { device } => {
let seat = &mut self.common.last_active_seat; let seat = &mut self.common.last_active_seat();
let userdata = seat.user_data(); let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap(); let devices = userdata.get::<Devices>().unwrap();
for cap in devices.add_device(&device) { for cap in devices.add_device(&device) {
@ -159,7 +161,7 @@ impl State {
} }
} }
InputEvent::DeviceRemoved { device } => { InputEvent::DeviceRemoved { device } => {
for seat in &mut self.common.seats { for seat in &mut self.common.seats() {
let userdata = seat.user_data(); let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap(); let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) { if devices.has_device(&device) {
@ -182,16 +184,17 @@ impl State {
use smithay::backend::input::KeyboardKeyEvent; use smithay::backend::input::KeyboardKeyEvent;
let device = event.device(); let device = event.device();
for seat in self.common.seats.clone().iter() { for seat in self.common.seats().cloned().collect::<Vec<_>>().iter() {
let current_output = active_output(seat, &self.common); let current_output = seat.active_output();
let workspace = self.common.shell.active_space_mut(&current_output); let workspace = self.common.shell.active_space_mut(&current_output);
let shortcuts_inhibited = workspace let shortcuts_inhibited = workspace
.focus_stack(seat) .focus_stack
.get(seat)
.last() .last()
.and_then(|window| { .and_then(|window| {
seat.keyboard_shortcuts_inhibitor_for_surface( window.wl_surface().and_then(|surface| {
&window.toplevel().wl_surface(), seat.keyboard_shortcuts_inhibitor_for_surface(&surface)
) })
}) })
.map(|inhibitor| inhibitor.is_active()) .map(|inhibitor| inhibitor.is_active())
.unwrap_or(false); .unwrap_or(false);
@ -309,34 +312,31 @@ impl State {
slog_scope::info!("Debug overlay not included in this version") slog_scope::info!("Debug overlay not included in this version")
} }
Action::Close => { Action::Close => {
let current_output = active_output(seat, &self.common); let current_output = seat.active_output();
let workspace = let workspace =
self.common.shell.active_space_mut(&current_output); self.common.shell.active_space_mut(&current_output);
if let Some(window) = workspace.focus_stack(seat).last() { if let Some(window) = workspace.focus_stack.get(seat).last() {
#[allow(irrefutable_let_patterns)] window.send_close();
if let Kind::Xdg(xdg) = &window.toplevel() {
xdg.send_close();
}
} }
} }
Action::Workspace(key_num) => { Action::Workspace(key_num) => {
let current_output = active_output(seat, &self.common); let current_output = seat.active_output();
let workspace = match key_num { let workspace = match key_num {
0 => 9, 0 => 9,
x => x - 1, x => x - 1,
}; };
if let Some(motion_event) = self.common.shell.activate( if let Some(motion_event) = self
seat, .common
&current_output, .shell
workspace as usize, .activate(&current_output, workspace as usize)
) { {
if let Some(ptr) = seat.get_pointer() { if let Some(ptr) = seat.get_pointer() {
ptr.motion(self, None, &motion_event); ptr.motion(self, None, &motion_event);
} }
} }
} }
Action::MoveToWorkspace(key_num) => { Action::MoveToWorkspace(key_num) => {
let current_output = active_output(seat, &self.common); let current_output = seat.active_output();
let workspace = match key_num { let workspace = match key_num {
0 => 9, 0 => 9,
x => x - 1, x => x - 1,
@ -348,52 +348,47 @@ impl State {
); );
} }
Action::Focus(focus) => { Action::Focus(focus) => {
let current_output = active_output(seat, &self.common); let current_output = seat.active_output();
let workspace = let workspace =
self.common.shell.active_space_mut(&current_output); self.common.shell.active_space_mut(&current_output);
let focus_stack = workspace.focus_stack(seat); let focus_stack = workspace.focus_stack.get(seat);
if let Some(window) = workspace.tiling_layer.move_focus( if let Some(target) = workspace.tiling_layer.next_focus(
focus, focus,
seat, seat,
&mut workspace.space,
focus_stack.iter(), focus_stack.iter(),
) { ) {
std::mem::drop(focus_stack); std::mem::drop(focus_stack);
Common::set_focus( Common::set_focus(self, Some(&target), seat, None);
self,
Some(window.toplevel().wl_surface()),
seat,
None,
);
} }
} }
Action::Fullscreen => { Action::Fullscreen => {
let current_output = active_output(seat, &self.common); let current_output = seat.active_output();
let workspace = let workspace =
self.common.shell.active_space_mut(&current_output); self.common.shell.active_space_mut(&current_output);
let focused_window = workspace.focus_stack(seat).last(); let focus_stack = workspace.focus_stack.get(seat);
if let Some(window) = focused_window { let focused_window = focus_stack.last();
if let Some(window) = focused_window.map(|f| f.active_window())
{
workspace.fullscreen_toggle(&window, &current_output); workspace.fullscreen_toggle(&window, &current_output);
} }
} }
Action::Orientation(orientation) => { Action::Orientation(orientation) => {
let output = active_output(seat, &self.common); let output = seat.active_output();
let workspace = self.common.shell.active_space_mut(&output); let workspace = self.common.shell.active_space_mut(&output);
let focus_stack = workspace.focus_stack(seat); let focus_stack = workspace.focus_stack.get(seat);
workspace.tiling_layer.update_orientation( workspace.tiling_layer.update_orientation(
orientation, orientation,
&seat, &seat,
&mut workspace.space,
focus_stack.iter(), focus_stack.iter(),
); );
} }
Action::ToggleTiling => { Action::ToggleTiling => {
let output = active_output(seat, &self.common); let output = seat.active_output();
let workspace = self.common.shell.active_space_mut(&output); let workspace = self.common.shell.active_space_mut(&output);
workspace.toggle_tiling(seat); workspace.toggle_tiling(seat);
} }
Action::ToggleWindowFloating => { Action::ToggleWindowFloating => {
let output = active_output(seat, &self.common); let output = seat.active_output();
let workspace = self.common.shell.active_space_mut(&output); let workspace = self.common.shell.active_space_mut(&output);
workspace.toggle_floating_window(seat); workspace.toggle_floating_window(seat);
} }
@ -407,80 +402,81 @@ impl State {
{ {
slog_scope::warn!("Failed to spawn: {}", err); slog_scope::warn!("Failed to spawn: {}", err);
} }
} } /*
Action::Screenshot => { Action::Screenshot => {
let home = match std::env::var("HOME") { let home = match std::env::var("HOME") {
Ok(home) => home, Ok(home) => home,
Err(err) => { Err(err) => {
slog_scope::error!( slog_scope::error!(
"$HOME is not set, can't save screenshots: {}", "$HOME is not set, can't save screenshots: {}",
err err
); );
break; break;
} }
}; };
let timestamp = match std::time::SystemTime::UNIX_EPOCH let timestamp = match std::time::SystemTime::UNIX_EPOCH
.elapsed() .elapsed()
{ {
Ok(duration) => duration.as_secs(), Ok(duration) => duration.as_secs(),
Err(err) => { Err(err) => {
slog_scope::error!("Unable to get timestamp, can't save screenshots: {}", err); slog_scope::error!("Unable to get timestamp, can't save screenshots: {}", err);
break; break;
} }
}; };
for output in self.common.shell.outputs.clone().into_iter() { for output in self.common.shell.outputs.clone().into_iter() {
match self match self
.backend .backend
.offscreen_for_output(&output, &mut self.common) .offscreen_for_output(&output, &mut self.common)
{ {
Ok((buffer, size)) => { Ok((buffer, size)) => {
let mut path = std::path::PathBuf::new(); let mut path = std::path::PathBuf::new();
path.push(&home); path.push(&home);
path.push(format!( path.push(format!(
"{}_{}.png", "{}_{}.png",
output.name(), output.name(),
timestamp timestamp
)); ));
fn write_png( fn write_png(
path: impl AsRef<std::path::Path>, path: impl AsRef<std::path::Path>,
data: Vec<u8>, data: Vec<u8>,
size: Size<i32, Buffer>, size: Size<i32, Buffer>,
) -> anyhow::Result<()> ) -> anyhow::Result<()>
{ {
use std::{fs, io}; use std::{fs, io};
let file = io::BufWriter::new( let file = io::BufWriter::new(
fs::File::create(&path)?, fs::File::create(&path)?,
); );
let mut encoder = png::Encoder::new( let mut encoder = png::Encoder::new(
file, file,
size.w as u32, size.w as u32,
size.h as u32, size.h as u32,
); );
encoder.set_color(png::ColorType::Rgba); encoder.set_color(png::ColorType::Rgba);
encoder.set_depth(png::BitDepth::Eight); encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder.write_header()?; let mut writer = encoder.write_header()?;
writer.write_image_data(&data)?; writer.write_image_data(&data)?;
Ok(()) Ok(())
} }
if let Err(err) = write_png(&path, buffer, size) { if let Err(err) = write_png(&path, buffer, size) {
slog_scope::error!( slog_scope::error!(
"Unable to save screenshot at {}: {}", "Unable to save screenshot at {}: {}",
path.display(), path.display(),
err err
); );
} }
} }
Err(err) => slog_scope::error!( Err(err) => slog_scope::error!(
"Could not save screenshot for output {}: {}", "Could not save screenshot for output {}: {}",
output.name(), output.name(),
err err
), ),
} }
} }
} }
*/
} }
} }
break; break;
@ -491,11 +487,11 @@ impl State {
use smithay::backend::input::PointerMotionEvent; use smithay::backend::input::PointerMotionEvent;
let device = event.device(); let device = event.device();
for seat in self.common.seats.clone().iter() { for seat in self.common.seats().cloned().collect::<Vec<_>>().iter() {
let userdata = seat.user_data(); let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap(); let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) { if devices.has_device(&device) {
let current_output = active_output(seat, &self.common); let current_output = seat.active_output();
let mut position = seat.get_pointer().unwrap().current_location(); let mut position = seat.get_pointer().unwrap().current_location();
position += event.delta(); position += event.delta();
@ -508,7 +504,7 @@ impl State {
.cloned() .cloned()
.unwrap_or(current_output.clone()); .unwrap_or(current_output.clone());
if output != current_output { if output != current_output {
set_active_output(seat, &output); seat.set_active_output(&output);
} }
let output_geometry = output.geometry(); let output_geometry = output.geometry();
@ -520,10 +516,7 @@ impl State {
.min((output_geometry.loc.y + output_geometry.size.h) as f64); .min((output_geometry.loc.y + output_geometry.size.h) as f64);
let serial = SERIAL_COUNTER.next_serial(); let serial = SERIAL_COUNTER.next_serial();
let relative_pos = self let relative_pos = self.common.shell.map_global_to_space(position, &output);
.common
.shell
.space_relative_output_geometry(position, &output);
let workspace = self.common.shell.active_space_mut(&output); let workspace = self.common.shell.active_space_mut(&output);
let under = State::surface_under( let under = State::surface_under(
position, position,
@ -559,18 +552,15 @@ impl State {
} }
InputEvent::PointerMotionAbsolute { event, .. } => { InputEvent::PointerMotionAbsolute { event, .. } => {
let device = event.device(); let device = event.device();
for seat in self.common.seats.clone().iter() { for seat in self.common.seats().cloned().collect::<Vec<_>>().iter() {
let userdata = seat.user_data(); let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap(); let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) { if devices.has_device(&device) {
let output = active_output(seat, &self.common); let output = seat.active_output();
let geometry = output.geometry(); let geometry = output.geometry();
let position = let position =
geometry.loc.to_f64() + event.position_transformed(geometry.size); geometry.loc.to_f64() + event.position_transformed(geometry.size);
let relative_pos = self let relative_pos = self.common.shell.map_global_to_space(position, &output);
.common
.shell
.space_relative_output_geometry(position, &output);
let workspace = self.common.shell.active_space_mut(&output); let workspace = self.common.shell.active_space_mut(&output);
let serial = SERIAL_COUNTER.next_serial(); let serial = SERIAL_COUNTER.next_serial();
let under = State::surface_under( let under = State::surface_under(
@ -609,7 +599,7 @@ impl State {
use smithay::backend::input::{ButtonState, PointerButtonEvent}; use smithay::backend::input::{ButtonState, PointerButtonEvent};
let device = event.device(); let device = event.device();
for seat in self.common.seats.clone().iter() { for seat in self.common.seats().cloned().collect::<Vec<_>>().iter() {
let userdata = seat.user_data(); let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap(); let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) { if devices.has_device(&device) {
@ -649,13 +639,10 @@ impl State {
if !seat.get_pointer().unwrap().is_grabbed() if !seat.get_pointer().unwrap().is_grabbed()
&& !seat.get_keyboard().map(|k| k.is_grabbed()).unwrap_or(false) && !seat.get_keyboard().map(|k| k.is_grabbed()).unwrap_or(false)
{ {
let output = active_output(seat, &self.common); let output = seat.active_output();
let pos = seat.get_pointer().unwrap().current_location(); let pos = seat.get_pointer().unwrap().current_location();
let output_geo = output.geometry(); let relative_pos =
let relative_pos = self self.common.shell.map_global_to_space(pos, &output);
.common
.shell
.space_relative_output_geometry(pos, &output);
let workspace = self.common.shell.active_space_mut(&output); let workspace = self.common.shell.active_space_mut(&output);
let layers = layer_map_for_output(&output); let layers = layer_map_for_output(&output);
let mut under = None; let mut under = None;
@ -665,23 +652,10 @@ impl State {
layers.layer_under(WlrLayer::Overlay, relative_pos) layers.layer_under(WlrLayer::Overlay, relative_pos)
{ {
if layer.can_receive_keyboard_focus() { if layer.can_receive_keyboard_focus() {
let layer_loc = under = Some(layer.clone().into());
layers.layer_geometry(layer).unwrap().loc;
under = layer
.surface_under(
pos - output_geo.loc.to_f64()
- layer_loc.to_f64(),
WindowSurfaceType::ALL,
)
.map(|(_, _)| layer.wl_surface().clone());
} }
} else { } else {
under = window under = Some(window.clone().into());
.surface_under(
pos - output_geo.loc.to_f64(),
WindowSurfaceType::ALL,
)
.map(|(_, _)| window.toplevel().wl_surface().clone());
} }
} else { } else {
if let Some(layer) = layers if let Some(layer) = layers
@ -689,35 +663,18 @@ impl State {
.or_else(|| layers.layer_under(WlrLayer::Top, relative_pos)) .or_else(|| layers.layer_under(WlrLayer::Top, relative_pos))
{ {
if layer.can_receive_keyboard_focus() { if layer.can_receive_keyboard_focus() {
let layer_loc = under = Some(layer.clone().into());
layers.layer_geometry(layer).unwrap().loc;
under = layer
.surface_under(
pos - output_geo.loc.to_f64()
- layer_loc.to_f64(),
WindowSurfaceType::ALL,
)
.map(|(_, _)| layer.wl_surface().clone());
} }
} else if let Some((window, _, _)) = workspace } else if let Some((window, _)) =
.space workspace.element_under(relative_pos)
.surface_under(relative_pos, WindowSurfaceType::ALL)
{ {
under = Some(window.toplevel().wl_surface().clone()); under = Some(window.clone().into());
} else if let Some(layer) = layers } else if let Some(layer) = layers
.layer_under(WlrLayer::Bottom, pos) .layer_under(WlrLayer::Bottom, pos)
.or_else(|| layers.layer_under(WlrLayer::Background, pos)) .or_else(|| layers.layer_under(WlrLayer::Background, pos))
{ {
if layer.can_receive_keyboard_focus() { if layer.can_receive_keyboard_focus() {
let layer_loc = under = Some(layer.clone().into());
layers.layer_geometry(layer).unwrap().loc;
under = layer
.surface_under(
pos - output_geo.loc.to_f64()
- layer_loc.to_f64(),
WindowSurfaceType::ALL,
)
.map(|(_, _)| layer.wl_surface().clone());
} }
}; };
} }
@ -740,7 +697,7 @@ impl State {
} }
InputEvent::PointerAxis { event, .. } => { InputEvent::PointerAxis { event, .. } => {
let device = event.device(); let device = event.device();
for seat in self.common.seats.clone().iter() { for seat in self.common.seats().cloned().collect::<Vec<_>>().iter() {
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
if self.common.seats.iter().position(|x| x == seat).unwrap() == 0 if self.common.seats.iter().position(|x| x == seat).unwrap() == 0
&& self.common.egui.active && self.common.egui.active
@ -820,7 +777,7 @@ impl State {
output: &Output, output: &Output,
output_geo: Rectangle<i32, Logical>, output_geo: Rectangle<i32, Logical>,
workspace: &Workspace, workspace: &Workspace,
) -> Option<(WlSurface, Point<i32, Logical>)> { ) -> Option<(PointerFocusTarget, Point<i32, Logical>)> {
let layers = layer_map_for_output(output); let layers = layer_map_for_output(output);
if let Some(window) = workspace.get_fullscreen(output) { if let Some(window) = workspace.get_fullscreen(output) {
if let Some(layer) = layers if let Some(layer) = layers
@ -828,16 +785,9 @@ impl State {
.or_else(|| layers.layer_under(WlrLayer::Top, relative_pos)) .or_else(|| layers.layer_under(WlrLayer::Top, relative_pos))
{ {
let layer_loc = layers.layer_geometry(layer).unwrap().loc; let layer_loc = layers.layer_geometry(layer).unwrap().loc;
layer Some((layer.clone().into(), output_geo.loc + layer_loc))
.surface_under(
global_pos - output_geo.loc.to_f64() - layer_loc.to_f64(),
WindowSurfaceType::ALL,
)
.map(|(s, loc)| (s, loc + layer_loc + output_geo.loc))
} else { } else {
window Some((window.clone().into(), output_geo.loc))
.surface_under(global_pos - output_geo.loc.to_f64(), WindowSurfaceType::ALL)
.map(|(s, loc)| (s, loc + output_geo.loc))
} }
} else { } else {
if let Some(layer) = layers if let Some(layer) = layers
@ -845,28 +795,18 @@ impl State {
.or_else(|| layers.layer_under(WlrLayer::Top, relative_pos)) .or_else(|| layers.layer_under(WlrLayer::Top, relative_pos))
{ {
let layer_loc = layers.layer_geometry(layer).unwrap().loc; let layer_loc = layers.layer_geometry(layer).unwrap().loc;
layer Some((layer.clone().into(), output_geo.loc + layer_loc))
.surface_under( } else if let Some((mapped, loc)) = workspace.element_under(relative_pos) {
global_pos - output_geo.loc.to_f64() - layer_loc.to_f64(), Some((
WindowSurfaceType::ALL, mapped.clone().into(),
) loc + (global_pos - relative_pos).to_i32_round(),
.map(|(s, loc)| (s, loc + layer_loc + output_geo.loc)) ))
} else if let Some((_, surface, loc)) = workspace
.space
.surface_under(relative_pos, WindowSurfaceType::ALL)
{
Some((surface, loc + (global_pos - relative_pos).to_i32_round()))
} else if let Some(layer) = layers } else if let Some(layer) = layers
.layer_under(WlrLayer::Bottom, relative_pos) .layer_under(WlrLayer::Bottom, relative_pos)
.or_else(|| layers.layer_under(WlrLayer::Background, relative_pos)) .or_else(|| layers.layer_under(WlrLayer::Background, relative_pos))
{ {
let layer_loc = layers.layer_geometry(layer).unwrap().loc; let layer_loc = layers.layer_geometry(layer).unwrap().loc;
layer Some((layer.clone().into(), output_geo.loc + layer_loc))
.surface_under(
global_pos - output_geo.loc.to_f64() - layer_loc.to_f64(),
WindowSurfaceType::ALL,
)
.map(|(s, loc)| (s, loc + layer_loc + output_geo.loc))
} else { } else {
None None
} }

View file

@ -68,7 +68,7 @@ fn main() -> Result<()> {
} }
// trigger routines // trigger routines
data.state.common.shell.refresh(&data.display.handle()); data.state.common.shell.refresh();
state::Common::refresh_focus(&mut data.state); state::Common::refresh_focus(&mut data.state);
// do we need to trigger another render // do we need to trigger another render

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::{ use crate::{
shell::{OutputBoundState, Shell, Workspace, WorkspaceMode}, shell::{element::CosmicMapped, Shell, Workspace},
state::Common, state::Common,
utils::prelude::*, utils::prelude::*,
wayland::handlers::xdg_shell::PopupGrabData, wayland::handlers::xdg_shell::PopupGrabData,
}; };
use indexmap::IndexSet; use indexmap::IndexSet;
use smithay::{ use smithay::{
desktop::{layer_map_for_output, PopupUngrabStrategy, Window, WindowSurfaceType}, desktop::{layer_map_for_output, PopupUngrabStrategy},
input::Seat, input::Seat,
reexports::wayland_server::protocol::wl_surface::WlSurface,
utils::{IsAlive, Serial, SERIAL_COUNTER}, 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; pub mod target;
@ -32,80 +26,59 @@ pub enum FocusDirection {
Out, Out,
} }
pub struct FocusStack<'a>(Ref<'a, IndexSet<Window>>); pub struct FocusStack<'a>(pub(super) Option<&'a IndexSet<CosmicMapped>>);
pub struct FocusStackMut<'a>(RefMut<'a, IndexSet<Window>>); pub struct FocusStackMut<'a>(pub(super) &'a mut IndexSet<CosmicMapped>);
impl<'a> FocusStack<'a> { impl<'a> FocusStack<'a> {
pub fn last(&self) -> Option<Window> { pub fn last(&self) -> Option<&CosmicMapped> {
self.0.iter().rev().find(|w| w.toplevel().alive()).cloned() self.0
.as_ref()
.and_then(|set| set.iter().rev().find(|w| w.alive()))
} }
pub fn iter(&self) -> impl Iterator<Item = &'_ Window> { pub fn iter(&self) -> impl Iterator<Item = &'_ CosmicMapped> {
self.0.iter().rev().filter(|w| w.toplevel().alive()) self.0
.iter()
.flat_map(|set| set.iter().rev().filter(|w| w.alive()))
} }
} }
impl<'a> FocusStackMut<'a> { impl<'a> FocusStackMut<'a> {
pub fn append(&mut self, window: &Window) { pub fn append(&mut self, window: &CosmicMapped) {
self.0.retain(|w| w.toplevel().alive()); self.0.retain(|w| w.alive());
self.0.shift_remove(window); self.0.shift_remove(window);
self.0.insert(window.clone()); self.0.insert(window.clone());
} }
pub fn last(&self) -> Option<Window> { pub fn last(&self) -> Option<&CosmicMapped> {
self.0.iter().rev().find(|w| w.toplevel().alive()).cloned() self.0.iter().rev().find(|w| w.alive())
} }
pub fn iter(&self) -> impl Iterator<Item = &'_ Window> { pub fn iter(&self) -> impl Iterator<Item = &'_ CosmicMapped> {
self.0.iter().rev().filter(|w| w.toplevel().alive()) self.0.iter().rev().filter(|w| w.alive())
} }
} }
type FocusStackData = RefCell<(HashMap<u8, IndexSet<Window>>, IndexSet<Window>)>; impl Workspace {}
impl Workspace { pub struct ActiveFocus(RefCell<Option<KeyboardFocusTarget>>);
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>>);
impl ActiveFocus { impl ActiveFocus {
fn set(seat: &Seat<State>, surface: Option<WlSurface>) { fn set(seat: &Seat<State>, target: Option<KeyboardFocusTarget>) {
if !seat if !seat
.user_data() .user_data()
.insert_if_missing(|| ActiveFocus(RefCell::new(surface.clone()))) .insert_if_missing(|| ActiveFocus(RefCell::new(target.clone())))
{ {
*seat *seat
.user_data() .user_data()
.get::<ActiveFocus>() .get::<ActiveFocus>()
.unwrap() .unwrap()
.0 .0
.borrow_mut() = surface; .borrow_mut() = target;
} }
} }
fn get(seat: &Seat<State>) -> Option<WlSurface> { fn get(seat: &Seat<State>) -> Option<KeyboardFocusTarget> {
seat.user_data() seat.user_data()
.get::<ActiveFocus>() .get::<ActiveFocus>()
.and_then(|a| a.0.borrow().clone()) .and_then(|a| a.0.borrow().clone())
@ -115,30 +88,25 @@ impl ActiveFocus {
impl Shell { impl Shell {
pub fn set_focus<'a>( pub fn set_focus<'a>(
state: &mut State, state: &mut State,
surface: Option<&WlSurface>, target: Option<&KeyboardFocusTarget>,
active_seat: &Seat<State>, active_seat: &Seat<State>,
serial: Option<Serial>, serial: Option<Serial>,
) { ) {
// update FocusStack and notify layouts about new focus (if any window) // update FocusStack and notify layouts about new focus (if any window)
if let Some(surface) = surface { if let Some(KeyboardFocusTarget::Element(mapped)) = target {
if let Some(workspace) = state.common.shell.space_for_window_mut(surface) { if let Some(workspace) = state.common.shell.space_for_mut(mapped) {
if let Some(window) = workspace let mut focus_stack = workspace.focus_stack.get_mut(active_seat);
.space if Some(mapped) != focus_stack.last() {
.window_for_surface(surface, WindowSurfaceType::ALL) slog_scope::debug!("Focusing window: {:?}", mapped);
{ focus_stack.append(mapped);
let mut focus_stack = workspace.focus_stack_mut(active_seat); // also remove popup grabs, if we are switching focus
if Some(window) != focus_stack.last().as_ref() { if let Some(mut popup_grab) = active_seat
slog_scope::debug!("Focusing window: {:?}", window); .user_data()
focus_stack.append(window); .get::<PopupGrabData>()
// also remove popup grabs, if we are switching focus .and_then(|x| x.take())
if let Some(mut popup_grab) = active_seat {
.user_data() if !popup_grab.has_ended() {
.get::<PopupGrabData>() popup_grab.ungrab(PopupUngrabStrategy::All);
.and_then(|x| x.take())
{
if !popup_grab.has_ended() {
popup_grab.ungrab(PopupUngrabStrategy::All);
}
} }
} }
} }
@ -147,10 +115,10 @@ impl Shell {
// update keyboard focus // update keyboard focus
if let Some(keyboard) = active_seat.get_keyboard() { if let Some(keyboard) = active_seat.get_keyboard() {
ActiveFocus::set(active_seat, surface.cloned()); ActiveFocus::set(active_seat, target.cloned());
keyboard.set_focus( keyboard.set_focus(
state, state,
surface.cloned(), target.cloned(),
serial.unwrap_or_else(|| SERIAL_COUNTER.next_serial()), serial.unwrap_or_else(|| SERIAL_COUNTER.next_serial()),
); );
} }
@ -160,30 +128,23 @@ impl Shell {
// update activate status // update activate status
let focused_windows = seats let focused_windows = seats
.flat_map(|seat| { .flat_map(|seat| {
self.outputs self.outputs.iter().flat_map(|o| {
.iter() let space = self.active_space(o);
.flat_map(|o| self.active_space(o).focus_stack(seat).last().clone()) let stack = space.focus_stack.get(seat);
stack.last().cloned()
})
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
for output in self.outputs.iter() { for output in self.outputs.iter() {
let workspace = match &self.workspace_mode { let workspace = self.workspaces.active_mut(output);
WorkspaceMode::OutputBound => {
let active = output
.user_data()
.get::<OutputBoundState>()
.unwrap()
.active
.get();
&mut self.spaces[active]
}
WorkspaceMode::Global { active, .. } => &mut self.spaces[*active],
};
for focused in focused_windows.iter() { 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() { for window in workspace.mapped() {
window.set_activated(focused_windows.contains(window)); window.set_activated(focused_windows.contains(&window));
window.configure(); window.configure();
} }
} }
@ -193,55 +154,50 @@ impl Shell {
impl Common { impl Common {
pub fn set_focus( pub fn set_focus(
state: &mut State, state: &mut State,
surface: Option<&WlSurface>, target: Option<&KeyboardFocusTarget>,
active_seat: &Seat<State>, active_seat: &Seat<State>,
serial: Option<Serial>, serial: Option<Serial>,
) { ) {
Shell::set_focus(state, surface, active_seat, serial); Shell::set_focus(state, target, active_seat, serial);
state.common.shell.update_active(state.common.seats.iter()); let seats = state.common.seats().cloned().collect::<Vec<_>>();
state.common.shell.update_active(seats.iter());
} }
pub fn refresh_focus(state: &mut State) { 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 { for seat in seats {
let output = active_output(&seat, &state.common); let output = seat.active_output();
let last_known_focus = ActiveFocus::get(&seat); let last_known_focus = ActiveFocus::get(&seat);
if let Some(surface) = last_known_focus { if let Some(target) = last_known_focus {
if surface.alive() { if target.alive() {
let is_toplevel = matches!(get_role(&surface), Some(XDG_TOPLEVEL_ROLE)); match target {
let is_layer = matches!(get_role(&surface), Some(LAYER_SURFACE_ROLE)); KeyboardFocusTarget::Element(mapped) => {
let workspace = state.common.shell.active_space(&output);
if let Some(popup) = state.common.shell.popups.find_popup(&surface) { let focus_stack = workspace.focus_stack.get(&seat);
if popup.alive() { if focus_stack.last().map(|m| m == &mapped).unwrap_or(false) {
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) {
continue; // Focus is valid continue; // Focus is valid
} else { } else {
slog_scope::debug!("Wrong Window, focus fixup"); slog_scope::debug!("Wrong Window, focus fixup");
} }
} else {
slog_scope::debug!("Different workspaces Window, focus fixup");
} }
} else { KeyboardFocusTarget::LayerSurface(layer) => {
// unknown surface type, fixup if layer_map_for_output(&output).layers().any(|l| l == &layer) {
slog_scope::debug!("Surface unmapped, focus fixup"); 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 { } else {
slog_scope::debug!("Surface dead, focus fixup"); slog_scope::debug!("Surface dead, focus fixup");
} }
@ -263,21 +219,24 @@ impl Common {
} }
// update keyboard focus // update keyboard focus
let surface = state let target = state
.common .common
.shell .shell
.active_space(&output) .active_space(&output)
.focus_stack(&seat) .focus_stack
.get(&seat)
.last() .last()
.map(|w| w.toplevel().wl_surface().clone()); .cloned()
.map(KeyboardFocusTarget::from);
if let Some(keyboard) = seat.get_keyboard() { if let Some(keyboard) = seat.get_keyboard() {
slog_scope::info!("restoring focus to: {:?}", surface.as_ref()); slog_scope::info!("restoring focus to: {:?}", target.as_ref());
keyboard.set_focus(state, surface.clone(), SERIAL_COUNTER.next_serial()); keyboard.set_focus(state, target.clone(), SERIAL_COUNTER.next_serial());
ActiveFocus::set(&seat, surface); 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::*; use std::sync::Weak;
pub use smithay::{
use crate::{shell::element::CosmicMapped, utils::prelude::*};
use id_tree::NodeId;
use smithay::{
backend::input::KeyState, backend::input::KeyState,
desktop::{LayerSurface, PopupKind, Window}, desktop::{LayerSurface, PopupKind, Window},
input::{ input::{
@ -7,83 +10,139 @@ pub use smithay::{
pointer::{AxisFrame, ButtonEvent, MotionEvent, PointerTarget}, pointer::{AxisFrame, ButtonEvent, MotionEvent, PointerTarget},
Seat, Seat,
}, },
output::WeakOutput,
reexports::wayland_server::{backend::ObjectId, protocol::wl_surface::WlSurface, Resource}, reexports::wayland_server::{backend::ObjectId, protocol::wl_surface::WlSurface, Resource},
utils::{IsAlive, Serial}, utils::{IsAlive, Serial},
wayland::seat::WaylandFocus, wayland::seat::WaylandFocus,
}; };
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum FocusTarget { pub enum PointerFocusTarget {
Window(Window), Element(CosmicMapped),
Fullscreen(Window),
LayerSurface(LayerSurface), LayerSurface(LayerSurface),
Popup(PopupKind), Popup(PopupKind),
} }
impl IsAlive for FocusTarget { #[derive(Debug, Clone, PartialEq)]
fn alive(&self) -> bool { pub enum KeyboardFocusTarget {
match self { Element(CosmicMapped),
FocusTarget::Window(w) => w.alive(), Fullscreen(Window),
FocusTarget::LayerSurface(l) => l.alive(), Group(WindowGroup),
FocusTarget::Popup(p) => p.alive(), 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) { fn enter(&self, seat: &Seat<State>, data: &mut State, event: &MotionEvent) {
match self { match self {
FocusTarget::Window(w) => { PointerFocusTarget::Element(w) => PointerTarget::enter(w, seat, data, event),
PointerTarget::enter(w.toplevel().wl_surface(), 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), PointerFocusTarget::Popup(p) => PointerTarget::enter(p.wl_surface(), seat, data, event),
FocusTarget::Popup(p) => PointerTarget::enter(p.wl_surface(), seat, data, event),
} }
} }
fn motion(&self, seat: &Seat<State>, data: &mut State, event: &MotionEvent) { fn motion(&self, seat: &Seat<State>, data: &mut State, event: &MotionEvent) {
match self { match self {
FocusTarget::Window(w) => { PointerFocusTarget::Element(w) => PointerTarget::motion(w, seat, data, event),
PointerTarget::motion(w.toplevel().wl_surface(), seat, data, event) PointerFocusTarget::Fullscreen(w) => PointerTarget::motion(w, seat, data, event),
} PointerFocusTarget::LayerSurface(l) => {
FocusTarget::LayerSurface(l) => {
PointerTarget::motion(l.wl_surface(), seat, data, event) 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) { fn button(&self, seat: &Seat<State>, data: &mut State, event: &ButtonEvent) {
match self { match self {
FocusTarget::Window(w) => { PointerFocusTarget::Element(w) => PointerTarget::button(w, seat, data, event),
PointerTarget::button(w.toplevel().wl_surface(), seat, data, event) PointerFocusTarget::Fullscreen(w) => PointerTarget::button(w, seat, data, event),
} PointerFocusTarget::LayerSurface(l) => {
FocusTarget::LayerSurface(l) => {
PointerTarget::button(l.wl_surface(), seat, data, event) 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) { fn axis(&self, seat: &Seat<State>, data: &mut State, frame: AxisFrame) {
match self { match self {
FocusTarget::Window(w) => { PointerFocusTarget::Element(w) => PointerTarget::axis(w, seat, data, frame),
PointerTarget::axis(w.toplevel().wl_surface(), 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), PointerFocusTarget::Popup(p) => PointerTarget::axis(p.wl_surface(), seat, data, frame),
FocusTarget::Popup(p) => PointerTarget::axis(p.wl_surface(), seat, data, frame),
} }
} }
fn leave(&self, seat: &Seat<State>, data: &mut State, serial: Serial, time: u32) { fn leave(&self, seat: &Seat<State>, data: &mut State, serial: Serial, time: u32) {
match self { match self {
FocusTarget::Window(w) => { PointerFocusTarget::Element(w) => PointerTarget::leave(w, seat, data, serial, time),
PointerTarget::leave(w.toplevel().wl_surface(), seat, data, serial, time) PointerFocusTarget::Fullscreen(w) => PointerTarget::leave(w, seat, data, serial, time),
} PointerFocusTarget::LayerSurface(l) => {
FocusTarget::LayerSurface(l) => {
PointerTarget::leave(l.wl_surface(), seat, data, serial, time) 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( fn enter(
&self, &self,
seat: &Seat<State>, seat: &Seat<State>,
@ -92,26 +151,30 @@ impl KeyboardTarget<State> for FocusTarget {
serial: Serial, serial: Serial,
) { ) {
match self { match self {
FocusTarget::Window(w) => { KeyboardFocusTarget::Element(w) => KeyboardTarget::enter(w, seat, data, keys, serial),
KeyboardTarget::enter(w.toplevel().wl_surface(), 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) KeyboardTarget::enter(l.wl_surface(), seat, data, keys, serial)
} }
FocusTarget::Popup(p) => { KeyboardFocusTarget::Popup(p) => {
KeyboardTarget::enter(p.wl_surface(), seat, data, keys, serial) KeyboardTarget::enter(p.wl_surface(), seat, data, keys, serial)
} }
} }
} }
fn leave(&self, seat: &Seat<State>, data: &mut State, serial: Serial) { fn leave(&self, seat: &Seat<State>, data: &mut State, serial: Serial) {
match self { match self {
FocusTarget::Window(w) => { KeyboardFocusTarget::Element(w) => KeyboardTarget::leave(w, seat, data, serial),
KeyboardTarget::leave(w.toplevel().wl_surface(), seat, data, serial) KeyboardFocusTarget::Fullscreen(w) => KeyboardTarget::leave(w, seat, data, serial),
} KeyboardFocusTarget::Group(_) => {}
FocusTarget::LayerSurface(l) => { KeyboardFocusTarget::LayerSurface(l) => {
KeyboardTarget::leave(l.wl_surface(), seat, data, serial) 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( fn key(
@ -124,19 +187,17 @@ impl KeyboardTarget<State> for FocusTarget {
time: u32, time: u32,
) { ) {
match self { match self {
FocusTarget::Window(w) => KeyboardTarget::key( KeyboardFocusTarget::Element(w) => {
w.toplevel().wl_surface(), KeyboardTarget::key(w, seat, data, key, state, serial, time)
seat, }
data, KeyboardFocusTarget::Fullscreen(w) => {
key, KeyboardTarget::key(w, seat, data, key, state, serial, time)
state, }
serial, KeyboardFocusTarget::Group(_) => {}
time, KeyboardFocusTarget::LayerSurface(l) => {
),
FocusTarget::LayerSurface(l) => {
KeyboardTarget::key(l.wl_surface(), seat, data, key, state, serial, time) 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) KeyboardTarget::key(p.wl_surface(), seat, data, key, state, serial, time)
} }
} }
@ -149,50 +210,113 @@ impl KeyboardTarget<State> for FocusTarget {
serial: Serial, serial: Serial,
) { ) {
match self { match self {
FocusTarget::Window(w) => { KeyboardFocusTarget::Element(w) => {
KeyboardTarget::modifiers(w.toplevel().wl_surface(), seat, data, modifiers, serial) 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) KeyboardTarget::modifiers(l.wl_surface(), seat, data, modifiers, serial)
} }
FocusTarget::Popup(p) => { KeyboardFocusTarget::Popup(p) => {
KeyboardTarget::modifiers(p.wl_surface(), seat, data, modifiers, serial) KeyboardTarget::modifiers(p.wl_surface(), seat, data, modifiers, serial)
} }
} }
} }
} }
impl WaylandFocus for FocusTarget { impl WaylandFocus for KeyboardFocusTarget {
fn wl_surface(&self) -> Option<&WlSurface> { fn wl_surface(&self) -> Option<WlSurface> {
Some(match self { match self {
FocusTarget::Window(w) => w.toplevel().wl_surface(), KeyboardFocusTarget::Element(w) => WaylandFocus::wl_surface(w),
FocusTarget::LayerSurface(l) => l.wl_surface(), KeyboardFocusTarget::Fullscreen(w) => WaylandFocus::wl_surface(w),
FocusTarget::Popup(p) => p.wl_surface(), 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 { fn same_client_as(&self, object_id: &ObjectId) -> bool {
match self { match self {
FocusTarget::Window(w) => w.toplevel().wl_surface().id().same_client_as(object_id), KeyboardFocusTarget::Element(w) => WaylandFocus::same_client_as(w, object_id),
FocusTarget::LayerSurface(l) => l.wl_surface().id().same_client_as(object_id), KeyboardFocusTarget::Fullscreen(w) => WaylandFocus::same_client_as(w, object_id),
FocusTarget::Popup(p) => p.wl_surface().id().same_client_as(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 { 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 { 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 { 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 .space
.outputs_for_window(&window) .outputs_for_window(&window)
.into_iter() .into_iter()
.find(|o| o.geometry().contains(pos.to_i32_round())) { .find(|o| o.geometry().contains(pos.to_i32_round()))
Some(o) => o, {
None => return, Some(o) => o,
}; None => return,
};
let mut initial_window_location = workspace.space.window_location(&window).unwrap(); let mut initial_window_location = workspace.space.window_location(&window).unwrap();
match &window.toplevel() { match &window.toplevel() {
@ -356,7 +357,7 @@ impl MoveSurfaceGrab {
time: u32, time: u32,
) { ) {
// No more buttons are pressed, release the grab. // 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(); let seat = self.seat.clone();
state.common.event_loop_handle.insert_idle(move |data| { state.common.event_loop_handle.insert_idle(move |data| {

View file

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

View file

@ -1,99 +1,86 @@
// SPDX-License-Identifier: GPL-3.0-only // SPDX-License-Identifier: GPL-3.0-only
use smithay::{ use smithay::{
desktop::{layer_map_for_output, space::RenderZindex, Kind, Space, Window}, backend::renderer::{ImportAll, Renderer},
input::{ desktop::{layer_map_for_output, space::SpaceElement, Space, Window},
pointer::{Focus, GrabStartData as PointerGrabStartData}, input::Seat,
Seat,
},
output::Output, output::Output,
reexports::wayland_protocols::xdg::shell::server::xdg_toplevel::{ render_elements,
ResizeEdge, State as XdgState, utils::{Logical, Point, Rectangle},
},
utils::{IsAlive, Logical, Point, Rectangle, Serial},
wayland::{compositor::with_states, shell::xdg::XdgToplevelSurfaceRoleAttributes},
}; };
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; mod grabs;
pub use self::grabs::*; pub use self::grabs::*;
pub const FLOATING_INDEX: u8 = RenderZindex::Shell as u8 + 1; #[derive(Debug)]
#[derive(Debug, Default)]
pub struct FloatingLayout { pub struct FloatingLayout {
pending_windows: Vec<Window>, pub(in crate::shell) space: Space<CosmicMapped>,
pub windows: HashSet<Window>,
} }
#[derive(Default)] impl Default for FloatingLayout {
pub struct WindowUserDataInner { fn default() -> Self {
last_geometry: Rectangle<i32, Logical>, FloatingLayout {
space: Space::new(None),
}
}
} }
pub type WindowUserData = Mutex<WindowUserDataInner>;
impl FloatingLayout { impl FloatingLayout {
pub fn new() -> FloatingLayout { pub fn new() -> FloatingLayout {
Default::default() 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, &mut self,
space: &mut Space, mapped: impl Into<CosmicMapped>,
window: Window,
seat: &Seat<State>, seat: &Seat<State>,
position: impl Into<Option<Point<i32, Logical>>>, position: impl Into<Option<Point<i32, Logical>>>,
) { ) {
if let Some(output) = super::output_from_seat(Some(seat), space) { let mapped = mapped.into();
self.map_window_internal(space, window, &output, position.into()); let output = seat.active_output();
} else { let position = position.into();
self.pending_windows.push(window);
} self.map_internal(mapped, &output, position)
} }
pub fn refresh(&mut self, space: &mut Space) { pub(in crate::shell) fn map_internal(
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(
&mut self, &mut self,
space: &mut Space, mapped: CosmicMapped,
window: Window,
output: &Output, output: &Output,
position: Option<Point<i32, Logical>>, position: Option<Point<i32, Logical>>,
) { ) {
let last_geometry = window let mut win_geo = mapped.geometry();
.user_data()
.get::<WindowUserData>()
.map(|u| u.lock().unwrap().last_geometry);
let mut win_geo = window.geometry();
let layers = layer_map_for_output(&output); let layers = layer_map_for_output(&output);
let geometry = layers.non_exclusive_zone(); let geometry = layers.non_exclusive_zone();
let last_geometry = mapped.last_geometry.lock().unwrap().clone();
let mut geo_updated = false; 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; geo_updated = win_geo.size == size;
win_geo.size = size; win_geo.size = size;
} }
{ {
let (min_size, max_size) = with_states(window.toplevel().wl_surface(), |states| { let (min_size, max_size) = (mapped.min_size(), mapped.max_size());
let attrs = states
.data_map
.get::<Mutex<XdgToplevelSurfaceRoleAttributes>>()
.unwrap()
.lock()
.unwrap();
(attrs.min_size, attrs.max_size)
});
if win_geo.size.w > geometry.size.w / 3 * 2 { if win_geo.size.w > geometry.size.w / 3 * 2 {
// try a more reasonable size // try a more reasonable size
let mut width = geometry.size.w / 3 * 2; let mut width = geometry.size.w / 3 * 2;
@ -136,103 +123,67 @@ impl FloatingLayout {
.into() .into()
}); });
#[allow(irrefutable_let_patterns)] // TODO: Move this into CosmicMapped, this needs to differciate between stacks and windows
if let Kind::Xdg(xdg) = &window.toplevel() { mapped.set_tiled(false);
xdg.with_pending_state(|state| { if geo_updated {
state.states.unset(XdgState::TiledLeft); mapped.set_size(win_geo.size);
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();
} }
mapped.configure();
space.map_window(&window, position, FLOATING_INDEX, false); self.space.map_element(mapped, position, false);
self.windows.insert(window);
} }
pub fn unmap_window(&mut self, space: &mut Space, window: &Window) { pub fn unmap(&mut self, window: &CosmicMapped) -> bool {
#[allow(irrefutable_let_patterns)] #[allow(irrefutable_let_patterns)]
let is_maximized = match &window.toplevel() { let is_maximized = window.is_maximized();
Kind::Xdg(surface) => {
surface.with_pending_state(|state| state.states.contains(XdgState::Maximized))
}
};
if !is_maximized { if !is_maximized {
if let Some(location) = space.window_location(window) { if let Some(location) = self.space.element_location(window) {
let user_data = window.user_data(); *window.last_geometry.lock().unwrap() = Some(Rectangle::from_loc_and_size(
user_data.insert_if_missing(|| WindowUserData::default()); location,
user_data window.geometry().size,
.get::<WindowUserData>() ));
.unwrap()
.lock()
.unwrap()
.last_geometry = Rectangle::from_loc_and_size(location, window.geometry().size);
} }
} }
space.unmap_window(window); let was_unmaped = self.space.elements().any(|e| e == window);
self.pending_windows.retain(|w| w != window); self.space.unmap_elem(&window);
self.windows.remove(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 layers = layer_map_for_output(&output);
let geometry = layers.non_exclusive_zone(); let geometry = layers.non_exclusive_zone();
if let Some(location) = space.window_location(window) { if let Some(location) = self.space.element_location(window) {
let user_data = window.user_data(); *window.last_geometry.lock().unwrap() = Some(Rectangle::from_loc_and_size(
user_data.insert_if_missing(|| WindowUserData::default()); location,
user_data window.geometry().size,
.get::<WindowUserData>() ));
.unwrap()
.lock()
.unwrap()
.last_geometry = Rectangle::from_loc_and_size(location, window.geometry().size);
} }
space.map_window( self.space.map_element(window.clone(), geometry.loc, true);
&window, window.set_maximized(true);
(geometry.loc.x, geometry.loc.y), window.set_size(geometry.size);
FLOATING_INDEX, window.configure();
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();
}
} }
pub fn unmaximize_request(&mut self, space: &mut Space, window: &Window) { pub fn unmaximize_request(&mut self, window: &CosmicMapped) {
let last_geometry = window let last_geometry = window.last_geometry.lock().unwrap().clone();
.user_data() window.set_maximized(false);
.get::<WindowUserData>() window.set_size(last_geometry.map(|g| g.size).expect("No previous size?"));
.map(|u| u.lock().unwrap().last_geometry); window.configure();
match window.toplevel() { let last_location = last_geometry.map(|g| g.loc).expect("No previous location?");
Kind::Xdg(toplevel) => { self.space.map_element(window.clone(), last_location, true);
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 resize_request( pub fn resize_request(
state: &mut State, window: &CosmicWindow,
window: &Window,
seat: &Seat<State>, seat: &Seat<State>,
serial: Serial, serial: Serial,
start_data: PointerGrabStartData<State>, start_data: PointerGrabStartData<State>,
@ -256,4 +207,111 @@ impl FloatingLayout {
pointer.set_grab(state, grab, serial, Focus::Clear); 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 // SPDX-License-Identifier: GPL-3.0-only
use crate::{input::ActiveOutput, state::State};
use regex::RegexSet; use regex::RegexSet;
use smithay::{ use smithay::{
desktop::{Space, Window}, desktop::Window,
input::Seat,
output::Output,
wayland::{compositor::with_states, shell::xdg::XdgToplevelSurfaceRoleAttributes}, wayland::{compositor::with_states, shell::xdg::XdgToplevelSurfaceRoleAttributes},
}; };
use std::sync::Mutex; use std::sync::Mutex;
@ -118,17 +115,3 @@ pub fn should_be_floating(window: &Window) -> bool {
false 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::{ use crate::{
shell::layout::{floating::FloatingLayout, tiling::TilingLayout}, shell::{
element::CosmicWindow,
layout::{floating::FloatingLayout, tiling::TilingLayout},
},
state::State, state::State,
utils::prelude::*,
wayland::protocols::workspace::WorkspaceHandle, wayland::protocols::workspace::WorkspaceHandle,
}; };
use indexmap::IndexSet;
use smithay::{ 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}, input::{pointer::GrabStartData as PointerGrabStartData, Seat},
output::Output, output::{Output, WeakOutput},
reexports::{ reexports::{
wayland_protocols::xdg::shell::server::xdg_toplevel::{self, ResizeEdge}, wayland_protocols::xdg::shell::server::xdg_toplevel::{self, ResizeEdge},
wayland_server::{protocol::wl_surface::WlSurface, DisplayHandle}, 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 struct Workspace {
pub idx: u8,
pub space: Space,
pub tiling_layer: TilingLayout, pub tiling_layer: TilingLayout,
pub floating_layer: FloatingLayout, pub floating_layer: FloatingLayout,
tiling_enabled: bool, pub tiling_enabled: bool,
pub fullscreen: HashMap<String, Window>, pub fullscreen: HashMap<Output, Window>,
pub handle: WorkspaceHandle, pub handle: WorkspaceHandle,
pub focus_stack: FocusStacks,
} }
#[derive(Debug, Default)]
pub struct FocusStacks(HashMap<Seat<State>, IndexSet<CosmicMapped>>);
impl Workspace { impl Workspace {
pub fn new(idx: u8, handle: WorkspaceHandle) -> Workspace { pub fn new(handle: WorkspaceHandle) -> Workspace {
Workspace { Workspace {
idx,
space: Space::new(None),
tiling_layer: TilingLayout::new(), tiling_layer: TilingLayout::new(),
floating_layer: FloatingLayout::new(), floating_layer: FloatingLayout::new(),
tiling_enabled: true, tiling_enabled: true,
fullscreen: HashMap::new(), fullscreen: HashMap::new(),
handle, handle,
focus_stack: FocusStacks::default(),
} }
} }
pub fn refresh(&mut self, dh: &DisplayHandle) { pub fn refresh(&mut self) {
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);
}
self.fullscreen.retain(|_, w| w.alive()); self.fullscreen.retain(|_, w| w.alive());
self.floating_layer.refresh(&mut self.space); self.floating_layer.refresh();
self.tiling_layer.refresh(&mut self.space); self.tiling_layer.refresh();
self.space.refresh(dh);
} }
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) { if self.fullscreen.values().any(|w| w == window) {
return; return;
} }
if self.floating_layer.windows.contains(window) { if self.floating_layer.windows.contains(window) {
self.floating_layer self.floating_layer
.maximize_request(&mut self.space, window, output); .maximize_request(, window, output);
} }
} }
pub fn unmaximize_request(&mut self, window: &CosmicMapped) {
pub fn unmaximize_request(&mut self, window: &Window) {
if self.fullscreen.values().any(|w| w == window) { if self.fullscreen.values().any(|w| w == window) {
return self.unfullscreen_request(window); return self.unfullscreen_request(window);
} }
if self.floating_layer.windows.contains(window) { if self.floating_layer.windows.contains(window) {
self.floating_layer 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) TilingLayout::resize_request(state, &window, seat, serial, start_data, edges)
} }
} }
*/
pub fn fullscreen_request(&mut self, window: &Window, output: &Output) { pub fn fullscreen_request(&mut self, window: &Window, output: &Output) {
if self.fullscreen.contains_key(&output.name()) { if self.fullscreen.contains_key(output) {
return; return;
} }
@ -123,7 +224,7 @@ impl Workspace {
}); });
xdg.send_configure(); 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.states.unset(xdg_toplevel::State::Fullscreen);
state.size = None; state.size = None;
}); });
self.floating_layer.refresh(&mut self.space); self.floating_layer.refresh();
self.tiling_layer.refresh(&mut self.space); self.tiling_layer.refresh();
xdg.send_configure(); xdg.send_configure();
} }
self.fullscreen.retain(|_, w| w != window); self.fullscreen.retain(|_, w| w != window);
@ -144,7 +245,7 @@ impl Workspace {
} }
pub fn fullscreen_toggle(&mut self, window: &Window, output: &Output) { 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) self.unfullscreen_request(window)
} else { } else {
self.fullscreen_request(window, output) self.fullscreen_request(window, output)
@ -152,26 +253,33 @@ impl Workspace {
} }
pub fn get_fullscreen(&self, output: &Output) -> Option<&Window> { pub fn get_fullscreen(&self, output: &Output) -> Option<&Window> {
if !self.space.outputs().any(|o| o == output) { self.fullscreen.get(output).filter(|w| w.alive())
return None;
}
self.fullscreen.get(&output.name()).filter(|w| w.alive())
} }
pub fn toggle_tiling(&mut self, seat: &Seat<State>) { pub fn toggle_tiling(&mut self, seat: &Seat<State>) {
if self.tiling_enabled { if self.tiling_enabled {
for window in self.tiling_layer.windows.clone().into_iter() { for window in self
self.tiling_layer.unmap_window(&mut self.space, &window); .tiling_layer
self.floating_layer .mapped()
.map_window(&mut self.space, window, seat, None); .map(|(_, m, _)| m.clone())
.collect::<Vec<_>>()
.into_iter()
{
self.tiling_layer.unmap(&window);
self.floating_layer.map(window, seat, None);
} }
self.tiling_enabled = false; self.tiling_enabled = false;
} else { } else {
let focus_stack = self.focus_stack(seat); let focus_stack = self.focus_stack.get(seat);
for window in self.floating_layer.windows.clone().into_iter() { for window in self
self.floating_layer.unmap_window(&mut self.space, &window); .floating_layer
self.tiling_layer .mapped()
.map_window(&mut self.space, window, seat, focus_stack.iter()) .cloned()
.collect::<Vec<_>>()
.into_iter()
{
self.floating_layer.unmap(&window);
self.tiling_layer.map(window, seat, focus_stack.iter())
} }
self.tiling_enabled = true; self.tiling_enabled = true;
} }
@ -179,18 +287,157 @@ impl Workspace {
pub fn toggle_floating_window(&mut self, seat: &Seat<State>) { pub fn toggle_floating_window(&mut self, seat: &Seat<State>) {
if self.tiling_enabled { if self.tiling_enabled {
if let Some(window) = self.focus_stack(seat).iter().next().cloned() { if let Some(window) = self.focus_stack.get(seat).iter().next().cloned() {
if self.tiling_layer.windows.contains(&window) { if self.tiling_layer.mapped().any(|(_, m, _)| m == &window) {
self.tiling_layer.unmap_window(&mut self.space, &window); self.tiling_layer.unmap(&window);
self.floating_layer self.floating_layer.map(window, seat, None);
.map_window(&mut self.space, window, seat, None); } else if self.floating_layer.mapped().any(|w| w == &window) {
} else if self.floating_layer.windows.contains(&window) { let focus_stack = self.focus_stack.get(seat);
let focus_stack = self.focus_stack(seat); self.floating_layer.unmap(&window);
self.floating_layer.unmap_window(&mut self.space, &window); self.tiling_layer.map(window, seat, focus_stack.iter())
self.tiling_layer
.map_window(&mut self.space, 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>,
} }

View file

@ -7,8 +7,8 @@ use crate::{
shell::Shell, shell::Shell,
utils::prelude::*, utils::prelude::*,
wayland::protocols::{ wayland::protocols::{
drm::WlDrmState, export_dmabuf::ExportDmabufState, drm::WlDrmState, output_configuration::OutputConfigurationState,
output_configuration::OutputConfigurationState, workspace::WorkspaceClientState, workspace::WorkspaceClientState,
}, },
}; };
use smithay::{ use smithay::{
@ -22,7 +22,6 @@ use smithay::{
Display, DisplayHandle, Display, DisplayHandle,
}, },
}, },
utils::{Buffer, Size},
wayland::{ wayland::{
compositor::CompositorState, data_device::DataDeviceState, dmabuf::DmabufState, compositor::CompositorState, data_device::DataDeviceState, dmabuf::DmabufState,
keyboard_shortcuts_inhibit::KeyboardShortcutsInhibitState, output::OutputManagerState, keyboard_shortcuts_inhibit::KeyboardShortcutsInhibitState, output::OutputManagerState,
@ -71,8 +70,8 @@ pub struct Common {
pub shell: Shell, pub shell: Shell,
pub dirty_flag: Arc<AtomicBool>, pub dirty_flag: Arc<AtomicBool>,
pub seats: Vec<Seat<State>>, seats: Vec<Seat<State>>,
pub last_active_seat: Seat<State>, last_active_seat: Option<Seat<State>>,
pub start_time: Instant, pub start_time: Instant,
pub should_stop: bool, pub should_stop: bool,
@ -85,7 +84,6 @@ pub struct Common {
pub compositor_state: CompositorState, pub compositor_state: CompositorState,
pub data_device_state: DataDeviceState, pub data_device_state: DataDeviceState,
pub dmabuf_state: DmabufState, pub dmabuf_state: DmabufState,
pub export_dmabuf_state: ExportDmabufState,
pub keyboard_shortcuts_inhibit_state: KeyboardShortcutsInhibitState, pub keyboard_shortcuts_inhibit_state: KeyboardShortcutsInhibitState,
pub output_state: OutputManagerState, pub output_state: OutputManagerState,
pub output_configuration_state: OutputConfigurationState<State>, pub output_configuration_state: OutputConfigurationState<State>,
@ -201,88 +199,6 @@ impl BackendData {
_ => unreachable!("No backend was initialized"), _ => unreachable!("No backend was initialized"),
} }
} }
pub fn offscreen_for_output(
&mut self,
output: &Output,
state: &mut Common,
) -> anyhow::Result<(Vec<u8>, Size<i32, Buffer>)> {
use crate::backend::render::{render_output, AsGles2Renderer, CustomElem};
use anyhow::Context;
use smithay::backend::renderer::{ImportAll, Renderer};
use smithay::desktop::space::RenderElement;
use smithay::{
backend::{
drm::NodeType,
renderer::{gles2::Gles2Renderbuffer, Bind, ExportMem, Offscreen},
},
utils::Rectangle,
};
fn capture<E, T, R>(
gpu: Option<DrmNode>,
renderer: &mut R,
output: &Output,
state: &mut Common,
) -> anyhow::Result<(Vec<u8>, Size<i32, Buffer>)>
where
E: std::error::Error + Send + Sync + 'static,
T: Clone + 'static,
R: Renderer<Error = E, TextureId = T>
+ ImportAll
+ AsGles2Renderer
+ Offscreen<Gles2Renderbuffer>
+ Bind<Gles2Renderbuffer>
+ ExportMem,
CustomElem: RenderElement<R>,
{
let size = output
.geometry()
.size
.to_f64()
.to_buffer(
output.current_scale().fractional_scale(),
output.current_transform().into(),
)
.to_i32_round();
let buffer = Offscreen::<Gles2Renderbuffer>::create_buffer(renderer, size)?;
renderer.bind(buffer)?;
render_output(
gpu.as_ref(),
renderer,
0,
state,
output,
false,
#[cfg(feature = "debug")]
None,
)
.map_err(|err| anyhow::anyhow!("Failed to render output: {:?}", err))?; // lifetime issue, grrr
let mapping = renderer.copy_framebuffer(Rectangle::from_loc_and_size((0, 0), size))?;
let data = Vec::from(renderer.map_texture(&mapping)?);
Ok((data, size))
}
match self {
BackendData::Winit(winit) => capture(None, winit.backend.renderer(), output, state),
BackendData::X11(x11) => capture(None, &mut x11.renderer, output, state),
BackendData::Kms(kms) => {
let node = kms
.target_node_for_output(output)
.unwrap_or(kms.primary)
.node_with_type(NodeType::Render)
.with_context(|| "Unable to find node")??;
capture(
Some(node),
&mut kms.api.renderer::<Gles2Renderbuffer>(&node, &node)?,
output,
state,
)
}
BackendData::Unset => unreachable!(),
}
}
} }
impl State { impl State {
@ -297,22 +213,16 @@ impl State {
let compositor_state = CompositorState::new::<Self, _>(dh, None); let compositor_state = CompositorState::new::<Self, _>(dh, None);
let data_device_state = DataDeviceState::new::<Self, _>(dh, None); let data_device_state = DataDeviceState::new::<Self, _>(dh, None);
let dmabuf_state = DmabufState::new(); let dmabuf_state = DmabufState::new();
let export_dmabuf_state = ExportDmabufState::new::<Self, _>(
dh,
//|client| client.get_data::<ClientState>().unwrap().privileged,
|_| true,
);
let keyboard_shortcuts_inhibit_state = KeyboardShortcutsInhibitState::new::<Self>(dh); let keyboard_shortcuts_inhibit_state = KeyboardShortcutsInhibitState::new::<Self>(dh);
let output_state = OutputManagerState::new_with_xdg_output::<Self>(dh); let output_state = OutputManagerState::new_with_xdg_output::<Self>(dh);
let output_configuration_state = OutputConfigurationState::new(dh, |_| true); let output_configuration_state = OutputConfigurationState::new(dh, |_| true);
let primary_selection_state = PrimarySelectionState::new::<Self, _>(dh, None); let primary_selection_state = PrimarySelectionState::new::<Self, _>(dh, None);
let shm_state = ShmState::new::<Self, _>(dh, vec![], None); let shm_state = ShmState::new::<Self, _>(dh, vec![], None);
let mut seat_state = SeatState::<Self>::new(); let seat_state = SeatState::<Self>::new();
let viewporter_state = ViewporterState::new::<Self, _>(dh, None); let viewporter_state = ViewporterState::new::<Self, _>(dh, None);
let wl_drm_state = WlDrmState; let wl_drm_state = WlDrmState;
let shell = Shell::new(&config, dh); let shell = Shell::new(&config, dh);
let initial_seat = crate::input::add_seat(dh, &mut seat_state, &config, "seat-0".into());
#[cfg(not(feature = "debug"))] #[cfg(not(feature = "debug"))]
let dirty_flag = Arc::new(AtomicBool::new(false)); let dirty_flag = Arc::new(AtomicBool::new(false));
@ -330,8 +240,8 @@ impl State {
shell, shell,
dirty_flag, dirty_flag,
seats: vec![initial_seat.clone()], seats: Vec::new(),
last_active_seat: initial_seat, last_active_seat: None,
start_time: Instant::now(), start_time: Instant::now(),
should_stop: false, should_stop: false,
@ -354,7 +264,6 @@ impl State {
compositor_state, compositor_state,
data_device_state, data_device_state,
dmabuf_state, dmabuf_state,
export_dmabuf_state,
shm_state, shm_state,
seat_state, seat_state,
keyboard_shortcuts_inhibit_state, keyboard_shortcuts_inhibit_state,
@ -376,10 +285,9 @@ impl State {
match std::env::var("COSMIC_RENDER_AUTO_ASSIGN").map(|val| val.to_lowercase()) { match std::env::var("COSMIC_RENDER_AUTO_ASSIGN").map(|val| val.to_lowercase()) {
Ok(val) if val == "y" || val == "yes" || val == "true" => Some( Ok(val) if val == "y" || val == "yes" || val == "true" => Some(
kms_state kms_state
.target_node_for_output(&active_output( .target_node_for_output(
&self.common.last_active_seat, &self.common.last_active_seat().active_output(),
&self.common, )
))
.unwrap_or(kms_state.primary), .unwrap_or(kms_state.primary),
), ),
_ => Some(kms_state.primary), _ => Some(kms_state.primary),
@ -415,6 +323,46 @@ impl State {
} }
} }
impl Common {
pub fn add_seat(&mut self, seat: Seat<State>) {
if self.seats.is_empty() {
self.last_active_seat = Some(seat.clone());
}
self.seats.push(seat);
}
pub fn remove_seat(&mut self, seat: &Seat<State>) {
self.seats.retain(|s| s != seat);
if self.seats.is_empty() {
self.last_active_seat = None;
} else if self.last_active_seat() == seat {
self.last_active_seat = Some(self.seats[0].clone());
}
}
pub fn seats(&self) -> impl Iterator<Item = &Seat<State>> {
self.seats.iter()
}
pub fn last_active_seat(&self) -> &Seat<State> {
self.last_active_seat.as_ref().expect("No seat?")
}
pub fn send_frames(&self, output: &Output) {
let workspace = self.shell.active_space(output);
workspace.mapped().for_each(|mapped| {
if workspace.outputs_for_element(mapped).any(|o| &o == output) {
let window = mapped.active_window();
window.send_frame(self.start_time.elapsed().as_millis() as u32)
}
});
let map = smithay::desktop::layer_map_for_output(output);
for layer_surface in map.layers() {
layer_surface.send_frame(self.start_time.elapsed().as_millis() as u32)
}
}
}
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
impl Fps { impl Fps {
const WINDOW_SIZE: usize = 100; const WINDOW_SIZE: usize = 100;

View file

@ -4,7 +4,6 @@ use smithay::{
output::Output, output::Output,
utils::{Logical, Rectangle, Transform}, utils::{Logical, Rectangle, Transform},
}; };
use std::cell::RefCell;
pub use crate::shell::{Shell, Workspace}; pub use crate::shell::{Shell, Workspace};
pub use crate::state::{Common, State}; pub use crate::state::{Common, State};
@ -32,34 +31,25 @@ impl OutputExt for Output {
pub trait SeatExt { pub trait SeatExt {
fn id(&self) -> usize; fn id(&self) -> usize;
fn active_output(&self) -> Output;
fn set_active_output(&self, output: &Output);
} }
impl SeatExt for Seat<State> { impl SeatExt for Seat<State> {
fn id(&self) -> usize { fn id(&self) -> usize {
self.user_data().get::<SeatId>().unwrap().0 self.user_data().get::<SeatId>().unwrap().0
} }
}
pub fn active_output(seat: &Seat<State>, state: &Common) -> Output { fn active_output(&self) -> Output {
seat.user_data() self.user_data()
.get::<ActiveOutput>() .get::<ActiveOutput>()
.map(|x| x.0.borrow().clone()) .map(|x| x.0.borrow().clone())
.unwrap_or_else(|| { .unwrap()
state }
.shell
.outputs()
.next()
.cloned()
.expect("Backend has no outputs?")
})
}
pub fn set_active_output(seat: &Seat<State>, output: &Output) { fn set_active_output(&self, output: &Output) {
if !seat *self
.user_data()
.insert_if_missing(|| ActiveOutput(RefCell::new(output.clone())))
{
*seat
.user_data() .user_data()
.get::<ActiveOutput>() .get::<ActiveOutput>()
.unwrap() .unwrap()

View file

@ -121,7 +121,7 @@ impl CompositorHandler for State {
state.wl_buffer().is_some() state.wl_buffer().is_some()
}) })
{ {
let output = active_output(&seat, &self.common); let output = seat.active_output();
Shell::map_window(self, &window, &output); Shell::map_window(self, &window, &output);
} else { } else {
return; return;
@ -147,6 +147,7 @@ impl CompositorHandler for State {
self.xdg_popup_ensure_initial_configure(&popup); self.xdg_popup_ensure_initial_configure(&popup);
} }
/*
// at last handle some special cases, like grabs and changing layer surfaces // at last handle some special cases, like grabs and changing layer surfaces
// If we would re-position the window inside the grab we would get a weird jittery animation. // If we would re-position the window inside the grab we would get a weird jittery animation.
@ -182,6 +183,7 @@ impl CompositorHandler for State {
} }
} }
} }
*/
// We need to know every potential output for importing to the right gpu and scheduling a render, // We need to know every potential output for importing to the right gpu and scheduling a render,
// so call this only after every potential surface map operation has been done. // so call this only after every potential surface map operation has been done.
@ -189,8 +191,8 @@ impl CompositorHandler for State {
// and refresh smithays internal state // and refresh smithays internal state
self.common.shell.popups.commit(surface); self.common.shell.popups.commit(surface);
for workspace in &self.common.shell.spaces { for workspace in self.common.shell.workspaces.spaces_mut() {
workspace.space.commit(surface); workspace.commit(surface);
} }
// re-arrange layer-surfaces (commits may change size and positioning) // re-arrange layer-surfaces (commits may change size and positioning)
@ -199,8 +201,7 @@ impl CompositorHandler for State {
map.layer_for_surface(surface, WindowSurfaceType::ALL) map.layer_for_surface(surface, WindowSurfaceType::ALL)
.is_some() .is_some()
}) { }) {
let dh = &self.common.display_handle; layer_map_for_output(output).arrange();
layer_map_for_output(output).arrange(dh);
} }
// schedule a new render // schedule a new render

View file

@ -213,7 +213,7 @@ impl ExportDmabufHandler for State {
if cursor_status != CursorImageStatus::Hidden { if cursor_status != CursorImageStatus::Hidden {
let workspace = workspace.as_deref()?; let workspace = workspace.as_deref()?;
let loc = seat.get_pointer().map(|ptr| ptr.current_location())?; let loc = seat.get_pointer().map(|ptr| ptr.current_location())?;
let output = active_output(seat, &self.common); let output = seat.active_output();
if self.common.shell.active_space(&output).idx == workspace.idx { if self.common.shell.active_space(&output).idx == workspace.idx {
let relative = self let relative = self

View file

@ -27,11 +27,11 @@ impl WlrLayerShellHandler for State {
namespace: String, namespace: String,
) { ) {
super::mark_dirty_on_drop(&self.common, surface.wl_surface()); super::mark_dirty_on_drop(&self.common, surface.wl_surface());
let seat = self.common.last_active_seat.clone(); let seat = self.common.last_active_seat().clone();
let output = wl_output let output = wl_output
.as_ref() .as_ref()
.and_then(Output::from_resource) .and_then(Output::from_resource)
.unwrap_or_else(|| active_output(&seat, &self.common)); .unwrap_or_else(|| seat.active_output());
self.common.shell.pending_layers.push(( self.common.shell.pending_layers.push((
LayerSurface::new(surface, namespace), LayerSurface::new(surface, namespace),
output, output,

View file

@ -4,7 +4,7 @@ pub mod buffer;
pub mod compositor; pub mod compositor;
pub mod data_device; pub mod data_device;
pub mod dmabuf; pub mod dmabuf;
pub mod export_dmabuf; //pub mod export_dmabuf;
pub mod keyboard_shortcuts_inhibit; pub mod keyboard_shortcuts_inhibit;
pub mod layer_shell; pub mod layer_shell;
pub mod output; pub mod output;

View file

@ -1,17 +1,23 @@
// SPDX-License-Identifier: GPL-3.0-only // SPDX-License-Identifier: GPL-3.0-only
use crate::state::State; use crate::{
shell::focus::target::{KeyboardFocusTarget, PointerFocusTarget},
state::State,
};
use smithay::{ use smithay::{
delegate_seat, delegate_seat,
input::{pointer::CursorImageStatus, SeatHandler, SeatState}, input::{pointer::CursorImageStatus, SeatHandler, SeatState},
reexports::wayland_server::{protocol::wl_surface::WlSurface, Resource}, reexports::wayland_server::Resource,
wayland::{data_device::set_data_device_focus, primary_selection::set_primary_focus}, wayland::{
data_device::set_data_device_focus, primary_selection::set_primary_focus,
seat::WaylandFocus,
},
}; };
use std::cell::RefCell; use std::cell::RefCell;
impl SeatHandler for State { impl SeatHandler for State {
type KeyboardFocus = WlSurface; type KeyboardFocus = KeyboardFocusTarget;
type PointerFocus = WlSurface; type PointerFocus = PointerFocusTarget;
fn seat_state(&mut self) -> &mut SeatState<Self> { fn seat_state(&mut self) -> &mut SeatState<Self> {
&mut self.common.seat_state &mut self.common.seat_state
@ -35,10 +41,12 @@ impl SeatHandler for State {
focused: Option<&Self::KeyboardFocus>, focused: Option<&Self::KeyboardFocus>,
) { ) {
let dh = &self.common.display_handle; let dh = &self.common.display_handle;
if let Some(client) = focused.and_then(|s| dh.get_client(s.id()).ok()) { if let Some(client) = focused
set_data_device_focus(dh, seat, Some(client)); .and_then(|t| t.wl_surface())
let client2 = focused.and_then(|s| dh.get_client(s.id()).ok()).unwrap(); .and_then(|s| dh.get_client(s.id()).ok())
set_primary_focus(dh, seat, Some(client2)) {
set_data_device_focus(dh, seat, Some(client.clone()));
set_primary_focus(dh, seat, Some(client))
} }
} }
} }

View file

@ -19,18 +19,35 @@ impl ToplevelManagementHandler for State {
} }
fn activate(&mut self, _dh: &DisplayHandle, window: &Window, seat: Option<Seat<Self>>) { fn activate(&mut self, _dh: &DisplayHandle, window: &Window, seat: Option<Seat<Self>>) {
if let Some(idx) = self for output in self
.common .common
.shell .shell
.space_for_window(window.toplevel().wl_surface()) .outputs()
.map(|w| w.idx) .cloned()
.collect::<Vec<_>>()
.iter()
{ {
let seat = seat.unwrap_or(self.common.last_active_seat.clone()); let maybe = self
let output = active_output(&seat, &self.common); .common
if self.common.shell.active_space(&output).idx != idx { .shell
self.common.shell.activate(&seat, &output, idx as usize); .workspaces
.spaces_for_output(output)
.enumerate()
.find(|(_, w)| w.windows().any(|w| &w == window));
if let Some((idx, workspace)) = maybe {
let seat = seat.unwrap_or(self.common.last_active_seat().clone());
let mapped = workspace
.mapped()
.find(|m| m.windows().any(|(w, _)| &w == window))
.unwrap()
.clone();
std::mem::drop(workspace);
self.common.shell.activate(&output, idx as usize);
mapped.focus_window(window);
Common::set_focus(self, Some(&mapped.clone().into()), &seat, None);
return;
} }
Common::set_focus(self, Some(window.toplevel().wl_surface()), &seat, None);
} }
} }

View file

@ -29,16 +29,15 @@ impl WorkspaceHandler for State {
for request in requests.into_iter() { for request in requests.into_iter() {
match request { match request {
Request::Activate(handle) => { Request::Activate(handle) => {
if let Some(idx) = self let output = self.common.last_active_seat().active_output();
let maybe_idx = self
.common .common
.shell .shell
.spaces .workspaces
.iter() .spaces_for_output(&output)
.position(|w| w.handle == handle) .position(|w| w.handle == handle);
{ if let Some(idx) = maybe_idx {
let seat = &self.common.last_active_seat; self.common.shell.activate(&output, idx);
let output = active_output(seat, &self.common);
self.common.shell.activate(seat, &output, idx);
} }
} }
_ => {} _ => {}

View file

@ -5,7 +5,7 @@ use smithay::{
delegate_xdg_shell, delegate_xdg_shell,
desktop::{ desktop::{
find_popup_root_surface, Kind, PopupGrab, PopupKeyboardGrab, PopupKind, PopupPointerGrab, find_popup_root_surface, Kind, PopupGrab, PopupKeyboardGrab, PopupKind, PopupPointerGrab,
PopupUngrabStrategy, Window, WindowSurfaceType, PopupUngrabStrategy, Window,
}, },
input::{ input::{
pointer::{Focus, GrabStartData as PointerGrabStartData}, pointer::{Focus, GrabStartData as PointerGrabStartData},
@ -17,8 +17,12 @@ use smithay::{
wayland_server::protocol::{wl_output::WlOutput, wl_seat::WlSeat, wl_surface::WlSurface}, wayland_server::protocol::{wl_output::WlOutput, wl_seat::WlSeat, wl_surface::WlSurface},
}, },
utils::Serial, utils::Serial,
wayland::shell::xdg::{ wayland::{
Configure, PopupSurface, PositionerState, ToplevelSurface, XdgShellHandler, XdgShellState, seat::WaylandFocus,
shell::xdg::{
Configure, PopupSurface, PositionerState, ToplevelSurface, XdgShellHandler,
XdgShellState,
},
}, },
}; };
use std::cell::Cell; use std::cell::Cell;
@ -35,13 +39,10 @@ impl XdgShellHandler for State {
fn new_toplevel(&mut self, surface: ToplevelSurface) { fn new_toplevel(&mut self, surface: ToplevelSurface) {
super::mark_dirty_on_drop(&self.common, surface.wl_surface()); super::mark_dirty_on_drop(&self.common, surface.wl_surface());
let seat = &self.common.last_active_seat; let seat = self.common.last_active_seat().clone();
let window = Window::new(Kind::Xdg(surface)); let window = Window::new(Kind::Xdg(surface));
self.common.shell.toplevel_info_state.new_toplevel(&window); self.common.shell.toplevel_info_state.new_toplevel(&window);
self.common self.common.shell.pending_windows.push((window, seat));
.shell
.pending_windows
.push((window, seat.clone()));
// We will position the window after the first commit, when we know its size hints // We will position the window after the first commit, when we know its size hints
} }
@ -72,17 +73,9 @@ impl XdgShellHandler for State {
// If we would re-position the window inside the grab we would get a weird jittery animation. // If we would re-position the window inside the grab we would get a weird jittery animation.
// We only want to resize once the client has acknoledged & commited the new size, // We only want to resize once the client has acknoledged & commited the new size,
// so we need to carefully track the state through different handlers. // so we need to carefully track the state through different handlers.
if let Some(window) = if let Some(mapped) = self.common.shell.element_for_surface(&surface) {
self.common //TODO
.shell //crate::shell::layout::floating::ResizeSurfaceGrab::ack_configure(window, configure)
.space_for_window(&surface)
.and_then(|workspace| {
workspace
.space
.window_for_surface(&surface, WindowSurfaceType::TOPLEVEL)
})
{
crate::shell::layout::floating::ResizeSurfaceGrab::ack_configure(window, configure)
} }
} }
} }
@ -90,12 +83,16 @@ impl XdgShellHandler for State {
fn grab(&mut self, surface: PopupSurface, seat: WlSeat, serial: Serial) { fn grab(&mut self, surface: PopupSurface, seat: WlSeat, serial: Serial) {
let seat = Seat::from_resource(&seat).unwrap(); let seat = Seat::from_resource(&seat).unwrap();
let kind = PopupKind::Xdg(surface); let kind = PopupKind::Xdg(surface);
if let Ok(root) = find_popup_root_surface(&kind) { if let Some(root) = find_popup_root_surface(&kind)
.ok()
.and_then(|root| self.common.shell.element_for_surface(&root))
{
let target = root.clone().into();
let ret = self let ret = self
.common .common
.shell .shell
.popups .popups
.grab_popup(root, kind, &seat, serial); .grab_popup(target, kind, &seat, serial);
if let Ok(mut grab) = ret { if let Ok(mut grab) = ret {
if let Some(keyboard) = seat.get_keyboard() { if let Some(keyboard) = seat.get_keyboard() {
@ -157,18 +154,9 @@ impl XdgShellHandler for State {
fn move_request(&mut self, surface: ToplevelSurface, seat: WlSeat, serial: Serial) { fn move_request(&mut self, surface: ToplevelSurface, seat: WlSeat, serial: Serial) {
let seat = Seat::from_resource(&seat).unwrap(); let seat = Seat::from_resource(&seat).unwrap();
if let Some(start_data) = check_grab_preconditions(&seat, surface.wl_surface(), serial) { if let Some(start_data) = check_grab_preconditions(&seat, surface.wl_surface(), serial) {
let workspace = self if let Some(mapped) = self.common.shell.element_for_surface(surface.wl_surface()) {
.common // Shell::move_request(self, &window, &seat, serial, start_data);
.shell }
.space_for_window_mut(surface.wl_surface())
.unwrap();
let window = workspace
.space
.window_for_surface(surface.wl_surface(), WindowSurfaceType::TOPLEVEL)
.unwrap()
.clone();
Shell::move_request(self, &window, &seat, serial, start_data);
} }
} }
@ -181,35 +169,31 @@ impl XdgShellHandler for State {
) { ) {
let seat = Seat::from_resource(&seat).unwrap(); let seat = Seat::from_resource(&seat).unwrap();
if let Some(start_data) = check_grab_preconditions(&seat, surface.wl_surface(), serial) { if let Some(start_data) = check_grab_preconditions(&seat, surface.wl_surface(), serial) {
Workspace::resize_request(self, surface.wl_surface(), &seat, serial, start_data, edges); if let Some(mapped) = self.common.shell.element_for_surface(surface.wl_surface()) {
// Shell::resize_request(self, mapped, &seat, serial, start_data, edges);
}
} }
} }
fn maximize_request(&mut self, surface: ToplevelSurface) { fn maximize_request(&mut self, surface: ToplevelSurface) {
let surface = surface.wl_surface(); let surface = surface.wl_surface();
let seat = &self.common.last_active_seat; let seat = self.common.last_active_seat();
let output = active_output(seat, &self.common); let output = seat.active_output();
if let Some(workspace) = self.common.shell.space_for_window_mut(surface) { if let Some(mapped) = self.common.shell.element_for_surface(surface).cloned() {
let window = workspace if let Some(workspace) = self.common.shell.space_for_mut(&mapped) {
.space //workspace.maximize_request(mapped, &output)
.window_for_surface(surface, WindowSurfaceType::TOPLEVEL) }
.unwrap()
.clone();
workspace.maximize_request(&window, &output)
} }
} }
fn unmaximize_request(&mut self, surface: ToplevelSurface) { fn unmaximize_request(&mut self, surface: ToplevelSurface) {
let surface = surface.wl_surface(); let surface = surface.wl_surface();
if let Some(workspace) = self.common.shell.space_for_window_mut(surface) { if let Some(mapped) = self.common.shell.element_for_surface(surface).cloned() {
let window = workspace if let Some(workspace) = self.common.shell.space_for_mut(&mapped) {
.space //workspace.unmaximize_request(mapped, &output)
.window_for_surface(surface, WindowSurfaceType::TOPLEVEL) }
.unwrap()
.clone();
workspace.unmaximize_request(&window)
} }
} }
@ -218,29 +202,25 @@ impl XdgShellHandler for State {
.as_ref() .as_ref()
.and_then(Output::from_resource) .and_then(Output::from_resource)
.unwrap_or_else(|| { .unwrap_or_else(|| {
let seat = &self.common.last_active_seat; let seat = self.common.last_active_seat();
active_output(seat, &self.common) seat.active_output()
}); });
let surface = surface.wl_surface(); let surface = surface.wl_surface();
if let Some(workspace) = self.common.shell.space_for_window_mut(surface) { if let Some(mapped) = self.common.shell.element_for_surface(surface).cloned() {
let window = workspace if let Some(workspace) = self.common.shell.space_for_mut(&mapped) {
.space workspace.fullscreen_request(&mapped.active_window(), &output)
.window_for_surface(surface, WindowSurfaceType::TOPLEVEL) }
.unwrap()
.clone();
workspace.fullscreen_request(&window, &output)
} }
} }
fn unfullscreen_request(&mut self, surface: ToplevelSurface) { fn unfullscreen_request(&mut self, surface: ToplevelSurface) {
let surface = surface.wl_surface(); if let Some((workspace, window)) = self.common.shell.workspaces.spaces_mut().find_map(|w| {
if let Some(workspace) = self.common.shell.space_for_window_mut(surface) { let window = w
let window = workspace .windows()
.space .find(|w| w.toplevel().wl_surface() == surface.wl_surface());
.window_for_surface(surface, WindowSurfaceType::TOPLEVEL) window.map(|win| (w, win))
.unwrap() }) {
.clone();
workspace.unfullscreen_request(&window) workspace.unfullscreen_request(&window)
} }
} }
@ -270,7 +250,6 @@ fn check_grab_preconditions(
.as_ref() .as_ref()
.unwrap() .unwrap()
.0 .0
.id()
.same_client_as(&surface.id()) .same_client_as(&surface.id())
{ {
return None; return None;

View file

@ -3,8 +3,7 @@
use crate::{shell::Shell, utils::prelude::*}; use crate::{shell::Shell, utils::prelude::*};
use smithay::{ use smithay::{
desktop::{ desktop::{
layer_map_for_output, LayerSurface, PopupKind, PopupManager, Space, Window, layer_map_for_output, LayerSurface, PopupKind, PopupManager, Window, WindowSurfaceType,
WindowSurfaceType,
}, },
output::Output, output::Output,
reexports::{ reexports::{
@ -27,12 +26,19 @@ use std::sync::Mutex;
impl Shell { impl Shell {
pub fn unconstrain_popup(&self, surface: &PopupSurface, positioner: &PositionerState) { pub fn unconstrain_popup(&self, surface: &PopupSurface, positioner: &PositionerState) {
if let Some(parent) = get_popup_toplevel(&surface) { if let Some(parent) = get_popup_toplevel(&surface) {
if let Some(workspace) = self.space_for_window(&parent) { if let Some(elem) = self.element_for_surface(&parent) {
let window = workspace let workspace = self.space_for(elem).unwrap();
.space let element_loc = workspace.element_geometry(elem).unwrap().loc;
.window_for_surface(&parent, WindowSurfaceType::ALL) let (window, offset) = elem
.windows()
.find(|(w, _)| w.toplevel().wl_surface() == &parent)
.unwrap(); .unwrap();
unconstrain_xdg_popup(surface, positioner, &workspace.space, window); let window_geo_offset = window.geometry().loc;
let window_loc = element_loc + offset + window_geo_offset;
let anchor_point = get_anchor_point(&positioner) + window_loc;
if let Some(output) = workspace.output_under(anchor_point) {
unconstrain_xdg_popup(surface, positioner, window_loc, output.geometry());
}
} else if let Some((output, layer_surface)) = self.outputs().find_map(|o| { } else if let Some((output, layer_surface)) = self.outputs().find_map(|o| {
let map = layer_map_for_output(o); let map = layer_map_for_output(o);
map.layer_for_surface(&parent, WindowSurfaceType::ALL) map.layer_for_surface(&parent, WindowSurfaceType::ALL)
@ -42,15 +48,14 @@ impl Shell {
} }
} }
} }
pub fn update_reactive_popups(&self, window: &Window) {
if let Some(workspace) = self.space_for_window(window.toplevel().wl_surface()) {
update_reactive_popups(&workspace.space, window);
}
}
} }
pub fn update_reactive_popups(space: &Space, window: &Window) { pub fn update_reactive_popups<'a>(
window: &Window,
loc: Point<i32, Logical>,
outputs: impl Iterator<Item = &'a Output>,
) {
let output_geo = outputs.map(|o| o.geometry()).collect::<Vec<_>>();
for (popup, _) in PopupManager::popups_for_surface(window.toplevel().wl_surface()) { for (popup, _) in PopupManager::popups_for_surface(window.toplevel().wl_surface()) {
match popup { match popup {
PopupKind::Xdg(surface) => { PopupKind::Xdg(surface) => {
@ -64,12 +69,19 @@ pub fn update_reactive_popups(space: &Space, window: &Window) {
attributes.current.positioner.clone() attributes.current.positioner.clone()
}); });
if positioner.reactive { if positioner.reactive {
unconstrain_xdg_popup(&surface, &positioner, space, window); let anchor_point = get_anchor_point(&positioner) + loc;
if let Err(err) = surface.send_configure() { if let Some(rect) = output_geo
slog_scope::warn!( .iter()
"Compositor bug: Unable to re-configure reactive popup: {}", .find(|geo| geo.contains(anchor_point))
err .copied()
); {
unconstrain_xdg_popup(&surface, &positioner, loc, rect);
if let Err(err) = surface.send_configure() {
slog_scope::warn!(
"Compositor bug: Unable to re-configure reactive popup: {}",
err
);
}
} }
} }
} }
@ -80,32 +92,18 @@ pub fn update_reactive_popups(space: &Space, window: &Window) {
fn unconstrain_xdg_popup( fn unconstrain_xdg_popup(
surface: &PopupSurface, surface: &PopupSurface,
positioner: &PositionerState, positioner: &PositionerState,
space: &Space, window_loc: Point<i32, Logical>,
window: &Window, rect: Rectangle<i32, Logical>,
) { ) {
let anchor_point = get_anchor_point(&positioner) + space.window_location(&window).unwrap(); let mut relative = rect;
if let Some(output_rect) = space relative.loc -= window_loc;
.outputs_for_window(window) let offset = check_constrained(&surface, positioner.get_geometry(), relative);
.into_iter()
.find(|o| {
space
.output_geometry(o)
.map(|rect| rect.contains(anchor_point))
.unwrap_or(false)
})
.map(|o| space.output_geometry(&o).unwrap())
{
// the output_rect represented relative to the parents coordinate system
let mut relative = output_rect;
relative.loc -= space.window_location(&window).unwrap();
let offset = check_constrained(&surface, positioner.get_geometry(), relative);
if offset.x != 0 || offset.y != 0 { if offset.x != 0 || offset.y != 0 {
slog_scope::debug!("Unconstraining popup: {:?}", surface); slog_scope::debug!("Unconstraining popup: {:?}", surface);
if !unconstrain_flip(&surface, &positioner, relative) { if !unconstrain_flip(&surface, &positioner, relative) {
if !unconstrain_slide(&surface, &positioner, relative) { if !unconstrain_slide(&surface, &positioner, relative) {
unconstrain_resize(&surface, &positioner, relative); unconstrain_resize(&surface, &positioner, relative);
}
} }
} }
} }

View file

@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only // SPDX-License-Identifier: GPL-3.0-only
pub mod drm; pub mod drm;
pub mod export_dmabuf; //pub mod export_dmabuf;
pub mod output_configuration; pub mod output_configuration;
pub mod toplevel_info; pub mod toplevel_info;
pub mod toplevel_management; pub mod toplevel_management;

View file

@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only // SPDX-License-Identifier: GPL-3.0-only
use smithay::{ use smithay::{
output::{Mode, Output, OutputData}, output::{Mode, Output},
reexports::{ reexports::{
wayland_protocols_wlr::output_management::v1::server::{ wayland_protocols_wlr::output_management::v1::server::{
zwlr_output_configuration_head_v1::{self, ZwlrOutputConfigurationHeadV1}, zwlr_output_configuration_head_v1::{self, ZwlrOutputConfigurationHeadV1},
@ -17,6 +17,7 @@ use smithay::{
}, },
}, },
utils::{Logical, Physical, Point, Size, Transform}, utils::{Logical, Physical, Point, Size, Transform},
wayland::output::WlOutputData,
}; };
use std::{ use std::{
convert::{TryFrom, TryInto}, convert::{TryFrom, TryInto},
@ -498,7 +499,7 @@ where
impl<D> OutputConfigurationState<D> impl<D> OutputConfigurationState<D>
where where
D: GlobalDispatch<ZwlrOutputManagerV1, OutputMngrGlobalData> D: GlobalDispatch<ZwlrOutputManagerV1, OutputMngrGlobalData>
+ GlobalDispatch<WlOutput, OutputData> + GlobalDispatch<WlOutput, WlOutputData>
+ Dispatch<ZwlrOutputManagerV1, OutputMngrInstanceData> + Dispatch<ZwlrOutputManagerV1, OutputMngrInstanceData>
+ Dispatch<ZwlrOutputHeadV1, Output> + Dispatch<ZwlrOutputHeadV1, Output>
+ Dispatch<ZwlrOutputModeV1, Mode> + Dispatch<ZwlrOutputModeV1, Mode>

View file

@ -413,7 +413,7 @@ fn send_toplevel_to_client<D>(
.iter() .iter()
.filter(|o| !handle_state.outputs.contains(o)) .filter(|o| !handle_state.outputs.contains(o))
{ {
new_output.with_client_outputs(dh, &client, |_dh, wl_output| { new_output.with_client_outputs(&client, |wl_output| {
instance.output_enter(wl_output); instance.output_enter(wl_output);
}); });
changed = true; changed = true;
@ -423,7 +423,7 @@ fn send_toplevel_to_client<D>(
.iter() .iter()
.filter(|o| !state.outputs.contains(o)) .filter(|o| !state.outputs.contains(o))
{ {
old_output.with_client_outputs(dh, &client, |_dh, wl_output| { old_output.with_client_outputs(&client, |wl_output| {
instance.output_leave(wl_output); instance.output_leave(wl_output);
}); });
changed = true; changed = true;

View file

@ -821,7 +821,7 @@ where
.iter() .iter()
.filter(|o| !handle_state.outputs.contains(o)) .filter(|o| !handle_state.outputs.contains(o))
{ {
new_output.with_client_outputs(dh, &client, |_dh, wl_output| { new_output.with_client_outputs(&client, |wl_output| {
instance.output_enter(wl_output); instance.output_enter(wl_output);
}); });
changed = true; changed = true;
@ -831,7 +831,7 @@ where
.iter() .iter()
.filter(|o| !group.outputs.contains(o)) .filter(|o| !group.outputs.contains(o))
{ {
old_output.with_client_outputs(dh, &client, |_dh, wl_output| { old_output.with_client_outputs(&client, |wl_output| {
instance.output_leave(wl_output); instance.output_leave(wl_output);
}); });
changed = true; changed = true;