Move some host input internals to Client.Common

This commit is contained in:
YoshiRulz 2020-10-06 08:44:32 +10:00 committed by James Groom
parent 4c8bdf9851
commit 02e0804712
11 changed files with 88 additions and 60 deletions

View File

@ -0,0 +1,15 @@
#nullable enable
using System;
namespace BizHawk.Client.Common
{
[Flags]
public enum ClientInputFocus
{
None = 0,
Mouse = 1,
Keyboard = 2,
Pad = 4
}
}

View File

@ -0,0 +1,23 @@
#nullable enable
using System;
using System.Collections.Generic;
namespace BizHawk.Client.Common
{
/// <remarks>this was easier than trying to make static classes instantiable...</remarks>
public interface HostInputAdapter
{
void DeInitAll();
void FirstInitAll(IntPtr mainFormHandle);
void ReInitGamepads(IntPtr mainFormHandle);
void PreprocessHostGamepads();
void ProcessHostGamepads(Action<string?, bool, ClientInputFocus> handleButton, Action<string?, int> handleAxis);
IEnumerable<KeyEvent> ProcessHostKeyboards();
}
}

View File

@ -0,0 +1,13 @@
#nullable enable
using OpenTK.Input;
namespace BizHawk.Client.Common
{
public struct KeyEvent
{
public Key Key;
public bool Pressed;
}
}

View File

