ipc: Add focus_timestsamp and WindowFocusTimestampChanged
Browse files at this commit3769e5da46064b1bd85d7791fc767bcfc36d5a1cParent: 933ffcb4 changed files +72 -1
- niri-ipc/src/lib.rs +45 -0
- niri-ipc/src/state.rs +11 -0
- src/ipc/client.rs +6 -0
- src/ipc/server.rs +10 -1
niri-ipc/src/lib.rsmodified+45 -0
| @@ -54,6 +54,7 @@ | ||
| 54 | 54 | |
| 55 | 55 | use std::collections::HashMap; |
| 56 | 56 | use std::str::FromStr; |
| 57 | +use std::time::Duration; | |
| 57 | 58 | |
| 58 | 59 | use serde::{Deserialize, Serialize}; |
| 59 | 60 | |
| @@ -1298,6 +1299,24 @@ pub struct Window { | ||
| 1298 | 1299 | pub is_urgent: bool, |
| 1299 | 1300 | /// Position- and size-related properties of the window. |
| 1300 | 1301 | 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, | |
| 1301 | 1320 | } |
| 1302 | 1321 | |
| 1303 | 1322 | /// Position- and size-related properties of a [`Window`]. |
| @@ -1513,6 +1532,17 @@ pub enum Event { | ||
| 1513 | 1532 | /// Id of the newly focused window, or `None` if no window is now focused. |
| 1514 | 1533 | id: Option<u64>, |
| 1515 | 1534 | }, |
| 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 | + }, | |
| 1516 | 1546 | /// Window urgency changed. |
| 1517 | 1547 | WindowUrgencyChanged { |
| 1518 | 1548 | /// Id of the window. |
| @@ -1560,6 +1590,21 @@ pub enum Event { | ||
| 1560 | 1590 | }, |
| 1561 | 1591 | } |
| 1562 | 1592 | |
| 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 | + | |
| 1563 | 1608 | impl FromStr for WorkspaceReferenceArg { |
| 1564 | 1609 | type Err = &'static str; |
| 1565 | 1610 |
niri-ipc/src/state.rsmodified+11 -0
| @@ -193,6 +193,17 @@ impl EventStreamStatePart for WindowsState { | ||
| 193 | 193 | win.is_focused = Some(win.id) == id; |
| 194 | 194 | } |
| 195 | 195 | } |
| 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 | + } | |
| 196 | 207 | Event::WindowUrgencyChanged { id, urgent } => { |
| 197 | 208 | for win in self.windows.values_mut() { |
| 198 | 209 | 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<()> { | ||
| 458 | 458 | Event::WindowFocusChanged { id } => { |
| 459 | 459 | println!("Window focus changed: {id:?}"); |
| 460 | 460 | } |
| 461 | + Event::WindowFocusTimestampChanged { | |
| 462 | + id, | |
| 463 | + focus_timestamp, | |
| 464 | + } => { | |
| 465 | + println!("Window {id}: focus timestamp changed to {focus_timestamp:?}"); | |
| 466 | + } | |
| 461 | 467 | Event::WindowUrgencyChanged { id, urgent } => { |
| 462 | 468 | println!("Window {id}: urgency changed to {urgent}"); |
| 463 | 469 | } |
src/ipc/server.rsmodified+10 -1
| @@ -18,7 +18,7 @@ use niri_config::OutputName; | ||
| 18 | 18 | use niri_ipc::state::{EventStreamState, EventStreamStatePart as _}; |
| 19 | 19 | use niri_ipc::{ |
| 20 | 20 | Action, Event, KeyboardLayouts, OutputConfigChanged, Overview, Reply, Request, Response, |
| 21 | - WindowLayout, Workspace, | |
| 21 | + Timestamp, WindowLayout, Workspace, | |
| 22 | 22 | }; |
| 23 | 23 | use smithay::desktop::layer_map_for_output; |
| 24 | 24 | use smithay::input::pointer::{ |
| @@ -514,6 +514,7 @@ fn make_ipc_window( | ||
| 514 | 514 | is_floating: mapped.is_floating(), |
| 515 | 515 | is_urgent: mapped.is_urgent(), |
| 516 | 516 | layout, |
| 517 | + focus_timestamp: mapped.get_focus_timestamp().map(Timestamp::from), | |
| 517 | 518 | }) |
| 518 | 519 | } |
| 519 | 520 | |
| @@ -725,6 +726,14 @@ impl State { | ||
| 725 | 726 | events.push(Event::WindowFocusChanged { id: Some(id) }); |
| 726 | 727 | } |
| 727 | 728 | |
| 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 | + | |
| 728 | 737 | let urgent = mapped.is_urgent(); |
| 729 | 738 | if urgent != ipc_win.is_urgent { |
| 730 | 739 | events.push(Event::WindowUrgencyChanged { id, urgent }) |