Files
chandra/compositor/host/compositor.mbt
T
Claude fa932f9ac0
CI / test (push) Successful in 3m44s
GTK windows (Gajim, GNOME apps) resize from their shadow and all four edges
GTK draws no border, only a shadow, and resizes only from a press in that
shadow, which Chandra never hands to the client. Chandra's own zones were
~4px wide around the visible edge and there was none on the top edge, so
Gajim (GTK 4) looked unresizable. Client-decorated windows now get zones
that reach up to 12px into the shadow (GTK's own handle size, at least 6px)
plus 3px over their edge, with 16px corners, top edge included.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hiu4r7mEXBdKY6cgv6
2026-11-26 02:48:22 +00:00

2024 lines
65 KiB
MoonBit

///| Pure-MoonBit Wayland compositor host wiring WaylandServer, lib/wm, Breeze SSD, SceneGraph, and SoftwareRenderer.
///|
pub(all) struct HostWindow {
id : UInt64
mut title : String
mut app_id : String
mut geometry : @scene.Rect
mut focused : Bool
mut maximized : Bool
mut fullscreen : Bool
mut surface : @scene.ShmBufferSnapshot?
/// A size change still to be sent to the client (coalesced while dragging a divider).
mut cfg_dirty : Bool
/// The client draws its own title bar and shadow (GTK: GNOME Files and most GNOME apps).
/// Chandra then draws no frame: the client fills the box the frame would have taken
/// (`client_rect`), and `geo_x`/`geo_y` is where that box starts inside its buffer.
mut csd : Bool
mut geo_x : Int
mut geo_y : Int
}
///| Height of the server-side title bar above `geometry`, and the frame's side/bottom border:
/// the layout in `@scene.build_breeze_decoration` (outer box w + 4, h + 34).
let frame_top : Int = 32
///|
let frame_side : Int = 2
///| The box the client's own window fills on screen. A server-decorated window's is its
/// geometry (Chandra's frame goes around it); a client-decorated one takes the frame's whole
/// outer box, so tiling, title-bar hit areas and resize zones are the same for both kinds.
pub fn HostWindow::client_rect(self : HostWindow) -> @scene.Rect {
let g = self.geometry
if self.csd && !self.fullscreen {
@scene.Rect::new(g.x - frame_side, g.y - frame_top, g.w + 2 * frame_side, g.h + frame_top + frame_side)
} else {
g
}
}
///| Screen position of the client buffer's top-left pixel (surface-local (0, 0)).
pub fn HostWindow::surface_origin(self : HostWindow) -> (Int, Int) {
let r = self.client_rect()
if self.csd {
(r.x - self.geo_x, r.y - self.geo_y)
} else {
(r.x, r.y)
}
}
///| A client popup (xdg_popup): menus, combo lists, tooltips.
pub(all) struct HostPopup {
id : UInt64
parent : UInt64
/// Position relative to the parent's content origin.
mut rx : Int
mut ry : Int
mut w : Int
mut h : Int
mut surface : @scene.ShmBufferSnapshot?
/// The client asked for an explicit grab (xdg_popup.grab): it takes keyboard focus once mapped.
mut grab : Bool
/// Window-geometry origin inside the buffer (client-drawn shadow); the buffer is drawn
/// this much up/left so the visible menu lands where the positioner put it.
mut geo_x : Int
mut geo_y : Int
}
///|
pub(all) struct ChandraCompositor {
/// Open popups, oldest first (the last is topmost).
popups : Array[HostPopup]
server : @wayland.WaylandServer
wm : @wm.WmState
/// The theme in use: `dark_theme` (the configured one) or Moon Day, per the Dark mode setting.
mut theme : @scene.ThemeTokens
dark_theme : @scene.ThemeTokens
/// Also set the system-wide dark/light preference for apps (system_theme.mbt). The login
/// session turns it on; nested windows and tests leave it off.
mut sync_system_theme : Bool
/// The xdg-desktop-portal Settings backend apps read the dark/light preference from
/// (compositor/portal), started with the first `sync_system_theme_pref`.
mut settings_portal : @portal.SettingsPortal?
/// The control socket (control.mbt): chandra-ctl reload / list. None until `start_control`.
mut control : ControlServer?
/// Running as the login session (--drm): power actions act on the machine (power.mbt).
mut login_session : Bool
/// Power (power.mbt): when input last came (monotonic ms), whether the screen is off for being
/// idle, and whether this idle spell already suspended.
mut last_input_ms : UInt64
mut screen_off : Bool
mut idle_suspended : Bool
mut output_w : Int
mut output_h : Int
mut work_area : @wm.Rect
windows : Map[UInt64, HostWindow]
damage : @scene.DamageTracker
shell : @shell.DesktopShell
mut ptr_x : Double
mut ptr_y : Double
mut ptr_win : UInt64?
/// Divider being dragged between tiled windows.
mut split_drag : @wm.SplitHandle?
/// The last left press on a title bar (titleclick.mbt): drag to move, double-click to maximize.
mut title_press : TitlePress?
/// True while a button press was forwarded to a client (so its release is too).
mut press_fwd : Bool
mut kbd_win : UInt64?
mut mods : UInt
/// A Super key is down and nothing else was pressed since: releasing it is a Super tap (the
/// launcher by default; shortcuts.mbt).
mut super_solo : Bool
/// Whether a Super key is held (shortcuts.mbt).
mut super_down : Bool
/// Keys a shortcut used, whose release must not reach a client either (shortcuts.mbt).
sc_swallow : Array[UInt]
/// While the Shortcuts tab waits for a combo: keys it took (their releases are swallowed) and
/// whether Super is down alone (a Super tap binds "Super").
capture_swallow : Array[UInt]
mut capture_super : Bool
/// Show desktop (showdesktop.mbt): the windows it minimized (bottom to top) so doing it again
/// can restore them.
mut desktop_hidden : Array[UInt64]
mut layout_mode : @wm.LayoutMode
/// Set when the user asked to end the session (the launcher's Log out); the main loop then exits.
mut quit_requested : Bool
/// Alt+Tab switcher (switcher.mbt): whether it is up, its window list (most recent first) and selection.
mut switcher_on : Bool
mut switcher : Array[UInt64]
mut switcher_sel : Int
/// The switcher's window previews (switcher.mbt), by window; kept while the window lives.
thumbs : Map[UInt64, SwitcherThumb]
/// Preview image handles of closed windows, reused for the next ones.
thumb_free : Array[UInt]
mut alt_down : Bool
/// The window overview (overview.mbt) while it is on screen, and whether the launcher was open
/// when last looked at (opening it opens the overview).
mut overview : Overview?
mut launcher_was_open : Bool
/// Milliseconds spent so far in the shell (panel modules) and in the rasteriser, for --profile.
/// The last composed frame and what it was made from: an unchanged desktop is not redrawn.
mut last_frame : @renderer.Pixmap?
mut last_sig : FrameSig?
/// True when the frame returned by the latest compose_frame differs from the one before it.
mut frame_changed : Bool
mut prof_shell_ms : UInt64
mut prof_raster_ms : UInt64
/// Session restore (session.mbt): apps launched at start whose window hasn't appeared yet,
/// until when they are waited for, and the windows that took their place.
restore_pending : Array[@shell.SessionEntry]
mut restore_until : UInt64
restore_claimed : Map[UInt64, Bool]
/// The session as last written, as last seen (and since when), and when to look again.
mut session_saved : String
mut session_seen : String
mut session_seen_at : UInt64
mut session_check_at : UInt64
/// Screenshots (screenshot.mbt): the selection overlay while it is up, the confirmation, a
/// panel module's request waiting for a frame without its popup ((mode, frames_built then)),
/// key codes whose release is swallowed, client frames held back while the screen is frozen,
/// and where pictures go ("" for ~/Pictures/Screenshots).
mut shot : ShotOverlay?
mut shot_toast : ShotToast?
mut shot_request : (ShotMode, Int)?
mut rec_request : (ShotMode, Int)?
mut frames_built : Int
shot_swallow : Array[UInt]
shot_frozen : Map[UInt64, @scene.ShmBufferSnapshot]
mut shot_dir : String
mut last_shot : String
/// Reads the screen for screenshots when the host draws it (the wasm compositor sets it);
/// None uses the software renderer's last frame.
mut frame_source : (() -> @renderer.Pixmap?)?
/// Records the screen on the host when the host draws it, so frames never cross into the
/// compositor (the wasm compositor sets it); None grabs pixels through `frame_source`.
mut frame_recorder : FrameRecorder?
/// Takes screenshots on the host when the host draws the screen (the wasm compositor sets
/// it); None crops pixels from `frame_source`.
mut frame_shots : FrameShots?
/// Screen recording (recorder.mbt): the recording running, whether it shows the pointer,
/// a press on the REC pill (its release is swallowed too) and the last video saved.
mut rec : Recording?
mut rec_pointer : Bool
mut rec_press : Bool
mut last_recording : String
/// `--kiosk`: one fullscreen app, locked down (kiosk.mbt). None: a normal desktop.
mut kiosk : Kiosk?
/// Chandra's polkit agent (auth.mbt), while it is registered; the system bus socket it uses
/// ("" finds it); the dialog while it is up; key releases it swallows; how to reach polkit's
/// helper (empty: the usual routes; tests set their own); the keymap typing goes through.
mut polkit : @polkit.PolkitAgent?
mut polkit_bus : String
mut auth : AuthUi?
auth_swallow : Array[UInt]
/// The Face unlock window (face_window.mbt): where its controls were drawn, and the change of
/// it last drawn.
face_hits : Array[(@scene.Rect, FaceHit)]
mut face_rev : Int
mut auth_routes : Array[@polkit.HelperRoute]
mut keymap : (Bytes, @polkit.Keymap)?
/// How requests are put into words (tests pass their own process lookups); None: the usual.
mut auth_describe : ((@polkit.AuthRequest) -> @polkit.Description)?
/// A third-party polkit agent was started (when ours is off or couldn't register).
mut fallback_agent_started : Bool
/// The rest of the xkb modifier state (`mods` holds the depressed modifiers).
mut mods_latched : UInt
mut mods_locked : UInt
mut mods_group : UInt
}
///|
pub fn ChandraCompositor::new(
socket_path : String,
theme : @scene.ThemeTokens,
output_w : Int,
output_h : Int,
) -> ChandraCompositor? {
ignore(@font.install()) // Adwaita Mono; keeps the bitmap font if it's missing
let server = match @wayland.WaylandServer::listen(socket_path, output_w, output_h) {
Some(s) => s
None => return None
}
let wm = @wm.WmState::new(
inner_gap=8,
outer_gap=8,
titlebar_h=32,
border_w=2,
)
let work_area = work_area_for(output_w, output_h, theme)
let shell = @shell.DesktopShell::new(socket_path)
shell.set_output(theme, output_w, output_h)
let comp : ChandraCompositor = {
server,
wm,
theme,
dark_theme: theme,
sync_system_theme: false,
polkit: None,
polkit_bus: "",
auth: None,
auth_swallow: [],
face_hits: [],
face_rev: -1,
auth_routes: [],
keymap: None,
auth_describe: None,
fallback_agent_started: false,
mods_latched: 0U,
mods_locked: 0U,
mods_group: 0U,
settings_portal: None,
control: None,
login_session: false,
last_input_ms: @sys.monotonic_ms(),
screen_off: false,
idle_suspended: false,
output_w,
output_h,
work_area,
windows: {},
damage: @scene.DamageTracker::new(),
shell,
ptr_x: 0.0,
ptr_y: 0.0,
popups: [],
ptr_win: None,
split_drag: None,
title_press: None,
press_fwd: false,
kbd_win: None,
mods: 0U,
super_solo: false,
super_down: false,
sc_swallow: [],
capture_swallow: [],
capture_super: false,
desktop_hidden: [],
layout_mode: @wm.LayoutMode::Tiling,
quit_requested: false,
switcher_on: false,
switcher: [],
switcher_sel: 0,
overview: None,
launcher_was_open: false,
thumbs: {},
thumb_free: [],
alt_down: false,
last_frame: None,
last_sig: None,
frame_changed: true,
prof_shell_ms: 0UL,
prof_raster_ms: 0UL,
restore_pending: [],
restore_until: 0UL,
restore_claimed: {},
session_saved: "",
session_seen: "",
session_seen_at: 0UL,
session_check_at: 0UL,
shot: None,
shot_toast: None,
shot_request: None,
rec_request: None,
frames_built: 0,
shot_swallow: [],
shot_frozen: {},
shot_dir: "",
last_shot: "",
frame_source: None,
frame_recorder: None,
frame_shots: None,
rec: None,
rec_pointer: true,
rec_press: false,
last_recording: "",
kiosk: None,
}
// Client popups (menus) are placed to stay on screen: the server asks for the output's
// rectangle relative to the parent's content origin when it applies constraint adjustment.
server.popup_bounds = fn(parent) {
let (ox, oy) = comp.origin_of(parent)
(-ox, -oy, comp.output_w, comp.output_h)
}
Some(comp)
}
///| The part of a `w` x `h` output windows are laid out in.
fn work_area_for(w : Int, h : Int, theme : @scene.ThemeTokens, kiosk~ : Bool = false) -> @wm.Rect {
if kiosk {
// A locked kiosk has no panel and its windows are fullscreen: the whole output.
return { x: 0, y: 0, w, h }
}
@wm.compute_work_area(
{ x: 0, y: 0, w, h },
excl_top=32, // room for the server-side title bar drawn above each client
excl_bottom=theme.panel_height,
)
}
///| The output changed size (the nested window was resized, maximized or made fullscreen):
/// lay the desktop out for the new size, so nothing is drawn past the edge and cropped away.
pub fn ChandraCompositor::resize(self : ChandraCompositor, w : Int, h : Int) -> Unit {
if w <= 0 || h <= 0 || (w == self.output_w && h == self.output_h) {
return
}
self.output_w = w
self.output_h = h
self.work_area = work_area_for(w, h, self.theme, kiosk=self.kiosk_strict())
self.close_overlay() // its frozen frame is the old size
self.stop_recording()
self.server.set_output_size(w, h)
self.shell.set_output(self.theme, w, h)
self.apply_actions(self.wm.fit_work_area(self.work_area))
self.last_frame = None
self.last_sig = None
self.frame_changed = true
self.damage.mark_full()
}
///|
pub fn ChandraCompositor::close(self : ChandraCompositor) -> Unit {
self.stop_recording()
for _, win in self.windows {
release_snapshot(win.surface)
}
self.stop_control()
self.close_auth(Some(Err("The session is ending")))
match self.polkit {
Some(a) => a.stop()
None => ()
}
self.shell.shutdown()
match self.settings_portal {
Some(p) => p.close()
None => ()
}
self.server.close()
}
///| Let the host forget a client surface it captured (a no-op without the host rasteriser).
fn release_snapshot(s : @scene.ShmBufferSnapshot?) -> Unit {
match s {
Some(snap) => if snap.host_id != 0U { @sys.surface_release(snap.host_id) }
None => ()
}
}
///| What a frame was drawn from, compared with the next frame's to see whether it changed.
pub(all) struct FrameSig {
bg : Array[@scene.DrawOp]
panel : Array[@scene.DrawOp]
wins : Array[(UInt64, @scene.Rect, Bool, Bool, Array[@scene.DrawOp])]
popups : Array[@scene.Rect]
}
///| The background ops are usually the very same (cached) array as last frame's: that is
/// checked first, instead of comparing its ~2000 ops one by one every frame.
pub impl Eq for FrameSig with equal(a, b) {
(physical_equal(a.bg, b.bg) || a.bg == b.bg) &&
a.panel == b.panel &&
a.wins == b.wins &&
a.popups == b.popups
}
///|
fn ChandraCompositor::apply_actions(
self : ChandraCompositor,
actions : Array[@wm.WmAction],
) -> Unit {
for act in actions {
match act {
@wm.WmAction::SetGeometry(wid, r) =>
match self.windows.get(wid) {
Some(win) => {
win.geometry = @scene.Rect::new(r.x, r.y, r.w, r.h)
if self.split_drag is Some(_) || self.wm.drag is Some(_) {
// Dragging: send one configure per frame, not one per mouse event.
win.cfg_dirty = true
} else {
win.cfg_dirty = false
self.configure_window(win)
}
self.damage.mark_full()
}
None => ()
}
@wm.WmAction::Focus(wid) => {
for id, w in self.windows {
w.focused = id == wid
}
self.set_kbd_focus(Some(wid))
self.damage.mark_full()
}
@wm.WmAction::Raise(_) => self.damage.mark_full()
@wm.WmAction::Close(wid) => self.server.send_toplevel_close(wid)
@wm.WmAction::SetMaximized(wid, m) =>
match self.windows.get(wid) {
Some(win) => win.maximized = m
None => ()
}
@wm.WmAction::SetFullscreen(wid, f) =>
match self.windows.get(wid) {
Some(win) => win.fullscreen = f
None => ()
}
}
}
}
///| Send the size changes coalesced during a divider drag.
fn ChandraCompositor::flush_configures(self : ChandraCompositor) -> Unit {
for wid, win in self.windows {
ignore(wid)
if win.cfg_dirty {
win.cfg_dirty = false
self.configure_window(win)
}
}
}
///| Send a window its size and states. A client-decorated window is asked for the frame's whole
/// box (`client_rect`): its own title bar goes where Chandra's would have been.
fn ChandraCompositor::configure_window(self : ChandraCompositor, win : HostWindow) -> Unit {
let r = win.client_rect()
let tiled = match self.wm.windows.get(win.id) {
Some(rec) => !rec.is_floating
None => false
}
self.server.send_toplevel_configure(
win.id,
r.w,
r.h,
win.focused,
win.maximized,
win.fullscreen,
tiled~,
)
}
///|
pub fn ChandraCompositor::step(self : ChandraCompositor) -> Unit {
self.flush_configures()
self.kiosk_tick()
self.portal_tick()
self.auth_tick()
self.face_window_tick()
self.control_tick()
let events = self.server.poll_events()
for ev in events {
match ev {
@wayland.CompositorEvent::ToplevelOpened(wid, app_id, title, min_w, min_h) => {
let win : HostWindow = {
id: wid,
title,
app_id,
geometry: @scene.Rect::new(100, 100, 640, 480),
focused: true,
maximized: false,
fullscreen: false,
surface: None,
cfg_dirty: false,
csd: false,
geo_x: 0,
geo_y: 0,
}
self.windows.set(wid, win)
// An app whose window was last left somewhere reopens there, floating, kept on screen.
let saved = self.shell.saved_window(app_id)
let g : @wm.Rect = match saved {
Some((sx, sy, sw, sh)) => {
let w = if sw > self.output_w { self.output_w } else { sw }
let h = if sh > self.output_h { self.output_h } else { sh }
let x = if sx + w > self.output_w { self.output_w - w } else if sx < 0 { 0 } else { sx }
let y = if sy + h > self.output_h { self.output_h - h } else if sy < 32 { 32 } else { sy }
{ x, y, w, h }
}
None => { x: 100, y: 100, w: 640, h: 480 }
}
win.geometry = @scene.Rect::new(g.x, g.y, g.w, g.h)
let rec : @wm.WindowRecord = {
id: wid,
app_id,
title,
geometry: g,
floating_geometry: g,
min_size: { w: min_w, h: min_h },
max_size: None,
focused: true,
maximized: false,
fullscreen: false,
minimized: false,
is_floating: saved is Some(_),
workspace: 0U,
can_move: true,
can_resize: true,
can_maximize: true,
can_minimize: true,
can_fullscreen: true,
}
let actions = self.wm.open_window(rec, self.work_area)
self.apply_actions(actions)
self.claim_restored(wid, app_id)
}
@wayland.CompositorEvent::ToplevelUpdated(wid, app_id, title) =>
match self.windows.get(wid) {
Some(win) => {
if app_id.length() > 0 {
win.app_id = app_id
self.claim_restored(wid, app_id)
}
if title.length() > 0 {
win.title = title
}
self.damage.mark_full()
}
None => ()
}
@wayland.CompositorEvent::ToplevelCommitted(wid, snap) =>
match self.windows.get(wid) {
Some(win) => {
match snap {
Some(s) =>
if self.shot is Some(_) {
// The screen is frozen under the screenshot overlay: show it afterwards.
release_snapshot(self.shot_frozen.get(wid))
self.shot_frozen.set(wid, s)
} else {
release_snapshot(win.surface)
win.surface = Some(s)
self.thumb_stale(wid)
self.damage.mark_rect(win.geometry)
}
None => ()
}
}
None => ()
}
@wayland.CompositorEvent::ToplevelSizeChosen(wid, w, h) =>
// The client keeps its own size after being asked twice. A floating window is the
// client's to size: the frame follows it instead of padding the buffer out. A tiled,
// maximized or fullscreen window keeps the compositor's size.
match (self.wm.windows.get(wid), self.windows.get(wid)) {
(Some(r), Some(win)) =>
if r.is_floating && !r.maximized && !r.fullscreen && self.wm.drag is None && self.split_drag is None {
// A client-decorated window's size includes the room of Chandra's frame.
let (w, h) = if win.csd { (w - 2 * frame_side, h - frame_top - frame_side) } else { (w, h) }
self.apply_actions(self.wm.handle_request(wid, @wm.WindowRequest::ResizeTo(w, h), self.work_area))
}
_ => ()
}
@wayland.CompositorEvent::ToplevelFrame(wid, csd, gx, gy) =>
match self.windows.get(wid) {
Some(win) => {
let changed = win.csd != csd
win.csd = csd
win.geo_x = gx
win.geo_y = gy
if changed {
// The client's box just grew or shrank by the frame: ask for the new size.
self.configure_window(win)
}
self.damage.mark_full()
}
None => ()
}
@wayland.CompositorEvent::ToplevelResizeRequest(wid, edges) => self.client_resize(wid, edges)
@wayland.CompositorEvent::PopupOpened(wid, parent, rx, ry, w, h) => {
// Keyboard focus stays on the parent until the popup grabs and maps: moving it away
// early makes clients (Chrome, Qt) take the keyboard leave as "dismiss the menu".
self.popups.push({ id: wid, parent, rx, ry, w, h, surface: None, grab: false, geo_x: 0, geo_y: 0 })
}
@wayland.CompositorEvent::PopupGeometry(wid, gx, gy) =>
for p in self.popups {
if p.id == wid {
p.geo_x = gx
p.geo_y = gy
self.damage.mark_full()
}
}
@wayland.CompositorEvent::PopupMoved(wid, rx, ry, w, h) =>
for p in self.popups {
if p.id == wid {
p.rx = rx
p.ry = ry
p.w = w
p.h = h
self.damage.mark_full()
}
}
@wayland.CompositorEvent::PopupGrabbed(wid) =>
for p in self.popups {
if p.id == wid {
p.grab = true
if p.surface is Some(_) {
self.set_kbd_focus(Some(wid))
}
}
}
@wayland.CompositorEvent::PopupCommitted(wid, snap) =>
for p in self.popups {
if p.id == wid {
match snap {
Some(_) => {
let first_map = p.surface is None
release_snapshot(p.surface)
p.surface = snap
if first_map && p.grab {
self.set_kbd_focus(Some(wid))
}
}
None => ()
}
self.damage.mark_full()
}
}
@wayland.CompositorEvent::PopupClosed(wid) => self.close_popup(wid)
@wayland.CompositorEvent::ToplevelClosed(wid) => {
self.close_popup(wid)
match self.windows.get(wid) {
Some(win) => release_snapshot(win.surface)
None => ()
}
self.thumb_forget(wid)
ignore(self.windows.remove(wid))
release_snapshot(self.shot_frozen.get(wid))
self.shot_frozen.remove(wid)
ignore(self.restore_claimed.remove(wid))
if self.ptr_win == Some(wid) {
self.ptr_win = None
}
if self.kbd_win == Some(wid) {
self.kbd_win = None
}
let actions = self.wm.close_window(wid, self.work_area)
self.apply_actions(actions)
}
@wayland.CompositorEvent::ToplevelMaximizeRequest(wid, m) => {
let actions = self.wm.handle_request(
wid,
@wm.WindowRequest::Maximize(m),
self.work_area,
)
self.apply_actions(actions)
}
@wayland.CompositorEvent::ToplevelFullscreenRequest(wid, f) => {
let actions = self.wm.handle_request(
wid,
@wm.WindowRequest::Fullscreen(f),
self.work_area,
)
self.apply_actions(actions)
}
@wayland.CompositorEvent::ToplevelMinimizeRequest(wid, m) => {
let actions = self.wm.handle_request(
wid,
@wm.WindowRequest::Minimize(m),
self.work_area,
)
self.apply_actions(actions)
}
@wayland.CompositorEvent::ToplevelMoveRequest(wid) => self.client_move(wid)
}
}
self.session_tick()
self.screenshot_tick()
self.recording_tick()
}
///| True while something runs on a timer rather than on input or client messages (a recording,
/// a pending screenshot, a toast counting down): the main loop keeps its frame pace then instead
/// of sleeping until an fd wakes it.
pub fn ChandraCompositor::has_timed_work(self : ChandraCompositor) -> Bool {
self.rec is Some(_) ||
self.shot_request is Some(_) ||
self.rec_request is Some(_) ||
self.shot_toast is Some(_) ||
self.auth_animating() ||
self.thumbs_pending() ||
self.overview_animating() ||
self.shell.animating() ||
self.shell.face_busy() ||
self.kiosk_waiting()
}
///| How long an idle main loop may sleep: `idle_ms` (until the wall clock's next second), or
/// less when a desktop module's next animation frame is due sooner.
pub fn ChandraCompositor::wake_timeout(self : ChandraCompositor, idle_ms : Int) -> Int {
let d = self.shell.desktop_wake_ms(@sys.monotonic_ms())
if d >= 0 && d < idle_ms {
d
} else {
idle_ms
}
}
///| True when a window covers the whole work area (or the output), give or take the tiling gaps:
/// the desktop can't be seen.
fn ChandraCompositor::desktop_covered(self : ChandraCompositor) -> Bool {
let m = 16
let wa = self.work_area
let wa : @wm.Rect = { x: wa.x + m, y: wa.y + m, w: wa.w - 2 * m, h: wa.h - 2 * m }
for wid in self.wm.stacking_order() {
match self.windows.get(wid) {
Some(win) => {
let g = win.geometry
if g.x <= wa.x && g.y <= wa.y && g.x + g.w >= wa.x + wa.w && g.y + g.h >= wa.y + wa.h {
return true
}
}
None => ()
}
}
false
}
///|
pub fn ChandraCompositor::set_layout_mode(
self : ChandraCompositor,
mode : @wm.LayoutMode,
) -> Unit {
self.layout_mode = mode
let actions = self.wm.set_layout_mode(mode, self.work_area)
self.apply_actions(actions)
// Remember it (a no-op unless persistence is on).
self.shell.layout_sel = match mode {
@wm.LayoutMode::Tiling => 0
@wm.LayoutMode::Monocle => 1
@wm.LayoutMode::Floating => 2
}
self.shell.save_state()
}
///| Restore what the last session left (layout mode, recent apps) from state.edn. Call once,
/// after turning `shell.persist` on; a no-op without persistence.
pub fn ChandraCompositor::restore_state(self : ChandraCompositor) -> Unit {
let st = self.shell.load_state()
self.shell.apply_state(st)
self.apply_theme()
match st.layout {
Some(0) => self.set_layout_mode(@wm.LayoutMode::Tiling)
Some(1) => self.set_layout_mode(@wm.LayoutMode::Monocle)
Some(2) => self.set_layout_mode(@wm.LayoutMode::Floating)
_ => ()
}
}
///|
fn ChandraCompositor::build_scene(
self : ChandraCompositor,
) -> (@scene.SceneGraph, Bool) {
self.flush_configures()
self.overview_sync()
let scene = @scene.SceneGraph::new(self.output_w, self.output_h)
let order = self.wm.stacking_order()
// The overview draws the windows itself, as previews.
let shown = if self.overview is Some(_) { [] } else { order }
for wid in shown {
match self.windows.get(wid) {
Some(win) if win.csd => {
// The client draws its own frame and shadow: no Chandra frame (`fullscreen` in the
// scene means "undecorated"), and the whole buffer is drawn, shadow included.
let (ox, oy) = win.surface_origin()
let r = win.client_rect()
let (bw, bh) = match win.surface {
Some(s) => (s.width, s.height)
None => (r.w, r.h)
}
scene.windows.push({
id: win.id,
title: win.title,
app_id: win.app_id,
focused: win.focused,
fullscreen: true,
geometry: @scene.Rect::new(ox, oy, bw, bh),
deco_insets: (0, 0, 0, 0),
deco_ops: [],
surface: win.surface,
})
}
Some(win) => {
let (insets, deco_ops) = @scene.build_breeze_decoration(
win.title,
win.app_id,
win.focused,
win.fullscreen,
win.geometry.w + 4,
win.geometry.h + 34,
self.theme,
)
scene.windows.push({
id: win.id,
title: win.title,
app_id: win.app_id,
focused: win.focused,
fullscreen: win.fullscreen,
geometry: win.geometry,
deco_insets: insets,
deco_ops,
surface: win.surface,
})
}
None => ()
}
}
// Client menus would draw above the auth dialog: none while it is up.
for p in (if self.auth is Some(_) { [] } else { self.popups }) {
match p.surface {
Some(snap) => if self.overview is None { scene.popups.push((self.popup_rect(p), snap)) }
None => ()
}
}
self.shell.layout_sel = match self.layout_mode {
@wm.LayoutMode::Tiling => 0
@wm.LayoutMode::Monocle => 1
@wm.LayoutMode::Floating => 2
}
// The task list shows every window, minimized ones too (so they can be restored).
let wins : Array[@shell.WinInfo] = []
for wid in order {
match self.windows.get(wid) {
Some(win) =>
wins.push({
id: win.id,
title: if win.title.length() > 0 { win.title } else { win.app_id },
focused: win.focused,
minimized: false,
maximized: win.maximized,
})
None => ()
}
}
for id, r in self.wm.windows {
if r.minimized {
match self.windows.get(id) {
Some(win) =>
wins.push({
id,
title: if win.title.length() > 0 { win.title } else { win.app_id },
focused: false,
minimized: true,
maximized: win.maximized,
})
None => ()
}
}
}
self.shell.windows = wins
// The window list the `windows` interface serves to wasm widgets (task-bar order).
let entries : Array[@wasm.WindowEntry] = []
for w in wins {
match self.windows.get(w.id) {
Some(win) => entries.push({ id: w.id, title: w.title, app_id: win.app_id, focused: w.focused })
None => ()
}
}
self.shell.win_list = entries
self.shell.screenshot_state = (if self.rec is Some(_) { 1 } else { 0 }) |
(if self.rec_pointer { 2 } else { 0 })
// A window over the whole desktop (maximized, fullscreen, monocle) pauses a desktop module's
// animation: nobody can see it.
self.shell.set_desktop_covered(self.desktop_covered())
let t0 = @sys.monotonic_ms()
let (bg_ops, shell_ops) = self.shell.render(self.output_w, self.output_h, self.theme)
// The overview goes under the panel and the launcher.
self.overview_sync()
let panel_ops = self.overview_ops()
panel_ops.append(shell_ops)
if self.switcher_on {
panel_ops.append(self.switcher_ops())
}
panel_ops.append(self.screenshot_ops())
panel_ops.append(self.face_window_ops())
panel_ops.append(self.auth_ops())
self.frames_built = self.frames_built + 1
self.run_shell_actions()
// The wallpaper (the first ops) is cached by the renderer; the desktop card after it changes.
let n_wall = if self.shell.wallpaper_len <= bg_ops.length() { self.shell.wallpaper_len } else { 0 }
scene.wallpaper_ops = Array::makei(n_wall, i => bg_ops[i])
scene.background_ops = Array::makei(bg_ops.length() - n_wall, i => bg_ops[n_wall + i])
scene.panel_ops = panel_ops
let damaged = self.damage.take_damage(self.output_w, self.output_h).length() > 0
let sig : FrameSig = {
bg: bg_ops,
panel: panel_ops,
wins: scene.windows.map(w => (w.id, w.geometry, w.focused, w.fullscreen, w.deco_ops)),
popups: scene.popups.map(p => p.0),
}
// Unchanged since the last scene (no client commit, same shell and decoration ops)?
let unchanged = match self.last_sig {
Some(ps) => !damaged && ps == sig
None => false
}
if !unchanged {
self.last_sig = Some(sig)
}
self.prof_shell_ms = self.prof_shell_ms + (@sys.monotonic_ms() - t0)
(scene, !unchanged)
}
///| Compose the desktop for the host rasteriser (the wasm compositor): the scene to draw, or None
/// when nothing changed since the last one, so an idle desktop costs nothing to draw or present.
pub fn ChandraCompositor::compose_scene(
self : ChandraCompositor,
) -> @scene.SceneGraph? {
let (scene, changed) = self.build_scene()
self.frame_changed = changed
if changed { Some(scene) } else { None }
}
///| Compose the desktop and draw it with the software renderer (the native build and the tests).
/// An unchanged desktop hands back the previous pixels instead of rasterising them again.
pub fn ChandraCompositor::compose_frame(
self : ChandraCompositor,
) -> @renderer.Pixmap {
let (scene, changed) = self.build_scene()
match self.last_frame {
Some(prev) =>
if !changed {
self.frame_changed = false
return prev
}
None => ()
}
self.frame_changed = true
let t1 = @sys.monotonic_ms()
let px = @renderer.render_scene(scene)
self.last_frame = Some(px)
self.prof_raster_ms = self.prof_raster_ms + (@sys.monotonic_ms() - t1)
px
}
///|
/// Compose the current desktop GUI frame and present it onto a wasi-gfx
/// `SurfaceFrameBufferContext`.
pub fn ChandraCompositor::present_to_gfx(
self : ChandraCompositor,
ctx : @gfx.SurfaceFrameBufferContext,
) -> @renderer.Pixmap {
let pixmap = self.compose_frame()
if self.frame_changed {
@gfx.present_pixmap(ctx, pixmap)
}
pixmap
}
///|
/// Process pending wasi-gfx surface input events (pointer clicks for focus).
pub fn ChandraCompositor::handle_gfx_events(
self : ChandraCompositor,
surface : @gfx.Surface,
) -> Unit {
let downs = surface.drain_pointer_down()
for ev in downs {
let px = ev.x.to_int()
let py = ev.y.to_int()
let order = self.wm.stacking_order()
// Top-most window hit-test (iterate reverse Z-order).
let mut i = order.length() - 1
while i >= 0 {
let wid = order[i]
match self.windows.get(wid) {
Some(win) =>
if px >= win.geometry.x &&
px < win.geometry.x + win.geometry.w &&
py >= win.geometry.y - 32 &&
py < win.geometry.y + win.geometry.h {
self.apply_actions(
self.wm.handle_request(wid, @wm.WindowRequest::Activate, self.work_area),
)
break
}
None => ()
}
i = i - 1
}
}
}
///| Content origin of a window or popup (a popup's window geometry, which child popups'
/// positioners are relative to).
fn ChandraCompositor::origin_of(self : ChandraCompositor, wid : UInt64) -> (Int, Int) {
match self.windows.get(wid) {
Some(win) => {
// Popups are placed relative to the window geometry (past a client-drawn shadow).
let r = win.client_rect()
(r.x, r.y)
}
None => {
for p in self.popups {
if p.id == wid {
let r = self.popup_rect(p)
return (r.x + p.geo_x, r.y + p.geo_y)
}
}
(0, 0)
}
}
}
///| Absolute rectangle of a popup's buffer. Its visible part (the window geometry the
/// positioner placed, `p.w` x `p.h`) is nudged to stay on screen as a safety net; the server
/// already keeps adjustable popups inside the output, and then no nudge is needed, so the
/// client's idea of where its menu is matches what is drawn.
fn ChandraCompositor::popup_rect(self : ChandraCompositor, p : HostPopup) -> @scene.Rect {
let (w, h) = match p.surface {
Some(s) => (s.width, s.height)
None => (p.w, p.h)
}
let (bx, by) = self.origin_of_parent(p)
let vx = bx + p.rx
let vy = by + p.ry
let vx = if vx + p.w > self.output_w { self.output_w - p.w } else { vx }
let vy = if vy + p.h > self.output_h { self.output_h - p.h } else { vy }
let vx = if vx < 0 { 0 } else { vx }
let vy = if vy < 0 { 0 } else { vy }
@scene.Rect::new(vx - p.geo_x, vy - p.geo_y, w, h)
}
///|
fn ChandraCompositor::origin_of_parent(self : ChandraCompositor, p : HostPopup) -> (Int, Int) {
if p.parent == p.id {
(0, 0)
} else {
self.origin_of(p.parent)
}
}
///| Top-most popup under (x, y).
fn ChandraCompositor::popup_at(self : ChandraCompositor, x : Int, y : Int) -> HostPopup? {
let mut i = self.popups.length() - 1
while i >= 0 {
let p = self.popups[i]
let r = self.popup_rect(p)
if p.surface is Some(_) && x >= r.x && x < r.x + r.w && y >= r.y && y < r.y + r.h {
return Some(p)
}
i = i - 1
}
None
}
///| The toplevel a popup chain hangs off (for returning keyboard focus).
fn ChandraCompositor::popup_root(self : ChandraCompositor, wid : UInt64) -> UInt64? {
let mut cur = wid
let mut n = 0
while n < 16 {
if self.windows.contains(cur) {
return Some(cur)
}
let mut next : UInt64? = None
for p in self.popups {
if p.id == cur {
next = Some(p.parent)
}
}
match next {
Some(x) => cur = x
None => return None
}
n = n + 1
}
None
}
///| Forget a popup (and any popups hanging off it); give keyboard focus back to its window.
fn ChandraCompositor::close_popup(self : ChandraCompositor, wid : UInt64) -> Unit {
let mut found = false
for p in self.popups {
if p.id == wid {
found = true
}
}
if !found {
return
}
let root = self.popup_root(wid)
for p in self.popups {
if p.id == wid {
release_snapshot(p.surface)
}
}
let keep = self.popups.filter(p => p.id != wid)
self.popups.clear()
for p in keep {
self.popups.push(p)
}
self.damage.mark_full()
if self.ptr_win == Some(wid) {
self.ptr_win = None
}
if self.kbd_win == Some(wid) {
self.kbd_win = None
match self.popups.last() {
Some(top) => self.set_kbd_focus(Some(top.id))
None => self.set_kbd_focus(root)
}
}
}
///| Send pointer enter/motion for a popup under the pointer.
fn ChandraCompositor::pointer_into_popup(self : ChandraCompositor, p : HostPopup, time_ms : UInt, motion : Bool) -> Unit {
let r = self.popup_rect(p)
let lx = self.ptr_x - r.x.to_double()
let ly = self.ptr_y - r.y.to_double()
if self.ptr_win != Some(p.id) {
self.leave_pointer()
self.ptr_win = Some(p.id)
self.server.pointer_enter(p.id, lx, ly)
}
if motion {
self.server.pointer_motion(p.id, time_ms, lx, ly)
}
}
///| Move keyboard focus (wl_keyboard leave/enter + current modifiers).
fn ChandraCompositor::set_kbd_focus(self : ChandraCompositor, wid : UInt64?) -> Unit {
if self.kbd_win == wid {
return
}
match self.kbd_win {
Some(old) => self.server.keyboard_leave(old)
None => ()
}
self.kbd_win = wid
match wid {
Some(w) => {
self.shell.desktop_unfocus() // a window has the keyboard now, not the desktop
self.server.keyboard_enter(w)
self.server.keyboard_modifiers(w, self.mods, 0U, 0U, 0U)
}
None => ()
}
}
///| Top-most window under (x, y): (id, in client area). The titlebar counts as the window but not the client.
fn ChandraCompositor::window_at(self : ChandraCompositor, x : Int, y : Int) -> (UInt64, Bool)? {
let order = self.wm.stacking_order()
let mut i = order.length() - 1
while i >= 0 {
match self.windows.get(order[i]) {
Some(win) => {
let g = win.geometry
if win.csd {
// A client-decorated window is all client: its title bar is its own.
let r = win.client_rect()
if x >= r.x && x < r.x + r.w && y >= r.y && y < r.y + r.h {
return Some((win.id, true))
}
i = i - 1
continue
}
if x >= g.x && x < g.x + g.w && y >= g.y && y < g.y + g.h {
return Some((win.id, true))
}
if x >= g.x - 2 && x < g.x + g.w + 2 && y >= g.y - 32 && y < g.y {
return Some((win.id, false))
}
}
None => ()
}
i = i - 1
}
None
}
///| How far a client-decorated window's resize zones reach out past its visible edge, at most:
/// GTK puts its resize handles in its shadow (12px, `RESIZE_HANDLE_SIZE` in gtkwindow.c).
let csd_grip_out : Int = 12
///| ... and at least, for a client with a thin or no shadow.
let csd_grip_min : Int = 6
///| ... and how far in over the client's own pixels (its header bar and borders).
let csd_grip_in : Int = 3
///| Corner zones: this far along each edge from a corner.
let csd_grip_corner : Int = 16
///| The resize zone a client-decorated window has at (x, y), if any. GTK (Gajim, GNOME apps)
/// draws no border at all, only a shadow, and only resizes from a press in that shadow, which
/// Chandra doesn't hand to the client: the shadow (up to 12px of it) plus a few pixels of the
/// window's own edge resize it from Chandra instead, on all four sides and corners.
fn csd_resize_zone(win : HostWindow, x : Int, y : Int) -> @wm.DragMode? {
let c = win.client_rect()
// The shadow the client draws on each side (buffer pixels around its window geometry).
let (bw, bh) = match win.surface {
Some(s) => (s.width, s.height)
None => (c.w, c.h)
}
let reach = fn(shadow : Int) {
if shadow > csd_grip_out {
csd_grip_out
} else if shadow < csd_grip_min {
csd_grip_min
} else {
shadow
}
}
let ol = reach(win.geo_x)
let ot = reach(win.geo_y)
let or_ = reach(bw - win.geo_x - c.w)
let ob = reach(bh - win.geo_y - c.h)
let l = c.x
let t = c.y
let r = c.x + c.w
let b = c.y + c.h
if x < l - ol || x >= r + or_ || y < t - ot || y >= b + ob {
return None
}
let on_l = x < l + csd_grip_in
let on_r = x >= r - csd_grip_in
let on_t = y < t + csd_grip_in
let on_b = y >= b - csd_grip_in
let near_l = x < l + csd_grip_corner
let near_r = x >= r - csd_grip_corner
let near_t = y < t + csd_grip_corner
let near_b = y >= b - csd_grip_corner
if (on_t && near_l) || (on_l && near_t) {
Some(@wm.DragMode::ResizeNW)
} else if (on_t && near_r) || (on_r && near_t) {
Some(@wm.DragMode::ResizeNE)
} else if (on_b && near_l) || (on_l && near_b) {
Some(@wm.DragMode::ResizeSW)
} else if (on_b && near_r) || (on_r && near_b) {
Some(@wm.DragMode::ResizeSE)
} else if on_t {
Some(@wm.DragMode::ResizeN)
} else if on_b {
Some(@wm.DragMode::ResizeS)
} else if on_l {
Some(@wm.DragMode::ResizeW)
} else if on_r {
Some(@wm.DragMode::ResizeE)
} else {
None
}
}
///| Resize zone under (x, y) on a floating window: its corners (12px) and left/right/bottom
/// edges. The first window whose outer box contains the point decides; tiled windows resize
/// through the dividers instead.
fn ChandraCompositor::resize_at(
self : ChandraCompositor,
x : Int,
y : Int,
) -> (UInt64, @wm.DragMode)? {
let order = self.wm.stacking_order()
let mut i = order.length() - 1
while i >= 0 {
let id = order[i]
match (self.windows.get(id), self.wm.windows.get(id)) {
(Some(win), Some(rec)) if win.csd && !win.fullscreen => {
let floating = rec.is_floating && !rec.maximized
match csd_resize_zone(win, x, y) {
Some(mode) if floating => return Some((id, mode))
_ => {
let c = win.client_rect()
if x >= c.x && x < c.x + c.w && y >= c.y && y < c.y + c.h {
return None // over the window itself: windows below don't count
}
}
}
}
(Some(win), Some(rec)) => {
let g = win.geometry
let l = g.x
let r = g.x + g.w
let t = g.y - 32
let b = g.y + g.h
if x >= l - 6 && x < r + 6 && y >= t - 6 && y < b + 6 {
if !rec.is_floating || rec.maximized || rec.fullscreen {
return None
}
let nl = x < l + 12
let nr = x >= r - 12
let in_l = x < l + 2
let in_r = x >= r - 2
let in_b = y >= b - 2
let in_t = y < t + 4
if in_t && nl {
return Some((id, @wm.DragMode::ResizeNW))
}
if in_t && nr {
return Some((id, @wm.DragMode::ResizeNE))
}
if in_b && nl {
return Some((id, @wm.DragMode::ResizeSW))
}
if in_b && nr {
return Some((id, @wm.DragMode::ResizeSE))
}
if in_b {
return Some((id, @wm.DragMode::ResizeS))
}
if in_l {
return Some((id, if y >= b - 12 { @wm.DragMode::ResizeSW } else { @wm.DragMode::ResizeW }))
}
if in_r {
return Some((id, if y >= b - 12 { @wm.DragMode::ResizeSE } else { @wm.DragMode::ResizeE }))
}
return None
}
}
_ => ()
}
i = i - 1
}
None
}
///| Title-bar button under (x, y) for window `wid`: 1 minimize, 2 maximize, 3 close, 0 none.
/// Mirrors the layout in `build_breeze_decoration` (18px buttons, 8px apart, 10px from the right edge).
fn ChandraCompositor::title_button_at(
self : ChandraCompositor,
wid : UInt64,
x : Int,
y : Int,
) -> Int {
match self.windows.get(wid) {
None => 0
Some(win) => {
let g = win.geometry
let outer_w = g.w + 4
let lx = x - (g.x - 2)
let ly = y - (g.y - 32)
let size = 18
let by = (32 - size) / 2
if ly < by - 3 || ly >= by + size + 3 {
return 0
}
let close_x = outer_w - 10 - size
let max_x = close_x - 8 - size
let min_x = max_x - 8 - size
if lx >= close_x - 3 && lx < close_x + size + 3 {
3
} else if lx >= max_x - 3 && lx < max_x + size + 3 {
2
} else if lx >= min_x - 3 && lx < min_x + size + 3 {
1
} else {
0
}
}
}
}
///| Activate the title-bar button `btn` of window `wid`.
fn ChandraCompositor::press_title_button(
self : ChandraCompositor,
wid : UInt64,
btn : Int,
) -> Unit {
self.leave_pointer()
let req = match btn {
1 => @wm.WindowRequest::Minimize(true)
2 => {
let maxed = match self.wm.windows.get(wid) {
Some(r) => r.maximized
None => false
}
@wm.WindowRequest::Maximize(!maxed)
}
_ => @wm.WindowRequest::Close
}
self.apply_actions(self.wm.handle_request(wid, req, self.work_area))
if btn == 1 {
// Hand focus to the top-most remaining window.
let order = self.wm.stacking_order()
if order.length() > 0 {
let top = order[order.length() - 1]
self.activate_window(top)
} else {
self.set_kbd_focus(None)
}
}
self.damage.mark_full()
}
///| Focus a window and bring it to the front through the window manager, so its stacking
/// order (not just the drawing) changes.
fn ChandraCompositor::activate_window(self : ChandraCompositor, wid : UInt64) -> Unit {
self.apply_actions(
self.wm.handle_request(wid, @wm.WindowRequest::Activate, self.work_area),
)
}
///| Restore a minimized window (or focus a visible one) from its task-bar pill.
fn ChandraCompositor::activate_task(self : ChandraCompositor, wid : UInt64) -> Unit {
let minimized = match self.wm.windows.get(wid) {
Some(r) => r.minimized
None => return
}
if minimized {
self.apply_actions(
self.wm.handle_request(wid, @wm.WindowRequest::Minimize(false), self.work_area),
)
}
self.apply_actions(
self.wm.handle_request(wid, @wm.WindowRequest::Activate, self.work_area),
)
}
///| A row of a task-bar pill's right-click menu: close (the app may still ask to save), minimize,
/// restore, or maximize / restore the size of window `wid`.
fn ChandraCompositor::window_command(
self : ChandraCompositor,
wid : UInt64,
cmd : @shell.WindowCommand,
) -> Unit {
if self.wm.windows.get(wid) is None {
return
}
match cmd {
Close => self.press_title_button(wid, 3)
Minimize => self.press_title_button(wid, 1)
Restore => self.activate_task(wid)
ToggleMaximize => {
// A minimized window comes back first, then (un)maximizes in front.
self.activate_task(wid)
self.press_title_button(wid, 2)
}
}
self.damage.mark_full()
}
///| Use the dark (configured) or the light theme, as the shell's Dark mode setting says, for the
/// shell, the panel modules, the window decorations and the switcher; redraws everything, and
/// (in the login session) tells apps through the system preference.
pub fn ChandraCompositor::apply_theme(self : ChandraCompositor) -> Unit {
let t = if self.shell.dark_mode { self.dark_theme } else { @scene.ThemeTokens::moon_day() }
self.theme = t
self.shell.set_output(t, self.output_w, self.output_h)
self.damage.mark_full()
self.sync_system_theme_pref(self.shell.dark_mode)
}
///| Carry out what panel modules asked for: focus a window, switch layout.
fn ChandraCompositor::run_shell_actions(self : ChandraCompositor) -> Unit {
for a in self.shell.take_actions() {
if self.kiosk_refuses(a) {
continue
}
match a {
@shell.ShellAction::FocusWindow(wid) => self.activate_task(wid)
@shell.ShellAction::Logout => self.quit_requested = true
@shell.ShellAction::ThemeChanged => self.apply_theme()
@shell.ShellAction::AuthAgentChanged => self.auth_agent_setting_changed()
@shell.ShellAction::Screenshot(code) => self.screenshot_request(code)
@shell.ShellAction::SystemPower(a) => self.system_power(a)
@shell.ShellAction::Window(wid, cmd) => self.window_command(wid, cmd)
@shell.ShellAction::ModulesReloaded => {
// The panel changed under the frame signature's feet (as after `chandra-ctl reload`).
self.frame_changed = true
self.damage.mark_full()
}
@shell.ShellAction::SetLayout(m) =>
match m {
0 => self.set_layout_mode(@wm.LayoutMode::Tiling)
1 => self.set_layout_mode(@wm.LayoutMode::Monocle)
2 => self.set_layout_mode(@wm.LayoutMode::Floating)
_ => ()
}
}
}
}
///| Tiling divider under (x, y), with a few pixels of slack either side.
fn ChandraCompositor::handle_at(
self : ChandraCompositor,
x : Int,
y : Int,
) -> @wm.SplitHandle? {
let slop = 3
for h in self.wm.split_handles(self.work_area) {
let r = h.rect
// Title bars sit 32px above the windows, so the hit area reaches up over them.
let hit = if h.horizontal {
x >= r.x - slop && x < r.x + r.w + slop && y >= r.y - 32 && y < r.y + r.h
} else {
y >= r.y - slop && y < r.y + r.h + slop && x >= r.x && x < r.x + r.w
}
if hit {
return Some(h)
}
}
None
}
///|
fn ChandraCompositor::leave_pointer(self : ChandraCompositor) -> Unit {
match self.ptr_win {
Some(w) => {
self.server.pointer_leave(w)
self.ptr_win = None
}
None => ()
}
}
///| evdev keycode -> the key name lib/launcher understands (US layout).
fn key_name(code : UInt, shift : Bool) -> String {
let rows = [
(16U, "qwertyuiop"),
(30U, "asdfghjkl"),
(44U, "zxcvbnm"),
(2U, "1234567890"),
]
for r in rows {
let (first, chars) = r
if code >= first && code < first + chars.length().reinterpret_as_uint() {
let c = Int::unsafe_to_char(chars[(code - first).reinterpret_as_int()].to_int())
return if shift && c >= 'a' && c <= 'z' {
Int::unsafe_to_char(c.to_int() - 32).to_string()
} else {
c.to_string()
}
}
}
match code {
1U => "Escape"
14U => "Backspace"
28U => "Enter"
57U => " "
12U => "-"
13U => if shift { "+" } else { "=" }
15U => "Tab"
103U => "ArrowUp"
108U => "ArrowDown"
105U => "ArrowLeft"
106U => "ArrowRight"
52U => "."
53U => "/"
_ => ""
}
}
///| Route host input: shell (launcher) first, then the window under the pointer
/// / the keyboard-focused window.
pub fn ChandraCompositor::handle_input(
self : ChandraCompositor,
ev : @nested.InputEvent,
time_ms : UInt,
) -> Unit {
self.note_activity(@sys.monotonic_ms())
match ev {
Modifiers(dep, lat, lock, group) => {
self.mods_latched = lat
self.mods_locked = lock
self.mods_group = group
if self.auth is Some(_) {
self.mods = dep
}
}
_ => ()
}
// The authentication dialog takes all input while it is up.
if self.auth_input(ev) {
return
}
// So does the Face unlock window (below the password dialog it opens on Apply).
if self.face_window_input(ev) {
return
}
match ev {
Key(code, pressed) => {
self.track_modifier_key(code, pressed)
if self.capture_key(code, pressed) {
return
}
}
_ => ()
}
if self.screenshot_input(ev, time_ms) {
return
}
match ev {
PointerMove(x, y) => {
self.ptr_x = x
self.ptr_y = y
let xi = x.to_int()
let yi = y.to_int()
match self.popup_at(xi, yi) {
Some(p) => {
self.pointer_into_popup(p, time_ms, true)
return
}
None => ()
}
if self.shell.inspecting() {
self.leave_pointer()
let id = match self.shell.component_at(xi, yi) {
"" =>
match self.window_at(xi, yi) {
Some((_, true)) => "window-surface"
Some((_, false)) => "window-decoration"
None => if self.shell.desk_hit(xi, yi) { "desktop-widget" } else { "desktop" }
}
sid => sid
}
self.shell.set_hover(id, xi, yi)
return
}
if self.overview_motion(xi, yi) {
return
}
if self.title_bar_motion(xi, yi) {
return
}
if self.wm.drag is Some(_) {
match self.wm.update_drag(xi, yi) {
Some(a) => self.apply_actions([a])
None => ()
}
return
}
if self.shell.desktop_grabbing() {
// A press on the desktop is held (an icon being dragged): the desktop module follows the
// pointer wherever it goes until the release.
self.leave_pointer()
ignore(self.shell.desktop_pointer_move(xi, yi))
return
}
if self.shell.pointer_move(xi, yi) || self.shell.hit(xi, yi) {
self.leave_pointer()
self.shell.desktop_leave()
return
}
match self.split_drag {
Some(h) => {
self.apply_actions(self.wm.drag_split(h, xi, yi, self.work_area))
return
}
None => ()
}
if self.handle_at(xi, yi) is Some(_) || self.resize_at(xi, yi) is Some(_) {
self.leave_pointer()
self.shell.desktop_leave()
return
}
match self.window_at(xi, yi) {
Some((wid, true)) => {
self.shell.desktop_leave()
match self.windows.get(wid) {
Some(win) => {
let (ox, oy) = win.surface_origin()
let lx = x - ox.to_double()
let ly = y - oy.to_double()
if self.ptr_win != Some(wid) {
self.leave_pointer()
self.ptr_win = Some(wid)
self.server.pointer_enter(wid, lx, ly)
}
self.server.pointer_motion(wid, time_ms, lx, ly)
}
None => ()
}
}
Some((_, false)) => {
self.leave_pointer()
self.shell.desktop_leave()
}
None => {
// The empty desktop: a desktop module sees the pointer here.
self.leave_pointer()
ignore(self.shell.desktop_pointer_move(xi, yi))
}
}
}
Scroll(axis, v, discrete) => {
if v == 0.0 && discrete == 0 {
return
}
let xi = self.ptr_x.to_int()
let yi = self.ptr_y.to_int()
match self.popup_at(xi, yi) {
Some(p) => {
self.pointer_into_popup(p, time_ms, false)
self.server.pointer_axis(p.id, time_ms, axis, v, discrete)
return
}
None => ()
}
if self.overview is Some(_) {
return // nothing scrolls under the overview
}
if axis == 0U && self.shell.inspecting() {
// ~10 units per wheel notch -> 3 lines.
let lines = if v > 0.0 { (v / 10.0).to_int() + 1 } else { (v / 10.0).to_int() - 1 }
ignore(self.shell.scroll_inspector(lines))
return
}
if self.wm.drag is Some(_) || self.split_drag is Some(_) {
return
}
// The wheel scrolls the window under the pointer (focused or not), like Plasma.
match self.window_at(xi, yi) {
Some((wid, true)) =>
match self.windows.get(wid) {
Some(win) => {
if self.ptr_win != Some(wid) {
self.leave_pointer()
self.ptr_win = Some(wid)
let (ox, oy) = win.surface_origin()
self.server.pointer_enter(wid, self.ptr_x - ox.to_double(), self.ptr_y - oy.to_double())
}
self.server.pointer_axis(wid, time_ms, axis, v, discrete)
}
None => ()
}
Some((_, false)) => ()
None =>
if !self.shell.hit(xi, yi) {
// The empty desktop: a desktop module gets the wheel (in pixels; a notch is ~10).
let d = if v != 0.0 { v.to_int() } else { discrete * 10 }
ignore(
self.shell.desktop_scroll(xi, yi, if axis == 1U { d } else { 0 }, if axis == 0U { d } else { 0 }),
)
}
}
}
PointerButton(button, pressed) => {
let xi = self.ptr_x.to_int()
let yi = self.ptr_y.to_int()
match self.popup_at(xi, yi) {
Some(p) => {
self.pointer_into_popup(p, time_ms, false)
if pressed {
self.press_fwd = true
self.server.pointer_button(p.id, time_ms, button, true)
} else if self.press_fwd {
self.press_fwd = false
self.server.pointer_button(p.id, time_ms, button, false)
}
return
}
None => ()
}
if pressed && self.popups.length() > 0 {
// A click outside the open popups dismisses them (the click is swallowed).
let mut i = self.popups.length() - 1
while i >= 0 {
self.server.popup_done(self.popups[i].id)
i = i - 1
}
return
}
if self.overview_button(button, pressed, xi, yi) {
return
}
if !pressed && button == 272U {
self.title_bar_release()
}
if !pressed && self.shell.pointer_up() {
self.run_shell_actions()
return
}
if !pressed && self.shell.desktop_release(xi, yi) {
self.run_shell_actions()
return
}
if !pressed && self.split_drag is Some(_) {
self.split_drag = None
return
}
if !pressed && self.wm.drag is Some(_) {
let dragged = self.wm.drag.unwrap().win_id
self.wm.end_drag()
// Remember where a floating window was left.
match (self.wm.windows.get(dragged), self.windows.get(dragged)) {
(Some(r), Some(win)) =>
if r.is_floating && !r.maximized && !r.fullscreen {
self.shell.remember_window(win.app_id, win.geometry.x, win.geometry.y, win.geometry.w, win.geometry.h)
}
_ => ()
}
self.flush_configures()
return
}
// Right button over a swappable panel widget opens its alternatives menu.
if button == 273U && pressed && !self.kiosk_strict() && self.shell.right_click(xi, yi) {
return
}
// Right button on the empty desktop: the desktop menu (module settings, system settings).
if button == 273U && pressed && !self.kiosk_strict() && self.window_at(xi, yi) is None &&
!self.shell.hit(xi, yi) && self.shell.desktop_menu(xi, yi) {
return
}
if pressed {
self.super_solo = false
if button != 273U && self.shell.pointer_down(xi, yi) {
self.run_shell_actions()
return
}
match self.window_at(xi, yi) {
Some((wid, false)) => {
let btn = self.title_button_at(wid, xi, yi)
if btn != 0 {
self.press_title_button(wid, btn)
return
}
if button == 272U && self.resize_at(xi, yi) is None {
// Drag the window by its title bar, or double-click it to maximize / restore.
self.title_bar_press(wid, xi, yi, time_ms)
return
}
}
_ => ()
}
if button == 272U {
match self.resize_at(xi, yi) {
Some((wid, mode)) => {
self.leave_pointer()
self.activate_window(wid)
ignore(self.wm.start_drag(wid, mode, xi, yi))
return
}
None => ()
}
}
match self.handle_at(xi, yi) {
Some(h) => {
self.split_drag = Some(h)
self.leave_pointer()
return
}
None => ()
}
if self.window_at(xi, yi) is None && self.shell.desk_press(xi, yi) {
self.leave_pointer()
return
}
// A left press on the empty desktop: a desktop module gets it, and the keyboard.
if button == 272U && self.window_at(xi, yi) is None && self.shell.desktop_press(xi, yi) {
self.leave_pointer()
self.set_kbd_focus(None)
self.run_shell_actions()
return
}
match self.window_at(xi, yi) {
Some((wid, _)) => {
self.activate_window(wid)
}
None => ()
}
}
match self.ptr_win {
Some(w) =>
if pressed {
self.press_fwd = true
self.server.pointer_button(w, time_ms, button, pressed)
} else if self.press_fwd {
self.press_fwd = false
self.server.pointer_button(w, time_ms, button, pressed)
}
None => ()
}
}
Key(code, pressed) =>
if self.kiosk_key(code, pressed) {
self.super_solo = false
} else if self.switcher_key(code, pressed, time_ms) {
self.super_solo = false
} else if !self.kiosk_strict() && self.shortcut_key(code, pressed) {
self.super_solo = false
} else if self.kiosk_strict() && (code == 125U || code == 126U) {
// A locked kiosk: Super opens nothing and isn't passed on either.
self.super_solo = false
} else if code == 125U || code == 126U {
// Super (left/right): a bare tap runs the shortcut bound to "Super" (the launcher by
// default); Super never reaches clients.
if pressed {
self.super_solo = true
} else if self.super_solo {
self.super_solo = false
self.super_tap()
}
} else if self.overview_key(code, pressed) {
self.super_solo = false
} else if self.shell.inspecting() {
self.super_solo = false
// Escape leaves inspect mode; other keys are not forwarded while inspecting.
if code == 1U && pressed {
self.shell.set_inspecting(false)
}
} else if self.shell.popup_open() {
self.super_solo = false
if pressed {
let name = key_name(code, (self.mods & 1U) != 0U)
if name.length() > 0 {
self.shell.key_down(name)
}
}
} else if self.kbd_win is None && self.shell.desktop_has_focus() {
// The desktop has the keyboard (it was clicked): keys go to the desktop module.
self.super_solo = false
if pressed {
let name = key_name(code, (self.mods & 1U) != 0U)
if name.length() > 0 && self.shell.desktop_key(name) {
self.run_shell_actions()
}
}
} else {
self.super_solo = false
match self.kbd_win {
Some(w) => self.server.keyboard_key(w, time_ms, code, pressed)
None => ()
}
}
Modifiers(dep, lat, lock, group) => {
self.mods = dep
match self.kbd_win {
Some(w) => self.server.keyboard_modifiers(w, dep, lat, lock, group)
None => ()
}
}
}
}
///| Cursor shape (wp_cursor_shape_v1 value) for the current pointer position: shell UI
/// decides over the launcher; a client window decides inside its client area; otherwise default.
pub fn ChandraCompositor::desired_cursor(self : ChandraCompositor) -> UInt {
match self.auth {
Some(ui) => return self.auth_cursor(ui)
None => ()
}
if self.shell.face.window is Some(_) {
return if self.face_window_hit() is Some(_) { 4U } else { 1U }
}
match self.shot {
Some(o) => return self.overlay_cursor(o)
None => ()
}
let shell_shape = self.shell.cursor_at(self.ptr_x.to_int(), self.ptr_y.to_int())
if shell_shape != 0U {
return shell_shape
}
fn mode_cursor(m : @wm.DragMode) -> UInt {
match m {
Move => 4U
ResizeE | ResizeW => 26U
ResizeN | ResizeS => 27U
ResizeNW | ResizeSE => 29U
ResizeNE | ResizeSW => 28U
}
}
match self.wm.drag {
Some(d) => return mode_cursor(d.mode)
None => ()
}
match self.resize_at(self.ptr_x.to_int(), self.ptr_y.to_int()) {
Some((_, m)) => return mode_cursor(m)
None => ()
}
let handle = match self.split_drag {
Some(h) => Some(h)
None => self.handle_at(self.ptr_x.to_int(), self.ptr_y.to_int())
}
match handle {
Some(h) => return if h.horizontal { 26U } else { 27U } // ew-resize / ns-resize
None => ()
}
let handle = match self.split_drag {
Some(h) => Some(h)
None => self.handle_at(self.ptr_x.to_int(), self.ptr_y.to_int())
}
match handle {
Some(h) => return if h.horizontal { 26U } else { 27U } // ew-resize / ns-resize
None => ()
}
if self.ptr_win is None && self.shell.desk_hit(self.ptr_x.to_int(), self.ptr_y.to_int()) &&
self.window_at(self.ptr_x.to_int(), self.ptr_y.to_int()) is None {
return 4U // the desktop card can be dragged
}
match self.ptr_win {
Some(w) => self.server.client_cursor_shape(w)
None => 1U
}
}