Add more error handling
This commit is contained in:
parent
d3ad58b8f1
commit
557bd26913
3
Makefile
3
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 -
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
#include "../twm.h"
|
||||
#include <signal.h>
|
||||
|
||||
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();
|
||||
}
|
|
@ -1,14 +1,19 @@
|
|||
#include "../twm.h"
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 x = 0; x < image.size.x; x++) {
|
||||
display_put_pixel((Vec2) { x + pos.x, y + pos.y }, image.data[y][x]);
|
||||
}
|
||||
}
|
||||
|
|
17
src/twm.h
17
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;
|
||||
|
|
Loading…
Reference in New Issue