Skip to content

Commit

Permalink
FIX: INPUT_BUFFER full check
Browse files Browse the repository at this point in the history
  • Loading branch information
ulixxe committed Sep 14, 2024
1 parent f056e8b commit b5c2a15
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
3 changes: 2 additions & 1 deletion input.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
23 changes: 20 additions & 3 deletions keyboard_remapper.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#define VERSION "1.1.1"
#define VERSION "1.1.2"

#include <windows.h>
#include <stdio.h>
#include <ctype.h>
#include <stdint.h>
#include <time.h>
#include "input.h"
#include "keys.c"
#include "remap.c"
Expand All @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
12 changes: 8 additions & 4 deletions mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit b5c2a15

Please sign in to comment.