Inline software keyboard without input pop up dialog (#2180)

* Initial implementation

* Refactor dynamic text input keys out to facilitate configuration via UI

* Fix code styling

* Add per applet indirect layer handles

* Remove static functions from SoftwareKeyboardRenderer

* Remove inline keyboard reset delay

* Remove inline keyboard V2 responses

* Add inline keyboard soft-lock recovering

* Add comments

* Forward accept and cancel key names to the keyboard and add soft-lock prevention line

* Add dummy window to handle paste events

* Rework inline keyboard state machine and graphics

* Implement IHostUiHandler interfaces on headless WindowBase class

* Add inline keyboard assets

* Fix coding style

* Fix coding style

* Change mode cycling shortcut to F6

* Fix invalid calc size error in games using extended calc

* Remove unnecessary namespaces
This commit is contained in:
Caian Benedicto 2021-10-12 16:54:21 -03:00 committed by GitHub
parent 69093cf2d6
commit 380b95bc59
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
47 changed files with 2853 additions and 344 deletions

View file

@ -11,23 +11,23 @@
Uninitialized = 0x0,
/// <summary>
/// A Calc was previously received and fulfilled, so the software keyboard is initialized, but is not processing input.
/// The software keyboard is initialized, but it is not visible and not processing input.
/// </summary>
Initialized = 0x1,
/// <summary>
/// A Calc was received and the software keyboard is processing input.
/// The software keyboard is transitioning to a visible state.
/// </summary>
Ready = 0x2,
Appearing = 0x2,
/// <summary>
/// New text data or cursor position of the software keyboard are available.
/// The software keyboard is visible and receiving processing input.
/// </summary>
DataAvailable = 0x3,
Shown = 0x3,
/// <summary>
/// The Calc request was fulfilled with either a text input or a cancel.
/// software keyboard is transitioning to a hidden state because the user pressed either OK or Cancel.
/// </summary>
Complete = 0x4
Disappearing = 0x4
}
}

View file

@ -38,9 +38,20 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
return (uint)text.Length; // Return the cursor position at the end of the text
}
private static void WriteStringWithCursor(string text, BinaryWriter writer, uint maxSize, Encoding encoding)
private static void WriteStringWithCursor(string text, uint cursor, BinaryWriter writer, uint maxSize, Encoding encoding, bool padMiddle)
{
uint cursor = WriteString(text, writer, maxSize, encoding);
uint length = WriteString(text, writer, maxSize, encoding);
if (cursor > length)
{
cursor = length;
}
if (padMiddle)
{
writer.Write((int)-1); // ?
writer.Write((int)-1); // ?
}
writer.Write(cursor); // Cursor position
}
@ -72,7 +83,7 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
}
}
public static byte[] ChangedString(string text, InlineKeyboardState state)
public static byte[] ChangedString(string text, uint cursor, InlineKeyboardState state)
{
uint resSize = 6 * sizeof(uint) + MaxStrLenUTF16;
@ -80,15 +91,13 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
using (BinaryWriter writer = new BinaryWriter(stream))
{
BeginResponse(state, InlineKeyboardResponse.ChangedString, writer);
WriteStringWithCursor(text, writer, MaxStrLenUTF16, Encoding.Unicode);
writer.Write((int)0); // ?
writer.Write((int)0); // ?
WriteStringWithCursor(text, cursor, writer, MaxStrLenUTF16, Encoding.Unicode, true);
return stream.ToArray();
}
}
public static byte[] MovedCursor(string text, InlineKeyboardState state)
public static byte[] MovedCursor(string text, uint cursor, InlineKeyboardState state)
{
uint resSize = 4 * sizeof(uint) + MaxStrLenUTF16;
@ -96,13 +105,13 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
using (BinaryWriter writer = new BinaryWriter(stream))
{
BeginResponse(state, InlineKeyboardResponse.MovedCursor, writer);
WriteStringWithCursor(text, writer, MaxStrLenUTF16, Encoding.Unicode);
WriteStringWithCursor(text, cursor, writer, MaxStrLenUTF16, Encoding.Unicode, false);
return stream.ToArray();
}
}
public static byte[] MovedTab(string text, InlineKeyboardState state)
public static byte[] MovedTab(string text, uint cursor, InlineKeyboardState state)
{
// Should be the same as MovedCursor.
@ -112,7 +121,7 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
using (BinaryWriter writer = new BinaryWriter(stream))
{
BeginResponse(state, InlineKeyboardResponse.MovedTab, writer);
WriteStringWithCursor(text, writer, MaxStrLenUTF16, Encoding.Unicode);
WriteStringWithCursor(text, cursor, writer, MaxStrLenUTF16, Encoding.Unicode, false);
return stream.ToArray();
}
@ -145,7 +154,7 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
}
}
public static byte[] ChangedStringUtf8(string text, InlineKeyboardState state)
public static byte[] ChangedStringUtf8(string text, uint cursor, InlineKeyboardState state)
{
uint resSize = 6 * sizeof(uint) + MaxStrLenUTF8;
@ -153,15 +162,13 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
using (BinaryWriter writer = new BinaryWriter(stream))
{
BeginResponse(state, InlineKeyboardResponse.ChangedStringUtf8, writer);
WriteStringWithCursor(text, writer, MaxStrLenUTF8, Encoding.UTF8);
writer.Write((int)0); // ?
writer.Write((int)0); // ?
WriteStringWithCursor(text, cursor, writer, MaxStrLenUTF8, Encoding.UTF8, true);
return stream.ToArray();
}
}
public static byte[] MovedCursorUtf8(string text, InlineKeyboardState state)
public static byte[] MovedCursorUtf8(string text, uint cursor, InlineKeyboardState state)
{
uint resSize = 4 * sizeof(uint) + MaxStrLenUTF8;
@ -169,7 +176,7 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
using (BinaryWriter writer = new BinaryWriter(stream))
{
BeginResponse(state, InlineKeyboardResponse.MovedCursorUtf8, writer);
WriteStringWithCursor(text, writer, MaxStrLenUTF8, Encoding.UTF8);
WriteStringWithCursor(text, cursor, writer, MaxStrLenUTF8, Encoding.UTF8, false);
return stream.ToArray();
}
@ -228,7 +235,7 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
}
}
public static byte[] ChangedStringV2(string text, InlineKeyboardState state)
public static byte[] ChangedStringV2(string text, uint cursor, InlineKeyboardState state)
{
uint resSize = 6 * sizeof(uint) + MaxStrLenUTF16 + 0x1;
@ -236,16 +243,14 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
using (BinaryWriter writer = new BinaryWriter(stream))
{
BeginResponse(state, InlineKeyboardResponse.ChangedStringV2, writer);
WriteStringWithCursor(text, writer, MaxStrLenUTF16, Encoding.Unicode);
writer.Write((int)0); // ?
writer.Write((int)0); // ?
WriteStringWithCursor(text, cursor, writer, MaxStrLenUTF16, Encoding.Unicode, true);
writer.Write((byte)0); // Flag == 0
return stream.ToArray();
}
}
public static byte[] MovedCursorV2(string text, InlineKeyboardState state)
public static byte[] MovedCursorV2(string text, uint cursor, InlineKeyboardState state)
{
uint resSize = 4 * sizeof(uint) + MaxStrLenUTF16 + 0x1;
@ -253,14 +258,14 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
using (BinaryWriter writer = new BinaryWriter(stream))
{
BeginResponse(state, InlineKeyboardResponse.MovedCursorV2, writer);
WriteStringWithCursor(text, writer, MaxStrLenUTF16, Encoding.Unicode);
WriteStringWithCursor(text, cursor, writer, MaxStrLenUTF16, Encoding.Unicode, false);
writer.Write((byte)0); // Flag == 0
return stream.ToArray();
}
}
public static byte[] ChangedStringUtf8V2(string text, InlineKeyboardState state)
public static byte[] ChangedStringUtf8V2(string text, uint cursor, InlineKeyboardState state)
{
uint resSize = 6 * sizeof(uint) + MaxStrLenUTF8 + 0x1;
@ -268,16 +273,14 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
using (BinaryWriter writer = new BinaryWriter(stream))
{
BeginResponse(state, InlineKeyboardResponse.ChangedStringUtf8V2, writer);
WriteStringWithCursor(text, writer, MaxStrLenUTF8, Encoding.UTF8);
writer.Write((int)0); // ?
writer.Write((int)0); // ?
WriteStringWithCursor(text, cursor, writer, MaxStrLenUTF8, Encoding.UTF8, true);
writer.Write((byte)0); // Flag == 0
return stream.ToArray();
}
}
public static byte[] MovedCursorUtf8V2(string text, InlineKeyboardState state)
public static byte[] MovedCursorUtf8V2(string text, uint cursor, InlineKeyboardState state)
{
uint resSize = 4 * sizeof(uint) + MaxStrLenUTF8 + 0x1;
@ -285,7 +288,7 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
using (BinaryWriter writer = new BinaryWriter(stream))
{
BeginResponse(state, InlineKeyboardResponse.MovedCursorUtf8V2, writer);
WriteStringWithCursor(text, writer, MaxStrLenUTF8, Encoding.UTF8);
WriteStringWithCursor(text, cursor, writer, MaxStrLenUTF8, Encoding.UTF8, false);
writer.Write((byte)0); // Flag == 0
return stream.ToArray();

View file

@ -0,0 +1,17 @@
using System;
namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
{
/// <summary>
/// Identifies prohibited buttons.
/// </summary>
[Flags]
enum InvalidButtonFlags : uint
{
None = 0,
AnalogStickL = 1 << 1,
AnalogStickR = 1 << 2,
ZL = 1 << 3,
ZR = 1 << 4,
}
}

View file

@ -0,0 +1,26 @@
using System;
namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
{
/// <summary>
/// Bitmask of commands encoded in the Flags field of the Calc structs.
/// </summary>
[Flags]
enum KeyboardCalcFlags : ulong
{
Initialize = 0x1,
SetVolume = 0x2,
Appear = 0x4,
SetInputText = 0x8,
SetCursorPos = 0x10,
SetUtf8Mode = 0x20,
SetKeyboardBackground = 0x100,
SetKeyboardOptions1 = 0x200,
SetKeyboardOptions2 = 0x800,
EnableSeGroup = 0x2000,
DisableSeGroup = 0x4000,
SetBackspaceEnabled = 0x8000,
AppearTrigger = 0x10000,
MustShow = Appear | SetInputText | AppearTrigger
}
}

View file

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
{
/// <summary>
/// Active input options set by the keyboard applet. These options allow keyboard
/// players to input text without conflicting with the controller mappings.
/// </summary>
enum KeyboardInputMode : uint
{
ControllerAndKeyboard,
KeyboardOnly,
ControllerOnly,
Count,
}
}

View file

@ -0,0 +1,12 @@
namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
{
/// <summary>
/// The miniaturization mode used by the keyboard in inline mode.
/// </summary>
enum KeyboardMiniaturizationMode : byte
{
None = 0,
Auto = 1,
Forced = 2
}
}

View file

@ -8,21 +8,24 @@
/// <summary>
/// A full alpha-numeric keyboard.
/// </summary>
Default,
Default = 0,
/// <summary>
/// Number pad.
/// </summary>
NumbersOnly,
NumbersOnly = 1,
/// <summary>
/// QWERTY (and variants) keyboard only.
/// ASCII characters keyboard.
/// </summary>
LettersOnly,
ASCII = 2,
/// <summary>
/// Unknown keyboard variant.
/// </summary>
Unknown
FullLatin = 3,
Alphabet = 4,
SimplifiedChinese = 5,
TraditionalChinese = 6,
Korean = 7,
LanguageSet2 = 8,
LanguageSet2Latin = 9,
}
}

View file

