2021-04-19 20:58:47 -04:00
|
|
|
#include <imgui.h>
|
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
#include <module.h>
|
|
|
|
#include <gui/gui.h>
|
|
|
|
#include <gui/style.h>
|
|
|
|
#include <core.h>
|
2021-04-20 14:26:48 -04:00
|
|
|
#include <cmath>
|
2021-04-19 21:19:16 -04:00
|
|
|
#include <discord_rpc.h>
|
|
|
|
|
2021-04-19 20:58:47 -04:00
|
|
|
SDRPP_MOD_INFO {
|
|
|
|
/* Name: */ "discord",
|
|
|
|
/* Description: */ "Discord Rich Presence module for SDR++",
|
|
|
|
/* Author: */ "Starman0620",
|
|
|
|
/* Version: */ 0, 0, 1,
|
|
|
|
/* Max instances */ -1
|
|
|
|
};
|
|
|
|
|
2021-04-20 14:26:48 -04:00
|
|
|
void ready(const DiscordUser *request);
|
2021-04-19 21:47:11 -04:00
|
|
|
static DiscordRichPresence presence;
|
2021-04-20 14:26:48 -04:00
|
|
|
static time_t lastUpdate = time(0);
|
|
|
|
static char* freq = new char[24];
|
2021-04-19 21:47:11 -04:00
|
|
|
|
2021-04-19 20:58:47 -04:00
|
|
|
class PresenceModule : public ModuleManager::Instance {
|
|
|
|
public:
|
|
|
|
PresenceModule(std::string name) {
|
|
|
|
this->name = name;
|
|
|
|
gui::menu.registerEntry(name, menuHandler, this, this);
|
2021-04-19 21:47:11 -04:00
|
|
|
|
2021-04-20 14:26:48 -04:00
|
|
|
startPresence();
|
2021-04-19 20:58:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
~PresenceModule() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void enable() {
|
2021-04-20 14:26:48 -04:00
|
|
|
startPresence();
|
2021-04-19 20:58:47 -04:00
|
|
|
enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void disable() {
|
2021-04-20 14:26:48 -04:00
|
|
|
Discord_ClearPresence();
|
2021-04-19 20:58:47 -04:00
|
|
|
enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isEnabled() {
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void menuHandler(void* ctx) {
|
|
|
|
PresenceModule* _this = (PresenceModule*)ctx;
|
2021-04-19 20:59:39 -04:00
|
|
|
if (!_this->enabled) { style::beginDisabled(); }
|
2021-04-19 20:58:47 -04:00
|
|
|
|
|
|
|
float menuWidth = ImGui::GetContentRegionAvailWidth();
|
|
|
|
|
2021-04-19 20:59:39 -04:00
|
|
|
// GUI
|
|
|
|
ImGui::BeginGroup();
|
2021-04-20 14:26:48 -04:00
|
|
|
ImGui::Text("SDR++ Rich Presence by Starman0620");
|
2021-04-19 20:58:47 -04:00
|
|
|
ImGui::EndGroup();
|
2021-04-20 14:26:48 -04:00
|
|
|
|
|
|
|
// Very basic method of implenting a 10s timer
|
|
|
|
if (time(0) - lastUpdate > 10) {
|
|
|
|
updatePresence();
|
|
|
|
lastUpdate = time(0);
|
|
|
|
}
|
2021-04-19 20:58:47 -04:00
|
|
|
|
2021-04-19 20:59:39 -04:00
|
|
|
if (!_this->enabled) { style::endDisabled(); }
|
2021-04-19 20:58:47 -04:00
|
|
|
}
|
|
|
|
|
2021-04-20 14:26:48 -04:00
|
|
|
static void updatePresence() {
|
|
|
|
presence.details = "Listening";
|
|
|
|
sprintf(freq, "%.2fMHz", gui::waterfall.getCenterFrequency()/1000000, 3);
|
|
|
|
presence.state = freq;
|
|
|
|
Discord_UpdatePresence(&presence);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void startPresence() {
|
|
|
|
// Discord initialization
|
|
|
|
DiscordEventHandlers handlers;
|
|
|
|
memset(&handlers, 0, sizeof(handlers));
|
|
|
|
memset(&presence, 0, sizeof(presence));
|
|
|
|
Discord_Initialize("833485588954742864", &handlers, 1, "");
|
|
|
|
|
|
|
|
// Set the first presence
|
|
|
|
presence.details = "Initializing rich presence...";
|
|
|
|
presence.startTimestamp = time(0);
|
|
|
|
presence.largeImageKey = "image_large";
|
|
|
|
Discord_UpdatePresence(&presence);
|
|
|
|
}
|
|
|
|
|
2021-04-19 20:58:47 -04:00
|
|
|
std::string name;
|
|
|
|
bool enabled = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
MOD_EXPORT void _INIT_() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
MOD_EXPORT ModuleManager::Instance* _CREATE_INSTANCE_(std::string name) {
|
|
|
|
return new PresenceModule(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOD_EXPORT void _DELETE_INSTANCE_(void* instance) {
|
|
|
|
delete (PresenceModule*)instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOD_EXPORT void _END_() {
|
|
|
|
|
|
|
|
}
|