1
0
Fork 0

[Backend+CMake] Added discord rich presence support.

This commit is contained in:
NamedKitten 2018-11-12 11:05:46 +00:00
parent e5f21a553f
commit 10dcc7cbad
4 changed files with 51 additions and 2 deletions

3
.gitignore vendored
View file

@ -17,4 +17,5 @@ sffmpeg
*.jsc
src_qml*
qmlcache*
qrc_src*
qrc_src*
discord-rpc

View file

@ -7,6 +7,8 @@ set(CMAKE_AUTOMOC ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -fstrict-aliasing -Wno-deprecated-declarations -Wno-unused-variable")
option(DEVELOP "Enable runtime QML reloading for developing." OFF)
option(DISCORD "Enable Discord SDK." ON)
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
@ -53,10 +55,20 @@ add_definitions(-DQRC_SOURCE_PATH="${PROJECT_SOURCE_DIR}/src/qml")
add_definitions(-DQT_QML_DEBUG)
endif(DEVELOP)
if(DISCORD)
add_definitions(-DDISCORD)
execute_process(
COMMAND git clone --depth 1 https://github.com/discordapp/discord-rpc
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
add_subdirectory(discord-rpc)
INCLUDE_DIRECTORIES(discord-rpc/include)
endif(DISCORD)
add_executable(KittehPlayer ${SOURCES} ${qml_QRC})
# Use the Qml/Quick modules from Qt 5.
target_link_libraries(KittehPlayer ${MPV_LIBRARIES} ${X11_LIBRARIES} ${Xext_LIBRARIES} Qt5::X11Extras)
target_link_libraries(KittehPlayer ${MPV_LIBRARIES} ${X11_LIBRARIES} ${Xext_LIBRARIES} Qt5::X11Extras discord-rpc)
qt5_use_modules(KittehPlayer Qml Quick Core Gui Widgets X11Extras)

View file

@ -12,6 +12,11 @@
#include <QSequentialIterable>
#include <math.h>
#ifdef DISCORD
#include "discord_rpc.h"
#endif
namespace {
void wakeup(void* ctx)
@ -101,6 +106,12 @@ MpvPlayerBackend::MpvPlayerBackend(QQuickItem* parent)
if (!mpv)
throw std::runtime_error("could not create mpv context");
#ifdef DISCORD
DiscordEventHandlers handlers;
memset(&handlers, 0, sizeof(handlers));
Discord_Initialize("511220330996432896", &handlers, 1, NULL);
#endif
mpv_set_option_string(mpv, "terminal", "yes");
mpv_set_option_string(mpv, "msg-level", "all=v");
@ -181,6 +192,22 @@ void MpvPlayerBackend::launchAboutQt()
qapp->aboutQt();
}
#ifdef DISCORD
void MpvPlayerBackend::updateDiscord()
{
char buffer[256];
DiscordRichPresence discordPresence;
memset(&discordPresence, 0, sizeof(discordPresence));
discordPresence.state = getProperty("pause").toBool() ? "Paused" : "Playing";
sprintf(buffer, "Currently Playing: Video %s", getProperty("media-title").toString().toUtf8().constData());
discordPresence.details = buffer;
discordPresence.startTimestamp = time(0);
discordPresence.endTimestamp = time(0) + (getProperty("duration").toFloat() - getProperty("time-pos").toFloat());
discordPresence.instance = 0;
Discord_UpdatePresence(&discordPresence);
}
#endif
QVariant MpvPlayerBackend::getaudioDevices() const
{
QVariant drivers = getProperty("audio-device-list");
@ -420,6 +447,9 @@ void MpvPlayerBackend::handle_mpv_event(mpv_event* event)
if (prop->format == MPV_FORMAT_STRING) {
char* title = *(char**)prop->data;
findChild<QObject*>("titleLabel")->setProperty("text", title);
#ifdef DISCORD
updateDiscord();
#endif
}
} else if (strcmp(prop->name, "sub-text") == 0) {
if (prop->format == MPV_FORMAT_STRING) {
@ -439,6 +469,9 @@ void MpvPlayerBackend::handle_mpv_event(mpv_event* event)
} else if (strcmp(prop->name, "pause") == 0) {
qDebug() << prop->data;
updatePlayPause(getProperty("pause"));
#ifdef DISCORD
updateDiscord();
#endif
} else if (strcmp(prop->name, "tracks-menu") == 0) {
QMetaObject::invokeMethod(findChild<QObject*>("menuBar"), "updateTracks");
} else if (strcmp(prop->name, "audio-device-list") == 0) {

View file

@ -29,6 +29,9 @@ public:
public slots:
void launchAboutQt();
#ifdef DISCORD
void updateDiscord();
#endif
void togglePlayPause();
void toggleMute();
void nextAudioTrack();