From 3dd666a408a4c2b340c84e83a0170319ebe0c171 Mon Sep 17 00:00:00 2001 From: Nathaniel van Diepen Date: Sun, 14 Jan 2024 18:58:48 -0700 Subject: [PATCH] Fix up build --- shared/liboxide/liboxide.cpp | 17 +++-------------- shared/liboxide/liboxide.h | 25 ++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/shared/liboxide/liboxide.cpp b/shared/liboxide/liboxide.cpp index e989116d6..8e7596330 100644 --- a/shared/liboxide/liboxide.cpp +++ b/shared/liboxide/liboxide.cpp @@ -98,22 +98,11 @@ namespace Oxide { } return pids; } - void dispatchToMainThread(std::function callback){ dispatchToThread(qApp->thread(), callback); } - void dispatchToThread(QThread* thread, std::function callback){ - if(QThread::currentThread() == thread){ - // Already on the thread + void dispatchToMainThread(std::function callback){ + dispatchToMainThread([callback]{ callback(); - return; - } - // Send to the thread - QTimer* timer = new QTimer(); - timer->moveToThread(thread); - timer->setSingleShot(true); - QObject::connect(timer, &QTimer::timeout, [=](){ - callback(); - timer->deleteLater(); + return 0; }); - QMetaObject::invokeMethod(timer, "start", Qt::BlockingQueuedConnection, Q_ARG(int, 0)); } uid_t getUID(const QString& name){ char buffer[1024]; diff --git a/shared/liboxide/liboxide.h b/shared/liboxide/liboxide.h index 0be192c53..3bf9ae3db 100644 --- a/shared/liboxide/liboxide.h +++ b/shared/liboxide/liboxide.h @@ -102,10 +102,29 @@ namespace Oxide { */ LIBOXIDE_EXPORT void dispatchToMainThread(std::function callback); /*! - * \brief Run code on a thread - * \param callback The code to run on the thread + * \brief Run code on the main Qt thread + * \param callback The code to run on the main thread + * \return Return value of callback + * + * \snippet examples/oxide.cpp dispatchToMainThread */ - LIBOXIDE_EXPORT void dispatchToThread(QThread* thread, std::function callback); + template LIBOXIDE_EXPORT T dispatchToMainThread(std::function callback){ + if(QThread::currentThread() == qApp->thread()){ + return callback(); + } + // any thread + QTimer* timer = new QTimer(); + timer->moveToThread(qApp->thread()); + timer->setSingleShot(true); + T result; + QObject::connect(timer, &QTimer::timeout, [timer, &result, callback](){ + // main thread + result = callback(); + timer->deleteLater(); + }); + QMetaObject::invokeMethod(timer, "start", Qt::BlockingQueuedConnection, Q_ARG(int, 0)); + return result; + } /*! * \brief Get the UID for a username * \param name Username to search for