Qt 4.8
qsound_win.cpp
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 
42 #include "qsound.h"
43 
44 #ifndef QT_NO_SOUND
45 
46 #include "qapplication.h"
47 #include "qapplication_p.h"
48 #include <qfile.h>
49 #include "qpointer.h"
50 #include "qsound_p.h"
51 
52 #include <qt_windows.h>
53 
55 
56 class QAuServerWindows : public QAuServer {
57  Q_OBJECT
58 
59 public:
62 
63  void playHelper(const QString &filename, int loop, QSound *snd);
64  void play(const QString& filename, int loop);
65  void play(QSound*);
66 
67  void stop(QSound*);
68  bool okay();
69 
70  int decLoop(QSound *snd) { return QAuServer::decLoop(snd); }
71 
75 };
76 
78  QAuServer(parent), current(0)
79 {
80  mutex = CreateMutex(0, 0, 0);
81  event = CreateEvent(0, FALSE, FALSE, 0);
82 }
83 
85 {
86  HANDLE mtx = mutex;
87  WaitForSingleObject(mtx, INFINITE);
88  mutex = 0;
89 
90  ReleaseMutex(mtx);
91  CloseHandle(mtx);
92  CloseHandle(event);
93 }
94 
95 struct SoundInfo
96 {
97  SoundInfo(const QString &fn, int lp, QSound *snd, QAuServerWindows *srv)
98  : sound(snd), server(srv), filename(fn), loops(lp)
99  {
100  }
101 
105  int loops;
106 };
107 
108 DWORD WINAPI SoundPlayProc(LPVOID param)
109 {
110  SoundInfo *info = (SoundInfo*)param;
111 
112  // copy data before waking up GUI thread
113  QAuServerWindows *server = info->server;
114  QSound *sound = info->sound;
115  int loops = info->loops;
116  QString filename = info->filename;
117  HANDLE mutex = server->mutex;
118  HANDLE event = server->event;
119  info = 0;
120 
121  // server must not be destroyed until thread finishes
122  // and all other sounds have to wait
123  WaitForSingleObject(mutex, INFINITE);
124 
125  if (loops <= 1) {
126  server->current = 0;
127  int flags = SND_FILENAME|SND_ASYNC;
128  if (loops == -1)
129  flags |= SND_LOOP;
130 
131  PlaySound((wchar_t*)filename.utf16(), 0, flags);
132  if (sound && loops == 1)
133  server->decLoop(sound);
134 
135  // GUI thread continues, but we are done as well.
136  SetEvent(event);
137  } else {
138  // signal GUI thread to continue - sound might be reset!
139  QPointer<QSound> guarded_sound = sound;
140  SetEvent(event);
141 
142  for (int l = 0; l < loops && server->current; ++l) {
143  PlaySound((wchar_t*)filename.utf16(), 0, SND_FILENAME | SND_SYNC);
144 
145  if (guarded_sound)
146  server->decLoop(guarded_sound);
147  }
148  server->current = 0;
149  }
150  ReleaseMutex(mutex);
151 
152  return 0;
153 }
154 
155 void QAuServerWindows::playHelper(const QString &filename, int loop, QSound *snd)
156 {
157  if (loop == 0)
158  return;
159  // busy?
160  if (WaitForSingleObject(mutex, 0) == WAIT_TIMEOUT)
161  return;
162  ReleaseMutex(mutex);
163 
164  DWORD threadid = 0;
165  SoundInfo info(filename, loop, snd, this);
166  current = CreateThread(0, 0, SoundPlayProc, &info, 0, &threadid);
167  CloseHandle(current);
168 
169  WaitForSingleObject(event, INFINITE);
170 }
171 
172 void QAuServerWindows::play(const QString& filename, int loop)
173 {
174  playHelper(filename, loop, 0);
175 }
176 
178 {
179  playHelper(s->fileName(), s->loops(), s);
180 }
181 
183 {
184  // stop unlooped sound
185  if (!current)
186  PlaySound(0, 0, 0);
187  // stop after loop is done
188  current = 0;
189 }
190 
192 {
193  return true;
194 }
195 
197 {
198  return new QAuServerWindows(qApp);
199 }
200 
202 
203 #include "qsound_win.moc"
204 
205 #endif // QT_NO_SOUND
void stop(QSound *)
Definition: qsound_win.cpp:182
QAuServerWindows(QObject *parent)
Definition: qsound_win.cpp:77
static mach_timebase_info_data_t info
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QAuServerWindows * server
Definition: qsound_win.cpp:103
The QString class provides a Unicode character string.
Definition: qstring.h:83
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
The QPointer class is a template class that provides guarded pointers to QObject. ...
Definition: qpointer.h:54
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void play(const QString &filename, int loop)
Definition: qsound_win.cpp:172
#define qApp
DWORD WINAPI SoundPlayProc(LPVOID param)
Definition: qsound_win.cpp:108
#define FALSE
Synonym for false.
Definition: qglobal.h:1019
void * HANDLE
Definition: qnamespace.h:1671
#define Q_OBJECT
Definition: qobjectdefs.h:157
QSound * sound
Definition: qsound_win.cpp:102
QString filename
Definition: qsound_win.cpp:104
static QAuServer & server()
Definition: qsound.cpp:79
void playHelper(const QString &filename, int loop, QSound *snd)
Definition: qsound_win.cpp:155
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QFactoryLoader * l
int decLoop(QSound *snd)
Definition: qsound_win.cpp:70
QString fileName() const
Returns the filename associated with this QSound object.
Definition: qsound.cpp:312
QAuServer * qt_new_audio_server()
Definition: qsound_win.cpp:196
The QSound class provides access to the platform audio facilities.
Definition: qsound.h:57
SoundInfo(const QString &fn, int lp, QSound *snd, QAuServerWindows *srv)
Definition: qsound_win.cpp:97
int loops() const
Returns the number of times the sound will play.
Definition: qsound.cpp:269
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290