diff --git a/Assets/uDesktopDuplication/Plugins/x86_64/uDesktopDuplication.dll b/Assets/uDesktopDuplication/Plugins/x86_64/uDesktopDuplication.dll index 0ab42cc..9a80636 100644 Binary files a/Assets/uDesktopDuplication/Plugins/x86_64/uDesktopDuplication.dll and b/Assets/uDesktopDuplication/Plugins/x86_64/uDesktopDuplication.dll differ diff --git a/Assets/uDesktopDuplication/Scripts/Lib.cs b/Assets/uDesktopDuplication/Scripts/Lib.cs index 9ba5b9b..9fe7252 100644 --- a/Assets/uDesktopDuplication/Scripts/Lib.cs +++ b/Assets/uDesktopDuplication/Scripts/Lib.cs @@ -139,17 +139,17 @@ public static class Lib [DllImport("uDesktopDuplication")] public static extern bool IsCursorVisible(int id); [DllImport("uDesktopDuplication")] - public static extern int GetCursorX(int id); + public static extern int GetCursorX(); [DllImport("uDesktopDuplication")] - public static extern int GetCursorY(int id); + public static extern int GetCursorY(); [DllImport("uDesktopDuplication")] - public static extern int GetCursorShapeWidth(int id); + public static extern int GetCursorShapeWidth(); [DllImport("uDesktopDuplication")] - public static extern int GetCursorShapeHeight(int id); + public static extern int GetCursorShapeHeight(); [DllImport("uDesktopDuplication")] - public static extern int GetCursorShapePitch(int id); + public static extern int GetCursorShapePitch(); [DllImport("uDesktopDuplication")] - public static extern CursorShapeType GetCursorShapeType(int id); + public static extern CursorShapeType GetCursorShapeType(); [DllImport("uDesktopDuplication")] public static extern void GetCursorTexture(int id, System.IntPtr ptr); [DllImport("uDesktopDuplication")] diff --git a/Assets/uDesktopDuplication/Scripts/Monitor.cs b/Assets/uDesktopDuplication/Scripts/Monitor.cs index e020417..72faa17 100644 --- a/Assets/uDesktopDuplication/Scripts/Monitor.cs +++ b/Assets/uDesktopDuplication/Scripts/Monitor.cs @@ -171,12 +171,12 @@ public class Monitor public int cursorX { - get { return Lib.GetCursorX(id); } + get { return Lib.GetCursorMonitorId() == id ? Lib.GetCursorX() : -1; } } public int cursorY { - get { return Lib.GetCursorY(id); } + get { return Lib.GetCursorMonitorId() == id ? Lib.GetCursorY() : -1; } } public int systemCursorX @@ -199,17 +199,17 @@ public class Monitor public int cursorShapeWidth { - get { return Lib.GetCursorShapeWidth(id); } + get { return Lib.GetCursorShapeWidth(); } } public int cursorShapeHeight { - get { return Lib.GetCursorShapeHeight(id); } + get { return Lib.GetCursorShapeHeight(); } } public CursorShapeType cursorShapeType { - get { return Lib.GetCursorShapeType(id); } + get { return Lib.GetCursorShapeType(); } } public int moveRectCount diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Cursor.cpp b/Plugins/uDesktopDuplication/uDesktopDuplication/Cursor.cpp index 1a63c22..97cf437 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Cursor.cpp +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Cursor.cpp @@ -8,8 +8,7 @@ using namespace Microsoft::WRL; -Cursor::Cursor(Monitor* monitor) - : monitor_(monitor) +Cursor::Cursor() { } @@ -19,7 +18,7 @@ Cursor::~Cursor() } -void Cursor::UpdateBuffer(const DXGI_OUTDUPL_FRAME_INFO& frameInfo) +void Cursor::UpdateBuffer(Monitor* monitor, const DXGI_OUTDUPL_FRAME_INFO& frameInfo) { if (frameInfo.LastMouseUpdateTime.QuadPart == 0) { @@ -29,18 +28,13 @@ void Cursor::UpdateBuffer(const DXGI_OUTDUPL_FRAME_INFO& frameInfo) isVisible_ = frameInfo.PointerPosition.Visible != 0; if (isVisible_) { - GetMonitorManager()->SetCursorMonitorId(monitor_->GetId()); + GetMonitorManager()->SetCursorMonitorId(monitor->GetId()); } x_ = frameInfo.PointerPosition.Position.x; y_ = frameInfo.PointerPosition.Position.y; timestamp_ = frameInfo.LastMouseUpdateTime; - if (!IsCursorOnParentMonitor()) - { - return; - } - if (frameInfo.PointerShapeBufferSize == 0) { return; @@ -55,7 +49,7 @@ void Cursor::UpdateBuffer(const DXGI_OUTDUPL_FRAME_INFO& frameInfo) // Get mouse pointer information UINT bufferSize; DXGI_OUTDUPL_POINTER_SHAPE_INFO shapeInfo; - const auto hr = monitor_->GetDeskDupl()->GetFramePointerShape( + const auto hr = monitor->GetDeskDupl()->GetFramePointerShape( apiBuffer_.Size(), apiBuffer_.Get(), &bufferSize, @@ -72,15 +66,10 @@ void Cursor::UpdateBuffer(const DXGI_OUTDUPL_FRAME_INFO& frameInfo) } -void Cursor::UpdateTexture() +void Cursor::Draw(Monitor* monitor) { - if (!IsCursorOnParentMonitor()) - { - return; - } - // Check desktop texure - if (monitor_->GetUnityTexture() == nullptr) + if (monitor->GetUnityTexture() == nullptr) { Debug::Error("Cursor::UpdateTexture() => Monitor::GetUnityTexture() is null."); return; @@ -107,15 +96,15 @@ void Cursor::UpdateTexture() // Calculate information to capture desktop image under cursor. D3D11_BOX box; - const auto monitorRot = static_cast(monitor_->GetRotation()); + const auto monitorRot = static_cast(monitor->GetRotation()); auto colMin = 0; auto colMax = cursorImageWidth; auto rowMin = 0; auto rowMax = cursorImageHeight; { - const auto monitorWidth = monitor_->GetWidth(); - const auto monitorHeight = monitor_->GetHeight(); + const auto monitorWidth = monitor->GetWidth(); + const auto monitorHeight = monitor->GetHeight(); const auto isVertical = monitorRot == DXGI_MODE_ROTATION_ROTATE90 || monitorRot == DXGI_MODE_ROTATION_ROTATE270; @@ -230,7 +219,7 @@ void Cursor::UpdateTexture() { ComPtr context; GetDevice()->GetImmediateContext(&context); - context->CopySubresourceRegion(texture.Get(), 0, 0, 0, 0, monitor_->GetUnityTexture(), 0, &box); + context->CopySubresourceRegion(texture.Get(), 0, 0, 0, 0, monitor->GetUnityTexture(), 0, &box); } // Get mapped surface @@ -265,6 +254,7 @@ void Cursor::UpdateTexture() return desktop32[col * desktopPitch + (capturedImageHeight - 1 - row)]; case DXGI_MODE_ROTATION_IDENTITY: case DXGI_MODE_ROTATION_UNSPECIFIED: + default: return desktop32[row * desktopPitch + col]; } }; @@ -389,7 +379,7 @@ void Cursor::UpdateTexture() { ComPtr context; GetDevice()->GetImmediateContext(&context); - context->UpdateSubresource(monitor_->GetUnityTexture(), 0, &box, bgraBuffer_.Get(), GetWidth() * 4, 0); + context->UpdateSubresource(monitor->GetUnityTexture(), 0, &box, bgraBuffer_.Get(), GetWidth() * 4, 0); } if (FAILED(surface->Unmap())) @@ -474,10 +464,4 @@ int Cursor::GetPitch() const int Cursor::GetType() const { return shapeInfo_.Type; -} - - -bool Cursor::IsCursorOnParentMonitor() const -{ - return GetMonitorManager()->GetCursorMonitorId() == monitor_->GetId(); } \ No newline at end of file diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Cursor.h b/Plugins/uDesktopDuplication/uDesktopDuplication/Cursor.h index c35372a..2ab9dbe 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Cursor.h +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Cursor.h @@ -10,10 +10,10 @@ class Monitor; class Cursor { public: - explicit Cursor(Monitor* monitor); + explicit Cursor(); ~Cursor(); - void UpdateBuffer(const DXGI_OUTDUPL_FRAME_INFO& frameInfo); - void UpdateTexture(); + void UpdateBuffer(Monitor* monitor, const DXGI_OUTDUPL_FRAME_INFO& frameInfo); + void Draw(Monitor* monitor); void GetTexture(ID3D11Texture2D* texture); bool IsVisible() const; @@ -25,9 +25,6 @@ public: int GetType() const; private: - bool IsCursorOnParentMonitor() const; - - Monitor* monitor_; bool isVisible_ = false; int x_ = -1; int y_ = -1; diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp b/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp index 6aa18bf..8278cf2 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp @@ -10,7 +10,6 @@ using namespace Microsoft::WRL; Monitor::Monitor(int id) : id_(id) - , cursor_(std::make_unique(this)) { } @@ -197,7 +196,16 @@ void Monitor::Render(UINT timeout) } UpdateMetadata(frameInfo); - UpdateCursor(frameInfo); + + if (frameInfo.PointerPosition.Visible) + { + GetMonitorManager()->SetCursorMonitorId(id_); + } + + if (GetMonitorManager()->GetCursorMonitorId() == id_) + { + UpdateCursor(frameInfo); + } hr = deskDupl_->ReleaseFrame(); if (FAILED(hr)) @@ -229,8 +237,9 @@ void Monitor::Render(UINT timeout) void Monitor::UpdateCursor(const DXGI_OUTDUPL_FRAME_INFO& frameInfo) { - cursor_->UpdateBuffer(frameInfo); - cursor_->UpdateTexture(); + auto cursor_ = GetMonitorManager()->GetCursor(); + cursor_->UpdateBuffer(this, frameInfo); + cursor_->Draw(this); } @@ -360,18 +369,6 @@ IDXGIOutputDuplication* Monitor::GetDeskDupl() } -const std::unique_ptr& Monitor::GetCursor() -{ - return cursor_; -} - - -void Monitor::GetCursorTexture(ID3D11Texture2D* texture) -{ - cursor_->GetTexture(texture); -} - - void Monitor::GetName(char* buf, int len) const { strcpy_s(buf, len, monitorInfo_.szDevice); diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.h b/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.h index e1bc4d0..3c7c01e 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.h +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.h @@ -6,8 +6,6 @@ #include #include "Common.h" -class Cursor; - enum class MonitorState { NotSet = -1, @@ -31,7 +29,6 @@ public: ~Monitor(); void Initialize(IDXGIOutput* output); void Render(UINT timeout = 0); - void GetCursorTexture(ID3D11Texture2D* texture); public: int GetId() const; @@ -50,7 +47,6 @@ public: int GetDpiX() const; int GetDpiY() const; IDXGIOutputDuplication* GetDeskDupl(); - const std::unique_ptr& GetCursor(); int GetMoveRectCount() const; DXGI_OUTDUPL_MOVE_RECT* GetMoveRects() const; int GetDirtyRectCount() const; @@ -66,7 +62,6 @@ private: UINT dpiX_ = -1, dpiY_ = -1; int width_ = -1, height_ = -1; State state_ = State::NotSet; - std::unique_ptr cursor_; IDXGIOutputDuplication* deskDupl_ = nullptr; ID3D11Texture2D* unityTexture_ = nullptr; DXGI_OUTPUT_DESC outputDesc_; diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/MonitorManager.cpp b/Plugins/uDesktopDuplication/uDesktopDuplication/MonitorManager.cpp index 90102bd..f7a12f8 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/MonitorManager.cpp +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/MonitorManager.cpp @@ -112,6 +112,12 @@ std::shared_ptr MonitorManager::GetMonitor(int id) const } +std::shared_ptr MonitorManager::GetCursor() const +{ + return cursor_; +} + + void MonitorManager::Update() { if (isReinitializationRequired_) diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/MonitorManager.h b/Plugins/uDesktopDuplication/uDesktopDuplication/MonitorManager.h index 34692ee..968f2d5 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/MonitorManager.h +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/MonitorManager.h @@ -21,6 +21,7 @@ public: void SetCursorMonitorId(int id) { cursorMonitorId_ = id; } int GetCursorMonitorId() const { return cursorMonitorId_; } std::shared_ptr GetMonitor(int id) const; + std::shared_ptr GetCursor() const; private: void Initialize(); @@ -41,7 +42,7 @@ public: private: int timeout_ = 10; std::vector> monitors_; - std::shared_ptr cursor_; + std::shared_ptr cursor_ = std::make_shared(); int cursorMonitorId_ = -1; bool isReinitializationRequired_ = false; }; \ No newline at end of file diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/main.cpp b/Plugins/uDesktopDuplication/uDesktopDuplication/main.cpp index d039fbb..6b4f667 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/main.cpp +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/main.cpp @@ -295,83 +295,52 @@ extern "C" return -1; } - UNITY_INTERFACE_EXPORT bool UNITY_INTERFACE_API IsCursorVisible(int id) + UNITY_INTERFACE_EXPORT bool UNITY_INTERFACE_API IsCursorVisible() { if (!g_manager) return false; - if (auto monitor = g_manager->GetMonitor(id)) - { - return monitor->GetCursor()->IsVisible(); - } - return false; + return g_manager->GetCursor()->IsVisible(); } - UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorX(int id) + UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorX() { if (!g_manager) return -1; - if (auto monitor = g_manager->GetMonitor(id)) - { - return monitor->GetCursor()->GetX(); - } - return -1; + return g_manager->GetCursor()->GetX(); } - UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorY(int id) + UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorY() { if (!g_manager) return -1; - if (auto monitor = g_manager->GetMonitor(id)) - { - return monitor->GetCursor()->GetY(); - } - return -1; + return g_manager->GetCursor()->GetY(); } - UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeWidth(int id) + UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeWidth() { if (!g_manager) return -1; - if (auto monitor = g_manager->GetMonitor(id)) - { - return monitor->GetCursor()->GetWidth(); - } - return -1; + return g_manager->GetCursor()->GetWidth(); } - UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeHeight(int id) + UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeHeight() { if (!g_manager) return -1; - if (auto monitor = g_manager->GetMonitor(id)) - { - return monitor->GetCursor()->GetHeight(); - } - return -1; + return g_manager->GetCursor()->GetHeight(); } - UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapePitch(int id) + UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapePitch() { if (!g_manager) return -1; - if (auto monitor = g_manager->GetMonitor(id)) - { - return monitor->GetCursor()->GetPitch(); - } - return -1; + return g_manager->GetCursor()->GetPitch(); } - UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeType(int id) + UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeType() { if (!g_manager) return -1; - if (auto monitor = g_manager->GetMonitor(id)) - { - return monitor->GetCursor()->GetType(); - } - return -1; + return g_manager->GetCursor()->GetType(); } - UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API GetCursorTexture(int id, ID3D11Texture2D* texture) + UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API GetCursorTexture(ID3D11Texture2D* texture) { if (!g_manager) return; - if (auto monitor = g_manager->GetMonitor(id)) - { - monitor->GetCursorTexture(texture); - } + return g_manager->GetCursor()->GetTexture(texture); } UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API SetTexturePtr(int id, void* texture) @@ -391,6 +360,7 @@ extern "C" { return monitor->GetMoveRectCount(); } + return 0; } UNITY_INTERFACE_EXPORT void* UNITY_INTERFACE_API GetMoveRects(int id) @@ -400,6 +370,7 @@ extern "C" { return monitor->GetMoveRects(); } + return nullptr; } UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetDirtyRectCount(int id) @@ -409,6 +380,7 @@ extern "C" { return monitor->GetDirtyRectCount(); } + return 0; } UNITY_INTERFACE_EXPORT void* UNITY_INTERFACE_API GetDirtyRects(int id) @@ -418,5 +390,6 @@ extern "C" { return monitor->GetDirtyRects(); } + return nullptr; } } \ No newline at end of file