EmbeddedWindow.cs 9.2 KB

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