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