@ -0,0 +1,14 @@
using System;
namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
{
/// <summary>
/// The intention of the user when they finish the interaction with the keyboard.
/// </summary>
enum KeyboardResult
{
NotSet = 0,
Accept = 1,
Cancel = 2,
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1 KiB

View file

@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="32"
height="32"
viewBox="0 0 8.4666665 8.4666669"
version="1.1"
id="svg8"
inkscape:export-filename="C:\Users\caian\source\repos\Ryujinx\Ryujinx.HLE\HOS\Applets\SoftwareKeyboard\Resources\Icon_Accept.png"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96"
sodipodi:docname="buttons_ab.svg"
inkscape:version="1.0.1 (3bc2e813f5, 2020-09-07)">
<defs
id="defs2" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="15.839192"
inkscape:cx="16.591066"
inkscape:cy="14.090021"
inkscape:document-units="mm"
inkscape:current-layer="layer1"
inkscape:document-rotation="0"
showgrid="false"
units="px"
showguides="false"
inkscape:window-width="1267"
inkscape:window-height="976"
inkscape:window-x="242"
inkscape:window-y="34"
inkscape:window-maximized="0" />
<metadata
id="metadata5">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1">
<circle
style="fill:#ffffff;stroke-width:1.57002;fill-opacity:1"
id="path839"
cx="4.2333331"
cy="4.2333331"
r="4.2333331" />
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:7.02011px;line-height:1.25;font-family:sans-serif;fill:#4b4b4b;fill-opacity:1;stroke:none;stroke-width:0.376071"
x="1.9222834"
y="6.5921373"
id="text835-2"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96"
inkscape:export-filename="C:\Users\caian\source\repos\Ryujinx\Ryujinx.HLE\HOS\Applets\SoftwareKeyboard\Resources\text835-2.png"><tspan
sodipodi:role="line"
id="tspan833-9"
x="1.9222834"
y="6.5921373"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:7.02011px;font-family:Arial;-inkscape-font-specification:Arial;fill:#4b4b4b;fill-opacity:1;stroke-width:0.376071">A</tspan></text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 992 B

View file

@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="32"
height="32"
viewBox="0 0 8.4666665 8.4666669"
version="1.1"
id="svg8"
inkscape:export-filename="C:\Users\caian\source\repos\Ryujinx\Ryujinx.HLE\HOS\Applets\SoftwareKeyboard\Resources\Icon_Accept.png"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96"
sodipodi:docname="buttons_ab.svg"
inkscape:version="1.0.1 (3bc2e813f5, 2020-09-07)">
<defs
id="defs2" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="15.839192"
inkscape:cx="16.591066"
inkscape:cy="14.090021"
inkscape:document-units="mm"
inkscape:current-layer="layer1"
inkscape:document-rotation="0"
showgrid="false"
units="px"
showguides="false"
inkscape:window-width="1267"
inkscape:window-height="976"
inkscape:window-x="242"
inkscape:window-y="34"
inkscape:window-maximized="0" />
<metadata
id="metadata5">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1">
<circle
style="fill:#ffffff;stroke-width:1.57002;fill-opacity:1"
id="path839"
cx="4.2333331"
cy="4.2333331"
r="4.2333331" />
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:7.02012px;line-height:1.25;font-family:sans-serif;fill:#4b4b4b;fill-opacity:1;stroke:none;stroke-width:0.37607"
x="2.0223334"
y="6.6920195"
id="text835"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96"><tspan
sodipodi:role="line"
id="tspan833"
x="2.0223334"
y="6.6920195"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:7.02012px;font-family:Arial;-inkscape-font-specification:Arial;fill:#4b4b4b;fill-opacity:1;stroke-width:0.37607">B</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:7.02011px;line-height:1.25;font-family:sans-serif;fill:#4b4b4b;fill-opacity:1;stroke:none;stroke-width:0.376071"
x="2.0223367"
y="6.6920156"
id="text835-2"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96"
inkscape:export-filename="C:\Users\caian\source\repos\Ryujinx\Ryujinx.HLE\HOS\Applets\SoftwareKeyboard\Resources\text835-2.png"><tspan
sodipodi:role="line"
id="tspan833-9"
x="2.0223367"
y="6.6920156"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:7.02011px;font-family:Arial;-inkscape-font-specification:Arial;fill:#4b4b4b;fill-opacity:1;stroke-width:0.376071">B</tspan></text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 842 B

View file

@ -0,0 +1,108 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="32"
height="32"
viewBox="0 0 8.4666665 8.4666669"
version="1.1"
id="svg8"
inkscape:export-filename="C:\Users\caian\source\repos\Ryujinx\Ryujinx.HLE\HOS\Applets\SoftwareKeyboard\Resources\Icon_KeyF5.png"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96"
sodipodi:docname="Icon_KeyF5.svg"
inkscape:version="1.0.1 (3bc2e813f5, 2020-09-07)">
<defs
id="defs2" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="15.839192"
inkscape:cx="16.591066"
inkscape:cy="14.090021"
inkscape:document-units="mm"
inkscape:current-layer="layer1"
inkscape:document-rotation="0"
showgrid="false"
units="px"
showguides="false"
inkscape:window-width="1267"
inkscape:window-height="976"
inkscape:window-x="242"
inkscape:window-y="25"
inkscape:window-maximized="0" />
<metadata
id="metadata5">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title />
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1">
<rect
style="fill:#ffffff;stroke-width:2.21199"
id="rect837"
width="8.4666662"
height="8.4666662"
x="1.3877788e-17"
y="0" />
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:4.23333px;line-height:1.25;font-family:sans-serif;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.264578"
x="1.0762799"
y="4.2016153"
id="text835"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96"
transform="scale(0.9999825,1.0000175)"><tspan
sodipodi:role="line"
id="tspan833"
x="1.0762799"
y="4.2016153"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333px;font-family:Consolas;-inkscape-font-specification:Consolas;stroke-width:0.264578">F6</tspan></text>
<rect
style="fill:none;fill-opacity:1;stroke:#757575;stroke-width:0.26458333;stroke-opacity:1;stroke-miterlimit:4;stroke-dasharray:none"
id="rect891"
width="6.9844265"
height="6.984426"
x="0.74112016"
y="0.47653681" />
<path
style="fill:none;stroke:#757575;stroke-width:0.264583px;stroke-linecap:round;stroke-linejoin:miter;stroke-opacity:1"
d="M 0,0 0.74112016,0.47653681"
id="path895"
sodipodi:nodetypes="cc" />
<path
style="fill:none;stroke:#757575;stroke-width:0.264583px;stroke-linecap:round;stroke-linejoin:miter;stroke-opacity:1"
d="M 8.4666662,0 7.7255461,0.47653681"
id="path897"
sodipodi:nodetypes="cc" />
<path
style="fill:none;stroke:#757575;stroke-width:0.264583px;stroke-linecap:round;stroke-linejoin:miter;stroke-opacity:1"
d="M 7.3685303e-7,8.4666667 0.7411209,7.4609628"
id="path901"
sodipodi:nodetypes="cc" />
<path
style="fill:none;stroke:#757575;stroke-width:0.264583px;stroke-linecap:round;stroke-linejoin:miter;stroke-opacity:1"
d="M 8.4666669,8.4666667 7.7255468,7.4609628"
id="path903"
sodipodi:nodetypes="cc" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 3.8 KiB

View file

@ -5,16 +5,12 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
/// <summary>
/// A structure with appearance configurations for the software keyboard when running in inline mode.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
struct SoftwareKeyboardAppear
{
private const int OkTextLength = 8;
public const int OkTextLength = SoftwareKeyboardAppearEx.OkTextLength;
/// <summary>
/// Some games send a Calc without intention of showing the keyboard, a
/// common trend observed is that this field will be != 0 in such cases.
/// </summary>
public uint ShouldBeHidden;
public KeyboardMode KeyboardMode;
/// <summary>
/// The string displayed in the Submit button.
@ -38,15 +34,26 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
[MarshalAs(UnmanagedType.I1)]
public bool PredictionEnabled;
public byte Empty;
/// <summary>
/// When set, there is only the option to accept the input.
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool CancelButtonDisabled;
/// <summary>
/// Specifies prohibited characters that cannot be input into the text entry area.
/// </summary>
public InvalidCharFlags InvalidCharFlag;
public InvalidCharFlags InvalidChars;
public int Padding1;
public int Padding2;
/// <summary>
/// Maximum text length allowed.
/// </summary>
public int TextMaxLength;
/// <summary>
/// Minimum text length allowed.
/// </summary>
public int TextMinLength;
/// <summary>
/// Indicates the return button is enabled in the keyboard. This allows for input with multiple lines.
@ -57,21 +64,56 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
/// <summary>
/// [10.0.0+] If value is 1 or 2, then keytopAsFloating=0 and footerScalable=1 in Calc.
/// </summary>
public byte Unknown1;
public KeyboardMiniaturizationMode MiniaturizationMode;
public byte Padding4;
public byte Padding5;
public byte Reserved1;
public byte Reserved2;
/// <summary>
/// Bitmask 0x1000 of the Calc and DirectionalButtonAssignEnabled in bitmask 0x10000000.
/// Bit field with invalid buttons for the keyboard.
/// </summary>
public uint CalcFlags;
public InvalidButtonFlags InvalidButtons;
public uint Padding6;
public uint Padding7;
public uint Padding8;
public uint Padding9;
public uint Padding10;
public uint Padding11;
[MarshalAs(UnmanagedType.I1)]
public bool UseSaveData;
public uint Reserved3;
public ushort Reserved4;
public byte Reserved5;
public ulong Reserved6;
public ulong Reserved7;
public SoftwareKeyboardAppearEx ToExtended()
{
SoftwareKeyboardAppearEx appear = new SoftwareKeyboardAppearEx();
appear.KeyboardMode = KeyboardMode;
appear.OkText = OkText;
appear.LeftOptionalSymbolKey = LeftOptionalSymbolKey;
appear.RightOptionalSymbolKey = RightOptionalSymbolKey;
appear.PredictionEnabled = PredictionEnabled;
appear.CancelButtonDisabled = CancelButtonDisabled;
appear.InvalidChars = InvalidChars;
appear.TextMaxLength = TextMaxLength;
appear.TextMinLength = TextMinLength;
appear.UseNewLine = UseNewLine;
appear.MiniaturizationMode = MiniaturizationMode;
appear.Reserved1 = Reserved1;
appear.Reserved2 = Reserved2;
appear.InvalidButtons = InvalidButtons;
appear.UseSaveData = UseSaveData;
appear.Reserved3 = Reserved3;
appear.Reserved4 = Reserved4;
appear.Reserved5 = Reserved5;
appear.Uid0 = Reserved6;
appear.Uid1 = Reserved7;
appear.SamplingNumber = 0;
appear.Reserved6 = 0;
appear.Reserved7 = 0;
appear.Reserved8 = 0;
appear.Reserved9 = 0;
return appear;
}
}
}

View file

@ -0,0 +1,100 @@
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
{
/// <summary>
/// A structure with appearance configurations for the software keyboard when running in inline mode.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
struct SoftwareKeyboardAppearEx
{
public const int OkTextLength = 8;
public KeyboardMode KeyboardMode;
/// <summary>
/// The string displayed in the Submit button.
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = OkTextLength + 1)]
public string OkText;
/// <summary>
/// The character displayed in the left button of the numeric keyboard.
/// </summary>
public char LeftOptionalSymbolKey;
/// <summary>
/// The character displayed in the right button of the numeric keyboard.
/// </summary>
public char RightOptionalSymbolKey;
/// <summary>
/// When set, predictive typing is enabled making use of the system dictionary, and any custom user dictionary.
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool PredictionEnabled;
/// <summary>
/// When set, there is only the option to accept the input.
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool CancelButtonDisabled;
/// <summary>
/// Specifies prohibited characters that cannot be input into the text entry area.
/// </summary>
public InvalidCharFlags InvalidChars;
/// <summary>
/// Maximum text length allowed.
/// </summary>
public int TextMaxLength;
/// <summary>
/// Minimum text length allowed.
/// </summary>
public int TextMinLength;
/// <summary>
/// Indicates the return button is enabled in the keyboard. This allows for input with multiple lines.
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool UseNewLine;
/// <summary>
/// [10.0.0+] If value is 1 or 2, then keytopAsFloating=0 and footerScalable=1 in Calc.
/// </summary>
public KeyboardMiniaturizationMode MiniaturizationMode;
public byte Reserved1;
public byte Reserved2;
/// <summary>
/// Bit field with invalid buttons for the keyboard.
/// </summary>
public InvalidButtonFlags InvalidButtons;
[MarshalAs(UnmanagedType.I1)]
public bool UseSaveData;
public uint Reserved3;
public ushort Reserved4;
public byte Reserved5;
/// <summary>
/// The id of the user associated with the appear request.
/// </summary>
public ulong Uid0;
public ulong Uid1;
/// <summary>
/// The sampling number for the keyboard appearance.
/// </summary>
public ulong SamplingNumber;
public ulong Reserved6;
public ulong Reserved7;
public ulong Reserved8;
public ulong Reserved9;
}
}

