|
1 | 1 | #![deny(clippy::unwrap_used, clippy::expect_used)]
|
2 | 2 |
|
3 | 3 | use crate::border_manager;
|
| 4 | +use crate::notify_subscribers; |
| 5 | +use crate::winevent::WinEvent; |
| 6 | +use crate::NotificationEvent; |
| 7 | +use crate::Window; |
4 | 8 | use crate::WindowManager;
|
| 9 | +use crate::WindowManagerEvent; |
| 10 | +use crate::DATA_DIR; |
| 11 | + |
| 12 | +use crossbeam_channel::Receiver; |
| 13 | +use crossbeam_channel::Sender; |
| 14 | +use lazy_static::lazy_static; |
5 | 15 | use parking_lot::Mutex;
|
| 16 | +use std::collections::HashMap; |
| 17 | +use std::fs::OpenOptions; |
6 | 18 | use std::sync::Arc;
|
| 19 | +use std::sync::OnceLock; |
7 | 20 | use std::time::Duration;
|
8 | 21 |
|
9 |
| -pub fn watch_for_orphans(wm: Arc<Mutex<WindowManager>>) { |
| 22 | +lazy_static! { |
| 23 | + pub static ref HWNDS_CACHE: Arc<Mutex<HashMap<isize, (usize, usize)>>> = |
| 24 | + Arc::new(Mutex::new(HashMap::new())); |
| 25 | +} |
| 26 | + |
| 27 | +pub struct ReaperNotification(pub HashMap<isize, (usize, usize)>); |
| 28 | + |
| 29 | +static CHANNEL: OnceLock<(Sender<ReaperNotification>, Receiver<ReaperNotification>)> = |
| 30 | + OnceLock::new(); |
| 31 | + |
| 32 | +pub fn channel() -> &'static (Sender<ReaperNotification>, Receiver<ReaperNotification>) { |
| 33 | + CHANNEL.get_or_init(|| crossbeam_channel::bounded(50)) |
| 34 | +} |
| 35 | + |
| 36 | +fn event_tx() -> Sender<ReaperNotification> { |
| 37 | + channel().0.clone() |
| 38 | +} |
| 39 | + |
| 40 | +fn event_rx() -> Receiver<ReaperNotification> { |
| 41 | + channel().1.clone() |
| 42 | +} |
| 43 | + |
| 44 | +pub fn send_notification(hwnds: HashMap<isize, (usize, usize)>) { |
| 45 | + if event_tx().try_send(ReaperNotification(hwnds)).is_err() { |
| 46 | + tracing::warn!("channel is full; dropping notification") |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +pub fn listen_for_notifications(wm: Arc<Mutex<WindowManager>>) { |
| 51 | + watch_for_orphans(wm.clone()); |
| 52 | + |
10 | 53 | std::thread::spawn(move || loop {
|
11 |
| - match find_orphans(wm.clone()) { |
| 54 | + match handle_notifications(wm.clone()) { |
12 | 55 | Ok(()) => {
|
13 | 56 | tracing::warn!("restarting finished thread");
|
14 | 57 | }
|
15 | 58 | Err(error) => {
|
16 |
| - if cfg!(debug_assertions) { |
17 |
| - tracing::error!("restarting failed thread: {:?}", error) |
18 |
| - } else { |
19 |
| - tracing::error!("restarting failed thread: {}", error) |
20 |
| - } |
| 59 | + tracing::warn!("restarting failed thread: {}", error); |
21 | 60 | }
|
22 | 61 | }
|
23 | 62 | });
|
24 | 63 | }
|
25 | 64 |
|
26 |
| -pub fn find_orphans(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result<()> { |
27 |
| - tracing::info!("watching"); |
28 |
| - |
29 |
| - let arc = wm.clone(); |
| 65 | +fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result<()> { |
| 66 | + tracing::info!("listening"); |
30 | 67 |
|
31 |
| - loop { |
32 |
| - std::thread::sleep(Duration::from_secs(1)); |
| 68 | + let receiver = event_rx(); |
33 | 69 |
|
34 |
| - let mut wm = arc.lock(); |
| 70 | + for notification in receiver { |
| 71 | + let orphan_hwnds = notification.0; |
| 72 | + let mut wm = wm.lock(); |
35 | 73 | let offset = wm.work_area_offset;
|
36 | 74 |
|
37 | 75 | let mut update_borders = false;
|
38 | 76 |
|
39 |
| - for (i, monitor) in wm.monitors_mut().iter_mut().enumerate() { |
40 |
| - let work_area = *monitor.work_area_size(); |
41 |
| - let window_based_work_area_offset = ( |
42 |
| - monitor.window_based_work_area_offset_limit(), |
43 |
| - monitor.window_based_work_area_offset(), |
44 |
| - ); |
45 |
| - |
46 |
| - let offset = if monitor.work_area_offset().is_some() { |
47 |
| - monitor.work_area_offset() |
48 |
| - } else { |
49 |
| - offset |
50 |
| - }; |
51 |
| - |
52 |
| - for (j, workspace) in monitor.workspaces_mut().iter_mut().enumerate() { |
53 |
| - let reaped_orphans = workspace.reap_orphans()?; |
54 |
| - if reaped_orphans.0 > 0 || reaped_orphans.1 > 0 { |
55 |
| - workspace.update(&work_area, offset, window_based_work_area_offset)?; |
56 |
| - update_borders = true; |
| 77 | + for (hwnd, (m_idx, w_idx)) in orphan_hwnds.iter() { |
| 78 | + if let Some(monitor) = wm.monitors_mut().get_mut(*m_idx) { |
| 79 | + let focused_workspace_idx = monitor.focused_workspace_idx(); |
| 80 | + let work_area = *monitor.work_area_size(); |
| 81 | + let window_based_work_area_offset = ( |
| 82 | + monitor.window_based_work_area_offset_limit(), |
| 83 | + monitor.window_based_work_area_offset(), |
| 84 | + ); |
| 85 | + |
| 86 | + let offset = if monitor.work_area_offset().is_some() { |
| 87 | + monitor.work_area_offset() |
| 88 | + } else { |
| 89 | + offset |
| 90 | + }; |
| 91 | + |
| 92 | + if let Some(workspace) = monitor.workspaces_mut().get_mut(*w_idx) { |
| 93 | + // Remove orphan window |
| 94 | + if let Err(error) = workspace.remove_window(*hwnd) { |
| 95 | + tracing::warn!( |
| 96 | + "error reaping orphan window ({}) on monitor: {}, workspace: {}. Error: {}", |
| 97 | + hwnd, |
| 98 | + m_idx, |
| 99 | + w_idx, |
| 100 | + error, |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + if focused_workspace_idx == *w_idx { |
| 105 | + // If this is not a focused workspace there is no need to update the |
| 106 | + // workspace or the borders. That will already be done when the user |
| 107 | + // changes to this workspace. |
| 108 | + workspace.update(&work_area, offset, window_based_work_area_offset)?; |
| 109 | + update_borders = true; |
| 110 | + } |
57 | 111 | tracing::info!(
|
58 |
| - "reaped {} orphan window(s) and {} orphaned container(s) on monitor: {}, workspace: {}", |
59 |
| - reaped_orphans.0, |
60 |
| - reaped_orphans.1, |
61 |
| - i, |
62 |
| - j |
| 112 | + "reaped orphan window ({}) on monitor: {}, workspace: {}", |
| 113 | + hwnd, |
| 114 | + m_idx, |
| 115 | + w_idx, |
63 | 116 | );
|
64 | 117 | }
|
65 | 118 | }
|
| 119 | + |
| 120 | + wm.known_hwnds.remove(hwnd); |
| 121 | + |
| 122 | + let window = Window::from(*hwnd); |
| 123 | + notify_subscribers( |
| 124 | + crate::Notification { |
| 125 | + event: NotificationEvent::WindowManager(WindowManagerEvent::Destroy( |
| 126 | + WinEvent::ObjectDestroy, |
| 127 | + window, |
| 128 | + )), |
| 129 | + state: wm.as_ref().into(), |
| 130 | + }, |
| 131 | + true, |
| 132 | + )?; |
66 | 133 | }
|
67 | 134 |
|
68 | 135 | if update_borders {
|
69 | 136 | border_manager::send_notification(None);
|
70 | 137 | }
|
| 138 | + |
| 139 | + // Save to file |
| 140 | + let hwnd_json = DATA_DIR.join("komorebi.hwnd.json"); |
| 141 | + let file = OpenOptions::new() |
| 142 | + .write(true) |
| 143 | + .truncate(true) |
| 144 | + .create(true) |
| 145 | + .open(hwnd_json)?; |
| 146 | + |
| 147 | + serde_json::to_writer_pretty(&file, &wm.known_hwnds.keys().collect::<Vec<_>>())?; |
| 148 | + } |
| 149 | + |
| 150 | + Ok(()) |
| 151 | +} |
| 152 | + |
| 153 | +fn watch_for_orphans(wm: Arc<Mutex<WindowManager>>) { |
| 154 | + // Cache current hwnds |
| 155 | + { |
| 156 | + let mut cache = HWNDS_CACHE.lock(); |
| 157 | + *cache = wm.lock().known_hwnds.clone(); |
| 158 | + } |
| 159 | + |
| 160 | + std::thread::spawn(move || loop { |
| 161 | + match find_orphans() { |
| 162 | + Ok(()) => { |
| 163 | + tracing::warn!("restarting finished thread"); |
| 164 | + } |
| 165 | + Err(error) => { |
| 166 | + if cfg!(debug_assertions) { |
| 167 | + tracing::error!("restarting failed thread: {:?}", error) |
| 168 | + } else { |
| 169 | + tracing::error!("restarting failed thread: {}", error) |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + }); |
| 174 | +} |
| 175 | + |
| 176 | +fn find_orphans() -> color_eyre::Result<()> { |
| 177 | + tracing::info!("watching"); |
| 178 | + |
| 179 | + loop { |
| 180 | + std::thread::sleep(Duration::from_millis(20)); |
| 181 | + |
| 182 | + let mut cache = HWNDS_CACHE.lock(); |
| 183 | + let mut orphan_hwnds = HashMap::new(); |
| 184 | + |
| 185 | + for (hwnd, (m_idx, w_idx)) in cache.iter() { |
| 186 | + let window = Window::from(*hwnd); |
| 187 | + |
| 188 | + if !window.is_window() |
| 189 | + // This one is a hack because WINWORD.EXE is an absolute trainwreck of an app |
| 190 | + // when multiple docs are open, it keeps open an invisible window, with WS_EX_LAYERED |
| 191 | + // (A STYLE THAT THE REGULAR WINDOWS NEED IN ORDER TO BE MANAGED!) when one of the |
| 192 | + // docs is closed |
| 193 | + // |
| 194 | + // I hate every single person who worked on Microsoft Office 365, especially Word |
| 195 | + || !window.is_visible() |
| 196 | + { |
| 197 | + orphan_hwnds.insert(window.hwnd, (*m_idx, *w_idx)); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + if !orphan_hwnds.is_empty() { |
| 202 | + // Update reaper cache |
| 203 | + cache.retain(|h, _| !orphan_hwnds.contains_key(h)); |
| 204 | + |
| 205 | + // Send handles to remove |
| 206 | + event_tx().send(ReaperNotification(orphan_hwnds))?; |
| 207 | + } |
71 | 208 | }
|
72 | 209 | }
|
0 commit comments