///| 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 /// Mapped wl_subsurfaces, bottom to top, drawn above the window (Chrome's toolbar bubbles). subs : Array[HostSub] } ///| A window's wl_subsurface: its wl_surface id, position relative to the window's surface /// origin, and last buffer. pub(all) struct HostSub { sid : UInt mut x : Int mut y : Int mut surface : @scene.ShmBufferSnapshot? } ///| The surface of window `self` under screen point (x, y): (subsurface id, or 0 for the window /// itself, and the point in that surface's coordinates). fn HostWindow::surface_at(self : HostWindow, x : Double, y : Double) -> (UInt, Double, Double) { let (ox, oy) = self.surface_origin() let mut i = self.subs.length() - 1 while i >= 0 { let sub = self.subs[i] match sub.surface { Some(snap) => { let sx = (ox + sub.x).to_double() let sy = (oy + sub.y).to_double() if x >= sx && x < sx + snap.width.to_double() && y >= sy && y < sy + snap.height.to_double() { return (sub.sid, x - sx, y - sy) } } None => () } i = i - 1 } (0U, x - ox.to_double(), y - oy.to_double()) } ///| Screen point (x, y) in the coordinates of window `self`'s surface `sub` (a subsurface's /// wl_surface id, 0 for the window itself), wherever the point is: the pointer can be held /// outside the surface during a drag. fn HostWindow::surface_local(self : HostWindow, sub : UInt, x : Double, y : Double) -> (Double, Double) { let (ox, oy) = self.surface_origin() for s in self.subs { if s.sid == sub { return (x - (ox + s.x).to_double(), y - (oy + s.y).to_double()) } } (x - ox.to_double(), y - oy.to_double()) } ///| The window style in use (Settings > Session > Flat window frames). Global because the frame's /// size is part of every window's on-screen box (`client_rect`); the compositor sets it from the /// shell's setting (`ChandraCompositor::apply_window_style`). let window_style : Ref[@scene.WindowStyle] = { val: @scene.WindowStyle::Flat } ///| Height of the server-side frame above `geometry` (title bar, outline, hairline) ... fn frame_top() -> Int { window_style.val.frame_top() } ///| ... and its border left, right and below. fn frame_side() -> Int { window_style.val.frame_side() } ///| The frame's outer box around a server-decorated window's `geometry`. fn frame_rect(g : @scene.Rect) -> @scene.Rect { let t = frame_top() let s = frame_side() @scene.Rect::new(g.x - s, g.y - t, g.w + 2 * s, g.h + t + s) } ///| 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 { frame_rect(g) } 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? /// The subsurface of `ptr_win` the pointer is over (its wl_surface id), 0 for the window itself. mut ptr_sub : UInt /// 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 /// Implicit pointer grab: a button was pressed on `ptr_win` (surface `ptr_sub`) and is still /// held, so motion, the wheel and the release all go to that surface, even outside it (text /// selection dragged past the window's edge, over the frame or the panel). `grab_buttons` /// counts the buttons held. Ends with the last release or when the compositor takes the /// pointer (`leave_pointer`: a window move, the overview, a screenshot). mut ptr_grab : Bool mut grab_buttons : Int 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 /// The title-bar button under the pointer and the one held down: (window, button), button /// 1 minimize, 2 maximize, 3 close. A press shows pressed; the release over it acts. mut deco_hover : (UInt64, Int)? mut deco_pressed : (UInt64, Int)? } ///| 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 } // A new compositor starts in the default style; the shell's setting is applied from the // first frame on (`sync_window_style`). window_style.val = @scene.WindowStyle::Flat let wm = @wm.WmState::new( inner_gap=8, outer_gap=8, titlebar_h=frame_top(), border_w=frame_side(), ) 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, deco_hover: None, deco_pressed: None, 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, ptr_sub: 0U, split_drag: None, title_press: None, press_fwd: false, ptr_grab: false, grab_buttons: 0, 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=frame_top(), // 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, subs: [], 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::SubsurfaceCommitted(wid, sid, x, y, snap) => match self.windows.get(wid) { Some(win) => { let sub = match win.subs.search_by(s => s.sid == sid) { Some(i) => win.subs[i] None => { let s : HostSub = { sid, x, y, surface: None } win.subs.push(s) s } } sub.x = x sub.y = y match snap { Some(sn) => { release_snapshot(sub.surface) sub.surface = Some(sn) } None => () } self.damage.mark_full() } None => release_snapshot(snap) } @wayland.CompositorEvent::SubsurfaceUnmapped(wid, sid) => match self.windows.get(wid) { Some(win) => match win.subs.search_by(s => s.sid == sid) { Some(i) => { release_snapshot(win.subs[i].surface) win.subs.remove(i) |> ignore if self.ptr_win == Some(wid) && self.ptr_sub == sid { // Gone from under the pointer: the next motion enters the window again. self.ptr_win = None self.ptr_sub = 0U } self.damage.mark_full() } None => () } 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) for sub in win.subs { release_snapshot(sub.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 self.ptr_sub = 0U } 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.shell.gamelib_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 now = @sys.monotonic_ms() let d = self.shell.desktop_wake_ms(now) let g = self.shell.gamelib_wake_ms(now) let d = if g >= 0 && (d < 0 || g < d) { g } else { d } 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() self.sync_window_style() 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) _ => () } } ///| ///| The scene node for window `wid`: Chandra's frame around it, or none when the client draws /// its own (or the window is fullscreen). fn ChandraCompositor::push_window_node(self : ChandraCompositor, scene : @scene.SceneGraph, wid : UInt64) -> Unit { 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) if win.fullscreen => scene.windows.push({ id: win.id, title: win.title, app_id: win.app_id, focused: win.focused, fullscreen: true, geometry: win.geometry, deco_insets: (0, 0, 0, 0), deco_ops: [], surface: win.surface, }) Some(win) => { let outer = frame_rect(win.geometry) let btn = fn(st : (UInt64, Int)?) { match st { Some((w, b)) if w == win.id => b _ => 0 } } let (insets, deco_ops) = @scene.build_window_decoration( window_style.val, { title: win.title, app_id: win.app_id, focused: win.focused, maximized: win.maximized, hover: btn(self.deco_hover), pressed: btn(self.deco_pressed), }, outer.w, outer.h, self.theme, fn(s, size, bold) { @renderer.text_width(s, size, bold, false) }, ) 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 => () } } ///| fn ChandraCompositor::build_scene( self : ChandraCompositor, ) -> (@scene.SceneGraph, Bool) { self.sync_window_style() 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 { self.push_window_node(scene, wid) // Its subsurfaces, above it (undecorated scene nodes at the subsurface's place). match self.windows.get(wid) { Some(win) if win.subs.length() > 0 => { let (ox, oy) = win.surface_origin() for sub in win.subs { match sub.surface { Some(snap) => scene.windows.push({ id: win.id, title: win.title, app_id: win.app_id, focused: win.focused, fullscreen: true, geometry: @scene.Rect::new(ox + sub.x, oy + sub.y, snap.width, snap.height), deco_insets: (0, 0, 0, 0), deco_ops: [], surface: Some(snap), }) 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) self.recording_frame_drawn() 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 - frame_top() && 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)) } let f = frame_rect(g) if x >= f.x && x < f.x + f.w && y >= f.y && 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 - frame_top() 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. /// The layout is the window style's (`@scene.WindowStyle::buttons`), as drawn. fn ChandraCompositor::title_button_at( self : ChandraCompositor, wid : UInt64, x : Int, y : Int, ) -> Int { match self.windows.get(wid) { Some(win) if !win.csd && !win.fullscreen => { let f = frame_rect(win.geometry) window_style.val.button_at(f.w, x - f.x, y - f.y) } _ => 0 } } ///| Track the title-bar button under the pointer (drawn in its hover state), redrawing when it /// changes. While a button is held, only that one shows hovered (pressed) when under the pointer. fn ChandraCompositor::title_button_hover(self : ChandraCompositor, x : Int, y : Int) -> Unit { let now = match self.window_at(x, y) { Some((wid, false)) => match self.title_button_at(wid, x, y) { 0 => None b => Some((wid, b)) } _ => None } if now != self.deco_hover { self.deco_hover = now self.damage.mark_full() } } ///| A release ends a press on a title-bar button: the button acts when the pointer is still over /// it (moving off first cancels, as in KWin). True when there was such a press. fn ChandraCompositor::release_title_button(self : ChandraCompositor, x : Int, y : Int) -> Bool { match self.deco_pressed { None => false Some((wid, btn)) => { self.deco_pressed = None self.damage.mark_full() let over = match self.window_at(x, y) { Some((w, false)) if w == wid => self.title_button_at(wid, x, y) == btn _ => false } if over { self.deco_hover = None self.press_title_button(wid, btn) } true } } } ///| 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 shell's window style (Settings > Session > Flat window frames) if it isn't the one in /// use: the frame's size changes, so the work area, the tiled layout and every client-decorated /// window's size (it fills the frame's box) follow. pub fn ChandraCompositor::sync_window_style(self : ChandraCompositor) -> Unit { let style = self.shell.window_style if style == window_style.val { return } window_style.val = style self.wm.titlebar_h = frame_top() self.wm.border_w = frame_side() self.work_area = work_area_for(self.output_w, self.output_h, self.theme, kiosk=self.kiosk_strict()) self.apply_actions(self.wm.fit_work_area(self.work_area)) for _, win in self.windows { if win.csd { self.configure_window(win) } } self.deco_hover = None self.deco_pressed = None self.last_frame = None self.last_sig = None 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::WindowStyleChanged => self.sync_window_style() @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 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 - frame_top() && 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 { self.ptr_grab = false self.grab_buttons = 0 match self.ptr_win { Some(w) => { self.server.pointer_leave(w, sub=self.ptr_sub) self.ptr_win = None self.ptr_sub = 0U } None => () } } ///| The window holding the implicit pointer grab, if one is held and the window still exists. fn ChandraCompositor::grab_window(self : ChandraCompositor) -> UInt64? { if !self.ptr_grab { return None } match self.ptr_win { Some(w) if self.windows.contains(w) => Some(w) _ => { self.ptr_grab = false self.grab_buttons = 0 None } } } ///| Pointer motion during an implicit grab: it goes to the grabbing surface, in its /// coordinates, wherever the pointer is. False when no grab is held. fn ChandraCompositor::grab_motion(self : ChandraCompositor, x : Double, y : Double, time_ms : UInt) -> Bool { match self.grab_window() { Some(wid) => { let (lx, ly) = self.windows.get(wid).unwrap().surface_local(self.ptr_sub, x, y) self.server.pointer_motion(wid, time_ms, lx, ly) true } None => false } } ///| 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.grab_motion(x, y, time_ms) { return } 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 } self.title_button_hover(xi, yi) 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 (sub, lx, ly) = win.surface_at(x, y) if self.ptr_win != Some(wid) || self.ptr_sub != sub { self.leave_pointer() self.ptr_win = Some(wid) self.ptr_sub = sub self.server.pointer_enter(wid, lx, ly, sub~) } 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 => () } match self.grab_window() { Some(w) => { self.server.pointer_axis(w, 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) => { let (sub, lx, ly) = win.surface_at(self.ptr_x, self.ptr_y) if self.ptr_win != Some(wid) || self.ptr_sub != sub { self.leave_pointer() self.ptr_win = Some(wid) self.ptr_sub = sub self.server.pointer_enter(wid, lx, ly, sub~) } 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 => () } match self.grab_window() { Some(w) => { self.server.pointer_button(w, time_ms, button, pressed) if pressed { self.grab_buttons = self.grab_buttons + 1 } else { self.grab_buttons = self.grab_buttons - 1 if self.grab_buttons <= 0 { // The last button is up: the grab ends, and the pointer goes to whatever is // under it now (the client sees it leave if that is elsewhere). self.ptr_grab = false self.grab_buttons = 0 self.press_fwd = false self.handle_input(PointerMove(self.ptr_x, self.ptr_y), time_ms) } } 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 && self.release_title_button(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 { // Shown pressed until the release, which acts if it is still over the button. self.leave_pointer() self.deco_pressed = Some((wid, btn)) self.damage.mark_full() 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.ptr_grab = true self.grab_buttons = 1 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 => () } match self.shell.face.window { Some(w) if !w.applying => 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 } }