From 4045be32b18fa4f61d5d28e9a93c86749ea0c98e Mon Sep 17 00:00:00 2001 From: Radegast Date: Wed, 21 May 2014 22:31:18 +0100 Subject: [PATCH] Replaced procfs dependent functions with native calls on OS X. #87 --- neo/CMakeLists.txt | 27 +- neo/framework/Licensee.h | 8 +- neo/idlib/Str.h | 5 - .../platform_linux.cpp} | 211 --------------- neo/sys/posix/posix_main.cpp | 241 ++++++++++++++---- neo/sys/posix/posix_public.h | 2 + 6 files changed, 211 insertions(+), 283 deletions(-) rename neo/sys/{linux/linux_main.cpp => posix/platform_linux.cpp} (75%) diff --git a/neo/CMakeLists.txt b/neo/CMakeLists.txt index 0d7fcf40..ec8fdd15 100644 --- a/neo/CMakeLists.txt +++ b/neo/CMakeLists.txt @@ -827,7 +827,7 @@ set(WIN32_RESOURCES file(GLOB POSIX_INCLUDES sys/posix/*.h) -file(GLOB POSIX_SOURCES sys/posix/*.cpp) +file(GLOB POSIX_SOURCES sys/posix/*.cpp sys/posix/*.mm) file(GLOB COMMON_INCLUDES sys/common/*.h) file(GLOB COMMON_SOURCES sys/common/*.cpp) @@ -1342,11 +1342,16 @@ else() include_directories(${SDL_INCLUDE_DIR}) set(SDLx_LIBRARY ${SDL_LIBRARY}) endif() - + + if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + list(REMOVE_ITEM POSIX_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/sys/posix/platform_linux.cpp) + else() + list(REMOVE_ITEM POSIX_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/sys/posix/platform_osx.mm) + endif() + list(APPEND RBDOOM3_SOURCES ${POSIX_INCLUDES} ${POSIX_SOURCES} - ${SDL_INCLUDES} ${SDL_SOURCES} - sys/linux/linux_main.cpp) + ${SDL_INCLUDES} ${SDL_SOURCES}) if(OPENAL) find_package(OpenAL REQUIRED) @@ -1443,15 +1448,15 @@ else() add_dependencies(rm_precomp_header RBDoom3BFG) - IF(NOT "${CMAKE_SYSTEM}" MATCHES "Darwin") - set(RT_LIBRARY rt) - ENDIF() + if(NOT WIN32) + if(NOT "${CMAKE_SYSTEM}" MATCHES "Darwin") + set(RT_LIBRARY rt) + endif() - IF(NOT "${CMAKE_SYSTEM}" MATCHES "FreeBSD") - set(DL_LIBRARY dl) - ENDIF() + if(NOT "${CMAKE_SYSTEM}" MATCHES "FreeBSD") + set(DL_LIBRARY dl) + endif() - IF(NOT WIN32) target_link_libraries(RBDoom3BFG idlib ${OPENGL_LIBRARIES} diff --git a/neo/framework/Licensee.h b/neo/framework/Licensee.h index 106d0bed..3ce42e85 100644 --- a/neo/framework/Licensee.h +++ b/neo/framework/Licensee.h @@ -62,8 +62,12 @@ If you have questions concerning this license or the applicable additional terms #define WIN32_FAKE_WINDOW_CLASS_NAME "D3BFG_WGL_FAKE" // RB begin -// Linux info -#define LINUX_DEFAULT_PATH "/usr/local/games/rbdoom3-bfg" +// Default base path (used only if none could be found) +#ifdef __APPLE__ +#define DEFAULT_BASEPATH "/Applications/rbdoom3-bfg.app/Contents/Resources" +#else +#define DEFAULT_BASEPATH "/usr/local/games/rbdoom3-bfg" +#endif // RB end diff --git a/neo/idlib/Str.h b/neo/idlib/Str.h index 331ad139..72bb40c1 100644 --- a/neo/idlib/Str.h +++ b/neo/idlib/Str.h @@ -83,11 +83,6 @@ enum utf8Encoding_t #define _strnicmp use_idStr_Icmpn #define _memicmp use_idStr_Icmpn -#define snprintf use_idStr_snPrintf -#define _snprintf use_idStr_snPrintf -#define vsnprintf use_idStr_vsnPrintf -#define _vsnprintf use_idStr_vsnPrintf - class idVec4; #ifndef FILE_HASH_SIZE diff --git a/neo/sys/linux/linux_main.cpp b/neo/sys/posix/platform_linux.cpp similarity index 75% rename from neo/sys/linux/linux_main.cpp rename to neo/sys/posix/platform_linux.cpp index 65007432..9cd947dd 100644 --- a/neo/sys/linux/linux_main.cpp +++ b/neo/sys/posix/platform_linux.cpp @@ -45,18 +45,10 @@ static const char** cmdargv = NULL; static int cmdargc = 0; // DG end -#if defined(__APPLE__) -#include -#endif - #ifdef ID_MCHECK #include #endif -static idStr basepath; -static idStr savepath; - - /* ============== Sys_DefaultSavePath @@ -92,70 +84,6 @@ const char* Sys_EXEPath() return buf; } -/* -================ -Sys_DefaultBasePath - -Get the default base path -- binary image path -- current directory -- hardcoded -Try to be intelligent: if there is no BASE_GAMEDIR, try the next path -================ -*/ -const char* Sys_DefaultBasePath() -{ - struct stat st; - idStr testbase; - basepath = Sys_EXEPath(); - if( basepath.Length() ) - { - basepath.StripFilename(); - testbase = basepath; - testbase += "/"; - testbase += BASE_GAMEDIR; - if( stat( testbase.c_str(), &st ) != -1 && S_ISDIR( st.st_mode ) ) - { - return basepath.c_str(); - } - else - { - common->Printf( "no '%s' directory in exe path %s, skipping\n", BASE_GAMEDIR, basepath.c_str() ); - } - } - if( basepath != Posix_Cwd() ) - { - basepath = Posix_Cwd(); - testbase = basepath; - testbase += "/"; - testbase += BASE_GAMEDIR; - if( stat( testbase.c_str(), &st ) != -1 && S_ISDIR( st.st_mode ) ) - { - return basepath.c_str(); - } - else - { - common->Printf( "no '%s' directory in cwd path %s, skipping\n", BASE_GAMEDIR, basepath.c_str() ); - } - } - common->Printf( "WARNING: using hardcoded default base path\n" ); - return LINUX_DEFAULT_PATH; -} - - - -/* -=============== -Sys_Shutdown -=============== -*/ -void Sys_Shutdown() -{ - basepath.Clear(); - savepath.Clear(); - Posix_Shutdown(); -} - /* =============== Sys_GetProcessorId @@ -176,73 +104,6 @@ const char* Sys_GetProcessorString() return "generic"; } -/* -=============== -Sys_FPU_EnableExceptions -=============== -*/ -//void Sys_FPU_EnableExceptions( int exceptions ) -//{ -//} - -/* -=============== -Sys_FPE_handler -=============== -*/ -void Sys_FPE_handler( int signum, siginfo_t* info, void* context ) -{ - assert( signum == SIGFPE ); - Sys_Printf( "FPE\n" ); -} - -/* -=============== -Sys_GetClockticks -=============== -*/ -double Sys_GetClockTicks() -{ -#if defined( __i386__ ) - unsigned long lo, hi; - - __asm__ __volatile__( - "push %%ebx\n" \ - "xor %%eax,%%eax\n" \ - "cpuid\n" \ - "rdtsc\n" \ - "mov %%eax,%0\n" \ - "mov %%edx,%1\n" \ - "pop %%ebx\n" - : "=r"( lo ), "=r"( hi ) ); - return ( double ) lo + ( double ) 0xFFFFFFFF * hi; -#else -//#error unsupported CPU -// RB begin - struct timespec now; - - clock_gettime( CLOCK_MONOTONIC, &now ); - - return now.tv_sec * 1000000000LL + now.tv_nsec; -// RB end -#endif -} - -/* -=============== -MeasureClockTicks -=============== -*/ -double MeasureClockTicks() -{ - double t0, t1; - - t0 = Sys_GetClockTicks( ); - Sys_Sleep( 1000 ); - t1 = Sys_GetClockTicks( ); - return t1 - t0; -} - /* =============== Sys_ClockTicksPerSecond @@ -417,23 +278,6 @@ returns in megabytes int Sys_GetSystemRam() { int mb; - -#if defined(__APPLE__) - int mib[2]; - mib[0] = CTL_HW; - mib[1] = HW_MEMSIZE; - int64_t size = 0; - size_t len = sizeof( size ); - if( sysctl( mib, 2, &size, &len, NULL, 0 ) == 0 ) - { - mb = size / ( 1024 * 1024 ); - mb = ( mb + 8 ) & ~15; - return mb; - } - - common->Printf( "GetSystemRam: sysctl HW_MEMSIZE failed\n" ); - return 512; -#else long count, page_size; count = sysconf( _SC_PHYS_PAGES ); @@ -452,11 +296,8 @@ int Sys_GetSystemRam() // round to the nearest 16Mb mb = ( mb + 8 ) & ~15; return mb; -#endif } - - /* ================== Sys_DoStartProcess @@ -532,58 +373,6 @@ void Sys_DoStartProcess( const char* exeName, bool dofork ) } } -/* -================= -Sys_OpenURL -================= -*/ -void idSysLocal::OpenURL( const char* url, bool quit ) -{ - const char* script_path; - idFile* script_file; - char cmdline[ 1024 ]; - - static bool quit_spamguard = false; - - if( quit_spamguard ) - { - common->DPrintf( "Sys_OpenURL: already in a doexit sequence, ignoring %s\n", url ); - return; - } - - common->Printf( "Open URL: %s\n", url ); - // opening an URL on *nix can mean a lot of things .. - // just spawn a script instead of deciding for the user :-) - - // look in the savepath first, then in the basepath - script_path = fileSystem->BuildOSPath( cvarSystem->GetCVarString( "fs_savepath" ), "", "openurl.sh" ); - script_file = fileSystem->OpenExplicitFileRead( script_path ); - if( !script_file ) - { - script_path = fileSystem->BuildOSPath( cvarSystem->GetCVarString( "fs_basepath" ), "", "openurl.sh" ); - script_file = fileSystem->OpenExplicitFileRead( script_path ); - } - if( !script_file ) - { - common->Printf( "Can't find URL script 'openurl.sh' in either savepath or basepath\n" ); - common->Printf( "OpenURL '%s' failed\n", url ); - return; - } - fileSystem->CloseFile( script_file ); - - // if we are going to quit, only accept a single URL before quitting and spawning the script - if( quit ) - { - quit_spamguard = true; - } - - common->Printf( "URL script: %s\n", script_path ); - - // StartProcess is going to execute a system() call with that - hence the & - idStr::snPrintf( cmdline, 1024, "%s '%s' &", script_path, url ); - sys->StartProcess( cmdline, quit ); -} - /* ================== Sys_DoPreferences diff --git a/neo/sys/posix/posix_main.cpp b/neo/sys/posix/posix_main.cpp index 71a6a7ca..21269096 100644 --- a/neo/sys/posix/posix_main.cpp +++ b/neo/sys/posix/posix_main.cpp @@ -49,12 +49,6 @@ If you have questions concerning this license or the applicable additional terms #include #endif -#if defined(__APPLE__) -#include -#include -#include -#endif - #include // RB end @@ -63,6 +57,9 @@ If you have questions concerning this license or the applicable additional terms #define MAX_OSPATH 256 #define COMMAND_HISTORY 64 +static idStr basepath; +static idStr savepath; + static int input_hide = 0; idEditField input_field; @@ -71,7 +68,7 @@ static char input_ret[256]; static idStr history[ COMMAND_HISTORY ]; // cycle buffer static int history_count = 0; // buffer fill up static int history_start = 0; // current history start -static int history_current = 0; // goes back in history +static int history_current = 0; // goes back in history idEditField history_backup; // the base edit line // terminal support @@ -179,6 +176,85 @@ void Sys_Quit() Posix_Exit( EXIT_SUCCESS ); } +/* +=============== +Sys_Shutdown +=============== +*/ +void Sys_Shutdown() +{ + basepath.Clear(); + savepath.Clear(); + Posix_Shutdown(); +} + +/* +=============== +Sys_FPU_EnableExceptions +=============== +*/ +//void Sys_FPU_EnableExceptions( int exceptions ) +//{ +//} + +/* +=============== +Sys_FPE_handler +=============== +*/ +void Sys_FPE_handler( int signum, siginfo_t* info, void* context ) +{ + assert( signum == SIGFPE ); + Sys_Printf( "FPE\n" ); +} + +/* +=============== +Sys_GetClockticks +=============== +*/ +double Sys_GetClockTicks() +{ +#if defined( __i386__ ) + unsigned long lo, hi; + + __asm__ __volatile__( + "push %%ebx\n" \ + "xor %%eax,%%eax\n" \ + "cpuid\n" \ + "rdtsc\n" \ + "mov %%eax,%0\n" \ + "mov %%edx,%1\n" \ + "pop %%ebx\n" + : "=r"( lo ), "=r"( hi ) ); + return ( double ) lo + ( double ) 0xFFFFFFFF * hi; +#else +//#error unsupported CPU +// RB begin + struct timespec now; + + clock_gettime( CLOCK_MONOTONIC, &now ); + + return now.tv_sec * 1000000000LL + now.tv_nsec; +// RB end +#endif +} + +/* +=============== +MeasureClockTicks +=============== +*/ +double MeasureClockTicks() +{ + double t0, t1; + + t0 = Sys_GetClockTicks( ); + Sys_Sleep( 1000 ); + t1 = Sys_GetClockTicks( ); + return t1 - t0; +} + /* ================ Sys_Milliseconds @@ -196,53 +272,6 @@ Sys_Milliseconds #define D3_CLOCK_TO_USE CLOCK_MONOTONIC #endif -#ifdef __APPLE__ -// OS X doesn't have clock_gettime() -int clock_gettime( clk_id_t clock, struct timespec* tp ) -{ - switch( clock ) - { - case CLOCK_MONOTONIC_RAW: - case CLOCK_MONOTONIC: - { - clock_serv_t clock_ref; - mach_timespec_t tm; - host_name_port_t self = mach_host_self(); - memset( &tm, 0, sizeof( tm ) ); - if( KERN_SUCCESS != host_get_clock_service( self, SYSTEM_CLOCK, &clock_ref ) ) - { - mach_port_deallocate( mach_task_self(), self ); - return -1; - } - if( KERN_SUCCESS != clock_get_time( clock_ref, &tm ) ) - { - mach_port_deallocate( mach_task_self(), self ); - return -1; - } - mach_port_deallocate( mach_task_self(), self ); - mach_port_deallocate( mach_task_self(), clock_ref ); - tp->tv_sec = tm.tv_sec; - tp->tv_nsec = tm.tv_nsec; - break; - } - - case CLOCK_REALTIME: - default: - { - struct timeval now; - if( KERN_SUCCESS != gettimeofday( &now, NULL ) ) - { - return -1; - } - tp->tv_sec = now.tv_sec; - tp->tv_nsec = now.tv_usec * 1000; - break; - } - } - return 0; -} -#endif // __APPLE__ - // RB: changed long to int unsigned int sys_timeBase = 0; // RB end @@ -338,6 +367,56 @@ uint64 Sys_Microseconds() #endif } +/* +================ +Sys_DefaultBasePath + +Get the default base path +- binary image path +- current directory +- hardcoded +Try to be intelligent: if there is no BASE_GAMEDIR, try the next path +================ +*/ +const char* Sys_DefaultBasePath() +{ + struct stat st; + idStr testbase; + basepath = Sys_EXEPath(); + if( basepath.Length() ) + { + basepath.StripFilename(); + testbase = basepath; + testbase += "/"; + testbase += BASE_GAMEDIR; + if( stat( testbase.c_str(), &st ) != -1 && S_ISDIR( st.st_mode ) ) + { + return basepath.c_str(); + } + else + { + common->Printf( "no '%s' directory in exe path %s, skipping\n", BASE_GAMEDIR, basepath.c_str() ); + } + } + if( basepath != Posix_Cwd() ) + { + basepath = Posix_Cwd(); + testbase = basepath; + testbase += "/"; + testbase += BASE_GAMEDIR; + if( stat( testbase.c_str(), &st ) != -1 && S_ISDIR( st.st_mode ) ) + { + return basepath.c_str(); + } + else + { + common->Printf( "no '%s' directory in cwd path %s, skipping\n", BASE_GAMEDIR, basepath.c_str() ); + } + } + common->Printf( "WARNING: using hardcoded default base path %s\n", DEFAULT_BASEPATH ); + return DEFAULT_BASEPATH; +} + /* ================ Sys_Mkdir @@ -1513,3 +1592,57 @@ void Sys_SetLanguageFromSystem() { sys_lang.SetString( Sys_DefaultLanguage() ); } + +/* +================= +Sys_OpenURL +================= +*/ +void idSysLocal::OpenURL( const char* url, bool quit ) +{ + const char* script_path; + idFile* script_file; + char cmdline[ 1024 ]; + + static bool quit_spamguard = false; + + if( quit_spamguard ) + { + common->DPrintf( "Sys_OpenURL: already in a doexit sequence, ignoring %s\n", url ); + return; + } + + common->Printf( "Open URL: %s\n", url ); + // opening an URL on *nix can mean a lot of things .. + // just spawn a script instead of deciding for the user :-) + + // look in the savepath first, then in the basepath + script_path = fileSystem->BuildOSPath( cvarSystem->GetCVarString( "fs_savepath" ), "", "openurl.sh" ); + script_file = fileSystem->OpenExplicitFileRead( script_path ); + if( !script_file ) + { + script_path = fileSystem->BuildOSPath( cvarSystem->GetCVarString( "fs_basepath" ), "", "openurl.sh" ); + script_file = fileSystem->OpenExplicitFileRead( script_path ); + } + if( !script_file ) + { + common->Printf( "Can't find URL script 'openurl.sh' in either savepath or basepath\n" ); + common->Printf( "OpenURL '%s' failed\n", url ); + return; + } + fileSystem->CloseFile( script_file ); + + // if we are going to quit, only accept a single URL before quitting and spawning the script + if( quit ) + { + quit_spamguard = true; + } + + common->Printf( "URL script: %s\n", script_path ); + + // StartProcess is going to execute a system() call with that - hence the & + idStr::snPrintf( cmdline, 1024, "%s '%s' &", script_path, url ); + sys->StartProcess( cmdline, quit ); +} + + diff --git a/neo/sys/posix/posix_public.h b/neo/sys/posix/posix_public.h index 5c6911f5..c0a9a05e 100644 --- a/neo/sys/posix/posix_public.h +++ b/neo/sys/posix/posix_public.h @@ -59,6 +59,8 @@ void Sys_DoStartProcess( const char* exeName, bool dofork = true ); // if not f char* Posix_ConsoleInput(); +double MeasureClockTicks(); + #ifdef __APPLE__ enum clk_id_t { CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_MONOTONIC_RAW }; int clock_gettime( clk_id_t clock, struct timespec* tp );