Source/GGBUtils.m
author Jens Alfke <jens@mooseyard.com>
Sat Jul 05 17:46:43 2008 -0700 (2008-07-05)
changeset 11 436cbdf56810
parent 9 a59acc683080
child 12 4e567e11f45f
permissions -rw-r--r--
* Improved drag-and-drop (supports CandyBar)
* Fixed DiscPiece
* Inheritable layer styles
etc.
     1 /*  Copyright © 2008 Jens Alfke. All Rights Reserved.
     2 
     3     Redistribution and use in source and binary forms, with or without modification, are permitted
     4     provided that the following conditions are met:
     5 
     6     * Redistributions of source code must retain the above copyright notice, this list of conditions
     7       and the following disclaimer.
     8     * Redistributions in binary form must reproduce the above copyright notice, this list of
     9       conditions and the following disclaimer in the documentation and/or other materials provided
    10       with the distribution.
    11 
    12     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
    13     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
    14     FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
    15     BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    16     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
    17     PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    18     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
    19     THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    20 */
    21 #import "GGBUtils.h"
    22 
    23 #if TARGET_OS_IPHONE
    24 #import <AudioToolbox/AudioToolbox.h>
    25 #endif
    26 
    27 
    28 void setObj( id *variable, id newValue )
    29 {
    30     if( *variable != newValue ) {
    31         [*variable release];
    32         *variable = [newValue retain];
    33     }
    34 }
    35 
    36 void setObjCopy( id<NSCopying> *variable, id<NSCopying> newValue )
    37 {
    38     if( *variable != newValue ) {
    39         [*variable release];
    40         *variable = [(id)newValue copy];
    41     }
    42 }
    43 
    44 
    45 void DelayFor( NSTimeInterval interval )
    46 {
    47     NSDate *end = [NSDate dateWithTimeIntervalSinceNow: interval];
    48     while( [end timeIntervalSinceNow] > 0 ) {
    49         if( ! [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: end] )
    50             break;
    51     }
    52 }    
    53 
    54 
    55 #if TARGET_OS_IPHONE
    56 static SystemSoundID GetSound( NSString *name )
    57 {
    58     static NSMutableDictionary *sSoundIDs;
    59     NSNumber *soundIDObj = [sSoundIDs objectForKey: name];
    60     if( ! soundIDObj ) {
    61         NSLog(@"Loading sound '%@'",name);
    62         NSString *type = name.pathExtension;
    63         if( ! type.length )
    64             type = @"aiff";
    65         NSString *path = [[NSBundle mainBundle] pathForResource: name.stringByDeletingPathExtension
    66                                                          ofType: type];
    67         NSURL *url;
    68         if( path )
    69             url = [NSURL fileURLWithPath: path];
    70         else {
    71             NSLog(@"Couldn't find sound %@",name);
    72             return 0;
    73         }
    74         //url = [NSURL fileURLWithPath: [@"/Library/Sounds/" stringByAppendingPathComponent: name]];
    75         SystemSoundID soundID;
    76         if( AudioServicesCreateSystemSoundID((CFURLRef)url,&soundID) != noErr ) {
    77             NSLog(@"Couldn't load sound %@",url);
    78             return 0;
    79         }
    80         
    81         soundIDObj = [NSNumber numberWithUnsignedInt: soundID];
    82         if( ! sSoundIDs )
    83             sSoundIDs = [[NSMutableDictionary alloc] init];
    84         [sSoundIDs setObject: soundIDObj forKey: name];
    85     }
    86     return [soundIDObj unsignedIntValue];
    87 }
    88 #endif
    89 
    90 
    91 void PreloadSound( NSString* name )
    92 {
    93 #if TARGET_OS_IPHONE
    94     GetSound(name);
    95 #else
    96     NSSound *sound = [[NSSound soundNamed: @"Pop"] copy];
    97     sound.volume = 0;
    98     [sound play];
    99     [sound release];
   100 #endif
   101 }    
   102 
   103 
   104 void PlaySound( NSString* name )
   105 {
   106 #if TARGET_OS_IPHONE
   107     AudioServicesPlaySystemSound( GetSound(name) );
   108 #else
   109     [[NSSound soundNamed: name] play];
   110 #endif
   111 }
   112 
   113 void Beep()
   114 {
   115 #if TARGET_OS_IPHONE
   116     AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
   117 #else
   118     NSBeep();
   119 #endif
   120 }