Repository navigation
Expand file tree
/
Copy pathbt.cpp
More file actions
242 lines (210 loc) · 6.99 KB
/
Copy pathbt.cpp
File metadata and controls
242 lines (210 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <format>
#include <thread>
#include "globals.h"
#include "common/str.h"
#include "common/process.h"
#include "app/url_pipeline.h"
#include "app/rule_hit_log.h"
#include "app/url_opener.h"
#include "cmdline.h"
#include "app/discovery.h"
#if PLATFORM_WINDOWS
#include <windows.h>
#include <shellapi.h>
#include "common/win32/os.h"
#include "common/win32/window.h"
#endif
//ui
#include "app/ui/config_app.h"
#include "app/ui/picker_app.h"
#include "app/ui/toast_app.h"
using namespace std;
using namespace grey::common;
using namespace bt;
void open(click_payload cp, bool force_picker = false) {
g_pipeline.process(cp);
// decision whether to show picker or not
bool show_picker{force_picker};
optional<picker_invoked_reason> picker_reason;
bool picker_hotkey_down = ui::picker_app::is_hotkey_down();
vector<browser_match_result> rule_matches;
if(!force_picker) {
if(ui::picker_app::is_hotkey_down()) {
show_picker = true;
picker_reason = picker_invoked_reason::hotkey_down;
} else {
rule_matches = browser::match(g_state.browsers, cp, g_script);
if(rule_matches.size() > 1) {
show_picker = true;
picker_reason = picker_invoked_reason::rule_conflict;
} else if(rule_matches.size() == 1) {
if(g_state.picker.invoke.on_no_rule && rule_matches[0].rule.is_fallback) {
show_picker = true;
picker_reason = picker_invoked_reason::no_rule_defined;
}
}
}
} else {
picker_reason = picker_invoked_reason::forced;
}
if(show_picker) {
// choice narrows down only on rule conflict
vector<profile_selection> limited_choices;
if(picker_reason == picker_invoked_reason::rule_conflict) {
for(auto& match : rule_matches) {
limited_choices.push_back(match.profile);
}
}
ui::picker_app app{cp, limited_choices.empty() ? std::nullopt : std::make_optional(limited_choices)};
const auto pr = app.run();
if(pr) {
cp.url = pr.url;
url_opener::open(*pr.choice, cp);
if(g_state.log_rule_hits) {
rule_hit_log::i.write(cp, *pr.choice, picker_reason, nullopt);
}
}
} else {
auto matches = browser::match(g_state.browsers, cp, g_script);
browser_match_result& first_match = matches[0];
first_match.rule.apply_to(cp);
url_opener::open(first_match.profile, cp);
if(g_state.log_rule_hits) {
rule_hit_log::i.write(cp, first_match.profile, std::nullopt, matches[0]);
}
if(g_state.toast.enabled) {
ui::toast_app app{cp, first_match};
app.run();
}
}
}
static string get_command(const string& data, string& command_data) {
// Find the position of the first space character
size_t pos = data.find(' ');
// If no space is found, return the entire string
if(pos == std::string::npos) {
return "";
}
// Return the substring from the beginning up to the space character
command_data = data.substr(pos + 1);
return data.substr(0, pos);
}
static void execute(const string& data) {
// will be set to "true" if "pick" command is detected
bool force_picker{false};
string clean_data = data; // copy of the data, will be used for further processing
if(data.empty() || data.starts_with(ArgSplitter)) {
// if data starts with ArgSplitter that means command line is empty, so we invoke the configuration util
ui::config_app app;
app.run();
return;
}
// try to extract command from the data, which is the first word in the data string
string command_data;
string command = get_command(data, command_data);
if(!command.empty()) {
if(command == "pick") {
// force-invoke the picker
force_picker = true;
clean_data = command_data;
} else if(command == "browser") {
cmdline c;
c.exec(command, command_data);
return;
} else if(command == "discover") {
vector<browser> fresh_browsers = discovery::discover_all_browsers();
fresh_browsers = browser::merge(fresh_browsers, g_state.browsers);
g_state.browsers = fresh_browsers;
g_config.serialize();
return;
}
}
// if we reached this point, it's a normal operation mode
// 0 - url
// 1 - HWND
auto parts = str::split(clean_data, ArgSplitter, true);
string url = parts[0];
click_payload cp{.url = url, .raw_url = url};
#if PLATFORM_WINDOWS
cp.source_window_handle = reinterpret_cast<HWND>(str::to_ulong(parts[1], 16));
win32::window win{cp.source_window_handle};
cp.window_title = win.get_text();
auto pid = win.get_pid();
cp.process_id = std::to_string(pid);
process proc{pid};
cp.process_path = proc.get_path();
cp.process_name = proc.get_name();
cp.process_description = proc.get_description();
#endif
#if PLATFORM_LINUX
int pid = str::to_int(parts[1]);
process proc{pid};
cp.process_id = std::to_string(pid);
cp.process_name = proc.get_name();
cp.process_path = proc.get_path();
cp.process_description = proc.get_description();
#endif
#ifndef NDEBUG
if(command == "toast") {
cp.url = command_data;
browser_match_result bmr{browser::get_default(g_state.browsers).value(), match_rule{"a rule description"}};
ui::toast_app app{cp, bmr};
app.run();
return;
}
#endif
cp.prettify();
open(cp, force_picker); // open-up hahaha
}
string get_parent_arg() {
#if PLATFORM_WINDOWS
return format("{:x}", (DWORD)(win32::window::get_foreground().get_handle()));
#else
// on Linux we can use parent process ID
process proc;
auto parent = proc.get_parent();
return format("{}", parent.get_pid());
#endif
}
/**
* @brief Parses incoming arguments and shapes them into universal command format.
* @param args
* @return
*/
string parse_args(const vector<string>& args) {
string arg;
for(int i = 0; i < args.size(); i++) {
const string pt = args[i];
if(!arg.empty()) arg += ' ';
arg += pt;
}
string parent_arg = get_parent_arg();
return format("{}{}{}",
arg,
ArgSplitter,
parent_arg);
}
string parse_args(string arg) {
str::trim(arg, "\""); // arg can be quoted
string parent_arg = get_parent_arg();
return format("{}{}{}",
arg,
ArgSplitter,
parent_arg);
}
#if PLATFORM_WINDOWS
int wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
// ::MessageBox(NULL, pCmdLine, NULL, MB_OK);
execute(parse_args(str::to_str(pCmdLine)));
return 0;
}
#else
int main(int argc, char* argv[]) {
vector<string> args;
for (int i = 1; i < argc; i++) {
args.push_back(string(argv[i]));
}
execute(parse_args(args));
return 0;
}
#endif