// Copyright (c) 2026 Storm Dragon // SPDX-License-Identifier: GPL-3.0-or-later package wine import ( "context" "errors" "git.stormux.org/storm/cthulhu/internal/atspi" "github.com/godbus/dbus/v5" "time" ) func (r *ReviewRegistry) Capture(parent context.Context, id atspi.ObjectID) (*atspi.AccessibleTree, error) { if !IsReviewID(id) { return nil, ErrReviewUnavailable } ctx, cancel := context.WithTimeout(parent, 500*time.Millisecond) defer cancel() select { case r.captureGate <- struct{}{}: defer func() { <-r.captureGate }() case <-ctx.Done(): return nil, ctx.Err() } for attempt := 0; attempt < 2; attempt++ { r.mutex.Lock() p := r.providers[id.BusName] if p == nil || p.ctx.Err() != nil || p.notice.ID() != id { r.mutex.Unlock() return nil, ErrReviewUnavailable } notice, lifetime, connection, call := p.notice, p.ctx, r.service, r.captureCall r.mutex.Unlock() if connection == nil && call == nil { return nil, ErrReviewUnavailable } callCtx, callCancel := context.WithCancel(ctx) stop := context.AfterFunc(lifetime, callCancel) var fields map[string]dbus.Variant var err error if call != nil { fields, err = call(callCtx, id.BusName, notice.Path, notice) } else { reply := connection.Object(id.BusName, notice.Path).CallWithContext(callCtx, ReviewInterface+".CaptureWindow", 0, notice.Window, notice.Focus, notice.Revision) err = reply.Err if err == nil { // Store rebuilds variants from their decoded Go values and loses // tuple signatures. Validate the original owned reply body. if len(reply.Body) != 1 { err = ErrReviewInvalid } else { var ok bool fields, ok = reply.Body[0].(map[string]dbus.Variant) if !ok { err = ErrReviewInvalid } } } } stop() callCancel() if err != nil { return nil, err } if ctx.Err() != nil { return nil, ctx.Err() } snapshot, err := DecodeReviewSnapshot(fields) if errors.Is(err, ErrReviewStale) && attempt == 0 { continue } if err != nil { return nil, err } if snapshot.Instance != notice.Instance || snapshot.Window != notice.Window || snapshot.Focus != notice.Focus || snapshot.Revision != notice.Revision || snapshot.Bounds != notice.Bounds { return nil, ErrReviewStale } r.mutex.Lock() if r.providers[id.BusName] != p || p.notice != notice || lifetime.Err() != nil { r.mutex.Unlock() return nil, ErrReviewStale } tree, _, err := ApplyReviewSnapshot(p.trees[notice.Window], snapshot, id.BusName) if err == nil { p.trees[notice.Window] = tree } r.mutex.Unlock() return tree, err } return nil, ErrReviewStale }