From 6adb42078e166dc355368356f0e427b10dbb4873 Mon Sep 17 00:00:00 2001 From: James Turner Date: Sun, 16 Oct 2011 19:55:04 +0100 Subject: [PATCH] Refactor fg-home computation, use Library/Application Support on Mac. --- CMakeLists.txt | 4 ++ src/Main/CMakeLists.txt | 1 + src/Main/fg_commands.cxx | 31 +++++-------- src/Main/fg_init.cxx | 97 ++++++++++++++++++++++++++-------------- 4 files changed, 79 insertions(+), 54 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 221efc659..15b8cb062 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,6 +51,10 @@ endif() IF(APPLE) set(EVENT_INPUT_DEFAULT 1) + + find_library(CORESERVICES_LIBRARY CoreServices) + list(APPEND PLATFORM_LIBS ${CORESERVICES_LIBRARY}) + elseif(CMAKE_SYSTEM_NAME MATCHES "Linux") # disabled while DBus / HAL / udev issues are decided #set(EVENT_INPUT_DEFAULT 1) diff --git a/src/Main/CMakeLists.txt b/src/Main/CMakeLists.txt index b7c7e961f..8398ff650 100644 --- a/src/Main/CMakeLists.txt +++ b/src/Main/CMakeLists.txt @@ -80,6 +80,7 @@ target_link_libraries(fgfs ${LIBSVN_LIBRARIES} ${HLA_LIBRARIES} ${SIMGEAR_CORE_LIBRARY_DEPENDENCIES} + ${PLATFORM_LIBS} ) install(TARGETS fgfs RUNTIME DESTINATION bin) diff --git a/src/Main/fg_commands.cxx b/src/Main/fg_commands.cxx index 14a04c3d0..a07739d2a 100644 --- a/src/Main/fg_commands.cxx +++ b/src/Main/fg_commands.cxx @@ -191,27 +191,18 @@ do_exit (const SGPropertyNode * arg) fgSetBool("/sim/signals/exit", true); if (fgGetBool("/sim/startup/save-on-exit")) { -#ifdef _WIN32 - char* envp = ::getenv( "APPDATA" ); - if ( envp != NULL ) { - SGPath config( envp ); - config.append( "flightgear.org" ); -#else - if ( homedir != NULL ) { - SGPath config( homedir ); - config.append( ".fgfs" ); -#endif - config.append( "autosave.xml" ); - config.create_dir( 0700 ); - SG_LOG(SG_IO, SG_INFO, "Saving user settings to " << config.str()); - try { - writeProperties(config.str(), globals->get_props(), false, SGPropertyNode::USERARCHIVE); - } catch (const sg_exception &e) { - guiErrorMessage("Error writing autosave.xml: ", e); - } + SGPath autosaveFile(fgGetString("/sim/fg-home")); + autosaveFile.append( "autosave.xml" ); + autosaveFile.create_dir( 0700 ); + SG_LOG(SG_IO, SG_INFO, "Saving user settings to " << autosaveFile.str()); + try { + writeProperties(autosaveFile.str(), globals->get_props(), false, SGPropertyNode::USERARCHIVE); + } catch (const sg_exception &e) { + guiErrorMessage("Error writing autosave.xml: ", e); + } + + SG_LOG(SG_INPUT, SG_DEBUG, "Finished Saving user settings"); - SG_LOG(SG_INPUT, SG_DEBUG, "Finished Saving user settings"); - } } fgOSExit(arg->getIntValue("status", 0)); diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx index e402167c1..073c53ac1 100644 --- a/src/Main/fg_init.cxx +++ b/src/Main/fg_init.cxx @@ -451,6 +451,43 @@ private: SGPropertyNode* _cache; }; +#ifdef _WIN32 +static SGPath platformDefaultDataPath() +{ + char *envp = ::getenv( "APPDATA" ); + SGPath config( envp ); + config.append( "flightgear.org" ); +} +#elif __APPLE__ + +#include + +static SGPath platformDefaultDataPath() +{ + FSRef ref; + OSErr err = FSFindFolder(kUserDomain, kApplicationSupportFolderType, false, &ref); + if (err) { + return SGPath(); + } + + unsigned char path[1024]; + if (FSRefMakePath(&ref, path, 1024) != noErr) { + return SGPath(); + } + + SGPath appData; + appData.set((const char*) path); + appData.append("flightgear.org"); + return appData; +} +#else +static SGPath platformDefaultDataPath() +{ + SGPath config( homedir ); + config.append( ".fgfs" ); +} +#endif + // Read in configuration (file and command line) bool fgInitConfig ( int argc, char **argv ) { @@ -467,42 +504,34 @@ bool fgInitConfig ( int argc, char **argv ) { } SGPropertyNode autosave; -#ifdef _WIN32 - char *envp = ::getenv( "APPDATA" ); - if (envp != NULL ) { - SGPath config( envp ); - config.append( "flightgear.org" ); -#else - if ( homedir != NULL ) { - SGPath config( homedir ); - config.append( ".fgfs" ); -#endif - const char *fg_home = getenv("FG_HOME"); - if (fg_home) - config = fg_home; + SGPath dataPath = platformDefaultDataPath(); + + const char *fg_home = getenv("FG_HOME"); + if (fg_home) + dataPath = fg_home; + + simgear::Dir exportDir(simgear::Dir(dataPath).file("Export")); + if (!exportDir.exists()) { + exportDir.create(0777); + } - SGPath home_export(config.str()); - home_export.append("Export/dummy"); - home_export.create_dir(0777); + // Set /sim/fg-home and don't allow malign code to override it until + // Nasal security is set up. Use FG_HOME if necessary. + SGPropertyNode *home = fgGetNode("/sim", true); + home->removeChild("fg-home", 0, false); + home = home->getChild("fg-home", 0, true); + home->setStringValue(dataPath.c_str()); + home->setAttribute(SGPropertyNode::WRITE, false); - // Set /sim/fg-home and don't allow malign code to override it until - // Nasal security is set up. Use FG_HOME if necessary. - SGPropertyNode *home = fgGetNode("/sim", true); - home->removeChild("fg-home", 0, false); - home = home->getChild("fg-home", 0, true); - home->setStringValue(config.c_str()); - home->setAttribute(SGPropertyNode::WRITE, false); - - config.append( "autosave.xml" ); - if (config.exists()) { - SG_LOG(SG_INPUT, SG_INFO, "Reading user settings from " << config.str()); - try { - readProperties(config.str(), &autosave, SGPropertyNode::USERARCHIVE); - } catch (sg_exception& e) { - SG_LOG(SG_INPUT, SG_WARN, "failed to read user settings:" << e.getMessage() - << "(from " << e.getOrigin() << ")"); - } - } + SGPath autosaveFile = simgear::Dir(dataPath).file("autosave.xml"); + if (autosaveFile.exists()) { + SG_LOG(SG_INPUT, SG_INFO, "Reading user settings from " << autosaveFile.str()); + try { + readProperties(autosaveFile.str(), &autosave, SGPropertyNode::USERARCHIVE); + } catch (sg_exception& e) { + SG_LOG(SG_INPUT, SG_WARN, "failed to read user settings:" << e.getMessage() + << "(from " << e.getOrigin() << ")"); + } } // Scan user config files and command line for a specified aircraft.