src/gui/kernel/qsound_x11.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 "qhash.h"
       
    47 #include "qsocketnotifier.h"
       
    48 #include "qapplication.h"
       
    49 #include "qsound_p.h"
       
    50 
       
    51 QT_BEGIN_NAMESPACE
       
    52 
       
    53 #ifndef QT_NO_NAS
       
    54 
       
    55 QT_BEGIN_INCLUDE_NAMESPACE
       
    56 #include <audio/audiolib.h>
       
    57 #include <audio/soundlib.h>
       
    58 QT_END_INCLUDE_NAMESPACE
       
    59 
       
    60 static AuServer *nas=0;
       
    61 
       
    62 static AuBool eventPred(AuServer *, AuEvent *e, AuPointer p)
       
    63 {
       
    64     if (e && (e->type == AuEventTypeElementNotify)) {
       
    65         if (e->auelementnotify.flow == *((AuFlowID *)p))
       
    66             return true;
       
    67     }
       
    68     return false;
       
    69 }
       
    70 
       
    71 class QAuBucketNAS : public QAuBucket {
       
    72 public:
       
    73     QAuBucketNAS(AuBucketID b, AuFlowID f = 0) : id(b), flow(f), stopped(true), numplaying(0) { }
       
    74     ~QAuBucketNAS()
       
    75     {
       
    76         if (nas) {
       
    77             AuSync(nas, false);
       
    78             AuDestroyBucket(nas, id, NULL);
       
    79 
       
    80             AuEvent ev;
       
    81             while (AuScanEvents(nas, AuEventsQueuedAfterFlush, true, eventPred, &flow, &ev))
       
    82                 ;
       
    83         }
       
    84     }
       
    85 
       
    86     AuBucketID id;
       
    87     AuFlowID flow;
       
    88     bool     stopped;
       
    89     int      numplaying;
       
    90 };
       
    91 
       
    92 class QAuServerNAS : public QAuServer {
       
    93     Q_OBJECT
       
    94 
       
    95     QSocketNotifier* sn;
       
    96 
       
    97 public:
       
    98     QAuServerNAS(QObject* parent);
       
    99     ~QAuServerNAS();
       
   100 
       
   101     void init(QSound*);
       
   102     void play(const QString& filename);
       
   103     void play(QSound*);
       
   104     void stop(QSound*);
       
   105     bool okay();
       
   106     void setDone(QSound*);
       
   107 
       
   108 public slots:
       
   109     void dataReceived();
       
   110     void soundDestroyed(QObject *o);
       
   111 
       
   112 private:
       
   113     QAuBucketNAS* bucket(QSound* s)
       
   114     {
       
   115         return (QAuBucketNAS*)QAuServer::bucket(s);
       
   116     }
       
   117 };
       
   118 
       
   119 QAuServerNAS::QAuServerNAS(QObject* parent) :
       
   120     QAuServer(parent)
       
   121 {
       
   122     setObjectName(QLatin1String("Network Audio System"));
       
   123     nas = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL);
       
   124     if (nas) {
       
   125         AuSetCloseDownMode(nas, AuCloseDownDestroy, NULL);
       
   126         // Ask Qt for async messages...
       
   127         sn=new QSocketNotifier(AuServerConnectionNumber(nas),
       
   128                 QSocketNotifier::Read);
       
   129         QObject::connect(sn, SIGNAL(activated(int)),
       
   130                 this, SLOT(dataReceived()));
       
   131     } else {
       
   132         sn = 0;
       
   133     }
       
   134 }
       
   135 
       
   136 QAuServerNAS::~QAuServerNAS()
       
   137 {
       
   138     if (nas)
       
   139         AuCloseServer(nas);
       
   140     delete sn;
       
   141     nas = 0;
       
   142 }
       
   143 
       
   144 typedef QHash<void*,QAuServerNAS*> AuServerHash;
       
   145 static AuServerHash *inprogress=0;
       
   146 
       
   147 void QAuServerNAS::soundDestroyed(QObject *o)
       
   148 {
       
   149     if (inprogress) {
       
   150         QSound *so = static_cast<QSound *>(o);
       
   151         while (inprogress->remove(so))
       
   152             ; // Loop while remove returns true
       
   153     }
       
   154 }
       
   155 
       
   156 void QAuServerNAS::play(const QString& filename)
       
   157 {
       
   158     if (nas) {
       
   159         int iv=100;
       
   160         AuFixedPoint volume=AuFixedPointFromFraction(iv,100);
       
   161         AuSoundPlayFromFile(nas, filename.toLocal8Bit().constData(), AuNone, volume,
       
   162                             NULL, NULL, NULL, NULL, NULL, NULL);
       
   163         AuFlush(nas);
       
   164         dataReceived();
       
   165         AuFlush(nas);
       
   166         qApp->flush();
       
   167     }
       
   168 }
       
   169 
       
   170 static void callback(AuServer*, AuEventHandlerRec*, AuEvent* e, AuPointer p)
       
   171 {
       
   172     if (inprogress->contains(p) && e) {
       
   173         if (e->type==AuEventTypeElementNotify &&
       
   174                     e->auelementnotify.kind==AuElementNotifyKindState) {
       
   175             if (e->auelementnotify.cur_state == AuStateStop) {
       
   176                 AuServerHash::Iterator it = inprogress->find(p);
       
   177                 if (it != inprogress->end())
       
   178                     (*it)->setDone((QSound*)p);
       
   179             }
       
   180         }
       
   181     }
       
   182 }
       
   183 
       
   184 void QAuServerNAS::setDone(QSound* s)
       
   185 {
       
   186     if (nas) {
       
   187         decLoop(s);
       
   188         if (s->loopsRemaining() && !bucket(s)->stopped) {
       
   189             bucket(s)->stopped = true;
       
   190             play(s);
       
   191         } else {
       
   192             if (--(bucket(s)->numplaying) == 0)
       
   193                 bucket(s)->stopped = true;
       
   194             inprogress->remove(s);
       
   195         }
       
   196     }
       
   197 }
       
   198 
       
   199 void QAuServerNAS::play(QSound* s)
       
   200 {
       
   201     if (nas) {
       
   202         ++(bucket(s)->numplaying);
       
   203         if (!bucket(s)->stopped) {
       
   204             stop(s);
       
   205         }
       
   206 
       
   207         bucket(s)->stopped = false;
       
   208         if (!inprogress)
       
   209             inprogress = new AuServerHash;
       
   210         inprogress->insert(s,this);
       
   211         int iv=100;
       
   212         AuFixedPoint volume=AuFixedPointFromFraction(iv,100);
       
   213         QAuBucketNAS *b = bucket(s);
       
   214         AuSoundPlayFromBucket(nas, b->id, AuNone, volume,
       
   215                               callback, s, 0, &b->flow, NULL, NULL, NULL);
       
   216         AuFlush(nas);
       
   217         dataReceived();
       
   218         AuFlush(nas);
       
   219         qApp->flush();
       
   220     }
       
   221 }
       
   222 
       
   223 void QAuServerNAS::stop(QSound* s)
       
   224 {
       
   225     if (nas && !bucket(s)->stopped) {
       
   226         bucket(s)->stopped = true;
       
   227         AuStopFlow(nas, bucket(s)->flow, NULL);
       
   228         AuFlush(nas);
       
   229         dataReceived();
       
   230         AuFlush(nas);
       
   231         qApp->flush();
       
   232     }
       
   233 }
       
   234 
       
   235 void QAuServerNAS::init(QSound* s)
       
   236 {
       
   237     connect(s, SIGNAL(destroyed(QObject*)),
       
   238             this, SLOT(soundDestroyed(QObject*)));
       
   239 
       
   240     if (nas) {
       
   241         AuBucketID b_id =
       
   242             AuSoundCreateBucketFromFile(nas, s->fileName().toLocal8Bit().constData(),
       
   243                                         0 /*AuAccessAllMasks*/, NULL, NULL);
       
   244         setBucket(s, new QAuBucketNAS(b_id));
       
   245     }
       
   246 }
       
   247 
       
   248 bool QAuServerNAS::okay()
       
   249 {
       
   250     return !!nas;
       
   251 }
       
   252 
       
   253 void QAuServerNAS::dataReceived()
       
   254 {
       
   255     AuHandleEvents(nas);
       
   256 }
       
   257 
       
   258 QT_BEGIN_INCLUDE_NAMESPACE
       
   259 #include "qsound_x11.moc"
       
   260 QT_END_INCLUDE_NAMESPACE
       
   261 
       
   262 #endif
       
   263 
       
   264 
       
   265 class QAuServerNull : public QAuServer
       
   266 {
       
   267 public:
       
   268     QAuServerNull(QObject* parent);
       
   269 
       
   270     void play(const QString&) { }
       
   271     void play(QSound*s) { while(decLoop(s) > 0) /* nothing */ ; }
       
   272     void stop(QSound*) { }
       
   273     bool okay() { return false; }
       
   274 };
       
   275 
       
   276 QAuServerNull::QAuServerNull(QObject* parent)
       
   277     : QAuServer(parent)
       
   278 {
       
   279 }
       
   280 
       
   281 
       
   282 QAuServer* qt_new_audio_server()
       
   283 {
       
   284 #ifndef QT_NO_NAS
       
   285     QAuServer* s = new QAuServerNAS(qApp);
       
   286     if (s->okay())
       
   287         return s;
       
   288     else
       
   289         delete s;
       
   290 #endif
       
   291     return new QAuServerNull(qApp);
       
   292 }
       
   293 
       
   294 QT_END_NAMESPACE
       
   295 
       
   296 #endif // QT_NO_SOUND