annotate dep/animia/src/strategist.cc @ 252:a0eeb2cc7e6d

*: resolve make distcheck failures
author Paper <paper@paper.us.eu.org>
date Tue, 06 Feb 2024 02:24:49 -0500
parents 4d461ef7d424
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
1 #include <regex>
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
2
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
3 #include "animia.h"
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
4 #include "animia/fd.h"
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 #include "animia/strategies.h"
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6 #include "animia/util.h"
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
7
236
4d461ef7d424 HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents: 232
diff changeset
8 #include <iostream>
4d461ef7d424 HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents: 232
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) {
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
31 case ResultType::Process: success |= ApplyOpenFilesStrategy(); break;
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
32 case ResultType::Window: success |= ApplyWindowTitleStrategy(); break;
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents: 198
diff changeset
33 default: break;
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
34 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 return success;
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 bool ApplyStrategies(std::vector<Result>& results) {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40 bool success = false;
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 for (auto& result : results) {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 Strategist strategist(result);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44 success |= strategist.ApplyStrategies();
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 return success;
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 ////////////////////////////////////////////////////////////////////////////////
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
52 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
53 if (format.empty())
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
54 return false;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
55
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
56 const std::regex pattern(format);
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
57 std::smatch match;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
58 std::regex_match(title, match, pattern);
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
59
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
60 // 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
61 // contain multiple sub-expressions.
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
62 for (size_t i = 1; i < match.size(); ++i) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
63 if (!match.str(i).empty()) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
64 title = match.str(i);
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
65 return true;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
66 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
67 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
68
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
69 // Results are empty, but the match was successful
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
70 if (!match.empty()) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
71 title.clear();
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
72 return true;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
73 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
74
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 static MediaInfoType InferMediaInformationType(const std::string& str) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
79 const std::regex path_pattern(R"(^(?:[A-Za-z]:[/\\]|\\\\)[^<>:"/\\|?*]+)");
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
80 return (std::regex_search(str, path_pattern)) ? MediaInfoType::File : MediaInfoType::Unknown;
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
81 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
82
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
83 bool Strategist::ApplyWindowTitleStrategy() {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
84 auto title = result_.window.text;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
85 ApplyWindowTitleFormat(result_.player.window_title_format, title);
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 return AddMedia({InferMediaInformationType(title), title});
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
88 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
89
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
90 bool Strategist::ApplyOpenFilesStrategy() {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
91 bool success = false;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
92
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
93 const std::set<pid_t> pids{result_.process.pid};
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
94
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
95 auto open_file_proc = [&](const OpenFile& file) -> bool {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
96 success |= AddMedia({MediaInfoType::File, file.path});
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
97 return true;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
98 };
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 151
diff changeset
99
198
bc1ae1810855 dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
100 EnumerateOpenFiles(pids, open_file_proc);
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
101
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
102 return success;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
103 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
104
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
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
107 bool Strategist::AddMedia(const MediaInfo media_information) {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
108 if (media_information.value.empty())
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
109 return false;
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 Media media;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
112 media.information.push_back(media_information);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
113 result_.media.push_back(std::move(media));
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 return true;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
116 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
117
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
118 } // namespace animia::internal