fork/GramFirmware

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

CarVacrebase to configurator818d962

gram
8.1 KiB248 linesraw
1#include "display/DefaultConfigMenu.hpp"
2
3#include "core/Persistence.hpp"
4#include "core/config_utils.hpp"
5#include "core/mode_selection.hpp"
6#include "reboot.hpp"
7
8DefaultConfigMenu::DefaultConfigMenu(
9    Config &config,
10    CommunicationBackend **backends,
11    size_t backends_count
12)
13    : ConfigMenu(config, backends, backends_count) {
14    /* Build default USB backends page */
15    MenuPage::MenuItem *usb_backend_options =
16        new MenuPage::MenuItem[config.communication_backend_configs_count];
17
18    size_t usb_backend_options_count = 0;
19    for (size_t i = 0; i < config.communication_backend_configs_count; i++) {
20        CommunicationBackendConfig &backend_config = config.communication_backend_configs[i];
21        MenuPage::MenuItem &current_option = usb_backend_options[usb_backend_options_count];
22
23        if (backend_config.backend_id != COMMS_BACKEND_XINPUT &&
24            backend_config.backend_id != COMMS_BACKEND_DINPUT &&
25            backend_config.backend_id != COMMS_BACKEND_NINTENDO_SWITCH &&
26            backend_config.backend_id != COMMS_BACKEND_CONFIGURATOR) {
27            continue;
28        }
29
30        strlcpy(
31            current_option.text,
32            backend_name(backend_config.backend_id),
33            sizeof(current_option.text)
34        );
35        current_option.key = i;
36        current_option.action = &SetUsbBackend;
37        usb_backend_options_count++;
38    }
39
40    _usb_backends_page = {
41        .items = usb_backend_options,
42        .items_count = usb_backend_options_count,
43    };
44
45    /* Build gamemodes page */
46    MenuPage::MenuItem *gamemode_options = new MenuPage::MenuItem[config.game_mode_configs_count];
47
48    size_t gamemode_options_count = 0;
49    for (size_t i = 0; i < config.game_mode_configs_count; i++) {
50        GameModeConfig &mode_config = config.game_mode_configs[i];
51        MenuPage::MenuItem &current_option = gamemode_options[gamemode_options_count];
52
53        // Don't show keyboard modes as a gamemode option unless using DInputBackend.
54        if (_backends_count > 0 && _backends[0] != nullptr) {
55            CommunicationBackendId primary_backend_id = _backends[0]->BackendId();
56            if (primary_backend_id != COMMS_BACKEND_DINPUT &&
57                mode_config.mode_id == MODE_KEYBOARD) {
58                continue;
59            }
60        }
61
62        if (strnlen(mode_config.name, sizeof(mode_config.name)) > 0) {
63            strlcpy(current_option.text, mode_config.name, sizeof(current_option.text));
64        } else {
65            strlcpy(
66                current_option.text,
67                gamemode_name(mode_config.mode_id),
68                sizeof(current_option.text)
69            );
70        }
71        current_option.key = i;
72        current_option.action = &SetDefaultMode;
73        gamemode_options_count++;
74    }
75
76    _gamemode_options_page = {
77        .items = gamemode_options,
78        .items_count = gamemode_options_count,
79    };
80
81    /* Build SOCD types page */
82    static MenuPage::MenuItem socd_options[_SocdType_MAX] = {};
83    for (uint8_t socd_type = SOCD_NEUTRAL; socd_type < _SocdType_ARRAYSIZE; socd_type++) {
84        MenuPage::MenuItem &current_option = socd_options[socd_type - 1];
85        strlcpy(current_option.text, socd_name((SocdType)socd_type), sizeof(current_option.text));
86        current_option.key = socd_type;
87        current_option.action = &SetSocdType;
88    }
89
90    static MenuPage socd_page = {
91        .items = socd_options,
92        .items_count = sizeof(socd_options) / sizeof(MenuPage::MenuItem),
93    };
94
95    /* Build top-level page */
96    // clang-format off
97    static MenuPage::MenuItem top_level_items[] = {
98        {
99            .text = "Profile",
100            .page = &_gamemode_options_page,
101        },
102        {
103            .text = "USB Mode",
104            .page = &_usb_backends_page,
105        },
106        {
107            .text = "SOCD Mode",
108            .page = &socd_page,
109        },
110        {
111            .text = "RGB Brightness",
112            .action = [](
113                IntegratedDisplay *display_backend,
114                ConfigMenu *menu,
115                Config &config,
116                uint8_t key
117            ) {
118                display_backend->SetDisplayMode(DISPLAY_MODE_RGB_BRIGHTNESS);
119            },
120        },
121        {
122            .text = "Input Viewer",
123            .action = [](
124                IntegratedDisplay *display_backend,
125                ConfigMenu *menu,
126                Config &config,
127                uint8_t key
128            ) {
129                // Restore gamemode.
130                DefaultConfigMenu *config_menu = (DefaultConfigMenu*)menu; 
131                if (config_menu->_backends[0] != nullptr) {
132                    config_menu->_backends[0]->SetGameMode(display_backend->CurrentGameMode());
133                }
134                display_backend->SetDisplayMode(DISPLAY_MODE_VIEWER);
135            },
136        },
137        {
138            .text = "Save changes",
139            .action = [](
140                IntegratedDisplay *display_backend,
141                ConfigMenu *menu,
142                Config &config,
143                uint8_t key
144            ) {
145                persistence.SaveConfig(config);
146                reboot_firmware();
147            },
148        },
149        {
150            .text = "Discard changes",
151            .action = [](
152                IntegratedDisplay *display_backend,
153                ConfigMenu *menu,
154                Config &config,
155                uint8_t key
156            ) {
157                reboot_firmware();
158            },
159        },
160        {
161            .text = "Firmware update",
162            .action = [](
163                IntegratedDisplay *display_backend,
164                ConfigMenu *menu,
165                Config &config,
166                uint8_t key
167            ) {
168                reboot_bootloader();
169            }
170        }
171    };
172    // clang-format on
173
174    static MenuPage top_level_page = {
175        .items = top_level_items,
176        .items_count = sizeof(top_level_items) / sizeof(MenuPage::MenuItem),
177    };
178    _top_level_page = &top_level_page;
179
180    _usb_backends_page.parent = _top_level_page;
181    _gamemode_options_page.parent = _top_level_page;
182    socd_page.parent = _top_level_page;
183
184    // Set initial page.
185    _current_menu_page = _top_level_page;
186}
187
188DefaultConfigMenu::~DefaultConfigMenu() {
189    delete[] _usb_backends_page.items;
190    delete[] _gamemode_options_page.items;
191}
192
193void DefaultConfigMenu::SetDefaultMode(
194    IntegratedDisplay *display_backend,
195    ConfigMenu *menu,
196    Config &config,
197    uint8_t mode_config_index
198) {
199    if (mode_config_index < 0 || mode_config_index >= config.game_mode_configs_count) {
200        return;
201    }
202
203    // Overwrite default game mode for all backend configs.
204    for (size_t i = 0; i < config.communication_backend_configs_count; i++) {
205        config.communication_backend_configs[i].default_mode_config = mode_config_index + 1;
206    }
207
208    // Update mode for all backends.
209    DefaultConfigMenu *config_menu = (DefaultConfigMenu *)menu;
210    for (size_t i = 0; i < config_menu->_backends_count; i++) {
211        set_mode(config_menu->_backends[i], config.game_mode_configs[mode_config_index], config);
212    }
213    set_mode(display_backend, config.game_mode_configs[mode_config_index], config);
214}
215
216void DefaultConfigMenu::SetUsbBackend(
217    IntegratedDisplay *display_backend,
218    ConfigMenu *menu,
219    Config &config,
220    uint8_t backend_config_index
221) {
222    if (backend_config_index < 0 ||
223        backend_config_index >= config.communication_backend_configs_count) {
224        return;
225    }
226    // Set backend in watchdog SCRATCH0 register and reboot.
227    watchdog_hw->scratch[0] = backend_config_index + 1;
228    reboot_firmware();
229}
230
231void DefaultConfigMenu::SetSocdType(
232    IntegratedDisplay *display_backend,
233    ConfigMenu *menu,
234    Config &config,
235    uint8_t socd_type
236) {
237    if (socd_type <= SOCD_UNSPECIFIED || socd_type > _SocdType_MAX) {
238        return;
239    }
240
241    // Overwrite SOCD type for all SOCD pairs of current gamemode's config.
242    GameModeConfig *mode_config = display_backend->CurrentGameMode()->GetConfig();
243    if (mode_config != nullptr) {
244        for (size_t i = 0; i < mode_config->socd_pairs_count; i++) {
245            mode_config->socd_pairs[i].socd_type = (SocdType)socd_type;
246        }
247    }
248}