From 3e3aff040e88ca4162d25b346c9198e23ef56f87 Mon Sep 17 00:00:00 2001 From: mittorn Date: Sat, 8 Feb 2020 23:00:39 +0700 Subject: [PATCH] platform: add Platfrom_Init, Platform_Shutdown, Platform_MouseMove move platform-specific code from host.c --- engine/client/input.c | 17 ++- engine/common/host.c | 175 +++++++++--------------------- engine/common/sys_con.c | 12 ++ engine/common/system.h | 1 - engine/platform/android/android.c | 11 +- engine/platform/linux/in_evdev.c | 6 + engine/platform/platform.h | 4 + engine/platform/posix/sys_posix.c | 63 ++++++++++- engine/platform/sdl/sys_sdl.c | 20 ++++ engine/platform/win32/sys_win.c | 13 +++ 10 files changed, 181 insertions(+), 141 deletions(-) diff --git a/engine/client/input.c b/engine/client/input.c index 5851b0ed..adf540bc 100644 --- a/engine/client/input.c +++ b/engine/client/input.c @@ -570,6 +570,7 @@ void IN_CollectInput( float *forward, float *side, float *pitch, float *yaw, qbo if( includeMouse ) { #if XASH_INPUT == INPUT_SDL + /// TODO: check if we may move this to platform if( includeSdlMouse ) { int x, y; @@ -577,16 +578,12 @@ void IN_CollectInput( float *forward, float *side, float *pitch, float *yaw, qbo *pitch += y * m_pitch->value; *yaw -= x * m_yaw->value; } -#endif // INPUT_SDL - -#if XASH_INPUT == INPUT_ANDROID - { - float x, y; - Android_MouseMove( &x, &y ); - *pitch += y * m_pitch->value; - *yaw -= x * m_yaw->value; - } -#endif // ANDROID +#else + float x, y; + Platform_MouseMove( &x, &y ); + *pitch += y * m_pitch->value; + *yaw -= x * m_yaw->value; +#endif // SDL #ifdef XASH_USE_EVDEV IN_EvdevMove( yaw, pitch ); diff --git a/engine/common/host.c b/engine/common/host.c index 6f440a11..a9f1758f 100644 --- a/engine/common/host.c +++ b/engine/common/host.c @@ -677,96 +677,6 @@ void Host_InitCommon( int argc, char **argv, const char *progname, qboolean bCha if( !Sys_CheckParm( "-noch" ) ) Sys_SetupCrashHandler(); - // to be accessed later - if( ( host.daemonized = Sys_CheckParm( "-daemonize" ) ) ) - { -#if defined(_POSIX_VERSION) && !defined(XASH_MOBILE_PLATFORM) - pid_t daemon; - - daemon = fork(); - - if( daemon < 0 ) - { - Host_Error( "fork() failed: %s\n", strerror( errno ) ); - } - - if( daemon > 0 ) - { - // parent - Con_Reportf( "Child pid: %i\n", daemon ); - exit( 0 ); - } - else - { - // don't be closed by parent - if( setsid() < 0 ) - { - Host_Error( "setsid() failed: %s\n", strerror( errno ) ); - } - - // set permissions - umask( 0 ); - - // engine will still use stdin/stdout, - // so just redirect them to /dev/null - close( STDIN_FILENO ); - close( STDOUT_FILENO ); - close( STDERR_FILENO ); - open("/dev/null", O_RDONLY); // becomes stdin - open("/dev/null", O_RDWR); // stdout - open("/dev/null", O_RDWR); // stderr - - // fallthrough - } -#elif defined(XASH_MOBILE_PLATFORM) - Sys_Error( "Can't run in background on mobile platforms!" ); -#else - Sys_Error( "Daemonize not supported on this platform!" ); -#endif - } - - if( ( baseDir = getenv( "XASH3D_BASEDIR" ) ) ) - { - Q_strncpy( host.rootdir, baseDir, sizeof(host.rootdir) ); - } - else - { -#if TARGET_OS_IOS - const char *IOS_GetDocsDir(); - Q_strncpy( host.rootdir, IOS_GetDocsDir(), sizeof(host.rootdir) ); -#elif XASH_SDL == 2 - char *szBasePath; - - if( !( szBasePath = SDL_GetBasePath() ) ) - Sys_Error( "couldn't determine current directory: %s", SDL_GetError() ); - Q_strncpy( host.rootdir, szBasePath, sizeof( host.rootdir ) ); - SDL_free( szBasePath ); -#else - if( !getcwd( host.rootdir, sizeof(host.rootdir) ) ) - { - Sys_Error( "couldn't determine current directory: %s", strerror( errno ) ); - host.rootdir[0] = 0; - } -#endif - } - - if( host.rootdir[Q_strlen( host.rootdir ) - 1] == '/' ) - host.rootdir[Q_strlen( host.rootdir ) - 1] = 0; - - // get readonly root. The order is: check for arg, then env. - // if still not got it, rodir is disabled. - host.rodir[0] = 0; - if( !Sys_GetParmFromCmdLine( "-rodir", host.rodir )) - { - char *roDir; - - if(( roDir = getenv( "XASH3D_RODIR" ))) - Q_strncpy( host.rodir, roDir, sizeof( host.rodir )); - } - - if( host.rodir[0] && host.rodir[Q_strlen( host.rodir ) - 1] == '/' ) - host.rodir[Q_strlen( host.rodir ) - 1] = 0; - host.enabledll = !Sys_CheckParm( "-nodll" ); host.change_game = bChangeGame; @@ -809,30 +719,6 @@ void Host_InitCommon( int argc, char **argv, const char *progname, qboolean bCha } #endif -#ifdef XASH_SDL -#ifndef SDL_INIT_EVENTS -#define SDL_INIT_EVENTS 0 -#endif - - if( SDL_Init( SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS ) ) - { - Sys_Warn( "SDL_Init failed: %s", SDL_GetError() ); - host.type = HOST_DEDICATED; - } - -#if XASH_SDL == 2 - SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0"); - SDL_StopTextInput(); -#endif // XASH_SDL == 2 -#endif // XASH_SDL - - if ( !host.rootdir[0] || SetCurrentDirectory( host.rootdir ) != 0) - Con_Reportf( "%s is working directory now\n", host.rootdir ); - else - Sys_Error( "Changing working directory to %s failed.\n", host.rootdir ); - - Sys_InitLog(); - // set default gamedir if( progname[0] == '#' ) progname++; @@ -856,13 +742,10 @@ void Host_InitCommon( int argc, char **argv, const char *progname, qboolean bCha // member console allowing host.allow_console_init = host.allow_console; -#ifdef _WIN32 - Wcon_CreateConsole(); // system console used by dedicated server or show fatal errors -#endif // timeBeginPeriod( 1 ); // a1ba: Do we need this? // NOTE: this message couldn't be passed into game console but it doesn't matter - Con_Reportf( "Sys_LoadLibrary: Loading xash.dll - ok\n" ); +// Con_Reportf( "Sys_LoadLibrary: Loading xash.dll - ok\n" ); // get default screen res VID_InitDefaultResolution(); @@ -889,6 +772,58 @@ void Host_InitCommon( int argc, char **argv, const char *progname, qboolean bCha } Con_Init(); // early console running to catch all the messages + + Platform_Init(); + + if( ( baseDir = getenv( "XASH3D_BASEDIR" ) ) ) + { + Q_strncpy( host.rootdir, baseDir, sizeof(host.rootdir) ); + } + else + { +#if TARGET_OS_IOS + const char *IOS_GetDocsDir(); + Q_strncpy( host.rootdir, IOS_GetDocsDir(), sizeof(host.rootdir) ); +#elif XASH_SDL == 2 + char *szBasePath; + + if( !( szBasePath = SDL_GetBasePath() ) ) + Sys_Error( "couldn't determine current directory: %s", SDL_GetError() ); + Q_strncpy( host.rootdir, szBasePath, sizeof( host.rootdir ) ); + SDL_free( szBasePath ); +#else + if( !getcwd( host.rootdir, sizeof(host.rootdir) ) ) + { + Sys_Error( "couldn't determine current directory: %s", strerror( errno ) ); + host.rootdir[0] = 0; + } +#endif + } + + if( host.rootdir[Q_strlen( host.rootdir ) - 1] == '/' ) + host.rootdir[Q_strlen( host.rootdir ) - 1] = 0; + + // get readonly root. The order is: check for arg, then env. + // if still not got it, rodir is disabled. + host.rodir[0] = 0; + if( !Sys_GetParmFromCmdLine( "-rodir", host.rodir )) + { + char *roDir; + + if(( roDir = getenv( "XASH3D_RODIR" ))) + Q_strncpy( host.rodir, roDir, sizeof( host.rodir )); + } + + if( host.rodir[0] && host.rodir[Q_strlen( host.rodir ) - 1] == '/' ) + host.rodir[Q_strlen( host.rodir ) - 1] = 0; + + if ( !host.rootdir[0] || SetCurrentDirectory( host.rootdir ) != 0) + Con_Reportf( "%s is working directory now\n", host.rootdir ); + else + Sys_Error( "Changing working directory to %s failed.\n", host.rootdir ); + + Sys_InitLog(); + Cmd_AddCommand( "exec", Host_Exec_f, "execute a script file" ); Cmd_AddCommand( "memlist", Host_MemStats_f, "prints memory pool information" ); @@ -1064,9 +999,7 @@ void EXPORT Host_Shutdown( void ) NET_Shutdown(); HTTP_Shutdown(); Host_FreeCommon(); -#ifdef _WIN32 - Wcon_DestroyConsole(); -#endif + Platform_Shutdown(); // must be last, console uses this Mem_FreePool( &host.mempool ); diff --git a/engine/common/sys_con.c b/engine/common/sys_con.c index 09bb8ebc..0f30ef9f 100644 --- a/engine/common/sys_con.c +++ b/engine/common/sys_con.c @@ -310,3 +310,15 @@ void Con_Reportf( const char *szFmt, ... ) Sys_Print( buffer ); } + + +#if XASH_MESSAGEBOX == MSGBOX_STDERR +void Platform_MessageBox( const char *title, const char *message, qboolean parentMainWindow ) +{ + fprintf( stderr, + "======================================\n" + "%s: %s\n" + "======================================\n", title, message ); +} +#endif + diff --git a/engine/common/system.h b/engine/common/system.h index ee9755c9..2096e6e0 100644 --- a/engine/common/system.h +++ b/engine/common/system.h @@ -86,7 +86,6 @@ int Sys_LogFileNo( void ); #if XASH_WIN32 void Wcon_InitConsoleCommands( void ); void Wcon_ShowConsole( qboolean show ); -void Wcon_Init( void ); void Wcon_CreateConsole( void ); void Wcon_DestroyConsole( void ); void Wcon_DisableInput( void ); diff --git a/engine/platform/android/android.c b/engine/platform/android/android.c index b7f82a78..0019f53f 100644 --- a/engine/platform/android/android.c +++ b/engine/platform/android/android.c @@ -586,16 +586,21 @@ JNIEXPORT jint JNICALL JNI_OnLoad( JavaVM *vm, void *reserved ) /* ======================== -Android_Init +Platform_Init Initialize android-related cvars ======================== */ -void Android_Init( void ) +void Platform_Init( void ) { android_sleep = Cvar_Get( "android_sleep", "1", FCVAR_ARCHIVE, "Enable sleep in background" ); } +void Platform_Shutdown( void ) +{ + +} + /* ======================== Android_EnableTextInput @@ -714,7 +719,7 @@ void Android_SaveID( const char *id ) Android_MouseMove ======================== */ -void Android_MouseMove( float *x, float *y ) +void Platform_MouseMove( float *x, float *y ) { *x = jnimouse.x; *y = jnimouse.y; diff --git a/engine/platform/linux/in_evdev.c b/engine/platform/linux/in_evdev.c index fcf1091e..05501363 100644 --- a/engine/platform/linux/in_evdev.c +++ b/engine/platform/linux/in_evdev.c @@ -439,6 +439,12 @@ void Platform_EnableTextInput( qboolean enable ) evdev.chars = enable; evdev.shift = false; } + +void Platfrom_MouseMove( float *yaw, float *pitch ) +{ + // already catched in IN_EvdevMove +} + #endif void Evdev_Init( void ) diff --git a/engine/platform/platform.h b/engine/platform/platform.h index ab83c8cd..134a2563 100644 --- a/engine/platform/platform.h +++ b/engine/platform/platform.h @@ -28,6 +28,9 @@ GNU General Public License for more details. ============================================================================== */ + +void Platform_Init( void ); +void Platform_Shutdown( void ); double Platform_DoubleTime( void ); void Platform_Sleep( int msec ); void Platform_ShellExecute( const char *path, const char *parms ); @@ -69,6 +72,7 @@ void Platform_RunEvents( void ); void Platform_GetMousePos( int *x, int *y ); void Platform_SetMousePos( int x, int y ); void Platform_PreCreateMove( void ); +void Platform_MouseMove( float *x, float *y ); // Clipboard void Platform_GetClipboardText( char *buffer, size_t size ); void Platform_SetClipboardText( const char *buffer, size_t size ); diff --git a/engine/platform/posix/sys_posix.c b/engine/platform/posix/sys_posix.c index b757f8b0..f766c21c 100644 --- a/engine/platform/posix/sys_posix.c +++ b/engine/platform/posix/sys_posix.c @@ -92,12 +92,63 @@ void Platform_ShellExecute( const char *path, const char *parms ) } #endif // XASH_ANDROID -#if XASH_MESSAGEBOX == MSGBOX_STDERR -void Platform_MessageBox( const char *title, const char *message, qboolean parentMainWindow ) +void Posix_Daemonize( void ) { - fprintf( stderr, - "======================================\n" - "%s: %s\n" - "======================================\n", title, message ); + // to be accessed later + if( ( host.daemonized = Sys_CheckParm( "-daemonize" ) ) ) + { +#if XASH_POSIX && defined(_POSIX_VERSION) && !defined(XASH_MOBILE_PLATFORM) + pid_t daemon; + + daemon = fork(); + + if( daemon < 0 ) + { + Host_Error( "fork() failed: %s\n", strerror( errno ) ); + } + + if( daemon > 0 ) + { + // parent + Con_Reportf( "Child pid: %i\n", daemon ); + exit( 0 ); + } + else + { + // don't be closed by parent + if( setsid() < 0 ) + { + Host_Error( "setsid() failed: %s\n", strerror( errno ) ); + } + + // set permissions + umask( 0 ); + + // engine will still use stdin/stdout, + // so just redirect them to /dev/null + close( STDIN_FILENO ); + close( STDOUT_FILENO ); + close( STDERR_FILENO ); + open("/dev/null", O_RDONLY); // becomes stdin + open("/dev/null", O_RDWR); // stdout + open("/dev/null", O_RDWR); // stderr + + // fallthrough + } +#elif defined(XASH_MOBILE_PLATFORM) + Sys_Error( "Can't run in background on mobile platforms!" ); +#else + Sys_Error( "Daemonize not supported on this platform!" ); +#endif + } + +} + +#if !XASH_SDL && !XASH_ANDROID + +void Platform_Init( void ) +{ + Posix_Daemonize(); } +void Platform_Shutdown( void ) {} #endif diff --git a/engine/platform/sdl/sys_sdl.c b/engine/platform/sdl/sys_sdl.c index 285cfc1b..68bc450e 100644 --- a/engine/platform/sdl/sys_sdl.c +++ b/engine/platform/sdl/sys_sdl.c @@ -44,3 +44,23 @@ void Platform_MessageBox( const char *title, const char *message, qboolean paren SDL_ShowSimpleMessageBox( SDL_MESSAGEBOX_ERROR, title, message, parentMainWindow ? host.hWnd : NULL ); } #endif // XASH_MESSAGEBOX == MSGBOX_SDL +void Posix_Daemonize( void ); +void Platform_Init( void ) +{ +#ifndef SDL_INIT_EVENTS +#define SDL_INIT_EVENTS 0 + + if( SDL_Init( SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS ) ) + { + Sys_Warn( "SDL_Init failed: %s", SDL_GetError() ); + host.type = HOST_DEDICATED; + } + +#if XASH_SDL == 2 + SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0"); + SDL_StopTextInput(); +#endif // XASH_SDL == 2 +#if XASH_POSIX + Posix_Daemonize(); +#endif +} \ No newline at end of file diff --git a/engine/platform/win32/sys_win.c b/engine/platform/win32/sys_win.c index bdf951ac..78132f0a 100644 --- a/engine/platform/win32/sys_win.c +++ b/engine/platform/win32/sys_win.c @@ -59,3 +59,16 @@ void Platform_MessageBox( const char *title, const char *message, qboolean paren MessageBox( parentMainWindow ? host.hWnd : NULL, message, title, MB_OK|MB_SETFOREGROUND|MB_ICONSTOP ); } #endif // XASH_MESSAGEBOX == MSGBOX_WIN32 + +#ifndef XASH_SDL + +void Platform_Init( void ) +{ + Wcon_CreateConsole(); // system console used by dedicated server or show fatal errors + +} +void Platform_Shutdown( void ) +{ + Wcon_DestroyConsole(); +} +#endif \ No newline at end of file