/ src / Ryujinx.Input / HLE / TouchScreenManager.cs
TouchScreenManager.cs
  1  using Ryujinx.HLE;
  2  using Ryujinx.HLE.HOS.Services.Hid;
  3  using Ryujinx.HLE.HOS.Services.Hid.Types.SharedMemory.TouchScreen;
  4  using System;
  5  
  6  namespace Ryujinx.Input.HLE
  7  {
  8      public class TouchScreenManager : IDisposable
  9      {
 10          private readonly IMouse _mouse;
 11          private Switch _device;
 12          private bool _wasClicking;
 13  
 14          public TouchScreenManager(IMouse mouse)
 15          {
 16              _mouse = mouse;
 17          }
 18  
 19          public void Initialize(Switch device)
 20          {
 21              _device = device;
 22          }
 23  
 24          public bool Update(bool isFocused, bool isClicking = false, float aspectRatio = 0)
 25          {
 26              if (!isFocused || (!_wasClicking && !isClicking))
 27              {
 28                  // In case we lost focus, send the end touch.
 29                  if (_wasClicking && !isClicking)
 30                  {
 31                      MouseStateSnapshot snapshot = IMouse.GetMouseStateSnapshot(_mouse);
 32                      var touchPosition = IMouse.GetScreenPosition(snapshot.Position, _mouse.ClientSize, aspectRatio);
 33  
 34                      TouchPoint currentPoint = new()
 35                      {
 36                          Attribute = TouchAttribute.End,
 37  
 38                          X = (uint)touchPosition.X,
 39                          Y = (uint)touchPosition.Y,
 40  
 41                          // Placeholder values till more data is acquired
 42                          DiameterX = 10,
 43                          DiameterY = 10,
 44                          Angle = 90,
 45                      };
 46  
 47                      _device.Hid.Touchscreen.Update(currentPoint);
 48  
 49                  }
 50  
 51                  _wasClicking = false;
 52  
 53                  _device.Hid.Touchscreen.Update();
 54  
 55                  return false;
 56              }
 57  
 58              if (aspectRatio > 0)
 59              {
 60                  MouseStateSnapshot snapshot = IMouse.GetMouseStateSnapshot(_mouse);
 61                  var touchPosition = IMouse.GetScreenPosition(snapshot.Position, _mouse.ClientSize, aspectRatio);
 62  
 63                  TouchAttribute attribute = TouchAttribute.None;
 64  
 65                  if (!_wasClicking && isClicking)
 66                  {
 67                      attribute = TouchAttribute.Start;
 68                  }
 69                  else if (_wasClicking && !isClicking)
 70                  {
 71                      attribute = TouchAttribute.End;
 72                  }
 73  
 74                  TouchPoint currentPoint = new()
 75                  {
 76                      Attribute = attribute,
 77  
 78                      X = (uint)touchPosition.X,
 79                      Y = (uint)touchPosition.Y,
 80  
 81                      // Placeholder values till more data is acquired
 82                      DiameterX = 10,
 83                      DiameterY = 10,
 84                      Angle = 90,
 85                  };
 86  
 87                  _device.Hid.Touchscreen.Update(currentPoint);
 88  
 89                  _wasClicking = isClicking;
 90  
 91                  return true;
 92              }
 93  
 94              return false;
 95          }
 96  
 97          public void Dispose()
 98          {
 99              GC.SuppressFinalize(this);
100          }
101      }
102  }