diff --git a/README.md b/README.md index 97fdee1..2b6d4be 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ Cthulhu is a Linux screen reader written in Go. - [Progress reporting](PROGRESS_BARS.md): web progress bars, Steam shader processing, and verification. - [Steam keyboard focus](STEAM_FOCUS.md): Xorg workspace focus recovery and verification. - [XTerm handoff](docs/testing/xterm-handoff.md): terminal reader keys, lock state, speech interruption, and verification. +- [Graphical terminals](docs/testing/graphical-terminals.md): repaint coalescing, editing feedback, title animation, and verification. - [Messaging support](docs/testing/messaging.md): Pidgin message review/copy shortcuts, application boundaries, and verification. - [Flatpak foreground accessibility](docs/testing/flatpak-foreground.md): process identity and proxy matching, tests, and live acceptance. - [Background event filtering](docs/testing/background-events.md): early foreground checks, notification exceptions, and performance verification. diff --git a/docs/testing/graphical-terminals.md b/docs/testing/graphical-terminals.md new file mode 100644 index 0000000..6449356 --- /dev/null +++ b/docs/testing/graphical-terminals.md @@ -0,0 +1,58 @@ +# Graphical terminal regression checks + +Terminal scrollback removal stays silent. In-place delete/insert repaints of +the same region coalesce to the latest insertion for up to 500 milliseconds; +continuous output retains that first deadline. Different terminals and regions +keep their own feedback, and appended output flushes an earlier held repaint +in order. Short updates such as `OK` and counts still speak. Backspace and +Delete feedback on the caret line remains available. Moving focus to another +control cancels the held repaint. + +Typing in a focused terminal can establish flat-review focus if the reader +missed its initial focus event. A different control's stale focused bit must +not replace known review focus. + +Animated window-title frames share a normalized repeat key for three seconds +after their last occurrence. History belongs to that window and survives a +workspace round trip; another window's identical title can still announce. +Explicit title reading remains available. See [title checks](../../tests/window_titles.md). + +Run from the repository root: + +```sh +go test ./internal/app -run 'Terminal|Title|Caret' -count=1 +go build -o /tmp/cthulhu-terminal-review ./cmd/cthulhu +CTHULHU_BINARY=/tmp/cthulhu-terminal-review python3 tests/native_terminal.py +``` + +The native fixture uses a private X11 display, accessibility bus, shell and +speech protocol recorder. It checks scrolling, command output, Backspace, +Ctrl+W, Delete, history and flat review without replacing the desktop reader. + +For live acceptance with a rebuilt reader: + +1. In a VTE terminal, run commands that scroll past the visible rows. Confirm + each command's output speaks without replaying removed prompts. +2. Type text, use Backspace, Ctrl+W, Left and Delete, then recall command + history. Check speech and Braille against the edited line. +3. Read current and previous lines with flat review. Repeat after starting + the reader while that terminal already has focus, without switching away. +4. Use a TUI that continuously rewrites a status region. Confirm its latest + status speaks during the activity, including short counts or `OK`. Check + separate regions, appended output, and two terminal panes. Move focus to + another control and confirm an old repaint does not follow it. +5. Check animated titles, meaningful title changes, workspace round trips, + and two windows with identical titles. Check explicit title reading and + disabled automatic title changes. Confirm XTerm still delegates input to + the terminal reader and desktop controls resume normally on return. + +Wayland Shift+digit heading navigation uses the default libxkbcommon keymap +(`XKB_DEFAULT_LAYOUT`, variant and options). These must match the compositor; +KeyboardMonitor does not expose its active keymap or group. Ambiguous shifted +symbols in multiple layouts are not registered as additional grabs. Verify +Shift+1 through Shift+6, Alt+Shift+digit heading lists, plain typing, learn mode, +and releasing Shift before the digit on the actual compositor. Private D-Bus +tests establish protocol dispatch, not physical key suppression. + +Automated checks do not establish installed-reader, audible speech, physical +keyboard or physical Braille acceptance. diff --git a/internal/app/app.go b/internal/app/app.go index fdb6f7a..a1ec161 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -137,6 +137,7 @@ type InputListener interface { const ( caretDebounce = 150 * time.Millisecond terminalMutationDebounce = 50 * time.Millisecond + terminalRepaintDebounce = 500 * time.Millisecond objectReviewCaptureTimeout = 2 * time.Second flatReviewCaptureTimeout = 10 * time.Second documentCaptureTimeout = 15 * time.Second diff --git a/internal/app/current_line.go b/internal/app/current_line.go index 335e5af..e850148 100644 --- a/internal/app/current_line.go +++ b/internal/app/current_line.go @@ -19,6 +19,7 @@ func (handler *eventReviewHandler) current_line(command input.Command) { } handler.state.pending.terminalFocus.clear() handler.state.pending.terminalDeletion.clear() + handler.state.pending.terminalRepaint.clear() style := review.FlatSpeak if command == input.SpellCurrentLine { style = review.FlatSpell diff --git a/internal/app/event_accessibility.go b/internal/app/event_accessibility.go index 175b557..9314238 100644 --- a/internal/app/event_accessibility.go +++ b/internal/app/event_accessibility.go @@ -254,15 +254,41 @@ func (handler *accessibilityEventHandler) handle(event atspi.Event) { if autocompleteConsumed { return } + flushTerminalRepaint := func() { + if state.pending.terminalRepaint.event.Type == "" { + return + } + pending, _ := state.pending.terminalRepaint.take() + if is_trivial_terminal_repaint(pending) { + logger.Debug("dropped trivial terminal repaint fragment", "role", pending.Source.Role) + return + } + speak(pending) + } + replacedDeletion := false if state.pending.terminalDeletion.event.Type != "" && event.Type != atspi.CaretEvent { - isReplacement := event.Type == atspi.TextInsertedEvent && + pendingDeletion := state.pending.terminalDeletion.event + // A user edit at the caret is confirmation speech, not repaint + // churn: never coalesce it away or drop it behind a following + // deletion. It flushes like any other pending deletion. + userDeletion := state.pending.terminalDeletion.intent == input.DeleteIntent + sameTerminal := event.Source.Role == "terminal" && event.Source.Path != "" && - event.Source.Path == state.pending.terminalDeletion.event.Source.Path && - event.Detail1 == state.pending.terminalDeletion.event.Detail1 + event.Source.Path == pendingDeletion.Source.Path && + event.Source.BusName == pendingDeletion.Source.BusName + isReplacement := event.Type == atspi.TextInsertedEvent && sameTerminal && !userDeletion && + terminalReplacementOverlap(pendingDeletion, event) if isReplacement { + replacedDeletion = true state.pending.terminalDeletion.clear() logger.Debug("coalesced terminal deletion with following insertion", "role", event.Source.Role) - } else { + } else if !(event.Type == atspi.TextDeletedEvent && sameTerminal && !userDeletion && + (should_ignore_terminal_scrollback(event) || terminalReplacementOverlap(pendingDeletion, event))) { + // An overlapping deletion from the same terminal is part of the same + // repaint burst. It is handled below: scroll shifts return + // early, anything else replaces the pending deletion. Either + // way the pending deletion must not flush early, or the burst + // speaks the text it is about to rewrite. flushTerminalDeletion() } } @@ -294,13 +320,45 @@ func (handler *accessibilityEventHandler) handle(event atspi.Event) { return } if event.Type == atspi.TextDeletedEvent && event.Source.Role == "terminal" { + if state.pending.terminalRepaint.event.Type != "" && + !should_ignore_terminal_scrollback(event) && + (is_user_terminal_deletion(event, intent) || !terminalReplacementOverlap(state.pending.terminalRepaint.event, event)) { + flushTerminalRepaint() + } if should_ignore_terminal_scrollback(event) { logger.Debug("skipped terminal scrollback deletion", "role", event.Source.Role) return } - state.pending.terminalDeletion.schedule(event, input.NoIntent, terminalMutationDebounce) + deletionIntent := input.NoIntent + if is_user_terminal_deletion(event, intent) { + deletionIntent = input.DeleteIntent + } + state.pending.terminalDeletion.schedule(event, deletionIntent, terminalMutationDebounce) return } + // Latest wins for in-place terminal repaints: hold the insertion + // briefly so a newer rewrite of the same region replaces it before + // either speaks. A rewrite of a different region, or appended output + // from the same terminal, first flushes the held repaint in order. + if is_latest_wins_terminal_repaint(event, intent, replacedDeletion) { + if state.pending.terminalRepaint.event.Type != "" && + !terminalReplacementOverlap(state.pending.terminalRepaint.event, event) { + flushTerminalRepaint() + } + state.pending.terminalRepaint.coalesce_terminal_repaint(event) + logger.Debug("queued latest-wins terminal repaint", "role", event.Source.Role) + if autocompleteRequest.Text != "" { + speech.Enqueue(autocompleteRequest) + } + return + } + if event.Type == atspi.TextInsertedEvent && event.Source.Role == "terminal" && + state.pending.terminalRepaint.event.Type != "" && + event.Source.Path != "" && + event.Source.Path == state.pending.terminalRepaint.event.Source.Path && + event.Source.BusName == state.pending.terminalRepaint.event.Source.BusName { + flushTerminalRepaint() + } speak(event) if autocompleteRequest.Text != "" { speech.Enqueue(autocompleteRequest) diff --git a/internal/app/event_caret.go b/internal/app/event_caret.go index a98fbf8..a6454c1 100644 --- a/internal/app/event_caret.go +++ b/internal/app/event_caret.go @@ -21,6 +21,23 @@ func (handler *accessibilityEventHandler) handleCaret(event atspi.Event, activit objectReview := handler.objectReview debounce := handler.debounce if event.Type == atspi.CaretEvent { + // A reader started while a terminal already has focus may never + // receive a focus-gain event for it. Caret movement caused by the + // user's own typing on the focused terminal is enough to anchor + // flat review; without this, review stays focusless until a + // workspace switch replays focus. Restricting this to typing and + // delete input keeps a background terminal's own output churn + // from claiming review focus. + if objectReview != nil && event.Source.Role == "terminal" && event.Source.Focused && + (!state.focusedID.Valid() || state.focusedID == event.Source.ID()) && + (intent == input.TypingIntent || intent == input.DeleteIntent) { + objectReview.SyncFocus(event.Source.ID()) + if !state.focusedID.Valid() { + state.owner.follow(event) + state.focusedID = event.Source.ID() + state.current = event.Source + } + } if event.Source.ReadOnlyCombo.Valid() && event.Source.ReadOnlyCombo == state.focusedID && state.current.ID() == state.focusedID && state.current.Role == "combo box" { // The combo's name/value event presents this selection, even when diff --git a/internal/app/event_caret_test.go b/internal/app/event_caret_test.go index 0425192..ba44643 100644 --- a/internal/app/event_caret_test.go +++ b/internal/app/event_caret_test.go @@ -26,6 +26,7 @@ import ( "git.stormux.org/storm/cthulhu/internal/atspi" "git.stormux.org/storm/cthulhu/internal/input" + "git.stormux.org/storm/cthulhu/internal/review" ) func TestProcessEventsSpeaksChangedCaretLines(t *testing.T) { @@ -156,6 +157,45 @@ func TestReadOnlyComboCaretRequiresExactOwnedCombo(t *testing.T) { } } +func TestTerminalCaretSeedsReviewFocusWithoutFocusEvent(t *testing.T) { + // A reader started while the terminal already has focus may never see + // a focus-gain event. Caret movement from the focused terminal is the + // evidence flat review needs to anchor to it. + worker, err := review.NewWorker(&documentLineCapturer{calls: make(chan documentLineCaptureCall, 1)}, time.Second) + if err != nil { + t.Fatal(err) + } + state := &eventLoopState{} + defer state.pending.clear() + handler := accessibilityEventHandler{state: state, logger: slog.New(slog.DiscardHandler), objectReview: worker, debounce: time.Hour} + terminal := atspi.Snapshot{BusName: ":1.9", Path: "/terminal", Role: "terminal", Focused: true} + // Output churn without user typing must not claim review focus. + handler.handleCaret(atspi.NewEvent(atspi.CaretEvent, terminal), input.Activity{}, input.NoIntent) + if got := worker.Focus(); got.Valid() { + t.Fatalf("review focus seeded without typing = %+v", got) + } + handler.handleCaret(atspi.NewEvent(atspi.CaretEvent, terminal), input.Activity{}, input.TypingIntent) + if got := worker.Focus(); got != terminal.ID() { + t.Fatalf("review focus = %+v, want terminal %+v", got, terminal.ID()) + } + if state.focusedID != terminal.ID() || state.current.ID() != terminal.ID() { + t.Fatalf("focused state = %+v/%+v, want terminal", state.focusedID, state.current.ID()) + } + // An unfocused terminal caret must not steal review focus. + other := atspi.Snapshot{BusName: ":1.9", Path: "/other-terminal", Role: "terminal"} + handler.handleCaret(atspi.NewEvent(atspi.CaretEvent, other), input.Activity{}, input.TypingIntent) + if got := worker.Focus(); got != terminal.ID() { + t.Fatalf("review focus after unfocused caret = %+v, want %+v", got, terminal.ID()) + } + // Another widget in the same application can retain a stale focused + // bit. Its caret must not move review away from the known focus. + other.Focused = true + handler.handleCaret(atspi.NewEvent(atspi.CaretEvent, other), input.Activity{}, input.TypingIntent) + if got := worker.Focus(); got != state.focusedID { + t.Fatalf("stale focused bit split review and input ownership: %+v != %+v", got, state.focusedID) + } +} + func TestProcessEventsSpeaksCaretCharacters(t *testing.T) { speech := &fakeSpeechQueue{spoken: make(chan string, 3)} adapter := &fakeAdapter{events: make(chan atspi.Event, 3)} diff --git a/internal/app/event_focus.go b/internal/app/event_focus.go index e166d3b..40e792a 100644 --- a/internal/app/event_focus.go +++ b/internal/app/event_focus.go @@ -47,6 +47,9 @@ func (handler *accessibilityEventHandler) acceptFocusedEvent(event atspi.Event, state.pending.browserButtonName.clear() } focusID := event.Source.ID() + if isFocusGainEvent(event) && state.pending.terminalRepaint.event.Source.ID() != focusID { + state.pending.terminalRepaint.clear() + } if isFocusGainEvent(event) { state.focusCaretID = focusID state.focusCaretSequence = activity.Sequence diff --git a/internal/app/event_loop.go b/internal/app/event_loop.go index 84df1d6..240f310 100644 --- a/internal/app/event_loop.go +++ b/internal/app/event_loop.go @@ -333,6 +333,17 @@ func processEventsWithAttributes( } event, _ := state.pending.terminalDeletion.take() speak(event) + case <-state.pending.terminalRepaint.ready: + if focusGate.Locked() { + state.pending.clear() + continue + } + event, _ := state.pending.terminalRepaint.take() + if is_trivial_terminal_repaint(event) { + logger.Debug("dropped trivial terminal repaint fragment", "role", event.Source.Role) + continue + } + speak(event) case command := <-reviewRequests: switch command { case input.PresentCurrentLine, input.SpellCurrentLine, input.PhoneticCurrentLine: diff --git a/internal/app/event_review_results.go b/internal/app/event_review_results.go index 878c057..87c6524 100644 --- a/internal/app/event_review_results.go +++ b/internal/app/event_review_results.go @@ -41,6 +41,7 @@ func (handler *eventReviewHandler) whereAmI() { state := handler.state state.pending.terminalFocus.clear() state.pending.terminalDeletion.clear() + state.pending.terminalRepaint.clear() // Python's Where Am I is speech-only. Do not suppress the role merely // because ordinary focus presentation replaces it with a sound cue. reviewOptions := state.presentationOptions @@ -209,6 +210,7 @@ func (handler *eventReviewHandler) handle(result review.Result) { } state.pending.terminalFocus.clear() state.pending.terminalDeletion.clear() + state.pending.terminalRepaint.clear() presentFlatReviewResult(logger, speech, result) if reservedWineID(result.FlatFocus) && brailleOutput != nil && result.Err == nil && (result.Moved || result.FlatStep == 0 && result.FlatUnit != review.WordAbove && result.FlatUnit != review.WordBelow) { diff --git a/internal/app/pending_presentation.go b/internal/app/pending_presentation.go index ba69b71..8efdc21 100644 --- a/internal/app/pending_presentation.go +++ b/internal/app/pending_presentation.go @@ -48,6 +48,7 @@ type pendingPresentations struct { caret pendingPresentation terminalFocus pendingPresentation terminalDeletion pendingPresentation + terminalRepaint pendingPresentation browserButtonName pendingBrowserButtonName radioSound pendingRadioSound } @@ -56,6 +57,7 @@ func (pending *pendingPresentations) clear() { pending.caret.clear() pending.terminalFocus.clear() pending.terminalDeletion.clear() + pending.terminalRepaint.clear() pending.browserButtonName.clear() pending.radioSound.clear() } diff --git a/internal/app/terminal_events.go b/internal/app/terminal_events.go index 8987d9b..da7bb00 100644 --- a/internal/app/terminal_events.go +++ b/internal/app/terminal_events.go @@ -5,15 +5,84 @@ package app -import "git.stormux.org/storm/cthulhu/internal/atspi" +import ( + "strings" + + "git.stormux.org/storm/cthulhu/internal/atspi" + "git.stormux.org/storm/cthulhu/internal/input" +) func should_ignore_terminal_scrollback(event atspi.Event) bool { - // VTE can remove old complete lines at the start of its accessible text - // while appending output at a different offset. These are not edits to - // the prompt. This event pattern occurs on both X11 and Wayland. + // VTE can remove old lines at the start of its accessible text while + // appending output at a different offset. A deletion entirely before + // the caret line is not an edit to the prompt. VTE wrapped rows have + // no trailing newline, so do not require one. This event pattern + // occurs on both X11 and Wayland. return event.Type == atspi.TextDeletedEvent && event.Source.Role == "terminal" && event.Detail1 == 0 && event.Source.TextCaretKnown && event.Source.BrailleLineStart > 0 && - event.TextChangeEndsLine && event.TextChangeLength > 0 && + event.TextChangeLength > 0 && event.TextChangeLength <= event.Source.BrailleLineStart } + +// is_user_terminal_deletion identifies a deletion the user just caused at +// the caret on its own line: a Backspace or Delete edit. The deleted text +// is the user's confirmation that the edit took, so repaint coalescing +// must not swallow it. Program repaints start above the caret line, and +// deletions without a preceding delete key are program output churn. +func is_user_terminal_deletion(event atspi.Event, intent input.Intent) bool { + return intent == input.DeleteIntent && + event.Type == atspi.TextDeletedEvent && event.Source.Role == "terminal" && + event.Source.TextCaretKnown && + event.Detail1 >= event.Source.BrailleLineStart && + event.Detail1 <= event.Source.BrailleCaret +} + +// is_latest_wins_terminal_repaint identifies an in-place rewrite: an +// insertion that replaced a just-deleted range. Status regions update +// this way several times a second and only the newest state is worth +// hearing. Editing feedback on the caret line after a typing or delete +// key stays immediate; appended output has no preceding deletion and +// never qualifies. +func is_latest_wins_terminal_repaint(event atspi.Event, intent input.Intent, replacedDeletion bool) bool { + if !replacedDeletion || event.Type != atspi.TextInsertedEvent || event.Source.Role != "terminal" { + return false + } + if (intent == input.TypingIntent || intent == input.DeleteIntent) && + event.Source.TextCaretKnown && event.Detail1 >= event.Source.BrailleLineStart { + return false + } + return true +} + +// Blank repaints carry no speech. Short nonblank updates can still be +// meaningful ("OK", a count, or one Unicode character) and must survive. +func is_trivial_terminal_repaint(event atspi.Event) bool { + return event.Type == atspi.TextInsertedEvent && event.Source.Role == "terminal" && + strings.TrimSpace(event.Payload) == "" +} + +// terminalReplacementOverlap reports whether an insertion rewrites any of +// the range a pending deletion removed. Offsets are not compared exactly: +// a scroll shift at the start of the buffer between the deletion and its +// replacement insertion moves the insertion's offset, and a repainted +// region is routinely rewritten at a neighboring offset. +func terminalReplacementOverlap(deletion, insertion atspi.Event) bool { + if deletion.Source.Role != "terminal" || insertion.Source.Role != "terminal" || + deletion.Source.Path == "" || deletion.Source.ID() != insertion.Source.ID() { + return false + } + deletionEnd := deletion.Detail1 + max(deletion.TextChangeLength, 1) + insertionEnd := insertion.Detail1 + max(insertion.TextChangeLength, 1) + return insertion.Detail1 < deletionEnd && deletion.Detail1 < insertionEnd +} + +// Preserve the first deadline so a continuously redrawn status still speaks +// its latest value. The event loop owns both the event and the timer. +func (pending *pendingPresentation) coalesce_terminal_repaint(event atspi.Event) { + if pending.event.Type == "" { + pending.schedule(event, input.NoIntent, terminalRepaintDebounce) + return + } + pending.event = event +} diff --git a/internal/app/terminal_events_test.go b/internal/app/terminal_events_test.go index 20df10e..8e6bc6f 100644 --- a/internal/app/terminal_events_test.go +++ b/internal/app/terminal_events_test.go @@ -25,6 +25,7 @@ import ( "time" "git.stormux.org/storm/cthulhu/internal/atspi" + "git.stormux.org/storm/cthulhu/internal/input" ) func TestProcessEventsTerminalScrollbackDeletionBothSessions(t *testing.T) { @@ -111,16 +112,22 @@ func TestTerminalScrollbackDeletionPreservesEditing(t *testing.T) { {"backspace", func(event *atspi.Event) { event.Payload = "x"; event.Detail1 = 109 }, false}, {"delete", func(event *atspi.Event) { event.Payload = "x"; event.Detail1 = 110 }, false}, {"delete word", func(event *atspi.Event) { event.Payload = "command"; event.Detail1 = 100 }, false}, - {"first character", func(event *atspi.Event) { + {"single character at start", func(event *atspi.Event) { + // The caret line starts at 100, so a deletion at offset 0 is a + // wrap/scroll shift, not an edit on the caret line. A real + // first-line backspace has a line start of 0 (see above). *event = atspi.NewEventWithPayload(atspi.TextDeletedEvent, terminal, "x") - }, false}, + }, true}, {"newline", func(event *atspi.Event) { event.Payload = "\n"; event.Detail1 = 100 }, false}, {"current first line", func(event *atspi.Event) { event.Source.BrailleLineStart = 0 }, false}, {"range reaches current line", func(event *atspi.Event) { event.Source.BrailleLineStart = 5 }, false}, {"unknown caret", func(event *atspi.Event) { event.Source.TextCaretKnown = false }, false}, {"partial line", func(event *atspi.Event) { + // VTE wrapped rows have no trailing newline, so a scrollback + // deletion entirely before the caret line is still a scroll, + // never an edit on the caret line. *event = atspi.NewEventWithPayload(atspi.TextDeletedEvent, terminal, "Old prompt") - }, false}, + }, true}, {"editor", func(event *atspi.Event) { event.Source.Role = "text" }, false}, {"insertion", func(event *atspi.Event) { event.Type = atspi.TextInsertedEvent }, false}, } { @@ -179,6 +186,262 @@ func TestProcessEventsCoalescesTerminalDeletionWithReplacementInsertion(t *testi } } +func TestProcessEventsCoalescesTerminalRepaintAcrossScrollShift(t *testing.T) { + speech := &fakeSpeechQueue{spoken: make(chan string, 3)} + adapter := &fakeAdapter{events: make(chan atspi.Event, 3)} + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + completed := make(chan struct{}) + go func() { + processEvents(ctx, slog.New(slog.DiscardHandler), speech, adapter, 0) + close(completed) + }() + + // A TUI repaints its live region: it deletes the old region, a + // one-character scroll shift at the start of the buffer moves every + // later offset back by one, then it inserts the new region. Only the + // insertion may speak. + terminal := atspi.NewSnapshot("/accessible/terminal", "", "terminal", "", "") + terminal.TextCaretKnown = true + terminal.BrailleLineStart = 190 + terminal.BrailleCaret = 195 + repaint := atspi.NewEventWithPayload(atspi.TextDeletedEvent, terminal, strings.Repeat("x", 50)) + repaint.Detail1 = 100 + shift := atspi.NewEventWithPayload(atspi.TextDeletedEvent, terminal, " ") + replacement := atspi.NewEventWithPayload(atspi.TextInsertedEvent, terminal, "new region text") + replacement.Detail1 = 99 + adapter.events <- repaint + adapter.events <- shift + adapter.events <- replacement + + select { + case text := <-speech.spoken: + if text != "new region text" { + t.Fatalf("spoken text = %q, want only the replacement insertion", text) + } + case <-time.After(time.Second): + t.Fatal("timed out waiting for replacement insertion") + } + select { + case text := <-speech.spoken: + t.Fatalf("terminal repaint deletion was presented as %q", text) + case <-time.After(2 * terminalMutationDebounce): + } + + cancel() + select { + case <-completed: + case <-time.After(time.Second): + t.Fatal("event loop did not stop") + } +} + +func repaintSequence(adapter *fakeAdapter, terminal atspi.Snapshot, detail1 int, texts ...string) { + for _, text := range texts { + deletion := atspi.NewEventWithPayload(atspi.TextDeletedEvent, terminal, strings.Repeat("x", len(text))) + deletion.Detail1 = detail1 + insertion := atspi.NewEventWithPayload(atspi.TextInsertedEvent, terminal, text) + insertion.Detail1 = detail1 + adapter.events <- deletion + adapter.events <- insertion + } +} + +func TestProcessEventsLatestTerminalRepaintWins(t *testing.T) { + speech := &fakeSpeechQueue{spoken: make(chan string, 4)} + adapter := &fakeAdapter{events: make(chan atspi.Event, 8)} + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + completed := make(chan struct{}) + go func() { + processEvents(ctx, slog.New(slog.DiscardHandler), speech, adapter, 0) + close(completed) + }() + + terminal := atspi.NewSnapshot("/accessible/terminal", "", "terminal", "", "") + terminal.TextCaretKnown = true + terminal.BrailleLineStart = 200 + terminal.BrailleCaret = 205 + repaintSequence(adapter, terminal, 100, "Thinking (1s)", "Thinking (2s)", "Thinking (3s)") + + select { + case text := <-speech.spoken: + if text != "Thinking (3s)" { + t.Fatalf("spoken text = %q, want only the latest repaint", text) + } + case <-time.After(3 * time.Second): + t.Fatal("latest repaint was not presented") + } + select { + case text := <-speech.spoken: + t.Fatalf("superseded repaint was presented as %q", text) + case <-time.After(terminalRepaintDebounce): + } + + cancel() + select { + case <-completed: + case <-time.After(time.Second): + t.Fatal("event loop did not stop") + } +} + +func TestProcessEventsTerminalRepaintsInDifferentRegionsBothSpeak(t *testing.T) { + speech := &fakeSpeechQueue{spoken: make(chan string, 4)} + adapter := &fakeAdapter{events: make(chan atspi.Event, 8)} + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + completed := make(chan struct{}) + go func() { + processEvents(ctx, slog.New(slog.DiscardHandler), speech, adapter, 0) + close(completed) + }() + + terminal := atspi.NewSnapshot("/accessible/terminal", "", "terminal", "", "") + terminal.TextCaretKnown = true + terminal.BrailleLineStart = 400 + terminal.BrailleCaret = 405 + repaintSequence(adapter, terminal, 100, "First region") + repaintSequence(adapter, terminal, 300, "Second region") + + for _, expected := range []string{"First region", "Second region"} { + select { + case text := <-speech.spoken: + if text != expected { + t.Fatalf("spoken text = %q, want %q", text, expected) + } + case <-time.After(3 * time.Second): + t.Fatalf("timed out waiting for %q", expected) + } + } + + cancel() + select { + case <-completed: + case <-time.After(time.Second): + t.Fatal("event loop did not stop") + } +} + +func TestProcessEventsPreservesShortTerminalRepaints(t *testing.T) { + speech := &fakeSpeechQueue{spoken: make(chan string, 4)} + adapter := &fakeAdapter{events: make(chan atspi.Event, 8)} + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + completed := make(chan struct{}) + go func() { + processEvents(ctx, slog.New(slog.DiscardHandler), speech, adapter, 0) + close(completed) + }() + + terminal := atspi.NewSnapshot("/accessible/terminal", "", "terminal", "", "") + terminal.TextCaretKnown = true + terminal.BrailleLineStart = 200 + terminal.BrailleCaret = 205 + for _, expected := range []string{"40", "OK", "猫"} { + repaintSequence(adapter, terminal, 100, expected) + select { + case text := <-speech.spoken: + if text != expected { + t.Fatalf("short repaint = %q, want %q", text, expected) + } + case <-time.After(3 * time.Second): + t.Fatalf("short repaint %q was lost", expected) + } + } + repaintSequence(adapter, terminal, 100, "Thinking (4s)") + select { + case text := <-speech.spoken: + if text != "Thinking (4s)" { + t.Fatalf("spoken text = %q, want the full repaint", text) + } + case <-time.After(3 * time.Second): + t.Fatal("full repaint was not presented") + } + + cancel() + select { + case <-completed: + case <-time.After(time.Second): + t.Fatal("event loop did not stop") + } +} + +func TestProcessEventsPresentsUserTerminalDeletionDespiteRedraw(t *testing.T) { + speech := &fakeSpeechQueue{spoken: make(chan string, 3)} + adapter := &fakeAdapter{events: make(chan atspi.Event, 3)} + tracker := &input.IntentTracker{} + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + completed := make(chan struct{}) + go func() { + processEventsWithInput(ctx, slog.New(slog.DiscardHandler), speech, adapter, nil, nil, 0, nil, tracker, nil, nil) + close(completed) + }() + + // Backspace in a terminal input redraws the erased cell: the deleted + // character is replaced by a blank at the same offset. The deletion is + // the user's confirmation that the edit took and must still speak. + terminal := atspi.NewSnapshot("/accessible/terminal", "", "terminal", "", "") + terminal.TextCaretKnown = true + terminal.BrailleLineStart = 100 + terminal.BrailleCaret = 105 + tracker.Observe(input.NewKeyEvent(0, true, "BackSpace")) + deletion := atspi.NewEventWithPayload(atspi.TextDeletedEvent, terminal, "x") + deletion.Detail1 = 105 + redraw := atspi.NewEventWithPayload(atspi.TextInsertedEvent, terminal, " ") + redraw.Detail1 = 105 + adapter.events <- deletion + adapter.events <- redraw + + select { + case text := <-speech.spoken: + if text != "x" { + t.Fatalf("spoken text = %q, want the deleted character", text) + } + case <-time.After(time.Second): + t.Fatal("user deletion was not presented") + } + + cancel() + select { + case <-completed: + case <-time.After(time.Second): + t.Fatal("event loop did not stop") + } +} + +func TestUserTerminalDeletionClassification(t *testing.T) { + terminal := atspi.NewSnapshot("/accessible/terminal", "", "terminal", "", "") + terminal.TextCaretKnown = true + terminal.BrailleLineStart = 100 + terminal.BrailleCaret = 105 + for _, test := range []struct { + name string + intent input.Intent + modify func(*atspi.Event) + want bool + }{ + {"backspace at caret", input.DeleteIntent, func(event *atspi.Event) { event.Detail1 = 105 }, true}, + {"backspace before caret", input.DeleteIntent, func(event *atspi.Event) { event.Detail1 = 104 }, true}, + {"typing intent", input.TypingIntent, func(event *atspi.Event) { event.Detail1 = 105 }, false}, + {"no intent", input.NoIntent, func(event *atspi.Event) { event.Detail1 = 105 }, false}, + {"repaint above caret line", input.DeleteIntent, func(event *atspi.Event) { event.Detail1 = 40 }, false}, + {"unknown caret", input.DeleteIntent, func(event *atspi.Event) { + event.Detail1 = 105 + event.Source.TextCaretKnown = false + }, false}, + } { + t.Run(test.name, func(t *testing.T) { + event := atspi.NewEventWithPayload(atspi.TextDeletedEvent, terminal, "x") + test.modify(&event) + if got := is_user_terminal_deletion(event, test.intent); got != test.want { + t.Fatalf("is_user_terminal_deletion = %v, want %v", got, test.want) + } + }) + } +} + func TestProcessEventsPresentsStandaloneTerminalDeletion(t *testing.T) { speech := &fakeSpeechQueue{spoken: make(chan string, 1)} adapter := &fakeAdapter{events: make(chan atspi.Event, 1)} @@ -318,3 +581,106 @@ func TestProcessEventsPresentsTerminalFocusWithoutFollowingInsertion(t *testing. t.Fatal("event loop did not stop") } } + +// Run the real event handler synchronously so coalescing assertions do not +// depend on a scheduler processing an entire burst before a timer fires. +func terminalHandlerFixture(t *testing.T) (*accessibilityEventHandler, *[]string) { + t.Helper() + state := &eventLoopState{} + t.Cleanup(state.pending.clear) + spoken := []string{} + handler := &accessibilityEventHandler{ + state: state, logger: slog.New(slog.DiscardHandler), adapter: &fakeAdapter{}, + syncForeground: func() {}, webScripts: &webScriptController{}, + speak: func(event atspi.Event, _ ...input.Intent) { spoken = append(spoken, event.Payload) }, + } + handler.flushTerminalDeletion = func() { + event, _ := state.pending.terminalDeletion.take() + handler.speak(event) + } + return handler, &spoken +} + +func TestTerminalRepaintsKeepTerminalIdentityAndFirstDeadline(t *testing.T) { + for _, differentTerminal := range []bool{false, true} { + t.Run(map[bool]string{false: "same terminal", true: "different bus"}[differentTerminal], func(t *testing.T) { + handler, spoken := terminalHandlerFixture(t) + terminal := atspi.Snapshot{BusName: ":1.10", Path: "/terminal", Role: "terminal"} + repaint := func(text string) { + deletion := atspi.NewEventWithPayload(atspi.TextDeletedEvent, terminal, "Old status") + insertion := atspi.NewEventWithPayload(atspi.TextInsertedEvent, terminal, text) + handler.handle(deletion) + handler.handle(insertion) + } + repaint("First status") + firstTimer := handler.state.pending.terminalRepaint.timer + if differentTerminal { + terminal.BusName = ":1.11" + } + repaint("Second status") + pending := &handler.state.pending.terminalRepaint + if differentTerminal { + if len(*spoken) != 1 || (*spoken)[0] != "First status" { + t.Fatalf("different terminal discarded earlier repaint: %v", *spoken) + } + } else if pending.timer != firstTimer { + t.Fatal("continuous repaint postponed the first deadline") + } + event, _ := pending.take() + if event.Payload != "Second status" { + t.Fatalf("latest repaint = %q", event.Payload) + } + }) + } +} + +func TestTerminalSeparateDeletionsPreserveEarlierFeedback(t *testing.T) { + handler, spoken := terminalHandlerFixture(t) + terminal := atspi.Snapshot{BusName: ":1.10", Path: "/terminal", Role: "terminal"} + first := atspi.NewEventWithPayload(atspi.TextDeletedEvent, terminal, "First deletion") + first.Detail1 = 100 + second := atspi.NewEventWithPayload(atspi.TextDeletedEvent, terminal, "Second deletion") + second.Detail1 = 300 + handler.handle(first) + handler.handle(second) + if len(*spoken) != 1 || (*spoken)[0] != first.Payload { + t.Fatalf("unrelated deletion discarded earlier feedback: %v", *spoken) + } + if handler.state.pending.terminalDeletion.event.Payload != second.Payload { + t.Fatal("later deletion was lost") + } +} + +func TestTerminalHeldRepaintPreservesOrderAndRetiresOnFocusChange(t *testing.T) { + for _, next := range []string{"append", "deletion", "focus"} { + t.Run(next, func(t *testing.T) { + handler, spoken := terminalHandlerFixture(t) + terminal := atspi.Snapshot{BusName: ":1.10", Path: "/terminal", Role: "terminal"} + deletion := atspi.NewEventWithPayload(atspi.TextDeletedEvent, terminal, "Old status") + insertion := atspi.NewEventWithPayload(atspi.TextInsertedEvent, terminal, "Latest status") + handler.handle(deletion) + handler.handle(insertion) + event := atspi.NewEventWithPayload(atspi.TextInsertedEvent, terminal, "Appended output") + event.Detail1 = 300 + switch next { + case "deletion": + event.Type, event.Payload = atspi.TextDeletedEvent, "Later deletion" + case "focus": + event = atspi.NewEvent(atspi.FocusEvent, atspi.Snapshot{BusName: ":1.10", Path: "/entry", Role: "text", Focused: true}) + } + handler.handle(event) + if handler.state.pending.terminalRepaint.event.Type != "" { + t.Fatal("old repaint remains pending") + } + if next == "focus" { + for _, text := range *spoken { + if text == "Latest status" { + t.Fatal("old terminal spoke after focus moved to another control") + } + } + } else if len(*spoken) == 0 || (*spoken)[0] != "Latest status" { + t.Fatalf("later output overtook pending repaint: %v", *spoken) + } + }) + } +} diff --git a/internal/app/window_titles.go b/internal/app/window_titles.go index f1de029..7ec6dd8 100644 --- a/internal/app/window_titles.go +++ b/internal/app/window_titles.go @@ -32,6 +32,12 @@ type pendingWindowTitle struct { inputSequence uint64 } +type windowTitleRepeatKey struct { + window atspi.ObjectID + nativeWindow, titleWindow uint64 + text string +} + // windowTitleController is owned by the accessibility event loop. Discovery // stays in the native adapter; this controller owns coalescing, preferences, // and duplicate suppression across AT-SPI and X11. @@ -46,6 +52,7 @@ type windowTitleController struct { inputSource any lastNative atspi.ForegroundTitle lastTitle string + recent map[windowTitleRepeatKey]time.Time application string pending *pendingWindowTitle timer *time.Timer @@ -55,6 +62,10 @@ type windowTitleController struct { const ( windowTitleChangeDelay = 25 * time.Millisecond windowTitleIntroductionDelay = 250 * time.Millisecond + // Animated titles (terminal TUI spinners) cycle through the same set + // of frames about once a second. A title already announced within + // this window is another cycle, not a new title. + windowTitleRepeatWindow = 3 * time.Second ) func (titles *windowTitleController) clearPending() { @@ -64,6 +75,10 @@ func (titles *windowTitleController) clearPending() { titles.timer, titles.due, titles.pending = nil, nil, nil } +// The animated-title repeat history (titles.recent) deliberately +// survives reset and activation: it expires by time on its own, and a +// workspace round-trip away from a window with an animated title must +// not restart its announcement cycle on return. func (titles *windowTitleController) reset() { titles.clearPending() titles.lastTitle, titles.application = "", "" @@ -140,6 +155,11 @@ func (titles *windowTitleController) accessible(event atspi.Event) bool { titles.state.owner.changeTitle(event) titles.clearPending() titles.lastTitle = "" + // Keep the repeat history: animated title cycles include a + // blank frame every cycle (observed from terminal TUIs), and + // clearing here would restart the cycle announcement each + // second. A title restored after a genuine removal announces + // again once the repeat window expires. return true } previousTitle := titles.state.owner.title @@ -171,6 +191,16 @@ func (titles *windowTitleController) nativeChanged() { accessibleActivation := titles.state.owner.activated && !wine.IsReviewID(titles.state.owner.window) if switched { titles.clearPending() + // Keep the repeat history: a native sampler can re-report the + // same client as a switch (new epoch or title-window id) on + // every title sample, and clearing here would defeat animated + // title suppression. History is scoped to the window identity, + // so another window can still announce the same title. + if titles.logger != nil { + titles.logger.Debug("native window title switch", + "epoch", latest.Epoch, "window", latest.Title.Window, + "title_window", latest.Title.TitleWindow) + } // An AT-SPI activation may already have introduced this client. titles.lastTitle = titles.state.owner.title if wine.IsReviewID(titles.state.owner.window) { @@ -200,6 +230,63 @@ func (titles *windowTitleController) nativeChanged() { native: latest.Title, changed: changed}) } +// animatedWindowTitleKey reduces a title to the part that carries meaning +// across animation frames. Only recognized, single-character spinner frames +// are removed; symbols such as a check or cross can carry meaningful status. +// Announcement suppression uses this only to detect cycles; the spoken text +// remains the original title. +func animatedWindowTitleKey(title string) string { + fields := strings.Fields(strings.ToLower(title)) + if len(fields) > 1 { + frame := []rune(fields[0]) + if len(frame) == 1 && (frame[0] >= 0x2800 && frame[0] <= 0x28ff || strings.ContainsRune("|/-\\", frame[0])) { + fields = fields[1:] + } + } + return strings.Join(fields, " ") +} + +// repeatedWithinWindow reports whether this title was already announced in +// the repeat window. A suppressed sighting refreshes its entry, so a +// running animation stays suppressed instead of breaking through once per +// window; it expires shortly after the animation stops. +func (titles *windowTitleController) repeatedWithinWindow(pending pendingWindowTitle, at time.Time) bool { + key := windowTitleRepeatKey{window: pending.window, text: animatedWindowTitleKey(pending.text)} + if key.text == "" { + return false + } + // Correlate AT-SPI and native samples through the same native client + // when available. Do not include its epoch: returning to that client + // should retain its animation history. + native := pending.native + if native.Window == 0 && titles.source != nil { + latest := titles.source.ForegroundTitle() + if latest.Epoch == pending.epoch { + native = latest.Title + } + } + if native.Window != 0 { + key.window = atspi.ObjectID{} + key.nativeWindow, key.titleWindow = native.Window, native.TitleWindow + } else if !key.window.Valid() { + return false + } + if at.IsZero() { + at = time.Now() + } + for seen, when := range titles.recent { + if at.Sub(when) > windowTitleRepeatWindow { + delete(titles.recent, seen) + } + } + if titles.recent == nil { + titles.recent = make(map[windowTitleRepeatKey]time.Time) + } + _, repeated := titles.recent[key] + titles.recent[key] = at + return repeated +} + func equivalentWindowTitles(first, second string) bool { normalize := func(value string) string { return strings.Join(strings.Fields(strings.ToLower(value)), " ") } first, second = normalize(first), normalize(second) @@ -327,6 +414,13 @@ func (titles *windowTitleController) flush() { if pending.inputSequence != recentInput(titles.inputSource).TitleInterruptionSequence { return } + if pending.changed && titles.repeatedWithinWindow(*pending, time.Now()) { + if titles.logger != nil { + titles.logger.Debug("suppressed repeated window title", + "key", animatedWindowTitleKey(pending.text), "recent", len(titles.recent)) + } + return + } if titles.logger != nil { titles.logger.Debug("queued automatic window title", "title_changed", pending.changed, "observed_to_queue_ms", time.Since(pending.observedAt).Milliseconds()) diff --git a/internal/app/window_titles_test.go b/internal/app/window_titles_test.go index 3c277a7..8eec8f4 100644 --- a/internal/app/window_titles_test.go +++ b/internal/app/window_titles_test.go @@ -10,6 +10,7 @@ import ( "log/slog" "path/filepath" "testing" + "time" "git.stormux.org/storm/cthulhu/internal/atspi" "git.stormux.org/storm/cthulhu/internal/config" @@ -76,8 +77,96 @@ func TestAutomaticTitlePolicyCoalescesAndPreservesSpeech(t *testing.T) { changed.Source.Name = "Loading 100%" titles.accessible(changed) titles.flush() + // A title restored within the repeat window is a repeat, not news; + // animated cycles pass through a blank frame every cycle and must + // not revive announcements. It announces again after the window. + if len(queue.requests) != 1 { + t.Fatal("restored title repeated within the repeat window") + } +} + +func TestAutomaticTitleSuppressesAnimatedSpinnerCycle(t *testing.T) { + titles, queue, changed := titleControllerFixture(t) + // Terminal TUIs animate a spinner glyph in the title several times a + // second while the meaningful title text stays the same. The cycle + // includes a blank frame; it must not reset suppression. + for _, text := range []string{"⠋ Terminal", "⠙ Terminal", "⠹ Terminal", "", "⠋ Terminal", "Terminal", "", "⠙ Terminal"} { + changed.Source.Name = text + titles.accessible(changed) + titles.flush() + } + if len(queue.requests) != 1 || queue.requests[0].Text != "⠋ Terminal" { + t.Fatalf("spinner title requests = %+v, want only the first frame", queue.requests) + } + // A different title is not part of the animation and still announces. + changed.Source.Name = "Build finished" + titles.accessible(changed) + titles.flush() + if len(queue.requests) != 2 || queue.requests[1].Text != "Build finished" { + t.Fatalf("title after spinner = %+v", queue.requests) + } + // A leading count is meaningful: each value announces, a rapid repeat + // of an earlier value in the same cycle does not. + for _, text := range []string{"(2) Terminal", "(3) Terminal", "(2) Terminal"} { + changed.Source.Name = text + titles.accessible(changed) + titles.flush() + } + if len(queue.requests) != 4 { + t.Fatalf("counted title requests = %+v, want the cycle repeat suppressed", queue.requests) + } +} + +func TestWindowReactivationDoesNotReviveSpinnerCycle(t *testing.T) { + titles, queue, changed := titleControllerFixture(t) + for _, text := range []string{"9 Terminal", "8 Terminal"} { + changed.Source.Name = text + titles.accessible(changed) + titles.flush() + } if len(queue.requests) != 2 { - t.Fatal("restored title was not announced") + t.Fatalf("initial spinner titles = %+v", queue.requests) + } + // Leaving for another workspace deactivates (reset) and returning + // reactivates the same window. Neither may restart the cycle. + titles.reset() + titles.activated(changed) + for _, text := range []string{"9 Terminal", "8 Terminal"} { + changed.Source.Name = text + titles.accessible(changed) + titles.flush() + } + if len(queue.requests) != 2 { + t.Fatalf("spinner revived by workspace round-trip = %+v", queue.requests) + } +} + +func TestNativeTitleSwitchSamplesDoNotReviveSpinnerCycle(t *testing.T) { + titles, queue, changed := titleControllerFixture(t) + source := &titleProbeSource{title: atspi.ForegroundTitle{Title: xorg.WindowTitle{Window: 10, TitleWindow: 10, Title: "Original"}}} + titles.source, titles.foreground = source, source + titles.nativeChanged() + titles.flush() + changed.Source.Name = "9 Terminal" + titles.accessible(changed) + titles.flush() + changed.Source.Name = "8 Terminal" + titles.accessible(changed) + titles.flush() + if len(queue.requests) != 2 { + t.Fatalf("initial spinner titles = %+v", queue.requests) + } + // A native sampler may re-report the same client as a switch on every + // sample. That must not wipe the animated-title repeat history, so the + // cycle returning to an earlier frame stays suppressed. + for _, text := range []string{"9 Terminal", "8 Terminal"} { + source.title.Epoch++ + source.title.Title.Title = text + titles.nativeChanged() + titles.flush() + } + if len(queue.requests) != 2 { + t.Fatalf("spinner revived by native switch samples = %+v", queue.requests) } } @@ -658,3 +747,60 @@ func TestWineReviewTitlesUseNativeApplicationPreferences(t *testing.T) { t.Fatal("Wine title ignored its application preference") } } + +func TestTitleRepeatHistoryIsScopedToWindow(t *testing.T) { + titles, queue, changed := titleControllerFixture(t) + changed.Source.Name = "Build finished" + titles.accessible(changed) + titles.flush() + other := ownerEvent(atspi.WindowActivatedEvent, ":1.51", "/window", "/window") + other.Source.Name, other.Source.Role = "Original", "frame" + titles.state.owner.activate(other) + titles.activated(other) + other.Type, other.Source.Name = atspi.AccessibleNameEvent, "Build finished" + titles.accessible(other) + titles.flush() + if len(queue.requests) != 2 { + t.Fatalf("another window's title was suppressed: %+v", queue.requests) + } +} + +func TestTitleStatusSymbolsRemainMeaningful(t *testing.T) { + titles, queue, changed := titleControllerFixture(t) + for _, text := range []string{"✓ Build", "✗ Build", "! Build", "? Build"} { + changed.Source.Name = text + titles.accessible(changed) + titles.flush() + if len(queue.requests) == 0 || queue.requests[len(queue.requests)-1].Text != text { + t.Fatalf("meaningful title status %q was suppressed: %+v", text, queue.requests) + } + } + if len(queue.requests) != 4 { + t.Fatalf("status titles = %+v", queue.requests) + } +} + +func TestTitleRepeatHistoryRefreshesAndExpires(t *testing.T) { + titles, _, changed := titleControllerFixture(t) + pending := pendingWindowTitle{window: changed.Source.ID(), text: "⠋ Terminal"} + start := time.Now() + if titles.repeatedWithinWindow(pending, start) { + t.Fatal("first title suppressed") + } + pending.text = "⠙ Terminal" + if !titles.repeatedWithinWindow(pending, start.Add(2*time.Second)) || + !titles.repeatedWithinWindow(pending, start.Add(4*time.Second)) { + t.Fatal("active animation history expired") + } + if titles.repeatedWithinWindow(pending, start.Add(8*time.Second)) { + t.Fatal("inactive animation history did not expire") + } + pending.native = xorg.WindowTitle{Window: 10, TitleWindow: 11} + if titles.repeatedWithinWindow(pending, start.Add(8*time.Second)) { + t.Fatal("native window inherited AT-SPI-only history") + } + pending.native.TitleWindow = 12 + if titles.repeatedWithinWindow(pending, start.Add(8*time.Second)) { + t.Fatal("another native child inherited title history") + } +} diff --git a/internal/platform/wayland/protocol_test.go b/internal/platform/wayland/protocol_test.go index 53e7253..0c41f75 100644 --- a/internal/platform/wayland/protocol_test.go +++ b/internal/platform/wayland/protocol_test.go @@ -242,6 +242,11 @@ func TestNativeKeyboardBrowseSecureAndLearnTransitions(t *testing.T) { monitor.key(t, 'h', 43, 0, false) monitor.key(t, 'h', 43, 0, true) await_command(t, keyboard, input.DocumentNextHeading) + if loadShiftPairs().unshifted['#'] == '3' { + monitor.key(t, '#', 12, 1, false) + monitor.key(t, '3', 12, 0, true) + await_command(t, keyboard, input.DocumentPreviousHeadingAtLevel3) + } keyboard.SetNativeButtonFocus(true) wait_snapshot(t, monitor, func(state MonitorSnapshot) bool { for _, grab := range state.grabs { diff --git a/internal/platform/wayland/routing.go b/internal/platform/wayland/routing.go index 101488c..8d53ba8 100644 --- a/internal/platform/wayland/routing.go +++ b/internal/platform/wayland/routing.go @@ -6,6 +6,7 @@ package wayland /* #cgo pkg-config: xkbcommon +#include #include */ import "C" @@ -14,6 +15,7 @@ import ( "errors" "fmt" "sort" + "sync" "time" "unsafe" @@ -51,12 +53,101 @@ func key_name(symbol uint32) string { func lower_symbol(symbol uint32) uint32 { return uint32(C.xkb_keysym_to_lower(C.xkb_keysym_t(symbol))) } -// KeyboardMonitor sends keysyms rather than a keymap. Letter case is defined -// by XKB; punctuation's physical Shift pairing cannot be inferred from it. +// KeyboardMonitor sends keysyms rather than a keymap, and the symbol for a +// shifted key is the shifted keysym: Shift+3 arrives as numbersign on a US +// layout. Letter case is defined by XKB alone, but a digit or punctuation +// key's Shift partner depends on the layout. Bindings name the unshifted +// symbol plus the Shift bit, as delivered by the Xorg path, so both grab +// registration and event handling translate through shift pairs taken from +// the default XKB keymap (the XKB_DEFAULT_* environment, else libxkbcommon +// defaults). The monitor interface does not expose the compositor's keymap; +// shifted digit and punctuation chords therefore require these defaults to +// match the compositor's layout. The protocol cannot verify that match. func shifted_symbol(symbol uint32) uint32 { return uint32(C.xkb_keysym_to_upper(C.xkb_keysym_t(symbol))) } +type shiftPairs struct { + shifted map[uint32][]uint32 // Unshifted symbol to its shifted partners. + unshifted map[uint32]uint32 // Shifted symbol to its one unshifted partner. +} + +var loadShiftPairs = sync.OnceValue(func() shiftPairs { return build_shift_pairs("") }) + +func shift_pair_base(symbol uint32) bool { + if symbol < '!' || symbol > '~' { + return false + } + return (symbol < 'A' || symbol > 'Z') && (symbol < 'a' || symbol > 'z') +} + +func shift_pair_partner(symbol, base uint32) bool { + if symbol == 0 || symbol == base || symbol >= 0xfe00 { + return false // NoSymbol, unchanged, or dead/function/modifier ranges. + } + return (symbol < 'A' || symbol > 'Z') && (symbol < 'a' || symbol > 'z') +} + +// An empty layout selects the default keymap; tests name layouts explicitly. +func build_shift_pairs(layout string) shiftPairs { + pairs := shiftPairs{shifted: make(map[uint32][]uint32), unshifted: make(map[uint32]uint32)} + context := C.xkb_context_new(C.XKB_CONTEXT_NO_FLAGS) + if context == nil { + return pairs + } + C.xkb_context_set_log_level(context, C.XKB_LOG_LEVEL_CRITICAL) // Failure means empty pairs, not stderr noise. + defer C.xkb_context_unref(context) + var names C.struct_xkb_rule_names + if layout != "" { + clayout := C.CString(layout) + defer C.free(unsafe.Pointer(clayout)) + names.layout = clayout + } + keymap := C.xkb_keymap_new_from_names(context, &names, C.XKB_KEYMAP_COMPILE_NO_FLAGS) + if keymap == nil { + return pairs + } + defer C.xkb_keymap_unref(keymap) + cshift := C.CString("Shift") + defer C.free(unsafe.Pointer(cshift)) + shiftIndex := C.xkb_keymap_mod_get_index(keymap, cshift) + if shiftIndex == C.XKB_MOD_INVALID { + return pairs + } + state := C.xkb_state_new(keymap) + if state == nil { + return pairs + } + defer C.xkb_state_unref(state) + ambiguous := make(map[uint32]bool) + groups := C.xkb_keymap_num_layouts(keymap) + for group := C.xkb_layout_index_t(0); group < groups; group++ { + for code := C.xkb_keymap_min_keycode(keymap); code <= C.xkb_keymap_max_keycode(keymap); code++ { + C.xkb_state_update_mask(state, 0, 0, 0, 0, 0, group) + base := uint32(C.xkb_state_key_get_one_sym(state, code)) + C.xkb_state_update_mask(state, 1<