Skip to content

Commit 089f76f

Browse files
committed
clean up some stuff, remove wine wrapper for now
1 parent 1a1a276 commit 089f76f

File tree

8 files changed

+35
-661
lines changed

8 files changed

+35
-661
lines changed

CMakeLists.txt

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,16 @@ target_include_directories(${PROJECT_NAME} PUBLIC include)
1111
# Windows has some extra code
1212
if (WIN32)
1313
target_sources(${PROJECT_NAME} PRIVATE src/platform/windows.cpp)
14-
15-
if (CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
16-
set(TARGET_MASM_FILE "${CMAKE_CURRENT_SOURCE_DIR}/src/platform/wine_masm.asm")
17-
set(TARGET_OBJ_FILE "${CMAKE_CURRENT_BINARY_DIR}/wine_masm.obj")
18-
add_custom_command(
19-
OUTPUT "${TARGET_OBJ_FILE}"
20-
COMMAND ml64 /c /Fo${TARGET_OBJ_FILE} "${TARGET_MASM_FILE}"
21-
DEPENDS "${TARGET_MASM_FILE}"
22-
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
23-
COMMENT "Assembling ${TARGET_MASM_FILE}"
24-
VERBATIM
25-
)
26-
27-
add_custom_target(assemble_wine_masm ALL DEPENDS "${TARGET_OBJ_FILE}")
28-
add_dependencies(${PROJECT_NAME} assemble_wine_masm)
29-
30-
target_link_libraries(${PROJECT_NAME} PRIVATE "${TARGET_OBJ_FILE}")
31-
endif()
3214
endif()
3315

34-
CPMAddPackage("gh:fmtlib/fmt#11.0.2")
35-
CPMAddPackage("gh:stephenberry/glaze@4.4.3")
16+
CPMAddPackage("gh:fmtlib/fmt#11.2.0")
17+
CPMAddPackage("gh:stephenberry/glaze@5.2.0")
3618
target_link_libraries(${PROJECT_NAME} PRIVATE fmt glaze::glaze)
3719

20+
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
21+
set(DISCORD_RPC_BUILD_TESTS ON)
22+
endif()
23+
3824
if (DISCORD_RPC_BUILD_TESTS)
3925
add_subdirectory(test)
4026
endif()

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ A simple Discord Rich Presence library for C++ made from scratch.
1010
- [x] Linux support
1111
- [x] MacOS support
1212
- [x] Windows support
13-
- [x] Out-of-the-box Wine support
13+
- [ ] ~~Out-of-the-box Wine support~~ (postponed until a better solution is found)
1414
- [ ] Events (join, spectate, etc.)
1515
- [ ] Auto register (for steam games)
1616
- [ ] Documentation
@@ -29,7 +29,7 @@ that uses a modern C++ style and has more features.
2929
- **Buttons**: Undocumented/unimplemented feature in discord, that allows you to add custom links to your presence.
3030
- **Project integration**: Say goodbye to copying library files or dealing with RapidJSON errors. This library can be easily integrated into your project without any hassle.
3131
- **Cross-platform**: This library is designed to work on all supported platforms, including Linux, macOS, and Windows.
32-
- **Wine support**: Library provides internal layer to support Wine, no extra configuration needed.
32+
- ~~**Wine support**: Library provides internal layer to support Wine, no extra configuration needed.~~
3333

3434
---
3535

src/platform/windows.cpp

Lines changed: 0 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -1,174 +1,7 @@
11
#include "windows.hpp"
22
#include <fmt/format.h>
33

4-
#ifndef DISCORD_DISABLE_WINE_LAYER
5-
#if defined(_M_IX86) || defined(_X86_) // x86
6-
#include "wine_x86.hpp"
7-
#elif defined(__GNUC__) || defined(__clang__) // x86_64 (GCC/Clang)
8-
#include "wine_gcc.hpp"
9-
#elif defined(_MSC_VER) // x86_64 (MSVC)
10-
extern "C" {
11-
int wine_close(uint32_t fd);
12-
int wine_open(const char* path, int flags, int mode);
13-
ssize_t wine_read(uint32_t fd, void* buffer, size_t count);
14-
ssize_t wine_write(uint32_t fd, const void* buf, size_t count);
15-
int wine_socket(int domain, int type, int protocol);
16-
int wine_connect(int sockfd, const struct sockaddr* addr, socklen_t addrlen);
17-
int wine_fcntl(int fd, int cmd, int arg);
18-
}
19-
#endif
20-
#endif
21-
224
namespace discord::platform {
23-
#ifndef DISCORD_DISABLE_WINE_LAYER
24-
HMODULE getNTDLL() noexcept {
25-
static HMODULE ntdll = ::GetModuleHandleW(L"ntdll.dll");
26-
return ntdll;
27-
}
28-
29-
bool isWine() noexcept {
30-
static auto isWine = []() {
31-
auto ntdll = getNTDLL();
32-
if (!ntdll) {
33-
return false;
34-
}
35-
36-
return ::GetProcAddress(ntdll, "wine_get_version") != nullptr;
37-
}();
38-
return isWine;
39-
}
40-
41-
const char* getenv(const char* name) noexcept {
42-
static char buffer[1024 * 1024]{};
43-
static const char* end = nullptr;
44-
45-
if (!end) {
46-
auto fd = wine_open("/proc/self/environ", 0, 0);
47-
if (fd < 0) {
48-
return nullptr;
49-
}
50-
51-
auto n = wine_read(fd, buffer, sizeof(buffer));
52-
if (n < 0) {
53-
wine_close(fd);
54-
return nullptr;
55-
}
56-
57-
wine_close(fd);
58-
end = buffer + n;
59-
}
60-
61-
auto namelen = ::strlen(name);
62-
63-
for (const char* ptr = buffer; ptr < end;) {
64-
if (!::strncmp(ptr, name, namelen) && ptr[namelen] == '=') {
65-
return ptr + namelen + 1;
66-
}
67-
68-
for (; *ptr && ptr < end; ++ptr);
69-
++ptr;
70-
}
71-
72-
return nullptr;
73-
}
74-
75-
const char* getTempPath() noexcept {
76-
static const char* path = []() {
77-
const char* tmp = getenv("XDG_RUNTIME_DIR");
78-
tmp = tmp ? tmp : getenv("TMPDIR");
79-
tmp = tmp ? tmp : getenv("TMP");
80-
tmp = tmp ? tmp : getenv("TEMP");
81-
return tmp ? tmp : "/tmp";
82-
}();
83-
return path;
84-
}
85-
86-
wine::WineConnector& wine::WineConnector::get() noexcept {
87-
static WineConnector instance;
88-
return instance;
89-
}
90-
91-
bool wine::WineConnector::open() noexcept {
92-
if (isOpen() || m_socket != -1) {
93-
return false;
94-
}
95-
96-
m_socket = wine_socket(AF_UNIX, SOCK_STREAM, 0);
97-
if (m_socket == -1) {
98-
return false;
99-
}
100-
101-
wine_fcntl(m_socket, F_SETFL, O_NONBLOCK);
102-
for (int i = 0; i < 10; ++i) {
103-
fmt::format_to(m_address.sun_path, "{}/discord-ipc-{}", getTempPath(), i);
104-
if (wine_connect(m_socket, reinterpret_cast<sockaddr*>(&m_address), sizeof(m_address)) == 0) {
105-
setOpen(true);
106-
return true;
107-
}
108-
}
109-
110-
m_socket = -1;
111-
return false;
112-
}
113-
114-
bool wine::WineConnector::close() noexcept {
115-
if (!isOpen() || m_socket == -1) {
116-
return false;
117-
}
118-
119-
wine_close(m_socket);
120-
m_socket = -1;
121-
setOpen(false);
122-
return true;
123-
}
124-
125-
bool wine::WineConnector::write(const void* data, size_t length) noexcept {
126-
if (!isOpen() || m_socket == -1) {
127-
return false;
128-
}
129-
130-
auto written = wine_write(m_socket, data, length);
131-
if (written < 0) {
132-
this->close();
133-
return false;
134-
}
135-
136-
return static_cast<size_t>(written) == length;
137-
}
138-
139-
bool wine::WineConnector::read(void* data, size_t length) noexcept {
140-
if (!isOpen() || m_socket == -1) {
141-
return false;
142-
}
143-
144-
auto received = wine_read(m_socket, data, length);
145-
if (received < 0) {
146-
if (received == -EAGAIN || received == -EWOULDBLOCK) {
147-
return false;
148-
}
149-
150-
this->close();
151-
return false;
152-
}
153-
154-
if (received == 0) {
155-
this->close();
156-
return false;
157-
}
158-
159-
return static_cast<size_t>(received) == length;
160-
}
161-
162-
bool wine::WineConnector::isOpen() noexcept {
163-
return PipeConnection::get().isOpen();
164-
}
165-
166-
void wine::WineConnector::setOpen(bool open) noexcept {
167-
PipeConnection::get().m_isOpen = open;
168-
}
169-
170-
#endif
171-
1725
size_t getProcessID() noexcept {
1736
return ::GetCurrentProcessId();
1747
}
@@ -183,12 +16,6 @@ namespace discord::platform {
18316
return false;
18417
}
18518

186-
#ifndef DISCORD_DISABLE_WINE_LAYER
187-
if (isWine()) {
188-
return wine::WineConnector::get().open();
189-
}
190-
#endif
191-
19219
wchar_t pipeName[] = L"\\\\?\\pipe\\discord-ipc-0";
19320
constexpr size_t pipeDigit = sizeof(pipeName) / sizeof(wchar_t) - 2;
19421
while (true) {
@@ -216,12 +43,6 @@ namespace discord::platform {
21643
}
21744

21845
bool PipeConnection::close() noexcept {
219-
#ifndef DISCORD_DISABLE_WINE_LAYER
220-
if (isWine()) {
221-
return wine::WineConnector::get().close();
222-
}
223-
#endif
224-
22546
if (m_pipe != INVALID_HANDLE_VALUE) {
22647
::CloseHandle(m_pipe);
22748
m_pipe = INVALID_HANDLE_VALUE;
@@ -235,12 +56,6 @@ namespace discord::platform {
23556
return true;
23657
}
23758

238-
#ifndef DISCORD_DISABLE_WINE_LAYER
239-
if (isWine()) {
240-
return wine::WineConnector::get().write(data, length);
241-
}
242-
#endif
243-
24459
if (m_pipe == INVALID_HANDLE_VALUE) {
24560
return false;
24661
}
@@ -262,12 +77,6 @@ namespace discord::platform {
26277
return false;
26378
}
26479

265-
#ifndef DISCORD_DISABLE_WINE_LAYER
266-
if (isWine()) {
267-
return wine::WineConnector::get().read(data, length);
268-
}
269-
#endif
270-
27180
if (m_pipe == INVALID_HANDLE_VALUE) {
27281
return false;
27382
}

src/platform/windows.hpp

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,63 +10,11 @@
1010
#include <cstdint>
1111
#include <Windows.h>
1212

13-
// only enable wine layer on x86 and x86_64
14-
#if !defined(_M_IX86) && !defined(_X86_) && !defined(_M_X64) && !defined(_AMD64_)
15-
#define DISCORD_DISABLE_WINE_LAYER 1
16-
#else
17-
18-
struct sockaddr_un {
19-
unsigned short sun_family;
20-
char sun_path[108];
21-
};
22-
23-
#define AF_UNIX 1
24-
#define SOCK_STREAM 1
25-
#define F_SETFL 4
26-
#define O_RDONLY 00000000
27-
#define O_WRONLY 00000001
28-
#define O_CREAT 00000100
29-
#define O_APPEND 00002000
30-
#define O_NONBLOCK 00004000
31-
#define BUFSIZE 2048
32-
33-
using socklen_t = uint32_t;
34-
using ssize_t = intptr_t;
35-
36-
#endif
37-
3813
namespace discord::platform {
39-
// wine compatibility layer
40-
#ifndef DISCORD_DISABLE_WINE_LAYER
41-
namespace wine {
42-
class WineConnector {
43-
WineConnector() noexcept {
44-
m_address.sun_family = AF_UNIX;
45-
}
46-
47-
public:
48-
static WineConnector& get() noexcept;
49-
bool open() noexcept;
50-
bool close() noexcept;
51-
52-
bool write(const void* data, size_t length) noexcept;
53-
bool read(void* data, size_t length) noexcept;
54-
55-
[[nodiscard]] static bool isOpen() noexcept;
56-
static void setOpen(bool open) noexcept;
57-
58-
private:
59-
sockaddr_un m_address{};
60-
int m_socket = -1;
61-
};
62-
}
63-
#endif
64-
6514
size_t getProcessID() noexcept;
6615

6716
class PipeConnection {
6817
PipeConnection() noexcept = default;
69-
friend class wine::WineConnector;
7018

7119
public:
7220
static PipeConnection& get() noexcept;

0 commit comments

Comments
 (0)