annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
1 #include <string>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
2 #include <vector>
146
d8a61e7e2a36 dep/animia: move fd stuff to a new fd.cc, don't force the user
Paper <mrpapersonic@gmail.com>
parents: 140
diff changeset
3 #include <set>
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
4
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
5 #include "animia.h"
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
6 #include "animia/util.h"
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
7 #include "animia/strategies.h"
139
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
8 #include "animia/types.h"
146
d8a61e7e2a36 dep/animia: move fd stuff to a new fd.cc, don't force the user
Paper <mrpapersonic@gmail.com>
parents: 140
diff changeset
9 #include "animia/fd.h"
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
10 #include "animia/win.h"
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
11
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
12 #include <iostream>
139
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
13
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
14 namespace animia {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
15
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
16 namespace internal {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
17
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
18 static bool IsExecutableInList(const Player& player, const std::string& name) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
19 std::string stem;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
20 #ifdef WIN32
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
21 if (!util::Stem(name, stem))
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
22 #endif
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
23 stem = name;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
24
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
25 for (const auto& pattern : player.executables)
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
26 if (util::CheckPattern(pattern, stem))
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
27 return true;
148
aa4df5a84338 dep/animia: use std::pair instead of std::tuple
Paper <mrpapersonic@gmail.com>
parents: 146
diff changeset
28
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
29 return false;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
30 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
31
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
32 static bool IsWindowInList(const Player& player, const std::string& name) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
33 for (const auto& pattern : player.windows)
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
34 if (util::CheckPattern(pattern, name))
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
35 return true;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
36
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
37 return false;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
38 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
39
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
40 static bool PlayerHasStrategy(const Player& player, const Strategy& strategy) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
41 for (const auto& pstrategy : player.strategies) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
42 if (pstrategy == strategy)
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
43 return true;
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
44 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
45 return false;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
46 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
47
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
48 } // namespace internal
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
49
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
50 bool GetResults(const std::vector<Player>& players, std::vector<Result>& results) {
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
51 /* Start out with file descriptors. */
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
52 auto process_proc = [&](const Process& process) -> bool {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
53 for (const auto& player : players) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
54 if (!internal::PlayerHasStrategy(player, Strategy::OpenFiles))
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
55 continue;
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
56
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
57 if (!internal::IsExecutableInList(player, process.name))
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
58 continue;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
59
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
60 results.push_back({ResultType::Process, player, process, {}, {}});
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
61 break;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
62 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
63 return true;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
64 };
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
65
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
66 if (!internal::fd.EnumerateOpenProcesses(process_proc))
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
67 return false;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
68
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
69 /* Then add our cool windows.
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
70 Note: X11 is stupid and there's no reliable way to get a PID from a given window.
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
71 This is because some windows might not even have a process attached to them.
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
72 We should set the PID of the process if we can get it, but that'll be for when
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
73 I can actually be arsed to implement the X11 backend. */
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
74 auto window_proc = [&](const Process& process, const Window& window) -> bool {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
75 for (const auto& player : players) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
76 if (!internal::PlayerHasStrategy(player, Strategy::WindowTitle))
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
77 continue;
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
78
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
79 if (!internal::IsWindowInList(player, window.class_name))
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
80 continue;
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
81
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
82 results.push_back({ResultType::Window, player, process, window, {}});
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
83 break;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
84 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
85 return true;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
86 };
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
87
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
88 if (!internal::win.EnumerateWindows(window_proc))
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 148
diff changeset
89 return false;
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
90
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
91 return internal::ApplyStrategies(results);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
92 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
93
140
1e696863b54c dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents: 139
diff changeset
94 }