summaryrefslogtreecommitdiff
path: root/reduced_system_layer.c
diff options
context:
space:
mode:
Diffstat (limited to 'reduced_system_layer.c')
-rwxr-xr-xreduced_system_layer.c113
1 files changed, 90 insertions, 23 deletions
diff --git a/reduced_system_layer.c b/reduced_system_layer.c
index 9c1e67e..7b18780 100755
--- a/reduced_system_layer.c
+++ b/reduced_system_layer.c
@@ -33,17 +33,37 @@
#/ - Conway's Game of Life
#/ - Julia Set
#/ - Labyrinth
-#/ - Landscape
-#/ - Features
-#/ - Sound
-#/ - Clipboard daemon
+#/ - Chat
+#/ - Graphics
+#/ - Vector math
+#/ - UI
+#/ - Wayland
+#/ - Windows graphics
+#/ - Sound
+#/ - ALSA -- https://www.alsa-project.org/alsa-doc/alsa-lib/_2test_2pcm_min_8c-example.html
+#/ - Windows audio
+#/ - WebAssembly audio
+#/ - Networking
+#/ - Windows sockets
+#/ - Web Sockets
#/
-#/ ALSA
-#/ https://www.alsa-project.org/alsa-doc/alsa-lib/_2test_2pcm_min_8c-example.html
+#/ Done
+#/
+#/ - Examples
+#/ - Echo
+#/ - UI
+#/ - Particles
+#/ - Graph
+#/ - Graphics
+#/ - Font
+#/ - X11
+#/ - WebAssembly
+#/ - Networking
+#/ - Unix UDP sockets
#/
#/ ----------------------------------------------------------------
#/
-#/ (C) 2024 Mitya Selivanov <guattari.tech>, MIT License
+#/ (C) 2024 Mitya Selivanov <guattari.tech>
#/
#/ ================================================================
#/
@@ -344,9 +364,21 @@ extern Platform platform;
#ifdef __wasm__
i32 main(i32 argc, c8 **argv);
-f64 sqrt(f64 x);
f64 floor(f64 x);
f64 ceil(f64 x);
+f64 sqrt(f64 x);
+f64 pow(f64 x, f64 y);
+f64 log(f64 x);
+f64 log2(f64 x);
+f64 log10(f64 x);
+f64 exp(f64 x);
+f64 sin(f64 x);
+f64 cos(f64 x);
+f64 tan(f64 x);
+f64 asin(f64 x);
+f64 acos(f64 x);
+f64 atan(f64 x);
+f64 atan2(f64 y, f64 x);
#ifndef NULL
#define NULL ((void *) 0)
@@ -546,7 +578,6 @@ void p_sleep_for(i64 duration) {
#include <stdio.h>
#include <string.h>
-#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <netinet/in.h>
@@ -655,8 +686,15 @@ 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) {
- assert(slot < MAX_NUM_SOCKETS);
- assert(address.protocol == IPv4_UDP || address.protocol == IPv6_UDP);
+ if (slot >= MAX_NUM_SOCKETS) {
+ fprintf(stderr, "%s:%d, %s: Invalid slot %d.\n", __FILE__, __LINE__, __FUNCTION__, (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__, __FUNCTION__, (i32) (u32) address.protocol);
+ return 0;
+ }
if (!sockets_open(slot, address, local_port))
return 0;
@@ -690,7 +728,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, "ERROR: recvfrom failed (errno %d)\n", errno);
+ fprintf(stderr, "%s:%d, %s: recvfrom failed (errno %d).\n", __FILE__, __LINE__, __FUNCTION__, errno);
return 0;
}
@@ -711,8 +749,15 @@ 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) {
- assert(slot < MAX_NUM_SOCKETS);
- assert(address.protocol == IPv4_UDP || address.protocol == IPv6_UDP);
+ if (slot >= MAX_NUM_SOCKETS) {
+ fprintf(stderr, "%s:%d, %s: Invalid slot %d.\n", __FILE__, __LINE__, __FUNCTION__, (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__, __FUNCTION__, (i32) (u32) address.protocol);
+ return 0;
+ }
IP_Address local_address = address;
local_address.port = 0;
@@ -757,7 +802,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, "ERROR: sendto failed (errno %d)\n", errno);
+ fprintf(stderr, "%s:%d, %s: sendto failed (errno %d).\n", __FILE__, __LINE__, __FUNCTION__, errno);
return 0;
}
@@ -797,7 +842,11 @@ static Atom _target = None;
void p_init(void) {
_display = XOpenDisplay(NULL);
- assert(_display != NULL);
+
+ if (_display == NULL) {
+ fprintf(stderr, "%s:%d, %s: XOpenDisplay failed.\n", __FILE__, __LINE__, __FUNCTION__);
+ return;
+ }
_key_table[XKeysymToKeycode(_display, XK_Left)] = KEY_LEFT;
_key_table[XKeysymToKeycode(_display, XK_Right)] = KEY_RIGHT;
@@ -906,7 +955,11 @@ void p_init(void) {
Visual *visual = DefaultVisual(_display, screen);
_gc = DefaultGC(_display, screen);
- assert(_gc != NULL);
+
+ if (_gc == NULL) {
+ fprintf(stderr, "%s:%d, %s: DefaultGC failed.\n", __FILE__, __LINE__, __FUNCTION__);
+ return;
+ }
XSetGraphicsExposures(_display, _gc, False);
@@ -924,10 +977,18 @@ void p_init(void) {
_window = XCreateWindow(_display, XDefaultRootWindow(_display), x, y, platform.frame_width, platform.frame_height, 0, depth, InputOutput, visual, CWEventMask, &(XSetWindowAttributes) { .event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask | VisibilityChangeMask | FocusChangeMask | StructureNotifyMask | SubstructureNotifyMask, });
_im = XOpenIM(_display, NULL, NULL, NULL);
- assert(_im != NULL);
+
+ if (_im == NULL) {
+ fprintf(stderr, "%s:%d, %s: XOpenIM failed.\n", __FILE__, __LINE__, __FUNCTION__);
+ return;
+ }
_ic = XCreateIC(_im, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, _window, NULL);
- assert(_ic != NULL);
+
+ if (_ic == NULL) {
+ fprintf(stderr, "%s:%d, %s: XCreateIC failed.\n", __FILE__, __LINE__, __FUNCTION__);
+ return;
+ }
platform.pixels = _buffer;
platform.input = _input;
@@ -986,6 +1047,9 @@ void p_cleanup(void) {
}
i32 p_handle_events(void) {
+ if (_display == NULL)
+ return 0;
+
i32 num_events = 0;
memset(platform.key_pressed, 0, sizeof platform.key_pressed);
@@ -1264,7 +1328,7 @@ i32 p_wait_events(void) {
do {
num_events = p_handle_events();
- sched_yield();
+ usleep(0);
} while (num_events == 0);
return num_events;
@@ -1279,7 +1343,10 @@ void p_render_frame(void) {
}
void p_clipboard_write(i64 size, c8 *data) {
- assert(size <= MAX_CLIPBOARD_SIZE);
+ if (size > MAX_CLIPBOARD_SIZE) {
+ fprintf(stderr, "%s:%d, %s: Size is too big %lld.\n", __FILE__, __LINE__, __FUNCTION__, size);
+ return;
+ }
XSetSelectionOwner(_display, _clipboard, _window, CurrentTime);
@@ -1300,14 +1367,14 @@ void p_clipboard_write(i64 size, c8 *data) {
void p_handle_audio(i64 time_elapsed) {
(void) time_elapsed;
- assert(0);
+ // TODO
}
void p_queue_sound(i64 delay, i64 num_samples, f32 *samples) {
(void) delay;
(void) num_samples;
(void) samples;
- assert(0);
+ // TODO
}
#endif // defined(__linux__)