summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2025-01-15 04:19:20 +0100
committerMitya Selivanov <automainint@guattari.tech>2025-01-15 04:19:20 +0100
commitc6c72f54ea7f25defa7d39216f7713c46b873362 (patch)
tree42af14ace38302349994ef0631f441c09a4fc4dc
parentfe6c2a13cb0d4819f05fb5867f8619e54c5b9191 (diff)
downloadreduced_system_layer-c6c72f54ea7f25defa7d39216f7713c46b873362.zip
Drop files procs; Logging macro
-rw-r--r--index.htm7
-rwxr-xr-xreduced_system_layer.c209
2 files changed, 167 insertions, 49 deletions
diff --git a/index.htm b/index.htm
index d426609..1339f23 100644
--- a/index.htm
+++ b/index.htm
@@ -17,6 +17,7 @@
this.frame_width = 0;
this.frame_height = 0;
+ this.pixels_size = 0;
let event_handlers = {
wasm : (ev) => {
@@ -96,6 +97,7 @@
resize : (ev) => {
this.frame_width = ev.width;
this.frame_height = ev.height;
+ this.pixels_size = ev.width * ev.height * 4;
},
mousedown : (ev) => {
@@ -162,9 +164,8 @@
this.program.exports.js_frame(this.frame_width, this.frame_height, num_samples);
- let pixels_size = this.frame_width * this.frame_height * 4;
- let pixels_buffer = new ArrayBuffer(pixels_size);
- new Uint8Array(pixels_buffer).set(new Uint8Array(this.memory_buffer, this.pixels_address, pixels_size));
+ let pixels_buffer = new ArrayBuffer(this.pixels_size);
+ new Uint8Array(pixels_buffer).set(new Uint8Array(this.memory_buffer, this.pixels_address, this.pixels_size));
this.port.postMessage({
id : "frame",
diff --git a/reduced_system_layer.c b/reduced_system_layer.c
index 8af0007..980e12c 100755
--- a/reduced_system_layer.c
+++ b/reduced_system_layer.c
@@ -22,9 +22,6 @@
#/ - Optimized to use in a single source file.
#/ Installation process? Ctrl+C, Ctrl+V, done.
#/
-#/ If you have an idea how to reduce the feature set further,
-#/ let me know!
-#/
#/ ----------------------------------------------------------------
#/
#/ To-Do list
@@ -51,6 +48,7 @@
#/ - Logging
#/ - Test suite
#/ - Long term
+#/ - Allocator
#/ - Parsing
#/ - Printing
#/ - File system
@@ -409,8 +407,6 @@ typedef struct {
} Input_Key;
typedef struct {
- i32 x;
- i32 y;
i64 name_size;
c8 *name;
i64 data_size;
@@ -441,6 +437,7 @@ typedef struct {
i64 wheel_dy;
i64 sound_clock_time;
i64 sound_clock_carry;
+ i64 num_sound_samples_elapsed;
i64 num_drop_files;
Drop_File *drop_files;
@@ -671,6 +668,19 @@ void p_event_loop(void) {
//
// ================================================================
+// TEMP
+#if defined(__wasm__)
+#define LOG_ERROR(...) do { } while (0)
+#else
+#include <stdio.h>
+#define LOG_ERROR(...) \
+ do { \
+ fprintf(stderr, "%s:%d, %s: ", __FILE__, __LINE__, __func__); \
+ fprintf(stderr, __VA_ARGS__); \
+ fprintf(stderr, "\n"); \
+ } while (0)
+#endif
+
c32 utf8_read(i64 len, c8 *s) {
if (len >= 1 &&
(s[0] & 0x80) == 0)
@@ -765,15 +775,121 @@ static i64 sound_samples_elapsed_(void) {
}
i64 time_elapsed = p_time() - g_platform.sound_clock_time;
- i64 sum = time_elapsed * SOUND_SAMPLE_RATE + g_platform.sound_clock_carry;
- i64 num_samples = sum / 1000;
+ i64 delta = time_elapsed * SOUND_SAMPLE_RATE + g_platform.sound_clock_carry;
+ i64 num_samples = delta / 1000;
g_platform.sound_clock_time += time_elapsed;
- g_platform.sound_clock_carry = sum % 1000;
+ g_platform.sound_clock_carry = delta % 1000;
return num_samples;
}
+// TEMP
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunused-function"
+
+static void drop_files_clean_(void) {
+ g_platform.num_drop_files = 0;
+ g_platform.drop_files = NULL;
+}
+
+static i64 align_size_(i64 x) {
+ return (x + 7) & ~7ll;
+}
+
+static i64 drop_files_data_size_(i64 num) {
+ if (num > g_platform.num_drop_files) {
+ LOG_ERROR("Sanity");
+ return 0;
+ }
+
+ if (num > 0 && g_platform.drop_files == NULL) {
+ LOG_ERROR("Sanity");
+ return 0;
+ }
+
+ i64 data_size = 0;
+
+ for (i64 i = 0; i < num; ++i)
+ data_size +=
+ align_size_(g_platform.drop_files[i].name_size) +
+ align_size_(g_platform.drop_files[i].data_size);
+
+ return data_size;
+}
+
+static void drop_files_set_num_(i64 num) {
+ if (num <= g_platform.num_drop_files) {
+ LOG_ERROR("Sanity");
+ return;
+ }
+
+ i64 available = DROP_FILE_BUFFER_SIZE - drop_files_data_size_(g_platform.num_drop_files);
+ if (available < num * (i64) sizeof(Drop_File))
+ return;
+
+ i64 src_size = (i64) sizeof(Drop_File) * g_platform.num_drop_files;
+ i64 dst_size = (i64) sizeof(Drop_File) * num;
+
+ u8 *src = g_platform.drop_buffer + (DROP_FILE_BUFFER_SIZE - src_size);
+ u8 *dst = g_platform.drop_buffer + (DROP_FILE_BUFFER_SIZE - num * (i64) sizeof(Drop_File));
+
+ for (i64 i = 0; i < src_size; ++i)
+ dst[i] = src[i];
+ for (i64 i = src_size; i < dst_size; ++i)
+ dst[i] = 0;
+
+ g_platform.num_drop_files = num;
+ g_platform.drop_files = (Drop_File *) dst;
+}
+
+static void drop_files_set_name_(i64 index, i64 name_size, c8 *name) {
+ if (g_platform.drop_files == NULL || index + 1 != g_platform.num_drop_files) {
+ LOG_ERROR("Sanity");
+ return;
+ }
+
+ i64 offset = drop_files_data_size_(index);
+ i64 available = DROP_FILE_BUFFER_SIZE - offset - g_platform.num_drop_files * sizeof(Drop_File);
+
+ if (available < name_size + 1) {
+ LOG_ERROR("Sanity");
+ return;
+ }
+
+ g_platform.drop_files[index].name_size = name_size + 1;
+ g_platform.drop_files[index].name = (c8 *) g_platform.drop_buffer + offset;
+
+ for (i64 i = 0; i < name_size; ++i)
+ g_platform.drop_files[index].name[i] = name[i];
+ g_platform.drop_files[index].name[name_size] = '\0';
+}
+
+static void drop_files_set_data_(i64 index, i64 data_size, u8 *data) {
+ if (g_platform.drop_files == NULL || index + 1 != g_platform.num_drop_files) {
+ LOG_ERROR("Sanity");
+ return;
+ }
+
+ g_platform.drop_files[index].data_size = 0;
+
+ i64 offset = drop_files_data_size_(index + 1);
+ i64 available = DROP_FILE_BUFFER_SIZE - offset - g_platform.num_drop_files * sizeof(Drop_File);
+
+ if (available < data_size) {
+ LOG_ERROR("Sanity");
+ return;
+ }
+
+ g_platform.drop_files[index].data_size = data_size;
+ g_platform.drop_files[index].data = g_platform.drop_buffer + offset;
+
+ for (i64 i = 0; i < data_size; ++i)
+ g_platform.drop_files[index].data[i] = data[i];
+}
+
+#pragma GCC diagnostic pop
+
// ================================================================
//
// Unix
@@ -821,7 +937,6 @@ void p_sleep_for(i64 duration) {
#if defined(__unix__)
-#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
@@ -887,7 +1002,7 @@ static b8 sockets_open(u16 slot, IP_Address address, u16 *local_port) {
_sockets[slot].socket = socket(address.protocol == IPv4_UDP ? AF_INET : AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (_sockets[slot].socket == -1) {
- fprintf(stderr, "ERROR: socket failed (errno %d)\n", errno);
+ LOG_ERROR("socket failed (errno %d)", errno);
return 0;
}
@@ -904,14 +1019,14 @@ static b8 sockets_open(u16 slot, IP_Address address, u16 *local_port) {
if (bind(_sockets[slot].socket, p, p_len) == -1) {
close(_sockets[slot].socket);
- fprintf(stderr, "ERROR: bind failed (errno %d)\n", errno);
+ LOG_ERROR("bind failed (errno %d)", errno);
return 0;
}
if (getsockname(_sockets[slot].socket, p, &(socklen_t) {p_len}) == -1) {
close(_sockets[slot].socket);
- fprintf(stderr, "ERROR: getsockname failed (errno %d)\n", errno);
+ LOG_ERROR("getsockname failed (errno %d)", errno);
return 0;
}
@@ -932,12 +1047,12 @@ static b8 sockets_open(u16 slot, IP_Address address, u16 *local_port) {
i64 p_recv(u16 slot, IP_Address address, i64 size, u8 *data, u16 *local_port, IP_Address *remote_address) {
if (slot >= MAX_NUM_SOCKETS) {
- fprintf(stderr, "%s:%d, %s: Invalid slot %d.\n", __FILE__, __LINE__, __func__, (i32) (u32) slot);
+ LOG_ERROR("Invalid slot %d.", (i32) (u32) slot);
return 0;
}
if (address.protocol != IPv4_UDP && address.protocol != IPv6_UDP) {
- fprintf(stderr, "%s:%d, %s: Invalid address protocol %d.\n", __FILE__, __LINE__, __func__, (i32) (u32) address.protocol);
+ LOG_ERROR("Invalid address protocol %d.", (i32) (u32) address.protocol);
return 0;
}
@@ -973,7 +1088,7 @@ i64 p_recv(u16 slot, IP_Address address, i64 size, u8 *data, u16 *local_port, IP
if (errno == EAGAIN || errno == EWOULDBLOCK)
return 0;
- fprintf(stderr, "%s:%d, %s: recvfrom failed (errno %d).\n", __FILE__, __LINE__, __func__, errno);
+ LOG_ERROR("recvfrom failed (errno %d).", errno);
return 0;
}
@@ -995,12 +1110,12 @@ i64 p_recv(u16 slot, IP_Address address, i64 size, u8 *data, u16 *local_port, IP
i64 p_send(u16 slot, IP_Address address, i64 size, u8 *data, u16 *local_port) {
if (slot >= MAX_NUM_SOCKETS) {
- fprintf(stderr, "%s:%d, %s: Invalid slot %d.\n", __FILE__, __LINE__, __func__, (i32) (u32) slot);
+ LOG_ERROR("Invalid slot %d.", (i32) (u32) slot);
return 0;
}
if (address.protocol != IPv4_UDP && address.protocol != IPv6_UDP) {
- fprintf(stderr, "%s:%d, %s: Invalid address protocol %d.\n", __FILE__, __LINE__, __func__, (i32) (u32) address.protocol);
+ LOG_ERROR("Invalid address protocol %d.", (i32) (u32) address.protocol);
return 0;
}
@@ -1047,7 +1162,7 @@ i64 p_send(u16 slot, IP_Address address, i64 size, u8 *data, u16 *local_port) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
return 0;
- fprintf(stderr, "%s:%d, %s: sendto failed (errno %d).\n", __FILE__, __LINE__, __func__, errno);
+ LOG_ERROR("sendto failed (errno %d).", errno);
return 0;
}
@@ -1080,7 +1195,7 @@ static void sound_init(void) {
s = snd_pcm_open(&_sound_out, "default", SND_PCM_STREAM_PLAYBACK, 0);
if (s < 0) {
- fprintf(stderr, "%s:%d, %s: snd_pcm_open failed: %s", __FILE__, __LINE__, __func__, snd_strerror(s));
+ LOG_ERROR("snd_pcm_open failed: %s", snd_strerror(s));
return;
}
@@ -1091,45 +1206,45 @@ static void sound_init(void) {
s = snd_pcm_hw_params_any(_sound_out, hw_params);
if (s < 0)
- fprintf(stderr, "%s:%d, %s: snd_pcm_hw_params_any failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s));
+ LOG_ERROR("snd_pcm_hw_params_any failed: %s", snd_strerror(s));
s = snd_pcm_hw_params_set_access(_sound_out, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
if (s < 0)
- fprintf(stderr, "%s:%d, %s: snd_pcm_hw_params_set_access failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s));
+ LOG_ERROR("snd_pcm_hw_params_set_access failed: %s", snd_strerror(s));
s = snd_pcm_hw_params_set_format(_sound_out, hw_params, SND_PCM_FORMAT_FLOAT_LE);
if (s < 0)
- fprintf(stderr, "%s:%d, %s: snd_pcm_hw_params_set_format failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s));
+ LOG_ERROR("snd_pcm_hw_params_set_format failed: %s", snd_strerror(s));
s = snd_pcm_hw_params_set_rate(_sound_out, hw_params, SOUND_SAMPLE_RATE, 0);
if (s < 0)
- fprintf(stderr, "%s:%d, %s: snd_pcm_hw_params_set_rate failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s));
+ LOG_ERROR("snd_pcm_hw_params_set_rate failed: %s", snd_strerror(s));
s = snd_pcm_hw_params_set_channels(_sound_out, hw_params, NUM_SOUND_CHANNELS);
if (s < 0)
- fprintf(stderr, "%s:%d, %s: snd_pcm_hw_params_set_channels failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s));
+ LOG_ERROR("snd_pcm_hw_params_set_channels failed: %s", snd_strerror(s));
s = snd_pcm_hw_params(_sound_out, hw_params);
if (s < 0)
- fprintf(stderr, "%s:%d, %s: snd_pcm_hw_params failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s));
+ LOG_ERROR("snd_pcm_hw_params failed: %s", snd_strerror(s));
snd_pcm_sw_params_alloca(&sw_params);
s = snd_pcm_sw_params_current(_sound_out, sw_params);
if (s < 0)
- fprintf(stderr, "%s:%d, %s: snd_pcm_sw_params_current failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s));
+ LOG_ERROR("snd_pcm_sw_params_current failed: %s", snd_strerror(s));
s = snd_pcm_sw_params_set_avail_min(_sound_out, sw_params, SOUND_AVAIL_MIN);
if (s < 0)
- fprintf(stderr, "%s:%d, %s: snd_pcm_sw_params_set_avail_min failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s));
+ LOG_ERROR("snd_pcm_sw_params_set_avail_min failed: %s", snd_strerror(s));
s = snd_pcm_sw_params(_sound_out, sw_params);
if (s < 0)
- fprintf(stderr, "%s:%d, %s: snd_pcm_sw_params failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s));
+ LOG_ERROR("snd_pcm_sw_params failed: %s", snd_strerror(s));
s = snd_pcm_prepare(_sound_out);
if (s < 0)
- fprintf(stderr, "%s:%d, %s: snd_pcm_prepare failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s));
+ LOG_ERROR("snd_pcm_prepare failed: %s", snd_strerror(s));
_sound_ready = 1;
}
@@ -1142,11 +1257,11 @@ static void sound_cleanup(void) {
s = snd_pcm_nonblock(_sound_out, 0);
if (s < 0)
- fprintf(stderr, "%s:%d, %s: snd_pcm_nonblock failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s));
+ LOG_ERROR("snd_pcm_nonblock failed: %s", snd_strerror(s));
s = snd_pcm_drain(_sound_out);
if (s < 0)
- fprintf(stderr, "%s:%d, %s: snd_pcm_drain failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s));
+ LOG_ERROR("snd_pcm_drain failed: %s", snd_strerror(s));
// FIXME Memory leaks, seems to be an ALSA bug.
// snd_pcm_close(_sound_out);
@@ -1158,11 +1273,11 @@ static void sound_cleanup(void) {
void p_handle_sound(void) {
sound_init();
- i64 samples_elapsed = sound_samples_elapsed_();
- i64 num_frames = samples_elapsed * NUM_SOUND_CHANNELS;
+ g_platform.num_sound_samples_elapsed = sound_samples_elapsed_();
+ i64 num_frames = g_platform.num_sound_samples_elapsed * NUM_SOUND_CHANNELS;
if (num_frames > MAX_NUM_SOUND_FRAMES) {
- fprintf(stderr, "%s:%d, %s: Sound buffer overflow.\n", __FILE__, __LINE__, __func__);
+ LOG_ERROR("Sound buffer overflow.");
num_frames = MAX_NUM_SOUND_FRAMES;
}
@@ -1171,7 +1286,7 @@ void p_handle_sound(void) {
if (num_frames <= MAX_NUM_SOUND_FRAMES - _sound_position) {
s = snd_pcm_writei(_sound_out, g_platform.sound_ring + _sound_position, num_frames / NUM_SOUND_CHANNELS);
if (s < 0)
- fprintf(stderr, "%s:%d, %s: snd_pcm_writei failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s));
+ LOG_ERROR("snd_pcm_writei failed: %s", snd_strerror(s));
memset(g_platform.sound_ring + _sound_position, 0, num_frames * sizeof *g_platform.sound_ring);
} else {
@@ -1180,11 +1295,11 @@ void p_handle_sound(void) {
s = snd_pcm_writei(_sound_out, g_platform.sound_ring + _sound_position, part_one / NUM_SOUND_CHANNELS);
if (s < 0)
- fprintf(stderr, "%s:%d, %s: snd_pcm_writei failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s));
+ LOG_ERROR("snd_pcm_writei failed: %s", snd_strerror(s));
s = snd_pcm_writei(_sound_out, g_platform.sound_ring, part_two / NUM_SOUND_CHANNELS);
if (s < 0)
- fprintf(stderr, "%s:%d, %s: snd_pcm_writei failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s));
+ LOG_ERROR("snd_pcm_writei failed: %s", snd_strerror(s));
memset(g_platform.sound_ring + _sound_position, 0, part_one * sizeof *g_platform.sound_ring);
memset(g_platform.sound_ring, 0, part_two * sizeof *g_platform.sound_ring);
@@ -1195,7 +1310,7 @@ void p_handle_sound(void) {
void p_queue_sound(i64 delay_in_samples, i64 num_samples, f32 *frames) {
if (num_samples < 0)
- fprintf(stderr, "%s:%d, %s: Invalid num samples %lld.", __FILE__, __LINE__, __func__, num_samples);
+ LOG_ERROR("Invalid num samples %lld.", num_samples);
if (frames == NULL)
return;
@@ -1211,7 +1326,7 @@ void p_queue_sound(i64 delay_in_samples, i64 num_samples, f32 *frames) {
i64 num_frames = num_samples * NUM_SOUND_CHANNELS;
if (num_frames > MAX_NUM_SOUND_FRAMES) {
- fprintf(stderr, "%s:%d, %s: Sound buffer overflow.\n", __FILE__, __LINE__, __func__);
+ LOG_ERROR("Sound buffer overflow.");
return;
}
@@ -1270,7 +1385,7 @@ void p_init(void) {
_display = XOpenDisplay(NULL);
if (_display == NULL) {
- fprintf(stderr, "%s:%d, %s: XOpenDisplay failed.\n", __FILE__, __LINE__, __func__);
+ LOG_ERROR("XOpenDisplay failed.");
return;
}
@@ -1383,7 +1498,7 @@ void p_init(void) {
_gc = DefaultGC(_display, screen);
if (_gc == NULL) {
- fprintf(stderr, "%s:%d, %s: DefaultGC failed.\n", __FILE__, __LINE__, __func__);
+ LOG_ERROR("DefaultGC failed.");
return;
}
@@ -1416,14 +1531,14 @@ void p_init(void) {
_im = XOpenIM(_display, NULL, NULL, NULL);
if (_im == NULL) {
- fprintf(stderr, "%s:%d, %s: XOpenIM failed.\n", __FILE__, __LINE__, __func__);
+ LOG_ERROR("XOpenIM failed.");
return;
}
_ic = XCreateIC(_im, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, _window, NULL);
if (_ic == NULL) {
- fprintf(stderr, "%s:%d, %s: XCreateIC failed.\n", __FILE__, __LINE__, __func__);
+ LOG_ERROR("XCreateIC failed.");
return;
}
@@ -1833,7 +1948,7 @@ void p_render_frame(void) {
void p_clipboard_write(i64 size, c8 *data) {
if (size > MAX_CLIPBOARD_SIZE) {
- fprintf(stderr, "%s:%d, %s: Size is too big %lld.\n", __FILE__, __LINE__, __func__, size);
+ LOG_ERROR("Size is too big %lld.", size);
return;
}
@@ -1958,8 +2073,8 @@ void p_clipboard_write(i64 size, c8 *data) {
}
void p_handle_sound(void) {
- i64 samples_elapsed = sound_samples_elapsed_();
- _sound_position = (_sound_position + samples_elapsed * NUM_SOUND_CHANNELS) % MAX_NUM_SOUND_FRAMES;
+ g_platform.num_sound_samples_elapsed = sound_samples_elapsed_();
+ _sound_position = (_sound_position + g_platform.num_sound_samples_elapsed * NUM_SOUND_CHANNELS) % MAX_NUM_SOUND_FRAMES;
}
void p_queue_sound(i64 delay_in_samples, i64 num_samples, f32 *frames) {
@@ -2271,5 +2386,7 @@ __attribute__((export_name("js_clipboard_buffer"))) void *js_clipboard_buffer(i3
// ================================================================
+#undef LOG_ERROR
+
#endif // REDUCED_SYSTEM_LAYER_IMPL_GUARD_
#endif // REDUCED_SYSTEM_LAYER_HEADER