BackendSurface.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Avalonia;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using static Ryujinx.Ava.Ui.Backend.Interop;
  5. namespace Ryujinx.Ava.Ui.Backend
  6. {
  7. public abstract class BackendSurface : IDisposable
  8. {
  9. protected IntPtr Display => _display;
  10. private IntPtr _display = IntPtr.Zero;
  11. [DllImport("libX11.so.6")]
  12. public static extern IntPtr XOpenDisplay(IntPtr display);
  13. [DllImport("libX11.so.6")]
  14. public static extern int XCloseDisplay(IntPtr display);
  15. private PixelSize _currentSize;
  16. public IntPtr Handle { get; protected set; }
  17. public bool IsDisposed { get; private set; }
  18. public BackendSurface(IntPtr handle)
  19. {
  20. Handle = handle;
  21. if (OperatingSystem.IsLinux())
  22. {
  23. _display = XOpenDisplay(IntPtr.Zero);
  24. }
  25. }
  26. public PixelSize Size
  27. {
  28. get
  29. {
  30. PixelSize size = new PixelSize();
  31. if (OperatingSystem.IsWindows())
  32. {
  33. GetClientRect(Handle, out var rect);
  34. size = new PixelSize(rect.right, rect.bottom);
  35. }
  36. else if (OperatingSystem.IsLinux())
  37. {
  38. XWindowAttributes attributes = new XWindowAttributes();
  39. XGetWindowAttributes(Display, Handle, ref attributes);
  40. size = new PixelSize(attributes.width, attributes.height);
  41. }
  42. _currentSize = size;
  43. return size;
  44. }
  45. }
  46. public PixelSize CurrentSize => _currentSize;
  47. public virtual void Dispose()
  48. {
  49. if (IsDisposed)
  50. {
  51. throw new ObjectDisposedException(nameof(BackendSurface));
  52. }
  53. IsDisposed = true;
  54. if (_display != IntPtr.Zero)
  55. {
  56. XCloseDisplay(_display);
  57. }
  58. }
  59. }
  60. }