1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/cocoa/nsSound.mm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,102 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "nsSound.h" 1.11 +#include "nsObjCExceptions.h" 1.12 +#include "nsNetUtil.h" 1.13 +#include "nsCOMPtr.h" 1.14 +#include "nsIURL.h" 1.15 +#include "nsString.h" 1.16 + 1.17 +#import <Cocoa/Cocoa.h> 1.18 + 1.19 +NS_IMPL_ISUPPORTS(nsSound, nsISound, nsIStreamLoaderObserver) 1.20 + 1.21 +nsSound::nsSound() 1.22 +{ 1.23 +} 1.24 + 1.25 +nsSound::~nsSound() 1.26 +{ 1.27 +} 1.28 + 1.29 +NS_IMETHODIMP 1.30 +nsSound::Beep() 1.31 +{ 1.32 + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; 1.33 + 1.34 + NSBeep(); 1.35 + return NS_OK; 1.36 + 1.37 + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; 1.38 +} 1.39 + 1.40 +NS_IMETHODIMP 1.41 +nsSound::OnStreamComplete(nsIStreamLoader *aLoader, 1.42 + nsISupports *context, 1.43 + nsresult aStatus, 1.44 + uint32_t dataLen, 1.45 + const uint8_t *data) 1.46 +{ 1.47 + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; 1.48 + 1.49 + NSData *value = [NSData dataWithBytes:data length:dataLen]; 1.50 + 1.51 + NSSound *sound = [[NSSound alloc] initWithData:value]; 1.52 + 1.53 + [sound play]; 1.54 + 1.55 + [sound autorelease]; 1.56 + 1.57 + return NS_OK; 1.58 + 1.59 + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; 1.60 +} 1.61 + 1.62 +NS_IMETHODIMP 1.63 +nsSound::Play(nsIURL *aURL) 1.64 +{ 1.65 + nsCOMPtr<nsIURI> uri(do_QueryInterface(aURL)); 1.66 + nsCOMPtr<nsIStreamLoader> loader; 1.67 + return NS_NewStreamLoader(getter_AddRefs(loader), uri, this); 1.68 +} 1.69 + 1.70 +NS_IMETHODIMP 1.71 +nsSound::Init() 1.72 +{ 1.73 + return NS_OK; 1.74 +} 1.75 + 1.76 +NS_IMETHODIMP 1.77 +nsSound::PlaySystemSound(const nsAString &aSoundAlias) 1.78 +{ 1.79 + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; 1.80 + 1.81 + if (NS_IsMozAliasSound(aSoundAlias)) { 1.82 + NS_WARNING("nsISound::playSystemSound is called with \"_moz_\" events, they are obsolete, use nsISound::playEventSound instead"); 1.83 + // Mac doesn't have system sound settings for each user actions. 1.84 + return NS_OK; 1.85 + } 1.86 + 1.87 + NSString *name = [NSString stringWithCharacters:reinterpret_cast<const unichar*>(aSoundAlias.BeginReading()) 1.88 + length:aSoundAlias.Length()]; 1.89 + NSSound *sound = [NSSound soundNamed:name]; 1.90 + if (sound) { 1.91 + [sound stop]; 1.92 + [sound play]; 1.93 + } 1.94 + 1.95 + return NS_OK; 1.96 + 1.97 + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; 1.98 +} 1.99 + 1.100 +NS_IMETHODIMP 1.101 +nsSound::PlayEventSound(uint32_t aEventId) 1.102 +{ 1.103 + // Mac doesn't have system sound settings for each user actions. 1.104 + return NS_OK; 1.105 +}