discord: Add namespace and cleanup CMakeLists
This commit is contained in:
4
.github/workflows/build.yml
vendored
4
.github/workflows/build.yml
vendored
@@ -302,7 +302,7 @@ jobs:
|
|||||||
desktop-file-utils
|
desktop-file-utils
|
||||||
appstream
|
appstream
|
||||||
appstream-util
|
appstream-util
|
||||||
hicolor-icon-themepen
|
hicolor-icon-theme
|
||||||
rapidjson
|
rapidjson
|
||||||
- name: Remove files
|
- name: Remove files
|
||||||
run: rm -rf /usr/lib64/qt6/lib/cmake/Qt6Sql/{Qt6QMYSQL*,Qt6QODBCD*,Qt6QPSQL*,Qt6QIBase*}
|
run: rm -rf /usr/lib64/qt6/lib/cmake/Qt6Sql/{Qt6QMYSQL*,Qt6QODBCD*,Qt6QPSQL*,Qt6QIBase*}
|
||||||
@@ -768,7 +768,7 @@ jobs:
|
|||||||
export LDFLAGS="-L/usr/local/lib"
|
export LDFLAGS="-L/usr/local/lib"
|
||||||
git config --global --add safe.directory ${GITHUB_WORKSPACE}
|
git config --global --add safe.directory ${GITHUB_WORKSPACE}
|
||||||
cmake -E make_directory build
|
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
|
cmake --build build --config Debug --parallel 4
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
1
3rdparty/discord-rpc/CMakeLists.txt
vendored
Normal file
1
3rdparty/discord-rpc/CMakeLists.txt
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
add_subdirectory(src)
|
||||||
@@ -5,6 +5,8 @@
|
|||||||
|
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
|
namespace discord_rpc {
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
@@ -70,4 +72,6 @@ void Discord_UpdateHandlers(DiscordEventHandlers* handlers);
|
|||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
} /* extern "C" */
|
} /* extern "C" */
|
||||||
|
} // namespace discord_rpc
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
41
3rdparty/discord-rpc/src/CMakeLists.txt
vendored
Normal file
41
3rdparty/discord-rpc/src/CMakeLists.txt
vendored
Normal file
@@ -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)
|
||||||
@@ -2,8 +2,10 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <stdint.h>
|
#include <cstdint>
|
||||||
#include <time.h>
|
#include <ctime>
|
||||||
|
|
||||||
|
namespace discord_rpc {
|
||||||
|
|
||||||
struct Backoff {
|
struct Backoff {
|
||||||
int64_t minAmount;
|
int64_t minAmount;
|
||||||
@@ -20,7 +22,7 @@ struct Backoff {
|
|||||||
, maxAmount(max)
|
, maxAmount(max)
|
||||||
, current(min)
|
, current(min)
|
||||||
, fails(0)
|
, fails(0)
|
||||||
, randGenerator((uint64_t)time(0))
|
, randGenerator(static_cast<uint64_t>(time(0)))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,8 +35,10 @@ struct Backoff {
|
|||||||
int64_t nextDelay()
|
int64_t nextDelay()
|
||||||
{
|
{
|
||||||
++fails;
|
++fails;
|
||||||
int64_t delay = (int64_t)((double)current * 2.0 * rand01());
|
int64_t delay = static_cast<int64_t>(static_cast<double>(current) * 2.0 * rand01());
|
||||||
current = std::min(current + delay, maxAmount);
|
current = std::min(current + delay, maxAmount);
|
||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
} // namespace discord_rpc
|
||||||
@@ -2,8 +2,9 @@
|
|||||||
|
|
||||||
// This is to wrap the platform specific kinds of connect/read/write.
|
// This is to wrap the platform specific kinds of connect/read/write.
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <cstdlib>
|
||||||
#include <stdlib.h>
|
|
||||||
|
namespace discord_rpc {
|
||||||
|
|
||||||
// not really connectiony, but need per-platform
|
// not really connectiony, but need per-platform
|
||||||
int GetProcessId();
|
int GetProcessId();
|
||||||
@@ -17,3 +18,5 @@ struct BaseConnection {
|
|||||||
bool Write(const void *data, size_t length);
|
bool Write(const void *data, size_t length);
|
||||||
bool Read(void *data, size_t length);
|
bool Read(void *data, size_t length);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
} // namespace discord_rpc
|
||||||
@@ -1,14 +1,16 @@
|
|||||||
#include "connection.h"
|
#include "connection.h"
|
||||||
|
|
||||||
#include <errno.h>
|
#include <cerrno>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <cstdio>
|
||||||
#include <string.h>
|
#include <cstring>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
namespace discord_rpc {
|
||||||
|
|
||||||
int GetProcessId() {
|
int GetProcessId() {
|
||||||
return ::getpid();
|
return ::getpid();
|
||||||
}
|
}
|
||||||
@@ -61,7 +63,7 @@ bool BaseConnection::Open() {
|
|||||||
for (int pipeNum = 0; pipeNum < 10; ++pipeNum) {
|
for (int pipeNum = 0; pipeNum < 10; ++pipeNum) {
|
||||||
snprintf(
|
snprintf(
|
||||||
PipeAddr.sun_path, sizeof(PipeAddr.sun_path), "%s/discord-ipc-%d", tempPath, pipeNum);
|
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<const sockaddr*>(&PipeAddr), sizeof(PipeAddr));
|
||||||
if (err == 0) {
|
if (err == 0) {
|
||||||
self->isOpen = true;
|
self->isOpen = true;
|
||||||
return true;
|
return true;
|
||||||
@@ -93,7 +95,7 @@ bool BaseConnection::Write(const void *data, size_t length) {
|
|||||||
if (sentBytes < 0) {
|
if (sentBytes < 0) {
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
return sentBytes == (ssize_t)length;
|
return sentBytes == static_cast<ssize_t>(length);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BaseConnection::Read(void *data, size_t length) {
|
bool BaseConnection::Read(void *data, size_t length) {
|
||||||
@@ -103,7 +105,7 @@ bool BaseConnection::Read(void *data, size_t length) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int res = (int)recv(self->sock, data, length, MsgFlags);
|
long res = recv(self->sock, data, length, MsgFlags);
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
if (errno == EAGAIN) {
|
if (errno == EAGAIN) {
|
||||||
return false;
|
return false;
|
||||||
@@ -113,5 +115,8 @@ bool BaseConnection::Read(void *data, size_t length) {
|
|||||||
else if (res == 0) {
|
else if (res == 0) {
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
return res == (int)length;
|
return static_cast<size_t>(res) == length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace discord_rpc
|
||||||
|
|
||||||
@@ -4,11 +4,13 @@
|
|||||||
#define NOMCX
|
#define NOMCX
|
||||||
#define NOSERVICE
|
#define NOSERVICE
|
||||||
#define NOIME
|
#define NOIME
|
||||||
#include <assert.h>
|
#include <cassert>
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|
||||||
|
namespace discord_rpc {
|
||||||
|
|
||||||
int GetProcessId() {
|
int GetProcessId() {
|
||||||
return (int)::GetCurrentProcessId();
|
return static_cast<int>(::GetCurrentProcessId());
|
||||||
}
|
}
|
||||||
|
|
||||||
struct BaseConnectionWin : public BaseConnection {
|
struct BaseConnectionWin : public BaseConnection {
|
||||||
@@ -22,7 +24,7 @@ static BaseConnectionWin Connection;
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*static*/ void BaseConnection::Destroy(BaseConnection *&c) {
|
/*static*/ void BaseConnection::Destroy(BaseConnection *&c) {
|
||||||
auto self = reinterpret_cast<BaseConnectionWin *>(c);
|
auto self = reinterpret_cast<BaseConnectionWin*>(c);
|
||||||
self->Close();
|
self->Close();
|
||||||
c = nullptr;
|
c = nullptr;
|
||||||
}
|
}
|
||||||
@@ -81,7 +83,7 @@ bool BaseConnection::Write(const void *data, size_t length) {
|
|||||||
if (!data) {
|
if (!data) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const DWORD bytesLength = (DWORD)length;
|
const DWORD bytesLength = static_cast<DWORD>(length);
|
||||||
DWORD bytesWritten = 0;
|
DWORD bytesWritten = 0;
|
||||||
return ::WriteFile(self->pipe, data, bytesLength, &bytesWritten, nullptr) == TRUE &&
|
return ::WriteFile(self->pipe, data, bytesLength, &bytesWritten, nullptr) == TRUE &&
|
||||||
bytesWritten == bytesLength;
|
bytesWritten == bytesLength;
|
||||||
@@ -103,7 +105,7 @@ bool BaseConnection::Read(void *data, size_t length) {
|
|||||||
DWORD bytesAvailable = 0;
|
DWORD bytesAvailable = 0;
|
||||||
if (::PeekNamedPipe(self->pipe, nullptr, 0, nullptr, &bytesAvailable, nullptr)) {
|
if (::PeekNamedPipe(self->pipe, nullptr, 0, nullptr, &bytesAvailable, nullptr)) {
|
||||||
if (bytesAvailable >= length) {
|
if (bytesAvailable >= length) {
|
||||||
DWORD bytesToRead = (DWORD)length;
|
DWORD bytesToRead = static_cast<DWORD>(length);
|
||||||
DWORD bytesRead = 0;
|
DWORD bytesRead = 0;
|
||||||
if (::ReadFile(self->pipe, data, bytesToRead, &bytesRead, nullptr) == TRUE) {
|
if (::ReadFile(self->pipe, data, bytesToRead, &bytesRead, nullptr) == TRUE) {
|
||||||
assert(bytesToRead == bytesRead);
|
assert(bytesToRead == bytesRead);
|
||||||
@@ -119,3 +121,6 @@ bool BaseConnection::Read(void *data, size_t length) {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace discord_rpc
|
||||||
|
|
||||||
@@ -1,14 +1,16 @@
|
|||||||
#include "discord_rpc.h"
|
#include "discord_rpc.h"
|
||||||
#include "discord_register.h"
|
#include "discord_register.h"
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdlib.h>
|
#include <cstdlib>
|
||||||
#include <string.h>
|
#include <cstring>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
static bool Mkdir(const char *path) {
|
static bool Mkdir(const char *path) {
|
||||||
int result = mkdir(path, 0755);
|
int result = mkdir(path, 0755);
|
||||||
if (result == 0) {
|
if (result == 0) {
|
||||||
@@ -20,6 +22,8 @@ static bool Mkdir(const char *path) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
// we want to register games so we can run them from Discord client as discord-<appid>://
|
// we want to register games so we can run them from Discord client as discord-<appid>://
|
||||||
extern "C" void Discord_Register(const char *applicationId, const char *command) {
|
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.
|
// 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];
|
char exePath[1024];
|
||||||
if (!command || !command[0]) {
|
if (!command || !command[0]) {
|
||||||
ssize_t size = readlink("/proc/self/exe", exePath, sizeof(exePath));
|
ssize_t size = readlink("/proc/self/exe", exePath, sizeof(exePath));
|
||||||
if (size <= 0 || size >= (ssize_t)sizeof(exePath)) {
|
if (size <= 0 || size >= static_cast<ssize_t>(sizeof(exePath))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
exePath[size] = '\0';
|
exePath[size] = '\0';
|
||||||
command = exePath;
|
command = exePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *desktopFileFormat = "[Desktop Entry]\n"
|
constexpr char desktopFileFormat[] = "[Desktop Entry]\n"
|
||||||
"Name=Game %s\n"
|
"Name=Game %s\n"
|
||||||
"Exec=%s %%u\n" // note: it really wants that %u in there
|
"Exec=%s %%u\n" // note: it really wants that %u in there
|
||||||
"Type=Application\n"
|
"Type=Application\n"
|
||||||
@@ -54,10 +58,10 @@ extern "C" void Discord_Register(const char *applicationId, const char *command)
|
|||||||
}
|
}
|
||||||
|
|
||||||
char desktopFilename[256];
|
char desktopFilename[256];
|
||||||
snprintf(desktopFilename, sizeof(desktopFilename), "/discord-%s.desktop", applicationId);
|
(void)snprintf(desktopFilename, sizeof(desktopFilename), "/discord-%s.desktop", applicationId);
|
||||||
|
|
||||||
char desktopFilePath[1024];
|
char desktopFilePath[1024];
|
||||||
snprintf(desktopFilePath, sizeof(desktopFilePath), "%s/.local", home);
|
(void)snprintf(desktopFilePath, sizeof(desktopFilePath), "%s/.local", home);
|
||||||
if (!Mkdir(desktopFilePath)) {
|
if (!Mkdir(desktopFilePath)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -97,3 +101,4 @@ extern "C" void Discord_RegisterSteamGame(const char *applicationId,
|
|||||||
sprintf(command, "xdg-open steam://rungameid/%s", steamId);
|
sprintf(command, "xdg-open steam://rungameid/%s", steamId);
|
||||||
Discord_Register(applicationId, command);
|
Discord_Register(applicationId, command);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,6 +45,7 @@ static HRESULT StringCbPrintfW(LPWSTR pszDest, size_t cbDest, LPCWSTR pszFormat,
|
|||||||
# undefine RegSetKeyValueW
|
# undefine RegSetKeyValueW
|
||||||
#endif
|
#endif
|
||||||
#define RegSetKeyValueW regset
|
#define RegSetKeyValueW regset
|
||||||
|
|
||||||
static LSTATUS regset(HKEY hkey,
|
static LSTATUS regset(HKEY hkey,
|
||||||
LPCWSTR subkey,
|
LPCWSTR subkey,
|
||||||
LPCWSTR name,
|
LPCWSTR name,
|
||||||
@@ -59,7 +60,7 @@ static LSTATUS regset(HKEY hkey,
|
|||||||
return ret;
|
return ret;
|
||||||
htkey = hsubkey;
|
htkey = hsubkey;
|
||||||
}
|
}
|
||||||
ret = RegSetValueExW(htkey, name, 0, type, (const BYTE *)data, len);
|
ret = RegSetValueExW(htkey, name, 0, type, static_cast<const BYTE*>(data), len);
|
||||||
if (hsubkey && hsubkey != hkey)
|
if (hsubkey && hsubkey != hkey)
|
||||||
RegCloseKey(hsubkey);
|
RegCloseKey(hsubkey);
|
||||||
return ret;
|
return ret;
|
||||||
@@ -100,14 +101,14 @@ static void Discord_RegisterW(const wchar_t *applicationId, const wchar_t *comma
|
|||||||
}
|
}
|
||||||
DWORD len;
|
DWORD len;
|
||||||
LSTATUS result;
|
LSTATUS result;
|
||||||
len = (DWORD)lstrlenW(protocolDescription) + 1;
|
len = static_cast<DWORD>(lstrlenW(protocolDescription) + 1);
|
||||||
result =
|
result =
|
||||||
RegSetKeyValueW(key, nullptr, nullptr, REG_SZ, protocolDescription, len * sizeof(wchar_t));
|
RegSetKeyValueW(key, nullptr, nullptr, REG_SZ, protocolDescription, len * sizeof(wchar_t));
|
||||||
if (FAILED(result)) {
|
if (FAILED(result)) {
|
||||||
fprintf(stderr, "Error writing description\n");
|
fprintf(stderr, "Error writing description\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
len = (DWORD)lstrlenW(protocolDescription) + 1;
|
len = static_cast<DWORD>(lstrlenW(protocolDescription) + 1);
|
||||||
result = RegSetKeyValueW(key, nullptr, L"URL Protocol", REG_SZ, &urlProtocol, sizeof(wchar_t));
|
result = RegSetKeyValueW(key, nullptr, L"URL Protocol", REG_SZ, &urlProtocol, sizeof(wchar_t));
|
||||||
if (FAILED(result)) {
|
if (FAILED(result)) {
|
||||||
fprintf(stderr, "Error writing description\n");
|
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");
|
fprintf(stderr, "Error writing icon\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
len = (DWORD)lstrlenW(openCommand) + 1;
|
len = static_cast<DWORD>(lstrlenW(openCommand) + 1);
|
||||||
result = RegSetKeyValueW(
|
result = RegSetKeyValueW(
|
||||||
key, L"shell\\open\\command", nullptr, REG_SZ, openCommand, len * sizeof(wchar_t));
|
key, L"shell\\open\\command", nullptr, REG_SZ, openCommand, len * sizeof(wchar_t));
|
||||||
if (FAILED(result)) {
|
if (FAILED(result)) {
|
||||||
@@ -179,3 +180,4 @@ extern "C" void Discord_RegisterSteamGame(const char *applicationId,
|
|||||||
|
|
||||||
Discord_RegisterW(appId, command);
|
Discord_RegisterW(appId, command);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -13,6 +13,8 @@
|
|||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
|
namespace discord_rpc {
|
||||||
|
|
||||||
constexpr size_t MaxMessageSize { 16 * 1024 };
|
constexpr size_t MaxMessageSize { 16 * 1024 };
|
||||||
constexpr size_t MessageQueueSize { 8 };
|
constexpr size_t MessageQueueSize { 8 };
|
||||||
constexpr size_t JoinQueueSize { 8 };
|
constexpr size_t JoinQueueSize { 8 };
|
||||||
@@ -472,3 +474,6 @@ extern "C" void Discord_UpdateHandlers(DiscordEventHandlers *newHandlers) {
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace discord_rpc
|
||||||
|
|
||||||
@@ -5,7 +5,9 @@
|
|||||||
// A simple queue. No locks, but only works with a single thread as producer and a single thread as
|
// 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.
|
// a consumer. Mutex up as needed.
|
||||||
|
|
||||||
template <typename ElementType, size_t QueueSize>
|
namespace discord_rpc {
|
||||||
|
|
||||||
|
template <typename ElementType, std::size_t QueueSize>
|
||||||
class MsgQueue {
|
class MsgQueue {
|
||||||
ElementType queue_[QueueSize];
|
ElementType queue_[QueueSize];
|
||||||
std::atomic_uint nextAdd_{0};
|
std::atomic_uint nextAdd_{0};
|
||||||
@@ -34,3 +36,5 @@ public:
|
|||||||
}
|
}
|
||||||
void CommitSend() { --pendingSends_; }
|
void CommitSend() { --pendingSends_; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
} // namespace discord_rpc
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#include "rpc_connection.h"
|
#include "rpc_connection.h"
|
||||||
#include "serialization.h"
|
#include "serialization.h"
|
||||||
|
|
||||||
#include <atomic>
|
namespace discord_rpc {
|
||||||
|
|
||||||
static const int RpcVersion = 1;
|
static const int RpcVersion = 1;
|
||||||
static RpcConnection Instance;
|
static RpcConnection Instance;
|
||||||
@@ -42,8 +42,7 @@ void RpcConnection::Open() {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
sendFrame.opcode = Opcode::Handshake;
|
sendFrame.opcode = Opcode::Handshake;
|
||||||
sendFrame.length = (uint32_t)JsonWriteHandshakeObj(
|
sendFrame.length = static_cast<uint32_t>(JsonWriteHandshakeObj(sendFrame.message, sizeof(sendFrame.message), RpcVersion, appId));
|
||||||
sendFrame.message, sizeof(sendFrame.message), RpcVersion, appId);
|
|
||||||
|
|
||||||
if (connection->Write(&sendFrame, sizeof(MessageFrameHeader) + sendFrame.length)) {
|
if (connection->Write(&sendFrame, sizeof(MessageFrameHeader) + sendFrame.length)) {
|
||||||
state = State::SentHandshake;
|
state = State::SentHandshake;
|
||||||
@@ -65,7 +64,7 @@ void RpcConnection::Close() {
|
|||||||
bool RpcConnection::Write(const void *data, size_t length) {
|
bool RpcConnection::Write(const void *data, size_t length) {
|
||||||
sendFrame.opcode = Opcode::Frame;
|
sendFrame.opcode = Opcode::Frame;
|
||||||
memcpy(sendFrame.message, data, length);
|
memcpy(sendFrame.message, data, length);
|
||||||
sendFrame.length = (uint32_t)length;
|
sendFrame.length = static_cast<uint32_t>(length);
|
||||||
if (!connection->Write(&sendFrame, sizeof(MessageFrameHeader) + length)) {
|
if (!connection->Write(&sendFrame, sizeof(MessageFrameHeader) + length)) {
|
||||||
Close();
|
Close();
|
||||||
return false;
|
return false;
|
||||||
@@ -82,7 +81,7 @@ bool RpcConnection::Read(JsonDocument &message) {
|
|||||||
bool didRead = connection->Read(&readFrame, sizeof(MessageFrameHeader));
|
bool didRead = connection->Read(&readFrame, sizeof(MessageFrameHeader));
|
||||||
if (!didRead) {
|
if (!didRead) {
|
||||||
if (!connection->isOpen) {
|
if (!connection->isOpen) {
|
||||||
lastErrorCode = (int)ErrorCode::PipeClosed;
|
lastErrorCode = static_cast<int>(ErrorCode::PipeClosed);
|
||||||
StringCopy(lastErrorMessage, "Pipe closed");
|
StringCopy(lastErrorMessage, "Pipe closed");
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
@@ -92,7 +91,7 @@ bool RpcConnection::Read(JsonDocument &message) {
|
|||||||
if (readFrame.length > 0) {
|
if (readFrame.length > 0) {
|
||||||
didRead = connection->Read(readFrame.message, readFrame.length);
|
didRead = connection->Read(readFrame.message, readFrame.length);
|
||||||
if (!didRead) {
|
if (!didRead) {
|
||||||
lastErrorCode = (int)ErrorCode::ReadCorrupt;
|
lastErrorCode = static_cast<int>(ErrorCode::ReadCorrupt);
|
||||||
StringCopy(lastErrorMessage, "Partial data in frame");
|
StringCopy(lastErrorMessage, "Partial data in frame");
|
||||||
Close();
|
Close();
|
||||||
return false;
|
return false;
|
||||||
@@ -122,10 +121,13 @@ bool RpcConnection::Read(JsonDocument &message) {
|
|||||||
case Opcode::Handshake:
|
case Opcode::Handshake:
|
||||||
default:
|
default:
|
||||||
// something bad happened
|
// something bad happened
|
||||||
lastErrorCode = (int)ErrorCode::ReadCorrupt;
|
lastErrorCode = static_cast<int>(ErrorCode::ReadCorrupt);
|
||||||
StringCopy(lastErrorMessage, "Bad ipc frame");
|
StringCopy(lastErrorMessage, "Bad ipc frame");
|
||||||
Close();
|
Close();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace discord_rpc
|
||||||
|
|
||||||
@@ -3,6 +3,8 @@
|
|||||||
#include "connection.h"
|
#include "connection.h"
|
||||||
#include "serialization.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
|
// I took this from the buffer size libuv uses for named pipes; I suspect ours would usually be much
|
||||||
// smaller.
|
// smaller.
|
||||||
constexpr size_t MaxRpcFrameSize = 64 * 1024;
|
constexpr size_t MaxRpcFrameSize = 64 * 1024;
|
||||||
@@ -57,3 +59,6 @@ struct RpcConnection {
|
|||||||
bool Write(const void *data, size_t length);
|
bool Write(const void *data, size_t length);
|
||||||
bool Read(JsonDocument &message);
|
bool Read(JsonDocument &message);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
} // namespace discord_rpc
|
||||||
|
|
||||||
@@ -2,6 +2,8 @@
|
|||||||
#include "connection.h"
|
#include "connection.h"
|
||||||
#include "discord_rpc.h"
|
#include "discord_rpc.h"
|
||||||
|
|
||||||
|
namespace discord_rpc {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void NumberToString(char *dest, T number) {
|
void NumberToString(char *dest, T number) {
|
||||||
if (!number) {
|
if (!number) {
|
||||||
@@ -18,7 +20,7 @@ void NumberToString(char *dest, T number) {
|
|||||||
while (number) {
|
while (number) {
|
||||||
auto digit = number % 10;
|
auto digit = number % 10;
|
||||||
number = number / 10;
|
number = number / 10;
|
||||||
temp[place++] = '0' + (char)digit;
|
temp[place++] = '0' + static_cast<char>(digit);
|
||||||
}
|
}
|
||||||
for (--place; place >= 0; --place) {
|
for (--place; place >= 0; --place) {
|
||||||
*dest++ = temp[place];
|
*dest++ = temp[place];
|
||||||
@@ -242,3 +244,6 @@ size_t JsonWriteJoinReply(char *dest, size_t maxLen, const char *userId, int rep
|
|||||||
|
|
||||||
return writer.Size();
|
return writer.Size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace discord_rpc
|
||||||
|
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdint.h>
|
#ifdef _MSC_VER
|
||||||
|
|
||||||
#ifndef __MINGW32__
|
|
||||||
# pragma warning(push)
|
# pragma warning(push)
|
||||||
|
|
||||||
# pragma warning(disable : 4061) // enum is not explicitly handled by a case label
|
# 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 : 4464) // relative include path contains
|
||||||
# pragma warning(disable : 4668) // is not defined as a preprocessor macro
|
# pragma warning(disable : 4668) // is not defined as a preprocessor macro
|
||||||
# pragma warning(disable : 6313) // Incorrect operator
|
# pragma warning(disable : 6313) // Incorrect operator
|
||||||
#endif // __MINGW32__
|
#endif
|
||||||
|
|
||||||
#include "rapidjson/document.h"
|
#include <rapidjson/document.h>
|
||||||
#include "rapidjson/stringbuffer.h"
|
#include <rapidjson/stringbuffer.h>
|
||||||
#include "rapidjson/writer.h"
|
#include <rapidjson/writer.h>
|
||||||
|
|
||||||
#ifndef __MINGW32__
|
#ifdef _MSC_VER
|
||||||
# pragma warning(pop)
|
# pragma warning(pop)
|
||||||
#endif // __MINGW32__
|
#endif
|
||||||
|
|
||||||
|
namespace discord_rpc {
|
||||||
|
|
||||||
// if only there was a standard library function for this
|
// if only there was a standard library function for this
|
||||||
template<size_t Len>
|
template<size_t Len>
|
||||||
@@ -118,7 +118,7 @@ class DirectStringBuffer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
void Flush() {}
|
void Flush() {}
|
||||||
size_t GetSize() const { return (size_t)(current_ - buffer_); }
|
size_t GetSize() const { return static_cast<size_t>(current_ - buffer_); }
|
||||||
};
|
};
|
||||||
|
|
||||||
using MallocAllocator = rapidjson::CrtAllocator;
|
using MallocAllocator = rapidjson::CrtAllocator;
|
||||||
@@ -193,3 +193,5 @@ inline const char *GetStrMember(JsonValue *obj,
|
|||||||
}
|
}
|
||||||
return notFoundDefault;
|
return notFoundDefault;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace discord_rpc
|
||||||
@@ -212,6 +212,8 @@ find_package(GTest)
|
|||||||
|
|
||||||
pkg_check_modules(LIBSPARSEHASH IMPORTED_TARGET libsparsehash)
|
pkg_check_modules(LIBSPARSEHASH IMPORTED_TARGET libsparsehash)
|
||||||
|
|
||||||
|
find_package(RapidJSON)
|
||||||
|
|
||||||
set(QT_VERSION_MAJOR 6)
|
set(QT_VERSION_MAJOR 6)
|
||||||
set(QT_MIN_VERSION 6.4.0)
|
set(QT_MIN_VERSION 6.4.0)
|
||||||
set(QT_DEFAULT_MAJOR_VERSION ${QT_VERSION_MAJOR})
|
set(QT_DEFAULT_MAJOR_VERSION ${QT_VERSION_MAJOR})
|
||||||
@@ -364,7 +366,9 @@ optional_component(STREAMTAGREADER ON "Stream tagreader"
|
|||||||
DEPENDS "sparsehash" LIBSPARSEHASH_FOUND
|
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)
|
if(HAVE_SONGFINGERPRINTING OR HAVE_MUSICBRAINZ)
|
||||||
set(HAVE_CHROMAPRINT ON)
|
set(HAVE_CHROMAPRINT ON)
|
||||||
@@ -1489,8 +1493,8 @@ if(LINUX AND LSB_RELEASE_EXEC AND DPKG_BUILDPACKAGE)
|
|||||||
endif()
|
endif()
|
||||||
|
|
||||||
if(HAVE_DISCORD_RPC)
|
if(HAVE_DISCORD_RPC)
|
||||||
add_subdirectory(thirdparty/discord-rpc)
|
add_subdirectory(3rdparty/discord-rpc)
|
||||||
target_include_directories(strawberry_lib PUBLIC thirdparty/discord-rpc/include)
|
target_include_directories(strawberry_lib PUBLIC 3rdparty/discord-rpc/include)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if(HAVE_TRANSLATIONS)
|
if(HAVE_TRANSLATIONS)
|
||||||
|
|||||||
@@ -35,6 +35,8 @@ constexpr qint64 kDiscordPresenceUpdateRateLimitMs = 2000;
|
|||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
using namespace discord_rpc;
|
||||||
|
|
||||||
namespace discord {
|
namespace discord {
|
||||||
|
|
||||||
RichPresence::RichPresence(const SharedPtr<Player> player,
|
RichPresence::RichPresence(const SharedPtr<Player> player,
|
||||||
|
|||||||
3
thirdparty/discord-rpc/CMakeLists.txt
vendored
3
thirdparty/discord-rpc/CMakeLists.txt
vendored
@@ -1,3 +0,0 @@
|
|||||||
find_package(RapidJSON REQUIRED)
|
|
||||||
|
|
||||||
add_subdirectory(src)
|
|
||||||
87
thirdparty/discord-rpc/src/CMakeLists.txt
vendored
87
thirdparty/discord-rpc/src/CMakeLists.txt
vendored
@@ -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})
|
|
||||||
Reference in New Issue
Block a user