CI / test (push) Failing after 49s
- Drop text-input and keyboard focus references when a client destroys a wl_surface, without sending leave for it - Skip wl_keyboard.leave and zwp_text_input_v3.leave/enter for surfaces the client no longer has - Skip pointer enter/leave for missing subsurfaces or toplevels - Add a regression test: closing a second toplevel (e.g. OAuth popup) must return focus to the main window without leaving stale events
218 lines
6.8 KiB
MoonBit
218 lines
6.8 KiB
MoonBit
///| text-input-v3 (zwp_text_input_v3): how Chandra types into apps, the way an input method does.
|
|
///
|
|
/// An app with a text field focused enables its text input; the compositor can then send it text
|
|
/// (commit_string) that the app inserts at its caret, whatever the keyboard layout. The emoji
|
|
/// picker uses it (compositor/host/emoji_picker.mbt). GTK 3 and 4, Qt 6, Firefox, foot and
|
|
/// Ghostty speak it; Chromium only with --enable-wayland-ime.
|
|
///
|
|
/// The protocol follows keyboard focus: a text input gets `enter` with the focused surface when
|
|
/// its client has the keyboard (or at once when created while it does) and `leave` when it goes.
|
|
/// enable / disable / set_cursor_rectangle are double-buffered and take effect on commit; `done`
|
|
/// carries how many commits the client has made, as the protocol asks.
|
|
|
|
///| One zwp_text_input_v3.
|
|
struct TextInputState {
|
|
/// The surface it was sent `enter` for (0: none).
|
|
mut surface : UInt
|
|
mut enabled : Bool
|
|
mut pend_enabled : Bool
|
|
/// The caret, surface-local (x, y, w, h); None until the client says.
|
|
mut cursor : (Int, Int, Int, Int)?
|
|
mut pend_cursor : (Int, Int, Int, Int)?
|
|
/// commit requests received (the serial `done` carries).
|
|
mut commits : UInt
|
|
}
|
|
|
|
///| Requests on the manager and on text inputs.
|
|
fn WaylandServer::text_input_request(
|
|
self : WaylandServer,
|
|
c : ClientConn,
|
|
kind : ObjKind,
|
|
obj_id : UInt,
|
|
opcode : UInt,
|
|
reader : WireReader,
|
|
) -> Unit {
|
|
match kind {
|
|
ObjTextInputMgr =>
|
|
if opcode == 0U {
|
|
// destroy
|
|
c.objects.remove(obj_id)
|
|
send_delete_id(c, obj_id)
|
|
} else if opcode == 1U {
|
|
// get_text_input(id: new_id, seat)
|
|
let id = reader.u32()
|
|
c.objects.set(id, ObjTextInput)
|
|
let ti : TextInputState = {
|
|
surface: 0U,
|
|
enabled: false,
|
|
pend_enabled: false,
|
|
cursor: None,
|
|
pend_cursor: None,
|
|
commits: 0U,
|
|
}
|
|
c.text_inputs.set(id, ti)
|
|
match self.kbd_surface {
|
|
Some((cid, sid)) if cid == c.client_id && c.surfaces.contains(sid) => {
|
|
ti.surface = sid
|
|
send_text_input_surface(c, id, 0U, sid)
|
|
}
|
|
_ => ()
|
|
}
|
|
}
|
|
ObjTextInput =>
|
|
match c.text_inputs.get(obj_id) {
|
|
None => ()
|
|
Some(ti) =>
|
|
match opcode {
|
|
0U => {
|
|
// destroy
|
|
c.text_inputs.remove(obj_id)
|
|
c.objects.remove(obj_id)
|
|
send_delete_id(c, obj_id)
|
|
}
|
|
1U => {
|
|
// enable: also resets the pending state
|
|
ti.pend_enabled = true
|
|
ti.pend_cursor = None
|
|
}
|
|
2U => ti.pend_enabled = false // disable
|
|
6U => {
|
|
// set_cursor_rectangle(x, y, width, height)
|
|
let x = reader.i32()
|
|
let y = reader.i32()
|
|
let w = reader.i32()
|
|
let h = reader.i32()
|
|
ti.pend_cursor = Some((x, y, w, h))
|
|
}
|
|
7U => {
|
|
// commit
|
|
ti.commits = ti.commits + 1U
|
|
ti.enabled = ti.pend_enabled
|
|
match ti.pend_cursor {
|
|
Some(r) => ti.cursor = Some(r)
|
|
None => if !ti.enabled { ti.cursor = None }
|
|
}
|
|
}
|
|
// set_surrounding_text, set_text_change_cause, set_content_type: not needed to
|
|
// insert text.
|
|
_ => ()
|
|
}
|
|
}
|
|
_ => ()
|
|
}
|
|
}
|
|
|
|
///| enter (opcode 0) or leave (1) for `surface`.
|
|
fn send_text_input_surface(c : ClientConn, id : UInt, opcode : UInt, surface : UInt) -> Unit {
|
|
let w = WireWriter::new()
|
|
w.u32(surface)
|
|
c.send_msg(w.finish_msg(id, opcode))
|
|
}
|
|
|
|
///| The client's keyboard focus moved to `surface`: tell its text inputs.
|
|
fn text_inputs_enter(c : ClientConn, surface : UInt) -> Unit {
|
|
for id, ti in c.text_inputs {
|
|
if ti.surface != 0U && ti.surface != surface {
|
|
if c.surfaces.contains(ti.surface) {
|
|
send_text_input_surface(c, id, 1U, ti.surface)
|
|
}
|
|
ti.enabled = false
|
|
ti.cursor = None
|
|
}
|
|
if ti.surface != surface {
|
|
if c.surfaces.contains(surface) {
|
|
ti.surface = surface
|
|
send_text_input_surface(c, id, 0U, surface)
|
|
} else {
|
|
ti.surface = 0U
|
|
ti.enabled = false
|
|
ti.cursor = None
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
///| The client lost keyboard focus on `surface`.
|
|
fn text_inputs_leave(c : ClientConn, surface : UInt) -> Unit {
|
|
for id, ti in c.text_inputs {
|
|
if ti.surface == surface {
|
|
ti.surface = 0U
|
|
// The client disables it in answer; until it commits that, it can't take text anyway.
|
|
ti.enabled = false
|
|
ti.cursor = None
|
|
if c.surfaces.contains(surface) {
|
|
send_text_input_surface(c, id, 1U, surface)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
///| A wl_surface was destroyed by the client: drop text-input references without sending `leave`.
|
|
fn text_inputs_surface_gone(c : ClientConn, surface : UInt) -> Unit {
|
|
for _, ti in c.text_inputs {
|
|
if ti.surface == surface {
|
|
ti.surface = 0U
|
|
ti.enabled = false
|
|
ti.cursor = None
|
|
}
|
|
}
|
|
}
|
|
|
|
///| The enabled text inputs on window `win_id`'s surface: (client, ids).
|
|
fn WaylandServer::active_text_inputs(self : WaylandServer, win_id : UInt64) -> (ClientConn, Array[UInt])? {
|
|
match self.win_client(win_id) {
|
|
None => None
|
|
Some((c, tl)) => {
|
|
let ids : Array[UInt] = []
|
|
for id, ti in c.text_inputs {
|
|
if ti.enabled && ti.surface == tl.wl_surface_id {
|
|
ids.push(id)
|
|
}
|
|
}
|
|
if ids.length() == 0 {
|
|
None
|
|
} else {
|
|
Some((c, ids))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
///| Whether window `win_id` has a text field taking text right now (an enabled text input on its
|
|
/// focused surface).
|
|
pub fn WaylandServer::text_input_active(self : WaylandServer, win_id : UInt64) -> Bool {
|
|
self.active_text_inputs(win_id) is Some(_)
|
|
}
|
|
|
|
///| The caret of window `win_id`'s active text input, relative to its surface: (x, y, w, h).
|
|
pub fn WaylandServer::text_input_cursor(self : WaylandServer, win_id : UInt64) -> (Int, Int, Int, Int)? {
|
|
match self.active_text_inputs(win_id) {
|
|
Some((c, ids)) =>
|
|
match c.text_inputs.get(ids[0]) {
|
|
Some(ti) => ti.cursor
|
|
None => None
|
|
}
|
|
None => None
|
|
}
|
|
}
|
|
|
|
///| Type `text` into window `win_id`'s focused text field (commit_string + done). False when it
|
|
/// has no enabled text input: nothing was sent.
|
|
pub fn WaylandServer::text_input_commit(self : WaylandServer, win_id : UInt64, text : String) -> Bool {
|
|
match self.active_text_inputs(win_id) {
|
|
None => false
|
|
Some((c, ids)) => {
|
|
for id in ids {
|
|
let ti = c.text_inputs.get(id).unwrap()
|
|
let w = WireWriter::new()
|
|
w.str(text)
|
|
c.send_msg(w.finish_msg(id, 3U)) // commit_string
|
|
let d = WireWriter::new()
|
|
d.u32(ti.commits)
|
|
c.send_msg(d.finish_msg(id, 5U)) // done
|
|
}
|
|
true
|
|
}
|
|
}
|
|
}
|