diff --git a/platform_all/4ed_shared_init_logic.cpp b/platform_all/4ed_shared_init_logic.cpp index 3ac737ec..f447e33e 100644 --- a/platform_all/4ed_shared_init_logic.cpp +++ b/platform_all/4ed_shared_init_logic.cpp @@ -43,5 +43,26 @@ system_memory_init(){ } } +internal void +load_app_code(){ + App_Get_Functions *get_funcs = 0; + + if (system_load_library(&libraries.app_code, "4ed_app")){ + get_funcs = (App_Get_Functions*)system_get_proc(&libraries.app_code, "app_get_functions"); + } + else{ + char msg[] = "Could not load '4ed_app." DLL "'. This file should be in the same directory as the main '4ed' executable."; + system_error_box(msg); + } + + if (get_funcs != 0){ + app = get_funcs(); + } + else{ + char msg[] = "Failed to get application code from '4ed_app." DLL "'."; + system_error_box(msg); + } +} + // BOTTOM diff --git a/platform_all/4ed_standard_libraries.cpp b/platform_all/4ed_standard_libraries.cpp new file mode 100644 index 00000000..1842ad0f --- /dev/null +++ b/platform_all/4ed_standard_libraries.cpp @@ -0,0 +1,17 @@ +/* + * Mr. 4th Dimention - Allen Webster + * + * 18.07.2017 + * + * Shared libraries struct. + * + */ + +// TOP + +struct Libraries{ + Library app_code; +}; + +// BOTTOM + diff --git a/platform_linux/linux_4ed.cpp b/platform_linux/linux_4ed.cpp index 8ae72102..69cdca09 100644 --- a/platform_linux/linux_4ed.cpp +++ b/platform_linux/linux_4ed.cpp @@ -119,7 +119,13 @@ struct Linux_Coroutine { // internal void LinuxStringDup(String*, void*, size_t); -internal void LinuxToggleFullscreen(Display*, Window); + +//////////////////////////////// + +#include "linux_4ed_libraries.cpp" +#include "4ed_standard_libraries.cpp" + +//////////////////////////////// struct Linux_Vars{ Display *XDisplay; @@ -168,14 +174,13 @@ struct Linux_Vars{ b32 hide_cursor; Cursor hidden_cursor; - void *app_code; + Library app_code; void *custom; sem_t thread_semaphore; i32 dpi_x, dpi_y; - App_Functions app; Custom_API custom_api; b32 vsync; @@ -190,6 +195,7 @@ global Render_Target target; global System_Functions sysfunc; global Application_Memory memory_vars; global Plat_Settings plat_settings; +global App_Functions app; //////////////////////////////// @@ -205,6 +211,7 @@ linux_set_icon(Display* d, Window w){ //////////////////////////////// #define SLASH '/' +#define DLL "so" internal sem_t* handle_sem(Plat_Handle h){ @@ -534,31 +541,6 @@ Sys_CLI_End_Update_Sig(system_cli_end_update){ // Linux init functions // -internal b32 -LinuxLoadAppCode(String* base_dir){ - b32 result = 0; - App_Get_Functions *get_funcs = 0; - - if (!sysshared_to_binary_path(base_dir, "4ed_app.so")){ - return 0; - } - - linuxvars.app_code = dlopen(base_dir->str, RTLD_LAZY); - if (linuxvars.app_code){ - get_funcs = (App_Get_Functions*) - dlsym(linuxvars.app_code, "app_get_functions"); - } else { - LOGF("dlopen failed: %s\n", dlerror()); - } - - if (get_funcs){ - result = 1; - linuxvars.app = get_funcs(); - } - - return(result); -} - internal void LinuxLoadRenderCode(){ target.push_clip = draw_push_clip; @@ -1658,14 +1640,7 @@ main(int argc, char **argv){ // System & Memory init // - char base_dir_mem[PATH_MAX]; - String base_dir = make_fixed_width_string(base_dir_mem); - - if (!LinuxLoadAppCode(&base_dir)){ - char msg[] = "Could not load '4ed_app.so'. This file should be in the same directory as the main '4ed' executable."; - system_error_box(msg); - } - + load_app_code(); link_system_code(&sysfunc); LinuxLoadRenderCode(); @@ -1694,7 +1669,7 @@ main(int argc, char **argv){ i32 *file_count; i32 output_size; - output_size = linuxvars.app.read_command_line(&sysfunc, &memory_vars, current_directory, &plat_settings, &files, &file_count, clparams); + output_size = app.read_command_line(&sysfunc, &memory_vars, current_directory, &plat_settings, &files, &file_count, clparams); if (output_size > 0){ LOGF("%.*s", output_size, (char*)memory_vars.target_memory); @@ -1711,6 +1686,9 @@ main(int argc, char **argv){ #ifdef FRED_SUPER + char base_dir_mem[PATH_MAX]; + String base_dir = make_fixed_width_string(base_dir_mem); + char *custom_file_default = "custom_4coder.so"; sysshared_to_binary_path(&base_dir, custom_file_default); custom_file_default = base_dir.str; @@ -1914,7 +1892,7 @@ main(int argc, char **argv){ XAddConnectionWatch(linuxvars.XDisplay, &LinuxX11ConnectionWatch, NULL); - linuxvars.app.init(&sysfunc, &target, &memory_vars, linuxvars.clipboard_contents, current_directory, linuxvars.custom_api); + app.init(&sysfunc, &target, &memory_vars, linuxvars.clipboard_contents, current_directory, linuxvars.custom_api); LinuxResizeTarget(window_width, window_height); @@ -2008,7 +1986,7 @@ main(int argc, char **argv){ b32 keep_running = linuxvars.keep_running; - linuxvars.app.step(&sysfunc, &target, &memory_vars, &linuxvars.input, &result, clparams); + app.step(&sysfunc, &target, &memory_vars, &linuxvars.input, &result, clparams); if (result.perform_kill){ break; diff --git a/platform_linux/linux_4ed_libraries.cpp b/platform_linux/linux_4ed_libraries.cpp new file mode 100644 index 00000000..a4530283 --- /dev/null +++ b/platform_linux/linux_4ed_libraries.cpp @@ -0,0 +1,53 @@ +/* + * Mr. 4th Dimention - Allen Webster + * + * 18.07.2017 + * + * Linux library wrapper. + * + */ + +// TOP + +struct Library{ + void *lib; +}; + +internal b32 +system_load_library(Library *library, char *name){ + String extension = file_extension(make_string_slowly(name)); + char space[4096]; + if (!match(extension, "so")){ + String full_name = make_fixed_width_string(space); + append(&full_name, name); + append(&full_name, ".so"); + terminate_with_null(&full_name); + name = space; + } + + char path_space[4096]; + i32 size = sysfunc.get_4ed_path(path_space, sizeof(path_space)); + String full_path = make_string(path_space, size, sizeof(path_space)); + append(&full_path, "/"); + append(&full_path, name); + terminate_with_null(&full_path); + + library->lib = dlopen(path, RTLD_LAZY); + b32 success = (library->lib != 0); + return(success); +} + +internal void* +system_get_proc(Library *library, char *name){ + void *result = dlsym(library->lib, name); + return(result); +} + +internal void +system_free_library(Library *library){ + dlclose(library->lib); + library->lib = 0; +} + +// BOTTOM + diff --git a/platform_linux/linux_font.cpp b/platform_linux/linux_font.cpp index 115aff03..e54b051d 100644 --- a/platform_linux/linux_font.cpp +++ b/platform_linux/linux_font.cpp @@ -51,12 +51,13 @@ linux_get_sys_font(char* name, i32 pt_size){ FcPatternDestroy(pat); - if(!result){ + if (!result){ char space[1024]; String str = make_fixed_width_string(space); - if(sysshared_to_binary_path(&str, name)){ + if (sysshared_to_binary_path(&str, name)){ result = strdup(space); - } else { + } + else{ result = strdup(name); } } diff --git a/platform_win32/win32_4ed.cpp b/platform_win32/win32_4ed.cpp index 0c9a16e0..4e956334 100644 --- a/platform_win32/win32_4ed.cpp +++ b/platform_win32/win32_4ed.cpp @@ -34,7 +34,6 @@ # include "4coder_API/style.h" # define FSTRING_IMPLEMENTATION -# define FSTRING_C # include "4coder_lib/4coder_string.h" # include "4coder_lib/4coder_mem.h" @@ -112,10 +111,15 @@ struct Win32_Coroutine{ i32 done; }; +//////////////////////////////// + +#include "win32_4ed_libraries.cpp" +#include "4ed_standard_libraries.cpp" + +//////////////////////////////// + struct Win32_Vars{ - App_Functions app; Custom_API custom_api; - HMODULE app_code; HMODULE custom; Win32_Coroutine coroutine_data[18]; @@ -159,6 +163,9 @@ global System_Functions sysfunc; global Application_Memory memory_vars; global Plat_Settings plat_settings; +global Libraries libraries; +global App_Functions app; + //////////////////////////////// #include "win32_error_box.cpp" @@ -166,6 +173,7 @@ global Plat_Settings plat_settings; //////////////////////////////// #define SLASH '\\' +#define DLL "dll" internal HANDLE handle_type(Plat_Handle h){ @@ -566,24 +574,6 @@ Sys_CLI_End_Update_Sig(system_cli_end_update){ // Linkage to Custom and Application // -internal b32 -Win32LoadAppCode(){ - b32 result = false; - App_Get_Functions *get_funcs = 0; - - win32vars.app_code = LoadLibraryA("4ed_app.dll"); - if (win32vars.app_code){ - get_funcs = (App_Get_Functions*)GetProcAddress(win32vars.app_code, "app_get_functions"); - } - - if (get_funcs){ - result = true; - win32vars.app = get_funcs(); - } - - return(result); -} - #include "4ed_font_data.h" #include "4ed_system_shared.cpp" @@ -1105,10 +1095,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdS // System and Application Layer Linkage // - if (!Win32LoadAppCode()){ - exit(1); - } - + load_app_code(); link_system_code(&sysfunc); Win32LoadRenderCode(); @@ -1138,7 +1125,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdS char **files = 0; i32 *file_count = 0; - win32vars.app.read_command_line(&sysfunc, &memory_vars, current_directory, &plat_settings, &files, &file_count, clparams); + app.read_command_line(&sysfunc, &memory_vars, current_directory, &plat_settings, &files, &file_count, clparams); sysshared_filter_real_files(files, file_count); LOG("Loaded system code, read command line.\n"); @@ -1306,7 +1293,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdS // LOG("Initializing application variables\n"); - win32vars.app.init(&sysfunc, &target, &memory_vars, win32vars.clipboard_contents, current_directory, win32vars.custom_api); + app.init(&sysfunc, &target, &memory_vars, win32vars.clipboard_contents, current_directory, win32vars.custom_api); system_memory_free(current_directory.str, 0); @@ -1501,7 +1488,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdS win32vars.send_exit_signal = false; } - win32vars.app.step(&sysfunc, &target, &memory_vars, &input, &result, clparams); + app.step(&sysfunc, &target, &memory_vars, &input, &result, clparams); if (result.perform_kill){ keep_running = false; diff --git a/platform_win32/win32_4ed_libraries.cpp b/platform_win32/win32_4ed_libraries.cpp new file mode 100644 index 00000000..df59f0e6 --- /dev/null +++ b/platform_win32/win32_4ed_libraries.cpp @@ -0,0 +1,46 @@ +/* + * Mr. 4th Dimention - Allen Webster + * + * 18.07.2017 + * + * Win32 library wrapper. + * + */ + +// TOP + +struct Library{ + HMODULE lib; +}; + +internal b32 +system_load_library(Library *library, char *name){ + String extension = file_extension(make_string_slowly(name)); + char space[4096]; + if (!match(extension, "dll")){ + String full_name = make_fixed_width_string(space); + append(&full_name, name); + append(&full_name, ".dll"); + terminate_with_null(&full_name); + name = space; + } + + library->lib = LoadLibraryA(name); + b32 success = (library->lib != 0); + return(success); +} + +internal void* +system_get_proc(Library *library, char *name){ + void *result = GetProcAddress(library->lib, name); + return(result); +} + +internal void +system_free_library(Library *library){ + FreeLibrary(library->lib); + library->lib = 0; +} + +// BOTTOM +