View file

@ -1,35 +1,35 @@
using Ryujinx.Common;
using Ryujinx.Common.Configuration.Hid;
using Ryujinx.Common.Logging;
using Ryujinx.HLE.HOS.Applets.SoftwareKeyboard;
using Ryujinx.HLE.HOS.Services.Am.AppletAE;
using Ryujinx.HLE.HOS.Services.Hid.Types.SharedMemory.Npad;
using Ryujinx.HLE.Ui;
using Ryujinx.HLE.Ui.Input;
using Ryujinx.Memory;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Ryujinx.HLE.HOS.Applets
{
internal class SoftwareKeyboardApplet : IApplet
{
private const string DefaultText = "Ryujinx";
private const string DefaultInputText = "Ryujinx";
private const long DebounceTimeMillis = 200;
private const int ResetDelayMillis = 500;
private const int StandardBufferSize = 0x7D8;
private const int MaxUserWords = 0x1388;
private const int MaxUiTextSize = 100;
private const Key CycleInputModesKey = Key.F6;
private readonly Switch _device;
private const int StandardBufferSize = 0x7D8;
private const int InteractiveBufferSize = 0x7D4;
private const int MaxUserWords = 0x1388;
private SoftwareKeyboardState _foregroundState = SoftwareKeyboardState.Uninitialized;
private SoftwareKeyboardState _foregroundState = SoftwareKeyboardState.Uninitialized;
private volatile InlineKeyboardState _backgroundState = InlineKeyboardState.Uninitialized;
private bool _isBackground = false;
private bool _alreadyShown = false;
private volatile bool _useChangedStringV2 = false;
private AppletSession _normalSession;
private AppletSession _interactiveSession;
@ -39,18 +39,24 @@ namespace Ryujinx.HLE.HOS.Applets
// Configuration for background (inline) mode.
private SoftwareKeyboardInitialize _keyboardBackgroundInitialize;
private SoftwareKeyboardCalc _keyboardBackgroundCalc;
private SoftwareKeyboardCustomizeDic _keyboardBackgroundDic;
private SoftwareKeyboardDictSet _keyboardBackgroundDictSet;
private SoftwareKeyboardUserWord[] _keyboardBackgroundUserWords;
private byte[] _transferMemory;
private string _textValue = "";
private bool _okPressed = false;
private Encoding _encoding = Encoding.Unicode;
private long _lastTextSetMillis = 0;
private bool _lastWasHidden = false;
private string _textValue = "";
private int _cursorBegin = 0;
private Encoding _encoding = Encoding.Unicode;
private KeyboardResult _lastResult = KeyboardResult.NotSet;
private IDynamicTextInputHandler _dynamicTextInputHandler = null;
private SoftwareKeyboardRenderer _keyboardRenderer = null;
private NpadReader _npads = null;
private bool _canAcceptController = false;
private KeyboardInputMode _inputMode = KeyboardInputMode.ControllerAndKeyboard;
private object _lock = new object();
public event EventHandler AppletStateChanged;
@ -62,58 +68,74 @@ namespace Ryujinx.HLE.HOS.Applets
public ResultCode Start(AppletSession normalSession,
AppletSession interactiveSession)
{
_normalSession = normalSession;
_interactiveSession = interactiveSession;
_interactiveSession.DataAvailable += OnInteractiveData;
_alreadyShown = false;
_useChangedStringV2 = false;
var launchParams = _normalSession.Pop();
var keyboardConfig = _normalSession.Pop();
if (keyboardConfig.Length == Marshal.SizeOf<SoftwareKeyboardInitialize>())
lock (_lock)
{
// Initialize the keyboard applet in background mode.
_normalSession = normalSession;
_interactiveSession = interactiveSession;
_isBackground = true;
_interactiveSession.DataAvailable += OnInteractiveData;
_keyboardBackgroundInitialize = ReadStruct<SoftwareKeyboardInitialize>(keyboardConfig);
_backgroundState = InlineKeyboardState.Uninitialized;
var launchParams = _normalSession.Pop();
var keyboardConfig = _normalSession.Pop();
return ResultCode.Success;
}
else
{
// Initialize the keyboard applet in foreground mode.
_isBackground = keyboardConfig.Length == Marshal.SizeOf<SoftwareKeyboardInitialize>();
_isBackground = false;
if (keyboardConfig.Length < Marshal.SizeOf<SoftwareKeyboardConfig>())
if (_isBackground)
{
Logger.Error?.Print(LogClass.ServiceAm, $"SoftwareKeyboardConfig size mismatch. Expected {Marshal.SizeOf<SoftwareKeyboardConfig>():x}. Got {keyboardConfig.Length:x}");
// Initialize the keyboard applet in background mode.
_keyboardBackgroundInitialize = ReadStruct<SoftwareKeyboardInitialize>(keyboardConfig);
_backgroundState = InlineKeyboardState.Uninitialized;
if (_device.UiHandler == null)
{
Logger.Error?.Print(LogClass.ServiceAm, "GUI Handler is not set, software keyboard applet will not work properly");
}
else
{
// Create a text handler that converts keyboard strokes to strings.
_dynamicTextInputHandler = _device.UiHandler.CreateDynamicTextInputHandler();
_dynamicTextInputHandler.TextChangedEvent += HandleTextChangedEvent;
_dynamicTextInputHandler.KeyPressedEvent += HandleKeyPressedEvent;
_npads = new NpadReader(_device);
_npads.NpadButtonDownEvent += HandleNpadButtonDownEvent;
_npads.NpadButtonUpEvent += HandleNpadButtonUpEvent;
_keyboardRenderer = new SoftwareKeyboardRenderer(_device.UiHandler.HostUiTheme);
}
return ResultCode.Success;
}
else
{
_keyboardForegroundConfig = ReadStruct<SoftwareKeyboardConfig>(keyboardConfig);
// Initialize the keyboard applet in foreground mode.
if (keyboardConfig.Length < Marshal.SizeOf<SoftwareKeyboardConfig>())
{
Logger.Error?.Print(LogClass.ServiceAm, $"SoftwareKeyboardConfig size mismatch. Expected {Marshal.SizeOf<SoftwareKeyboardConfig>():x}. Got {keyboardConfig.Length:x}");
}
else
{
_keyboardForegroundConfig = ReadStruct<SoftwareKeyboardConfig>(keyboardConfig);
}
if (!_normalSession.TryPop(out _transferMemory))
{
Logger.Error?.Print(LogClass.ServiceAm, "SwKbd Transfer Memory is null");
}
if (_keyboardForegroundConfig.UseUtf8)
{
_encoding = Encoding.UTF8;
}
_foregroundState = SoftwareKeyboardState.Ready;
ExecuteForegroundKeyboard();
return ResultCode.Success;
}
if (!_normalSession.TryPop(out _transferMemory))
{
Logger.Error?.Print(LogClass.ServiceAm, "SwKbd Transfer Memory is null");
}
if (_keyboardForegroundConfig.UseUtf8)
{
_encoding = Encoding.UTF8;
}
_foregroundState = SoftwareKeyboardState.Ready;
ExecuteForegroundKeyboard();
return ResultCode.Success;
}
}
@ -122,14 +144,33 @@ namespace Ryujinx.HLE.HOS.Applets
return ResultCode.Success;
}
private InlineKeyboardState GetInlineState()
private bool IsKeyboardActive()
{
return _backgroundState;
return _backgroundState >= InlineKeyboardState.Appearing && _backgroundState < InlineKeyboardState.Disappearing;
}
private void SetInlineState(InlineKeyboardState state)
private bool InputModeControllerEnabled()
{
_backgroundState = state;
return _inputMode == KeyboardInputMode.ControllerAndKeyboard ||
_inputMode == KeyboardInputMode.ControllerOnly;
}
private bool InputModeTypingEnabled()
{
return _inputMode == KeyboardInputMode.ControllerAndKeyboard ||
_inputMode == KeyboardInputMode.KeyboardOnly;
}
private void AdvanceInputMode()
{
_inputMode = (KeyboardInputMode)((int)(_inputMode + 1) % (int)KeyboardInputMode.Count);
}
public bool DrawTo(RenderingSurfaceInfo surfaceInfo, IVirtualMemoryManager destination, ulong position)
{
_npads?.Update();
return _keyboardRenderer?.DrawTo(surfaceInfo, destination, position) ?? false;
}
private void ExecuteForegroundKeyboard()
@ -151,30 +192,32 @@ namespace Ryujinx.HLE.HOS.Applets
_keyboardForegroundConfig.StringLengthMax = 100;
}
var args = new SoftwareKeyboardUiArgs
{
HeaderText = _keyboardForegroundConfig.HeaderText,
SubtitleText = _keyboardForegroundConfig.SubtitleText,
GuideText = _keyboardForegroundConfig.GuideText,
SubmitText = (!string.IsNullOrWhiteSpace(_keyboardForegroundConfig.SubmitText) ?
_keyboardForegroundConfig.SubmitText : "OK"),
StringLengthMin = _keyboardForegroundConfig.StringLengthMin,
StringLengthMax = _keyboardForegroundConfig.StringLengthMax,
InitialText = initialText
};
// Call the configured GUI handler to get user's input
if (_device.UiHandler == null)
{
Logger.Warning?.Print(LogClass.Application, "GUI Handler is not set. Falling back to default");
_okPressed = true;
_textValue = DefaultInputText;
_lastResult = KeyboardResult.Accept;
}
else
{
_okPressed = _device.UiHandler.DisplayInputDialog(args, out _textValue);
}
// Call the configured GUI handler to get user's input.
_textValue ??= initialText ?? DefaultText;
var args = new SoftwareKeyboardUiArgs
{
HeaderText = _keyboardForegroundConfig.HeaderText,
SubtitleText = _keyboardForegroundConfig.SubtitleText,
GuideText = _keyboardForegroundConfig.GuideText,
SubmitText = (!string.IsNullOrWhiteSpace(_keyboardForegroundConfig.SubmitText) ?
_keyboardForegroundConfig.SubmitText : "OK"),
StringLengthMin = _keyboardForegroundConfig.StringLengthMin,
StringLengthMax = _keyboardForegroundConfig.StringLengthMax,
InitialText = initialText
};
_lastResult = _device.UiHandler.DisplayInputDialog(args, out _textValue) ? KeyboardResult.Accept : KeyboardResult.Cancel;
_textValue ??= initialText ?? DefaultInputText;
}
// If the game requests a string with a minimum length less
// than our default text, repeat our default text until we meet
@ -189,7 +232,7 @@ namespace Ryujinx.HLE.HOS.Applets
// we truncate it.
if (_textValue.Length > _keyboardForegroundConfig.StringLengthMax)
{
_textValue = _textValue.Substring(0, (int)_keyboardForegroundConfig.StringLengthMax);
_textValue = _textValue.Substring(0, _keyboardForegroundConfig.StringLengthMax);
}
// Does the application want to validate the text itself?
@ -201,7 +244,7 @@ namespace Ryujinx.HLE.HOS.Applets
// back a validation status, which is handled in OnInteractiveDataPushIn.
_foregroundState = SoftwareKeyboardState.ValidationPending;
_interactiveSession.Push(BuildResponse(_textValue, true));
_interactiveSession.Push(BuildForegroundResponse());
}
else
{
@ -210,7 +253,7 @@ namespace Ryujinx.HLE.HOS.Applets
// and poll it for completion.
_foregroundState = SoftwareKeyboardState.Complete;
_normalSession.Push(BuildResponse(_textValue, false));
_normalSession.Push(BuildForegroundResponse());
AppletStateChanged?.Invoke(this, null);
}
@ -223,7 +266,10 @@ namespace Ryujinx.HLE.HOS.Applets
if (_isBackground)
{
OnBackgroundInteractiveData(data);
lock (_lock)
{
OnBackgroundInteractiveData(data);
}
}
else
{
@ -241,7 +287,7 @@ namespace Ryujinx.HLE.HOS.Applets
// For now we assume success, so we push the final result
// to the standard output buffer and carry on our merry way.
_normalSession.Push(BuildResponse(_textValue, false));
_normalSession.Push(BuildForegroundResponse());
AppletStateChanged?.Invoke(this, null);
@ -251,7 +297,7 @@ namespace Ryujinx.HLE.HOS.Applets
{
// If we have already completed, we push the result text
// back on the output buffer and poll the application.
_normalSession.Push(BuildResponse(_textValue, false));
_normalSession.Push(BuildForegroundResponse());
AppletStateChanged?.Invoke(this, null);
}
@ -271,19 +317,19 @@ namespace Ryujinx.HLE.HOS.Applets
using (MemoryStream stream = new MemoryStream(data))
using (BinaryReader reader = new BinaryReader(stream))
{
InlineKeyboardRequest request = (InlineKeyboardRequest)reader.ReadUInt32();
InlineKeyboardState state = GetInlineState();
var request = (InlineKeyboardRequest)reader.ReadUInt32();
long remaining;
Logger.Debug?.Print(LogClass.ServiceAm, $"Keyboard received command {request} in state {state}");
Logger.Debug?.Print(LogClass.ServiceAm, $"Keyboard received command {request} in state {_backgroundState}");
switch (request)
{
case InlineKeyboardRequest.UseChangedStringV2:
_useChangedStringV2 = true;
Logger.Stub?.Print(LogClass.ServiceAm, "Inline keyboard request UseChangedStringV2");
break;
case InlineKeyboardRequest.UseMovedCursorV2:
// Not used because we only reply with the final string.
Logger.Stub?.Print(LogClass.ServiceAm, "Inline keyboard request UseMovedCursorV2");
break;
case InlineKeyboardRequest.SetUserWordInfo:
// Read the user word info data.
@ -317,7 +363,7 @@ namespace Ryujinx.HLE.HOS.Applets
}
}
}
_interactiveSession.Push(InlineResponses.ReleasedUserWordInfo(state));
_interactiveSession.Push(InlineResponses.ReleasedUserWordInfo(_backgroundState));
break;
case InlineKeyboardRequest.SetCustomizeDic:
// Read the custom dic data.
@ -331,7 +377,6 @@ namespace Ryujinx.HLE.HOS.Applets
var keyboardDicData = reader.ReadBytes((int)remaining);
_keyboardBackgroundDic = ReadStruct<SoftwareKeyboardCustomizeDic>(keyboardDicData);
}
_interactiveSession.Push(InlineResponses.UnsetCustomizeDic(state));
break;
case InlineKeyboardRequest.SetCustomizedDictionaries:
// Read the custom dictionaries data.
@ -345,52 +390,89 @@ namespace Ryujinx.HLE.HOS.Applets
var keyboardDictData = reader.ReadBytes((int)remaining);
_keyboardBackgroundDictSet = ReadStruct<SoftwareKeyboardDictSet>(keyboardDictData);
}
_interactiveSession.Push(InlineResponses.UnsetCustomizedDictionaries(state));
break;
case InlineKeyboardRequest.Calc:
// The Calc request tells the Applet to enter the main input handling loop, which will end
// with either a text being submitted or a cancel request from the user.
// NOTE: Some Calc requests happen early in the application and are not meant to be shown. This possibly
// happens because the game has complete control over when the inline keyboard is drawn, but here it
// would cause a dialog to pop in the emulator, which is inconvenient. An algorithm is applied to
// decide whether it is a dummy Calc or not, but regardless of the result, the dummy Calc appears to
// never happen twice, so the keyboard will always show if it has already been shown before.
bool shouldShowKeyboard = _alreadyShown;
_alreadyShown = true;
// The Calc request is used to communicate configuration changes and commands to the keyboard.
// Fields in the Calc struct and operations are masked by the Flags field.
// Read the Calc data.
SoftwareKeyboardCalcEx newCalc;
remaining = stream.Length - stream.Position;
if (remaining != Marshal.SizeOf<SoftwareKeyboardCalc>())
if (remaining == Marshal.SizeOf<SoftwareKeyboardCalc>())
{
Logger.Error?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard Calc of {remaining} bytes");
var keyboardCalcData = reader.ReadBytes((int)remaining);
var keyboardCalc = ReadStruct<SoftwareKeyboardCalc>(keyboardCalcData);
newCalc = keyboardCalc.ToExtended();
}
else if (remaining == Marshal.SizeOf<SoftwareKeyboardCalcEx>() || remaining == SoftwareKeyboardCalcEx.AlternativeSize)
{
var keyboardCalcData = reader.ReadBytes((int)remaining);
newCalc = ReadStruct<SoftwareKeyboardCalcEx>(keyboardCalcData);
}
else
{
var keyboardCalcData = reader.ReadBytes((int)remaining);
_keyboardBackgroundCalc = ReadStruct<SoftwareKeyboardCalc>(keyboardCalcData);
Logger.Error?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard Calc of {remaining} bytes");
// Check if the application expects UTF8 encoding instead of UTF16.
if (_keyboardBackgroundCalc.UseUtf8)
{
_encoding = Encoding.UTF8;
}
// Force showing the keyboard regardless of the state, an unwanted
// input dialog may show, but it is better than a soft lock.
if (_keyboardBackgroundCalc.Appear.ShouldBeHidden == 0)
{
shouldShowKeyboard = true;
}
newCalc = new SoftwareKeyboardCalcEx();
}
// Send an initialization finished signal.
state = InlineKeyboardState.Ready;
SetInlineState(state);
_interactiveSession.Push(InlineResponses.FinishedInitialize(state));
// Start a task with the GUI handler to get user's input.
new Task(() => { GetInputTextAndSend(shouldShowKeyboard, state); }).Start();
// Process each individual operation specified in the flags.
bool updateText = false;
if ((newCalc.Flags & KeyboardCalcFlags.Initialize) != 0)
{
_interactiveSession.Push(InlineResponses.FinishedInitialize(_backgroundState));
_backgroundState = InlineKeyboardState.Initialized;
}
if ((newCalc.Flags & KeyboardCalcFlags.SetCursorPos) != 0)
{
_cursorBegin = newCalc.CursorPos;
updateText = true;
Logger.Debug?.Print(LogClass.ServiceAm, $"Cursor position set to {_cursorBegin}");
}
if ((newCalc.Flags & KeyboardCalcFlags.SetInputText) != 0)
{
_textValue = newCalc.InputText;
updateText = true;
Logger.Debug?.Print(LogClass.ServiceAm, $"Input text set to {_textValue}");
}
if ((newCalc.Flags & KeyboardCalcFlags.SetUtf8Mode) != 0)
{
_encoding = newCalc.UseUtf8 ? Encoding.UTF8 : Encoding.Default;
Logger.Debug?.Print(LogClass.ServiceAm, $"Encoding set to {_encoding}");
}
if (updateText)
{
_dynamicTextInputHandler.SetText(_textValue, _cursorBegin);
_keyboardRenderer.UpdateTextState(_textValue, _cursorBegin, _cursorBegin, null, null);
}
if ((newCalc.Flags & KeyboardCalcFlags.MustShow) != 0)
{
ActivateFrontend();
_backgroundState = InlineKeyboardState.Shown;
PushChangedString(_textValue, (uint)_cursorBegin, _backgroundState);
}
// Send the response to the Calc
_interactiveSession.Push(InlineResponses.Default(_backgroundState));
break;
case InlineKeyboardRequest.Finalize:
// Destroy the frontend.
DestroyFrontend();
// The calling application wants to close the keyboard applet and will wait for a state change.
_backgroundState = InlineKeyboardState.Uninitialized;
AppletStateChanged?.Invoke(this, null);
@ -398,137 +480,234 @@ namespace Ryujinx.HLE.HOS.Applets
default:
// We shouldn't be able to get here through standard swkbd execution.
Logger.Warning?.Print(LogClass.ServiceAm, $"Invalid Software Keyboard request {request} during state {_backgroundState}");
_interactiveSession.Push(InlineResponses.Default(state));
_interactiveSession.Push(InlineResponses.Default(_backgroundState));
break;
}
}
}
private void GetInputTextAndSend(bool shouldShowKeyboard, InlineKeyboardState oldState)
private void ActivateFrontend()
{
bool submit = true;
Logger.Debug?.Print(LogClass.ServiceAm, $"Activating software keyboard frontend");
// Use the text specified by the Calc if it is available, otherwise use the default one.
string inputText = (!string.IsNullOrWhiteSpace(_keyboardBackgroundCalc.InputText) ?
_keyboardBackgroundCalc.InputText : DefaultText);
_inputMode = KeyboardInputMode.ControllerAndKeyboard;
// Compute the elapsed time for the debouncing algorithm.
long currentMillis = PerformanceCounter.ElapsedMilliseconds;
long inputElapsedMillis = currentMillis - _lastTextSetMillis;
_npads.Update(true);
// Reset the input text before submitting the final result, that's because some games do not expect
// consecutive submissions to abruptly shrink and they will crash if it happens. Changing the string
// before the final submission prevents that.
InlineKeyboardState newState = InlineKeyboardState.DataAvailable;
SetInlineState(newState);
ChangedString("", newState);
NpadButton buttons = _npads.GetCurrentButtonsOfAllNpads();
if (!_lastWasHidden && (inputElapsedMillis < DebounceTimeMillis))
{
// A repeated Calc request has been received without player interaction, after the input has been
// sent. This behavior happens in some games, so instead of showing another dialog, just apply a
// time-based debouncing algorithm and repeat the last submission, either a value or a cancel.
// It is also possible that the first Calc request was hidden by accident, in this case use the
// debouncing as an oportunity to properly ask for input.
inputText = _textValue;
submit = _textValue != null;
_lastWasHidden = false;
// Block the input if the current accept key is pressed so the applet won't be instantly closed.
_canAcceptController = (buttons & NpadButton.A) == 0;
Logger.Warning?.Print(LogClass.Application, "Debouncing repeated keyboard request");
}
else if (!shouldShowKeyboard)
{
// Submit the default text to avoid soft locking if the keyboard was ignored by
// accident. It's better to change the name than being locked out of the game.
inputText = DefaultText;
_lastWasHidden = true;
_dynamicTextInputHandler.TextProcessingEnabled = true;
Logger.Debug?.Print(LogClass.Application, "Received a dummy Calc, keyboard will not be shown");
}
else if (_device.UiHandler == null)
{
Logger.Warning?.Print(LogClass.Application, "GUI Handler is not set. Falling back to default");
_lastWasHidden = false;
}
else
{
// Call the configured GUI handler to get user's input.
var args = new SoftwareKeyboardUiArgs
{
HeaderText = "", // The inline keyboard lacks these texts
SubtitleText = "",
GuideText = "",
SubmitText = (!string.IsNullOrWhiteSpace(_keyboardBackgroundCalc.Appear.OkText) ?
_keyboardBackgroundCalc.Appear.OkText : "OK"),
StringLengthMin = 0,
StringLengthMax = 100,
InitialText = inputText
};
submit = _device.UiHandler.DisplayInputDialog(args, out inputText);
inputText = submit ? inputText : null;
_lastWasHidden = false;
}
// The 'Complete' state indicates the Calc request has been fulfilled by the applet.
newState = InlineKeyboardState.Complete;
if (submit)
{
Logger.Debug?.Print(LogClass.ServiceAm, "Sending keyboard OK");
DecidedEnter(inputText, newState);
}
else
{
Logger.Debug?.Print(LogClass.ServiceAm, "Sending keyboard Cancel");
DecidedCancel(newState);
}
_interactiveSession.Push(InlineResponses.Default(newState));
// The constant calls to PopInteractiveData suggest that the keyboard applet continuously reports
// data back to the application and this can also be time-sensitive. Pushing a state reset right
// after the data has been sent does not work properly and the application will soft-lock. This
// delay gives time for the application to catch up with the data and properly process the state
// reset.
Thread.Sleep(ResetDelayMillis);
// 'Initialized' is the only known state so far that does not soft-lock the keyboard after use.
newState = InlineKeyboardState.Initialized;
Logger.Debug?.Print(LogClass.ServiceAm, $"Resetting state of the keyboard to {newState}");
SetInlineState(newState);
_interactiveSession.Push(InlineResponses.Default(newState));
// Keep the text and the timestamp of the input for the debouncing algorithm.
_textValue = inputText;
_lastTextSetMillis = PerformanceCounter.ElapsedMilliseconds;
_keyboardRenderer.UpdateCommandState(null, null, true);
_keyboardRenderer.UpdateTextState(null, null, null, null, true);
}
private void ChangedString(string text, InlineKeyboardState state)
private void DeactivateFrontend()
{
if (_encoding == Encoding.UTF8)
Logger.Debug?.Print(LogClass.ServiceAm, $"Deactivating software keyboard frontend");
_inputMode = KeyboardInputMode.ControllerAndKeyboard;
_canAcceptController = false;
_dynamicTextInputHandler.TextProcessingEnabled = false;
_dynamicTextInputHandler.SetText(_textValue, _cursorBegin);
}
private void DestroyFrontend()
{
Logger.Debug?.Print(LogClass.ServiceAm, $"Destroying software keyboard frontend");
_keyboardRenderer?.Dispose();
_keyboardRenderer = null;
if (_dynamicTextInputHandler != null)
{
if (_useChangedStringV2)
_dynamicTextInputHandler.TextChangedEvent -= HandleTextChangedEvent;
_dynamicTextInputHandler.KeyPressedEvent -= HandleKeyPressedEvent;
_dynamicTextInputHandler.Dispose();
_dynamicTextInputHandler = null;
}
if (_npads != null)
{
_npads.NpadButtonDownEvent -= HandleNpadButtonDownEvent;
_npads.NpadButtonUpEvent -= HandleNpadButtonUpEvent;
_npads = null;
}
}
private bool HandleKeyPressedEvent(Key key)
{
if (key == CycleInputModesKey)
{
lock (_lock)
{
_interactiveSession.Push(InlineResponses.ChangedStringUtf8V2(text, state));
if (IsKeyboardActive())
{
AdvanceInputMode();
bool typingEnabled = InputModeTypingEnabled();
bool controllerEnabled = InputModeControllerEnabled();
_dynamicTextInputHandler.TextProcessingEnabled = typingEnabled;
_keyboardRenderer.UpdateTextState(null, null, null, null, typingEnabled);
_keyboardRenderer.UpdateCommandState(null, null, controllerEnabled);
}
}
else
}
return true;
}
private void HandleTextChangedEvent(string text, int cursorBegin, int cursorEnd, bool overwriteMode)
{
lock (_lock)
{
// Text processing should not run with typing disabled.
Debug.Assert(InputModeTypingEnabled());
if (text.Length > MaxUiTextSize)
{
_interactiveSession.Push(InlineResponses.ChangedStringUtf8(text, state));
// Limit the text size and change it back.
text = text.Substring(0, MaxUiTextSize);
cursorBegin = Math.Min(cursorBegin, MaxUiTextSize);
cursorEnd = Math.Min(cursorEnd, MaxUiTextSize);
_dynamicTextInputHandler.SetText(text, cursorBegin, cursorEnd);
}
_textValue = text;
_cursorBegin = cursorBegin;
_keyboardRenderer.UpdateTextState(text, cursorBegin, cursorEnd, overwriteMode, null);
PushUpdatedState(text, cursorBegin, KeyboardResult.NotSet);
}
}
private void HandleNpadButtonDownEvent(int npadIndex, NpadButton button)
{
lock (_lock)
{
if (!IsKeyboardActive())
{
return;
}
switch (button)
{
case NpadButton.A:
_keyboardRenderer.UpdateCommandState(_canAcceptController, null, null);
break;
case NpadButton.B:
_keyboardRenderer.UpdateCommandState(null, _canAcceptController, null);
break;
}
}
}
private void HandleNpadButtonUpEvent(int npadIndex, NpadButton button)
{
lock (_lock)
{
KeyboardResult result = KeyboardResult.NotSet;
switch (button)
{
case NpadButton.A:
result = KeyboardResult.Accept;
_keyboardRenderer.UpdateCommandState(false, null, null);
break;
case NpadButton.B:
result = KeyboardResult.Cancel;
_keyboardRenderer.UpdateCommandState(null, false, null);
break;
}
if (IsKeyboardActive())
{
if (!_canAcceptController)
{
_canAcceptController = true;
}
else if (InputModeControllerEnabled())
{
PushUpdatedState(_textValue, _cursorBegin, result);
}
}
}
}
private void PushUpdatedState(string text, int cursorBegin, KeyboardResult result)
{
_lastResult = result;
_textValue = text;
bool cancel = result == KeyboardResult.Cancel;
bool accept = result == KeyboardResult.Accept;
if (!IsKeyboardActive())
{
// Keyboard is not active.
return;
}
if (accept == false && cancel == false)
{
Logger.Debug?.Print(LogClass.ServiceAm, $"Updating keyboard text to {text} and cursor position to {cursorBegin}");
PushChangedString(text, (uint)cursorBegin, _backgroundState);
}
else
{
if (_useChangedStringV2)
// Disable the frontend.
DeactivateFrontend();
// The 'Complete' state indicates the Calc request has been fulfilled by the applet.
_backgroundState = InlineKeyboardState.Disappearing;
if (accept)
{
_interactiveSession.Push(InlineResponses.ChangedStringV2(text, state));
Logger.Debug?.Print(LogClass.ServiceAm, $"Sending keyboard OK with text {text}");
DecidedEnter(text, _backgroundState);
}
else
else if (cancel)
{
_interactiveSession.Push(InlineResponses.ChangedString(text, state));
Logger.Debug?.Print(LogClass.ServiceAm, "Sending keyboard Cancel");
DecidedCancel(_backgroundState);
}
_interactiveSession.Push(InlineResponses.Default(_backgroundState));
Logger.Debug?.Print(LogClass.ServiceAm, $"Resetting state of the keyboard to {_backgroundState}");
// Set the state of the applet to 'Initialized' as it is the only known state so far
// that does not soft-lock the keyboard after use.
_backgroundState = InlineKeyboardState.Initialized;
_interactiveSession.Push(InlineResponses.Default(_backgroundState));
}
}
private void PushChangedString(string text, uint cursor, InlineKeyboardState state)
{
// TODO (Caian): The *V2 methods are not supported because the applications that request
// them do not seem to accept them. The regular methods seem to work just fine in all cases.
if (_encoding == Encoding.UTF8)
{
_interactiveSession.Push(InlineResponses.ChangedStringUtf8(text, cursor, state));
}
else
{
_interactiveSession.Push(InlineResponses.ChangedString(text, cursor, state));
}
}
@ -549,27 +728,17 @@ namespace Ryujinx.HLE.HOS.Applets
_interactiveSession.Push(InlineResponses.DecidedCancel(state));
}
private byte[] BuildResponse(string text, bool interactive)
private byte[] BuildForegroundResponse()
{
int bufferSize = interactive ? InteractiveBufferSize : StandardBufferSize;
int bufferSize = StandardBufferSize;
using (MemoryStream stream = new MemoryStream(new byte[bufferSize]))
using (BinaryWriter writer = new BinaryWriter(stream))
{
byte[] output = _encoding.GetBytes(text);
if (!interactive)
{
// Result Code
writer.Write(_okPressed ? 0U : 1U);
}
else
{
// In interactive mode, we write the length of the text as a long, rather than
// a result code. This field is inclusive of the 64-bit size.
writer.Write((long)output.Length + 8);
}
byte[] output = _encoding.GetBytes(_textValue);
// Result Code.
writer.Write(_lastResult == KeyboardResult.Accept ? 0U : 1U);
writer.Write(output);
return stream.ToArray();

View file

@ -8,7 +8,7 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet = CharSet.Unicode)]
struct SoftwareKeyboardCalc
{
private const int InputTextLength = 505;
public const int InputTextLength = SoftwareKeyboardCalcEx.InputTextLength;
public uint Unknown;
@ -21,22 +21,26 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
public byte Unknown2;
/// <summary>
/// Configuration flags. Their purpose is currently unknown.
/// Configuration flags. Each bit in the bitfield enabled a different operation of the keyboard
/// using the data provided with the Calc structure.
/// </summary>
public ulong Flags;
public KeyboardCalcFlags Flags;
/// <summary>
/// The original parameters used when initializing the keyboard applet.
/// Flag: 0x1
/// </summary>
public SoftwareKeyboardInitialize Initialize;
/// <summary>
/// The audio volume used by the sound effects of the keyboard.
/// Flag: 0x2
/// </summary>
public float Volume;
/// <summary>
/// The initial position of the text cursor (caret) in the provided input text.
/// Flag: 0x10
/// </summary>
public int CursorPos;
@ -47,12 +51,14 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
/// <summary>
/// The initial input text to be used by the software keyboard.
/// Flag: 0x8
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = InputTextLength + 1)]
public string InputText;
/// <summary>
/// When set, the strings communicated by software keyboard will be encoded as UTF-8 instead of UTF-16.
/// Flag: 0x20
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool UseUtf8;
@ -61,6 +67,7 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
/// <summary>
/// [5.0.0+] Enable the backspace key in the software keyboard.
/// Flag: 0x8000
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool BackspaceEnabled;
@ -68,25 +75,39 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
public short Unknown4;
public byte Unknown5;
/// <summary>
/// Flag: 0x200
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool KeytopAsFloating;
/// <summary>
/// Flag: 0x100
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool FooterScalable;
/// <summary>
/// Flag: 0x100
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool AlphaEnabledInInputMode;
/// <summary>
/// Flag: 0x100
/// </summary>
public byte InputModeFadeType;
/// <summary>
/// When set, the software keyboard ignores touch input.
/// Flag: 0x200
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool TouchDisabled;
/// <summary>
/// When set, the software keyboard ignores hardware keyboard commands.
/// Flag: 0x800
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool HardwareKeyboardDisabled;
@ -96,11 +117,13 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
/// <summary>
/// Default value is 1.0.
/// Flag: 0x200
/// </summary>
public float KeytopScale0;
/// <summary>
/// Default value is 1.0.
/// Flag: 0x200
/// </summary>
public float KeytopScale1;
@ -109,16 +132,19 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
/// <summary>
/// Default value is 1.0.
/// Flag: 0x100
/// </summary>
public float KeytopBgAlpha;
/// <summary>
/// Default value is 1.0.
/// Flag: 0x100
/// </summary>
public float FooterBgAlpha;
/// <summary>
/// Default value is 1.0.
/// Flag: 0x200
/// </summary>
public float BalloonScale;
@ -129,6 +155,8 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
/// <summary>
/// [5.0.0+] Enable sound effect.
/// Flag: Enable: 0x2000
/// Disable: 0x4000
/// </summary>
public byte SeGroup;
@ -143,5 +171,50 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
public byte Trigger;
public byte Padding;
public SoftwareKeyboardCalcEx ToExtended()
{
SoftwareKeyboardCalcEx calc = new SoftwareKeyboardCalcEx();
calc.Unknown = Unknown;
calc.Size = Size;
calc.Unknown1 = Unknown1;
calc.Unknown2 = Unknown2;
calc.Flags = Flags;
calc.Initialize = Initialize;
calc.Volume = Volume;
calc.CursorPos = CursorPos;
calc.Appear = Appear.ToExtended();
calc.InputText = InputText;
calc.UseUtf8 = UseUtf8;
calc.Unknown3 = Unknown3;
calc.BackspaceEnabled = BackspaceEnabled;
calc.Unknown4 = Unknown4;
calc.Unknown5 = Unknown5;
calc.KeytopAsFloating = KeytopAsFloating;
calc.FooterScalable = FooterScalable;
calc.AlphaEnabledInInputMode = AlphaEnabledInInputMode;
calc.InputModeFadeType = InputModeFadeType;
calc.TouchDisabled = TouchDisabled;
calc.HardwareKeyboardDisabled = HardwareKeyboardDisabled;
calc.Unknown6 = Unknown6;
calc.Unknown7 = Unknown7;
calc.KeytopScale0 = KeytopScale0;
calc.KeytopScale1 = KeytopScale1;
calc.KeytopTranslate0 = KeytopTranslate0;
calc.KeytopTranslate1 = KeytopTranslate1;
calc.KeytopBgAlpha = KeytopBgAlpha;
calc.FooterBgAlpha = FooterBgAlpha;
calc.BalloonScale = BalloonScale;
calc.Unknown8 = Unknown8;
calc.Unknown9 = Unknown9;
calc.Unknown10 = Unknown10;
calc.Unknown11 = Unknown11;
calc.SeGroup = SeGroup;
calc.TriggerFlag = TriggerFlag;
calc.Trigger = Trigger;
return calc;
}
}
}

