From b5c2a1593827be8555661f5a8fa20fb0c748f3ca Mon Sep 17 00:00:00 2001 From: ulixxe <1309337+ulixxe@users.noreply.github.com> Date: Sat, 7 Sep 2024 16:51:59 +0200 Subject: [PATCH] FIX: INPUT_BUFFER full check --- input.h | 3 ++- keyboard_remapper.c | 23 ++++++++++++++++++++--- mouse.c | 12 ++++++++---- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/input.h b/input.h index 0221ecc..04aa715 100644 --- a/input.h +++ b/input.h @@ -48,7 +48,7 @@ static inline uint32_t input_buffer_move_prod_head(struct InputBuffer * input_bu do { old = input_buffer->prod; } while (old.pos.head != old.pos.tail); - if ((INPUT_BUFFER_SIZE - 1 + input_buffer->cons.pos.tail - old.pos.head) & INPUT_BUFFER_MASK == 0) + if (INPUT_BUFFER_SIZE - 1 - ((old.pos.head - input_buffer->cons.pos.tail) & INPUT_BUFFER_MASK) == 0) return 0; new.pos.tail = old.pos.tail; new.pos.head = old.pos.head + 1; @@ -111,6 +111,7 @@ enum Direction { DOWN, }; +void debug_file(const char * message); void debug_print(const char * color, const char * format, ...); void send_input(int scan_code, int virt_code, enum Direction direction, int remap_id, struct InputBuffer * input_buffer); void rehook(); diff --git a/keyboard_remapper.c b/keyboard_remapper.c index 348ec96..88b5ba4 100644 --- a/keyboard_remapper.c +++ b/keyboard_remapper.c @@ -1,9 +1,10 @@ -#define VERSION "1.1.1" +#define VERSION "1.1.2" #include #include #include #include +#include #include "input.h" #include "keys.c" #include "remap.c" @@ -20,6 +21,20 @@ HANDLE ghEvent; HANDLE ghTimerQueue = NULL; struct InputBuffer g_input_buffer; +void debug_file(const char * message) { + FILE * log_file = fopen("debug.log", "a"); + if (log_file == NULL) { + return; + } + + time_t now = time(NULL); + char * timestamp = ctime(&now); + timestamp[strlen(timestamp) - 1] = '\0'; + + fprintf(log_file, "[%s] %s\n", timestamp, message); + fclose(log_file); +} + void debug_print(const char * color, const char * format, ...) { va_list args; va_start(args, format); @@ -38,7 +53,8 @@ void send_input(int scan_code, int virt_code, enum Direction direction, int rema n = input_buffer_move_prod_head(input_buffer, &tail); index = tail & INPUT_BUFFER_MASK; if (n == 0) { - debug_print(RED, "\nError: input buffer is full!"); + if (g_debug) debug_print(RED, "\nError: input buffer is full!"); + debug_file("Error: input buffer is full!"); return; } ZeroMemory(&input_buffer->inputs[index], sizeof(INPUT)); @@ -92,7 +108,8 @@ LRESULT CALLBACK mouse_callback(int msg_code, WPARAM w_param, LPARAM l_param) { n = input_buffer_move_prod_head(&g_input_buffer, &tail); index = tail & INPUT_BUFFER_MASK; if (n == 0) { - debug_print(RED, "\nError: input buffer is full!"); + if (g_debug) debug_print(RED, "\nError: input buffer is full!"); + debug_file("Error: input buffer is full!"); return 1; } ZeroMemory(&g_input_buffer.inputs[index], sizeof(INPUT)); diff --git a/mouse.c b/mouse.c index f24544b..2a58000 100644 --- a/mouse.c +++ b/mouse.c @@ -118,7 +118,8 @@ void buttons_send(struct MouseState * state, int remap_id, struct InputBuffer * n = input_buffer_move_prod_head(input_buffer, &tail); index = tail & INPUT_BUFFER_MASK; if (n == 0) { - debug_print(RED, "\nError: input buffer is full!"); + if (g_debug) debug_print(RED, "\nError: input buffer is full!"); + debug_file("Error: input buffer is full!"); return; } ZeroMemory(&input_buffer->inputs[index], sizeof(INPUT)); @@ -173,7 +174,8 @@ void buttons_send(struct MouseState * state, int remap_id, struct InputBuffer * n = input_buffer_move_prod_head(input_buffer, &tail); index = tail & INPUT_BUFFER_MASK; if (n == 0) { - debug_print(RED, "\nError: input buffer is full!"); + if (g_debug) debug_print(RED, "\nError: input buffer is full!"); + debug_file("Error: input buffer is full!"); return; } ZeroMemory(&input_buffer->inputs[index], sizeof(INPUT)); @@ -205,7 +207,8 @@ void move_send(struct MouseState * state, int remap_id, struct InputBuffer * inp n = input_buffer_move_prod_head(input_buffer, &tail); index = tail & INPUT_BUFFER_MASK; if (n == 0) { - debug_print(RED, "\nError: input buffer is full!"); + if (g_debug) debug_print(RED, "\nError: input buffer is full!"); + debug_file("Error: input buffer is full!"); return; } ZeroMemory(&input_buffer->inputs[index], sizeof(INPUT)); @@ -282,7 +285,8 @@ void move_send(struct MouseState * state, int remap_id, struct InputBuffer * inp n = input_buffer_move_prod_head(input_buffer, &tail); index = tail & INPUT_BUFFER_MASK; if (n == 0) { - debug_print(RED, "\nError: input buffer is full!"); + if (g_debug) debug_print(RED, "\nError: input buffer is full!"); + debug_file("Error: input buffer is full!"); return; } ZeroMemory(&input_buffer->inputs[index], sizeof(INPUT));