DungeonCrawl
Loading...
Searching...
No Matches
input_handler.h File Reference

Exposes functions for working with input. More...

#include "input_types.h"
#include <notcurses/notcurses.h>

Go to the source code of this file.

Functions

bool init_input_handler (struct notcurses *nc)
 Initialize the input handler.
bool get_input_blocking (input_event_t *event)
 Get the next input event (blocking)
bool get_input_nonblocking (input_event_t *event)
 Get the next input event (non-blocking)
input_t translate_input (const ncinput *raw_input)
 Translate a raw Notcurses input to a logical input type.
void shutdown_input_handler (void)
 Shutdown the input handler.

Detailed Description

Exposes functions for working with input.

Definition in file input_handler.h.

Function Documentation

◆ get_input_blocking()

bool get_input_blocking ( input_event_t * event)

Get the next input event (blocking)

Waits for an input event and translates it to a logical input type. This function blocks until input is received.

Parameters
[out]eventPointer to an input_event_t structure to fill with the input event
Returns
true if an event was retrieved, false on error

Definition at line 147 of file input_handler.c.

147 {
148 if (!event || !nc) {
149 log_msg(ERROR, "input_handler", "Null event pointer or uninitialized handler");
150 return false;
151 }
152
153 // Get input directly using notcurses
154 ncinput raw_input;
155 memset(&raw_input, 0, sizeof(ncinput));
156
157 // Set default values in case we return early
158 event->type = INPUT_NONE;
159 memset(&event->raw_input, 0, sizeof(ncinput));
160
161 // Loop until we get a valid input or notcurses returns an error
162 while (true) {
163 uint32_t ret = notcurses_get_blocking(nc, &raw_input);
164 if (ret <= 0) {
165 // Error or no input
166 return false;
167 }
168
169 // Debounce - if we're getting keys too fast, ignore some
170 if (!should_process_key()) {
171 continue;
172 }
173
174 // Translate and fill the event structure
175 event->type = translate_input(&raw_input);
176 event->raw_input = raw_input;
177 return true;
178 }
179}
input_t translate_input(const ncinput *raw_input)
Translate a raw Notcurses input to a logical input type.
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.
Definition logger.c:246

◆ get_input_nonblocking()

bool get_input_nonblocking ( input_event_t * event)

Get the next input event (non-blocking)

Checks for an input event and translates it to a logical input type. This function does not block if no input is available.

Parameters
[out]eventPointer to an input_event_t structure to fill with the input event
Returns
true if an event was retrieved, false if no events are available

Definition at line 181 of file input_handler.c.

181 {
182 if (!event || !nc) {
183 log_msg(ERROR, "input_handler", "Null event pointer or uninitialized handler");
184 return false;
185 }
186
187 // Get input directly using notcurses
188 ncinput raw_input;
189 memset(&raw_input, 0, sizeof(ncinput));
190
191 // Set default values in case we return early
192 event->type = INPUT_NONE;
193 memset(&event->raw_input, 0, sizeof(ncinput));
194
195 uint32_t ret = notcurses_get_nblock(nc, &raw_input);
196 if (ret <= 0) {
197 // No input available
198 return false;
199 }
200
201 // Debounce - if we're getting keys too fast, ignore some
202 if (!should_process_key()) {
203 return false;
204 }
205
206 // Translate and fill the event structure
207 event->type = translate_input(&raw_input);
208 event->raw_input = raw_input;
209 return true;
210}

◆ init_input_handler()

bool init_input_handler ( struct notcurses * nc)

Initialize the input handler.

Sets up the input handling system. This function must be called before any other input functions.

Parameters
ncThe Notcurses instance to use for input handling
Returns
true on success, false on failure

Definition at line 100 of file input_handler.c.

100 {
101 if (!notcurses_ptr) {
102 log_msg(ERROR, "input_handler", "Null Notcurses instance provided");
103 return false;
104 }
105
106 // Assign to the global variable
107 nc = notcurses_ptr;
108
109 // Initialize input timing
110 input_timing.first_key = true;
111
112 return true;
113}

◆ shutdown_input_handler()

void shutdown_input_handler ( void )

Shutdown the input handler.

Cleans up resources. This function should be called when shutting down the game.

Definition at line 212 of file input_handler.c.

212 {
213 nc = NULL;
214}

◆ translate_input()

input_t translate_input ( const ncinput * raw_input)

Translate a raw Notcurses input to a logical input type.

Helper function to convert from hardware-specific to logical inputs.

Parameters
raw_inputThe raw Notcurses input
Returns
The corresponding logical input type

Definition at line 62 of file input_handler.c.

62 {
63 if (!raw_input) {
64 return INPUT_NONE;
65 }
66
67 // Handle special case for Ctrl+C to quit
68 if (raw_input->id == 'c' && (raw_input->modifiers & NCKEY_MOD_CTRL)) {
69 return INPUT_QUIT;
70 }
71
72 // Check if this is a key event (allow both NCTYPE_UNKNOWN and NCTYPE_PRESS)
73 if (raw_input->evtype == NCTYPE_PRESS || raw_input->evtype == NCTYPE_UNKNOWN) {
74 // Arrow keys for navigation
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;
79
80 // Enter key for confirmation
81 if (raw_input->id == NCKEY_ENTER || raw_input->id == ' ') return INPUT_CONFIRM;
82
83 // Cancel key (C)
84 if (raw_input->id == 'c' || raw_input->id == 'C') return INPUT_CANCEL;
85
86 // Menu key (M)
87 if (raw_input->id == 'm' || raw_input->id == 'M') return INPUT_MENU;
88
89 // Stats key (L)
90 if (raw_input->id == 'l' || raw_input->id == 'L') return INPUT_STATS;
91
92 // Inventory key (I)
93 if (raw_input->id == 'i' || raw_input->id == 'I') return INPUT_INVENTORY;
94 }
95
96 // If we didn't match anything, return INPUT_NONE
97 return INPUT_NONE;
98}