Files
Devin PraterandClaude Opus 5.5 418f671066 Add Orca extension for Wine accessibility
Publish the Wine Access protocol on Orca’s service connection and manage helpers per active Wine prefix. Add presentation, dialog capture, documentation, and tests.

Preserve the Cthulhu default and shared helper code. Document the single-reader limit and verify Orca cleanup leaves Cthulhu-managed helpers alone. Retain integration artifacts on exceptional failures.

Validation: 45 Orca unit tests passed; 11 existing test-environment checks passed; version metadata checks, Python compilation, and staged whitespace checks passed. Full Wine integration and live speech/braille remain unverified here because MinGW, Xvfb, and Orca are unavailable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 15:42:13 -04:00

246 lines
8.8 KiB
Python

# SPDX-License-Identifier: LGPL-2.1-or-later
"""Turns Wine Access helper events into Orca presentations.
The helper sends normalized MSAA, menu and editor events through
PresentAccessibleEvent. Wine produces bursts of transient focus events, so
focus is coalesced briefly before it is spoken; editor actions are presented
immediately and never deduplicated, so repeated characters are all heard.
This class has no Orca dependency: output and timers are injected.
"""
from __future__ import annotations
from dataclasses import dataclass, field
import time
from typing import Callable
FOCUS_DEBOUNCE_MS = 50
# How long text for a window waits for that window's focus to be presented.
HOLD_MS = 300
DUPLICATE_SECONDS = 0.1
# Normalized MSAA state labels worth speaking. Editor diagnostics (offsets,
# action kinds) share the states field and must never be spoken.
SPOKEN_STATES = (
"checked", "not checked", "half checked", "selected", "pressed",
"expanded", "collapsed", "unavailable", "read only", "password", "busy",
)
EVENT_TYPES = {"focus", "value", "selection", "text", "name", "state"}
INSERTED_ACTION = "kind=8"
def _state_labels(states: str) -> list[str]:
return [label.strip() for label in states.split(",") if label.strip()]
@dataclass
class WineEvent:
"""One PresentAccessibleEvent call, in the helper's argument order."""
source: str
kind: str
name: str = ""
role: str = ""
value: str = ""
states: str = ""
description: str = ""
position: int = 0
count: int = 0
window: str = ""
@property
def labels(self) -> list[str]:
return _state_labels(self.states)
@property
def is_password(self) -> bool:
return self.description == "password" or "password" in self.labels
@dataclass
class _Pending:
source: str
window: str
message: str
parts: list[str] = field(default_factory=list)
after: list[str] = field(default_factory=list)
class WineEventPresenter:
"""Applies focus tracking, filtering and coalescing to helper events.
present(message, interrupt) speaks and brailles control feedback.
echo(text, inserted) presents editor feedback; inserted is True for typed
characters and False for navigation, selection and deletion reports.
schedule(milliseconds, callback) returns a handle for cancel(handle).
"""
def __init__(
self,
present: Callable[[str, bool], None],
echo: Callable[[str, bool], None],
schedule: Callable[[int, Callable[[], bool]], object],
cancel: Callable[[object], None],
clock: Callable[[], float] = time.monotonic,
) -> None:
self._present = present
self._echo = echo
self._schedule = schedule
self._cancel = cancel
self._clock = clock
self._timer: object | None = None
self._pending: _Pending | None = None
self._focused = ""
self._protected = False
self._window = ""
self._last_key = ""
self._last_time = float("-inf")
self._held: tuple[str, list[str], object] | None = None
self._closed = False
def handle(self, event: WineEvent) -> bool:
"""Presents or queues one event. Returns True if it was accepted."""
if self._closed:
return False
kind = event.kind.casefold()
if kind not in EVENT_TYPES:
return False
if kind == "focus":
self._focused = event.source
self._protected = event.is_password
elif event.source != self._focused:
# Only list selections in the focused window may come from
# another source, e.g. a combo box's internal list.
window = self._pending.window if self._pending else self._window
if kind != "selection" or not event.window or event.window != window:
return False
if kind == "state":
self._protected = event.is_password
if self._protected and event.source == self._focused:
event.value = ""
if kind == "text":
if self._protected or not event.name:
return False
# Speak the queued focus first; never merge or drop editor actions.
self._flush()
self._echo(event.name, INSERTED_ACTION in event.states.split())
return True
parts = self._parts(event)
if not parts and not (kind == "focus" and event.window):
return False
message = ", ".join(parts)
key = f"{event.source}\0{message or event.window}"
# Losing focus reports a state event for the old control; stay quiet
# if nothing a user would hear has changed.
if kind == "state" and key == self._last_key:
return False
if key == self._last_key and self._clock() - self._last_time < DUPLICATE_SECONDS:
if kind == "focus":
self._pending = None
return False
if kind == "focus":
previous = self._pending
self._pending = _Pending(event.source, event.window, message, parts)
if previous is not None and previous.window == event.window:
self._pending.after = previous.after
if self._held is not None and self._held[0] == event.window:
self._pending.after.extend(self._held[1])
self._cancel(self._held[2])
self._held = None
if self._timer is None:
self._timer = self._schedule(FOCUS_DEBOUNCE_MS, self._on_timer)
return True
pending = self._pending
if pending is not None and (
pending.source == event.source or (event.window and pending.window == event.window)
):
# A value or selection inside the focus burst refines the focus.
pending.source, pending.message, pending.parts = event.source, message, parts
return True
self._last_key, self._last_time = key, self._clock()
self._present(message, False)
return True
@staticmethod
def _parts(event: WineEvent) -> list[str]:
role = "" if event.role.casefold() == "client" else event.role
parts = [part for part in (event.name, role, event.value, event.description) if part]
for label in event.labels:
if label in SPOKEN_STATES and label not in parts:
parts.append(label)
if event.position > 0 and event.count > 0:
parts.append(f"{event.position} of {event.count}")
return parts
def present_after_focus(self, text: str, window: str) -> None:
"""Presents text about a window, e.g. a dialog's message, after that
window's focus, so the focus announcement can't interrupt it."""
if self._closed or not text:
return
if self._pending is not None and self._pending.window == window:
self._pending.after.append(text)
return
if self._held is not None and self._held[0] == window:
self._held[1].append(text)
return
self._release_held()
# The focus may still be on its way, or may already have been spoken.
self._held = (window, [text], self._schedule(HOLD_MS, self._on_hold_timer))
def _on_hold_timer(self) -> bool:
held, self._held = self._held, None
if held is not None and not self._closed:
for text in held[1]:
self._present(text, False)
return False
def _release_held(self) -> None:
if self._held is not None:
self._cancel(self._held[2])
self._on_hold_timer()
def _on_timer(self) -> bool:
self._timer = None
self._flush()
return False
def _flush(self) -> None:
if self._timer is not None:
self._cancel(self._timer)
self._timer = None
pending, self._pending = self._pending, None
if pending is None or self._closed:
return
message = pending.message
if pending.window and pending.window != self._window:
# Wine windows are invisible to AT-SPI, so name a new window here.
if pending.window not in pending.parts:
message = f"{message}, {pending.window}" if message else pending.window
self._window = pending.window
self._last_key = f"{pending.source}\0{pending.message or pending.window}"
self._last_time = self._clock()
if message:
self._present(message, True)
for text in pending.after:
self._present(text, False)
def close(self) -> None:
"""Drops queued output; later events are ignored."""
self._closed = True
self._pending = None
if self._held is not None:
self._cancel(self._held[2])
self._held = None
if self._timer is not None:
self._cancel(self._timer)
self._timer = None