Add audio deps; extract input handling to threads

Add ALSA and Rodio crates to Cargo.toml and lockfile to enable audio
playback.
Update Nix expressions to include `alsa-lib` as a build input.
Refactor input event handling into a new `app::threads` module:
 - Define `AppEvent` enum and `handle_input_events` function there.
 - Adjust imports in `app/app.rs` and `main.rs` accordingly.
   Remove the now‑unused `handle_input_events` and `AppEvent`
   definitions from
   `app/app.rs`.
This commit is contained in:
2026-04-06 11:47:46 +02:00
parent 92ace142b4
commit 30f2e17ed7
11 changed files with 954 additions and 59 deletions
Generated
+904 -21
View File
File diff suppressed because it is too large Load Diff
+1
View File
@@ -14,3 +14,4 @@ clap = { version = "4.5.53", features = ["derive"] }
ratatui = "0.30.0" ratatui = "0.30.0"
log = "0.4.29" log = "0.4.29"
simplelog = "0.12.2" simplelog = "0.12.2"
rodio = "0.22.2"
+2 -1
View File
@@ -2,6 +2,7 @@
lib, lib,
rustPlatform, rustPlatform,
pkg-config, pkg-config,
alsa-lib,
}: }:
rustPlatform.buildRustPackage { rustPlatform.buildRustPackage {
name = "war-in-tunnels"; name = "war-in-tunnels";
@@ -9,7 +10,7 @@ rustPlatform.buildRustPackage {
version = "0.1.0"; version = "0.1.0";
src = ./.; src = ./.;
# buildInputs = [ ]; buildInputs = [alsa-lib];
nativeBuildInputs = [pkg-config]; nativeBuildInputs = [pkg-config];
cargoHash = lib.fakeHash; cargoHash = lib.fakeHash;
+3 -2
View File
@@ -42,15 +42,16 @@
develop = naerskLib.buildPackage { develop = naerskLib.buildPackage {
name = "war-in-tunnels"; name = "war-in-tunnels";
src = ./.; src = ./.;
# buildInputs = with pkgs; []; buildInputs = with pkgs; [alsa-lib];
nativeBuildInputs = with pkgs; [pkg-config]; nativeBuildInputs = with pkgs; [pkg-config];
}; };
}; };
devShells.${system}.default = pkgs.mkShell { devShells.${system}.default = pkgs.mkShell {
env.RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; env.RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
buildInputs = [ buildInputs = with pkgs; [
rustToolchain rustToolchain
alsa-lib
]; ];
nativeBuildInputs = with pkgs; [pkg-config]; nativeBuildInputs = with pkgs; [pkg-config];
Binary file not shown.
+3 -33
View File
@@ -1,23 +1,14 @@
use crate::{ use crate::{
app::{GameStates, handle_keybindings, view::View}, app::{GameStates, handle_keybindings, threads::AppEvent, view::View},
cli::Cli, cli::Cli,
}; };
use ratatui::{ use ratatui::{DefaultTerminal, Frame, crossterm::event::KeyEvent, layout::Rect};
DefaultTerminal, Frame,
crossterm::event::{Event, KeyEvent, read},
layout::Rect,
};
use std::{ use std::{
io::Result, io::Result,
sync::mpsc::{Receiver, RecvTimeoutError, Sender}, sync::mpsc::{Receiver, RecvTimeoutError},
time::Duration, time::Duration,
}; };
pub enum AppEvent {
Input(KeyEvent),
Resize(u16, u16),
}
pub struct App { pub struct App {
pub exit: bool, pub exit: bool,
pub view: View, pub view: View,
@@ -85,24 +76,3 @@ impl App {
Ok(()) Ok(())
} }
} }
pub fn handle_input_events(tx: Sender<AppEvent>) {
loop {
match read() {
Ok(ev) => match ev {
Event::Key(key) => {
if tx.send(AppEvent::Input(key)).is_err() {
break;
}
}
Event::Resize(c, r) => {
if tx.send(AppEvent::Resize(c, r)).is_err() {
break;
}
}
_ => {}
},
Err(_) => continue,
}
}
}
+2 -1
View File
@@ -4,11 +4,12 @@ pub mod keybind;
pub mod keybindings; pub mod keybindings;
pub mod state; pub mod state;
pub mod states; pub mod states;
pub mod threads;
pub mod view; pub mod view;
pub mod views; pub mod views;
pub mod widgets; pub mod widgets;
pub use app::{App, AppEvent, handle_input_events}; pub use app::App;
pub use keybind::handle_keybindings; pub use keybind::handle_keybindings;
pub use state::GameStates; pub use state::GameStates;
pub use view::View; pub use view::View;
+6
View File
@@ -0,0 +1,6 @@
use ratatui::crossterm::event::KeyEvent;
pub enum AppEvent {
Input(KeyEvent),
Resize(u16, u16),
}
+24
View File
@@ -0,0 +1,24 @@
use crate::app::threads::AppEvent;
use ratatui::crossterm::event::{Event, read};
use std::sync::mpsc::Sender;
pub fn handle_input_events(tx: Sender<AppEvent>) {
loop {
match read() {
Ok(ev) => match ev {
Event::Key(key) => {
if tx.send(AppEvent::Input(key)).is_err() {
break;
}
}
Event::Resize(c, r) => {
if tx.send(AppEvent::Resize(c, r)).is_err() {
break;
}
}
_ => {}
},
Err(_) => continue,
}
}
}
+5
View File
@@ -0,0 +1,5 @@
pub mod events;
pub mod handle_input_events;
pub use events::AppEvent;
pub use handle_input_events::handle_input_events;
+4 -1
View File
@@ -5,7 +5,10 @@ use std::{
thread::{self, JoinHandle}, thread::{self, JoinHandle},
}; };
use war_in_tunnels::{ use war_in_tunnels::{
app::{App, AppEvent, handle_input_events}, app::{
App,
threads::{AppEvent, handle_input_events},
},
cli::{Cli, get_args}, cli::{Cli, get_args},
logs::init_logger, logs::init_logger,
}; };