View file

@ -0,0 +1,182 @@
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
{
/// <summary>
/// A structure with configuration options of the software keyboard when starting a new input request in inline mode.
/// This is the extended version of the structure with extended appear options.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet = CharSet.Unicode)]
struct SoftwareKeyboardCalcEx
{
/// <summary>
/// This struct was built following Switchbrew's specs, but this size (larger) is also found in real games.
/// It's assumed that this is padding at the end of this struct, because all members seem OK.
/// </summary>
public const int AlternativeSize = 1256;
public const int InputTextLength = 505;
public uint Unknown;
/// <summary>
/// The size of the Calc struct, as reported by the process communicating with the applet.
/// </summary>
public ushort Size;
public byte Unknown1;
public byte Unknown2;
/// <summary>
/// Configuration flags. Each bit in the bitfield enabled a different operation of the keyboard
/// using the data provided with the Calc structure.
/// </summary>
public KeyboardCalcFlags Flags;
/// <summary>
/// The original parameters used when initializing the keyboard applet.
/// Flag: 0x1
/// </summary>
public SoftwareKeyboardInitialize Initialize;
/// <summary>
/// The audio volume used by the sound effects of the keyboard.
/// Flag: 0x2
/// </summary>
public float Volume;
/// <summary>
/// The initial position of the text cursor (caret) in the provided input text.
/// Flag: 0x10
/// </summary>
public int CursorPos;
/// <summary>
/// Appearance configurations for the on-screen keyboard.
/// </summary>
public SoftwareKeyboardAppearEx Appear;
/// <summary>
/// The initial input text to be used by the software keyboard.
/// Flag: 0x8
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = InputTextLength + 1)]
public string InputText;
/// <summary>
/// When set, the strings communicated by software keyboard will be encoded as UTF-8 instead of UTF-16.
/// Flag: 0x20
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool UseUtf8;
public byte Unknown3;
/// <summary>
/// [5.0.0+] Enable the backspace key in the software keyboard.
/// Flag: 0x8000
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool BackspaceEnabled;
public short Unknown4;
public byte Unknown5;
/// <summary>
/// Flag: 0x200
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool KeytopAsFloating;
/// <summary>
/// Flag: 0x100
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool FooterScalable;
/// <summary>
/// Flag: 0x100
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool AlphaEnabledInInputMode;
/// <summary>
/// Flag: 0x100
/// </summary>
public byte InputModeFadeType;
/// <summary>
/// When set, the software keyboard ignores touch input.
/// Flag: 0x200
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool TouchDisabled;
/// <summary>
/// When set, the software keyboard ignores hardware keyboard commands.
/// Flag: 0x800
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool HardwareKeyboardDisabled;
public uint Unknown6;
public uint Unknown7;
/// <summary>
/// Default value is 1.0.
/// Flag: 0x200
/// </summary>
public float KeytopScale0;
/// <summary>
/// Default value is 1.0.
/// Flag: 0x200
/// </summary>
public float KeytopScale1;
public float KeytopTranslate0;
public float KeytopTranslate1;
/// <summary>
/// Default value is 1.0.
/// Flag: 0x100
/// </summary>
public float KeytopBgAlpha;
/// <summary>
/// Default value is 1.0.
/// Flag: 0x100
/// </summary>
public float FooterBgAlpha;
/// <summary>
/// Default value is 1.0.
/// Flag: 0x200
/// </summary>
public float BalloonScale;
public float Unknown8;
public uint Unknown9;
public uint Unknown10;
public uint Unknown11;
/// <summary>
/// [5.0.0+] Enable sound effect.
/// Flag: Enable: 0x2000
/// Disable: 0x4000
/// </summary>
public byte SeGroup;
/// <summary>
/// [6.0.0+] Enables the Trigger field when Trigger is non-zero.
/// </summary>
public byte TriggerFlag;
/// <summary>
/// [6.0.0+] Always set to zero.
/// </summary>
public byte Trigger;
public byte Padding;
}
}

