diff --git a/Cargo.lock b/Cargo.lock index e2cc11e..58b4381 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,6 +23,12 @@ version = "1.0.81" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247" +[[package]] +name = "atomic_float" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c4b08ed8a30ff7320117c190eb4d73d47f0ac0c930ab853b8224cef7cd9a5e7" + [[package]] name = "autocfg" version = "1.1.0" @@ -126,6 +132,7 @@ name = "danmaku" version = "0.0.0" dependencies = [ "anyhow", + "atomic_float", "hex", "md-5", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index db5d931..1230e41 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ crate-type = ["cdylib"] [dependencies] anyhow = "1.0" +atomic_float = "1.0" hex = "0.4" md-5 = "0.10" reqwest = { version = "0.12", features = ["json"] } diff --git a/src/lib.rs b/src/lib.rs index 76f395a..5ce4f06 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,7 @@ use crate::{ options::read_options, }; use anyhow::anyhow; +use atomic_float::AtomicF64; use ffi::{mpv_event_property, mpv_node}; use options::{Filter, Options}; use std::{ @@ -41,6 +42,7 @@ pub static mut CTX: *mut mpv_handle = null_mut(); pub static mut CLIENT_NAME: &str = ""; static ENABLED: AtomicBool = AtomicBool::new(false); +static DELAY: AtomicF64 = AtomicF64::new(0.); static COMMENTS: LazyLock>>> = LazyLock::new(|| Mutex::new(None)); #[no_mangle] @@ -69,7 +71,7 @@ async fn main(ctx: *mut mpv_handle) -> c_int { } } - let (mut options, filter) = read_options(); + let (options, filter) = read_options(); let mut handle = spawn(async {}); let mut pause = true; loop { @@ -87,7 +89,7 @@ async fn main(ctx: *mut mpv_handle) -> c_int { mpv_event_id::MPV_EVENT_FILE_LOADED => { handle.abort(); *COMMENTS.lock().await = None; - options.delay = 0.; + DELAY.store(0., Ordering::SeqCst); if ENABLED.load(Ordering::SeqCst) { remove_overlay(); handle = spawn(get(filter.clone(), options)); @@ -193,13 +195,14 @@ async fn main(ctx: *mut mpv_handle) -> c_int { Some(seconds) => { match seconds.to_str().ok().and_then(|s| s.parse::().ok()) { Some(seconds) => { - options.delay += seconds; + let delay = + DELAY.fetch_add(seconds, Ordering::SeqCst) + seconds; if let Some(comments) = &mut *COMMENTS.lock().await { reset(comments); } osd_message(&format!( "Danmaku delay: {:.0} ms", - options.delay * 1000. + delay * 1000. )); } None => { @@ -247,8 +250,9 @@ fn render(comments: &mut [Danmaku], options: Options) -> Option<()> { ); let mut danmaku = Vec::new(); + let delay = DELAY.load(Ordering::SeqCst); for comment in comments.iter_mut().filter(|c| !c.blocked) { - let time = comment.time + options.delay; + let time = comment.time + delay; if time > pos + DURATION / 2. { break; } diff --git a/src/options.rs b/src/options.rs index 3d8ebee..56fbe01 100644 --- a/src/options.rs +++ b/src/options.rs @@ -21,7 +21,6 @@ pub struct Options { pub font_size: f64, pub transparency: u8, pub reserved_space: f64, - pub delay: f64, } #[derive(Default)] @@ -36,7 +35,6 @@ pub fn read_options() -> (Options, Arc) { font_size: 40., transparency: 0x30, reserved_space: 0., - delay: 0., }; let Some(path) = expand_path(&format!("~~/script-opts/{}.conf", unsafe { CLIENT_NAME })) else { return (opts, Default::default());