Bridge NVDA Braille traffic to the reader's remote.braille IPC

A session-only option requests braille-stream. A controlled computer
requests geometry, applies the controller's width, sends coalesced
display cells and applies pan and route gestures. A controller
advertises its display width, shows coalesced peer cells and sends pan
and route gestures. Other gestures stay inert.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chase CichorzandClaude Opus 5.5 committed 2026-11-06 16:06:38 -05:00
1 parent db19f04c43
commit 764d2c1e9e
11 files changed
+492 -12

No files matched your search

+170
View File
@@ -0,0 +1,170 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package remote
import (
"slices"
sdk "git.stormux.org/storm/cthulhu/sdk/go/tentacle"
)
// Braille bridges NVDA display traffic and the reader's remote.braille IPC.
// Cell streams are coalesced to the newest frame in both directions: an old
// display is never worth delivering. Other operations are ordered and
// bounded; overflow stops the connection.
const maxBrailleOps = 16
func (stream *speechStream) braille_enabled() bool {
return slices.Contains(stream.grants, "braille-stream")
}
// queue_braille_op_locked sends one ordered operation to the reader.
func (c *coordinator) queue_braille_op_locked(stream *speechStream, value sdk.RemoteBraille) {
select {
case stream.brailleOps <- value:
default:
c.stop_locked(errLimit)
}
}
// queue_relay_locked sends one small Braille frame on the ordered writer.
func (c *coordinator) queue_relay_locked(stream *speechStream, kind string, payload any) {
frame, err := encodeMessage(kind, payload)
if err != nil {
c.stop_locked(errProtocol)
return
}
if stream.items == maxSpeechItems || stream.bytes+len(frame) > maxQueuedBytes {
c.stop_locked(errLimit)
return
}
stream.items++
stream.bytes += len(frame)
stream.outgoing <- frame
}
// braille_ready_locked starts negotiation once the peer has joined: a
// controlled peer asks for the controller's geometry; a controller asks its
// reader for the local display width.
func (c *coordinator) braille_ready_locked(a *connectionAttempt, stream *speechStream) {
if stream.brailleStarted || !stream.braille_enabled() || !c.speech_ready_locked(a, stream) {
return
}
stream.brailleStarted = true
if stream.role == "controlled" {
c.queue_relay_locked(stream, "set_braille_info", map[string]any{})
} else {
c.queue_braille_op_locked(stream, sdk.RemoteBraille{Op: "geometry"})
}
}
// receive_braille handles peer Braille traffic. Membership, origin and
// direction were checked by the relay reader.
func (c *coordinator) receive_braille(a *connectionAttempt, stream *speechStream, message message) bool {
switch message.kind {
case "set_braille_info", "set_display_size", "display", "braille_input":
default:
return false
}
if !stream.braille_enabled() {
return false
}
c.mu.Lock()
defer c.mu.Unlock()
if !c.speech_ready_locked(a, stream) {
return true
}
switch {
case stream.role == "controlled" && message.kind == "set_braille_info":
info, ok := message.payload.(brailleInfo)
if !ok {
return false
}
c.queue_braille_op_locked(stream, sdk.RemoteBraille{Op: "size", Width: info.Cells})
case stream.role == "controlled" && message.kind == "set_display_size":
sizes, ok := message.payload.([]int)
if !ok {
return false
}
width := 0
for _, size := range sizes {
if size > 0 && (width == 0 || size < width) {
width = size
}
}
c.queue_braille_op_locked(stream, sdk.RemoteBraille{Op: "size", Width: width})
case stream.role == "controlled" && message.kind == "braille_input":
g, ok := message.payload.(gesture)
if !ok || g.action == "" {
return false // Unsupported gestures stay inert.
}
value := sdk.RemoteBraille{Op: "gesture", Gesture: g.action}
if g.action == "route" {
value.Index = g.index
}
c.queue_braille_op_locked(stream, value)
case stream.role == "controller" && message.kind == "set_braille_info":
// The controlled peer asks for geometry again.
c.queue_braille_op_locked(stream, sdk.RemoteBraille{Op: "geometry"})
case stream.role == "controller" && message.kind == "display":
cells, ok := message.payload.([]int)
if !ok || len(cells) == 0 {
return false
}
stream.readerCells = slices.Clone(cells)
select {
case stream.readerCellsSignal <- struct{}{}:
default:
}
default:
return false
}
return true
}
// braille_event_locked handles reader remote.braille events for this lease.
func (c *coordinator) braille_event_locked(event sdk.SessionEvent) {
a := c.active
if a == nil || a.speech == nil || event.Braille == nil {
return
}
stream := a.speech
if !stream.braille_enabled() || event.Lease != stream.lease.Lease || event.Epoch != stream.lease.Epoch || !c.speech_ready_locked(a, stream) {
return
}
value := *event.Braille
switch {
case stream.role == "controlled" && value.Op == "cells":
stream.displayCells = slices.Clone(value.Cells)
select {
case stream.displaySignal <- struct{}{}:
default:
}
case stream.role == "controller" && value.Op == "geometry":
c.queue_relay_locked(stream, "set_braille_info", brailleInfo{Name: "Cthulhu", Cells: value.Width})
case stream.role == "controller" && value.Op == "gesture":
script := map[string]string{"pan-left": "braille_scrollBack", "pan-right": "braille_scrollForward", "route": "braille_routeTo"}[value.Gesture]
if script == "" {
return
}
payload := map[string]any{"scriptPath": []string{"globalCommands", "GlobalCommands", script}}
if value.Gesture == "route" {
payload["routingIndex"] = value.Index
}
c.queue_relay_locked(stream, "braille_input", payload)
}
}
// take_display_locked returns the newest cells for the relay, once.
func (stream *speechStream) take_display_locked() []int {
cells := stream.displayCells
stream.displayCells = nil
return cells
}
func (stream *speechStream) take_reader_cells_locked() []int {
cells := stream.readerCells
stream.readerCells = nil
return cells
}
+186
View File
@@ -0,0 +1,186 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package remote
import (
"context"
"io"
"net"
"reflect"
"strings"
"testing"
"time"
sdk "git.stormux.org/storm/cthulhu/sdk/go/tentacle"
)
// brailleRelay joins one opposite-role peer, writes frames and records every
// Braille frame this computer sends.
func brailleRelay(t *testing.T, r role, frames []string, received chan<- string) endpoint {
t.Helper()
cert, roots := testCertificate(t, true)
peer := "slave"
if r == controlled {
peer = "master"
}
e := testRelay(t, cert, func(conn net.Conn) {
reader := relayHandshake(t, conn, r)
if reader == nil {
return
}
_, _ = io.WriteString(conn, `{"type":"channel_joined","channel":"synthetic-fixture","clients":[{"id":7,"connection_type":"`+peer+`"}]}`+"\n")
for _, frame := range frames {
_, _ = io.WriteString(conn, frame+"\n")
}
for {
line, err := reader.ReadString('\n')
if err != nil {
return
}
for _, kind := range []string{"set_braille_info", "display", "braille_input"} {
if strings.Contains(line, `"type":"`+kind+`"`) {
received <- strings.TrimSpace(line)
}
}
}
})
e.roots = roots
return e
}
func nextBrailleCall(t *testing.T, f *fixtureClient) sdk.RemoteBraille {
t.Helper()
for {
select {
case call := <-f.calls:
if call.method != "remote.braille" {
continue
}
request := call.params.(sdk.RemoteBrailleRequest)
if !sdk.ValidRemoteBrailleRequest(request) {
t.Fatal("invalid Braille request", request)
}
return request.Braille
case <-time.After(4 * time.Second):
t.Fatal("missing remote.braille call")
return sdk.RemoteBraille{}
}
}
}
func nextRelayFrame(t *testing.T, received <-chan string) string {
t.Helper()
select {
case frame := <-received:
return frame
case <-time.After(4 * time.Second):
t.Fatal("missing relay Braille frame")
return ""
}
}
func TestBrailleControlledExportsCellsAndAppliesGestures(t *testing.T) {
c, f := fixtureCoordinator(t, nil)
action := approveGrants(t, c, f, 1, "controlled", "output-observe", "braille-stream")
received := make(chan string, 8)
frames := []string{
`{"type":"set_braille_info","origin":7,"name":"Focus","numCells":40}`,
`{"type":"braille_input","origin":7,"scriptPath":["globalCommands","GlobalCommands","braille_routeTo"],"routingIndex":5}`,
`{"type":"braille_input","origin":7,"scriptPath":["globalCommands","GlobalCommands","braille_scrollForward"]}`,
`{"type":"braille_input","origin":7,"scriptPath":["globalCommands","GlobalCommands","sayAll"]}`,
}
if err := c.connect(action, brailleRelay(t, controlled, frames, received), "synthetic-fixture"); err != nil {
t.Fatal(err)
}
if frame := nextRelayFrame(t, received); frame != `{"type":"set_braille_info"}` {
t.Fatal("controlled peer did not ask for geometry", frame)
}
for _, want := range []sdk.RemoteBraille{{Op: "size", Width: 40}, {Op: "gesture", Gesture: "route", Index: 5}, {Op: "gesture", Gesture: "pan-right"}} {
if got := nextBrailleCall(t, f); !reflect.DeepEqual(got, want) {
t.Fatalf("got %+v, want %+v", got, want)
}
}
lease := strings.Repeat("b", 32)
for index, cells := range [][]int{{1, 2}, {3, 4}, {5, 6, 7}} {
value := sdk.RemoteBraille{Op: "cells", Cells: cells}
f.events <- sdk.SessionEvent{Sequence: uint64(2 + index), Kind: "remote.braille", Lease: lease, Epoch: 1, Braille: &value}
}
// Coalescing may skip intermediate frames but always ends at the newest.
deadline := time.After(4 * time.Second)
for {
select {
case frame := <-received:
if !strings.Contains(frame, `"type":"display"`) {
t.Fatal("unexpected frame", frame)
}
if strings.Contains(frame, `"cells":[5,6,7]`) {
return
}
case <-deadline:
t.Fatal("newest display cells not sent")
}
}
}
func TestBrailleControllerShowsPeerCellsAndSendsGestures(t *testing.T) {
c, f := fixtureCoordinator(t, func(_ context.Context, method string, params, result any) error {
if method == "remote.open" {
*result.(*sdk.RemoteLease) = sdk.RemoteLease{Lease: strings.Repeat("b", 32), Epoch: params.(sdk.RemoteOpen).Epoch}
}
return nil
})
action := approveGrants(t, c, f, 1, "controller", "remote-present", "braille-stream")
received := make(chan string, 8)
frames := []string{`{"type":"display","origin":7,"cells":[0,1,255]}`}
if err := c.connect(action, brailleRelay(t, controller, frames, received), "synthetic-fixture"); err != nil {
t.Fatal(err)
}
if got := nextBrailleCall(t, f); got.Op != "geometry" {
t.Fatal("controller did not ask for local geometry", got)
}
if got := nextBrailleCall(t, f); !reflect.DeepEqual(got, sdk.RemoteBraille{Op: "cells", Cells: []int{0, 1, 255}}) {
t.Fatal("peer cells changed", got)
}
lease := strings.Repeat("b", 32)
geometry := sdk.RemoteBraille{Op: "geometry", Width: 32}
f.events <- sdk.SessionEvent{Sequence: 2, Kind: "remote.braille", Lease: lease, Epoch: 1, Braille: &geometry}
if frame := nextRelayFrame(t, received); !strings.Contains(frame, `"numCells":32`) || !strings.Contains(frame, `"name":"Cthulhu"`) {
t.Fatal("geometry not advertised", frame)
}
for index, value := range []sdk.RemoteBraille{{Op: "gesture", Gesture: "route", Index: 2}, {Op: "gesture", Gesture: "pan-left"}} {
f.events <- sdk.SessionEvent{Sequence: uint64(3 + index), Kind: "remote.braille", Lease: lease, Epoch: 1, Braille: &value}
}
if frame := nextRelayFrame(t, received); !strings.Contains(frame, `braille_routeTo`) || !strings.Contains(frame, `"routingIndex":2`) {
t.Fatal("route gesture changed", frame)
}
if frame := nextRelayFrame(t, received); !strings.Contains(frame, `braille_scrollBack`) || strings.Contains(frame, "routingIndex") {
t.Fatal("pan gesture changed", frame)
}
}
func TestBrailleRequiresGrant(t *testing.T) {
c, f := fixtureCoordinator(t, nil)
action := approveGrants(t, c, f, 1, "controlled", "output-observe")
received := make(chan string, 4)
frames := []string{`{"type":"set_braille_info","origin":7,"name":"Focus","numCells":40}`}
if err := c.connect(action, brailleRelay(t, controlled, frames, received), "synthetic-fixture"); err != nil {
t.Fatal(err)
}
nextCall(t, f, "remote.open")
awaitStatus(t, c, func(s connectionStatus) bool { return s.state == "ready" && s.ignored == 1 })
select {
case frame := <-received:
t.Fatal("Braille sent without grant", frame)
case <-time.After(200 * time.Millisecond):
}
for {
select {
case call := <-f.calls:
if call.method == "remote.braille" {
t.Fatal("Braille request without grant")
}
default:
return
}
}
}
+7 -1
View File
@@ -49,6 +49,7 @@ type connectionCommand struct {
keyboard bool // Session-only; never persisted.
keyProfile string // "portable" or "us"; session-only.
clipboard bool // Session-only; never persisted.
braille bool // Session-only; never persisted.
}
func (command *connectionCommand) commands() []sdk.Command {
@@ -93,7 +94,7 @@ func (command *connectionCommand) selection_form(notice string) sdk.Interaction
if command.keyProfile == "us" {
profiles[0], profiles[1] = profiles[1], profiles[0]
}
result := sdk.Interaction{Form: &sdk.Form{Title: "Remote relay and role. Experimental speech and keyboard; clipboard and Braille services are unavailable.", Elements: []sdk.Element{
result := sdk.Interaction{Form: &sdk.Form{Title: "Remote relay and role. Experimental speech, keyboard, clipboard and Braille; live verification is pending.", Elements: []sdk.Element{
{ID: "cancel", Label: "Cancel", Kind: sdk.Button},
{ID: "host", Label: "Relay host", Kind: sdk.Entry, Text: command.selected.Host, MaxLength: 253},
{ID: "port", Label: "Relay port", Kind: sdk.Entry, Text: strconv.Itoa(command.selected.Port), MaxLength: 5},
@@ -101,6 +102,7 @@ func (command *connectionCommand) selection_form(notice string) sdk.Interaction
{ID: "keyboard", Label: "Include keyboard control for this session: controlling sends this keyboard when remote focus is on; allowing access lets the helper type here", Kind: sdk.CheckBox, Checked: command.keyboard},
{ID: "key-profile", Label: "Keys that may be sent", Kind: sdk.List, Items: profiles},
{ID: "clipboard", Label: "Allow explicit clipboard text transfer in both directions for this session", Kind: sdk.CheckBox, Checked: command.clipboard},
{ID: "braille", Label: "Include Braille for this session: controlling shows the other computer's Braille on this display; allowing access sends this computer's Braille", Kind: sdk.CheckBox, Checked: command.braille},
{ID: "review-access", Label: "Review access for this session", Kind: sdk.Button},
{ID: "save-settings", Label: "Save relay and role without connecting", Kind: sdk.Button},
}}, Next: "remote-select"}
@@ -174,6 +176,7 @@ func (command *connectionCommand) select_relay(ctx context.Context, raw json.Raw
command.selected = selected
command.keyboard = value.Values["keyboard"].Checked
command.clipboard = value.Values["clipboard"].Checked
command.braille = value.Values["braille"].Checked
command.keyProfile = "portable"
if value.Values["key-profile"].Selected == "us" {
command.keyProfile = "us"
@@ -199,6 +202,9 @@ func (command *connectionCommand) select_relay(ctx context.Context, raw json.Raw
if command.clipboard {
grants = append(grants, "clipboard-exchange")
}
if command.braille {
grants = append(grants, "braille-stream")
}
command.awaiting = true
return sdk.Interaction{Remote: &sdk.RemoteRequest{Role: selected.Role, Grants: grants}, Next: "remote-approved"}, nil
}
+8 -2
View File
@@ -192,12 +192,18 @@ func session_scope_text(action sdk.SessionEvent) string {
text += " and keyboard control of this computer by the helper"
}
if slices.Contains(action.Grants, "clipboard-exchange") {
text += ", and explicit clipboard text transfer"
text += ", explicit clipboard text transfer"
}
text += "; live verification is pending. Braille services are unavailable."
if slices.Contains(action.Grants, "braille-stream") {
text += ", Braille display output and routing"
}
text += "; live verification is pending."
if !slices.Contains(action.Grants, "clipboard-exchange") {
text += " Clipboard transfer was not approved."
}
if !slices.Contains(action.Grants, "braille-stream") {
text += " Braille was not approved."
}
return text
}
+2 -2
View File
@@ -72,7 +72,7 @@ func TestConnectionWorkflowBindsReaderActionAndFreezesTrust(t *testing.T) {
}
result := w.form()
encoded, _ := json.Marshal(result)
if result.Form == nil || result.Next != "remote-connection" || !strings.Contains(string(encoded), "services are unavailable") {
if result.Form == nil || result.Next != "remote-connection" || !strings.Contains(string(encoded), "was not approved") {
t.Fatal("missing form, continuation or service limitation")
}
workflowSubmit(t, w, "connect", strings.Repeat("猫", 200))
@@ -155,7 +155,7 @@ func TestConnectionWorkflowConnectUsesLoadedKeyWithoutEcho(t *testing.T) {
awaitStatus(t, c, func(s connectionStatus) bool { return s.state == "waiting" })
result = workflowSubmit(t, w, "refresh", "")
encoded, _ := json.Marshal(result)
if !strings.Contains(string(encoded), "waiting for one opposite-role peer") || !strings.Contains(string(encoded), "Braille services are unavailable") || !strings.Contains(string(encoded), "Clipboard transfer was not approved") || strings.Contains(string(encoded), "keyboard control") {
if !strings.Contains(string(encoded), "waiting for one opposite-role peer") || !strings.Contains(string(encoded), "Braille was not approved") || !strings.Contains(string(encoded), "Clipboard transfer was not approved") || strings.Contains(string(encoded), "keyboard control") {
t.Fatal("status advertised desktop control or omitted waiting state")
}
if w.key != "" {
+11
View File
@@ -118,6 +118,8 @@ func (c *coordinator) read_events() {
c.capture_locked(event)
case "remote.focus":
c.focus_locked(event)
case "remote.braille":
c.braille_event_locked(event)
}
c.mu.Unlock()
}
@@ -393,6 +395,9 @@ func (c *coordinator) run(a *connectionAttempt, approval sdk.SessionEvent, e end
}
c.speech_output(a, stream, conn)
}()
c.mu.Lock()
c.braille_ready_locked(a, stream)
c.mu.Unlock()
for a.ctx.Err() == nil {
m, err := conn.read()
if err != nil {
@@ -409,6 +414,9 @@ func (c *coordinator) run(a *connectionAttempt, approval sdk.SessionEvent, e end
if !c.update(a, s) {
return
}
c.mu.Lock()
c.braille_ready_locked(a, stream)
c.mu.Unlock()
if accepted {
handled, err := c.receive_speech(a, stream, m)
if err == nil && !handled {
@@ -417,6 +425,9 @@ func (c *coordinator) run(a *connectionAttempt, approval sdk.SessionEvent, e end
if err == nil && !handled {
handled = c.receive_clipboard(a, stream, m)
}
if err == nil && !handled {
handled = c.receive_braille(a, stream, m)
}
if err != nil {
failure = err
return
+61 -1
View File
@@ -46,13 +46,21 @@ type speechStream struct {
// Explicit clipboard transfer; coordinator.mu.
received string
hasReceived bool
// Braille; coordinator.mu. Cell streams keep only the newest frame.
brailleStarted bool
brailleOps chan sdk.RemoteBraille
readerCells []int
readerCellsSignal chan struct{}
displayCells []int
displaySignal chan struct{}
}
func newSpeechStream(lease sdk.RemoteLease, approval sdk.SessionEvent) *speechStream {
return &speechStream{lease: lease, role: approval.Role, grants: slices.Clone(approval.Grants),
incoming: make(chan speechItem, maxSpeechItems), outgoing: make(chan []byte, maxSpeechItems),
keys: make(chan sdk.RemoteKey, maxKeyItems), captured: make(chan sdk.RemoteCapturedKey, maxCapturedKeys),
focusRequests: make(chan bool, 4)}
focusRequests: make(chan bool, 4), brailleOps: make(chan sdk.RemoteBraille, maxBrailleOps),
readerCellsSignal: make(chan struct{}, 1), displaySignal: make(chan struct{}, 1)}
}
func (stream *speechStream) keyboard_enabled() bool {
@@ -73,6 +81,7 @@ func (stream *speechStream) drain() {
case <-stream.captured:
stream.capturedItems--
case <-stream.focusRequests:
case <-stream.brailleOps:
default:
return
}
@@ -221,6 +230,17 @@ func (c *coordinator) speech_operations(a *connectionAttempt, stream *speechStre
if !heartbeat() {
return
}
case value := <-stream.brailleOps:
if !c.braille_call(a, stream, value) {
return
}
case <-stream.readerCellsSignal:
c.mu.Lock()
cells := stream.take_reader_cells_locked()
c.mu.Unlock()
if cells != nil && !c.braille_call(a, stream, sdk.RemoteBraille{Op: "cells", Cells: cells}) {
return
}
case remote := <-stream.focusRequests:
c.mu.Lock()
ready := c.speech_ready_locked(a, stream)
@@ -303,11 +323,51 @@ func (c *coordinator) deliver_key(a *connectionAttempt, stream *speechStream, ke
return true
}
// braille_call sends one remote.braille operation in the shared sequence.
func (c *coordinator) braille_call(a *connectionAttempt, stream *speechStream, value sdk.RemoteBraille) bool {
c.mu.Lock()
ready := c.speech_ready_locked(a, stream)
c.mu.Unlock()
if !ready {
return false
}
stream.sequence++
request := sdk.RemoteBrailleRequest{RemoteOperation: sdk.RemoteOperation{Lease: stream.lease.Lease, Epoch: a.epoch, Sequence: stream.sequence}, Braille: value}
if c.call("remote.braille", request, nil) != nil {
c.mu.Lock()
c.stop_locked(errLease)
c.mu.Unlock()
return false
}
return true
}
func (c *coordinator) speech_output(a *connectionAttempt, stream *speechStream, conn *relayConn) {
for {
select {
case <-a.ctx.Done():
return
case <-stream.displaySignal:
c.mu.Lock()
ready := c.speech_ready_locked(a, stream)
cells := stream.take_display_locked()
c.mu.Unlock()
if !ready {
return
}
if cells == nil {
continue
}
frame, err := encodeMessage("display", map[string][]int{"cells": cells})
if err == nil {
err = conn.write(a.ctx, frame)
}
if err != nil {
c.mu.Lock()
c.stop_locked(err)
c.mu.Unlock()
return
}
case frame := <-stream.outgoing:
c.mu.Lock()
ready := c.speech_ready_locked(a, stream)