Skip to content

Commit

Permalink
runa-orbiter: re-structure surface/role mods
Browse files Browse the repository at this point in the history
Signed-off-by: Yuxuan Shui <[email protected]>
  • Loading branch information
yshui committed Mar 11, 2024
1 parent 7d3a198 commit 7f9db14
Show file tree
Hide file tree
Showing 12 changed files with 538 additions and 523 deletions.
2 changes: 1 addition & 1 deletion crescent/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl Renderer {

fn render(&mut self, output: wgpu::SurfaceTexture) {
use runa_orbiter::{
shell::surface::roles::subsurface_iter, utils::geometry::coords::Map as _,
shell::surface::roles::subsurface::subsurface_iter, utils::geometry::coords::Map as _,
};
let shell = self.shell.borrow();
let output_scale = shell.scale_f32().map(|f| NotNan::try_from(f).unwrap());
Expand Down
14 changes: 7 additions & 7 deletions crescent/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use runa_orbiter::{
shell::{
buffers,
output::OutputChange,
surface::{self, roles::subsurface_iter},
surface::{self, roles::subsurface::subsurface_iter},
xdg::{Layout, XdgShell},
Shell, ShellEvent,
},
Expand Down Expand Up @@ -86,15 +86,15 @@ impl Window {

#[derive(Debug)]
pub struct DefaultShellSurfaceState<B: buffers::BufferLike> {
pub base: surface::SurfaceState<DefaultShell<B>>,
pub base: surface::State<DefaultShell<B>>,
/// Whether this state is the current state of its corresponding surface.
pub is_current: bool,
/// The index of this state in the window stack, if it's in the stack.
pub stack_index: Option<Index<Window>>,
}

impl<B: buffers::BufferLike> DefaultShellSurfaceState<B> {
pub fn new(base: surface::SurfaceState<DefaultShell<B>>) -> Self {
pub fn new(base: surface::State<DefaultShell<B>>) -> Self {
Self {
base,
is_current: false,
Expand Down Expand Up @@ -143,7 +143,7 @@ impl<B: buffers::BufferLike> DefaultShell<B> {
let state = self.get(top_level.surface_state);
let surface = state.surface().unwrap();
let role = surface
.role::<runa_orbiter::shell::xdg::TopLevel>()
.role::<runa_orbiter::shell::surface::roles::xdg::TopLevel>()
.unwrap();
if let Some(geometry) = role.geometry() {
if !geometry.to().contains(position) {
Expand Down Expand Up @@ -383,7 +383,7 @@ impl<B: buffers::BufferLike> Shell for DefaultShell<B> {
type Token = DefaultKey;

#[tracing::instrument(skip_all)]
fn allocate(&mut self, state: surface::SurfaceState<Self>) -> Self::Token {
fn allocate(&mut self, state: surface::State<Self>) -> Self::Token {
self.storage.insert(DefaultShellSurfaceState::new(state))
}

Expand All @@ -392,7 +392,7 @@ impl<B: buffers::BufferLike> Shell for DefaultShell<B> {
tracing::debug!("Released {:?}, #surfaces left: {}", key, self.storage.len());
}

fn get(&self, key: Self::Token) -> &surface::SurfaceState<Self> {
fn get(&self, key: Self::Token) -> &surface::State<Self> {
// This unwrap cannot fail, unless there is a bug in this implementation to
// cause it to return an invalid token.
self.storage
Expand All @@ -404,7 +404,7 @@ impl<B: buffers::BufferLike> Shell for DefaultShell<B> {
fn get_disjoint_mut<const N: usize>(
&mut self,
keys: [Self::Token; N],
) -> [&mut surface::SurfaceState<Self>; N] {
) -> [&mut surface::State<Self>; N] {
self.storage
.get_disjoint_mut(keys)
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion runa-orbiter/src/globals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ where
continue
}
self.surfaces.extend(
crate::shell::surface::roles::subsurface_iter(
crate::shell::surface::roles::subsurface::subsurface_iter(
surface.inner.current_key(),
&*shell,
)
Expand Down
4 changes: 2 additions & 2 deletions runa-orbiter/src/objects/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ where
async move {
let surface = ctx.objects().get::<Self>(object_id).unwrap().0.clone();
let role = surface
.role::<roles::Subsurface<<Ctx::ServerContext as HasShell>::Shell>>()
.role::<roles::subsurface::Subsurface<<Ctx::ServerContext as HasShell>::Shell>>()
.unwrap();
let parent = role.parent().upgrade().unwrap();
let mut parent_stack_pending = parent.pending_mut();
Expand Down Expand Up @@ -768,7 +768,7 @@ where
if !ctx.objects().contains(id.0) {
let shell = ctx.server_context().shell();
let mut shell = shell.borrow_mut();
if !crate::shell::surface::roles::Subsurface::attach(
if !roles::subsurface::Subsurface::attach(
parent,
surface.clone(),
&mut shell,
Expand Down
4 changes: 2 additions & 2 deletions runa-orbiter/src/objects/shm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ lazy_static::lazy_static! {
}

thread_local! {
static SIGBUS_COUNT: Cell<usize> = Cell::new(0);
static SIGBUS_COUNT: Cell<usize> = const { Cell::new(0) };
}

/// The number of times a recoverable SIGBUS has occurred for the current
Expand Down Expand Up @@ -86,7 +86,7 @@ unsafe fn map_zeroed(addr: *const libc::c_void, len: usize) -> Result<(), libc::
}

/// Handle a SIGBUS signal. Tries to recover from SIGBUS caused by a client
/// shrinking its shm pool. You MUST call this function is your SIGBUS handler
/// shrinking its shm pool. You MUST call this function in your SIGBUS handler
/// if you want to map shm pools.
///
/// Returns `true` if the signal was handled, `false` otherwise. Usually you
Expand Down
50 changes: 27 additions & 23 deletions runa-orbiter/src/objects/xdg_shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use runa_wayland_protocols::stable::xdg_shell::{
use runa_wayland_types::{NewId, Object as WaylandObject, Str};

use crate::{
shell::{surface::LayoutEvent, xdg, HasShell, Shell},
shell::{
surface::{roles::xdg as xdg_roles, LayoutEvent},
HasShell, Shell,
},
utils::geometry::{Extent, Point, Rectangle},
};

Expand All @@ -28,7 +31,7 @@ use crate::{
pub struct WmBase;

#[wayland_object]
impl<S: xdg::XdgShell, Ctx: Client> xdg_wm_base::RequestDispatch<Ctx> for WmBase
impl<S: crate::shell::xdg::XdgShell, Ctx: Client> xdg_wm_base::RequestDispatch<Ctx> for WmBase
where
Ctx::ServerContext: HasShell<Shell = S>,
Ctx::Object: From<Surface<S>>,
Expand Down Expand Up @@ -74,7 +77,7 @@ where
let mut shell = server_context.shell().borrow_mut();
let inserted = objects
.try_insert_with(id.0, || {
let role = crate::shell::xdg::Surface::new(id);
let role = xdg_roles::Surface::new(id);
surface.set_role(role, &mut shell);
Surface { inner: surface }.into()
})
Expand Down Expand Up @@ -125,10 +128,7 @@ where
message: &'ctx mut Self::Message,
) -> Self::Future<'ctx> {
tracing::debug!(?message, "LayoutEventHandler::poll_handle_event");
use crate::shell::{
surface::Role,
xdg::{Surface as XdgSurface, TopLevel},
};
use crate::shell::surface::roles::Role;
let surface = objects
.get::<crate::objects::compositor::Surface<<Ctx::ServerContext as HasShell>::Shell>>(
self.surface_object_id,
Expand All @@ -137,13 +137,17 @@ where
let mut connection = Pin::new(connection);
async move {
if let Some(size) = message.0.extent {
if let Some(role_object_id) = surface.inner.role::<TopLevel>().map(|r| {
assert!(
<TopLevel as Role<<Ctx::ServerContext as HasShell>::Shell>>::is_active(&r),
"TopLevel role no longer active"
);
r.object_id
}) {
if let Some(role_object_id) =
surface.inner.role::<xdg_roles::TopLevel>().map(|r| {
assert!(
<xdg_roles::TopLevel as Role<
<Ctx::ServerContext as HasShell>::Shell,
>>::is_active(&r),
"TopLevel role no longer active"
);
r.object_id
})
{
connection
.as_mut()
.send(role_object_id, xdg_toplevel::events::Configure {
Expand All @@ -159,7 +163,7 @@ where
}
// Send xdg_surface.configure event
let (serial, role_object_id) = {
let mut role = surface.inner.role_mut::<XdgSurface>().unwrap();
let mut role = surface.inner.role_mut::<xdg_roles::Surface>().unwrap();
let serial = role.serial;
role.serial = role.serial.checked_add(1).unwrap_or(1.try_into().unwrap());
role.pending_serial.push_back(serial);
Expand Down Expand Up @@ -303,8 +307,8 @@ where
let surface = objects.get::<Self>(object_id).unwrap().inner.clone();
let inserted = objects
.try_insert_with(id.0, || {
let base_role = surface.role::<xdg::Surface>().unwrap().clone();
let role = crate::shell::xdg::TopLevel::new(base_role, id.0);
let base_role = surface.role::<xdg_roles::Surface>().unwrap().clone();
let role = xdg_roles::TopLevel::new(base_role, id.0);
surface.set_role(role, &mut server_context.shell().borrow_mut());

// Start listening to layout events
Expand All @@ -329,7 +333,7 @@ where
fn ack_configure(ctx: &mut Ctx, object_id: u32, serial: u32) -> Self::AckConfigureFut<'_> {
async move {
let this = ctx.objects().get::<Self>(object_id).unwrap();
let mut role = this.inner.role_mut::<xdg::Surface>().unwrap();
let mut role = this.inner.role_mut::<xdg_roles::Surface>().unwrap();
while let Some(front) = role.pending_serial.front() {
if front.get() > serial {
break
Expand All @@ -353,7 +357,7 @@ where
) -> Self::SetWindowGeometryFut<'_> {
async move {
let this = ctx.objects().get::<Self>(object_id).unwrap();
let mut role = this.inner.role_mut::<xdg::Surface>().unwrap();
let mut role = this.inner.role_mut::<xdg_roles::Surface>().unwrap();
role.pending_geometry = Some(Rectangle {
loc: Point::new(x, y),
size: Extent::new(width, height),
Expand Down Expand Up @@ -439,7 +443,7 @@ where
fn set_title<'a>(ctx: &'a mut Ctx, object_id: u32, title: Str<'a>) -> Self::SetTitleFut<'a> {
async move {
let this = ctx.objects().get::<Self>(object_id).unwrap();
let mut role = this.0.role_mut::<xdg::TopLevel>().unwrap();
let mut role = this.0.role_mut::<xdg_roles::TopLevel>().unwrap();
role.title = Some(String::from_utf8_lossy(title.0).into_owned());
Ok(())
}
Expand All @@ -455,7 +459,7 @@ where
fn set_app_id<'a>(ctx: &'a mut Ctx, object_id: u32, app_id: Str<'a>) -> Self::SetAppIdFut<'a> {
async move {
let this = ctx.objects().get::<Self>(object_id).unwrap();
let mut role = this.0.role_mut::<xdg::TopLevel>().unwrap();
let mut role = this.0.role_mut::<xdg_roles::TopLevel>().unwrap();
role.app_id = Some(String::from_utf8_lossy(app_id.0).into_owned());
Ok(())
}
Expand All @@ -469,7 +473,7 @@ where
) -> Self::SetMaxSizeFut<'_> {
async move {
let this = ctx.objects().get::<Self>(object_id).unwrap();
let mut role = this.0.role_mut::<xdg::TopLevel>().unwrap();
let mut role = this.0.role_mut::<xdg_roles::TopLevel>().unwrap();
role.pending.max_size = Some(Extent::new(width, height));
Ok(())
}
Expand All @@ -483,7 +487,7 @@ where
) -> Self::SetMinSizeFut<'_> {
async move {
let this = ctx.objects().get::<Self>(object_id).unwrap();
let mut role = this.0.role_mut::<xdg::TopLevel>().unwrap();
let mut role = this.0.role_mut::<xdg_roles::TopLevel>().unwrap();
role.pending.min_size = Some(Extent::new(width, height));
Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions runa-orbiter/src/shell/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ impl<Base: Debug + 'static> BufferLike for Buffer<Base> {

/// An empty private trait just to make enum_dispatch generate the `From` impls.
#[enum_dispatch::enum_dispatch]
#[allow(dead_code)]
trait BufferBaseFrom {}

/// An enum of all buffer base types defined in this crate.
Expand Down
37 changes: 32 additions & 5 deletions runa-orbiter/src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
pub mod buffers;
pub mod output;
pub mod surface;
pub mod xdg;
use std::cell::RefCell;

use runa_core::events::EventSource;
Expand Down Expand Up @@ -53,18 +52,18 @@ pub trait Shell: Sized + EventSource<ShellEvent> + 'static {
type Buffer: buffers::BufferLike;

/// Allocate a SurfaceState and returns a handle to it.
fn allocate(&mut self, state: surface::SurfaceState<Self>) -> Self::Token;
fn allocate(&mut self, state: surface::State<Self>) -> Self::Token;

/// Release a token.
fn destroy(&mut self, key: Self::Token);

/// Get a reference to a SurfaceState by key.
///
/// Returns None if the key is invalid.
fn get(&self, key: Self::Token) -> &surface::SurfaceState<Self>;
fn get(&self, key: Self::Token) -> &surface::State<Self>;

/// Get a mutable reference to a SurfaceState.
fn get_mut(&mut self, key: Self::Token) -> &mut surface::SurfaceState<Self> {
fn get_mut(&mut self, key: Self::Token) -> &mut surface::State<Self> {
self.get_disjoint_mut([key])[0]
}

Expand All @@ -77,7 +76,7 @@ pub trait Shell: Sized + EventSource<ShellEvent> + 'static {
fn get_disjoint_mut<const N: usize>(
&mut self,
keys: [Self::Token; N],
) -> [&mut surface::SurfaceState<Self>; N];
) -> [&mut surface::State<Self>; N];

/// Callback which is called when a role is added to a surface corresponds
/// to the given surface state. A role can be attached using a committed
Expand Down Expand Up @@ -192,3 +191,31 @@ pub trait Seat: EventSource<SeatEvent> {
/// Get the name of the seat.
fn name(&self) -> &str;
}

pub mod xdg {
//! Extensions to [`super::Shell`] to provide xdg shell specific
//! functionalities.

use crate::utils::geometry::{coords, Extent, Point};

/// Surface layout
///
/// A surface layout is where the surface is positioned on the screen, and
/// its screen space size.
#[derive(Debug, Default, Clone, Copy)]
pub struct Layout {
/// The position of the surface on the screen.
pub position: Option<Point<i32, coords::Screen>>,
/// The size of the surface on the screen.
pub extent: Option<Extent<u32, coords::Screen>>,
}

/// Extension of [`super::Shell`] to provide xdg shell specific
/// information.
pub trait XdgShell: super::Shell {
/// Ask the shell to calculate the layout of the given surface.
fn layout(&self, _key: Self::Token) -> Layout {
Layout::default()
}
}
}
Loading

0 comments on commit 7f9db14

Please sign in to comment.