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 discordPresence.largeImageKey = "mgba";
34 Discord_UpdatePresence(&discordPresence);
35 } else {
36 Discord_ClearPresence();
37 }
38}
39
40void init() {
41 if (s_inited) {
42 return;
43 }
44 DiscordEventHandlers handlers{};
45 Discord_Initialize("554440738952183828", &handlers, 1, nullptr);
46 s_inited = true;
47 updatePresence();
48}
49
50void deinit() {
51 if (!s_inited) {
52 return;
53 }
54 Discord_ClearPresence();
55 Discord_Shutdown();
56 s_inited = false;
57 s_gameRunning = false;
58}
59
60void gameStarted(std::shared_ptr<CoreController> controller) {
61 if (s_gameRunning) {
62 return;
63 }
64 s_gameRunning = true;
65
66 CoreController::Interrupter interrupter(controller);
67 mCore *core = controller->thread()->core;
68 s_title = core->dirs.baseName;
69
70#ifdef USE_SQLITE3
71 const NoIntroDB* db = GBAApp::app()->gameDB();
72 NoIntroGame game{};
73 uint32_t crc32 = 0;
74 core->checksum(core, &crc32, CHECKSUM_CRC32);
75
76 if (db && crc32 && NoIntroDBLookupGameByCRC(db, crc32, &game)) {
77 s_title = QLatin1String(game.name);
78 }
79#endif
80
81 updatePresence();
82}
83
84void gameStopped() {
85 if (!s_gameRunning) {
86 return;
87 }
88 s_gameRunning = false;
89 updatePresence();
90}
91
92}
93
94}