maug
Quick and dirty C mini-augmentation library.
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
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_idc = 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 maug_cleanup_if_not_ok();
279 retval = retrogui_set_ctl_text(
280 &(con->gui), RETROCON_IDC_CON_BASE + i,
281 RETROCON_LBUFFER_SZ_MAX, sbuffer_shift );
282 maug_cleanup_if_not_ok();
283 }
284
285 /* Put line in first sbuffer line. */
286 retval = retrogui_set_ctl_text(
287 &(con->gui), RETROCON_IDC_CON_BASE,
288 RETROCON_LBUFFER_SZ_MAX, line );
289 maug_cleanup_if_not_ok();
290
291cleanup:
292
293 return retval;
294}
295
296/* === */
297
298MERROR_RETVAL retrocon_exec_line(
299 struct RETROCON* con, char* line, size_t line_sz
300) {
301 MERROR_RETVAL retval = MERROR_OK;
302 size_t i = 0;
303 char line_cap[RETROCON_LBUFFER_SZ_MAX + 1];
304
305 /* Create an uppercase for comparison. */
306 maug_mzero( line_cap, RETROCON_LBUFFER_SZ_MAX + 1 );
307 maug_strncpy( line_cap, line, RETROCON_LBUFFER_SZ_MAX );
308 maug_str_upper( line_cap, line_sz );
309
310 /* Find callback with name starting line. */
311 for( i = 0 ; con->callbacks_sz > i ; i++ ) {
312 if(
313 0 == strncmp(
314 /* TODO: Compare up to first space in line. */
315 con->callback_names[i], line_cap,
316 maug_strlen( con->callback_names[i] ) )
317 ) {
318 retval = con->callbacks[i](
319 con, line, line_sz, con->callback_data[i] );
320 goto cleanup;
321 }
322 }
323
324 retrocon_print_line( con, "COMMAND NOT FOUND!" );
325
326cleanup:
327
328 return retval;
329}
330
331/* === */
332
334 struct RETROCON* con, RETROFLAT_IN_KEY* p_c,
335 struct RETROFLAT_INPUT* input_evt,
336 retrogui_idc_t* p_idc_out, struct MDATA_VECTOR* win_stack
337) {
338 MERROR_RETVAL retval = MERROR_OK;
339 char lbuffer[RETROCON_LBUFFER_SZ_MAX + 1] = { 0 };
340
341 *p_idc_out = RETROGUI_IDC_NONE;
342
343 if( *p_c == RETROCON_ACTIVE_KEY ) {
344 debug_printf( RETROCON_TRACE_LVL, "active key pressed" );
345 if( RETROCON_FLAG_ACTIVE != (RETROCON_FLAG_ACTIVE & (con)->flags) ) {
346 debug_printf( RETROCON_TRACE_LVL, "opening console..." );
347 (con)->flags |= RETROCON_FLAG_ACTIVE;
348 (con)->gui.flags |= RETROGUI_FLAGS_DIRTY;
349
350 if( NULL != win_stack ) {
351 retval = retrowin_push_win(
352 &((con)->gui), win_stack,
353 /* Use the IDC provided in p_idc_out for the window IDC. */
354 *p_idc_out,
355 NULL, (con)->gui.x, (con)->gui.y,
356 (con)->gui.w, (con)->gui.h, RETROWIN_FLAG_BORDER_BLUE );
357 maug_cleanup_if_not_ok();
358 }
359
360 debug_printf( RETROCON_TRACE_LVL, "console open!" );
361 } else {
362 debug_printf( RETROCON_TRACE_LVL, "closing console..." );
363 con->flags &= ~RETROCON_FLAG_ACTIVE;
364
365 if( NULL != win_stack ) {
366 retrowin_destroy_win( win_stack, *p_idc_out );
367 debug_printf( RETROCON_TRACE_LVL, "console closed!" );
368 }
369
370 /* Return, in case the caller needs to do something with this. */
371 *p_idc_out = RETROCON_IDC_CLOSE;
372 }
373 *p_c = 0;
374 goto cleanup;
375 } else if( RETROCON_FLAG_ACTIVE != (RETROCON_FLAG_ACTIVE & (con)->flags) ) {
376 goto cleanup;
377 }
378
379 /* debug_printf( RETROCON_TRACE_LVL, "processing console input..." ); */
380
381 *p_idc_out = retrogui_poll_ctls( &(con->gui), p_c, input_evt );
382
383 *p_c = 0; /* If we got this far then don't pass keystroke back. */
384
385 if( RETROCON_IDC_TEXTBOX == *p_idc_out ) {
386 retval = retrogui_get_ctl_text(
387 &(con->gui), RETROCON_IDC_TEXTBOX, lbuffer, RETROCON_LBUFFER_SZ_MAX );
388 maug_cleanup_if_not_ok();
389 retval = retrogui_set_ctl_text(
390 &(con->gui), RETROCON_IDC_TEXTBOX, 1, "" );
391 maug_cleanup_if_not_ok();
392
393 if( 0 == maug_strlen( lbuffer ) ) {
394 /* Do nothing if line is empty. */
395 goto cleanup;
396 }
397
398 /* Execute/reset line. */
399 retval = retrocon_exec_line( con, lbuffer, maug_strlen( lbuffer ) );
400 }
401
402#if 0
403 /* Debounce retrocon track only! */
404 if( !retrocon_debounce( con, c ) ) {
405 goto cleanup;
406 }
407
408 /* Process input. */
409 switch( c ) {
410 case RETROCON_ACTIVE_KEY:
411 if( RETROCON_FLAG_ACTIVE == (RETROCON_FLAG_ACTIVE & con->flags) ) {
412 con->flags &= ~RETROCON_FLAG_ACTIVE;
413 } else {
414 con->flags |= RETROCON_FLAG_ACTIVE;
415 }
416 break;
417
418 case 0:
419 break;
420
421 case 0x08:
422 /* Backspace. */
423 if( 0 < con->lbuffer_sz ) {
424 con->lbuffer_sz--;
425 con->lbuffer[con->lbuffer_sz] = '\0';
426 }
427 break;
428
429 case '\r':
430 case '\n':
431 if( 0 == con->lbuffer_sz ) {
432 /* Do nothing if line is empty. */
433 break;
434 }
435
436 /* Execute/reset line. */
437 retval = retrocon_exec_line( con, con->lbuffer, con->lbuffer_sz );
438 con->lbuffer_sz = 0;
439 con->lbuffer[con->lbuffer_sz] = '\0';
440 break;
441
442 default:
443 c = retroflat_vk_to_ascii(
444 c, input_evt->key_flags | RETROFLAT_INPUT_FORCE_UPPER );
445 if(
446 /* Active and printable chars get added to line buffer. */
447 RETROCON_FLAG_ACTIVE == (RETROCON_FLAG_ACTIVE & con->flags) &&
448 0 < c
449 ) {
450 con->lbuffer[con->lbuffer_sz++] = c;
451 con->lbuffer[con->lbuffer_sz] = '\0';
452 }
453 break;
454 }
455#endif
456
457cleanup:
458
459 return retval;
460}
461
462/* === */
463
464MERROR_RETVAL retrocon_display(
465 struct RETROCON* con, retroflat_blit_t* gui_bmp
466) {
467 MERROR_RETVAL retval = MERROR_OK;
468
469 if( RETROCON_FLAG_ACTIVE != (RETROCON_FLAG_ACTIVE & (con)->flags) ) {
470 goto cleanup;
471 }
472
473 (con)->gui.draw_bmp = (gui_bmp);
474 (con)->gui.flags |= RETROGUI_FLAGS_DIRTY;
475
476 retrogui_redraw_ctls( &((con)->gui) );
477
478cleanup:
479
480 return retval;
481}
482
483/* === */
484
485void retrocon_shutdown( struct RETROCON* con ) {
486#ifndef RETROGXC_PRESENT
487 retrofont_free( &(con->gui.font.handle) );
488#endif /* !RETROGXC_PRESENT */
489 retrogui_destroy( &(con->gui) );
490}
491
492#endif /* RETROCON_C */
493 /* maug_console */
495
496#endif /* RETROCON_DISABLE */
497
498#endif /* !RETROCON_H */
499
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:328
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...
MERROR_RETVAL retrogui_destroy(struct RETROGUI *gui)
Free memory held by a RETROGUI controller internally and clean up any subordinate controls.
MERROR_RETVAL retrogui_init(struct RETROGUI *gui)
Prepare a RETROGUI controller for use.
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:865
Definition: retrogui.h:468
#define RETROGUI_FLAGS_DIRTY
RETROGUI::flags indicating controls should be redrawn.
Definition: retrogui.h:263
union RETROGXC_CACHABLE font
Font used to draw any attached RETROGUI_CTL.
Definition: retrogui.h:489
retrogui_idc_t focus_idc
Unique identifying index for current highlighted RETROGUI_CTL.
Definition: retrogui.h:478
Definition: retrogui.h:450