Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub enum Hotkey {
SkipKey,
UndoKey,
PauseKey,
SwitchComparisonPrevious,
SwitchComparisonNext,
ToggleGlobalHotkeys,
}

Expand Down Expand Up @@ -162,6 +164,8 @@ impl KeyState {
b"SkipKey" if in_profile => XmlExpect::Hotkey(Hotkey::SkipKey),
b"UndoKey" if in_profile => XmlExpect::Hotkey(Hotkey::UndoKey),
b"PauseKey" if in_profile => XmlExpect::Hotkey(Hotkey::PauseKey),
b"SwitchComparisonPrevious" if in_profile => XmlExpect::Hotkey(Hotkey::SwitchComparisonPrevious),
b"SwitchComparisonNext" if in_profile => XmlExpect::Hotkey(Hotkey::SwitchComparisonNext),
b"ToggleGlobalHotkeys" if in_profile => {
XmlExpect::Hotkey(Hotkey::ToggleGlobalHotkeys)
}
Expand Down
143 changes: 143 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::mem::size_of;
use std::ptr::addr_of;
use std::env;
use std::collections::HashMap;
use std::collections::HashSet;

use anyhow::{anyhow, Context, Result};
use async_std::channel::{unbounded, Receiver, Sender};
Expand All @@ -17,6 +20,9 @@ use libc::input_event;
mod key;
use key::*;

use quick_xml::events::Event as XmlEvent;
use quick_xml::Reader as XmlReader;

/// Listen for LiveSplit hotkeys
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
Expand Down Expand Up @@ -52,6 +58,107 @@ impl HotkeyListener {
Ok(Self { args, key_state })
}

const COMPARISONS: [&'static str; 8] = [
"Best Segments",
"Best Split Times",
"Average Segments",
"Median Segments",
"Worst Segments",
"Balanced PB",
"Latest Run",
"Personal Best",
];

pub const COMPARISON_COMMANDS: [&'static [u8]; 8] = [
b"setcomparison Best Segments\r\n",
b"setcomparison Best Split Times\r\n",
b"setcomparison Average Segments\r\n",
b"setcomparison Median Segments\r\n",
b"setcomparison Worst Segments\r\n",
b"setcomparison Balanced PB\r\n",
b"setcomparison Latest Run\r\n",
b"setcomparison Personal Best\r\n",
];

fn read_last_comparison(settings_path: Option<&str>) -> Result<Option<String>> {
let mut reader = match settings_path {
Some(s) => XmlReader::from_file(s),
None => XmlReader::from_file(env::var("HOME")? + "/LiveSplit/settings.cfg"),
}
.context("Failed to open LiveSplit settings")?;
reader.trim_text(true);
let mut buf = Vec::new();
let mut last_comparison = None;
let mut found = false;

loop {
match reader.read_event_into(&mut buf)? {
XmlEvent::Start(e) if e.name().as_ref() == b"LastComparison" => {
found = true;
}
XmlEvent::Text(e) if found => {
last_comparison = Some(e.unescape()?.to_string());
break;
}
XmlEvent::Eof => break,
_ => (),
}
}
Ok(last_comparison)
}

fn read_enabled_comparisons(settings_path: Option<&str>) -> Result<Vec<&'static str>> {
let mut reader = match settings_path {
Some(s) => XmlReader::from_file(s),
None => XmlReader::from_file(env::var("HOME")? + "/LiveSplit/settings.cfg"),
}
.context("Failed to open LiveSplit settings")?;
reader.trim_text(true);
let mut buf = Vec::new();
let mut in_states = false;
let mut enabled_map: HashMap<String, bool> = HashMap::new();
let mut current_name: Option<String> = None;

loop {
match reader.read_event_into(&mut buf)? {
XmlEvent::Start(e) => {
if e.name().as_ref() == b"ComparisonGeneratorStates" {
in_states = true;
} else if in_states && e.name().as_ref() == b"Generator" {
for attr in e.attributes().flatten() {
if attr.key.as_ref() == b"name" {
current_name = Some(attr.unescape_value()?.to_string());
}
}
}
}
XmlEvent::Text(e) => {
if in_states {
if let Some(name) = current_name.take() {
let value = e.unescape()?.to_ascii_lowercase();
enabled_map.insert(name, value == "true");
}
}
}
XmlEvent::End(e) => {
if e.name().as_ref() == b"ComparisonGeneratorStates" {
break;
}
}
XmlEvent::Eof => break,
_ => (),
}
}
// Always include "Personal Best"
let mut enabled: Vec<&'static str> = Self::COMPARISONS
.iter()
.filter(|&&c| enabled_map.get(c).copied().unwrap_or(false))
.copied()
.collect();
enabled.push("Personal Best");
Ok(enabled)
}

async fn listen_keyboard(sender: Sender<(u32, bool)>, path: PathBuf) -> Result<()> {
let ev_key = EV["KEY"] as u16;
let mut file = File::open(path).await?;
Expand All @@ -78,8 +185,32 @@ impl HotkeyListener {
.context("Could not connect to LiveSplit server")?;
let mut paused = false;

let enabled_comparisons = Self::read_enabled_comparisons(self.args.settings.as_deref())?;

let enabled_indices: Vec<usize> = enabled_comparisons
.iter()
.filter_map(|&name| {
Self::COMPARISONS.iter().position(|&c| c == name)
})
.collect();

let last_comparison = Self::read_last_comparison(self.args.settings.as_deref())?
.unwrap_or_else(|| "Personal Best".to_string());

let mut comparison_index = enabled_comparisons
.iter()
.position(|&c| c == last_comparison)
.unwrap_or(0);

let mut last_states: HashSet<(u32, bool)> = HashSet::new();

loop {
let (code, is_pressed) = receiver.recv().await?;
if !last_states.insert((code, is_pressed)) {
continue; // duplicate, skip
}
// Remove the opposite state to keep the set small
last_states.remove(&(code, !is_pressed));
if self.args.verbose > 1 {
println!("Key {} = {}", code, is_pressed);
}
Expand All @@ -103,6 +234,18 @@ impl HotkeyListener {
paused = !paused;
command
}
Hotkey::SwitchComparisonNext => {
comparison_index = (comparison_index + 1) % enabled_indices.len();
Self::COMPARISON_COMMANDS[enabled_indices[comparison_index]]
}
Hotkey::SwitchComparisonPrevious => {
if comparison_index == 0 {
comparison_index = enabled_indices.len() - 1;
} else {
comparison_index -= 1;
}
Self::COMPARISON_COMMANDS[enabled_indices[comparison_index]]
}
_ => continue,
};

Expand Down