Read attached MATE menus outside the focused panel

Validate attached panel menus through fresh showing state and selected menu ancestors, and recognize explicit GTK menu focus without a FOCUSED state. Preserve X11 foreground ownership and reject stale menu focus and actions after dismissal.

Add native ownership and speech/braille regressions covering desktop, bottom panel, top panel, editor, reopening, submenus, and impostor rejection.

Validation: targeted native race tests, AT-SPI race suite, normal app suite, required speech regression check, build, vet, and parity ledger passed. The broader app race run exceeded its three-minute limit while compressing a Tentacle fixture. golangci-lint is unavailable; live MATE speech and physical braille acceptance remain pending.
This commit is contained in:
ak47 committed 2026-11-11 23:12:33 -05:00
1 parent 8592d3be11
commit f27dec8502
15 files changed
+206 -34

No files matched your search

+50 -2
View File
@@ -24,6 +24,24 @@ func TestNativeMatePanelMenuPresentation(t *testing.T) {
if os.Getenv("CTHULHU_NATIVE_PANEL_MENU") != "1" {
t.Skip("requires private Xorg/AT-SPI session")
}
for _, test := range []struct {
name, priorFocus string
attached bool
}{
{"detached", "editor", false},
{"attached over editor", "editor", true},
{"attached over desktop", "desktop", true},
{"attached over bottom panel", "bottom", true},
{"attached over top panel", "top", true},
} {
t.Run(test.name, func(t *testing.T) {
checkNativePanelMenuPresentation(t, test.attached, test.priorFocus)
})
}
}
func checkNativePanelMenuPresentation(t *testing.T, attached bool, priorFocus string) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
adapter := atspi.New()
@@ -33,7 +51,11 @@ func TestNativeMatePanelMenuPresentation(t *testing.T) {
defer adapter.Stop(context.Background())
start := func(name string) (io.WriteCloser, string) {
t.Helper()
command := exec.CommandContext(ctx, "/usr/bin/python3", "../../tests/gtk_panel_menu.py", name)
args := []string{"../../tests/gtk_panel_menu.py", name}
if attached && name != "editor" {
args = append(args, "--attached")
}
command := exec.CommandContext(ctx, "/usr/bin/python3", args...)
input, err := command.StdinPipe()
if err != nil {
t.Fatal(err)
@@ -54,7 +76,7 @@ func TestNativeMatePanelMenuPresentation(t *testing.T) {
}
return input, fields[1]
}
panel, _ := start("mate-panel")
panel, panelXID := start("mate-panel")
editor, xid := start("editor")
key := func(args ...string) {
t.Helper()
@@ -92,6 +114,29 @@ func TestNativeMatePanelMenuPresentation(t *testing.T) {
fmt.Fprintln(editor, "edit Foreground ready")
wait("Foreground ready")
for iteration := 0; iteration < 2; iteration++ {
switch priorFocus {
case "desktop":
key("windowfocus", "0")
for adapter.Foreground().Client.Window != 0 {
select {
case <-adapter.ForegroundChanges():
case <-ctx.Done():
t.Fatal("empty desktop foreground missing")
}
}
case "bottom":
output, err := exec.CommandContext(ctx, "xdotool", "search", "--name", "^Bottom panel fixture mate-panel$").Output()
if err != nil {
t.Fatal(err)
}
key("windowfocus", "--sync", strings.TrimSpace(string(output)))
fmt.Fprintln(panel, "bottom")
wait("Bottom panel ready")
case "top":
key("windowfocus", "--sync", panelXID)
fmt.Fprintf(panel, "edit Top panel ready %d\n", iteration)
wait("Top panel ready")
}
fmt.Fprintln(panel, "popup")
wait("menu")
// The initial bare menu may have no label. Navigation must acquire
@@ -104,6 +149,9 @@ func TestNativeMatePanelMenuPresentation(t *testing.T) {
wait(step.name)
}
key("key", "Escape")
if priorFocus != "editor" {
key("windowfocus", "--sync", xid)
}
text := fmt.Sprintf("Returned to editor %d", iteration)
fmt.Fprintln(editor, "edit "+text)
wait(text)
+1 -1
View File
@@ -70,7 +70,7 @@ func (adapter *Adapter) native_action_source(object ObjectID) (*C.AtspiAccessibl
defer C.g_object_unref(C.gpointer(window))
adapter.refreshForeground()
windowID := nativeObjectID(window)
if !adapter.matchesForeground(window, windowID) {
if !adapter.matchesForeground(source, window, windowID) {
return nil, ActionTarget{}, ErrStaleObject
}
role := C.atspi_accessible_get_role_name(source, nil)
+1 -1
View File
@@ -220,7 +220,7 @@ func (adapter *Adapter) activateNativeDocumentObject(ctx context.Context, positi
if window == nil {
return ErrStaleObject
}
foreground := adapter.matchesForeground(window, nativeObjectID(window))
foreground := adapter.matchesForeground(source, window, nativeObjectID(window))
C.g_object_unref(C.gpointer(window))
if !foreground {
return ErrStaleObject
@@ -287,9 +287,9 @@ func (adapter *Adapter) enrichNativeWindowEvent(event *C.AtspiEvent, target *C.A
focusedMenuItem := (eventType == FocusEvent || eventType == FocusedStateEvent) &&
(snapshot.Role == "menu item" || snapshot.Role == "check menu item" || snapshot.Role == "radio menu item") && snapshot.Focused
if eventType == WindowActivatedEvent {
focusEvent.ForegroundMismatch = !adapter.matchForeground(window, focusEvent.Window, &focusEvent.ForegroundCheck, false)
focusEvent.ForegroundMismatch = !adapter.matchForeground(target, window, focusEvent.Window, &focusEvent.ForegroundCheck, false)
} else {
focusEvent.ForegroundMismatch = !adapter.matchForeground(window, focusEvent.Window, nil, focusedMenuItem)
focusEvent.ForegroundMismatch = !adapter.matchForeground(target, window, focusEvent.Window, nil, focusedMenuItem)
}
if !focusEvent.ForegroundMismatch && window != nil &&
(eventType == WindowActivatedEvent || snapshot.Role == "table" || snapshot.Role == "table cell") {
@@ -58,7 +58,7 @@ func (adapter *Adapter) filterNativeBackgroundEvent(event *C.AtspiEvent, eventTy
focusedMenuItem = true
}
}
if adapter.matchForeground(window, windowID, nil, focusedMenuItem) {
if adapter.matchForeground(event.source, window, windowID, nil, focusedMenuItem) {
return false
}
if origin.BusName == adapter.foregroundWindow.BusName {
@@ -117,11 +117,12 @@ func normalizeNativeMenuFocus(event *C.AtspiEvent, target *C.AtspiAccessible, ev
// Shared Qt actions can report focus through their original, hidden menu
// even while another menu emits focus-gained for that very accessible.
// The explicit event establishes focus; the queried state may disagree.
// GTK menu containers also emit focus-gained without a FOCUSED state.
// Do not transfer this evidence to a redirected descendant or a dead item.
if snapshot.ID() == originID && !snapshot.Defunct &&
(eventType == FocusEvent || eventType == FocusedStateEvent && C.cthulhu_event_detail1(event) == 1) {
switch snapshot.Role {
case "menu item", "check menu item", "radio menu item":
case "menu", "menu item", "check menu item", "radio menu item":
snapshot.Focused = true
}
}
+39
View File
@@ -605,6 +605,45 @@ gboolean cthulhu_active_menu_window(AtspiAccessible *window) {
return menu;
}
// GTK attaches panel menus to their menu-bar item, so their accessible parent
// chain reaches the inactive panel rather than a separate popup window. Require
// a showing menu source and selected menu ancestors up to the menu bar. A panel
// window, ordinary control, closed menu, or hidden submenu grants no exception.
gboolean cthulhu_showing_attached_menu(AtspiAccessible *source, AtspiAccessible *window) {
if (source == NULL || window == NULL) return FALSE;
atspi_accessible_clear_cache_single(window);
AtspiStateSet *states = atspi_accessible_get_state_set(window);
gboolean available = states != NULL && atspi_state_set_contains(states, ATSPI_STATE_SHOWING) &&
!atspi_state_set_contains(states, ATSPI_STATE_ICONIFIED) &&
!atspi_state_set_contains(states, ATSPI_STATE_DEFUNCT);
if (states != NULL) g_object_unref(states);
if (!available) return FALSE;
AtspiAccessible *current = g_object_ref(source);
for (int depth = 0; current != NULL && depth < 32; depth++) {
atspi_accessible_clear_cache_single(current);
AtspiRole role = atspi_accessible_get_role(current, NULL);
if (role != ATSPI_ROLE_MENU && role != ATSPI_ROLE_MENU_ITEM &&
role != ATSPI_ROLE_CHECK_MENU_ITEM && role != ATSPI_ROLE_RADIO_MENU_ITEM) break;
states = atspi_accessible_get_state_set(current);
gboolean showing = states != NULL && atspi_state_set_contains(states, ATSPI_STATE_SHOWING) &&
!atspi_state_set_contains(states, ATSPI_STATE_DEFUNCT);
gboolean selected = states != NULL && atspi_state_set_contains(states, ATSPI_STATE_SELECTED);
if (states != NULL) g_object_unref(states);
if (!showing || (depth > 0 && !selected)) break;
AtspiAccessible *parent = atspi_accessible_get_parent(current, NULL);
if (parent == NULL) break;
if (atspi_accessible_get_role(parent, NULL) == ATSPI_ROLE_MENU_BAR) {
g_object_unref(parent);
g_object_unref(current);
return selected;
}
g_object_unref(current);
current = parent;
}
if (current != NULL) g_object_unref(current);
return FALSE;
}
static gboolean cthulhu_is_menu(AtspiAccessible *source) {
return atspi_accessible_get_role(source, NULL) == ATSPI_ROLE_MENU;
}
+16 -7
View File
@@ -62,11 +62,11 @@ func cthulhuAtspiForegroundCallback(data unsafe.Pointer) {
adapter.recoverForeground()
}
func (adapter *Adapter) matchesForeground(window *C.AtspiAccessible, id ObjectID) bool {
return adapter.matchForeground(window, id, nil, false)
func (adapter *Adapter) matchesForeground(source, window *C.AtspiAccessible, id ObjectID) bool {
return adapter.matchForeground(source, window, id, nil, false)
}
func (adapter *Adapter) matchForeground(window *C.AtspiAccessible, id ObjectID, check *ForegroundCheck, focusedMenuItem bool) bool {
func (adapter *Adapter) matchForeground(source, window *C.AtspiAccessible, id ObjectID, check *ForegroundCheck, focusedMenuItem bool) bool {
state := adapter.Foreground().Client
if !state.Known {
return true
@@ -85,7 +85,7 @@ func (adapter *Adapter) matchForeground(window *C.AtspiAccessible, id ObjectID,
// Desktop panel menus can grab the keyboard while X11 still names the
// previous application. Orca accepts their active, showing menu window.
// Keep the correlated X11 owner so dismissal can return to that client.
if adapter.is_panel_menu(window) {
if adapter.is_panel_menu(source, window) {
return true
}
if state.Window == 0 {
@@ -98,6 +98,12 @@ func (adapter *Adapter) matchForeground(window *C.AtspiAccessible, id ObjectID,
if adapter.foregroundWindow == id {
return true
}
// Panel menus must pass the fresh popup/attached-menu checks above.
// A delayed focused-item signal from a closed menu must not borrow
// another panel's same-process foreground ownership.
if adapter.applicationProcess(window) == "mate-panel" {
focusedMenuItem = false
}
matched := foregroundMenuPopupMatches(state, adapter.foregroundWindow, id, pid,
focusedMenuItem || C.cthulhu_active_menu_window(window) != 0)
if matched {
@@ -188,12 +194,15 @@ func (adapter *Adapter) matchForeground(window *C.AtspiAccessible, id ObjectID,
// The exception is for a native menu from the panel process, never an
// arbitrary panel control or a provider claiming the panel's accessible name.
// Query fresh window state on the GLib owner; no popup state is retained.
func (adapter *Adapter) is_panel_menu(window *C.AtspiAccessible) bool {
func (adapter *Adapter) is_panel_menu(source, window *C.AtspiAccessible) bool {
if adapter.applicationProcess(window) != "mate-panel" {
return false
}
C.atspi_accessible_clear_cache_single(window)
return C.cthulhu_active_menu_window(window) != 0
if C.cthulhu_active_menu_window(window) != 0 {
return true
}
return adapter.applicationProcess(source) == "mate-panel" && C.cthulhu_showing_attached_menu(source, window) != 0
}
func (adapter *Adapter) replacesForegroundWindow(window *C.AtspiAccessible, id ObjectID, foregroundPID, pid int) bool {
@@ -310,7 +319,7 @@ func (adapter *Adapter) recoverForeground() {
if window == nil {
continue
}
if state.Epoch == adapter.Foreground().Epoch && adapter.matchesForeground(window, nativeObjectID(window)) {
if state.Epoch == adapter.Foreground().Epoch && adapter.matchesForeground(window, window, nativeObjectID(window)) {
target := adapter.libreOfficeFrameTarget(window)
officeFrame := target != nil
if target == nil {
+1
View File
@@ -140,6 +140,7 @@ gboolean cthulhu_active_document(AtspiAccessible *source);
gboolean cthulhu_in_active_document(AtspiAccessible *source);
gboolean cthulhu_in_inactive_window(AtspiAccessible *source);
gboolean cthulhu_active_menu_window(AtspiAccessible *window);
gboolean cthulhu_showing_attached_menu(AtspiAccessible *source, AtspiAccessible *window);
AtspiAccessible *cthulhu_popup_menu_target(AtspiAccessible *window);
AtspiAccessible *cthulhu_inline_menu_container(AtspiAccessible *source);
const gchar *cthulhu_accessible_path(AtspiAccessible *source);
+1 -1
View File
@@ -108,7 +108,7 @@ func (adapter *Adapter) route_document_braille(ctx context.Context, route Braill
if window == nil {
return ErrStaleObject
}
foreground := adapter.matchesForeground(window, nativeObjectID(window))
foreground := adapter.matchesForeground(source, window, nativeObjectID(window))
C.g_object_unref(C.gpointer(window))
if !foreground {
return ErrStaleObject
+46 -9
View File
@@ -23,16 +23,24 @@ func TestNativeMatePanelMenuOutsideForeground(t *testing.T) {
for _, test := range []struct {
name, processName string
empty bool
attached bool
bottom bool
}{
{"over editor", "mate-panel", false},
{"empty desktop", "mate-panel", true},
{"impostor", "impostor", false},
{"over editor", "mate-panel", false, false, false},
{"empty desktop", "mate-panel", true, false, false},
{"impostor", "impostor", false, false, false},
{"attached over editor", "mate-panel", false, true, false},
{"attached empty desktop", "mate-panel", true, true, false},
{"attached over bottom panel", "mate-panel", false, true, true},
{"attached impostor", "impostor", false, true, false},
} {
t.Run(test.name, func(t *testing.T) { check_native_panel_menu(t, test.processName, test.empty) })
t.Run(test.name, func(t *testing.T) {
check_native_panel_menu(t, test.processName, test.empty, test.attached, test.bottom)
})
}
}
func check_native_panel_menu(t *testing.T, processName string, empty bool) {
func check_native_panel_menu(t *testing.T, processName string, empty, attached, bottom bool) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
@@ -43,7 +51,11 @@ func check_native_panel_menu(t *testing.T, processName string, empty bool) {
defer adapter.Stop(context.Background())
start := func(name string) (io.WriteCloser, string) {
t.Helper()
command := exec.CommandContext(ctx, "/usr/bin/python3", "../../tests/gtk_panel_menu.py", name)
args := []string{"../../tests/gtk_panel_menu.py", name}
if attached && name != "editor" {
args = append(args, "--attached")
}
command := exec.CommandContext(ctx, "/usr/bin/python3", args...)
input, err := command.StdinPipe()
if err != nil {
t.Fatal(err)
@@ -95,6 +107,16 @@ func check_native_panel_menu(t *testing.T, processName string, empty bool) {
}
fmt.Fprintln(editor, "edit Foreground ready")
owned := wait(func(event Event) bool { return event.Payload == "Foreground ready" && !event.ForegroundMismatch })
editorOwned := owned
if bottom {
output, err := exec.CommandContext(ctx, "xdotool", "search", "--name", "^Bottom panel fixture mate-panel$").Output()
if err != nil {
t.Fatal(err)
}
key("windowfocus", "--sync", strings.TrimSpace(string(output)))
fmt.Fprintln(panel, "bottom")
owned = wait(func(event Event) bool { return event.Payload == "Bottom panel ready" && !event.ForegroundMismatch })
}
if empty {
key("windowfocus", "0")
for adapter.Foreground().Client.Window != 0 {
@@ -108,6 +130,10 @@ func check_native_panel_menu(t *testing.T, processName string, empty bool) {
}
fmt.Fprintln(panel, "popup")
activation := wait(func(event Event) bool {
if attached && processName == "mate-panel" {
return event.Source.Role == "menu" && event.Source.ApplicationProcess == "mate-panel" &&
(event.Type == FocusEvent || event.Type == FocusedStateEvent && event.Detail1 == 1)
}
return event.Type == WindowActivatedEvent && event.Source.ApplicationName == "mate-panel"
})
if processName != "mate-panel" {
@@ -120,6 +146,7 @@ func check_native_panel_menu(t *testing.T, processName string, empty bool) {
if activation.ForegroundMismatch || activation.Foreground != owned.Foreground {
t.Fatalf("panel popup rejected with unchanged X11 foreground: %+v", activation)
}
var firstItem ObjectID
for _, step := range []struct{ key, name string }{
{"Down", "First application"}, {"Down", "Utilities"}, {"Right", "Example utility"},
{"Left", "Utilities"}, {"Down", "Last application"}, {"Up", "Utilities"},
@@ -131,15 +158,25 @@ func check_native_panel_menu(t *testing.T, processName string, empty bool) {
if event.ForegroundMismatch || event.Foreground != owned.Foreground || event.Source.ApplicationProcess != "mate-panel" {
t.Fatalf("panel navigation lost ownership: %+v", event)
}
if step.name == "First application" {
firstItem = event.Source.ID()
actions, err := adapter.AccessibleActions(ctx, firstItem)
if err != nil || actions.Target.Window != event.Window || len(actions.Actions) == 0 {
t.Fatalf("open menu actions lost ownership: %+v, %v", actions, err)
}
}
}
key("key", "Escape")
fmt.Fprintln(panel, "ordinary")
ordinary := wait(func(event Event) bool {
return event.Type == WindowActivatedEvent && event.Source.ApplicationName == "mate-panel"
return event.Type == WindowActivatedEvent && event.WindowTitle == "Panel menu fixture "+processName
})
if !ordinary.ForegroundMismatch {
t.Fatalf("ordinary panel window borrowed menu exception: %+v", ordinary)
}
if _, err := adapter.AccessibleActions(ctx, firstItem); err == nil {
t.Fatal("closed menu retained accessible action ownership")
}
fmt.Fprintln(panel, "stale")
wait(func(event Event) bool {
if event.Source.Name == "First application" && (event.Type == FocusEvent || event.Type == FocusedStateEvent) {
@@ -147,12 +184,12 @@ func check_native_panel_menu(t *testing.T, processName string, empty bool) {
}
return event.Payload == "Stale barrier"
})
if empty {
if empty || bottom {
key("windowfocus", "--sync", xid)
}
fmt.Fprintln(editor, "edit Foreground restored")
restored := wait(func(event Event) bool { return event.Payload == "Foreground restored" && !event.ForegroundMismatch })
if restored.Window != owned.Window || !empty && restored.Foreground != owned.Foreground {
if restored.Window != editorOwned.Window || !empty && !bottom && restored.Foreground != editorOwned.Foreground {
t.Fatalf("panel dismissal changed editor ownership: %+v", restored)
}
}
+1 -1
View File
@@ -41,7 +41,7 @@ func TestNativeWaylandForegroundPolicy(t *testing.T) {
if adapter.Foreground().Client.Known {
t.Fatal("X11 foreground used as Wayland desktop authority")
}
if !adapter.matchesForeground(nil, ObjectID{}) {
if !adapter.matchesForeground(nil, nil, ObjectID{}) {
t.Fatal("missing X11 foreground rejected Wayland accessibility")
}
}