mirror of
https://github.com/scp-fs2open/fs2open.github.com.git
synced 2026-11-07 17:52:15 -10:00
* android build support * Update es_compatibility.h * Support reporting flags in android * include pref_path in json v2 * add vulkan headers * shaderc includes * remove headers * force use prebuilt vulkan * fix vulkan inverted screen * fix find sdl3 precompiled android * Update CMakeLists.txt * replace sdl2 android function names for sdl3 versions * change sdl includes in osapi * fix sdl3 includes, again * rename function to sdl3 name * remove unused definition * copy libshaderc_shared.so to output in android * do not look for vulkan-loader in android * revert changes to SDL2.cmake * add comment to vulkan pretransform setup * cleanup maxsize function * rename new android functions in osapi * android trap back button * Remove calculate_max_resize_max_size * touch ui opacity control * change opacity control to int * fix opacity slider * Corrected android working folder fallback and set borderless mode as default * Implement shared helper for JNI calls * Add version tag and sha256 to android prebuilt libs * vulkan android check eSuboptimal * An already requested rebuild also avoids redundant Android surface queries * add cache bool to cmakelist * set string ids
1108 lines
31 KiB
C++
1108 lines
31 KiB
C++
/*
|
|
* Copyright (C) Volition, Inc. 1999. All rights reserved.
|
|
*
|
|
* All source code herein is the property of Volition, Inc. You may not sell
|
|
* or otherwise commercially exploit the source or things you created based on the
|
|
* source.
|
|
*
|
|
*/
|
|
|
|
#include "freespace.h"
|
|
#include "cmdline/cmdline.h"
|
|
#include "gamesequence/gamesequence.h"
|
|
#include "globalincs/pstypes.h"
|
|
#include "parse/parselo.h"
|
|
#include "graphics/2d.h"
|
|
#include "graphics/openxr.h"
|
|
#include "io/joy_ff.h"
|
|
|
|
#include <fcntl.h>
|
|
#include <utf8.h>
|
|
|
|
#include "imgui.h"
|
|
#include "backends/imgui_impl_sdl3.h"
|
|
#include "io/spacemouse.h"
|
|
|
|
#ifdef SCP_UNIX
|
|
#include <sys/stat.h>
|
|
#elif defined(WIN32)
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
#endif
|
|
|
|
#ifdef __ANDROID__
|
|
#include <SDL3/SDL_system.h>
|
|
#include <SDL3/SDL.h>
|
|
#include <jni.h>
|
|
#include "options/Option.h"
|
|
#include "utils/finally.h"
|
|
#endif
|
|
|
|
namespace
|
|
{
|
|
const char* ORGANIZATION_NAME = "HardLightProductions";
|
|
const char* APPLICATION_NAME = "FreeSpaceOpen";
|
|
|
|
bool checkedLegacyMode = false;
|
|
bool legacyMode = false;
|
|
|
|
SCP_vector<std::unique_ptr<os::Viewport>> viewports;
|
|
os::Viewport* mainViewPort = nullptr;
|
|
SDL_Window* mainSDLWindow = nullptr;
|
|
|
|
SCP_string getPreferencesPath()
|
|
{
|
|
static SCP_string preferencesPath;
|
|
|
|
// Lazily initialize the preferences path
|
|
if (preferencesPath.empty()) {
|
|
//Check for a custom path set by env variable
|
|
auto envPreferencesPath = getenv("FSO_PREFERENCES_PATH");
|
|
if (envPreferencesPath != nullptr && strlen(envPreferencesPath) > 0) {
|
|
preferencesPath = SCP_string(envPreferencesPath);
|
|
}
|
|
else {
|
|
char* sdlPreferencesPath = SDL_GetPrefPath(ORGANIZATION_NAME, APPLICATION_NAME);
|
|
if (sdlPreferencesPath != nullptr) {
|
|
preferencesPath = SCP_string(sdlPreferencesPath);
|
|
SDL_free(sdlPreferencesPath);
|
|
sdlPreferencesPath = nullptr;
|
|
}
|
|
else {
|
|
// this section will at least tell the user if something is seriously wrong instead of just crashing without a message or debug log.
|
|
// It may crash later, especially when trying to load sound. But let's let it *try* to run in the current directory at least.
|
|
static bool sdl_is_borked_warning = false;
|
|
if (!sdl_is_borked_warning) {
|
|
ReleaseWarning(LOCATION, "%s\n\nSDL and Windows are unable to get the preferred path for the reason above. "
|
|
"Installing FSO, its executables and DLLs in another non-protected folder may fix the issue.\n\n"
|
|
"You may experience issues if you continue playing, and FSO may crash. Please report this error if it persists.\n\n"
|
|
"Report at www.hard-light.net or the hard-light discord.", SDL_GetError());
|
|
sdl_is_borked_warning = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
// No preferences path, try current directory.
|
|
if (preferencesPath.empty()) {
|
|
Cmdline_portable_mode = true;
|
|
return "." DIR_SEPARATOR_STR;
|
|
}
|
|
|
|
// Ensure path ends with a path separator (slash)
|
|
if (!preferencesPath.empty() && (preferencesPath.back() != DIR_SEPARATOR_CHAR)) {
|
|
preferencesPath += DIR_SEPARATOR_CHAR;
|
|
}
|
|
#ifdef WIN32
|
|
try {
|
|
auto current = preferencesPath.begin();
|
|
const auto prefPathEnd = preferencesPath.end();
|
|
while (current != prefPathEnd) {
|
|
const auto cp = utf8::next(current, prefPathEnd);
|
|
if (cp > 127) {
|
|
// On Windows, we currently do not support Unicode paths so force portable mode let the user
|
|
// know
|
|
const auto invalid_end = current;
|
|
static bool force_portable_warning = false;
|
|
if (!force_portable_warning) {
|
|
utf8::prior(current, preferencesPath.begin());
|
|
ReleaseWarning(LOCATION,
|
|
"Determined the preferences path as \"%s\". That path is not supported since it "
|
|
"contains a Unicode character (%s). Using portable mode. Set -portable_mode in "
|
|
"the commandline to avoid this message in the future.",
|
|
preferencesPath.c_str(), std::string(current, invalid_end).c_str());
|
|
force_portable_warning = true;
|
|
}
|
|
Cmdline_portable_mode = true;
|
|
return "." DIR_SEPARATOR_STR;
|
|
}
|
|
}
|
|
} catch (const std::exception& e) {
|
|
Error(LOCATION, "UTF-8 error while checking the preferences path \"%s\": %s", preferencesPath.c_str(),
|
|
e.what());
|
|
}
|
|
#endif
|
|
}
|
|
|
|
if (!preferencesPath.empty()) {
|
|
return preferencesPath;
|
|
}
|
|
else {
|
|
// No preferences path, try current directory
|
|
return "." DIR_SEPARATOR_STR;
|
|
}
|
|
|
|
}
|
|
|
|
bool fAppActive = false;
|
|
bool window_event_handler(const SDL_Event& e)
|
|
{
|
|
Assertion(mainSDLWindow != nullptr, "This function may only be called with a valid SDL Window.");
|
|
if (os::events::isWindowEvent(e, mainSDLWindow)) {
|
|
switch (e.type) {
|
|
case SDL_EVENT_WINDOW_MINIMIZED:
|
|
case SDL_EVENT_WINDOW_FOCUS_LOST: {
|
|
if (fAppActive) {
|
|
game_pause();
|
|
joy_unacquire_ff();
|
|
|
|
fAppActive = false;
|
|
}
|
|
break;
|
|
}
|
|
|
|
case SDL_EVENT_WINDOW_MAXIMIZED:
|
|
case SDL_EVENT_WINDOW_RESTORED:
|
|
case SDL_EVENT_WINDOW_FOCUS_GAINED: {
|
|
if (!fAppActive) {
|
|
joy_reacquire_ff();
|
|
game_unpause();
|
|
|
|
fAppActive = true;
|
|
}
|
|
break;
|
|
}
|
|
|
|
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
|
|
gameseq_post_event(GS_EVENT_QUIT_GAME);
|
|
break;
|
|
|
|
case SDL_EVENT_WINDOW_RESIZED:
|
|
// FSO does not handle live resizing at all. It will break the internal renderbuffer and swap chain, so prevent this
|
|
if (Fred_running) {
|
|
gr_screen_resize(e.window.data1, e.window.data2);
|
|
}
|
|
break;
|
|
}
|
|
|
|
gr_activate(fAppActive);
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool quit_handler(const SDL_Event& /*e*/) {
|
|
mprintf(("Received quit signal\n"));
|
|
gameseq_post_event(GS_EVENT_QUIT_GAME);
|
|
return true;
|
|
}
|
|
|
|
const char* mapCategory(int category) {
|
|
switch (category) {
|
|
case SDL_LOG_CATEGORY_APPLICATION:
|
|
return "APPLICATION";
|
|
case SDL_LOG_CATEGORY_ERROR:
|
|
return "ERROR";
|
|
case SDL_LOG_CATEGORY_ASSERT:
|
|
return "ASSERT";
|
|
case SDL_LOG_CATEGORY_SYSTEM:
|
|
return "SYSTEM";
|
|
case SDL_LOG_CATEGORY_AUDIO:
|
|
return "AUDIO";
|
|
case SDL_LOG_CATEGORY_VIDEO:
|
|
return "VIDEO";
|
|
case SDL_LOG_CATEGORY_RENDER:
|
|
return "RENDER";
|
|
case SDL_LOG_CATEGORY_INPUT:
|
|
return "INPUT";
|
|
case SDL_LOG_CATEGORY_TEST:
|
|
return "TEST";
|
|
|
|
default:
|
|
return "UNKNOWN";
|
|
}
|
|
}
|
|
|
|
const char* mapPriority(SDL_LogPriority prio)
|
|
{
|
|
switch (prio) {
|
|
case SDL_LOG_PRIORITY_VERBOSE:
|
|
return "VRB";
|
|
case SDL_LOG_PRIORITY_DEBUG:
|
|
return "DBG";
|
|
case SDL_LOG_PRIORITY_INFO:
|
|
return "INF";
|
|
case SDL_LOG_PRIORITY_WARN:
|
|
return "WRN";
|
|
case SDL_LOG_PRIORITY_ERROR:
|
|
return "ERR";
|
|
case SDL_LOG_PRIORITY_CRITICAL:
|
|
return "CRI";
|
|
|
|
default:
|
|
return "UNK";
|
|
}
|
|
}
|
|
|
|
void SDLCALL logHandler(void*, int category, SDL_LogPriority priority, const char* message) {
|
|
if (priority >= SDL_LOG_PRIORITY_INFO) {
|
|
mprintf(("SDL [%s][%s]: %s\n", mapPriority(priority), mapCategory(category), message));
|
|
} else {
|
|
nprintf(("SDL", "SDL [%s][%s]: %s\n", mapPriority(priority), mapCategory(category), message));
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------------------
|
|
// PLATFORM SPECIFIC FUNCTION FOLLOWING
|
|
//
|
|
|
|
#ifdef WIN32
|
|
|
|
// Windows specific includes
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#include <windows.h>
|
|
#include <backends/imgui_impl_sdl3.h>
|
|
|
|
// go through all windows and try and find the one that matches the search string
|
|
BOOL __stdcall os_enum_windows( HWND hwnd, LPARAM param )
|
|
{
|
|
const char* search_string = reinterpret_cast<const char*>(param);
|
|
char tmp[128];
|
|
int len;
|
|
|
|
len = GetWindowText( hwnd, tmp, 127 );
|
|
|
|
if ( len ) {
|
|
if ( strstr( tmp, search_string )) {
|
|
Os_debugger_running = 1; // found the search string!
|
|
return FALSE; // stop enumerating windows
|
|
}
|
|
}
|
|
|
|
return TRUE; // continue enumeration
|
|
}
|
|
|
|
// Fills in the Os_debugger_running with non-zero if debugger detected.
|
|
void os_check_debugger()
|
|
{
|
|
HMODULE hMod;
|
|
char search_string[256];
|
|
char myname[128];
|
|
int namelen;
|
|
char * p;
|
|
|
|
Os_debugger_running = 0; // Assume its not
|
|
|
|
// Find my EXE file name
|
|
hMod = GetModuleHandle(NULL);
|
|
if ( !hMod ) return;
|
|
namelen = GetModuleFileName( hMod, myname, 127 );
|
|
if ( namelen < 1 ) return;
|
|
|
|
// Strip off the .EXE
|
|
p = strstr( myname, ".exe" );
|
|
if (!p) return;
|
|
*p = '\0';
|
|
|
|
// Move p to point to first letter of EXE filename
|
|
while( (*p!='\\') && (*p!='/') && (*p!=':') )
|
|
p--;
|
|
p++;
|
|
if ( strlen(p) < 1 ) return;
|
|
|
|
// Build what the debugger's window title would be if the debugger is running...
|
|
sprintf( search_string, "[run] - %s -", p );
|
|
|
|
// ... and then search for it.
|
|
EnumWindows(os_enum_windows, reinterpret_cast<LPARAM>(&search_string));
|
|
}
|
|
|
|
void os_set_process_affinity()
|
|
{
|
|
HANDLE pHandle = GetCurrentProcess();
|
|
DWORD_PTR pMaskProcess = 0, pMaskSystem = 0;
|
|
|
|
if ( GetProcessAffinityMask(pHandle, &pMaskProcess, &pMaskSystem) ) {
|
|
// only do this if we have at least 2 procs
|
|
if (pMaskProcess >= 3) {
|
|
// prefer running on the second processor by default
|
|
pMaskProcess = os_config_read_uint(NULL, "ProcessorAffinity", 2);
|
|
|
|
if (pMaskProcess > 0) {
|
|
SetProcessAffinityMask(pHandle, pMaskProcess);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// custom message preprocessor
|
|
// check/handle/modify native events as needed before processing by SDL
|
|
// return true if SDL should process the event, false otherwise
|
|
// NOTE: this function can slow oeverall event processing so don't put anything
|
|
// in here unless it's ABSOLUTELY NECESSARY!!!
|
|
#ifdef FS2_VOICER
|
|
static bool windows_message_hook(void *userdata, MSG *msg)
|
|
{
|
|
SDL_Event event{};
|
|
|
|
#define WM_RECOEVENT WM_USER+190 // from voicerec.h
|
|
if (msg->message == WM_RECOEVENT) {
|
|
extern bool VOICEREC_get_sdl_event(SDL_Event *event);
|
|
if (VOICEREC_get_sdl_event(&event)) {
|
|
SDL_PushEvent(&event);
|
|
}
|
|
return false; // skip further processing by SDL
|
|
}
|
|
|
|
// have SDL process this message
|
|
return true;
|
|
}
|
|
#endif
|
|
|
|
#endif // WIN32
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------------------
|
|
// OSAPI DEFINES/VARS
|
|
//
|
|
|
|
// os-wide globals
|
|
static char szWinTitle[128];
|
|
static char szWinClass[128];
|
|
static int Os_inited = 0;
|
|
|
|
static SCP_vector<SDL_Event> deferred_events;
|
|
|
|
int Os_debugger_running = 0;
|
|
|
|
#ifdef SCP_UNIX
|
|
const char* os_get_legacy_user_dir()
|
|
{
|
|
static bool user_dir_initialized = false;
|
|
static SCP_string Os_user_dir_legacy;
|
|
|
|
if (user_dir_initialized) {
|
|
return Os_user_dir_legacy.c_str();
|
|
}
|
|
|
|
extern const char* Osreg_user_dir_legacy;
|
|
sprintf(Os_user_dir_legacy, "%s/%s", getenv("HOME"), Osreg_user_dir_legacy);
|
|
user_dir_initialized = true;
|
|
|
|
return Os_user_dir_legacy.c_str();
|
|
}
|
|
#endif
|
|
|
|
// ----------------------------------------------------------------------------------------------------
|
|
// OSAPI FORWARD DECLARATIONS
|
|
//
|
|
void os_deinit();
|
|
|
|
// ----------------------------------------------------------------------------------------------------
|
|
// OSAPI FUNCTIONS
|
|
//
|
|
|
|
// initialization/shutdown functions -----------------------------------------------
|
|
|
|
// If app_name is NULL or ommited, then TITLE is used
|
|
// for the app name, which is where registry keys are stored.
|
|
void os_init(const char * wclass, const char * title, const char * app_name)
|
|
{
|
|
if (app_name == nullptr || !app_name[0])
|
|
{
|
|
os_init_registry_stuff(Osreg_company_name, title);
|
|
}
|
|
else
|
|
{
|
|
os_init_registry_stuff(Osreg_company_name, app_name);
|
|
}
|
|
|
|
strcpy_s( szWinTitle, title );
|
|
strcpy_s( szWinClass, wclass );
|
|
|
|
auto ver = SDL_GetVersion();
|
|
|
|
mprintf((" Initializing SDL %d.%d.%d (compiled with %d.%d.%d)...\n",
|
|
SDL_VERSIONNUM_MAJOR(ver), SDL_VERSIONNUM_MINOR(ver), SDL_VERSIONNUM_MICRO(ver),
|
|
SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_MICRO_VERSION));
|
|
|
|
if (LoggingEnabled) {
|
|
SDL_SetLogPriorities(SDL_LOG_PRIORITY_VERBOSE);
|
|
SDL_SetLogOutputFunction(&logHandler, nullptr);
|
|
}
|
|
|
|
// Initialize SDL's HIDAPI up front. SDL_hid_enumerate() nominally auto-inits, but FRED's space
|
|
// mouse support only reliably finds an already-connected device when HIDAPI is initialized here
|
|
// rather than lazily on first enumeration. Paired with SDL_hid_exit() in os_deinit().
|
|
if ( SDL_hid_init() != 0 )
|
|
{
|
|
mprintf(("Couldn't init SDL HIDAPI (space mouse may be unavailable): %s\n", SDL_GetError()));
|
|
}
|
|
|
|
if ( !SDL_Init(SDL_INIT_EVENTS) )
|
|
{
|
|
fprintf(stderr, "Couldn't init SDL: %s", SDL_GetError());
|
|
mprintf(("Couldn't init SDL: %s\n", SDL_GetError()));
|
|
|
|
exit(1);
|
|
return;
|
|
}
|
|
|
|
|
|
// initialized
|
|
Os_inited = 1;
|
|
|
|
#ifdef WIN32
|
|
// check to see if we're running under msdev
|
|
os_check_debugger();
|
|
|
|
if (Cmdline_set_cpu_affinity)
|
|
{
|
|
// deal with processor affinity
|
|
os_set_process_affinity();
|
|
}
|
|
#endif // WIN32
|
|
|
|
os::events::addEventListener(SDL_EVENT_WINDOW_RESIZED, os::events::DEFAULT_LISTENER_WEIGHT, window_event_handler);
|
|
os::events::addEventListener(SDL_EVENT_WINDOW_RESTORED, os::events::DEFAULT_LISTENER_WEIGHT, window_event_handler);
|
|
os::events::addEventListener(SDL_EVENT_WINDOW_MINIMIZED, os::events::DEFAULT_LISTENER_WEIGHT, window_event_handler);
|
|
os::events::addEventListener(SDL_EVENT_WINDOW_MAXIMIZED, os::events::DEFAULT_LISTENER_WEIGHT, window_event_handler);
|
|
os::events::addEventListener(SDL_EVENT_WINDOW_FOCUS_LOST, os::events::DEFAULT_LISTENER_WEIGHT, window_event_handler);
|
|
os::events::addEventListener(SDL_EVENT_WINDOW_FOCUS_GAINED, os::events::DEFAULT_LISTENER_WEIGHT, window_event_handler);
|
|
os::events::addEventListener(SDL_EVENT_WINDOW_CLOSE_REQUESTED, os::events::DEFAULT_LISTENER_WEIGHT, window_event_handler);
|
|
|
|
os::events::addEventListener(SDL_EVENT_QUIT, os::events::DEFAULT_LISTENER_WEIGHT, quit_handler);
|
|
|
|
#ifdef WIN32
|
|
#ifdef FS2_VOICER
|
|
SDL_SetWindowsMessageHook(windows_message_hook, nullptr);
|
|
#endif
|
|
#endif
|
|
}
|
|
|
|
// set the main window title
|
|
void os_set_title( const char * title )
|
|
{
|
|
Assertion(mainSDLWindow != nullptr, "This function may only be called with a valid SDL Window.");
|
|
strcpy_s( szWinTitle, title );
|
|
|
|
SDL_SetWindowTitle(mainSDLWindow, szWinTitle);
|
|
}
|
|
|
|
// call at program end
|
|
void os_cleanup()
|
|
{
|
|
os_deinit_registry_stuff();
|
|
|
|
if (LoggingEnabled) {
|
|
outwnd_close();
|
|
}
|
|
|
|
os_deinit();
|
|
}
|
|
|
|
// window management -----------------------------------------------------------------
|
|
|
|
// Returns 1 if app is not the foreground app.
|
|
bool os_foreground()
|
|
{
|
|
return fAppActive;
|
|
}
|
|
|
|
// process management -----------------------------------------------------------------
|
|
|
|
// Sleeps for n milliseconds or until app becomes active.
|
|
void os_sleep(uint ms)
|
|
{
|
|
SDL_Delay(ms);
|
|
}
|
|
|
|
static bool file_exists(const SCP_string& path) {
|
|
std::ofstream str(path, std::ios::in);
|
|
return str.good();
|
|
}
|
|
|
|
static time_t get_file_modification_time(const SCP_string& path) {
|
|
#ifdef SCP_UNIX
|
|
struct stat file_stats{};
|
|
if(stat(path.c_str(), &file_stats) < 0) {
|
|
return 0;
|
|
}
|
|
|
|
return file_stats.st_mtime;
|
|
#elif defined(WIN32)
|
|
struct _stat buf{};
|
|
if (_stat(path.c_str(), &buf) != 0) {
|
|
return 0;
|
|
}
|
|
return buf.st_mtime;
|
|
#else
|
|
#error Unsupported platform!
|
|
#endif
|
|
}
|
|
|
|
const char* Osapi_legacy_mode_reason = nullptr;
|
|
|
|
bool os_is_legacy_mode()
|
|
{
|
|
// Make this check a little faster by caching the result
|
|
if (checkedLegacyMode)
|
|
{
|
|
return legacyMode;
|
|
}
|
|
|
|
if (Cmdline_portable_mode) {
|
|
// When the portable mode option is given, non-legacy is implied
|
|
legacyMode = false;
|
|
checkedLegacyMode = true;
|
|
|
|
Osapi_legacy_mode_reason = "Legacy mode disabled since portable mode was enabled.";
|
|
}
|
|
else {
|
|
SCP_stringstream path_stream;
|
|
path_stream << getPreferencesPath() << Osreg_config_file_name;
|
|
|
|
auto new_config_exists = file_exists(path_stream.str());
|
|
time_t new_config_time = 0;
|
|
if (new_config_exists) {
|
|
new_config_time = get_file_modification_time(path_stream.str());
|
|
}
|
|
|
|
// Also check the modification times of the command line files in case the launcher did not change the settings
|
|
// file
|
|
path_stream.str("");
|
|
path_stream << getPreferencesPath() << "data" << DIR_SEPARATOR_CHAR << "cmdline_fso.cfg";
|
|
new_config_time = std::max(new_config_time, get_file_modification_time(path_stream.str()));
|
|
#ifdef SCP_UNIX
|
|
path_stream.str("");
|
|
path_stream << os_get_legacy_user_dir() << DIR_SEPARATOR_CHAR << Osreg_config_file_name;
|
|
|
|
auto old_config_exists = file_exists(path_stream.str());
|
|
time_t old_config_time = 0;
|
|
if (old_config_exists) {
|
|
old_config_time = get_file_modification_time(path_stream.str());
|
|
}
|
|
|
|
path_stream.str("");
|
|
path_stream << os_get_legacy_user_dir() << DIR_SEPARATOR_CHAR << "data" << DIR_SEPARATOR_CHAR
|
|
<< "cmdline_fso.cfg";
|
|
old_config_time = std::max(old_config_time, get_file_modification_time(path_stream.str()));
|
|
#else
|
|
std::optional<time_t> optional_old_config_time = os_registry_get_last_modification_time();
|
|
time_t old_config_time = 0;
|
|
|
|
//Check if the registry key exists
|
|
auto old_config_exists = optional_old_config_time.has_value();
|
|
|
|
if (old_config_exists) {
|
|
old_config_time = optional_old_config_time.value();
|
|
// On Windows the cmdline_fso file was stored in the game root directory which should be in the current directory
|
|
// Only get this if the old config exists
|
|
path_stream.str("");
|
|
path_stream << "." << DIR_SEPARATOR_CHAR << "data" << DIR_SEPARATOR_CHAR << "cmdline_fso.cfg";
|
|
old_config_time = std::max(old_config_time, get_file_modification_time(path_stream.str()));
|
|
}
|
|
#endif
|
|
|
|
if (new_config_exists && old_config_exists) {
|
|
// Both config files exists so we need to decide which to use based on their last modification times
|
|
// if the old config was modified more recently than the new config then we use the legacy mode since the
|
|
// user probably used an outdated launcher after using a more recent one
|
|
legacyMode = old_config_time > new_config_time;
|
|
|
|
if (legacyMode) {
|
|
Osapi_legacy_mode_reason = "Legacy mode enabled since the old config location was used more recently than the new location.";
|
|
} else {
|
|
Osapi_legacy_mode_reason = "Legacy mode disabled since the new config location was used more recently than the old location.";
|
|
}
|
|
} else if (new_config_exists) {
|
|
// If the new config exists and the old one doesn't then we can safely disable the legacy mode
|
|
legacyMode = false;
|
|
|
|
Osapi_legacy_mode_reason = "Legacy mode disabled since the old config does not exist while the new config exists.";
|
|
} else if (old_config_exists) {
|
|
// Old config exists but new doesn't -> use legacy mode
|
|
legacyMode = true;
|
|
|
|
Osapi_legacy_mode_reason = "Legacy mode enabled since the old config exists while the new config does not exist.";
|
|
} else {
|
|
// Neither old nor new config exists -> this is a new install
|
|
legacyMode = false;
|
|
|
|
Osapi_legacy_mode_reason = "Legacy mode disabled since no existing config was detected.";
|
|
}
|
|
}
|
|
|
|
if (legacyMode) {
|
|
// Print a message for the people running it from the terminal
|
|
fprintf(stdout, "FSO is running in legacy config mode. Please either update your launcher or"
|
|
" copy the configuration and pilot files to '%s' for better future compatibility.\n", getPreferencesPath().c_str());
|
|
}
|
|
|
|
checkedLegacyMode = true;
|
|
return legacyMode;
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------------------------------
|
|
// OSAPI FORWARD DECLARATIONS
|
|
//
|
|
|
|
// called at shutdown. Makes sure all thread processing terminates.
|
|
void os_deinit()
|
|
{
|
|
// Free the view ports
|
|
os::closeAllViewports();
|
|
|
|
//If we have an open space mouse, close it
|
|
io::spacemouse::SpaceMouse::shutdownSharedSpaceMouse();
|
|
|
|
SDL_hid_exit();
|
|
|
|
SDL_Quit();
|
|
}
|
|
|
|
void debug_int3(const char *file, int line)
|
|
{
|
|
mprintf(("Int3(): From %s at line %d\n", file, line));
|
|
|
|
gr_activate(false);
|
|
|
|
mprintf(("%s\n", dump_stacktrace().c_str()));
|
|
|
|
#ifndef NDEBUG
|
|
SDL_TriggerBreakpoint();
|
|
#endif
|
|
|
|
gr_activate(true);
|
|
}
|
|
|
|
namespace os
|
|
{
|
|
Viewport* addViewport(std::unique_ptr<Viewport>&& viewport) {
|
|
auto port = viewport.get();
|
|
viewports.push_back(std::move(viewport));
|
|
return port;
|
|
}
|
|
void setMainViewPort(Viewport* mainView) {
|
|
mainViewPort = mainView;
|
|
mainSDLWindow = mainView->toSDLWindow();
|
|
}
|
|
SDL_Window* getSDLMainWindow() {
|
|
return mainSDLWindow;
|
|
}
|
|
Viewport* getMainViewport() {
|
|
return mainViewPort;
|
|
}
|
|
void closeAllViewports() {
|
|
viewports.clear();
|
|
}
|
|
|
|
namespace events
|
|
{
|
|
namespace
|
|
{
|
|
ListenerIdentifier nextListenerIdentifier;
|
|
|
|
struct EventListenerData
|
|
{
|
|
ListenerIdentifier identifier;
|
|
Listener listener;
|
|
|
|
uint32_t type;
|
|
int weight;
|
|
|
|
bool operator<(const EventListenerData& other) const
|
|
{
|
|
if (type < other.type)
|
|
{
|
|
return true;
|
|
}
|
|
if (type > other.type)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Type is the same
|
|
return weight < other.weight;
|
|
}
|
|
};
|
|
|
|
bool compare_type(const EventListenerData& left, const EventListenerData& right)
|
|
{
|
|
return left.type < right.type;
|
|
}
|
|
|
|
SCP_vector<EventListenerData> eventListeners;
|
|
}
|
|
|
|
ListenerIdentifier addEventListener(SDL_EventType type, int weight, const Listener& listener)
|
|
{
|
|
Assertion(listener, "Invalid event handler passed!");
|
|
|
|
EventListenerData data;
|
|
data.identifier = ++nextListenerIdentifier;
|
|
data.listener = listener;
|
|
|
|
data.weight = weight;
|
|
data.type = static_cast<uint32_t>(type);
|
|
|
|
eventListeners.push_back(data);
|
|
// This is suboptimal for runtime but we will iterate that vector often so cache hits are more important
|
|
std::sort(eventListeners.begin(), eventListeners.end());
|
|
|
|
return data.identifier;
|
|
}
|
|
|
|
bool removeEventListener(ListenerIdentifier identifier)
|
|
{
|
|
auto endIter = end(eventListeners);
|
|
for (auto iter = begin(eventListeners); iter != endIter; ++iter)
|
|
{
|
|
if (iter->identifier == identifier)
|
|
{
|
|
eventListeners.erase(iter);
|
|
return true; // Identifiers are unique
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool isWindowEvent(const SDL_Event& e, SDL_Window* window)
|
|
{
|
|
auto mainId = SDL_GetWindowID(window);
|
|
|
|
if ((e.type >= SDL_EVENT_WINDOW_FIRST) && (e.type <= SDL_EVENT_WINDOW_LAST)) {
|
|
return mainId == e.window.windowID;
|
|
}
|
|
|
|
switch(e.type)
|
|
{
|
|
case SDL_EVENT_KEY_DOWN:
|
|
case SDL_EVENT_KEY_UP:
|
|
return mainId == e.key.windowID;
|
|
case SDL_EVENT_TEXT_EDITING:
|
|
return mainId == e.edit.windowID;
|
|
case SDL_EVENT_TEXT_INPUT:
|
|
return mainId == e.text.windowID;
|
|
case SDL_EVENT_MOUSE_MOTION:
|
|
return mainId == e.motion.windowID;
|
|
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
|
case SDL_EVENT_MOUSE_BUTTON_UP:
|
|
return mainId == e.button.windowID;
|
|
case SDL_EVENT_MOUSE_WHEEL:
|
|
return mainId == e.wheel.windowID;
|
|
default:
|
|
// Event doesn't have a window ID
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void os_defer_events_on_load_screen() {
|
|
SDL_Event event;
|
|
while (SDL_PollEvent(&event)) {
|
|
// Add event to be handled later
|
|
deferred_events.push_back(event);
|
|
}
|
|
}
|
|
|
|
// ImGui lays out at the render resolution (see gr_imgui_begin_frame), but SDL reports mouse
|
|
// coordinates in window pixels, so positional events have to be converted before they reach it.
|
|
//
|
|
// Note that ImGui_ImplSDL3_NewFrame has a SDL_GetGlobalMouseState fallback that queues an
|
|
// unconverted position. It only fires when no window is hovered and no buttons are held, i.e. the
|
|
// cursor is outside the window entirely, so it cannot affect hit testing.
|
|
static SDL_Event scale_imgui_mouse_event(const SDL_Event& event)
|
|
{
|
|
SDL_Event scaled = event;
|
|
|
|
switch (event.type) {
|
|
case SDL_EVENT_MOUSE_MOTION:
|
|
gr_window_to_render_pos(scaled.motion.x, scaled.motion.y);
|
|
gr_window_to_render_pos(scaled.motion.xrel, scaled.motion.yrel);
|
|
break;
|
|
|
|
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
|
case SDL_EVENT_MOUSE_BUTTON_UP:
|
|
gr_window_to_render_pos(scaled.button.x, scaled.button.y);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return scaled;
|
|
}
|
|
|
|
static void handle_sdl_event(const SDL_Event& event) {
|
|
using namespace os::events;
|
|
|
|
bool imgui_processed_this = false;
|
|
if ((gameseq_get_state() == GS_STATE_LAB) || (gameseq_get_state() == GS_STATE_INGAME_OPTIONS)) {
|
|
//In these states, we always need to forward inputs to ImGUI, and depending on the ImGUI state and the input type, we must consume it here instead of passing it to FSO.
|
|
const SDL_Event imgui_event = scale_imgui_mouse_event(event);
|
|
ImGui_ImplSDL3_ProcessEvent(&imgui_event);
|
|
|
|
imgui_processed_this = (ImGui::GetIO().WantCaptureKeyboard &&
|
|
(event.type == SDL_EventType::SDL_EVENT_KEY_UP ||
|
|
event.type == SDL_EventType::SDL_EVENT_KEY_DOWN)) ||
|
|
(ImGui::GetIO().WantCaptureMouse &&
|
|
(event.type == SDL_EventType::SDL_EVENT_MOUSE_BUTTON_UP ||
|
|
event.type == SDL_EventType::SDL_EVENT_MOUSE_BUTTON_DOWN||
|
|
event.type == SDL_EventType::SDL_EVENT_MOUSE_MOTION));
|
|
}
|
|
|
|
if (!imgui_processed_this) {
|
|
EventListenerData data;
|
|
data.type = event.type;
|
|
|
|
auto iter = std::lower_bound(eventListeners.begin(), eventListeners.end(), data, compare_type);
|
|
|
|
if (iter != eventListeners.end()) {
|
|
// The vector contains all event listeners, the listeners are sorted for type and weight
|
|
// -> iterating through all listeners will yield them in increasing weight order
|
|
// but we can only do this until we have reached the end of the vector or the type has changed
|
|
for (; iter != eventListeners.end() && iter->type == event.type; ++iter) {
|
|
if (iter->listener(event)) {
|
|
// Listener has handled the event
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void os_remove_deferred_cutscene_key_events() {
|
|
deferred_events.erase(
|
|
std::remove_if(deferred_events.begin(), deferred_events.end(), [](const SDL_Event &event)
|
|
{
|
|
return (event.type == SDL_EVENT_KEY_UP) && (
|
|
event.key.key == SDLK_KP_ENTER ||
|
|
event.key.key == SDLK_RETURN ||
|
|
event.key.key == SDLK_SPACE
|
|
);
|
|
}),
|
|
deferred_events.end()
|
|
);
|
|
}
|
|
|
|
void os_poll()
|
|
{
|
|
// Replay the deferred events
|
|
auto end = deferred_events.end();
|
|
for (auto it = deferred_events.begin(); it != end; ++it) {
|
|
handle_sdl_event(*it);
|
|
}
|
|
deferred_events.clear();
|
|
|
|
SDL_Event event;
|
|
|
|
while (SDL_PollEvent(&event)) {
|
|
handle_sdl_event(event);
|
|
}
|
|
|
|
openxr_poll();
|
|
}
|
|
|
|
SCP_string os_get_config_path(const SCP_string& subpath)
|
|
{
|
|
// Make path platform compatible
|
|
SCP_string compatiblePath(subpath);
|
|
std::replace(compatiblePath.begin(), compatiblePath.end(), '/', DIR_SEPARATOR_CHAR);
|
|
|
|
SCP_stringstream ss;
|
|
|
|
if (Cmdline_portable_mode) {
|
|
// Use the current directory
|
|
ss << "." << DIR_SEPARATOR_CHAR << compatiblePath;
|
|
return ss.str();
|
|
}
|
|
|
|
// Avoid infinite recursion when checking legacy mode
|
|
if (os_is_legacy_mode()) {
|
|
#ifdef WIN32
|
|
// Use the current directory
|
|
ss << ".";
|
|
#else
|
|
extern const char* Osreg_user_dir_legacy;
|
|
// Use the home directory
|
|
ss << getenv("HOME") << DIR_SEPARATOR_CHAR << Osreg_user_dir_legacy;
|
|
#endif
|
|
|
|
ss << DIR_SEPARATOR_CHAR << compatiblePath;
|
|
return ss.str();
|
|
}
|
|
|
|
ss << getPreferencesPath() << compatiblePath;
|
|
|
|
return ss.str();
|
|
}
|
|
|
|
/*
|
|
Special functions for Android
|
|
*/
|
|
|
|
#ifdef __ANDROID__
|
|
static bool android_check_exception(JNIEnv* env, const char* operation)
|
|
{
|
|
if (!env->ExceptionCheck()) {
|
|
return false;
|
|
}
|
|
|
|
mprintf(("OSAPI: JNI exception in %s.\n", operation));
|
|
env->ExceptionDescribe();
|
|
env->ExceptionClear();
|
|
return true;
|
|
}
|
|
|
|
template <typename Callback>
|
|
static void android_with_static_method(const char* name, const char* signature, Callback&& callback)
|
|
{
|
|
JNIEnv* env = static_cast<JNIEnv*>(SDL_GetAndroidJNIEnv());
|
|
if (env == nullptr) {
|
|
mprintf(("OSAPI: Couldn't get JNI environment for %s.\n", name));
|
|
return;
|
|
}
|
|
if (android_check_exception(env, name)) {
|
|
return;
|
|
}
|
|
|
|
if (env->PushLocalFrame(4) < 0) {
|
|
android_check_exception(env, "PushLocalFrame");
|
|
mprintf(("OSAPI: Couldn't create JNI local frame for %s.\n", name));
|
|
return;
|
|
}
|
|
// Release the activity, class, and temporary strings on every exit path.
|
|
auto local_frame = util::finally([env]() { env->PopLocalFrame(nullptr); });
|
|
|
|
jobject activity = static_cast<jobject>(SDL_GetAndroidActivity());
|
|
if (android_check_exception(env, "SDL_GetAndroidActivity") || activity == nullptr) {
|
|
mprintf(("OSAPI: Couldn't get Android activity for %s.\n", name));
|
|
return;
|
|
}
|
|
|
|
jclass activity_class = env->GetObjectClass(activity);
|
|
if (android_check_exception(env, "GetObjectClass") || activity_class == nullptr) {
|
|
mprintf(("OSAPI: Couldn't get Android activity class for %s.\n", name));
|
|
return;
|
|
}
|
|
|
|
jmethodID method = env->GetStaticMethodID(activity_class, name, signature);
|
|
if (android_check_exception(env, name) || method == nullptr) {
|
|
mprintf(("OSAPI: Couldn't find static method %s with signature %s.\n", name, signature));
|
|
return;
|
|
}
|
|
|
|
callback(env, activity_class, method);
|
|
android_check_exception(env, name);
|
|
}
|
|
|
|
void os_android_set_flags_string(const char* json)
|
|
{
|
|
android_with_static_method("setFlagsJson", "(Ljava/lang/String;)V",
|
|
[json](JNIEnv* env, jclass activity_class, jmethodID method) {
|
|
jstring value = env->NewStringUTF(json ? json : "");
|
|
if (android_check_exception(env, "NewStringUTF") || value == nullptr) {
|
|
return;
|
|
}
|
|
env->CallStaticVoidMethod(activity_class, method, value);
|
|
});
|
|
}
|
|
|
|
SCP_string os_android_get_working_folder_path()
|
|
{
|
|
SCP_string wfp{};
|
|
android_with_static_method("getWorkingFolder", "()Ljava/lang/String;",
|
|
[&wfp](JNIEnv* env, jclass activity_class, jmethodID method) {
|
|
jstring value = static_cast<jstring>(env->CallStaticObjectMethod(activity_class, method));
|
|
if (android_check_exception(env, "getWorkingFolder") || value == nullptr) {
|
|
return;
|
|
}
|
|
|
|
const char* working_folder = env->GetStringUTFChars(value, nullptr);
|
|
if (android_check_exception(env, "GetStringUTFChars") || working_folder == nullptr) {
|
|
return;
|
|
}
|
|
auto release_chars = util::finally([env, value, working_folder]() {
|
|
env->ReleaseStringUTFChars(value, working_folder);
|
|
});
|
|
wfp = working_folder;
|
|
});
|
|
|
|
if (wfp.empty()) {
|
|
mprintf(("Couldn't get working folder path from Java class, reverting to SDL default.\n"));
|
|
// SDL's external storage path already includes the app's files directory.
|
|
const char* fallbackPath = SDL_GetAndroidExternalStoragePath();
|
|
if (fallbackPath) {
|
|
wfp = SCP_string(fallbackPath);
|
|
}
|
|
}
|
|
|
|
// Ensure path ends with a dir separator
|
|
if (!wfp.empty() && (wfp.back() != DIR_SEPARATOR_CHAR)) {
|
|
wfp += DIR_SEPARATOR_CHAR;
|
|
}
|
|
mprintf(("Using working folder: %s\n", wfp.c_str()));
|
|
return wfp;
|
|
}
|
|
|
|
void os_android_touch_overlay_toggle(bool status)
|
|
{
|
|
android_with_static_method(status ? "enableOverlay" : "disableOverlay", "()V",
|
|
[](JNIEnv* env, jclass activity_class, jmethodID method) {
|
|
env->CallStaticVoidMethod(activity_class, method);
|
|
});
|
|
}
|
|
|
|
void os_android_touch_overlay_set_opacity(int opacity)
|
|
{
|
|
android_with_static_method("setOverlayOpacity", "(F)V",
|
|
[opacity](JNIEnv* env, jclass activity_class, jmethodID method) {
|
|
env->CallStaticVoidMethod(activity_class, method, static_cast<jfloat>(opacity) / 100.0f);
|
|
});
|
|
}
|
|
|
|
static bool touch_ui_change(bool new_val, bool initial)
|
|
{
|
|
if (initial) {
|
|
return false;
|
|
}
|
|
os_android_touch_overlay_toggle(new_val);
|
|
return true;
|
|
}
|
|
|
|
static auto TouchOverlayOption = options::OptionBuilder<bool>("Input.TouchOverlay",
|
|
std::pair<const char*, int>{"Touch Overlay", 1936},
|
|
std::pair<const char*, int>{"Enable or disable the touch overlay", 1937})
|
|
.category(std::make_pair("Input", 1827))
|
|
.level(options::ExpertLevel::Beginner)
|
|
.change_listener(touch_ui_change)
|
|
.default_val(true)
|
|
.importance(2)
|
|
.finish();
|
|
|
|
static bool touch_ui_opacity_change(int new_val, bool initial)
|
|
{
|
|
Assertion(new_val >= 0 && new_val <= 100, "Invalid value %d supplied by options system!", new_val);
|
|
if (initial) {
|
|
return false;
|
|
}
|
|
os_android_touch_overlay_set_opacity(new_val);
|
|
return true;
|
|
}
|
|
|
|
static auto TouchOverlayOpacityOption = options::OptionBuilder<int>("Input.TouchOverlayOpacity",
|
|
std::pair<const char*, int>{"Touch Overlay Opacity", 1938},
|
|
std::pair<const char*, int>{"Set the opacity of the touch overlay", 1939})
|
|
.category(std::make_pair("Input", 1827))
|
|
.level(options::ExpertLevel::Beginner)
|
|
.range(0, 100)
|
|
.default_val(20)
|
|
.flags({ options::OptionFlags::RangeTypeInteger })
|
|
.change_listener(touch_ui_opacity_change)
|
|
.importance(1)
|
|
.finish();
|
|
|
|
void os_android_touch_overlay_init()
|
|
{
|
|
os_android_touch_overlay_toggle(TouchOverlayOption->getValue());
|
|
os_android_touch_overlay_set_opacity(TouchOverlayOpacityOption->getValue());
|
|
}
|
|
#endif
|
|
|