15#ifndef CLOCK_MONOTONIC
16 #define CLOCK_MONOTONIC 1
21 #define KEY_EVENT NCTYPE_PRESS
23 #define KEY_EVENT NCTYPE_UNKNOWN
27#define KEY_DEBOUNCE_TIME_MS 20
31 struct timespec last_key_time;
49static long get_time_diff_ms(
struct timespec* start,
struct timespec* end) {
50 return (end->tv_sec - start->tv_sec) * 1000 +
51 (end->tv_nsec - start->tv_nsec) / 1000000;
68 if (raw_input->id ==
'c' && (raw_input->modifiers & NCKEY_MOD_CTRL)) {
73 if (raw_input->evtype == NCTYPE_PRESS || raw_input->evtype == NCTYPE_UNKNOWN) {
75 if (raw_input->id == NCKEY_UP)
return INPUT_UP;
76 if (raw_input->id == NCKEY_DOWN)
return INPUT_DOWN;
77 if (raw_input->id == NCKEY_LEFT)
return INPUT_LEFT;
78 if (raw_input->id == NCKEY_RIGHT)
return INPUT_RIGHT;
81 if (raw_input->id == NCKEY_ENTER || raw_input->id ==
' ')
return INPUT_CONFIRM;
84 if (raw_input->id ==
'c' || raw_input->id ==
'C')
return INPUT_CANCEL;
87 if (raw_input->id ==
'm' || raw_input->id ==
'M')
return INPUT_MENU;
90 if (raw_input->id ==
'l' || raw_input->id ==
'L')
return INPUT_STATS;
93 if (raw_input->id ==
'i' || raw_input->id ==
'I')
return INPUT_INVENTORY;
101 if (!notcurses_ptr) {
102 log_msg(ERROR,
"input_handler",
"Null Notcurses instance provided");
110 input_timing.first_key =
true;
125static bool should_process_key() {
126 struct timespec current_time;
127 clock_gettime(CLOCK_MONOTONIC, ¤t_time);
129 if (input_timing.first_key) {
130 input_timing.first_key =
false;
131 input_timing.last_key_time = current_time;
135 long time_diff = get_time_diff_ms(&input_timing.last_key_time, ¤t_time);
137 if (time_diff < KEY_DEBOUNCE_TIME_MS) {
143 input_timing.last_key_time = current_time;
149 log_msg(ERROR,
"input_handler",
"Null event pointer or uninitialized handler");
155 memset(&raw_input, 0,
sizeof(ncinput));
158 event->type = INPUT_NONE;
159 memset(&event->raw_input, 0,
sizeof(ncinput));
163 uint32_t ret = notcurses_get_blocking(nc, &raw_input);
170 if (!should_process_key()) {
176 event->raw_input = raw_input;
183 log_msg(ERROR,
"input_handler",
"Null event pointer or uninitialized handler");
189 memset(&raw_input, 0,
sizeof(ncinput));
192 event->type = INPUT_NONE;
193 memset(&event->raw_input, 0,
sizeof(ncinput));
195 uint32_t ret = notcurses_get_nblock(nc, &raw_input);
202 if (!should_process_key()) {
208 event->raw_input = raw_input;
Exposes functions for the IO-Handler.
void log_msg(const log_level_t level, const char *module, const char *format,...)
Logs a formatted message with a specified log level and module.
Header file for logging functionality of the game.