2021-10-12 12:54:21 -07:00
|
|
|
|
using Ryujinx.HLE.Ui;
|
|
|
|
|
using Ryujinx.Memory;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2021-11-28 12:24:17 -08:00
|
|
|
|
/// Class that manages the renderer base class and its state in a multithreaded context.
|
2021-10-12 12:54:21 -07:00
|
|
|
|
/// </summary>
|
|
|
|
|
internal class SoftwareKeyboardRenderer : IDisposable
|
|
|
|
|
{
|
2021-11-28 12:24:17 -08:00
|
|
|
|
private const int TextBoxBlinkSleepMilliseconds = 100;
|
|
|
|
|
private const int RendererWaitTimeoutMilliseconds = 100;
|
2021-10-12 12:54:21 -07:00
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
private readonly object _stateLock = new object();
|
2021-10-12 12:54:21 -07:00
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
private SoftwareKeyboardUiState _state = new SoftwareKeyboardUiState();
|
|
|
|
|
private SoftwareKeyboardRendererBase _renderer;
|
2021-10-12 12:54:21 -07:00
|
|
|
|
|
|
|
|
|
private TimedAction _textBoxBlinkTimedAction = new TimedAction();
|
2021-11-28 12:24:17 -08:00
|
|
|
|
private TimedAction _renderAction = new TimedAction();
|
2021-10-12 12:54:21 -07:00
|
|
|
|
|
|
|
|
|
public SoftwareKeyboardRenderer(IHostUiTheme uiTheme)
|
|
|
|
|
{
|
2021-11-28 12:24:17 -08:00
|
|
|
|
_renderer = new SoftwareKeyboardRendererBase(uiTheme);
|
2021-10-12 12:54:21 -07:00
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
StartTextBoxBlinker(_textBoxBlinkTimedAction, _state, _stateLock);
|
|
|
|
|
StartRenderer(_renderAction, _renderer, _state, _stateLock);
|
2021-10-12 12:54:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
private static void StartTextBoxBlinker(TimedAction timedAction, SoftwareKeyboardUiState state, object stateLock)
|
2021-10-12 12:54:21 -07:00
|
|
|
|
{
|
|
|
|
|
timedAction.Reset(() =>
|
|
|
|
|
{
|
2021-11-28 12:24:17 -08:00
|
|
|
|
lock (stateLock)
|
2021-10-12 12:54:21 -07:00
|
|
|
|
{
|
2021-11-28 12:24:17 -08:00
|
|
|
|
// The blinker is on half of the time and events such as input
|
|
|
|
|
// changes can reset the blinker.
|
|
|
|
|
state.TextBoxBlinkCounter = (state.TextBoxBlinkCounter + 1) % (2 * SoftwareKeyboardRendererBase.TextBoxBlinkThreshold);
|
2021-10-12 12:54:21 -07:00
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
// Tell the render thread there is something new to render.
|
|
|
|
|
Monitor.PulseAll(stateLock);
|
|
|
|
|
}
|
|
|
|
|
}, TextBoxBlinkSleepMilliseconds);
|
2021-10-12 12:54:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
private static void StartRenderer(TimedAction timedAction, SoftwareKeyboardRendererBase renderer, SoftwareKeyboardUiState state, object stateLock)
|
2021-10-12 12:54:21 -07:00
|
|
|
|
{
|
2021-11-28 12:24:17 -08:00
|
|
|
|
SoftwareKeyboardUiState internalState = new SoftwareKeyboardUiState();
|
2021-10-12 12:54:21 -07:00
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
bool canCreateSurface = false;
|
|
|
|
|
bool needsUpdate = true;
|
2021-10-12 12:54:21 -07:00
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
timedAction.Reset(() =>
|
2021-10-12 12:54:21 -07:00
|
|
|
|
{
|
2021-11-28 12:24:17 -08:00
|
|
|
|
lock (stateLock)
|
2021-10-12 12:54:21 -07:00
|
|
|
|
{
|
2021-11-28 12:24:17 -08:00
|
|
|
|
if (!Monitor.Wait(stateLock, RendererWaitTimeoutMilliseconds))
|
2021-10-12 12:54:21 -07:00
|
|
|
|
{
|
2021-11-28 12:24:17 -08:00
|
|
|
|
return;
|
2021-10-12 12:54:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
needsUpdate = UpdateStateField(ref state.InputText, ref internalState.InputText);
|
|
|
|
|
needsUpdate |= UpdateStateField(ref state.CursorBegin, ref internalState.CursorBegin);
|
|
|
|
|
needsUpdate |= UpdateStateField(ref state.CursorEnd, ref internalState.CursorEnd);
|
|
|
|
|
needsUpdate |= UpdateStateField(ref state.AcceptPressed, ref internalState.AcceptPressed);
|
|
|
|
|
needsUpdate |= UpdateStateField(ref state.CancelPressed, ref internalState.CancelPressed);
|
|
|
|
|
needsUpdate |= UpdateStateField(ref state.OverwriteMode, ref internalState.OverwriteMode);
|
|
|
|
|
needsUpdate |= UpdateStateField(ref state.TypingEnabled, ref internalState.TypingEnabled);
|
|
|
|
|
needsUpdate |= UpdateStateField(ref state.ControllerEnabled, ref internalState.ControllerEnabled);
|
|
|
|
|
needsUpdate |= UpdateStateField(ref state.TextBoxBlinkCounter, ref internalState.TextBoxBlinkCounter);
|
2021-10-12 12:54:21 -07:00
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
canCreateSurface = state.SurfaceInfo != null && internalState.SurfaceInfo == null;
|
2021-10-12 12:54:21 -07:00
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
if (canCreateSurface)
|
2021-10-12 12:54:21 -07:00
|
|
|
|
{
|
2021-11-28 12:24:17 -08:00
|
|
|
|
internalState.SurfaceInfo = state.SurfaceInfo;
|
2021-10-12 12:54:21 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
if (canCreateSurface)
|
2021-10-12 12:54:21 -07:00
|
|
|
|
{
|
2021-11-28 12:24:17 -08:00
|
|
|
|
renderer.CreateSurface(internalState.SurfaceInfo);
|
2021-10-12 12:54:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
if (needsUpdate)
|
|
|
|
|
{
|
|
|
|
|
renderer.DrawMutableElements(internalState);
|
|
|
|
|
renderer.CopyImageToBuffer();
|
|
|
|
|
needsUpdate = false;
|
2021-10-12 12:54:21 -07:00
|
|
|
|
}
|
2021-11-28 12:24:17 -08:00
|
|
|
|
});
|
2021-10-12 12:54:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
private static bool UpdateStateField<T>(ref T source, ref T destination) where T : IEquatable<T>
|
2021-10-12 12:54:21 -07:00
|
|
|
|
{
|
2021-11-28 12:24:17 -08:00
|
|
|
|
if (!source.Equals(destination))
|
2021-10-12 12:54:21 -07:00
|
|
|
|
{
|
2021-11-28 12:24:17 -08:00
|
|
|
|
destination = source;
|
|
|
|
|
return true;
|
2021-10-12 12:54:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
return false;
|
2021-10-12 12:54:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
#pragma warning disable CS8632
|
|
|
|
|
public void UpdateTextState(string? inputText, int? cursorBegin, int? cursorEnd, bool? overwriteMode, bool? typingEnabled)
|
|
|
|
|
#pragma warning restore CS8632
|
2021-10-12 12:54:21 -07:00
|
|
|
|
{
|
2021-11-28 12:24:17 -08:00
|
|
|
|
lock (_stateLock)
|
2021-10-12 12:54:21 -07:00
|
|
|
|
{
|
2021-11-28 12:24:17 -08:00
|
|
|
|
// Update the parameters that were provided.
|
|
|
|
|
_state.InputText = inputText != null ? inputText : _state.InputText;
|
|
|
|
|
_state.CursorBegin = cursorBegin.GetValueOrDefault(_state.CursorBegin);
|
|
|
|
|
_state.CursorEnd = cursorEnd.GetValueOrDefault(_state.CursorEnd);
|
|
|
|
|
_state.OverwriteMode = overwriteMode.GetValueOrDefault(_state.OverwriteMode);
|
|
|
|
|
_state.TypingEnabled = typingEnabled.GetValueOrDefault(_state.TypingEnabled);
|
2021-11-01 15:38:13 -07:00
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
// Reset the cursor blink.
|
|
|
|
|
_state.TextBoxBlinkCounter = 0;
|
2021-11-01 15:38:13 -07:00
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
// Tell the render thread there is something new to render.
|
|
|
|
|
Monitor.PulseAll(_stateLock);
|
2021-10-12 12:54:21 -07:00
|
|
|
|
}
|
2021-11-28 12:24:17 -08:00
|
|
|
|
}
|
2021-10-12 12:54:21 -07:00
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
public void UpdateCommandState(bool? acceptPressed, bool? cancelPressed, bool? controllerEnabled)
|
|
|
|
|
{
|
|
|
|
|
lock (_stateLock)
|
2021-10-12 12:54:21 -07:00
|
|
|
|
{
|
2021-11-28 12:24:17 -08:00
|
|
|
|
// Update the parameters that were provided.
|
|
|
|
|
_state.AcceptPressed = acceptPressed.GetValueOrDefault(_state.AcceptPressed);
|
|
|
|
|
_state.CancelPressed = cancelPressed.GetValueOrDefault(_state.CancelPressed);
|
|
|
|
|
_state.ControllerEnabled = controllerEnabled.GetValueOrDefault(_state.ControllerEnabled);
|
2021-10-12 12:54:21 -07:00
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
// Tell the render thread there is something new to render.
|
|
|
|
|
Monitor.PulseAll(_stateLock);
|
|
|
|
|
}
|
2021-10-12 12:54:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
public void SetSurfaceInfo(RenderingSurfaceInfo surfaceInfo)
|
2021-10-12 12:54:21 -07:00
|
|
|
|
{
|
2021-11-28 12:24:17 -08:00
|
|
|
|
lock (_stateLock)
|
2021-10-12 12:54:21 -07:00
|
|
|
|
{
|
2021-11-28 12:24:17 -08:00
|
|
|
|
_state.SurfaceInfo = surfaceInfo;
|
2021-10-12 12:54:21 -07:00
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
// Tell the render thread there is something new to render.
|
|
|
|
|
Monitor.PulseAll(_stateLock);
|
2021-10-12 12:54:21 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 12:24:17 -08:00
|
|
|
|
internal bool DrawTo(IVirtualMemoryManager destination, ulong position)
|
|
|
|
|
{
|
|
|
|
|
return _renderer.WriteBufferToMemory(destination, position);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 12:54:21 -07:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_textBoxBlinkTimedAction.RequestCancel();
|
2021-11-28 12:24:17 -08:00
|
|
|
|
_renderAction.RequestCancel();
|
2021-10-12 12:54:21 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|