fc2775ca49115efeb343b8207f5e70ba2e1ed592
[vg.git] / src / vg / vg_console.h
1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
2
3 #ifndef VG_CONSOLE_H
4 #define VG_CONSOLE_H
5
6 #include "vg/vg_ui.h"
7 #include "vg/vg_log.h"
8
9 struct vg_console
10 {
11 struct vg_convar
12 {
13 void *data;
14 void (*update)(void);
15 const char *name;
16
17 enum vg_convar_dtype
18 {
19 k_convar_dtype_i32,
20 k_convar_dtype_u32,
21 k_convar_dtype_f32
22 }
23 data_type;
24
25 union
26 {
27 struct
28 {
29 int min, max, clamp;
30 }
31 opt_i32;
32
33 struct
34 {
35 float min, max;
36 int clamp;
37 }
38 opt_f32;
39 };
40
41 int persistent; /* Should this var be stored to cfg/auto.conf? */
42 }
43 *convars;
44
45 struct vg_cmd
46 {
47 int (*function)( int argc, char const *argv[] );
48 const char *name;
49 }
50 *functions;
51
52 u32 convar_count, convar_cap,
53 function_count, function_cap;
54
55 char input[96];
56 int cursor_user, cursor_pos, string_length;
57
58 char history[32][96];
59 int history_last, history_pos, history_count;
60
61 int enabled;
62 int scale;
63 }
64 vg_console = { .scale = 1 };
65
66 static void vg_convar_push( struct vg_convar cv );
67 static void vg_function_push( struct vg_cmd cmd );
68
69 static void vg_console_draw( void );
70 void vg_console_println( const char *str );
71 static int vg_console_list( int argc, char const *argv[] );
72 static void vg_console_init(void);
73 static void vg_console_write_persistent(void);
74 static void vg_console_free(void);
75 static void execute_console_input( const char *cmd );
76
77 /*
78 * Console interface
79 */
80 static void console_make_selection( int* start, int* end );
81 static void console_move_cursor( int* cursor0, int* cursor1,
82 int dir, int snap_together );
83 static int console_makeroom( int datastart, int length );
84 static int console_delete_char( int direction );
85 static void console_to_clipboard(void);
86 static void console_clipboard_paste(void);
87 static void console_put_char( char c );
88 static void console_history_get( char* buf, int entry_num );
89 static void console_proc_key( GLFWwindow* ptrW, int key,
90 int scancode, int action, int mods );
91 static void console_proc_wchar( GLFWwindow* ptrW, u32 uWchar );
92 static int vg_console_enabled(void);
93
94 /*
95 * Implementation
96 */
97 static int vg_console_enabled(void)
98 {
99 return vg_console.enabled;
100 }
101
102 static void vg_convar_push( struct vg_convar cv )
103 {
104 vg_info( "Console variable '%s' registered\n", cv.name );
105 vg_console.convars = buffer_reserve( vg_console.convars,
106 vg_console.convar_count,
107 &vg_console.convar_cap, 1,
108 sizeof( struct vg_convar ) );
109
110 vg_console.convars[ vg_console.convar_count ++ ] = cv;
111 }
112
113 static void vg_function_push( struct vg_cmd cmd )
114 {
115 vg_info( "Console command '%s' registered\n", cmd.name );
116 vg_console.functions = buffer_reserve( vg_console.functions,
117 vg_console.function_count,
118 &vg_console.function_cap, 1,
119 sizeof( struct vg_cmd ) );
120
121 vg_console.functions[ vg_console.function_count ++ ] = cmd;
122 }
123
124 static void vg_console_draw( void )
125 {
126 if( !vg_console.enabled )
127 return;
128
129 vg_mutex_lock( &log_print_mutex );
130
131 int ptr = vg_log.buffer_line_current;
132 int const fh = 14;
133 int console_lines = VG_MIN( 16, vg_log.buffer_line_count );
134
135 ui_global_ctx.cursor[0] = 0;
136 ui_global_ctx.cursor[1] = 0;
137 ui_global_ctx.cursor[3] = 16*fh*vg_console.scale;
138 ui_fill_x( &ui_global_ctx );
139
140 ui_new_node( &ui_global_ctx );
141 {
142 ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0x77333333 );
143
144 ui_global_ctx.cursor[3] = fh*vg_console.scale;
145 ui_align_bottom( &ui_global_ctx );
146
147 for( int i=0; i<console_lines; i ++ )
148 {
149 ptr --;
150
151 if( ptr < 0 )
152 ptr = vg_list_size( vg_log.buffer )-1;
153
154 ui_text( &ui_global_ctx, ui_global_ctx.cursor,
155 vg_log.buffer[ptr], vg_console.scale, 0 );
156 ui_global_ctx.cursor[1] -= fh*vg_console.scale;
157 }
158
159 }
160 ui_end_down( &ui_global_ctx );
161
162 ui_global_ctx.cursor[1] += 2;
163 ui_global_ctx.cursor[3] = fh*vg_console.scale;
164
165 ui_new_node( &ui_global_ctx );
166 {
167 ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0x77333333 );
168
169 ui_text( &ui_global_ctx, ui_global_ctx.cursor,
170 vg_console.input, vg_console.scale, 0 );
171
172 int start = VG_MIN( vg_console.cursor_pos, vg_console.cursor_user ),
173 end = VG_MAX( vg_console.cursor_pos, vg_console.cursor_user );
174
175 ui_global_ctx.cursor[0] = (start * UI_GLYPH_SPACING_X * vg_console.scale);
176 ui_global_ctx.cursor[2] = (start == end? 0.5f: (float)(end-start))
177 * (float)UI_GLYPH_SPACING_X * (float)vg_console.scale;
178
179 ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0x66ffffff );
180 }
181 ui_end_down( &ui_global_ctx );
182 vg_mutex_unlock( &log_print_mutex );
183 }
184
185 static int vg_console_list( int argc, char const *argv[] )
186 {
187 for( int i=0; i<vg_console.function_count; i ++ )
188 {
189 struct vg_cmd *cmd = &vg_console.functions[ i ];
190 vg_info( "* %s\n", cmd->name );
191 }
192
193 vg_info( "* snowsound\n" );
194
195 for( int i=0; i<vg_console.convar_count; i ++ )
196 {
197 struct vg_convar *cv = &vg_console.convars[ i ];
198 vg_info( "%s\n", cv->name );
199 }
200
201 return 0;
202 }
203
204 static int vg_console_chartest( int argc, char const *argv[] )
205 {
206 vg_info(" Copyright . . . -----, ,----- ,---. .---. " );
207 vg_info(" 2021-2022 |\\ /| | / | | | | /| " );
208 vg_info(" | \\ / | +-- / +----- +---' | / | " );
209 vg_info(" | \\ / | | / | | \\ | / | " );
210 vg_info(" | \\/ | | / | | \\ | / | " );
211 vg_info(" ' ' '--' [] '----- '----- ' ' '---' "
212 "SOFTWARE" );
213
214 vg_info( "\"THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG\"\n" );
215 vg_info( "'the quick brown fox jumps over the lazy dog'\n" );
216 vg_info( ":;!@#$%^& 0123456789 +-*/=~ ()[]{}<>\n" );
217 return 0;
218 }
219
220 static int _test_break( int argc, const char *argv[] )
221 {
222 vg_fatal_exit_loop( "Test crash from main, after loading (console)" );
223 return 0;
224 }
225
226 static void vg_console_init(void)
227 {
228 vg_convar_push( (struct vg_convar)
229 {
230 .name = "console_scale", .data = &vg_console.scale,
231 .data_type = k_convar_dtype_i32,
232 .opt_i32 = { .clamp = 1, .min = 1, .max = 7 },
233 .update = NULL
234 });
235
236 vg_function_push( (struct vg_cmd)
237 {
238 .name = "list",
239 .function = vg_console_list
240 });
241
242 vg_function_push( (struct vg_cmd)
243 {
244 .name = "chartest",
245 .function = vg_console_chartest
246 });
247
248 vg_function_push( (struct vg_cmd)
249 {
250 .name = "crash",
251 .function = _test_break
252 });
253 }
254
255 static void vg_console_load_autos(void)
256 {
257 /* Read and exec persistent commands */
258 FILE *fp = fopen( "cfg/auto.conf", "r" );
259 if( fp )
260 {
261 char line[256];
262
263 while( fgets( line, sizeof( line ), fp ) )
264 {
265 line[ strcspn( line, "\r\n#" ) ] = 0x00;
266
267 if( line[0] != 0x00 )
268 {
269 execute_console_input( line );
270 }
271 }
272
273 fclose( fp );
274 }
275 }
276
277 static void vg_console_write_persistent(void)
278 {
279 FILE *fp = fopen( "cfg/auto.conf", "w" );
280
281 for( int i=0; i<vg_console.convar_count; i ++ )
282 {
283 struct vg_convar *cv = &vg_console.convars[i];
284
285 if( cv->persistent )
286 {
287 switch( cv->data_type )
288 {
289 case k_convar_dtype_i32:
290 fprintf( fp, "%s %d\n", cv->name, *(i32 *)(cv->data) );
291 break;
292 case k_convar_dtype_u32:
293 fprintf( fp, "%s %u\n", cv->name, *(u32 *)(cv->data) );
294 break;
295 case k_convar_dtype_f32:
296 fprintf( fp, "%s %.5f\n", cv->name, *(float *)(cv->data ) );
297 break;
298 }
299 }
300 }
301
302 fclose( fp );
303 }
304
305 static void vg_console_free(void)
306 {
307 vg_console_write_persistent();
308
309 vg_free( vg_console.convars );
310 vg_free( vg_console.functions );
311 }
312
313 static void execute_console_input( const char *cmd )
314 {
315 char temp[512];
316 char const *args[9];
317 int arg_count = 0;
318
319 int in_token = 0;
320
321 /* Split string into tokens */
322 for( int i = 0; i < vg_list_size( temp ); i ++ )
323 {
324 if( cmd[i] )
325 {
326 if( cmd[i] == ' ' || cmd[i] == '\t' )
327 {
328 temp[i] = '\0';
329 in_token = 0;
330
331 if( arg_count == vg_list_size( args ) )
332 break;
333 }
334 else
335 {
336 temp[i] = cmd[i];
337
338 if( !in_token )
339 {
340 args[ arg_count ++ ] = temp + i;
341 in_token = 1;
342 }
343 }
344 }
345 else
346 {
347 temp[i] = '\0';
348 break;
349 }
350 }
351
352 if( arg_count == 0 )
353 return;
354
355 int data_int;
356 float data_float;
357
358 for( int i=0; i<vg_console.convar_count; i ++ )
359 {
360 struct vg_convar *cv = &vg_console.convars[ i ];
361 if( !strcmp( cv->name, args[0] ) )
362 {
363 /* Cvar Matched, try get value */
364 if( arg_count >= 2 )
365 {
366 switch( cv->data_type )
367 {
368 case k_convar_dtype_u32:
369 case k_convar_dtype_i32:
370
371 data_int = atoi( args[1] );
372
373 *((int *)cv->data) = cv->opt_i32.clamp?
374 VG_MIN( VG_MAX(data_int, cv->opt_i32.min), cv->opt_i32.max ):
375 data_int;
376
377 break;
378 case k_convar_dtype_f32:
379 data_float = atof( args[1] );
380 *((float *)cv->data) = cv->opt_f32.clamp?
381 vg_minf( vg_maxf( data_float, cv->opt_f32.min), cv->opt_f32.max ):
382 data_float;
383 break;
384 }
385
386 if( cv->update )
387 cv->update();
388 }
389 else
390 {
391 switch( cv->data_type )
392 {
393 case k_convar_dtype_i32:
394 vg_info( "= %d\n", *((int *)cv->data) );
395 break;
396 case k_convar_dtype_u32:
397 vg_info( "= %u\n", *((u32 *)cv->data) );
398 break;
399 case k_convar_dtype_f32:
400 vg_info( "= %.4f\n", *((float *)cv->data) );
401 break;
402 }
403 }
404
405 return;
406 }
407 }
408
409 /*
410 * Find and excecute command
411 */
412 for( int i=0; i<vg_console.function_count; i ++ )
413 {
414 struct vg_cmd *cmd = &vg_console.functions[ i ];
415 if( !strcmp( cmd->name, args[0] ) )
416 {
417 cmd->function( arg_count-1, args+1 );
418 return;
419 }
420 }
421
422 vg_error( "No command/var named '%s'. Use 'list' to view all\n", args[0] );
423 }
424
425 /*
426 * Console Interface
427 */
428 static void console_make_selection( int* start, int* end )
429 {
430 *start = VG_MIN( vg_console.cursor_pos, vg_console.cursor_user );
431 *end = VG_MAX( vg_console.cursor_pos, vg_console.cursor_user );
432 }
433
434 static void console_move_cursor( int* cursor0, int* cursor1,
435 int dir, int snap_together )
436 {
437 *cursor0 = VG_MAX( 0, vg_console.cursor_user + dir );
438 *cursor0 =
439 VG_MIN(
440 VG_MIN( vg_list_size( vg_console.input ), strlen( vg_console.input )),
441 *cursor0 );
442
443 if( snap_together )
444 *cursor1 = *cursor0;
445 }
446
447 static int console_makeroom( int datastart, int length )
448 {
449 int move_to = VG_MIN( datastart+length, vg_list_size( vg_console.input ) );
450 int move_amount = strlen( vg_console.input )-datastart;
451 int move_end =
452 VG_MIN( move_to+move_amount, vg_list_size( vg_console.input ) );
453 move_amount = move_end-move_to;
454
455 if( move_amount )
456 memmove( &vg_console.input[ move_to ],
457 &vg_console.input[ datastart ],
458 move_end-move_to );
459
460 vg_console.input[ move_end ] = '\0';
461
462 return VG_MIN( length, vg_list_size( vg_console.input )-datastart );
463 }
464
465 static int console_delete_char( int direction )
466 {
467 int start, end;
468 console_make_selection( &start, &end );
469
470 /* There is no selection */
471 if( !(end-start) )
472 {
473 if( direction == 1 ) end = VG_MIN( end+1, strlen( vg_console.input ) );
474 else if( direction == -1 ) start = VG_MAX( start-1, 0 );
475 }
476
477 /* Still no selction, no need to do anything */
478 if( !(end-start) )
479 return start;
480
481 /* Copy the end->terminator to start */
482 int remaining_length = strlen( vg_console.input )+1-end;
483 memmove( &vg_console.input[ start ],
484 &vg_console.input[ end ],
485 remaining_length );
486 return start;
487 }
488
489 static void console_to_clipboard(void)
490 {
491 int start, end;
492 console_make_selection( &start, &end );
493 char buffer[512];
494
495 if( end-start )
496 {
497 memcpy( buffer, &vg_console.input[ start ], end-start );
498 buffer[ end-start ] = 0x00;
499 glfwSetClipboardString( NULL, buffer );
500 }
501 }
502
503 static void console_clipboard_paste(void)
504 {
505 int datastart = console_delete_char(0);
506 const char* clipboard = glfwGetClipboardString(NULL);
507 int length = strlen(clipboard);
508
509 int cpylength = console_makeroom(datastart, length);
510
511 memcpy( vg_console.input + datastart, clipboard, cpylength);
512 console_move_cursor( &vg_console.cursor_user,
513 &vg_console.cursor_pos, cpylength, 1 );
514 }
515
516 static void console_put_char( char c )
517 {
518 if( !vg_console.enabled )
519 return;
520
521 vg_console.cursor_user = console_delete_char(0);
522
523 if( console_makeroom( vg_console.cursor_user, 1 ) )
524 vg_console.input[ vg_console.cursor_user ] = c;
525
526 console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, 1, 1 );
527 }
528
529 static void console_history_get( char* buf, int entry_num )
530 {
531 if( !vg_console.history_count )
532 return;
533
534 int offset = VG_MIN( entry_num, vg_console.history_count -1 ),
535 pick = (vg_console.history_last - offset) %
536 vg_list_size( vg_console.history );
537 strcpy( buf, vg_console.history[ pick ] );
538 }
539
540 /* Receed secondary cursor */
541 static void _console_left_select(void)
542 {
543 console_move_cursor( &vg_console.cursor_user, NULL, -1, 0 );
544 }
545
546 /* Match and receed both cursors */
547 static void _console_left(void)
548 {
549 int cursor_diff = vg_console.cursor_pos - vg_console.cursor_user? 0: 1;
550
551 console_move_cursor( &vg_console.cursor_user,
552 &vg_console.cursor_pos, -cursor_diff, 1 );
553 }
554
555 static void _console_right_select(void)
556 {
557 console_move_cursor( &vg_console.cursor_user, NULL, 1, 0 );
558 }
559
560 static void _console_right(void)
561 {
562 int cursor_diff = vg_console.cursor_pos - vg_console.cursor_user? 0: 1;
563
564 console_move_cursor( &vg_console.cursor_user,
565 &vg_console.cursor_pos, +cursor_diff, 1 );
566 }
567
568 static void _console_down(void)
569 {
570 vg_console.history_pos = VG_MAX( 0, vg_console.history_pos-1 );
571 console_history_get( vg_console.input, vg_console.history_pos );
572
573 console_move_cursor( &vg_console.cursor_user,
574 &vg_console.cursor_pos,
575 vg_list_size( vg_console.input ), 1 );
576 }
577
578 static void _console_up(void)
579 {
580 vg_console.history_pos = VG_MAX
581 (
582 0,
583 VG_MIN
584 (
585 vg_console.history_pos+1,
586 VG_MIN
587 (
588 vg_list_size( vg_console.history ),
589 vg_console.history_count - 1
590 )
591 )
592 );
593
594 console_history_get( vg_console.input, vg_console.history_pos );
595 console_move_cursor( &vg_console.cursor_user,
596 &vg_console.cursor_pos,
597 vg_list_size( vg_console.input ), 1);
598 }
599
600 static void _console_backspace(void)
601 {
602 vg_console.cursor_user = console_delete_char( -1 );
603 vg_console.cursor_pos = vg_console.cursor_user;
604 }
605
606 static void _console_delete(void)
607 {
608 vg_console.cursor_user = console_delete_char( 1 );
609 vg_console.cursor_pos = vg_console.cursor_user;
610 }
611
612 static void _console_home_select(void)
613 {
614 console_move_cursor( &vg_console.cursor_user, NULL, -10000, 0 );
615 }
616
617 static void _console_home(void)
618 {
619 console_move_cursor( &vg_console.cursor_user,
620 &vg_console.cursor_pos, -10000, 1 );
621 }
622
623 static void _console_end_select(void)
624 {
625 console_move_cursor( &vg_console.cursor_user, NULL, 10000, 0 );
626 }
627
628 static void _console_end(void)
629 {
630 console_move_cursor( &vg_console.cursor_user,
631 &vg_console.cursor_pos,
632 vg_list_size( vg_console.input ), 1 );
633 }
634
635 static void _console_select_all(void)
636 {
637 console_move_cursor( &vg_console.cursor_user, NULL, 10000, 0);
638 console_move_cursor( &vg_console.cursor_pos, NULL, -10000, 0);
639 }
640
641 static void _console_cut(void)
642 {
643 console_to_clipboard();
644 vg_console.cursor_user = console_delete_char(0);
645 vg_console.cursor_pos = vg_console.cursor_user;
646 }
647
648 static void _console_enter(void)
649 {
650 if( !strlen( vg_console.input ) )
651 return;
652
653 vg_info( "%s\n", vg_console.input );
654
655 if( strcmp( vg_console.input,
656 vg_console.history[ vg_console.history_last ]) )
657 {
658 vg_console.history_last = ( vg_console.history_last + 1) %
659 vg_list_size(vg_console.history );
660 vg_console.history_count =
661 VG_MIN( vg_list_size( vg_console.history ),
662 vg_console.history_count + 1 );
663 strcpy( vg_console.history[ vg_console.history_last ],
664 vg_console.input );
665 }
666
667 vg_console.history_pos = -1;
668 execute_console_input( vg_console.input );
669 console_move_cursor( &vg_console.cursor_user,
670 &vg_console.cursor_pos, -10000, 1 );
671 vg_console.input[0] = '\0';
672 }
673
674 static void console_proc_key( GLFWwindow* ptrW, int key, int scancode,
675 int action, int mods )
676 {
677 if( !action )
678 return;
679
680 /* Open / close console */
681 if( key == GLFW_KEY_GRAVE_ACCENT )
682 vg_console.enabled = !vg_console.enabled;
683
684 if( !vg_console.enabled ) return;
685
686 struct console_mapping
687 {
688 u32 mod, key;
689 void (*handler)(void);
690 }
691 mapping[] =
692 {
693 { 0, GLFW_KEY_LEFT, _console_left },
694 { GLFW_MOD_SHIFT, GLFW_KEY_LEFT, _console_left_select },
695 { 0, GLFW_KEY_RIGHT, _console_right },
696 { GLFW_MOD_SHIFT, GLFW_KEY_RIGHT, _console_right_select },
697 { 0, GLFW_KEY_DOWN, _console_down },
698 { 0, GLFW_KEY_UP, _console_up },
699 { 0, GLFW_KEY_BACKSPACE, _console_backspace },
700 { 0, GLFW_KEY_DELETE, _console_delete },
701 { 0, GLFW_KEY_HOME, _console_home },
702 { GLFW_MOD_SHIFT, GLFW_KEY_HOME, _console_home_select },
703 { 0, GLFW_KEY_END, _console_end },
704 { GLFW_MOD_SHIFT, GLFW_KEY_END, _console_end_select },
705 { GLFW_MOD_CONTROL, GLFW_KEY_A, _console_select_all },
706 { GLFW_MOD_CONTROL, GLFW_KEY_C, console_to_clipboard },
707 { GLFW_MOD_CONTROL, GLFW_KEY_X, _console_cut },
708 { GLFW_MOD_CONTROL, GLFW_KEY_V, console_clipboard_paste },
709 { 0, GLFW_KEY_ENTER, _console_enter }
710 };
711
712 for( int i=0; i<vg_list_size( mapping ); i++ )
713 {
714 struct console_mapping *mk = &mapping[i];
715
716 if( mk->key == key )
717 {
718 if( mk->mod == 0 )
719 {
720 if( mods == 0 )
721 {
722 mk->handler();
723 return;
724 }
725 }
726 else if( (mods & mk->mod) == mk->mod )
727 {
728 mk->handler();
729 return;
730 }
731 }
732 }
733 }
734
735 /* Handle an OS based input of UTF32 character from the keyboard or such */
736 static void console_proc_wchar( GLFWwindow* ptrW, u32 uWchar )
737 {
738 if( uWchar <= 0x7F && (char)uWchar != 0x60)
739 {
740 console_put_char((char)uWchar);
741 }
742 }
743
744 #endif /* VG_CONSOLE_H */