Skip to content

Commit a69f069

Browse files
committed
added axis support
1 parent 895fda5 commit a69f069

File tree

12 files changed

+206
-224
lines changed

12 files changed

+206
-224
lines changed

Core/Config/ConfigService.cs

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
11
using System;
22
using System.IO;
3+
using NetJoy.Core.Utils;
34
using Nett;
45

56
namespace NetJoy.Core.Config
67
{
7-
public class ConfigService
8+
public static class ConfigService
89
{
910

1011
//the filename for the toml file
11-
private static readonly string fileName = "config.toml";
12-
13-
public ConfigService(){}
12+
private const string FileName = "config.toml";
1413

1514
public static Configuration ReadConfig()
1615
{
1716
try
1817
{
1918

2019
//If the file exists, just read it
21-
if (File.Exists(fileName))
20+
if (File.Exists(FileName))
2221
{
23-
return Toml.ReadFile<Configuration>(fileName);
22+
return Toml.ReadFile<Configuration>(FileName);
2423
}
2524

2625
//write the default config file
27-
Console.WriteLine("No config file detected, making one with default settings");
28-
Toml.WriteFile(new DefaultConfig(), fileName);
26+
Logger.Log("No config file detected, making one with default settings");
27+
Toml.WriteFile(new DefaultConfig(), FileName);
2928

30-
return Toml.ReadFile<Configuration>(fileName);
29+
return Toml.ReadFile<Configuration>(FileName);
3130
}
3231
catch(Exception e)
3332
{
34-
Console.WriteLine(e);
35-
Console.WriteLine("Error reading config file, maybe it's corrupted?");
33+
Logger.LogError(e.Message);
34+
Logger.LogError("Error reading config file, maybe it's corrupted?");
3635
}
3736

3837
return new Configuration();
+54-142
Original file line numberDiff line numberDiff line change
@@ -1,176 +1,88 @@
11
using System;
2+
using System.Threading;
3+
using Nefarius.ViGEm.Client;
4+
using Nefarius.ViGEm.Client.Targets;
5+
using Nefarius.ViGEm.Client.Targets.Xbox360;
6+
using NetJoy.Core.NetJoy.Packets;
7+
using NetJoy.Core.Utils;
8+
using NetJoy.Core.Utils.Controller;
9+
using SharpDX.DirectInput;
210
using vJoyInterfaceWrap;
11+
using static System.Int16;
312

413
namespace NetJoy.Core.NetJoy.Client.Handling
514
{
615

716
// Don't forget to add this
817
public class JoyHandler
918
{
10-
//the port for the controller
11-
private readonly uint _port;
12-
private readonly vJoy _joystick;
13-
14-
public JoyHandler(uint port)
19+
private readonly IXbox360Controller _controller;
20+
public JoyHandler()
1521
{
22+
//Create a new ViGem Client
23+
var client = new ViGEmClient();
1624

17-
//set the joystick port
18-
_port = port;
19-
20-
//create a new vJoy instance
21-
_joystick = new vJoy();
22-
23-
//if vJoy is not enabled
24-
if (!_joystick.vJoyEnabled())
25-
{
26-
//if vJoy is not enabled throw an exception
27-
throw new Exception("vJoy is not enabled!");
28-
}
29-
30-
//log information about the joystick driver
31-
LogJoystickInfo(_joystick);
25+
//create the controller
26+
_controller = client.CreateXbox360Controller();
3227

33-
//check whether the dll version and the driver version match
34-
CheckVersions(_joystick);
35-
36-
//check whether the device is available
37-
var available = IsDeviceAvailable(_joystick, _port);
38-
39-
//if the joystick is not available we can't use it
40-
if (!available)
41-
{
42-
throw new Exception("Joystick is not available and therefore cannot be used! Free the joystick before restarting the program!");
43-
}
44-
45-
//try to acquire the device
46-
var acquired = AcquireDevice(_joystick, port);
28+
//connect the controller?
29+
_controller.Connect();
30+
31+
Thread.Sleep(500);
32+
_controller.SetButtonState(Xbox360Button.X, false);
4733

48-
//if we failed to acquire the device, throw an exception
49-
if (!acquired)
50-
{
51-
throw new Exception($"Failed to acquire device @ port {_port}");
52-
}
5334
}
5435

5536
/// <summary>
56-
/// Try to set the button with the given id to the given sate
57-
/// </summary>
58-
/// <param name="id">of button to change state of</param>
59-
/// <param name="pressed">whether the button should be pressed</param>
60-
public void setButton(int id, bool pressed)
61-
{
62-
try
63-
{
64-
_joystick.SetBtn(pressed, _port, (uint) id);
65-
}
66-
catch
67-
{
68-
//ignored
69-
}
70-
}
71-
/// <summary>
72-
/// Convert a double percentage to an axis value
37+
/// Set a button from the given packet
7338
/// </summary>
74-
/// <param name="percent"></param>
75-
/// <returns>The Joystick value from 0 - 65536</returns>
76-
private int PercentToAxisValue(double percent)
39+
/// <param name="packet">data to use for setting button</param>
40+
public void SetButton(StatePacket packet)
7741
{
78-
return (int) (short.MaxValue * percent);
79-
}
80-
81-
/// <summary>
82-
/// Acquire the device to be fed from joystick input
83-
/// </summary>
84-
/// <param name="joy">driver to use</param>
85-
/// <param name="port">to acquire</param>
86-
/// <returns></returns>
87-
private bool AcquireDevice(vJoy joy, uint port)
88-
{
89-
//get the status of the joystick @ the given port
90-
var status = joy.GetVJDStatus(port);
91-
92-
// Acquire the target
93-
if (status == VjdStat.VJD_STAT_OWN ||
94-
(status == VjdStat.VJD_STAT_FREE) && (!joy.AcquireVJD(port)))
42+
//Get the button from the packet data
43+
var button = ControllerUtils.StateToXbox360Button(packet);
44+
45+
//if we got an incompatible button, return
46+
if (button == null)
9547
{
96-
Console.WriteLine($"Failed to acquire vJoy device number {port}.");
97-
return false;
48+
return;
9849
}
9950

100-
Console.WriteLine($"Acquired: vJoy device number {port}.");
101-
return true;
102-
51+
//get whether the button was pressed
52+
var pressed = ( packet.value == 128 );
53+
54+
//set the given button
55+
_controller.SetButtonState(button, pressed);
10356
}
104-
105-
/// <summary>
106-
/// Check whether the given device is available or not
107-
/// </summary>
108-
/// <param name="joy">to check</param>
109-
/// <param name="id">of the device in question</param>
110-
/// <returns></returns>
111-
private bool IsDeviceAvailable(vJoy joy, uint id)
112-
{
113-
// Get the state of the requested device
114-
var status = joy.GetVJDStatus(id);
115-
switch (status)
116-
{
117-
case VjdStat.VJD_STAT_FREE:
118-
Console.WriteLine("vJoy Device {0} is free\n", id);
119-
return true;
120-
case VjdStat.VJD_STAT_OWN:
121-
Console.WriteLine("vJoy Device {0} is already owned by this feeder\n", id);
122-
return true;
123-
case VjdStat.VJD_STAT_BUSY:
124-
Console.WriteLine(
125-
"vJoy Device {0} is already owned by another feeder\nCannot continue\n", id);
126-
break;
127-
case VjdStat.VJD_STAT_MISS:
128-
Console.WriteLine(
129-
"vJoy Device {0} is not installed or disabled\nCannot continue\n", id);
130-
break;
131-
case VjdStat.VJD_STAT_UNKN:
132-
break;
133-
default:
134-
Console.WriteLine("vJoy Device {0} general error\nCannot continue\n", id);
135-
break;
136-
};
13757

138-
return false;
139-
}
140-
14158
/// <summary>
142-
/// Log information about the joystick driver
59+
/// Set the value of the axis to the given one
14360
/// </summary>
144-
/// <param name="joy"></param>
145-
private void LogJoystickInfo(vJoy joy)
61+
/// <param name="state">The state to use for setting the controller</param>
62+
public void SetAxis(StatePacket state)
14663
{
147-
//log vJoy information
148-
Console.WriteLine("vJoy Information:\nVendor: {0}\nProduct: {1}\nVersion Number: {2}\n",
149-
joy.GetvJoyManufacturerString(),
150-
joy.GetvJoyProductString(),
151-
joy.GetvJoySerialNumberString());
152-
}
153-
154-
/// <summary>
155-
/// Check whether the versions of the driver and the dll used match
156-
/// </summary>
157-
/// <param name="joy">The vJoy instance to check</param>
158-
private void CheckVersions(vJoy joy)
159-
{
160-
//Check if dll and driver version match
161-
uint dllVer = 0, drvVer = 0;
162-
var match = joy.DriverMatch(ref dllVer, ref drvVer);
64+
//convert the state to a controller axis
65+
var axis = ControllerUtils.StateToXbox360Axis(state);
16366

164-
//Log data depending on whether they matched or not
165-
if (match)
67+
try
16668
{
167-
Console.WriteLine("Version of Driver Matches DLL Version ({0:X})\n", dllVer);
69+
70+
var src = state.value - MaxValue;
71+
72+
//convert the ushort value to a short
73+
var value = src > (ushort) MaxValue
74+
? MaxValue
75+
: (short) src;
76+
77+
//NOTE: 0 is the middle point, values go from (-32768 => 32768)
78+
//set the value of the axis from the controller
79+
_controller.SetAxisValue(axis, value);
16880
}
169-
else
81+
catch (Exception e)
17082
{
171-
Console.WriteLine("Version of Driver ({0:X}) does NOT match DLL Version ({1:X})\nIf you experience errors please upgrade/downgrade accordingly!",
172-
drvVer, dllVer);
83+
Logger.LogError(e.Message);
17384
}
85+
17486
}
17587
}
17688
}

0 commit comments

Comments
 (0)