Commit 1dabebb8 authored by Albert Gräf's avatar Albert Gräf
Browse files

Speed up the saving of the preferences on macOS.

This is done by importing the config data using 'defaults import' in
one go, which is *much* faster than the previous implementation which
invoked the shell to run 'defaults write' for each individual key.
parent f366c4cf
...@@ -269,11 +269,6 @@ static char *sys_prefbuf; ...@@ -269,11 +269,6 @@ static char *sys_prefbuf;
// with the previous method which invoked 'defaults read' on each individual // with the previous method which invoked 'defaults read' on each individual
// key. // key.
// XXXTODO: In principle, this approach should also work in reverse to import
// the data into the defaults storage in one go. Presumably this should also
// be much faster than the current implementation which invokes the shell to
// run 'defaults write' for each individual key.
static void sys_initloadpreferences(void) static void sys_initloadpreferences(void)
{ {
char cmdbuf[MAXPDSTRING], *buf; char cmdbuf[MAXPDSTRING], *buf;
...@@ -317,7 +312,7 @@ static void sys_initloadpreferences(void) ...@@ -317,7 +312,7 @@ static void sys_initloadpreferences(void)
// stages of the pipe are: // stages of the pipe are:
// 1. defaults export: grab our defaults in XML format // 1. defaults export: grab our defaults in XML format
// 2. plutil -convert json -r -o - -: convert to JSON // 2. plutil -convert json -r -o - -: convert to JSON
// 3. sed: a few edits remove the extra JSON bits (curly brances, 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
...@@ -413,21 +408,56 @@ static void sys_doneloadpreferences( void) ...@@ -413,21 +408,56 @@ static void sys_doneloadpreferences( void)
sys_prefbuf = NULL; sys_prefbuf = NULL;
} }
// AG: We use a similar approach here to import the data into the defaults
// storage in one go. To these ends, a temporary plist file in xml format is
// created which is then submitted to 'defaults import'. This is *much* faster
// than the previous implementation which invoked the shell to run 'defaults
// write' for each individual key.
#define save_prefs_template "/tmp/pd-l2ork.defaults.plist.XXXXXX"
static FILE *save_fp;
static char save_prefs[] = save_prefs_template;
static void sys_initsavepreferences( void) static void sys_initsavepreferences( void)
{ {
strcpy(save_prefs, save_prefs_template);
int fd = mkstemp(save_prefs);
if (fd < 0) {
error("save preferences: %s", strerror(errno));
return;
}
save_fp = fdopen(fd, "w");
if (!save_fp) {
error("save preferences: %s", strerror(errno));
return;
}
fprintf(save_fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
"<plist version=\"1.0\">\n"
"<dict>\n");
} }
static void sys_putpreference(const char *key, const char *value) static void sys_putpreference(const char *key, const char *value)
{ {
char cmdbuf[MAXPDSTRING]; if (!save_fp) return;
snprintf(cmdbuf, MAXPDSTRING, fprintf(save_fp,
"defaults write '%s' %s \"%s\" 2> /dev/null\n", "<key>%s</key>\n<string>%s</string>\n",
current_prefs, key, value); key, value);
system(cmdbuf);
} }
static void sys_donesavepreferences( void) static void sys_donesavepreferences( void)
{ {
if (!save_fp) return;
fprintf(save_fp, "</dict>\n</plist>\n");
fclose(save_fp);
save_fp = 0;
char cmdbuf[MAXPDSTRING];
snprintf(cmdbuf, MAXPDSTRING,
"defaults import '%s' '%s' 2> /dev/null",
current_prefs, save_prefs);
system(cmdbuf);
unlink(save_prefs);
} }
#endif /* __APPLE__ */ #endif /* __APPLE__ */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment