From: hgn Date: Wed, 1 Oct 2025 23:27:30 +0000 (+0000) Subject: sqek X-Git-Url: https://skaterift.com/git/?a=commitdiff_plain;h=b6234d67506ed28a4d1f0157b2ec16c5111b18b6;p=vg.git sqek --- diff --git a/foundation.kv b/foundation.kv index 763c2a7..b67b322 100644 --- a/foundation.kv +++ b/foundation.kv @@ -36,120 +36,149 @@ add source/foundation/async.c add source/engine/main.c add source/engine/ui.c add source/engine/shader.c - add source/engine/input.c add source/engine/console.c add source/console_core.c - append graphics.kv - append glfw3.kv - input_layer { name console } - input { name console type action layer_mask console - bind GRAVE_ACCENT } + append graphics.kv + append glfw3.kv + + add source/engine/input.c + ccmd + { + name bind + function _input_bind_ccmd + description "Bind device input to a button, action or axis" + + parameter + { + description "Device input alias" + } + + parameter + { + description "button,action,axis name" + } + } + + ccmd + { + name exec + function _console_exec_ccmd + description "Execute a configuration file" + + parameter + { + description "The path to the config" + } + } + + config "bind ALT+GRAVE_ACCENT console" + config "bind BACKSPACE ui_backspace" + config "bind DELETE ui_delete" + config "bind ENTER ui_enter" + config "bind TAB ui_indent" + config "bind HOME ui_home" + config "bind SHIFT+HOME ui_home_select" + config "bind END ui_end" + config "bind SHIFT+END ui_end_select" + config "bind LEFT ui_left" + config "bind SHIFT+LEFT ui_left_select" + config "bind RIGHT ui_right" + config "bind SHIFT+RIGHT ui_right_select" + config "bind UP ui_up" + config "bind SHIFT+UP ui_up_select" + config "bind DOWN ui_down" + config "bind SHIFT+DOWN ui_down_select" + input { - name ui_backspace - bind BACKSPACE + name ui_delete type action } input { - name ui_delete - bind DELETE + name ui_backspace type action } input { name ui_enter - bind ENTER type action } input { name ui_indent - bind TAB type action } input { name ui_home - bind HOME type action } input { name ui_home_select - bind "SHIFT HOME" type action } input { name ui_end - bind END type action } input { name ui_end_select - bind "SHIFT END" type action } input { name ui_left - bind LEFT type action } input { name ui_left_select - bind "SHIFT LEFT" type action } input { name ui_right - bind RIGHT type action } input { name ui_right_select - bind "SHIFT RIGHT" type action } input { name ui_up - bind UP type action } input { name ui_up_select - bind "SHIFT UP" type action } input { name ui_down - bind DOWN type action } input { name ui_down_select - bind "SHIFT DOWN" type action } @@ -170,19 +199,6 @@ add source/foundation/async.c description "This is just a test variable!" } - ccmd - { - name list - function _console_list_ccmd - description "List everything the console knows" - - parameter - { - enum "a b c d e f g" - description "Nothing!" - } - } - event { name TEST_HOOK diff --git a/include/common_api.h b/include/common_api.h index 70e4938..4b95e61 100644 --- a/include/common_api.h +++ b/include/common_api.h @@ -205,7 +205,7 @@ bool stream_error( struct stream *stream ); const c8 *string_get( struct stream *string ); void string_clip( struct stream *string, i32 length ); -void string_append( struct stream *string, const c8 *substring ); +void string_append( struct stream *string, const c8 *substring, u32 length ); void string_append_c8( struct stream *string, c8 c ); void string_append_i64( struct stream *string, i64 value, u64 base ); void string_append_i64r( struct stream *string, i64 value, u64 base, u32 width, c8 blank_c8acter ); @@ -224,7 +224,11 @@ struct v_string_arg u32 type; u32 base; + union + { u32 decimals; + u32 length; + }; }; void v_string( struct stream *string, struct v_string_arg *argument_list ); @@ -235,7 +239,7 @@ void v_string( struct stream *string, struct v_string_arg *argument_list ); #define k_$float 4 #define k_$errno 5 -#define $string( X, ... ) { string_get( X ), __VA_ARGS__ } +#define $string( X, ... ) { X, __VA_ARGS__ } #define $unsigned( X, ... ) { .type=k_$unsigned, ._u64=X, __VA_ARGS__ } #define $signed( X, ... ) { .type=k_$signed, ._i64=X, __VA_ARGS__ } #define $float( X, ... ) { .type=k_$float, ._f64=X, __VA_ARGS__ } diff --git a/include/engine_interface.h b/include/engine_interface.h index 211d6f7..c0dd5bc 100644 --- a/include/engine_interface.h +++ b/include/engine_interface.h @@ -34,6 +34,7 @@ enum input_type enum input_device { + k_input_device_none = 0, k_input_device_keyboard, k_input_device_controller }; diff --git a/source/console_core.c b/source/console_core.c index 0b386ab..c9db750 100644 --- a/source/console_core.c +++ b/source/console_core.c @@ -1,3 +1,11 @@ +#include "console_core.h" + +struct console_command +{ + const c8 *alias; + i32 (*fn)( struct console_arguments *args ); +}; + #include "generated/console.c" struct @@ -12,30 +20,115 @@ struct history_item const c8 *entry; }; +i32 _console_exec_ccmd( struct console_arguments *args ) +{ + const c8 *path = console_get_argument( args, 0 ); + if( !path ) + { + $log( $error, {"Usage: exec "} ); + return -1; + } + + struct stream file; + if( !stream_open_file( &file, path, k_stream_read ) ) + return -1; + + bool more_to_go = 1; + while( more_to_go ) + { + u32 temp_frame = _start_temporary_frame(); + + struct stream line; + stream_open_stack( &line, _temporary_stack_allocator(), k_stream_null_terminate ); + + c8 c; + more_to_go = 0; + while( stream_read( &file, &c, 1 ) ) + { + if( c == '\n' ) + { + more_to_go = 1; + break; + } + string_append_c8( &line, c ); + } + + if( stream_offset( &line ) ) + _console_execute( string_get( &line ), 1, 0 ); + + _end_temporary_frame( temp_frame ); + } + + return 0; +} + void _console_execute( const c8 *command, bool silent, bool cheat_allowed ) { + if( !silent ) + $log( $shell, {command} ); + struct stream string; - stream_open_buffer_read( &string, command, buffer_last_index( command, 0, 0 ), 0 ); + stream_open_buffer_read( &string, command, buffer_last_index( command, 0, 0 )+1, 0 ); - c8 *tokens[32]; - u32 token_count = 0; + struct console_arguments args; + args.count = 0; u32 temp_frame = _start_temporary_frame(); + struct console_command *target_command = NULL; - for( u32 i=0; i<32; i ++ ) + // TODO: warning if we put to many or to few arguments & required ones. + for( u32 i=0; ialias, 0, command+start, length ) ) + { + target_command = cmd; + break; + } + } + + if( !target_command ) + { + $log( $error, {"There is no command called '"}, $string( command+start, .length=length ), {"'"} ); + return; + } + } + else + { + c8 *buffer = _temporary_allocate( length + 1, 4 ); + buffer_copy( command + start, length, buffer, length ); + buffer[ length ] = 0; - $log( $info, {":: "}, {tokens[ token_count ]} ); + args.arguments[ args.count ] = buffer; + args.count ++; + } } + else break; } + if( target_command ) + target_command->fn( &args ); + _end_temporary_frame( temp_frame ); } + +const c8 *console_get_argument( struct console_arguments *args, u32 index ) +{ + if( index < args->count ) + return args->arguments[ index ]; + else + return NULL; +} + +void _console_init(void) +{ + _console_execute( "exec cfg/default.cfg", 1, 0 ); +} diff --git a/source/console_core.h b/source/console_core.h index 862bd99..91928ac 100644 --- a/source/console_core.h +++ b/source/console_core.h @@ -1 +1,11 @@ #include "generated/console.h" + +void _console_init(void); +void _console_execute( const c8 *command, bool silent, bool cheat_allowed ); + +struct console_arguments +{ + const c8 *arguments[ 32 ]; + u32 count; +}; +const c8 *console_get_argument( struct console_arguments *args, u32 index ); diff --git a/source/engine/input.c b/source/engine/input.c index edb1359..bf0c5ec 100644 --- a/source/engine/input.c +++ b/source/engine/input.c @@ -1,7 +1,156 @@ #include "glfw.h" #include "engine_interface.h" +#include "console_core.h" #include "generated/input.c" +struct input_alias +{ + const c8 *alias; + u8 device_type; + u32 key, mod; + u32 alias_hash; +} +_input_aliases[] = +{ + { "SPACE", k_input_device_keyboard, .key = GLFW_KEY_SPACE }, + { "APOSTROPHE", k_input_device_keyboard, .key = GLFW_KEY_APOSTROPHE }, + { "COMMA", k_input_device_keyboard, .key = GLFW_KEY_COMMA }, + { "MINUS", k_input_device_keyboard, .key = GLFW_KEY_MINUS }, + { "PERIOD", k_input_device_keyboard, .key = GLFW_KEY_PERIOD }, + { "SLASH", k_input_device_keyboard, .key = GLFW_KEY_SLASH }, + { "0", k_input_device_keyboard, .key = GLFW_KEY_0 }, + { "1", k_input_device_keyboard, .key = GLFW_KEY_1 }, + { "2", k_input_device_keyboard, .key = GLFW_KEY_2 }, + { "3", k_input_device_keyboard, .key = GLFW_KEY_3 }, + { "4", k_input_device_keyboard, .key = GLFW_KEY_4 }, + { "5", k_input_device_keyboard, .key = GLFW_KEY_5 }, + { "6", k_input_device_keyboard, .key = GLFW_KEY_6 }, + { "7", k_input_device_keyboard, .key = GLFW_KEY_7 }, + { "8", k_input_device_keyboard, .key = GLFW_KEY_8 }, + { "9", k_input_device_keyboard, .key = GLFW_KEY_9 }, + { "SEMICOLON", k_input_device_keyboard, .key = GLFW_KEY_SEMICOLON }, + { "EQUAL", k_input_device_keyboard, .key = GLFW_KEY_EQUAL }, + { "A", k_input_device_keyboard, .key = GLFW_KEY_A }, + { "B", k_input_device_keyboard, .key = GLFW_KEY_B }, + { "C", k_input_device_keyboard, .key = GLFW_KEY_C }, + { "D", k_input_device_keyboard, .key = GLFW_KEY_D }, + { "E", k_input_device_keyboard, .key = GLFW_KEY_E }, + { "F", k_input_device_keyboard, .key = GLFW_KEY_F }, + { "G", k_input_device_keyboard, .key = GLFW_KEY_G }, + { "H", k_input_device_keyboard, .key = GLFW_KEY_H }, + { "I", k_input_device_keyboard, .key = GLFW_KEY_I }, + { "J", k_input_device_keyboard, .key = GLFW_KEY_J }, + { "K", k_input_device_keyboard, .key = GLFW_KEY_K }, + { "L", k_input_device_keyboard, .key = GLFW_KEY_L }, + { "M", k_input_device_keyboard, .key = GLFW_KEY_M }, + { "N", k_input_device_keyboard, .key = GLFW_KEY_N }, + { "O", k_input_device_keyboard, .key = GLFW_KEY_O }, + { "P", k_input_device_keyboard, .key = GLFW_KEY_P }, + { "Q", k_input_device_keyboard, .key = GLFW_KEY_Q }, + { "R", k_input_device_keyboard, .key = GLFW_KEY_R }, + { "S", k_input_device_keyboard, .key = GLFW_KEY_S }, + { "T", k_input_device_keyboard, .key = GLFW_KEY_T }, + { "U", k_input_device_keyboard, .key = GLFW_KEY_U }, + { "V", k_input_device_keyboard, .key = GLFW_KEY_V }, + { "W", k_input_device_keyboard, .key = GLFW_KEY_W }, + { "X", k_input_device_keyboard, .key = GLFW_KEY_X }, + { "Y", k_input_device_keyboard, .key = GLFW_KEY_Y }, + { "Z", k_input_device_keyboard, .key = GLFW_KEY_Z }, + { "LEFT_BRACKET", k_input_device_keyboard, .key = GLFW_KEY_LEFT_BRACKET }, + { "BACKSLASH", k_input_device_keyboard, .key = GLFW_KEY_BACKSLASH }, + { "RIGHT_BRACKET", k_input_device_keyboard, .key = GLFW_KEY_RIGHT_BRACKET }, + { "GRAVE_ACCENT", k_input_device_keyboard, .key = GLFW_KEY_GRAVE_ACCENT }, + { "WORLD_1", k_input_device_keyboard, .key = GLFW_KEY_WORLD_1 }, + { "WORLD_2", k_input_device_keyboard, .key = GLFW_KEY_WORLD_2 }, + { "ESCAPE", k_input_device_keyboard, .key = GLFW_KEY_ESCAPE }, + { "ENTER", k_input_device_keyboard, .key = GLFW_KEY_ENTER }, + { "TAB", k_input_device_keyboard, .key = GLFW_KEY_TAB }, + { "BACKSPACE", k_input_device_keyboard, .key = GLFW_KEY_BACKSPACE }, + { "INSERT", k_input_device_keyboard, .key = GLFW_KEY_INSERT }, + { "DELETE", k_input_device_keyboard, .key = GLFW_KEY_DELETE }, + { "RIGHT", k_input_device_keyboard, .key = GLFW_KEY_RIGHT }, + { "LEFT", k_input_device_keyboard, .key = GLFW_KEY_LEFT }, + { "DOWN", k_input_device_keyboard, .key = GLFW_KEY_DOWN }, + { "UP", k_input_device_keyboard, .key = GLFW_KEY_UP }, + { "PAGE_UP", k_input_device_keyboard, .key = GLFW_KEY_PAGE_UP }, + { "PAGE_DOWN", k_input_device_keyboard, .key = GLFW_KEY_PAGE_DOWN }, + { "HOME", k_input_device_keyboard, .key = GLFW_KEY_HOME }, + { "END", k_input_device_keyboard, .key = GLFW_KEY_END }, + { "CAPS_LOCK", k_input_device_keyboard, .key = GLFW_KEY_CAPS_LOCK }, + { "SCROLL_LOCK", k_input_device_keyboard, .key = GLFW_KEY_SCROLL_LOCK }, + { "NUM_LOCK", k_input_device_keyboard, .key = GLFW_KEY_NUM_LOCK }, + { "PRINT_SCREEN", k_input_device_keyboard, .key = GLFW_KEY_PRINT_SCREEN }, + { "PAUSE", k_input_device_keyboard, .key = GLFW_KEY_PAUSE }, + { "F1", k_input_device_keyboard, .key = GLFW_KEY_F1 }, + { "F2", k_input_device_keyboard, .key = GLFW_KEY_F2 }, + { "F3", k_input_device_keyboard, .key = GLFW_KEY_F3 }, + { "F4", k_input_device_keyboard, .key = GLFW_KEY_F4 }, + { "F5", k_input_device_keyboard, .key = GLFW_KEY_F5 }, + { "F6", k_input_device_keyboard, .key = GLFW_KEY_F6 }, + { "F7", k_input_device_keyboard, .key = GLFW_KEY_F7 }, + { "F8", k_input_device_keyboard, .key = GLFW_KEY_F8 }, + { "F9", k_input_device_keyboard, .key = GLFW_KEY_F9 }, + { "F10", k_input_device_keyboard, .key = GLFW_KEY_F10 }, + { "F11", k_input_device_keyboard, .key = GLFW_KEY_F11 }, + { "F12", k_input_device_keyboard, .key = GLFW_KEY_F12 }, + { "F13", k_input_device_keyboard, .key = GLFW_KEY_F13 }, + { "F14", k_input_device_keyboard, .key = GLFW_KEY_F14 }, + { "F15", k_input_device_keyboard, .key = GLFW_KEY_F15 }, + { "F16", k_input_device_keyboard, .key = GLFW_KEY_F16 }, + { "F17", k_input_device_keyboard, .key = GLFW_KEY_F17 }, + { "F18", k_input_device_keyboard, .key = GLFW_KEY_F18 }, + { "F19", k_input_device_keyboard, .key = GLFW_KEY_F19 }, + { "F20", k_input_device_keyboard, .key = GLFW_KEY_F20 }, + { "F21", k_input_device_keyboard, .key = GLFW_KEY_F21 }, + { "F22", k_input_device_keyboard, .key = GLFW_KEY_F22 }, + { "F23", k_input_device_keyboard, .key = GLFW_KEY_F23 }, + { "F24", k_input_device_keyboard, .key = GLFW_KEY_F24 }, + { "F25", k_input_device_keyboard, .key = GLFW_KEY_F25 }, + { "KP_0", k_input_device_keyboard, .key = GLFW_KEY_KP_0 }, + { "KP_1", k_input_device_keyboard, .key = GLFW_KEY_KP_1 }, + { "KP_2", k_input_device_keyboard, .key = GLFW_KEY_KP_2 }, + { "KP_3", k_input_device_keyboard, .key = GLFW_KEY_KP_3 }, + { "KP_4", k_input_device_keyboard, .key = GLFW_KEY_KP_4 }, + { "KP_5", k_input_device_keyboard, .key = GLFW_KEY_KP_5 }, + { "KP_6", k_input_device_keyboard, .key = GLFW_KEY_KP_6 }, + { "KP_7", k_input_device_keyboard, .key = GLFW_KEY_KP_7 }, + { "KP_8", k_input_device_keyboard, .key = GLFW_KEY_KP_8 }, + { "KP_9", k_input_device_keyboard, .key = GLFW_KEY_KP_9 }, + { "KP_DECIMAL", k_input_device_keyboard, .key = GLFW_KEY_KP_DECIMAL }, + { "KP_DIVIDE", k_input_device_keyboard, .key = GLFW_KEY_KP_DIVIDE }, + { "KP_MULTIPLY", k_input_device_keyboard, .key = GLFW_KEY_KP_MULTIPLY }, + { "KP_SUBTRACT", k_input_device_keyboard, .key = GLFW_KEY_KP_SUBTRACT }, + { "KP_ADD", k_input_device_keyboard, .key = GLFW_KEY_KP_ADD }, + { "KP_ENTER", k_input_device_keyboard, .key = GLFW_KEY_KP_ENTER }, + { "KP_EQUAL", k_input_device_keyboard, .key = GLFW_KEY_KP_EQUAL }, + { "LEFT_SHIFT", k_input_device_keyboard, .key = GLFW_KEY_LEFT_SHIFT }, + { "LEFT_CONTROL", k_input_device_keyboard, .key = GLFW_KEY_LEFT_CONTROL }, + { "LEFT_ALT", k_input_device_keyboard, .key = GLFW_KEY_LEFT_ALT }, + { "LEFT_SUPER", k_input_device_keyboard, .key = GLFW_KEY_LEFT_SUPER }, + { "RIGHT_SHIFT", k_input_device_keyboard, .key = GLFW_KEY_RIGHT_SHIFT }, + { "RIGHT_CONTROL", k_input_device_keyboard, .key = GLFW_KEY_RIGHT_CONTROL }, + { "RIGHT_ALT", k_input_device_keyboard, .key = GLFW_KEY_RIGHT_ALT }, + { "RIGHT_SUPER", k_input_device_keyboard, .key = GLFW_KEY_RIGHT_SUPER }, + { "MENU", k_input_device_keyboard, .key = GLFW_KEY_MENU }, + { "SHIFT", k_input_device_keyboard, .mod = GLFW_MOD_SHIFT }, + { "CONTROL", k_input_device_keyboard, .mod = GLFW_MOD_CONTROL }, + { "ALT", k_input_device_keyboard, .mod = GLFW_MOD_ALT }, + { "SUPER", k_input_device_keyboard, .mod = GLFW_MOD_SUPER }, +}; + +static struct input_alias *_input_alias_find( const c8 *alias, u32 alias_length ) +{ + u32 hash = buffer_djb2( alias, alias_length ); + for( u32 i=0; ialias_hash == hash ) + if( compare_buffers( alias_i->alias, 0, alias, alias_length ) ) + return alias_i; + } + return NULL; +} + static i32 _input_page = 0; union input_state @@ -30,14 +179,119 @@ static _input_states[2][k_input_count]; struct bind { u16 device, input_index; - struct - { - u32 main, modifiers; - } - keyboard; + u32 id; + u32 modifiers; }; struct stretchy_allocator _bind_allocator; +i32 _input_bind_ccmd( struct console_arguments *args ) +{ + const c8 *buttons = console_get_argument( args, 0 ); + if( !buttons ) + { + $log( $error, {"Usage: bind