Implemented all system wake up timer functions.

master
Yuval Dolev 2019-12-30 00:34:53 +02:00
parent 76069e9ac1
commit dc213307a9
2 changed files with 59 additions and 42 deletions

View File

@ -83,13 +83,13 @@
//////////////////////////////// ////////////////////////////////
typedef i32 Win32_Object_Kind; typedef i32 Mac_Object_Kind;
enum{ enum{
Win32ObjectKind_ERROR = 0, MacObjectKind_ERROR = 0,
Win32ObjectKind_Timer = 1, MacObjectKind_Timer = 1,
Win32ObjectKind_Thread = 2, MacObjectKind_Thread = 2,
Win32ObjectKind_Mutex = 3, MacObjectKind_Mutex = 3,
Win32ObjectKind_CV = 4, MacObjectKind_CV = 4,
}; };
struct Mac_Object{ struct Mac_Object{
@ -122,7 +122,7 @@ global Render_Target target;
//////////////////////////////// ////////////////////////////////
function inline Plat_Handle function inline Plat_Handle
mac_to_plat_handle(Mac_Object* object){ mac_to_plat_handle(Mac_Object *object){
Plat_Handle result = *(Plat_Handle*)(&object); Plat_Handle result = *(Plat_Handle*)(&object);
return(result); return(result);
} }
@ -135,7 +135,7 @@ mac_to_object(Plat_Handle handle){
function function
mac_alloc_object(Mac_Object_Kind kind){ 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){ if (mac_vars.free_mac_objects.next != &mac_vars.free_mac_objects){
result = CastFromMember(Mac_Object, node, mac_vars.free_mac_objects.next); result = CastFromMember(Mac_Object, node, mac_vars.free_mac_objects.next);
@ -143,19 +143,19 @@ mac_alloc_object(Mac_Object_Kind kind){
if (!result){ if (!result){
i32 count = 512; 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; objects[0].node.prev = &mac_vars.free_mac_objects;
mac_vars.free_mac_objects.next = &objects[0].node; 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){ for (i32 chain_index = 1; chain_index < count; chain_index += 1){
objects[chain_index - 1].node.next = &objects[chain_index].node; objects[chain_index - 1].node.next = &objects[chain_index].node;
objects[chain_index].node.prev = &objects[chain_index - 1].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; objects[count - 1].node.next = &mac_vars.free_mac_objects;
mac_vars.free_mac_objects.prev = &objects[count - 1].node; mac_vars.free_mac_objects.prev = &objects[count - 1].node;
@ -170,6 +170,14 @@ mac_alloc_object(Mac_Object_Kind kind){
return(result); 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; return YES;
} }
- (void)applicationWillTerminate:(NSNotification *)notification{ - (void)applicationWillTerminate:(NSNotification*)notification{
} }
- (NSSize)windowWillResize:(NSWindow*)window toSize:(NSSize)frame_size{ - (NSSize)windowWillResize:(NSWindow*)window toSize:(NSSize)frame_size{
@ -207,10 +215,10 @@ int
main(int arg_count, char **args){ main(int arg_count, char **args){
@autoreleasepool{ @autoreleasepool{
// NOTE(yuval): Create NSApplication & Delegate // NOTE(yuval): Create NSApplication & Delegate
NSApplication* app = [NSApplication sharedApplication]; NSApplication *app = [NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
App_Delegate* app_delegate = [[App_Delegate alloc] init]; App_Delegate *app_delegate = [[App_Delegate alloc] init];
[app setDelegate:app_delegate]; [app setDelegate:app_delegate];
[NSApp finishLaunching]; [NSApp finishLaunching];

View File

@ -82,7 +82,7 @@ mac_get_file_attributes(struct stat file_stat) {
} }
function inline File_Attributes function inline File_Attributes
mac_file_attributes_from_path(char* path) { mac_file_attributes_from_path(char *path) {
File_Attributes result = {}; File_Attributes result = {};
struct stat file_stat; struct stat file_stat;
@ -109,7 +109,7 @@ function
system_get_file_list_sig(){ system_get_file_list_sig(){
File_List result = {}; 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); block_copy(c_directory, directory.str, directory.size);
c_directory[directory.size] = 0; c_directory[directory.size] = 0;
@ -129,7 +129,7 @@ system_get_file_list_sig(){
continue; 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); sll_queue_push(first, last, info);
count += 1; count += 1;
@ -146,8 +146,8 @@ system_get_file_list_sig(){
file_path_size += 1; file_path_size += 1;
} }
char* file_path = push_array(arena, char, file_path_size + 1); char *file_path = push_array(arena, char, file_path_size + 1);
char* file_path_at = file_path; char *file_path_at = file_path;
block_copy(file_path_at, directory.str, directory.size); block_copy(file_path_at, directory.str, directory.size);
file_path_at += directory.size; file_path_at += directory.size;
@ -174,7 +174,7 @@ system_get_file_list_sig(){
result.count = count; result.count = count;
i32 index = 0; i32 index = 0;
for (File_Info* node = first; for (File_Info *node = first;
node != 0; node != 0;
node = node->next){ node = node->next){
result.infos[index] = node; result.infos[index] = node;
@ -189,7 +189,7 @@ function
system_quick_file_attributes_sig(){ system_quick_file_attributes_sig(){
Temp_Memory temp = begin_temp(scratch); 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); block_copy(c_file_name, file_name.str, file_name.size);
c_file_name[file_name.size] = 0; 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); i32 fd = open(file_name, O_WRONLY | O_TRUNC | O_CREAT, 00640);
if (fd != -1) { if (fd != -1) {
u8* data_str = data.str;
u64 data_size = data.size;
do{ do{
ssize_t bytes_written = write(fd, data.str, data.size); ssize_t bytes_written = write(fd, data.str, data.size);
if (bytes_written == -1){ if (bytes_written == -1){
@ -302,14 +299,14 @@ system_save_file_sig(){
//////////////////////////////// ////////////////////////////////
function inline System_Library 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); System_Library result = *(System_Library*)(&dl_handle);
return(result); return(result);
} }
function inline void* function inline void*
mac_to_dl_handle(System_Library system_lib){ mac_to_dl_handle(System_Library system_lib){
void* result = *(void**)(&system_lib); void *result = *(void**)(&system_lib);
return(result); return(result);
} }
@ -317,13 +314,13 @@ function
system_load_library_sig(){ system_load_library_sig(){
b32 result = false; b32 result = false;
void* lib = 0; void *lib = 0;
// NOTE(yuval): Open library handle // NOTE(yuval): Open library handle
{ {
Temp_Memory temp = begin_temp(scratch); 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); block_copy(c_file_name, file_name.str, file_name.size);
c_file_name[file_name.size] = 0; c_file_name[file_name.size] = 0;
@ -342,7 +339,7 @@ system_load_library_sig(){
function function
system_release_library_sig(){ system_release_library_sig(){
void* lib = mac_to_dl_handle(handle); void *lib = mac_to_dl_handle(handle);
i32 rc = dlclose(lib); i32 rc = dlclose(lib);
b32 result = (rc == 0); b32 result = (rc == 0);
@ -351,8 +348,8 @@ system_release_library_sig(){
function function
system_get_proc_sig(){ system_get_proc_sig(){
void* lib = mac_to_dl_handle(handle); void *lib = mac_to_dl_handle(handle);
Void_Func* result = (Void_Func*)dlsym(lib, proc_name); Void_Func *result = (Void_Func*)dlsym(lib, proc_name);
return(result); return(result);
} }
@ -364,9 +361,9 @@ system_now_time_sig(){
u64 now = mach_absolute_time(); u64 now = mach_absolute_time();
// NOTE(yuval): Now time nanoseconds conversion // NOTE(yuval): Now time nanoseconds conversion
f64 now_nano = (f64)(((f64)now) * f64 now_nano = (f64)((f64)now *
((f64)mac_vars.timebase_info.numer) / (f64)mac_vars.timebase_info.numer /
((f64)mac_vars.timebase_info.denom)); (f64)mac_vars.timebase_info.denom);
// NOTE(yuval): Conversion to useconds // NOTE(yuval): Conversion to useconds
u64 result = (u64)(now_nano * 1.0E-3); u64 result = (u64)(now_nano * 1.0E-3);
@ -375,24 +372,36 @@ system_now_time_sig(){
function function
system_wake_up_timer_create_sig(){ 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 object->timer.timer = nil;
target: view
selector: @selector(requestDisplay)
userInfo: nil repeats:NO];
Plat_Handle result = mac_to_plat_handle(object);
return(result); return(result);
} }
function function
system_wake_up_timer_release_sig(){ 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 function
system_wake_up_timer_set_sig(){ 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 function