102 Patches: Detours to the Rescue
C reference for DttR maintainers and modders.
Loading...
Searching...
No Matches
hook_dinput_poll.c
Go to the documentation of this file.
1#include <SDL3/SDL.h>
2#include <dttr_pcdogs.h>
3#include <stdint.h>
4#include <string.h>
5#include <windows.h>
6
7#include <dttr_config.h>
8#include <dttr_log.h>
9
10#include "hooks_private.h"
11#include "inputs_private.h"
12#include "sidecar_private.h"
13
14typedef struct {
15 LONG x;
16 LONG y;
17 LONG z;
18 LONG rx;
19 LONG ry;
20 LONG rz;
21 LONG sliders[2];
23 BYTE buttons[32];
25
26enum {
29};
30
31#define DINPUT_POV_CENTERED 0xFFFFFFFF
32
33#define DINPUT_BUTTON_PRESSED 0x80
34
36 memset(state, 0, sizeof(*state));
37
38 for (int i = 0; i < 4; i++) {
39 state->pov[i] = DINPUT_POV_CENTERED;
40 }
41}
42
45 bool dir_up,
46 bool dir_down,
47 bool dir_left,
48 bool dir_right
49) {
50 if (dir_up) {
52 }
53
54 if (dir_down) {
56 }
57
58 if (dir_left) {
60 }
61
62 if (dir_right) {
64 }
65}
66
67static bool is_source_pressed(int source) {
69 return false;
70 }
71
74 const SDL_GamepadAxis axis = (source == DTTR_GAMEPAD_SOURCE_TRIGGER_LEFT)
75 ? SDL_GAMEPAD_AXIS_LEFT_TRIGGER
76 : SDL_GAMEPAD_AXIS_RIGHT_TRIGGER;
77 return SDL_GetGamepadAxis(dttr_inputs_gamepad, axis) / DTTR_DINPUT_AXIS_SCALE
79 }
80
81 return SDL_GetGamepadButton(dttr_inputs_gamepad, (SDL_GamepadButton)source);
82}
83
84static LONG read_axis(int axis_idx) {
85 const int sdl_axis = dttr_config.gamepad_axes[axis_idx];
86
88 return 0;
89 }
90
91 const LONG value = SDL_GetGamepadAxis(dttr_inputs_gamepad, sdl_axis)
93 const LONG deadzone = dttr_config.gamepad_axis_deadzone[axis_idx];
94
95 return (value > -deadzone && value < deadzone) ? 0 : value;
96}
97
98void *__cdecl dttr_inputs_hook_dinput_poll_callback(void *device) {
100 DTTR_Result alloc_result = DTTR_PCDOGS_F_Mem_MallocCRT->Call(
102 sizeof(di_joy_state),
103 (void **)&state
104 );
105
106 if (!DTTR_ResultOK(alloc_result) || !state) {
108 "Failed to allocate joystick poll state: %s",
109 dttr_sidecar_result_detail(alloc_result)
110 );
111 return NULL;
112 }
113
115
116 if (!dttr_inputs_gamepad || !dttr_config.gamepad_enabled) {
117 return state;
118 }
119
123
124 bool dir_up = false;
125 bool dir_down = false;
126 bool dir_left = false;
127 bool dir_right = false;
128
129 for (int src = 0; src < DTTR_GAMEPAD_SOURCE_COUNT; src++) {
130 const int action = dttr_config.gamepad_button_map[src];
131
133 continue;
134 }
135
136 if (action >= PCDOGS_GAMEPAD_IDX_BTN_0 && action <= PCDOGS_GAMEPAD_IDX_BTN_12) {
138 continue;
139 }
140
141 switch (action) {
143 dir_up = true;
144 break;
146 dir_down = true;
147 break;
149 dir_left = true;
150 break;
152 dir_right = true;
153 break;
154 default:
155 break;
156 }
157 }
158
159 apply_direction_state(state, dir_up, dir_down, dir_left, dir_right);
160
161 return state;
162}
const DTTR_BackendState * state
const uint8_t * src
DTTR_Graphics_COM_DirectDrawSurface7 DWORD flags void NULL
#define DTTR_GAMEPAD_SOURCE_TRIGGER_RIGHT
Definition dttr_config.h:52
#define DTTR_GAMEPAD_AXIS_IDX_STICK_Y
Definition dttr_config.h:61
#define DTTR_GAMEPAD_SOURCE_COUNT
Definition dttr_config.h:53
#define DTTR_GAMEPAD_AXIS_IDX_CAMERA_RZ
Definition dttr_config.h:62
#define DTTR_GAMEPAD_TRIGGER_THRESHOLD
Definition dttr_config.h:49
#define DTTR_GAMEPAD_MAPPING_NONE
Definition dttr_config.h:48
#define DTTR_GAMEPAD_SOURCE_TRIGGER_LEFT
Definition dttr_config.h:51
DTTR_Config dttr_config
Definition defaults.c:53
#define PCDOGS_GAMEPAD_IDX_BTN_0
Definition dttr_config.h:70
#define PCDOGS_GAMEPAD_IDX_UP
Definition dttr_config.h:64
#define PCDOGS_GAMEPAD_IDX_BTN_12
Definition dttr_config.h:82
#define DTTR_GAMEPAD_AXIS_IDX_STICK_X
Definition dttr_config.h:60
#define PCDOGS_GAMEPAD_IDX_LEFT
Definition dttr_config.h:66
#define PCDOGS_GAMEPAD_IDX_RIGHT
Definition dttr_config.h:67
#define PCDOGS_GAMEPAD_IDX_DOWN
Definition dttr_config.h:65
#define DTTR_LOG_ERROR(...)
Definition dttr_log.h:31
DTTR_PCDOGS_API const struct dttr_pcdogs_function_accessor_Mem_MallocCRT *const DTTR_PCDOGS_F_Mem_MallocCRT
Accessor object for Mem_MallocCRT.
bool DTTR_ResultOK(DTTR_Result result)
Definition core.c:78
const DTTR_Core_Context * dttr_sidecar_runtime_context()
Definition entrypoint.c:156
static game_data_source source
Definition game_data.c:21
#define DINPUT_BUTTON_PRESSED
static LONG read_axis(int axis_idx)
@ DTTR_DINPUT_AXIS_FULL_DEFLECTION
@ DTTR_DINPUT_AXIS_SCALE
static bool is_source_pressed(int source)
static void apply_direction_state(di_joy_state *state, bool dir_up, bool dir_down, bool dir_left, bool dir_right)
#define DINPUT_POV_CENTERED
void * dttr_inputs_hook_dinput_poll_callback(void *device)
static void init_poll_state(di_joy_state *state)
SDL_Gamepad * dttr_inputs_gamepad
Definition inputs.c:11
static const char * dttr_sidecar_result_detail(DTTR_Result result)