diff --git a/komorebi/src/lib.rs b/komorebi/src/lib.rs index 0d87323c..8d87e90a 100644 --- a/komorebi/src/lib.rs +++ b/komorebi/src/lib.rs @@ -65,6 +65,7 @@ use crate::core::config_generation::WorkspaceMatchingRule; use color_eyre::Result; use os_info::Version; use parking_lot::Mutex; +use parking_lot::RwLock; use regex::Regex; use serde::Deserialize; use serde::Serialize; @@ -131,8 +132,8 @@ lazy_static! { static ref TRANSPARENCY_BLACKLIST: Arc>> = Arc::new(Mutex::new(Vec::new())); static ref MONITOR_INDEX_PREFERENCES: Arc>> = Arc::new(Mutex::new(HashMap::new())); - static ref DISPLAY_INDEX_PREFERENCES: Arc>> = - Arc::new(Mutex::new(HashMap::new())); + static ref DISPLAY_INDEX_PREFERENCES: Arc>> = + Arc::new(RwLock::new(HashMap::new())); static ref WORKSPACE_MATCHING_RULES: Arc>> = Arc::new(Mutex::new(Vec::new())); static ref REGEX_IDENTIFIERS: Arc>> = diff --git a/komorebi/src/monitor_reconciliator/mod.rs b/komorebi/src/monitor_reconciliator/mod.rs index 718d0a46..b9d57816 100644 --- a/komorebi/src/monitor_reconciliator/mod.rs +++ b/komorebi/src/monitor_reconciliator/mod.rs @@ -12,6 +12,7 @@ use crate::NotificationEvent; use crate::State; use crate::WindowManager; use crate::WindowsApi; +use crate::DISPLAY_INDEX_PREFERENCES; use crate::WORKSPACE_MATCHING_RULES; use crossbeam_channel::Receiver; use crossbeam_channel::Sender; @@ -66,11 +67,20 @@ pub fn send_notification(notification: MonitorNotification) { } pub fn insert_in_monitor_cache(serial_or_device_id: &str, monitor: Monitor) { + let dip = DISPLAY_INDEX_PREFERENCES.read(); + let mut dip_ids = dip.values(); + let preferred_id = if dip_ids.any(|id| id == monitor.device_id()) { + monitor.device_id().clone() + } else if dip_ids.any(|id| Some(id) == monitor.serial_number_id().as_ref()) { + monitor.serial_number_id().clone().unwrap_or_default() + } else { + serial_or_device_id.to_string() + }; let mut monitor_cache = MONITOR_CACHE .get_or_init(|| Mutex::new(HashMap::new())) .lock(); - monitor_cache.insert(serial_or_device_id.to_string(), monitor); + monitor_cache.insert(preferred_id, monitor); } pub fn attached_display_devices() -> color_eyre::Result> { @@ -380,8 +390,18 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result workspace_rules.remove(i); } - // Let's add their state to the cache for later - monitor_cache.insert(id, m.clone()); + // Let's add their state to the cache for later, make sure to use what + // the user set as preference as the id. + let dip = DISPLAY_INDEX_PREFERENCES.read(); + let mut dip_ids = dip.values(); + let preferred_id = if dip_ids.any(|id| id == m.device_id()) { + m.device_id().clone() + } else if dip_ids.any(|id| Some(id) == m.serial_number_id().as_ref()) { + m.serial_number_id().clone().unwrap_or_default() + } else { + id + }; + monitor_cache.insert(preferred_id, m.clone()); } } diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 4b87b3c3..9fca4b83 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -1152,7 +1152,7 @@ impl WindowManager { ); } SocketMessage::DisplayIndexPreference(index_preference, ref display) => { - let mut display_index_preferences = DISPLAY_INDEX_PREFERENCES.lock(); + let mut display_index_preferences = DISPLAY_INDEX_PREFERENCES.write(); display_index_preferences.insert(index_preference, display.clone()); } SocketMessage::EnsureWorkspaces(monitor_idx, workspace_count) => { diff --git a/komorebi/src/static_config.rs b/komorebi/src/static_config.rs index 594fbfb8..51fea21c 100644 --- a/komorebi/src/static_config.rs +++ b/komorebi/src/static_config.rs @@ -740,7 +740,7 @@ impl From<&WindowManager> for StaticConfig { .collect::>(), ), monitor_index_preferences: Option::from(MONITOR_INDEX_PREFERENCES.lock().clone()), - display_index_preferences: Option::from(DISPLAY_INDEX_PREFERENCES.lock().clone()), + display_index_preferences: Option::from(DISPLAY_INDEX_PREFERENCES.read().clone()), stackbar: None, animation: None, theme: None, @@ -768,7 +768,7 @@ impl StaticConfig { } if let Some(display_index_preferences) = &self.display_index_preferences { - let mut preferences = DISPLAY_INDEX_PREFERENCES.lock(); + let mut preferences = DISPLAY_INDEX_PREFERENCES.write(); preferences.clone_from(display_index_preferences); } @@ -1276,7 +1276,7 @@ impl StaticConfig { let mut wm = wm.lock(); let configs_with_preference: Vec<_> = - DISPLAY_INDEX_PREFERENCES.lock().keys().copied().collect(); + DISPLAY_INDEX_PREFERENCES.read().keys().copied().collect(); let mut configs_used = Vec::new(); let mut workspace_matching_rules = WORKSPACE_MATCHING_RULES.lock(); @@ -1286,7 +1286,7 @@ impl StaticConfig { let offset = wm.work_area_offset; for (i, monitor) in wm.monitors_mut().iter_mut().enumerate() { let preferred_config_idx = { - let display_index_preferences = DISPLAY_INDEX_PREFERENCES.lock(); + let display_index_preferences = DISPLAY_INDEX_PREFERENCES.read(); let c_idx = display_index_preferences.iter().find_map(|(c_idx, id)| { (monitor .serial_number_id() @@ -1385,7 +1385,7 @@ impl StaticConfig { .filter(|i| !configs_used.contains(i)) { let id = { - let display_index_preferences = DISPLAY_INDEX_PREFERENCES.lock(); + let display_index_preferences = DISPLAY_INDEX_PREFERENCES.read(); display_index_preferences.get(i).cloned() }; if let (Some(id), Some(monitor_config)) = @@ -1445,7 +1445,7 @@ impl StaticConfig { value.apply_globals()?; let configs_with_preference: Vec<_> = - DISPLAY_INDEX_PREFERENCES.lock().keys().copied().collect(); + DISPLAY_INDEX_PREFERENCES.read().keys().copied().collect(); let mut configs_used = Vec::new(); let mut workspace_matching_rules = WORKSPACE_MATCHING_RULES.lock(); @@ -1455,7 +1455,7 @@ impl StaticConfig { let offset = wm.work_area_offset; for (i, monitor) in wm.monitors_mut().iter_mut().enumerate() { let preferred_config_idx = { - let display_index_preferences = DISPLAY_INDEX_PREFERENCES.lock(); + let display_index_preferences = DISPLAY_INDEX_PREFERENCES.read(); let c_idx = display_index_preferences.iter().find_map(|(c_idx, id)| { (monitor .serial_number_id() @@ -1557,7 +1557,7 @@ impl StaticConfig { .filter(|i| !configs_used.contains(i)) { let id = { - let display_index_preferences = DISPLAY_INDEX_PREFERENCES.lock(); + let display_index_preferences = DISPLAY_INDEX_PREFERENCES.read(); display_index_preferences.get(i).cloned() }; if let (Some(id), Some(monitor_config)) = diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index de5392e7..23cc72f1 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -272,7 +272,7 @@ impl Default for GlobalState { tray_and_multi_window_identifiers: TRAY_AND_MULTI_WINDOW_IDENTIFIERS.lock().clone(), name_change_on_launch_identifiers: OBJECT_NAME_CHANGE_ON_LAUNCH.lock().clone(), monitor_index_preferences: MONITOR_INDEX_PREFERENCES.lock().clone(), - display_index_preferences: DISPLAY_INDEX_PREFERENCES.lock().clone(), + display_index_preferences: DISPLAY_INDEX_PREFERENCES.read().clone(), workspace_rules: WORKSPACE_MATCHING_RULES.lock().clone(), window_hiding_behaviour: *HIDING_BEHAVIOUR.lock(), configuration_dir: HOME_DIR.clone(), diff --git a/komorebi/src/windows_api.rs b/komorebi/src/windows_api.rs index 7b7ab78c..c25526cb 100644 --- a/komorebi/src/windows_api.rs +++ b/komorebi/src/windows_api.rs @@ -299,7 +299,7 @@ impl WindowsApi { } } - let display_index_preferences = DISPLAY_INDEX_PREFERENCES.lock(); + let display_index_preferences = DISPLAY_INDEX_PREFERENCES.read(); for (index, id) in &*display_index_preferences { if m.serial_number_id().as_ref().is_some_and(|sn| sn == id) || id.eq(m.device_id()) { @@ -334,7 +334,7 @@ impl WindowsApi { // Rebuild monitor index map *monitor_usr_idx_map = HashMap::new(); let mut added_monitor_idxs = Vec::new(); - for (index, id) in &*DISPLAY_INDEX_PREFERENCES.lock() { + for (index, id) in &*DISPLAY_INDEX_PREFERENCES.read() { if let Some(m_idx) = monitors.elements().iter().position(|m| { m.serial_number_id().as_ref().is_some_and(|sn| sn == id) || m.device_id() == id }) {