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 mCore *core = controller->thread()->core;
67 s_title = core->dirs.baseName;
68
69#ifdef USE_SQLITE3
70 const NoIntroDB* db = GBAApp::app()->gameDB();
71 NoIntroGame game{};
72 uint32_t crc32 = 0;
73 core->checksum(core, &crc32, CHECKSUM_CRC32);
74
75 if (db && crc32 && NoIntroDBLookupGameByCRC(db, crc32, &game)) {
76 s_title = QLatin1String(game.name);
77 }
78#endif
79
80 updatePresence();
81}
82
83void gameStopped() {
84 if (!s_gameRunning) {
85 return;
86 }
87 s_gameRunning = false;
88 updatePresence();
89}
90
91}
92
93}