Mercurial > minori
annotate dep/animia/src/strategist.cc @ 155:d2bbb5773616
dep/animia: add quartz backend for windows
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 15 Nov 2023 15:24:39 -0500 |
parents | bd439dd6ffc5 |
children | cdf79282d647 |
rev | line source |
---|---|
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
1 #include "animia/strategies.h" |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
2 #include "animia/util.h" |
139
478f3b366199
dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents:
138
diff
changeset
|
3 #include "animia/fd.h" |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
4 #include "animia.h" |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
5 |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
6 #include <iostream> |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
7 |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
8 #include <regex> |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
9 |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
10 namespace animia::internal { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
11 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
12 class Strategist { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
13 public: |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
14 Strategist(Result& result) : result_(result) {} |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
15 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
16 bool ApplyStrategies(); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
17 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
18 private: |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
19 bool AddMedia(const MediaInfo media_information); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
20 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
21 bool ApplyOpenFilesStrategy(); |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
22 bool ApplyWindowTitleStrategy(); |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
23 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
24 Result& result_; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
25 }; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
26 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
27 bool Strategist::ApplyStrategies() { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
28 bool success = false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
29 |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
30 switch (result_.type) { |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
31 case ResultType::Process: |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
32 success |= ApplyOpenFilesStrategy(); |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
33 break; |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
34 case ResultType::Window: |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
35 success |= ApplyWindowTitleStrategy(); |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
36 break; |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
37 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
38 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
39 return success; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
40 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
41 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
42 bool ApplyStrategies(std::vector<Result>& results) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
43 bool success = false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
44 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
45 for (auto& result : results) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
46 Strategist strategist(result); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
47 success |= strategist.ApplyStrategies(); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
48 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
49 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
50 return success; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
51 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
52 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
53 //////////////////////////////////////////////////////////////////////////////// |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
54 |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
55 static bool ApplyWindowTitleFormat(const std::string& format, std::string& title) { |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
56 if (format.empty()) |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
57 return false; |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
58 |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
59 const std::regex pattern(format); |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
60 std::smatch match; |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
61 std::regex_match(title, match, pattern); |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
62 |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
63 // Use the first non-empty match result, because the regular expression may |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
64 // contain multiple sub-expressions. |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
65 for (size_t i = 1; i < match.size(); ++i) { |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
66 if (!match.str(i).empty()) { |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
67 title = match.str(i); |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
68 return true; |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
69 } |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
70 } |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
71 |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
72 // Results are empty, but the match was successful |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
73 if (!match.empty()) { |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
74 title.clear(); |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
75 return true; |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
76 } |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
77 |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
78 return true; |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
79 } |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
80 |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
81 static MediaInfoType InferMediaInformationType(const std::string& str) { |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
82 const std::regex path_pattern(R"(^(?:[A-Za-z]:[/\\]|\\\\)[^<>:"/\\|?*]+)"); |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
83 return (std::regex_search(str, path_pattern)) |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
84 ? MediaInfoType::File : MediaInfoType::Unknown; |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
85 } |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
86 |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
87 bool Strategist::ApplyWindowTitleStrategy() { |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
88 auto title = result_.window.text; |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
89 ApplyWindowTitleFormat(result_.player.window_title_format, title); |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
90 |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
91 return AddMedia({InferMediaInformationType(title), title}); |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
92 } |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
93 |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
94 bool Strategist::ApplyOpenFilesStrategy() { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
95 bool success = false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
96 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
97 const std::set<pid_t> pids{result_.process.pid}; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
98 |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
99 auto open_file_proc = [&](const OpenFile& file) -> bool { |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
100 success |= AddMedia({MediaInfoType::File, file.path}); |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
101 return true; |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
102 }; |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
103 |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
104 fd.EnumerateOpenFiles(pids, open_file_proc); |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
105 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
106 return success; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
107 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
108 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
109 //////////////////////////////////////////////////////////////////////////////// |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
110 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
111 bool Strategist::AddMedia(const MediaInfo media_information) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
112 if (media_information.value.empty()) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
113 return false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
114 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
115 Media media; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
116 media.information.push_back(media_information); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
117 result_.media.push_back(std::move(media)); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
118 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
119 return true; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
120 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
121 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
122 } |