///| Desktop modules (wit/desktop.wit, docs/desktop-modules.md): a wasm module that fills the /// desktop, the whole output behind the windows, instead of a box in the panel. /// /// What fills the desktop is one of: the wallpaper alone, the built-in clock card on it (the /// default), or one installed desktop module, chosen in Settings > Desktop (the `wallpaper` /// capability's `desktops` / `set-desktop`) and saved in state.edn as `:desktop`. The module is a /// plasmoid like any panel module (same sandbox, draw ops, capabilities and right-click settings), /// kept apart from the panel's `widgets` in `desk_mod` under the slot name "desktop". /// /// The compositor routes input here only where nothing else is: no window, panel, popup or menu /// (desktop_pointer_move / desktop_press / desktop_release / desktop_scroll), keys only while the /// desktop has the keyboard (desktop_key), and a right-click on the empty desktop opens the /// desktop menu (desktop_menu) instead of reaching the module. The module's drawing is cached and /// redrawn only when it asks (an event returning true, an animation frame, a resize or theme /// change); animation frames come from its `tick`, and the main loop sleeps until the next one is /// due (desktop_wake_ms), so a still desktop module costs nothing while idle. ///| The slot name of the desktop module (menus, reload, settings). pub let desktop_slot : String = "desktop" ///| The `:desktop` choices that aren't modules. pub let wallpaper_desktop : String = "wallpaper" ///| pub let clock_card_desktop : String = "clock-card" ///| pub let icon_desktop : String = "icon-desktop" ///| Default desktop choice for a fresh session: Icon Desktop on handsets (or /// CHANDRA_DEFAULT_DESKTOP when set), otherwise the clock card. Tests keep the clock card. pub fn default_desktop_id() -> String { if @sys.getenv("CHANDRA_TEST_CONFIG") != "" { return clock_card_desktop } let env = @sys.getenv("CHANDRA_DEFAULT_DESKTOP") if env.length() > 0 { return env } let info = @utf8.decode_lossy(@sys.read_file_bytes("/etc/machine-info")) if info.contains("CHASSIS=handset") { return icon_desktop } clock_card_desktop } ///| A module's frame request is capped at this (ms): a module can't put itself to sleep for /// hours by mistake and miss its own animation. let max_frame_delay : Int = 10000 ///| Is any loaded module (panel or desktop) granted `cap`? fn DesktopShell::any_cap(self : DesktopShell, cap : String) -> Bool { self.widgets.iter().any(w => w.has_cap(cap)) || (match self.desk_mod { Some(d) => d.has_cap(cap) None => false }) } ///| The desktop module, if one is loaded. pub fn DesktopShell::desktop_module(self : DesktopShell) -> Widget? { self.desk_mod } ///| What fills the desktop now: "wallpaper", "clock-card" or the module's id. pub fn DesktopShell::desktop_choice(self : DesktopShell) -> String { self.read_desktop_choice() self.desk_choice } // ---------------------------------------------------------------- choosing and loading ///| Read `:desktop` from state.edn once (with `persist`; else the default stays). fn DesktopShell::read_desktop_choice(self : DesktopShell) -> Unit { if self.desk_choice_read { return } self.desk_choice_read = true if !self.persist { return } match lookup_key(read_state_file(), "desktop") { Some(Text(s)) if s.length() > 0 => self.desk_choice = s Some(Keyword(s)) if s.length() > 0 => self.desk_choice = s _ => () } } ///| Write `:desktop` (keeping every other key); nothing without `persist`. The default is left out. fn DesktopShell::write_desktop_choice(self : DesktopShell) -> Unit { if !self.persist { return } let entries : Array[(@edn.Value, @edn.Value)] = [] for e in read_state_file() { match e.0 { Keyword("desktop") => () _ => entries.push(e) } } if self.desk_choice != default_desktop_id() { entries.push((@edn.Value::Keyword("desktop"), @edn.Value::Text(self.desk_choice))) } write_state_file(entries) } ///| At boot: load the saved desktop module, if that is the choice. A module that is gone or /// fails leaves the clock card (the saved choice is kept for when it is back). fn DesktopShell::boot_desktop(self : DesktopShell) -> Unit { self.read_desktop_choice() let c = self.desk_choice if c == wallpaper_desktop || c == clock_card_desktop || self.desk_mod is Some(_) { return } match self.load_desktop(c) { Ok(_) => () Err(e) => self.say(e) } } ///| The desktop modules on the module path: (id, name), in catalog order. pub fn DesktopShell::desktop_modules(self : DesktopShell) -> Array[(String, String)] { self.name_catalog() let out : Array[(String, String)] = [] for e in self.catalog { if e.desktop { out.push((e.id, e.name)) } } out } ///| The Desktops table the settings module lists: wallpaper only, the clock card, each module; /// and which of them is current. fn DesktopShell::desktop_rows(self : DesktopShell) -> (Array[String], Int) { let names = ["Wallpaper only", "Clock card"] let mods = self.desktop_modules() let choice = self.desktop_choice() let mut cur = if choice == wallpaper_desktop { 0 } else { 1 } for i, m in mods { names.push(m.1) match self.desk_mod { Some(d) if d.module_id == m.0 => cur = i + 2 _ => () } } (names, cur) } ///| fn DesktopShell::publish_desktops(self : DesktopShell) -> Unit { let (names, cur) = self.desktop_rows() let t = @wasm.Table::new() for n in names { t.str(n) t.end_row() } @wasm.publish(Desktops, t) @wasm.publish_desktop(cur) } ///| The settings module's `set-desktop`: index into the Desktops table. fn DesktopShell::set_desktop_index(self : DesktopShell, i : Int) -> Unit { let mods = self.desktop_modules() let id = match i { 0 => wallpaper_desktop 1 => clock_card_desktop _ if i - 2 < mods.length() && i >= 2 => mods[i - 2].0 _ => return } ignore(self.set_desktop(id)) } ///| Put `id` on the desktop ("wallpaper", "clock-card", or a desktop module's id or path) and /// remember it. False, with a notice, when the module can't be loaded (what was there stays). pub fn DesktopShell::set_desktop(self : DesktopShell, id : String) -> Bool { self.ensure_booted() self.read_desktop_choice() if id == wallpaper_desktop || id == clock_card_desktop { self.unload_desktop() } else { match self.load_desktop(id) { Ok(w) => self.say("Desktop: " + w.pl.name) Err(e) => { self.say(e) return false } } } self.desk_choice = id self.write_desktop_choice() self.bg_key = None self.publish_desktops() true } ///| Load desktop module `id` (catalog id or path) in place of the current one. fn DesktopShell::load_desktop(self : DesktopShell, id : String) -> Result[Widget, String] { let path = match self.find_module(id) { Some(c) => c.path None => if id.contains("/") { id } else { return Err("Desktop module '" + id + "' not found") } } match @wasm.load(path, self.tokens) { Err(e) => Err("Can't load desktop module: " + e) Ok(pl) => { let backdrop = pl.desktop_backdrop() if backdrop < 0 { pl.unload() return Err(pl.name + " is a panel module, not a desktop module") } let known = match self.find_module(stem(path)) { Some(c) => c.path == path None => false } let w : Widget = { slot: desktop_slot, module_id: if known { stem(path) } else { path }, path, pl, caps: pl.imports(), rect: { x: 0, y: 0, w: self.screen_w, h: self.screen_h }, popup_rect: { x: 0, y: 0, w: 0, h: 0 }, popup_ops: [], popup_hot: false, slide_t0: 0, slide_in: false, ghost_rect: { x: 0, y: 0, w: 0, h: 0 }, ghost_ops: [], } self.unload_desktop() self.desk_mod = Some(w) self.desk_backdrop = backdrop self.apply_module_cfg(w) self.desk_saved.clear() for it in w.pl.config_items() { if it.label.length() == 0 { self.desk_saved.set(it.key, it.value) } } self.desk_t0 = @sys.monotonic_ms() self.desk_next = 0 self.desk_dirty = true self.desk_drawn = None self.bg_key = None // Its clock starts: the first frame request. self.desktop_tick(self.desk_t0) Ok(w) } } } ///| Unload the desktop module (the wallpaper / card shows again). fn DesktopShell::unload_desktop(self : DesktopShell) -> Unit { match self.desk_mod { Some(d) => { d.pl.unload() self.desk_mod = None self.desk_ops = [] self.desk_next = 0 self.desk_focus = false self.desk_hover = false self.desk_grab = false self.desk_drawn = None self.bg_key = None if self.menu_slot == desktop_slot { self.menu_open = false } } None => () } } ///| Reload the desktop module from its file (chandra-ctl reload): settings and hidden state are /// handed back as at login. On failure the old instance stays. fn DesktopShell::reload_desktop(self : DesktopShell) -> Result[Widget, String] { match self.desk_mod { None => Err("") Some(old) => { // Load the new one before dropping the old, so a broken build leaves the desktop as it was. match @wasm.load(old.path, self.tokens) { Err(e) => Err("Can't load module: " + e) Ok(probe) => { let ok = probe.desktop_backdrop() >= 0 probe.unload() if !ok { return Err("not a desktop module any more") } let id = if old.module_id.contains("/") { old.path } else { old.module_id } let path = old.path self.unload_desktop() match self.load_desktop(if self.find_module(id) is Some(_) { id } else { path }) { Ok(w) => Ok(w) Err(e) => Err(e) } } } } } } // ---------------------------------------------------------------- drawing and frames ///| Redraw the module's ops when needed: asked for, or the output size or theme changed (the /// module is told the new size through `render`). fn DesktopShell::desktop_render( self : DesktopShell, w : Int, h : Int, theme : @scene.ThemeTokens, tokens : @theme.Tokens, ) -> Unit { match self.desk_mod { None => () Some(d) => { self.desktop_pump(@sys.monotonic_ms()) let same = match self.desk_drawn { Some((dw, dh, t)) => dw == w && dh == h && t == theme None => false } if same && !self.desk_dirty { return } let out : Array[@scene.DrawOp] = [] translate(d.pl.render(w, h, tokens), 0, 0, out) self.process_requests() d.rect = { x: 0, y: 0, w, h } self.desk_ops = out self.desk_gen = self.desk_gen + 1 self.desk_dirty = false self.desk_drawn = Some((w, h, theme)) } } } ///| Run the module's clock: ask it when the next frame is due, and whether to redraw. fn DesktopShell::desktop_tick(self : DesktopShell, now : UInt64) -> Unit { match self.desk_mod { None => () Some(d) => { let ms = (now - self.desk_t0).to_int() let r = d.pl.desktop_tick(ms) let r = if r > max_frame_delay { max_frame_delay } else { r } // Software rendering: at most ~30 frames a second, however often the module asks. let min = self.desktop_min_frame_ms() let r = if r > 0 && r < min { min } else { r } self.desk_next = if r > 0 { now + r.to_uint64() } else { 0 } } } } ///| A due animation frame: tick, then redraw. Frames pause while a window covers the desktop. fn DesktopShell::desktop_pump(self : DesktopShell, now : UInt64) -> Unit { if self.desk_next != 0 && now >= self.desk_next && !self.desk_covered { self.desktop_tick(now) self.desk_dirty = true } } ///| Milliseconds until the desktop module's next animation frame, -1 when none is due (no /// module, it asked for none, or a window covers the desktop). The main loop's idle sleep is /// capped by this. pub fn DesktopShell::desktop_wake_ms(self : DesktopShell, now : UInt64) -> Int { if self.desk_mod is None || self.desk_next == 0 || self.desk_covered { -1 } else if now >= self.desk_next { 0 } else { (self.desk_next - now).to_int() } } ///| True while the desktop module is animating (it asked for a frame). pub fn DesktopShell::desktop_animating(self : DesktopShell) -> Bool { self.desk_mod is Some(_) && self.desk_next != 0 } ///| Tell the shell whether a window covers the whole desktop (the compositor, every frame). pub fn DesktopShell::set_desktop_covered(self : DesktopShell, covered : Bool) -> Unit { self.desk_covered = covered } ///| After the module handled something: redraw if it asked, restart its clock, and save hidden /// settings that changed (`save`: on releases and keys, not on every motion). fn DesktopShell::desktop_changed(self : DesktopShell, redraw : Bool, save? : Bool = false) -> Unit { if redraw { self.desk_dirty = true } self.desktop_tick(@sys.monotonic_ms()) if save { self.save_desktop_hidden() } } ///| Remember the module's hidden settings (empty label: e.g. where icons were put) that changed. fn DesktopShell::save_desktop_hidden(self : DesktopShell) -> Unit { match self.desk_mod { None => () Some(d) => for it in d.pl.config_items() { if it.label.length() == 0 && self.desk_saved.get(it.key) != Some(it.value) { self.desk_saved.set(it.key, it.value) self.remember_module_setting(d.module_id, it.key, it.value) } } } } ///| Deliver one event to the desktop module and carry out its requests. fn DesktopShell::desktop_event(self : DesktopShell, ev : @wasm.PlasmoidEvent, save? : Bool = false) -> Bool { match self.desk_mod { None => false Some(d) => { self.publish_host_data() let redraw = d.pl.on_event(false, ev) self.process_requests() self.desktop_changed(redraw, save~) true } } } // ---------------------------------------------------------------- input ///| The pointer moved over the empty desktop (no window, panel, popup or menu there). True when /// a desktop module took it. pub fn DesktopShell::desktop_pointer_move(self : DesktopShell, x : Int, y : Int) -> Bool { if self.desk_mod is None { return false } self.desk_hover = true self.desktop_event(PointerMove(x, y)) } ///| The pointer left the empty desktop (onto a window, the panel ...). While a press on the /// desktop is held it keeps getting the motion instead (see `desktop_grabbing`). pub fn DesktopShell::desktop_leave(self : DesktopShell) -> Unit { if self.desk_hover && !self.desk_grab { self.desk_hover = false ignore(self.desktop_event(PointerLeave)) } } ///| A left press on the empty desktop: the desktop takes the keyboard and the module the press. /// True when a desktop module took it. pub fn DesktopShell::desktop_press(self : DesktopShell, x : Int, y : Int) -> Bool { self.ensure_booted() if self.desk_mod is None { return false } self.dismiss_popups("") self.desk_focus = true self.desk_grab = true self.desk_hover = true self.desktop_event(PointerDown(x, y)) } ///| True while a press on the desktop is held: motion and the release go to the module wherever /// the pointer is. pub fn DesktopShell::desktop_grabbing(self : DesktopShell) -> Bool { self.desk_grab && self.desk_mod is Some(_) } ///| The release of a press that went to the desktop. True when there was one. pub fn DesktopShell::desktop_release(self : DesktopShell, x : Int, y : Int) -> Bool { if !self.desk_grab { return false } self.desk_grab = false ignore(self.desktop_event(PointerUp(x, y), save=true)) true } ///| Wheel over the empty desktop. True when a desktop module took it. pub fn DesktopShell::desktop_scroll(self : DesktopShell, x : Int, y : Int, dx : Int, dy : Int) -> Bool { match self.desk_mod { None => false Some(d) => { self.publish_host_data() let redraw = d.pl.desktop_scroll(x, y, dx, dy) self.process_requests() self.desktop_changed(redraw, save=true) true } } } ///| Does the desktop have the keyboard (clicked, and no window focused since)? pub fn DesktopShell::desktop_has_focus(self : DesktopShell) -> Bool { self.desk_focus && self.desk_mod is Some(_) } ///| A key for the desktop module (only while it has the keyboard). True when it took it. pub fn DesktopShell::desktop_key(self : DesktopShell, name : String) -> Bool { if !self.desktop_has_focus() { return false } self.desktop_event(KeyDown(name), save=true) } ///| A window took the keyboard: the desktop module hears `focus-lost`. pub fn DesktopShell::desktop_unfocus(self : DesktopShell) -> Unit { if self.desk_focus { self.desk_focus = false ignore(self.desktop_event(FocusLost)) } } ///| Right-click on the empty desktop: the desktop menu at the pointer, with the module's settings /// (when it has any) and System settings on the Desktop tab. Always consumed. pub fn DesktopShell::desktop_menu(self : DesktopShell, x : Int, y : Int) -> Bool { self.ensure_booted() if self.inspect_on { return false } self.dismiss_popups("") let rows : Array[MenuRow] = [] let title = match self.desk_mod { Some(d) => { if config_rows(d.pl.config_items()).length() > 0 { rows.push(menu_row("Module settings...", 3)) } else { rows.push(menu_row("No module settings", 5)) } d.pl.name } None => "Desktop" } rows.push(menu_row("System settings...", 2, arg="desktop")) self.open_menu(title, rows, desktop_slot, x, y~) true }