Replace last_focus_time with last_focus_tick

Fixes: https://todo.sr.ht/~tsdh/swayr/12
timeout_old
Tassilo Horn 3 years ago
parent b167acac88
commit 7b07141722
  1. 6
      src/cmds.rs
  2. 31
      src/demon.rs
  3. 17
      src/tree.rs

@ -238,7 +238,7 @@ pub fn exec_swayr_cmd(args: ExecSwayrCmdArgs) {
if !before { if !before {
let mut map = props.write().unwrap(); let mut map = props.write().unwrap();
for val in map.values_mut() { for val in map.values_mut() {
val.last_focus_time_for_next_prev_seq = val.last_focus_time; val.last_focus_tick_for_next_prev_seq = val.last_focus_tick;
} }
} }
} else { } else {
@ -806,8 +806,8 @@ pub fn focus_window_in_direction(
} }
wins.sort_by(|a, b| { wins.sort_by(|a, b| {
let lru_a = tree.last_focus_time_for_next_prev_seq(a.node.id); let lru_a = tree.last_focus_tick_for_next_prev_seq(a.node.id);
let lru_b = tree.last_focus_time_for_next_prev_seq(b.node.id); let lru_b = tree.last_focus_tick_for_next_prev_seq(b.node.id);
lru_a.cmp(&lru_b).reverse() lru_a.cmp(&lru_b).reverse()
}); });

