discord-rpc/src/discord-rpc.cpp

60 lines
1.5 KiB
C++
Raw Normal View History

2017-06-30 23:18:54 +00:00
#include "discord-rpc.h"
#include "connection.h"
#include "yolojson.h"
static RpcConnection* MyConnection = nullptr;
static char ApplicationId[64]{};
static DiscordEventHandlers Handlers{};
2017-07-07 16:41:20 +00:00
static bool wasJustConnected = false;
static bool wasJustDisconnected = false;
2017-06-30 23:18:54 +00:00
2017-07-07 21:00:29 +00:00
extern "C" void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handlers)
2017-06-30 23:18:54 +00:00
{
if (handlers) {
Handlers = *handlers;
}
else {
Handlers = {};
}
2017-07-10 22:25:47 +00:00
MyConnection = RpcConnection::Create(applicationId);
2017-07-07 16:41:20 +00:00
MyConnection->onConnect = []() { wasJustConnected = true; };
MyConnection->onDisconnect = []() { wasJustDisconnected = true; };
2017-06-30 23:18:54 +00:00
MyConnection->Open();
}
2017-07-07 21:00:29 +00:00
extern "C" void Discord_Shutdown()
2017-06-30 23:18:54 +00:00
{
Handlers = {};
MyConnection->Close();
RpcConnection::Destroy(MyConnection);
}
2017-07-07 21:00:29 +00:00
extern "C" void Discord_UpdatePresence(const DiscordRichPresence* presence)
2017-06-30 23:18:54 +00:00
{
auto frame = MyConnection->GetNextFrame();
frame->opcode = OPCODE::FRAME;
2017-06-30 23:18:54 +00:00
char* jsonWrite = frame->message;
JsonWriteRichPresenceObj(jsonWrite, presence);
2017-07-10 22:25:47 +00:00
frame->length = jsonWrite - frame->message;
2017-06-30 23:18:54 +00:00
MyConnection->WriteFrame(frame);
}
2017-07-07 16:41:20 +00:00
2017-07-07 21:00:29 +00:00
extern "C" void Discord_Update()
2017-07-07 16:41:20 +00:00
{
// check for messages
// todo
// fire callbacks
if (wasJustDisconnected && Handlers.disconnected) {
wasJustDisconnected = false;
Handlers.disconnected();
}
if (wasJustConnected && Handlers.ready) {
wasJustConnected = false;
Handlers.ready();
}
}