Files

171 lines
7.4 KiB
C++

#include "scrcpy_host.h"
#include <flutter/standard_method_codec.h>
#include <cmath>
#include <string>
namespace {
using flutter::EncodableMap;
using flutter::EncodableValue;
int64_t Number(const EncodableMap& map, const char* key) {
const auto it = map.find(EncodableValue(key));
if (it == map.end()) return 0;
if (const auto* n = std::get_if<int32_t>(&it->second)) return *n;
if (const auto* n = std::get_if<int64_t>(&it->second)) return *n;
if (const auto* n = std::get_if<double>(&it->second)) return static_cast<int64_t>(std::lround(*n));
return 0;
}
struct Search { DWORD pid; HWND found = nullptr; };
BOOL CALLBACK FindWindowForProcess(HWND window, LPARAM data) {
auto* search = reinterpret_cast<Search*>(data);
DWORD pid = 0;
GetWindowThreadProcessId(window, &pid);
if (pid != search->pid || !IsWindowVisible(window)) return TRUE;
wchar_t title[256] = {};
GetWindowTextW(window, title, 256);
if (std::wstring(title).find(L"ADS Flutter") != 0) return TRUE;
search->found = window;
return FALSE;
}
}
ScrcpyHost::ScrcpyHost(flutter::BinaryMessenger* messenger, HWND parent)
: parent_(parent) {
SetWindowLongPtr(parent_, GWL_STYLE, GetWindowLongPtr(parent_, GWL_STYLE) | WS_CLIPCHILDREN);
channel_ = std::make_unique<flutter::MethodChannel<EncodableValue>>(
messenger, "ads/scrcpy_host", &flutter::StandardMethodCodec::GetInstance());
channel_->SetMethodCallHandler([this](const auto& call, auto result) {
Handle(call, std::move(result));
});
}
ScrcpyHost::~ScrcpyHost() {
channel_->SetMethodCallHandler(nullptr);
// Dart normally stops owned processes first. Never leave an embedded orphan
// visible if the native host is destroyed unexpectedly.
while (!windows_.empty()) {
const auto pid = windows_.begin()->first;
const auto window = windows_.begin()->second.window;
Release(pid);
if (IsWindow(window)) PostMessage(window, WM_CLOSE, 0, 0);
}
}
void ScrcpyHost::Release(DWORD pid) {
auto it = windows_.find(pid);
if (it == windows_.end()) return;
const auto entry = it->second;
DWORD actual = 0;
GetWindowThreadProcessId(entry.window, &actual);
if (IsWindow(entry.window) && actual == pid) {
ShowWindow(entry.window, SW_HIDE);
SetParent(entry.window, nullptr);
SetWindowLongPtr(entry.window, GWL_STYLE, entry.style);
SetWindowLongPtr(entry.window, GWL_EXSTYLE, entry.extended);
SetWindowPos(entry.window, nullptr, entry.rect.left, entry.rect.top,
entry.rect.right-entry.rect.left, entry.rect.bottom-entry.rect.top,
SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED | SWP_ASYNCWINDOWPOS);
}
windows_.erase(it);
}
void ScrcpyHost::Handle(const flutter::MethodCall<EncodableValue>& call,
std::unique_ptr<flutter::MethodResult<EncodableValue>> result) {
const auto* args = call.arguments() ? std::get_if<EncodableMap>(call.arguments()) : nullptr;
if (!args) { result->Error("arguments", "Expected a map"); return; }
// HWNDs may be recycled after a process exits. Validate ownership before
// every operation, including hiding sessions on pages that are no longer open.
for (auto it = windows_.begin(); it != windows_.end();) {
DWORD actual = 0;
GetWindowThreadProcessId(it->second.window, &actual);
if (!IsWindow(it->second.window) || actual != it->first) it = windows_.erase(it);
else ++it;
}
const auto pid = static_cast<DWORD>(Number(*args, "pid"));
if (call.method_name() == "attach") {
if (!windows_.count(pid)) {
Search search{pid};
EnumWindows(FindWindowForProcess, reinterpret_cast<LPARAM>(&search));
if (!search.found) { result->Success(EncodableValue(false)); return; }
Entry entry{search.found, GetWindowLongPtr(search.found, GWL_STYLE),
GetWindowLongPtr(search.found, GWL_EXSTYLE), {}};
GetWindowRect(search.found, &entry.rect);
ShowWindow(search.found, SW_HIDE);
SetWindowLongPtr(search.found, GWL_STYLE,
(entry.style & ~(WS_POPUP | WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU))
| WS_CHILD | WS_CLIPSIBLINGS);
SetWindowLongPtr(search.found, GWL_EXSTYLE,
(entry.extended & ~WS_EX_APPWINDOW) | WS_EX_TOOLWINDOW);
SetLastError(0);
const auto previous = SetParent(search.found, parent_);
if (!previous && GetLastError() != 0) {
const auto error = GetLastError();
SetWindowLongPtr(search.found, GWL_STYLE, entry.style);
SetWindowLongPtr(search.found, GWL_EXSTYLE, entry.extended);
ShowWindow(search.found, SW_SHOWNOACTIVATE);
result->Error("set_parent", "Windows embedding failed: " + std::to_string(error));
return;
}
windows_.emplace(pid, entry);
}
result->Success(EncodableValue(true)); return;
}
if (call.method_name() == "release") {
Release(pid); result->Success(); return;
}
if (call.method_name() == "hide") {
auto it = windows_.find(pid);
if (it != windows_.end()) ShowWindow(it->second.window, SW_HIDE);
result->Success(); return;
}
if (call.method_name() == "layout") {
auto it = windows_.find(pid);
DWORD actual = 0;
if (it != windows_.end()) GetWindowThreadProcessId(it->second.window, &actual);
if (it == windows_.end() || !IsWindow(it->second.window) || actual != pid) {
windows_.erase(pid); result->Error("window_closed", "scrcpy window closed"); return;
}
RECT client{};
GetClientRect(parent_, &client);
const int x = static_cast<int>(Number(*args, "x"));
const int y = static_cast<int>(Number(*args, "y"));
const int width = static_cast<int>(Number(*args, "width"));
const int height = static_cast<int>(Number(*args, "height"));
// Hide stale geometry while Flutter catches up to a resize/DPI change.
if (x < 0 || y < 0 || width <= 0 || height <= 0 ||
x + width > client.right + 1 || y + height > client.bottom + 1) {
ShowWindow(it->second.window, SW_HIDE); result->Success(); return;
}
for (const auto& pair : windows_) {
if (pair.first != pid) ShowWindow(pair.second.window, SW_HIDE);
}
RECT previous{};
GetWindowRect(it->second.window, &previous);
MapWindowPoints(HWND_DESKTOP, parent_, reinterpret_cast<POINT*>(&previous), 2);
if (previous.left == x && previous.top == y &&
previous.right - previous.left == width &&
previous.bottom - previous.top == height &&
IsWindowVisible(it->second.window)) {
result->Success(); return;
}
SetWindowPos(it->second.window, HWND_TOP, x, y, width, height,
SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_ASYNCWINDOWPOS);
result->Success(); return;
}
if (call.method_name() == "standaloneVisible") {
Search search{pid};
EnumWindows(FindWindowForProcess, reinterpret_cast<LPARAM>(&search));
result->Success(EncodableValue(search.found != nullptr));
return;
}
if (call.method_name() == "state") {
auto it = windows_.find(pid);
RECT rect{};
const bool exists = it != windows_.end() && IsWindow(it->second.window);
if (exists) GetWindowRect(it->second.window, &rect);
result->Success(EncodableValue(EncodableMap{
{EncodableValue("embedded"), EncodableValue(exists && GetParent(it->second.window) == parent_)},
{EncodableValue("visible"), EncodableValue(exists && IsWindowVisible(it->second.window) != FALSE)},
{EncodableValue("width"), EncodableValue(static_cast<int32_t>(rect.right-rect.left))},
{EncodableValue("height"), EncodableValue(static_cast<int32_t>(rect.bottom-rect.top))}
}));
return;
}
result->NotImplemented();
}