-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathTiming.c
422 lines (333 loc) · 12.4 KB
/
Timing.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/***************************************************************************
* PPJoy Virtual Joystick for Microsoft Windows *
* Copyright (C) 2011 Deon van der Westhuysen *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
***************************************************************************/
/***************************************************************************/
/** **/
/** Parallel port joystick driver, (C) Deon van der Westhuysen 2002 **/
/** **/
/** Mapping.c Routines to handle remapping of joystick axes and buttons **/
/** **/
/** **/
/** **/
/** **/
/** Portions of this file derived from the Windows 2000 DDK **/
/** **/
/***************************************************************************/
#include "PPortJoy.h"
//#include "JoyDefs.h"
#include <wchar.h>
#ifdef ALLOC_PRAGMA
#pragma alloc_text (PAGE,PPJOY_ComputeTimingSize)
#pragma alloc_text (PAGE,PPJoy_InitTimingValues)
#pragma alloc_text (PAGE,PPJoy_DeleteTimingFromReg)
#pragma alloc_text (PAGE,PPJoy_DeleteDeviceTiming)
#pragma alloc_text (PAGE,PPJoy_DeleteDriverTiming)
#pragma alloc_text (PAGE,PPJoy_WriteTimingToReg)
#pragma alloc_text (PAGE,PPJoy_WriteDeviceTiming)
#pragma alloc_text (PAGE,PPJoy_WriteDriverTiming)
#pragma alloc_text (PAGE,PPJoy_ReadTimingFromReg)
#pragma alloc_text (PAGE,PPJoy_ReadDeviceTiming)
#pragma alloc_text (PAGE,PPJoy_ReadDriverTiming)
#pragma alloc_text (PAGE,PPJoy_BuildDefaultTiming)
#pragma alloc_text (PAGE,PPJoy_LoadJoystickTiming)
#endif
ULONG PPJOY_ComputeTimingSize (UCHAR JoyType)
{
PAGED_CODE();
switch (JoyType)
{
case IF_GENESISLIN:
case IF_GENESISDPP:
case IF_GENESISNTP:
case IF_GENESISCC:
case IF_GENESISSNES:
case IF_GENESISDPP6:
return sizeof(TIMING_GENESIS);
case IF_SNESPADLIN:
case IF_SNESPADDPP:
case IF_POWERPADLIN:
return sizeof(TIMING_SNES);
case IF_PSXPBLIB:
case IF_PSXDPADPRO:
case IF_PSXLINUX:
case IF_PSXNTPADXP:
case IF_PSXMEGATAP:
return sizeof(TIMING_PSX);
case IF_LPTSWITCH:
return sizeof(TIMING_LPTSWITCH);
case IF_FMSBUDDYBOX:
return sizeof(TIMING_FMSPPM);
default:
return 0;
}
}
void PPJoy_InitTimingValues (IN PDEVICE_OBJECT DeviceObject)
{
PDEVICE_EXTENSION DeviceExtension;
LARGE_INTEGER TickCount;
PAGED_CODE();
DeviceExtension= GET_MINIDRIVER_DEVICE_EXTENSION(DeviceObject);
if (DeviceExtension->Config.JoyType==IF_FMSBUDDYBOX)
{
PTIMING_FMSPPM PPMTiming;
PPPMBUDDYBOX PPMParams;
PPMTiming= (PTIMING_FMSPPM) DeviceExtension->Timing;
PPMParams= &(DeviceExtension->Param.PPMBuddyBox);
KeQueryPerformanceCounter(&TickCount);
PPMParams->MaxPulseTicks.QuadPart= (TickCount.QuadPart*(PPMTiming->MaxPulseWidth/*us*/)/1000000);
PPMParams->MinPulseTicks.QuadPart= (TickCount.QuadPart*(PPMTiming->MinPulseWidth/*us*/)/1000000);
PPMParams->MinSyncTicks.QuadPart= (TickCount.QuadPart*(PPMTiming->MinSyncWidth/*us*/)/1000000);
}
}
NTSTATUS PPJoy_DeleteTimingFromReg (UCHAR JoyType, HANDLE RegKey)
{
NTSTATUS ntStatus;
UNICODE_STRING ValueName;
WCHAR ValueNameBuffer[128];
PAGED_CODE();
swprintf (ValueNameBuffer,L"TimingV1_%02X",JoyType);
RtlInitUnicodeString(&ValueName,ValueNameBuffer);
ntStatus= ZwSetValueKey (RegKey,&ValueName,0,REG_BINARY,ValueNameBuffer,0);
return ntStatus;
}
NTSTATUS PPJoy_DeleteDeviceTiming (IN PDEVICE_OBJECT DeviceObject, UCHAR JoyType)
{
HANDLE RegKey;
NTSTATUS ntStatus;
RegKey= PPJoy_OpenDeviceRegKey(DeviceObject);
if (!RegKey)
return STATUS_UNSUCCESSFUL;
ntStatus= PPJoy_DeleteTimingFromReg (JoyType,RegKey);
ZwClose(RegKey);
return ntStatus;
}
NTSTATUS PPJoy_DeleteDriverTiming (IN PDEVICE_OBJECT DeviceObject, UCHAR JoyType)
{
HANDLE RegKey;
NTSTATUS ntStatus;
RegKey= PPJoy_OpenDriverRegKey();
if (!RegKey)
return STATUS_UNSUCCESSFUL;
ntStatus= PPJoy_DeleteTimingFromReg (JoyType,RegKey);
ZwClose(RegKey);
return ntStatus;
}
NTSTATUS PPJoy_WriteTimingToReg (PJOYTIMING Timing, UCHAR JoyType, HANDLE RegKey)
{
NTSTATUS ntStatus;
UNICODE_STRING ValueName;
WCHAR ValueNameBuffer[128];
ULONG TimingSize;
PAGED_CODE();
swprintf (ValueNameBuffer,L"TimingV1_%02X",JoyType);
RtlInitUnicodeString(&ValueName,ValueNameBuffer);
TimingSize= PPJOY_ComputeTimingSize(JoyType);
if (!TimingSize)
return STATUS_SUCCESS;
ntStatus= ZwSetValueKey (RegKey,&ValueName,0,REG_BINARY,Timing,TimingSize);
return ntStatus;
}
NTSTATUS PPJoy_WriteDeviceTiming (IN PDEVICE_OBJECT DeviceObject, PJOYTIMING Timing, UCHAR JoyType)
{
HANDLE RegKey;
NTSTATUS ntStatus;
RegKey= PPJoy_OpenDeviceRegKey(DeviceObject);
if (!RegKey)
return STATUS_UNSUCCESSFUL;
ntStatus= PPJoy_WriteTimingToReg (Timing,JoyType,RegKey);
ZwClose(RegKey);
return ntStatus;
}
NTSTATUS PPJoy_WriteDriverTiming (IN PDEVICE_OBJECT DeviceObject, PJOYTIMING Timing, UCHAR JoyType)
{
HANDLE RegKey;
NTSTATUS ntStatus;
RegKey= PPJoy_OpenDriverRegKey();
if (!RegKey)
return STATUS_UNSUCCESSFUL;
ntStatus= PPJoy_WriteTimingToReg (Timing,JoyType,RegKey);
ZwClose(RegKey);
return ntStatus;
}
PJOYTIMING PPJoy_ReadTimingFromReg (UCHAR JoyType, HANDLE RegKey)
{
NTSTATUS ntStatus;
ULONG NumRead;
UNICODE_STRING ValueName;
WCHAR ValueNameBuffer[128];
UCHAR Buffer[sizeof(KEY_VALUE_PARTIAL_INFORMATION)+1024];
ULONG TimingSize;
PJOYTIMING Timing;
PAGED_CODE();
Timing= NULL;
swprintf (ValueNameBuffer,L"TimingV1_%02X",JoyType);
RtlInitUnicodeString(&ValueName,ValueNameBuffer);
NumRead= 0;
ntStatus= ZwQueryValueKey(RegKey,&ValueName,KeyValuePartialInformation,Buffer,sizeof(Buffer),&NumRead);
if(!NT_SUCCESS(ntStatus))
{
PPJOY_DBGPRINT (FILE_TIMING|PPJOY_WARN, ("Error 0x%X reading value %S. NumRead= %d",ntStatus,ValueName.Buffer,NumRead) );
goto Exit;
}
if (((PKEY_VALUE_PARTIAL_INFORMATION)Buffer)->Type!=REG_BINARY)
{
PPJOY_DBGPRINT (FILE_TIMING|PPJOY_WARN, ("Wrong registry datatype %d reading value %S",((PKEY_VALUE_PARTIAL_INFORMATION)Buffer)->Type,ValueName.Buffer) );
goto Exit;
}
if (((PKEY_VALUE_PARTIAL_INFORMATION)Buffer)->DataLength==0)
{
PPJOY_DBGPRINT (FILE_TIMING|PPJOY_BABBLE, ("Empty registry key, ignoring") );
goto Exit;
}
TimingSize= PPJOY_ComputeTimingSize(JoyType);
if (((PKEY_VALUE_PARTIAL_INFORMATION)Buffer)->DataLength!=TimingSize)
{
PPJOY_DBGPRINT (FILE_TIMING|PPJOY_WARN, ("Invalid registry value size. Value size= %d expected %d",((PKEY_VALUE_PARTIAL_INFORMATION)Buffer)->DataLength,TimingSize) );
goto Exit;
}
Timing= ExAllocatePoolWithTag (NonPagedPool,TimingSize,PPJOY_POOL_TAG);
if (!Timing)
{
PPJOY_DBGPRINT (FILE_TIMING|PPJOY_ERROR, ("Cannot allocate nonpaged pool to store joystick timing. Help!") );
goto Exit;
}
RtlCopyMemory (Timing,((PKEY_VALUE_PARTIAL_INFORMATION)Buffer)->Data,TimingSize);
Exit:
return Timing;
}
PJOYTIMING PPJoy_ReadDeviceTiming (IN PDEVICE_OBJECT DeviceObject, UCHAR JoyType)
{
HANDLE RegKey;
PJOYTIMING Timing;
RegKey= PPJoy_OpenDeviceRegKey(DeviceObject);
if (!RegKey)
return NULL;
Timing= PPJoy_ReadTimingFromReg(JoyType,RegKey);
ZwClose(RegKey);
return Timing;
}
PJOYTIMING PPJoy_ReadDriverTiming (IN PDEVICE_OBJECT DeviceObject, UCHAR JoyType)
{
HANDLE RegKey;
PJOYTIMING Timing;
RegKey= PPJoy_OpenDriverRegKey();
if (!RegKey)
return NULL;
Timing= PPJoy_ReadTimingFromReg(JoyType,RegKey);
ZwClose(RegKey);
return Timing;
}
PJOYTIMING PPJoy_BuildDefaultTiming (UCHAR JoyType)
{
NTSTATUS ntStatus;
PJOYTIMING Timing;
// PJOYSTICK_MAP SrcMapping;
int TimingSize;
PAGED_CODE();
TimingSize= PPJOY_ComputeTimingSize(JoyType);
if (!TimingSize)
return NULL;
Timing= ExAllocatePoolWithTag (NonPagedPool,TimingSize,PPJOY_POOL_TAG);
if (!Timing)
{
PPJOY_DBGPRINT (FILE_TIMING|PPJOY_ERROR, ("Cannot allocate nonpaged pool to store joystick timing. Help!") );
goto Exit;
}
switch (JoyType)
{
case IF_GENESISLIN:
case IF_GENESISDPP:
case IF_GENESISNTP:
case IF_GENESISCC:
case IF_GENESISSNES:
case IF_GENESISDPP6:
((PTIMING_GENESIS)Timing)->BitDelay= GENESIS_DELAY;
((PTIMING_GENESIS)Timing)->Bit6Delay= GENESIS6_DELAY;
break;
case IF_SNESPADLIN:
case IF_SNESPADDPP:
case IF_POWERPADLIN:
((PTIMING_SNES)Timing)->BitDelay= SNES_BITDELAY;
((PTIMING_SNES)Timing)->SetupDelay= SNES_SETUPDELAY;
break;
case IF_PSXPBLIB:
case IF_PSXDPADPRO:
case IF_PSXLINUX:
case IF_PSXNTPADXP:
case IF_PSXMEGATAP:
((PTIMING_PSX)Timing)->Ack1Delay= PSXACK1_DELAY;
((PTIMING_PSX)Timing)->AckXDelay= PSXACKX_DELAY;
((PTIMING_PSX)Timing)->BitDelay= PSXBIT_DELAY;
((PTIMING_PSX)Timing)->SelDelay= PSXSEL_DELAY;
((PTIMING_PSX)Timing)->TailDelay= PSXTAIL_DELAY;
break;
case IF_LPTSWITCH:
((PTIMING_LPTSWITCH)Timing)->RowDelay= LPTSWITCH_DELAY;
break;
case IF_FMSBUDDYBOX:
((PTIMING_FMSPPM)Timing)->MaxPulseWidth= FMS_MAXPULSE;
((PTIMING_FMSPPM)Timing)->MinPulseWidth= FMS_MINPULSE;
((PTIMING_FMSPPM)Timing)->MinSyncWidth= FMS_MINSYNC;
break;
}
Exit:
return Timing;
}
NTSTATUS PPJoy_LoadJoystickTiming (IN PDEVICE_OBJECT DeviceObject)
{
NTSTATUS ntStatus;
PDEVICE_EXTENSION DeviceExtension;
PJOYTIMING OldTiming;
PJOYTIMING NewTiming;
PAGED_CODE();
DeviceExtension= GET_MINIDRIVER_DEVICE_EXTENSION(DeviceObject);
if (!PPJOY_ComputeTimingSize(DeviceExtension->Config.JoyType))
{
PPJOY_DBGPRINT (FILE_TIMING|PPJOY_BABBLE2, ("Joystick timing not required for this device") );
return STATUS_SUCCESS;
}
PPJOY_DBGPRINT (FILE_TIMING|PPJOY_BABBLE2, ("Attempting to read joystick timing from device registry key") );
NewTiming= PPJoy_ReadDeviceTiming(DeviceObject,DeviceExtension->Config.JoyType);
if (NewTiming)
{
PPJOY_DBGPRINT (FILE_TIMING|PPJOY_BABBLE, ("Joystick timing successfully read from the device registry") );
goto UpdateTiming;
}
PPJOY_DBGPRINT (FILE_TIMING|PPJOY_BABBLE2, ("Attempting to read joystick timing from driver registry key") );
NewTiming= PPJoy_ReadDriverTiming(DeviceObject,DeviceExtension->Config.JoyType);
if (NewTiming)
{
PPJOY_DBGPRINT (FILE_TIMING|PPJOY_BABBLE, ("Joystick timing successfully read from the driver registry") );
goto UpdateTiming;
}
PPJOY_DBGPRINT (FILE_TIMING|PPJOY_BABBLE2, ("Building default joystick timing for device") );
NewTiming= PPJoy_BuildDefaultTiming(DeviceExtension->Config.JoyType);
if (NewTiming)
{
PPJOY_DBGPRINT (FILE_TIMING|PPJOY_BABBLE, ("Joystick timing successfully built manually") );
goto UpdateTiming;
}
return STATUS_UNSUCCESSFUL;
UpdateTiming:
OldTiming= DeviceExtension->Timing;
DeviceExtension->Timing= NewTiming;
PPJoy_InitTimingValues (DeviceObject);
if (OldTiming)
ExFreePool(OldTiming);
return STATUS_SUCCESS;
}