EmbeddedWindow.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 SPB.Windowing;
  10. using System;
  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 = new GLXWindow(new NativeHandle(X11.DefaultDisplay), new NativeHandle(parent.Handle));
  84. WindowHandle = X11Window.WindowHandle.RawHandle;
  85. X11Display = X11Window.DisplayHandle.RawHandle;
  86. X11Window.Hide();
  87. return new PlatformHandle(WindowHandle, "X11");
  88. }
  89. [SupportedOSPlatform("windows")]
  90. unsafe IPlatformHandle CreateWin32(IPlatformHandle parent)
  91. {
  92. _className = "NativeWindow-" + Guid.NewGuid();
  93. _wndProcDelegate = WndProc;
  94. var wndClassEx = new WNDCLASSEX
  95. {
  96. cbSize = Marshal.SizeOf<WNDCLASSEX>(),
  97. hInstance = GetModuleHandle(null),
  98. lpfnWndProc = _wndProcDelegate,
  99. style = ClassStyles.CS_OWNDC,
  100. lpszClassName = _className,
  101. hCursor = LoadCursor(IntPtr.Zero, (IntPtr)Cursors.IDC_ARROW)
  102. };
  103. var atom = RegisterClassEx(ref wndClassEx);
  104. var handle = CreateWindowEx(
  105. 0,
  106. _className,
  107. "NativeWindow",
  108. WindowStyles.WS_CHILD,
  109. 0,
  110. 0,
  111. 640,
  112. 480,
  113. parent.Handle,
  114. IntPtr.Zero,
  115. IntPtr.Zero,
  116. IntPtr.Zero);
  117. WindowHandle = handle;
  118. return new PlatformHandle(WindowHandle, "HWND");
  119. }
  120. [SupportedOSPlatform("windows")]
  121. internal IntPtr WndProc(IntPtr hWnd, WindowsMessages msg, IntPtr wParam, IntPtr lParam)
  122. {
  123. var point = new Point((long)lParam & 0xFFFF, ((long)lParam >> 16) & 0xFFFF);
  124. var root = VisualRoot as Window;
  125. bool isLeft = false;
  126. switch (msg)
  127. {
  128. case WindowsMessages.LBUTTONDOWN:
  129. case WindowsMessages.RBUTTONDOWN:
  130. isLeft = msg == WindowsMessages.LBUTTONDOWN;
  131. this.RaiseEvent(new PointerPressedEventArgs(
  132. this,
  133. new Avalonia.Input.Pointer(0, PointerType.Mouse, true),
  134. root,
  135. this.TranslatePoint(point, root).Value,
  136. (ulong)Environment.TickCount64,
  137. new PointerPointProperties(isLeft ? RawInputModifiers.LeftMouseButton : RawInputModifiers.RightMouseButton, isLeft ? PointerUpdateKind.LeftButtonPressed : PointerUpdateKind.RightButtonPressed),
  138. KeyModifiers.None));
  139. break;
  140. case WindowsMessages.LBUTTONUP:
  141. case WindowsMessages.RBUTTONUP:
  142. isLeft = msg == WindowsMessages.LBUTTONUP;
  143. this.RaiseEvent(new PointerReleasedEventArgs(
  144. this,
  145. new Avalonia.Input.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.LeftButtonReleased : PointerUpdateKind.RightButtonReleased),
  150. KeyModifiers.None,
  151. isLeft ? MouseButton.Left : MouseButton.Right));
  152. break;
  153. case WindowsMessages.MOUSEMOVE:
  154. this.RaiseEvent(new PointerEventArgs(
  155. PointerMovedEvent,
  156. this,
  157. new Avalonia.Input.Pointer(0, PointerType.Mouse, true),
  158. root,
  159. this.TranslatePoint(point, root).Value,
  160. (ulong)Environment.TickCount64,
  161. new PointerPointProperties(RawInputModifiers.None, PointerUpdateKind.Other),
  162. KeyModifiers.None));
  163. break;
  164. }
  165. return DefWindowProc(hWnd, msg, (IntPtr)wParam, (IntPtr)lParam);
  166. }
  167. void DestroyLinux()
  168. {
  169. X11Window?.Dispose();
  170. }
  171. [SupportedOSPlatform("windows")]
  172. void DestroyWin32(IPlatformHandle handle)
  173. {
  174. DestroyWindow(handle.Handle);
  175. UnregisterClass(_className, GetModuleHandle(null));
  176. }
  177. }
  178. }