Files
wine-access/msaa_events.cpp
deedra 4b58fc1cdc Keep Wine event feedback responsive when MSAA providers stall
Synchronous AccessibleObjectFromEvent and IAccessible property queries on
the helper's WinEvent thread could block native typing and focus feedback
while an application-owned accessibility provider stopped responding.

Move those calls into MsaaEventReader, a single COM worker that owns its
native references and exchanges copied requests and results with the event
thread. Bound both queues to 32 entries, invalidate queued work on focus or
foreground changes, and discard results older than 500 ms. Validate the
window, process, thread, generation, and foreground relationship before
presenting a result. Read protected state before querying accessible values.

Reject unrelated background presentation events before invoking providers
or invalidating foreground work. Preserve accessible events from visible
popups owned by the foreground window. Keep existing native menu, editor,
combo, list, and item-control paths, and use native focus fallback when an
MSAA focus request expires or fails.

Poll completed results from the existing event timer. On shutdown, retire
pending work and wait at most 100 ms for the worker; a blocked COM call keeps
only its own state until it completes or the helper process exits. Never
spawn additional workers behind an unresponsive provider.

Validation: CMake configure/build and git diff --check passed. On a private
dummy Xorg display, all four CTest checks passed, as did six event-stall
runs (default, --32-bit, --getter, --background, --popup, --reader orca)
and the existing menu, edit, controls, and list fixtures in both 64-bit and
32-bit modes. See the accompanying regression-test commit for commands.

Private recording-service fixtures do not establish audible speech, physical braille,
or acceptance of every live MUSHclient workflow. A provider that never
returns occupies the single MSAA worker until helper exit.
2026-10-12 23:31:03 -07:00

173 lines
6.8 KiB
C++

