osx_common: cache OSX version number

This commit makes `is_osx_version_at_least` cache the result of reading
`/System/Library/CoreServices/SystemVersion.plist`. Since that is a file
read operation it was bad to use this function frequently (i.e.: when
processing user events).

Remove `is_lion_or_above` (introduced in c9396c0a) as that was a more
specialized wrapper which had the only advantage of adding it's own cache.
This commit is contained in:
Stefano Pigozzi 2013-01-27 18:30:36 +01:00
parent fddba2d529
commit a3322cb171
2 changed files with 33 additions and 36 deletions

View File

@ -147,20 +147,10 @@ static struct vo_cocoa_state *vo_cocoa_init_state(struct vo *vo)
return s;
}
static bool is_lion_or_above()
{
static bool result = false, checked = false;
if (!checked) {
result = is_osx_version_at_least(10, 7, 0);
checked = true;
}
return result;
}
static bool supports_hidpi(NSView *view)
{
SEL hdpi_selector = @selector(setWantsBestResolutionOpenGLSurface:);
return is_lion_or_above() && view &&
return is_osx_version_at_least(10, 7, 0) && view &&
[view respondsToSelector:hdpi_selector];
}
@ -765,7 +755,8 @@ void create_menu()
delta = - [theEvent deltaX];
}
if (is_lion_or_above() && [theEvent hasPreciseScrollingDeltas]) {
if (is_osx_version_at_least(10, 7, 0) &&
[theEvent hasPreciseScrollingDeltas]) {
s->accumulated_scroll += delta;
static const CGFloat threshold = 10;
while (s->accumulated_scroll >= threshold) {

View File

@ -124,33 +124,39 @@ int convert_key(unsigned key, unsigned charcode)
*/
int is_osx_version_at_least(int majorv, int minorv, int bugfixv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *plist = @"/System/Library/CoreServices/SystemVersion.plist";
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:plist];
NSString *version = [dict objectForKey:@"ProductVersion"];
NSArray *components = [version componentsSeparatedByString:@"."];
int rv = 0;
// Initialize cache
static int c_majorv = -1, c_minorv = -1, c_bugfixv = -1;
// All the above code just sends messages to nil. If anything failed,
// we just end up with an invalid components array.
if ([components count] != 3) {
mp_msg(MSGT_VO, MSGL_ERR, "[osx] Failed to get your system version. "
"Please open a bug report.\n");
goto cleanup_and_return;
// If version cache is empty, fill it
if (c_majorv < 0 && c_minorv < 0 && c_bugfixv < 0) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *plist = @"/System/Library/CoreServices/SystemVersion.plist";
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:plist];
NSString *version = [dict objectForKey:@"ProductVersion"];
NSArray *components = [version componentsSeparatedByString:@"."];
// All the above code just sends messages to nil. If anything failed,
// we just end up with an invalid components array.
if ([components count] != 3) {
mp_msg(MSGT_VO, MSGL_ERR, "[osx] Failed to get your system version. "
"Please open a bug report.\n");
[pool release];
return -1;
}
c_majorv = [[components objectAtIndex:0] intValue];
c_minorv = [[components objectAtIndex:1] intValue];
c_bugfixv = [[components objectAtIndex:2] intValue];
[pool release];
}
int major = [[components objectAtIndex:0] intValue];
int minor = [[components objectAtIndex:1] intValue];
int bugfix = [[components objectAtIndex:2] intValue];
if(major > majorv ||
(major == majorv && (minor > minorv ||
(minor == minorv && bugfix >= bugfixv))))
rv = 1;
cleanup_and_return:
[pool release];
return rv;
if(c_majorv > majorv ||
(c_majorv == majorv && (c_minorv > minorv ||
(c_minorv == minorv && c_bugfixv >= bugfixv))))
return 1;
else
return 0;
}
struct escape_couple {