1
1
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 ;
2
10
using vJoyInterfaceWrap ;
11
+ using static System . Int16 ;
3
12
4
13
namespace NetJoy . Core . NetJoy . Client . Handling
5
14
{
6
15
7
16
// Don't forget to add this
8
17
public class JoyHandler
9
18
{
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 ( )
15
21
{
22
+ //Create a new ViGem Client
23
+ var client = new ViGEmClient ( ) ;
16
24
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 ( ) ;
32
27
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 ) ;
47
33
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
- }
53
34
}
54
35
55
36
/// <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
73
38
/// </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 )
77
41
{
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 )
95
47
{
96
- Console . WriteLine ( $ "Failed to acquire vJoy device number { port } .") ;
97
- return false ;
48
+ return ;
98
49
}
99
50
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 ) ;
103
56
}
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\n Cannot continue\n " , id ) ;
126
- break ;
127
- case VjdStat . VJD_STAT_MISS :
128
- Console . WriteLine (
129
- "vJoy Device {0} is not installed or disabled\n Cannot continue\n " , id ) ;
130
- break ;
131
- case VjdStat . VJD_STAT_UNKN :
132
- break ;
133
- default :
134
- Console . WriteLine ( "vJoy Device {0} general error\n Cannot continue\n " , id ) ;
135
- break ;
136
- } ;
137
57
138
- return false ;
139
- }
140
-
141
58
/// <summary>
142
- /// Log information about the joystick driver
59
+ /// Set the value of the axis to the given one
143
60
/// </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 )
146
63
{
147
- //log vJoy information
148
- Console . WriteLine ( "vJoy Information:\n Vendor: {0}\n Product: {1}\n Version 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 ) ;
163
66
164
- //Log data depending on whether they matched or not
165
- if ( match )
67
+ try
166
68
{
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 ) ;
168
80
}
169
- else
81
+ catch ( Exception e )
170
82
{
171
- Console . WriteLine ( "Version of Driver ({0:X}) does NOT match DLL Version ({1:X})\n If you experience errors please upgrade/downgrade accordingly!" ,
172
- drvVer , dllVer ) ;
83
+ Logger . LogError ( e . Message ) ;
173
84
}
85
+
174
86
}
175
87
}
176
88
}
0 commit comments