| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 | #include <qapplication.h>
|
|---|
| 42 | #include "qsound.h"
|
|---|
| 43 | #include "qsound_p.h"
|
|---|
| 44 | #include <private/qt_mac_p.h>
|
|---|
| 45 | #include <qhash.h>
|
|---|
| 46 | #include <qdebug.h>
|
|---|
| 47 | #import <AppKit/AppKit.h>
|
|---|
| 48 |
|
|---|
| 49 | #include <AppKit/NSSound.h>
|
|---|
| 50 |
|
|---|
| 51 | QT_BEGIN_NAMESPACE
|
|---|
| 52 |
|
|---|
| 53 | void qt_mac_beep()
|
|---|
| 54 | {
|
|---|
| 55 | NSBeep();
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | QT_END_NAMESPACE
|
|---|
| 59 |
|
|---|
| 60 | #ifndef QT_NO_SOUND
|
|---|
| 61 |
|
|---|
| 62 | QT_BEGIN_NAMESPACE
|
|---|
| 63 |
|
|---|
| 64 | typedef QHash<QSound *, NSSound const *> Sounds;
|
|---|
| 65 | static Sounds sounds;
|
|---|
| 66 |
|
|---|
| 67 | class QAuServerMac : public QAuServer
|
|---|
| 68 | {
|
|---|
| 69 | Q_OBJECT
|
|---|
| 70 | public:
|
|---|
| 71 | QAuServerMac(QObject* parent) : QAuServer(parent) { }
|
|---|
| 72 | void play(const QString& filename);
|
|---|
| 73 | void play(QSound *s);
|
|---|
| 74 | void stop(QSound*);
|
|---|
| 75 | bool okay() { return true; }
|
|---|
| 76 | using QAuServer::decLoop; // promote to public.
|
|---|
| 77 | protected:
|
|---|
| 78 | NSSound *createNSSound(const QString &filename, QSound *qSound);
|
|---|
| 79 | };
|
|---|
| 80 |
|
|---|
| 81 | QT_END_NAMESPACE
|
|---|
| 82 |
|
|---|
| 83 | QT_USE_NAMESPACE
|
|---|
| 84 |
|
|---|
| 85 | @interface QMacSoundDelegate : NSObject {
|
|---|
| 86 | QSound *qSound; // may be null.
|
|---|
| 87 | QAuServerMac* server;
|
|---|
| 88 | }
|
|---|
| 89 | -(id)initWithQSound:(QSound*)sound:(QAuServerMac*)server;
|
|---|
| 90 | -(void)sound:(NSSound *)sound didFinishPlaying:(BOOL)aBool;
|
|---|
| 91 | @end
|
|---|
| 92 |
|
|---|
| 93 | @implementation QMacSoundDelegate
|
|---|
| 94 | -(id)initWithQSound:(QSound*)s:(QAuServerMac*)serv {
|
|---|
| 95 | self = [super init];
|
|---|
| 96 | if(self) {
|
|---|
| 97 | qSound = s;
|
|---|
| 98 | server = serv;
|
|---|
| 99 | }
|
|---|
| 100 | return self;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | // Delegate function that gets called each time a sound finishes.
|
|---|
| 104 | -(void)sound:(NSSound *)sound didFinishPlaying:(BOOL)finishedOk
|
|---|
| 105 | {
|
|---|
| 106 | // qSound is null if this sound was started by play(QString),
|
|---|
| 107 | // in which case there is no corresponding QSound object.
|
|---|
| 108 | if (qSound == 0) {
|
|---|
| 109 | [sound release];
|
|---|
| 110 | [self release];
|
|---|
| 111 | return;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | // finishedOk is false if the sound cold not be played or
|
|---|
| 115 | // if it was interrupted by stop().
|
|---|
| 116 | if (finishedOk == false) {
|
|---|
| 117 | sounds.remove(qSound);
|
|---|
| 118 | [sound release];
|
|---|
| 119 | [self release];
|
|---|
| 120 | return;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | // Check if the sound should loop "forever" (until stop).
|
|---|
| 124 | if (qSound->loops() == -1) {
|
|---|
| 125 | [sound play];
|
|---|
| 126 | return;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | const int remainingIterations = server->decLoop(qSound);
|
|---|
| 130 | if (remainingIterations > 0) {
|
|---|
| 131 | [sound play];
|
|---|
| 132 | } else {
|
|---|
| 133 | sounds.remove(qSound);
|
|---|
| 134 | [sound release];
|
|---|
| 135 | [self release];
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 | @end
|
|---|
| 139 |
|
|---|
| 140 | QT_BEGIN_NAMESPACE
|
|---|
| 141 |
|
|---|
| 142 | void QAuServerMac::play(const QString &fileName)
|
|---|
| 143 | {
|
|---|
| 144 | QMacCocoaAutoReleasePool pool;
|
|---|
| 145 | NSSound * const nsSound = createNSSound(fileName, 0);
|
|---|
| 146 | [nsSound play];
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | void QAuServerMac::play(QSound *qSound)
|
|---|
| 150 | {
|
|---|
| 151 | QMacCocoaAutoReleasePool pool;
|
|---|
| 152 | NSSound * const nsSound = createNSSound(qSound->fileName(), qSound);
|
|---|
| 153 | [nsSound play];
|
|---|
| 154 | // Keep track of the nsSound object so we can find it again in stop().
|
|---|
| 155 | sounds[qSound] = nsSound;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | void QAuServerMac::stop(QSound *qSound)
|
|---|
| 159 | {
|
|---|
| 160 | Sounds::const_iterator it = sounds.constFind(qSound);
|
|---|
| 161 | if (it != sounds.constEnd())
|
|---|
| 162 | [*it stop];
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | // Creates an NSSound object and installs a "sound finished" callack delegate on it.
|
|---|
| 166 | NSSound *QAuServerMac::createNSSound(const QString &fileName, QSound *qSound)
|
|---|
| 167 | {
|
|---|
| 168 | NSString *nsFileName = const_cast<NSString *>(reinterpret_cast<const NSString *>(QCFString::toCFStringRef(fileName)));
|
|---|
| 169 | NSSound * const nsSound = [[NSSound alloc] initWithContentsOfFile: nsFileName byReference:YES];
|
|---|
| 170 | QMacSoundDelegate * const delegate = [[QMacSoundDelegate alloc] initWithQSound:qSound:this];
|
|---|
| 171 | [nsSound setDelegate:delegate];
|
|---|
| 172 | return nsSound;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | QAuServer* qt_new_audio_server()
|
|---|
| 176 | {
|
|---|
| 177 | return new QAuServerMac(qApp);
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | QT_END_NAMESPACE
|
|---|
| 181 |
|
|---|
| 182 | #include "qsound_mac.moc"
|
|---|
| 183 |
|
|---|
| 184 | #endif // QT_NO_SOUND
|
|---|