EmbeddedWindow.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Input;
  4. using Avalonia.Platform;
  5. using SPB.Graphics;
  6. using SPB.Platform;
  7. using SPB.Platform.GLX;
  8. using System;
  9. using System.Runtime.InteropServices;
  10. using System.Runtime.Versioning;
  11. using System.Threading.Tasks;
  12. using static Ryujinx.Ava.UI.Helpers.Win32NativeInterop;
  13. namespace Ryujinx.Ava.UI.Helpers
  14. {
  15. public class EmbeddedWindow : NativeControlHost
  16. {
  17. private WindowProc _wndProcDelegate;
  18. private string _className;
  19. protected GLXWindow X11Window { get; set; }
  20. protected IntPtr WindowHandle { get; set; }
  21. protected IntPtr X11Display { get; set; }
  22. protected IntPtr NsView { get; set; }
  23. protected IntPtr MetalLayer { get; set; }
  24. private UpdateBoundsCallbackDelegate _updateBoundsCallback;
  25. public event EventHandler<IntPtr> WindowCreated;
  26. public event EventHandler<Size> SizeChanged;
  27. protected virtual void OnWindowDestroyed() { }
  28. protected virtual void OnWindowDestroying()
  29. {
  30. WindowHandle = IntPtr.Zero;
  31. X11Display = IntPtr.Zero;
  32. NsView = IntPtr.Zero;
  33. MetalLayer = IntPtr.Zero;
  34. }
  35. public EmbeddedWindow()
  36. {
  37. var stateObserverable = this.GetObservable(BoundsProperty);
  38. stateObserverable.Subscribe(StateChanged);
  39. Initialized += NativeEmbeddedWindow_Initialized;
  40. }
  41. public virtual void OnWindowCreated() { }
  42. private void NativeEmbeddedWindow_Initialized(object sender, EventArgs e)
  43. {
  44. OnWindowCreated();
  45. Task.Run(() =>
  46. {
  47. WindowCreated?.Invoke(this, WindowHandle);
  48. });
  49. }
  50. private void StateChanged(Rect rect)
  51. {
  52. SizeChanged?.Invoke(this, rect.Size);
  53. _updateBoundsCallback?.Invoke(rect);
  54. }
  55. protected override IPlatformHandle CreateNativeControlCore(IPlatformHandle parent)
  56. {
  57. if (OperatingSystem.IsLinux())
  58. {
  59. return CreateLinux(parent);
  60. }
  61. else if (OperatingSystem.IsWindows())
  62. {
  63. return CreateWin32(parent);
  64. }
  65. else if (OperatingSystem.IsMacOS())
  66. {
  67. return CreateMacOs(parent);
  68. }
  69. return base.CreateNativeControlCore(parent);
  70. }
  71. protected override void DestroyNativeControlCore(IPlatformHandle control)
  72. {
  73. OnWindowDestroying();
  74. if (OperatingSystem.IsLinux())
  75. {
  76. DestroyLinux();
  77. }
  78. else if (OperatingSystem.IsWindows())
  79. {
  80. DestroyWin32(control);
  81. }
  82. else if (OperatingSystem.IsMacOS())
  83. {
  84. DestroyMacOS();
  85. }
  86. else
  87. {
  88. base.DestroyNativeControlCore(control);
  89. }
  90. OnWindowDestroyed();
  91. }
  92. [SupportedOSPlatform("linux")]
  93. protected virtual IPlatformHandle CreateLinux(IPlatformHandle parent)
  94. {
  95. X11Window = PlatformHelper.CreateOpenGLWindow(FramebufferFormat.Default, 0, 0, 100, 100) as GLXWindow;
  96. WindowHandle = X11Window.WindowHandle.RawHandle;
  97. X11Display = X11Window.DisplayHandle.RawHandle;
  98. return new PlatformHandle(WindowHandle, "X11");
  99. }
  100. [SupportedOSPlatform("windows")]
  101. IPlatformHandle CreateWin32(IPlatformHandle parent)
  102. {
  103. _className = "NativeWindow-" + Guid.NewGuid();
  104. _wndProcDelegate = WndProc;
  105. var wndClassEx = new WNDCLASSEX
  106. {
  107. cbSize = Marshal.SizeOf<WNDCLASSEX>(),
  108. hInstance = GetModuleHandle(null),
  109. lpfnWndProc = Marshal.GetFunctionPointerForDelegate(_wndProcDelegate),
  110. style = ClassStyles.CS_OWNDC,
  111. lpszClassName = Marshal.StringToHGlobalUni(_className),
  112. hCursor = CreateArrowCursor()
  113. };
  114. var atom = RegisterClassEx(ref wndClassEx);
  115. var handle = CreateWindowEx(
  116. 0,
  117. _className,
  118. "NativeWindow",
  119. WindowStyles.WS_CHILD,
  120. 0,
  121. 0,
  122. 640,
  123. 480,
  124. parent.Handle,
  125. IntPtr.Zero,
  126. IntPtr.Zero,
  127. IntPtr.Zero);
  128. WindowHandle = handle;
  129. Marshal.FreeHGlobal(wndClassEx.lpszClassName);
  130. return new PlatformHandle(WindowHandle, "HWND");
  131. }
  132. [SupportedOSPlatform("windows")]
  133. IntPtr WndProc(IntPtr hWnd, WindowsMessages msg, IntPtr wParam, IntPtr lParam)
  134. {
  135. var point = new Point((long)lParam & 0xFFFF, ((long)lParam >> 16) & 0xFFFF);
  136. var root = VisualRoot as Window;
  137. bool isLeft = false;
  138. switch (msg)
  139. {
  140. case WindowsMessages.LBUTTONDOWN:
  141. case WindowsMessages.RBUTTONDOWN:
  142. isLeft = msg == WindowsMessages.LBUTTONDOWN;
  143. this.RaiseEvent(new PointerPressedEventArgs(
  144. this,
  145. new Pointer(0, PointerType.Mouse, true),
  146. root,
  147. this.TranslatePoint(point, root).Value,
  148. (ulong)Environment.TickCount64,
  149. new PointerPointProperties(isLeft ? RawInputModifiers.LeftMouseButton : RawInputModifiers.RightMouseButton, isLeft ? PointerUpdateKind.LeftButtonPressed : PointerUpdateKind.RightButtonPressed),
  150. KeyModifiers.None));
  151. break;
  152. case WindowsMessages.LBUTTONUP:
  153. case WindowsMessages.RBUTTONUP:
  154. isLeft = msg == WindowsMessages.LBUTTONUP;
  155. this.RaiseEvent(new PointerReleasedEventArgs(
  156. this,
  157. new Pointer(0, PointerType.Mouse, true),
  158. root,
  159. this.TranslatePoint(point, root).Value,
  160. (ulong)Environment.TickCount64,
  161. new PointerPointProperties(isLeft ? RawInputModifiers.LeftMouseButton : RawInputModifiers.RightMouseButton, isLeft ? PointerUpdateKind.LeftButtonReleased : PointerUpdateKind.RightButtonReleased),
  162. KeyModifiers.None,
  163. isLeft ? MouseButton.Left : MouseButton.Right));
  164. break;
  165. case WindowsMessages.MOUSEMOVE:
  166. this.RaiseEvent(new PointerEventArgs(
  167. PointerMovedEvent,
  168. this,
  169. new Pointer(0, PointerType.Mouse, true),
  170. root,
  171. this.TranslatePoint(point, root).Value,
  172. (ulong)Environment.TickCount64,
  173. new PointerPointProperties(RawInputModifiers.None, PointerUpdateKind.Other),
  174. KeyModifiers.None));
  175. break;
  176. }
  177. return DefWindowProc(hWnd, msg, wParam, lParam);
  178. }
  179. [SupportedOSPlatform("macos")]
  180. IPlatformHandle CreateMacOs(IPlatformHandle parent)
  181. {
  182. MetalLayer = MetalHelper.GetMetalLayer(out IntPtr nsView, out _updateBoundsCallback);
  183. NsView = nsView;
  184. return new PlatformHandle(nsView, "NSView");
  185. }
  186. void DestroyLinux()
  187. {
  188. X11Window?.Dispose();
  189. }
  190. [SupportedOSPlatform("windows")]
  191. void DestroyWin32(IPlatformHandle handle)
  192. {
  193. DestroyWindow(handle.Handle);
  194. UnregisterClass(_className, GetModuleHandle(null));
  195. }
  196. [SupportedOSPlatform("macos")]
  197. void DestroyMacOS()
  198. {
  199. MetalHelper.DestroyMetalLayer(NsView, MetalLayer);
  200. }
  201. }
  202. }