1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-11-25 12:13:00 +01:00

Add basic windows pipe support

This commit is contained in:
Elias Steurer 2023-09-01 14:30:34 +02:00
parent c70fa6224a
commit 1650c33a42
3 changed files with 89 additions and 13 deletions

View File

@ -13,6 +13,12 @@
int ScreenPlayGodotWallpaper::sInstanceCount = 0;
int ScreenPlayGodotWallpaper::sLastID = 0;
void ScreenPlayGodotWallpaper::_bind_methods()
{
godot::ClassDB::bind_method(godot::D_METHOD("init"), &ScreenPlayGodotWallpaper::init);
godot::ClassDB::bind_method(godot::D_METHOD("create_named_pipe"), &ScreenPlayGodotWallpaper::create_named_pipe);
}
ScreenPlayGodotWallpaper::ScreenPlayGodotWallpaper()
{
mID = ++sLastID;
@ -32,15 +38,36 @@ ScreenPlayGodotWallpaper::~ScreenPlayGodotWallpaper()
// Somehow this gets called at editor startup
// so just return if not initialized
if (!m_hook)
if (m_hook) {
ShowWindow(m_hook->windowHandle, SW_HIDE);
// Force refresh so that we display the regular
// desktop wallpaper again
ShowWindow(m_hook->windowHandleWorker, SW_HIDE);
ShowWindow(m_hook->windowHandleWorker, SW_SHOW);
}
// Destructor
if (hPipe != INVALID_HANDLE_VALUE) {
CloseHandle(hPipe);
}
}
void ScreenPlayGodotWallpaper::_process(double delta)
{
if (!isPipeActive) {
return;
ShowWindow(m_hook->windowHandle, SW_HIDE);
// Force refresh so that we display the regular
// desktop wallpaper again
ShowWindow(m_hook->windowHandleWorker, SW_HIDE);
ShowWindow(m_hook->windowHandleWorker, SW_SHOW);
}
timesinceLastRead += delta;
// 0.05 seconds = 50ms
if (timesinceLastRead >= 0.05) {
godot::String data = read_from_pipe();
if (!data.is_empty()) {
godot::UtilityFunctions::print("Received data: " + data);
}
timesinceLastRead = 0.0;
}
}
bool ScreenPlayGodotWallpaper::configureWindowGeometry()
@ -111,7 +138,49 @@ bool ScreenPlayGodotWallpaper::init(int activeScreen)
return true;
}
void ScreenPlayGodotWallpaper::_bind_methods()
bool ScreenPlayGodotWallpaper::create_named_pipe(godot::String pipeName)
{
godot::ClassDB::bind_method(godot::D_METHOD("init"), &ScreenPlayGodotWallpaper::init);
godot::String fullPipeName = "\\\\.\\pipe\\" + pipeName;
std::wstring wPipeName = std::wstring(fullPipeName.wide_string());
hPipe = CreateNamedPipeW(
wPipeName.c_str(),
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
1,
1024 * 16,
1024 * 16,
NMPWAIT_USE_DEFAULT_WAIT,
NULL);
if (hPipe == INVALID_HANDLE_VALUE) {
isPipeActive = false;
godot::UtilityFunctions::print("CreateNamedPipe failed, error code: " + godot::String::num_int64(GetLastError()));
return false;
}
if (ConnectNamedPipe(hPipe, NULL) == FALSE) {
CloseHandle(hPipe);
godot::UtilityFunctions::print("ConnectNamedPipe failed, error code: " + godot::String::num_int64(GetLastError()));
isPipeActive = false;
return false;
}
isPipeActive = true;
return true;
}
godot::String ScreenPlayGodotWallpaper::read_from_pipe()
{
char buffer[128];
DWORD bytesRead;
godot::String result;
if (ReadFile(hPipe, buffer, sizeof(buffer) - 1, &bytesRead, NULL)) {
buffer[bytesRead] = '\0';
result = godot::String(buffer);
} else {
godot::UtilityFunctions::print("ReadFile from pipe failed, error code: " + godot::String::num_int64(GetLastError()));
}
return result;
}

View File

@ -5,6 +5,7 @@
#include "godot_cpp/classes/global_constants.hpp"
#include "godot_cpp/classes/viewport.hpp"
#include "godot_cpp/core/binder_common.hpp"
#include "godot_cpp/variant/string.hpp"
#include <memory>
@ -19,6 +20,9 @@ public:
~ScreenPlayGodotWallpaper() override;
bool init(int activeScreen);
bool create_named_pipe(godot::String pipeName);
godot::String read_from_pipe();
void _process(double delta);
protected:
static void _bind_methods();
@ -32,4 +36,7 @@ private:
int mID;
std::unique_ptr<WindowsHook> m_hook;
HANDLE hPipe;
double time_since_last_read = 0.0;
bool isPipeActive = false;
};

View File

@ -5,6 +5,6 @@ compatibility_minimum = 4.1
[libraries]
linux.debug.x86_64 = "res://ScreenPlayGodotWallpaper/lib/Linux-x86_64/libScreenPlayGodotWallpaper-d.so"
macos.debug = "res://ScreenPlayGodotWallpaper/lib/Darwin-Universal/libScreenPlayGodotWallpaper-d.dylib"
windows.debug.x86_64 = "res://ScreenPlayGodotWallpaper/lib/Windows-AMD64/ScreenPlayGodotWallpaper-d.dll"
linux.debug.x86_64 = "lib/Linux-x86_64/libScreenPlayGodotWallpaper-d.so"
macos.debug = "lib/Darwin-Universal/libScreenPlayGodotWallpaper-d.dylib"
windows.debug.x86_64 = "lib/Windows-AMD64/ScreenPlayGodotWallpaper-d.dll"