2018-05-29 04:43:30 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2018 - Andrés Suárez
|
|
|
|
*
|
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "discord.h"
|
2018-08-04 22:41:07 +00:00
|
|
|
#include "retroarch.h"
|
|
|
|
#include "core.h"
|
|
|
|
#include "core_info.h"
|
|
|
|
#include "paths.h"
|
|
|
|
#include <file/file_path.h>
|
2018-05-29 04:43:30 +00:00
|
|
|
|
2018-05-29 09:14:21 +00:00
|
|
|
#include "../msg_hash.h"
|
|
|
|
|
2018-08-05 00:14:21 +00:00
|
|
|
static const char* APPLICATION_ID = "475456035851599874";
|
2018-05-29 08:40:01 +00:00
|
|
|
static int FrustrationLevel = 0;
|
|
|
|
static int64_t start_time = 0;
|
2018-05-29 04:43:30 +00:00
|
|
|
|
2018-05-29 08:40:01 +00:00
|
|
|
static bool discord_ready = false;
|
|
|
|
static unsigned discord_status = 0;
|
2018-05-29 04:43:30 +00:00
|
|
|
|
|
|
|
DiscordRichPresence discord_presence;
|
|
|
|
|
|
|
|
static void handle_discord_ready(const DiscordUser* connectedUser)
|
|
|
|
{
|
2018-05-29 08:40:01 +00:00
|
|
|
RARCH_LOG("[Discord] connected to user %s#%s - %s\n",
|
2018-05-29 04:43:30 +00:00
|
|
|
connectedUser->username,
|
|
|
|
connectedUser->discriminator,
|
|
|
|
connectedUser->userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_discord_disconnected(int errcode, const char* message)
|
|
|
|
{
|
2018-05-29 08:40:01 +00:00
|
|
|
RARCH_LOG("[Discord] disconnected (%d: %s)\n", errcode, message);
|
2018-05-29 04:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_discord_error(int errcode, const char* message)
|
|
|
|
{
|
2018-05-29 08:40:01 +00:00
|
|
|
RARCH_LOG("[Discord] error (%d: %s)\n", errcode, message);
|
2018-05-29 04:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_discord_join(const char* secret)
|
|
|
|
{
|
2018-05-29 08:40:01 +00:00
|
|
|
RARCH_LOG("[Discord] join (%s)\n", secret);
|
2018-05-29 04:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_discord_spectate(const char* secret)
|
|
|
|
{
|
2018-05-29 08:40:01 +00:00
|
|
|
RARCH_LOG("[Discord] spectate (%s)\n", secret);
|
2018-05-29 04:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_discord_join_request(const DiscordUser* request)
|
|
|
|
{
|
|
|
|
int response = -1;
|
|
|
|
char yn[4];
|
2018-05-29 08:40:01 +00:00
|
|
|
RARCH_LOG("[Discord] join request from %s#%s - %s\n",
|
2018-05-29 04:43:30 +00:00
|
|
|
request->username,
|
|
|
|
request->discriminator,
|
|
|
|
request->userId);
|
|
|
|
}
|
|
|
|
|
2018-05-29 09:09:13 +00:00
|
|
|
void discord_update(enum discord_presence presence)
|
2018-05-29 04:43:30 +00:00
|
|
|
{
|
2018-08-04 22:41:07 +00:00
|
|
|
rarch_system_info_t *system = runloop_get_system_info();
|
|
|
|
core_info_t *core_info = NULL;
|
|
|
|
|
2018-05-29 08:40:01 +00:00
|
|
|
if (!discord_ready)
|
2018-05-29 04:43:30 +00:00
|
|
|
return;
|
2018-08-12 14:33:52 +00:00
|
|
|
|
2018-05-29 08:40:01 +00:00
|
|
|
if (
|
|
|
|
(discord_status != DISCORD_PRESENCE_MENU) &&
|
|
|
|
(discord_status == presence))
|
|
|
|
return;
|
|
|
|
|
|
|
|
RARCH_LOG("[Discord] updating (%d)\n", presence);
|
2018-05-29 04:43:30 +00:00
|
|
|
|
|
|
|
memset(&discord_presence, 0, sizeof(discord_presence));
|
|
|
|
|
|
|
|
switch (presence)
|
|
|
|
{
|
|
|
|
case DISCORD_PRESENCE_MENU:
|
2018-05-29 09:14:21 +00:00
|
|
|
discord_presence.state = msg_hash_to_str(MENU_ENUM_LABEL_VALUE_DISCORD_IN_MENU);
|
2018-08-04 22:41:07 +00:00
|
|
|
discord_presence.largeImageKey = "base";
|
2018-05-29 08:40:01 +00:00
|
|
|
discord_presence.instance = 0;
|
|
|
|
discord_presence.startTimestamp = start_time;
|
2018-05-29 04:43:30 +00:00
|
|
|
break;
|
|
|
|
case DISCORD_PRESENCE_GAME:
|
2018-08-12 14:33:52 +00:00
|
|
|
core_info_get_current_core(&core_info);
|
|
|
|
|
|
|
|
if (core_info)
|
2018-08-04 22:41:07 +00:00
|
|
|
{
|
2018-08-12 14:33:52 +00:00
|
|
|
const char *system_name = string_replace_substring(
|
|
|
|
string_to_lower(core_info->core_name), " ", "_");
|
2018-08-04 22:41:07 +00:00
|
|
|
|
|
|
|
start_time = time(0);
|
|
|
|
discord_presence.state = system ? system->info.library_name : "---";
|
|
|
|
discord_presence.details = path_basename(path_get(RARCH_PATH_BASENAME));
|
|
|
|
#if 1
|
|
|
|
RARCH_LOG("[Discord] system name: %s\n", system_name);
|
2018-05-29 08:40:01 +00:00
|
|
|
#endif
|
2018-08-04 22:41:07 +00:00
|
|
|
discord_presence.largeImageKey = system_name;
|
|
|
|
|
|
|
|
discord_presence.smallImageKey = "base";
|
|
|
|
|
|
|
|
discord_presence.instance = 0;
|
|
|
|
discord_presence.startTimestamp = start_time;
|
|
|
|
|
|
|
|
}
|
2018-05-29 04:43:30 +00:00
|
|
|
break;
|
2018-05-29 09:09:13 +00:00
|
|
|
case DISCORD_PRESENCE_NETPLAY_HOSTING:
|
|
|
|
case DISCORD_PRESENCE_NETPLAY_CLIENT:
|
|
|
|
case DISCORD_PRESENCE_CHEEVO_UNLOCKED:
|
|
|
|
/* TODO/FIXME */
|
2018-05-29 04:43:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
Discord_UpdatePresence(&discord_presence);
|
2018-05-29 08:40:01 +00:00
|
|
|
discord_status = presence;
|
2018-05-29 04:43:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 08:40:01 +00:00
|
|
|
void discord_init(void)
|
2018-05-29 04:43:30 +00:00
|
|
|
{
|
|
|
|
DiscordEventHandlers handlers;
|
2018-05-29 08:40:01 +00:00
|
|
|
|
2018-05-29 09:14:21 +00:00
|
|
|
RARCH_LOG("[Discord] initializing ..\n");
|
2018-05-29 08:40:01 +00:00
|
|
|
start_time = time(0);
|
|
|
|
|
2018-05-29 04:43:30 +00:00
|
|
|
memset(&handlers, 0, sizeof(handlers));
|
2018-05-29 08:40:01 +00:00
|
|
|
handlers.ready = handle_discord_ready;
|
2018-05-29 04:43:30 +00:00
|
|
|
handlers.disconnected = handle_discord_disconnected;
|
2018-05-29 08:40:01 +00:00
|
|
|
handlers.errored = handle_discord_error;
|
|
|
|
handlers.joinGame = handle_discord_join;
|
2018-05-29 04:43:30 +00:00
|
|
|
handlers.spectateGame = handle_discord_spectate;
|
2018-05-29 08:40:01 +00:00
|
|
|
handlers.joinRequest = handle_discord_join_request;
|
|
|
|
|
2018-05-29 04:43:30 +00:00
|
|
|
Discord_Initialize(APPLICATION_ID, &handlers, 1, NULL);
|
|
|
|
|
2018-05-29 08:40:01 +00:00
|
|
|
discord_ready = true;
|
2018-05-29 04:43:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 08:40:01 +00:00
|
|
|
void discord_shutdown(void)
|
2018-05-29 04:43:30 +00:00
|
|
|
{
|
2018-05-29 09:14:21 +00:00
|
|
|
RARCH_LOG("[Discord] shutting down ..\n");
|
2018-05-29 04:43:30 +00:00
|
|
|
Discord_ClearPresence();
|
|
|
|
Discord_Shutdown();
|
|
|
|
discord_ready = false;
|
2018-05-29 08:40:01 +00:00
|
|
|
}
|