Moved discordrpc over to the new architecture
This commit is contained in:
+51
-46
@@ -10,7 +10,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "external/discord-rpc/include/discord_rpc.h"
|
#include "external/discord-rpc/include/discord_rpc.h"
|
||||||
#include "libopensubsonic/logger.h"
|
|
||||||
#include "configHandler.h"
|
#include "configHandler.h"
|
||||||
#include "discordrpc.h"
|
#include "discordrpc.h"
|
||||||
|
|
||||||
@@ -23,79 +22,85 @@ const char* discordrpc_appid = "1407025303779278980";
|
|||||||
char* discordrpc_osString = NULL;
|
char* discordrpc_osString = NULL;
|
||||||
static int rc = 0;
|
static int rc = 0;
|
||||||
|
|
||||||
void discordrpc_struct_init(discordrpc_data** discordrpc_struct) {
|
OSSP_discordrpc_t* OSSP_discordrpc_Constructor() {
|
||||||
(*discordrpc_struct) = malloc(sizeof(discordrpc_data));
|
printf("Running discordrpc Constructor.\n");
|
||||||
(*discordrpc_struct)->state = 0;
|
OSSP_discordrpc_t* obj = malloc(sizeof(OSSP_discordrpc_t));
|
||||||
(*discordrpc_struct)->songLength = 0;
|
if (obj == NULL) {
|
||||||
(*discordrpc_struct)->songTitle = NULL;
|
return NULL;
|
||||||
(*discordrpc_struct)->songArtist = NULL;
|
}
|
||||||
(*discordrpc_struct)->coverArtUrl = NULL;
|
obj->state = 0;
|
||||||
|
obj->songLength = 0;
|
||||||
|
obj->startTime = 0;
|
||||||
|
obj->songTitle = NULL;
|
||||||
|
obj->songArtist = NULL;
|
||||||
|
obj->coverArtUrl = NULL;
|
||||||
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
void discordrpc_struct_deinit(discordrpc_data** discordrpc_struct) {
|
void OSSP_discordrpc_Deconstructor(OSSP_discordrpc_t* obj) {
|
||||||
if ((*discordrpc_struct)->songTitle != NULL) { free((*discordrpc_struct)->songTitle); }
|
printf("Running discordrpc Deconstructor.\n");
|
||||||
if ((*discordrpc_struct)->songArtist != NULL) { free((*discordrpc_struct)->songArtist); }
|
if (obj->songTitle != NULL) { free(obj->songTitle); }
|
||||||
if ((*discordrpc_struct)->coverArtUrl != NULL) { free((*discordrpc_struct)->coverArtUrl); }
|
if (obj->songArtist != NULL) { free(obj->songArtist); }
|
||||||
if (*discordrpc_struct != NULL) { free(*discordrpc_struct); }
|
if (obj->coverArtUrl != NULL) { free(obj->coverArtUrl); }
|
||||||
|
if (obj != NULL) { free(obj); }
|
||||||
}
|
}
|
||||||
|
|
||||||
int discordrpc_init() {
|
int OSSP_discordrpc_Init() {
|
||||||
printf("[DiscordRPC] Initializing.\n");
|
printf("[DiscordRPC] Initializing.\n");
|
||||||
// TODO Can I just not deal with the handler callbacks at all?
|
|
||||||
DiscordEventHandlers handlers;
|
DiscordEventHandlers handlers;
|
||||||
memset(&handlers, 0, sizeof(handlers));
|
memset(&handlers, 0, sizeof(handlers));
|
||||||
Discord_Initialize(discordrpc_appid, &handlers, 1, NULL);
|
Discord_Initialize(discordrpc_appid, &handlers, 1, NULL);
|
||||||
|
|
||||||
// Fetch OS String for RPC (Heap-allocated)
|
// Fetch OS String for RPC (Heap-allocated)
|
||||||
discordrpc_osString = discordrpc_getOS();
|
discordrpc_osString = OSSP_discordrpc_getOS();
|
||||||
if (discordrpc_osString == NULL) {
|
if (discordrpc_osString == NULL) {
|
||||||
logger_log_error(__func__, "asprintf() or strdup() failed.");
|
printf("[DiscordRPC] (%s) asprintf() or strdup() failed.\n", __func__);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void discordrpc_update(discordrpc_data** discordrpc_struct) {
|
void OSSP_discordrpc_update(OSSP_discordrpc_t* obj) {
|
||||||
printf("[DiscordRPC] Updating...\n");
|
printf("[DiscordRPC] Updating...\n");
|
||||||
DiscordRichPresence presence;
|
DiscordRichPresence presence;
|
||||||
char* detailsString = NULL;
|
char* detailsString = NULL;
|
||||||
char* stateString = NULL;
|
char* stateString = NULL;
|
||||||
memset(&presence, 0, sizeof(presence));
|
memset(&presence, 0, sizeof(presence));
|
||||||
|
|
||||||
if ((*discordrpc_struct)->state == DISCORDRPC_STATE_IDLE) {
|
if (obj->state == DISCORDRPC_STATE_IDLE) {
|
||||||
printf("[DiscordRPC] Issuing Idle RPC.\n");
|
printf("[DiscordRPC] Issuing Idle RPC.\n");
|
||||||
asprintf(&detailsString, "Idle");
|
asprintf(&detailsString, "Idle");
|
||||||
presence.details = detailsString;
|
presence.details = detailsString;
|
||||||
} else if ((*discordrpc_struct)->state == DISCORDRPC_STATE_PLAYING_OPENSUBSONIC ||
|
} else if (obj->state == DISCORDRPC_STATE_PLAYING_OPENSUBSONIC ||
|
||||||
((*discordrpc_struct)->state == DISCORDRPC_STATE_PLAYING_LOCALFILE)) {
|
(obj->state == DISCORDRPC_STATE_PLAYING_LOCALFILE)) {
|
||||||
// Playing a song from an OpenSubsonic server
|
// Playing a song from an OpenSubsonic server
|
||||||
printf("[DiscordRPC] Issuing OpenSubsonic/Local File Song RPC.\n");
|
printf("[DiscordRPC] Issuing OpenSubsonic/Local File Song RPC.\n");
|
||||||
asprintf(&detailsString, "%s", (*discordrpc_struct)->songTitle);
|
asprintf(&detailsString, "%s", obj->songTitle);
|
||||||
asprintf(&stateString, "by %s", (*discordrpc_struct)->songArtist);
|
asprintf(&stateString, "by %s", obj->songArtist);
|
||||||
presence.details = detailsString;
|
presence.details = detailsString;
|
||||||
presence.state = stateString;
|
presence.state = stateString;
|
||||||
if ((*discordrpc_struct)->state == DISCORDRPC_STATE_PLAYING_OPENSUBSONIC) {
|
if (obj->state == DISCORDRPC_STATE_PLAYING_OPENSUBSONIC) {
|
||||||
// TODO As of now, local file playback does NOT deal with cover art
|
// TODO As of now, local file playback does NOT deal with cover art
|
||||||
presence.largeImageKey = (*discordrpc_struct)->coverArtUrl;
|
presence.largeImageKey = obj->coverArtUrl;
|
||||||
}
|
}
|
||||||
presence.startTimestamp = (long)((*discordrpc_struct)->startTime);
|
presence.startTimestamp = (long)(obj->startTime);
|
||||||
presence.endTimestamp = (long)((*discordrpc_struct)->startTime) + (*discordrpc_struct)->songLength;
|
presence.endTimestamp = (long)(obj->startTime) + obj->songLength;
|
||||||
if (configObj->discordrpc_showSysDetails) {
|
if (configObj->discordrpc_showSysDetails) {
|
||||||
presence.largeImageText = discordrpc_osString;
|
presence.largeImageText = discordrpc_osString;
|
||||||
}
|
}
|
||||||
} else if ((*discordrpc_struct)->state == DISCORDRPC_STATE_PLAYING_INTERNETRADIO) {
|
} else if (obj->state == DISCORDRPC_STATE_PLAYING_INTERNETRADIO) {
|
||||||
// Playing an internet radio station
|
// Playing an internet radio station
|
||||||
printf("[DiscordRPC] Issuing Internet Radio RPC.\n");
|
printf("[DiscordRPC] Issuing Internet Radio RPC.\n");
|
||||||
asprintf(&detailsString, "%s", (*discordrpc_struct)->songTitle);
|
asprintf(&detailsString, "%s", obj->songTitle);
|
||||||
asprintf(&stateString, "Internet radio station");
|
asprintf(&stateString, "Internet radio station");
|
||||||
presence.details = detailsString;
|
presence.details = detailsString;
|
||||||
presence.state = stateString;
|
presence.state = stateString;
|
||||||
presence.largeImageKey = (*discordrpc_struct)->coverArtUrl;
|
presence.largeImageKey = obj->coverArtUrl;
|
||||||
presence.startTimestamp = (long)((*discordrpc_struct)->startTime);
|
presence.startTimestamp = (long)(obj->startTime);
|
||||||
if (configObj->discordrpc_showSysDetails) {
|
if (configObj->discordrpc_showSysDetails) {
|
||||||
presence.largeImageText = discordrpc_osString;
|
presence.largeImageText = discordrpc_osString;
|
||||||
}
|
}
|
||||||
} else if ((*discordrpc_struct)->state == DISCORDRPC_STATE_PAUSED) {
|
} else if (obj->state == DISCORDRPC_STATE_PAUSED) {
|
||||||
// Player is paused
|
// Player is paused
|
||||||
printf("[DiscordRPC] Issuing Paused RPC.\n");
|
printf("[DiscordRPC] Issuing Paused RPC.\n");
|
||||||
asprintf(&detailsString, "Paused");
|
asprintf(&detailsString, "Paused");
|
||||||
@@ -109,46 +114,46 @@ void discordrpc_update(discordrpc_data** discordrpc_struct) {
|
|||||||
if (stateString != NULL) { free(stateString); }
|
if (stateString != NULL) { free(stateString); }
|
||||||
}
|
}
|
||||||
|
|
||||||
char* discordrpc_getOS() {
|
char* OSSP_discordrpc_getOS() {
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
// NOTE: Could have made a sysctl function, but this is literally only done here, not worth it
|
// NOTE: Could have made a sysctl function, but this is literally only done here, not worth it
|
||||||
FILE* fp_ostype = fopen("/proc/sys/kernel/ostype", "r");
|
FILE* fp_ostype = fopen("/proc/sys/kernel/ostype", "r");
|
||||||
char buf_ostype[16];
|
char buf_ostype[16];
|
||||||
if (!fp_ostype) {
|
if (!fp_ostype) {
|
||||||
logger_log_error(__func__, "Could not perform kernel.ostype sysctl.");
|
printf("[DiscordRPC] (%s) Could not perform kernel.ostype sysctl.\n", __func__);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE* fp_osrelease = fopen("/proc/sys/kernel/osrelease", "r");
|
FILE* fp_osrelease = fopen("/proc/sys/kernel/osrelease", "r");
|
||||||
char buf_osrelease[32];
|
char buf_osrelease[32];
|
||||||
if (!fp_osrelease) {
|
if (!fp_osrelease) {
|
||||||
logger_log_error(__func__, "Could not perform kernel.osrelease sysctl.");
|
printf("[DiscordRPC] (%s) Could not perform kernel.osrelease sysctl.\n", __func__);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE* fp_osarch = fopen("/proc/sys/kernel/arch", "r");
|
FILE* fp_osarch = fopen("/proc/sys/kernel/arch", "r");
|
||||||
char buf_osarch[16];
|
char buf_osarch[16];
|
||||||
if (!fp_osarch) {
|
if (!fp_osarch) {
|
||||||
logger_log_error(__func__, "Could not perform kernel.arch sysctl.");
|
printf("[DiscordRPC] (%s) Could not perform kernel.arch sysctl.\n", __func__);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fgets(buf_ostype, sizeof(buf_ostype), fp_ostype) == NULL) {
|
if (fgets(buf_ostype, sizeof(buf_ostype), fp_ostype) == NULL) {
|
||||||
logger_log_error(__func__, "Could not perform kernel.ostype sysctl.");
|
printf("[DiscordRPC] (%s) Could not perform kernel.ostype sysctl.\n", __func__);
|
||||||
fclose(fp_ostype);
|
fclose(fp_ostype);
|
||||||
fclose(fp_osrelease);
|
fclose(fp_osrelease);
|
||||||
fclose(fp_osarch);
|
fclose(fp_osarch);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (fgets(buf_osrelease, sizeof(buf_osrelease), fp_osrelease) == NULL) {
|
if (fgets(buf_osrelease, sizeof(buf_osrelease), fp_osrelease) == NULL) {
|
||||||
logger_log_error(__func__, "Could not perform kernel.osrelease sysctl.");
|
printf("[DiscordRPC] (%s) Could not perform kernel.osrelease sysctl.\n", __func__);
|
||||||
fclose(fp_ostype);
|
fclose(fp_ostype);
|
||||||
fclose(fp_osrelease);
|
fclose(fp_osrelease);
|
||||||
fclose(fp_osarch);
|
fclose(fp_osarch);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (fgets(buf_osarch, sizeof(buf_osarch), fp_osarch) == NULL) {
|
if (fgets(buf_osarch, sizeof(buf_osarch), fp_osarch) == NULL) {
|
||||||
logger_log_error(__func__, "Could not perform kernel.arch sysctl.");
|
printf("[DiscordRPC] (%s) Could not perform kernel.arch sysctl.\n", __func__);
|
||||||
fclose(fp_ostype);
|
fclose(fp_ostype);
|
||||||
fclose(fp_osrelease);
|
fclose(fp_osrelease);
|
||||||
fclose(fp_osarch);
|
fclose(fp_osarch);
|
||||||
@@ -166,7 +171,7 @@ char* discordrpc_getOS() {
|
|||||||
char* osString = NULL;
|
char* osString = NULL;
|
||||||
rc = asprintf(&osString, "on %s %s %s", buf_ostype, buf_osarch, buf_osrelease);
|
rc = asprintf(&osString, "on %s %s %s", buf_ostype, buf_osarch, buf_osrelease);
|
||||||
if (rc == -1) {
|
if (rc == -1) {
|
||||||
logger_log_error(__func__, "asprintf() failed.");
|
printf("[DiscordRPC] (%s) asprintf() failed.\n", __func__);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return osString;
|
return osString;
|
||||||
@@ -184,24 +189,24 @@ char* discordrpc_getOS() {
|
|||||||
mib[0] = CTL_KERN;
|
mib[0] = CTL_KERN;
|
||||||
mib[1] = KERN_OSTYPE;
|
mib[1] = KERN_OSTYPE;
|
||||||
if (sysctl(mib, 2, buf_ostype, &sz_ostype, NULL, 0) == -1) {
|
if (sysctl(mib, 2, buf_ostype, &sz_ostype, NULL, 0) == -1) {
|
||||||
logger_log_error(__func__, "Could not perform kern.ostype sysctl.");
|
printf("[DiscordRPC] (%s) Could not perform kern.ostype sysctl.\n", __func__);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
mib[1] = KERN_OSRELEASE;
|
mib[1] = KERN_OSRELEASE;
|
||||||
if (sysctl(mib, 2, buf_osrelease, &sz_osrelease, NULL, 0) == -1) {
|
if (sysctl(mib, 2, buf_osrelease, &sz_osrelease, NULL, 0) == -1) {
|
||||||
logger_log_error(__func__, "Could not perform kern.osrelease sysctl.");
|
printf("[DiscordRPC] (%s) Could not perform kern.osrelease sysctl.\n", __func__);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// hw.optional.arm64 does not seem to have a direct mib0/1 route
|
// hw.optional.arm64 does not seem to have a direct mib0/1 route
|
||||||
size_t mib_len = CTL_MAXNAME;
|
size_t mib_len = CTL_MAXNAME;
|
||||||
if (sysctlnametomib("hw.optional.arm64", mib, &mib_len) != 0) {
|
if (sysctlnametomib("hw.optional.arm64", mib, &mib_len) != 0) {
|
||||||
logger_log_error(__func__, "Could not perform hw.optional.arm64 sysctl.");
|
printf("[DiscordRPC] (%s) Could not perform hw.optional.arm64 sysctl.\n", __func__);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (sysctl(mib, mib_len, &isArm64, &sz_isArm64, NULL, 0) != 0) {
|
if (sysctl(mib, mib_len, &isArm64, &sz_isArm64, NULL, 0) != 0) {
|
||||||
logger_log_error(__func__, "Could not perform hw.optional.arm64 sysctl.");
|
printf("[DiscordRPC] (%s) Could not perform hw.optional.arm64 sysctl.\n", __func__);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,13 +217,13 @@ char* discordrpc_getOS() {
|
|||||||
rc = asprintf(&osString, "on %s XNU x86_64 %s", buf_ostype, buf_osrelease);
|
rc = asprintf(&osString, "on %s XNU x86_64 %s", buf_ostype, buf_osrelease);
|
||||||
}
|
}
|
||||||
if (rc == -1) {
|
if (rc == -1) {
|
||||||
logger_log_error(__func__, "asprintf() failed.");
|
printf("[DiscordRPC] (%s) asprintf() failed.\n", __func__);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return osString;
|
return osString;
|
||||||
#else
|
#else
|
||||||
// NOTE: This is not a critical error, just let the user know
|
// NOTE: This is not a critical error, just let the user know
|
||||||
logger_log_error(__func__, "Could not fetch OS details.");
|
printf("[DiscordRPC] (%s) Could not fetch OS details.\n", __func__);
|
||||||
return strdup("on Unknown");
|
return strdup("on Unknown");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-7
@@ -21,13 +21,12 @@ typedef struct {
|
|||||||
char* songTitle;
|
char* songTitle;
|
||||||
char* songArtist;
|
char* songArtist;
|
||||||
char* coverArtUrl;
|
char* coverArtUrl;
|
||||||
} discordrpc_data;
|
} OSSP_discordrpc_t;
|
||||||
|
|
||||||
|
OSSP_discordrpc_t* OSSP_discordrpc_Constructor();
|
||||||
void discordrpc_struct_init(discordrpc_data** discordrpc_struct);
|
void OSSP_discordrpc_Deconstructor(OSSP_discordrpc_t* obj);
|
||||||
void discordrpc_struct_deinit(discordrpc_data** discordrpc_struct);
|
int OSSP_discordrpc_Init();
|
||||||
int discordrpc_init();
|
void OSSP_discordrpc_update(OSSP_discordrpc_t* obj);
|
||||||
void discordrpc_update(discordrpc_data** discordrpc_struct);
|
char* OSSP_discordrpc_getOS();
|
||||||
char* discordrpc_getOS();
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user