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