src/platform/qt/DiscordCoordinator.cpp (view raw)
1/* Copyright (c) 2013-2019 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "DiscordCoordinator.h"
7
8#include "CoreController.h"
9#include "GBAApp.h"
10
11#ifdef USE_SQLITE3
12#include "feature/sqlite3/no-intro.h"
13#endif
14
15#include "discord_rpc.h"
16
17namespace QGBA {
18
19namespace DiscordCoordinator {
20
21static bool s_gameRunning = false;
22static bool s_inited = false;
23static QString s_title;
24
25static void updatePresence() {
26 if (!s_inited) {
27 return;
28 }
29 if (s_gameRunning) {
30 DiscordRichPresence discordPresence{};
31 discordPresence.details = s_title.toUtf8().constData();
32 discordPresence.instance = 1;
33 Discord_UpdatePresence(&discordPresence);
34 } else {
35 Discord_ClearPresence();
36 }
37}
38
39void init() {
40 if (s_inited) {
41 return;
42 }
43 DiscordEventHandlers handlers{};
44 Discord_Initialize("554440738952183828", &handlers, 1, nullptr);
45 s_inited = true;
46 updatePresence();
47}
48
49void deinit() {
50 if (!s_inited) {
51 return;
52 }
53 Discord_ClearPresence();
54 Discord_Shutdown();
55 s_inited = false;
56 s_gameRunning = false;
57}
58
59void gameStarted(std::shared_ptr<CoreController> controller) {
60 if (s_gameRunning) {
61 return;
62 }
63 s_gameRunning = true;
64
65 CoreController::Interrupter interrupter(controller);
66 const NoIntroDB* db = GBAApp::app()->gameDB();
67 NoIntroGame game{};
68 uint32_t crc32 = 0;
69 controller->thread()->core->checksum(controller->thread()->core, &crc32, CHECKSUM_CRC32);
70
71 char gameTitle[17] = { '\0' };
72 mCore* core = controller->thread()->core;
73 core->getGameTitle(core, gameTitle);
74 s_title = gameTitle;
75
76#ifdef USE_SQLITE3
77 if (db && crc32 && NoIntroDBLookupGameByCRC(db, crc32, &game)) {
78 s_title = QLatin1String(game.name);
79 }
80#endif
81
82 updatePresence();
83}
84
85void gameStopped() {
86 if (!s_gameRunning) {
87 return;
88 }
89 s_gameRunning = false;
90 updatePresence();
91}
92
93}
94
95}