wip(nescope): desktop mode places windows one by one

This commit is contained in:
Wanjohi
2026-10-25 14:19:19 +03:00
parent 466fbf26b3
commit effb3da60b
6 changed files with 197 additions and 12 deletions
+35 -9
View File
@@ -471,10 +471,18 @@ impl XwmHandler for CalloopData {
class = ?window.class(),
"X11 map_window_request"
);
let geo = Rectangle::new(
(0, 0).into(),
(self.state.width as i32, self.state.height as i32).into(),
);
let output = (self.state.width as i32, self.state.height as i32);
let geo = if self.state.desktop {
// The size and place the window asked for, kept on the output.
let asked = window.geometry();
let w = if asked.size.w > 1 { asked.size.w.min(output.0) } else { output.0 };
let h = if asked.size.h > 1 { asked.size.h.min(output.1) } else { output.1 };
let x = asked.loc.x.clamp(0, output.0 - w);
let y = asked.loc.y.clamp(0, output.1 - h);
Rectangle::new((x, y).into(), (w, h).into())
} else {
Rectangle::new((0, 0).into(), output.into())
};
if let Err(e) = window.configure(geo) {
tracing::warn!("configure failed: {e}");
}
@@ -482,17 +490,26 @@ impl XwmHandler for CalloopData {
tracing::error!("set_mapped failed: {e}");
return;
}
let x11 = window.clone();
let win = Window::new_x11_window(window);
self.state.space.map_element(win.clone(), (0, 0), true);
self.state.space.map_element(win.clone(), geo.loc, true);
// An id on first map; a window unmapped and mapped again keeps it.
self.state.window_opened(&win);
self.state.window_placed(&x11);
self.state.determine_and_apply_focus();
}
fn mapped_override_redirect_window(&mut self, _xwm: XwmId, window: X11Surface) {
let location = window.geometry().loc;
let x11 = window.clone();
let win = Window::new_x11_window(window);
self.state.space.map_element(win, location, false);
self.state.space.map_element(win.clone(), location, false);
// A menu or tooltip is a window of its own when windows are streamed
// one by one; otherwise it is drawn into the window below it.
if self.state.desktop {
self.state.window_opened(&win);
self.state.window_placed(&x11);
}
}
fn unmapped_window(&mut self, _xwm: XwmId, window: X11Surface) {
@@ -511,6 +528,8 @@ impl XwmHandler for CalloopData {
if !window.is_override_redirect() {
let _ = window.set_mapped(false);
}
if !window.is_override_redirect() || self.state.desktop {
// Closed for the hub; mapped again, it opens under the same id.
self.state
.window_unmapped(&crate::state::WinKey::X11(window.window_id()));
@@ -531,8 +550,8 @@ impl XwmHandler for CalloopData {
&mut self,
_xwm: XwmId,
window: X11Surface,
_x: Option<i32>,
_y: Option<i32>,
x: Option<i32>,
y: Option<i32>,
w: Option<u32>,
h: Option<u32>,
_reorder: Option<Reorder>,
@@ -545,8 +564,14 @@ impl XwmHandler for CalloopData {
}
let (req_w, req_h) = (w, h);
let cur = window.geometry();
// A desktop window may also move itself; a game's stays at the origin.
let (cx, cy) = if self.state.desktop {
(x.unwrap_or(cur.loc.x), y.unwrap_or(cur.loc.y))
} else {
(cur.loc.x, cur.loc.y)
};
let (x, y, w, h) = crate::state::clamp_to_output(
(cur.loc.x, cur.loc.y, cur.size.w, cur.size.h),
(cx, cy, cur.size.w, cur.size.h),
(w, h),
(self.state.width as i32, self.state.height as i32),
);
@@ -575,6 +600,7 @@ impl XwmHandler for CalloopData {
if let Some(elem) = target_elem {
self.state.space.map_element(elem, geometry.loc, false);
}
self.state.window_placed(&window);
}
fn property_notify(&mut self, _xwm: XwmId, window: X11Surface, property: WmWindowProperty) {
+8
View File
@@ -175,6 +175,13 @@ struct Args {
#[arg(long, env = "NESCOPE_X_DISPLAY", default_value_t = 1)]
x_display: u32,
/// Let every window keep the size and place it asks for, give menus and
/// popups ids of their own, and say where each window is. For streaming a
/// desktop's windows one by one; without it every window fills the output,
/// which is what a single fullscreen game wants.
#[arg(long, env = "NESCOPE_WINDOWS")]
windows: bool,
/// Game command — everything after '--'.
///
/// **Optional.** With one, nescope launches it and exits once it has
@@ -334,6 +341,7 @@ fn main() {
args.hdr,
args.render_device.clone(),
);
state.desktop = args.windows;
if args.xwayland {
state.init_xwayland(&loop_handle, Some(args.x_display));
}
+67 -2
View File
@@ -284,6 +284,8 @@ pub struct NescopeState {
pub loop_handle: LoopHandle<'static, CalloopData>,
/// True after the game command has been spawned.
pub game_launched: bool,
/// `--windows`: windows keep their own geometry and are placed one by one.
pub desktop: bool,
/// Time since all mapped windows disappeared (for auto-exit).
pub no_clients_since: Option<std::time::Instant>,
}
@@ -419,6 +421,7 @@ impl NescopeState {
fps,
loop_handle,
game_launched: false,
desktop: false,
no_clients_since: None,
};
@@ -592,7 +595,9 @@ impl NescopeState {
});
tl.send_pending_configure();
}
if let Some(x11) = window.x11_surface() {
if let Some(x11) = window.x11_surface()
&& !self.desktop
{
let geo = Rectangle::new((0, 0).into(), (width as i32, height as i32).into());
if let Err(e) = x11.configure(geo) {
tracing::warn!("Failed to reconfigure X11 window on resize: {e}");
@@ -910,7 +915,13 @@ impl NescopeState {
native,
title,
app_id,
size: (clamp16(self.width), clamp16(self.height)),
size: match window.x11_surface() {
Some(x11) if self.desktop => {
let g = x11.geometry();
(clamp16(g.size.w.max(1) as u32), clamp16(g.size.h.max(1) as u32))
}
_ => (clamp16(self.width), clamp16(self.height)),
},
};
if let Some(messages) =
self.windows
@@ -922,6 +933,60 @@ impl NescopeState {
}
}
/// Tell the hub where an X11 window is, what kind, and whose it is.
/// Desktop mode only: without it every window fills the output and there
/// is nothing to place.
pub(crate) fn window_placed(&mut self, x11: &smithay::xwayland::X11Surface) {
use nesprotocol::window::{
WINDOW_KIND_DIALOG, WINDOW_KIND_NORMAL, WINDOW_KIND_POPUP, WindowEvent,
encode_event_message,
};
if !self.desktop {
return;
}
let Some(window) = self.windows.id_of(&WinKey::X11(x11.window_id())) else {
return;
};
let parent = x11
.is_transient_for()
.and_then(|p| self.windows.id_of(&WinKey::X11(p)));
let kind = if x11.is_override_redirect() {
WINDOW_KIND_POPUP
} else if parent.is_some()
|| matches!(
x11.window_type(),
Some(smithay::xwayland::xwm::WmWindowType::Dialog)
)
{
WINDOW_KIND_DIALOG
} else {
WINDOW_KIND_NORMAL
};
// A menu names no parent; it belongs to the window that has focus.
let parent = parent.or_else(|| {
(kind == WINDOW_KIND_POPUP)
.then_some(self.focused_x11_window)
.flatten()
.and_then(|f| self.windows.id_of(&WinKey::X11(f)))
.filter(|p| *p != window)
});
let g = x11.geometry();
let mut m = Vec::new();
encode_event_message(
&mut m,
&WindowEvent::Placed {
window,
parent,
kind,
x: g.loc.x.clamp(i16::MIN as i32, i16::MAX as i32) as i16,
y: g.loc.y.clamp(i16::MIN as i32, i16::MAX as i32) as i16,
width: clamp16(g.size.w.max(1) as u32),
height: clamp16(g.size.h.max(1) as u32),
},
);
self.send_box_message(&m);
}
/// A toplevel was destroyed.
pub(crate) fn window_closed(&mut self, key: &WinKey) {
let id = self.windows.id_of(key);
+1
View File
@@ -208,6 +208,7 @@ fn is_cursor_or_window(kind: u8) -> bool {
| MSG_WINDOW_ICON
| MSG_WINDOW_SIZE
| MSG_WINDOW_CLOSED
| MSG_WINDOW_PLACED
| MSG_WINDOW_PID
| MSG_CURSOR_WINDOW
)
+9
View File
@@ -92,6 +92,8 @@ pub fn wants_cursor(sub: Subscription, cursor_window: WindowId) -> bool {
struct OpenWindow {
opened: WindowEvent,
icon: Option<WindowEvent>,
/// The latest placement, so a client that joins late places the window too.
placed: Option<WindowEvent>,
}
/// The box's windows, their owners, and which encoder is primary.
@@ -124,6 +126,7 @@ impl WindowTable {
OpenWindow {
opened: event.clone(),
icon: None,
placed: None,
},
);
}
@@ -153,6 +156,11 @@ impl WindowTable {
open.icon = Some(event.clone());
}
}
WindowEvent::Placed { window, .. } => {
if let Some(open) = self.open.get_mut(window) {
open.placed = Some(event.clone());
}
}
WindowEvent::Closed { window } => {
self.open.remove(window);
let retired = &mut self.retired;
@@ -203,6 +211,7 @@ impl WindowTable {
for open in self.open.values() {
out.push(open.opened.clone());
out.extend(open.icon.clone());
out.extend(open.placed.clone());
}
out
}
+77 -1
View File
@@ -186,6 +186,19 @@ pub const MSG_WINDOW_TITLE: u8 = 0x21;
pub const MSG_WINDOW_ICON: u8 = 0x22;
pub const MSG_WINDOW_SIZE: u8 = 0x23;
pub const MSG_WINDOW_CLOSED: u8 = 0x24;
/// Where a window is, what kind it is, and which window it belongs to. Sent
/// after [`MSG_WINDOW_OPENED`] and again whenever it moves or resizes, by a
/// compositor that lets windows keep their own size. A client that ignores it
/// still gets every window, just without placement.
pub const MSG_WINDOW_PLACED: u8 = 0x25;
/// [`WindowEvent::Placed`] `kind`: an ordinary top-level window.
pub const WINDOW_KIND_NORMAL: u8 = 0;
/// A dialog: kept above its parent and moved with it.
pub const WINDOW_KIND_DIALOG: u8 = 1;
/// A menu, tooltip or other popup: no frame, placed relative to its parent,
/// gone when it loses the pointer.
pub const WINDOW_KIND_POPUP: u8 = 2;
/// Largest icon accepted, per side. A window icon is a few dozen pixels; this
/// bounds the allocation a corrupt length could ask for.
@@ -324,6 +337,19 @@ pub enum WindowEvent {
Closed {
window: WindowId,
},
Placed {
window: WindowId,
/// The window this one belongs to, when it has one.
parent: Option<WindowId>,
/// One of the `WINDOW_KIND_*` values.
kind: u8,
/// Position on the box's screen. A client places a child relative
/// to its parent by the difference of the two.
x: i16,
y: i16,
width: u16,
height: u16,
},
}
impl WindowEvent {
@@ -333,7 +359,8 @@ impl WindowEvent {
| WindowEvent::Title { window, .. }
| WindowEvent::Icon { window, .. }
| WindowEvent::Size { window, .. }
| WindowEvent::Closed { window } => *window,
| WindowEvent::Closed { window }
| WindowEvent::Placed { window, .. } => *window,
}
}
@@ -345,6 +372,7 @@ impl WindowEvent {
WindowEvent::Icon { .. } => MSG_WINDOW_ICON,
WindowEvent::Size { .. } => MSG_WINDOW_SIZE,
WindowEvent::Closed { .. } => MSG_WINDOW_CLOSED,
WindowEvent::Placed { .. } => MSG_WINDOW_PLACED,
}
}
@@ -387,6 +415,23 @@ impl WindowEvent {
buf.extend_from_slice(&height.to_le_bytes());
}
WindowEvent::Closed { .. } => {}
WindowEvent::Placed {
parent,
kind,
x,
y,
width,
height,
..
} => {
buf.push(u8::from(parent.is_some()));
buf.extend_from_slice(&parent.unwrap_or(0).to_le_bytes());
buf.push(*kind);
buf.extend_from_slice(&x.to_le_bytes());
buf.extend_from_slice(&y.to_le_bytes());
buf.extend_from_slice(&width.to_le_bytes());
buf.extend_from_slice(&height.to_le_bytes());
}
}
}
@@ -439,6 +484,19 @@ impl WindowEvent {
height: r.u16()?,
},
MSG_WINDOW_CLOSED => WindowEvent::Closed { window },
MSG_WINDOW_PLACED => {
let has_parent = r.take(1)?[0] != 0;
let parent = r.u16()?;
WindowEvent::Placed {
window,
parent: has_parent.then_some(parent),
kind: r.take(1)?[0],
x: r.u16()? as i16,
y: r.u16()? as i16,
width: r.u16()?,
height: r.u16()?,
}
}
_ => return None,
})
}
@@ -620,6 +678,24 @@ mod tests {
fn events() -> Vec<WindowEvent> {
vec![
WindowEvent::Placed {
window: 7,
parent: Some(5),
kind: WINDOW_KIND_DIALOG,
x: -12,
y: 40,
width: 1024,
height: 768,
},
WindowEvent::Placed {
window: 8,
parent: None,
kind: WINDOW_KIND_POPUP,
x: 0,
y: 0,
width: 1,
height: 1,
},
WindowEvent::Opened {
window: 5,
width: 1920,