annotate dep/animone/src/strategist.cc @ 342:adb79bdde329

dep/animone: fix tons of issues for example, the window ID stuff was just... completely wrong. since we're supporting multiple different window systems, it *has* to be a union rather than just a single integer type. HWND is also not a DWORD, it's a pointer(!), so now it's stored as a std::uintptr_t. (this probably breaks things)
author Paper <paper@paper.us.eu.org>
date Thu, 20 Jun 2024 03:03:05 -0400
parents 74e2365326c6
children 886f66775f31
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
1 #include <regex>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
2 #include <unordered_map>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
3
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
4 #include "animone.h"
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
5 #include "animone/a11y.h"
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
6 #include "animone/fd.h"
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
7 #include "animone/strategies.h"
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
8 #include "animone/util.h"
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
9
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
10 /* This file was changed lots from anisthesia. Most notably we don't use classes here
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
11 * anymore, and we just pass the result vector to different function that append
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
12 * to the result (which is better imo) */
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
13
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
14 namespace animone::internal {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
15
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
16 static bool ApplyWindowTitleFormat(const std::string& format, std::string& title) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
17 if (format.empty())
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
18 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
19
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
20 const std::regex pattern(format);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
21 std::smatch match;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
22 std::regex_match(title, match, pattern);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
23
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
24 // Use the first non-empty match result, because the regular expression may
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
25 // contain multiple sub-expressions.
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
26 for (size_t i = 1; i < match.size(); ++i) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
27 if (!match.str(i).empty()) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
28 title = match.str(i);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
29 return true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
30 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
31 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
32
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
33 // Results are empty, but the match was successful
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
34 if (!match.empty()) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
35 title.clear();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
36 return true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
37 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
38
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
39 return true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
40 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
41
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
42 static MediaInfoType InferMediaInformationType(const std::string& str) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
43 const std::regex path_pattern(R"(^(?:[A-Za-z]:[/\\]|\\\\)[^<>:"/\\|?*]+)");
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
44 return (std::regex_search(str, path_pattern)) ? MediaInfoType::File : MediaInfoType::Unknown;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
45 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
46
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
47 static bool AddMedia(Result& result, const MediaInfo media_information) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
48 if (media_information.value.empty())
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
49 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
50
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
51 Media media = {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
52 .information = {media_information}
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
53 };
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
54 result.media.push_back(std::move(media));
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
55
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
56 return true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
57 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
58
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
59 /* ------------------------------------------------------------------------- */
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
60 /* strategies */
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
61
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
62 static bool ApplyWindowTitleStrategy(std::vector<Result>& results) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
63 bool success = false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
64
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
65 for (auto& result : results) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
66 auto title = result.window.text;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
67 if (title.empty())
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
68 continue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
69
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
70 ApplyWindowTitleFormat(result.player.window_title_format, title);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
71
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
72 success |= AddMedia(result, {InferMediaInformationType(title), title});
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
73 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
74
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
75 return success;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
76 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
77
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
78 static bool ApplyOpenFilesStrategy(std::vector<Result>& results) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
79 bool success = false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
80
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
81 /* map pids to our results, saves time with open_file_proc */
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
82 std::unordered_map<pid_t, Result*> pid_map;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
83 std::set<pid_t> pids;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
84
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
85 for (Result& result : results) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
86 const pid_t pid = result.process.pid;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
87 if (!pid)
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
88 continue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
89
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
90 pid_map.insert({pid, &result});
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
91 pids.insert(pid);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
92 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
93
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
94 auto open_file_proc = [&](const OpenFile& file) -> bool {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
95 success |= AddMedia(*pid_map[file.pid], {MediaInfoType::File, file.path});
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
96 return true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
97 };
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
98
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
99 EnumerateOpenFiles(pids, open_file_proc);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
100
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
101 return success;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
102 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
103
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
104 static bool ApplyAccessibilityStrategy(std::vector<Result>& results) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
105 bool success = false;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
106
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
107 for (Result& result : results) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
108 auto web_browser_proc = [&result](const WebBrowserInformation& info) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
109 auto value = info.value;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
110
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
111 switch (info.type) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
112 case WebBrowserInformationType::Address:
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
113 AddMedia(result, {MediaInfoType::Url, value});
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
114 break;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
115 case WebBrowserInformationType::Title:
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
116 ApplyWindowTitleFormat(result.player.window_title_format, value);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
117 AddMedia(result, {MediaInfoType::Title, value});
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
118 break;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
119 case WebBrowserInformationType::Tab:
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
120 AddMedia(result, {MediaInfoType::Tab, value});
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
121 break;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
122 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
123 };
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
124
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
125 success |= GetWebBrowserInformation(result.window, web_browser_proc);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
126 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
127
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
128 return success;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
129 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
130
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
131 /* ------------------------------------------------------------------------- */
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
132
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
133 bool ApplyStrategies(std::vector<Result>& results) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
134 bool success = false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
135
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
136 success |= ApplyWindowTitleStrategy(results);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
137 success |= ApplyOpenFilesStrategy(results);
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
138 success |= ApplyAccessibilityStrategy(results);
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
139
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
140 return success;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
141 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
142
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
143 } // namespace animone::internal