1
+ using System ;
2
+ using vJoyInterfaceWrap ;
3
+
4
+ namespace NetJoy . Core . NetJoy . Client . Handling
5
+ {
6
+
7
+ // Don't forget to add this
8
+ public class JoyHandler
9
+ {
10
+ //the port for the controller
11
+ private readonly uint _port ;
12
+ private readonly vJoy _joystick ;
13
+
14
+ public JoyHandler ( uint port )
15
+ {
16
+
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 ) ;
32
+
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 ) ;
47
+
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
+ }
54
+
55
+ /// <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
73
+ /// </summary>
74
+ /// <param name="percent"></param>
75
+ /// <returns>The Joystick value from 0 - 65536</returns>
76
+ private int PercentToAxisValue ( double percent )
77
+ {
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 ) ) )
95
+ {
96
+ Console . WriteLine ( $ "Failed to acquire vJoy device number { port } .") ;
97
+ return false ;
98
+ }
99
+
100
+ Console . WriteLine ( $ "Acquired: vJoy device number { port } .") ;
101
+ return true ;
102
+
103
+ }
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
+
138
+ return false ;
139
+ }
140
+
141
+ /// <summary>
142
+ /// Log information about the joystick driver
143
+ /// </summary>
144
+ /// <param name="joy"></param>
145
+ private void LogJoystickInfo ( vJoy joy )
146
+ {
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 ) ;
163
+
164
+ //Log data depending on whether they matched or not
165
+ if ( match )
166
+ {
167
+ Console . WriteLine ( "Version of Driver Matches DLL Version ({0:X})\n " , dllVer ) ;
168
+ }
169
+ else
170
+ {
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 ) ;
173
+ }
174
+ }
175
+ }
176
+ }
0 commit comments