GTK3KeyboardDriver.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using Gdk;
  2. using Gtk;
  3. using System;
  4. using System.Collections.Generic;
  5. using GtkKey = Gdk.Key;
  6. namespace Ryujinx.Input.GTK3
  7. {
  8. public class GTK3KeyboardDriver : IGamepadDriver
  9. {
  10. private readonly Widget _widget;
  11. private readonly HashSet<GtkKey> _pressedKeys;
  12. public GTK3KeyboardDriver(Widget widget)
  13. {
  14. _widget = widget;
  15. _pressedKeys = new HashSet<GtkKey>();
  16. _widget.KeyPressEvent += OnKeyPress;
  17. _widget.KeyReleaseEvent += OnKeyRelease;
  18. }
  19. public string DriverName => "GTK3";
  20. private static readonly string[] _keyboardIdentifers = new string[1] { "0" };
  21. public ReadOnlySpan<string> GamepadsIds => _keyboardIdentifers;
  22. public event Action<string> OnGamepadConnected
  23. {
  24. add { }
  25. remove { }
  26. }
  27. public event Action<string> OnGamepadDisconnected
  28. {
  29. add { }
  30. remove { }
  31. }
  32. protected virtual void Dispose(bool disposing)
  33. {
  34. if (disposing)
  35. {
  36. _widget.KeyPressEvent -= OnKeyPress;
  37. _widget.KeyReleaseEvent -= OnKeyRelease;
  38. }
  39. }
  40. public void Dispose()
  41. {
  42. GC.SuppressFinalize(this);
  43. Dispose(true);
  44. }
  45. [GLib.ConnectBefore]
  46. protected void OnKeyPress(object sender, KeyPressEventArgs args)
  47. {
  48. GtkKey key = (GtkKey)Keyval.ToLower((uint)args.Event.Key);
  49. _pressedKeys.Add(key);
  50. }
  51. [GLib.ConnectBefore]
  52. protected void OnKeyRelease(object sender, KeyReleaseEventArgs args)
  53. {
  54. GtkKey key = (GtkKey)Keyval.ToLower((uint)args.Event.Key);
  55. _pressedKeys.Remove(key);
  56. }
  57. internal bool IsPressed(Key key)
  58. {
  59. if (key == Key.Unbound || key == Key.Unknown)
  60. {
  61. return false;
  62. }
  63. GtkKey nativeKey = GTK3MappingHelper.ToGtkKey(key);
  64. return _pressedKeys.Contains(nativeKey);
  65. }
  66. public void Clear()
  67. {
  68. _pressedKeys.Clear();
  69. }
  70. public IGamepad GetGamepad(string id)
  71. {
  72. if (!_keyboardIdentifers[0].Equals(id))
  73. {
  74. return null;
  75. }
  76. return new GTK3Keyboard(this, _keyboardIdentifers[0], "All keyboards");
  77. }
  78. }
  79. }