EmbeddedWindow.cs 7.0 KB

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