Skip to content

Commit

Permalink
Fix up build
Browse files Browse the repository at this point in the history
  • Loading branch information
Eeems committed Jan 15, 2024
1 parent ddaf335 commit 3dd666a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
17 changes: 3 additions & 14 deletions shared/liboxide/liboxide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,11 @@ namespace Oxide {
}
return pids;
}
void dispatchToMainThread(std::function<void()> callback){ dispatchToThread(qApp->thread(), callback); }
void dispatchToThread(QThread* thread, std::function<void()> callback){
if(QThread::currentThread() == thread){
// Already on the thread
void dispatchToMainThread(std::function<void()> callback){
dispatchToMainThread<int>([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];
Expand Down
25 changes: 22 additions & 3 deletions shared/liboxide/liboxide.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,29 @@ namespace Oxide {
*/
LIBOXIDE_EXPORT void dispatchToMainThread(std::function<void()> 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<void()> callback);
template<typename T> LIBOXIDE_EXPORT T dispatchToMainThread(std::function<T()> 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
Expand Down

0 comments on commit 3dd666a

Please sign in to comment.