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 65535
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 /* TODO: Parse font height from filename and only load printable glyphs. */
184#ifdef RETROGXC_PRESENT
185 con->gui.font_idx = retrogxc_load_font( font_name, 0, 33, 93 );
186#else
187 retval = retrofont_load( font_name, &(con->gui.font_h), 0, 33, 93 );
188#endif /* RETROGXC_PRESENT */
189 maug_cleanup_if_not_ok();
190
191 con->sbuffer_color = RETROFLAT_COLOR_DARKBLUE;
192 con->lbuffer_color = RETROFLAT_COLOR_BLACK;
193 con->gui.x = x;
194 con->gui.y = y;
195 con->gui.w = w;
196 con->gui.h = h;
197
198 retrogui_lock( &(con->gui) );
199
200 retrogui_init_ctl(
201 &ctl, RETROGUI_CTL_TYPE_TEXTBOX, RETROCON_IDC_TEXTBOX );
202
203 ctl.base.x = 5;
204 ctl.base.y = 5;
205 ctl.base.w = con->gui.w - 10;
206 ctl.base.h = 20; /* TODO: Dynamic height based on font. */
207 ctl.base.fg_color = RETROFLAT_COLOR_BLACK;
208
209 retval = retrogui_push_ctl( &(con->gui), &ctl );
210 maug_cleanup_if_not_ok();
211
212 ctl_y_iter = ctl.base.y + ctl.base.h + 1;
213 while( h - 5 > ctl_y_iter + 8 + 1 ) {
214 retrogui_init_ctl(
215 &ctl, RETROGUI_CTL_TYPE_LABEL,
216 RETROCON_IDC_CON_BASE + con->sbuffer_lines );
217
218 ctl.base.x = 5;
219 ctl.base.y = ctl_y_iter;
220 ctl.base.w = con->gui.w - 10;
221 ctl.base.h = 8; /* TODO: Dynamic height based on font. */
222 ctl.base.fg_color = RETROFLAT_COLOR_DARKGRAY;
223 ctl.LABEL.label = "xxxxxx";
224
225 retval = retrogui_push_ctl( &(con->gui), &ctl );
226 maug_cleanup_if_not_ok();
227
228 ctl_y_iter += ctl.base.h + 1;
229 con->sbuffer_lines++;
230 }
231
232 retrogui_unlock( &(con->gui) );
233
234 con->gui.focus = RETROCON_IDC_TEXTBOX;
235
236 retval = retrocon_add_command( con, "PRINT", retrocon_cmd_print, NULL );
237 maug_cleanup_if_not_ok();
238 retval = retrocon_add_command( con, "QUIT", retrocon_cmd_quit, NULL );
239 maug_cleanup_if_not_ok();
240
241cleanup:
242
243 return retval;
244}
245
246MERROR_RETVAL retrocon_add_command(
247 struct RETROCON* con, const char* cmd, retrocon_cb cb, void* cb_data
248) {
249 MERROR_RETVAL retval = MERROR_OK;
250
251 debug_printf( RETROCON_TRACE_LVL, "adding console command: %s", cmd );
252
253 maug_cleanup_if_ge_overflow( con->callbacks_sz + 1, RETROCON_CB_SZ_MAX );
254
255 maug_strncpy(
256 con->callback_names[con->callbacks_sz], cmd, RETROCON_CB_NAME_SZ_MAX );
257
258 con->callbacks[con->callbacks_sz] = cb;
259
260 con->callback_data[con->callbacks_sz] = cb_data;
261
262 con->callbacks_sz++;
263
264cleanup:
265
266 return retval;
267}
268
269/* === */
270
271MERROR_RETVAL retrocon_print_line( struct RETROCON* con, const char* line ) {
272 char sbuffer_shift[RETROCON_LBUFFER_SZ_MAX + 1] = { 0 };
273 size_t i = 0;
274 MERROR_RETVAL retval = MERROR_OK;
275
276 /* TODO: Escape newlines in line. */
277
278 /* Shift existing screen buffer lines all down by one. */
279 for( i = con->sbuffer_lines - 1 ; 0 < i ; i-- ) {
280 debug_printf( RETROCON_TRACE_LVL,
281 "copying sbuffer line " SIZE_T_FMT " to " SIZE_T_FMT "...",
282 i - 1, i );
283 maug_mzero( sbuffer_shift, RETROCON_LBUFFER_SZ_MAX + 1 );
284 retrogui_lock( &(con->gui) );
285 retval = retrogui_get_ctl_text(
286 &(con->gui), RETROCON_IDC_CON_BASE + i - 1,
287 sbuffer_shift, RETROCON_LBUFFER_SZ_MAX );
288 retval = retrogui_set_ctl_text(
289 &(con->gui), RETROCON_IDC_CON_BASE + i,
290 RETROCON_LBUFFER_SZ_MAX, sbuffer_shift );
291 retrogui_unlock( &(con->gui) );
292 maug_cleanup_if_not_ok();
293 }
294
295 /* Put line in first sbuffer line. */
296 retrogui_lock( &(con->gui) );
297 retval = retrogui_set_ctl_text(
298 &(con->gui), RETROCON_IDC_CON_BASE,
299 RETROCON_LBUFFER_SZ_MAX, line );
300 retrogui_unlock( &(con->gui) );
301 maug_cleanup_if_not_ok();
302
303cleanup:
304
305 return retval;
306}
307
308/* === */
309
310MERROR_RETVAL retrocon_exec_line(
311 struct RETROCON* con, char* line, size_t line_sz
312) {
313 MERROR_RETVAL retval = MERROR_OK;
314 size_t i = 0;
315 char line_cap[RETROCON_LBUFFER_SZ_MAX + 1];
316
317 /* Create an uppercase for comparison. */
318 maug_mzero( line_cap, RETROCON_LBUFFER_SZ_MAX + 1 );
319 strncpy( line_cap, line, RETROCON_LBUFFER_SZ_MAX );
320 maug_str_upper( line_cap, line_sz );
321
322 /* Find callback with name starting line. */
323 for( i = 0 ; con->callbacks_sz > i ; i++ ) {
324 if(
325 0 == strncmp(
326 /* TODO: Compare up to first space in line. */
327 con->callback_names[i], line_cap,
328 maug_strlen( con->callback_names[i] ) )
329 ) {
330 retval = con->callbacks[i](
331 con, line, line_sz, con->callback_data[i] );
332 goto cleanup;
333 }
334 }
335
336 retrocon_print_line( con, "COMMAND NOT FOUND!" );
337
338cleanup:
339
340 return retval;
341}
342
343/* === */
344
346 struct RETROCON* con, RETROFLAT_IN_KEY* p_c,
347 struct RETROFLAT_INPUT* input_evt,
348 retrogui_idc_t* p_idc_out, struct MDATA_VECTOR* win_stack
349) {
350 MERROR_RETVAL retval = MERROR_OK;
351 char lbuffer[RETROCON_LBUFFER_SZ_MAX + 1] = { 0 };
352
353 *p_idc_out = RETROGUI_IDC_NONE;
354
355 if( *p_c == RETROCON_ACTIVE_KEY ) {
356 debug_printf( RETROCON_TRACE_LVL, "active key pressed" );
357 if( RETROCON_FLAG_ACTIVE != (RETROCON_FLAG_ACTIVE & (con)->flags) ) {
358 debug_printf( RETROCON_TRACE_LVL, "opening console..." );
359 (con)->flags |= RETROCON_FLAG_ACTIVE;
360 (con)->gui.flags |= RETROGUI_FLAGS_DIRTY;
361
362 if( NULL != win_stack ) {
363 retval = retrowin_push_win(
364 &((con)->gui), win_stack,
365 /* Use the IDC provided in p_idc_out for the window IDC. */
366 *p_idc_out,
367 NULL, (con)->gui.x, (con)->gui.y,
368 (con)->gui.w, (con)->gui.h, RETROWIN_FLAG_BORDER_BLUE );
369 maug_cleanup_if_not_ok();
370 }
371
372 debug_printf( RETROCON_TRACE_LVL, "console open!" );
373 } else {
374 debug_printf( RETROCON_TRACE_LVL, "closing console..." );
375 con->flags &= ~RETROCON_FLAG_ACTIVE;
376
377 if( NULL != win_stack ) {
378 retrowin_destroy_win( win_stack, *p_idc_out );
379 debug_printf( RETROCON_TRACE_LVL, "console closed!" );
380 }
381
382# if defined( RETROTILE_PRESENT ) && !defined( RETROFLAT_NO_VIEWPORT_REFRESH )
383 /* Mark all tiles as dirty when console is opened/closed. */
384 retroflat_viewport_lock_refresh();
385 retval = retrotile_clear_refresh( retroflat_viewport_screen_h() );
386 retroflat_viewport_unlock_refresh();
387# endif /* RETROTILE_PRESENT && !RETROFLAT_NO_VIEWPORT_REFRESH */
388
389 /* Return, in case the caller needs to do something with this. */
390 *p_idc_out = RETROCON_IDC_CLOSE;
391 }
392 *p_c = 0;
393 goto cleanup;
394 } else if( RETROCON_FLAG_ACTIVE != (RETROCON_FLAG_ACTIVE & (con)->flags) ) {
395 goto cleanup;
396 }
397
398 /* debug_printf( RETROCON_TRACE_LVL, "processing console input..." ); */
399
400 retrogui_lock( &(con->gui) );
401
402 *p_idc_out = retrogui_poll_ctls( &(con->gui), p_c, input_evt );
403
404 retrogui_unlock( &(con->gui) );
405
406 *p_c = 0; /* If we got this far then don't pass keystroke back. */
407
408 if( RETROCON_IDC_TEXTBOX == *p_idc_out ) {
409 retrogui_lock( &(con->gui) );
410 retval = retrogui_get_ctl_text(
411 &(con->gui), RETROCON_IDC_TEXTBOX, lbuffer, RETROCON_LBUFFER_SZ_MAX );
412 retval = retrogui_set_ctl_text(
413 &(con->gui), RETROCON_IDC_TEXTBOX, 1, "" );
414 retrogui_unlock( &(con->gui) );
415 maug_cleanup_if_not_ok();
416
417 if( 0 == maug_strlen( lbuffer ) ) {
418 /* Do nothing if line is empty. */
419 goto cleanup;
420 }
421
422 /* Execute/reset line. */
423 retval = retrocon_exec_line( con, lbuffer, maug_strlen( lbuffer ) );
424 }
425
426#if 0
427 /* Debounce retrocon track only! */
428 if( !retrocon_debounce( con, c ) ) {
429 goto cleanup;
430 }
431
432 /* Process input. */
433 switch( c ) {
434 case RETROCON_ACTIVE_KEY:
435 if( RETROCON_FLAG_ACTIVE == (RETROCON_FLAG_ACTIVE & con->flags) ) {
436 con->flags &= ~RETROCON_FLAG_ACTIVE;
437 } else {
438 con->flags |= RETROCON_FLAG_ACTIVE;
439 }
440 break;
441
442 case 0:
443 break;
444
445 case 0x08:
446 /* Backspace. */
447 if( 0 < con->lbuffer_sz ) {
448 con->lbuffer_sz--;
449 con->lbuffer[con->lbuffer_sz] = '\0';
450 }
451 break;
452
453 case '\r':
454 case '\n':
455 if( 0 == con->lbuffer_sz ) {
456 /* Do nothing if line is empty. */
457 break;
458 }
459
460 /* Execute/reset line. */
461 retval = retrocon_exec_line( con, con->lbuffer, con->lbuffer_sz );
462 con->lbuffer_sz = 0;
463 con->lbuffer[con->lbuffer_sz] = '\0';
464 break;
465
466 default:
467 c = retroflat_vk_to_ascii(
468 c, input_evt->key_flags | RETROFLAT_INPUT_FORCE_UPPER );
469 if(
470 /* Active and printable chars get added to line buffer. */
471 RETROCON_FLAG_ACTIVE == (RETROCON_FLAG_ACTIVE & con->flags) &&
472 0 < c
473 ) {
474 con->lbuffer[con->lbuffer_sz++] = c;
475 con->lbuffer[con->lbuffer_sz] = '\0';
476 }
477 break;
478 }
479#endif
480
481cleanup:
482
483 return retval;
484}
485
486/* === */
487
488MERROR_RETVAL retrocon_display(
489 struct RETROCON* con, retroflat_blit_t* gui_bmp
490) {
491 MERROR_RETVAL retval = MERROR_OK;
492
493 if( RETROCON_FLAG_ACTIVE != (RETROCON_FLAG_ACTIVE & (con)->flags) ) {
494 goto cleanup;
495 }
496
497 (con)->gui.draw_bmp = (gui_bmp);
498 (con)->gui.flags |= RETROGUI_FLAGS_DIRTY;
499
500 retrogui_lock( &((con)->gui) );
501 retrogui_redraw_ctls( &((con)->gui) );
502 retrogui_unlock( &((con)->gui) );
503
504cleanup:
505
506 return retval;
507}
508
509/* === */
510
511void retrocon_shutdown( struct RETROCON* con ) {
512#ifndef RETROGXC_PRESENT
513 maug_mfree( con->gui.font_h );
514#endif /* !RETROGXC_PRESENT */
515 retrogui_free( &(con->gui) );
516}
517
518#endif /* RETROCON_C */
519 /* maug_console */
521
522#endif /* RETROCON_DISABLE */
523
524#endif /* !RETROCON_H */
525
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.
int MERROR_RETVAL
Return type indicating function returns a value from this list.
Definition merror.h:19
#define maug_mzero(ptr, sz)
Zero the block of memory pointed to by ptr.
Definition mmem.h:62
int8_t RETROFLAT_COLOR
Defines an index in the platform-specific color-table.
Definition retroflt.h:325
#define RETROGUI_FLAGS_DIRTY
RETROGUI::flags indicating controls should be redrawn.
Definition retrogui.h:28
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.
size_t retrogui_idc_t
Unique identifying constant number for controls.
Definition retrogui.h:103
#define retroflat_quit(retval_in)
This should be called in order to quit a program using RetroFlat.
Definition retpltd.h:47
A vector of uniformly-sized objects, stored contiguously.
Definition mdata.h:89
Definition retrocon.h:84
Struct passed to retroflat_poll_input() to hold return data.
Definition retroflt.h:842
Definition retrogui.h:185
Definition retrogui.h:175