Skip to content

Experiment in using input events from controllers via MSFS #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions Controlzmo/GameControllers/Wibbleator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Lombok.NET;
using Microsoft.Extensions.Logging;
using Microsoft.FlightSimulator.SimConnect;
using SimConnectzmo;
using System;

namespace Controlzmo.GameControllers
{
[Component, RequiredArgsConstructor]
public partial class Wibbleator : IButtonCallback<T16000mHotas>, IEvent, IEventNotification
{
private readonly ILogger<Wibbleator> log;
public int GetButton() => T16000mHotas.BUTTON_FRONT_LEFT_RED;

public virtual void OnPress(ExtendedSimConnect sc) {
//TODO: can we use this to find new events, if there are any? And perhaps avoid vJoy?
sc.EnumerateControllers();
log.LogCritical($"Asked for wibbleations 1 => {sc.GetLastSentPacketID()}");
}

public void OnRecvControllersList(ExtendedSimConnect sc, SIMCONNECT_RECV_CONTROLLERS_LIST data)
{
try {
log.LogCritical($"wibbleate 1 @ {data.dwEntryNumber}! {data.rgData.Length} v {data.dwArraySize}");
for (var i = 0 ; i < data.dwArraySize; ++i)
{
var item = data.rgData[i] as SIMCONNECT_CONTROLLER_ITEM;
// Name is whatever random text you've altered MSFS to carry for that device.
// DevID is just a sequential number starting from 0 and probably reflecting the order they show up in in the control options screen.
// ProductID is the same as the USB one. No VendorID though, making it not far off useless!
// CompositeID always seems to be 0.
log.LogCritical($"wibbleate 1 [{i}] = {item.DeviceName}@{item.DeviceId} = {item.ProductId} and {item.CompositeID}");
if ("TWCS Throttle" == item.DeviceName)
{
EVENT @event = sc.eventToEnum![this];
var inputDefinition = $"joystick:{item.DeviceId}:button:{T16000mHotas.BUTTON_FRONT_LEFT_RED}";
sc.RemoveInputEvent(GROUP.JUST_MASKABLE, inputDefinition);
//TODO: how do we avoid having down AND up events? Can i use 0 in one of them?
sc.MapInputEventToClientEvent_EX1(GROUP.JUST_MASKABLE, inputDefinition, @event, 0, @event, 0, true);
log.LogCritical($"Mapped event => {sc.GetLastSentPacketID()}");
}
}
} catch (Exception e) { log.LogCritical(e, "Bugger"); }
}

public IEvent GetEvent() => this;

public void OnRecieve(ExtendedSimConnect simConnect, SIMCONNECT_RECV_EVENT data)
{
log.LogCritical("Got the test event!");
}

public string SimEvent() => "gizmo.test1";
}
}
4 changes: 4 additions & 0 deletions Controlzmo/SimConnectzmo/ExtendedSimConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.FlightSimulator.SimConnect;
using Controlzmo;
using System.Text.RegularExpressions;
using Controlzmo.GameControllers;
using Controlzmo.SimConnectzmo;

namespace SimConnectzmo
Expand All @@ -34,6 +35,7 @@ public class ExtendedSimConnect : SimConnect
private IEnumerable<IOnSimStarted>? onSimStartedHandlers;
private IEnumerable<IOnSimFrame>? onSimFrameHandlers;
private IEnumerable<IOnAircraftLoaded> onAircraftLoadedHandlers;
private Wibbleator wibble;
private InputEvents inputEventsHandler;

public bool? IsSimStarted;
Expand All @@ -49,6 +51,7 @@ internal ExtendedSimConnect(string szName, uint UserEventWin32, WaitHandle waitH
OnRecvEventFrame += Handle_OnRecvEventFrame;
OnRecvException += Handle_Exception;
OnRecvSystemState += Handle_OnRecvSystemState;
OnRecvControllersList += (sc, data) => wibble!.OnRecvControllersList((ExtendedSimConnect) sc, data);
OnRecvEnumerateInputEvents += (sc, data) => inputEventsHandler!.OnRecvEnumerateInputEvents((ExtendedSimConnect) sc, data);
OnRecvGetInputEvent += (sc, data) => inputEventsHandler!.OnRecvGetInputEvent((ExtendedSimConnect) sc, data);
OnRecvEnumerateInputEventParams += (sc, data) => inputEventsHandler!.OnRecvEnumerateInputEventParams((ExtendedSimConnect) sc, data);
Expand All @@ -68,6 +71,7 @@ internal ExtendedSimConnect AssignIds(IServiceProvider serviceProvider)
_logging = serviceProvider.GetRequiredService<ILogger<ExtendedSimConnect>>();
serializedExecutor = serviceProvider.GetRequiredService<SerializedExecutor>();
inputEventsHandler = serviceProvider.GetRequiredService<InputEvents>();
wibble = serviceProvider.GetRequiredService<Wibbleator>();

typeToStruct = serviceProvider
.GetServices<IData>()
Expand Down