annotate dep/animone/src/strategist.cc @ 285:65df2813d0de

dep/animone: header cleanup
author Paper <paper@paper.us.eu.org>
date Wed, 08 May 2024 16:43:32 -0400
parents 5437009cb10e
children b1f625b0227c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1 #include <regex>
285
65df2813d0de dep/animone: header cleanup
Paper <paper@paper.us.eu.org>
parents: 272
diff changeset
2 #include <unordered_map>
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
3
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
4 #include "animone.h"
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
5 #include "animone/fd.h"
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
6 #include "animone/strategies.h"
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
7 #include "animone/util.h"
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
8
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
9 /* this was STUPIDLY slow in Anisthesia, oops! */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
10
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
11 namespace animone::internal {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
12
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
13 static bool ApplyWindowTitleFormat(const std::string& format, std::string& title) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
14 if (format.empty())
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
15 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
16
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
17 const std::regex pattern(format);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
18 std::smatch match;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
19 std::regex_match(title, match, pattern);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
20
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
21 // Use the first non-empty match result, because the regular expression may
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
22 // contain multiple sub-expressions.
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
23 for (size_t i = 1; i < match.size(); ++i) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
24 if (!match.str(i).empty()) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
25 title = match.str(i);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
26 return true;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
27 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
28 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
29
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
30 // Results are empty, but the match was successful
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
31 if (!match.empty()) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
32 title.clear();
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
33 return true;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
34 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
35
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
36 return true;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
37 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
38
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
39 static MediaInfoType InferMediaInformationType(const std::string& str) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
40 const std::regex path_pattern(R"(^(?:[A-Za-z]:[/\\]|\\\\)[^<>:"/\\|?*]+)");
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
41 return (std::regex_search(str, path_pattern)) ? MediaInfoType::File : MediaInfoType::Unknown;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
42 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
43
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
44 static bool AddMedia(Result& result, const MediaInfo media_information) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
45 if (media_information.value.empty())
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
46 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
47
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
48 Media media;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
49 media.information.push_back(media_information);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
50 result.media.push_back(std::move(media));
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
51
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
52 return true;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
53 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
54
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
55 static bool ApplyWindowTitleStrategy(std::vector<Result>& results) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
56 bool success = false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
57
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
58 for (auto& result : results) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
59 auto title = result.window.text;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
60 if (title.empty())
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
61 continue;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
62
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
63 ApplyWindowTitleFormat(result.player.window_title_format, title);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
64
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
65 success |= AddMedia(result, {InferMediaInformationType(title), title});
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
66 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
67
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
68 return success;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
69 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
70
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
71 static bool ApplyOpenFilesStrategy(std::vector<Result>& results) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
72 bool success = false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
73
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
74 /* map pids to our results, saves time with open_file_proc */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
75 std::unordered_map<pid_t, Result*> pid_map;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
76 std::set<pid_t> pids;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
77
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
78 for (Result& result : results) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
79 const pid_t pid = result.process.pid;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
80 if (!pid)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
81 continue;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
82
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
83 pid_map.insert({pid, &result});
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
84 pids.insert(pid);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
85 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
86
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
87 auto open_file_proc = [&](const OpenFile& file) -> bool {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
88 success |= AddMedia(*pid_map[file.pid], {MediaInfoType::File, file.path});
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
89 return true;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
90 };
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
91
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
92 EnumerateOpenFiles(pids, open_file_proc);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
93
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
94 return success;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
95 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
96
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
97 bool ApplyStrategies(std::vector<Result>& results) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
98 bool success = false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
99
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
100 success |= ApplyWindowTitleStrategy(results);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
101 success |= ApplyOpenFilesStrategy(results);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
102
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
103 return success;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
104 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
105
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
106 ////////////////////////////////////////////////////////////////////////////////
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
107
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
108 } // namespace animone::internal