-
Notifications
You must be signed in to change notification settings - Fork 0
/
precision-gamepad-source.c
422 lines (354 loc) · 13.3 KB
/
precision-gamepad-source.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
#include <windows.h> // Needed for XInput architecture detection
#include <Xinput.h>
#include <obs-module.h>
#include <stdlib.h>
#include <stdbool.h>
const SHORT THUMBLX_MAX = 32767;
bool isPressed(const WORD button, WORD buttons)
{
return (buttons & button) != 0;
}
WORD getButton(int id)
{
XINPUT_STATE state;
ZeroMemory(&state, sizeof(XINPUT_STATE));
int timer = 0;
WORD button = -1;
const int sleeptime = 100;
const int waittime = 10000; // 10 sec
while (state.Gamepad.wButtons == 0 || timer < waittime)
{
XInputGetState(id, &state);
if (state.Gamepad.wButtons != 0)
{
button = state.Gamepad.wButtons;
return button;
}
Sleep(sleeptime);
timer += sleeptime;
}
return button;
}
struct pg_data {
uint32_t throttle_color;
uint32_t brake_color;
uint32_t steer_color;
uint32_t background_color;
uint32_t background_transparency;
uint32_t foreground_transparency;
bool outline;
uint32_t width;
uint32_t height;
int player_id;
WORD throttle_button;
WORD brake_button;
SHORT deadzone;
bool throttle_pressed;
bool brake_pressed;
SHORT steer_input;
gs_vertbuffer_t *vbuf;
obs_source_t *src;
};
gs_vertbuffer_t *create_vbuffer() {
obs_enter_graphics();
gs_vertbuffer_t *vbuf = NULL;
struct gs_vb_data *vrect = NULL;
// 2 triangle, 2 steer trapez, 2 button = 24
const int num_verts = 24;
vrect = gs_vbdata_create();
vrect->num = num_verts;
vrect->points = (struct vec3 *)bmalloc(sizeof(struct vec3) * num_verts);
vrect->num_tex = 1;
vrect->tvarray = (struct gs_tvertarray *)bmalloc(sizeof(struct gs_tvertarray));
vrect->tvarray[0].width = 2;
vrect->tvarray[0].array = bmalloc(sizeof(struct vec2) * num_verts);
memset(vrect->points, 0, sizeof(struct vec3) * num_verts);
memset(vrect->tvarray[0].array, 1, sizeof(struct vec2) * num_verts);
vbuf = gs_vertexbuffer_create(vrect, GS_DYNAMIC);
if (vbuf == NULL) {
blog(LOG_INFO, "Couldn't create UV vertex buffer.");
}
obs_leave_graphics();
return vbuf;
}
static const char *pg_getname(void *unused)
{
UNUSED_PARAMETER(unused);
return obs_module_text("PrecisionGamepadSource");
}
static void pg_update(void *data, obs_data_t *settings)
{
struct pg_data *context = data;
uint32_t throttle_color = (uint32_t)obs_data_get_int(settings, "throttle_color");
uint32_t brake_color = (uint32_t)obs_data_get_int(settings, "brake_color");
uint32_t steer_color = (uint32_t)obs_data_get_int(settings, "steer_color");
uint32_t background_color = (uint32_t)obs_data_get_int(settings, "background_color");
uint32_t background_transparency = (uint32_t)obs_data_get_int(settings, "background_transparency");
uint32_t foreground_transparency = (uint32_t)obs_data_get_int(settings, "foreground_transparency");
uint32_t width = (uint32_t)obs_data_get_int(settings, "width");
uint32_t height = (uint32_t)obs_data_get_int(settings, "height");
int player_id = obs_data_get_int(settings, "player_id");
WORD throttle_button = (WORD)obs_data_get_int(settings, "throttle_button");
WORD brake_button = (WORD)obs_data_get_int(settings, "brake_button");
SHORT deadzone = (SHORT)obs_data_get_int(settings, "deadzone");
const uint32_t TRANS_MASK = 0x00FFFFFF;
const uint32_t SHIFT = 24;
uint32_t foreground_mask = (foreground_transparency << SHIFT) | TRANS_MASK;
uint32_t background_mask = (background_transparency << SHIFT) | TRANS_MASK;
context->throttle_color = foreground_mask & throttle_color;
context->brake_color = foreground_mask & brake_color;
context->steer_color = foreground_mask & steer_color;
context->background_color = background_mask & background_color;
context->background_transparency = background_transparency;
context->foreground_transparency = foreground_transparency;
context->width = width;
context->height = height;
context->player_id = player_id;
context->throttle_button = throttle_button;
context->brake_button = brake_button;
context->deadzone = deadzone;
}
static void *pg_create(obs_data_t *settings, obs_source_t *source)
{
UNUSED_PARAMETER(source);
struct pg_data *context = bzalloc(sizeof(struct pg_data));
context->throttle_pressed = false;
context->brake_pressed = false;
context->steer_input = THUMBLX_MAX / 2;
context->src = source;
context->vbuf = create_vbuffer();
pg_update(context, settings);
return context;
}
static void pg_destroy(void *data)
{
struct pg_data *context = data;
obs_enter_graphics();
if (context->vbuf != NULL) {
gs_vertbuffer_t *tmpvbuf = context->vbuf;
context->vbuf = NULL;
gs_vertexbuffer_destroy(tmpvbuf);
}
obs_leave_graphics();
bfree(data);
}
static obs_properties_t *pg_properties(void *unused)
{
UNUSED_PARAMETER(unused);
obs_properties_t *props = obs_properties_create();
obs_properties_add_color(props, "throttle_color",
obs_module_text("Color.Throttle"));
obs_properties_add_color(props, "brake_color",
obs_module_text("Color.Brake"));
obs_properties_add_color(props, "steer_color",
obs_module_text("Color.Steer"));
obs_properties_add_color(props, "background_color",
obs_module_text("Color.Background"));
obs_properties_add_int_slider(props, "background_transparency",
obs_module_text("Transparency.Background"), 0, 255, 1);
obs_properties_add_int_slider(props, "foreground_transparency",
obs_module_text("Transparency.Foreground"), 0, 255, 1);
obs_properties_add_int_slider(props, "deadzone", obs_module_text("Deadzone"), 0, THUMBLX_MAX, 1);
obs_properties_add_int_slider(props, "player_id", obs_module_text("Controller"), 0, 3, 1);
obs_property_t *throttle_list = obs_properties_add_list(props, "throttle_button", obs_module_text("Button.Throttle"), OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
obs_property_list_add_int(throttle_list, "X", XINPUT_GAMEPAD_X);
obs_property_list_add_int(throttle_list, "Y", XINPUT_GAMEPAD_Y);
obs_property_list_add_int(throttle_list, "A", XINPUT_GAMEPAD_A);
obs_property_list_add_int(throttle_list, "B", XINPUT_GAMEPAD_B);
obs_property_list_add_int(throttle_list, "LS", XINPUT_GAMEPAD_LEFT_SHOULDER);
obs_property_list_add_int(throttle_list, "RS", XINPUT_GAMEPAD_RIGHT_SHOULDER);
obs_property_t *brake_list = obs_properties_add_list(props, "brake_button", obs_module_text("Button.Brake"), OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
obs_property_list_add_int(brake_list, "X", XINPUT_GAMEPAD_X);
obs_property_list_add_int(brake_list, "Y", XINPUT_GAMEPAD_Y);
obs_property_list_add_int(brake_list, "A", XINPUT_GAMEPAD_A);
obs_property_list_add_int(brake_list, "B", XINPUT_GAMEPAD_B);
obs_property_list_add_int(brake_list, "LS", XINPUT_GAMEPAD_LEFT_SHOULDER);
obs_property_list_add_int(brake_list, "RS", XINPUT_GAMEPAD_RIGHT_SHOULDER);
obs_properties_add_int(props, "width",
obs_module_text("Width"), 0, 4096,
1);
obs_properties_add_int(props, "height",
obs_module_text("Height"), 0, 4096,
1);
return props;
}
void build_graphics(struct pg_data *context) {
struct gs_vb_data *vdata = gs_vertexbuffer_get_data(context->vbuf);
struct vec2 *tvarray = vdata->tvarray[0].array;
float width = (float)context->width;
float height = (float)context->height;
float w_middle = width / 2.0f;
float h_middle = height / 2.0f;
float button_width = width * 0.09f;
float button_cap = height * 0.1f;
float gap = width * 0.02f;
float gap2 = gap / 2.0f;
float throttle_button[5][2] =
{
{w_middle, 0.0f}, // Top
{w_middle - button_width, 0.0f + button_cap}, // TopLeft
{w_middle + button_width, 0.0f + button_cap}, // TopRight
{w_middle - button_width, h_middle - gap2}, // BottomLeft
{w_middle + button_width, h_middle - gap2}, // Bottomright
};
float brake_button[5][2] =
{
{w_middle, height}, // Top
{w_middle + button_width, height - button_cap}, // TopRight
{w_middle - button_width, height - button_cap}, // TopLeft
{w_middle + button_width, h_middle + gap2}, // Bottomright
{w_middle - button_width, h_middle + gap2}, // BottomLeft
};
for (int i = 0; i < 5; ++i) {
vec3_set(vdata->points + i, throttle_button[i][0], throttle_button[i][1], 0.0f);
}
for (int i = 0; i < 5; ++i) {
vec3_set(vdata->points + 5 + i, brake_button[i][0], brake_button[i][1], 0.0f);
}
// Left Steer
float left_inner = w_middle - button_width - gap;
float right_inner = w_middle + button_width + gap;
float tri_top = 0.0f + button_cap + gap2;
float tri_bot = height - button_cap - gap2;
vec3_set(vdata->points + 10, 0.0f, h_middle, 0.0f);
vec3_set(vdata->points + 11, left_inner, tri_top, 0.0f);
vec3_set(vdata->points + 12, left_inner, tri_bot, 0.0f);
// Right Steer
vec3_set(vdata->points + 13, width, h_middle, 0.0f);
vec3_set(vdata->points + 14, right_inner, tri_top, 0.0f);
vec3_set(vdata->points + 15, right_inner, tri_bot, 0.0f);
// Steer stuff
float tri_width = left_inner; // a
float tri_height = h_middle - tri_top; // b
float deadzone = (float)context->deadzone;
float steer_input = ((abs((float)context->steer_input) - deadzone) / (1 - deadzone / (float)THUMBLX_MAX) / (float)THUMBLX_MAX);
float steer_width = tri_width * steer_input;
float angle = tri_height / tri_width; // tan(rho) = b / a
float steer_height = tri_height - angle * steer_width; // b' = tan(rho) * a
// Left Steer Active
vec3_set(vdata->points + 16, left_inner - steer_width, h_middle - steer_height, 0.0f);
vec3_set(vdata->points + 17, left_inner - steer_width, h_middle + steer_height, 0.0f);
vec3_set(vdata->points + 18, left_inner, tri_top, 0.0f);
vec3_set(vdata->points + 19, left_inner, tri_bot, 0.0f);
// Right Steer Active
vec3_set(vdata->points + 20, right_inner + steer_width, h_middle - steer_height, 0.0f);
vec3_set(vdata->points + 21, right_inner + steer_width, h_middle + steer_height, 0.0f);
vec3_set(vdata->points + 22, right_inner, tri_top, 0.0f);
vec3_set(vdata->points + 23, right_inner, tri_bot, 0.0f);
}
static void pg_render(void *data, gs_effect_t *effect)
{
UNUSED_PARAMETER(effect);
struct pg_data *context = data;
gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_SOLID);
gs_eparam_t *color = gs_effect_get_param_by_name(solid, "color");
gs_technique_t *tech = gs_effect_get_technique(solid, "Solid");
struct vec4 colorVal;
gs_technique_begin(tech);
gs_technique_begin_pass(tech, 0);
gs_vertbuffer_t *vbuf = context->vbuf;
build_graphics(context);
gs_vertexbuffer_flush(vbuf);
gs_load_vertexbuffer(vbuf);
gs_load_indexbuffer(NULL);
// Throttle
bool tp = context->throttle_pressed;
vec4_from_rgba(&colorVal, tp ? context->throttle_color : context->background_color);
gs_effect_set_vec4(color, &colorVal);
gs_draw(GS_TRISTRIP, 0, 5);
// Brake
bool bp = context->brake_pressed;
vec4_from_rgba(&colorVal, bp ? context->brake_color : context->background_color);
gs_effect_set_vec4(color, &colorVal);
gs_draw(GS_TRISTRIP, 5, 5);
// Left Background
vec4_from_rgba(&colorVal, context->background_color);
gs_effect_set_vec4(color, &colorVal);
gs_draw(GS_TRIS, 10, 3);
// Right Background
gs_draw(GS_TRIS, 13, 3);
vec4_from_rgba(&colorVal, context->steer_color);
gs_effect_set_vec4(color, &colorVal);
if (abs(context->steer_input) > context->deadzone) {
if (context->steer_input < 0) {
// Left Input
gs_draw(GS_TRISTRIP, 16, 4);
} else {
// Right
gs_draw(GS_TRISTRIP, 20, 4);
}
}
gs_technique_end_pass(tech);
gs_technique_end(tech);
}
static void pg_tick(void *data, float seconds) {
UNUSED_PARAMETER(seconds);
struct pg_data *context = data;
XINPUT_STATE state;
ZeroMemory(&state, sizeof(XINPUT_STATE));
WORD res = XInputGetState(context->player_id, &state);
if(res != ERROR_SUCCESS) {
return;
}
WORD buttons = state.Gamepad.wButtons;
if (isPressed(context->throttle_button, buttons)) {
context->throttle_pressed = true;
} else {
context->throttle_pressed = false;
}
if (isPressed(context->brake_button, buttons)) {
context->brake_pressed = true;
} else {
context->brake_pressed = false;
}
context->steer_input = state.Gamepad.sThumbLX;
bool tp = context->throttle_pressed;
}
static uint32_t pg_get_width(void *data)
{
struct pg_data *context = data;
return context->width;
}
static uint32_t pg_get_height(void *data)
{
struct pg_data *context = data;
return context->height;
}
static void pg_defaults(obs_data_t *settings)
{
obs_data_set_default_int(settings, "throttle_color", 0xFF61BC78);
obs_data_set_default_int(settings, "brake_color", 0xFF403DA6);
obs_data_set_default_int(settings, "steer_color", 0xFF72B8E9);
obs_data_set_default_int(settings, "background_color", 0xFFE2E2E2);
obs_data_set_default_int(settings, "background_transparency", 150);
obs_data_set_default_int(settings, "foreground_transparency", 255);
obs_data_set_default_int(settings, "width", 500);
obs_data_set_default_int(settings, "height", 300);
obs_data_set_default_int(settings, "player_id", 0);
obs_data_set_default_int(settings, "throttle_button", XINPUT_GAMEPAD_X);
obs_data_set_default_int(settings, "brake_button", XINPUT_GAMEPAD_RIGHT_SHOULDER);
obs_data_set_default_int(settings, "deadzone", 7000);
}
struct obs_source_info pg_source_info = {
.id = "precision_gamepad_source",
.type = OBS_SOURCE_TYPE_INPUT,
.output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
.get_name = pg_getname,
.create = pg_create,
.destroy = pg_destroy,
.update = pg_update,
.video_render = pg_render,
.video_tick = pg_tick,
.get_defaults = pg_defaults,
.get_properties = pg_properties,
// .icon_type = OBS_ICON_TYPE_GAME_CAPTURE,
.get_width = pg_get_width,
.get_height = pg_get_height,
};
OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("precision-gamepad-source", "en-US")
bool obs_module_load(void)
{
obs_register_source(&pg_source_info);
return true;
}