2020-06-16 14:28:02 -04:00
|
|
|
|
using ARMeilleure.Translation.PTC;
|
2020-05-02 22:00:53 -04:00
|
|
|
|
using Gdk;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.Graphics;
|
|
|
|
|
using OpenTK.Graphics.OpenGL;
|
|
|
|
|
using OpenTK.Input;
|
|
|
|
|
using Ryujinx.Configuration;
|
2020-05-02 22:00:53 -04:00
|
|
|
|
using Ryujinx.Common.Configuration.Hid;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
using Ryujinx.Graphics.OpenGL;
|
|
|
|
|
using Ryujinx.HLE;
|
2020-04-02 20:10:02 -04:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Hid;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Ui
|
|
|
|
|
{
|
2020-05-02 22:00:53 -04:00
|
|
|
|
public class GlRenderer : GLWidget
|
2020-02-11 19:56:19 -05:00
|
|
|
|
{
|
2020-05-02 22:00:53 -04:00
|
|
|
|
private const int SwitchPanelWidth = 1280;
|
2020-02-18 06:34:57 -05:00
|
|
|
|
private const int SwitchPanelHeight = 720;
|
2020-05-02 22:00:53 -04:00
|
|
|
|
private const int TargetFps = 60;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
|
|
|
|
public ManualResetEvent WaitEvent { get; set; }
|
|
|
|
|
|
2020-03-06 21:40:06 -05:00
|
|
|
|
public static event EventHandler<StatusUpdatedEventArgs> StatusUpdatedEvent;
|
|
|
|
|
|
2020-02-11 19:56:19 -05:00
|
|
|
|
public bool IsActive { get; set; }
|
|
|
|
|
public bool IsStopped { get; set; }
|
|
|
|
|
public bool IsFocused { get; set; }
|
|
|
|
|
|
|
|
|
|
private double _mouseX;
|
|
|
|
|
private double _mouseY;
|
2020-05-02 22:00:53 -04:00
|
|
|
|
private bool _mousePressed;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
|
|
|
|
private bool _toggleFullscreen;
|
|
|
|
|
|
|
|
|
|
private readonly long _ticksPerFrame;
|
|
|
|
|
|
|
|
|
|
private long _ticks = 0;
|
|
|
|
|
|
|
|
|
|
private System.Diagnostics.Stopwatch _chrono;
|
|
|
|
|
|
|
|
|
|
private Switch _device;
|
|
|
|
|
|
|
|
|
|
private Renderer _renderer;
|
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
|
private HotkeyButtons _prevHotkeyButtons;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
|
public GlRenderer(Switch device)
|
2020-03-14 17:36:56 -04:00
|
|
|
|
: base (GetGraphicsMode(),
|
2020-02-18 06:34:57 -05:00
|
|
|
|
3, 3,
|
2020-02-11 19:56:19 -05:00
|
|
|
|
GraphicsContextFlags.ForwardCompatible)
|
|
|
|
|
{
|
|
|
|
|
WaitEvent = new ManualResetEvent(false);
|
|
|
|
|
|
|
|
|
|
_device = device;
|
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
|
this.Initialized += GLRenderer_Initialized;
|
|
|
|
|
this.Destroyed += GLRenderer_Destroyed;
|
2020-02-13 12:43:29 -05:00
|
|
|
|
this.ShuttingDown += GLRenderer_ShuttingDown;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
|
|
|
|
Initialize();
|
|
|
|
|
|
|
|
|
|
_chrono = new System.Diagnostics.Stopwatch();
|
|
|
|
|
|
|
|
|
|
_ticksPerFrame = System.Diagnostics.Stopwatch.Frequency / TargetFps;
|
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
|
AddEvents((int)(EventMask.ButtonPressMask
|
|
|
|
|
| EventMask.ButtonReleaseMask
|
|
|
|
|
| EventMask.PointerMotionMask
|
|
|
|
|
| EventMask.KeyPressMask
|
|
|
|
|
| EventMask.KeyReleaseMask));
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
|
|
|
|
this.Shown += Renderer_Shown;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-14 17:36:56 -04:00
|
|
|
|
private static GraphicsMode GetGraphicsMode()
|
|
|
|
|
{
|
2020-05-02 22:00:53 -04:00
|
|
|
|
return Environment.OSVersion.Platform == PlatformID.Unix ? new GraphicsMode(new ColorFormat(24)) : new GraphicsMode(new ColorFormat());
|
2020-03-14 17:36:56 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-13 12:43:29 -05:00
|
|
|
|
private void GLRenderer_ShuttingDown(object sender, EventArgs args)
|
|
|
|
|
{
|
2020-02-14 05:52:34 -05:00
|
|
|
|
_device.DisposeGpu();
|
2020-02-13 12:43:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-11 19:56:19 -05:00
|
|
|
|
private void Parent_FocusOutEvent(object o, Gtk.FocusOutEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
IsFocused = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Parent_FocusInEvent(object o, Gtk.FocusInEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
IsFocused = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GLRenderer_Destroyed(object sender, EventArgs e)
|
|
|
|
|
{
|
2020-02-13 12:43:29 -05:00
|
|
|
|
Dispose();
|
2020-02-11 19:56:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void Renderer_Shown(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
IsFocused = this.ParentWindow.State.HasFlag(Gdk.WindowState.Focused);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void HandleScreenState(KeyboardState keyboard)
|
|
|
|
|
{
|
2020-05-02 22:00:53 -04:00
|
|
|
|
bool toggleFullscreen = keyboard.IsKeyDown(OpenTK.Input.Key.F11)
|
2020-02-18 06:34:57 -05:00
|
|
|
|
|| ((keyboard.IsKeyDown(OpenTK.Input.Key.AltLeft)
|
|
|
|
|
|| keyboard.IsKeyDown(OpenTK.Input.Key.AltRight))
|
2020-02-13 12:43:29 -05:00
|
|
|
|
&& keyboard.IsKeyDown(OpenTK.Input.Key.Enter))
|
2020-05-02 22:00:53 -04:00
|
|
|
|
|| keyboard.IsKeyDown(OpenTK.Input.Key.Escape);
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-02-13 12:43:29 -05:00
|
|
|
|
bool fullScreenToggled = ParentWindow.State.HasFlag(Gdk.WindowState.Fullscreen);
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-02-13 12:43:29 -05:00
|
|
|
|
if (toggleFullscreen != _toggleFullscreen)
|
2020-02-11 19:56:19 -05:00
|
|
|
|
{
|
2020-02-13 12:43:29 -05:00
|
|
|
|
if (toggleFullscreen)
|
2020-02-11 19:56:19 -05:00
|
|
|
|
{
|
2020-02-13 12:43:29 -05:00
|
|
|
|
if (fullScreenToggled)
|
2020-02-11 19:56:19 -05:00
|
|
|
|
{
|
2020-02-13 12:43:29 -05:00
|
|
|
|
ParentWindow.Unfullscreen();
|
|
|
|
|
(Toplevel as MainWindow)?.ToggleExtraWidgets(true);
|
2020-02-11 19:56:19 -05:00
|
|
|
|
}
|
2020-02-13 12:43:29 -05:00
|
|
|
|
else
|
2020-02-11 19:56:19 -05:00
|
|
|
|
{
|
2020-02-13 12:43:29 -05:00
|
|
|
|
if (keyboard.IsKeyDown(OpenTK.Input.Key.Escape))
|
|
|
|
|
{
|
2020-03-29 08:47:37 -04:00
|
|
|
|
if (GtkDialog.CreateExitDialog())
|
|
|
|
|
{
|
|
|
|
|
Exit();
|
|
|
|
|
}
|
2020-02-13 12:43:29 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ParentWindow.Fullscreen();
|
|
|
|
|
(Toplevel as MainWindow)?.ToggleExtraWidgets(false);
|
|
|
|
|
}
|
2020-02-11 19:56:19 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-13 12:43:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_toggleFullscreen = toggleFullscreen;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GLRenderer_Initialized(object sender, EventArgs e)
|
|
|
|
|
{
|
2020-02-14 05:52:34 -05:00
|
|
|
|
// Release the GL exclusivity that OpenTK gave us as we aren't going to use it in GTK Thread.
|
2020-02-11 19:56:19 -05:00
|
|
|
|
GraphicsContext.MakeCurrent(null);
|
|
|
|
|
|
|
|
|
|
WaitEvent.Set();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnConfigureEvent(EventConfigure evnt)
|
|
|
|
|
{
|
2020-05-02 22:00:53 -04:00
|
|
|
|
bool result = base.OnConfigureEvent(evnt);
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-02-18 06:34:57 -05:00
|
|
|
|
Gdk.Monitor monitor = Display.GetMonitorAtWindow(Window);
|
|
|
|
|
|
|
|
|
|
_renderer.Window.SetSize(evnt.Width * monitor.ScaleFactor, evnt.Height * monitor.ScaleFactor);
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
|
|
|
|
IsRenderHandler = true;
|
|
|
|
|
|
|
|
|
|
_chrono.Restart();
|
|
|
|
|
|
|
|
|
|
IsActive = true;
|
|
|
|
|
|
|
|
|
|
Gtk.Window parent = this.Toplevel as Gtk.Window;
|
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
|
parent.FocusInEvent += Parent_FocusInEvent;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
parent.FocusOutEvent += Parent_FocusOutEvent;
|
|
|
|
|
|
|
|
|
|
Gtk.Application.Invoke(delegate
|
|
|
|
|
{
|
|
|
|
|
parent.Present();
|
2020-03-06 21:40:06 -05:00
|
|
|
|
|
2020-05-15 02:16:46 -04:00
|
|
|
|
string titleNameSection = string.IsNullOrWhiteSpace(_device.Application.TitleName) ? string.Empty
|
|
|
|
|
: $" - {_device.Application.TitleName}";
|
2020-04-12 17:02:37 -04:00
|
|
|
|
|
2020-06-16 14:28:02 -04:00
|
|
|
|
string titleVersionSection = string.IsNullOrWhiteSpace(_device.Application.DisplayVersion) ? string.Empty
|
|
|
|
|
: $" v{_device.Application.DisplayVersion}";
|
2020-03-06 21:40:06 -05:00
|
|
|
|
|
2020-05-15 02:16:46 -04:00
|
|
|
|
string titleIdSection = string.IsNullOrWhiteSpace(_device.Application.TitleIdText) ? string.Empty
|
|
|
|
|
: $" ({_device.Application.TitleIdText.ToUpper()})";
|
2020-04-12 17:02:37 -04:00
|
|
|
|
|
2020-05-15 02:16:46 -04:00
|
|
|
|
string titleArchSection = _device.Application.TitleIs64Bit ? " (64-bit)" : " (32-bit)";
|
2020-03-06 21:40:06 -05:00
|
|
|
|
|
2020-04-12 17:02:37 -04:00
|
|
|
|
parent.Title = $"Ryujinx {Program.Version}{titleNameSection}{titleVersionSection}{titleIdSection}{titleArchSection}";
|
2020-02-11 19:56:19 -05:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Thread renderLoopThread = new Thread(Render)
|
|
|
|
|
{
|
|
|
|
|
Name = "GUI.RenderLoop"
|
|
|
|
|
};
|
|
|
|
|
renderLoopThread.Start();
|
|
|
|
|
|
|
|
|
|
MainLoop();
|
|
|
|
|
|
|
|
|
|
renderLoopThread.Join();
|
|
|
|
|
|
|
|
|
|
Exit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnButtonPressEvent(EventButton evnt)
|
|
|
|
|
{
|
|
|
|
|
_mouseX = evnt.X;
|
|
|
|
|
_mouseY = evnt.Y;
|
|
|
|
|
|
|
|
|
|
if (evnt.Button == 1)
|
|
|
|
|
{
|
|
|
|
|
_mousePressed = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnButtonReleaseEvent(EventButton evnt)
|
|
|
|
|
{
|
|
|
|
|
if (evnt.Button == 1)
|
|
|
|
|
{
|
|
|
|
|
_mousePressed = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnMotionNotifyEvent(EventMotion evnt)
|
|
|
|
|
{
|
|
|
|
|
if (evnt.Device.InputSource == InputSource.Mouse)
|
|
|
|
|
{
|
|
|
|
|
_mouseX = evnt.X;
|
|
|
|
|
_mouseY = evnt.Y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 06:34:57 -05:00
|
|
|
|
protected override void OnGetPreferredHeight(out int minimumHeight, out int naturalHeight)
|
|
|
|
|
{
|
|
|
|
|
Gdk.Monitor monitor = Display.GetMonitorAtWindow(Window);
|
|
|
|
|
|
|
|
|
|
// If the monitor is at least 1080p, use the Switch panel size as minimal size.
|
|
|
|
|
if (monitor.Geometry.Height >= 1080)
|
|
|
|
|
{
|
|
|
|
|
minimumHeight = SwitchPanelHeight;
|
|
|
|
|
}
|
|
|
|
|
// Otherwise, we default minimal size to 480p 16:9.
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
minimumHeight = 480;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
naturalHeight = minimumHeight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnGetPreferredWidth(out int minimumWidth, out int naturalWidth)
|
|
|
|
|
{
|
|
|
|
|
Gdk.Monitor monitor = Display.GetMonitorAtWindow(Window);
|
|
|
|
|
|
|
|
|
|
// If the monitor is at least 1080p, use the Switch panel size as minimal size.
|
|
|
|
|
if (monitor.Geometry.Height >= 1080)
|
|
|
|
|
{
|
|
|
|
|
minimumWidth = SwitchPanelWidth;
|
|
|
|
|
}
|
|
|
|
|
// Otherwise, we default minimal size to 480p 16:9.
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
minimumWidth = 854;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
naturalWidth = minimumWidth;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-11 19:56:19 -05:00
|
|
|
|
public void Exit()
|
|
|
|
|
{
|
|
|
|
|
if (IsStopped)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IsStopped = true;
|
2020-02-14 05:52:34 -05:00
|
|
|
|
IsActive = false;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Initialize()
|
|
|
|
|
{
|
|
|
|
|
if (!(_device.Gpu.Renderer is Renderer))
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException($"GPU renderer must be an OpenGL renderer when using GLRenderer!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_renderer = (Renderer)_device.Gpu.Renderer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Render()
|
|
|
|
|
{
|
2020-02-14 05:52:34 -05:00
|
|
|
|
// First take exclusivity on the OpenGL context.
|
|
|
|
|
GraphicsContext.MakeCurrent(WindowInfo);
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-02-14 05:52:34 -05:00
|
|
|
|
_renderer.Initialize();
|
|
|
|
|
|
|
|
|
|
// Make sure the first frame is not transparent.
|
|
|
|
|
GL.ClearColor(OpenTK.Color.Black);
|
|
|
|
|
GL.Clear(ClearBufferMask.ColorBufferBit);
|
|
|
|
|
SwapBuffers();
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
|
|
|
|
while (IsActive)
|
|
|
|
|
{
|
|
|
|
|
if (IsStopped)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 05:52:34 -05:00
|
|
|
|
_ticks += _chrono.ElapsedTicks;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-02-14 05:52:34 -05:00
|
|
|
|
_chrono.Restart();
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-02-14 05:52:34 -05:00
|
|
|
|
if (_device.WaitFifo())
|
|
|
|
|
{
|
|
|
|
|
_device.ProcessFrame();
|
|
|
|
|
}
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-05-04 18:10:01 -04:00
|
|
|
|
string dockedMode = ConfigurationState.Instance.System.EnableDockedMode ? "Docked" : "Handheld";
|
|
|
|
|
|
2020-02-14 05:52:34 -05:00
|
|
|
|
if (_ticks >= _ticksPerFrame)
|
|
|
|
|
{
|
|
|
|
|
_device.PresentFrame(SwapBuffers);
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-02-14 05:52:34 -05:00
|
|
|
|
_device.Statistics.RecordSystemFrameTime();
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-03-06 21:40:06 -05:00
|
|
|
|
StatusUpdatedEvent?.Invoke(this, new StatusUpdatedEventArgs(
|
2020-05-04 18:10:01 -04:00
|
|
|
|
_device.EnableDeviceVsync,
|
|
|
|
|
dockedMode,
|
|
|
|
|
$"Host: {_device.Statistics.GetSystemFrameRate():00.00} FPS",
|
2020-03-24 17:54:09 -04:00
|
|
|
|
$"Game: {_device.Statistics.GetGameFrameRate():00.00} FPS",
|
2020-05-02 22:00:53 -04:00
|
|
|
|
$"GPU: {_renderer.GpuVendor}"));
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-02-14 05:52:34 -05:00
|
|
|
|
_ticks = Math.Min(_ticks - _ticksPerFrame, _ticksPerFrame);
|
2020-02-11 19:56:19 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SwapBuffers()
|
|
|
|
|
{
|
|
|
|
|
OpenTK.Graphics.GraphicsContext.CurrentContext.SwapBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void MainLoop()
|
|
|
|
|
{
|
|
|
|
|
while (IsActive)
|
|
|
|
|
{
|
2020-02-15 06:30:53 -05:00
|
|
|
|
UpdateFrame();
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
|
|
|
|
// Polling becomes expensive if it's not slept
|
|
|
|
|
Thread.Sleep(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool UpdateFrame()
|
|
|
|
|
{
|
|
|
|
|
if (!IsActive)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (IsStopped)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 06:30:53 -05:00
|
|
|
|
if (IsFocused)
|
2020-02-11 19:56:19 -05:00
|
|
|
|
{
|
2020-02-15 06:30:53 -05:00
|
|
|
|
Gtk.Application.Invoke(delegate
|
|
|
|
|
{
|
2020-06-16 14:28:02 -04:00
|
|
|
|
KeyboardState keyboard = OpenTK.Input.Keyboard.GetState();
|
|
|
|
|
|
|
|
|
|
HandleScreenState(keyboard);
|
|
|
|
|
|
|
|
|
|
if (keyboard.IsKeyDown(OpenTK.Input.Key.Delete))
|
|
|
|
|
{
|
|
|
|
|
if (!ParentWindow.State.HasFlag(Gdk.WindowState.Fullscreen))
|
|
|
|
|
{
|
|
|
|
|
Ptc.Continue();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-15 06:30:53 -05:00
|
|
|
|
});
|
2020-05-02 22:00:53 -04:00
|
|
|
|
}
|
2020-02-15 06:30:53 -05:00
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
|
List<GamepadInput> gamepadInputs = new List<GamepadInput>();
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-06-22 16:19:30 -04:00
|
|
|
|
foreach (InputConfig inputConfig in ConfigurationState.Instance.Hid.InputConfig.Value.ToArray())
|
2020-05-02 22:00:53 -04:00
|
|
|
|
{
|
|
|
|
|
ControllerKeys currentButton = 0;
|
|
|
|
|
JoystickPosition leftJoystick = new JoystickPosition();
|
|
|
|
|
JoystickPosition rightJoystick = new JoystickPosition();
|
|
|
|
|
KeyboardInput? hidKeyboard = null;
|
|
|
|
|
|
|
|
|
|
int leftJoystickDx = 0;
|
|
|
|
|
int leftJoystickDy = 0;
|
|
|
|
|
int rightJoystickDx = 0;
|
|
|
|
|
int rightJoystickDy = 0;
|
|
|
|
|
|
|
|
|
|
if (inputConfig is KeyboardConfig keyboardConfig)
|
2020-02-15 06:30:53 -05:00
|
|
|
|
{
|
2020-05-02 22:00:53 -04:00
|
|
|
|
if (IsFocused)
|
|
|
|
|
{
|
|
|
|
|
// Keyboard Input
|
|
|
|
|
KeyboardController keyboardController = new KeyboardController(keyboardConfig);
|
2020-02-15 06:30:53 -05:00
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
|
currentButton = keyboardController.GetButtons();
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
|
(leftJoystickDx, leftJoystickDy) = keyboardController.GetLeftStick();
|
|
|
|
|
(rightJoystickDx, rightJoystickDy) = keyboardController.GetRightStick();
|
|
|
|
|
|
|
|
|
|
leftJoystick = new JoystickPosition
|
|
|
|
|
{
|
|
|
|
|
Dx = leftJoystickDx,
|
|
|
|
|
Dy = leftJoystickDy
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
rightJoystick = new JoystickPosition
|
|
|
|
|
{
|
|
|
|
|
Dx = rightJoystickDx,
|
|
|
|
|
Dy = rightJoystickDy
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (ConfigurationState.Instance.Hid.EnableKeyboard)
|
|
|
|
|
{
|
|
|
|
|
hidKeyboard = keyboardController.GetKeysDown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!hidKeyboard.HasValue)
|
|
|
|
|
{
|
|
|
|
|
hidKeyboard = new KeyboardInput
|
|
|
|
|
{
|
|
|
|
|
Modifier = 0,
|
|
|
|
|
Keys = new int[0x8]
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ConfigurationState.Instance.Hid.EnableKeyboard)
|
|
|
|
|
{
|
|
|
|
|
_device.Hid.Keyboard.Update(hidKeyboard.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (inputConfig is Common.Configuration.Hid.ControllerConfig controllerConfig)
|
2020-02-11 19:56:19 -05:00
|
|
|
|
{
|
2020-05-02 22:00:53 -04:00
|
|
|
|
// Controller Input
|
|
|
|
|
JoystickController joystickController = new JoystickController(controllerConfig);
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
|
currentButton |= joystickController.GetButtons();
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
|
(leftJoystickDx, leftJoystickDy) = joystickController.GetLeftStick();
|
|
|
|
|
(rightJoystickDx, rightJoystickDy) = joystickController.GetRightStick();
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
|
leftJoystick = new JoystickPosition
|
|
|
|
|
{
|
|
|
|
|
Dx = controllerConfig.LeftJoycon.InvertStickX ? -leftJoystickDx : leftJoystickDx,
|
|
|
|
|
Dy = controllerConfig.LeftJoycon.InvertStickY ? -leftJoystickDy : leftJoystickDy
|
|
|
|
|
};
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
|
rightJoystick = new JoystickPosition
|
|
|
|
|
{
|
|
|
|
|
Dx = controllerConfig.RightJoycon.InvertStickX ? -rightJoystickDx : rightJoystickDx,
|
|
|
|
|
Dy = controllerConfig.RightJoycon.InvertStickY ? -rightJoystickDy : rightJoystickDy
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
|
currentButton |= _device.Hid.UpdateStickButtons(leftJoystick, rightJoystick);
|
|
|
|
|
|
|
|
|
|
gamepadInputs.Add(new GamepadInput
|
|
|
|
|
{
|
|
|
|
|
PlayerId = (HLE.HOS.Services.Hid.PlayerIndex)inputConfig.PlayerIndex,
|
|
|
|
|
Buttons = currentButton,
|
|
|
|
|
LStick = leftJoystick,
|
|
|
|
|
RStick = rightJoystick
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
|
_device.Hid.Npads.SetGamepadsInput(gamepadInputs.ToArray());
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2020-06-26 06:30:16 -04:00
|
|
|
|
// Hotkeys
|
|
|
|
|
HotkeyButtons currentHotkeyButtons = KeyboardController.GetHotkeyButtons(OpenTK.Input.Keyboard.GetState());
|
|
|
|
|
|
|
|
|
|
if (currentHotkeyButtons.HasFlag(HotkeyButtons.ToggleVSync) &&
|
|
|
|
|
!_prevHotkeyButtons.HasFlag(HotkeyButtons.ToggleVSync))
|
|
|
|
|
{
|
|
|
|
|
_device.EnableDeviceVsync = !_device.EnableDeviceVsync;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_prevHotkeyButtons = currentHotkeyButtons;
|
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
|
//Touchscreen
|
2020-02-11 19:56:19 -05:00
|
|
|
|
bool hasTouch = false;
|
|
|
|
|
|
|
|
|
|
// Get screen touch position from left mouse click
|
|
|
|
|
// OpenTK always captures mouse events, even if out of focus, so check if window is focused.
|
|
|
|
|
if (IsFocused && _mousePressed)
|
|
|
|
|
{
|
2020-05-02 22:00:53 -04:00
|
|
|
|
int screenWidth = AllocatedWidth;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
int screenHeight = AllocatedHeight;
|
|
|
|
|
|
2020-02-18 06:34:57 -05:00
|
|
|
|
if (AllocatedWidth > (AllocatedHeight * SwitchPanelWidth) / SwitchPanelHeight)
|
2020-02-11 19:56:19 -05:00
|
|
|
|
{
|
2020-02-18 06:34:57 -05:00
|
|
|
|
screenWidth = (AllocatedHeight * SwitchPanelWidth) / SwitchPanelHeight;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-02-18 06:34:57 -05:00
|
|
|
|
screenHeight = (AllocatedWidth * SwitchPanelHeight) / SwitchPanelWidth;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
|
int startX = (AllocatedWidth - screenWidth) >> 1;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
int startY = (AllocatedHeight - screenHeight) >> 1;
|
|
|
|
|
|
|
|
|
|
int endX = startX + screenWidth;
|
|
|
|
|
int endY = startY + screenHeight;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (_mouseX >= startX &&
|
|
|
|
|
_mouseY >= startY &&
|
|
|
|
|
_mouseX < endX &&
|
|
|
|
|
_mouseY < endY)
|
|
|
|
|
{
|
|
|
|
|
int screenMouseX = (int)_mouseX - startX;
|
|
|
|
|
int screenMouseY = (int)_mouseY - startY;
|
|
|
|
|
|
2020-02-18 06:34:57 -05:00
|
|
|
|
int mX = (screenMouseX * SwitchPanelWidth) / screenWidth;
|
|
|
|
|
int mY = (screenMouseY * SwitchPanelHeight) / screenHeight;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
|
|
|
|
TouchPoint currentPoint = new TouchPoint
|
|
|
|
|
{
|
2020-04-02 20:10:02 -04:00
|
|
|
|
X = (uint)mX,
|
|
|
|
|
Y = (uint)mY,
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
|
|
|
|
// Placeholder values till more data is acquired
|
|
|
|
|
DiameterX = 10,
|
|
|
|
|
DiameterY = 10,
|
2020-05-02 22:00:53 -04:00
|
|
|
|
Angle = 90
|
2020-02-11 19:56:19 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
hasTouch = true;
|
|
|
|
|
|
2020-04-02 20:10:02 -04:00
|
|
|
|
_device.Hid.Touchscreen.Update(currentPoint);
|
2020-02-11 19:56:19 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!hasTouch)
|
|
|
|
|
{
|
2020-04-02 20:10:02 -04:00
|
|
|
|
_device.Hid.Touchscreen.Update();
|
2020-02-11 19:56:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-02 20:10:02 -04:00
|
|
|
|
_device.Hid.DebugPad.Update();
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|