maug
Quick and dirty C mini-augmentation library.
Loading...
Searching...
No Matches
retrocon.h
1
2#ifndef RETROCON_H
3#define RETROCON_H
4
5#define RETROCON_DEBOUNCE_WAIT 3
6
7#define RETROCON_FLAG_ACTIVE 0x01
8
9#define RETROCON_IDC_TEXTBOX 1
10
11#define RETROCON_IDC_CON_BASE 10
12
13#define RETROCON_IDC_CLOSE (-1)
14
15#ifndef RETROCON_TRACE_LVL
16# define RETROCON_TRACE_LVL 0
17#endif /* !RETROCON_TRACE_LVL */
18
19#ifdef RETROCON_DISABLE
20
21struct RETROCON {
22 uint8_t flags;
23 RETROFLAT_COLOR lbuffer_color;
24 RETROFLAT_COLOR sbuffer_color;
25 RETROFLAT_COLOR bg_color;
26};
27
28# define retrocon_init( con, fn, x, y, w, h ) (MERROR_OK)
29
30# define retrocon_add_command( con, cmd, cb, cb_data ) (MERROR_OK)
31
32# define retrocon_display( con, display )
33
34# define retrocon_print_line( con, line )
35
36# define retrocon_exec_line( con, line, line_sz )
37
38# define retrocon_debounce( con, c )
39
40# define retrocon_input( con, p_c, input_evt, p_idc, win_stack )
41
42# define retrocon_shutdown( con )
43
44#else
45
50
51#ifndef RETROCON_TRACE_LVL
52# define RETROCON_TRACE_LVL 0
53#endif /* !RETROCON_TRACE_LVL */
54
55#ifndef RETROCON_SBUFFER_SZ_MAX
56# define RETROCON_SBUFFER_SZ_MAX 4096
57#endif /* !RETROCON_SBUFFER_SZ_MAX */
58
59#ifndef RETROCON_SBUFFER_LINES_MAX
60# define RETROCON_SBUFFER_LINES_MAX 30
61#endif /* !RETROCON_SBUFFER_LINES_MAX */
62
63#ifndef RETROCON_LBUFFER_SZ_MAX
64# define RETROCON_LBUFFER_SZ_MAX 256
65#endif /* !RETROCON_LBUFFER_SZ_MAX */
66
67#ifndef RETROCON_ACTIVE_KEY
68# define RETROCON_ACTIVE_KEY RETROFLAT_KEY_GRAVE
69#endif /* !RETROCON_ACTIVE_KEY */
70
71#ifndef RETROCON_CB_NAME_SZ_MAX
72# define RETROCON_CB_NAME_SZ_MAX 32
73#endif /* !RETROCON_CB_NAME_SZ_MAX */
74
75#ifndef RETROCON_CB_SZ_MAX
76# define RETROCON_CB_SZ_MAX 128
77#endif /* !RETROCON_CB_SZ_MAX */
78
79struct RETROCON;
80
81typedef MERROR_RETVAL (*retrocon_cb)(
82 struct RETROCON* con, const char* line, size_t line_sz, void* data );
83
84struct RETROCON {
85 uint8_t flags;
86 struct RETROGUI gui;
87 int input_prev;
88 int debounce_wait;
89 void* callback_data[RETROCON_CB_SZ_MAX];
90 char callback_names[RETROCON_CB_SZ_MAX][RETROCON_CB_NAME_SZ_MAX + 1];
91 retrocon_cb callbacks[RETROCON_CB_SZ_MAX];
92 size_t callbacks_sz;
93 RETROFLAT_COLOR lbuffer_color;
94 RETROFLAT_COLOR sbuffer_color;
95 RETROFLAT_COLOR bg_color;
96 size_t sbuffer_lines;
97};
98
99MERROR_RETVAL retrocon_init(
100 struct RETROCON* con, const char* font_name,
101 size_t x, size_t y, size_t w, size_t h );
102
103MERROR_RETVAL retrocon_add_command(
104 struct RETROCON* con, const char* cmd, retrocon_cb cb, void* cb_data );
105
106MERROR_RETVAL retrocon_print_line( struct RETROCON* con, const char* line );
107
108MERROR_RETVAL retrocon_exec_line(
109 struct RETROCON* con, char* line, size_t line_sz );
110
111int retrocon_debounce( struct RETROCON* con, int c );
112
127 struct RETROCON* con, RETROFLAT_IN_KEY* p_c,
128 struct RETROFLAT_INPUT* input_evt,
129 retrogui_idc_t* p_idc_out, struct MDATA_VECTOR* win_stack );
130
131MERROR_RETVAL retrocon_display(
132 struct RETROCON* con, retroflat_blit_t* gui_bmp );
133
134void retrocon_shutdown( struct RETROCON* con );
135
136#ifdef RETROCON_C
137
138static MERROR_RETVAL retrocon_cmd_print(
139 struct RETROCON* con, const char* line, size_t line_sz, void* data
140) {
141 MERROR_RETVAL retval = MERROR_OK;
142 char* print_line = NULL;
143
144 print_line = maug_strchr( line, ' ' );
145 if( NULL == print_line ) {
146 /* Not technically an error. */
147 goto cleanup;
148 }
149
150 /* Skip space. */
151 print_line++;
152
153 retrocon_print_line( con, print_line );
154
155cleanup:
156
157 return retval;
158}
159
160static MERROR_RETVAL retrocon_cmd_quit(
161 struct RETROCON* con, const char* line, size_t line_sz, void* data
162) {
163 MERROR_RETVAL retval = MERROR_OK;
164
165 retroflat_quit( 0 );
166
167 return retval;
168}
169
170MERROR_RETVAL retrocon_init(
171 struct RETROCON* con, const char* font_name,
172 size_t x, size_t y, size_t w, size_t h
173) {
174 MERROR_RETVAL retval = MERROR_OK;
175 union RETROGUI_CTL ctl;
176 size_t ctl_y_iter = 0;
177
178 debug_printf( RETROCON_TRACE_LVL, "initializing console..." );
179
180 retval = retrogui_init( &(con->gui) );
181 maug_cleanup_if_not_ok();
182
183 retval = retrogui_set_font( &(con->gui), font_name );
184 maug_cleanup_if_not_ok();
185
186 con->sbuffer_color = RETROFLAT_COLOR_DARKBLUE;
187 con->lbuffer_color = RETROFLAT_COLOR_DARKBLUE;
188 con->gui.x = x;
189 con->gui.y = y;
190 con->gui.w = w;
191 con->gui.h = h;
192
193 retrogui_init_ctl(
194 &ctl, RETROGUI_CTL_TYPE_TEXTBOX, RETROCON_IDC_TEXTBOX );
195
196 ctl.base.x = 5;
197 ctl.base.y = 5;
198 ctl.base.w = con->gui.w - 10;
199 ctl.base.h = 20; /* TODO: Dynamic height based on font. */
200 ctl.base.fg_color = RETROFLAT_COLOR_DARKBLUE;
201
202 retval = retrogui_push_ctl( &(con->gui), &ctl );
203 maug_cleanup_if_not_ok();
204
205 ctl_y_iter = ctl.base.y + ctl.base.h + 1;
206 while( h - 5 > ctl_y_iter + 8 + 1 ) {
207 retrogui_init_ctl(
208 &ctl, RETROGUI_CTL_TYPE_LABEL,
209 RETROCON_IDC_CON_BASE + con->sbuffer_lines );
210
211 ctl.base.x = 5;
212 ctl.base.y = ctl_y_iter;
213 ctl.base.w = con->gui.w - 10;
214 ctl.base.h = 8; /* TODO: Dynamic height based on font. */
215 ctl.base.fg_color = RETROFLAT_COLOR_DARKGRAY;
216 ctl.LABEL.label = "xxxxxx";
217
218 retval = retrogui_push_ctl( &(con->gui), &ctl );
219 maug_cleanup_if_not_ok();
220
221 ctl_y_iter += ctl.base.h + 1;
222 con->sbuffer_lines++;
223 }
224
225 con->gui.focus = RETROCON_IDC_TEXTBOX;
226
227 retval = retrocon_add_command( con, "PRINT", retrocon_cmd_print, NULL );
228 maug_cleanup_if_not_ok();
229 retval = retrocon_add_command( con, "QUIT", retrocon_cmd_quit, NULL );
230 maug_cleanup_if_not_ok();
231
232cleanup:
233
234 return retval;
235}
236
237MERROR_RETVAL retrocon_add_command(
238 struct RETROCON* con, const char* cmd, retrocon_cb cb, void* cb_data
239) {
240 MERROR_RETVAL retval = MERROR_OK;
241
242 debug_printf( RETROCON_TRACE_LVL, "adding console command: %s", cmd );
243
244 maug_cleanup_if_ge_overflow( con->callbacks_sz + 1, RETROCON_CB_SZ_MAX );
245
246 maug_strncpy(
247 con->callback_names[con->callbacks_sz], cmd, RETROCON_CB_NAME_SZ_MAX );
248
249 con->callbacks[con->callbacks_sz] = cb;
250
251 con->callback_data[con->callbacks_sz] = cb_data;
252
253 con->callbacks_sz++;
254
255cleanup:
256
257 return retval;
258}
259
260/* === */
261
262MERROR_RETVAL retrocon_print_line( struct RETROCON* con, const char* line ) {
263 char sbuffer_shift[RETROCON_LBUFFER_SZ_MAX + 1] = { 0 };
264 size_t i = 0;
265 MERROR_RETVAL retval = MERROR_OK;
266
267 /* TODO: Escape newlines in line. */
268
269 /* Shift existing screen buffer lines all down by one. */
270 for( i = con->sbuffer_lines - 1 ; 0 < i ; i-- ) {
271 debug_printf( RETROCON_TRACE_LVL,
272 "copying sbuffer line " SIZE_T_FMT " to " SIZE_T_FMT "...",
273 i - 1, i );
274 maug_mzero( sbuffer_shift, RETROCON_LBUFFER_SZ_MAX + 1 );
275 retval = retrogui_get_ctl_text(
276 &(con->gui), RETROCON_IDC_CON_BASE + i - 1,
277 sbuffer_shift, RETROCON_LBUFFER_SZ_MAX );
278 retval = retrogui_set_ctl_text(
279 &(con->gui), RETROCON_IDC_CON_BASE + i,
280 RETROCON_LBUFFER_SZ_MAX, sbuffer_shift );
281 maug_cleanup_if_not_ok();
282 }
283
284 /* Put line in first sbuffer line. */
285 retval = retrogui_set_ctl_text(
286 &(con->gui), RETROCON_IDC_CON_BASE,
287 RETROCON_LBUFFER_SZ_MAX, line );
288 maug_cleanup_if_not_ok();
289
290cleanup:
291
292 return retval;
293}
294
295/* === */
296
297MERROR_RETVAL retrocon_exec_line(
298 struct RETROCON* con, char* line, size_t line_sz
299) {
300 MERROR_RETVAL retval = MERROR_OK;
301 size_t i = 0;
302 char line_cap[RETROCON_LBUFFER_SZ_MAX + 1];
303
304 /* Create an uppercase for comparison. */
305 maug_mzero( line_cap, RETROCON_LBUFFER_SZ_MAX + 1 );
306 maug_strncpy( line_cap, line, RETROCON_LBUFFER_SZ_MAX );
307 maug_str_upper( line_cap, line_sz );
308
309 /* Find callback with name starting line. */
310 for( i = 0 ; con->callbacks_sz > i ; i++ ) {
311 if(
312 0 == strncmp(
313 /* TODO: Compare up to first space in line. */
314 con->callback_names[i], line_cap,
315 maug_strlen( con->callback_names[i] ) )
316 ) {
317 retval = con->callbacks[i](
318 con, line, line_sz, con->callback_data[i] );
319 goto cleanup;
320 }
321 }
322
323 retrocon_print_line( con, "COMMAND NOT FOUND!" );
324
325cleanup:
326
327 return retval;
328}
329
330/* === */
331
333 struct RETROCON* con, RETROFLAT_IN_KEY* p_c,
334 struct RETROFLAT_INPUT* input_evt,
335 retrogui_idc_t* p_idc_out, struct MDATA_VECTOR* win_stack
336) {
337 MERROR_RETVAL retval = MERROR_OK;
338 char lbuffer[RETROCON_LBUFFER_SZ_MAX + 1] = { 0 };
339
340 *p_idc_out = RETROGUI_IDC_NONE;
341
342 if( *p_c == RETROCON_ACTIVE_KEY ) {
343 debug_printf( RETROCON_TRACE_LVL, "active key pressed" );
344 if( RETROCON_FLAG_ACTIVE != (RETROCON_FLAG_ACTIVE & (con)->flags) ) {
345 debug_printf( RETROCON_TRACE_LVL, "opening console..." );
346 (con)->flags |= RETROCON_FLAG_ACTIVE;
347 (con)->gui.flags |= RETROGUI_FLAGS_DIRTY;
348
349 if( NULL != win_stack ) {
350 retval = retrowin_push_win(
351 &((con)->gui), win_stack,
352 /* Use the IDC provided in p_idc_out for the window IDC. */
353 *p_idc_out,
354 NULL, (con)->gui.x, (con)->gui.y,
355 (con)->gui.w, (con)->gui.h, RETROWIN_FLAG_BORDER_BLUE );
356 maug_cleanup_if_not_ok();
357 }
358
359 debug_printf( RETROCON_TRACE_LVL, "console open!" );
360 } else {
361 debug_printf( RETROCON_TRACE_LVL, "closing console..." );
362 con->flags &= ~RETROCON_FLAG_ACTIVE;
363
364 if( NULL != win_stack ) {
365 retrowin_destroy_win( win_stack, *p_idc_out );
366 debug_printf( RETROCON_TRACE_LVL, "console closed!" );
367 }
368
369# if defined( RETROTILE_PRESENT ) && !defined( RETROFLAT_NO_VIEWPORT_REFRESH )
370 /* Mark all tiles as dirty when console is opened/closed. */
371 retroflat_viewport_lock_refresh();
372 retval = retrotile_clear_refresh( retroflat_viewport_screen_h() );
373 retroflat_viewport_unlock_refresh();
374# endif /* RETROTILE_PRESENT && !RETROFLAT_NO_VIEWPORT_REFRESH */
375
376 /* Return, in case the caller needs to do something with this. */
377 *p_idc_out = RETROCON_IDC_CLOSE;
378 }
379 *p_c = 0;
380 goto cleanup;
381 } else if( RETROCON_FLAG_ACTIVE != (RETROCON_FLAG_ACTIVE & (con)->flags) ) {
382 goto cleanup;
383 }
384
385 /* debug_printf( RETROCON_TRACE_LVL, "processing console input..." ); */
386
387 *p_idc_out = retrogui_poll_ctls( &(con->gui), p_c, input_evt );
388
389 *p_c = 0; /* If we got this far then don't pass keystroke back. */
390
391 if( RETROCON_IDC_TEXTBOX == *p_idc_out ) {
392 retval = retrogui_get_ctl_text(
393 &(con->gui), RETROCON_IDC_TEXTBOX, lbuffer, RETROCON_LBUFFER_SZ_MAX );
394 retval = retrogui_set_ctl_text(
395 &(con->gui), RETROCON_IDC_TEXTBOX, 1, "" );
396 maug_cleanup_if_not_ok();
397
398 if( 0 == maug_strlen( lbuffer ) ) {
399 /* Do nothing if line is empty. */
400 goto cleanup;
401 }
402
403 /* Execute/reset line. */
404 retval = retrocon_exec_line( con, lbuffer, maug_strlen( lbuffer ) );
405 }
406
407#if 0
408 /* Debounce retrocon track only! */
409 if( !retrocon_debounce( con, c ) ) {
410 goto cleanup;
411 }
412
413 /* Process input. */
414 switch( c ) {
415 case RETROCON_ACTIVE_KEY:
416 if( RETROCON_FLAG_ACTIVE == (RETROCON_FLAG_ACTIVE & con->flags) ) {
417 con->flags &= ~RETROCON_FLAG_ACTIVE;
418 } else {
419 con->flags |= RETROCON_FLAG_ACTIVE;
420 }
421 break;
422
423 case 0:
424 break;
425
426 case 0x08:
427 /* Backspace. */
428 if( 0 < con->lbuffer_sz ) {
429 con->lbuffer_sz--;
430 con->lbuffer[con->lbuffer_sz] = '\0';
431 }
432 break;
433
434 case '\r':
435 case '\n':
436 if( 0 == con->lbuffer_sz ) {
437 /* Do nothing if line is empty. */
438 break;
439 }
440
441 /* Execute/reset line. */
442 retval = retrocon_exec_line( con, con->lbuffer, con->lbuffer_sz );
443 con->lbuffer_sz = 0;
444 con->lbuffer[con->lbuffer_sz] = '\0';
445 break;
446
447 default:
448 c = retroflat_vk_to_ascii(
449 c, input_evt->key_flags | RETROFLAT_INPUT_FORCE_UPPER );
450 if(
451 /* Active and printable chars get added to line buffer. */
452 RETROCON_FLAG_ACTIVE == (RETROCON_FLAG_ACTIVE & con->flags) &&
453 0 < c
454 ) {
455 con->lbuffer[con->lbuffer_sz++] = c;
456 con->lbuffer[con->lbuffer_sz] = '\0';
457 }
458 break;
459 }
460#endif
461
462cleanup:
463
464 return retval;
465}
466
467/* === */
468
469MERROR_RETVAL retrocon_display(
470 struct RETROCON* con, retroflat_blit_t* gui_bmp
471) {
472 MERROR_RETVAL retval = MERROR_OK;
473
474 if( RETROCON_FLAG_ACTIVE != (RETROCON_FLAG_ACTIVE & (con)->flags) ) {
475 goto cleanup;
476 }
477
478 (con)->gui.draw_bmp = (gui_bmp);
479 (con)->gui.flags |= RETROGUI_FLAGS_DIRTY;
480
481 retrogui_redraw_ctls( &((con)->gui) );
482
483cleanup:
484
485 return retval;
486}
487
488/* === */
489
490void retrocon_shutdown( struct RETROCON* con ) {
491#ifndef RETROGXC_PRESENT
492 retrofont_free( &(con->gui.font.handle) );
493#endif /* !RETROGXC_PRESENT */
494 retrogui_destroy( &(con->gui) );
495}
496
497#endif /* RETROCON_C */
498 /* maug_console */
500
501#endif /* RETROCON_DISABLE */
502
503#endif /* !RETROCON_H */
504
MERROR_RETVAL retrocon_input(struct RETROCON *con, RETROFLAT_IN_KEY *p_c, struct RETROFLAT_INPUT *input_evt, retrogui_idc_t *p_idc_out, struct MDATA_VECTOR *win_stack)
Process input from retroflat_poll_input() and apply it to the console, if open.
uint16_t MERROR_RETVAL
Return type indicating function returns a value from this list.
Definition merror.h:28
int8_t RETROFLAT_COLOR
Defines an index in the platform-specific color-table.
Definition retroflt.h:326
retrogui_idc_t retrogui_poll_ctls(struct RETROGUI *gui, RETROFLAT_IN_KEY *p_input, struct RETROFLAT_INPUT *input_evt)
Poll for the last clicked control and maintain listboxes and menus.
MERROR_RETVAL retrogui_set_font(struct RETROGUI *gui, const maug_path font_path)
Load the RetroFont API for the given RETROGUI to draw its controls with. Use RetroGXCache API if avai...
int16_t retrogui_idc_t
Unique identifying constant number for controls.
Definition retrogui.h:330
MERROR_RETVAL retrowin_destroy_win(struct MDATA_VECTOR *win_stack, retrogui_idc_t idc)
Destroy the given window's resources and remove it from the window stack.
ssize_t retrowin_push_win(struct RETROGUI *gui, struct MDATA_VECTOR *win_stack, retrogui_idc_t idc, const maug_path font_filename, retroflat_pxxy_t x, retroflat_pxxy_t y, retroflat_pxxy_t w, retroflat_pxxy_t h, uint8_t flags)
Create a new window on the given win_stack.
A vector of uniformly-sized objects, stored contiguously.
Definition mdata.h:108
Definition retrocon.h:84
Struct passed to retroflat_poll_input() to hold return data.
Definition retroflt.h:829
Definition retrogui.h:468
union RETROGXC_CACHABLE font
Font used to draw any attached RETROGUI_CTL.
Definition retrogui.h:489
retrogui_idc_t focus
Unique identifying index for current highlighted RETROGUI_CTL.
Definition retrogui.h:478
Definition retrogui.h:450