forge
Explore
Log in

Don't you dare go Hollow

ipc: Add focus_timestsamp and WindowFocusTimestampChanged

Browse files at this commit

3769e5da46064b1bd85d7791fc767bcfc36d5a1cIvan MolodetskikhParent: 933ffcb
niri-ipc/src/lib.rsmodified+45 -0
@@ -54,6 +54,7 @@
5454
5555 use std::collections::HashMap;
5656 use std::str::FromStr;
57+use std::time::Duration;
5758
5859 use serde::{Deserialize, Serialize};
5960
@@ -1298,6 +1299,24 @@ pub struct Window {
12981299 pub is_urgent: bool,
12991300 /// Position- and size-related properties of the window.
13001301 pub layout: WindowLayout,
1302+ /// Timestamp when the window was most recently focused.
1303+ ///
1304+ /// This timestamp is intended for most-recently-used window switchers, i.e. Alt-Tab. It only
1305+ /// updates after some debounce time so that quick window switching doesn't mark intermediate
1306+ /// windows as recently focused.
1307+ ///
1308+ /// The timestamp comes from the monotonic clock.
1309+ pub focus_timestamp: Option<Timestamp>,
1310+}
1311+
1312+/// A moment in time.
1313+#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
1314+#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1315+pub struct Timestamp {
1316+ /// Number of whole seconds.
1317+ pub secs: u64,
1318+ /// Fractional part of the timestamp in nanoseconds (10<sup>-9</sup> seconds).
1319+ pub nanos: u32,
13011320 }
13021321
13031322 /// Position- and size-related properties of a [`Window`].
@@ -1513,6 +1532,17 @@ pub enum Event {
15131532 /// Id of the newly focused window, or `None` if no window is now focused.
15141533 id: Option<u64>,
15151534 },
1535+ /// Window focus timestamp changed.
1536+ ///
1537+ /// This event is separate from [`Event::WindowFocusChanged`] because the focus timestamp only
1538+ /// updates after some debounce time so that quick window switching doesn't mark intermediate
1539+ /// windows as recently focused.
1540+ WindowFocusTimestampChanged {
1541+ /// Id of the window.
1542+ id: u64,
1543+ /// The new focus timestamp.
1544+ focus_timestamp: Option<Timestamp>,
1545+ },
15161546 /// Window urgency changed.
15171547 WindowUrgencyChanged {
15181548 /// Id of the window.
@@ -1560,6 +1590,21 @@ pub enum Event {
15601590 },
15611591 }
15621592
1593+impl From<Duration> for Timestamp {
1594+ fn from(value: Duration) -> Self {
1595+ Timestamp {
1596+ secs: value.as_secs(),
1597+ nanos: value.subsec_nanos(),
1598+ }
1599+ }
1600+}
1601+
1602+impl From<Timestamp> for Duration {
1603+ fn from(value: Timestamp) -> Self {
1604+ Duration::new(value.secs, value.nanos)
1605+ }
1606+}
1607+
15631608 impl FromStr for WorkspaceReferenceArg {
15641609 type Err = &'static str;
15651610
niri-ipc/src/state.rsmodified+11 -0
@@ -193,6 +193,17 @@ impl EventStreamStatePart for WindowsState {
193193 win.is_focused = Some(win.id) == id;
194194 }
195195 }
196+ Event::WindowFocusTimestampChanged {
197+ id,
198+ focus_timestamp,
199+ } => {
200+ for win in self.windows.values_mut() {
201+ if win.id == id {
202+ win.focus_timestamp = focus_timestamp;
203+ break;
204+ }
205+ }
206+ }
196207 Event::WindowUrgencyChanged { id, urgent } => {
197208 for win in self.windows.values_mut() {
198209 if win.id == id {
src/ipc/client.rsmodified+6 -0
@@ -458,6 +458,12 @@ pub fn handle_msg(mut msg: Msg, json: bool) -> anyhow::Result<()> {
458458 Event::WindowFocusChanged { id } => {
459459 println!("Window focus changed: {id:?}");
460460 }
461+ Event::WindowFocusTimestampChanged {
462+ id,
463+ focus_timestamp,
464+ } => {
465+ println!("Window {id}: focus timestamp changed to {focus_timestamp:?}");
466+ }
461467 Event::WindowUrgencyChanged { id, urgent } => {
462468 println!("Window {id}: urgency changed to {urgent}");
463469 }
src/ipc/server.rsmodified+10 -1
@@ -18,7 +18,7 @@ use niri_config::OutputName;
1818 use niri_ipc::state::{EventStreamState, EventStreamStatePart as _};
1919 use niri_ipc::{
2020 Action, Event, KeyboardLayouts, OutputConfigChanged, Overview, Reply, Request, Response,
21- WindowLayout, Workspace,
21+ Timestamp, WindowLayout, Workspace,
2222 };
2323 use smithay::desktop::layer_map_for_output;
2424 use smithay::input::pointer::{
@@ -514,6 +514,7 @@ fn make_ipc_window(
514514 is_floating: mapped.is_floating(),
515515 is_urgent: mapped.is_urgent(),
516516 layout,
517+ focus_timestamp: mapped.get_focus_timestamp().map(Timestamp::from),
517518 })
518519 }
519520
@@ -725,6 +726,14 @@ impl State {
725726 events.push(Event::WindowFocusChanged { id: Some(id) });
726727 }
727728
729+ let focus_timestamp = mapped.get_focus_timestamp().map(Timestamp::from);
730+ if focus_timestamp != ipc_win.focus_timestamp {
731+ events.push(Event::WindowFocusTimestampChanged {
732+ id,
733+ focus_timestamp,
734+ });
735+ }
736+
728737 let urgent = mapped.is_urgent();
729738 if urgent != ipc_win.is_urgent {
730739 events.push(Event::WindowUrgencyChanged { id, urgent })