// SPDX-License-Identifier: LGPL-2.1-or-later
#include "msaa_events.h"
#include "msaa_states.h"
#include <oleacc.h>
#include <condition_variable>
#include <mutex>
#include <deque>
#include <atomic>
#include <utility>
namespace {
// MSAA notifications describe transient state, unlike ordered editor actions.
// Bound pending work and favor current focus if an application floods events.
constexpr size_t capacity = 32;
std::string text(BSTR value) {
if (!value) return {};
int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, value, -1, nullptr, 0, nullptr, nullptr);
if (size <= 1) return {};
std::string result(size, '\0');
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, value, -1, result.data(), size, nullptr, nullptr);
result.resize(size - 1);
return result;
}
std::string property(IAccessible *object,
HRESULT (STDMETHODCALLTYPE IAccessible::*getter)(VARIANT, BSTR *), VARIANT child) {
BSTR value = nullptr;
HRESULT hr = (object->*getter)(child, &value);
auto result = SUCCEEDED(hr) ? text(value) : std::string{};
SysFreeString(value);
return result;
}
struct Accessible {
IAccessible *object = nullptr;
VARIANT child{};
~Accessible() { if (object) object->Release(); VariantClear(&child); }
};
}
bool msaa_event_in_foreground(HWND window) {
HWND foreground = GetForegroundWindow();
HWND root = GetAncestor(window, GA_ROOT);
if (!foreground || !root) return false;
if (root == foreground) return true;
if (!IsWindowVisible(root) || !(GetWindowLongPtrW(root, GWL_STYLE) & WS_POPUP)) return false;
// Bound traversal even if a malformed owner chain cycles. Owned popups can
// expose MSAA focus while the foreground owner retains native keyboard focus.
for (unsigned depth = 0; root && depth < 32; ++depth) {
root = GetWindow(root, GW_OWNER);
if (root == foreground) return true;
}
return false;
}
struct MsaaEventReader::State {
std::mutex mutex;
std::condition_variable changed;
std::deque<MsaaEvent> pending;
std::deque<MsaaResult> ready;
std::atomic<bool> stopping{false};
uint64_t generation = 0;
bool current(const MsaaEvent &event) {
if (stopping || GetTickCount64() >= event.deadline ||
!msaa_event_in_foreground(event.window)) return false;
std::lock_guard<std::mutex> lock(mutex);
return generation == event.generation;
}
MsaaResult read(const MsaaEvent &event) {
MsaaResult result; result.source = event;
Accessible accessible;
if (!current(event) || FAILED(AccessibleObjectFromEvent(event.window, event.object,
event.child, &accessible.object, &accessible.child)) || !accessible.object)
return result;
if (!current(event)) return result;
VARIANT role{};
LONG roleId = 0;
if (SUCCEEDED(accessible.object->get_accRole(accessible.child, &role))) {
if (role.vt == VT_I4) {
roleId = role.lVal;
WCHAR label[128]{};
if (GetRoleTextW(roleId, label, 128)) result.role = text(label);
else result.role = std::to_string(roleId);
} else if (role.vt == VT_BSTR) result.role = text(role.bstrVal);
}
VariantClear(&role);
if (!current(event)) return result;
VARIANT state{};
if (SUCCEEDED(accessible.object->get_accState(accessible.child, &state)) && state.vt == VT_I4) {
result.states = msaa_states(static_cast<DWORD>(state.lVal), roleId);
result.password = state.lVal & STATE_SYSTEM_PROTECTED;
}
VariantClear(&state);
if (!current(event)) return result;
result.name = property(accessible.object, &IAccessible::get_accName, accessible.child);
if (!current(event)) return result;
// Protected values must be excluded before querying the provider.
if (!result.password) result.value = property(accessible.object, &IAccessible::get_accValue, accessible.child);
if (!current(event)) return result;
result.description = property(accessible.object, &IAccessible::get_accDescription, accessible.child);
if (!current(event)) return result;
accessible.object->get_accChildCount(&result.count);
result.native_client = accessible.child.vt == VT_I4 && accessible.child.lVal == CHILDID_SELF &&
(result.role.empty() || roleId == ROLE_SYSTEM_CLIENT);
result.available = true;
return result;
}
static DWORD WINAPI work(LPVOID pointer) {
std::unique_ptr<std::shared_ptr<State>> retained(static_cast<std::shared_ptr<State>*>(pointer));
auto state = *retained;
const HRESULT com = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
for (;;) {
MsaaEvent event;
{
std::unique_lock<std::mutex> lock(state->mutex);
state->changed.wait(lock, [&] { return state->stopping || !state->pending.empty(); });
if (state->stopping) break;
event = state->pending.front(); state->pending.pop_front();
}
auto result = SUCCEEDED(com) ? state->read(event) : MsaaResult{};
result.source = event;
if (!state->current(event)) continue;
std::lock_guard<std::mutex> lock(state->mutex);
if (state->stopping || event.generation != state->generation) continue;
if (state->ready.size() == capacity) state->ready.pop_front();
state->ready.push_back(std::move(result));
}
if (SUCCEEDED(com)) CoUninitialize();
return 0;
}
};
MsaaEventReader::~MsaaEventReader() { stop(); }
bool MsaaEventReader::start() {
if (state_) return false;
state_ = std::make_shared<State>();
auto retained = new std::shared_ptr<State>(state_);
worker_ = CreateThread(nullptr, 0, State::work, retained, 0, nullptr);
if (!worker_) { delete retained; state_.reset(); return false; }
return true;
}
void MsaaEventReader::invalidate(uint64_t generation) {
if (!state_) return;
std::lock_guard<std::mutex> lock(state_->mutex);
state_->generation = generation;
state_->pending.clear(); state_->ready.clear();
}
void MsaaEventReader::submit(const MsaaEvent &event) {
if (!state_) return;
std::lock_guard<std::mutex> lock(state_->mutex);
if (state_->stopping || state_->generation != event.generation) return;
if (state_->pending.size() == capacity) state_->pending.pop_front();
state_->pending.push_back(event); state_->changed.notify_one();
}
bool MsaaEventReader::take(MsaaResult &result) {
if (!state_) return false;
std::lock_guard<std::mutex> lock(state_->mutex);
if (state_->ready.empty()) return false;
result = std::move(state_->ready.front()); state_->ready.pop_front();
return true;
}
void MsaaEventReader::stop() {
if (!state_) return;
{
std::lock_guard<std::mutex> lock(state_->mutex);
state_->stopping = true; state_->pending.clear(); state_->ready.clear();
state_->changed.notify_one();
}
// A hung COM call cannot be safely terminated. It retains only its own
// shared state and native references until completion or process exit.
WaitForSingleObject(worker_, 100);
CloseHandle(worker_); worker_ = nullptr;
state_.reset();
}