// Copyright Epic Games, Inc. All Rights Reserved. #include "MacInterface.h" #include #include #include #include #include #include "MacInstance.h" using namespace UE::StylusInput::Private; namespace UE::StylusInput::Mac { FName FMacInterface::GetName() const { static FName Name("NSEvent"); return Name; } IStylusInputInstance* FMacInterface::CreateInstance(SWindow& Window) { if (FRefCountedInstance* ExistingRefCountedInstance = Instances.Find(&Window)) { ++ExistingRefCountedInstance->RefCount; return ExistingRefCountedInstance->Instance.Get(); } FCocoaWindow* OSWindowHandle = [&Window] { const TSharedPtr NativeWindow = Window.GetNativeWindow(); return NativeWindow.IsValid() ? static_cast(NativeWindow->GetOSWindowHandle()) : nullptr; }(); if (!OSWindowHandle) { LogError("MacInterface", "Could not get native window handle."); return nullptr; } FMacInstance* NewInstance = Instances.Emplace( &Window, {MakeUnique(NextInstanceID++, OSWindowHandle), 1}).Instance.Get(); if (!ensureMsgf(NewInstance, TEXT("MacInterface: Failed to create stylus input instance."))) { Instances.Remove(&Window); return nullptr; } return NewInstance; } bool FMacInterface::ReleaseInstance(IStylusInputInstance* Instance) { check(Instance); FMacInstance *const MacInstance = static_cast(Instance); // Find existing instance for (TTuple& Entry : Instances) { FRefCountedInstance& RefCountedInstance = Entry.Get<1>(); if (RefCountedInstance.Instance.Get() == MacInstance) { // Decrease reference count check(RefCountedInstance.RefCount > 0); if (--RefCountedInstance.RefCount == 0) { // Delete if there are no references left Instances.Remove(Entry.Key); } return true; } } ensureMsgf(false, TEXT("MacInterface: Failed to find provided instance.")); return false; } TUniquePtr FMacInterface::Create() { TUniquePtr MacImpl = MakeUnique(); return TUniquePtr(MoveTemp(MacImpl)); } }