1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
9 typedef struct vg_convar vg_convar
;
10 typedef struct vg_cmd vg_cmd
;
44 int persistent
; /* Should this var be stored to cfg/auto.conf? */
50 int (*function
)( int argc
, char const *argv
[] );
55 u32 convar_count
, function_count
;
58 int cursor_user
, cursor_pos
, string_length
;
61 int history_last
, history_pos
, history_count
;
67 VG_STATIC
void vg_convar_push( struct vg_convar cv
);
68 VG_STATIC
void vg_function_push( struct vg_cmd cmd
);
70 VG_STATIC
void vg_console_draw( void );
71 void vg_console_println( const char *str
);
72 VG_STATIC
int vg_console_list( int argc
, char const *argv
[] );
73 VG_STATIC
void vg_console_init(void);
74 VG_STATIC
void vg_console_write_persistent(void);
75 VG_STATIC
void vg_console_free(void);
76 VG_STATIC
void execute_console_input( const char *cmd
);
81 VG_STATIC
void console_make_selection( int* start
, int* end
);
82 VG_STATIC
void console_move_cursor( int* cursor0
, int* cursor1
,
83 int dir
, int snap_together
);
84 VG_STATIC
int console_makeroom( int datastart
, int length
);
85 VG_STATIC
int console_delete_char( int direction
);
86 VG_STATIC
void console_to_clipboard(void);
87 VG_STATIC
void console_clipboard_paste(void);
88 VG_STATIC
void console_put_char( char c
);
89 VG_STATIC
void console_history_get( char* buf
, int entry_num
);
90 VG_STATIC
void console_proc_key( GLFWwindow
* ptrW
, int key
,
91 int scancode
, int action
, int mods
);
92 VG_STATIC
void console_proc_wchar( GLFWwindow
* ptrW
, u32 uWchar
);
93 VG_STATIC
int vg_console_enabled(void);
98 VG_STATIC
int vg_console_enabled(void)
100 return vg_console
.enabled
;
103 VG_STATIC
void vg_convar_push( vg_convar cv
)
105 if( vg_console
.convar_count
> vg_list_size(vg_console
.convars
) )
106 vg_fatal_exit_loop( "Too many convars registered" );
108 vg_info( "Console variable '%s' registered\n", cv
.name
);
109 vg_console
.convars
[ vg_console
.convar_count
++ ] = cv
;
112 VG_STATIC
void vg_function_push( struct vg_cmd cmd
)
114 if( vg_console
.function_count
> vg_list_size(vg_console
.functions
) )
115 vg_fatal_exit_loop( "Too many functions registered" );
117 vg_console
.functions
[ vg_console
.function_count
++ ] = cmd
;
120 VG_STATIC
void vg_console_draw( void )
122 if( !vg_console
.enabled
)
125 vg_mutex_lock( &log_print_mutex
);
127 int ptr
= vg_log
.buffer_line_current
;
129 int console_lines
= VG_MIN( 16, vg_log
.buffer_line_count
);
131 vg_uictx
.cursor
[0] = 0;
132 vg_uictx
.cursor
[1] = 0;
133 vg_uictx
.cursor
[3] = 16*fh
;
138 ui_fill_rect( vg_uictx
.cursor
, 0x77333333 );
140 vg_uictx
.cursor
[3] = fh
;
143 for( int i
=0; i
<console_lines
; i
++ )
148 ptr
= vg_list_size( vg_log
.buffer
)-1;
150 ui_text( vg_uictx
.cursor
, vg_log
.buffer
[ptr
], 1, 0 );
151 vg_uictx
.cursor
[1] -= fh
;
157 vg_uictx
.cursor
[1] += 2;
158 vg_uictx
.cursor
[3] = fh
;
162 ui_fill_rect( vg_uictx
.cursor
, 0x77333333 );
163 ui_text( vg_uictx
.cursor
, vg_console
.input
, 1, 0 );
165 int start
= VG_MIN( vg_console
.cursor_pos
, vg_console
.cursor_user
),
166 end
= VG_MAX( vg_console
.cursor_pos
, vg_console
.cursor_user
);
168 vg_uictx
.cursor
[0] = start
* UI_GLYPH_SPACING_X
;
169 vg_uictx
.cursor
[2] = (start
== end
? 0.5f
: (float)(end
-start
))
170 * (float)UI_GLYPH_SPACING_X
;
172 ui_fill_rect( vg_uictx
.cursor
, 0x66ffffff );
175 vg_mutex_unlock( &log_print_mutex
);
178 VG_STATIC
int vg_console_list( int argc
, char const *argv
[] )
180 for( int i
=0; i
<vg_console
.function_count
; i
++ )
182 struct vg_cmd
*cmd
= &vg_console
.functions
[ i
];
183 vg_info( "* %s\n", cmd
->name
);
186 for( int i
=0; i
<vg_console
.convar_count
; i
++ )
188 struct vg_convar
*cv
= &vg_console
.convars
[ i
];
189 vg_info( "%s\n", cv
->name
);
195 int _test_break( int argc
, const char *argv
[] )
197 vg_fatal_exit_loop( "Test crash from main, after loading (console)" );
201 VG_STATIC
void vg_console_init(void)
203 vg_function_push( (struct vg_cmd
)
206 .function
= vg_console_list
209 vg_function_push( (struct vg_cmd
)
212 .function
= _test_break
216 VG_STATIC
void vg_console_load_autos(void)
218 /* Read and exec persistent commands */
219 FILE *fp
= fopen( "cfg/auto.conf", "r" );
224 while( fgets( line
, sizeof( line
), fp
) )
226 line
[ strcspn( line
, "\r\n#" ) ] = 0x00;
228 if( line
[0] != 0x00 )
230 execute_console_input( line
);
238 VG_STATIC
void vg_console_write_persistent(void)
240 FILE *fp
= fopen( "cfg/auto.conf", "w" );
242 for( int i
=0; i
<vg_console
.convar_count
; i
++ )
244 struct vg_convar
*cv
= &vg_console
.convars
[i
];
248 switch( cv
->data_type
)
250 case k_convar_dtype_i32
:
251 fprintf( fp
, "%s %d\n", cv
->name
, *(i32
*)(cv
->data
) );
253 case k_convar_dtype_u32
:
254 fprintf( fp
, "%s %u\n", cv
->name
, *(u32
*)(cv
->data
) );
256 case k_convar_dtype_f32
:
257 fprintf( fp
, "%s %.5f\n", cv
->name
, *(float *)(cv
->data
) );
266 VG_STATIC
void vg_console_free(void)
268 vg_console_write_persistent();
271 VG_STATIC
void execute_console_input( const char *cmd
)
279 /* Split string into tokens */
280 for( int i
= 0; i
< vg_list_size( temp
); i
++ )
284 if( cmd
[i
] == ' ' || cmd
[i
] == '\t' )
289 if( arg_count
== vg_list_size( args
) )
298 args
[ arg_count
++ ] = temp
+ i
;
316 for( int i
=0; i
<vg_console
.convar_count
; i
++ )
318 struct vg_convar
*cv
= &vg_console
.convars
[ i
];
319 if( !strcmp( cv
->name
, args
[0] ) )
321 /* Cvar Matched, try get value */
324 switch( cv
->data_type
)
326 case k_convar_dtype_u32
:
327 case k_convar_dtype_i32
:
329 data_int
= atoi( args
[1] );
331 *((int *)cv
->data
) = cv
->opt_i32
.clamp
?
332 VG_MIN( VG_MAX(data_int
, cv
->opt_i32
.min
), cv
->opt_i32
.max
):
336 case k_convar_dtype_f32
:
337 data_float
= atof( args
[1] );
338 *((float *)cv
->data
) = cv
->opt_f32
.clamp
?
339 vg_minf( vg_maxf( data_float
, cv
->opt_f32
.min
),
350 switch( cv
->data_type
)
352 case k_convar_dtype_i32
:
353 vg_info( "= %d\n", *((int *)cv
->data
) );
355 case k_convar_dtype_u32
:
356 vg_info( "= %u\n", *((u32
*)cv
->data
) );
358 case k_convar_dtype_f32
:
359 vg_info( "= %.4f\n", *((float *)cv
->data
) );
369 * Find and excecute command
371 for( int i
=0; i
<vg_console
.function_count
; i
++ )
373 struct vg_cmd
*cmd
= &vg_console
.functions
[ i
];
374 if( !strcmp( cmd
->name
, args
[0] ) )
376 cmd
->function( arg_count
-1, args
+1 );
381 vg_error( "No command/var named '%s'. Use 'list' to view all\n", args
[0] );
387 VG_STATIC
void console_make_selection( int* start
, int* end
)
389 *start
= VG_MIN( vg_console
.cursor_pos
, vg_console
.cursor_user
);
390 *end
= VG_MAX( vg_console
.cursor_pos
, vg_console
.cursor_user
);
393 VG_STATIC
void console_move_cursor( int* cursor0
, int* cursor1
,
394 int dir
, int snap_together
)
396 *cursor0
= VG_MAX( 0, vg_console
.cursor_user
+ dir
);
399 VG_MIN( vg_list_size( vg_console
.input
), strlen( vg_console
.input
)),
406 VG_STATIC
int console_makeroom( int datastart
, int length
)
408 int move_to
= VG_MIN( datastart
+length
, vg_list_size( vg_console
.input
) );
409 int move_amount
= strlen( vg_console
.input
)-datastart
;
411 VG_MIN( move_to
+move_amount
, vg_list_size( vg_console
.input
) );
412 move_amount
= move_end
-move_to
;
415 memmove( &vg_console
.input
[ move_to
],
416 &vg_console
.input
[ datastart
],
419 vg_console
.input
[ move_end
] = '\0';
421 return VG_MIN( length
, vg_list_size( vg_console
.input
)-datastart
);
424 VG_STATIC
int console_delete_char( int direction
)
427 console_make_selection( &start
, &end
);
429 /* There is no selection */
432 if( direction
== 1 ) end
= VG_MIN( end
+1, strlen( vg_console
.input
) );
433 else if( direction
== -1 ) start
= VG_MAX( start
-1, 0 );
436 /* Still no selction, no need to do anything */
440 /* Copy the end->terminator to start */
441 int remaining_length
= strlen( vg_console
.input
)+1-end
;
442 memmove( &vg_console
.input
[ start
],
443 &vg_console
.input
[ end
],
448 VG_STATIC
void console_to_clipboard(void)
451 console_make_selection( &start
, &end
);
456 memcpy( buffer
, &vg_console
.input
[ start
], end
-start
);
457 buffer
[ end
-start
] = 0x00;
458 glfwSetClipboardString( NULL
, buffer
);
462 VG_STATIC
void console_clipboard_paste(void)
464 int datastart
= console_delete_char(0);
465 const char* clipboard
= glfwGetClipboardString(NULL
);
466 int length
= strlen(clipboard
);
468 int cpylength
= console_makeroom(datastart
, length
);
470 memcpy( vg_console
.input
+ datastart
, clipboard
, cpylength
);
471 console_move_cursor( &vg_console
.cursor_user
,
472 &vg_console
.cursor_pos
, cpylength
, 1 );
475 VG_STATIC
void console_put_char( char c
)
477 if( !vg_console
.enabled
)
480 vg_console
.cursor_user
= console_delete_char(0);
482 if( console_makeroom( vg_console
.cursor_user
, 1 ) )
483 vg_console
.input
[ vg_console
.cursor_user
] = c
;
485 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, 1, 1 );
488 VG_STATIC
void console_history_get( char* buf
, int entry_num
)
490 if( !vg_console
.history_count
)
493 int offset
= VG_MIN( entry_num
, vg_console
.history_count
-1 ),
494 pick
= (vg_console
.history_last
- offset
) %
495 vg_list_size( vg_console
.history
);
496 strcpy( buf
, vg_console
.history
[ pick
] );
499 /* Receed secondary cursor */
500 VG_STATIC
void _console_left_select(void)
502 console_move_cursor( &vg_console
.cursor_user
, NULL
, -1, 0 );
505 /* Match and receed both cursors */
506 VG_STATIC
void _console_left(void)
508 int cursor_diff
= vg_console
.cursor_pos
- vg_console
.cursor_user
? 0: 1;
510 console_move_cursor( &vg_console
.cursor_user
,
511 &vg_console
.cursor_pos
, -cursor_diff
, 1 );
514 VG_STATIC
void _console_right_select(void)
516 console_move_cursor( &vg_console
.cursor_user
, NULL
, 1, 0 );
519 VG_STATIC
void _console_right(void)
521 int cursor_diff
= vg_console
.cursor_pos
- vg_console
.cursor_user
? 0: 1;
523 console_move_cursor( &vg_console
.cursor_user
,
524 &vg_console
.cursor_pos
, +cursor_diff
, 1 );
527 VG_STATIC
void _console_down(void)
529 vg_console
.history_pos
= VG_MAX( 0, vg_console
.history_pos
-1 );
530 console_history_get( vg_console
.input
, vg_console
.history_pos
);
532 console_move_cursor( &vg_console
.cursor_user
,
533 &vg_console
.cursor_pos
,
534 vg_list_size( vg_console
.input
), 1 );
537 VG_STATIC
void _console_up(void)
539 vg_console
.history_pos
= VG_MAX
544 vg_console
.history_pos
+1,
547 vg_list_size( vg_console
.history
),
548 vg_console
.history_count
- 1
553 console_history_get( vg_console
.input
, vg_console
.history_pos
);
554 console_move_cursor( &vg_console
.cursor_user
,
555 &vg_console
.cursor_pos
,
556 vg_list_size( vg_console
.input
), 1);
559 VG_STATIC
void _console_backspace(void)
561 vg_console
.cursor_user
= console_delete_char( -1 );
562 vg_console
.cursor_pos
= vg_console
.cursor_user
;
565 VG_STATIC
void _console_delete(void)
567 vg_console
.cursor_user
= console_delete_char( 1 );
568 vg_console
.cursor_pos
= vg_console
.cursor_user
;
571 VG_STATIC
void _console_home_select(void)
573 console_move_cursor( &vg_console
.cursor_user
, NULL
, -10000, 0 );
576 VG_STATIC
void _console_home(void)
578 console_move_cursor( &vg_console
.cursor_user
,
579 &vg_console
.cursor_pos
, -10000, 1 );
582 VG_STATIC
void _console_end_select(void)
584 console_move_cursor( &vg_console
.cursor_user
, NULL
, 10000, 0 );
587 VG_STATIC
void _console_end(void)
589 console_move_cursor( &vg_console
.cursor_user
,
590 &vg_console
.cursor_pos
,
591 vg_list_size( vg_console
.input
), 1 );
594 VG_STATIC
void _console_select_all(void)
596 console_move_cursor( &vg_console
.cursor_user
, NULL
, 10000, 0);
597 console_move_cursor( &vg_console
.cursor_pos
, NULL
, -10000, 0);
600 VG_STATIC
void _console_cut(void)
602 console_to_clipboard();
603 vg_console
.cursor_user
= console_delete_char(0);
604 vg_console
.cursor_pos
= vg_console
.cursor_user
;
607 VG_STATIC
void _console_enter(void)
609 if( !strlen( vg_console
.input
) )
612 vg_info( "%s\n", vg_console
.input
);
614 if( strcmp( vg_console
.input
,
615 vg_console
.history
[ vg_console
.history_last
]) )
617 vg_console
.history_last
= ( vg_console
.history_last
+ 1) %
618 vg_list_size(vg_console
.history
);
619 vg_console
.history_count
=
620 VG_MIN( vg_list_size( vg_console
.history
),
621 vg_console
.history_count
+ 1 );
622 strcpy( vg_console
.history
[ vg_console
.history_last
],
626 vg_console
.history_pos
= -1;
627 execute_console_input( vg_console
.input
);
628 console_move_cursor( &vg_console
.cursor_user
,
629 &vg_console
.cursor_pos
, -10000, 1 );
630 vg_console
.input
[0] = '\0';
633 VG_STATIC
void console_proc_key( GLFWwindow
* ptrW
, int key
, int scancode
,
634 int action
, int mods
)
639 /* Open / close console */
640 if( key
== GLFW_KEY_GRAVE_ACCENT
)
641 vg_console
.enabled
= !vg_console
.enabled
;
643 if( !vg_console
.enabled
) return;
645 struct console_mapping
648 void (*handler
)(void);
652 { 0, GLFW_KEY_LEFT
, _console_left
},
653 { GLFW_MOD_SHIFT
, GLFW_KEY_LEFT
, _console_left_select
},
654 { 0, GLFW_KEY_RIGHT
, _console_right
},
655 { GLFW_MOD_SHIFT
, GLFW_KEY_RIGHT
, _console_right_select
},
656 { 0, GLFW_KEY_DOWN
, _console_down
},
657 { 0, GLFW_KEY_UP
, _console_up
},
658 { 0, GLFW_KEY_BACKSPACE
, _console_backspace
},
659 { 0, GLFW_KEY_DELETE
, _console_delete
},
660 { 0, GLFW_KEY_HOME
, _console_home
},
661 { GLFW_MOD_SHIFT
, GLFW_KEY_HOME
, _console_home_select
},
662 { 0, GLFW_KEY_END
, _console_end
},
663 { GLFW_MOD_SHIFT
, GLFW_KEY_END
, _console_end_select
},
664 { GLFW_MOD_CONTROL
, GLFW_KEY_A
, _console_select_all
},
665 { GLFW_MOD_CONTROL
, GLFW_KEY_C
, console_to_clipboard
},
666 { GLFW_MOD_CONTROL
, GLFW_KEY_X
, _console_cut
},
667 { GLFW_MOD_CONTROL
, GLFW_KEY_V
, console_clipboard_paste
},
668 { 0, GLFW_KEY_ENTER
, _console_enter
}
671 for( int i
=0; i
<vg_list_size( mapping
); i
++ )
673 struct console_mapping
*mk
= &mapping
[i
];
685 else if( (mods
& mk
->mod
) == mk
->mod
)
694 /* Handle an OS based input of UTF32 character from the keyboard or such */
695 VG_STATIC
void console_proc_wchar( GLFWwindow
* ptrW
, u32 uWchar
)
697 if( uWchar
<= 0x7F && (char)uWchar
!= 0x60)
699 console_put_char((char)uWchar
);
703 #endif /* VG_CONSOLE_H */