2019-12-25 01:17:12 +00:00
|
|
|
/* macOS System/Graphics/Font API Implementations */
|
|
|
|
|
2020-01-12 01:08:07 +00:00
|
|
|
/********************/
|
|
|
|
/* System API */
|
|
|
|
/********************/
|
2019-12-27 21:30:46 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
////////////////////////////////
|
2019-12-24 20:31:24 +00:00
|
|
|
|
|
|
|
function
|
|
|
|
system_get_path_sig(){
|
|
|
|
String_Const_u8 result = {};
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
switch (path_code){
|
|
|
|
case SystemPath_CurrentDirectory:
|
|
|
|
{
|
|
|
|
char *working_dir = getcwd(NULL, 0);
|
|
|
|
u64 working_dir_length = cstring_length(working_dir);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
// TODO(yuval): Maybe use push_string_copy instead
|
|
|
|
u8 *out = push_array(arena, u8, working_dir_length);
|
|
|
|
block_copy(out, working_dir, working_dir_length);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
free(working_dir);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
result = SCu8(out, working_dir_length);
|
|
|
|
} break;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
case SystemPath_Binary:
|
|
|
|
{
|
|
|
|
local_persist b32 has_stashed_4ed_path = false;
|
|
|
|
if (!has_stashed_4ed_path){
|
2020-01-19 03:30:04 +00:00
|
|
|
local_const u32 binary_path_capacity = PATH_MAX;
|
2020-01-08 19:44:47 +00:00
|
|
|
u8 *memory = (u8*)system_memory_allocate(binary_path_capacity, file_name_line_number_lit_u8);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-27 21:59:59 +00:00
|
|
|
pid_t pid = getpid();
|
|
|
|
i32 size = proc_pidpath(pid, memory, binary_path_capacity);
|
2020-01-19 03:30:04 +00:00
|
|
|
Assert(size < binary_path_capacity);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
mac_vars.binary_path = SCu8(memory, size);
|
|
|
|
mac_vars.binary_path = string_remove_last_folder(mac_vars.binary_path);
|
|
|
|
mac_vars.binary_path.str[mac_vars.binary_path.size] = 0;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
has_stashed_4ed_path = true;
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
result = push_string_copy(arena, mac_vars.binary_path);
|
|
|
|
} break;
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_get_canonical_sig(){
|
2019-12-27 21:59:59 +00:00
|
|
|
NSString *path_ns_str =
|
|
|
|
[[NSString alloc] initWithBytes:name.data length:name.size encoding:NSUTF8StringEncoding];
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-27 21:59:59 +00:00
|
|
|
NSString *standardized_path_ns_str = [path_ns_str stringByStandardizingPath];
|
2019-12-28 22:11:34 +00:00
|
|
|
String_Const_u8 standardized_path = SCu8((u8*)[standardized_path_ns_str UTF8String],[standardized_path_ns_str lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-27 21:59:59 +00:00
|
|
|
String_Const_u8 result = push_string_copy(arena, standardized_path);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-27 21:59:59 +00:00
|
|
|
[path_ns_str release];
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 21:30:46 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
2019-12-26 22:16:15 +00:00
|
|
|
function File_Attributes
|
|
|
|
mac_get_file_attributes(struct stat file_stat) {
|
|
|
|
File_Attributes result;
|
|
|
|
result.size = file_stat.st_size;
|
|
|
|
result.last_write_time = file_stat.st_mtimespec.tv_sec;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 22:16:15 +00:00
|
|
|
result.flags = 0;
|
|
|
|
if (S_ISDIR(file_stat.st_mode)) {
|
|
|
|
result.flags |= FileAttribute_IsDirectory;
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 22:16:15 +00:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function inline File_Attributes
|
2019-12-29 22:34:53 +00:00
|
|
|
mac_file_attributes_from_path(char *path) {
|
2019-12-26 22:16:15 +00:00
|
|
|
File_Attributes result = {};
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 22:16:15 +00:00
|
|
|
struct stat file_stat;
|
|
|
|
if (stat(path, &file_stat) == 0){
|
|
|
|
result = mac_get_file_attributes(file_stat);
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 22:16:15 +00:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function inline File_Attributes
|
|
|
|
mac_file_attributes_from_fd(i32 fd) {
|
|
|
|
File_Attributes result = {};
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 22:16:15 +00:00
|
|
|
struct stat file_stat;
|
|
|
|
if (fstat(fd, &file_stat) == 0){
|
|
|
|
result = mac_get_file_attributes(file_stat);
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 22:16:15 +00:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2019-12-24 20:31:24 +00:00
|
|
|
function
|
|
|
|
system_get_file_list_sig(){
|
|
|
|
File_List result = {};
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-29 22:34:53 +00:00
|
|
|
u8 *c_directory = push_array(arena, u8, directory.size + 1);
|
2019-12-26 01:16:34 +00:00
|
|
|
block_copy(c_directory, directory.str, directory.size);
|
|
|
|
c_directory[directory.size] = 0;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 01:16:34 +00:00
|
|
|
DIR *dir = opendir((char*)c_directory);
|
|
|
|
if (dir){
|
|
|
|
File_Info* first = 0;
|
|
|
|
File_Info* last = 0;
|
|
|
|
i32 count = 0;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 01:16:34 +00:00
|
|
|
for (struct dirent *entry = readdir(dir);
|
|
|
|
entry;
|
|
|
|
entry = readdir(dir)){
|
|
|
|
char *c_file_name = entry->d_name;
|
|
|
|
String_Const_u8 file_name = SCu8(c_file_name);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 01:16:34 +00:00
|
|
|
if (string_match(file_name, string_u8_litexpr(".")) || string_match(file_name, string_u8_litexpr(".."))){
|
|
|
|
continue;
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-29 22:34:53 +00:00
|
|
|
File_Info *info = push_array(arena, File_Info, 1);
|
2019-12-26 01:16:34 +00:00
|
|
|
sll_queue_push(first, last, info);
|
|
|
|
count += 1;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 01:16:34 +00:00
|
|
|
info->file_name = push_string_copy(arena, file_name);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 15:54:06 +00:00
|
|
|
// NOTE(yuval): Get file attributes
|
|
|
|
{
|
|
|
|
Temp_Memory temp = begin_temp(arena);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 15:54:06 +00:00
|
|
|
b32 append_slash = false;
|
|
|
|
u64 file_path_size = directory.size + file_name.size;
|
2019-12-26 19:30:13 +00:00
|
|
|
if (string_get_character(directory, directory.size - 1) != '/'){
|
2019-12-26 15:54:06 +00:00
|
|
|
append_slash = true;
|
|
|
|
file_path_size += 1;
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-29 22:34:53 +00:00
|
|
|
char *file_path = push_array(arena, char, file_path_size + 1);
|
|
|
|
char *file_path_at = file_path;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 15:54:06 +00:00
|
|
|
block_copy(file_path_at, directory.str, directory.size);
|
|
|
|
file_path_at += directory.size;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 19:30:13 +00:00
|
|
|
if (append_slash){
|
2019-12-26 15:54:06 +00:00
|
|
|
*file_path_at = '/';
|
|
|
|
file_path_at += 1;
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 15:54:06 +00:00
|
|
|
block_copy(file_path_at, file_name.str, file_name.size);
|
|
|
|
file_path_at += file_name.size;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 15:54:06 +00:00
|
|
|
*file_path_at = 0;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 22:16:15 +00:00
|
|
|
info->attributes = mac_file_attributes_from_path(file_path);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 15:54:06 +00:00
|
|
|
end_temp(temp);
|
|
|
|
}
|
2019-12-26 01:16:34 +00:00
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 19:30:13 +00:00
|
|
|
closedir(dir);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 19:30:13 +00:00
|
|
|
result.infos = push_array(arena, File_Info*, count);
|
|
|
|
result.count = count;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 19:30:13 +00:00
|
|
|
i32 index = 0;
|
2019-12-29 22:34:53 +00:00
|
|
|
for (File_Info *node = first;
|
2019-12-26 19:30:13 +00:00
|
|
|
node != 0;
|
|
|
|
node = node->next){
|
|
|
|
result.infos[index] = node;
|
|
|
|
index += 1;
|
2019-12-26 01:16:34 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_quick_file_attributes_sig(){
|
2019-12-26 22:16:15 +00:00
|
|
|
Temp_Memory temp = begin_temp(scratch);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-29 22:34:53 +00:00
|
|
|
char *c_file_name = push_array(scratch, char, file_name.size + 1);
|
2019-12-26 22:16:15 +00:00
|
|
|
block_copy(c_file_name, file_name.str, file_name.size);
|
|
|
|
c_file_name[file_name.size] = 0;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 22:16:15 +00:00
|
|
|
File_Attributes result = mac_file_attributes_from_path(c_file_name);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 22:16:15 +00:00
|
|
|
end_temp(temp);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 23:54:33 +00:00
|
|
|
function inline Plat_Handle
|
|
|
|
mac_to_plat_handle(i32 fd){
|
|
|
|
Plat_Handle result = *(Plat_Handle*)(&fd);
|
2019-12-29 01:09:28 +00:00
|
|
|
return(result);
|
2019-12-27 23:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function inline i32
|
|
|
|
mac_to_fd(Plat_Handle handle){
|
|
|
|
i32 result = *(i32*)(&handle);
|
2019-12-29 01:09:28 +00:00
|
|
|
return(result);
|
2019-12-27 23:54:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 20:31:24 +00:00
|
|
|
function
|
|
|
|
system_load_handle_sig(){
|
|
|
|
b32 result = false;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 22:16:15 +00:00
|
|
|
i32 fd = open(file_name, O_RDONLY);
|
|
|
|
if ((fd != -1) && (fd != 0)) {
|
2019-12-27 23:54:33 +00:00
|
|
|
*out = mac_to_plat_handle(fd);
|
2019-12-26 22:16:15 +00:00
|
|
|
result = true;
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_load_attributes_sig(){
|
2019-12-27 23:54:33 +00:00
|
|
|
i32 fd = mac_to_fd(handle);
|
2019-12-26 22:16:15 +00:00
|
|
|
File_Attributes result = mac_file_attributes_from_fd(fd);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_load_file_sig(){
|
2019-12-27 23:54:33 +00:00
|
|
|
i32 fd = mac_to_fd(handle);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 22:16:15 +00:00
|
|
|
do{
|
|
|
|
ssize_t bytes_read = read(fd, buffer, size);
|
|
|
|
if (bytes_read == -1){
|
|
|
|
if (errno != EINTR){
|
|
|
|
// NOTE(yuval): An error occured while reading from the file descriptor
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else{
|
|
|
|
size -= bytes_read;
|
|
|
|
buffer += bytes_read;
|
|
|
|
}
|
|
|
|
} while (size > 0);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 22:16:15 +00:00
|
|
|
b32 result = (size == 0);
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_load_close_sig(){
|
2019-12-26 22:16:15 +00:00
|
|
|
b32 result = true;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-27 23:54:33 +00:00
|
|
|
i32 fd = mac_to_fd(handle);
|
2019-12-26 22:16:15 +00:00
|
|
|
if (close(fd) == -1){
|
|
|
|
// NOTE(yuval): An error occured while close the file descriptor
|
|
|
|
result = false;
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_save_file_sig(){
|
|
|
|
File_Attributes result = {};
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 22:16:15 +00:00
|
|
|
i32 fd = open(file_name, O_WRONLY | O_TRUNC | O_CREAT, 00640);
|
|
|
|
if (fd != -1) {
|
|
|
|
do{
|
|
|
|
ssize_t bytes_written = write(fd, data.str, data.size);
|
|
|
|
if (bytes_written == -1){
|
|
|
|
if (errno != EINTR){
|
|
|
|
// NOTE(yuval): An error occured while writing to the file descriptor
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else{
|
|
|
|
data.size -= bytes_written;
|
|
|
|
data.str += bytes_written;
|
|
|
|
}
|
|
|
|
} while (data.size > 0);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 22:16:15 +00:00
|
|
|
if (data.size == 0) {
|
|
|
|
result = mac_file_attributes_from_fd(fd);
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-26 22:16:15 +00:00
|
|
|
close(fd);
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 21:30:46 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
2019-12-27 23:54:33 +00:00
|
|
|
function inline System_Library
|
2019-12-29 22:34:53 +00:00
|
|
|
mac_to_system_library(void *dl_handle){
|
2019-12-27 23:54:33 +00:00
|
|
|
System_Library result = *(System_Library*)(&dl_handle);
|
2019-12-29 01:09:28 +00:00
|
|
|
return(result);
|
2019-12-27 23:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function inline void*
|
|
|
|
mac_to_dl_handle(System_Library system_lib){
|
2019-12-29 22:34:53 +00:00
|
|
|
void *result = *(void**)(&system_lib);
|
2019-12-29 01:09:28 +00:00
|
|
|
return(result);
|
2019-12-27 23:54:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 20:31:24 +00:00
|
|
|
function
|
|
|
|
system_load_library_sig(){
|
|
|
|
b32 result = false;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-29 22:34:53 +00:00
|
|
|
void *lib = 0;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-27 23:54:33 +00:00
|
|
|
// NOTE(yuval): Open library handle
|
|
|
|
{
|
|
|
|
Temp_Memory temp = begin_temp(scratch);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-29 22:34:53 +00:00
|
|
|
char *c_file_name = push_array(scratch, char, file_name.size + 1);
|
2019-12-27 23:54:33 +00:00
|
|
|
block_copy(c_file_name, file_name.str, file_name.size);
|
|
|
|
c_file_name[file_name.size] = 0;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-27 23:54:33 +00:00
|
|
|
lib = dlopen(c_file_name, RTLD_LAZY | RTLD_GLOBAL);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-27 23:54:33 +00:00
|
|
|
end_temp(temp);
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-27 23:54:33 +00:00
|
|
|
if (lib){
|
|
|
|
*out = mac_to_system_library(lib);
|
|
|
|
result = true;
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_release_library_sig(){
|
2019-12-29 22:34:53 +00:00
|
|
|
void *lib = mac_to_dl_handle(handle);
|
2019-12-27 23:54:33 +00:00
|
|
|
i32 rc = dlclose(lib);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-27 23:54:33 +00:00
|
|
|
b32 result = (rc == 0);
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_get_proc_sig(){
|
2019-12-29 22:34:53 +00:00
|
|
|
void *lib = mac_to_dl_handle(handle);
|
|
|
|
Void_Func *result = (Void_Func*)dlsym(lib, proc_name);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 21:30:46 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
2019-12-24 20:31:24 +00:00
|
|
|
function
|
|
|
|
system_now_time_sig(){
|
2019-12-28 22:11:34 +00:00
|
|
|
u64 now = mach_absolute_time();
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-29 01:09:28 +00:00
|
|
|
// NOTE(yuval): Now time nanoseconds conversion
|
2019-12-29 22:34:53 +00:00
|
|
|
f64 now_nano = (f64)((f64)now *
|
2020-01-02 22:09:22 +00:00
|
|
|
((f64)mac_vars.timebase_info.numer /
|
|
|
|
(f64)mac_vars.timebase_info.denom));
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-28 22:11:34 +00:00
|
|
|
// NOTE(yuval): Conversion to useconds
|
2019-12-29 01:09:28 +00:00
|
|
|
u64 result = (u64)(now_nano * 1.0E-3);
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_wake_up_timer_create_sig(){
|
2019-12-29 22:34:53 +00:00
|
|
|
Mac_Object *object = mac_alloc_object(MacObjectKind_Timer);
|
|
|
|
dll_insert(&mac_vars.timer_objects, &object->node);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 11:14:05 +00:00
|
|
|
object->timer = nil;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-29 22:34:53 +00:00
|
|
|
Plat_Handle result = mac_to_plat_handle(object);
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_wake_up_timer_release_sig(){
|
2019-12-31 01:22:50 +00:00
|
|
|
Mac_Object *object = mac_to_object(handle);
|
2019-12-29 22:34:53 +00:00
|
|
|
if (object->kind == MacObjectKind_Timer){
|
2019-12-31 11:14:05 +00:00
|
|
|
if ((object->timer != nil) && [object->timer isValid]) {
|
|
|
|
[object->timer invalidate];
|
2019-12-29 22:34:53 +00:00
|
|
|
mac_free_object(object);
|
|
|
|
}
|
|
|
|
}
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_wake_up_timer_set_sig(){
|
2019-12-31 01:22:50 +00:00
|
|
|
Mac_Object *object = mac_to_object(handle);
|
2019-12-29 22:34:53 +00:00
|
|
|
if (object->kind == MacObjectKind_Timer){
|
|
|
|
f64 time_seconds = ((f64)time_milliseconds / 1000.0);
|
2019-12-31 11:14:05 +00:00
|
|
|
object->timer = [NSTimer scheduledTimerWithTimeInterval:time_seconds
|
2019-12-30 22:04:33 +00:00
|
|
|
target:mac_vars.view
|
2020-01-11 20:19:43 +00:00
|
|
|
selector:@selector(request_display)
|
2019-12-30 22:04:33 +00:00
|
|
|
userInfo:nil repeats:NO];
|
2019-12-29 22:34:53 +00:00
|
|
|
}
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_signal_step_sig(){
|
2020-01-31 05:02:18 +00:00
|
|
|
#if 0
|
2020-01-12 21:16:03 +00:00
|
|
|
if (!mac_vars.step_requested){
|
|
|
|
[NSTimer scheduledTimerWithTimeInterval:0.0
|
|
|
|
target:mac_vars.view
|
|
|
|
selector:@selector(request_display)
|
|
|
|
userInfo:nil repeats:NO];
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-12 21:16:03 +00:00
|
|
|
mac_vars.step_requested = true;
|
|
|
|
}
|
2020-01-31 05:02:18 +00:00
|
|
|
#else
|
|
|
|
mac_vars.step_requested = true;
|
|
|
|
dispatch_async(dispatch_get_main_queue(),
|
|
|
|
^{
|
|
|
|
[NSTimer scheduledTimerWithTimeInterval:0.0
|
|
|
|
target:mac_vars.view
|
|
|
|
selector:@selector(request_display)
|
|
|
|
userInfo:nil repeats:NO];
|
|
|
|
});
|
|
|
|
#endif
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_sleep_sig(){
|
2019-12-30 23:56:12 +00:00
|
|
|
u64 nanoseconds = (microseconds * Thousand(1));
|
|
|
|
u64 abs_sleep_time = (u64)((f64)nanoseconds *
|
2020-01-02 22:09:22 +00:00
|
|
|
((f64)mac_vars.timebase_info.denom /
|
|
|
|
(f64)mac_vars.timebase_info.numer));
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-30 23:56:12 +00:00
|
|
|
u64 now = mach_absolute_time();
|
|
|
|
mach_wait_until(now + abs_sleep_time);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 21:30:46 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
2019-12-24 20:31:24 +00:00
|
|
|
function
|
|
|
|
system_post_clipboard_sig(){
|
2020-01-13 01:20:58 +00:00
|
|
|
Arena *arena = &mac_vars.clip_post_arena;
|
|
|
|
if (arena->base_allocator == 0){
|
|
|
|
*arena = make_arena_system();
|
|
|
|
} else{
|
|
|
|
linalloc_clear(arena);
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 01:20:58 +00:00
|
|
|
mac_vars.clip_post.str = push_array(arena, u8, str.size + 1);
|
|
|
|
if (mac_vars.clip_post.str != 0){
|
|
|
|
block_copy(mac_vars.clip_post.str, str.str, str.size);
|
|
|
|
mac_vars.clip_post.str[str.size] = 0;
|
|
|
|
mac_vars.clip_post.size = str.size;
|
|
|
|
} else{
|
|
|
|
// NOTE(yuval): Failed to allocate buffer for clipboard post
|
|
|
|
}
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 21:30:46 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
2019-12-24 20:31:24 +00:00
|
|
|
function
|
|
|
|
system_cli_call_sig(){
|
|
|
|
b32 result = false;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
int pipe_fds[2];
|
|
|
|
if (pipe(pipe_fds) == -1){
|
|
|
|
perror("system_cli_call: pipe");
|
|
|
|
return(false);
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
pid_t child_pid = fork();
|
|
|
|
if (child_pid == -1){
|
|
|
|
perror("system_cli_call: fork");
|
|
|
|
return(false);
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
enum { PIPE_FD_READ, PIPE_FD_WRITE };
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
if (child_pid == 0){
|
|
|
|
// NOTE(yuval): Child Process
|
|
|
|
close(pipe_fds[PIPE_FD_READ]);
|
|
|
|
dup2(pipe_fds[PIPE_FD_WRITE], STDOUT_FILENO);
|
|
|
|
dup2(pipe_fds[PIPE_FD_WRITE], STDERR_FILENO);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
if (chdir(path) == -1){
|
|
|
|
perror("system_cli_call: chdir");
|
|
|
|
exit(1);
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
char* argv[] = {"sh", "-c", script, 0};
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
if (execv("/bin/sh", argv) == -1){
|
|
|
|
perror("system_cli_call: execv");
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
exit(1);
|
|
|
|
} else{
|
|
|
|
// NOTE(yuval): Parent Process
|
|
|
|
close(pipe_fds[PIPE_FD_WRITE]);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
*(pid_t*)&cli_out->proc = child_pid;
|
|
|
|
*(int*)&cli_out->out_read = pipe_fds[PIPE_FD_READ];
|
|
|
|
*(int*)&cli_out->out_write = pipe_fds[PIPE_FD_WRITE];
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
mac_vars.running_cli += 1;
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
return(true);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_cli_begin_update_sig(){
|
2020-01-13 14:15:25 +00:00
|
|
|
// NOTE(yuval): Nothing to do here.
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_cli_update_step_sig(){
|
2020-01-13 14:15:25 +00:00
|
|
|
int pipe_read_fd = *(int*)&cli->out_read;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
fd_set fds;
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(pipe_read_fd, &fds);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
struct timeval tv = {};
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
size_t space_left = max;
|
|
|
|
char* ptr = dest;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
while (space_left > 0 && (select(pipe_read_fd + 1, &fds, NULL, NULL, &tv) == 1)){
|
|
|
|
ssize_t num = read(pipe_read_fd, ptr, space_left);
|
|
|
|
if (num == -1){
|
|
|
|
perror("system_cli_update_step: read");
|
|
|
|
} else if (num == 0){
|
|
|
|
// NOTE(inso): EOF
|
|
|
|
break;
|
|
|
|
} else{
|
|
|
|
ptr += num;
|
|
|
|
space_left -= num;
|
|
|
|
}
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
*amount = (ptr - dest);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
b32 result = ((ptr - dest) > 0);
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_cli_end_update_sig(){
|
2020-01-13 14:15:25 +00:00
|
|
|
b32 close_me = false;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
pid_t pid = *(pid_t*)&cli->proc;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
int status;
|
|
|
|
if (pid && (waitpid(pid, &status, WNOHANG) > 0)){
|
|
|
|
cli->exit = WEXITSTATUS(status);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
close(*(int*)&cli->out_read);
|
|
|
|
close(*(int*)&cli->out_write);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
mac_vars.running_cli -= 1;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
close_me = true;
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2020-01-13 14:15:25 +00:00
|
|
|
return(close_me);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 21:30:46 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
2019-12-24 20:31:24 +00:00
|
|
|
function
|
|
|
|
system_open_color_picker_sig(){
|
|
|
|
NotImplemented;
|
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_get_screen_scale_factor_sig(){
|
2019-12-30 23:56:12 +00:00
|
|
|
f32 result = mac_vars.screen_scale_factor;
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 21:30:46 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
2019-12-31 01:22:50 +00:00
|
|
|
function void*
|
|
|
|
mac_thread_wrapper(void *ptr){
|
|
|
|
Mac_Object *object = (Mac_Object*)ptr;
|
|
|
|
Thread_Function *proc = object->thread.proc;
|
|
|
|
void *object_ptr = object->thread.ptr;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 01:22:50 +00:00
|
|
|
pthread_mutex_lock(&mac_vars.thread_launch_mutex);
|
|
|
|
{
|
|
|
|
mac_vars.waiting_for_launch = false;
|
|
|
|
pthread_cond_signal(&mac_vars.thread_launch_cv);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&mac_vars.thread_launch_mutex);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 01:22:50 +00:00
|
|
|
proc(object_ptr);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 01:22:50 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2019-12-24 20:31:24 +00:00
|
|
|
function
|
|
|
|
system_thread_launch_sig(){
|
2019-12-31 01:22:50 +00:00
|
|
|
Mac_Object *object = mac_alloc_object(MacObjectKind_Thread);
|
|
|
|
object->thread.proc = proc;
|
|
|
|
object->thread.ptr = ptr;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 01:22:50 +00:00
|
|
|
pthread_mutex_lock(&mac_vars.thread_launch_mutex);
|
|
|
|
{
|
|
|
|
mac_vars.waiting_for_launch = true;
|
|
|
|
pthread_create(&object->thread.thread, 0, mac_thread_wrapper, object);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 01:22:50 +00:00
|
|
|
while (mac_vars.waiting_for_launch){
|
|
|
|
pthread_cond_wait(&mac_vars.thread_launch_cv, &mac_vars.thread_launch_mutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&mac_vars.thread_launch_mutex);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 11:14:05 +00:00
|
|
|
System_Thread result = mac_to_plat_handle(object);
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_thread_join_sig(){
|
2019-12-31 01:22:50 +00:00
|
|
|
Mac_Object *object = mac_to_object(thread);
|
|
|
|
if (object->kind == MacObjectKind_Thread){
|
|
|
|
pthread_join(object->thread.thread, 0);
|
|
|
|
}
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_thread_free_sig(){
|
2019-12-31 01:22:50 +00:00
|
|
|
Mac_Object* object = mac_to_object(thread);
|
|
|
|
if (object->kind == MacObjectKind_Thread){
|
|
|
|
mac_free_object(object);
|
|
|
|
}
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_thread_get_id_sig(){
|
2019-12-31 11:14:05 +00:00
|
|
|
pthread_t id = pthread_self();
|
|
|
|
i32 result = *(i32*)(&id);
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_mutex_make_sig(){
|
2019-12-31 11:14:05 +00:00
|
|
|
Mac_Object *object = mac_alloc_object(MacObjectKind_Mutex);
|
2020-01-19 03:47:01 +00:00
|
|
|
mac_init_recursive_mutex(&object->mutex);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 11:14:05 +00:00
|
|
|
System_Mutex result = mac_to_plat_handle(object);
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_mutex_acquire_sig(){
|
2019-12-31 11:14:05 +00:00
|
|
|
Mac_Object *object = mac_to_object(mutex);
|
|
|
|
if (object->kind == MacObjectKind_Mutex){
|
|
|
|
pthread_mutex_lock(&object->mutex);
|
|
|
|
}
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_mutex_release_sig(){
|
2019-12-31 11:14:05 +00:00
|
|
|
Mac_Object *object = mac_to_object(mutex);
|
|
|
|
if (object->kind == MacObjectKind_Mutex){
|
|
|
|
pthread_mutex_unlock(&object->mutex);
|
|
|
|
}
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_mutex_free_sig(){
|
2019-12-31 11:14:05 +00:00
|
|
|
Mac_Object *object = mac_to_object(mutex);
|
|
|
|
if (object->kind == MacObjectKind_Mutex){
|
|
|
|
pthread_mutex_destroy(&object->mutex);
|
|
|
|
mac_free_object(object);
|
|
|
|
}
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 01:22:50 +00:00
|
|
|
function
|
|
|
|
system_acquire_global_frame_mutex_sig(){
|
2020-02-09 10:22:35 +00:00
|
|
|
if (tctx->kind == ThreadKind_AsyncTasks ||
|
|
|
|
tctx->kind == ThreadKind_MainCoroutine){
|
2019-12-31 11:14:05 +00:00
|
|
|
system_mutex_acquire(mac_vars.global_frame_mutex);
|
|
|
|
}
|
2019-12-31 01:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_release_global_frame_mutex_sig(){
|
2020-02-09 10:22:35 +00:00
|
|
|
if (tctx->kind == ThreadKind_AsyncTasks ||
|
|
|
|
tctx->kind == ThreadKind_MainCoroutine){
|
2019-12-31 11:14:05 +00:00
|
|
|
system_mutex_release(mac_vars.global_frame_mutex);
|
|
|
|
}
|
2019-12-31 01:22:50 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 20:31:24 +00:00
|
|
|
function
|
|
|
|
system_condition_variable_make_sig(){
|
2019-12-31 11:14:05 +00:00
|
|
|
Mac_Object *object = mac_alloc_object(MacObjectKind_CV);
|
|
|
|
pthread_cond_init(&object->cv, 0);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 11:14:05 +00:00
|
|
|
System_Condition_Variable result = mac_to_plat_handle(object);
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_condition_variable_wait_sig(){
|
2019-12-31 11:14:05 +00:00
|
|
|
Mac_Object *object_cv = mac_to_object(cv);
|
|
|
|
Mac_Object *object_mutex = mac_to_object(mutex);
|
|
|
|
if ((object_cv->kind == MacObjectKind_CV) && (object_mutex->kind == MacObjectKind_Mutex)){
|
|
|
|
pthread_cond_wait(&object_cv->cv, &object_mutex->mutex);
|
|
|
|
}
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_condition_variable_signal_sig(){
|
2019-12-31 11:14:05 +00:00
|
|
|
Mac_Object *object = mac_to_object(cv);
|
|
|
|
if (object->kind == MacObjectKind_CV){
|
|
|
|
pthread_cond_signal(&object->cv);
|
|
|
|
}
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_condition_variable_free_sig(){
|
2019-12-31 11:14:05 +00:00
|
|
|
Mac_Object *object = mac_to_object(cv);
|
|
|
|
if (object->kind == MacObjectKind_CV){
|
|
|
|
pthread_cond_destroy(&object->cv);
|
|
|
|
mac_free_object(object);
|
|
|
|
}
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 21:30:46 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
2019-12-31 21:36:12 +00:00
|
|
|
struct Memory_Annotation_Tracker_Node{
|
|
|
|
Memory_Annotation_Tracker_Node *next;
|
|
|
|
Memory_Annotation_Tracker_Node *prev;
|
|
|
|
String_Const_u8 location;
|
|
|
|
u64 size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Memory_Annotation_Tracker{
|
|
|
|
Memory_Annotation_Tracker_Node *first;
|
|
|
|
Memory_Annotation_Tracker_Node *last;
|
|
|
|
i32 count;
|
|
|
|
};
|
|
|
|
|
|
|
|
global Memory_Annotation_Tracker memory_tracker = {};
|
|
|
|
global pthread_mutex_t memory_tracker_mutex;
|
|
|
|
|
|
|
|
global_const u64 ALLOCATION_SIZE_ADJUSTMENT = 64;
|
|
|
|
|
|
|
|
function void*
|
|
|
|
mac_memory_allocate_extended(void *base, u64 size, String_Const_u8 location){
|
|
|
|
u64 adjusted_size = size + ALLOCATION_SIZE_ADJUSTMENT;
|
|
|
|
void *memory = mmap(base, adjusted_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
2020-01-19 03:30:04 +00:00
|
|
|
Assert(memory != MAP_FAILED);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 21:36:12 +00:00
|
|
|
Memory_Annotation_Tracker_Node *node = (Memory_Annotation_Tracker_Node*)memory;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 21:36:12 +00:00
|
|
|
pthread_mutex_lock(&memory_tracker_mutex);
|
|
|
|
{
|
|
|
|
zdll_push_back(memory_tracker.first, memory_tracker.last, node);
|
|
|
|
memory_tracker.count += 1;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&memory_tracker_mutex);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 21:36:12 +00:00
|
|
|
node->location = location;
|
|
|
|
node->size = size;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 21:36:12 +00:00
|
|
|
void* result = (node + 1);
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function void
|
|
|
|
mac_memory_free_extended(void *ptr){
|
|
|
|
Memory_Annotation_Tracker_Node *node = (Memory_Annotation_Tracker_Node*)ptr;
|
|
|
|
node -= 1;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 21:36:12 +00:00
|
|
|
pthread_mutex_lock(&memory_tracker_mutex);
|
|
|
|
{
|
|
|
|
zdll_remove(memory_tracker.first, memory_tracker.last, node);
|
|
|
|
memory_tracker.count -= 1;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&memory_tracker_mutex);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 21:36:12 +00:00
|
|
|
munmap(node, node->size + ALLOCATION_SIZE_ADJUSTMENT);
|
|
|
|
}
|
|
|
|
|
2019-12-24 20:31:24 +00:00
|
|
|
function
|
|
|
|
system_memory_allocate_sig(){
|
2019-12-31 21:36:12 +00:00
|
|
|
void* result = mac_memory_allocate_extended(0, size, location);
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_memory_set_protection_sig(){
|
2019-12-31 21:36:12 +00:00
|
|
|
b32 result = true;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 21:36:12 +00:00
|
|
|
int protect = 0;
|
|
|
|
switch (flags & 0x7){
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
protect = PROT_NONE;
|
|
|
|
} break;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 21:36:12 +00:00
|
|
|
case MemProtect_Read:
|
|
|
|
{
|
|
|
|
protect = PROT_READ;
|
|
|
|
} break;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 21:36:12 +00:00
|
|
|
case MemProtect_Write:
|
|
|
|
case MemProtect_Read | MemProtect_Write:
|
|
|
|
{
|
|
|
|
protect = PROT_READ | PROT_WRITE;
|
|
|
|
} break;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 21:36:12 +00:00
|
|
|
case MemProtect_Execute:
|
|
|
|
{
|
|
|
|
protect = PROT_EXEC;
|
|
|
|
} break;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 21:36:12 +00:00
|
|
|
case MemProtect_Execute | MemProtect_Read:
|
|
|
|
{
|
|
|
|
protect = PROT_READ | PROT_EXEC;
|
|
|
|
} break;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 21:36:12 +00:00
|
|
|
// NOTE(inso): some W^X protection things might be unhappy about this one
|
|
|
|
case MemProtect_Execute | MemProtect_Write:
|
|
|
|
case MemProtect_Execute | MemProtect_Write | MemProtect_Read:
|
|
|
|
{
|
|
|
|
protect = PROT_READ | PROT_WRITE | PROT_EXEC;
|
|
|
|
} break;
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 21:36:12 +00:00
|
|
|
Memory_Annotation_Tracker_Node *node = (Memory_Annotation_Tracker_Node*)ptr;
|
|
|
|
node -= 1;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 21:36:12 +00:00
|
|
|
if(mprotect(node, size, protect) == -1){
|
|
|
|
result = false;
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_memory_free_sig(){
|
2019-12-31 21:36:12 +00:00
|
|
|
mac_memory_free_extended(ptr);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_memory_annotation_sig(){
|
|
|
|
Memory_Annotation result = {};
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 21:36:12 +00:00
|
|
|
pthread_mutex_lock(&memory_tracker_mutex);
|
|
|
|
{
|
|
|
|
for (Memory_Annotation_Tracker_Node *node = memory_tracker.first;
|
|
|
|
node != 0;
|
|
|
|
node = node->next){
|
2020-01-19 03:47:01 +00:00
|
|
|
Memory_Annotation_Node *r_node = push_array(arena, Memory_Annotation_Node, 1);
|
2019-12-31 21:36:12 +00:00
|
|
|
sll_queue_push(result.first, result.last, r_node);
|
|
|
|
result.count += 1;
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 21:36:12 +00:00
|
|
|
r_node->location = node->location;
|
|
|
|
r_node->address = node + 1;
|
|
|
|
r_node->size = node->size;
|
|
|
|
}
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 21:36:12 +00:00
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&memory_tracker_mutex);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 21:30:46 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
2019-12-24 20:31:24 +00:00
|
|
|
function
|
|
|
|
system_show_mouse_cursor_sig(){
|
2019-12-31 22:07:21 +00:00
|
|
|
mac_vars.cursor_show = show;
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_set_fullscreen_sig(){
|
2019-12-31 22:07:21 +00:00
|
|
|
// NOTE(yuval): Read comment in system_set_fullscreen_sig in win32_4ed.cpp
|
|
|
|
mac_vars.do_toggle = (mac_vars.full_screen != full_screen);
|
2020-02-09 10:22:35 +00:00
|
|
|
|
2019-12-31 22:07:21 +00:00
|
|
|
b32 success = true;
|
|
|
|
return(success);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_is_fullscreen_sig(){
|
2019-12-31 22:07:21 +00:00
|
|
|
// NOTE(yuval): Read comment in system_is_fullscreen_sig in win32_4ed.cpp
|
|
|
|
b32 result = (mac_vars.full_screen != mac_vars.do_toggle);
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
system_get_keyboard_modifiers_sig(){
|
2019-12-31 22:07:21 +00:00
|
|
|
Input_Modifier_Set result = copy_modifier_set(arena, &mac_vars.input_chunk.pers.modifiers);
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
2019-12-24 20:31:24 +00:00
|
|
|
}
|
2019-12-25 01:17:12 +00:00
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
2020-01-12 01:08:07 +00:00
|
|
|
/**********************/
|
|
|
|
/* Graphics API */
|
|
|
|
/**********************/
|
2019-12-27 21:30:46 +00:00
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
function
|
|
|
|
graphics_get_texture_sig(){
|
2020-01-09 00:15:25 +00:00
|
|
|
u32 result = renderer->get_texture(renderer, dim, texture_kind);
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
graphics_fill_texture_sig(){
|
2020-01-09 00:15:25 +00:00
|
|
|
b32 result = renderer->fill_texture(renderer, texture_kind, texture, p, dim, data);
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
2020-01-12 01:08:07 +00:00
|
|
|
/******************/
|
|
|
|
/* Font API */
|
|
|
|
/******************/
|
2019-12-27 21:30:46 +00:00
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
2019-12-25 01:17:12 +00:00
|
|
|
function
|
|
|
|
font_make_face_sig(){
|
2020-01-01 23:13:32 +00:00
|
|
|
Face* result = ft__font_make_face(arena, description, scale_factor);
|
2019-12-25 01:17:12 +00:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2020-01-19 20:00:58 +00:00
|
|
|
////////////////////////////////
|