| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066 |
- using ARMeilleure.Translation;
- using Avalonia.Controls;
- using Avalonia.Controls.ApplicationLifetimes;
- using Avalonia.Input;
- using Avalonia.Threading;
- using LibHac.Tools.FsSystem;
- using Ryujinx.Audio.Backends.Dummy;
- using Ryujinx.Audio.Backends.OpenAL;
- using Ryujinx.Audio.Backends.SDL2;
- using Ryujinx.Audio.Backends.SoundIo;
- using Ryujinx.Audio.Integration;
- using Ryujinx.Ava.Common;
- using Ryujinx.Ava.Common.Locale;
- using Ryujinx.Ava.Input;
- using Ryujinx.Ava.UI.Controls;
- using Ryujinx.Ava.UI.Helpers;
- using Ryujinx.Ava.UI.Models;
- using Ryujinx.Ava.UI.ViewModels;
- using Ryujinx.Ava.UI.Windows;
- using Ryujinx.Common;
- using Ryujinx.Common.Configuration;
- using Ryujinx.Common.Logging;
- using Ryujinx.Common.SystemInterop;
- using Ryujinx.Graphics.GAL;
- using Ryujinx.Graphics.GAL.Multithreading;
- using Ryujinx.Graphics.Gpu;
- using Ryujinx.Graphics.OpenGL;
- using Ryujinx.Graphics.Vulkan;
- using Ryujinx.HLE.FileSystem;
- using Ryujinx.HLE.HOS;
- using Ryujinx.HLE.HOS.Services.Account.Acc;
- using Ryujinx.HLE.HOS.SystemState;
- using Ryujinx.Input;
- using Ryujinx.Input.HLE;
- using Ryujinx.Ui.Common;
- using Ryujinx.Ui.Common.Configuration;
- using Ryujinx.Ui.Common.Helper;
- using SixLabors.ImageSharp;
- using SixLabors.ImageSharp.Formats.Png;
- using SixLabors.ImageSharp.PixelFormats;
- using SixLabors.ImageSharp.Processing;
- using SPB.Graphics.Vulkan;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Runtime.Versioning;
- using System.Threading;
- using System.Threading.Tasks;
- using static Ryujinx.Ava.UI.Helpers.Win32NativeInterop;
- using Image = SixLabors.ImageSharp.Image;
- using InputManager = Ryujinx.Input.HLE.InputManager;
- using Key = Ryujinx.Input.Key;
- using MouseButton = Ryujinx.Input.MouseButton;
- using Size = Avalonia.Size;
- using Switch = Ryujinx.HLE.Switch;
- using WindowState = Avalonia.Controls.WindowState;
- namespace Ryujinx.Ava
- {
- internal class AppHost
- {
- private const int CursorHideIdleTime = 5; // Hide Cursor seconds.
- private const float MaxResolutionScale = 4.0f; // Max resolution hotkeys can scale to before wrapping.
- private const int TargetFps = 60;
- private const float VolumeDelta = 0.05f;
- private static readonly Cursor InvisibleCursor = new(StandardCursorType.None);
- private readonly IntPtr InvisibleCursorWin;
- private readonly IntPtr DefaultCursorWin;
- private readonly long _ticksPerFrame;
- private readonly Stopwatch _chrono;
- private long _ticks;
- private readonly AccountManager _accountManager;
- private readonly UserChannelPersistence _userChannelPersistence;
- private readonly InputManager _inputManager;
- private readonly MainWindowViewModel _viewModel;
- private readonly IKeyboard _keyboardInterface;
- private readonly TopLevel _topLevel;
- private readonly GraphicsDebugLevel _glLogLevel;
- private float _newVolume;
- private KeyboardHotkeyState _prevHotkeyState;
- private long _lastCursorMoveTime;
- private bool _isCursorInRenderer;
- private bool _isStopped;
- private bool _isActive;
- private bool _renderingStarted;
- private IRenderer _renderer;
- private readonly Thread _renderingThread;
- private readonly CancellationTokenSource _gpuCancellationTokenSource;
- private WindowsMultimediaTimerResolution _windowsMultimediaTimerResolution;
- private bool _dialogShown;
- private readonly bool _isFirmwareTitle;
- private readonly object _lockObject = new();
- public event EventHandler AppExit;
- public event EventHandler<StatusUpdatedEventArgs> StatusUpdatedEvent;
- public RendererHost Renderer { get; }
- public VirtualFileSystem VirtualFileSystem { get; }
- public ContentManager ContentManager { get; }
- public NpadManager NpadManager { get; }
- public TouchScreenManager TouchScreenManager { get; }
- public Switch Device { get; set; }
- public int Width { get; private set; }
- public int Height { get; private set; }
- public string ApplicationPath { get; private set; }
- public bool ScreenshotRequested { get; set; }
- public AppHost(
- RendererHost renderer,
- InputManager inputManager,
- string applicationPath,
- VirtualFileSystem virtualFileSystem,
- ContentManager contentManager,
- AccountManager accountManager,
- UserChannelPersistence userChannelPersistence,
- MainWindowViewModel viewmodel,
- TopLevel topLevel)
- {
- _viewModel = viewmodel;
- _inputManager = inputManager;
- _accountManager = accountManager;
- _userChannelPersistence = userChannelPersistence;
- _renderingThread = new Thread(RenderLoop, 1 * 1024 * 1024) { Name = "GUI.RenderThread" };
- _lastCursorMoveTime = Stopwatch.GetTimestamp();
- _glLogLevel = ConfigurationState.Instance.Logger.GraphicsDebugLevel;
- _topLevel = topLevel;
- _inputManager.SetMouseDriver(new AvaloniaMouseDriver(_topLevel, renderer));
- _keyboardInterface = (IKeyboard)_inputManager.KeyboardDriver.GetGamepad("0");
- NpadManager = _inputManager.CreateNpadManager();
- TouchScreenManager = _inputManager.CreateTouchScreenManager();
- Renderer = renderer;
- ApplicationPath = applicationPath;
- VirtualFileSystem = virtualFileSystem;
- ContentManager = contentManager;
- _chrono = new Stopwatch();
- _ticksPerFrame = Stopwatch.Frequency / TargetFps;
- if (ApplicationPath.StartsWith("@SystemContent"))
- {
- ApplicationPath = _viewModel.VirtualFileSystem.SwitchPathToSystemPath(ApplicationPath);
- _isFirmwareTitle = true;
- }
- ConfigurationState.Instance.HideCursorOnIdle.Event += HideCursorState_Changed;
- _topLevel.PointerMoved += TopLevel_PointerMoved;
- if (OperatingSystem.IsWindows())
- {
- InvisibleCursorWin = CreateEmptyCursor();
- DefaultCursorWin = CreateArrowCursor();
- }
- ConfigurationState.Instance.System.IgnoreMissingServices.Event += UpdateIgnoreMissingServicesState;
- ConfigurationState.Instance.Graphics.AspectRatio.Event += UpdateAspectRatioState;
- ConfigurationState.Instance.System.EnableDockedMode.Event += UpdateDockedModeState;
- ConfigurationState.Instance.System.AudioVolume.Event += UpdateAudioVolumeState;
- _gpuCancellationTokenSource = new CancellationTokenSource();
- }
- private void TopLevel_PointerMoved(object sender, PointerEventArgs e)
- {
- if (sender is MainWindow window)
- {
- _lastCursorMoveTime = Stopwatch.GetTimestamp();
- if ((Renderer.Content as EmbeddedWindow).TransformedBounds != null)
- {
- var point = e.GetCurrentPoint(window).Position;
- var bounds = (Renderer.Content as EmbeddedWindow).TransformedBounds.Value.Clip;
- _isCursorInRenderer = point.X >= bounds.X &&
- point.X <= bounds.Width + bounds.X &&
- point.Y >= bounds.Y &&
- point.Y <= bounds.Height + bounds.Y;
- }
- }
- }
- private void ShowCursor()
- {
- Dispatcher.UIThread.Post(() =>
- {
- _viewModel.Cursor = Cursor.Default;
- if (OperatingSystem.IsWindows())
- {
- SetCursor(DefaultCursorWin);
- }
- });
- }
- private void HideCursor()
- {
- Dispatcher.UIThread.Post(() =>
- {
- _viewModel.Cursor = InvisibleCursor;
- if (OperatingSystem.IsWindows())
- {
- SetCursor(InvisibleCursorWin);
- }
- });
- }
- private void SetRendererWindowSize(Size size)
- {
- if (_renderer != null)
- {
- double scale = _topLevel.PlatformImpl.RenderScaling;
- _renderer.Window?.SetSize((int)(size.Width * scale), (int)(size.Height * scale));
- }
- }
- private unsafe void Renderer_ScreenCaptured(object sender, ScreenCaptureImageInfo e)
- {
- if (e.Data.Length > 0 && e.Height > 0 && e.Width > 0)
- {
- Task.Run(() =>
- {
- lock (_lockObject)
- {
- DateTime currentTime = DateTime.Now;
- string filename = $"ryujinx_capture_{currentTime}-{currentTime:D2}-{currentTime:D2}_{currentTime:D2}-{currentTime:D2}-{currentTime:D2}.png";
-
- string directory = AppDataManager.Mode switch
- {
- AppDataManager.LaunchMode.Portable => Path.Combine(AppDataManager.BaseDirPath, "screenshots"),
- _ => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "Ryujinx")
- };
- string path = Path.Combine(directory, filename);
- try
- {
- Directory.CreateDirectory(directory);
- }
- catch (Exception ex)
- {
- Logger.Error?.Print(LogClass.Application, $"Failed to create directory at path {directory}. Error : {ex.GetType().Name}", "Screenshot");
- return;
- }
- Image image = e.IsBgra ? Image.LoadPixelData<Bgra32>(e.Data, e.Width, e.Height)
- : Image.LoadPixelData<Rgba32>(e.Data, e.Width, e.Height);
- if (e.FlipX)
- {
- image.Mutate(x => x.Flip(FlipMode.Horizontal));
- }
- if (e.FlipY)
- {
- image.Mutate(x => x.Flip(FlipMode.Vertical));
- }
- image.SaveAsPng(path, new PngEncoder()
- {
- ColorType = PngColorType.Rgb
- });
- image.Dispose();
- Logger.Notice.Print(LogClass.Application, $"Screenshot saved to {path}", "Screenshot");
- }
- });
- }
- else
- {
- Logger.Error?.Print(LogClass.Application, $"Screenshot is empty. Size : {e.Data.Length} bytes. Resolution : {e.Width}x{e.Height}", "Screenshot");
- }
- }
- public void Start()
- {
- if (OperatingSystem.IsWindows())
- {
- _windowsMultimediaTimerResolution = new WindowsMultimediaTimerResolution(1);
- }
- DisplaySleep.Prevent();
- NpadManager.Initialize(Device, ConfigurationState.Instance.Hid.InputConfig, ConfigurationState.Instance.Hid.EnableKeyboard, ConfigurationState.Instance.Hid.EnableMouse);
- TouchScreenManager.Initialize(Device);
- _viewModel.IsGameRunning = true;
- string titleNameSection = string.IsNullOrWhiteSpace(Device.Application.TitleName) ? string.Empty : $" - {Device.Application.TitleName}";
- string titleVersionSection = string.IsNullOrWhiteSpace(Device.Application.DisplayVersion) ? string.Empty : $" v{Device.Application.DisplayVersion}";
- string titleIdSection = string.IsNullOrWhiteSpace(Device.Application.TitleIdText) ? string.Empty : $" ({Device.Application.TitleIdText.ToUpper()})";
- string titleArchSection = Device.Application.TitleIs64Bit ? " (64-bit)" : " (32-bit)";
- Dispatcher.UIThread.InvokeAsync(() =>
- {
- _viewModel.Title = $"Ryujinx {Program.Version}{titleNameSection}{titleVersionSection}{titleIdSection}{titleArchSection}";
- });
- _viewModel.SetUIProgressHandlers(Device);
- Renderer.SizeChanged += Window_SizeChanged;
- _isActive = true;
- _renderingThread.Start();
- _viewModel.Volume = ConfigurationState.Instance.System.AudioVolume.Value;
- MainLoop();
- Exit();
- }
- private void UpdateIgnoreMissingServicesState(object sender, ReactiveEventArgs<bool> args)
- {
- if (Device != null)
- {
- Device.Configuration.IgnoreMissingServices = args.NewValue;
- }
- }
- private void UpdateAspectRatioState(object sender, ReactiveEventArgs<AspectRatio> args)
- {
- if (Device != null)
- {
- Device.Configuration.AspectRatio = args.NewValue;
- }
- }
- private void UpdateDockedModeState(object sender, ReactiveEventArgs<bool> e)
- {
- Device?.System.ChangeDockedModeState(e.NewValue);
- }
- private void UpdateAudioVolumeState(object sender, ReactiveEventArgs<float> e)
- {
- Device?.SetVolume(e.NewValue);
- Dispatcher.UIThread.Post(() =>
- {
- _viewModel.Volume = e.NewValue;
- });
- }
- public void Stop()
- {
- _isActive = false;
- }
- private void Exit()
- {
- (_keyboardInterface as AvaloniaKeyboard)?.Clear();
- if (_isStopped)
- {
- return;
- }
- _isStopped = true;
- _isActive = false;
- }
- public void DisposeContext()
- {
- Dispose();
- _isActive = false;
- if (_renderingThread.IsAlive)
- {
- _renderingThread.Join();
- }
- DisplaySleep.Restore();
- NpadManager.Dispose();
- TouchScreenManager.Dispose();
- Device.Dispose();
- DisposeGpu();
- AppExit?.Invoke(this, EventArgs.Empty);
- }
- private void Dispose()
- {
- if (Device.Application != null)
- {
- _viewModel.UpdateGameMetadata(Device.Application.TitleIdText);
- }
- ConfigurationState.Instance.System.IgnoreMissingServices.Event -= UpdateIgnoreMissingServicesState;
- ConfigurationState.Instance.Graphics.AspectRatio.Event -= UpdateAspectRatioState;
- ConfigurationState.Instance.System.EnableDockedMode.Event -= UpdateDockedModeState;
- ConfigurationState.Instance.System.AudioVolume.Event -= UpdateAudioVolumeState;
- _topLevel.PointerMoved -= TopLevel_PointerMoved;
- _gpuCancellationTokenSource.Cancel();
- _gpuCancellationTokenSource.Dispose();
- _chrono.Stop();
- }
- public void DisposeGpu()
- {
- if (OperatingSystem.IsWindows())
- {
- _windowsMultimediaTimerResolution?.Dispose();
- _windowsMultimediaTimerResolution = null;
- }
- Renderer?.MakeCurrent();
- Device.DisposeGpu();
- Renderer?.MakeCurrent(null);
- }
- private void HideCursorState_Changed(object sender, ReactiveEventArgs<bool> state)
- {
- if (state.NewValue)
- {
- _lastCursorMoveTime = Stopwatch.GetTimestamp();
- }
- }
- public async Task<bool> LoadGuestApplication()
- {
- InitializeSwitchInstance();
- MainWindow.UpdateGraphicsConfig();
- SystemVersion firmwareVersion = ContentManager.GetCurrentFirmwareVersion();
- if (Avalonia.Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
- {
- if (!SetupValidator.CanStartApplication(ContentManager, ApplicationPath, out UserError userError))
- {
- {
- if (SetupValidator.CanFixStartApplication(ContentManager, ApplicationPath, userError, out firmwareVersion))
- {
- if (userError == UserError.NoFirmware)
- {
- UserResult result = await ContentDialogHelper.CreateConfirmationDialog(
- LocaleManager.Instance[LocaleKeys.DialogFirmwareNoFirmwareInstalledMessage],
- string.Format(LocaleManager.Instance[LocaleKeys.DialogFirmwareInstallEmbeddedMessage],
- firmwareVersion.VersionString),
- LocaleManager.Instance[LocaleKeys.InputDialogYes],
- LocaleManager.Instance[LocaleKeys.InputDialogNo],
- "");
- if (result != UserResult.Yes)
- {
- await UserErrorDialog.ShowUserErrorDialog(userError, (desktop.MainWindow as MainWindow));
- Device.Dispose();
- return false;
- }
- }
- if (!SetupValidator.TryFixStartApplication(ContentManager, ApplicationPath, userError, out _))
- {
- await UserErrorDialog.ShowUserErrorDialog(userError, (desktop.MainWindow as MainWindow));
- Device.Dispose();
- return false;
- }
- // Tell the user that we installed a firmware for them.
- if (userError == UserError.NoFirmware)
- {
- firmwareVersion = ContentManager.GetCurrentFirmwareVersion();
- _viewModel.RefreshFirmwareStatus();
- await ContentDialogHelper.CreateInfoDialog(
- string.Format(LocaleManager.Instance[LocaleKeys.DialogFirmwareInstalledMessage],
- firmwareVersion.VersionString),
- string.Format(LocaleManager.Instance[LocaleKeys.DialogFirmwareInstallEmbeddedSuccessMessage],
- firmwareVersion.VersionString),
- LocaleManager.Instance[LocaleKeys.InputDialogOk],
- "",
- LocaleManager.Instance[LocaleKeys.RyujinxInfo]);
- }
- }
- else
- {
- await UserErrorDialog.ShowUserErrorDialog(userError, (desktop.MainWindow as MainWindow));
- Device.Dispose();
- return false;
- }
- }
- }
- }
- Logger.Notice.Print(LogClass.Application, $"Using Firmware Version: {firmwareVersion?.VersionString}");
- if (_isFirmwareTitle)
- {
- Logger.Info?.Print(LogClass.Application, "Loading as Firmware Title (NCA).");
- Device.LoadNca(ApplicationPath);
- }
- else if (Directory.Exists(ApplicationPath))
- {
- string[] romFsFiles = Directory.GetFiles(ApplicationPath, "*.istorage");
- if (romFsFiles.Length == 0)
- {
- romFsFiles = Directory.GetFiles(ApplicationPath, "*.romfs");
- }
- if (romFsFiles.Length > 0)
- {
- Logger.Info?.Print(LogClass.Application, "Loading as cart with RomFS.");
- Device.LoadCart(ApplicationPath, romFsFiles[0]);
- }
- else
- {
- Logger.Info?.Print(LogClass.Application, "Loading as cart WITHOUT RomFS.");
- Device.LoadCart(ApplicationPath);
- }
- }
- else if (File.Exists(ApplicationPath))
- {
- switch (Path.GetExtension(ApplicationPath).ToLowerInvariant())
- {
- case ".xci":
- {
- Logger.Info?.Print(LogClass.Application, "Loading as XCI.");
- Device.LoadXci(ApplicationPath);
- break;
- }
- case ".nca":
- {
- Logger.Info?.Print(LogClass.Application, "Loading as NCA.");
- Device.LoadNca(ApplicationPath);
- break;
- }
- case ".nsp":
- case ".pfs0":
- {
- Logger.Info?.Print(LogClass.Application, "Loading as NSP.");
- Device.LoadNsp(ApplicationPath);
- break;
- }
- default:
- {
- Logger.Info?.Print(LogClass.Application, "Loading as homebrew.");
- try
- {
- Device.LoadProgram(ApplicationPath);
- }
- catch (ArgumentOutOfRangeException)
- {
- Logger.Error?.Print(LogClass.Application, "The specified file is not supported by Ryujinx.");
- Dispose();
- return false;
- }
- break;
- }
- }
- }
- else
- {
- Logger.Warning?.Print(LogClass.Application, "Please specify a valid XCI/NCA/NSP/PFS0/NRO file.");
- Dispose();
- return false;
- }
- DiscordIntegrationModule.SwitchToPlayingState(Device.Application.TitleIdText, Device.Application.TitleName);
- _viewModel.ApplicationLibrary.LoadAndSaveMetaData(Device.Application.TitleIdText, appMetadata =>
- {
- appMetadata.LastPlayed = DateTime.UtcNow.ToString();
- });
- return true;
- }
- internal void Resume()
- {
- Device?.System.TogglePauseEmulation(false);
- _viewModel.IsPaused = false;
- }
- internal void Pause()
- {
- Device?.System.TogglePauseEmulation(true);
- _viewModel.IsPaused = true;
- }
- private void InitializeSwitchInstance()
- {
- // Initialize KeySet.
- VirtualFileSystem.ReloadKeySet();
- // Initialize Renderer.
- IRenderer renderer;
- if (Renderer.IsVulkan)
- {
- string preferredGpu = ConfigurationState.Instance.Graphics.PreferredGpu.Value;
- renderer = new VulkanRenderer(Renderer.CreateVulkanSurface, VulkanHelper.GetRequiredInstanceExtensions, preferredGpu);
- }
- else
- {
- renderer = new OpenGLRenderer();
- }
- BackendThreading threadingMode = ConfigurationState.Instance.Graphics.BackendThreading;
- var isGALthreaded = threadingMode == BackendThreading.On || (threadingMode == BackendThreading.Auto && renderer.PreferThreading);
- if (isGALthreaded)
- {
- renderer = new ThreadedRenderer(renderer);
- }
- Logger.Info?.PrintMsg(LogClass.Gpu, $"Backend Threading ({threadingMode}): {isGALthreaded}");
- // Initialize Configuration.
- var memoryConfiguration = ConfigurationState.Instance.System.ExpandRam.Value ? HLE.MemoryConfiguration.MemoryConfiguration6GiB : HLE.MemoryConfiguration.MemoryConfiguration4GiB;
- HLE.HLEConfiguration configuration = new(VirtualFileSystem,
- _viewModel.LibHacHorizonManager,
- ContentManager,
- _accountManager,
- _userChannelPersistence,
- renderer,
- InitializeAudio(),
- memoryConfiguration,
- _viewModel.UiHandler,
- (SystemLanguage)ConfigurationState.Instance.System.Language.Value,
- (RegionCode)ConfigurationState.Instance.System.Region.Value,
- ConfigurationState.Instance.Graphics.EnableVsync,
- ConfigurationState.Instance.System.EnableDockedMode,
- ConfigurationState.Instance.System.EnablePtc,
- ConfigurationState.Instance.System.EnableInternetAccess,
- ConfigurationState.Instance.System.EnableFsIntegrityChecks ? IntegrityCheckLevel.ErrorOnInvalid : IntegrityCheckLevel.None,
- ConfigurationState.Instance.System.FsGlobalAccessLogMode,
- ConfigurationState.Instance.System.SystemTimeOffset,
- ConfigurationState.Instance.System.TimeZone,
- ConfigurationState.Instance.System.MemoryManagerMode,
- ConfigurationState.Instance.System.IgnoreMissingServices,
- ConfigurationState.Instance.Graphics.AspectRatio,
- ConfigurationState.Instance.System.AudioVolume);
- Device = new Switch(configuration);
- }
- private static IHardwareDeviceDriver InitializeAudio()
- {
- var availableBackends = new List<AudioBackend>()
- {
- AudioBackend.SDL2,
- AudioBackend.SoundIo,
- AudioBackend.OpenAl,
- AudioBackend.Dummy
- };
- AudioBackend preferredBackend = ConfigurationState.Instance.System.AudioBackend.Value;
- for (int i = 0; i < availableBackends.Count; i++)
- {
- if (availableBackends[i] == preferredBackend)
- {
- availableBackends.RemoveAt(i);
- availableBackends.Insert(0, preferredBackend);
- break;
- }
- }
- static IHardwareDeviceDriver InitializeAudioBackend<T>(AudioBackend backend, AudioBackend nextBackend) where T : IHardwareDeviceDriver, new()
- {
- if (T.IsSupported)
- {
- return new T();
- }
- else
- {
- Logger.Warning?.Print(LogClass.Audio, $"{backend} is not supported, falling back to {nextBackend}.");
- return null;
- }
- }
- IHardwareDeviceDriver deviceDriver = null;
- for (int i = 0; i < availableBackends.Count; i++)
- {
- AudioBackend currentBackend = availableBackends[i];
- AudioBackend nextBackend = i + 1 < availableBackends.Count ? availableBackends[i + 1] : AudioBackend.Dummy;
- deviceDriver = currentBackend switch
- {
- AudioBackend.SDL2 => InitializeAudioBackend<SDL2HardwareDeviceDriver>(AudioBackend.SDL2, nextBackend),
- AudioBackend.SoundIo => InitializeAudioBackend<SoundIoHardwareDeviceDriver>(AudioBackend.SoundIo, nextBackend),
- AudioBackend.OpenAl => InitializeAudioBackend<OpenALHardwareDeviceDriver>(AudioBackend.OpenAl, nextBackend),
- _ => new DummyHardwareDeviceDriver()
- };
- if (deviceDriver != null)
- {
- ConfigurationState.Instance.System.AudioBackend.Value = currentBackend;
- break;
- }
- }
- MainWindowViewModel.SaveConfig();
- return deviceDriver;
- }
- private void Window_SizeChanged(object sender, Size e)
- {
- Width = (int)e.Width;
- Height = (int)e.Height;
- SetRendererWindowSize(e);
- }
- private void MainLoop()
- {
- while (_isActive)
- {
- UpdateFrame();
- // Polling becomes expensive if it's not slept.
- Thread.Sleep(1);
- }
- }
- private unsafe void RenderLoop()
- {
- Dispatcher.UIThread.InvokeAsync(() =>
- {
- if (_viewModel.StartGamesInFullscreen)
- {
- _viewModel.WindowState = WindowState.FullScreen;
- }
- if (_viewModel.WindowState == WindowState.FullScreen)
- {
- _viewModel.ShowMenuAndStatusBar = false;
- }
- });
- _renderer = Device.Gpu.Renderer is ThreadedRenderer tr ? tr.BaseRenderer : Device.Gpu.Renderer;
- _renderer.ScreenCaptured += Renderer_ScreenCaptured;
- (_renderer as OpenGLRenderer)?.InitializeBackgroundContext(SPBOpenGLContext.CreateBackgroundContext(Renderer.GetContext()));
- Renderer.MakeCurrent();
- Device.Gpu.Renderer.Initialize(_glLogLevel);
- Width = (int)Renderer.Bounds.Width;
- Height = (int)Renderer.Bounds.Height;
- _renderer.Window.SetSize((int)(Width * _topLevel.PlatformImpl.RenderScaling), (int)(Height * _topLevel.PlatformImpl.RenderScaling));
- _chrono.Start();
- Device.Gpu.Renderer.RunLoop(() =>
- {
- Device.Gpu.SetGpuThread();
- Device.Gpu.InitializeShaderCache(_gpuCancellationTokenSource.Token);
- Translator.IsReadyForTranslation.Set();
- while (_isActive)
- {
- _ticks += _chrono.ElapsedTicks;
- _chrono.Restart();
- if (Device.WaitFifo())
- {
- Device.Statistics.RecordFifoStart();
- Device.ProcessFrame();
- Device.Statistics.RecordFifoEnd();
- }
- while (Device.ConsumeFrameAvailable())
- {
- if (!_renderingStarted)
- {
- _renderingStarted = true;
- _viewModel.SwitchToRenderer(false);
- }
- Device.PresentFrame(() => Renderer?.SwapBuffers());
- }
- if (_ticks >= _ticksPerFrame)
- {
- UpdateStatus();
- }
- }
- });
- Renderer?.MakeCurrent(null);
- }
- public void UpdateStatus()
- {
- // Run a status update only when a frame is to be drawn. This prevents from updating the ui and wasting a render when no frame is queued.
- string dockedMode = ConfigurationState.Instance.System.EnableDockedMode ? LocaleManager.Instance[LocaleKeys.Docked] : LocaleManager.Instance[LocaleKeys.Handheld];
-
- if (GraphicsConfig.ResScale != 1)
- {
- dockedMode += $" ({GraphicsConfig.ResScale}x)";
- }
- StatusUpdatedEvent?.Invoke(this, new StatusUpdatedEventArgs(
- Device.EnableDeviceVsync,
- LocaleManager.Instance[LocaleKeys.VolumeShort] + $": {(int)(Device.GetVolume() * 100)}%",
- Renderer.IsVulkan ? "Vulkan" : "OpenGL",
- dockedMode,
- ConfigurationState.Instance.Graphics.AspectRatio.Value.ToText(),
- LocaleManager.Instance[LocaleKeys.Game] + $": {Device.Statistics.GetGameFrameRate():00.00} FPS ({Device.Statistics.GetGameFrameTime():00.00} ms)",
- $"FIFO: {Device.Statistics.GetFifoPercent():00.00} %",
- $"GPU: {_renderer.GetHardwareInfo().GpuVendor}"));
- }
- public async Task ShowExitPrompt()
- {
- bool shouldExit = !ConfigurationState.Instance.ShowConfirmExit;
- if (!shouldExit)
- {
- if (_dialogShown)
- {
- return;
- }
- _dialogShown = true;
- shouldExit = await ContentDialogHelper.CreateStopEmulationDialog();
- _dialogShown = false;
- }
- if (shouldExit)
- {
- Stop();
- }
- }
- private bool UpdateFrame()
- {
- if (!_isActive)
- {
- return false;
- }
- NpadManager.Update(ConfigurationState.Instance.Graphics.AspectRatio.Value.ToFloat());
- if (_viewModel.IsActive)
- {
- if (ConfigurationState.Instance.Hid.EnableMouse)
- {
- if (_isCursorInRenderer)
- {
- HideCursor();
- }
- else
- {
- ShowCursor();
- }
- }
- else
- {
- if (ConfigurationState.Instance.HideCursorOnIdle)
- {
- if (Stopwatch.GetTimestamp() - _lastCursorMoveTime >= CursorHideIdleTime * Stopwatch.Frequency)
- {
- HideCursor();
- }
- else
- {
- ShowCursor();
- }
- }
- }
- Dispatcher.UIThread.Post(() =>
- {
- if (_keyboardInterface.GetKeyboardStateSnapshot().IsPressed(Key.Delete) && _viewModel.WindowState != WindowState.FullScreen)
- {
- Device.Application.DiskCacheLoadState?.Cancel();
- }
- });
- KeyboardHotkeyState currentHotkeyState = GetHotkeyState();
- if (currentHotkeyState != _prevHotkeyState)
- {
- switch (currentHotkeyState)
- {
- case KeyboardHotkeyState.ToggleVSync:
- Device.EnableDeviceVsync = !Device.EnableDeviceVsync;
- break;
- case KeyboardHotkeyState.Screenshot:
- ScreenshotRequested = true;
- break;
- case KeyboardHotkeyState.ShowUi:
- _viewModel.ShowMenuAndStatusBar = true;
- break;
- case KeyboardHotkeyState.Pause:
- if (_viewModel.IsPaused)
- {
- Resume();
- }
- else
- {
- Pause();
- }
- break;
- case KeyboardHotkeyState.ToggleMute:
- if (Device.IsAudioMuted())
- {
- Device.SetVolume(ConfigurationState.Instance.System.AudioVolume);
- }
- else
- {
- Device.SetVolume(0);
- }
- _viewModel.Volume = Device.GetVolume();
- break;
- case KeyboardHotkeyState.ResScaleUp:
- GraphicsConfig.ResScale = GraphicsConfig.ResScale % MaxResolutionScale + 1;
- break;
- case KeyboardHotkeyState.ResScaleDown:
- GraphicsConfig.ResScale =
- (MaxResolutionScale + GraphicsConfig.ResScale - 2) % MaxResolutionScale + 1;
- break;
- case KeyboardHotkeyState.VolumeUp:
- _newVolume = MathF.Round((Device.GetVolume() + VolumeDelta), 2);
- Device.SetVolume(_newVolume);
- _viewModel.Volume = Device.GetVolume();
- break;
- case KeyboardHotkeyState.VolumeDown:
- _newVolume = MathF.Round((Device.GetVolume() - VolumeDelta), 2);
- Device.SetVolume(_newVolume);
- _viewModel.Volume = Device.GetVolume();
- break;
- case KeyboardHotkeyState.None:
- (_keyboardInterface as AvaloniaKeyboard).Clear();
- break;
- }
- }
- _prevHotkeyState = currentHotkeyState;
- if (ScreenshotRequested)
- {
- ScreenshotRequested = false;
- _renderer.Screenshot();
- }
- }
- // Touchscreen.
- bool hasTouch = false;
- if (_viewModel.IsActive && !ConfigurationState.Instance.Hid.EnableMouse)
- {
- hasTouch = TouchScreenManager.Update(true, (_inputManager.MouseDriver as AvaloniaMouseDriver).IsButtonPressed(MouseButton.Button1), ConfigurationState.Instance.Graphics.AspectRatio.Value.ToFloat());
- }
- if (!hasTouch)
- {
- Device.Hid.Touchscreen.Update();
- }
- Device.Hid.DebugPad.Update();
- return true;
- }
- private KeyboardHotkeyState GetHotkeyState()
- {
- KeyboardHotkeyState state = KeyboardHotkeyState.None;
- if (_keyboardInterface.IsPressed((Key)ConfigurationState.Instance.Hid.Hotkeys.Value.ToggleVsync))
- {
- state = KeyboardHotkeyState.ToggleVSync;
- }
- else if (_keyboardInterface.IsPressed((Key)ConfigurationState.Instance.Hid.Hotkeys.Value.Screenshot))
- {
- state = KeyboardHotkeyState.Screenshot;
- }
- else if (_keyboardInterface.IsPressed((Key)ConfigurationState.Instance.Hid.Hotkeys.Value.ShowUi))
- {
- state = KeyboardHotkeyState.ShowUi;
- }
- else if (_keyboardInterface.IsPressed((Key)ConfigurationState.Instance.Hid.Hotkeys.Value.Pause))
- {
- state = KeyboardHotkeyState.Pause;
- }
- else if (_keyboardInterface.IsPressed((Key)ConfigurationState.Instance.Hid.Hotkeys.Value.ToggleMute))
- {
- state = KeyboardHotkeyState.ToggleMute;
- }
- else if (_keyboardInterface.IsPressed((Key)ConfigurationState.Instance.Hid.Hotkeys.Value.ResScaleUp))
- {
- state = KeyboardHotkeyState.ResScaleUp;
- }
- else if (_keyboardInterface.IsPressed((Key)ConfigurationState.Instance.Hid.Hotkeys.Value.ResScaleDown))
- {
- state = KeyboardHotkeyState.ResScaleDown;
- }
- else if (_keyboardInterface.IsPressed((Key)ConfigurationState.Instance.Hid.Hotkeys.Value.VolumeUp))
- {
- state = KeyboardHotkeyState.VolumeUp;
- }
- else if (_keyboardInterface.IsPressed((Key)ConfigurationState.Instance.Hid.Hotkeys.Value.VolumeDown))
- {
- state = KeyboardHotkeyState.VolumeDown;
- }
- return state;
- }
- }
- }
|