diff --git a/common/port.h b/common/port.h index a21f26b5..104ba6c2 100644 --- a/common/port.h +++ b/common/port.h @@ -47,11 +47,11 @@ GNU General Public License for more details. #if defined(__APPLE__) #include #define OS_LIB_EXT "dylib" - #define OPEN_COMMAND "open" + #define OPEN_COMMAND "open" #include "TargetConditionals.h" #else #define OS_LIB_EXT "so" - #define OPEN_COMMAND "xdg-open" + #define OPEN_COMMAND "xdg-open" #endif #define OS_LIB_PREFIX "lib" diff --git a/engine/common/system.c b/engine/common/system.c index d215103b..109b71a4 100644 --- a/engine/common/system.c +++ b/engine/common/system.c @@ -138,54 +138,6 @@ char *Sys_GetCurrentUser( void ) return "Player"; } -#if (defined(__linux__) && !defined(__ANDROID__)) || defined (__FreeBSD__) || defined (__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__) -static qboolean Sys_FindExecutable( const char *baseName, char *buf, size_t size ) -{ - char *envPath; - char *part; - size_t length; - size_t baseNameLength; - size_t needTrailingSlash; - - if( !baseName || !baseName[0] ) - return false; - - envPath = getenv( "PATH" ); - if( !envPath ) - return false; - - baseNameLength = Q_strlen( baseName ); - while( *envPath ) - { - part = Q_strchr( envPath, ':' ); - if( part ) - length = part - envPath; - else - length = Q_strlen( envPath ); - - if( length > 0 ) - { - needTrailingSlash = ( envPath[length - 1] == '/' ) ? 0 : 1; - if( length + baseNameLength + needTrailingSlash < size ) - { - Q_strncpy( buf, envPath, length + 1 ); - if( needTrailingSlash ) - Q_strcpy( buf + length, "/" ); - Q_strcpy( buf + length + needTrailingSlash, baseName ); - buf[length + needTrailingSlash + baseNameLength] = '\0'; - if( access( buf, X_OK ) == 0 ) - return true; - } - } - - envPath += length; - if( *envPath == ':' ) - envPath++; - } - return false; -} -#endif - /* ================= Sys_ShellExecute @@ -193,32 +145,7 @@ Sys_ShellExecute */ void Sys_ShellExecute( const char *path, const char *parms, int shouldExit ) { -#ifdef _WIN32 - if( !Q_strcmp( path, GENERIC_UPDATE_PAGE ) || !Q_strcmp( path, PLATFORM_UPDATE_PAGE )) - path = DEFAULT_UPDATE_PAGE; - - ShellExecute( NULL, "open", path, parms, NULL, SW_SHOW ); -#elif (defined(__linux__) && !defined (__ANDROID__)) || defined (__FreeBSD__) || defined (__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__) - - if( !Q_strcmp( path, GENERIC_UPDATE_PAGE ) || !Q_strcmp( path, PLATFORM_UPDATE_PAGE )) - path = DEFAULT_UPDATE_PAGE; - - char xdgOpen[128]; - if( Sys_FindExecutable( OPEN_COMMAND, xdgOpen, sizeof( xdgOpen ) ) ) - { - const char *argv[] = {xdgOpen, path, NULL}; - pid_t id = fork( ); - if( id == 0 ) - { - execv( xdgOpen, (char **)argv ); - fprintf( stderr, "error opening %s %s", xdgOpen, path ); - _exit( 1 ); - } - } - else Con_Reportf( S_WARN "Could not find "OPEN_COMMAND" utility\n" ); -#elif defined(__ANDROID__) && !defined(XASH_DEDICATED) - Android_ShellExecute( path, parms ); -#endif + Platform_ShellExecute( path, parms ); if( shouldExit ) Sys_Quit(); diff --git a/engine/platform/platform.h b/engine/platform/platform.h index 65cab49d..687b4239 100644 --- a/engine/platform/platform.h +++ b/engine/platform/platform.h @@ -30,6 +30,7 @@ GNU General Public License for more details. */ double Platform_DoubleTime( void ); void Platform_Sleep( int msec ); +void Platform_ShellExecute( const char *path, const char *parms ); // commented out, as this is an optional feature or maybe implemented in system API directly // see system.c // qboolean Sys_DebuggerPresent( void ); diff --git a/engine/platform/posix/sys_posix.c b/engine/platform/posix/sys_posix.c new file mode 100644 index 00000000..5f6907f2 --- /dev/null +++ b/engine/platform/posix/sys_posix.c @@ -0,0 +1,90 @@ +/* +sys_win.c - posix system utils +Copyright (C) 2019 a1batross + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. +*/ + +#include // fork +#include +#include +#include +#include "platform/platform.h" + +static qboolean Sys_FindExecutable( const char *baseName, char *buf, size_t size ) +{ + char *envPath; + char *part; + size_t length; + size_t baseNameLength; + size_t needTrailingSlash; + + if( !baseName || !baseName[0] ) + return false; + + envPath = getenv( "PATH" ); + if( !envPath ) + return false; + + baseNameLength = Q_strlen( baseName ); + while( *envPath ) + { + part = Q_strchr( envPath, ':' ); + if( part ) + length = part - envPath; + else + length = Q_strlen( envPath ); + + if( length > 0 ) + { + needTrailingSlash = ( envPath[length - 1] == '/' ) ? 0 : 1; + if( length + baseNameLength + needTrailingSlash < size ) + { + Q_strncpy( buf, envPath, length + 1 ); + if( needTrailingSlash ) + Q_strcpy( buf + length, "/" ); + Q_strcpy( buf + length + needTrailingSlash, baseName ); + buf[length + needTrailingSlash + baseNameLength] = '\0'; + if( access( buf, X_OK ) == 0 ) + return true; + } + } + + envPath += length; + if( *envPath == ':' ) + envPath++; + } + return false; +} + +void Platform_ShellExecute( const char *path, const char *parms ) +{ + char xdgOpen[128]; + + if( !Q_strcmp( path, GENERIC_UPDATE_PAGE ) || !Q_strcmp( path, PLATFORM_UPDATE_PAGE )) + path = DEFAULT_UPDATE_PAGE; + + if( Sys_FindExecutable( OPEN_COMMAND, xdgOpen, sizeof( xdgOpen ) ) ) + { + const char *argv[] = { xdgOpen, path, NULL }; + pid_t id = fork( ); + if( id == 0 ) + { + execv( xdgOpen, (char **)argv ); + fprintf( stderr, "error opening %s %s", xdgOpen, path ); + _exit( 1 ); + } + } + else + { + Con_Reportf( S_WARN "Could not find "OPEN_COMMAND" utility\n" ); + } +} diff --git a/engine/platform/win32/sys_win.c b/engine/platform/win32/sys_win.c index 4c3628df..c8f2d1a0 100644 --- a/engine/platform/win32/sys_win.c +++ b/engine/platform/win32/sys_win.c @@ -47,4 +47,10 @@ qboolean Sys_DebuggerPresent( void ) return IsDebuggerPresent(); } +void Platform_ShellExecute( const char *path, const char *parms ) +{ + if( !Q_strcmp( path, GENERIC_UPDATE_PAGE ) || !Q_strcmp( path, PLATFORM_UPDATE_PAGE )) + path = DEFAULT_UPDATE_PAGE; + ShellExecute( NULL, "open", path, parms, NULL, SW_SHOW ); +}