Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / runtime / phonon / xine / xinethread.cpp
blob93bb1de5140cb838a0b7124c2a7251278f043913
1 /* This file is part of the KDE project
2 Copyright (C) 2007 Matthias Kretz <kretz@kde.org>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
21 #include "xinethread.h"
22 #include <QtCore/QMutexLocker>
23 #include <QtCore/QTimer>
24 #include <QtCore/QCoreApplication>
25 #include "xineengine.h"
26 #include "xinestream.h"
27 #include "events.h"
29 namespace Phonon
31 namespace Xine
34 XineThread::XineThread()
35 : m_newStream(0),
36 m_eventLoopReady(false)
40 XineThread::~XineThread()
44 // called from main thread
45 // should never be called from ByteStream
46 void XineThread::waitForEventLoop()
48 m_mutex.lock();
49 if (!m_eventLoopReady) {
50 m_waitingForEventLoop.wait(&m_mutex);
52 m_mutex.unlock();
55 XineStream *XineThread::newStream()
57 XineThread *that = XineEngine::thread();
59 QMutexLocker locker(&that->m_mutex);
60 Q_ASSERT(that->m_newStream == 0);
61 QCoreApplication::postEvent(that, new QEVENT(NewStream));
62 that->m_waitingForNewStream.wait(&that->m_mutex);
63 Q_ASSERT(that->m_newStream);
64 XineStream *ret = that->m_newStream;
65 that->m_newStream = 0;
66 return ret;
69 void XineThread::quit()
71 foreach (QObject *child, children()) {
72 kDebug(610) << child;
74 QThread::quit();
77 bool XineThread::event(QEvent *e)
79 switch (e->type()) {
80 case Event::NewStream:
81 e->accept();
82 m_mutex.lock();
83 Q_ASSERT(m_newStream == 0);
84 m_newStream = new XineStream;
85 m_newStream->moveToThread(this);
86 m_mutex.unlock();
87 m_waitingForNewStream.wakeAll();
88 return true;
89 case Event::Rewire:
90 e->accept();
91 kDebug(610) << "XineThread Rewire event:";
93 RewireEvent *ev = static_cast<RewireEvent *>(e);
94 foreach (WireCall wire, ev->wireCalls) {
95 kDebug(610) << " " << wire.source << " -> " << wire.sink;
96 wire.sink->assert();
97 wire.source->assert();
98 wire.sink->rewireTo(wire.source.data());
101 return true;
102 default:
103 return QThread::event(e);
107 void XineThread::run()
109 Q_ASSERT(QThread::currentThread() == this);
110 QTimer::singleShot(0, this, SLOT(eventLoopReady()));
111 exec();
112 m_eventLoopReady = false;
113 // there should be no remaining XineStreams
114 const QList<QObject *> c = children();
115 foreach (QObject *obj, c) {
116 XineStream *xs = qobject_cast<XineStream *>(obj);
117 if (xs) {
118 delete xs;
123 // xine thread
124 void XineThread::eventLoopReady()
126 m_mutex.lock();
127 m_eventLoopReady = true;
128 m_mutex.unlock();
129 m_waitingForEventLoop.wakeAll();
132 } // namespace Xine
133 } // namespace Phonon
135 #include "xinethread.moc"
136 // vim: sw=4 sts=4 et tw=100