source: trunk/src/gui/kernel/qsound_win.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 5.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** 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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
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
54QT_BEGIN_NAMESPACE
55
56class QAuServerWindows : public QAuServer {
57 Q_OBJECT
58
59public:
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
77QAuServerWindows::QAuServerWindows(QObject* parent) :
78 QAuServer(parent), current(0)
79{
80 mutex = CreateMutex(0, 0, 0);
81 event = CreateEvent(0, FALSE, FALSE, 0);
82}
83
84QAuServerWindows::~QAuServerWindows()
85{
86 HANDLE mtx = mutex;
87 WaitForSingleObject(mtx, INFINITE);
88 mutex = 0;
89
90 ReleaseMutex(mtx);
91 CloseHandle(mtx);
92 CloseHandle(event);
93}
94
95struct 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
102 QSound *sound;
103 QAuServerWindows *server;
104 QString filename;
105 int loops;
106};
107
108DWORD 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
155void 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
172void QAuServerWindows::play(const QString& filename, int loop)
173{
174 playHelper(filename, loop, 0);
175}
176
177void QAuServerWindows::play(QSound* s)
178{
179 playHelper(s->fileName(), s->loops(), s);
180}
181
182void QAuServerWindows::stop(QSound*)
183{
184 // stop unlooped sound
185 if (!current)
186 PlaySound(0, 0, 0);
187 // stop after loop is done
188 current = 0;
189}
190
191bool QAuServerWindows::okay()
192{
193 return true;
194}
195
196QAuServer* qt_new_audio_server()
197{
198 return new QAuServerWindows(qApp);
199}
200
201QT_END_NAMESPACE
202
203#include "qsound_win.moc"
204
205#endif // QT_NO_SOUND
Note: See TracBrowser for help on using the repository browser.