From cf9880281fc5fa36ad1aabdfd0804d73858ba4cc Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 7 Oct 2024 22:41:12 +0200 Subject: [PATCH] only one timer at a time, usage of atomic --- AutoOBSRBRestart.lua | 17 ++++++++++------- monitor.c | 11 +++++++---- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/AutoOBSRBRestart.lua b/AutoOBSRBRestart.lua index 1d93e07..fdd2ec2 100644 --- a/AutoOBSRBRestart.lua +++ b/AutoOBSRBRestart.lua @@ -28,28 +28,31 @@ function script_unload() monitor.stop_hook() end + function check_restart() + obs.timer_remove(check_restart) monitor.start_hook() obs.timer_add(check_idle, idle_time * 1000) end function check_idle() + obs.timer_remove(check_idle) local current_time = os.time() local last_input_time = monitor.last_input_received() if current_time - last_input_time >= idle_time then monitor.stop_hook() - obs.timer_remove(check_idle) - restart_replay_buffer() - end -end -function restart_replay_buffer() - obs.obs_frontend_replay_buffer_stop() - obs.timer_add(start_replay_buffer, 2000) + obs.obs_frontend_replay_buffer_stop() + obs.timer_add(start_replay_buffer, 2000) + else + obs.timer_add(check_idle, idle_time * 1000) + end end function start_replay_buffer() obs.timer_remove(start_replay_buffer) obs.obs_frontend_replay_buffer_start() + + obs.timer_add(check_restart, wait_time * 1000) end diff --git a/monitor.c b/monitor.c index c926eef..ddf07cc 100644 --- a/monitor.c +++ b/monitor.c @@ -1,20 +1,23 @@ +#include #include #include -static long long last_input_time = 0; +static atomic_llong last_input_time = 0; HHOOK hKeyboardHook = NULL; LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode == HC_ACTION) { - last_input_time = time(NULL); + atomic_store(&last_input_time, time(NULL)); } return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam); } __declspec(dllexport) void start_hook(void) { - hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, NULL, 0); + if (!hKeyboardHook) { + hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, NULL, 0); + } } __declspec(dllexport) void stop_hook(void) { @@ -25,5 +28,5 @@ __declspec(dllexport) void stop_hook(void) { } __declspec(dllexport) long long last_input_received(void) { - return last_input_time; + return atomic_load(&last_input_time); }