aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Ava/Ui/Backend/BackendSurface.cs
blob: 423fe038ebaae98bdf27e022265eff9aba4cac6c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using Avalonia;
using System;
using System.Runtime.InteropServices;
using static Ryujinx.Ava.Ui.Backend.Interop;

namespace Ryujinx.Ava.Ui.Backend
{
    public abstract class BackendSurface : IDisposable
    {
        protected IntPtr Display => _display;

        private IntPtr _display = IntPtr.Zero;

        [DllImport("libX11.so.6")]
        public static extern IntPtr XOpenDisplay(IntPtr display);

        [DllImport("libX11.so.6")]
        public static extern int XCloseDisplay(IntPtr display);

        private PixelSize _currentSize;
        public IntPtr Handle { get; protected set; }

        public bool IsDisposed { get; private set; }

        public BackendSurface(IntPtr handle)
        {
            Handle = handle;

            if (OperatingSystem.IsLinux())
            {
                _display = XOpenDisplay(IntPtr.Zero);
            }
        }

        public PixelSize Size
        {
            get
            {
                PixelSize size = new PixelSize();
                if (OperatingSystem.IsWindows())
                {
                    GetClientRect(Handle, out var rect);
                    size = new PixelSize(rect.right, rect.bottom);
                }
                else if (OperatingSystem.IsLinux())
                {
                    XWindowAttributes attributes = new XWindowAttributes();
                    XGetWindowAttributes(Display, Handle, ref attributes);

                    size = new PixelSize(attributes.width, attributes.height);
                }

                _currentSize = size;

                return size;
            }
        }

        public PixelSize CurrentSize => _currentSize;

        public virtual void Dispose()
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException(nameof(BackendSurface));
            }

            IsDisposed = true;

            if (_display != IntPtr.Zero)
            {
                XCloseDisplay(_display);
            }
        }
    }
}