Switched the 10s timer in discord-integration to be thread based

This commit is contained in:
Starman0620 2021-04-20 18:31:28 -04:00
parent 99ec2a12f1
commit 8e194ba5a9

View File

@ -4,21 +4,25 @@
#include <gui/gui.h> #include <gui/gui.h>
#include <gui/style.h> #include <gui/style.h>
#include <core.h> #include <core.h>
#include <cmath>
#include <discord_rpc.h> #include <discord_rpc.h>
#include <thread>
SDRPP_MOD_INFO { SDRPP_MOD_INFO {
/* Name: */ "discord-integration", /* Name: */ "discord-integration",
/* Description: */ "Discord Rich Presence module for SDR++", /* Description: */ "Discord Rich Presence module for SDR++",
/* Author: */ "Starman0620", /* Author: */ "Starman0620 & Ryzerth",
/* Version: */ 0, 0, 1, /* Version: */ 0, 0, 1,
/* Max instances */ 1 /* Max instances */ 1
}; };
void ready(const DiscordUser *request); DiscordRichPresence presence;
static DiscordRichPresence presence; uint64_t lastFreq;
static uint64_t lastFreq; char* freq = new char[24];
static char* freq = new char[24];
// Threading
int workerCounter = 0;
std::thread workerThread;
bool workerRunning;
class PresenceModule : public ModuleManager::Instance { class PresenceModule : public ModuleManager::Instance {
public: public:
@ -26,20 +30,26 @@ public:
this->name = name; this->name = name;
gui::menu.registerEntry(name, menuHandler, this, this); gui::menu.registerEntry(name, menuHandler, this, this);
workerRunning = true;
workerThread = std::thread(&PresenceModule::worker, this);
startPresence(); startPresence();
} }
~PresenceModule() { ~PresenceModule() {
workerRunning = false;
if (workerThread.joinable()) { workerThread.join(); }
} }
void enable() { void enable() {
startPresence(); startPresence();
workerRunning = true;
enabled = true; enabled = true;
} }
void disable() { void disable() {
Discord_ClearPresence(); Discord_ClearPresence();
workerRunning = false;
enabled = false; enabled = false;
} }
@ -48,6 +58,19 @@ public:
} }
private: private:
// Main thread
void worker() {
while (workerRunning) {
workerCounter++;
if(workerCounter >= 1000) {
workerCounter = 0;
updatePresence();
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
static void menuHandler(void* ctx) { static void menuHandler(void* ctx) {
PresenceModule* _this = (PresenceModule*)ctx; PresenceModule* _this = (PresenceModule*)ctx;
if (!_this->enabled) { style::beginDisabled(); } if (!_this->enabled) { style::beginDisabled(); }