Browse Source

platform: move Sys_ShellExecute implementation to platform backends

pull/2/head
Alibek Omarov 6 years ago
parent
commit
e3e2f3afe5
  1. 4
      common/port.h
  2. 75
      engine/common/system.c
  3. 1
      engine/platform/platform.h
  4. 90
      engine/platform/posix/sys_posix.c
  5. 6
      engine/platform/win32/sys_win.c

4
common/port.h

@ -47,11 +47,11 @@ GNU General Public License for more details.
#if defined(__APPLE__) #if defined(__APPLE__)
#include <sys/syslimits.h> #include <sys/syslimits.h>
#define OS_LIB_EXT "dylib" #define OS_LIB_EXT "dylib"
#define OPEN_COMMAND "open" #define OPEN_COMMAND "open"
#include "TargetConditionals.h" #include "TargetConditionals.h"
#else #else
#define OS_LIB_EXT "so" #define OS_LIB_EXT "so"
#define OPEN_COMMAND "xdg-open" #define OPEN_COMMAND "xdg-open"
#endif #endif
#define OS_LIB_PREFIX "lib" #define OS_LIB_PREFIX "lib"

75
engine/common/system.c

@ -138,54 +138,6 @@ char *Sys_GetCurrentUser( void )
return "Player"; 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 Sys_ShellExecute
@ -193,32 +145,7 @@ Sys_ShellExecute
*/ */
void Sys_ShellExecute( const char *path, const char *parms, int shouldExit ) void Sys_ShellExecute( const char *path, const char *parms, int shouldExit )
{ {
#ifdef _WIN32 Platform_ShellExecute( path, 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 );
#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
if( shouldExit ) if( shouldExit )
Sys_Quit(); Sys_Quit();

1
engine/platform/platform.h

@ -30,6 +30,7 @@ GNU General Public License for more details.
*/ */
double Platform_DoubleTime( void ); double Platform_DoubleTime( void );
void Platform_Sleep( int msec ); 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 // commented out, as this is an optional feature or maybe implemented in system API directly
// see system.c // see system.c
// qboolean Sys_DebuggerPresent( void ); // qboolean Sys_DebuggerPresent( void );

90
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 <unistd.h> // fork
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#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" );
}
}

6
engine/platform/win32/sys_win.c

@ -47,4 +47,10 @@ qboolean Sys_DebuggerPresent( void )
return IsDebuggerPresent(); 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 );
}

Loading…
Cancel
Save