X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=OSX%2FPrefsReader.m;h=95dc8ebe66dd35affbcbdc894babdaf105cf1164;hb=6f5482d73adb0165c0130bb47d852644ab0c4869;hp=3c20f3dcf328db28ecbd24bba93b6a83b39a334a;hpb=49f5b54f312fe4ac2e9bc47581a72451bd0e8439;p=xscreensaver diff --git a/OSX/PrefsReader.m b/OSX/PrefsReader.m index 3c20f3dc..95dc8ebe 100644 --- a/OSX/PrefsReader.m +++ b/OSX/PrefsReader.m @@ -1,4 +1,4 @@ -/* xscreensaver, Copyright (c) 2006 Jamie Zawinski +/* xscreensaver, Copyright (c) 2006-2012 Jamie Zawinski * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that @@ -16,12 +16,39 @@ the UI (XScreenSaverConfigSheet). */ -#import +#ifndef USE_IPHONE +# import +#endif + #import "PrefsReader.h" #import "screenhackI.h" @implementation PrefsReader +/* Normally we read resources by looking up "KEY" in the database + "org.jwz.xscreensaver.SAVERNAME". But in the all-in-one iPhone + app, everything is stored in the database "org.jwz.xscreensaver" + instead, so transform keys to "SAVERNAME.KEY". + + NOTE: This is duplicated in XScreenSaverConfigSheet.m, cause I suck. +*/ +- (NSString *) makeKey:(NSString *)key +{ +# ifdef USE_IPHONE + NSString *prefix = [saver_name stringByAppendingString:@"."]; + if (! [key hasPrefix:prefix]) // Don't double up! + key = [prefix stringByAppendingString:key]; +# endif + return key; +} + +- (NSString *) makeCKey:(const char *)key +{ + return [self makeKey:[NSString stringWithCString:key + encoding:NSUTF8StringEncoding]]; +} + + /* Converts an array of "key:value" strings to an NSDictionary. */ - (NSDictionary *) defaultsToDict: (const char * const *) defs @@ -49,8 +76,7 @@ // decide whether it's a boolean, int, float, or string, and store // an object of the appropriate type in the prefs. // - NSString *nskey = [NSString stringWithCString:key - encoding:NSUTF8StringEncoding]; + NSString *nskey = [self makeCKey:key]; NSObject *nsval; int dd; double ff; @@ -66,7 +92,7 @@ else nsval = [NSString stringWithCString:val encoding:NSUTF8StringEncoding]; -// NSLog (@"default: \"%@\" = \"%@\" [%@]\n", nskey, nsval, [nsval class]); +// NSLog (@"default: \"%@\" = \"%@\" [%@]", nskey, nsval, [nsval class]); [dict setValue:nsval forKey:nskey]; free (line); defs++; @@ -88,9 +114,24 @@ NSDictionary *defsdict = [self defaultsToDict:defs]; [userDefaults registerDefaults:defsdict]; + // Save a copy of the default options, since iOS doesn't have + // [userDefaultsController initialValues]. + // + if (defaultOptions) + [defaultOptions release]; + defaultOptions = [[NSMutableDictionary dictionaryWithCapacity:20] + retain]; + for (NSString *key in defsdict) { + [defaultOptions setValue:[defsdict objectForKey:key] forKey:key]; + } + +# ifndef USE_IPHONE userDefaultsController = [[NSUserDefaultsController alloc] initWithDefaults:userDefaults initialValues:defsdict]; +# else // USE_IPHONE + userDefaultsController = userDefaults; +# endif // USE_IPHONE NSDictionary *optsdict = [NSMutableDictionary dictionaryWithCapacity:20]; @@ -100,15 +141,15 @@ while (*resource == '.' || *resource == '*') resource++; - NSString *nsresource = [NSString stringWithCString:resource - encoding:NSUTF8StringEncoding]; + NSString *nsresource = [self makeCKey:resource]; // make sure there's no resource mentioned in options and not defaults. if (![defsdict objectForKey:nsresource]) { - if (! (!strcmp(resource, "font") || // don't warn about these + if (! (!strcmp(resource, "font") || // don't warn about these !strcmp(resource, "textLiteral") || - !strcmp(resource, "textFile") || - !strcmp(resource, "textURL") || + !strcmp(resource, "textFile") || + !strcmp(resource, "textURL") || + !strcmp(resource, "textProgram") || !strcmp(resource, "imageDirectory"))) NSLog (@"warning: \"%s\" is in options but not defaults", resource); } @@ -117,11 +158,9 @@ opts++; } - // make sure there's no resource mentioned in defaults and not options. - NSEnumerator *enumerator = [defsdict keyEnumerator]; - NSString *key; - while ((key = [enumerator nextObject])) { #if 0 + // make sure there's no resource mentioned in defaults and not options. + for (NSString *key in defsdict) { if (! [optsdict objectForKey:key]) if (! ([key isEqualToString:@"foreground"] || // don't warn about these [key isEqualToString:@"background"] || @@ -142,8 +181,18 @@ [key isEqualToString:@"TVTint"] )) NSLog (@"warning: \"%@\" is in defaults but not options", key); + } #endif /* 0 */ + +#if 0 + // Dump the entire resource database. + NSDictionary *d = [userDefaults dictionaryRepresentation]; + for (NSObject *key in [[d allKeys] + sortedArrayUsingSelector:@selector(compare:)]) { + NSObject *val = [d objectForKey:key]; + NSLog (@"%@ = %@", key, val); } +#endif } @@ -153,12 +202,17 @@ return userDefaultsController; } +- (NSDictionary *) defaultOptions +{ + NSAssert(defaultOptions, @"userDefaultsController uninitialized"); + return defaultOptions; +} + - (NSObject *) getObjectResource: (const char *) name { while (1) { - NSString *key = [NSString stringWithCString:name - encoding:NSUTF8StringEncoding]; + NSString *key = [self makeCKey:name]; NSObject *obj = [userDefaults objectForKey:key]; if (obj) return obj; @@ -177,7 +231,7 @@ - (char *) getStringResource: (const char *) name { NSObject *o = [self getObjectResource:name]; - //NSLog(@"%s = %@\n",name,o); + //NSLog(@"%s = %@",name,o); if (o == nil) { if (! (!strcmp(name, "eraseMode") || // erase.c // xlockmore.c reads all of these whether used or not... @@ -188,26 +242,39 @@ !strcmp(name, "font") || !strcmp(name, "labelFont") || // grabclient.c !strcmp(name, "titleFont") || - !strcmp(name, "background") + !strcmp(name, "fpsFont") || // fps.c + !strcmp(name, "foreground") || // fps.c + !strcmp(name, "background") || + !strcmp(name, "textLiteral") )) - NSLog(@"warning: no preference \"%s\" [string]\n", name); + NSLog(@"warning: no preference \"%s\" [string]", name); return NULL; } -#if 0 - if (! [o isKindOfClass:[NSString class]]) { - NSAssert2(0, @"%s = \"%@\" but should have been an NSString", name, o); - abort(); - } -#else if (! [o isKindOfClass:[NSString class]]) { NSLog(@"asked for %s as a string, but it is a %@", name, [o class]); o = [(NSNumber *) o stringValue]; } -#endif NSString *os = (NSString *) o; - const char *result = [os cStringUsingEncoding:NSUTF8StringEncoding]; - return strdup (result); + char *result = strdup ([os cStringUsingEncoding:NSUTF8StringEncoding]); + + // Kludge: if the string is surrounded with single-quotes, remove them. + // This happens when the .xml file says things like arg="-foo 'bar baz'" + if (result[0] == '\'' && result[strlen(result)-1] == '\'') { + result[strlen(result)-1] = 0; + strcpy (result, result+1); + } + + // Kludge: assume that any string that begins with "~" and has a "/" + // anywhere in it should be expanded as if it is a pathname. + if (result[0] == '~' && strchr (result, '/')) { + os = [NSString stringWithCString:result encoding:NSUTF8StringEncoding]; + free (result); + result = strdup ([[os stringByExpandingTildeInPath] + cStringUsingEncoding:NSUTF8StringEncoding]); + } + + return result; } @@ -227,9 +294,10 @@ !strcmp(name, "mono") || !strcmp(name, "count") || !strcmp(name, "ncolors") || + !strcmp(name, "doFPS") || // fps.c !strcmp(name, "eraseSeconds") // erase.c )) - NSLog(@"warning: no preference \"%s\" [float]\n", name); + NSLog(@"warning: no preference \"%s\" [float]", name); return 0.0; } if ([o isKindOfClass:[NSString class]]) { @@ -245,17 +313,40 @@ - (int) getIntegerResource: (const char *) name { - return (int) [self getFloatResource:name]; + // Sliders might store float values for integral resources; round them. + float v = [self getFloatResource:name]; + int i = (int) (v + (v < 0 ? -0.5 : 0.5)); // ignore sign or -1 rounds to 0 + // if (i != v) NSLog(@"%s: rounded %.3f to %d", name, v, i); + return i; } - (BOOL) getBooleanResource: (const char *) name { - int n = [self getIntegerResource:name]; - if (n == 0) return NO; - else if (n == 1) return YES; - else { - NSAssert2(0, @"%s = %d but should have been 0 or 1\n", name, n); + NSObject *o = [self getObjectResource:name]; + if (! o) { + return NO; + } else if ([o isKindOfClass:[NSNumber class]]) { + double n = [(NSNumber *) o doubleValue]; + if (n == 0) return NO; + else if (n == 1) return YES; + else goto FAIL; + } else if ([o isKindOfClass:[NSString class]]) { + NSString *s = (NSString *) o; + if ([s isEqualToString:@"true"] || + [s isEqualToString:@"yes"] || + [s isEqualToString:@"1"]) + return YES; + else if ([s isEqualToString:@"false"] || + [s isEqualToString:@"no"] || + [s isEqualToString:@"0"] || + [s isEqualToString:@""]) + return NO; + else + goto FAIL; + } else { + FAIL: + NSAssert2(0, @"%s = \"%@\" but should have been a boolean", name, o); abort(); } } @@ -268,7 +359,18 @@ self = [self init]; if (!self) return nil; +# ifndef USE_IPHONE userDefaults = [ScreenSaverDefaults defaultsForModuleWithName:name]; +# else // USE_IPHONE + userDefaults = [NSUserDefaults standardUserDefaults]; +# endif // USE_IPHONE + + // Convert "org.jwz.xscreensaver.NAME" to just "NAME". + NSRange r = [name rangeOfString:@"." options:NSBackwardsSearch]; + if (r.length) + name = [name substringFromIndex:r.location+1]; + name = [name stringByReplacingOccurrencesOfString:@" " withString:@""]; + saver_name = [name retain]; [self registerXrmKeys:opts defaults:defs]; return self; @@ -276,6 +378,7 @@ - (void) dealloc { + [saver_name release]; [userDefaultsController release]; [super dealloc]; }