102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
devices.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
8#include "pcdogs/math/types.h"
11#include <math.h>
12#include <stdarg.h>
13#include <string.h>
14
15// Original pcdogs.exe: 0x41a070
16// Creates a DirectInput 7 interface and returns its COM reference only when the HRESULT is exactly zero. Other results return null.
17IDirectInputA *DInput_CreateInterface(HINSTANCE h_instance)
18{
19 struct IDirectInputA *result;
20
21 return DInput_CreateA(h_instance, INPUT_DIRECTINPUT_VERSION, &result, NULL) ? NULL : result;
22}
23
24// Original pcdogs.exe: 0x41a0a0
25// Creates a base input device, queries its Device2A interface, and releases the base reference after the query. The returned Device2A reference belongs to the caller.
26IDirectInputDevice2A *DInput_CreateDevice2A(IDirectInputA *dinput_interface, GUID device_guid)
27{
28 IDirectInputDeviceA *base_device;
29
30 if (dinput_interface->lpVtbl->CreateDevice(dinput_interface, &device_guid, &base_device, NULL))
31 return NULL;
32
33 IDirectInputDevice2A *result = DInput_QueryDevice2AInterface(base_device);
34 base_device->lpVtbl->Release(base_device);
35
36 return result;
37}
38
39// Original pcdogs.exe: 0x41a0e0
40// Queries a Device2A interface through the same slot holding the incoming base pointer and returns the overwritten slot. It ignores HRESULT and relies on the COM failure-output contract to clear that slot.
41IDirectInputDevice2A *DInput_QueryDevice2AInterface(IDirectInputDeviceA *device)
42{
43 /* The native stack slot initially holds the input and is replaced by QueryInterface. */
44 void *result = device;
45
46 device->lpVtbl->QueryInterface(device, &DInput_Device2AIID, &result);
47
48 return (IDirectInputDevice2A *)result;
49}
50
51// Original pcdogs.exe: 0x41a100
52// Sets the device's cooperative level and returns true only for HRESULT zero. Device and window arguments are not validated.
53BOOL Input_SetDeviceCooperativeLevel(IDirectInputDeviceA *device, HWND hwnd, DWORD flags)
54{
55 // Convert HRESULT to boolean using NEG+SBB+INC sequence. S_OK (0) becomes 1 (TRUE), any error code becomes 0
56 // (FALSE).
57 return !(device->lpVtbl->SetCooperativeLevel(device, hwnd, flags));
58}
59
60// Original pcdogs.exe: 0x41a120
61// Sets the device data format and returns true only for HRESULT zero. Device and format pointers are not validated.
62BOOL Input_SetDeviceDataFormat(IDirectInputDeviceA *device, DIDATAFORMAT const *data_format)
63{
64 return !(device->lpVtbl->SetDataFormat(device, data_format));
65}
66
67// Original pcdogs.exe: 0x41a140
68// Acquires the input device and returns true only for HRESULT zero. The device pointer is not validated.
69BOOL Input_AcquireDevice(IDirectInputDeviceA *device)
70{
71 // Convert HRESULT to boolean: NEG + SBB + INC converts S_OK (0) to TRUE (1), any failure HRESULT to FALSE
72 // (0). NEG sets carry flag if non-zero, SBB creates -1 if carry else 0, INC adjusts to 1 or 0.
73 // Return true if HRESULT is S_OK (0), false otherwise
74 return !(device->lpVtbl->Acquire(device));
75}
76
77// Original pcdogs.exe: 0x41a150
78// Allocates a device-state buffer and returns it only when GetDeviceState returns exactly zero. Allocation failure is unchecked, rejected queries leak their buffer, and successful callers own the allocation.
79void *Input_GetDeviceStateBuffer(IDirectInputDeviceA *device, DWORD state_size)
80{
81 void *state_buffer = CRT_malloc(state_size);
82
83 return device->lpVtbl->GetDeviceState(device, state_size, state_buffer) == 0 ? state_buffer : NULL;
84}
85
86// Original pcdogs.exe: 0x41a180
87// Appends each enumerated joystick GUID to the caller's buffer and continues enumeration. Null inputs are rejected, but the output count has no capacity check.
88BOOL __stdcall DInput_EnumJoystickDeviceCallback(DIDEVICEINSTANCEA const *device_instance, void *context)
89{
90 struct DInput_DeviceEnumContext *enum_context = context;
91 if (!device_instance || !enum_context)
92 return 0;
93
94 enum_context->guid_list[enum_context->count] = device_instance->guidInstance;
95 enum_context->count += 1;
97
98 return 1;
99}
100
101// Original pcdogs.exe: 0x41a1d0
102// Sets a joystick axis range by object offset using the DirectInput range property. It returns true only for HRESULT zero.
103BOOL Input_SetJoystickAxisRangeProperty(IDirectInputDeviceA *device, DWORD axis_offset, LONG min_value, LONG max_value)
104{
105 DIPROPRANGE range_property;
106
107 range_property.diph.dwObj = axis_offset;
108 range_property.lMax = max_value;
109 range_property.lMin = min_value;
110 IDirectInputDeviceAVtbl *vtable = device->lpVtbl;
111 range_property.diph.dwSize = sizeof(DIPROPRANGE);
112 range_property.diph.dwHeaderSize = sizeof(DIPROPHEADER);
113 range_property.diph.dwHow = DIPH_BYOFFSET;
114
115 return !(vtable->SetProperty(device, DIPROP_RANGE, &range_property.diph));
116}
117
118// Original pcdogs.exe: 0x41a220
119// Sets the joystick X-axis range and forwards the range setter's result.
120BOOL Input_SetJoystickXAxisRange(IDirectInputDeviceA *device, LONG min_value, LONG max_value)
121{
122 return Input_SetJoystickAxisRangeProperty(device, DIJOFS_X, min_value, max_value);
123}
124
125// Original pcdogs.exe: 0x41a240
126// Sets the joystick Y-axis range and forwards the range setter's result.
127BOOL Input_SetJoystickYAxisRange(IDirectInputDeviceA *device, LONG min_value, LONG max_value)
128{
129 return Input_SetJoystickAxisRangeProperty(device, DIJOFS_Y, min_value, max_value);
130}
131
132// Original pcdogs.exe: 0x41a260
133// Sets the joystick Z-axis range and forwards the range setter's result.
134BOOL Input_SetJoystickZAxisRange(IDirectInputDeviceA *device, LONG min_value, LONG max_value)
135{
136 return Input_SetJoystickAxisRangeProperty(device, DIJOFS_Z, min_value, max_value);
137}
138
139// Original pcdogs.exe: 0x41a280
140// Sets the joystick Rz-axis range and forwards the range setter's result.
141BOOL Input_SetJoystickRzAxisRange(IDirectInputDeviceA *device, LONG min_value, LONG max_value)
142{
143 return Input_SetJoystickAxisRangeProperty(device, DIJOFS_RZ, min_value, max_value);
144}
145
146// Original pcdogs.exe: 0x41a2f0
147// Enumerates attached DirectInput 7 joysticks into the caller's GUID buffer and returns the collected count. Enumeration does not bound the output buffer.
148int32_t DInput_EnumerateAttachedJoysticks(IDirectInputA *direct_input, GUID *device_guid_buffer)
149{
150 struct DInput_DeviceEnumContext enum_context = {.guid_list = device_guid_buffer};
151 direct_input->lpVtbl->EnumDevices(direct_input, DIDEVTYPE_JOYSTICK, DInput_EnumJoystickDeviceCallback,
152 &enum_context, DIEDFL_ATTACHEDONLY);
153 return enum_context.count;
154}
155
156// Original pcdogs.exe: 0x41a330
157// Sets exclusive foreground cooperation for the joystick and forwards the result.
158BOOL Input_SetExclusiveForegroundCooperativeLevel(IDirectInputDeviceA *device, HWND hwnd)
159{
160 return Input_SetDeviceCooperativeLevel(device, hwnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND);
161}
162
163// Original pcdogs.exe: 0x41a350
164// Selects the basic DIJOYSTATE data format and forwards the format setter's result.
165BOOL Input_SetJoystickDataFormat(IDirectInputDeviceA *device)
166{
168}
169
170// Original pcdogs.exe: 0x41a370
171// Selects the extended DIJOYSTATE2 data format and forwards the format setter's result.
172BOOL Input_SetJoystick2DataFormat(IDirectInputDeviceA *device)
173{
175}
176
177// Original pcdogs.exe: 0x41a390
178// Polls the joystick and requests a newly allocated state buffer, retrying once after acquisition on failure. Poll status is ignored, failed state queries leak allocations, and successful callers must free the returned buffer.
179struct Input_JoystickState *Input_PollJoystickState(IDirectInputDevice2A *device)
180{
181 IDirectInputDeviceA *base_device = (IDirectInputDeviceA *)device;
182 device->lpVtbl->Poll(device);
183
184 struct Input_JoystickState *state = Input_GetDeviceStateBuffer(base_device, sizeof(*state));
185 if (!state && Input_AcquireDevice(base_device))
186 state = Input_GetDeviceStateBuffer(base_device, sizeof(*state));
187
188 return state;
189}
190
191// Original pcdogs.exe: 0x41a3d0
192// Returns the joystick state's X axis without checking the state pointer.
194{
195 // Returns X-axis value from DIJOYSTATE at offset 0x00. Range approx -1000 to +1000. Negative=left,
196 // positive=right
197 return joy_state->pos.x;
198}
199
200// Original pcdogs.exe: 0x41a3e0
201// Returns the joystick state's Y axis without checking the state pointer.
203{
204 // Returns Y-axis value from DIJOYSTATE at offset 0x04. Range approx -1000 to +1000. Negative=up,
205 // positive=down
206 return joy_state->pos.y;
207}
208
209// Original pcdogs.exe: 0x41a3f0
210// Returns the joystick state's Rz axis without checking the state pointer.
212{
213 // Returns Z-rotation value from DIJOYSTATE at offset 0x14. Range approx -1000 to +1000. Used for POV hat
214 // or secondary stick
215 return joy_state->rot.z;
216}
217
218// Original pcdogs.exe: 0x41a400
219// Returns a raw joystick button byte without checking the state pointer or button index.
220uint8_t Input_GetJoystickButtonByte(struct Input_JoystickState *joy_state, int32_t button_index)
221{
222 // Read byte from state buffer at offset +0x30 + byteIndex. State buffer contains device capability flags.
223 return joy_state->rgb_buttons[button_index];
224}
225
226// Original pcdogs.exe: 0x41a410
227// Returns whether the selected joystick button byte has its high bit set. It adds no pointer or index validation.
228BOOL Input_IsJoystickButtonPressed(struct Input_JoystickState *joy_state, int32_t button_index)
229{
230 return (uint32_t)Input_GetJoystickButtonByte(joy_state, button_index) >> INPUT_BUTTON_DOWN_BIT_INDEX;
231}
232
233// Original pcdogs.exe: 0x41a430
234// Enumerates and creates a Device2A joystick, configures its format and cooperation, and optionally creates force feedback before acquisition. Negative indices are not rejected, post-creation failures leak references, and discovery availability can remain set without a valid effect.
235IDirectInputDevice2A *DInput_CreateConfiguredJoystickDevice(IDirectInputA *direct_input, HWND hwnd, int32_t device_index, int32_t setup_mode)
236{
237 GUID device_guids[INPUT_DEVICE_ENUM_CAPACITY];
238
239 int32_t count = DInput_EnumerateForceFeedbackJoysticks(direct_input, device_guids);
240
241 if (!count)
242 count = DInput_EnumerateAttachedJoysticks(direct_input, device_guids);
243
244 if (device_index >= count)
245 return NULL;
246
247 /* The original passes all four GUID words by value. */
248 IDirectInputDevice2A *device = DInput_CreateDevice2A(direct_input, device_guids[device_index]);
249
250 if (!device)
251 return NULL;
252
253 if (setup_mode == INPUT_JOYSTICK_BASIC)
254 {
255 if (!Input_SetJoystickDataFormat((IDirectInputDeviceA *)device))
256 return NULL;
257 }
258 else
259 {
260 if (setup_mode != INPUT_JOYSTICK_EXTENDED || !Input_SetJoystick2DataFormat((IDirectInputDeviceA *)device))
261 return NULL;
262 }
263
264 if (!Input_SetExclusiveForegroundCooperativeLevel((IDirectInputDeviceA *)device, hwnd))
265 return NULL;
266
268 {
269 DIPROPDWORD auto_center = {{sizeof(DIPROPDWORD), sizeof(DIPROPHEADER), 0, 0}, 0};
270
271 if (device->lpVtbl->SetProperty(device, DIPROP_AUTOCENTER, &auto_center.diph) < 0)
272 return NULL;
273
274 DWORD axes[2] = {DIJOFS_X, DIJOFS_Y};
275
276 LONG direction[2] = {0, 0};
277 DICONSTANTFORCE force = {0};
278 DIEFFECT effect = {0};
279 effect.dwSize = sizeof(DIEFFECT);
280 effect.dwFlags = DIEFF_OBJECTOFFSETS | DIEFF_CARTESIAN;
281 effect.dwDuration = INFINITE;
282 effect.dwGain = DI_FFNOMINALMAX;
283 effect.dwTriggerButton = DIEB_NOTRIGGER;
284 effect.cAxes = 2;
285 effect.rgdwAxes = axes;
286 effect.rglDirection = direction;
287 effect.cbTypeSpecificParams = sizeof(DICONSTANTFORCE);
288 effect.lpvTypeSpecificParams = &force;
289
290 if (device->lpVtbl->CreateEffect(device, (const GUID *)&Input_GUID_ConstantForce, &effect,
292 return NULL;
293 }
294
295 return Input_AcquireDevice((IDirectInputDeviceA *)device) ? device : NULL;
296}
297
298// Original pcdogs.exe: 0x41a700
299// Creates a joystick Device2A using setup mode one, selecting the basic state format and available force-feedback setup. The caller owns the returned reference.
300IDirectInputDevice2A *DInput_CreateJoystickDevice(IDirectInputA *direct_input, HWND hwnd, int32_t device_index)
301{
302 return DInput_CreateConfiguredJoystickDevice(direct_input, hwnd, device_index, INPUT_JOYSTICK_BASIC);
303}
304
305// Original pcdogs.exe: 0x41a720
306// Creates the DirectInput interface and first joystick, then requests axis ranges from -1000 to 1000. Range-setting results are ignored, and later setup failure can leave created globals and force-feedback discovery state intact.
307BOOL DInput_InitializeJoystickInput(HWND hwnd, HINSTANCE h_instance)
308{
309 g_windowHandle = hwnd;
310 IDirectInputA *direct_input = DInput_CreateInterface(h_instance);
311 g_pDirectInput = direct_input;
312
313 if (!direct_input)
314 return 0;
315
316 GUID device_guid_buffer[INPUT_DEVICE_ENUM_CAPACITY];
317
318 if (!DInput_EnumerateForceFeedbackJoysticks(direct_input, device_guid_buffer))
319 {
320 int32_t attached_joystick_count = DInput_EnumerateAttachedJoysticks(g_pDirectInput, device_guid_buffer);
321
322 if (!attached_joystick_count)
323 return attached_joystick_count;
324 }
325
326 IDirectInputDevice2A *device =
329
330 if (!device)
331 return 0;
332
333 IDirectInputDeviceA *base_device = (IDirectInputDeviceA *)device;
338
339 return 1;
340}
341
342// Original pcdogs.exe: 0x43dbc0
343// Clears one input record, merges keyboard input, then merges gamepad input. The player-index argument is unused.
344void Input_ReadDevices(int32_t unused_player_index, struct Input_State *state)
345{
346 (void)unused_player_index;
347 *state = (struct Input_State){0};
348 Input_ReadKeyboard(state);
349 Input_ReadGamepad(state);
350}
351
352// Original pcdogs.exe: 0x43dbf0
353// Frees cached joystick state and keyboard mappings, releases the joystick and DirectInput interface, and clears their globals. It does not release the separate constant-force effect.
355{
356 struct Input_JoystickState *old_joystick_state = g_pJoystickState;
357
358 if (old_joystick_state)
359 {
360 CRT_free(old_joystick_state);
361 g_pJoystickState = NULL;
362 }
363
364 IDirectInputDevice2A *joystick_device = g_pDirectInputJoystickDevice;
365
366 if (joystick_device)
367 {
368 joystick_device->lpVtbl->Release(joystick_device);
370 }
371
372 IDirectInputA *input_interface = g_pDirectInput;
373
374 if (input_interface)
375 {
376 input_interface->lpVtbl->Release(input_interface);
377 g_pDirectInput = NULL;
378 }
379
380 int32_t *key_mappings = g_keyboardMappingKeys;
381
382 if (key_mappings)
383 {
384 CRT_free(key_mappings);
386 }
387
388 uint32_t *button_mappings = g_keyboardMappingButtons;
389
390 if (button_mappings)
391 {
392 CRT_free(button_mappings);
394 }
395}
396
397// Original pcdogs.exe: 0x44512c
398// Forwards the four arguments to the imported DirectInputCreateA function.
399HRESULT __stdcall DInput_CreateA(HINSTANCE hinst, DWORD dw_version, IDirectInputA **pp_di, IUnknown *p_unk_outer)
400{
401 /* tailcall */
402 return DirectInputCreateA(hinst, dw_version, pp_di, p_unk_outer);
403}
int32_t Input_GetJoystickAxisY(struct Input_JoystickState *joy_state)
Definition devices.c:202
uint8_t Input_GetJoystickButtonByte(struct Input_JoystickState *joy_state, int32_t button_index)
Definition devices.c:220
BOOL Input_SetJoystickDataFormat(IDirectInputDeviceA *device)
Definition devices.c:165
BOOL Input_SetJoystick2DataFormat(IDirectInputDeviceA *device)
Definition devices.c:172
BOOL Input_SetJoystickXAxisRange(IDirectInputDeviceA *device, LONG min_value, LONG max_value)
Definition devices.c:120
void Input_ReadDevices(int32_t unused_player_index, struct Input_State *state)
Definition devices.c:344
BOOL Input_SetJoystickRzAxisRange(IDirectInputDeviceA *device, LONG min_value, LONG max_value)
Definition devices.c:141
BOOL Input_SetDeviceCooperativeLevel(IDirectInputDeviceA *device, HWND hwnd, DWORD flags)
Definition devices.c:53
BOOL DInput_InitializeJoystickInput(HWND hwnd, HINSTANCE h_instance)
Definition devices.c:307
int32_t Input_GetJoystickAxisRz(struct Input_JoystickState *joy_state)
Definition devices.c:211
struct Input_JoystickState * Input_PollJoystickState(IDirectInputDevice2A *device)
Definition devices.c:179
void DInput_ReleaseResources()
Definition devices.c:354
HRESULT DInput_CreateA(HINSTANCE hinst, DWORD dw_version, IDirectInputA **pp_di, IUnknown *p_unk_outer)
Definition devices.c:399
IDirectInputDevice2A * DInput_CreateJoystickDevice(IDirectInputA *direct_input, HWND hwnd, int32_t device_index)
Definition devices.c:300
void * Input_GetDeviceStateBuffer(IDirectInputDeviceA *device, DWORD state_size)
Definition devices.c:79
BOOL Input_IsJoystickButtonPressed(struct Input_JoystickState *joy_state, int32_t button_index)
Definition devices.c:228
IDirectInputDevice2A * DInput_CreateDevice2A(IDirectInputA *dinput_interface, GUID device_guid)
Definition devices.c:26
IDirectInputDevice2A * DInput_CreateConfiguredJoystickDevice(IDirectInputA *direct_input, HWND hwnd, int32_t device_index, int32_t setup_mode)
Definition devices.c:235
IDirectInputDevice2A * DInput_QueryDevice2AInterface(IDirectInputDeviceA *device)
Definition devices.c:41
BOOL Input_SetDeviceDataFormat(IDirectInputDeviceA *device, DIDATAFORMAT const *data_format)
Definition devices.c:62
BOOL Input_SetExclusiveForegroundCooperativeLevel(IDirectInputDeviceA *device, HWND hwnd)
Definition devices.c:158
BOOL Input_SetJoystickZAxisRange(IDirectInputDeviceA *device, LONG min_value, LONG max_value)
Definition devices.c:134
BOOL Input_SetJoystickYAxisRange(IDirectInputDeviceA *device, LONG min_value, LONG max_value)
Definition devices.c:127
int32_t DInput_EnumerateAttachedJoysticks(IDirectInputA *direct_input, GUID *device_guid_buffer)
Definition devices.c:148
BOOL DInput_EnumJoystickDeviceCallback(DIDEVICEINSTANCEA const *device_instance, void *context)
Definition devices.c:88
BOOL Input_AcquireDevice(IDirectInputDeviceA *device)
Definition devices.c:69
IDirectInputA * DInput_CreateInterface(HINSTANCE h_instance)
Definition devices.c:17
BOOL Input_SetJoystickAxisRangeProperty(IDirectInputDeviceA *device, DWORD axis_offset, LONG min_value, LONG max_value)
Definition devices.c:103
int32_t Input_GetJoystickAxisX(struct Input_JoystickState *joy_state)
Definition devices.c:193
#define Input_Joystick2DataFormat
#define Input_JoystickDataFormat
#define Input_GUID_ConstantForce
HRESULT WINAPI DirectInputCreateA(HINSTANCE instance, DWORD version, LPDIRECTINPUTA *out, LPUNKNOWN outer)
#define INPUT_DIRECTINPUT_VERSION
Definition constants.h:21
#define INPUT_JOYSTICK_AXIS_MIN
Definition constants.h:23
@ INPUT_JOYSTICK_BASIC
Definition constants.h:65
@ INPUT_JOYSTICK_EXTENDED
Definition constants.h:66
#define INPUT_BUTTON_DOWN_BIT_INDEX
Definition constants.h:35
#define INPUT_JOYSTICK_AXIS_MAX
Definition constants.h:24
#define INPUT_DEVICE_ENUM_CAPACITY
Definition constants.h:22
void Input_ReadGamepad(struct Input_State *state)
Definition state.c:233
void Input_ReadKeyboard(struct Input_State *state)
Definition state.c:204
BOOL DInput_EnumJoystickDeviceCallback(DIDEVICEINSTANCEA const *device_instance, void *context)
Definition devices.c:88
int32_t DInput_EnumerateForceFeedbackJoysticks(IDirectInputA *direct_input, GUID *device_guid_buffer)
#define g_pJoystickState
Definition globals.h:47
#define g_pDInputConstantForceEffect
Definition globals.h:53
#define g_pDirectInputJoystickDevice
Definition globals.h:50
#define DInput_Device2AIID
Definition globals.h:23
#define g_dinputForceFeedbackAvailable
Definition globals.h:129
#define g_dinputEnumDeviceSeen
Definition globals.h:132
#define g_pDirectInput
Definition globals.h:44
#define g_windowHandle
Definition globals.h:219
void * CRT_malloc(uint32_t size)
Definition heap.c:16
void CRT_free(void *ptr)
Definition heap.c:164
struct Math_Vec3I32 pos
Definition types.h:69
uint8_t rgb_buttons[0x20]
Definition types.h:73
struct Math_Vec3I32 rot
Definition types.h:70
int32_t y
Definition types.h:471
int32_t x
Definition types.h:470
int32_t z
Definition types.h:472
#define g_keyboardMappingButtons
Definition values.h:489
#define g_keyboardMappingKeys
Definition values.h:486