GTK3KeyboardDriver.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 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. Dispose(true);
  43. }
  44. [GLib.ConnectBefore]
  45. protected void OnKeyPress(object sender, KeyPressEventArgs args)
  46. {
  47. GtkKey key = (GtkKey)Keyval.ToLower((uint)args.Event.Key);
  48. _pressedKeys.Add(key);
  49. }
  50. [GLib.ConnectBefore]
  51. protected void OnKeyRelease(object sender, KeyReleaseEventArgs args)
  52. {
  53. GtkKey key = (GtkKey)Keyval.ToLower((uint)args.Event.Key);
  54. _pressedKeys.Remove(key);
  55. }
  56. internal bool IsPressed(Key key)
  57. {
  58. if (key == Key.Unbound || key == Key.Unknown)
  59. {
  60. return false;
  61. }
  62. GtkKey nativeKey = GTK3MappingHelper.ToGtkKey(key);
  63. return _pressedKeys.Contains(nativeKey);
  64. }
  65. public IGamepad GetGamepad(string id)
  66. {
  67. if (!_keyboardIdentifers[0].Equals(id))
  68. {
  69. return null;
  70. }
  71. return new GTK3Keyboard(this, _keyboardIdentifers[0], "All keyboards");
  72. }
  73. }
  74. }