feat: add Flutter USB mirroring and single-instance desktop clients
This commit is contained in:
1 parent
3a7bb071f0
commit
a339465392
30 files changed
+1688
-104
No files matched your search
@@ -8,6 +8,7 @@ project(runner LANGUAGES CXX)
|
||||
# Any new source files that you add to the application should be added here.
|
||||
add_executable(${BINARY_NAME} WIN32
|
||||
"flutter_window.cpp"
|
||||
"scrcpy_host.cpp"
|
||||
"main.cpp"
|
||||
"utils.cpp"
|
||||
"win32_window.cpp"
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef RUNNER_DESKTOP_INSTANCE_H_
|
||||
#define RUNNER_DESKTOP_INSTANCE_H_
|
||||
#include <windows.h>
|
||||
|
||||
// Shared with the Avalonia entry point, across installed/portable copies in
|
||||
// the same interactive Windows session.
|
||||
class DesktopInstance {
|
||||
public:
|
||||
DesktopInstance() {
|
||||
event_ = CreateEventW(nullptr, FALSE, FALSE, L"Local\\TTSTD.ADS.Desktop.Activate");
|
||||
mutex_ = CreateMutexW(nullptr, TRUE, L"Local\\TTSTD.ADS.Desktop.Instance");
|
||||
primary_ = mutex_ && GetLastError() != ERROR_ALREADY_EXISTS;
|
||||
}
|
||||
~DesktopInstance() {
|
||||
if (wait_) UnregisterWaitEx(wait_, INVALID_HANDLE_VALUE);
|
||||
if (primary_) ReleaseMutex(mutex_);
|
||||
if (mutex_) CloseHandle(mutex_);
|
||||
if (event_) CloseHandle(event_);
|
||||
}
|
||||
bool valid() const { return event_ && mutex_; }
|
||||
bool primary() const { return primary_; }
|
||||
void Activate() {
|
||||
AllowSetForegroundWindow(ASFW_ANY);
|
||||
SetEvent(event_);
|
||||
}
|
||||
bool Listen(HWND window) {
|
||||
return RegisterWaitForSingleObject(&wait_, event_, Wake,
|
||||
reinterpret_cast<void*>(window), INFINITE, WT_EXECUTEDEFAULT) != FALSE;
|
||||
}
|
||||
static UINT ActivationMessage() {
|
||||
return RegisterWindowMessageW(L"TTSTD.ADS.Desktop.Activate");
|
||||
}
|
||||
private:
|
||||
static void CALLBACK Wake(void* context, BOOLEAN) {
|
||||
PostMessageW(reinterpret_cast<HWND>(context), ActivationMessage(), 0, 0);
|
||||
}
|
||||
HANDLE event_ = nullptr, mutex_ = nullptr, wait_ = nullptr;
|
||||
bool primary_ = false;
|
||||
};
|
||||
#endif
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "flutter_window.h"
|
||||
#include "desktop_instance.h"
|
||||
|
||||
#include <optional>
|
||||
|
||||
@@ -26,6 +27,7 @@ bool FlutterWindow::OnCreate() {
|
||||
}
|
||||
RegisterPlugins(flutter_controller_->engine());
|
||||
SetChildContent(flutter_controller_->view()->GetNativeWindow());
|
||||
scrcpy_host_ = std::make_unique<ScrcpyHost>(flutter_controller_->engine()->messenger(), flutter_controller_->view()->GetNativeWindow());
|
||||
|
||||
flutter_controller_->engine()->SetNextFrameCallback([&]() {
|
||||
this->Show();
|
||||
@@ -40,6 +42,7 @@ bool FlutterWindow::OnCreate() {
|
||||
}
|
||||
|
||||
void FlutterWindow::OnDestroy() {
|
||||
scrcpy_host_.reset();
|
||||
if (flutter_controller_) {
|
||||
flutter_controller_ = nullptr;
|
||||
}
|
||||
@@ -51,6 +54,11 @@ LRESULT
|
||||
FlutterWindow::MessageHandler(HWND hwnd, UINT const message,
|
||||
WPARAM const wparam,
|
||||
LPARAM const lparam) noexcept {
|
||||
if (message == DesktopInstance::ActivationMessage()) {
|
||||
ShowWindow(hwnd, IsIconic(hwnd) ? SW_RESTORE : SW_SHOW);
|
||||
SetForegroundWindow(hwnd);
|
||||
return 0;
|
||||
}
|
||||
// Give Flutter, including plugins, an opportunity to handle window messages.
|
||||
if (flutter_controller_) {
|
||||
std::optional<LRESULT> result =
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <memory>
|
||||
|
||||
#include "win32_window.h"
|
||||
#include "scrcpy_host.h"
|
||||
|
||||
// A window that does nothing but host a Flutter view.
|
||||
class FlutterWindow : public Win32Window {
|
||||
@@ -23,6 +24,7 @@ class FlutterWindow : public Win32Window {
|
||||
LPARAM const lparam) noexcept override;
|
||||
|
||||
private:
|
||||
std::unique_ptr<ScrcpyHost> scrcpy_host_;
|
||||
// The project to run.
|
||||
flutter::DartProject project_;
|
||||
|
||||
|
||||
@@ -4,9 +4,16 @@
|
||||
|
||||
#include "flutter_window.h"
|
||||
#include "utils.h"
|
||||
#include "desktop_instance.h"
|
||||
|
||||
int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
|
||||
_In_ wchar_t *command_line, _In_ int show_command) {
|
||||
DesktopInstance desktop_instance;
|
||||
if (!desktop_instance.valid()) return EXIT_FAILURE;
|
||||
if (!desktop_instance.primary()) {
|
||||
desktop_instance.Activate();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
// Attach to console when present (e.g., 'flutter run') or create a
|
||||
// new console when running with a debugger.
|
||||
if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
|
||||
@@ -30,6 +37,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
|
||||
if (!window.Create(L"ads_manager", origin, size)) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (!desktop_instance.Listen(window.GetHandle())) return EXIT_FAILURE;
|
||||
window.SetQuitOnClose(true);
|
||||
|
||||
::MSG msg;
|
||||
|
||||
@@ -0,0 +1,170 @@
|
||||
#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();
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef RUNNER_SCRCPY_HOST_H_
|
||||
#define RUNNER_SCRCPY_HOST_H_
|
||||
#include <windows.h>
|
||||
#include <flutter/method_channel.h>
|
||||
#include <flutter/encodable_value.h>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
|
||||
class ScrcpyHost {
|
||||
public:
|
||||
ScrcpyHost(flutter::BinaryMessenger* messenger, HWND parent);
|
||||
~ScrcpyHost();
|
||||
private:
|
||||
struct Entry {
|
||||
HWND window;
|
||||
LONG_PTR style;
|
||||
LONG_PTR extended;
|
||||
RECT rect;
|
||||
};
|
||||
HWND parent_;
|
||||
std::map<DWORD, Entry> windows_;
|
||||
std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> channel_;
|
||||
void Release(DWORD pid);
|
||||
void Handle(const flutter::MethodCall<flutter::EncodableValue>& call,
|
||||
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
|
||||
};
|
||||
#endif
|
||||
Reference in new issue
Block a user