@ -4,26 +4,10 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using static BizHawk.Client.EmuHawk.Input; using BizHawk.Client.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {
/// <remarks>this was easier than trying to make static classes instantiable...</remarks>
public interface HostInputAdapter
{
void DeInitAll();
void FirstInitAll(IntPtr mainFormHandle);
void ReInitGamepads(IntPtr mainFormHandle);
void PreprocessHostGamepads();
void ProcessHostGamepads(Action<string?, bool, InputFocus> handleButton, Action<string?, int> handleAxis);
IEnumerable<KeyEvent> ProcessHostKeyboards();
}
internal sealed class DirectInputAdapter : HostInputAdapter internal sealed class DirectInputAdapter : HostInputAdapter
{ {
public void DeInitAll() public void DeInitAll()
@ -51,18 +35,18 @@ namespace BizHawk.Client.EmuHawk
GamePad360.UpdateAll(); GamePad360.UpdateAll();
} }
public void ProcessHostGamepads(Action<string?, bool, InputFocus> handleButton, Action<string?, int> handleAxis) public void ProcessHostGamepads(Action<string?, bool, ClientInputFocus> handleButton, Action<string?, int> handleAxis)
{ {
foreach (var pad in GamePad360.EnumerateDevices()) foreach (var pad in GamePad360.EnumerateDevices())
{ {
var inputNamePrefix = $"X{pad.PlayerNumber} "; var inputNamePrefix = $"X{pad.PlayerNumber} ";
for (int b = 0, n = pad.NumButtons; b < n; b++) handleButton(inputNamePrefix + pad.ButtonName(b), pad.Pressed(b), InputFocus.Pad); for (int b = 0, n = pad.NumButtons; b < n; b++) handleButton(inputNamePrefix + pad.ButtonName(b), pad.Pressed(b), ClientInputFocus.Pad);
foreach (var (axisName, f) in pad.GetAxes()) handleAxis(inputNamePrefix + axisName, (int) f); foreach (var (axisName, f) in pad.GetAxes()) handleAxis(inputNamePrefix + axisName, (int) f);
} }
foreach (var pad in GamePad.EnumerateDevices()) foreach (var pad in GamePad.EnumerateDevices())
{ {
var inputNamePrefix = $"J{pad.PlayerNumber} "; var inputNamePrefix = $"J{pad.PlayerNumber} ";
for (int b = 0, n = pad.NumButtons; b < n; b++) handleButton(inputNamePrefix + pad.ButtonName(b), pad.Pressed(b), InputFocus.Pad); for (int b = 0, n = pad.NumButtons; b < n; b++) handleButton(inputNamePrefix + pad.ButtonName(b), pad.Pressed(b), ClientInputFocus.Pad);
foreach (var (axisName, f) in pad.GetAxes()) handleAxis(inputNamePrefix + axisName, (int) f); foreach (var (axisName, f) in pad.GetAxes()) handleAxis(inputNamePrefix + axisName, (int) f);
} }
} }
@ -84,11 +68,11 @@ namespace BizHawk.Client.EmuHawk
public void PreprocessHostGamepads() => OTK_GamePad.UpdateAll(); public void PreprocessHostGamepads() => OTK_GamePad.UpdateAll();
public void ProcessHostGamepads(Action<string?, bool, InputFocus> handleButton, Action<string?, int> handleAxis) public void ProcessHostGamepads(Action<string?, bool, ClientInputFocus> handleButton, Action<string?, int> handleAxis)
{ {
foreach (var pad in OTK_GamePad.EnumerateDevices()) foreach (var pad in OTK_GamePad.EnumerateDevices())
{ {
foreach (var but in pad.buttonObjects) handleButton(pad.InputNamePrefix + but.ButtonName, but.ButtonAction(), InputFocus.Pad); foreach (var but in pad.buttonObjects) handleButton(pad.InputNamePrefix + but.ButtonName, but.ButtonAction(), ClientInputFocus.Pad);
foreach (var (axisID, f) in pad.GetAxes()) handleAxis($"{pad.InputNamePrefix}{axisID} Axis", (int) f); foreach (var (axisID, f) in pad.GetAxes()) handleAxis($"{pad.InputNamePrefix}{axisID} Axis", (int) f);
} }
} }

View File

@ -2,6 +2,9 @@
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using System.IO.Pipes; using System.IO.Pipes;
using BizHawk.Client.Common;
using SlimDX.DirectInput; using SlimDX.DirectInput;
// this is not a very safe or pretty protocol, I'm not proud of it // this is not a very safe or pretty protocol, I'm not proud of it

View File

@ -60,15 +60,6 @@ namespace BizHawk.Client.EmuHawk
public class Input public class Input
{ {
[Flags]
public enum InputFocus
{
None = 0,
Mouse = 1,
Keyboard = 2,
Pad = 4
}
public enum AllowInput public enum AllowInput
{ {
None = 0, None = 0,
@ -82,10 +73,10 @@ namespace BizHawk.Client.EmuHawk
/// Why is this receiving a control, but actually using it as a Form (where the WantingMouseFocus is checked?) /// Why is this receiving a control, but actually using it as a Form (where the WantingMouseFocus is checked?)
/// Because later we might change it to work off the control, specifically, if a control is supplied (normally actually a Form will be supplied) /// Because later we might change it to work off the control, specifically, if a control is supplied (normally actually a Form will be supplied)
/// </summary> /// </summary>
public void ControlInputFocus(Control c, InputFocus types, bool wants) public void ControlInputFocus(Control c, ClientInputFocus types, bool wants)
{ {
if (types.HasFlag(InputFocus.Mouse) && wants) _wantingMouseFocus.Add(c); if (types.HasFlag(ClientInputFocus.Mouse) && wants) _wantingMouseFocus.Add(c);
if (types.HasFlag(InputFocus.Mouse) && !wants) _wantingMouseFocus.Remove(c); if (types.HasFlag(ClientInputFocus.Mouse) && !wants) _wantingMouseFocus.Remove(c);
} }
private readonly HashSet<Control> _wantingMouseFocus = new HashSet<Control>(); private readonly HashSet<Control> _wantingMouseFocus = new HashSet<Control>();
@ -191,7 +182,7 @@ namespace BizHawk.Client.EmuHawk
{ {
public LogicalButton LogicalButton; public LogicalButton LogicalButton;
public InputEventType EventType; public InputEventType EventType;
public InputFocus Source; public ClientInputFocus Source;
public override string ToString() public override string ToString()
{ {
return $"{EventType}:{LogicalButton}"; return $"{EventType}:{LogicalButton}";
@ -205,7 +196,7 @@ namespace BizHawk.Client.EmuHawk
private bool _trackDeltas; private bool _trackDeltas;
private bool _ignoreEventsNextPoll; private bool _ignoreEventsNextPoll;
private void HandleButton(string button, bool newState, InputFocus source) private void HandleButton(string button, bool newState, ClientInputFocus source)
{ {
ModifierKey currentModifier = ButtonToModifierKey(button); ModifierKey currentModifier = ButtonToModifierKey(button);
if (EnableIgnoreModifiers && currentModifier != ModifierKey.None) return; if (EnableIgnoreModifiers && currentModifier != ModifierKey.None) return;
@ -350,7 +341,7 @@ namespace BizHawk.Client.EmuHawk
//analyze keys //analyze keys
foreach (var ke in keyEvents) foreach (var ke in keyEvents)
HandleButton(ke.Key.ToString(), ke.Pressed, InputFocus.Keyboard); HandleButton(ke.Key.ToString(), ke.Pressed, ClientInputFocus.Keyboard);
lock (_axisValues) lock (_axisValues)
{ {
@ -373,21 +364,21 @@ namespace BizHawk.Client.EmuHawk
_axisValues["WMouse Y"] = mousePos.Y; _axisValues["WMouse Y"] = mousePos.Y;
var mouseBtns = Control.MouseButtons; var mouseBtns = Control.MouseButtons;
HandleButton("WMouse L", (mouseBtns & MouseButtons.Left) != 0, InputFocus.Mouse); HandleButton("WMouse L", (mouseBtns & MouseButtons.Left) != 0, ClientInputFocus.Mouse);
HandleButton("WMouse C", (mouseBtns & MouseButtons.Middle) != 0, InputFocus.Mouse); HandleButton("WMouse C", (mouseBtns & MouseButtons.Middle) != 0, ClientInputFocus.Mouse);
HandleButton("WMouse R", (mouseBtns & MouseButtons.Right) != 0, InputFocus.Mouse); HandleButton("WMouse R", (mouseBtns & MouseButtons.Right) != 0, ClientInputFocus.Mouse);
HandleButton("WMouse 1", (mouseBtns & MouseButtons.XButton1) != 0, InputFocus.Mouse); HandleButton("WMouse 1", (mouseBtns & MouseButtons.XButton1) != 0, ClientInputFocus.Mouse);
HandleButton("WMouse 2", (mouseBtns & MouseButtons.XButton2) != 0, InputFocus.Mouse); HandleButton("WMouse 2", (mouseBtns & MouseButtons.XButton2) != 0, ClientInputFocus.Mouse);
} }
else else
{ {
#if false // don't do this: for now, it will interfere with the virtualpad. don't do something similar for the mouse position either #if false // don't do this: for now, it will interfere with the virtualpad. don't do something similar for the mouse position either
// unpress all buttons // unpress all buttons
HandleButton("WMouse L", false, InputFocus.Mouse); HandleButton("WMouse L", false, ClientInputFocus.Mouse);
HandleButton("WMouse C", false, InputFocus.Mouse); HandleButton("WMouse C", false, ClientInputFocus.Mouse);
HandleButton("WMouse R", false, InputFocus.Mouse); HandleButton("WMouse R", false, ClientInputFocus.Mouse);
HandleButton("WMouse 1", false, InputFocus.Mouse); HandleButton("WMouse 1", false, ClientInputFocus.Mouse);
HandleButton("WMouse 2", false, InputFocus.Mouse); HandleButton("WMouse 2", false, ClientInputFocus.Mouse);
#endif #endif
} }
} }
@ -419,7 +410,7 @@ namespace BizHawk.Client.EmuHawk
private static bool ShouldSwallow(AllowInput allowInput, InputEvent inputEvent) private static bool ShouldSwallow(AllowInput allowInput, InputEvent inputEvent)
{ {
return allowInput == AllowInput.None || (allowInput == AllowInput.OnlyController && inputEvent.Source != InputFocus.Pad); return allowInput == AllowInput.None || (allowInput == AllowInput.OnlyController && inputEvent.Source != ClientInputFocus.Pad);
} }
public void StartListeningForAxisEvents() public void StartListeningForAxisEvents()

View File

@ -1,6 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using BizHawk.Client.Common;
using SlimDX; using SlimDX;
using SlimDX.DirectInput; using SlimDX.DirectInput;

View File

@ -1,4 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using BizHawk.Client.Common;
using OpenTK.Input; using OpenTK.Input;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
@ -102,10 +105,4 @@ namespace BizHawk.Client.EmuHawk
} }
} }
} }
public struct KeyEvent
{
public Key Key;
public bool Pressed;
}
} }

View File

@ -891,12 +891,12 @@ namespace BizHawk.Client.EmuHawk
protected override void OnActivated(EventArgs e) protected override void OnActivated(EventArgs e)
{ {
base.OnActivated(e); base.OnActivated(e);
Input.Instance.ControlInputFocus(this, Input.InputFocus.Mouse, true); Input.Instance.ControlInputFocus(this, ClientInputFocus.Mouse, true);
} }
protected override void OnDeactivate(EventArgs e) protected override void OnDeactivate(EventArgs e)
{ {
Input.Instance.ControlInputFocus(this, Input.InputFocus.Mouse, false); Input.Instance.ControlInputFocus(this, ClientInputFocus.Mouse, false);
base.OnDeactivate(e); base.OnDeactivate(e);
} }

View File

@ -54,13 +54,13 @@ namespace BizHawk.Client.EmuHawk
protected override void OnActivated(EventArgs e) protected override void OnActivated(EventArgs e)
{ {
base.OnActivated(e); base.OnActivated(e);
Input.Instance.ControlInputFocus(this, Input.InputFocus.Mouse, true); Input.Instance.ControlInputFocus(this, ClientInputFocus.Mouse, true);
} }
protected override void OnDeactivate(EventArgs e) protected override void OnDeactivate(EventArgs e)
{ {
base.OnDeactivate(e); base.OnDeactivate(e);
Input.Instance.ControlInputFocus(this, Input.InputFocus.Mouse, false); Input.Instance.ControlInputFocus(this, ClientInputFocus.Mouse, false);
} }
private void ControllerConfig_Load(object sender, EventArgs e) private void ControllerConfig_Load(object sender, EventArgs e)

View File

@ -23,13 +23,13 @@ namespace BizHawk.Client.EmuHawk
protected override void OnActivated(EventArgs e) protected override void OnActivated(EventArgs e)
{ {
base.OnActivated(e); base.OnActivated(e);
Input.Instance.ControlInputFocus(this, Input.InputFocus.Mouse, true); Input.Instance.ControlInputFocus(this, ClientInputFocus.Mouse, true);
} }
protected override void OnDeactivate(EventArgs e) protected override void OnDeactivate(EventArgs e)
{ {
base.OnDeactivate(e); base.OnDeactivate(e);
Input.Instance.ControlInputFocus(this, Input.InputFocus.Mouse, false); Input.Instance.ControlInputFocus(this, ClientInputFocus.Mouse, false);
} }
private void HotkeyConfig_Load(object sender, EventArgs e) private void HotkeyConfig_Load(object sender, EventArgs e)