Skip to content
Snippets Groups Projects
Commit cf3769a6 authored by Jonathan Wilkes's avatar Jonathan Wilkes
Browse files

Merge branch 'aggraef/purr-data-osx-loadprefs-fixes'

parents 9366e8ef a6a61b00
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<key>defeatrt</key> <key>defeatrt</key>
<string>0</string> <string>0</string>
<key>flags</key> <key>flags</key>
<string>-helppath ~/Library/Pd -helppath /Library/Pd</string> <string>-helppath ~/Library/Pd-l2ork -helppath /Library/Pd-l2ork</string>
<key>loadlib1</key> <key>loadlib1</key>
<string>libdir</string> <string>libdir</string>
<key>loadlib2</key> <key>loadlib2</key>
......
...@@ -259,15 +259,14 @@ static char *sys_prefbuf; ...@@ -259,15 +259,14 @@ static char *sys_prefbuf;
#define BUFSZ 4096 #define BUFSZ 4096
// AG: We have to go to some lengths here since 'defaults read' doesn't // AG: We have to go to some lengths here since 'defaults read' doesn't
// properly deal with UTF-8 characters in the prefs data. 'defaults export' // properly deal with UTF-8 characters in the prefs data. 'plutil' does the
// does the trick, however, so we use that to read the entire prefs data at // trick, however, so we use that to read the entire prefs data at once from a
// once from a pipe, using plutil to convert the resulting data to JSON format // pipe, converting it to JSON format which can then be translated to Pd's
// which can then be translated to Pd's Unix preferences file format using // Unix preferences file format using sed. The result is stored in a character
// sed. The result is stored in a character buffer for efficient access. From // buffer for efficient access. From there we can retrieve the individual keys
// there we can retrieve the individual keys in the same fashion as on Unix. A // in the same fashion as on Unix. A welcome side effect is that loading the
// welcome side effect is that loading the prefs is *much* faster now than // prefs is *much* faster now than with the previous method which invoked
// with the previous method which invoked 'defaults read' on each individual // 'defaults read' on each individual key.
// key.
static void sys_initloadpreferences(void) static void sys_initloadpreferences(void)
{ {
...@@ -275,8 +274,9 @@ static void sys_initloadpreferences(void) ...@@ -275,8 +274,9 @@ static void sys_initloadpreferences(void)
FILE *fp; FILE *fp;
size_t sz, n = 0; size_t sz, n = 0;
int res; int res;
char default_prefs[FILENAME_MAX]; // default prefs embedded in the package char default_prefs[FILENAME_MAX]; // default prefs embedded in the package
char embedded_prefs[FILENAME_MAX]; // overrides others for standalone app char embedded_prefs[FILENAME_MAX]; // overrides others for standalone app
char user_prefs[FILENAME_MAX]; // user preferences
char embedded_prefs_file[FILENAME_MAX]; char embedded_prefs_file[FILENAME_MAX];
char user_prefs_file[FILENAME_MAX]; char user_prefs_file[FILENAME_MAX];
const char *prefs, *homedir = getenv("HOME"); const char *prefs, *homedir = getenv("HOME");
...@@ -292,9 +292,10 @@ static void sys_initloadpreferences(void) ...@@ -292,9 +292,10 @@ static void sys_initloadpreferences(void)
snprintf(embedded_prefs, FILENAME_MAX, snprintf(embedded_prefs, FILENAME_MAX,
"%s/../org.puredata.pd-l2ork", "%s/../org.puredata.pd-l2ork",
sys_libdir->s_name); sys_libdir->s_name);
snprintf(user_prefs, FILENAME_MAX,
"%s/Library/Preferences/org.puredata.pd-l2ork", homedir);
snprintf(embedded_prefs_file, FILENAME_MAX, "%s.plist", embedded_prefs); snprintf(embedded_prefs_file, FILENAME_MAX, "%s.plist", embedded_prefs);
snprintf(user_prefs_file, FILENAME_MAX, snprintf(user_prefs_file, FILENAME_MAX, "%s.plist", user_prefs);
"%s/Library/Preferences/org.puredata.pd-l2ork.plist", homedir);
if (stat(embedded_prefs_file, &statbuf) == 0) { if (stat(embedded_prefs_file, &statbuf) == 0) {
// Read from and write to the embedded prefs (standalone app). // Read from and write to the embedded prefs (standalone app).
prefs = embedded_prefs; prefs = embedded_prefs;
...@@ -302,17 +303,16 @@ static void sys_initloadpreferences(void) ...@@ -302,17 +303,16 @@ static void sys_initloadpreferences(void)
} else if (stat(user_prefs_file, &statbuf) == 0) { } else if (stat(user_prefs_file, &statbuf) == 0) {
// Read from and write to the user prefs. // Read from and write to the user prefs.
prefs = current_prefs; prefs = current_prefs;
strcpy(current_prefs, "org.puredata.pd-l2ork"); strncpy(current_prefs, user_prefs, FILENAME_MAX);
} else { } else {
// Read from the package defaults and write to the user prefs. // Read from the package defaults and write to the user prefs.
prefs = default_prefs; prefs = default_prefs;
strcpy(current_prefs, "org.puredata.pd-l2ork"); strncpy(current_prefs, user_prefs, FILENAME_MAX);
} }
// This looks complicated, but is rather straightforward. The individual // This looks complicated, but is rather straightforward. The individual
// stages of the pipe are: // stages of the pipe are:
// 1. defaults export: grab our defaults in XML format // 1. plutil -convert json -r -o -: grab our defaults and convert to JSON
// 2. plutil -convert json -r -o - -: convert to JSON // 2. sed: a few edits remove the extra JSON bits (curly braces, string
// 3. sed: a few edits remove the extra JSON bits (curly braces, string
// quotes, unwanted whitespace and character escapes) and produce // quotes, unwanted whitespace and character escapes) and produce
// Pd-L2Ork's Unix prefs format, i.e.: // Pd-L2Ork's Unix prefs format, i.e.:
// JSON --> Unix prefs // JSON --> Unix prefs
...@@ -322,8 +322,7 @@ static void sys_initloadpreferences(void) ...@@ -322,8 +322,7 @@ static void sys_initloadpreferences(void)
// "path1" : "\/System\/Library\/Fonts" path1: /System/Library/Fonts // "path1" : "\/System\/Library\/Fonts" path1: /System/Library/Fonts
// } // }
snprintf(cmdbuf, MAXPDSTRING, snprintf(cmdbuf, MAXPDSTRING,
"defaults export %s - " "plutil -convert json -r -o - %s.plist "
"| plutil -convert json -r -o - - "
"| sed -E " "| sed -E "
"-e 's/[{}]//g' " "-e 's/[{}]//g' "
"-e 's/^ *\"(([^\"]|\\\\.)*)\" *: *\"(([^\"]|\\\\.)*)\".*/\\1: \\3/' " "-e 's/^ *\"(([^\"]|\\\\.)*)\" *: *\"(([^\"]|\\\\.)*)\".*/\\1: \\3/' "
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment