all repos — mgba @ c4b38790f211b65cb15af26cebe9fc25e3c19914

mGBA Game Boy Advance Emulator

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	s_title = controller->thread()->core->dirs.baseName;
68	QString dbTitle = controller->dbTitle();
69	if (!dbTitle.isNull()) {
70		s_title = dbTitle;
71	}
72	updatePresence();
73}
74
75void gameStopped() {
76	if (!s_gameRunning) {
77		return;
78	}
79	s_gameRunning = false;
80	updatePresence();
81}
82
83}
84
85}