This commit is contained in:
parent
39dadd8cfa
commit
1f5e245541
2 changed files with 8 additions and 23 deletions
|
@ -15,7 +15,7 @@ function script_load()
|
|||
ffi.cdef[[
|
||||
void start_hook(void);
|
||||
void stop_hook(void);
|
||||
time_t last_time_input_received(void);
|
||||
long long last_input_received(void);
|
||||
]]
|
||||
monitor = ffi.load(script_path() .. "monitor.dll")
|
||||
|
||||
|
@ -25,6 +25,7 @@ end
|
|||
function script_unload()
|
||||
obs.timer_remove(check_restart)
|
||||
obs.timer_remove(check_idle)
|
||||
monitor.stop_hook()
|
||||
end
|
||||
|
||||
function check_restart()
|
||||
|
@ -34,7 +35,7 @@ end
|
|||
|
||||
function check_idle()
|
||||
local current_time = os.time()
|
||||
local last_input_time = monitor.last_time_input_received()
|
||||
local last_input_time = monitor.last_input_received()
|
||||
|
||||
if current_time - last_input_time >= idle_time then
|
||||
monitor.stop_hook()
|
||||
|
|
26
monitor.c
26
monitor.c
|
@ -1,31 +1,20 @@
|
|||
#include <stdatomic.h>
|
||||
#include <time.h>
|
||||
#include <windows.h>
|
||||
|
||||
static atomic_llong last_input_time = 0;
|
||||
static long long last_input_time = 0;
|
||||
|
||||
HHOOK hKeyboardHook;
|
||||
HHOOK hMouseHook;
|
||||
HHOOK hKeyboardHook = NULL;
|
||||
|
||||
LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
|
||||
if (nCode == HC_ACTION) {
|
||||
atomic_store(&last_input_time, time(NULL));
|
||||
last_input_time = time(NULL);
|
||||
}
|
||||
|
||||
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
|
||||
}
|
||||
|
||||
LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
|
||||
if (nCode == HC_ACTION) {
|
||||
atomic_store(&last_input_time, time(NULL));
|
||||
}
|
||||
|
||||
return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
|
||||
}
|
||||
|
||||
__declspec(dllexport) void start_hook(void) {
|
||||
hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, NULL, 0);
|
||||
hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, NULL, 0);
|
||||
}
|
||||
|
||||
__declspec(dllexport) void stop_hook(void) {
|
||||
|
@ -33,13 +22,8 @@ __declspec(dllexport) void stop_hook(void) {
|
|||
UnhookWindowsHookEx(hKeyboardHook);
|
||||
hKeyboardHook = NULL;
|
||||
}
|
||||
|
||||
if (hMouseHook) {
|
||||
UnhookWindowsHookEx(hMouseHook);
|
||||
hMouseHook = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
__declspec(dllexport) time_t last_time_input_received(void) {
|
||||
return atomic_load(&last_input_time);
|
||||
__declspec(dllexport) long long last_input_received(void) {
|
||||
return last_input_time;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue