-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathAccessibility.cpp
More file actions
196 lines (173 loc) · 6.39 KB
/
Copy pathAccessibility.cpp
File metadata and controls
196 lines (173 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "Common/UI/Accessibility.h"
#include <algorithm>
#include <exception>
#include <map>
#include <mutex>
#include "Common/Input/InputState.h"
#include "Common/Log.h"
#include "Common/System/NativeApp.h"
#include "Common/UI/Screen.h"
#include "Common/UI/UIScreen.h"
namespace UI {
static std::mutex g_accessibilitySnapshotLock;
static std::mutex g_accessibilityInputLock;
static std::vector<AccessibilityElementInfo> g_accessibilitySnapshot;
static uint64_t g_accessibilitySnapshotVersion;
static bool g_accessibilityEnabled;
static std::map<int, int> g_heldAccessibilityPointers;
static constexpr int ACCESSIBILITY_TAP_POINTER = 7;
static constexpr int ACCESSIBILITY_FIRST_HOLD_POINTER = 8;
static constexpr int ACCESSIBILITY_LAST_HOLD_POINTER = TOUCH_MAX_POINTERS - 1;
static bool BoundsEqual(const Bounds &a, const Bounds &b) {
return a.x == b.x && a.y == b.y && a.w == b.w && a.h == b.h;
}
static bool AccessibilitySnapshotsEqual(const std::vector<AccessibilityElementInfo> &a, const std::vector<AccessibilityElementInfo> &b) {
if (a.size() != b.size()) {
return false;
}
for (size_t i = 0; i < a.size(); ++i) {
if (a[i].id != b[i].id || a[i].label != b[i].label || !BoundsEqual(a[i].bounds, b[i].bounds) ||
a[i].role != b[i].role || a[i].enabled != b[i].enabled || a[i].checked != b[i].checked || a[i].selected != b[i].selected ||
a[i].clickable != b[i].clickable || a[i].longClickable != b[i].longClickable ||
a[i].touchX != b[i].touchX || a[i].touchY != b[i].touchY) {
return false;
}
}
return true;
}
std::vector<AccessibilityElementInfo> BuildAccessibilitySnapshot(ScreenManager *screenManager) {
std::vector<AccessibilityElementInfo> elements;
if (!screenManager) {
return elements;
}
Screen *screen = screenManager->topScreen();
UIScreen *uiScreen = dynamic_cast<UIScreen *>(screen);
if (!uiScreen) {
return elements;
}
uiScreen->GetAccessibilityElements(elements);
return elements;
}
void UpdateCachedAccessibilitySnapshot(ScreenManager *screenManager) {
std::vector<AccessibilityElementInfo> snapshot;
try {
snapshot = BuildAccessibilitySnapshot(screenManager);
} catch (const std::exception &e) {
WARN_LOG(Log::UI, "Accessibility snapshot failed: %s", e.what());
} catch (...) {
WARN_LOG(Log::UI, "Accessibility snapshot failed with unknown exception");
}
bool releaseInputs = false;
{
std::lock_guard<std::mutex> guard(g_accessibilitySnapshotLock);
if (!AccessibilitySnapshotsEqual(g_accessibilitySnapshot, snapshot)) {
const bool hadGamepadControls = std::any_of(g_accessibilitySnapshot.begin(), g_accessibilitySnapshot.end(), [](const AccessibilityElementInfo &info) {
return info.role == AccessibilityRole::GamepadControl;
});
const bool hasGamepadControls = std::any_of(snapshot.begin(), snapshot.end(), [](const AccessibilityElementInfo &info) {
return info.role == AccessibilityRole::GamepadControl;
});
releaseInputs = hadGamepadControls && !hasGamepadControls;
g_accessibilitySnapshot = std::move(snapshot);
++g_accessibilitySnapshotVersion;
NOTICE_LOG(Log::UI, "Accessibility snapshot updated: version=%llu elements=%zu",
(unsigned long long)g_accessibilitySnapshotVersion, g_accessibilitySnapshot.size());
}
}
if (releaseInputs) {
ReleaseAccessibilityInputs();
}
}
std::vector<AccessibilityElementInfo> GetCachedAccessibilitySnapshot() {
std::lock_guard<std::mutex> guard(g_accessibilitySnapshotLock);
return g_accessibilitySnapshot;
}
uint64_t GetCachedAccessibilitySnapshotVersion() {
std::lock_guard<std::mutex> guard(g_accessibilitySnapshotLock);
return g_accessibilitySnapshotVersion;
}
void ClearCachedAccessibilitySnapshot() {
ReleaseAccessibilityInputs();
std::lock_guard<std::mutex> guard(g_accessibilitySnapshotLock);
if (!g_accessibilitySnapshot.empty()) {
g_accessibilitySnapshot.clear();
++g_accessibilitySnapshotVersion;
}
}
void SetAccessibilityEnabled(bool enabled) {
if (!enabled) {
ClearCachedAccessibilitySnapshot();
}
std::lock_guard<std::mutex> guard(g_accessibilitySnapshotLock);
g_accessibilityEnabled = enabled;
}
bool IsAccessibilityEnabled() {
std::lock_guard<std::mutex> guard(g_accessibilitySnapshotLock);
return g_accessibilityEnabled;
}
bool FocusAccessibilityElement(ScreenManager *screenManager, int id) {
if (!screenManager) {
return false;
}
UIScreen *screen = dynamic_cast<UIScreen *>(screenManager->topScreen());
return screen && screen->FocusAccessibilityElement(id);
}
static void SendAccessibilityTouch(const AccessibilityElementInfo &info, int pointerId, TouchInputFlags flags) {
TouchInput touch{};
touch.id = pointerId;
touch.x = info.touchX;
touch.y = info.touchY;
touch.flags = flags;
NativeTouch(touch);
}
bool PerformAccessibilityClick(int id, bool longClick) {
AccessibilityElementInfo info;
{
std::lock_guard<std::mutex> guard(g_accessibilitySnapshotLock);
auto iter = std::find_if(g_accessibilitySnapshot.begin(), g_accessibilitySnapshot.end(), [id](const AccessibilityElementInfo &candidate) {
return candidate.id == id;
});
if (iter == g_accessibilitySnapshot.end() || !iter->enabled || !iter->clickable) {
return false;
}
info = *iter;
}
{
std::lock_guard<std::mutex> guard(g_accessibilityInputLock);
auto held = g_heldAccessibilityPointers.find(id);
if (held != g_heldAccessibilityPointers.end()) {
SendAccessibilityTouch(info, held->second, TouchInputFlags::UP);
g_heldAccessibilityPointers.erase(held);
return true;
}
if (!longClick || !info.longClickable) {
if (info.role == AccessibilityRole::Tab) {
return NativeAccessibilityClick(id);
}
SendAccessibilityTouch(info, ACCESSIBILITY_TAP_POINTER, TouchInputFlags::DOWN);
SendAccessibilityTouch(info, ACCESSIBILITY_TAP_POINTER, TouchInputFlags::UP);
return true;
}
for (int pointerId = ACCESSIBILITY_FIRST_HOLD_POINTER; pointerId <= ACCESSIBILITY_LAST_HOLD_POINTER; ++pointerId) {
const bool used = std::any_of(g_heldAccessibilityPointers.begin(), g_heldAccessibilityPointers.end(), [pointerId](const auto &entry) {
return entry.second == pointerId;
});
if (!used) {
SendAccessibilityTouch(info, pointerId, TouchInputFlags::DOWN);
g_heldAccessibilityPointers[id] = pointerId;
return true;
}
}
}
return false;
}
void ReleaseAccessibilityInputs() {
{
std::lock_guard<std::mutex> guard(g_accessibilityInputLock);
g_heldAccessibilityPointers.clear();
}
TouchInput touch{};
touch.flags = TouchInputFlags::RELEASE_ALL;
NativeTouch(touch);
}
} // namespace UI