EmbeddedWindow.cs 7.9 KB

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