Add a built-in on-screen keyboard for handset sessions
CI / test (push) Failing after 52s

- Opens for text-input-v3 fields, terminals, launcher search, Network password, emoji picker and the password dialog; a KB pill summons it manually.
- Enabled on CHASSIS=handset; CHANDRA_OSK=1|0 overrides, and it stays off under CHANDRA_TEST_CONFIG.
- Work area and panel popups shrink above the keyboard so focused content stays visible.
- Keys send evdev codes with Shift, Ctrl and Alt modifiers.
- chandra-ctl: `screenshot [PATH]` saves a PNG without opening the editor.
- Key names cover the full punctuation set and shifted symbols.
- Launcher plasmoid min width drops from 60 to 36.
This commit is contained in:
nandi committed 2026-11-28 00:18:32 -08:00
1 parent 1205329abd
commit 905dab8725
21 files changed
+1335 -68

No files matched your search

+2 -1
View File
@@ -433,7 +433,8 @@ fn ChandraCompositor::auth_ops(self : ChandraCompositor) -> Array[@scene.DrawOp]
// Place the card in the middle (shaking after a failure) with a soft shadow.
let h = y
let cx = (sw - w) / 2 + d.shake_offset(now)
let cyy = ((sh - h) / 2).max(8)
let avail_h = if self.osk_visible { sh - self.osk_height() } else { sh }
let cyy = ((avail_h - h) / 2).max(8)
for k = 1; k <= 4; k = k + 1 {
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(cx - k * 2, cyy - k * 2 + 6, w + k * 4, h + k * 4), @scene.Rgba::new(0, 0, 0, 22), rad + 6 + k * 2))
}
+111 -13
View File
@@ -312,6 +312,20 @@ pub(all) struct ChandraCompositor {
/// External windows from the host X11 WM (`--x11`), bridged into the dock/panel only.
mut ext_windows : Array[ExternalWindow]
mut ext_handler : ExternalWindowHandler?
/// Built-in on-screen keyboard (osk.mbt).
mut osk_enabled : Bool
mut osk_visible : Bool
mut osk_shift : Bool
mut osk_ctrl : Bool
mut osk_alt : Bool
mut osk_sym : Bool
mut osk_manual_hide : Bool
mut osk_manual_show : Bool
mut osk_popup_typing : Bool
mut osk_target : String
mut osk_pressed : Int?
mut osk_hover : Int?
mut osk_pointer_grab : Bool
}
///|
@@ -448,6 +462,19 @@ pub fn ChandraCompositor::new(
kiosk: None,
ext_windows: [],
ext_handler: None,
osk_enabled: osk_default_enabled(),
osk_visible: false,
osk_shift: false,
osk_ctrl: false,
osk_alt: false,
osk_sym: false,
osk_manual_hide: false,
osk_manual_show: false,
osk_popup_typing: false,
osk_target: "",
osk_pressed: None,
osk_hover: None,
osk_pointer_grab: false,
}
// 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.
@@ -459,18 +486,44 @@ pub fn ChandraCompositor::new(
}
///| 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 {
fn work_area_for(
w : Int,
h : Int,
theme : @scene.ThemeTokens,
kiosk~ : Bool = false,
bottom_extra~ : Int = 0,
) -> @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 }
// A locked kiosk has no panel and its windows are fullscreen: the whole output (above OSK).
let bh = h - bottom_extra
return { x: 0, y: 0, w, h: if bh > 1 { bh } else { 1 } }
}
let excl_bottom = if bottom_extra > theme.panel_height { bottom_extra } else { theme.panel_height }
@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,
excl_bottom~,
)
}
///| Recompute the window work area and shell popup clearance (e.g. when OSK opens/closes).
fn ChandraCompositor::recompute_work_area(self : ChandraCompositor) -> Unit {
let bottom_extra = if self.osk_visible { self.osk_height() } else { 0 }
self.work_area = work_area_for(
self.output_w,
self.output_h,
self.theme,
kiosk=self.kiosk_strict(),
bottom_extra~,
)
self.shell.set_popup_bottom_inset(bottom_extra)
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()
}
///| 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 {
@@ -479,11 +532,13 @@ pub fn ChandraCompositor::resize(self : ChandraCompositor, w : Int, h : Int) ->
}
self.output_w = w
self.output_h = h
self.work_area = work_area_for(w, h, self.theme, kiosk=self.kiosk_strict())
let bottom_extra = if self.osk_visible { self.osk_height() } else { 0 }
self.work_area = work_area_for(w, h, self.theme, kiosk=self.kiosk_strict(), bottom_extra~)
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.shell.set_popup_bottom_inset(bottom_extra)
self.apply_actions(self.wm.fit_work_area(self.work_area))
self.last_frame = None
self.last_sig = None
@@ -901,6 +956,7 @@ pub fn ChandraCompositor::step(self : ChandraCompositor) -> Unit {
self.session_tick()
self.screenshot_tick()
self.recording_tick()
self.osk_sync()
}
///| True while something runs on a timer rather than on input or client messages (a recording,
@@ -1143,6 +1199,7 @@ fn ChandraCompositor::build_scene(
panel_ops.append(self.face_window_ops())
panel_ops.append(self.auth_ops())
panel_ops.append(self.auth_notice_ops())
panel_ops.append(self.osk_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.
@@ -1749,7 +1806,14 @@ pub fn ChandraCompositor::sync_window_style(self : ChandraCompositor) -> Unit {
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())
let bottom_extra = if self.osk_visible { self.osk_height() } else { 0 }
self.work_area = work_area_for(
self.output_w,
self.output_h,
self.theme,
kiosk=self.kiosk_strict(),
bottom_extra~,
)
self.apply_actions(self.wm.fit_work_area(self.work_area))
for _, win in self.windows {
if win.csd {
@@ -1875,7 +1939,6 @@ fn key_name(code : UInt, shift : Bool) -> String {
(16U, "qwertyuiop"),
(30U, "asdfghjkl"),
(44U, "zxcvbnm"),
(2U, "1234567890"),
]
for r in rows {
let (first, chars) = r
@@ -1888,30 +1951,53 @@ fn key_name(code : UInt, shift : Bool) -> String {
}
}
}
if code >= 2U && code <= 11U {
let digits = "1234567890"
let shifted = "!@#$%^&*()"
let s = if shift { shifted } else { digits }
let c = Int::unsafe_to_char(s[(code - 2U).reinterpret_as_int()].to_int())
return c.to_string()
}
match code {
1U => "Escape"
14U => "Backspace"
28U => "Enter"
57U => " "
12U => "-"
12U => if shift { "_" } else { "-" }
13U => if shift { "+" } else { "=" }
15U => "Tab"
26U => if shift { "{" } else { "[" }
27U => if shift { "}" } else { "]" }
39U => if shift { ":" } else { ";" }
40U => if shift { "\"" } else { "'" }
41U => if shift { "~" } else { "`" }
43U => if shift { "|" } else { "\\" }
51U => if shift { "<" } else { "," }
52U => if shift { ">" } else { "." }
53U => if shift { "?" } else { "/" }
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.
///| Route host input: OSK first, then grabs, shell (launcher), and focused window.
pub fn ChandraCompositor::handle_input(
self : ChandraCompositor,
ev : @nested.InputEvent,
time_ms : UInt,
) -> Unit {
self.handle_input_inner(ev, time_ms)
self.osk_sync()
}
///|
fn ChandraCompositor::handle_input_inner(
self : ChandraCompositor,
ev : @nested.InputEvent,
time_ms : UInt,
) -> Unit {
self.note_activity(@sys.monotonic_ms())
match ev {
@@ -1925,6 +2011,9 @@ pub fn ChandraCompositor::handle_input(
}
_ => ()
}
if self.osk_input(ev, time_ms) {
return
}
// The password dialog takes all input while it is up; else the Face unlock window does.
if self.grab_input(ev) {
return
@@ -2199,6 +2288,9 @@ pub fn ChandraCompositor::handle_input(
self.super_solo = false
if button != 273U && self.shell.pointer_down(xi, yi) {
self.run_shell_actions()
if button == 272U {
self.osk_on_shell_click(xi, yi)
}
return
}
match self.window_at(xi, yi) {
@@ -2250,8 +2342,11 @@ pub fn ChandraCompositor::handle_input(
return
}
match self.window_at(xi, yi) {
Some((wid, _)) => {
Some((wid, client)) => {
self.activate_window(wid)
if button == 272U && client {
self.osk_on_window_click(wid)
}
}
None => ()
}
@@ -2334,6 +2429,9 @@ pub fn ChandraCompositor::handle_input(
///| 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 {
if self.osk_hit_any(self.ptr_x.to_int(), self.ptr_y.to_int()) {
return 4U
}
match self.auth {
Some(ui) => return self.auth_cursor(ui)
None => ()
+30
View File
@@ -212,6 +212,7 @@ pub fn ChandraCompositor::control_command(self : ChandraCompositor, line : Strin
"kiosk [lock|unlock|status] kiosk mode (--kiosk): lock it down or unlock it for admin work",
"launch ID start desktop app ID (e.g. org.kde.kwi) as the dock does; a failed start shows its notice",
"windows each window: frame, layout, box, shadow, size limits; and what is under the pointer",
"screenshot [PATH] save a full-screen PNG directly (without opening the editor)",
"auth-demo [MODE] show the password dialog with a pretend check (password \"demo\"); MODE: " +
@polkit.demo_modes.join(", "),
"auth-status the password dialog's agent: running, registered, or why not",
@@ -329,6 +330,35 @@ pub fn ChandraCompositor::control_command(self : ChandraCompositor, line : Strin
out.push("ok " + self.windows.length().to_string() + " windows")
(out, true)
}
"screenshot" => {
if words.length() > 2 {
return (["error usage: screenshot [PATH]"], false)
}
match self.capture_png() {
None => (["error could not capture screen"], false)
Some(png) =>
if words.length() == 2 {
let path = words[1]
if @sys.write_file_bytes(path, png) == 0 {
(["ok " + path], true)
} else {
(["error could not write " + path], false)
}
} else {
let dir = if self.shot_dir.length() > 0 {
self.shot_dir
} else {
pictures_dir() + "/Screenshots"
}
let path = save_png(dir, png)
if path != "" {
(["ok " + path], true)
} else {
(["error could not save screenshot in " + dir], false)
}
}
}
}
other => (["error unknown command '" + other + "' (try: help)"], false)
}
}
+9 -3
View File
@@ -181,7 +181,12 @@ fn ChandraCompositor::emoji_place(self : ChandraCompositor, ui : EmojiUi) -> Uni
let (ox, oy) = win.surface_origin()
x = ox + cx
y = oy + cy + ch + 6
if y + h > sh - self.theme.panel_height {
let bottom_edge = if self.osk_visible {
self.output_h - self.osk_height()
} else {
sh - self.theme.panel_height
}
if y + h > bottom_edge {
y = oy + cy - h - 6 // no room under the caret: above it
}
}
@@ -195,8 +200,9 @@ fn ChandraCompositor::emoji_place(self : ChandraCompositor, ui : EmojiUi) -> Uni
}
None => ()
}
x = x.min(sw - w - 8).max(8)
y = y.min(sh - h - 8).max(8)
x = x.min((sw - w - 8).max(8)).max(8)
let max_y = (if self.osk_visible { sh - self.osk_height() } else { sh }) - h - 8
y = y.min(max_y.max(8)).max(8)
ui.rect = @scene.Rect::new(x, y, w, h)
}
+8 -1
View File
@@ -177,7 +177,14 @@ fn ChandraCompositor::apply_kiosk_lock(self : ChandraCompositor) -> Unit {
self.switcher_on = false
}
}
self.work_area = work_area_for(self.output_w, self.output_h, self.theme, kiosk=locked)
let bottom_extra = if self.osk_visible { self.osk_height() } else { 0 }
self.work_area = work_area_for(
self.output_w,
self.output_h,
self.theme,
kiosk=locked,
bottom_extra~,
)
if !locked {
// Give the windows the kiosk made fullscreen back to the normal layout.
let ids : Array[UInt64] = []
+698
View File
@@ -0,0 +1,698 @@
///| Built-in on-screen keyboard (OSK) for touch / handset sessions.
///
/// Chandra does not expose external wlr-layer-shell / virtual-keyboard protocols, so a built-in
/// compositor OSK works uniformly across Wayland clients (wl_keyboard evdev codes + modifiers),
/// panel popups (Launcher search, Network password), the emoji picker, and the polkit dialog.
///
/// Enablement: `CHANDRA_OSK=1|0` overrides; under `CHANDRA_TEST_CONFIG=1` it stays off unless
/// explicitly enabled; otherwise enabled when `/etc/machine-info` reports `CHASSIS=handset`.
///|
priv enum OskAction {
OskKeyCode(UInt, Bool) // evdev keycode, implicit Shift
OskShift
OskCtrl
OskAlt
OskSym
OskHide
}
///|
priv struct OskKey {
rect : @scene.Rect
label : String
action : OskAction
active : Bool
accent : Bool
}
///| Default OSK enablement at compositor startup.
fn osk_default_enabled() -> Bool {
let env = @sys.getenv("CHANDRA_OSK").to_lower()
if env == "1" || env == "true" || env == "on" {
return true
}
if env == "0" || env == "false" || env == "off" {
return false
}
if @sys.getenv("CHANDRA_TEST_CONFIG") != "" {
return false
}
let info = @utf8.decode_lossy(@sys.read_file_bytes("/etc/machine-info"))
info.contains("CHASSIS=handset")
}
///| Whether `app_id` belongs to a terminal emulator (terminals use direct keystrokes rather than
/// zwp_text_input_v3, so focusing one auto-opens the OSK).
fn osk_is_terminal_app(app_id : String) -> Bool {
let id = app_id.to_lower()
if id.length() == 0 {
return false
}
for name in [
"foot", "kitty", "alacritty", "wezterm", "konsole", "ghostty", "xterm", "urxvt", "st",
"gnome-terminal", "terminal",
] {
if id.contains(name) {
return true
}
}
false
}
///| Enable or disable the on-screen keyboard at runtime (used in tests and toggles).
pub fn ChandraCompositor::set_osk_enabled(self : ChandraCompositor, enabled : Bool) -> Unit {
if self.osk_enabled == enabled {
return
}
self.osk_enabled = enabled
if !enabled {
self.osk_manual_hide = false
self.osk_manual_show = false
self.osk_popup_typing = false
self.osk_set_visible(false)
} else {
self.osk_sync()
}
self.damage.mark_full()
}
///| Whether the on-screen keyboard is currently visible.
pub fn ChandraCompositor::osk_is_visible(self : ChandraCompositor) -> Bool {
self.osk_visible
}
///| Center `(x, y)` of the visible OSK key matching `label` (useful for automated tests).
pub fn ChandraCompositor::osk_key_center(self : ChandraCompositor, label : String) -> (Int, Int)? {
if !self.osk_visible {
return None
}
for k in self.osk_keys() {
if k.label == label {
return Some((k.rect.x + k.rect.w / 2, k.rect.y + k.rect.h / 2))
}
}
None
}
///| Height of the on-screen keyboard when shown.
pub fn ChandraCompositor::osk_height(self : ChandraCompositor) -> Int {
let half = self.output_h / 2
let h = if 236 < half { 236 } else { half }
if h < 120 { (120).min(self.output_h) } else { h }
}
///| Bounding rectangle of the visible on-screen keyboard.
pub fn ChandraCompositor::osk_rect(self : ChandraCompositor) -> @scene.Rect {
let h = self.osk_height()
@scene.Rect::new(0, self.output_h - h, self.output_w, h)
}
///| Compact "KB" pill above the bottom-right corner of the panel when the OSK is enabled but
/// currently hidden, letting users summon the keyboard anywhere manually.
pub fn ChandraCompositor::osk_toggle_rect(self : ChandraCompositor) -> @scene.Rect {
let w = 44
let h = 28
let x = (self.output_w - w - 6).max(4)
let y = (self.output_h - self.theme.panel_height - h - 6).max(4)
@scene.Rect::new(x, y, w, h)
}
///| Show or hide the OSK, resizing `work_area` and lifting panel popups so focused content stays
/// above the keyboard.
fn ChandraCompositor::osk_set_visible(self : ChandraCompositor, want : Bool) -> Unit {
if self.osk_visible == want {
return
}
self.osk_visible = want
if !want {
self.osk_pressed = None
self.osk_hover = None
self.osk_pointer_grab = false
self.osk_shift = false
self.osk_ctrl = false
self.osk_alt = false
}
self.recompute_work_area()
match self.emoji_ui {
Some(ui) => self.emoji_place(ui)
None => ()
}
}
///| Synchronize OSK visibility with the active focus target (auth dialog, emoji picker, open
/// shell popup, active text-input-v3 or terminal window).
pub fn ChandraCompositor::osk_sync(self : ChandraCompositor) -> Unit {
if !self.osk_enabled {
if self.osk_visible {
self.osk_set_visible(false)
}
return
}
let (target, auto_want) : (String, Bool) = if self.auth is Some(_) {
("auth", true)
} else if self.emoji_ui is Some(_) {
("emoji", true)
} else if self.shell.popup_open() {
let mid = self.shell.open_popup_module_id()
let t = if mid.length() > 0 { "popup:" + mid } else { "menu" }
let aw = mid == "launcher" || (mid == "network" && self.osk_popup_typing)
(t, aw)
} else {
match self.kbd_win {
Some(wid) => {
let ti = self.server.text_input_active(wid)
let term = match self.windows.get(wid) {
Some(win) => osk_is_terminal_app(win.app_id)
None => false
}
let aw = ti || term
let ti_tag = if ti { ":ti1" } else { ":ti0" }
("win:" + wid.to_string() + ti_tag, aw)
}
None => ("", false)
}
}
if target != self.osk_target {
let prev_ti = self.osk_target.has_prefix("win:") && self.osk_target.contains(":ti")
let next_ti = target.has_prefix("win:") && target.contains(":ti1")
self.osk_target = target
self.osk_manual_hide = false
// Transitioning out of non-popup contexts clears popup typing state.
if !target.has_prefix("popup:") {
self.osk_popup_typing = false
}
// Keep manual show only if staying off-auto wasn't invalidated by switching targets.
if !(prev_ti && next_ti && auto_want) {
self.osk_manual_show = false
}
}
let want = if self.osk_manual_hide {
false
} else {
auto_want || self.osk_manual_show
}
self.osk_set_visible(want)
}
///| Notify the OSK when the user clicks inside an already-open shell popup (e.g. Launcher search
/// or Network password entry) so the keyboard opens or reopens after being hidden.
fn ChandraCompositor::osk_on_shell_click(self : ChandraCompositor, xi : Int, yi : Int) -> Unit {
if !self.osk_enabled {
return
}
let mid = self.shell.open_popup_module_id()
if mid == "launcher" {
self.osk_manual_hide = false
} else if mid == "network" {
match self.shell.open_popup_rect() {
Some(r) if r.contains(xi, yi) => {
self.osk_popup_typing = true
self.osk_manual_hide = false
}
_ => ()
}
}
}
///| Notify the OSK when the user clicks inside a client window's surface: if it has an active
/// text input or is a terminal (even after tapping Hide), bring the OSK back up.
fn ChandraCompositor::osk_on_window_click(self : ChandraCompositor, wid : UInt64) -> Unit {
if !self.osk_enabled {
return
}
let ti = self.server.text_input_active(wid)
let term = match self.windows.get(wid) {
Some(w) => osk_is_terminal_app(w.app_id)
None => false
}
if ti || term {
self.osk_manual_hide = false
}
}
///| Lay out one row of keys inside `(rx, y, rw, h)`. `lead_units` adds half-key indentation (e.g.
/// `0.5` for the home row) within `total_units`.
fn osk_layout_row(
rx : Int,
y : Int,
rw : Int,
h : Int,
lead_units : Double,
total_units : Double,
specs : Array[(String, Double, OskAction, Bool, Bool)],
out : Array[OskKey],
) -> Unit {
let pad_x = 4
let gap = 4
let usable_w = (rw - pad_x * 2).max(40).to_double()
let mut cursor_u = lead_units
for sp in specs {
let (label, units, action, active, accent) = sp
let x0 = rx + pad_x + (cursor_u / total_units * usable_w).to_int()
let x1 = rx + pad_x + ((cursor_u + units) / total_units * usable_w).to_int()
let kw = (x1 - x0 - gap).max(12)
out.push({ rect: @scene.Rect::new(x0 + gap / 2, y, kw, h), label, action, active, accent })
cursor_u = cursor_u + units
}
}
///| Alpha/symbol letter label (capitalized when Shift is active).
fn osk_letter_label(s : String, upper : Bool) -> String {
if upper { s.to_upper() } else { s }
}
///| Build all clickable keys for the current OSK state.
fn ChandraCompositor::osk_keys(self : ChandraCompositor) -> Array[OskKey] {
let r = self.osk_rect()
let keys : Array[OskKey] = []
let top_pad = 5
let bot_pad = 5
let row_gap = 4
let inner_h = (r.h - top_pad - bot_pad - row_gap * 4).max(80)
let h0 = (inner_h * 16 / 100).max(24).min(34)
let key_h = ((inner_h - h0) / 4).max(14)
let y0 = r.y + top_pad
let y1 = y0 + h0 + row_gap
let y2 = y1 + key_h + row_gap
let y3 = y2 + key_h + row_gap
let y4 = y3 + key_h + row_gap
// Row 0: terminal/navigation utilities + Hide
osk_layout_row(
r.x,
y0,
r.w,
h0,
0.0,
10.0,
[
("Esc", 1.0, OskKeyCode(1U, false), false, false),
("Tab", 1.0, OskKeyCode(15U, false), false, false),
("Ctrl", 1.2, OskCtrl, self.osk_ctrl, false),
("Alt", 1.1, OskAlt, self.osk_alt, false),
("←", 1.0, OskKeyCode(105U, false), false, false),
("↓", 1.0, OskKeyCode(108U, false), false, false),
("↑", 1.0, OskKeyCode(103U, false), false, false),
("→", 1.0, OskKeyCode(106U, false), false, false),
("Hide", 1.7, OskHide, false, false),
],
keys,
)
if !self.osk_sym {
let sh = self.osk_shift
// Row 1: q..p
let r1 : Array[(String, Double, OskAction, Bool, Bool)] = []
let q_chars = ["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"]
for i, ch in q_chars {
r1.push(
(osk_letter_label(ch, sh), 1.0, OskKeyCode((16 + i).reinterpret_as_uint(), false), false, false),
)
}
osk_layout_row(r.x, y1, r.w, key_h, 0.0, 10.0, r1, keys)
// Row 2: a..l
let r2 : Array[(String, Double, OskAction, Bool, Bool)] = []
let a_chars = ["a", "s", "d", "f", "g", "h", "j", "k", "l"]
for i, ch in a_chars {
r2.push(
(osk_letter_label(ch, sh), 1.0, OskKeyCode((30 + i).reinterpret_as_uint(), false), false, false),
)
}
osk_layout_row(r.x, y2, r.w, key_h, 0.5, 10.0, r2, keys)
// Row 3: Shift z..m Backspace
let r3 : Array[(String, Double, OskAction, Bool, Bool)] = [
("Shift", 1.5, OskShift, self.osk_shift, false),
]
let z_chars = ["z", "x", "c", "v", "b", "n", "m"]
for i, ch in z_chars {
r3.push(
(osk_letter_label(ch, sh), 1.0, OskKeyCode((44 + i).reinterpret_as_uint(), false), false, false),
)
}
r3.push(("⌫", 1.5, OskKeyCode(14U, false), false, false))
osk_layout_row(r.x, y3, r.w, key_h, 0.0, 10.0, r3, keys)
// Row 4: 123 , Space . Enter
osk_layout_row(
r.x,
y4,
r.w,
key_h,
0.0,
10.0,
[
("123", 1.5, OskSym, false, false),
(",", 1.0, OskKeyCode(51U, false), false, false),
("Space", 4.5, OskKeyCode(57U, false), false, false),
(".", 1.0, OskKeyCode(52U, false), false, false),
("↵", 2.0, OskKeyCode(28U, false), false, true),
],
keys,
)
} else {
// Symbols Row 1: 1..0
let r1 : Array[(String, Double, OskAction, Bool, Bool)] = []
let digits = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
for i, d in digits {
r1.push((d, 1.0, OskKeyCode((2 + i).reinterpret_as_uint(), false), false, false))
}
osk_layout_row(r.x, y1, r.w, key_h, 0.0, 10.0, r1, keys)
// Symbols Row 2: !..)
let r2 : Array[(String, Double, OskAction, Bool, Bool)] = []
let syms = ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")"]
for i, s in syms {
r2.push((s, 1.0, OskKeyCode((2 + i).reinterpret_as_uint(), true), false, false))
}
osk_layout_row(r.x, y2, r.w, key_h, 0.0, 10.0, r2, keys)
// Symbols Row 3: common punctuation & shell operators + Backspace
osk_layout_row(
r.x,
y3,
r.w,
key_h,
0.0,
10.0,
[
("-", 0.95, OskKeyCode(12U, false), false, false),
("_", 0.95, OskKeyCode(12U, true), false, false),
("=", 0.95, OskKeyCode(13U, false), false, false),
("+", 0.95, OskKeyCode(13U, true), false, false),
("/", 0.95, OskKeyCode(53U, false), false, false),
("\\", 0.95, OskKeyCode(43U, false), false, false),
(":", 0.95, OskKeyCode(39U, true), false, false),
("'", 0.95, OskKeyCode(40U, false), false, false),
("?", 0.95, OskKeyCode(53U, true), false, false),
("⌫", 1.45, OskKeyCode(14U, false), false, false),
],
keys,
)
// Symbols Row 4: ABC ; " Space ~ | Enter
osk_layout_row(
r.x,
y4,
r.w,
key_h,
0.0,
10.0,
[
("ABC", 1.5, OskSym, true, false),
(";", 1.0, OskKeyCode(39U, false), false, false),
("\"", 1.0, OskKeyCode(40U, true), false, false),
("Space", 2.5, OskKeyCode(57U, false), false, false),
("~", 1.0, OskKeyCode(41U, true), false, false),
("|", 1.0, OskKeyCode(43U, true), false, false),
("↵", 2.0, OskKeyCode(28U, false), false, true),
],
keys,
)
}
keys
}
///| Key index at screen position `(x, y)`, if any.
fn ChandraCompositor::osk_key_at(self : ChandraCompositor, x : Int, y : Int) -> Int? {
if !self.osk_visible || !self.osk_rect().contains(x, y) {
return None
}
let keys = self.osk_keys()
for i, k in keys {
if k.rect.contains(x, y) {
return Some(i)
}
}
None
}
///| Whether `(x, y)` hits either the open OSK or its compact reopen pill.
fn ChandraCompositor::osk_hit_any(self : ChandraCompositor, x : Int, y : Int) -> Bool {
if !self.osk_enabled {
return false
}
if self.osk_visible {
self.osk_rect().contains(x, y)
} else {
!self.kiosk_strict() && self.osk_toggle_rect().contains(x, y)
}
}
///| Route one virtual key press+release to the active target (auth dialog, emoji picker, open
/// shell popup, focused Wayland client, or focused desktop module).
fn ChandraCompositor::osk_send_key(
self : ChandraCompositor,
code : UInt,
implicit_shift : Bool,
time_ms : UInt,
) -> Unit {
let shift = implicit_shift || self.osk_shift
let ctrl = self.osk_ctrl
let alt = self.osk_alt
let mod_bits : UInt = (if shift { 1U } else { 0U }) |
(if ctrl { 4U } else { 0U }) |
(if alt { 8U } else { 0U })
match self.auth {
Some(ui) => {
let saved_mods = self.mods
let saved_lat = self.mods_latched
self.mods = mod_bits
self.mods_latched = 0U
ignore(self.auth_input(ui, @nested.InputEvent::Key(code, true)))
if self.auth is Some(ui2) {
ignore(self.auth_input(ui2, @nested.InputEvent::Key(code, false)))
}
self.mods = saved_mods
self.mods_latched = saved_lat
}
None =>
if self.emoji_ui is Some(_) {
let saved_mods = self.mods
let saved_lat = self.mods_latched
self.mods = mod_bits
self.mods_latched = 0U
ignore(self.emoji_input(@nested.InputEvent::Key(code, true), time_ms))
if self.emoji_ui is Some(_) {
ignore(self.emoji_input(@nested.InputEvent::Key(code, false), time_ms + 1U))
}
self.mods = saved_mods
self.mods_latched = saved_lat
} else if self.shell.popup_open() {
let name = key_name(code, shift)
if name.length() > 0 {
self.shell.key_down(name)
self.run_shell_actions()
}
} else {
match self.kbd_win {
Some(wid) => {
let need_mod = mod_bits != self.mods || self.mods_latched != 0U
if need_mod {
self.server.keyboard_modifiers(wid, mod_bits, 0U, self.mods_locked, self.mods_group)
}
self.server.keyboard_key(wid, time_ms, code, true)
self.server.keyboard_key(wid, time_ms + 1U, code, false)
if need_mod {
self.server.keyboard_modifiers(
wid,
self.mods,
self.mods_latched,
self.mods_locked,
self.mods_group,
)
}
}
None =>
if self.shell.desktop_has_focus() {
let name = key_name(code, shift)
if name.length() > 0 && self.shell.desktop_key(name) {
self.run_shell_actions()
}
}
}
}
}
self.osk_shift = false
self.osk_ctrl = false
self.osk_alt = false
}
///| Execute the action of key `idx`.
fn ChandraCompositor::osk_activate_key(
self : ChandraCompositor,
idx : Int,
time_ms : UInt,
) -> Unit {
let keys = self.osk_keys()
if idx < 0 || idx >= keys.length() {
return
}
match keys[idx].action {
OskKeyCode(code, implicit_shift) => self.osk_send_key(code, implicit_shift, time_ms)
OskShift => self.osk_shift = !self.osk_shift
OskCtrl => self.osk_ctrl = !self.osk_ctrl
OskAlt => self.osk_alt = !self.osk_alt
OskSym => {
self.osk_sym = !self.osk_sym
self.osk_shift = false
}
OskHide => {
self.osk_manual_hide = true
self.osk_manual_show = false
self.osk_set_visible(false)
}
}
self.damage.mark_full()
}
///| Handle host input when over the OSK or its toggle pill. True if consumed.
fn ChandraCompositor::osk_input(
self : ChandraCompositor,
ev : @nested.InputEvent,
time_ms : UInt,
) -> Bool {
if !self.osk_enabled {
return false
}
match ev {
PointerMove(x, y) => {
self.ptr_x = x
self.ptr_y = y
let xi = x.to_int()
let yi = y.to_int()
if self.osk_visible {
if self.osk_pointer_grab || self.osk_rect().contains(xi, yi) {
let h = self.osk_key_at(xi, yi)
if h != self.osk_hover {
self.osk_hover = h
self.damage.mark_full()
}
return true
}
if self.osk_hover is Some(_) {
self.osk_hover = None
self.damage.mark_full()
}
return false
}
if !self.kiosk_strict() && self.osk_toggle_rect().contains(xi, yi) {
if self.osk_hover != Some(-1) {
self.osk_hover = Some(-1)
self.damage.mark_full()
}
return true
}
if self.osk_hover == Some(-1) {
self.osk_hover = None
self.damage.mark_full()
}
false
}
PointerButton(button, pressed) => {
let xi = self.ptr_x.to_int()
let yi = self.ptr_y.to_int()
if !pressed && button == 272U && self.osk_pointer_grab {
self.osk_pointer_grab = false
let idx_opt = self.osk_pressed
self.osk_pressed = None
self.damage.mark_full()
match idx_opt {
Some(-1) =>
if !self.osk_visible && !self.kiosk_strict() && self.osk_toggle_rect().contains(xi, yi) {
self.osk_manual_hide = false
self.osk_manual_show = true
self.osk_sync()
}
Some(idx) =>
if self.osk_visible && self.osk_key_at(xi, yi) == Some(idx) {
self.osk_activate_key(idx, time_ms)
self.osk_sync()
}
None => ()
}
return true
}
if self.osk_visible && self.osk_rect().contains(xi, yi) {
if button == 272U && pressed {
self.osk_pointer_grab = true
self.osk_pressed = self.osk_key_at(xi, yi)
self.damage.mark_full()
}
return true
}
if !self.osk_visible && !self.kiosk_strict() && self.osk_toggle_rect().contains(xi, yi) {
if button == 272U && pressed {
self.osk_pointer_grab = true
self.osk_pressed = Some(-1)
self.damage.mark_full()
}
return true
}
false
}
Scroll(_, _, _) => {
let xi = self.ptr_x.to_int()
let yi = self.ptr_y.to_int()
if (self.osk_visible && self.osk_rect().contains(xi, yi)) || self.osk_pointer_grab {
return true
}
false
}
_ => false
}
}
///| Draw the OSK or its compact summon pill when enabled.
fn ChandraCompositor::osk_ops(self : ChandraCompositor) -> Array[@scene.DrawOp] {
let ops : Array[@scene.DrawOp] = []
if !self.osk_enabled {
return ops
}
let pal = self.theme.palette
let fs = self.theme.font_size
let rad = self.theme.radius
if !self.osk_visible {
if self.kiosk_strict() {
return ops
}
let tr = self.osk_toggle_rect()
let bg = if self.osk_pressed == Some(-1) {
pal.accent
} else if self.osk_hover == Some(-1) {
pal.surface_hover
} else {
@scene.Rgba::new(pal.surface_popup.r.to_int(), pal.surface_popup.g.to_int(), pal.surface_popup.b.to_int(), 230)
}
let fg = if self.osk_pressed == Some(-1) { pal.accent_foreground } else { pal.foreground }
ops.push(@scene.DrawOp::FillRect(tr, bg, rad + 4))
ops.push(@scene.DrawOp::StrokeRect(tr, pal.border, 1, rad + 4))
ops.push(@scene.DrawOp::Text(tr.x + tr.w / 2, tr.y + tr.h / 2, "KB", (fs - 2).max(10), fg, @scene.TextAlign::Center, true))
return ops
}
let r = self.osk_rect()
let panel_bg = @scene.Rgba::new(pal.surface_popup.r.to_int(), pal.surface_popup.g.to_int(), pal.surface_popup.b.to_int(), 248)
ops.push(@scene.DrawOp::FillRect(r, panel_bg, 0))
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(r.x, r.y, r.w, 1), pal.border, 0))
let keys = self.osk_keys()
let krad = rad.min(8).max(4)
for i, k in keys {
let down = self.osk_pressed == Some(i)
let lit = k.active || down || k.accent
let kbg = if lit {
pal.accent
} else if self.osk_hover == Some(i) {
pal.surface_hover
} else {
pal.surface
}
let kfg = if lit { pal.accent_foreground } else { pal.foreground }
let kfs = if k.label.length() > 1 { (fs - 2).max(10) } else { fs }
ops.push(@scene.DrawOp::FillRect(k.rect, kbg, krad))
ops.push(@scene.DrawOp::StrokeRect(k.rect, pal.border, 1, krad))
ops.push(@scene.DrawOp::Text(k.rect.x + k.rect.w / 2, k.rect.y + k.rect.h / 2, k.label, kfs, kfg, @scene.TextAlign::Center, lit))
}
ops
}
+139
View File
@@ -0,0 +1,139 @@
///| Built-in on-screen keyboard (osk.mbt): auto-open on text-input-v3 and terminal/launcher
/// focus, work_area resizing, popup lifting, virtual evdev key + Shift delivery, and Hide/reopen.
///|
fn osk_comp(tag : String, enable_osk? : Bool = true) -> @host.ChandraCompositor {
let socket_path = "/tmp/wayland-moon-osk-" + tag + "-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 540, 1170).unwrap()
if enable_osk {
comp.set_osk_enabled(true)
}
ignore(comp.compose_frame())
comp
}
///|
test "OSK stays disabled under CHANDRA_TEST_CONFIG=1 unless explicitly enabled" {
let socket_path = "/tmp/wayland-moon-osk-disabled-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 540, 1170).unwrap()
let (a, shm_a, surf_a, xdg_a, _, _) = open_and_configure_client(comp, socket_path, "Editor", "editor")
commit_shm_frame(comp, a, shm_a, surf_a, xdg_a, 1UL)
let (ti, _) = text_input_for(comp, a)
text_input_state(comp, a, ti, true, 50, 50, 16)
assert_false(comp.osk_is_visible())
comp.close()
}
///|
test "OSK auto-opens on zwp_text_input_v3 enable, shrinks work_area, and auto-hides on disable" {
let socket_path = "/tmp/wayland-moon-osk-ti-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 540, 1170).unwrap()
comp.set_osk_enabled(true)
let full_wa_h = comp.work_area.h
let (a, shm_a, surf_a, xdg_a, _, _) = open_and_configure_client(comp, socket_path, "Editor", "editor")
commit_shm_frame(comp, a, shm_a, surf_a, xdg_a, 1UL)
assert_eq(comp.kbd_win, Some(1UL))
let (ti, _) = text_input_for(comp, a)
assert_false(comp.osk_is_visible())
// Enabling text-input-v3 raises the OSK and shrinks the window work area cleanly above it.
text_input_state(comp, a, ti, true, 50, 50, 16)
assert_true(comp.osk_is_visible())
assert_eq(comp.work_area.h, full_wa_h - (comp.osk_height() - comp.theme.panel_height))
// Disabling text-input-v3 hides the OSK and restores the full work area.
text_input_state(comp, a, ti, false, 0, 0, 0)
assert_false(comp.osk_is_visible())
assert_eq(comp.work_area.h, full_wa_h)
comp.close()
}
///|
test "OSK delivers evdev keys and one-shot Shift modifier to focused Wayland client" {
let socket_path = "/tmp/wayland-moon-osk-keys-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 540, 1170).unwrap()
comp.set_osk_enabled(true)
let (a, shm_a, surf_a, xdg_a, _, _) = open_and_configure_client(comp, socket_path, "Editor", "editor")
commit_shm_frame(comp, a, shm_a, surf_a, xdg_a, 1UL)
let (ti, kbd) = text_input_for(comp, a)
text_input_state(comp, a, ti, true, 50, 50, 16)
assert_true(comp.osk_is_visible())
ignore(drain_args(a))
// Tap 'a' (evdev 30) -> press + release sent via wl_keyboard.key (opcode 3).
let (ax, ay) = comp.osk_key_center("a").unwrap()
click_at(comp, ax, ay, 20U)
let msgs1 = drain_args(a)
let keys1 = msgs1.filter(m => m.0 == kbd && m.1 == 3U)
assert_eq(keys1.length(), 2)
assert_eq((keys1[0].2[2], keys1[0].2[3]), (30U, 1U))
assert_eq((keys1[1].2[2], keys1[1].2[3]), (30U, 0U))
// Tap 'Shift', verify key labels uppercase, tap 'A' -> Shift mod active then released.
let (sx, sy) = comp.osk_key_center("Shift").unwrap()
click_at(comp, sx, sy, 30U)
let (ux, uy) = comp.osk_key_center("A").unwrap()
click_at(comp, ux, uy, 40U)
let msgs2 = drain_args(a)
let mods2 = msgs2.filter(m => m.0 == kbd && m.1 == 4U)
let keys2 = msgs2.filter(m => m.0 == kbd && m.1 == 3U)
assert_true(mods2.length() >= 2)
assert_eq(mods2[0].2[1], 1U) // Shift depressed
assert_eq(mods2[mods2.length() - 1].2[1], 0U) // Restored
assert_eq(keys2.length(), 2)
assert_eq((keys2[0].2[2], keys2[0].2[3]), (30U, 1U))
assert_eq((keys2[1].2[2], keys2[1].2[3]), (30U, 0U))
// One-shot Shift cleared back to lowercase.
assert_true(comp.osk_key_center("a") is Some(_))
comp.close()
}
///|
test "OSK Hide button dismisses keyboard and KB pill or client tap reopens it" {
let socket_path = "/tmp/wayland-moon-osk-hide-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 540, 1170).unwrap()
comp.set_osk_enabled(true)
let (a, shm_a, surf_a, xdg_a, _, _) = open_and_configure_client(comp, socket_path, "Notes", "notes")
commit_shm_frame(comp, a, shm_a, surf_a, xdg_a, 1UL)
let (ti, _) = text_input_for(comp, a)
text_input_state(comp, a, ti, true, 50, 50, 16)
assert_true(comp.osk_is_visible())
// Hide button hides the keyboard.
let (hx, hy) = comp.osk_key_center("Hide").unwrap()
click_at(comp, hx, hy, 20U)
assert_false(comp.osk_is_visible())
// Tapping the compact "KB" toggle pill reopens it.
let tr = comp.osk_toggle_rect()
click_at(comp, tr.x + tr.w / 2, tr.y + tr.h / 2, 30U)
assert_true(comp.osk_is_visible())
// Hide again, then tap inside the client window with active text input -> reopens.
let (hx2, hy2) = comp.osk_key_center("Hide").unwrap()
click_at(comp, hx2, hy2, 40U)
assert_false(comp.osk_is_visible())
let g = comp.windows.get(1UL).unwrap().geometry
click_at(comp, g.x + g.w / 2, g.y + g.h / 2, 50U)
assert_true(comp.osk_is_visible())
comp.close()
}
///|
test "OSK auto-opens on launcher popup, lifts popup above OSK, and types into search" {
let comp = osk_comp("launcher")
let (lx, ly) = center_of(comp, "launcher")
click_at(comp, lx.to_int(), ly.to_int(), 10U)
assert_true(comp.shell.popup_open())
assert_true(comp.osk_is_visible())
// Launcher popup sits entirely above the OSK.
let pr = comp.shell.open_popup_rect().unwrap()
assert_true(pr.y + pr.h <= comp.osk_rect().y)
// Tapping a letter key updates the launcher search query.
let (tx, ty) = comp.osk_key_center("t").unwrap()
click_at(comp, tx, ty, 20U)
assert_eq(comp.shell.launcher_state.query, "t")
comp.close()
}
+24
View File
@@ -217,6 +217,30 @@ fn ChandraCompositor::freeze_screen(self : ChandraCompositor) -> @renderer.Pixma
}
}
///| Capture the current screen directly to PNG bytes without opening the editor.
pub fn ChandraCompositor::capture_png(self : ChandraCompositor) -> Bytes? {
let full = @scene.Rect::new(0, 0, self.output_w, self.output_h)
match self.frame_shots {
Some(host) =>
if (host.snapshot)() {
let png = (host.png)(full)
if png.length() > 0 { Some(png) } else { None }
} else {
None
}
None =>
match self.grab_screen() {
Some(frame) => {
for i = 3; i < frame.data.length(); i = i + 4 {
frame.data[i] = b'\xff'
}
Some(@renderer.encode_png_compressed(frame))
}
None => None
}
}
}
///| A window's rectangle on screen with its title bar and borders (Spectacle's default).
fn ChandraCompositor::outer_rect(self : ChandraCompositor, wid : UInt64) -> @scene.Rect? {
match self.windows.get(wid) {