You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
5.0 KiB

using Mindmagma.Curses;
namespace SCI.CursesWrapper;
public class InputHandler {
/// <summary>
/// Handler called by the input handler routine when a key is pressed
/// </summary>
/// <param name="sender">Sender object of event callback</param>
/// <param name="e">Event arguments with details like the pressed key code</param>
public delegate void KeypressEventHandler (object sender, NCursesKeyPressEventArgs e);
/// <summary>
/// Event handlers for NCurses key presses
/// </summary>
public event KeypressEventHandler? OnKeyPress;
/// <summary>
/// Event Handlers called before the regular OnKeyPress handlers are called
/// </summary>
public event KeypressEventHandler? OnKeyPressPrivileged;
/// <summary>
/// Gets or sets the window this InputHandler is listening to
/// </summary>
private Window? _activeWindow;
public Window? ActiveWindow {
get {
return _activeWindow;
}
set {
_activeWindow = value;
}
}
/// <summary>
/// Root screen to fall back to when no window is active
/// </summary>
private nint rootScreen;
/// <summary>
/// Task running the endless input handler routine
/// </summary>
private Task? listeningTask;
/// <summary>
/// Cancellation Token Source to stop the listening task
/// </summary>
private CancellationTokenSource? listeningRoutineCts;
/// <summary>
/// Class constructor
/// <paramref name="screen">Parent screen this InputHandler is attached to</paramref>
/// </summary>
public InputHandler (nint screen) {
rootScreen = screen;
}
/// <summary>
/// Start listening to key presses and call registered callbacks
/// </summary>
public void StartListening () {
if (IsListening ()) return;
listeningRoutineCts = new CancellationTokenSource ();
listeningTask = Task.Run (_ListeningRoutine);
while (listeningTask.Status == TaskStatus.WaitingToRun) Thread.Sleep (50);
}
/// <summary>
/// Stop listening to key presses
/// </summary>
public void StopListening () {
if (! IsListening ()) return;
if (listeningRoutineCts is null) return;
if (listeningTask is null) return;
listeningRoutineCts.Cancel ();
listeningTask.Wait ();
listeningTask = null;
if (ActiveWindow is not null) {
NCurses.Keypad (ActiveWindow.WindowId, false);
}
}
/// <summary>
/// Returns the current listening status of the input event handler
/// </summary>
/// <returns>True if the event handler routine is listening to key presses</returns>
public bool IsListening () {
return listeningTask is not null && (
listeningTask.Status == TaskStatus.Running ||
listeningTask.Status == TaskStatus.WaitingForActivation
);
}
/// <summary>
/// Prepares the window for proper key press reading
/// </summary>
private void WindowSetup () {
if (ActiveWindow is not null) {
NCurses.Keypad (ActiveWindow.WindowId, true);
NCurses.NoDelay (ActiveWindow.WindowId, false);
} else {
NCurses.Keypad (rootScreen, true);
NCurses.NoDelay (rootScreen, false);
}
}
/// <summary>
/// Undoes any changes the setup routine configured for the active window
/// </summary>
private void WindowTeardown () {
if (ActiveWindow is not null) {
NCurses.Keypad (ActiveWindow.WindowId, false);
NCurses.NoDelay (ActiveWindow.WindowId, true);
} else {
NCurses.Keypad (rootScreen, false);
NCurses.NoDelay (rootScreen, true);
}
}
/// <summary>
/// Actual keypress handler being called as Task by StartListening ()
/// </summary>
private void _ListeningRoutine () {
if (listeningRoutineCts is null) return;
WindowSetup ();
int keyCode = -1;
while (!listeningRoutineCts.Token.IsCancellationRequested) {
if (OnKeyPress is null) {
Console.Title = "No handlers";
} else {
Console.Title = $"Got {OnKeyPress.GetInvocationList ().Length} handlers";
}
if (ActiveWindow is not null) {
keyCode = NCurses.WindowGetChar (ActiveWindow.WindowId);
} else {
keyCode = NCurses.GetChar ();
}
// Do nothing until the key code is larger than -1
if (keyCode < 0) continue;
var eventArgs = new NCursesKeyPressEventArgs (keyCode, ActiveWindow);
// Handle any registered privileged key handlers
if (OnKeyPressPrivileged is not null) {
foreach (KeypressEventHandler h in OnKeyPressPrivileged.GetInvocationList ()) {
h (this, eventArgs);
if (eventArgs.CancelNextEvent == true) {
break;
}
}
}
// Handle any regular registered key handlers
if (OnKeyPress is not null) {
foreach (KeypressEventHandler h in OnKeyPress.GetInvocationList ()) {
h (this, eventArgs);
if (eventArgs.CancelNextEvent == true) continue ;
}
}
}
listeningRoutineCts.Dispose ();
listeningRoutineCts = null;
}
}
public class NCursesKeyPressEventArgs : EventArgs {
private int _keyCode;
public int KeyCode { get { return _keyCode; }}
private Window? _sourceWindow;
public Window? SourceWindow { get { return _sourceWindow; }}
public bool CancelNextEvent { get; set; } = false;
public NCursesKeyPressEventArgs (int keyCode, Window? sourceWindow) {
_keyCode = keyCode;
_sourceWindow = sourceWindow;
}
}