only one timer at a time, usage of atomic
All checks were successful
Upload release / build (push) Successful in 33s
All checks were successful
Upload release / build (push) Successful in 33s
This commit is contained in:
parent
1f5e245541
commit
cf9880281f
2 changed files with 17 additions and 11 deletions
|
@ -28,28 +28,31 @@ function script_unload()
|
||||||
monitor.stop_hook()
|
monitor.stop_hook()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function check_restart()
|
function check_restart()
|
||||||
|
obs.timer_remove(check_restart)
|
||||||
monitor.start_hook()
|
monitor.start_hook()
|
||||||
obs.timer_add(check_idle, idle_time * 1000)
|
obs.timer_add(check_idle, idle_time * 1000)
|
||||||
end
|
end
|
||||||
|
|
||||||
function check_idle()
|
function check_idle()
|
||||||
|
obs.timer_remove(check_idle)
|
||||||
local current_time = os.time()
|
local current_time = os.time()
|
||||||
local last_input_time = monitor.last_input_received()
|
local last_input_time = monitor.last_input_received()
|
||||||
|
|
||||||
if current_time - last_input_time >= idle_time then
|
if current_time - last_input_time >= idle_time then
|
||||||
monitor.stop_hook()
|
monitor.stop_hook()
|
||||||
obs.timer_remove(check_idle)
|
|
||||||
restart_replay_buffer()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function restart_replay_buffer()
|
obs.obs_frontend_replay_buffer_stop()
|
||||||
obs.obs_frontend_replay_buffer_stop()
|
obs.timer_add(start_replay_buffer, 2000)
|
||||||
obs.timer_add(start_replay_buffer, 2000)
|
else
|
||||||
|
obs.timer_add(check_idle, idle_time * 1000)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function start_replay_buffer()
|
function start_replay_buffer()
|
||||||
obs.timer_remove(start_replay_buffer)
|
obs.timer_remove(start_replay_buffer)
|
||||||
obs.obs_frontend_replay_buffer_start()
|
obs.obs_frontend_replay_buffer_start()
|
||||||
|
|
||||||
|
obs.timer_add(check_restart, wait_time * 1000)
|
||||||
end
|
end
|
||||||
|
|
11
monitor.c
11
monitor.c
|
@ -1,20 +1,23 @@
|
||||||
|
#include <stdatomic.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|
||||||
static long long last_input_time = 0;
|
static atomic_llong last_input_time = 0;
|
||||||
|
|
||||||
HHOOK hKeyboardHook = NULL;
|
HHOOK hKeyboardHook = NULL;
|
||||||
|
|
||||||
LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
|
LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
|
||||||
if (nCode == HC_ACTION) {
|
if (nCode == HC_ACTION) {
|
||||||
last_input_time = time(NULL);
|
atomic_store(&last_input_time, time(NULL));
|
||||||
}
|
}
|
||||||
|
|
||||||
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
|
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
|
||||||
}
|
}
|
||||||
|
|
||||||
__declspec(dllexport) void start_hook(void) {
|
__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) {
|
__declspec(dllexport) void stop_hook(void) {
|
||||||
|
@ -25,5 +28,5 @@ __declspec(dllexport) void stop_hook(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
__declspec(dllexport) long long last_input_received(void) {
|
__declspec(dllexport) long long last_input_received(void) {
|
||||||
return last_input_time;
|
return atomic_load(&last_input_time);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue