diff --git a/.forgejo/workflows/release.yml b/.forgejo/workflows/release.yml index 2a90e26..5a11458 100644 --- a/.forgejo/workflows/release.yml +++ b/.forgejo/workflows/release.yml @@ -16,10 +16,12 @@ jobs: apt-get install -y mingw-w64 zip - name: Build - run: make + run: | + make && + mv LICENSE LICENSE-AORBR - name: Create ZIP file - run: zip -r auto-obs-rp-restart.zip LICENSE AutoOBSRPRestart.lua monitor.dll + run: zip -r auto-obs-rb-restart.zip LICENSE-AORBR AutoOBSRBRestart.lua monitor.dll - name: Create release uses: akkuman/gitea-release-action@v1 @@ -27,4 +29,4 @@ jobs: token: ${{ secrets.TOKEN }} name: Latest version tag_name: latest - files: auto-obs-rp-restart.zip + files: auto-obs-rb-restart.zip diff --git a/AutoOBSRPRestart.lua b/AutoOBSRBRestart.lua similarity index 96% rename from AutoOBSRPRestart.lua rename to AutoOBSRBRestart.lua index b0e1f33..0744990 100644 --- a/AutoOBSRPRestart.lua +++ b/AutoOBSRBRestart.lua @@ -15,7 +15,7 @@ function script_load() ffi.cdef[[ void start_hook(void); void stop_hook(void); - int last_time_input_received(void); + time_t last_time_input_received(void); ]] monitor = ffi.load(script_path() .. "monitor.dll") diff --git a/README.md b/README.md index 6dd4a61..4be1b8b 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,10 @@ -# Auto OBS RP Restart [![status-badge](https://git.mylloon.fr/Anri/auto-obs-rp-restart/badges/workflows/release.yml/badge.svg)](https://git.mylloon.fr/Anri/auto-obs-rp-restart/actions?workflow=release.yml) +# Automatic OBS ReplayBuffer Restart [![status-badge](https://git.mylloon.fr/Anri/auto-obs-rb-restart/badges/workflows/release.yml/badge.svg)](https://git.mylloon.fr/Anri/auto-obs-rb-restart/actions?workflow=release.yml) Automatically restarts the replay buffer when IDLE -- [Download the latest release here](https://git.mylloon.fr/Anri/auto-obs-rp-restart/releases/tag/latest) +- [Download the latest release here](https://git.mylloon.fr/Anri/auto-obs-rb-restart/releases/tag/latest) + +The script wait 2 hours before looking for any input for the last 2 minutes. +If there is none, then it restarts the replay buffer. +Else it waits for the next 2 minutes, etc. +Once restarted, the script wait 2 other hours. diff --git a/monitor.c b/monitor.c index 3eb7cdc..9ff555b 100644 --- a/monitor.c +++ b/monitor.c @@ -1,26 +1,45 @@ +#include #include #include -static DWORD last_input_time = 0; // Store the last input time in milliseconds +static atomic_llong last_input_time = 0; HHOOK hKeyboardHook; +HHOOK hMouseHook; 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); } +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) { - UnhookWindowsHookEx(hKeyboardHook); + if (hKeyboardHook) { + UnhookWindowsHookEx(hKeyboardHook); + hKeyboardHook = NULL; + } + + if (hMouseHook) { + UnhookWindowsHookEx(hMouseHook); + hMouseHook = NULL; + } } -__declspec(dllexport) int last_time_input_received(void) { - return last_input_time; +__declspec(dllexport) time_t last_time_input_received(void) { + return atomic_load(&last_input_time); }