@ -26,7 +26,6 @@ use std::os::unix::net::{UnixListener, UnixStream};
use std::sync::Arc; use std::sync::Arc;
use std::sync::RwLock; use std::sync::RwLock;
use std::thread; use std::thread;
use std::time::{SystemTime, UNIX_EPOCH};
use swayipc as s; use swayipc as s;
pub fn run_demon() { pub fn run_demon() {
@ -53,6 +52,7 @@ pub fn monitor_sway_events(
extra_props: Arc<RwLock<HashMap<i64, t::ExtraProps>>>, extra_props: Arc<RwLock<HashMap<i64, t::ExtraProps>>>,
) { ) {
let config = config::load_config(); let config = config::load_config();
let mut focus_counter = 0;
let mut resets = 0; let mut resets = 0;
let max_resets = 10; let max_resets = 10;
@ -76,17 +76,21 @@ pub fn monitor_sway_events(
Ok(ev) => match ev { Ok(ev) => match ev {
s::Event::Window(win_ev) => { s::Event::Window(win_ev) => {
let extra_props_clone = extra_props.clone(); let extra_props_clone = extra_props.clone();
focus_counter += 1;
show_extra_props_state = handle_window_event( show_extra_props_state = handle_window_event(
win_ev, win_ev,
extra_props_clone, extra_props_clone,
&config, &config,
focus_counter,
); );
} }
s::Event::Workspace(ws_ev) => { s::Event::Workspace(ws_ev) => {
let extra_props_clone = extra_props.clone(); let extra_props_clone = extra_props.clone();
focus_counter += 1;
show_extra_props_state = handle_workspace_event( show_extra_props_state = handle_workspace_event(
ws_ev, ws_ev,
extra_props_clone, extra_props_clone,
focus_counter,
); );
} }
s::Event::Shutdown(sd_ev) => { s::Event::Shutdown(sd_ev) => {
@ -124,6 +128,7 @@ fn handle_window_event(
ev: Box<s::WindowEvent>, ev: Box<s::WindowEvent>,
extra_props: Arc<RwLock<HashMap<i64, t::ExtraProps>>>, extra_props: Arc<RwLock<HashMap<i64, t::ExtraProps>>>,
config: &config::Config, config: &config::Config,
focus_val: u64,
) -> bool { ) -> bool {
let s::WindowEvent { let s::WindowEvent {
change, container, .. change, container, ..
@ -131,13 +136,13 @@ fn handle_window_event(
match change { match change {
s::WindowChange::Focus => { s::WindowChange::Focus => {
layout::maybe_auto_tile(config); layout::maybe_auto_tile(config);
update_last_focus_time(container.id, extra_props); update_last_focus_tick(container.id, extra_props, focus_val);
println!("Handled window event type {:?}", change); println!("Handled window event type {:?}", change);
true true
} }
s::WindowChange::New => { s::WindowChange::New => {
layout::maybe_auto_tile(config); layout::maybe_auto_tile(config);
update_last_focus_time(container.id, extra_props); update_last_focus_tick(container.id, extra_props, focus_val);
println!("Handled window event type {:?}", change); println!("Handled window event type {:?}", change);
true true
} }
@ -162,6 +167,7 @@ fn handle_window_event(
fn handle_workspace_event( fn handle_workspace_event(
ev: Box<s::WorkspaceEvent>, ev: Box<s::WorkspaceEvent>,
extra_props: Arc<RwLock<HashMap<i64, t::ExtraProps>>>, extra_props: Arc<RwLock<HashMap<i64, t::ExtraProps>>>,
focus_val: u64,
) -> bool { ) -> bool {
let s::WorkspaceEvent { let s::WorkspaceEvent {
change, change,
@ -171,11 +177,12 @@ fn handle_workspace_event(
} = *ev; } = *ev;
match change { match change {
s::WorkspaceChange::Init | s::WorkspaceChange::Focus => { s::WorkspaceChange::Init | s::WorkspaceChange::Focus => {
update_last_focus_time( update_last_focus_tick(
current current
.expect("No current in Init or Focus workspace event") .expect("No current in Init or Focus workspace event")
.id, .id,
extra_props, extra_props,
focus_val,
); );
println!("Handled workspace event type {:?}", change); println!("Handled workspace event type {:?}", change);
true true
@ -192,19 +199,20 @@ fn handle_workspace_event(
} }
} }
fn update_last_focus_time( fn update_last_focus_tick(
id: i64, id: i64,
extra_props: Arc<RwLock<HashMap<i64, t::ExtraProps>>>, extra_props: Arc<RwLock<HashMap<i64, t::ExtraProps>>>,
focus_val: u64,
) { ) {
let mut write_lock = extra_props.write().unwrap(); let mut write_lock = extra_props.write().unwrap();
if let Some(wp) = write_lock.get_mut(&id) { if let Some(wp) = write_lock.get_mut(&id) {
wp.last_focus_time = get_epoch_time_as_millis(); wp.last_focus_tick = focus_val;
} else { } else {
write_lock.insert( write_lock.insert(
id, id,
t::ExtraProps { t::ExtraProps {
last_focus_time: get_epoch_time_as_millis(), last_focus_tick: focus_val,
last_focus_time_for_next_prev_seq: 0, last_focus_tick_for_next_prev_seq: 0,
}, },
); );
} }
@ -217,13 +225,6 @@ fn remove_extra_props(
extra_props.write().unwrap().remove(&id); extra_props.write().unwrap().remove(&id);
} }
fn get_epoch_time_as_millis() -> u128 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Couldn't get epoch time!")
.as_millis()
}
pub fn serve_client_requests( pub fn serve_client_requests(
extra_props: Arc<RwLock<HashMap<i64, t::ExtraProps>>>, extra_props: Arc<RwLock<HashMap<i64, t::ExtraProps>>>,
) { ) {

@ -160,9 +160,8 @@ impl NodeMethods for s::Node {
/// Extra properties gathered by swayrd for windows and workspaces. /// Extra properties gathered by swayrd for windows and workspaces.
#[derive(Copy, Clone, Debug, Deserialize, Serialize)] #[derive(Copy, Clone, Debug, Deserialize, Serialize)]
pub struct ExtraProps { pub struct ExtraProps {
/// Milliseconds since UNIX epoch. pub last_focus_tick: u64,
pub last_focus_time: u128, pub last_focus_tick_for_next_prev_seq: u64,
pub last_focus_time_for_next_prev_seq: u128,
} }
pub struct Tree<'a> { pub struct Tree<'a> {
@ -211,14 +210,14 @@ impl<'a> Tree<'a> {
} }
} }
pub fn last_focus_time(&self, id: i64) -> u128 { pub fn last_focus_tick(&self, id: i64) -> u64 {
self.extra_props.get(&id).map_or(0, |wp| wp.last_focus_time) self.extra_props.get(&id).map_or(0, |wp| wp.last_focus_tick)
} }
pub fn last_focus_time_for_next_prev_seq(&self, id: i64) -> u128 { pub fn last_focus_tick_for_next_prev_seq(&self, id: i64) -> u64 {
self.extra_props self.extra_props
.get(&id) .get(&id)
.map_or(0, |wp| wp.last_focus_time_for_next_prev_seq) .map_or(0, |wp| wp.last_focus_tick_for_next_prev_seq)
} }
fn sorted_nodes_of_type_1( fn sorted_nodes_of_type_1(
@ -321,8 +320,8 @@ impl<'a> Tree<'a> {
} else if !a.urgent && b.urgent { } else if !a.urgent && b.urgent {
cmp::Ordering::Greater cmp::Ordering::Greater
} else { } else {
let lru_a = self.last_focus_time(a.id); let lru_a = self.last_focus_tick(a.id);
let lru_b = self.last_focus_time(b.id); let lru_b = self.last_focus_tick(b.id);
lru_a.cmp(&lru_b).reverse() lru_a.cmp(&lru_b).reverse()
} }
}); });

Loading…
Cancel
Save