comparison dep/animia/src/animia.cc @ 152:8700806c2cc2

dep/animia: awesome new breaking changes! I'm so tired
author Paper <mrpapersonic@gmail.com>
date Wed, 15 Nov 2023 02:34:59 -0500
parents aa4df5a84338
children bd439dd6ffc5
comparison
equal deleted inserted replaced
151:54744a48a7d7 152:8700806c2cc2
1 #include <string> 1 #include <string>
2 #include <vector> 2 #include <vector>
3 #include <set> 3 #include <set>
4 4
5 #include "animia.h" 5 #include "animia.h"
6 #include "animia/util.h"
6 #include "animia/strategies.h" 7 #include "animia/strategies.h"
7 #include "animia/types.h" 8 #include "animia/types.h"
8 #include "animia/fd.h" 9 #include "animia/fd.h"
10 #include "animia/win.h"
11
12 #include <iostream>
9 13
10 namespace animia { 14 namespace animia {
11 15
12 static bool ProcessInPlayers(const std::vector<Player>& players, std::string name, Player& player_) { 16 namespace internal {
13 for (const auto& player : players) {
14 for (const auto& exe : player.executables) {
15 /* this is only really relevant on Windows, and even then
16 executables can end in any number of extensions, so we should
17 really remove them all... */
18 auto pos = name.rfind(".exe");
19 if (pos != std::string::npos)
20 name = name.substr(0, pos);
21 17
22 if (exe == name) { 18 static bool IsExecutableInList(const Player& player, const std::string& name) {
23 player_ = player; 19 std::string stem;
24 return true; 20 #ifdef WIN32
25 } 21 if (!util::Stem(name, stem))
26 } 22 #endif
23 stem = name;
24
25 for (const auto& pattern : player.executables)
26 if (util::CheckPattern(pattern, stem))
27 return true;
28
29 return false;
30 }
31
32 static bool IsWindowInList(const Player& player, const std::string& name) {
33 for (const auto& pattern : player.windows)
34 if (util::CheckPattern(pattern, name))
35 return true;
36
37 return false;
38 }
39
40 static bool PlayerHasStrategy(const Player& player, const Strategy& strategy) {
41 for (const auto& pstrategy : player.strategies) {
42 if (pstrategy == strategy)
43 return true;
27 } 44 }
28 return false; 45 return false;
29 } 46 }
30 47
48 } // namespace internal
49
31 bool GetResults(const std::vector<Player>& players, std::vector<Result>& results) { 50 bool GetResults(const std::vector<Player>& players, std::vector<Result>& results) {
32 std::set<internal::pid_t> pids; 51 /* Start out with file descriptors. */
52 auto process_proc = [&](const Process& process) -> bool {
53 for (const auto& player : players) {
54 if (!internal::PlayerHasStrategy(player, Strategy::OpenFiles))
55 continue;
33 56
34 if (!internal::fd.GetAllPids(pids)) 57 if (!internal::IsExecutableInList(player, process.name))
58 continue;
59
60 results.push_back({ResultType::Process, player, process, {}, {}});
61 break;
62 }
63 return true;
64 };
65
66 if (!internal::fd.EnumerateOpenProcesses(process_proc))
35 return false; 67 return false;
36 68
37 for (const auto& pid : pids) { 69 /* Then add our cool windows.
38 std::string name; 70 Note: X11 is stupid and there's no reliable way to get a PID from a given window.
39 internal::fd.GetProcessName(pid, name); 71 This is because some windows might not even have a process attached to them.
72 We should set the PID of the process if we can get it, but that'll be for when
73 I can actually be arsed to implement the X11 backend. */
74 auto window_proc = [&](const Process& process, const Window& window) -> bool {
75 for (const auto& player : players) {
76 if (!internal::PlayerHasStrategy(player, Strategy::WindowTitle))
77 continue;
40 78
41 Player player; 79 if (!internal::IsWindowInList(player, window.class_name))
42 if (!ProcessInPlayers(players, name, player)) 80 continue;
43 continue;
44 81
45 Result result; 82 results.push_back({ResultType::Window, player, process, window, {}});
46 result.process.pid = pid; 83 break;
47 result.process.name = name; 84 }
48 result.player = player; 85 return true;
49 results.push_back(result); 86 };
50 } 87
88 if (!internal::win.EnumerateWindows(window_proc))
89 return false;
51 90
52 return internal::ApplyStrategies(results); 91 return internal::ApplyStrategies(results);
53 } 92 }
54 93
55 } 94 }