Qt 4.8
qsound_mac.mm
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
52 
53 void qt_mac_beep()
54 {
55  NSBeep();
56 }
57 
59 
60 #ifndef QT_NO_SOUND
61 
63 
65 static Sounds sounds;
66 
67 class QAuServerMac : public QAuServer
68 {
69  Q_OBJECT
70 public:
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 
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 
90 
91 @interface QT_MANGLE_NAMESPACE(QMacSoundDelegate) : NSObject<NSSoundDelegate> {
92  QSound *qSound; // may be null.
94 }
95 -(id)initWithQSound:(QSound*)sound:(QAuServerMac*)server;
96 @end
97 
98 @implementation QT_MANGLE_NAMESPACE(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 
146 
148 {
150  NSSound * const nsSound = createNSSound(fileName, 0);
151  [nsSound play];
152 }
153 
155 {
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 
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.
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  QT_MANGLE_NAMESPACE(QMacSoundDelegate) * const delegate = [[QT_MANGLE_NAMESPACE(QMacSoundDelegate) alloc] initWithQSound:qSound:this];
176  [nsSound setDelegate:delegate];
177  [nsFileName release];
178  return nsSound;
179 }
180 
182 {
183  return new QAuServerMac(qApp);
184 }
185 
187 
188 #include "qsound_mac.moc"
189 
190 #endif // QT_NO_SOUND
void qt_mac_beep()
Definition: qsound_mac.mm:53
void stop(QSound *)
Definition: qsound_mac.mm:163
The QHash::const_iterator class provides an STL-style const iterator for QHash and QMultiHash...
Definition: qhash.h:395
static Sounds sounds
Definition: qsound_mac.mm:65
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int remove(const Key &key)
Removes all the items that have the key from the hash.
Definition: qhash.h:784
#define it(className, varName)
QHash< QSound *, NSSound const * > Sounds
Definition: qsound_mac.mm:64
QAuServerMac(QObject *parent)
Definition: qsound_mac.mm:71
static CFStringRef toCFStringRef(const QString &str)
Definition: qcore_mac.cpp:69
void play(const QString &filename)
Definition: qsound_mac.mm:147
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
int decLoop(QSound *)
Decrements the QSound::loopRemaining() value for sound s, returning the result.
Definition: qsound.cpp:374
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
QAuServerMac * server
Definition: qsound_mac.mm:93
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
#define qApp
const_iterator constFind(const Key &key) const
Returns an iterator pointing to the item with the key in the hash.
Definition: qhash.h:859
#define Q_OBJECT
Definition: qobjectdefs.h:157
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:469
NSSound * createNSSound(const QString &filename, QSound *qSound)
Definition: qsound_mac.mm:171
#define QT_MANGLE_NAMESPACE(name)
Definition: qglobal.h:106
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
bool okay()
Definition: qsound_mac.mm:75
QAuServer * qt_new_audio_server()
Definition: qsound_mac.mm:181
#define QT_USE_NAMESPACE
This macro expands to using QT_NAMESPACE if QT_NAMESPACE is defined and nothing otherwise.
Definition: qglobal.h:88
QString fileName() const
Returns the filename associated with this QSound object.
Definition: qsound.cpp:312
The QSound class provides access to the platform audio facilities.
Definition: qsound.h:57
static QString fileName(const QString &fileUrl)