258
|
1 #include "animone.h"
|
|
2 #include "animone/fd.h"
|
|
3 #include "animone/strategies.h"
|
|
4 #include "animone/types.h"
|
|
5 #include "animone/util.h"
|
|
6 #include "animone/win.h"
|
|
7
|
|
8 #include <set>
|
|
9 #include <string>
|
|
10 #include <vector>
|
|
11
|
|
12 namespace animone {
|
|
13
|
|
14 namespace internal {
|
|
15
|
|
16 static bool IsExecutableInList(const Player& player, const std::string& name) {
|
|
17 std::string stem;
|
|
18 #ifdef WIN32
|
|
19 if (!util::Stem(name, stem))
|
|
20 #endif
|
|
21 stem = name;
|
|
22
|
|
23 for (const auto& pattern : player.executables)
|
|
24 if (util::CheckPattern(pattern, stem))
|
|
25 return true;
|
|
26
|
|
27 return false;
|
|
28 }
|
|
29
|
|
30 static bool IsWindowInList(const Player& player, const Window& window) {
|
|
31 for (const auto& pattern : player.windows)
|
|
32 if (util::CheckPattern(pattern, window.class_name))
|
|
33 return true;
|
|
34
|
|
35 return false;
|
|
36 }
|
|
37
|
|
38 static bool PlayerHasStrategy(const Player& player, const Strategy& strategy) {
|
|
39 for (const auto& pstrategy : player.strategies)
|
|
40 if (pstrategy == strategy)
|
|
41 return true;
|
|
42
|
|
43 return false;
|
|
44 }
|
|
45
|
|
46 } // namespace internal
|
|
47
|
|
48 bool GetResults(const std::vector<Player>& players, std::vector<Result>& results) {
|
|
49 auto window_proc = [&](const Process& process, const Window& window) -> bool {
|
|
50 for (const auto& player : players) {
|
|
51 if (internal::IsWindowInList(player, window))
|
|
52 results.push_back({player, process, window, {}});
|
|
53 }
|
|
54
|
|
55 return true;
|
|
56 };
|
|
57
|
|
58 if (internal::EnumerateWindows(window_proc))
|
|
59 return internal::ApplyStrategies(results);
|
|
60
|
|
61 /* fallback, enumerate over open processes instead */
|
|
62 auto process_proc = [&](const Process& process) -> bool {
|
|
63 for (const auto& player : players)
|
|
64 if (internal::IsExecutableInList(player, process.name))
|
|
65 results.push_back({player, process, {}, {}});
|
|
66
|
|
67 return true;
|
|
68 };
|
|
69
|
|
70 if (internal::EnumerateOpenProcesses(process_proc))
|
|
71 return internal::ApplyStrategies(results);
|
|
72
|
|
73 return false;
|
|
74 }
|
|
75
|
|
76 } // namespace animone
|