|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtGui module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
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 #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5 |
|
84 @protocol NSSoundDelegate <NSObject> |
|
85 -(void)sound:(NSSound *)sound didFinishPlaying:(BOOL)aBool; |
|
86 @end |
|
87 #endif |
|
88 |
|
89 QT_USE_NAMESPACE |
|
90 |
|
91 @interface QMacSoundDelegate : NSObject<NSSoundDelegate> { |
|
92 QSound *qSound; // may be null. |
|
93 QAuServerMac* server; |
|
94 } |
|
95 -(id)initWithQSound:(QSound*)sound:(QAuServerMac*)server; |
|
96 @end |
|
97 |
|
98 @implementation QMacSoundDelegate |
|
99 -(id)initWithQSound:(QSound*)s:(QAuServerMac*)serv { |
|
100 self = [super init]; |
|
101 if(self) { |
|
102 qSound = s; |
|
103 server = serv; |
|
104 } |
|
105 return self; |
|
106 } |
|
107 |
|
108 // Delegate function that gets called each time a sound finishes. |
|
109 -(void)sound:(NSSound *)sound didFinishPlaying:(BOOL)finishedOk |
|
110 { |
|
111 // qSound is null if this sound was started by play(QString), |
|
112 // in which case there is no corresponding QSound object. |
|
113 if (qSound == 0) { |
|
114 [sound release]; |
|
115 [self release]; |
|
116 return; |
|
117 } |
|
118 |
|
119 // finishedOk is false if the sound cold not be played or |
|
120 // if it was interrupted by stop(). |
|
121 if (finishedOk == false) { |
|
122 sounds.remove(qSound); |
|
123 [sound release]; |
|
124 [self release]; |
|
125 return; |
|
126 } |
|
127 |
|
128 // Check if the sound should loop "forever" (until stop). |
|
129 if (qSound->loops() == -1) { |
|
130 [sound play]; |
|
131 return; |
|
132 } |
|
133 |
|
134 const int remainingIterations = server->decLoop(qSound); |
|
135 if (remainingIterations > 0) { |
|
136 [sound play]; |
|
137 } else { |
|
138 sounds.remove(qSound); |
|
139 [sound release]; |
|
140 [self release]; |
|
141 } |
|
142 } |
|
143 @end |
|
144 |
|
145 QT_BEGIN_NAMESPACE |
|
146 |
|
147 void QAuServerMac::play(const QString &fileName) |
|
148 { |
|
149 QMacCocoaAutoReleasePool pool; |
|
150 NSSound * const nsSound = createNSSound(fileName, 0); |
|
151 [nsSound play]; |
|
152 } |
|
153 |
|
154 void QAuServerMac::play(QSound *qSound) |
|
155 { |
|
156 QMacCocoaAutoReleasePool pool; |
|
157 NSSound * const nsSound = createNSSound(qSound->fileName(), qSound); |
|
158 [nsSound play]; |
|
159 // Keep track of the nsSound object so we can find it again in stop(). |
|
160 sounds[qSound] = nsSound; |
|
161 } |
|
162 |
|
163 void QAuServerMac::stop(QSound *qSound) |
|
164 { |
|
165 Sounds::const_iterator it = sounds.constFind(qSound); |
|
166 if (it != sounds.constEnd()) |
|
167 [*it stop]; |
|
168 } |
|
169 |
|
170 // Creates an NSSound object and installs a "sound finished" callack delegate on it. |
|
171 NSSound *QAuServerMac::createNSSound(const QString &fileName, QSound *qSound) |
|
172 { |
|
173 NSString *nsFileName = const_cast<NSString *>(reinterpret_cast<const NSString *>(QCFString::toCFStringRef(fileName))); |
|
174 NSSound * const nsSound = [[NSSound alloc] initWithContentsOfFile: nsFileName byReference:YES]; |
|
175 QMacSoundDelegate * const delegate = [[QMacSoundDelegate alloc] initWithQSound:qSound:this]; |
|
176 [nsSound setDelegate:delegate]; |
|
177 return nsSound; |
|
178 } |
|
179 |
|
180 QAuServer* qt_new_audio_server() |
|
181 { |
|
182 return new QAuServerMac(qApp); |
|
183 } |
|
184 |
|
185 QT_END_NAMESPACE |
|
186 |
|
187 #include "qsound_mac.moc" |
|
188 |
|
189 #endif // QT_NO_SOUND |