fork/GramFirmware

git clone https://git.julia.blue/fork/GramFirmware

CarVacrebase to configurator818d962

gram
3.1 KiB86 linesraw
1#include "core/InputMode.hpp"
2
3#include "core/socd.hpp"
4#include "core/state.hpp"
5#include "util/state_util.hpp"
6
7InputMode::InputMode() {}
8
9GameModeConfig *InputMode::GetConfig() {
10    return _config;
11}
12
13void InputMode::SetConfig(GameModeConfig &config) {
14    _config = &config;
15}
16
17void InputMode::HandleSocd(InputState &inputs) {
18    if (_config == nullptr) {
19        return;
20    }
21    // Handle SOCD resolution for each SOCD button pair.
22    for (size_t i = 0; i < _config->socd_pairs_count; i++) {
23        const SocdPair &pair = _config->socd_pairs[i];
24        switch (pair.socd_type) {
25            case SOCD_NEUTRAL:
26                socd::neutral(inputs, pair.button_dir1, pair.button_dir2);
27                break;
28            case SOCD_2IP:
29                socd::second_input_priority(
30                    inputs,
31                    pair.button_dir1,
32                    pair.button_dir2,
33                    _socd_states[i]
34                );
35                break;
36            case SOCD_2IP_NO_REAC:
37                socd::second_input_priority_no_reactivation(
38                    inputs,
39                    pair.button_dir1,
40                    pair.button_dir2,
41                    _socd_states[i]
42                );
43                break;
44            case SOCD_DIR1_PRIORITY:
45                socd::dir1_priority(inputs, pair.button_dir1, pair.button_dir2);
46                break;
47            case SOCD_DIR2_PRIORITY:
48                socd::dir1_priority(inputs, pair.button_dir2, pair.button_dir1);
49                break;
50            case SOCD_UNSPECIFIED:
51            default:
52                break;
53        }
54    }
55}
56
57void InputMode::HandleRemap(const InputState &original_inputs, InputState &remapped_inputs) {
58    if (_config == nullptr) {
59        return;
60    }
61    remapped_inputs.buttons = 0;
62
63    // Keep track of which buttons have been remapped so that we can prevent macro remapping.
64    uint64_t physical_buttons_already_remapped = 0;
65    for (size_t i = 0; i < _config->button_remapping_count; i++) {
66        const ButtonRemap &remapping = _config->button_remapping[i];
67        // If this physical button was already remapped to something else, ignore this remapping.
68        // This is to prevent creating macro behaviour through remapping.
69        if (get_button(physical_buttons_already_remapped, remapping.physical_button)) {
70            continue;
71        }
72
73        // Either use the value of the physical button, or if the physical button is not pressed,
74        // but the target button has another physical button remapped to it, and is considered to be
75        // pressed, leave it as pressed.
76        bool should_be_pressed = get_button(original_inputs.buttons, remapping.physical_button) ||
77                                 get_button(remapped_inputs.buttons, remapping.activates);
78        set_button(remapped_inputs.buttons, remapping.activates, should_be_pressed);
79
80        // Track which buttons have been remapped.
81        set_button(physical_buttons_already_remapped, remapping.physical_button, true);
82    }
83
84    // Copy over original button states for buttons that were not remapped.
85    remapped_inputs.buttons |= original_inputs.buttons & ~physical_buttons_already_remapped;
86}