src/gui/kernel/qsound_win.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     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 
       
    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     mutex = CreateMutex(0, 0, 0);
       
    81     event = CreateEvent(0, FALSE, FALSE, 0);
       
    82 }
       
    83 
       
    84 QAuServerWindows::~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 
       
    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 
       
   102     QSound *sound;
       
   103     QAuServerWindows *server;
       
   104     QString filename;
       
   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 
       
   177 void QAuServerWindows::play(QSound* s)
       
   178 {
       
   179     playHelper(s->fileName(), s->loops(), s);
       
   180 }
       
   181 
       
   182 void 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 
       
   191 bool QAuServerWindows::okay()
       
   192 {
       
   193     return true;
       
   194 }
       
   195 
       
   196 QAuServer* qt_new_audio_server()
       
   197 {
       
   198     return new QAuServerWindows(qApp);
       
   199 }
       
   200 
       
   201 QT_END_NAMESPACE
       
   202 
       
   203 #include "qsound_win.moc"
       
   204 
       
   205 #endif // QT_NO_SOUND