| | 1 | | using System; |
| | 2 | | using System.Collections.Concurrent; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Diagnostics; |
| | 5 | | using System.Drawing; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Runtime.CompilerServices; |
| | 8 | | using System.Runtime.InteropServices; |
| | 9 | | using static Imagini.ErrorHandler; |
| | 10 | | using static SDL2.SDL_error; |
| | 11 | | using static SDL2.SDL_hints; |
| | 12 | | using static SDL2.SDL_render; |
| | 13 | | using static SDL2.SDL_syswm; |
| | 14 | | using static SDL2.SDL_version; |
| | 15 | | using static SDL2.SDL_video; |
| | 16 | |
|
| | 17 | | namespace Imagini |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// An app window. |
| | 21 | | /// </summary> |
| | 22 | | public sealed class Window : Resource |
| | 23 | | { |
| | 24 | | /// <summary> |
| | 25 | | /// Gets the handle of this window. |
| | 26 | | /// </summary> |
| 939 | 27 | | public IntPtr Handle { get; private set; } |
| | 28 | | internal uint ID; |
| | 29 | |
|
| | 30 | |
|
| 0 | 31 | | private static Dictionary<uint, Window> _windows = |
| 0 | 32 | | new Dictionary<uint, Window>(); |
| | 33 | | /// <summary> |
| | 34 | | /// Returns all existing app windows. |
| | 35 | | /// </summary> |
| 5 | 36 | | public static IReadOnlyCollection<Window> Windows => _windows.Values; |
| | 37 | |
|
| | 38 | | internal static Window Current |
| | 39 | | { |
| | 40 | | get |
| | 41 | | { |
| 23 | 42 | | if (_overridedCurrent != null && !_overridedCurrent.IsDisposed) |
| 22 | 43 | | return _overridedCurrent; |
| 2 | 44 | | if (_windows.Count == 0) return null; |
| 0 | 45 | | return _windows.Values |
| 0 | 46 | | .Where(window => !window.IsDisposed && window.IsVisible && window.IsFocused) |
| 0 | 47 | | .FirstOrDefault(); |
| | 48 | | } |
| | 49 | | } |
| | 50 | |
|
| 1 | 51 | | private static Window _overridedCurrent = null; |
| | 52 | | /// <summary> |
| | 53 | | /// Sets the specified window as the current one no matter |
| | 54 | | /// if it's focused and visible or not. If a null is passed, |
| | 55 | | /// then the overriding is disabled. If the specified window is |
| | 56 | | /// disposed, then the overriding is disabled. |
| | 57 | | /// </summary> |
| 9 | 58 | | public static void OverrideCurrentWith(Window window) => _overridedCurrent = window; |
| | 59 | |
|
| | 60 | | internal static Window GetByID(uint id) |
| | 61 | | { |
| 58 | 62 | | if (_windows.ContainsKey(id)) |
| 58 | 63 | | return _windows[id]; |
| 0 | 64 | | return null; |
| | 65 | | } |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Gets or sets the size of the window's client area. Affects only |
| | 69 | | /// non-fullscreen modes. |
| | 70 | | /// </summary> |
| | 71 | | /// <remarks> |
| | 72 | | /// The window size in screen coordinates may differ from the size in |
| | 73 | | /// pixels, if the window was created with <see cref="WindowSettings.AllowHighDpi" />. |
| | 74 | | /// </remarks> |
| | 75 | | public Size Size |
| | 76 | | { |
| | 77 | | get |
| | 78 | | { |
| 3 | 79 | | CheckIfNotDisposed(); |
| 3 | 80 | | SDL_GetWindowSize(Handle, out int w, out int h); |
| 3 | 81 | | return new Size(w, h); |
| | 82 | | } |
| | 83 | | set |
| | 84 | | { |
| 1 | 85 | | CheckIfNotDisposed(); |
| 1 | 86 | | SDL_SetWindowSize(Handle, value.Width, value.Height); |
| 1 | 87 | | } |
| | 88 | | } |
| | 89 | |
|
| | 90 | | /// <summary> |
| | 91 | | /// Gets the size of the window's client area in pixels. |
| | 92 | | /// </summary> |
| | 93 | | /// <seealso cref="Size" /> |
| | 94 | | public Size SizeInPixels |
| | 95 | | { |
| | 96 | | get |
| | 97 | | { |
| 1 | 98 | | CheckIfNotDisposed(); |
| 1 | 99 | | SDL_GL_GetDrawableSize(Handle, out int w, out int h); |
| 1 | 100 | | return new Size(w, h); |
| | 101 | | } |
| | 102 | | } |
| | 103 | |
|
| | 104 | | /// <summary> |
| | 105 | | /// Gets or sets the window minimum size. |
| | 106 | | /// </summary> |
| | 107 | | public Size MinimumSize |
| | 108 | | { |
| | 109 | | get |
| | 110 | | { |
| 1 | 111 | | CheckIfNotDisposed(); |
| 1 | 112 | | SDL_GetWindowMinimumSize(Handle, out int w, out int h); |
| 1 | 113 | | return new Size(w, h); |
| | 114 | | } |
| | 115 | | set |
| | 116 | | { |
| 1 | 117 | | CheckIfNotDisposed(); |
| 1 | 118 | | SDL_SetWindowMinimumSize(Handle, value.Width, value.Height); |
| 1 | 119 | | } |
| | 120 | | } |
| | 121 | |
|
| | 122 | | /// <summary> |
| | 123 | | /// Gets or sets the window minimum size. |
| | 124 | | /// </summary> |
| | 125 | | public Size MaximumSize |
| | 126 | | { |
| | 127 | | get |
| | 128 | | { |
| 1 | 129 | | CheckIfNotDisposed(); |
| 1 | 130 | | SDL_GetWindowMaximumSize(Handle, out int w, out int h); |
| 1 | 131 | | return new Size(w, h); |
| | 132 | | } |
| | 133 | | set |
| | 134 | | { |
| 1 | 135 | | CheckIfNotDisposed(); |
| 1 | 136 | | SDL_SetWindowMaximumSize(Handle, value.Width, value.Height); |
| 1 | 137 | | } |
| | 138 | | } |
| | 139 | |
|
| | 140 | | /// <summary> |
| | 141 | | /// Returns the settings for this window. |
| | 142 | | /// </summary> |
| 159 | 143 | | public WindowSettings Settings { get; private set; } |
| | 144 | |
|
| | 145 | | /// <summary> |
| | 146 | | /// Returns the <see cref="WindowMode" /> for this window. |
| | 147 | | /// </summary> |
| 1 | 148 | | public WindowMode Mode => Settings.WindowMode; |
| | 149 | |
|
| 76 | 150 | | public Window(WindowSettings settings, uint additionalFlags = 0) |
| | 151 | | { |
| 0 | 152 | | Handle = SDL_CreateWindow(settings.Title, |
| 0 | 153 | | SDL_WINDOWPOS_CENTERED_DISPLAY(settings.DisplayIndex), |
| 0 | 154 | | SDL_WINDOWPOS_CENTERED_DISPLAY(settings.DisplayIndex), |
| 0 | 155 | | settings.WindowWidth, settings.WindowHeight, settings.GetFlags() | additionalFlags); |
| 76 | 156 | | CheckWindowHandle(); |
| 76 | 157 | | Title = settings.Title; |
| 76 | 158 | | Apply(settings); |
| 76 | 159 | | Register(); |
| 76 | 160 | | Raise(); |
| 76 | 161 | | } |
| | 162 | |
|
| 0 | 163 | | public Window(IntPtr handle) |
| | 164 | | { |
| 0 | 165 | | Handle = handle; |
| 0 | 166 | | CheckWindowHandle(); |
| 0 | 167 | | Register(); |
| 0 | 168 | | } |
| | 169 | |
|
| | 170 | | private void CheckWindowHandle() |
| | 171 | | { |
| 76 | 172 | | if (Handle == IntPtr.Zero) |
| 0 | 173 | | throw new ImaginiException($"Could not create window: {SDL_GetError()}"); |
| 76 | 174 | | ID = SDL_GetWindowID(Handle); |
| 76 | 175 | | if (ID == 0) |
| 0 | 176 | | throw new ImaginiException($"Could not get the window ID: {SDL_GetError()}"); |
| 76 | 177 | | } |
| | 178 | |
|
| | 179 | | private void Register() |
| | 180 | | { |
| | 181 | | // Generally should not happen, but it's better to check anyways |
| 76 | 182 | | if (_windows.ContainsKey(ID)) |
| 0 | 183 | | throw new ImaginiException("Window with the specified ID already exists"); |
| | 184 | | else |
| 76 | 185 | | _windows.Add(ID, this); |
| 76 | 186 | | GetSubsystem(); |
| 76 | 187 | | } |
| | 188 | |
|
| | 189 | | /// <summary> |
| | 190 | | /// Applies the specified settings to this window. |
| | 191 | | /// </summary> |
| | 192 | | public void Apply(WindowSettings settings) |
| | 193 | | { |
| 77 | 194 | | CheckIfNotDisposed(); |
| 77 | 195 | | settings.Apply(Handle); |
| 77 | 196 | | Settings = settings.Clone() as WindowSettings; |
| 77 | 197 | | if (settings.WindowMode == WindowMode.Fullscreen) |
| 1 | 198 | | settings.FullscreenDisplayMode = this.Display.CurrentMode; |
| 77 | 199 | | var h = SDL_GetHint(SDL_HINT_RENDER_VSYNC); |
| 77 | 200 | | Settings.VSync = SDL_GetHint(SDL_HINT_RENDER_VSYNC) == "1"; |
| 77 | 201 | | Title = settings.Title; |
| 77 | 202 | | OnSettingsChanged?.Invoke(this, settings); |
| 0 | 203 | | } |
| | 204 | |
|
| | 205 | | /// <summary> |
| | 206 | | /// Fires after <see cref="Apply()">. |
| | 207 | | /// </summary> |
| | 208 | | public event EventHandler<WindowSettings> OnSettingsChanged; |
| | 209 | |
|
| | 210 | | /// <summary> |
| | 211 | | /// Shows this window. |
| | 212 | | /// </summary> |
| 2 | 213 | | public void Show() => NotDisposed(() => SDL_ShowWindow(Handle)); |
| | 214 | | /// <summary> |
| | 215 | | /// Hides this window. |
| | 216 | | /// </summary> |
| 2 | 217 | | public void Hide() => NotDisposed(() => SDL_HideWindow(Handle)); |
| | 218 | |
|
| | 219 | | /// <summary> |
| | 220 | | /// Indicates if the window is visible. |
| | 221 | | /// </summary> |
| | 222 | | public bool IsVisible |
| | 223 | | { |
| 1 | 224 | | get => HasFlag(SDL_WindowFlags.SDL_WINDOW_SHOWN); |
| | 225 | | } |
| | 226 | |
|
| | 227 | | /// <summary> |
| | 228 | | /// Returns true if this window has input focus. |
| | 229 | | /// </summary> |
| 0 | 230 | | public bool IsFocused => HasFlag(SDL_WindowFlags.SDL_WINDOW_INPUT_FOCUS) || |
| 0 | 231 | | HasFlag(SDL_WindowFlags.SDL_WINDOW_MOUSE_FOCUS) || |
| 0 | 232 | | HasFlag(SDL_WindowFlags.SDL_WINDOW_INPUT_GRABBED); |
| | 233 | |
|
| | 234 | | /// <summary> |
| | 235 | | /// Minimizes this window. |
| | 236 | | /// </summary> |
| 2 | 237 | | public void Minimize() => NotDisposed(() => SDL_MinimizeWindow(Handle)); |
| | 238 | |
|
| | 239 | | /// <summary> |
| | 240 | | /// Indicates if the window is minimized. |
| | 241 | | /// </summary> |
| | 242 | | public bool IsMinimized |
| | 243 | | { |
| 8 | 244 | | get => HasFlag(SDL_WindowFlags.SDL_WINDOW_MINIMIZED); |
| | 245 | | } |
| | 246 | |
|
| | 247 | | /// <summary> |
| | 248 | | /// Maximizes this window. |
| | 249 | | /// </summary> |
| 2 | 250 | | public void Maximize() => NotDisposed(() => SDL_MaximizeWindow(Handle)); |
| | 251 | |
|
| | 252 | | /// <summary> |
| | 253 | | /// Indicates if the window is maximized. |
| | 254 | | /// </summary> |
| | 255 | | public bool IsMaximized |
| | 256 | | { |
| 2 | 257 | | get => HasFlag(SDL_WindowFlags.SDL_WINDOW_MAXIMIZED); |
| | 258 | | } |
| | 259 | |
|
| | 260 | | /// <summary> |
| | 261 | | /// Restores the window. Called when IsMinimized or IsMaximized is set |
| | 262 | | /// to false. |
| | 263 | | /// </summary> |
| 2 | 264 | | public void Restore() => NotDisposed(() => SDL_RestoreWindow(Handle)); |
| | 265 | |
|
| | 266 | | /// <summary> |
| | 267 | | /// Raises this window above other windows and sets the input focus. |
| | 268 | | /// </summary> |
| 0 | 269 | | public void Raise() => NotDisposed(() => |
| 79 | 270 | | { |
| 79 | 271 | | SDL_RaiseWindow(Handle); |
| 79 | 272 | | SDL_SetWindowInputFocus(Handle); // X11 only |
| 79 | 273 | | }); |
| | 274 | |
|
| | 275 | | /// <summary> |
| | 276 | | /// Gets or sets the window title. |
| | 277 | | /// </summary> |
| | 278 | | public string Title |
| | 279 | | { |
| 2 | 280 | | get => NotDisposed(() => SDL_GetWindowTitle(Handle)); |
| 306 | 281 | | set => NotDisposed(() => SDL_SetWindowTitle(Handle, value)); |
| | 282 | | } |
| | 283 | |
|
| | 284 | | /// <summary> |
| | 285 | | /// Returns the display index of this window. |
| | 286 | | /// </summary> |
| 1 | 287 | | public int DisplayIndex => NotDisposed(() => |
| 1 | 288 | | Display.GetCurrentDisplayIndexForWindow(Handle)); |
| | 289 | | /// <summary> |
| | 290 | | /// Returns the display of this window. |
| | 291 | | /// </summary> |
| 2 | 292 | | public Display Display => NotDisposed(() => |
| 2 | 293 | | Display.GetCurrentDisplayForWindow(Handle)); |
| | 294 | |
|
| 11 | 295 | | private uint _flags => SDL_GetWindowFlags(Handle); |
| | 296 | |
|
| | 297 | | private bool HasFlag(SDL_WindowFlags flag) => |
| 22 | 298 | | NotDisposed(() => (_flags & (uint)flag) == (uint)flag); |
| | 299 | |
|
| 1 | 300 | | static Window() => Lifecycle.TryInitialize(); |
| | 301 | |
|
| | 302 | | internal override void Destroy() |
| | 303 | | { |
| 76 | 304 | | if (IsDisposed) return; |
| 76 | 305 | | base.Destroy(); |
| 76 | 306 | | SDL_DestroyWindow(Handle); |
| 76 | 307 | | _windows.Remove(ID); |
| 76 | 308 | | Handle = IntPtr.Zero; |
| 76 | 309 | | } |
| | 310 | |
|
| | 311 | | /// <summary> |
| | 312 | | /// Returns the underlying window subsystem type. |
| | 313 | | /// </summary> |
| 152 | 314 | | public WindowSubsystem Subsystem { get; private set; } |
| | 315 | |
|
| | 316 | | private unsafe void GetSubsystem() |
| | 317 | | { |
| 76 | 318 | | SDL_GetVersion(out SDL_Version version); |
| 76 | 319 | | var container = Marshal.AllocHGlobal(32); |
| 76 | 320 | | var p = (byte*)container; |
| 76 | 321 | | *p = version.major; |
| 76 | 322 | | *(p + 1) = version.minor; |
| 76 | 323 | | *(p + 2) = version.patch; |
| 76 | 324 | | SDL_GetWindowWMInfo(Handle, container); |
| 76 | 325 | | Subsystem = WindowSubsystem.Unknown; |
| 76 | 326 | | var min = Enum.GetValues(typeof(WindowSubsystem)).Cast<int>().Min(); |
| 76 | 327 | | var max = Enum.GetValues(typeof(WindowSubsystem)).Cast<int>().Max(); |
| 304 | 328 | | for (int i = 0; i < 4; i++) |
| | 329 | | { |
| | 330 | | // run through enum bytes because the num size is not fixed |
| 152 | 331 | | var value = *(p + 3 + i); |
| 152 | 332 | | if (value >= min && value <= max) |
| | 333 | | { |
| 76 | 334 | | Subsystem = (WindowSubsystem)value; |
| 76 | 335 | | break; |
| | 336 | | } |
| | 337 | | } |
| 76 | 338 | | Marshal.FreeHGlobal(container); |
| 76 | 339 | | } |
| | 340 | | } |
| | 341 | |
|
| | 342 | | /// <summary> |
| | 343 | | /// Defines the windowing system type. |
| | 344 | | /// </summary> |
| | 345 | | public enum WindowSubsystem |
| | 346 | | { |
| | 347 | | Unknown, |
| | 348 | | Windows, |
| | 349 | | X11, |
| | 350 | | DirectFB, |
| | 351 | | Cocoa, |
| | 352 | | UIKit, |
| | 353 | | Wayland, |
| | 354 | | Mir, |
| | 355 | | WinRT, |
| | 356 | | Android, |
| | 357 | | Vivante |
| | 358 | | } |
| | 359 | | } |