| 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 |
|
|---|
| 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 |
|
|---|
| 54 | QT_BEGIN_NAMESPACE
|
|---|
| 55 |
|
|---|
| 56 | class QAuServerWindows : public QAuServer {
|
|---|
| 57 | Q_OBJECT
|
|---|
| 58 |
|
|---|
| 59 | public:
|
|---|
| 60 | QAuServerWindows(QObject* parent);
|
|---|
| 61 | ~QAuServerWindows();
|
|---|
| 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 |
|
|---|
| 72 | HANDLE current;
|
|---|
| 73 | HANDLE mutex;
|
|---|
| 74 | HANDLE event;
|
|---|
| 75 | };
|
|---|
| 76 |
|
|---|
| 77 | QAuServerWindows::QAuServerWindows(QObject* parent) :
|
|---|
| 78 | QAuServer(parent), current(0)
|
|---|
| 79 | {
|
|---|
| 80 | QT_WA({
|
|---|
| 81 | mutex = CreateMutexW(0, 0, 0);
|
|---|
| 82 | event = CreateEventW(0, FALSE, FALSE, 0);
|
|---|
| 83 | } , {
|
|---|
| 84 | mutex = CreateMutexA(0, 0, 0);
|
|---|
| 85 | event = CreateEventA(0, FALSE, FALSE, 0);
|
|---|
| 86 | });
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | QAuServerWindows::~QAuServerWindows()
|
|---|
| 90 | {
|
|---|
| 91 | HANDLE mtx = mutex;
|
|---|
| 92 | WaitForSingleObject(mtx, INFINITE);
|
|---|
| 93 | mutex = 0;
|
|---|
| 94 |
|
|---|
| 95 | ReleaseMutex(mtx);
|
|---|
| 96 | CloseHandle(mtx);
|
|---|
| 97 | CloseHandle(event);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | struct SoundInfo
|
|---|
| 101 | {
|
|---|
| 102 | SoundInfo(const QString &fn, int lp, QSound *snd, QAuServerWindows *srv)
|
|---|
| 103 | : sound(snd), server(srv), filename(fn), loops(lp)
|
|---|
| 104 | {
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | QSound *sound;
|
|---|
| 108 | QAuServerWindows *server;
|
|---|
| 109 | QString filename;
|
|---|
| 110 | int loops;
|
|---|
| 111 | };
|
|---|
| 112 |
|
|---|
| 113 | DWORD WINAPI SoundPlayProc(LPVOID param)
|
|---|
| 114 | {
|
|---|
| 115 | SoundInfo *info = (SoundInfo*)param;
|
|---|
| 116 |
|
|---|
| 117 | // copy data before waking up GUI thread
|
|---|
| 118 | QAuServerWindows *server = info->server;
|
|---|
| 119 | QSound *sound = info->sound;
|
|---|
| 120 | int loops = info->loops;
|
|---|
| 121 | QString filename = info->filename;
|
|---|
| 122 | HANDLE mutex = server->mutex;
|
|---|
| 123 | HANDLE event = server->event;
|
|---|
| 124 | info = 0;
|
|---|
| 125 |
|
|---|
| 126 | // server must not be destroyed until thread finishes
|
|---|
| 127 | // and all other sounds have to wait
|
|---|
| 128 | WaitForSingleObject(mutex, INFINITE);
|
|---|
| 129 |
|
|---|
| 130 | if (loops <= 1) {
|
|---|
| 131 | server->current = 0;
|
|---|
| 132 | int flags = SND_FILENAME|SND_ASYNC;
|
|---|
| 133 | if (loops == -1)
|
|---|
| 134 | flags |= SND_LOOP;
|
|---|
| 135 |
|
|---|
| 136 | QT_WA({
|
|---|
| 137 | PlaySoundW((TCHAR*)filename.utf16(), 0, flags);
|
|---|
| 138 | } , {
|
|---|
| 139 | PlaySoundA(QFile::encodeName(filename).data(), 0, flags);
|
|---|
| 140 | });
|
|---|
| 141 | if (sound && loops == 1)
|
|---|
| 142 | server->decLoop(sound);
|
|---|
| 143 |
|
|---|
| 144 | // GUI thread continues, but we are done as well.
|
|---|
| 145 | SetEvent(event);
|
|---|
| 146 | } else {
|
|---|
| 147 | // signal GUI thread to continue - sound might be reset!
|
|---|
| 148 | QPointer<QSound> guarded_sound = sound;
|
|---|
| 149 | SetEvent(event);
|
|---|
| 150 |
|
|---|
| 151 | for (int l = 0; l < loops && server->current; ++l) {
|
|---|
| 152 | QT_WA( {
|
|---|
| 153 | PlaySoundW( (TCHAR*)filename.utf16(), 0, SND_FILENAME|SND_SYNC );
|
|---|
| 154 | } , {
|
|---|
| 155 | PlaySoundA( QFile::encodeName(filename).data(), 0,
|
|---|
| 156 | SND_FILENAME|SND_SYNC );
|
|---|
| 157 | } );
|
|---|
| 158 |
|
|---|
| 159 | if (guarded_sound)
|
|---|
| 160 | server->decLoop(guarded_sound);
|
|---|
| 161 | }
|
|---|
| 162 | server->current = 0;
|
|---|
| 163 | }
|
|---|
| 164 | ReleaseMutex(mutex);
|
|---|
| 165 |
|
|---|
| 166 | return 0;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | void QAuServerWindows::playHelper(const QString &filename, int loop, QSound *snd)
|
|---|
| 170 | {
|
|---|
| 171 | if (loop == 0)
|
|---|
| 172 | return;
|
|---|
| 173 | // busy?
|
|---|
| 174 | if (WaitForSingleObject(mutex, 0) == WAIT_TIMEOUT)
|
|---|
| 175 | return;
|
|---|
| 176 | ReleaseMutex(mutex);
|
|---|
| 177 |
|
|---|
| 178 | DWORD threadid = 0;
|
|---|
| 179 | SoundInfo info(filename, loop, snd, this);
|
|---|
| 180 | current = CreateThread(0, 0, SoundPlayProc, &info, 0, &threadid);
|
|---|
| 181 | CloseHandle(current);
|
|---|
| 182 |
|
|---|
| 183 | WaitForSingleObject(event, INFINITE);
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | void QAuServerWindows::play(const QString& filename, int loop)
|
|---|
| 187 | {
|
|---|
| 188 | playHelper(filename, loop, 0);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | void QAuServerWindows::play(QSound* s)
|
|---|
| 192 | {
|
|---|
| 193 | playHelper(s->fileName(), s->loops(), s);
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | void QAuServerWindows::stop(QSound*)
|
|---|
| 197 | {
|
|---|
| 198 | // stop unlooped sound
|
|---|
| 199 | if (!current)
|
|---|
| 200 | PlaySound(0, 0, 0);
|
|---|
| 201 | // stop after loop is done
|
|---|
| 202 | current = 0;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | bool QAuServerWindows::okay()
|
|---|
| 206 | {
|
|---|
| 207 | return true;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | QAuServer* qt_new_audio_server()
|
|---|
| 211 | {
|
|---|
| 212 | return new QAuServerWindows(qApp);
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | QT_END_NAMESPACE
|
|---|
| 216 |
|
|---|
| 217 | #include "qsound_win.moc"
|
|---|
| 218 |
|
|---|
| 219 | #endif // QT_NO_SOUND
|
|---|