comparison src/strategist.cc @ 31:668f4f31ddda

strategist: outward APIs are now in C
author Paper <paper@tflc.us>
date Mon, 10 Feb 2025 00:07:21 -0500
parents fac2b2d242d3
children
comparison
equal deleted inserted replaced
30:a76e55e098d1 31:668f4f31ddda
8 #include "animone/util.h" 8 #include "animone/util.h"
9 9
10 /* This file was changed lots from anisthesia. Most notably we don't use classes here 10 /* This file was changed lots from anisthesia. Most notably we don't use classes here
11 * anymore, and we just pass the result vector to different function that append 11 * anymore, and we just pass the result vector to different function that append
12 * to the result (which is better imo) */ 12 * to the result (which is better imo) */
13
14 namespace animone::internal {
15 13
16 static bool ApplyWindowTitleFormat(const std::string& format, std::string& title) { 14 static bool ApplyWindowTitleFormat(const std::string& format, std::string& title) {
17 if (format.empty()) 15 if (format.empty())
18 return false; 16 return false;
19 17
37 } 35 }
38 36
39 return true; 37 return true;
40 } 38 }
41 39
42 static MediaInfoType InferMediaInformationType(const std::string& str) { 40 static animone::MediaInfoType InferMediaInformationType(const std::string& str) {
43 const std::regex path_pattern(R"(^(?:[A-Za-z]:[/\\]|\\\\)[^<>:"/\\|?*]+)"); 41 const std::regex path_pattern(R"(^(?:[A-Za-z]:[/\\]|\\\\)[^<>:"/\\|?*]+)");
44 return (std::regex_search(str, path_pattern)) ? MediaInfoType::File : MediaInfoType::Unknown; 42 return (std::regex_search(str, path_pattern)) ? animone::MediaInfoType::File : animone::MediaInfoType::Unknown;
45 } 43 }
46 44
47 static bool AddMedia(Result& result, const MediaInfo media_information) { 45 static bool AddMedia(animone::Result *result, const animone::MediaInfo media_information) {
48 if (media_information.value.empty()) 46 if (media_information.value.empty())
49 return false; 47 return false;
50 48
51 Media media = { 49 animone::Media media;
52 .information = {media_information} 50 media.information = {media_information};
53 }; 51 result->media.push_back(std::move(media));
54 result.media.push_back(std::move(media));
55 52
56 return true; 53 return true;
57 } 54 }
58 55
59 /* ------------------------------------------------------------------------- */ 56 /* ------------------------------------------------------------------------- */
60 /* strategies */ 57 /* strategies */
61 58
62 static bool ApplyWindowTitleStrategy(std::vector<Result>& results) { 59 typedef bool (*strategy_spec_t)(animone::Result *results, size_t results_size);
60
61 static bool ApplyWindowTitleStrategy(animone::Result *results, size_t results_size) {
63 bool success = false; 62 bool success = false;
64 63
65 for (auto& result : results) { 64 for (size_t i = 0; i < results_size; i++) {
66 auto title = result.window.text; 65 auto title = results[i].window.text;
67 if (title.empty()) 66 if (title.empty())
68 continue; 67 continue;
69 68
70 ApplyWindowTitleFormat(result.player.window_title_format, title); 69 ApplyWindowTitleFormat(results[i].player.window_title_format, title);
71 70
72 success |= AddMedia(result, {InferMediaInformationType(title), title}); 71 success |= AddMedia(&results[i], {InferMediaInformationType(title), title});
73 } 72 }
74 73
75 return success; 74 return success;
76 } 75 }
77 76
78 static bool ApplyOpenFilesStrategy(std::vector<Result>& results) { 77 static bool ApplyOpenFilesStrategy(animone::Result *results, size_t results_size) {
79 bool success = false; 78 bool success = false;
80 79
81 /* map pids to our results, saves time with open_file_proc */ 80 std::set<animone::internal::pid_t> pids;
82 std::unordered_map<pid_t, Result*> pid_map;
83 std::set<pid_t> pids;
84 81
85 for (Result& result : results) { 82 for (size_t i = 0; i < results_size; i++)
86 const pid_t pid = result.process.pid; 83 pids.insert(results[i].process.pid);
87 if (!pid)
88 continue;
89 84
90 pid_map.insert({pid, &result}); 85 auto open_file_proc = [&](const animone::internal::OpenFile& file) -> bool {
91 pids.insert(pid); 86 animone::Result *result = NULL;
92 }
93 87
94 auto open_file_proc = [&](const OpenFile& file) -> bool { 88 for (size_t i = 0; i < results_size; i++) {
95 success |= AddMedia(*pid_map[file.pid], {MediaInfoType::File, file.path}); 89 if (results[i].process.pid == file.pid) {
90 result = &results[i];
91 break;
92 }
93 }
94
95 if (!result)
96 return false;
97
98 success |= AddMedia(result, {animone::MediaInfoType::File, file.path});
96 return true; 99 return true;
97 }; 100 };
98 101
99 EnumerateOpenFiles(pids, open_file_proc); 102 animone::internal::EnumerateOpenFiles(pids, open_file_proc);
100 103
101 return success; 104 return success;
102 } 105 }
103 106
104 static bool ApplyAccessibilityStrategy(std::vector<Result>& results) { 107 static bool ApplyAccessibilityStrategy(animone::Result *results, size_t results_size) {
105 bool success = false; 108 int success = 0;
106 109
107 for (Result& result : results) { 110 for (size_t i = 0; i < results_size; i++) {
108 auto web_browser_proc = [&result](const WebBrowserInformation& info) { 111 // TODO refactor our processing function specs to take in userdata
112
113 auto web_browser_proc = [results, i](const animone::internal::WebBrowserInformation& info) -> void {
109 auto value = info.value; 114 auto value = info.value;
110 115
111 switch (info.type) { 116 switch (info.type) {
112 case WebBrowserInformationType::Address: 117 case animone::internal::WebBrowserInformationType::Address:
113 AddMedia(result, {MediaInfoType::Url, value}); 118 AddMedia(&results[i], {animone::MediaInfoType::Url, value});
114 break; 119 break;
115 case WebBrowserInformationType::Title: 120 case animone::internal::WebBrowserInformationType::Title:
116 ApplyWindowTitleFormat(result.player.window_title_format, value); 121 ApplyWindowTitleFormat(results[i].player.window_title_format, value);
117 AddMedia(result, {MediaInfoType::Title, value}); 122 AddMedia(&results[i], {animone::MediaInfoType::Title, value});
118 break; 123 break;
119 case WebBrowserInformationType::Tab: 124 case animone::internal::WebBrowserInformationType::Tab:
120 AddMedia(result, {MediaInfoType::Tab, value}); 125 AddMedia(&results[i], {animone::MediaInfoType::Tab, value});
121 break; 126 break;
122 } 127 }
123 }; 128 };
124 129
125 success |= GetWebBrowserInformation(result, web_browser_proc); 130 success |= animone::internal::GetWebBrowserInformation(results[i], web_browser_proc);
126 } 131 }
127 132
128 return success; 133 return success;
129 } 134 }
130 135
131 /* ------------------------------------------------------------------------- */ 136 /* ------------------------------------------------------------------------- */
132 137
133 bool ApplyStrategies(std::vector<Result>& results) { 138 int animone_internal_ApplyStrategies(animone::Result *results, size_t results_size)
134 bool success = false; 139 {
140 static const strategy_spec_t strategies[] = {
141 ApplyWindowTitleStrategy,
142 ApplyOpenFilesStrategy,
143 ApplyAccessibilityStrategy
144 };
135 145
136 success |= ApplyWindowTitleStrategy(results); 146 int success = 0;
137 success |= ApplyOpenFilesStrategy(results); 147
138 success |= ApplyAccessibilityStrategy(results); 148 for (size_t i = 0; i < (sizeof(strategies)/sizeof(*strategies)); i++)
149 success |= strategies[i](results, results_size);
139 150
140 return success; 151 return success;
141 } 152 }
142 153
143 } // namespace animone::internal