2019-12-04 13:12:34 +01:00
|
|
|
/*
|
2022-11-13 16:37:58 -05:00
|
|
|
* 86Box A hypervisor and IBM PC system emulator that specializes in
|
|
|
|
|
* running old operating systems and software designed for IBM
|
|
|
|
|
* PC systems and compatibles from 1981 through fairly recent
|
|
|
|
|
* system designs based on the PCI bus.
|
2019-12-04 13:12:34 +01:00
|
|
|
*
|
2022-11-13 16:37:58 -05:00
|
|
|
* This file is part of the 86Box distribution.
|
2019-12-04 13:12:34 +01:00
|
|
|
*
|
2022-11-13 16:37:58 -05:00
|
|
|
* Discord integration module.
|
2019-12-04 13:12:34 +01:00
|
|
|
*
|
2020-03-25 00:46:02 +02:00
|
|
|
*
|
2019-12-04 13:12:34 +01:00
|
|
|
*
|
2022-11-13 16:37:58 -05:00
|
|
|
* Authors: David Hrdlička, <hrdlickadavid@outlook.com>
|
2019-12-04 13:12:34 +01:00
|
|
|
*
|
2022-11-13 16:37:58 -05:00
|
|
|
* Copyright 2019 David Hrdlička.
|
2019-12-04 13:12:34 +01:00
|
|
|
*/
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <stdint.h>
|
2022-07-15 16:01:24 +02:00
|
|
|
#include <stdio.h>
|
2019-12-04 13:12:34 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#define HAVE_STDARG_H
|
2022-01-09 01:04:59 +06:00
|
|
|
#include "cpu/cpu.h"
|
2022-07-15 16:01:24 +02:00
|
|
|
#include <86box/86box.h>
|
|
|
|
|
#include <86box/discord.h>
|
2020-03-29 14:24:42 +02:00
|
|
|
#include <86box/machine.h>
|
|
|
|
|
#include <86box/plat.h>
|
|
|
|
|
#include <86box/plat_dynld.h>
|
|
|
|
|
#include <discord_game_sdk.h>
|
2019-12-04 13:12:34 +01:00
|
|
|
|
2022-01-09 01:04:59 +06:00
|
|
|
#ifdef _WIN32
|
2022-07-15 16:01:24 +02:00
|
|
|
# define PATH_DISCORD_DLL "discord_game_sdk.dll"
|
2022-01-09 01:04:59 +06:00
|
|
|
#elif defined __APPLE__
|
2022-07-15 16:01:24 +02:00
|
|
|
# define PATH_DISCORD_DLL "discord_game_sdk.dylib"
|
2022-01-09 01:04:59 +06:00
|
|
|
#else
|
2022-07-15 16:01:24 +02:00
|
|
|
# define PATH_DISCORD_DLL "discord_game_sdk.so"
|
2022-01-09 01:04:59 +06:00
|
|
|
#endif
|
2019-12-04 13:12:34 +01:00
|
|
|
|
2022-07-15 16:01:24 +02:00
|
|
|
int discord_loaded = 0;
|
2019-12-04 21:55:35 +01:00
|
|
|
|
2022-07-15 16:01:24 +02:00
|
|
|
static void *discord_handle = NULL;
|
|
|
|
|
static struct IDiscordCore *discord_core = NULL;
|
|
|
|
|
static struct IDiscordActivityManager *discord_activities = NULL;
|
2019-12-04 13:12:34 +01:00
|
|
|
|
2022-07-15 16:01:24 +02:00
|
|
|
static enum EDiscordResult(DISCORD_API *discord_create)(DiscordVersion version, struct DiscordCreateParams *params, struct IDiscordCore **result);
|
2019-12-04 13:12:34 +01:00
|
|
|
|
|
|
|
|
static dllimp_t discord_imports[] = {
|
2022-07-15 16:01:24 +02:00
|
|
|
{"DiscordCreate", &discord_create},
|
|
|
|
|
{ NULL, NULL }
|
2019-12-04 13:12:34 +01:00
|
|
|
};
|
|
|
|
|
|
2020-11-12 21:27:11 +01:00
|
|
|
#ifdef ENABLE_DISCORD_LOG
|
2019-12-04 13:12:34 +01:00
|
|
|
int discord_do_log = 1;
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
discord_log(const char *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
|
|
if (discord_do_log) {
|
2022-07-15 16:01:24 +02:00
|
|
|
va_start(ap, fmt);
|
|
|
|
|
pclog_ex(fmt, ap);
|
|
|
|
|
va_end(ap);
|
2019-12-04 13:12:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#else
|
2022-07-15 16:01:24 +02:00
|
|
|
# define discord_log(fmt, ...)
|
2019-12-04 13:12:34 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
discord_update_activity(int paused)
|
|
|
|
|
{
|
|
|
|
|
struct DiscordActivity activity;
|
2022-07-15 16:01:24 +02:00
|
|
|
char cpufamily[1024];
|
|
|
|
|
char *paren;
|
2019-12-04 13:12:34 +01:00
|
|
|
|
2022-07-15 16:01:24 +02:00
|
|
|
if (discord_activities == NULL)
|
|
|
|
|
return;
|
2019-12-04 13:12:34 +01:00
|
|
|
|
2022-01-09 01:04:59 +06:00
|
|
|
discord_log("discord: discord_update_activity(paused=%d)\n", paused);
|
2019-12-04 13:12:34 +01:00
|
|
|
|
|
|
|
|
memset(&activity, 0x00, sizeof(activity));
|
|
|
|
|
|
2021-07-09 00:24:21 +05:00
|
|
|
strncpy(cpufamily, cpu_f->name, sizeof(cpufamily) - 1);
|
|
|
|
|
paren = strchr(cpufamily, '(');
|
|
|
|
|
if (paren)
|
2022-07-15 16:01:24 +02:00
|
|
|
*(paren - 1) = '\0';
|
2021-07-09 00:24:21 +05:00
|
|
|
|
2022-04-14 06:56:03 +05:00
|
|
|
#pragma GCC diagnostic push
|
2023-02-28 23:24:58 -05:00
|
|
|
#if defined(__GNUC__) && !defined(__clang__)
|
2022-07-15 16:01:24 +02:00
|
|
|
# pragma GCC diagnostic ignored "-Wformat-truncation"
|
2022-04-22 20:58:08 -04:00
|
|
|
#endif
|
2022-07-15 16:01:24 +02:00
|
|
|
if (strlen(vm_name) < 100) {
|
|
|
|
|
snprintf(activity.details, sizeof(activity.details), "Running \"%s\"", vm_name);
|
|
|
|
|
snprintf(activity.state, sizeof(activity.state), "%s (%s/%s)", strchr(machine_getname(), ']') + 2, cpufamily, cpu_s->name);
|
|
|
|
|
} else {
|
|
|
|
|
strncpy(activity.details, strchr(machine_getname(), ']') + 2, sizeof(activity.details) - 1);
|
|
|
|
|
snprintf(activity.state, sizeof(activity.state), "%s/%s", cpufamily, cpu_s->name);
|
2019-12-04 13:12:34 +01:00
|
|
|
}
|
2022-04-14 06:56:03 +05:00
|
|
|
#pragma GCC diagnostic pop
|
2019-12-04 13:12:34 +01:00
|
|
|
|
|
|
|
|
activity.timestamps.start = time(NULL);
|
|
|
|
|
|
2022-07-15 16:01:24 +02:00
|
|
|
/* Icon choosing for Discord based on 86Box.rc */
|
2021-11-08 19:10:37 +01:00
|
|
|
|
2019-12-04 13:12:34 +01:00
|
|
|
#ifdef RELEASE_BUILD
|
2022-07-15 16:01:24 +02:00
|
|
|
/* Icon by OBattler and laciba96 (green for release builds)*/
|
2021-11-08 19:10:37 +01:00
|
|
|
strcpy(activity.assets.large_image, "86box-green");
|
|
|
|
|
#elif BETA_BUILD
|
2022-07-15 16:01:24 +02:00
|
|
|
/* Icon by OBattler and laciba96 (yellow for beta builds done by Jenkins)*/
|
2021-11-08 19:10:37 +01:00
|
|
|
strcpy(activity.assets.large_image, "86box-yellow");
|
|
|
|
|
#elif ALPHA_BUILD
|
2022-07-15 16:01:24 +02:00
|
|
|
/* Icon by OBattler and laciba96 (red for alpha builds done by Jenkins)*/
|
2021-11-08 19:10:37 +01:00
|
|
|
strcpy(activity.assets.large_image, "86box-red");
|
2019-12-04 13:12:34 +01:00
|
|
|
#else
|
2022-07-15 16:01:24 +02:00
|
|
|
/* Icon by OBattler and laciba96 (gray for builds of branches and from the git master)*/
|
2021-11-08 21:55:55 +01:00
|
|
|
strcpy(activity.assets.large_image, "86box");
|
2019-12-04 13:12:34 +01:00
|
|
|
#endif
|
|
|
|
|
|
2022-07-15 16:01:24 +02:00
|
|
|
/* End of icon choosing */
|
2021-11-08 19:10:37 +01:00
|
|
|
|
2022-07-15 16:01:24 +02:00
|
|
|
if (paused) {
|
|
|
|
|
strcpy(activity.assets.small_image, "status-paused");
|
|
|
|
|
strcpy(activity.assets.small_text, "Paused");
|
|
|
|
|
} else {
|
|
|
|
|
strcpy(activity.assets.small_image, "status-running");
|
|
|
|
|
strcpy(activity.assets.small_text, "Running");
|
2019-12-04 13:12:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
discord_activities->update_activity(discord_activities, &activity, NULL, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 21:55:35 +01:00
|
|
|
int
|
2022-11-17 22:44:06 +01:00
|
|
|
discord_load(void)
|
2019-12-04 13:12:34 +01:00
|
|
|
{
|
2019-12-04 21:55:35 +01:00
|
|
|
if (discord_handle != NULL)
|
2023-05-11 03:02:36 -04:00
|
|
|
return 1;
|
2019-12-04 13:12:34 +01:00
|
|
|
|
|
|
|
|
// Try to load the DLL
|
2019-12-04 21:55:35 +01:00
|
|
|
discord_handle = dynld_module(PATH_DISCORD_DLL, discord_imports);
|
2019-12-04 13:12:34 +01:00
|
|
|
|
2022-07-15 16:01:24 +02:00
|
|
|
if (discord_handle == NULL) {
|
|
|
|
|
discord_log("discord: couldn't load " PATH_DISCORD_DLL "\n");
|
|
|
|
|
discord_close();
|
2019-12-04 21:55:35 +01:00
|
|
|
|
2023-05-11 03:02:36 -04:00
|
|
|
return 0;
|
2019-12-04 13:12:34 +01:00
|
|
|
}
|
|
|
|
|
|
2019-12-04 21:55:35 +01:00
|
|
|
discord_loaded = 1;
|
2023-05-11 03:02:36 -04:00
|
|
|
return 1;
|
2019-12-04 21:55:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2022-11-17 22:44:06 +01:00
|
|
|
discord_init(void)
|
2019-12-04 21:55:35 +01:00
|
|
|
{
|
2022-07-15 16:01:24 +02:00
|
|
|
enum EDiscordResult result;
|
2019-12-04 21:55:35 +01:00
|
|
|
struct DiscordCreateParams params;
|
|
|
|
|
|
2022-07-15 16:01:24 +02:00
|
|
|
if (discord_handle == NULL)
|
|
|
|
|
return;
|
2019-12-04 21:55:35 +01:00
|
|
|
|
2019-12-04 13:12:34 +01:00
|
|
|
DiscordCreateParamsSetDefault(¶ms);
|
2021-11-07 18:29:49 +01:00
|
|
|
params.client_id = 906956844956782613;
|
2022-07-15 16:01:24 +02:00
|
|
|
params.flags = DiscordCreateFlags_NoRequireDiscord;
|
2019-12-04 13:12:34 +01:00
|
|
|
|
|
|
|
|
result = discord_create(DISCORD_VERSION, ¶ms, &discord_core);
|
2022-07-15 16:01:24 +02:00
|
|
|
if (result != DiscordResult_Ok) {
|
|
|
|
|
discord_log("discord: DiscordCreate returned %d\n", result);
|
|
|
|
|
discord_close();
|
|
|
|
|
return;
|
2019-12-04 13:12:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
discord_activities = discord_core->get_activity_manager(discord_core);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2022-11-17 22:44:06 +01:00
|
|
|
discord_close(void)
|
2019-12-04 13:12:34 +01:00
|
|
|
{
|
|
|
|
|
if (discord_core != NULL)
|
2022-07-15 16:01:24 +02:00
|
|
|
discord_core->destroy(discord_core);
|
2019-12-04 13:12:34 +01:00
|
|
|
|
2022-07-15 16:01:24 +02:00
|
|
|
discord_core = NULL;
|
2019-12-04 13:12:34 +01:00
|
|
|
discord_activities = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2022-11-17 22:44:06 +01:00
|
|
|
discord_run_callbacks(void)
|
2019-12-04 13:12:34 +01:00
|
|
|
{
|
2022-07-15 16:01:24 +02:00
|
|
|
if (discord_core == NULL)
|
|
|
|
|
return;
|
2019-12-04 13:12:34 +01:00
|
|
|
|
|
|
|
|
discord_core->run_callbacks(discord_core);
|
|
|
|
|
}
|