aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlualeet <43719546+lualeet@users.noreply.github.com>2025-03-29 10:13:59 +0000
committerGitHub <noreply@github.com>2025-03-29 10:13:59 +0000
commit8d43efe4ac38ddb6ce298d6fbdfc3ec610b8da56 (patch)
tree8ae0d0fd527de6f26599d6357ba9c5d11ec2c518
parent3bb7e603115d59441a700deadf11d9fd834386ea (diff)
downloadniri-8d43efe4ac38ddb6ce298d6fbdfc3ec610b8da56.tar.gz
niri-8d43efe4ac38ddb6ce298d6fbdfc3ec610b8da56.tar.bz2
niri-8d43efe4ac38ddb6ce298d6fbdfc3ec610b8da56.zip
Add option 'focus-at-startup' to focus a chosen output on start (#1323)
* Implement default-output * Fix incorrect wiki string * Center mouse on start * Move default-output to Output.focus-at-startup * fixes --------- Co-authored-by: lualeet <lualeet@null.null> Co-authored-by: Ivan Molodetskikh <yalterz@gmail.com>
-rw-r--r--niri-config/src/lib.rs5
-rw-r--r--src/niri.rs25
-rw-r--r--wiki/Configuration:-Outputs.md23
3 files changed, 53 insertions, 0 deletions
diff --git a/niri-config/src/lib.rs b/niri-config/src/lib.rs
index 6ae66aeb..4b597f99 100644
--- a/niri-config/src/lib.rs
+++ b/niri-config/src/lib.rs
@@ -440,6 +440,8 @@ pub struct Output {
pub mode: Option<ConfiguredMode>,
#[knuffel(child)]
pub variable_refresh_rate: Option<Vrr>,
+ #[knuffel(child)]
+ pub focus_at_startup: bool,
#[knuffel(child, default = DEFAULT_BACKGROUND_COLOR)]
pub background_color: Color,
}
@@ -462,6 +464,7 @@ impl Default for Output {
fn default() -> Self {
Self {
off: false,
+ focus_at_startup: false,
name: String::new(),
scale: None,
transform: Transform::Normal,
@@ -3746,6 +3749,7 @@ mod tests {
}
output "eDP-1" {
+ focus-at-startup
scale 2
transform "flipped-90"
position x=10 y=20
@@ -4105,6 +4109,7 @@ mod tests {
on_demand: true,
},
),
+ focus_at_startup: true,
background_color: Color {
r: 0.09803922,
g: 0.09803922,
diff --git a/src/niri.rs b/src/niri.rs
index 35c8b2d7..14c48f17 100644
--- a/src/niri.rs
+++ b/src/niri.rs
@@ -630,6 +630,8 @@ impl State {
state.load_xkb_file();
// Initialize some IPC server state.
state.ipc_keyboard_layouts_changed();
+ // Focus the default monitor if set by the user.
+ state.focus_default_monitor();
Ok(state)
}
@@ -794,6 +796,29 @@ impl State {
rv
}
+ pub fn focus_default_monitor(&mut self) {
+ // Our default target is the first output in sorted order.
+ let Some(mut target) = self.niri.sorted_outputs.first().cloned() else {
+ // No outputs are connected.
+ return;
+ };
+
+ let config = self.niri.config.borrow();
+ for config in &config.outputs.0 {
+ if !config.focus_at_startup {
+ continue;
+ }
+ if let Some(output) = self.niri.output_by_name_match(&config.name) {
+ target = output.clone();
+ break;
+ }
+ }
+ drop(config);
+
+ self.niri.layout.focus_output(&target);
+ self.move_cursor_to_output(&target);
+ }
+
/// Focus a specific window, taking care of a potential active output change and cursor
/// warp.
pub fn focus_window(&mut self, window: &Window) {
diff --git a/wiki/Configuration:-Outputs.md b/wiki/Configuration:-Outputs.md
index 22e1c639..2a2ee4cf 100644
--- a/wiki/Configuration:-Outputs.md
+++ b/wiki/Configuration:-Outputs.md
@@ -13,6 +13,7 @@ output "eDP-1" {
transform "90"
position x=1280 y=0
variable-refresh-rate // on-demand=true
+ focus-at-startup
background-color "#003300"
}
@@ -164,6 +165,28 @@ output "HDMI-A-1" {
}
```
+### `focus-at-startup`
+
+<sup>Since: next release</sup>
+
+Focus this output by default when niri starts.
+
+If multiple outputs with `focus-at-startup` are connected, they are prioritized in the order that they appear in the config.
+
+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).
+
+```kdl
+// Focus HDMI-A-1 by default.
+output "HDMI-A-1" {
+ focus-at-startup
+}
+
+// ...if HDMI-A-1 wasn't connected, focus DP-2 instead.
+output "DP-2" {
+ focus-at-startup
+}
+```
+
### `background-color`
<sup>Since: 0.1.8</sup>