From dc213307a92c8f3e477b7f9f2080a6940a7b2ed9 Mon Sep 17 00:00:00 2001 From: Yuval Dolev Date: Mon, 30 Dec 2019 00:34:53 +0200 Subject: [PATCH] Implemented all system wake up timer functions. --- platform_mac/mac_4ed.mm | 38 +++++++++++-------- platform_mac/mac_4ed_functions.mm | 63 ++++++++++++++++++------------- 2 files changed, 59 insertions(+), 42 deletions(-) diff --git a/platform_mac/mac_4ed.mm b/platform_mac/mac_4ed.mm index 501614b1..9a87b069 100644 --- a/platform_mac/mac_4ed.mm +++ b/platform_mac/mac_4ed.mm @@ -83,13 +83,13 @@ //////////////////////////////// -typedef i32 Win32_Object_Kind; +typedef i32 Mac_Object_Kind; enum{ - Win32ObjectKind_ERROR = 0, - Win32ObjectKind_Timer = 1, - Win32ObjectKind_Thread = 2, - Win32ObjectKind_Mutex = 3, - Win32ObjectKind_CV = 4, + MacObjectKind_ERROR = 0, + MacObjectKind_Timer = 1, + MacObjectKind_Thread = 2, + MacObjectKind_Mutex = 3, + MacObjectKind_CV = 4, }; struct Mac_Object{ @@ -122,7 +122,7 @@ global Render_Target target; //////////////////////////////// function inline Plat_Handle -mac_to_plat_handle(Mac_Object* object){ +mac_to_plat_handle(Mac_Object *object){ Plat_Handle result = *(Plat_Handle*)(&object); return(result); } @@ -135,7 +135,7 @@ mac_to_object(Plat_Handle handle){ function mac_alloc_object(Mac_Object_Kind kind){ - Mac_Object* result = 0; + Mac_Object *result = 0; if (mac_vars.free_mac_objects.next != &mac_vars.free_mac_objects){ result = CastFromMember(Mac_Object, node, mac_vars.free_mac_objects.next); @@ -143,19 +143,19 @@ mac_alloc_object(Mac_Object_Kind kind){ if (!result){ i32 count = 512; - Mac_Object* objects = (Mac_Object*)system_memory_allocate(count * sizeof(Mac_Object), file_name_line_number); + Mac_Object *objects = (Mac_Object*)system_memory_allocate(count * sizeof(Mac_Object), file_name_line_number_lit_u8); - // NOTE(yuval): Link the first chain of the dll to the sentinel + // NOTE(yuval): Link the first node of the dll to the sentinel objects[0].node.prev = &mac_vars.free_mac_objects; mac_vars.free_mac_objects.next = &objects[0].node; - // NOTE(yuval): Link all dll chains to each other + // NOTE(yuval): Link all dll nodes to each other for (i32 chain_index = 1; chain_index < count; chain_index += 1){ objects[chain_index - 1].node.next = &objects[chain_index].node; objects[chain_index].node.prev = &objects[chain_index - 1].node; } - // NOTE(yuval): Link the last chain of the dll to the sentinel + // NOTE(yuval): Link the last node of the dll to the sentinel objects[count - 1].node.next = &mac_vars.free_mac_objects; mac_vars.free_mac_objects.prev = &objects[count - 1].node; @@ -170,6 +170,14 @@ mac_alloc_object(Mac_Object_Kind kind){ return(result); } +function +mac_free_object(Mac_Object *object){ + if (object->node.next != 0){ + dll_remove(&object->node); + } + + dll_insert(&mac_vars.free_mac_objects, &object->node); +} //////////////////////////////// @@ -188,7 +196,7 @@ mac_alloc_object(Mac_Object_Kind kind){ return YES; } -- (void)applicationWillTerminate:(NSNotification *)notification{ +- (void)applicationWillTerminate:(NSNotification*)notification{ } - (NSSize)windowWillResize:(NSWindow*)window toSize:(NSSize)frame_size{ @@ -207,10 +215,10 @@ int main(int arg_count, char **args){ @autoreleasepool{ // NOTE(yuval): Create NSApplication & Delegate - NSApplication* app = [NSApplication sharedApplication]; + NSApplication *app = [NSApplication sharedApplication]; [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; - App_Delegate* app_delegate = [[App_Delegate alloc] init]; + App_Delegate *app_delegate = [[App_Delegate alloc] init]; [app setDelegate:app_delegate]; [NSApp finishLaunching]; diff --git a/platform_mac/mac_4ed_functions.mm b/platform_mac/mac_4ed_functions.mm index a4009db7..b98780d8 100644 --- a/platform_mac/mac_4ed_functions.mm +++ b/platform_mac/mac_4ed_functions.mm @@ -82,7 +82,7 @@ mac_get_file_attributes(struct stat file_stat) { } function inline File_Attributes -mac_file_attributes_from_path(char* path) { +mac_file_attributes_from_path(char *path) { File_Attributes result = {}; struct stat file_stat; @@ -109,7 +109,7 @@ function system_get_file_list_sig(){ File_List result = {}; - u8* c_directory = push_array(arena, u8, directory.size + 1); + u8 *c_directory = push_array(arena, u8, directory.size + 1); block_copy(c_directory, directory.str, directory.size); c_directory[directory.size] = 0; @@ -129,7 +129,7 @@ system_get_file_list_sig(){ continue; } - File_Info* info = push_array(arena, File_Info, 1); + File_Info *info = push_array(arena, File_Info, 1); sll_queue_push(first, last, info); count += 1; @@ -146,8 +146,8 @@ system_get_file_list_sig(){ file_path_size += 1; } - char* file_path = push_array(arena, char, file_path_size + 1); - char* file_path_at = file_path; + char *file_path = push_array(arena, char, file_path_size + 1); + char *file_path_at = file_path; block_copy(file_path_at, directory.str, directory.size); file_path_at += directory.size; @@ -174,7 +174,7 @@ system_get_file_list_sig(){ result.count = count; i32 index = 0; - for (File_Info* node = first; + for (File_Info *node = first; node != 0; node = node->next){ result.infos[index] = node; @@ -189,7 +189,7 @@ function system_quick_file_attributes_sig(){ Temp_Memory temp = begin_temp(scratch); - char* c_file_name = push_array(scratch, char, file_name.size + 1); + char *c_file_name = push_array(scratch, char, file_name.size + 1); block_copy(c_file_name, file_name.str, file_name.size); c_file_name[file_name.size] = 0; @@ -273,9 +273,6 @@ system_save_file_sig(){ i32 fd = open(file_name, O_WRONLY | O_TRUNC | O_CREAT, 00640); if (fd != -1) { - u8* data_str = data.str; - u64 data_size = data.size; - do{ ssize_t bytes_written = write(fd, data.str, data.size); if (bytes_written == -1){ @@ -302,14 +299,14 @@ system_save_file_sig(){ //////////////////////////////// function inline System_Library -mac_to_system_library(void* dl_handle){ +mac_to_system_library(void *dl_handle){ System_Library result = *(System_Library*)(&dl_handle); return(result); } function inline void* mac_to_dl_handle(System_Library system_lib){ - void* result = *(void**)(&system_lib); + void *result = *(void**)(&system_lib); return(result); } @@ -317,13 +314,13 @@ function system_load_library_sig(){ b32 result = false; - void* lib = 0; + void *lib = 0; // NOTE(yuval): Open library handle { Temp_Memory temp = begin_temp(scratch); - char* c_file_name = push_array(scratch, char, file_name.size + 1); + char *c_file_name = push_array(scratch, char, file_name.size + 1); block_copy(c_file_name, file_name.str, file_name.size); c_file_name[file_name.size] = 0; @@ -342,7 +339,7 @@ system_load_library_sig(){ function system_release_library_sig(){ - void* lib = mac_to_dl_handle(handle); + void *lib = mac_to_dl_handle(handle); i32 rc = dlclose(lib); b32 result = (rc == 0); @@ -351,8 +348,8 @@ system_release_library_sig(){ function system_get_proc_sig(){ - void* lib = mac_to_dl_handle(handle); - Void_Func* result = (Void_Func*)dlsym(lib, proc_name); + void *lib = mac_to_dl_handle(handle); + Void_Func *result = (Void_Func*)dlsym(lib, proc_name); return(result); } @@ -364,9 +361,9 @@ system_now_time_sig(){ u64 now = mach_absolute_time(); // NOTE(yuval): Now time nanoseconds conversion - f64 now_nano = (f64)(((f64)now) * - ((f64)mac_vars.timebase_info.numer) / - ((f64)mac_vars.timebase_info.denom)); + f64 now_nano = (f64)((f64)now * + (f64)mac_vars.timebase_info.numer / + (f64)mac_vars.timebase_info.denom); // NOTE(yuval): Conversion to useconds u64 result = (u64)(now_nano * 1.0E-3); @@ -375,24 +372,36 @@ system_now_time_sig(){ function system_wake_up_timer_create_sig(){ - Plat_Handle result = {}; + Mac_Object *object = mac_alloc_object(MacObjectKind_Timer); + dll_insert(&mac_vars.timer_objects, &object->node); - NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval: 0.0 - target: view - selector: @selector(requestDisplay) - userInfo: nil repeats:NO]; + object->timer.timer = nil; + Plat_Handle result = mac_to_plat_handle(object); return(result); } function system_wake_up_timer_release_sig(){ - NotImplemented; + Mac_Object *object = (Mac_Object*)mac_to_object(handle); + if (object->kind == MacObjectKind_Timer){ + if ((object->timer != nil) && [object->timer isValid]) { + [object->timer invalidate]; + mac_free_object(object); + } + } } function system_wake_up_timer_set_sig(){ - NotImplemented; + Mac_Object *object = (Mac_Object*)mac_to_object(handle); + if (object->kind == MacObjectKind_Timer){ + f64 time_seconds = ((f64)time_milliseconds / 1000.0); + object->timer = [NSTimer scheduledTimerWithTimeInterval: time_seconds + target: mac_vars.view + selector: @selector(requestDisplay) + userInfo: nil repeats:NO]; + } } function