From 557bd26913230999ef1ae7b593e62afe85340b14 Mon Sep 17 00:00:00 2001 From: pcmpstri Date: Thu, 28 Aug 2025 08:52:06 +1000 Subject: [PATCH] Add more error handling --- Makefile | 3 +++ src/core/abort.c | 17 +++++++++++++++++ src/core/main.c | 21 +++++++++++++-------- src/core/module.c | 4 ++++ src/modules/drm.c | 4 +++- src/modules/fb.c | 5 +++-- src/twm.h | 17 ++++++++++++----- 7 files changed, 55 insertions(+), 16 deletions(-) create mode 100644 src/core/abort.c diff --git a/Makefile b/Makefile index b684b49..e6472c7 100755 --- a/Makefile +++ b/Makefile @@ -18,3 +18,6 @@ clean: run: all cd build; ./twm drm fb uevent ; cd - + +run_fb: all + cd build; ./twm fb drm uevent ; cd - diff --git a/src/core/abort.c b/src/core/abort.c new file mode 100644 index 0000000..2b1df4f --- /dev/null +++ b/src/core/abort.c @@ -0,0 +1,17 @@ +#include "../twm.h" +#include + +void signal_handler(int signum) { + LOG_ERROR("Recieved signal %d", signum); + twm_abort(); +} + +void setup_signals() { + signal(SIGINT, signal_handler); + signal(SIGSEGV, signal_handler); +} + +void twm_abort() { + unload_modules(loaded_modules); + abort(); +} diff --git a/src/core/main.c b/src/core/main.c index 3ba59b8..64558e3 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1,14 +1,19 @@ #include "../twm.h" #include -int main(int argc, char* argv[]) { - uint64_t modules_mask = 0; - Modules modules = load_modules("modules", argv + 1, argc - 1, &modules_mask); +Modules loaded_modules; - Vec2 (*get_size)() = module_find_func(modules, DISPLAY_GET_SIZE_V1); - void (*flip)() = module_find_func(modules, DISPLAY_FLIP_V1); - void (*put_pixel)(Vec2, Color) = module_find_func(modules, DISPLAY_PUT_PIXEL_V1); - InputEvent (*poll_event)() = module_find_func(modules, INPUT_POLL_V1); +int main(int argc, char* argv[]) { + + setup_signals(); + + uint64_t modules_mask = 0; + loaded_modules = load_modules("modules", argv + 1, argc - 1, &modules_mask); + + Vec2 (*get_size)() = module_find_func(loaded_modules, DISPLAY_GET_SIZE_V1); + void (*flip)() = module_find_func(loaded_modules, DISPLAY_FLIP_V1); + void (*put_pixel)(Vec2, Color) = module_find_func(loaded_modules, DISPLAY_PUT_PIXEL_V1); + InputEvent (*poll_event)() = module_find_func(loaded_modules, INPUT_POLL_V1); Vec2 display_size = get_size(); for (int x = 0; x < display_size.x; x++) { @@ -38,6 +43,6 @@ int main(int argc, char* argv[]) { flip(); } - unload_modules(modules); + unload_modules(loaded_modules); return 0; } diff --git a/src/core/module.c b/src/core/module.c index 3b2f2f5..91cb129 100644 --- a/src/core/module.c +++ b/src/core/module.c @@ -14,6 +14,7 @@ int bitcount(uint64_t v) { } void unload_modules(Modules ms) { + LOG_INFO("Unloading %d modules", ms.count); for (size_t i = 0; i < ms.count; i++) { ms.modules[i].close(); dlclose(ms.modules[i].dll); @@ -21,6 +22,7 @@ void unload_modules(Modules ms) { } Modules load_modules(char* module_dir, char* modules[], size_t num_modules, uint64_t* features) { + LOG_INFO("Loading modules from %s", module_dir); Modules ms = { 0 }; uint64_t needed = BIT(FEATURE_MAX) - 1; @@ -59,6 +61,8 @@ Modules load_modules(char* module_dir, char* modules[], size_t num_modules, uint twm_assert(m.init, "Module doesn't have module_init"); m.close = (ModuleClose)dlsym(m.dll, "module_close"); twm_assert(m.close, "Module doesn't have module_close"); + void (**mod_twm_abort)() = dlsym(m.dll, "twm_abort"); + *mod_twm_abort = twm_abort; if ((m.funcs = (void*)m.init(best_feature)) == NULL) { LOG_WARNING("Module %s couldn't load", dll_name); diff --git a/src/modules/drm.c b/src/modules/drm.c index 5ad0944..2b8ba26 100644 --- a/src/modules/drm.c +++ b/src/modules/drm.c @@ -100,8 +100,10 @@ void display_put_pixel(Vec2 position, Color col) { DRMConnector* conn = data->connectors; while (conn) { if (conn->enabled) { - if (position.x < 0 || position.x >= conn->size.x || position.y < 0 || position.y >= conn->size.y) + if (position.x < 0 || position.x >= conn->size.x || position.y < 0 || position.y >= conn->size.y) { + conn = conn->next; continue; + } uint8_t* row = conn->frame.data + position.y * conn->frame.pitch; row[position.x * 4 + 0] = col.blue; row[position.x * 4 + 1] = col.green; diff --git a/src/modules/fb.c b/src/modules/fb.c index 9954218..9e371a0 100644 --- a/src/modules/fb.c +++ b/src/modules/fb.c @@ -25,13 +25,14 @@ uint32_t get_pixel_color(Color col) { } void display_put_pixel(Vec2 pos, Color col) { + twm_assert(!(pos.x < 0 || pos.x >= data->vscrinfo.xres || pos.y < 0 || pos.y >= data->vscrinfo.yres), "Writing out of bounds to screen"); long pixel_offset = (pos.x + data->vscrinfo.xoffset) * (data->vscrinfo.bits_per_pixel / 8) + (pos.y + data->vscrinfo.yoffset) * data->fscrinfo.line_length; *((uint32_t*)(data->frame_buffer + pixel_offset)) = get_pixel_color(col); } void display_blit(Vec2 pos, Image image) { - for (int x = 0; x < image.size.x; x++) { - for (int y = 0; y < image.size.y; y++) { + for (int y = 0; y < image.size.y; y++) { + for (int x = 0; x < image.size.x; x++) { display_put_pixel((Vec2) { x + pos.x, y + pos.y }, image.data[y][x]); } } diff --git a/src/twm.h b/src/twm.h index e400ad9..e3a7fcf 100644 --- a/src/twm.h +++ b/src/twm.h @@ -24,6 +24,17 @@ typedef enum { #define BIT(n) ((uint64_t)1 << (n)) +#ifdef MODULE_NAME // We're in a module, implement cleanup stuff +void (*twm_abort)(); +#else +void twm_abort(); +extern Modules loaded_modules; +Modules load_modules(char*, char*[], size_t, uint64_t*); +void unload_modules(Modules); +void* module_find_func(Modules ms, ModuleFunc type); +void setup_signals(); +#endif + #ifdef MODULE_NAME #define LOG_WARNING(f, ...) \ do { \ @@ -62,7 +73,7 @@ typedef enum { do { \ if (!(e)) { \ LOG_ERROR("Assertion Failed: %s (%s:%d %s)", m, __FILE__, __LINE__, #e); \ - abort(); \ + twm_abort(); \ } \ } while (0) #define twm_assert_custom(e, m, c) \ @@ -73,10 +84,6 @@ typedef enum { } \ } while (0) -Modules load_modules(char*, char*[], size_t, uint64_t*); -void unload_modules(Modules); -void* module_find_func(Modules ms, ModuleFunc type); - typedef struct { int x; int y;