Skip to content

Commit

Permalink
fix delay
Browse files Browse the repository at this point in the history
  • Loading branch information
rkscv committed Jul 31, 2024
1 parent b4d522d commit 2cc4ea3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
14 changes: 9 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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<Mutex<Option<Vec<Danmaku>>>> = LazyLock::new(|| Mutex::new(None));

#[no_mangle]
Expand Down Expand Up @@ -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 {
Expand All @@ -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));
Expand Down Expand Up @@ -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::<f64>().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 => {
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 0 additions & 2 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub struct Options {
pub font_size: f64,
pub transparency: u8,
pub reserved_space: f64,
pub delay: f64,
}

#[derive(Default)]
Expand All @@ -36,7 +35,6 @@ pub fn read_options() -> (Options, Arc<Filter>) {
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());
Expand Down

0 comments on commit 2cc4ea3

Please sign in to comment.