mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-16 05:41:16 +01:00
feat(wm): add display reconfiguration listener
This commit adds a display reconfiguration listener which emits notifications to a monitor reconciliator module which operates on a background thread. So far I'm only handling the resize notification because I have to change the scaling when I'm recording devlogs and I don't want to have to restart komorebi every time I do that.
This commit is contained in:
1 parent
a4bc40aa5e
commit
98616c4288
4 files changed
+220
No files matched your search
@@ -0,0 +1,91 @@
|
||||
use crate::core_graphics::error::CoreGraphicsError;
|
||||
use crate::monitor_reconciliator;
|
||||
use crate::monitor_reconciliator::MonitorNotification;
|
||||
use objc2_core_graphics::CGDirectDisplayID;
|
||||
use objc2_core_graphics::CGDisplayChangeSummaryFlags;
|
||||
use objc2_core_graphics::CGDisplayRegisterReconfigurationCallback;
|
||||
use objc2_core_graphics::CGDisplayRemoveReconfigurationCallback;
|
||||
use std::ffi::c_void;
|
||||
|
||||
pub struct DisplayReconfigurationListener {}
|
||||
|
||||
unsafe extern "C-unwind" fn callback(
|
||||
display_id: CGDirectDisplayID,
|
||||
flags: CGDisplayChangeSummaryFlags,
|
||||
_user_info: *mut c_void,
|
||||
) {
|
||||
if flags.contains(CGDisplayChangeSummaryFlags::DesktopShapeChangedFlag) {
|
||||
tracing::debug!("display: {display_id} resized");
|
||||
monitor_reconciliator::send_notification(MonitorNotification::Resize(display_id));
|
||||
}
|
||||
|
||||
// if flags.contains(CGDisplayChangeSummaryFlags::BeginConfigurationFlag) {
|
||||
// tracing::debug!("display: {display_id} configured");
|
||||
// }
|
||||
//
|
||||
// if flags.contains(CGDisplayChangeSummaryFlags::AddFlag) {
|
||||
// tracing::debug!("display: {display_id} added");
|
||||
// }
|
||||
//
|
||||
// if flags.contains(CGDisplayChangeSummaryFlags::RemoveFlag) {
|
||||
// tracing::debug!("display: {display_id} removed");
|
||||
// }
|
||||
//
|
||||
// if flags.contains(CGDisplayChangeSummaryFlags::MirrorFlag) {
|
||||
// tracing::debug!("display: {display_id} mirrored");
|
||||
// }
|
||||
//
|
||||
// if flags.contains(CGDisplayChangeSummaryFlags::UnMirrorFlag) {
|
||||
// tracing::debug!("display: {display_id} unmirrored");
|
||||
// }
|
||||
//
|
||||
// if flags.contains(CGDisplayChangeSummaryFlags::EnabledFlag) {
|
||||
// tracing::debug!("display: {display_id} enabled");
|
||||
// }
|
||||
//
|
||||
// if flags.contains(CGDisplayChangeSummaryFlags::DisabledFlag) {
|
||||
// tracing::debug!("display: {display_id} disabled");
|
||||
// }
|
||||
//
|
||||
// if flags.contains(CGDisplayChangeSummaryFlags::DisabledFlag) {
|
||||
// tracing::debug!("display: {display_id} disabled");
|
||||
// }
|
||||
//
|
||||
// tracing::trace!("display: {display_id}, flags: {flags:?}");
|
||||
}
|
||||
|
||||
impl DisplayReconfigurationListener {
|
||||
pub fn init() -> Result<(), CoreGraphicsError> {
|
||||
tracing::info!("registering display reconfiguration listener callback");
|
||||
|
||||
unsafe {
|
||||
match CoreGraphicsError::from(CGDisplayRegisterReconfigurationCallback(
|
||||
Some(callback),
|
||||
std::ptr::null_mut(),
|
||||
)) {
|
||||
CoreGraphicsError::Success => Ok(()),
|
||||
err => Err(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for DisplayReconfigurationListener {
|
||||
fn drop(&mut self) {
|
||||
tracing::info!("removing display reconfiguration listener callback");
|
||||
|
||||
unsafe {
|
||||
match CoreGraphicsError::from(CGDisplayRemoveReconfigurationCallback(
|
||||
Some(callback),
|
||||
std::ptr::null_mut(),
|
||||
)) {
|
||||
CoreGraphicsError::Success => {}
|
||||
error => {
|
||||
tracing::error!(
|
||||
"failed to remove display reconfiguration listener callback {error}"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,9 +34,11 @@ pub mod ax_event_listener;
|
||||
mod container;
|
||||
pub mod core;
|
||||
pub mod core_graphics;
|
||||
pub mod display_reconfiguration_listener;
|
||||
pub mod lockable_sequence;
|
||||
pub mod macos_api;
|
||||
pub mod monitor;
|
||||
pub mod monitor_reconciliator;
|
||||
pub mod notification_center_listener;
|
||||
pub mod process_command;
|
||||
pub mod process_event;
|
||||
|
||||
@@ -4,6 +4,8 @@ use color_eyre::eyre;
|
||||
use color_eyre::eyre::OptionExt;
|
||||
use komorebi::DATA_DIR;
|
||||
use komorebi::ax_event_listener;
|
||||
use komorebi::display_reconfiguration_listener::DisplayReconfigurationListener;
|
||||
use komorebi::monitor_reconciliator;
|
||||
use komorebi::notification_center_listener::NotificationCenterListener;
|
||||
use komorebi::process_command::listen_for_commands;
|
||||
use komorebi::process_event::listen_for_events;
|
||||
@@ -101,6 +103,7 @@ fn main() -> eyre::Result<()> {
|
||||
}
|
||||
|
||||
let _notification_center_listener = NotificationCenterListener::init();
|
||||
let _display_reconfiguration_listener = DisplayReconfigurationListener::init();
|
||||
|
||||
let display_size = unsafe { CGDisplayBounds(CGMainDisplayID()) };
|
||||
tracing::info!("display size for main display is: {:?}", display_size);
|
||||
@@ -116,6 +119,7 @@ fn main() -> eyre::Result<()> {
|
||||
|
||||
listen_for_commands(wm.clone());
|
||||
listen_for_events(wm.clone());
|
||||
monitor_reconciliator::listen_for_notifications(wm.clone())?;
|
||||
|
||||
let quit_ctrlc = Arc::new(AtomicBool::new(false));
|
||||
let quit_thread = quit_ctrlc.clone();
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
use crate::core::rect::Rect;
|
||||
use crate::core_graphics::CoreGraphicsApi;
|
||||
use crate::window_manager::WindowManager;
|
||||
use crossbeam_channel::Receiver;
|
||||
use crossbeam_channel::Sender;
|
||||
use objc2_core_graphics::CGDirectDisplayID;
|
||||
use parking_lot::Mutex;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use std::sync::Arc;
|
||||
use std::sync::OnceLock;
|
||||
use strum::Display;
|
||||
|
||||
#[derive(Debug, Copy, Clone, Serialize, Deserialize, Display)]
|
||||
|
||||
pub enum MonitorNotification {
|
||||
Resize(CGDirectDisplayID),
|
||||
}
|
||||
|
||||
static CHANNEL: OnceLock<(Sender<MonitorNotification>, Receiver<MonitorNotification>)> =
|
||||
OnceLock::new();
|
||||
pub fn channel() -> &'static (Sender<MonitorNotification>, Receiver<MonitorNotification>) {
|
||||
CHANNEL.get_or_init(|| crossbeam_channel::bounded(20))
|
||||
}
|
||||
|
||||
fn event_tx() -> Sender<MonitorNotification> {
|
||||
channel().0.clone()
|
||||
}
|
||||
|
||||
fn event_rx() -> Receiver<MonitorNotification> {
|
||||
channel().1.clone()
|
||||
}
|
||||
|
||||
pub fn send_notification(notification: MonitorNotification) {
|
||||
if event_tx().try_send(notification).is_err() {
|
||||
tracing::warn!("channel is full; dropping notification")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn listen_for_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result<()> {
|
||||
std::thread::spawn(move || {
|
||||
loop {
|
||||
match handle_notifications(wm.clone()) {
|
||||
Ok(()) => {
|
||||
tracing::warn!("restarting finished thread");
|
||||
}
|
||||
Err(error) => {
|
||||
if cfg!(debug_assertions) {
|
||||
tracing::error!("restarting failed thread: {:?}", error)
|
||||
} else {
|
||||
tracing::error!("restarting failed thread: {}", error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result<()> {
|
||||
tracing::info!("listening");
|
||||
|
||||
let receiver = event_rx();
|
||||
|
||||
for notification in receiver {
|
||||
let mut wm = wm.lock();
|
||||
|
||||
match notification {
|
||||
MonitorNotification::Resize(display_id) => {
|
||||
tracing::debug!("handling resize notification");
|
||||
let new_size = CoreGraphicsApi::display_bounds(display_id);
|
||||
for monitor in wm.monitors_mut() {
|
||||
let mut should_update = false;
|
||||
|
||||
if Rect::from(new_size) != monitor.size {
|
||||
// need to do this because we can't call NSScreen from a background thread
|
||||
monitor.work_area_size = calculate_scaled_work_area(
|
||||
&Rect::from(new_size),
|
||||
&monitor.size,
|
||||
&monitor.work_area_size,
|
||||
);
|
||||
monitor.size = Rect::from(new_size);
|
||||
should_update = true;
|
||||
}
|
||||
|
||||
if should_update {
|
||||
tracing::info!("updated size and work area for monitor: {}", monitor.id);
|
||||
monitor.update_focused_workspace(None)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn calculate_scaled_work_area(
|
||||
new_size: &Rect,
|
||||
monitor_size: &Rect,
|
||||
monitor_work_area_size: &Rect,
|
||||
) -> Rect {
|
||||
let scale_x = new_size.right as f64 / monitor_size.right as f64;
|
||||
let scale_y = new_size.bottom as f64 / monitor_size.bottom as f64;
|
||||
|
||||
let top_offset = monitor_work_area_size.top - monitor_size.top;
|
||||
let bottom_offset = monitor_size.bottom - monitor_work_area_size.bottom;
|
||||
let left_offset = monitor_work_area_size.left - monitor_size.left;
|
||||
let right_offset = monitor_size.right - monitor_work_area_size.right;
|
||||
|
||||
let scaled_top_offset = (top_offset as f64 * scale_y).round() as i32;
|
||||
let scaled_bottom_offset = (bottom_offset as f64 * scale_y).round() as i32;
|
||||
let scaled_left_offset = (left_offset as f64 * scale_x).round() as i32;
|
||||
let scaled_right_offset = (right_offset as f64 * scale_x).round() as i32;
|
||||
|
||||
Rect {
|
||||
left: new_size.left + scaled_left_offset,
|
||||
top: new_size.top + scaled_top_offset,
|
||||
right: new_size.right - scaled_right_offset,
|
||||
bottom: new_size.bottom - scaled_bottom_offset,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user