]> git.mxchange.org Git - flightgear.git/commitdiff
Refactor fg-home computation, use Library/Application Support on Mac.
authorJames Turner <zakalawe@mac.com>
Sun, 16 Oct 2011 18:55:04 +0000 (19:55 +0100)
committerJames Turner <zakalawe@mac.com>
Sun, 16 Oct 2011 18:55:04 +0000 (19:55 +0100)
CMakeLists.txt
src/Main/CMakeLists.txt
src/Main/fg_commands.cxx
src/Main/fg_init.cxx

index 221efc659926bc6555097246ccd79a749d56f0b7..15b8cb062f4c34b22be21b88d158b1d6af263012 100644 (file)
@@ -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)
index b7c7e961f2a7eacfefa6240ceb50a79ab451922e..8398ff65008693cb33d278c0712814a3dd5cce11 100644 (file)
@@ -80,6 +80,7 @@ target_link_libraries(fgfs
        ${LIBSVN_LIBRARIES}
        ${HLA_LIBRARIES}
        ${SIMGEAR_CORE_LIBRARY_DEPENDENCIES}
+       ${PLATFORM_LIBS}
 )
 
 install(TARGETS fgfs RUNTIME DESTINATION bin)
index 14a04c3d06a5260ee4b4528730a9115f351f57b2..a07739d2ae74352e831ce59ee31ccb3eebdff6f6 100644 (file)
@@ -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));
index e402167c139520fb993e6e52a2e90a30a363de35..073c53ac1a8e06e24fef63f12ed12656ee676eba 100644 (file)
@@ -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 <CoreServices/CoreServices.h>
+
+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 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(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 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);
+    }
+
+    // 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);
+
+    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.