///| Keyboard shortcuts: one table of the compositor's named actions and the key combo each is /// bound to. The compositor looks keys up here (compositor/host/shortcuts.mbt), the settings /// module's Shortcuts tab lists and rebinds them (the `shortcuts` capability), and state.edn keeps /// the ones that differ from the defaults: /// /// :shortcuts {:show-desktop "Super+Ctrl+D" :screenshot-full ""} ; "" unbinds /// /// A combo is modifiers plus one key, written "Super+Ctrl+Alt+Shift+Key" (modifiers in that order). /// "Super" alone is a tap of the Super key: pressed and released with nothing else in between. ///| Modifier bits, as in wl_keyboard.modifiers (xkb's depressed mask for the usual keymap). pub let mod_shift : UInt = 1 ///| pub let mod_ctrl : UInt = 4 ///| pub let mod_alt : UInt = 8 ///| pub let mod_logo : UInt = 64 ///| The modifiers a combo can hold (caps and num lock never count). pub let combo_mods : UInt = 1U | 4U | 8U | 64U ///| evdev code of the left Super key; a combo with this key and no modifiers is a Super tap. pub let key_super : UInt = 125 ///| A key combo: modifier bits and an evdev key code. pub(all) struct KeyCombo { mods : UInt key : UInt } derive(Eq, Debug) ///| What a shortcut does. pub(all) enum ShortcutAction { /// Open or close the launcher search. Launcher /// Minimize every window, or bring them back (showdesktop.mbt). ShowDesktop /// Hold the combo's modifiers and press its key to step through the windows (switcher.mbt); /// with Shift added it steps backwards. SwitchWindows /// Screenshots (screenshot.mbt). ShotRegion ShotFull ShotActiveWindow ShotWindowUnderCursor /// Recordings (recorder.mbt); the same shortcut again stops. RecordRegion RecordScreen RecordWindow /// The dock's apps by number: the combo opens the first, the same modifiers with 2 .. 9 the /// second to ninth (when the combo's key is 1). Like clicking the icon: start the app or bring /// its windows forward. See `Shortcuts::dock_app`. DockApps } derive(Eq, Debug) ///| One row of the shortcut table. pub(all) struct ShortcutDef { /// Its key in state.edn's :shortcuts map. id : String /// What the settings module shows. label : String action : ShortcutAction /// The default combo, as text. default_text : String } ///| Every shortcut, in the order the settings module lists them. When two are bound to the same /// combo the earlier one wins. The defaults are what the compositor has always used. pub let shortcut_table : Array[ShortcutDef] = [ { id: "launcher", label: "Open the launcher", action: Launcher, default_text: "Super" }, { id: "show-desktop", label: "Show the desktop", action: ShowDesktop, default_text: "Super+D" }, { id: "switch-windows", label: "Switch windows", action: SwitchWindows, default_text: "Alt+Tab" }, { id: "screenshot-region", label: "Screenshot: region", action: ShotRegion, default_text: "Super+Shift+S" }, { id: "screenshot-region-print", label: "Screenshot: region (Print)", action: ShotRegion, default_text: "Super+Shift+Print" }, { id: "screenshot-full", label: "Screenshot: full screen", action: ShotFull, default_text: "Print" }, { id: "screenshot-window", label: "Screenshot: active window", action: ShotActiveWindow, default_text: "Super+Print" }, { id: "screenshot-under-cursor", label: "Screenshot: window under cursor", action: ShotWindowUnderCursor, default_text: "Super+Ctrl+Print" }, { id: "record-region", label: "Record a region", action: RecordRegion, default_text: "Super+Shift+R" }, { id: "record-screen", label: "Record the screen", action: RecordScreen, default_text: "Super+Alt+R" }, { id: "record-window", label: "Record a window", action: RecordWindow, default_text: "Super+Ctrl+R" }, { id: "dock-apps", label: "Open dock app 1-9", action: DockApps, default_text: "Super+1" }, ] ///| evdev code -> key name, for the keys a combo can end in (US layout names). let key_names : Array[(UInt, String)] = [ (1, "Escape"), (2, "1"), (3, "2"), (4, "3"), (5, "4"), (6, "5"), (7, "6"), (8, "7"), (9, "8"), (10, "9"), (11, "0"), (12, "Minus"), (13, "Equal"), (14, "Backspace"), (15, "Tab"), (16, "Q"), (17, "W"), (18, "E"), (19, "R"), (20, "T"), (21, "Y"), (22, "U"), (23, "I"), (24, "O"), (25, "P"), (26, "BracketLeft"), (27, "BracketRight"), (28, "Enter"), (30, "A"), (31, "S"), (32, "D"), (33, "F"), (34, "G"), (35, "H"), (36, "J"), (37, "K"), (38, "L"), (39, "Semicolon"), (40, "Apostrophe"), (41, "Grave"), (43, "Backslash"), (44, "Z"), (45, "X"), (46, "C"), (47, "V"), (48, "B"), (49, "N"), (50, "M"), (51, "Comma"), (52, "Period"), (53, "Slash"), (57, "Space"), (59, "F1"), (60, "F2"), (61, "F3"), (62, "F4"), (63, "F5"), (64, "F6"), (65, "F7"), (66, "F8"), (67, "F9"), (68, "F10"), (87, "F11"), (88, "F12"), (99, "Print"), (102, "Home"), (103, "Up"), (104, "PageUp"), (105, "Left"), (106, "Right"), (107, "End"), (108, "Down"), (109, "PageDown"), (110, "Insert"), (111, "Delete"), (113, "Mute"), (114, "VolumeDown"), (115, "VolumeUp"), (119, "Pause"), (163, "Next"), (164, "PlayPause"), (165, "Previous"), (224, "BrightnessDown"), (225, "BrightnessUp"), ] ///| Other spellings `KeyCombo::parse` accepts (lower case). let key_aliases : Array[(String, String)] = [ ("esc", "escape"), ("return", "enter"), ("del", "delete"), ("ins", "insert"), ("pgup", "pageup"), ("pgdn", "pagedown"), ("prtsc", "print"), ("printscreen", "print"), ("sysrq", "print"), ("-", "minus"), ("=", "equal"), ("[", "bracketleft"), ("]", "bracketright"), (";", "semicolon"), ("'", "apostrophe"), ("`", "grave"), ("\\", "backslash"), (",", "comma"), (".", "period"), ("/", "slash"), ] ///| evdev codes of the modifier keys themselves (Ctrl, Shift, Alt, Super; left and right). pub fn is_modifier_key(code : UInt) -> Bool { code == 29U || code == 97U || code == 42U || code == 54U || code == 56U || code == 100U || code == 125U || code == 126U } ///| The modifier bit a modifier key sets (0 for other keys). pub fn modifier_of_key(code : UInt) -> UInt { match code { 29U | 97U => mod_ctrl 42U | 54U => mod_shift 56U | 100U => mod_alt 125U | 126U => mod_logo _ => 0U } } ///| The left-hand key for a modifier bit (what a client is told was released). pub fn key_of_modifier(m : UInt) -> UInt { if m == mod_ctrl { 29U } else if m == mod_shift { 42U } else if m == mod_alt { 56U } else { 125U } } ///| The name of an evdev key, or "" when combos can't use it. pub fn key_code_name(code : UInt) -> String { for e in key_names { if e.0 == code { return e.1 } } "" } ///| Whether this combo is a bare tap of the Super key. pub fn KeyCombo::is_super_tap(self : KeyCombo) -> Bool { self.mods == 0U && (self.key == 125U || self.key == 126U) } ///| A combo as text: "Super+Ctrl+Alt+Shift+Key", or "Super" for a Super tap. pub fn KeyCombo::to_text(self : KeyCombo) -> String { if self.is_super_tap() { return "Super" } let parts : Array[String] = [] if (self.mods & mod_logo) != 0U { parts.push("Super") } if (self.mods & mod_ctrl) != 0U { parts.push("Ctrl") } if (self.mods & mod_alt) != 0U { parts.push("Alt") } if (self.mods & mod_shift) != 0U { parts.push("Shift") } let name = key_code_name(self.key) parts.push(if name == "" { "Key" + self.key.to_string() } else { name }) parts.join("+") } ///| Parse "Super+Shift+S" (case doesn't matter; Meta/Win/Logo mean Super, Control means Ctrl). /// "Super" alone is a Super tap. None for anything else: unknown keys, no key, a modifier as the key. pub fn KeyCombo::parse(text : String) -> KeyCombo? { let parts : Array[String] = [] for p in text.split("+") { parts.push(p.trim(chars=" ").to_string()) } // "Ctrl++" names the plus key, which combos don't support; "" is no combo. if parts.length() == 0 || parts.iter().any(p => p == "") { return None } let mut mods = 0U for i = 0; i < parts.length() - 1; i = i + 1 { let m = mod_named(parts[i].to_lower()) if m == 0U || (mods & m) != 0U { return None } mods = mods | m } let last = parts[parts.length() - 1].to_lower() if mod_named(last) == mod_logo && mods == 0U { return Some({ mods: 0U, key: key_super }) } let mut name = last for a in key_aliases { if a.0 == last { name = a.1 } } for e in key_names { if e.1.to_lower() == name { return Some({ mods, key: e.0 }) } } None } ///| fn mod_named(s : String) -> UInt { match s { "super" | "meta" | "win" | "logo" | "mod4" => mod_logo "ctrl" | "control" => mod_ctrl "alt" | "mod1" => mod_alt "shift" => mod_shift _ => 0U } } ///| The default combo of shortcut `i`. pub fn default_combo(i : Int) -> KeyCombo? { KeyCombo::parse(shortcut_table[i].default_text) } ///| The current bindings, one per `shortcut_table` row (None: unbound). pub(all) struct Shortcuts { bindings : Array[KeyCombo?] } ///| Every shortcut on its default combo. pub fn Shortcuts::defaults() -> Shortcuts { let bindings : Array[KeyCombo?] = [] for i = 0; i < shortcut_table.length(); i = i + 1 { bindings.push(default_combo(i)) } { bindings, } } ///| The combo shortcut `i` is bound to. pub fn Shortcuts::binding(self : Shortcuts, i : Int) -> KeyCombo? { if i >= 0 && i < self.bindings.length() { self.bindings[i] } else { None } } ///| The combo shortcut `i` is bound to, as text ("" when unbound). pub fn Shortcuts::binding_text(self : Shortcuts, i : Int) -> String { match self.binding(i) { Some(c) => c.to_text() None => "" } } ///| Bind shortcut `i` to `combo` (None unbinds). False when `i` is out of range or the combo /// can't work for that action (window switching needs a modifier to hold). pub fn Shortcuts::set(self : Shortcuts, i : Int, combo : KeyCombo?) -> Bool { if i < 0 || i >= self.bindings.length() { return false } match combo { Some(c) => if !combo_allowed(shortcut_table[i].action, c) { return false } None => () } self.bindings[i] = combo true } ///| Whether `action` can be bound to `c`: window switching is "hold modifiers, press a key", so it /// needs a modifier and a real key; anything can take a Super tap or a plain combo. pub fn combo_allowed(action : ShortcutAction, c : KeyCombo) -> Bool { match action { SwitchWindows => (c.mods & combo_mods) != 0U && !c.is_super_tap() _ => true } } ///| Put shortcut `i` back to its default. pub fn Shortcuts::reset(self : Shortcuts, i : Int) -> Unit { if i >= 0 && i < self.bindings.length() { self.bindings[i] = default_combo(i) } } ///| Put every shortcut back to its default. pub fn Shortcuts::reset_all(self : Shortcuts) -> Unit { for i = 0; i < self.bindings.length(); i = i + 1 { self.bindings[i] = default_combo(i) } } ///| Whether shortcut `i` is on its default combo. pub fn Shortcuts::is_default(self : Shortcuts, i : Int) -> Bool { self.binding(i) == default_combo(i) } ///| The first shortcut bound to exactly `combo` (modifiers outside `combo_mods` are ignored). pub fn Shortcuts::find(self : Shortcuts, combo : KeyCombo) -> Int? { let want : KeyCombo = { mods: combo.mods & combo_mods, key: combo.key } for i = 0; i < self.bindings.length(); i = i + 1 { match self.bindings[i] { Some(c) => if c.mods == want.mods && same_key(c.key, want.key) { return Some(i) } None => () } } None } ///| The left and right Super keys are one key for combos. fn same_key(a : UInt, b : UInt) -> Bool { a == b || ((a == 125U || a == 126U) && (b == 125U || b == 126U)) } ///| The action bound to `combo`, if any. pub fn Shortcuts::lookup(self : Shortcuts, combo : KeyCombo) -> ShortcutAction? { match self.find(combo) { Some(i) => Some(shortcut_table[i].action) None => None } } ///| Another shortcut bound to the same combo as shortcut `i` (the first such), if any. pub fn Shortcuts::conflict(self : Shortcuts, i : Int) -> Int? { match self.binding(i) { None => None Some(c) => { for j = 0; j < self.bindings.length(); j = j + 1 { if j != i { match self.bindings[j] { Some(d) => if d.mods == c.mods && same_key(d.key, c.key) { return Some(j) } None => () } } } None } } } ///| The bound combo of the first shortcut with `action` (for showing it, e.g. "Alt+Tab"). pub fn Shortcuts::combo_for(self : Shortcuts, action : ShortcutAction) -> KeyCombo? { for i = 0; i < self.bindings.length(); i = i + 1 { if shortcut_table[i].action == action { match self.bindings[i] { Some(c) => return Some(c) None => () } } } None } ///| The shortcuts that differ from their defaults, as (state.edn id, combo text; "" unbound). pub fn Shortcuts::changed(self : Shortcuts) -> Array[(String, String)] { let out : Array[(String, String)] = [] for i = 0; i < self.bindings.length(); i = i + 1 { if !self.is_default(i) { out.push((shortcut_table[i].id, self.binding_text(i))) } } out } ///| Apply saved (id, text) pairs over the defaults: "" unbinds; unknown ids and combos that don't /// parse are ignored (the default stays). pub fn Shortcuts::apply_saved(self : Shortcuts, saved : Array[(String, String)]) -> Unit { for s in saved { for i = 0; i < shortcut_table.length(); i = i + 1 { if shortcut_table[i].id == s.0 { if s.1.trim(chars=" ").to_string() == "" { ignore(self.set(i, None)) } else { match KeyCombo::parse(s.1) { Some(c) => ignore(self.set(i, Some(c))) None => () } } } } } } ///| Shortcut requests from the `shortcuts` capability (wit/host/plasmoids.wit `shortcut(s32)`): /// code = action * 1000 + index, action 0 capture, 1 reset, 2 reset all, 3 cancel the capture. pub fn DesktopShell::shortcut_request(self : DesktopShell, code : Int) -> Unit { if code < 0 { return } let i = code % 1000 match code / 1000 { 0 => self.start_shortcut_capture(i) 1 => self.reset_shortcut(i) 2 => self.reset_all_shortcuts() 3 => self.cancel_shortcut_capture() _ => () } } ///| The shortcut waiting for its new combo, or -1. pub fn DesktopShell::shortcut_capturing(self : DesktopShell) -> Int { self.shortcut_capture } ///| Wait for the next key combo and bind shortcut `i` to it (`finish_shortcut_capture`). pub fn DesktopShell::start_shortcut_capture(self : DesktopShell, i : Int) -> Unit { if i >= 0 && i < shortcut_table.length() { self.shortcut_capture = i self.publish_shortcuts() } } ///| Stop waiting for a combo; the binding stays as it was. pub fn DesktopShell::cancel_shortcut_capture(self : DesktopShell) -> Unit { if self.shortcut_capture >= 0 { self.shortcut_capture = -1 self.publish_shortcuts() } } ///| The combo pressed while capturing (None: unbind). Binds it, saves and ends the capture; false /// (still capturing) when the shortcut can't take that combo. pub fn DesktopShell::finish_shortcut_capture(self : DesktopShell, combo : KeyCombo?) -> Bool { let i = self.shortcut_capture if i < 0 || !self.shortcuts.set(i, combo) { return false } self.shortcut_capture = -1 match self.shortcuts.conflict(i) { Some(j) => self.say(combo.map(c => c.to_text()).unwrap_or("") + " is also " + shortcut_table[j].label) None => () } self.publish_shortcuts() self.save_state() true } ///| Bind shortcut `i` to `combo` directly (None unbinds) and save. False when it can't take it. pub fn DesktopShell::set_shortcut(self : DesktopShell, i : Int, combo : KeyCombo?) -> Bool { if !self.shortcuts.set(i, combo) { return false } self.publish_shortcuts() self.save_state() true } ///| Put shortcut `i` back to its default and save. pub fn DesktopShell::reset_shortcut(self : DesktopShell, i : Int) -> Unit { self.shortcut_capture = -1 self.shortcuts.reset(i) self.publish_shortcuts() self.save_state() } ///| Put every shortcut back to its default and save. pub fn DesktopShell::reset_all_shortcuts(self : DesktopShell) -> Unit { self.shortcut_capture = -1 self.shortcuts.reset_all() self.publish_shortcuts() self.save_state() } ///| Publish the shortcut table for the `shortcuts` capability: per row name, binding, default, /// the name of a conflicting shortcut ("" none), and whether it is capturing. pub fn DesktopShell::publish_shortcuts(self : DesktopShell) -> Unit { let t = @wasm.Table::new() for i = 0; i < shortcut_table.length(); i = i + 1 { t.str(shortcut_table[i].label) t.str(self.shortcuts.binding_text(i)) t.str(shortcut_table[i].default_text) t.str( match self.shortcuts.conflict(i) { Some(j) => shortcut_table[j].label None => "" }, ) t.int(if self.shortcut_capture == i { 1 } else { 0 }) t.end_row() } @wasm.publish(Shortcuts, t) } ///| evdev codes of the 1 and 9 keys (the row above the letters). let key_1 : UInt = 2 ///| let key_9 : UInt = 10 ///| Which of the dock's apps (1-9) `combo` opens: the "Open dock app" combo itself is the first, /// and when that combo ends in 1, the same modifiers with 2 .. 9 open the second to ninth. pub fn Shortcuts::dock_app(self : Shortcuts, combo : KeyCombo) -> Int? { let mods = combo.mods & combo_mods for i = 0; i < self.bindings.length(); i = i + 1 { if shortcut_table[i].action == DockApps { match self.bindings[i] { Some(c) if c.mods == mods => { if c.key == key_1 && combo.key >= key_1 && combo.key <= key_9 { return Some((combo.key - key_1).reinterpret_as_int() + 1) } if same_key(c.key, combo.key) { return Some(1) } } _ => () } } } None } ///| test "Super+1 .. Super+9 open the dock's apps" { let sc = Shortcuts::defaults() assert_eq(sc.dock_app({ mods: mod_logo, key: 2 }), Some(1)) assert_eq(sc.dock_app({ mods: mod_logo, key: 6 }), Some(5)) assert_eq(sc.dock_app({ mods: mod_logo, key: 10 }), Some(9)) assert_eq(sc.dock_app({ mods: mod_logo, key: 11 }), None) // 0 assert_eq(sc.dock_app({ mods: mod_logo | mod_shift, key: 3 }), None) assert_eq(sc.dock_app({ mods: 0, key: 3 }), None) }