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