1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/*
* KMix -- KDE's full featured mini mixer
*
*
* Copyright (C) 2020 Martin Sandsmark <martin.sandsmark@kde.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#pragma once
#include <QCoreApplication>
#include <QTimer>
#include <QSocketNotifier>
#include <QDateTime>
#include <QDebug>
#include <pulse/mainloop-api.h>
// Not really its own mainloop, just wraps Qt stuff for pulseaudio
struct QtPaMainLoop {
pa_mainloop_api pa_vtable{};
QtPaMainLoop()
{
pa_vtable.userdata = this;
pa_vtable.io_new = newIoEvent;
pa_vtable.io_enable = setIoEnabled;
pa_vtable.io_free = ioDestroy;
pa_vtable.io_set_destroy = setIoDestructor;
pa_vtable.time_new = newTimer;
pa_vtable.time_restart = restartTimer;
pa_vtable.time_free = freeTimer;
pa_vtable.time_set_destroy = timerSetDestructor;
pa_vtable.time_set_destroy = timerSetDestructor;
pa_vtable.defer_new = newDefer;
pa_vtable.defer_enable = setDeferEnabled;
pa_vtable.defer_free = freeDefer;
pa_vtable.defer_set_destroy = deferSetDestructor;
pa_vtable.quit = quit;
}
static int msecsUntilTimeval(const struct timeval *tv)
{
time_t target = tv->tv_sec * 1000;
if (tv->tv_usec) {
target += tv->tv_usec / 1000;
}
return QDateTime::currentDateTime().msecsTo(QDateTime::fromMSecsSinceEpoch(target));
}
static pa_time_event *newTimer(pa_mainloop_api *a, const struct timeval *tv, pa_time_event_cb_t callback, void *userdata)
{
QTimer *timer = new QTimer;
timer->setProperty("PA_USERDATA", QVariant::fromValue(userdata));
timer->setParent(qApp);
timer->setSingleShot(true);
Qt::TimerType timerType = Qt::VeryCoarseTimer;
if (tv->tv_usec) {
timerType = Qt::PreciseTimer;
}
timer->setTimerType(timerType);
QObject::connect(timer, &QTimer::timeout, [ = ]() {
callback(a, reinterpret_cast<pa_time_event *>(timer), tv, userdata);
});
int duration = msecsUntilTimeval(tv);
if (duration < 0) {
qWarning() << "Invalid timer target, sec:" << tv->tv_sec << "usec" << tv->tv_usec;
}
timer->start(duration);
return reinterpret_cast<pa_time_event *>(timer);
}
static void restartTimer(pa_time_event *e, const timeval *tv)
{
QTimer *timer = reinterpret_cast<QTimer *>(e);
Qt::TimerType timerType = Qt::VeryCoarseTimer;
if (tv->tv_usec) {
timerType = Qt::PreciseTimer;
}
timer->setTimerType(timerType);
int duration = msecsUntilTimeval(tv);
if (duration < 0) {
qWarning() << "Invalid restart timer target, sec:" << tv->tv_sec << "usec" << tv->tv_usec;
}
timer->start(duration);
}
static void freeTimer(pa_time_event *e)
{
QTimer *timer = reinterpret_cast<QTimer *>(e);
delete timer;
}
static void timerSetDestructor(pa_time_event *e, pa_time_event_destroy_cb_t destructor)
{
QTimer *timer = reinterpret_cast<QTimer *>(e);
QObject::connect(timer, &QTimer::destroyed, [ = ]() {
destructor(reinterpret_cast<pa_mainloop_api *>(timer->parent()), e, qvariant_cast<void *>(timer->property("PA_USERDATA")));
});
}
struct SocketNotifierWrapper {
SocketNotifierWrapper() = default;
SocketNotifierWrapper(const SocketNotifierWrapper &) = delete;
const SocketNotifierWrapper &operator=(const SocketNotifierWrapper &) = delete;
~SocketNotifierWrapper()
{
delete readNotifier;
delete writeNotifier;
delete errorNotifier;
if (destructor) {
destructor(a, reinterpret_cast<pa_io_event *>(this), userdata);
}
}
QSocketNotifier *readNotifier = nullptr;
QSocketNotifier *writeNotifier = nullptr;
QSocketNotifier *errorNotifier = nullptr;
pa_io_event_destroy_cb_t destructor = nullptr;
void *userdata;
pa_mainloop_api *a;
};
static pa_io_event *newIoEvent(pa_mainloop_api *a, int fd, pa_io_event_flags_t events, pa_io_event_cb_t cb, void *userdata)
{
SocketNotifierWrapper *wrapper = new SocketNotifierWrapper;
wrapper->userdata = userdata;
wrapper->a = a;
wrapper->readNotifier = new QSocketNotifier(fd, QSocketNotifier::Read, qApp);
QObject::connect(wrapper->readNotifier, &QSocketNotifier::activated, [ = ]() {
cb(a, reinterpret_cast<pa_io_event *>(wrapper), fd, PA_IO_EVENT_INPUT, userdata);
});
wrapper->writeNotifier = new QSocketNotifier(fd, QSocketNotifier::Write, qApp);
QObject::connect(wrapper->writeNotifier, &QSocketNotifier::activated, [ = ]() {
cb(a, reinterpret_cast<pa_io_event *>(wrapper), fd, PA_IO_EVENT_OUTPUT, userdata);
});
wrapper->errorNotifier = new QSocketNotifier(fd, QSocketNotifier::Exception, qApp);
QObject::connect(wrapper->errorNotifier, &QSocketNotifier::activated, [ = ]() {
cb(a, reinterpret_cast<pa_io_event *>(wrapper), fd, PA_IO_EVENT_ERROR, userdata);
});
if (events & PA_IO_EVENT_INPUT) {
wrapper->readNotifier->setEnabled(true);
} else {
wrapper->readNotifier->setEnabled(false);
}
if (events & PA_IO_EVENT_OUTPUT) {
wrapper->writeNotifier->setEnabled(true);
} else {
wrapper->writeNotifier->setEnabled(false);
}
if (events & PA_IO_EVENT_ERROR || events & PA_IO_EVENT_HANGUP) {
wrapper->errorNotifier->setEnabled(true);
} else {
wrapper->errorNotifier->setEnabled(false);
}
return reinterpret_cast<pa_io_event *>(wrapper);
}
static void setIoEnabled(pa_io_event *e, pa_io_event_flags_t events)
{
SocketNotifierWrapper *wrapper = reinterpret_cast<SocketNotifierWrapper *>(e);
if (events & PA_IO_EVENT_INPUT) {
wrapper->readNotifier->setEnabled(true);
} else {
wrapper->readNotifier->setEnabled(false);
}
if (events & PA_IO_EVENT_OUTPUT) {
wrapper->writeNotifier->setEnabled(true);
} else {
wrapper->writeNotifier->setEnabled(false);
}
if (events & PA_IO_EVENT_ERROR || events & PA_IO_EVENT_HANGUP) {
wrapper->errorNotifier->setEnabled(true);
} else {
wrapper->errorNotifier->setEnabled(false);
}
}
static void ioDestroy(pa_io_event *e)
{
SocketNotifierWrapper *wrapper = reinterpret_cast<SocketNotifierWrapper *>(e);
delete wrapper;
}
static void setIoDestructor(pa_io_event *e, pa_io_event_destroy_cb_t cb)
{
SocketNotifierWrapper *wrapper = reinterpret_cast<SocketNotifierWrapper *>(e);
wrapper->destructor = cb;
}
static pa_defer_event *newDefer(pa_mainloop_api *a, pa_defer_event_cb_t callback, void *userdata)
{
QTimer *timer = new QTimer();
timer->setProperty("PA_USERDATA", QVariant::fromValue(userdata));
timer->setParent(qApp);
//timer->setParent(reinterpret_cast<QtPaMainLoop*>(a));
timer->setSingleShot(true);
QObject::connect(timer, &QTimer::timeout, [ = ]() {
callback(a, reinterpret_cast<pa_defer_event *>(timer), userdata);
});
timer->start(0);
return reinterpret_cast<pa_defer_event *>(timer);
}
static void setDeferEnabled(pa_defer_event *e, int b)
{
QTimer *timer = reinterpret_cast<QTimer *>(e);
if (b) {
timer->start(0);
} else {
timer->stop();
}
}
static void freeDefer(pa_defer_event *e)
{
QTimer *timer = reinterpret_cast<QTimer *>(e);
delete timer;
}
static void deferSetDestructor(pa_defer_event *e, pa_defer_event_destroy_cb_t destructor)
{
QTimer *timer = reinterpret_cast<QTimer *>(e);
QObject::connect(timer, &QTimer::destroyed, [ = ]() {
destructor(reinterpret_cast<pa_mainloop_api *>(timer->parent()), e, qvariant_cast<void *>(timer->property("PA_USERDATA")));
});
}
static void quit(pa_mainloop_api *a, int retval)
{
Q_UNUSED(a);
qApp->exit(retval);
}
};
|