From 10dcc7cbad3994970b5fc2609d81a78fd8b67760 Mon Sep 17 00:00:00 2001 From: NamedKitten Date: Mon, 12 Nov 2018 11:05:46 +0000 Subject: [PATCH] [Backend+CMake] Added discord rich presence support. --- .gitignore | 3 ++- CMakeLists.txt | 14 +++++++++++++- src/MpvPlayerBackend.cpp | 33 +++++++++++++++++++++++++++++++++ src/MpvPlayerBackend.h | 3 +++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 6bc3eac..c851c51 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,5 @@ sffmpeg *.jsc src_qml* qmlcache* -qrc_src* \ No newline at end of file +qrc_src* +discord-rpc diff --git a/CMakeLists.txt b/CMakeLists.txt index dd6b0c3..c374eb6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/src/MpvPlayerBackend.cpp b/src/MpvPlayerBackend.cpp index 74a7d3c..9525c38 100644 --- a/src/MpvPlayerBackend.cpp +++ b/src/MpvPlayerBackend.cpp @@ -12,6 +12,11 @@ #include #include +#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("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("menuBar"), "updateTracks"); } else if (strcmp(prop->name, "audio-device-list") == 0) { diff --git a/src/MpvPlayerBackend.h b/src/MpvPlayerBackend.h index e9214ed..96accd1 100644 --- a/src/MpvPlayerBackend.h +++ b/src/MpvPlayerBackend.h @@ -29,6 +29,9 @@ public: public slots: void launchAboutQt(); +#ifdef DISCORD + void updateDiscord(); +#endif void togglePlayPause(); void toggleMute(); void nextAudioTrack();