From b62b8fe5ad957bdf86f4a6bc25b2a6a0be104ac9 Mon Sep 17 00:00:00 2001 From: David Ludwig Date: Sat, 22 Jul 2017 21:09:53 -0400 Subject: [PATCH] read/write preferences from HOME/.basilisk_ii_prefs --- .../BasiliskII.xcodeproj/project.pbxproj | 8 +- BasiliskII/src/SDL/prefs_sdl.cpp | 104 ++++++++++++++++++ 2 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 BasiliskII/src/SDL/prefs_sdl.cpp diff --git a/BasiliskII/src/MacOSX/BasiliskII.xcodeproj/project.pbxproj b/BasiliskII/src/MacOSX/BasiliskII.xcodeproj/project.pbxproj index 3069555a..149dd2f4 100644 --- a/BasiliskII/src/MacOSX/BasiliskII.xcodeproj/project.pbxproj +++ b/BasiliskII/src/MacOSX/BasiliskII.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 752F26F21F240140001032B4 /* SDL2.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 7539E1E51F23B288006B2DF2 /* SDL2.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 752F26F91F240E51001032B4 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 752F26F81F240E51001032B4 /* Foundation.framework */; }; 752F26FB1F240E69001032B4 /* IOKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 752F26FA1F240E69001032B4 /* IOKit.framework */; }; + 752F27011F242BAF001032B4 /* prefs_sdl.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 752F27001F242BAF001032B4 /* prefs_sdl.cpp */; }; 7539DFBF1F23B17E006B2DF2 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7539DFBE1F23B17E006B2DF2 /* Assets.xcassets */; }; 7539E1251F23B25A006B2DF2 /* adb.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7539DFC91F23B25A006B2DF2 /* adb.cpp */; }; 7539E1261F23B25A006B2DF2 /* audio.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7539DFCA1F23B25A006B2DF2 /* audio.cpp */; }; @@ -85,7 +86,6 @@ 7539E2801F23C4CA006B2DF2 /* main_unix.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7539E27F1F23C4CA006B2DF2 /* main_unix.cpp */; }; 7539E28E1F23C56F006B2DF2 /* clip_dummy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7539E2851F23C56F006B2DF2 /* clip_dummy.cpp */; }; 7539E28F1F23C56F006B2DF2 /* ether_dummy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7539E2861F23C56F006B2DF2 /* ether_dummy.cpp */; }; - 7539E2901F23C56F006B2DF2 /* prefs_dummy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7539E2871F23C56F006B2DF2 /* prefs_dummy.cpp */; }; 7539E2911F23C56F006B2DF2 /* prefs_editor_dummy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7539E2881F23C56F006B2DF2 /* prefs_editor_dummy.cpp */; }; 7539E2921F23C56F006B2DF2 /* scsi_dummy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7539E2891F23C56F006B2DF2 /* scsi_dummy.cpp */; }; 7539E2931F23C56F006B2DF2 /* serial_dummy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7539E28A1F23C56F006B2DF2 /* serial_dummy.cpp */; }; @@ -119,6 +119,7 @@ /* Begin PBXFileReference section */ 752F26F81F240E51001032B4 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 752F26FA1F240E69001032B4 /* IOKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = IOKit.framework; path = System/Library/Frameworks/IOKit.framework; sourceTree = SDKROOT; }; + 752F27001F242BAF001032B4 /* prefs_sdl.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = prefs_sdl.cpp; sourceTree = ""; }; 7539DFB21F23B17E006B2DF2 /* BasiliskII.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BasiliskII.app; sourceTree = BUILT_PRODUCTS_DIR; }; 7539DFBE1F23B17E006B2DF2 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 7539DFC91F23B25A006B2DF2 /* adb.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = adb.cpp; path = ../adb.cpp; sourceTree = ""; }; @@ -263,7 +264,6 @@ 7539E27F1F23C4CA006B2DF2 /* main_unix.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = main_unix.cpp; sourceTree = ""; }; 7539E2851F23C56F006B2DF2 /* clip_dummy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = clip_dummy.cpp; sourceTree = ""; }; 7539E2861F23C56F006B2DF2 /* ether_dummy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ether_dummy.cpp; sourceTree = ""; }; - 7539E2871F23C56F006B2DF2 /* prefs_dummy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = prefs_dummy.cpp; sourceTree = ""; }; 7539E2881F23C56F006B2DF2 /* prefs_editor_dummy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = prefs_editor_dummy.cpp; sourceTree = ""; }; 7539E2891F23C56F006B2DF2 /* scsi_dummy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = scsi_dummy.cpp; sourceTree = ""; }; 7539E28A1F23C56F006B2DF2 /* serial_dummy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = serial_dummy.cpp; sourceTree = ""; }; @@ -412,6 +412,7 @@ 7539E0711F23B25A006B2DF2 /* SDL */ = { isa = PBXGroup; children = ( + 752F27001F242BAF001032B4 /* prefs_sdl.cpp */, 7539E0721F23B25A006B2DF2 /* audio_sdl.cpp */, 7539E0731F23B25A006B2DF2 /* keycodes */, 7539E0761F23B25A006B2DF2 /* video_sdl.cpp */, @@ -590,7 +591,6 @@ children = ( 7539E2851F23C56F006B2DF2 /* clip_dummy.cpp */, 7539E2861F23C56F006B2DF2 /* ether_dummy.cpp */, - 7539E2871F23C56F006B2DF2 /* prefs_dummy.cpp */, 7539E2881F23C56F006B2DF2 /* prefs_editor_dummy.cpp */, 7539E2891F23C56F006B2DF2 /* scsi_dummy.cpp */, 7539E28A1F23C56F006B2DF2 /* serial_dummy.cpp */, @@ -729,11 +729,11 @@ 7539E2801F23C4CA006B2DF2 /* main_unix.cpp in Sources */, 7539E2A61F23CB9B006B2DF2 /* cpuemu.cpp in Sources */, 7539E1E11F23B25A006B2DF2 /* user_strings.cpp in Sources */, + 752F27011F242BAF001032B4 /* prefs_sdl.cpp in Sources */, 7539E2A51F23CB9B006B2DF2 /* cpuemu_nf.cpp in Sources */, 7539E2971F23C5FD006B2DF2 /* newcpu.cpp in Sources */, 7539E12A1F23B25A006B2DF2 /* vm_alloc.cpp in Sources */, 7539E16C1F23B25A006B2DF2 /* main.cpp in Sources */, - 7539E2901F23C56F006B2DF2 /* prefs_dummy.cpp in Sources */, 7539E26D1F23B32A006B2DF2 /* strlcpy.c in Sources */, 7539E26E1F23B32A006B2DF2 /* sys_unix.cpp in Sources */, 7539E1271F23B25A006B2DF2 /* cdrom.cpp in Sources */, diff --git a/BasiliskII/src/SDL/prefs_sdl.cpp b/BasiliskII/src/SDL/prefs_sdl.cpp new file mode 100644 index 00000000..577d1084 --- /dev/null +++ b/BasiliskII/src/SDL/prefs_sdl.cpp @@ -0,0 +1,104 @@ +/* + * prefs_sdl.cpp - Preferences handling, SDL2 implementation + * + * Basilisk II (C) 1997-2008 Christian Bauer + * + * 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 2 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "sysdeps.h" + +#include +#include +#include +#include + +#include "prefs.h" + + +// Platform-specific preferences items +prefs_desc platform_prefs_items[] = { + {NULL, TYPE_END, false} // End of list +}; + + +// Prefs file name and path +const char PREFS_FILE_NAME[] = ".basilisk_ii_prefs"; + +std::string UserPrefsPath; + + +/* + * Load preferences from settings file + */ + +void LoadPrefs(const char * vmdir) // TODO: load prefs from 'vmdir' +{ + // Build a full-path to the settings file + char prefs_path[4096]; + if (!vmdir) { + vmdir = SDL_getenv("HOME"); + } + if (!vmdir) { + vmdir = "./"; + } + SDL_snprintf(prefs_path, sizeof(prefs_path), "%s/%s", vmdir, PREFS_FILE_NAME); + + // Read preferences from settings file + FILE *f = fopen(prefs_path, "r"); + if (f != NULL) { + + // Prefs file found, load settings + LoadPrefsFromStream(f); + fclose(f); + + } else { + + // No prefs file, save defaults + SavePrefs(); + } +} + + +/* + * Save preferences to settings file + */ + +void SavePrefs(void) +{ + // Build a full-path to the settings file + char prefs_path[4096]; + const char * dir = SDL_getenv("HOME"); + if (!dir) { + dir = "./"; + } + SDL_snprintf(prefs_path, sizeof(prefs_path), "%s/%s", dir, PREFS_FILE_NAME); + + FILE *f; + if ((f = fopen(prefs_path, "w")) != NULL) { + SavePrefsToStream(f); + fclose(f); + } +} + + +/* + * Add defaults of platform-specific prefs items + * You may also override the defaults set in PrefsInit() + */ + +void AddPlatformPrefsDefaults(void) +{ +}