From bbd8a24b7515b3d06477e4dbcdba54f6f213808a Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Sun, 23 Mar 2025 19:55:50 +0100 Subject: [PATCH] discord: Add namespace and cleanup CMakeLists --- .github/workflows/build.yml | 4 +- 3rdparty/discord-rpc/CMakeLists.txt | 1 + {thirdparty => 3rdparty}/discord-rpc/LICENSE | 0 .../discord-rpc/README.md | 0 .../discord-rpc/include/discord_register.h | 0 .../discord-rpc/include/discord_rpc.h | 4 + 3rdparty/discord-rpc/src/CMakeLists.txt | 41 +++++++++ .../discord-rpc/src/backoff.h | 12 ++- .../discord-rpc/src/connection.h | 7 +- .../discord-rpc/src/connection_unix.cpp | 19 ++-- .../discord-rpc/src/connection_win.cpp | 15 ++-- .../src/discord_register_linux.cpp | 19 ++-- .../discord-rpc/src/discord_register_osx.m | 0 .../discord-rpc/src/discord_register_win.cpp | 10 ++- .../discord-rpc/src/discord_rpc.cpp | 5 ++ .../discord-rpc/src/msg_queue.h | 6 +- .../discord-rpc/src/rpc_connection.cpp | 16 ++-- .../discord-rpc/src/rpc_connection.h | 5 ++ .../discord-rpc/src/serialization.cpp | 7 +- .../discord-rpc/src/serialization.h | 22 ++--- CMakeLists.txt | 10 ++- src/discord/richpresence.cpp | 2 + thirdparty/discord-rpc/CMakeLists.txt | 3 - thirdparty/discord-rpc/src/CMakeLists.txt | 87 ------------------- 24 files changed, 152 insertions(+), 143 deletions(-) create mode 100644 3rdparty/discord-rpc/CMakeLists.txt rename {thirdparty => 3rdparty}/discord-rpc/LICENSE (100%) rename {thirdparty => 3rdparty}/discord-rpc/README.md (100%) rename {thirdparty => 3rdparty}/discord-rpc/include/discord_register.h (100%) rename {thirdparty => 3rdparty}/discord-rpc/include/discord_rpc.h (97%) create mode 100644 3rdparty/discord-rpc/src/CMakeLists.txt rename {thirdparty => 3rdparty}/discord-rpc/src/backoff.h (73%) rename {thirdparty => 3rdparty}/discord-rpc/src/connection.h (84%) rename {thirdparty => 3rdparty}/discord-rpc/src/connection_unix.cpp (86%) rename {thirdparty => 3rdparty}/discord-rpc/src/connection_win.cpp (90%) rename {thirdparty => 3rdparty}/discord-rpc/src/discord_register_linux.cpp (86%) rename {thirdparty => 3rdparty}/discord-rpc/src/discord_register_osx.m (100%) rename {thirdparty => 3rdparty}/discord-rpc/src/discord_register_win.cpp (95%) rename {thirdparty => 3rdparty}/discord-rpc/src/discord_rpc.cpp (99%) rename {thirdparty => 3rdparty}/discord-rpc/src/msg_queue.h (89%) rename {thirdparty => 3rdparty}/discord-rpc/src/rpc_connection.cpp (88%) rename {thirdparty => 3rdparty}/discord-rpc/src/rpc_connection.h (96%) rename {thirdparty => 3rdparty}/discord-rpc/src/serialization.cpp (98%) rename {thirdparty => 3rdparty}/discord-rpc/src/serialization.h (95%) delete mode 100644 thirdparty/discord-rpc/CMakeLists.txt delete mode 100644 thirdparty/discord-rpc/src/CMakeLists.txt diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 81227f3f2..5e893a598 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -302,7 +302,7 @@ jobs: desktop-file-utils appstream appstream-util - hicolor-icon-themepen + hicolor-icon-theme rapidjson - name: Remove files run: rm -rf /usr/lib64/qt6/lib/cmake/Qt6Sql/{Qt6QMYSQL*,Qt6QODBCD*,Qt6QPSQL*,Qt6QIBase*} @@ -768,7 +768,7 @@ jobs: export LDFLAGS="-L/usr/local/lib" git config --global --add safe.directory ${GITHUB_WORKSPACE} cmake -E make_directory build - cmake -S . -B build -DCMAKE_BUILD_TYPE="Debug" -DENABLE_ALSA=OFF + cmake -S . -B build -DCMAKE_BUILD_TYPE="Debug" -DENABLE_ALSA=OFF -DENABLE_DISCORD_RPC=OFF cmake --build build --config Debug --parallel 4 diff --git a/3rdparty/discord-rpc/CMakeLists.txt b/3rdparty/discord-rpc/CMakeLists.txt new file mode 100644 index 000000000..febd4f0ab --- /dev/null +++ b/3rdparty/discord-rpc/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(src) diff --git a/thirdparty/discord-rpc/LICENSE b/3rdparty/discord-rpc/LICENSE similarity index 100% rename from thirdparty/discord-rpc/LICENSE rename to 3rdparty/discord-rpc/LICENSE diff --git a/thirdparty/discord-rpc/README.md b/3rdparty/discord-rpc/README.md similarity index 100% rename from thirdparty/discord-rpc/README.md rename to 3rdparty/discord-rpc/README.md diff --git a/thirdparty/discord-rpc/include/discord_register.h b/3rdparty/discord-rpc/include/discord_register.h similarity index 100% rename from thirdparty/discord-rpc/include/discord_register.h rename to 3rdparty/discord-rpc/include/discord_register.h diff --git a/thirdparty/discord-rpc/include/discord_rpc.h b/3rdparty/discord-rpc/include/discord_rpc.h similarity index 97% rename from thirdparty/discord-rpc/include/discord_rpc.h rename to 3rdparty/discord-rpc/include/discord_rpc.h index f47709a17..eaadbeff5 100644 --- a/thirdparty/discord-rpc/include/discord_rpc.h +++ b/3rdparty/discord-rpc/include/discord_rpc.h @@ -5,6 +5,8 @@ // clang-format on +namespace discord_rpc { + #ifdef __cplusplus extern "C" { #endif @@ -70,4 +72,6 @@ void Discord_UpdateHandlers(DiscordEventHandlers* handlers); #ifdef __cplusplus } /* extern "C" */ +} // namespace discord_rpc + #endif diff --git a/3rdparty/discord-rpc/src/CMakeLists.txt b/3rdparty/discord-rpc/src/CMakeLists.txt new file mode 100644 index 000000000..b65b3b846 --- /dev/null +++ b/3rdparty/discord-rpc/src/CMakeLists.txt @@ -0,0 +1,41 @@ +set(DISCORD_RPC_SOURCES + ../include/discord_rpc.h + ../include/discord_register.h + discord_rpc.cpp + rpc_connection.h + rpc_connection.cpp + serialization.h + serialization.cpp + connection.h + backoff.h + msg_queue.h +) + +if(UNIX) + list(APPEND DISCORD_RPC_SOURCES connection_unix.cpp) + if(APPLE) + list(APPEND DISCORD_RPC_SOURCES discord_register_osx.m) + add_definitions(-DDISCORD_OSX) + else() + list(APPEND DISCORD_RPC_SOURCES discord_register_linux.cpp) + add_definitions(-DDISCORD_LINUX) + endif() +endif() + +if(WIN32) + list(APPEND DISCORD_RPC_SOURCES connection_win.cpp discord_register_win.cpp) + add_definitions(-DDISCORD_WINDOWS) +endif() + +add_library(discord-rpc STATIC ${DISCORD_RPC_SOURCES}) + +if(APPLE) + target_link_libraries(discord-rpc PRIVATE "-framework AppKit") +endif() + +if(WIN32) + target_link_libraries(discord-rpc PRIVATE psapi advapi32) +endif() + +target_include_directories(discord-rpc SYSTEM PRIVATE ${RapidJSON_INCLUDE_DIRS}) +target_include_directories(discord-rpc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../include) diff --git a/thirdparty/discord-rpc/src/backoff.h b/3rdparty/discord-rpc/src/backoff.h similarity index 73% rename from thirdparty/discord-rpc/src/backoff.h rename to 3rdparty/discord-rpc/src/backoff.h index a3e736fb7..2775b8809 100644 --- a/thirdparty/discord-rpc/src/backoff.h +++ b/3rdparty/discord-rpc/src/backoff.h @@ -2,8 +2,10 @@ #include #include -#include -#include +#include +#include + +namespace discord_rpc { struct Backoff { int64_t minAmount; @@ -20,7 +22,7 @@ struct Backoff { , maxAmount(max) , current(min) , fails(0) - , randGenerator((uint64_t)time(0)) + , randGenerator(static_cast(time(0))) { } @@ -33,8 +35,10 @@ struct Backoff { int64_t nextDelay() { ++fails; - int64_t delay = (int64_t)((double)current * 2.0 * rand01()); + int64_t delay = static_cast(static_cast(current) * 2.0 * rand01()); current = std::min(current + delay, maxAmount); return current; } }; + +} // namespace discord_rpc diff --git a/thirdparty/discord-rpc/src/connection.h b/3rdparty/discord-rpc/src/connection.h similarity index 84% rename from thirdparty/discord-rpc/src/connection.h rename to 3rdparty/discord-rpc/src/connection.h index 30f851a8e..597517bbb 100644 --- a/thirdparty/discord-rpc/src/connection.h +++ b/3rdparty/discord-rpc/src/connection.h @@ -2,8 +2,9 @@ // This is to wrap the platform specific kinds of connect/read/write. -#include -#include +#include + +namespace discord_rpc { // not really connectiony, but need per-platform int GetProcessId(); @@ -17,3 +18,5 @@ struct BaseConnection { bool Write(const void *data, size_t length); bool Read(void *data, size_t length); }; + +} // namespace discord_rpc diff --git a/thirdparty/discord-rpc/src/connection_unix.cpp b/3rdparty/discord-rpc/src/connection_unix.cpp similarity index 86% rename from thirdparty/discord-rpc/src/connection_unix.cpp rename to 3rdparty/discord-rpc/src/connection_unix.cpp index 0c72a6072..f52249716 100644 --- a/thirdparty/discord-rpc/src/connection_unix.cpp +++ b/3rdparty/discord-rpc/src/connection_unix.cpp @@ -1,14 +1,16 @@ #include "connection.h" -#include +#include #include -#include -#include +#include +#include #include #include #include #include +namespace discord_rpc { + int GetProcessId() { return ::getpid(); } @@ -61,7 +63,7 @@ bool BaseConnection::Open() { for (int pipeNum = 0; pipeNum < 10; ++pipeNum) { snprintf( PipeAddr.sun_path, sizeof(PipeAddr.sun_path), "%s/discord-ipc-%d", tempPath, pipeNum); - int err = connect(self->sock, (const sockaddr *)&PipeAddr, sizeof(PipeAddr)); + int err = connect(self->sock, reinterpret_cast(&PipeAddr), sizeof(PipeAddr)); if (err == 0) { self->isOpen = true; return true; @@ -93,7 +95,7 @@ bool BaseConnection::Write(const void *data, size_t length) { if (sentBytes < 0) { Close(); } - return sentBytes == (ssize_t)length; + return sentBytes == static_cast(length); } bool BaseConnection::Read(void *data, size_t length) { @@ -103,7 +105,7 @@ bool BaseConnection::Read(void *data, size_t length) { return false; } - int res = (int)recv(self->sock, data, length, MsgFlags); + long res = recv(self->sock, data, length, MsgFlags); if (res < 0) { if (errno == EAGAIN) { return false; @@ -113,5 +115,8 @@ bool BaseConnection::Read(void *data, size_t length) { else if (res == 0) { Close(); } - return res == (int)length; + return static_cast(res) == length; } + +} // namespace discord_rpc + diff --git a/thirdparty/discord-rpc/src/connection_win.cpp b/3rdparty/discord-rpc/src/connection_win.cpp similarity index 90% rename from thirdparty/discord-rpc/src/connection_win.cpp rename to 3rdparty/discord-rpc/src/connection_win.cpp index ab216479e..a626f100c 100644 --- a/thirdparty/discord-rpc/src/connection_win.cpp +++ b/3rdparty/discord-rpc/src/connection_win.cpp @@ -4,11 +4,13 @@ #define NOMCX #define NOSERVICE #define NOIME -#include +#include #include +namespace discord_rpc { + int GetProcessId() { - return (int)::GetCurrentProcessId(); + return static_cast(::GetCurrentProcessId()); } struct BaseConnectionWin : public BaseConnection { @@ -22,7 +24,7 @@ static BaseConnectionWin Connection; } /*static*/ void BaseConnection::Destroy(BaseConnection *&c) { - auto self = reinterpret_cast(c); + auto self = reinterpret_cast(c); self->Close(); c = nullptr; } @@ -81,7 +83,7 @@ bool BaseConnection::Write(const void *data, size_t length) { if (!data) { return false; } - const DWORD bytesLength = (DWORD)length; + const DWORD bytesLength = static_cast(length); DWORD bytesWritten = 0; return ::WriteFile(self->pipe, data, bytesLength, &bytesWritten, nullptr) == TRUE && bytesWritten == bytesLength; @@ -103,7 +105,7 @@ bool BaseConnection::Read(void *data, size_t length) { DWORD bytesAvailable = 0; if (::PeekNamedPipe(self->pipe, nullptr, 0, nullptr, &bytesAvailable, nullptr)) { if (bytesAvailable >= length) { - DWORD bytesToRead = (DWORD)length; + DWORD bytesToRead = static_cast(length); DWORD bytesRead = 0; if (::ReadFile(self->pipe, data, bytesToRead, &bytesRead, nullptr) == TRUE) { assert(bytesToRead == bytesRead); @@ -119,3 +121,6 @@ bool BaseConnection::Read(void *data, size_t length) { } return false; } + +} // namespace discord_rpc + diff --git a/thirdparty/discord-rpc/src/discord_register_linux.cpp b/3rdparty/discord-rpc/src/discord_register_linux.cpp similarity index 86% rename from thirdparty/discord-rpc/src/discord_register_linux.cpp rename to 3rdparty/discord-rpc/src/discord_register_linux.cpp index 65028221f..5e011a1ee 100644 --- a/thirdparty/discord-rpc/src/discord_register_linux.cpp +++ b/3rdparty/discord-rpc/src/discord_register_linux.cpp @@ -1,14 +1,16 @@ #include "discord_rpc.h" #include "discord_register.h" -#include +#include #include -#include -#include +#include +#include #include #include #include +namespace { + static bool Mkdir(const char *path) { int result = mkdir(path, 0755); if (result == 0) { @@ -20,6 +22,8 @@ static bool Mkdir(const char *path) { return false; } +} // namespace + // we want to register games so we can run them from Discord client as discord-:// extern "C" void Discord_Register(const char *applicationId, const char *command) { // Add a desktop file and update some mime handlers so that xdg-open does the right thing. @@ -32,14 +36,14 @@ extern "C" void Discord_Register(const char *applicationId, const char *command) char exePath[1024]; if (!command || !command[0]) { ssize_t size = readlink("/proc/self/exe", exePath, sizeof(exePath)); - if (size <= 0 || size >= (ssize_t)sizeof(exePath)) { + if (size <= 0 || size >= static_cast(sizeof(exePath))) { return; } exePath[size] = '\0'; command = exePath; } - const char *desktopFileFormat = "[Desktop Entry]\n" + constexpr char desktopFileFormat[] = "[Desktop Entry]\n" "Name=Game %s\n" "Exec=%s %%u\n" // note: it really wants that %u in there "Type=Application\n" @@ -54,10 +58,10 @@ extern "C" void Discord_Register(const char *applicationId, const char *command) } char desktopFilename[256]; - snprintf(desktopFilename, sizeof(desktopFilename), "/discord-%s.desktop", applicationId); + (void)snprintf(desktopFilename, sizeof(desktopFilename), "/discord-%s.desktop", applicationId); char desktopFilePath[1024]; - snprintf(desktopFilePath, sizeof(desktopFilePath), "%s/.local", home); + (void)snprintf(desktopFilePath, sizeof(desktopFilePath), "%s/.local", home); if (!Mkdir(desktopFilePath)) { return; } @@ -97,3 +101,4 @@ extern "C" void Discord_RegisterSteamGame(const char *applicationId, sprintf(command, "xdg-open steam://rungameid/%s", steamId); Discord_Register(applicationId, command); } + diff --git a/thirdparty/discord-rpc/src/discord_register_osx.m b/3rdparty/discord-rpc/src/discord_register_osx.m similarity index 100% rename from thirdparty/discord-rpc/src/discord_register_osx.m rename to 3rdparty/discord-rpc/src/discord_register_osx.m diff --git a/thirdparty/discord-rpc/src/discord_register_win.cpp b/3rdparty/discord-rpc/src/discord_register_win.cpp similarity index 95% rename from thirdparty/discord-rpc/src/discord_register_win.cpp rename to 3rdparty/discord-rpc/src/discord_register_win.cpp index a5f121505..4b33038c8 100644 --- a/thirdparty/discord-rpc/src/discord_register_win.cpp +++ b/3rdparty/discord-rpc/src/discord_register_win.cpp @@ -45,6 +45,7 @@ static HRESULT StringCbPrintfW(LPWSTR pszDest, size_t cbDest, LPCWSTR pszFormat, # undefine RegSetKeyValueW #endif #define RegSetKeyValueW regset + static LSTATUS regset(HKEY hkey, LPCWSTR subkey, LPCWSTR name, @@ -59,7 +60,7 @@ static LSTATUS regset(HKEY hkey, return ret; htkey = hsubkey; } - ret = RegSetValueExW(htkey, name, 0, type, (const BYTE *)data, len); + ret = RegSetValueExW(htkey, name, 0, type, static_cast(data), len); if (hsubkey && hsubkey != hkey) RegCloseKey(hsubkey); return ret; @@ -100,14 +101,14 @@ static void Discord_RegisterW(const wchar_t *applicationId, const wchar_t *comma } DWORD len; LSTATUS result; - len = (DWORD)lstrlenW(protocolDescription) + 1; + len = static_cast(lstrlenW(protocolDescription) + 1); result = RegSetKeyValueW(key, nullptr, nullptr, REG_SZ, protocolDescription, len * sizeof(wchar_t)); if (FAILED(result)) { fprintf(stderr, "Error writing description\n"); } - len = (DWORD)lstrlenW(protocolDescription) + 1; + len = static_cast(lstrlenW(protocolDescription) + 1); result = RegSetKeyValueW(key, nullptr, L"URL Protocol", REG_SZ, &urlProtocol, sizeof(wchar_t)); if (FAILED(result)) { fprintf(stderr, "Error writing description\n"); @@ -119,7 +120,7 @@ static void Discord_RegisterW(const wchar_t *applicationId, const wchar_t *comma fprintf(stderr, "Error writing icon\n"); } - len = (DWORD)lstrlenW(openCommand) + 1; + len = static_cast(lstrlenW(openCommand) + 1); result = RegSetKeyValueW( key, L"shell\\open\\command", nullptr, REG_SZ, openCommand, len * sizeof(wchar_t)); if (FAILED(result)) { @@ -179,3 +180,4 @@ extern "C" void Discord_RegisterSteamGame(const char *applicationId, Discord_RegisterW(appId, command); } + diff --git a/thirdparty/discord-rpc/src/discord_rpc.cpp b/3rdparty/discord-rpc/src/discord_rpc.cpp similarity index 99% rename from thirdparty/discord-rpc/src/discord_rpc.cpp rename to 3rdparty/discord-rpc/src/discord_rpc.cpp index c8cd27eac..a27d64c83 100644 --- a/thirdparty/discord-rpc/src/discord_rpc.cpp +++ b/3rdparty/discord-rpc/src/discord_rpc.cpp @@ -13,6 +13,8 @@ #include #include +namespace discord_rpc { + constexpr size_t MaxMessageSize { 16 * 1024 }; constexpr size_t MessageQueueSize { 8 }; constexpr size_t JoinQueueSize { 8 }; @@ -472,3 +474,6 @@ extern "C" void Discord_UpdateHandlers(DiscordEventHandlers *newHandlers) { } return; } + +} // namespace discord_rpc + diff --git a/thirdparty/discord-rpc/src/msg_queue.h b/3rdparty/discord-rpc/src/msg_queue.h similarity index 89% rename from thirdparty/discord-rpc/src/msg_queue.h rename to 3rdparty/discord-rpc/src/msg_queue.h index 77f380e70..47d9275d5 100644 --- a/thirdparty/discord-rpc/src/msg_queue.h +++ b/3rdparty/discord-rpc/src/msg_queue.h @@ -5,7 +5,9 @@ // A simple queue. No locks, but only works with a single thread as producer and a single thread as // a consumer. Mutex up as needed. -template +namespace discord_rpc { + +template class MsgQueue { ElementType queue_[QueueSize]; std::atomic_uint nextAdd_{0}; @@ -34,3 +36,5 @@ public: } void CommitSend() { --pendingSends_; } }; + +} // namespace discord_rpc diff --git a/thirdparty/discord-rpc/src/rpc_connection.cpp b/3rdparty/discord-rpc/src/rpc_connection.cpp similarity index 88% rename from thirdparty/discord-rpc/src/rpc_connection.cpp rename to 3rdparty/discord-rpc/src/rpc_connection.cpp index 92b767ab6..41203d585 100644 --- a/thirdparty/discord-rpc/src/rpc_connection.cpp +++ b/3rdparty/discord-rpc/src/rpc_connection.cpp @@ -1,7 +1,7 @@ #include "rpc_connection.h" #include "serialization.h" -#include +namespace discord_rpc { static const int RpcVersion = 1; static RpcConnection Instance; @@ -42,8 +42,7 @@ void RpcConnection::Open() { } else { sendFrame.opcode = Opcode::Handshake; - sendFrame.length = (uint32_t)JsonWriteHandshakeObj( - sendFrame.message, sizeof(sendFrame.message), RpcVersion, appId); + sendFrame.length = static_cast(JsonWriteHandshakeObj(sendFrame.message, sizeof(sendFrame.message), RpcVersion, appId)); if (connection->Write(&sendFrame, sizeof(MessageFrameHeader) + sendFrame.length)) { state = State::SentHandshake; @@ -65,7 +64,7 @@ void RpcConnection::Close() { bool RpcConnection::Write(const void *data, size_t length) { sendFrame.opcode = Opcode::Frame; memcpy(sendFrame.message, data, length); - sendFrame.length = (uint32_t)length; + sendFrame.length = static_cast(length); if (!connection->Write(&sendFrame, sizeof(MessageFrameHeader) + length)) { Close(); return false; @@ -82,7 +81,7 @@ bool RpcConnection::Read(JsonDocument &message) { bool didRead = connection->Read(&readFrame, sizeof(MessageFrameHeader)); if (!didRead) { if (!connection->isOpen) { - lastErrorCode = (int)ErrorCode::PipeClosed; + lastErrorCode = static_cast(ErrorCode::PipeClosed); StringCopy(lastErrorMessage, "Pipe closed"); Close(); } @@ -92,7 +91,7 @@ bool RpcConnection::Read(JsonDocument &message) { if (readFrame.length > 0) { didRead = connection->Read(readFrame.message, readFrame.length); if (!didRead) { - lastErrorCode = (int)ErrorCode::ReadCorrupt; + lastErrorCode = static_cast(ErrorCode::ReadCorrupt); StringCopy(lastErrorMessage, "Partial data in frame"); Close(); return false; @@ -122,10 +121,13 @@ bool RpcConnection::Read(JsonDocument &message) { case Opcode::Handshake: default: // something bad happened - lastErrorCode = (int)ErrorCode::ReadCorrupt; + lastErrorCode = static_cast(ErrorCode::ReadCorrupt); StringCopy(lastErrorMessage, "Bad ipc frame"); Close(); return false; } } } + +} // namespace discord_rpc + diff --git a/thirdparty/discord-rpc/src/rpc_connection.h b/3rdparty/discord-rpc/src/rpc_connection.h similarity index 96% rename from thirdparty/discord-rpc/src/rpc_connection.h rename to 3rdparty/discord-rpc/src/rpc_connection.h index fd7d05130..d110e86a1 100644 --- a/thirdparty/discord-rpc/src/rpc_connection.h +++ b/3rdparty/discord-rpc/src/rpc_connection.h @@ -3,6 +3,8 @@ #include "connection.h" #include "serialization.h" +namespace discord_rpc { + // I took this from the buffer size libuv uses for named pipes; I suspect ours would usually be much // smaller. constexpr size_t MaxRpcFrameSize = 64 * 1024; @@ -57,3 +59,6 @@ struct RpcConnection { bool Write(const void *data, size_t length); bool Read(JsonDocument &message); }; + +} // namespace discord_rpc + diff --git a/thirdparty/discord-rpc/src/serialization.cpp b/3rdparty/discord-rpc/src/serialization.cpp similarity index 98% rename from thirdparty/discord-rpc/src/serialization.cpp rename to 3rdparty/discord-rpc/src/serialization.cpp index 1cd3a4645..4ce67dc45 100644 --- a/thirdparty/discord-rpc/src/serialization.cpp +++ b/3rdparty/discord-rpc/src/serialization.cpp @@ -2,6 +2,8 @@ #include "connection.h" #include "discord_rpc.h" +namespace discord_rpc { + template void NumberToString(char *dest, T number) { if (!number) { @@ -18,7 +20,7 @@ void NumberToString(char *dest, T number) { while (number) { auto digit = number % 10; number = number / 10; - temp[place++] = '0' + (char)digit; + temp[place++] = '0' + static_cast(digit); } for (--place; place >= 0; --place) { *dest++ = temp[place]; @@ -242,3 +244,6 @@ size_t JsonWriteJoinReply(char *dest, size_t maxLen, const char *userId, int rep return writer.Size(); } + +} // namespace discord_rpc + diff --git a/thirdparty/discord-rpc/src/serialization.h b/3rdparty/discord-rpc/src/serialization.h similarity index 95% rename from thirdparty/discord-rpc/src/serialization.h rename to 3rdparty/discord-rpc/src/serialization.h index 3b461c54a..9c2c87cb7 100644 --- a/thirdparty/discord-rpc/src/serialization.h +++ b/3rdparty/discord-rpc/src/serialization.h @@ -1,8 +1,6 @@ #pragma once -#include - -#ifndef __MINGW32__ +#ifdef _MSC_VER # pragma warning(push) # pragma warning(disable : 4061) // enum is not explicitly handled by a case label @@ -10,15 +8,17 @@ # pragma warning(disable : 4464) // relative include path contains # pragma warning(disable : 4668) // is not defined as a preprocessor macro # pragma warning(disable : 6313) // Incorrect operator -#endif // __MINGW32__ +#endif -#include "rapidjson/document.h" -#include "rapidjson/stringbuffer.h" -#include "rapidjson/writer.h" +#include +#include +#include -#ifndef __MINGW32__ +#ifdef _MSC_VER # pragma warning(pop) -#endif // __MINGW32__ +#endif + +namespace discord_rpc { // if only there was a standard library function for this template @@ -118,7 +118,7 @@ class DirectStringBuffer { } } void Flush() {} - size_t GetSize() const { return (size_t)(current_ - buffer_); } + size_t GetSize() const { return static_cast(current_ - buffer_); } }; using MallocAllocator = rapidjson::CrtAllocator; @@ -193,3 +193,5 @@ inline const char *GetStrMember(JsonValue *obj, } return notFoundDefault; } + +} // namespace discord_rpc diff --git a/CMakeLists.txt b/CMakeLists.txt index 30f03eb96..9f9a08ed5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -212,6 +212,8 @@ find_package(GTest) pkg_check_modules(LIBSPARSEHASH IMPORTED_TARGET libsparsehash) +find_package(RapidJSON) + set(QT_VERSION_MAJOR 6) set(QT_MIN_VERSION 6.4.0) set(QT_DEFAULT_MAJOR_VERSION ${QT_VERSION_MAJOR}) @@ -364,7 +366,9 @@ optional_component(STREAMTAGREADER ON "Stream tagreader" DEPENDS "sparsehash" LIBSPARSEHASH_FOUND ) -optional_component(DISCORD_RPC ON "Discord Rich Presence") +optional_component(DISCORD_RPC ON "Discord Rich Presence" + DEPENDS "RapidJSON" RapidJSON_FOUND +) if(HAVE_SONGFINGERPRINTING OR HAVE_MUSICBRAINZ) set(HAVE_CHROMAPRINT ON) @@ -1489,8 +1493,8 @@ if(LINUX AND LSB_RELEASE_EXEC AND DPKG_BUILDPACKAGE) endif() if(HAVE_DISCORD_RPC) - add_subdirectory(thirdparty/discord-rpc) - target_include_directories(strawberry_lib PUBLIC thirdparty/discord-rpc/include) + add_subdirectory(3rdparty/discord-rpc) + target_include_directories(strawberry_lib PUBLIC 3rdparty/discord-rpc/include) endif() if(HAVE_TRANSLATIONS) diff --git a/src/discord/richpresence.cpp b/src/discord/richpresence.cpp index e03573255..32dbfcf13 100644 --- a/src/discord/richpresence.cpp +++ b/src/discord/richpresence.cpp @@ -35,6 +35,8 @@ constexpr qint64 kDiscordPresenceUpdateRateLimitMs = 2000; } // namespace +using namespace discord_rpc; + namespace discord { RichPresence::RichPresence(const SharedPtr player, diff --git a/thirdparty/discord-rpc/CMakeLists.txt b/thirdparty/discord-rpc/CMakeLists.txt deleted file mode 100644 index 92612bbc6..000000000 --- a/thirdparty/discord-rpc/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -find_package(RapidJSON REQUIRED) - -add_subdirectory(src) diff --git a/thirdparty/discord-rpc/src/CMakeLists.txt b/thirdparty/discord-rpc/src/CMakeLists.txt deleted file mode 100644 index 33da8f28b..000000000 --- a/thirdparty/discord-rpc/src/CMakeLists.txt +++ /dev/null @@ -1,87 +0,0 @@ -include_directories(${PROJECT_SOURCE_DIR}/include) - -set(CMAKE_CXX_STANDARD 17) - -set(BASE_RPC_SRC - ../include/discord_rpc.h - discord_rpc.cpp - ../include/discord_register.h - rpc_connection.h - rpc_connection.cpp - serialization.h - serialization.cpp - connection.h - backoff.h - msg_queue.h -) - -if(WIN32) - add_definitions(-DDISCORD_WINDOWS) - set(BASE_RPC_SRC ${BASE_RPC_SRC} connection_win.cpp discord_register_win.cpp) - add_library(discord-rpc ${BASE_RPC_SRC}) - if (MSVC) - if(USE_STATIC_CRT) - foreach(CompilerFlag - CMAKE_CXX_FLAGS - CMAKE_CXX_FLAGS_DEBUG - CMAKE_CXX_FLAGS_RELEASE - CMAKE_C_FLAGS - CMAKE_C_FLAGS_DEBUG - CMAKE_C_FLAGS_RELEASE) - string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}") - endforeach() - endif(USE_STATIC_CRT) - target_compile_options(discord-rpc PRIVATE /EHsc - /Wall - /wd4100 # unreferenced formal parameter - /wd4514 # unreferenced inline - /wd4625 # copy constructor deleted - /wd5026 # move constructor deleted - /wd4626 # move assignment operator deleted - /wd4668 # not defined preprocessor macro - /wd4710 # function not inlined - /wd4711 # function was inlined - /wd4820 # structure padding - /wd4946 # reinterpret_cast used between related classes - /wd5027 # move assignment operator was implicitly defined as deleted - ) - endif(MSVC) - target_link_libraries(discord-rpc PRIVATE psapi advapi32) -endif(WIN32) - -if(UNIX) - set(BASE_RPC_SRC ${BASE_RPC_SRC} connection_unix.cpp) - - if (APPLE) - add_definitions(-DDISCORD_OSX) - set(BASE_RPC_SRC ${BASE_RPC_SRC} discord_register_osx.m) - else (APPLE) - add_definitions(-DDISCORD_LINUX) - set(BASE_RPC_SRC ${BASE_RPC_SRC} discord_register_linux.cpp) - endif(APPLE) - - add_library(discord-rpc ${BASE_RPC_SRC}) - target_link_libraries(discord-rpc PUBLIC pthread) - - if (APPLE) - target_link_libraries(discord-rpc PRIVATE "-framework AppKit, -mmacosx-version-min=10.10") - endif (APPLE) - - target_compile_options(discord-rpc PRIVATE - -Wno-unknown-pragmas # pragma push thing doesn't work on clang - -Wno-old-style-cast # it's fine - -Wno-c++98-compat # that was almost 2 decades ago - -Wno-c++98-compat-pedantic - -Wno-missing-noreturn - -Wno-padded # structure padding - -Wno-covered-switch-default - -Wno-exit-time-destructors # not sure about these - -Wno-global-constructors - ) - - if (APPLE) - target_link_libraries(discord-rpc PRIVATE "-framework AppKit") - endif (APPLE) -endif(UNIX) - -target_include_directories(discord-rpc PRIVATE ../include ${RAPIDJSON_INCLUDE_DIRS})