From fe6c2a13cb0d4819f05fb5867f8619e54c5b9191 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Wed, 15 Jan 2025 00:47:52 +0100 Subject: Defaults in web; Drop file interface --- reduced_system_layer.c | 283 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 220 insertions(+), 63 deletions(-) (limited to 'reduced_system_layer.c') diff --git a/reduced_system_layer.c b/reduced_system_layer.c index d86945e..8af0007 100755 --- a/reduced_system_layer.c +++ b/reduced_system_layer.c @@ -49,7 +49,23 @@ #/ - Vector math #/ - Anti-aliasing #/ - Logging -#/ - Parsing +#/ - Test suite +#/ - Long term +#/ - Parsing +#/ - Printing +#/ - File system +#/ - Secure random +#/ - Process +#/ - Shared memory +#/ - Shared mutex +#/ - Stackless coroutines +#/ - Big integer +#/ - Mersenne Twister 64 +#/ - Arithmetic coding +#/ - Threads - https://nachtimwald.com/2019/04/05/cross-platform-thread-wrapper +#/ - Web sockets +#/ - HTTP +#/ - Cryptography - https://github.com/jedisct1/supercop #/ #/ Done #/ @@ -191,6 +207,14 @@ typedef struct { f64 v[16]; } mat4; #define MAX_PIXEL_SIZE 16 #endif +#ifndef DEFAULT_PIXEL_SIZE +#if defined(__wasm__) +#define DEFAULT_PIXEL_SIZE 3 +#else +#define DEFAULT_PIXEL_SIZE 1 +#endif +#endif + #ifndef MIN_FRAME_DURATION #define MIN_FRAME_DURATION 9 #endif @@ -219,6 +243,10 @@ typedef struct { f64 v[16]; } mat4; #define MAX_NUM_SOUND_FRAMES (10 * SOUND_SAMPLE_RATE * NUM_SOUND_CHANNELS) #endif +#ifndef DROP_FILE_BUFFER_SIZE +#define DROP_FILE_BUFFER_SIZE (20 * 1024 * 1024) +#endif + // ================================================================ // // PLATFORM API @@ -240,7 +268,9 @@ extern "C" { enum { IPv4_UDP = 1, IPv6_UDP = 2, +}; +enum { MOD_CTRL = 1, MOD_SHIFT = 2, MOD_ALT = 3, @@ -378,6 +408,15 @@ typedef struct { c32 c; } Input_Key; +typedef struct { + i32 x; + i32 y; + i64 name_size; + c8 *name; + i64 data_size; + u8 *data; +} Drop_File; + typedef struct { c8 *title; i32 frame_width; @@ -388,8 +427,10 @@ typedef struct { b8 done; b8 has_focus; b8 has_cursor; + b8 files_dropped; i32 real_width; i32 real_height; + i32 pixel_size; i64 input_size; i64 clipboard_size; i32 cursor_x; @@ -401,12 +442,16 @@ typedef struct { i64 sound_clock_time; i64 sound_clock_carry; + i64 num_drop_files; + Drop_File *drop_files; + vec4_f32 pixels [MAX_NUM_PIXELS]; f32 sound_ring [MAX_NUM_SOUND_FRAMES]; Input_Key input [MAX_INPUT_SIZE]; c8 clipboard [MAX_CLIPBOARD_SIZE]; b8 key_down [MAX_NUM_KEYS]; b8 key_pressed [MAX_NUM_KEYS]; + u8 drop_buffer [DROP_FILE_BUFFER_SIZE]; } Platform; typedef struct { @@ -1203,7 +1248,6 @@ void p_queue_sound(i64 delay_in_samples, i64 num_samples, f32 *frames) { #include static i64 _frame_time = 0; -static i32 _pixel_size = 0; static XImage _image = {0}; static Display *_display = NULL; static GC _gc = NULL; @@ -1708,29 +1752,32 @@ i32 p_handle_events(void) { _image.width = attrs.width; _image.height = attrs.height; _image.bytes_per_line = _image.width * 4; + } - i32 width = _image.width; - i32 height = _image.height; + i32 width = _image.width; + i32 height = _image.height; - if (_pixel_size <= MIN_PIXEL_SIZE) - _pixel_size = MIN_PIXEL_SIZE; + if (g_platform.pixel_size <= 0) + g_platform.pixel_size = DEFAULT_PIXEL_SIZE; - if (_pixel_size > 1) { - width = (i32) floor(((f64) _image.width) / _pixel_size + .5); - height = (i32) floor(((f64) _image.height) / _pixel_size + .5); - } + if (g_platform.pixel_size <= MIN_PIXEL_SIZE) + g_platform.pixel_size = MIN_PIXEL_SIZE; - if (g_platform.real_width != _image.width || g_platform.real_height != _image.height) { - ++num_events; - g_platform.real_width = _image.width; - g_platform.real_height = _image.height; - } + if (g_platform.pixel_size > 1) { + width = (i32) floor(((f64) _image.width) / g_platform.pixel_size + .5); + height = (i32) floor(((f64) _image.height) / g_platform.pixel_size + .5); + } - if (g_platform.frame_width != width || g_platform.frame_height != height) { - ++num_events; - g_platform.frame_width = width; - g_platform.frame_height = height; - } + if (g_platform.real_width != _image.width || g_platform.real_height != _image.height) { + ++num_events; + g_platform.real_width = _image.width; + g_platform.real_height = _image.height; + } + + if (g_platform.frame_width != width || g_platform.frame_height != height) { + ++num_events; + g_platform.frame_width = width; + g_platform.frame_height = height; } return num_events; @@ -1777,10 +1824,10 @@ void p_render_frame(void) { if (!g_platform.exact_resolution) { i64 frame_duration = p_time() - _frame_time; - if (_pixel_size < MAX_PIXEL_SIZE && frame_duration > MAX_FRAME_DURATION) - ++_pixel_size; - else if (_pixel_size > MIN_PIXEL_SIZE && frame_duration < MIN_FRAME_DURATION) - --_pixel_size; + if (g_platform.pixel_size < MAX_PIXEL_SIZE && frame_duration > MAX_FRAME_DURATION) + ++g_platform.pixel_size; + else if (g_platform.pixel_size > MIN_PIXEL_SIZE && frame_duration < MIN_FRAME_DURATION) + --g_platform.pixel_size; } } @@ -1809,7 +1856,6 @@ void p_clipboard_write(i64 size, c8 *data) { static i32 _frame_width = 0; static i32 _frame_height = 0; -static i32 _pixel_size = 0; static i32 _num_events = 0; static i32 _input_size = 0; static b8 _wait_events = 0; @@ -1817,6 +1863,7 @@ static i64 _timeout = 0; static i64 _sound_position = 0; static i64 _sound_read = 0; +static u16 _key_map [MAX_NUM_KEYS] = {0}; static c8 _href [4096] = {0}; static u32 _pixels_scaled [MAX_NUM_PIXELS] = {0}; static u32 _pixels_internal [MAX_NUM_PIXELS] = {0}; @@ -1948,6 +1995,129 @@ void p_queue_sound(i64 delay_in_samples, i64 num_samples, f32 *frames) { } } +__attribute__((export_name("js_max_num_keys"))) i32 js_max_num_keys(void) { + return MAX_NUM_KEYS; +} + +__attribute__((export_name("js_key_map"))) void *js_key_map(void) { + i32 n = 0; + + _key_map[n++] = KEY_BACKSPACE; + _key_map[n++] = KEY_TAB; + _key_map[n++] = KEY_ENTER; + _key_map[n++] = KEY_LCTRL; + _key_map[n++] = KEY_RCTRL; + _key_map[n++] = KEY_LSHIFT; + _key_map[n++] = KEY_RSHIFT; + _key_map[n++] = KEY_LALT; + _key_map[n++] = KEY_RALT; + _key_map[n++] = KEY_LEFT; + _key_map[n++] = KEY_RIGHT; + _key_map[n++] = KEY_UP; + _key_map[n++] = KEY_DOWN; + _key_map[n++] = KEY_PAUSE; + _key_map[n++] = KEY_INSERT; + _key_map[n++] = KEY_HOME; + _key_map[n++] = KEY_END; + _key_map[n++] = KEY_PAGEUP; + _key_map[n++] = KEY_PAGEDOWN; + _key_map[n++] = KEY_ESCAPE; + _key_map[n++] = KEY_PRINTSCREEN; + _key_map[n++] = KEY_SPACE; + _key_map[n++] = KEY_LMETA; + _key_map[n++] = KEY_RMETA; + _key_map[n++] = KEY_QUOTE; + _key_map[n++] = KEY_COMMA; + _key_map[n++] = KEY_MINUS; + _key_map[n++] = KEY_PERIOD; + _key_map[n++] = KEY_SLASH; + _key_map[n++] = KEY_0; + _key_map[n++] = KEY_1; + _key_map[n++] = KEY_2; + _key_map[n++] = KEY_3; + _key_map[n++] = KEY_4; + _key_map[n++] = KEY_5; + _key_map[n++] = KEY_6; + _key_map[n++] = KEY_7; + _key_map[n++] = KEY_8; + _key_map[n++] = KEY_9; + _key_map[n++] = KEY_COLON; + _key_map[n++] = KEY_EQUAL; + _key_map[n++] = KEY_LBRACE; + _key_map[n++] = KEY_BACKSLASH; + _key_map[n++] = KEY_RBRACE; + _key_map[n++] = KEY_TILDE; + _key_map[n++] = KEY_A; + _key_map[n++] = KEY_B; + _key_map[n++] = KEY_C; + _key_map[n++] = KEY_D; + _key_map[n++] = KEY_E; + _key_map[n++] = KEY_F; + _key_map[n++] = KEY_G; + _key_map[n++] = KEY_H; + _key_map[n++] = KEY_I; + _key_map[n++] = KEY_J; + _key_map[n++] = KEY_K; + _key_map[n++] = KEY_L; + _key_map[n++] = KEY_M; + _key_map[n++] = KEY_N; + _key_map[n++] = KEY_O; + _key_map[n++] = KEY_P; + _key_map[n++] = KEY_Q; + _key_map[n++] = KEY_R; + _key_map[n++] = KEY_S; + _key_map[n++] = KEY_T; + _key_map[n++] = KEY_U; + _key_map[n++] = KEY_V; + _key_map[n++] = KEY_W; + _key_map[n++] = KEY_X; + _key_map[n++] = KEY_Y; + _key_map[n++] = KEY_Z; + _key_map[n++] = KEY_DELETE; + _key_map[n++] = KEY_F1; + _key_map[n++] = KEY_F2; + _key_map[n++] = KEY_F3; + _key_map[n++] = KEY_F4; + _key_map[n++] = KEY_F5; + _key_map[n++] = KEY_F6; + _key_map[n++] = KEY_F7; + _key_map[n++] = KEY_F8; + _key_map[n++] = KEY_F9; + _key_map[n++] = KEY_F10; + _key_map[n++] = KEY_F11; + _key_map[n++] = KEY_F12; + _key_map[n++] = KEY_F13; + _key_map[n++] = KEY_F14; + _key_map[n++] = KEY_F15; + _key_map[n++] = KEY_F16; + _key_map[n++] = KEY_F17; + _key_map[n++] = KEY_F18; + _key_map[n++] = KEY_F19; + _key_map[n++] = KEY_F20; + _key_map[n++] = KEY_F21; + _key_map[n++] = KEY_F22; + _key_map[n++] = KEY_F23; + _key_map[n++] = KEY_F24; + + return _key_map; +} + +__attribute__((export_name("js_sound_sample_rate"))) f64 js_sound_sample_rate(void) { + return (f64) SOUND_SAMPLE_RATE; +} + +__attribute__((export_name("js_num_sound_channels"))) i32 js_num_sound_channels(void) { + return NUM_SOUND_CHANNELS; +} + +__attribute__((export_name("js_max_num_sound_frames"))) i32 js_max_num_sound_frames(void) { + return MAX_NUM_SOUND_FRAMES; +} + +__attribute__((export_name("js_sound_buffer"))) void *js_sound_buffer(void) { + return _sound_buffer; +} + __attribute__((export_name("js_href"))) void *js_href(void) { return _href; } @@ -1974,29 +2144,32 @@ __attribute__((export_name("js_frame"))) void js_frame(i32 frame_width, i32 fram if (frame_width > 0 && frame_height > 0 && frame_width * frame_height <= MAX_NUM_PIXELS) { _frame_width = frame_width; _frame_height = frame_height; + } - i32 width = _frame_width; - i32 height = _frame_height; + i32 width = _frame_width; + i32 height = _frame_height; - if (_pixel_size <= MIN_PIXEL_SIZE) - _pixel_size = MIN_PIXEL_SIZE; + if (g_platform.pixel_size <= 0) + g_platform.pixel_size = DEFAULT_PIXEL_SIZE; - if (_pixel_size > 1) { - width = (i32) floor(((f64) _frame_width) / _pixel_size + .5); - height = (i32) floor(((f64) _frame_height) / _pixel_size + .5); - } + if (g_platform.pixel_size <= MIN_PIXEL_SIZE) + g_platform.pixel_size = MIN_PIXEL_SIZE; - if (g_platform.real_width != _frame_width || g_platform.real_height != _frame_height) { - ++_num_events; - g_platform.real_width = frame_width; - g_platform.real_height = frame_height; - } + if (g_platform.pixel_size > 1) { + width = (i32) floor(((f64) _frame_width) / g_platform.pixel_size + .5); + height = (i32) floor(((f64) _frame_height) / g_platform.pixel_size + .5); + } - if (g_platform.frame_width != width || g_platform.frame_height != height) { - ++_num_events; - g_platform.frame_width = width; - g_platform.frame_height = height; - } + if (g_platform.real_width != _frame_width || g_platform.real_height != _frame_height) { + ++_num_events; + g_platform.real_width = _frame_width; + g_platform.real_height = _frame_height; + } + + if (g_platform.frame_width != width || g_platform.frame_height != height) { + ++_num_events; + g_platform.frame_width = width; + g_platform.frame_height = height; } g_platform.done = 0; @@ -2021,10 +2194,10 @@ __attribute__((export_name("js_frame"))) void js_frame(i32 frame_width, i32 fram if (!g_platform.exact_resolution && do_render) { i64 frame_duration = p_time() - frame_time; - if (_pixel_size < MAX_PIXEL_SIZE && frame_duration > MAX_FRAME_DURATION) - ++_pixel_size; - else if (_pixel_size > MIN_PIXEL_SIZE && frame_duration < MIN_FRAME_DURATION) - --_pixel_size; + if (g_platform.pixel_size < MAX_PIXEL_SIZE && frame_duration > MAX_FRAME_DURATION) + ++g_platform.pixel_size; + else if (g_platform.pixel_size > MIN_PIXEL_SIZE && frame_duration < MIN_FRAME_DURATION) + --g_platform.pixel_size; } } @@ -2081,22 +2254,6 @@ __attribute__((export_name("js_keyup"))) void js_keyup(u32 key, u32 mod) { g_platform.key_down[MOD_META] = (mod & 8) ? 1 : 0; } -__attribute__((export_name("js_sample_rate"))) f64 js_sample_rate(void) { - return (f64) SOUND_SAMPLE_RATE; -} - -__attribute__((export_name("js_num_sound_channels"))) i32 js_num_sound_channels(void) { - return NUM_SOUND_CHANNELS; -} - -__attribute__((export_name("js_max_num_sound_frames"))) i32 js_max_num_sound_frames(void) { - return MAX_NUM_SOUND_FRAMES; -} - -__attribute__((export_name("js_sound_buffer"))) void *js_sound_buffer(void) { - return _sound_buffer; -} - __attribute__((export_name("js_clipboard_size"))) i32 js_clipboard_size(void) { return g_platform.clipboard_size; } -- cgit v1.2.3