comparison dep/animone/src/animone.cc @ 258:862d0d8619f6

*: HUUUGE changes animia has been renamed to animone, so instead of thinking of a health condition, you think of a beautiful flower :) I've also edited some of the code for animone, but I have no idea if it even works or not because I don't have a mac or windows machine lying around. whoops! ... anyway, all of the changes divergent from Anisthesia are now licensed under BSD. it's possible that I could even rewrite most of the code to where I don't even have to keep the MIT license, but that's thinking too far into the future I've been slacking off on implementing the anime seasons page, mostly out of laziness. I think I'd have to create another db file specifically for the seasons anyway, this code is being pushed *primarily* because the hard drive it's on is failing! yay :)
author Paper <paper@paper.us.eu.org>
date Mon, 01 Apr 2024 02:43:44 -0400
parents
children 1a6a5d3a94cd
comparison
equal deleted inserted replaced
257:699a20c57dc8 258:862d0d8619f6
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