diff --git a/all.h b/all.h index ea83e5e..787c9c9 100644 --- a/all.h +++ b/all.h @@ -288,6 +288,11 @@ typedef u32 b32; #define false 0 // Structs +typedef struct Resulti64 { + bool success; + i64 value; +} Resulti64; + typedef struct Arena { u8* memory; u64 max; @@ -462,6 +467,7 @@ union Range1f32 int poll(struct pollfd *fds, nfds_t nfds, int timeout); #else +# include # include # include # include @@ -598,6 +604,7 @@ fn bool isSimplePrintable(u8 c); ///// OS-wrapped apis void osInit(); +void osSetCtrlCCallback(void (*handler)()); void* osThreadContextGet(); void osThreadContextSet(void* ctx); bool osThreadJoin(Thread handle, u64 endt_us); @@ -620,6 +627,9 @@ fn String osFileRead(Arena* arena, ptr filepath); fn bool osFileCreate(String filename); fn bool osFileCreateWrite(String filename, String data); fn bool osFileWrite(String filename, String data); +fn Resulti64 osFileOpenForWriting(String filename); +fn Resulti64 osFileClose(Resulti64 handle); +fn bool osFileWriteOpenFile(Resulti64 handle, String data); fn void osDebugPrint(bool debug_mode, const char* format, ...); diff --git a/mac_os.c b/mac_os.c index ede0437..f8c294e 100644 --- a/mac_os.c +++ b/mac_os.c @@ -124,6 +124,33 @@ fn bool osFileWrite(String filename, String data) { return result; } +fn Resulti64 osFileOpenForWriting(String filename) { + size_t handle = open((str)filename.bytes, O_WRONLY | O_APPEND, S_IRUSR | S_IRGRP | S_IROTH); + Resulti64 result = { + .success = handle != -1, + .value = (i64)handle, + }; + return result; +} + +fn Resulti64 osFileClose(Resulti64 handle) { + i32 close_result = close((size_t)handle.value); + Resulti64 result = { + .success = close_result != -1, + .value = (i64)close_result, + }; + return result; +} + +fn bool osFileWriteOpenFile(Resulti64 handle, String data) { + assert(handle.success == true); + i32 wrote_this_round = 0; + for (i32 bytes_written = 0; bytes_written < data.length; bytes_written += wrote_this_round) { + wrote_this_round = write((size_t)handle.value, data.bytes + bytes_written, data.length - bytes_written); + if (wrote_this_round == -1) return false; + } + return true; +} // Misc fn void osDebugPrint(bool debug_mode, const char * format, ... ) { diff --git a/unix_os.c b/unix_os.c index c39c96f..ad6b599 100644 --- a/unix_os.c +++ b/unix_os.c @@ -1,11 +1,23 @@ #include "all.h" global pthread_key_t linux_thread_context_key; +global void (*_ctrl_c_handler_fn_ptr)() = NULL; // ThreadContext void osInit() { pthread_key_create(&linux_thread_context_key, NULL); } +void _osGenericSignalHandler(i32 signal) { + if (signal == SIGINT && _ctrl_c_handler_fn_ptr != NULL) { + _ctrl_c_handler_fn_ptr(); + } +} + +void osSetCtrlCCallback(void (*handler)()) { + _ctrl_c_handler_fn_ptr = handler; + signal(SIGINT, _osGenericSignalHandler); +} + void* osThreadContextGet() { return pthread_getspecific(linux_thread_context_key); } diff --git a/win32_os.c b/win32_os.c index 4f1b061..5713792 100644 --- a/win32_os.c +++ b/win32_os.c @@ -5,6 +5,7 @@ static u64 w32_ticks_per_sec = 1; static u32 w32_thread_context_index; +global void (*_ctrl_c_handler_fn_ptr)() = NULL; void osInit() { LARGE_INTEGER perf_freq = {0}; @@ -16,6 +17,19 @@ void osInit() { w32_thread_context_index = TlsAlloc(); } +BOOL WINAPI _osGenericSignalHandler(DWORD event) { + if (event == CTRL_C_EVENT && _ctrl_c_handler_fn_ptr != NULL) { + _ctrl_c_handler_fn_ptr(); + return TRUE; + } + return FALSE; +} + +void osSetCtrlCCallback(void (*handler)()) { + _ctrl_c_handler_fn_ptr = handler; + SetConsoleCtrlHandler(_osGenericSignalHandler, TRUE); +} + void* osThreadContextGet() { return TlsGetValue(w32_thread_context_index); } @@ -90,6 +104,21 @@ fn bool osFileWrite(String filename, String data) { return result; } +fn Resulti64 osFileOpenForWriting(String filename) { + assert(false && "Not Implemented"); + return (Resulti64) {}; +} + +fn Resulti64 osFileClose(Resulti64 handle) { + assert(false && "Not Implemented"); + return (Resulti64) {}; +} + +fn bool osFileWriteOpenFile(Resulti64 handle, String data) { + assert(false && "Not Implemented"); + return false; +} + // Misc fn void osDebugPrint(bool debug_mode, const char * format, ... ) {