View file

@ -0,0 +1,717 @@
using Ryujinx.HLE.Ui;
using Ryujinx.Memory;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
{
/// <summary>
/// Class that generates the graphics for the software keyboard applet during inline mode.
/// </summary>
internal class SoftwareKeyboardRenderer : IDisposable
{
const int TextBoxBlinkThreshold = 8;
const int TextBoxBlinkSleepMilliseconds = 100;
const int TextBoxBlinkJoinWaitMilliseconds = 1000;
const string MessageText = "Please use the keyboard to input text";
const string AcceptText = "Accept";
const string CancelText = "Cancel";
const string ControllerToggleText = "Toggle input";
private RenderingSurfaceInfo _surfaceInfo;
private Bitmap _surface = null;
private object _renderLock = new object();
private string _inputText = "";
private int _cursorStart = 0;
private int _cursorEnd = 0;
private bool _acceptPressed = false;
private bool _cancelPressed = false;
private bool _overwriteMode = false;
private bool _typingEnabled = true;
private bool _controllerEnabled = true;
private Image _ryujinxLogo = null;
private Image _padAcceptIcon = null;
private Image _padCancelIcon = null;
private Image _keyModeIcon = null;
private float _textBoxOutlineWidth;
private float _padPressedPenWidth;
private Brush _panelBrush;
private Brush _disabledBrush;
private Brush _textNormalBrush;
private Brush _textSelectedBrush;
private Brush _textOverCursorBrush;
private Brush _cursorBrush;
private Brush _selectionBoxBrush;
private Brush _keyCapBrush;
private Brush _keyProgressBrush;
private Pen _gridSeparatorPen;
private Pen _textBoxOutlinePen;
private Pen _cursorPen;
private Pen _selectionBoxPen;
private Pen _padPressedPen;
private int _inputTextFontSize;
private int _padButtonFontSize;
private Font _messageFont;
private Font _inputTextFont;
private Font _labelsTextFont;
private Font _padSymbolFont;
private Font _keyCapFont;
private float _inputTextCalibrationHeight;
private float _panelPositionY;
private RectangleF _panelRectangle;
private PointF _logoPosition;
private float _messagePositionY;
private TRef<int> _textBoxBlinkCounter = new TRef<int>(0);
private TimedAction _textBoxBlinkTimedAction = new TimedAction();
public SoftwareKeyboardRenderer(IHostUiTheme uiTheme)
{
_surfaceInfo = new RenderingSurfaceInfo(0, 0, 0, 0, 0);
string ryujinxLogoPath = "Ryujinx.Ui.Resources.Logo_Ryujinx.png";
int ryujinxLogoSize = 32;
_ryujinxLogo = LoadResource(Assembly.GetEntryAssembly(), ryujinxLogoPath, ryujinxLogoSize, ryujinxLogoSize);
string padAcceptIconPath = "Ryujinx.HLE.HOS.Applets.SoftwareKeyboard.Resources.Icon_BtnA.png";
string padCancelIconPath = "Ryujinx.HLE.HOS.Applets.SoftwareKeyboard.Resources.Icon_BtnB.png";
string keyModeIconPath = "Ryujinx.HLE.HOS.Applets.SoftwareKeyboard.Resources.Icon_KeyF6.png";
_padAcceptIcon = LoadResource(Assembly.GetExecutingAssembly(), padAcceptIconPath , 0, 0);
_padCancelIcon = LoadResource(Assembly.GetExecutingAssembly(), padCancelIconPath , 0, 0);
_keyModeIcon = LoadResource(Assembly.GetExecutingAssembly(), keyModeIconPath , 0, 0);
Color panelColor = ToColor(uiTheme.DefaultBackgroundColor, 255);
Color panelTransparentColor = ToColor(uiTheme.DefaultBackgroundColor, 150);
Color normalTextColor = ToColor(uiTheme.DefaultForegroundColor);
Color invertedTextColor = ToColor(uiTheme.DefaultForegroundColor, null, true);
Color selectedTextColor = ToColor(uiTheme.SelectionForegroundColor);
Color borderColor = ToColor(uiTheme.DefaultBorderColor);
Color selectionBackgroundColor = ToColor(uiTheme.SelectionBackgroundColor);
Color gridSeparatorColor = Color.FromArgb(180, 255, 255, 255);
float cursorWidth = 2;
_textBoxOutlineWidth = 2;
_padPressedPenWidth = 2;
_panelBrush = new SolidBrush(panelColor);
_disabledBrush = new SolidBrush(panelTransparentColor);
_textNormalBrush = new SolidBrush(normalTextColor);
_textSelectedBrush = new SolidBrush(selectedTextColor);
_textOverCursorBrush = new SolidBrush(invertedTextColor);
_cursorBrush = new SolidBrush(normalTextColor);
_selectionBoxBrush = new SolidBrush(selectionBackgroundColor);
_keyCapBrush = Brushes.White;
_keyProgressBrush = new SolidBrush(borderColor);
_gridSeparatorPen = new Pen(gridSeparatorColor, 2);
_textBoxOutlinePen = new Pen(borderColor, _textBoxOutlineWidth);
_cursorPen = new Pen(normalTextColor, cursorWidth);
_selectionBoxPen = new Pen(selectionBackgroundColor, cursorWidth);
_padPressedPen = new Pen(borderColor, _padPressedPenWidth);
_inputTextFontSize = 20;
_padButtonFontSize = 24;
string font = uiTheme.FontFamily;
_messageFont = new Font(font, 26, FontStyle.Regular, GraphicsUnit.Pixel);
_inputTextFont = new Font(font, _inputTextFontSize, FontStyle.Regular, GraphicsUnit.Pixel);
_labelsTextFont = new Font(font, 24, FontStyle.Regular, GraphicsUnit.Pixel);
_padSymbolFont = new Font(font, _padButtonFontSize, FontStyle.Regular, GraphicsUnit.Pixel);
_keyCapFont = new Font(font, 15, FontStyle.Regular, GraphicsUnit.Pixel);
// System.Drawing has serious problems measuring strings, so it requires a per-pixel calibration
// to ensure we are rendering text inside the proper region
_inputTextCalibrationHeight = CalibrateTextHeight(_inputTextFont);
StartTextBoxBlinker(_textBoxBlinkTimedAction, _textBoxBlinkCounter);
}
private static void StartTextBoxBlinker(TimedAction timedAction, TRef<int> blinkerCounter)
{
timedAction.Reset(() =>
{
// The blinker is on falf of the time and events such as input
// changes can reset the blinker.
var value = Volatile.Read(ref blinkerCounter.Value);
value = (value + 1) % (2 * TextBoxBlinkThreshold);
Volatile.Write(ref blinkerCounter.Value, value);
}, TextBoxBlinkSleepMilliseconds);
}
private Color ToColor(ThemeColor color, byte? overrideAlpha = null, bool flipRgb = false)
{
var a = (byte)(color.A * 255);
var r = (byte)(color.R * 255);
var g = (byte)(color.G * 255);
var b = (byte)(color.B * 255);
if (flipRgb)
{
r = (byte)(255 - r);
g = (byte)(255 - g);
b = (byte)(255 - b);
}
return Color.FromArgb(overrideAlpha.GetValueOrDefault(a), r, g, b);
}
private Image LoadResource(Assembly assembly, string resourcePath, int newWidth, int newHeight)
{
Stream resourceStream = assembly.GetManifestResourceStream(resourcePath);
Debug.Assert(resourceStream != null);
var originalImage = Image.FromStream(resourceStream);
if (newHeight == 0 || newWidth == 0)
{
return originalImage;
}
var newSize = new Rectangle(0, 0, newWidth, newHeight);
var newImage = new Bitmap(newWidth, newHeight);
using (var graphics = System.Drawing.Graphics.FromImage(newImage))
using (var wrapMode = new ImageAttributes())
{
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.SmoothingMode = SmoothingMode.HighQuality;
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(originalImage, newSize, 0, 0, originalImage.Width, originalImage.Height, GraphicsUnit.Pixel, wrapMode);
}
return newImage;
}
#pragma warning disable CS8632
public void UpdateTextState(string? inputText, int? cursorStart, int? cursorEnd, bool? overwriteMode, bool? typingEnabled)
#pragma warning restore CS8632
{
lock (_renderLock)
{
// Update the parameters that were provided.
_inputText = inputText != null ? inputText : _inputText;
_cursorStart = cursorStart.GetValueOrDefault(_cursorStart);
_cursorEnd = cursorEnd.GetValueOrDefault(_cursorEnd);
_overwriteMode = overwriteMode.GetValueOrDefault(_overwriteMode);
_typingEnabled = typingEnabled.GetValueOrDefault(_typingEnabled);
// Reset the cursor blink.
Volatile.Write(ref _textBoxBlinkCounter.Value, 0);
}
}
public void UpdateCommandState(bool? acceptPressed, bool? cancelPressed, bool? controllerEnabled)
{
lock (_renderLock)
{
// Update the parameters that were provided.
_acceptPressed = acceptPressed.GetValueOrDefault(_acceptPressed);
_cancelPressed = cancelPressed.GetValueOrDefault(_cancelPressed);
_controllerEnabled = controllerEnabled.GetValueOrDefault(_controllerEnabled);
}
}
private void Redraw()
{
if (_surface == null)
{
return;
}
using (var graphics = CreateGraphics())
{
var messageRectangle = MeasureString(graphics, MessageText, _messageFont);
float messagePositionX = (_panelRectangle.Width - messageRectangle.Width) / 2 - messageRectangle.X;
float messagePositionY = _messagePositionY - messageRectangle.Y;
PointF messagePosition = new PointF(messagePositionX, messagePositionY);
graphics.Clear(Color.Transparent);
graphics.TranslateTransform(0, _panelPositionY);
graphics.FillRectangle(_panelBrush, _panelRectangle);
graphics.DrawImage(_ryujinxLogo, _logoPosition);
DrawString(graphics, MessageText, _messageFont, _textNormalBrush, messagePosition);
if (!_typingEnabled)
{
// Just draw a semi-transparent rectangle on top to fade the component with the background.
// TODO (caian): This will not work if one decides to add make background semi-transparent as well.
graphics.FillRectangle(_disabledBrush, messagePositionX, messagePositionY, messageRectangle.Width, messageRectangle.Height);
}
DrawTextBox(graphics);
float halfWidth = _panelRectangle.Width / 2;
PointF acceptButtonPosition = new PointF(halfWidth - 180, 185);
PointF cancelButtonPosition = new PointF(halfWidth , 185);
PointF disableButtonPosition = new PointF(halfWidth + 180, 185);
DrawPadButton (graphics, acceptButtonPosition , _padAcceptIcon, AcceptText, _acceptPressed, _controllerEnabled);
DrawPadButton (graphics, cancelButtonPosition , _padCancelIcon, CancelText, _cancelPressed, _controllerEnabled);
DrawControllerToggle(graphics, disableButtonPosition, _controllerEnabled);
}
}
private void RecreateSurface()
{
Debug.Assert(_surfaceInfo.ColorFormat == Services.SurfaceFlinger.ColorFormat.A8B8G8R8);
// Use the whole area of the image to draw, even the alignment, otherwise it may shear the final
// image if the pitch is different.
uint totalWidth = _surfaceInfo.Pitch / 4;
uint totalHeight = _surfaceInfo.Size / _surfaceInfo.Pitch;
Debug.Assert(_surfaceInfo.Width <= totalWidth);
Debug.Assert(_surfaceInfo.Height <= totalHeight);
Debug.Assert(_surfaceInfo.Pitch * _surfaceInfo.Height <= _surfaceInfo.Size);
_surface = new Bitmap((int)totalWidth, (int)totalHeight, PixelFormat.Format32bppArgb);
}
private void RecomputeConstants()
{
float totalWidth = _surfaceInfo.Width;
float totalHeight = _surfaceInfo.Height;
float panelHeight = 240;
_panelPositionY = totalHeight - panelHeight;
_panelRectangle = new RectangleF(0, 0, totalWidth, panelHeight);
_messagePositionY = 60;
float logoPositionX = (totalWidth - _ryujinxLogo.Width) / 2;
float logoPositionY = 18;
_logoPosition = new PointF(logoPositionX, logoPositionY);
}
private StringFormat CreateStringFormat(string text)
{
StringFormat format = new StringFormat(StringFormat.GenericTypographic);
format.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
format.SetMeasurableCharacterRanges(new CharacterRange[] { new CharacterRange(0, text.Length) });
return format;
}
private RectangleF MeasureString(System.Drawing.Graphics graphics, string text, System.Drawing.Font font)
{
bool isEmpty = false;
if (string.IsNullOrEmpty(text))
{
isEmpty = true;
text = " ";
}
var format = CreateStringFormat(text);
var rectangle = new RectangleF(0, 0, float.PositiveInfinity, float.PositiveInfinity);
var regions = graphics.MeasureCharacterRanges(text, font, rectangle, format);
Debug.Assert(regions.Length == 1);
rectangle = regions[0].GetBounds(graphics);
if (isEmpty)
{
rectangle.Width = 0;
}
else
{
rectangle.Width += 1.0f;
}
return rectangle;
}
private float CalibrateTextHeight(Font font)
{
// This is a pixel-wise calibration that tests the offset of a reference character because Windows text measurement
// is horrible when compared to other frameworks like Cairo and diverge across systems and fonts.
Debug.Assert(font.Unit == GraphicsUnit.Pixel);
var surfaceSize = (int)Math.Ceiling(2 * font.Size);
string calibrationText = "|";
using (var surface = new Bitmap(surfaceSize, surfaceSize, PixelFormat.Format32bppArgb))
using (var graphics = CreateGraphics(surface))
{
var measuredRectangle = MeasureString(graphics, calibrationText, font);
Debug.Assert(measuredRectangle.Right <= surfaceSize);
Debug.Assert(measuredRectangle.Bottom <= surfaceSize);
var textPosition = new PointF(0, 0);
graphics.Clear(Color.Transparent);
DrawString(graphics, calibrationText, font, Brushes.White, textPosition);
var lockRectangle = new Rectangle(0, 0, surface.Width, surface.Height);
var surfaceData = surface.LockBits(lockRectangle, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
var surfaceBytes = new byte[surfaceData.Stride * surfaceData.Height];
Marshal.Copy(surfaceData.Scan0, surfaceBytes, 0, surfaceBytes.Length);
Point topLeft = new Point();
Point bottomLeft = new Point();
bool foundTopLeft = false;
for (int y = 0; y < surfaceData.Height; y++)
{
for (int x = 0; x < surfaceData.Stride; x += 4)
{
int position = y * surfaceData.Stride + x;
if (surfaceBytes[position] != 0)
{
if (!foundTopLeft)
{
topLeft.X = x;
topLeft.Y = y;
foundTopLeft = true;
break;
}
else
{
bottomLeft.X = x;
bottomLeft.Y = y;
break;
}
}
}
}
return bottomLeft.Y - topLeft.Y;
}
}
private void DrawString(System.Drawing.Graphics graphics, string text, Font font, Brush brush, PointF point)
{
var format = CreateStringFormat(text);
graphics.DrawString(text, font, brush, point, format);
}
private System.Drawing.Graphics CreateGraphics()
{
return CreateGraphics(_surface);
}
private System.Drawing.Graphics CreateGraphics(Image surface)
{
var graphics = System.Drawing.Graphics.FromImage(surface);
graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.CompositingMode = CompositingMode.SourceOver;
graphics.PixelOffsetMode = PixelOffsetMode.HighSpeed;
graphics.SmoothingMode = SmoothingMode.HighSpeed;
return graphics;
}
private void DrawTextBox(System.Drawing.Graphics graphics)
{
var inputTextRectangle = MeasureString(graphics, _inputText, _inputTextFont);
float boxWidth = (int)(Math.Max(300, inputTextRectangle.Width + inputTextRectangle.X + 8));
float boxHeight = 32;
float boxY = 110;
float boxX = (int)((_panelRectangle.Width - boxWidth) / 2);
graphics.DrawRectangle(_textBoxOutlinePen, boxX, boxY, boxWidth, boxHeight);
float inputTextX = (_panelRectangle.Width - inputTextRectangle.Width) / 2 - inputTextRectangle.X;
float inputTextY = boxY + boxHeight - inputTextRectangle.Bottom - 5;
var inputTextPosition = new PointF(inputTextX, inputTextY);
DrawString(graphics, _inputText, _inputTextFont, _textNormalBrush, inputTextPosition);
// Draw the cursor on top of the text and redraw the text with a different color if necessary.
Brush cursorTextBrush;
Brush cursorBrush;
Pen cursorPen;
float cursorPositionYBottom = inputTextY + inputTextRectangle.Bottom;
float cursorPositionYTop = cursorPositionYBottom - _inputTextCalibrationHeight - 2;
float cursorPositionXLeft;
float cursorPositionXRight;
bool cursorVisible = false;
if (_cursorStart != _cursorEnd)
{
cursorTextBrush = _textSelectedBrush;
cursorBrush = _selectionBoxBrush;
cursorPen = _selectionBoxPen;
string textUntilBegin = _inputText.Substring(0, _cursorStart);
string textUntilEnd = _inputText.Substring(0, _cursorEnd);
RectangleF selectionBeginRectangle = MeasureString(graphics, textUntilBegin, _inputTextFont);
RectangleF selectionEndRectangle = MeasureString(graphics, textUntilEnd , _inputTextFont);
cursorVisible = true;
cursorPositionXLeft = inputTextX + selectionBeginRectangle.Width + selectionBeginRectangle.X;
cursorPositionXRight = inputTextX + selectionEndRectangle.Width + selectionEndRectangle.X;
}
else
{
cursorTextBrush = _textOverCursorBrush;
cursorBrush = _cursorBrush;
cursorPen = _cursorPen;
if (Volatile.Read(ref _textBoxBlinkCounter.Value) < TextBoxBlinkThreshold)
{
// Show the blinking cursor.
int cursorStart = Math.Min(_inputText.Length, _cursorStart);
string textUntilCursor = _inputText.Substring(0, cursorStart);
RectangleF cursorTextRectangle = MeasureString(graphics, textUntilCursor, _inputTextFont);
cursorVisible = true;
cursorPositionXLeft = inputTextX + cursorTextRectangle.Width + cursorTextRectangle.X;
if (_overwriteMode)
{
// The blinking cursor is in overwrite mode so it takes the size of a character.
if (_cursorStart < _inputText.Length)
{
textUntilCursor = _inputText.Substring(0, cursorStart + 1);
cursorTextRectangle = MeasureString(graphics, textUntilCursor, _inputTextFont);
cursorPositionXRight = inputTextX + cursorTextRectangle.Width + cursorTextRectangle.X;
}
else
{
cursorPositionXRight = cursorPositionXLeft + _inputTextFontSize / 2;
}
}
else
{
// The blinking cursor is in insert mode so it is only a line.
cursorPositionXRight = cursorPositionXLeft;
}
}
else
{
cursorPositionXLeft = inputTextX;
cursorPositionXRight = inputTextX;
}
}
if (_typingEnabled && cursorVisible)
{
float cursorWidth = cursorPositionXRight - cursorPositionXLeft;
float cursorHeight = cursorPositionYBottom - cursorPositionYTop;
if (cursorWidth == 0)
{
graphics.DrawLine(cursorPen, cursorPositionXLeft, cursorPositionYTop, cursorPositionXLeft, cursorPositionYBottom);
}
else
{
graphics.DrawRectangle(cursorPen, cursorPositionXLeft, cursorPositionYTop, cursorWidth, cursorHeight);
graphics.FillRectangle(cursorBrush, cursorPositionXLeft, cursorPositionYTop, cursorWidth, cursorHeight);
var cursorRectangle = new RectangleF(cursorPositionXLeft, cursorPositionYTop, cursorWidth, cursorHeight);
var oldClip = graphics.Clip;
graphics.Clip = new Region(cursorRectangle);
DrawString(graphics, _inputText, _inputTextFont, cursorTextBrush, inputTextPosition);
graphics.Clip = oldClip;
}
}
else if (!_typingEnabled)
{
// Just draw a semi-transparent rectangle on top to fade the component with the background.
// TODO (caian): This will not work if one decides to add make background semi-transparent as well.
graphics.FillRectangle(_disabledBrush, boxX - _textBoxOutlineWidth, boxY - _textBoxOutlineWidth,
boxWidth + 2* _textBoxOutlineWidth, boxHeight + 2* _textBoxOutlineWidth);
}
}
private void DrawPadButton(System.Drawing.Graphics graphics, PointF point, Image icon, string label, bool pressed, bool enabled)
{
// Use relative positions so we can center the the entire drawing later.
float iconX = 0;
float iconY = 0;
float iconWidth = icon.Width;
float iconHeight = icon.Height;
var labelRectangle = MeasureString(graphics, label, _labelsTextFont);
float labelPositionX = iconWidth + 8 - labelRectangle.X;
float labelPositionY = (iconHeight - labelRectangle.Height) / 2 - labelRectangle.Y - 1;
float fullWidth = labelPositionX + labelRectangle.Width + labelRectangle.X;
float fullHeight = iconHeight;
// Convert all relative positions into absolute.
float originX = (int)(point.X - fullWidth / 2);
float originY = (int)(point.Y - fullHeight / 2);
iconX += originX;
iconY += originY;
var labelPosition = new PointF(labelPositionX + originX, labelPositionY + originY);
graphics.DrawImageUnscaled(icon, (int)iconX, (int)iconY);
DrawString(graphics, label, _labelsTextFont, _textNormalBrush, labelPosition);
GraphicsPath frame = new GraphicsPath();
frame.AddRectangle(new RectangleF(originX - 2 * _padPressedPenWidth, originY - 2 * _padPressedPenWidth,
fullWidth + 4 * _padPressedPenWidth, fullHeight + 4 * _padPressedPenWidth));
if (enabled)
{
if (pressed)
{
graphics.DrawPath(_padPressedPen, frame);
}
}
else
{
// Just draw a semi-transparent rectangle on top to fade the component with the background.
// TODO (caian): This will not work if one decides to add make background semi-transparent as well.
graphics.FillPath(_disabledBrush, frame);
}
}
private void DrawControllerToggle(System.Drawing.Graphics graphics, PointF point, bool enabled)
{
var labelRectangle = MeasureString(graphics, ControllerToggleText, _labelsTextFont);
// Use relative positions so we can center the the entire drawing later.
float keyWidth = _keyModeIcon.Width;
float keyHeight = _keyModeIcon.Height;
float labelPositionX = keyWidth + 8 - labelRectangle.X;
float labelPositionY = -labelRectangle.Y - 1;
float keyX = 0;
float keyY = (int)((labelPositionY + labelRectangle.Height - keyHeight) / 2);
float fullWidth = labelPositionX + labelRectangle.Width;
float fullHeight = Math.Max(labelPositionY + labelRectangle.Height, keyHeight);
// Convert all relative positions into absolute.
float originX = (int)(point.X - fullWidth / 2);
float originY = (int)(point.Y - fullHeight / 2);
keyX += originX;
keyY += originY;
var labelPosition = new PointF(labelPositionX + originX, labelPositionY + originY);
var overlayPosition = new Point((int)keyX, (int)keyY);
graphics.DrawImageUnscaled(_keyModeIcon, overlayPosition);
DrawString(graphics, ControllerToggleText, _labelsTextFont, _textNormalBrush, labelPosition);
}
private unsafe bool TryCopyTo(IVirtualMemoryManager destination, ulong position)
{
if (_surface == null)
{
return false;
}
Rectangle lockRectangle = new Rectangle(0, 0, _surface.Width, _surface.Height);
BitmapData surfaceData = _surface.LockBits(lockRectangle, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
Debug.Assert(surfaceData.Stride == _surfaceInfo.Pitch);
Debug.Assert(surfaceData.Stride * surfaceData.Height == _surfaceInfo.Size);
// Convert the pixel format used in System.Drawing to the one required by a Switch Surface.
int dataLength = surfaceData.Stride * surfaceData.Height;
byte* dataPointer = (byte*)surfaceData.Scan0;
byte* dataEnd = dataPointer + dataLength;
for (; dataPointer < dataEnd; dataPointer += 4)
{
*(uint*)dataPointer = (uint)(
(*(dataPointer + 0) << 16) |
(*(dataPointer + 1) << 8 ) |
(*(dataPointer + 2) << 0 ) |
(*(dataPointer + 3) << 24));
}
try
{
Span<byte> dataSpan = new Span<byte>((void*)surfaceData.Scan0, dataLength);
destination.Write(position, dataSpan);
}
finally
{
_surface.UnlockBits(surfaceData);
}
return true;
}
internal bool DrawTo(RenderingSurfaceInfo surfaceInfo, IVirtualMemoryManager destination, ulong position)
{
lock (_renderLock)
{
if (!_surfaceInfo.Equals(surfaceInfo))
{
_surfaceInfo = surfaceInfo;
RecreateSurface();
RecomputeConstants();
}
Redraw();
return TryCopyTo(destination, position);
}
}
public void Dispose()
{
_textBoxBlinkTimedAction.RequestCancel();
}
}
}

View file

@ -0,0 +1,19 @@
namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
{
/// <summary>
/// Wraps a type in a class so it gets stored in the GC managed heap. This is used as communication mechanism
/// between classed that need to be disposed and, thus, can't share their references.
/// </summary>
/// <typeparam name="T">The internal type.</typeparam>
class TRef<T>
{
public T Value;
public TRef() { }
public TRef(T value)
{
Value = value;
}
}
}

View file

@ -0,0 +1,172 @@
using System;
using System.Threading;
namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
{
/// <summary>
/// A threaded executor of periodic actions that can be cancelled. The total execution time is optional
/// and, in this case, a progress is reported back to the action.
/// </summary>
class TimedAction
{
public const int MaxThreadSleep = 100;
private class SleepSubstepData
{
public readonly int SleepMilliseconds;
public readonly int SleepCount;
public readonly int SleepRemainderMilliseconds;
public SleepSubstepData(int sleepMilliseconds)
{
SleepMilliseconds = Math.Min(sleepMilliseconds, MaxThreadSleep);
SleepCount = sleepMilliseconds / SleepMilliseconds;
SleepRemainderMilliseconds = sleepMilliseconds - SleepCount * SleepMilliseconds;
}
}
private TRef<bool> _cancelled = null;
private Thread _thread = null;
private object _lock = new object();
public bool IsRunning
{
get
{
lock (_lock)
{
if (_thread == null)
{
return false;
}
return _thread.IsAlive;
}
}
}
public void RequestCancel()
{
lock (_lock)
{
if (_cancelled != null)
{
Volatile.Write(ref _cancelled.Value, true);
}
}
}
public TimedAction() { }
private void Reset(Thread thread, TRef<bool> cancelled)
{
lock (_lock)
{
// Cancel the current task.
if (_cancelled != null)
{
Volatile.Write(ref _cancelled.Value, true);
}
_cancelled = cancelled;
_thread = thread;
_thread.IsBackground = true;
_thread.Start();
}
}
public void Reset(Action<float> action, int totalMilliseconds, int sleepMilliseconds)
{
// Create a dedicated cancel token for each task.
var cancelled = new TRef<bool>(false);
Reset(new Thread(() =>
{
var substepData = new SleepSubstepData(sleepMilliseconds);
int totalCount = totalMilliseconds / sleepMilliseconds;
int totalRemainder = totalMilliseconds - totalCount * sleepMilliseconds;
if (Volatile.Read(ref cancelled.Value))
{
action(-1);
return;
}
action(0);
for (int i = 1; i <= totalCount; i++)
{
if (SleepWithSubstep(substepData, cancelled))
{
action(-1);
return;
}
action((float)(i * sleepMilliseconds) / totalMilliseconds);
}
if (totalRemainder > 0)
{
if (SleepWithSubstep(substepData, cancelled))
{
action(-1);
return;
}
action(1);
}
}), cancelled);
}
public void Reset(Action action, int sleepMilliseconds)
{
// Create a dedicated cancel token for each task.
var cancelled = new TRef<bool>(false);
Reset(new Thread(() =>
{
var substepData = new SleepSubstepData(sleepMilliseconds);
while (!Volatile.Read(ref cancelled.Value))
{
action();
if (SleepWithSubstep(substepData, cancelled))
{
return;
}
}
}), cancelled);
}
private static bool SleepWithSubstep(SleepSubstepData substepData, TRef<bool> cancelled)
{
for (int i = 0; i < substepData.SleepCount; i++)
{
if (Volatile.Read(ref cancelled.Value))
{
return true;
}
Thread.Sleep(substepData.SleepMilliseconds);
}
if (substepData.SleepRemainderMilliseconds > 0)
{
if (Volatile.Read(ref cancelled.Value))
{
return true;
}
Thread.Sleep(substepData.SleepRemainderMilliseconds);
}
return Volatile.Read(ref cancelled.Value);
}
}
}