-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rs
303 lines (261 loc) · 9.56 KB
/
main.rs
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use clap::Parser;
use color_eyre::Result;
use std::path::PathBuf;
use windows::core::Result as WindowsCrateResult;
use windows::Win32::Foundation::HWND;
use windows::Win32::Foundation::POINT;
use windows::Win32::UI::Input::KeyboardAndMouse::SendInput;
use windows::Win32::UI::Input::KeyboardAndMouse::INPUT;
use windows::Win32::UI::Input::KeyboardAndMouse::INPUT_MOUSE;
use windows::Win32::UI::WindowsAndMessaging::GetCursorPos;
use windows::Win32::UI::WindowsAndMessaging::GetForegroundWindow;
use windows::Win32::UI::WindowsAndMessaging::RealGetWindowClassW;
use windows::Win32::UI::WindowsAndMessaging::SetForegroundWindow;
use windows::Win32::UI::WindowsAndMessaging::SetWindowPos;
use windows::Win32::UI::WindowsAndMessaging::WindowFromPoint;
use windows::Win32::UI::WindowsAndMessaging::HWND_TOP;
use windows::Win32::UI::WindowsAndMessaging::SWP_NOMOVE;
use windows::Win32::UI::WindowsAndMessaging::SWP_NOSIZE;
use windows::Win32::UI::WindowsAndMessaging::SWP_SHOWWINDOW;
use winput::message_loop;
use winput::message_loop::Event;
const CLASS_ALLOWLIST: [&str; 1] = [
"Chrome_RenderWidgetHostHWND", // gross electron apps
];
const CLASS_BLOCKLIST: [&str; 5] = [
"SHELLDLL_DefView", // desktop window
"Shell_TrayWnd", // tray
"TrayNotifyWnd", // tray
"MSTaskSwWClass", // start bar icons
"Windows.UI.Core.CoreWindow", // start menu
];
#[derive(Parser)]
#[clap(author, about, version)]
struct Opts {
/// Path to a file with known focus-able HWNDs (e.g. komorebi.hwnd.json)
#[clap(long)]
hwnds: Option<PathBuf>,
}
fn main() -> Result<()> {
let opts: Opts = Opts::parse();
let hwnds = match opts.hwnds {
None => {
let hwnds: PathBuf = dirs::data_local_dir()
.expect("there is no local data directory")
.join("komorebi")
.join("komorebi.hwnd.json");
// TODO: We can add checks for other window managers here
if hwnds.is_file() {
Some(hwnds)
} else {
None
}
}
Some(hwnds) => {
if hwnds.is_file() {
Some(hwnds)
} else {
None
}
}
};
if std::env::var("RUST_LIB_BACKTRACE").is_err() {
std::env::set_var("RUST_LIB_BACKTRACE", "1");
}
color_eyre::install()?;
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "info");
}
tracing::subscriber::set_global_default(
tracing_subscriber::fmt::Subscriber::builder()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.finish(),
)?;
listen_for_movements(hwnds.clone());
match hwnds {
None => tracing::info!("masir is now running"),
Some(hwnds) => tracing::info!(
"masir is now running, and additionally checking hwnds against {}",
hwnds.display()
),
}
let (ctrlc_sender, ctrlc_receiver) = std::sync::mpsc::channel();
ctrlc::set_handler(move || {
ctrlc_sender
.send(())
.expect("could not send signal on ctrl-c channel");
})?;
ctrlc_receiver
.recv()
.expect("could not receive signal on ctrl-c channel");
tracing::info!("received ctrl-c, exiting");
Ok(())
}
pub fn listen_for_movements(hwnds: Option<PathBuf>) {
std::thread::spawn(move || {
let receiver = message_loop::start().expect("could not start winput message loop");
loop {
if let Event::MouseMoveRelative { .. } = receiver.next_event() {
if let (Ok(cursor_pos_hwnd), Ok(foreground_hwnd)) =
(window_at_cursor_pos(), foreground_window())
{
if cursor_pos_hwnd != foreground_hwnd {
let mut should_raise = false;
// step one: test against known classes
if let Ok(class) = real_window_class_w(cursor_pos_hwnd) {
// fail fast, exit this iteration of the loop and avoid any processing
// if we hit a blocklist entry
if CLASS_BLOCKLIST.contains(&&*class) {
tracing::debug!("window class {class} is blocklisted");
continue;
}
if CLASS_ALLOWLIST.contains(&&*class) {
tracing::debug!("window class {class} is allowlisted");
should_raise = true;
}
if !should_raise {
tracing::trace!("window class is {class}");
}
}
// step two: if available, test against known hwnds
if !should_raise {
if let Some(hwnds) = &hwnds {
if let Ok(raw_hwnds) = std::fs::read_to_string(hwnds) {
if raw_hwnds.contains(&cursor_pos_hwnd.to_string()) {
tracing::debug!(
"hwnd {cursor_pos_hwnd} was found in {}",
hwnds.display()
);
should_raise = true;
}
}
}
}
if should_raise {
match raise_and_focus_window(cursor_pos_hwnd) {
Ok(_) => {
tracing::info!("raised hwnd {cursor_pos_hwnd}");
}
Err(error) => {
tracing::error!(
"failed to raise hwnd {cursor_pos_hwnd}: {error}"
);
}
}
}
}
}
}
}
});
}
macro_rules! as_ptr {
($value:expr) => {
$value as *mut core::ffi::c_void
};
}
pub enum WindowsResult<T, E> {
Err(E),
Ok(T),
}
macro_rules! impl_from_integer_for_windows_result {
( $( $integer_type:ty ),+ ) => {
$(
impl From<$integer_type> for WindowsResult<$integer_type, color_eyre::eyre::Error> {
fn from(return_value: $integer_type) -> Self {
match return_value {
0 => Self::Err(std::io::Error::last_os_error().into()),
_ => Self::Ok(return_value),
}
}
}
)+
};
}
impl_from_integer_for_windows_result!(usize, isize, u16, u32, i32);
impl<T, E> From<WindowsResult<T, E>> for Result<T, E> {
fn from(result: WindowsResult<T, E>) -> Self {
match result {
WindowsResult::Err(error) => Err(error),
WindowsResult::Ok(ok) => Ok(ok),
}
}
}
pub trait ProcessWindowsCrateResult<T> {
fn process(self) -> Result<T>;
}
macro_rules! impl_process_windows_crate_integer_wrapper_result {
( $($input:ty => $deref:ty),+ $(,)? ) => (
paste::paste! {
$(
impl ProcessWindowsCrateResult<$deref> for $input {
fn process(self) -> Result<$deref> {
if self == $input(std::ptr::null_mut()) {
Err(std::io::Error::last_os_error().into())
} else {
Ok(self.0 as $deref)
}
}
}
)+
}
);
}
impl_process_windows_crate_integer_wrapper_result!(
HWND => isize,
);
impl<T> ProcessWindowsCrateResult<T> for WindowsCrateResult<T> {
fn process(self) -> Result<T> {
match self {
Ok(value) => Ok(value),
Err(error) => Err(error.into()),
}
}
}
pub fn window_from_point(point: POINT) -> Result<isize> {
unsafe { WindowFromPoint(point) }.process()
}
pub fn window_at_cursor_pos() -> Result<isize> {
window_from_point(cursor_pos()?)
}
pub fn foreground_window() -> Result<isize> {
unsafe { GetForegroundWindow() }.process()
}
pub fn cursor_pos() -> Result<POINT> {
let mut cursor_pos = POINT::default();
unsafe { GetCursorPos(&mut cursor_pos) }.process()?;
Ok(cursor_pos)
}
pub fn raise_and_focus_window(hwnd: isize) -> Result<()> {
let event = [INPUT {
r#type: INPUT_MOUSE,
..Default::default()
}];
unsafe {
// Send an input event to our own process first so that we pass the
// foreground lock check
SendInput(&event, size_of::<INPUT>() as i32);
// Error ignored, as the operation is not always necessary.
let _ = SetWindowPos(
HWND(as_ptr!(hwnd)),
HWND_TOP,
0,
0,
0,
0,
SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW,
)
.process();
SetForegroundWindow(HWND(as_ptr!(hwnd)))
}
.ok()
.process()
}
pub fn real_window_class_w(hwnd: isize) -> Result<String> {
const BUF_SIZE: usize = 512;
let mut class: [u16; BUF_SIZE] = [0; BUF_SIZE];
let len = Result::from(WindowsResult::from(unsafe {
RealGetWindowClassW(HWND(as_ptr!(hwnd)), &mut class)
}))?;
Ok(String::from_utf16(&class[0..len as usize])?)
}