Skip to content

Commit

Permalink
XInput support (for XBOX controllers).
Browse files Browse the repository at this point in the history
  • Loading branch information
David Meyer committed Mar 15, 2019
1 parent 2fcc745 commit cdf455f
Show file tree
Hide file tree
Showing 10 changed files with 810 additions and 210 deletions.
204 changes: 105 additions & 99 deletions src/JsPie.Cli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,99 +1,105 @@
using JsPie.Plugins.Keyboard;
using System.Windows.Forms;
using JsPie.Core;
using JsPie.Scripting;
using JsPie.Runtime;
using System.Collections.Generic;
using JsPie.Plugins.Ps3;
using System.Linq;
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using JsPie.Plugins.VJoy;

namespace JsPie.Cli
{
class Program
{
static KeyboardPlugin _keyboardPlugin;
static Ps3Plugin _ps3Plugin;
static VJoyPlugin _vJoyPlugin;
static ConcurrentQueue<IScriptInput> _queue;
static AutoResetEvent _event;
static IJsPieServiceProvider _serviceProvider;
static Task _scriptTask;

static object _lockObject;

static void Main(string[] args)
{
_lockObject = new object();
_queue = new ConcurrentQueue<IScriptInput>();
_event = new AutoResetEvent(false);

_queue.Enqueue(new ScriptInput(new ControlEvent[0]));

using (_keyboardPlugin = new KeyboardPlugin())
using (_ps3Plugin = new Ps3Plugin())
using (_vJoyPlugin = new VJoyPlugin())
{
_keyboardPlugin.ControlEvent += OnControlEvent;
_keyboardPlugin.ControlEvents += OnControlEvents;

_ps3Plugin.ControlEvent += OnControlEvent;
_ps3Plugin.ControlEvents += OnControlEvents;

var serviceProvider = new DefaultJsPieServiceProvider();
serviceProvider.Register<IScriptConsole>(() => new ScriptConsole(ScriptSeverity.Info));
var settings = new ScriptEngineSettings(args.Length > 0 ? args[0] : "D:\\Development\\JsPie\\test.js");
serviceProvider.Register(() => settings);
var directory = new ControllerDirectory(
new IInputPlugin[] { _keyboardPlugin, _ps3Plugin }.SelectMany(p => p.GetControllers()),
new IOutputPlugin[] { _keyboardPlugin, _vJoyPlugin }.SelectMany(p => p.GetControllers()));
serviceProvider.Register(() => directory);
_serviceProvider = serviceProvider;
_scriptTask = Task.Factory.StartNew(Run);

Application.Run();
}
}

private static void OnControlEvent(object sender, ControlEvent e)
{
_queue.Enqueue(new ScriptInput(e));
_event.Set();
}

private static void OnControlEvents(object sender, IReadOnlyList<ControlEvent> e)
{
_queue.Enqueue(new ScriptInput(e));
_event.Set();
}

private static void Run()
{
using (var engine = _serviceProvider.GetRequiredService<IScriptEngine>())
{
engine.Initialize();

while (true)
{
IScriptInput input;
if (_queue.TryDequeue(out input))
{
var output = engine.Run(input);
if (output.WasSuccessful && output.HasValue)
{
_keyboardPlugin.ProcessEvents(output.Value.ControlEvents);
_vJoyPlugin.ProcessEvents(output.Value.ControlEvents);
}
continue;
}

_event.WaitOne();
}
}
}
}
}
using JsPie.Plugins.Keyboard;
using System.Windows.Forms;
using JsPie.Core;
using JsPie.Scripting;
using JsPie.Runtime;
using System.Collections.Generic;
using JsPie.Plugins.Ps3;
using System.Linq;
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using JsPie.Plugins.VJoy;
using JsPie.Plugins.XInput;

namespace JsPie.Cli
{
class Program
{
static KeyboardPlugin _keyboardPlugin;
static Ps3Plugin _ps3Plugin;
static VJoyPlugin _vJoyPlugin;
static XInputPlugin _xInputPlugin;
static ConcurrentQueue<IScriptInput> _queue;
static AutoResetEvent _event;
static IJsPieServiceProvider _serviceProvider;
static Task _scriptTask;

static object _lockObject;

static void Main(string[] args)
{
_lockObject = new object();
_queue = new ConcurrentQueue<IScriptInput>();
_event = new AutoResetEvent(false);

_queue.Enqueue(new ScriptInput(new ControlEvent[0]));

using (_keyboardPlugin = new KeyboardPlugin())
using (_ps3Plugin = new Ps3Plugin())
using (_vJoyPlugin = new VJoyPlugin())
using (_xInputPlugin = new XInputPlugin())
{
_keyboardPlugin.ControlEvent += OnControlEvent;
_keyboardPlugin.ControlEvents += OnControlEvents;

_ps3Plugin.ControlEvent += OnControlEvent;
_ps3Plugin.ControlEvents += OnControlEvents;

_xInputPlugin.ControlEvent += OnControlEvent;
_xInputPlugin.ControlEvents += OnControlEvents;

var serviceProvider = new DefaultJsPieServiceProvider();
serviceProvider.Register<IScriptConsole>(() => new ScriptConsole(ScriptSeverity.Info));
var settings = new ScriptEngineSettings(args.Length > 0 ? args[0] : "D:\\Development\\JsPie\\test.js");
serviceProvider.Register(() => settings);
var directory = new ControllerDirectory(
new IInputPlugin[] { _keyboardPlugin, _ps3Plugin, _xInputPlugin }.SelectMany(p => p.GetControllers()),
new IOutputPlugin[] { _keyboardPlugin, _vJoyPlugin }.SelectMany(p => p.GetControllers()));
serviceProvider.Register(() => directory);
_serviceProvider = serviceProvider;
_scriptTask = Task.Factory.StartNew(Run);

Application.Run();
}
}

private static void OnControlEvent(object sender, ControlEvent e)
{
_queue.Enqueue(new ScriptInput(e));
_event.Set();
}

private static void OnControlEvents(object sender, IReadOnlyList<ControlEvent> e)
{
_queue.Enqueue(new ScriptInput(e));
_event.Set();
}

private static void Run()
{
using (var engine = _serviceProvider.GetRequiredService<IScriptEngine>())
{
engine.Initialize();

while (true)
{
IScriptInput input;
if (_queue.TryDequeue(out input))
{
var output = engine.Run(input);
if (output.WasSuccessful && output.HasValue)
{
_keyboardPlugin.ProcessEvents(output.Value.ControlEvents);
_vJoyPlugin.ProcessEvents(output.Value.ControlEvents);
}
continue;
}

_event.WaitOne();
}
}
}
}
}
Loading

0 comments on commit cdf455f

Please sign in to comment.