Add binds to move between monitors
Browse files at this commitd96daf68a7d1b843b5231e2c416349214c5629d2Parent: 81e8e6e5 changed files +213 -8
- README.md +2 -0
- src/input.rs +73 -4
- src/layout.rs +42 -0
- src/niri.rs +91 -3
- src/utils.rs +5 -1
README.mdmodified+2 -0
| @@ -63,6 +63,8 @@ The general system is: if a hotkey switches somewhere, then adding <kbd>Ctrl</kb | |||
| 63 | | <kbd>Mod</kbd><kbd>Ctrl</kbd><kbd>L</kbd> or <kbd>Mod</kbd><kbd>Ctrl</kbd><kbd>→</kbd> | Move the focused column to the right | | 63 | | <kbd>Mod</kbd><kbd>Ctrl</kbd><kbd>L</kbd> or <kbd>Mod</kbd><kbd>Ctrl</kbd><kbd>→</kbd> | Move the focused column to the right | |
| 64 | | <kbd>Mod</kbd><kbd>Ctrl</kbd><kbd>J</kbd> or <kbd>Mod</kbd><kbd>Ctrl</kbd><kbd>↓</kbd> | Move the focused window below in a column | | 64 | | <kbd>Mod</kbd><kbd>Ctrl</kbd><kbd>J</kbd> or <kbd>Mod</kbd><kbd>Ctrl</kbd><kbd>↓</kbd> | Move the focused window below in a column | |
| 65 | | <kbd>Mod</kbd><kbd>Ctrl</kbd><kbd>K</kbd> or <kbd>Mod</kbd><kbd>Ctrl</kbd><kbd>↑</kbd> | Move the focused window above in a column | | 65 | | <kbd>Mod</kbd><kbd>Ctrl</kbd><kbd>K</kbd> or <kbd>Mod</kbd><kbd>Ctrl</kbd><kbd>↑</kbd> | Move the focused window above in a column | |
| 66 | +| <kbd>Mod</kbd><kbd>Shift</kbd><kbd>H</kbd><kbd>J</kbd><kbd>K</kbd><kbd>L</kbd> or <kbd>Mod</kbd><kbd>Shift</kbd><kbd>←</kbd><kbd>↓</kbd><kbd>↑</kbd><kbd>→</kbd> | Focus the monitor to the side | | ||
| 67 | +| <kbd>Mod</kbd><kbd>Ctrl</kbd><kbd>Shift</kbd><kbd>H</kbd><kbd>J</kbd><kbd>K</kbd><kbd>L</kbd> or <kbd>Mod</kbd><kbd>Ctrl</kbd><kbd>Shift</kbd><kbd>←</kbd><kbd>↓</kbd><kbd>↑</kbd><kbd>→</kbd> | Move the focused window to the monitor to the side | | ||
| 66 | | <kbd>Mod</kbd><kbd>U</kbd> | Switch to the workspace below | | 68 | | <kbd>Mod</kbd><kbd>U</kbd> | Switch to the workspace below | |
| 67 | | <kbd>Mod</kbd><kbd>I</kbd> | Switch to the workspace above | | 69 | | <kbd>Mod</kbd><kbd>I</kbd> | Switch to the workspace above | |
| 68 | | <kbd>Mod</kbd><kbd>Ctrl</kbd><kbd>U</kbd> | Move the focused window to the workspace below | | 70 | | <kbd>Mod</kbd><kbd>Ctrl</kbd><kbd>U</kbd> | Move the focused window to the workspace below | |
src/input.rsmodified+73 -4
| @@ -34,6 +34,14 @@ enum Action { | |||
| 34 | SwitchWorkspaceUp, | 34 | SwitchWorkspaceUp, |
| 35 | MoveToWorkspaceDown, | 35 | MoveToWorkspaceDown, |
| 36 | MoveToWorkspaceUp, | 36 | MoveToWorkspaceUp, |
| 37 | + FocusMonitorLeft, | ||
| 38 | + FocusMonitorRight, | ||
| 39 | + FocusMonitorDown, | ||
| 40 | + FocusMonitorUp, | ||
| 41 | + MoveToMonitorLeft, | ||
| 42 | + MoveToMonitorRight, | ||
| 43 | + MoveToMonitorDown, | ||
| 44 | + MoveToMonitorUp, | ||
| 37 | ToggleWidth, | 45 | ToggleWidth, |
| 38 | ToggleFullWidth, | 46 | ToggleFullWidth, |
| 39 | } | 47 | } |
| @@ -80,22 +88,35 @@ fn action(comp_mod: CompositorMod, keysym: KeysymHandle, mods: ModifiersState) - | |||
| 80 | KEY_n => Action::Spawn("nautilus".to_owned()), | 88 | KEY_n => Action::Spawn("nautilus".to_owned()), |
| 81 | KEY_q => Action::CloseWindow, | 89 | KEY_q => Action::CloseWindow, |
| 82 | KEY_F => Action::ToggleFullscreen, | 90 | KEY_F => Action::ToggleFullscreen, |
| 91 | + KEY_comma => Action::ConsumeIntoColumn, | ||
| 92 | + KEY_period => Action::ExpelFromColumn, | ||
| 93 | + KEY_r => Action::ToggleWidth, | ||
| 94 | + KEY_f => Action::ToggleFullWidth, | ||
| 95 | + // Move to monitor. | ||
| 96 | + KEY_H | KEY_Left if mods.shift && mods.ctrl => Action::MoveToMonitorLeft, | ||
| 97 | + KEY_L | KEY_Right if mods.shift && mods.ctrl => Action::MoveToMonitorRight, | ||
| 98 | + KEY_J | KEY_Down if mods.shift && mods.ctrl => Action::MoveToMonitorDown, | ||
| 99 | + KEY_K | KEY_Up if mods.shift && mods.ctrl => Action::MoveToMonitorUp, | ||
| 100 | + // Focus monitor. | ||
| 101 | + KEY_H | KEY_Left if mods.shift => Action::FocusMonitorLeft, | ||
| 102 | + KEY_L | KEY_Right if mods.shift => Action::FocusMonitorRight, | ||
| 103 | + KEY_J | KEY_Down if mods.shift => Action::FocusMonitorDown, | ||
| 104 | + KEY_K | KEY_Up if mods.shift => Action::FocusMonitorUp, | ||
| 105 | + // Move. | ||
| 83 | KEY_h | KEY_Left if mods.ctrl => Action::MoveLeft, | 106 | KEY_h | KEY_Left if mods.ctrl => Action::MoveLeft, |
| 84 | KEY_l | KEY_Right if mods.ctrl => Action::MoveRight, | 107 | KEY_l | KEY_Right if mods.ctrl => Action::MoveRight, |
| 85 | KEY_j | KEY_Down if mods.ctrl => Action::MoveDown, | 108 | KEY_j | KEY_Down if mods.ctrl => Action::MoveDown, |
| 86 | KEY_k | KEY_Up if mods.ctrl => Action::MoveUp, | 109 | KEY_k | KEY_Up if mods.ctrl => Action::MoveUp, |
| 110 | + // Focus. | ||
| 87 | KEY_h | KEY_Left => Action::FocusLeft, | 111 | KEY_h | KEY_Left => Action::FocusLeft, |
| 88 | KEY_l | KEY_Right => Action::FocusRight, | 112 | KEY_l | KEY_Right => Action::FocusRight, |
| 89 | KEY_j | KEY_Down => Action::FocusDown, | 113 | KEY_j | KEY_Down => Action::FocusDown, |
| 90 | KEY_k | KEY_Up => Action::FocusUp, | 114 | KEY_k | KEY_Up => Action::FocusUp, |
| 115 | + // Workspaces. | ||
| 91 | KEY_u if mods.ctrl => Action::MoveToWorkspaceDown, | 116 | KEY_u if mods.ctrl => Action::MoveToWorkspaceDown, |
| 92 | KEY_i if mods.ctrl => Action::MoveToWorkspaceUp, | 117 | KEY_i if mods.ctrl => Action::MoveToWorkspaceUp, |
| 93 | KEY_u => Action::SwitchWorkspaceDown, | 118 | KEY_u => Action::SwitchWorkspaceDown, |
| 94 | KEY_i => Action::SwitchWorkspaceUp, | 119 | KEY_i => Action::SwitchWorkspaceUp, |
| 95 | - KEY_comma => Action::ConsumeIntoColumn, | ||
| 96 | - KEY_period => Action::ExpelFromColumn, | ||
| 97 | - KEY_r => Action::ToggleWidth, | ||
| 98 | - KEY_f => Action::ToggleFullWidth, | ||
| 99 | _ => Action::None, | 120 | _ => Action::None, |
| 100 | } | 121 | } |
| 101 | } | 122 | } |
| @@ -229,6 +250,54 @@ impl Niri { | |||
| 229 | Action::ToggleFullWidth => { | 250 | Action::ToggleFullWidth => { |
| 230 | self.monitor_set.toggle_full_width(); | 251 | self.monitor_set.toggle_full_width(); |
| 231 | } | 252 | } |
| 253 | + Action::FocusMonitorLeft => { | ||
| 254 | + if let Some(output) = self.output_left() { | ||
| 255 | + self.monitor_set.focus_output(&output); | ||
| 256 | + self.move_cursor_to_output(&output); | ||
| 257 | + } | ||
| 258 | + } | ||
| 259 | + Action::FocusMonitorRight => { | ||
| 260 | + if let Some(output) = self.output_right() { | ||
| 261 | + self.monitor_set.focus_output(&output); | ||
| 262 | + self.move_cursor_to_output(&output); | ||
| 263 | + } | ||
| 264 | + } | ||
| 265 | + Action::FocusMonitorDown => { | ||
| 266 | + if let Some(output) = self.output_down() { | ||
| 267 | + self.monitor_set.focus_output(&output); | ||
| 268 | + self.move_cursor_to_output(&output); | ||
| 269 | + } | ||
| 270 | + } | ||
| 271 | + Action::FocusMonitorUp => { | ||
| 272 | + if let Some(output) = self.output_up() { | ||
| 273 | + self.monitor_set.focus_output(&output); | ||
| 274 | + self.move_cursor_to_output(&output); | ||
| 275 | + } | ||
| 276 | + } | ||
| 277 | + Action::MoveToMonitorLeft => { | ||
| 278 | + if let Some(output) = self.output_left() { | ||
| 279 | + self.monitor_set.move_to_output(&output); | ||
| 280 | + self.move_cursor_to_output(&output); | ||
| 281 | + } | ||
| 282 | + } | ||
| 283 | + Action::MoveToMonitorRight => { | ||
| 284 | + if let Some(output) = self.output_right() { | ||
| 285 | + self.monitor_set.move_to_output(&output); | ||
| 286 | + self.move_cursor_to_output(&output); | ||
| 287 | + } | ||
| 288 | + } | ||
| 289 | + Action::MoveToMonitorDown => { | ||
| 290 | + if let Some(output) = self.output_down() { | ||
| 291 | + self.monitor_set.move_to_output(&output); | ||
| 292 | + self.move_cursor_to_output(&output); | ||
| 293 | + } | ||
| 294 | + } | ||
| 295 | + Action::MoveToMonitorUp => { | ||
| 296 | + if let Some(output) = self.output_up() { | ||
| 297 | + self.monitor_set.move_to_output(&output); | ||
| 298 | + self.move_cursor_to_output(&output); | ||
| 299 | + } | ||
| 300 | + } | ||
| 232 | } | 301 | } |
| 233 | } | 302 | } |
| 234 | } | 303 | } |
src/layout.rsmodified+42 -0
| @@ -776,6 +776,48 @@ impl<W: LayoutElement> MonitorSet<W> { | |||
| 776 | }; | 776 | }; |
| 777 | monitor.toggle_full_width(); | 777 | monitor.toggle_full_width(); |
| 778 | } | 778 | } |
| 779 | + | ||
| 780 | + pub fn focus_output(&mut self, output: &Output) { | ||
| 781 | + if let MonitorSet::Normal { | ||
| 782 | + monitors, | ||
| 783 | + active_monitor_idx, | ||
| 784 | + .. | ||
| 785 | + } = self | ||
| 786 | + { | ||
| 787 | + for (idx, mon) in monitors.iter().enumerate() { | ||
| 788 | + if &mon.output == output { | ||
| 789 | + *active_monitor_idx = idx; | ||
| 790 | + return; | ||
| 791 | + } | ||
| 792 | + } | ||
| 793 | + } | ||
| 794 | + } | ||
| 795 | + | ||
| 796 | + pub fn move_to_output(&mut self, output: &Output) { | ||
| 797 | + if let MonitorSet::Normal { | ||
| 798 | + monitors, | ||
| 799 | + active_monitor_idx, | ||
| 800 | + .. | ||
| 801 | + } = self | ||
| 802 | + { | ||
| 803 | + let new_idx = monitors | ||
| 804 | + .iter() | ||
| 805 | + .position(|mon| &mon.output == output) | ||
| 806 | + .unwrap(); | ||
| 807 | + | ||
| 808 | + let current = &mut monitors[*active_monitor_idx]; | ||
| 809 | + let ws = current.active_workspace(); | ||
| 810 | + if !ws.has_windows() { | ||
| 811 | + return; | ||
| 812 | + } | ||
| 813 | + let column = &ws.columns[ws.active_column_idx]; | ||
| 814 | + let window = column.windows[column.active_window_idx].clone(); | ||
| 815 | + ws.remove_window(&window); | ||
| 816 | + | ||
| 817 | + let workspace_idx = monitors[new_idx].active_workspace_idx; | ||
| 818 | + self.add_window(new_idx, workspace_idx, window, true); | ||
| 819 | + } | ||
| 820 | + } | ||
| 779 | } | 821 | } |
| 780 | 822 | ||
| 781 | impl MonitorSet<Window> { | 823 | impl MonitorSet<Window> { |
src/niri.rsmodified+91 -3
| @@ -15,7 +15,7 @@ use smithay::desktop::{ | |||
| 15 | layer_map_for_output, LayerSurface, PopupManager, Space, Window, WindowSurfaceType, | 15 | layer_map_for_output, LayerSurface, PopupManager, Space, Window, WindowSurfaceType, |
| 16 | }; | 16 | }; |
| 17 | use smithay::input::keyboard::XkbConfig; | 17 | use smithay::input::keyboard::XkbConfig; |
| 18 | -use smithay::input::pointer::{CursorImageAttributes, CursorImageStatus}; | 18 | +use smithay::input::pointer::{CursorImageAttributes, CursorImageStatus, MotionEvent}; |
| 19 | use smithay::input::{Seat, SeatState}; | 19 | use smithay::input::{Seat, SeatState}; |
| 20 | use smithay::output::Output; | 20 | use smithay::output::Output; |
| 21 | use smithay::reexports::calloop::generic::Generic; | 21 | use smithay::reexports::calloop::generic::Generic; |
| @@ -26,7 +26,7 @@ use smithay::reexports::wayland_server::backend::{ | |||
| 26 | }; | 26 | }; |
| 27 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; | 27 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| 28 | use smithay::reexports::wayland_server::{Display, DisplayHandle}; | 28 | use smithay::reexports::wayland_server::{Display, DisplayHandle}; |
| 29 | -use smithay::utils::{IsAlive, Logical, Physical, Point, Scale, SERIAL_COUNTER}; | 29 | +use smithay::utils::{IsAlive, Logical, Physical, Point, Rectangle, Scale, SERIAL_COUNTER}; |
| 30 | use smithay::wayland::compositor::{with_states, CompositorClientState, CompositorState}; | 30 | use smithay::wayland::compositor::{with_states, CompositorClientState, CompositorState}; |
| 31 | use smithay::wayland::data_device::DataDeviceState; | 31 | use smithay::wayland::data_device::DataDeviceState; |
| 32 | use smithay::wayland::output::OutputManagerState; | 32 | use smithay::wayland::output::OutputManagerState; |
| @@ -38,7 +38,7 @@ use smithay::wayland::socket::ListeningSocketSource; | |||
| 38 | use crate::backend::Backend; | 38 | use crate::backend::Backend; |
| 39 | use crate::frame_clock::FrameClock; | 39 | use crate::frame_clock::FrameClock; |
| 40 | use crate::layout::{MonitorRenderElement, MonitorSet}; | 40 | use crate::layout::{MonitorRenderElement, MonitorSet}; |
| 41 | -use crate::utils::load_default_cursor; | 41 | +use crate::utils::{center, get_monotonic_time, load_default_cursor}; |
| 42 | use crate::LoopData; | 42 | use crate::LoopData; |
| 43 | 43 | ||
| 44 | pub struct Niri { | 44 | pub struct Niri { |
| @@ -268,6 +268,94 @@ impl Niri { | |||
| 268 | self.global_space.output_under(pos).next().cloned() | 268 | self.global_space.output_under(pos).next().cloned() |
| 269 | } | 269 | } |
| 270 | 270 | ||
| 271 | + pub fn output_left(&self) -> Option<Output> { | ||
| 272 | + let active = self.monitor_set.active_output()?; | ||
| 273 | + let active_geo = self.global_space.output_geometry(active).unwrap(); | ||
| 274 | + let extended_geo = Rectangle::from_loc_and_size( | ||
| 275 | + (i32::MIN / 2, active_geo.loc.y), | ||
| 276 | + (i32::MAX, active_geo.size.h), | ||
| 277 | + ); | ||
| 278 | + | ||
| 279 | + self.global_space | ||
| 280 | + .outputs() | ||
| 281 | + .map(|output| (output, self.global_space.output_geometry(output).unwrap())) | ||
| 282 | + .filter(|(_, geo)| center(*geo).x < center(active_geo).x && geo.overlaps(extended_geo)) | ||
| 283 | + .min_by_key(|(_, geo)| center(active_geo).x - center(*geo).x) | ||
| 284 | + .map(|(output, _)| output) | ||
| 285 | + .cloned() | ||
| 286 | + } | ||
| 287 | + | ||
| 288 | + pub fn output_right(&self) -> Option<Output> { | ||
| 289 | + let active = self.monitor_set.active_output()?; | ||
| 290 | + let active_geo = self.global_space.output_geometry(active).unwrap(); | ||
| 291 | + let extended_geo = Rectangle::from_loc_and_size( | ||
| 292 | + (i32::MIN / 2, active_geo.loc.y), | ||
| 293 | + (i32::MAX, active_geo.size.h), | ||
| 294 | + ); | ||
| 295 | + | ||
| 296 | + self.global_space | ||
| 297 | + .outputs() | ||
| 298 | + .map(|output| (output, self.global_space.output_geometry(output).unwrap())) | ||
| 299 | + .filter(|(_, geo)| center(*geo).x > center(active_geo).x && geo.overlaps(extended_geo)) | ||
| 300 | + .min_by_key(|(_, geo)| center(*geo).x - center(active_geo).x) | ||
| 301 | + .map(|(output, _)| output) | ||
| 302 | + .cloned() | ||
| 303 | + } | ||
| 304 | + | ||
| 305 | + pub fn output_up(&self) -> Option<Output> { | ||
| 306 | + let active = self.monitor_set.active_output()?; | ||
| 307 | + let active_geo = self.global_space.output_geometry(active).unwrap(); | ||
| 308 | + let extended_geo = Rectangle::from_loc_and_size( | ||
| 309 | + (active_geo.loc.x, i32::MIN / 2), | ||
| 310 | + (active_geo.size.w, i32::MAX), | ||
| 311 | + ); | ||
| 312 | + | ||
| 313 | + self.global_space | ||
| 314 | + .outputs() | ||
| 315 | + .map(|output| (output, self.global_space.output_geometry(output).unwrap())) | ||
| 316 | + .filter(|(_, geo)| center(*geo).y < center(active_geo).y && geo.overlaps(extended_geo)) | ||
| 317 | + .min_by_key(|(_, geo)| center(active_geo).y - center(*geo).y) | ||
| 318 | + .map(|(output, _)| output) | ||
| 319 | + .cloned() | ||
| 320 | + } | ||
| 321 | + | ||
| 322 | + pub fn output_down(&self) -> Option<Output> { | ||
| 323 | + let active = self.monitor_set.active_output()?; | ||
| 324 | + let active_geo = self.global_space.output_geometry(active).unwrap(); | ||
| 325 | + let extended_geo = Rectangle::from_loc_and_size( | ||
| 326 | + (active_geo.loc.x, i32::MIN / 2), | ||
| 327 | + (active_geo.size.w, i32::MAX), | ||
| 328 | + ); | ||
| 329 | + | ||
| 330 | + self.global_space | ||
| 331 | + .outputs() | ||
| 332 | + .map(|output| (output, self.global_space.output_geometry(output).unwrap())) | ||
| 333 | + .filter(|(_, geo)| center(active_geo).y < center(*geo).y && geo.overlaps(extended_geo)) | ||
| 334 | + .min_by_key(|(_, geo)| center(*geo).y - center(active_geo).y) | ||
| 335 | + .map(|(output, _)| output) | ||
| 336 | + .cloned() | ||
| 337 | + } | ||
| 338 | + | ||
| 339 | + pub fn move_cursor(&mut self, location: Point<f64, Logical>) { | ||
| 340 | + let under = self.surface_under_and_global_space(location); | ||
| 341 | + self.seat.get_pointer().unwrap().motion( | ||
| 342 | + self, | ||
| 343 | + under, | ||
| 344 | + &MotionEvent { | ||
| 345 | + location, | ||
| 346 | + serial: SERIAL_COUNTER.next_serial(), | ||
| 347 | + time: get_monotonic_time().as_millis() as u32, | ||
| 348 | + }, | ||
| 349 | + ); | ||
| 350 | + // FIXME: granular | ||
| 351 | + self.queue_redraw_all(); | ||
| 352 | + } | ||
| 353 | + | ||
| 354 | + pub fn move_cursor_to_output(&mut self, output: &Output) { | ||
| 355 | + let geo = self.global_space.output_geometry(output).unwrap(); | ||
| 356 | + self.move_cursor(center(geo).to_f64()); | ||
| 357 | + } | ||
| 358 | + | ||
| 271 | fn layer_surface_focus(&self) -> Option<WlSurface> { | 359 | fn layer_surface_focus(&self) -> Option<WlSurface> { |
| 272 | let output = self.monitor_set.active_output()?; | 360 | let output = self.monitor_set.active_output()?; |
| 273 | let layers = layer_map_for_output(output); | 361 | let layers = layer_map_for_output(output); |
src/utils.rsmodified+5 -1
| @@ -7,7 +7,7 @@ use smithay::backend::allocator::Fourcc; | |||
| 7 | use smithay::backend::renderer::element::texture::TextureBuffer; | 7 | use smithay::backend::renderer::element::texture::TextureBuffer; |
| 8 | use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; | 8 | use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; |
| 9 | use smithay::reexports::nix::time::{clock_gettime, ClockId}; | 9 | use smithay::reexports::nix::time::{clock_gettime, ClockId}; |
| 10 | -use smithay::utils::{Physical, Point, Transform}; | 10 | +use smithay::utils::{Logical, Physical, Point, Rectangle, Transform}; |
| 11 | use xcursor::parser::parse_xcursor; | 11 | use xcursor::parser::parse_xcursor; |
| 12 | use xcursor::CursorTheme; | 12 | use xcursor::CursorTheme; |
| 13 | 13 | ||
| @@ -18,6 +18,10 @@ pub fn get_monotonic_time() -> Duration { | |||
| 18 | Duration::from(clock_gettime(ClockId::CLOCK_MONOTONIC).unwrap()) | 18 | Duration::from(clock_gettime(ClockId::CLOCK_MONOTONIC).unwrap()) |
| 19 | } | 19 | } |
| 20 | 20 | ||
| 21 | +pub fn center(rect: Rectangle<i32, Logical>) -> Point<i32, Logical> { | ||
| 22 | + rect.loc + rect.size.downscale(2).to_point() | ||
| 23 | +} | ||
| 24 | + | ||
| 21 | fn load_xcursor() -> anyhow::Result<xcursor::parser::Image> { | 25 | fn load_xcursor() -> anyhow::Result<xcursor::parser::Image> { |
| 22 | let theme = CursorTheme::load("default"); | 26 | let theme = CursorTheme::load("default"); |
| 23 | let path = theme | 27 | let path = theme |