forge
Explore
Log in

Don't you dare go Hollow

Add option 'focus-at-startup' to focus a chosen output on start (#1323)

Browse files at this commit

8d43efe4ac38ddb6ce298d6fbdfc3ec610b8da56Unverifiedlualeet and lualeet, Ivan MolodetskikhParent: 3bb7e60
* Implement default-output

* Fix incorrect wiki string

* Center mouse on start

* Move default-output to Output.focus-at-startup

* fixes

---------
niri-config/src/lib.rsmodified+5 -0
@@ -440,6 +440,8 @@ pub struct Output {
440 pub mode: Option<ConfiguredMode>,440 pub mode: Option<ConfiguredMode>,
441 #[knuffel(child)]441 #[knuffel(child)]
442 pub variable_refresh_rate: Option<Vrr>,442 pub variable_refresh_rate: Option<Vrr>,
443+ #[knuffel(child)]
444+ pub focus_at_startup: bool,
443 #[knuffel(child, default = DEFAULT_BACKGROUND_COLOR)]445 #[knuffel(child, default = DEFAULT_BACKGROUND_COLOR)]
444 pub background_color: Color,446 pub background_color: Color,
445 }447 }
@@ -462,6 +464,7 @@ impl Default for Output {
462 fn default() -> Self {464 fn default() -> Self {
463 Self {465 Self {
464 off: false,466 off: false,
467+ focus_at_startup: false,
465 name: String::new(),468 name: String::new(),
466 scale: None,469 scale: None,
467 transform: Transform::Normal,470 transform: Transform::Normal,
@@ -3746,6 +3749,7 @@ mod tests {
3746 }3749 }
3747 3750
3748 output "eDP-1" {3751 output "eDP-1" {
3752+ focus-at-startup
3749 scale 23753 scale 2
3750 transform "flipped-90"3754 transform "flipped-90"
3751 position x=10 y=203755 position x=10 y=20
@@ -4105,6 +4109,7 @@ mod tests {
4105 on_demand: true,4109 on_demand: true,
4106 },4110 },
4107 ),4111 ),
4112+ focus_at_startup: true,
4108 background_color: Color {4113 background_color: Color {
4109 r: 0.09803922,4114 r: 0.09803922,
4110 g: 0.09803922,4115 g: 0.09803922,
src/niri.rsmodified+25 -0
@@ -630,6 +630,8 @@ impl State {
630 state.load_xkb_file();630 state.load_xkb_file();
631 // Initialize some IPC server state.631 // Initialize some IPC server state.
632 state.ipc_keyboard_layouts_changed();632 state.ipc_keyboard_layouts_changed();
633+ // Focus the default monitor if set by the user.
634+ state.focus_default_monitor();
633 635
634 Ok(state)636 Ok(state)
635 }637 }
@@ -794,6 +796,29 @@ impl State {
794 rv796 rv
795 }797 }
796 798
799+ pub fn focus_default_monitor(&mut self) {
800+ // Our default target is the first output in sorted order.
801+ let Some(mut target) = self.niri.sorted_outputs.first().cloned() else {
802+ // No outputs are connected.
803+ return;
804+ };
805+
806+ let config = self.niri.config.borrow();
807+ for config in &config.outputs.0 {
808+ if !config.focus_at_startup {
809+ continue;
810+ }
811+ if let Some(output) = self.niri.output_by_name_match(&config.name) {
812+ target = output.clone();
813+ break;
814+ }
815+ }
816+ drop(config);
817+
818+ self.niri.layout.focus_output(&target);
819+ self.move_cursor_to_output(&target);
820+ }
821+
797 /// Focus a specific window, taking care of a potential active output change and cursor822 /// Focus a specific window, taking care of a potential active output change and cursor
798 /// warp.823 /// warp.
799 pub fn focus_window(&mut self, window: &Window) {824 pub fn focus_window(&mut self, window: &Window) {
wiki/Configuration:-Outputs.mdmodified+23 -0
@@ -13,6 +13,7 @@ output "eDP-1" {
13 transform "90"13 transform "90"
14 position x=1280 y=014 position x=1280 y=0
15 variable-refresh-rate // on-demand=true15 variable-refresh-rate // on-demand=true
16+ focus-at-startup
16 background-color "#003300"17 background-color "#003300"
17 }18 }
18 19
@@ -164,6 +165,28 @@ output "HDMI-A-1" {
164 }165 }
165 ```166 ```
166 167
168+### `focus-at-startup`
169+
170+<sup>Since: next release</sup>
171+
172+Focus this output by default when niri starts.
173+
174+If multiple outputs with `focus-at-startup` are connected, they are prioritized in the order that they appear in the config.
175+
176+When none of the connected outputs are explicitly `focus-at-startup`, niri will focus the first one sorted by name (same output sorting as used elsewhere in niri).
177+
178+```kdl
179+// Focus HDMI-A-1 by default.
180+output "HDMI-A-1" {
181+ focus-at-startup
182+}
183+
184+// ...if HDMI-A-1 wasn't connected, focus DP-2 instead.
185+output "DP-2" {
186+ focus-at-startup
187+}
188+```
189+
167 ### `background-color`190 ### `background-color`
168 191
169 <sup>Since: 0.1.8</sup>192 <sup>Since: 0.1.8</sup>