| 1 | // SPDX-FileCopyrightText: 2009 Ingo Ruhnke <[email protected]>cxx . . $ `<file>/src/joystick_config_files.cpp`/ |
| 2 | // SPDX-FileCopyrightText: 2025 Raphael Rosch <[email protected]> |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "joystick_config_files.hpp"cxx . . $ `<file>/src/joystick_config_files.hpp`/ |
| 6 | #include "main.hpp"cxx . . $ `<file>/src/main.hpp`/ |
| 7 | |
| 8 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<JoystickConfigcxx . . $ JoystickConfig#> joystick_configscxx . . $ joystick_configs.; |
| 9 | |
| 10 | bool file_is_goodcxx . . $ file_is_good(7c222eea44a24d43).(const stdcxx . . $ std/::stringcxx . . $ std/string#& filename) { |
| 11 | stdcxx . . $ std/::ifstreamcxx . . $ std/ifstream# filecxx . . $ std/basic_ifstream#basic_ifstream(be6c264dafcf0985).(filename); |
| 12 | return file.goodcxx . . $ std/basic_ios#good(50ce9a9e25b4a850).(); |
| 13 | } |
| 14 | |
| 15 | JoystickConfigcxx . . $ JoystickConfig# get_config_for_usb_idcxx . . $ get_config_for_usb_id(846110c2b27fd439).(const stdcxx . . $ std/::stringcxx . . $ std/string#& usb_id) { |
| 16 | JoystickConfigcxx . . $ JoystickConfig# ret; |
| 17 | for (const auto& config : joystick_configscxx . . $ joystick_configs.) { |
| 18 | for (const auto& cur_usb_id : config.usb_idscxx . . $ JoystickConfig#usb_ids.) { |
| 19 | //std::cout << " USB ID: " << cur_usb_id << "\n"; |
| 20 | if (cur_usb_id ==cxx . . $ std/`operator==`(2965e344aacb545b). usb_id) { |
| 21 | if (m_verbosecxx . . $ m_verbose.) |
| 22 | stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " FOUND usb_id: " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). usb_id <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n"; |
| 23 | return config; |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | return ret; |
| 28 | } |
| 29 | |
| 30 | JoystickConfigcxx . . $ JoystickConfig# load_configcxx . . $ load_config(846110c2b27fd439).(const stdcxx . . $ std/::stringcxx . . $ std/string#& filename) { |
| 31 | JoystickConfigcxx . . $ JoystickConfig# config; |
| 32 | stdcxx . . $ std/::ifstreamcxx . . $ std/ifstream# filecxx . . $ std/basic_ifstream#basic_ifstream(be6c264dafcf0985).(filename); |
| 33 | stdcxx . . $ std/::stringcxx . . $ std/string# linecxx . . $ std/__cxx11/basic_string#basic_string(49f6e7a06ebc5aa8).; |
| 34 | |
| 35 | while (stdcxx . . $ std/::getlinecxx . . $ std/getline(4ec722ed0b69c2ab).(file, line)) { |
| 36 | // Remove comments |
| 37 | size_t comment_pos = line.findcxx . . $ std/__cxx11/basic_string#find(625b4b37461b5dfd).('#'); |
| 38 | if (comment_pos != stdcxx . . $ std/::stringcxx . . $ std/string#::nposcxx . . $ std/__cxx11/basic_string#npos.) { |
| 39 | line =cxx . . $ std/__cxx11/basic_string#`operator=`(706c69e8c742c906). line.substrcxx . . $ std/__cxx11/basic_string#substr(149c655009c9bbd2).(0, comment_pos); |
| 40 | } |
| 41 | |
| 42 | if (line.emptycxx . . $ std/__cxx11/basic_string#empty(50ce9a9e25b4a850).()) continue; |
| 43 | |
| 44 | // Parse name=value |
| 45 | size_t equals_pos = line.findcxx . . $ std/__cxx11/basic_string#find(625b4b37461b5dfd).('='); |
| 46 | if (equals_pos == stdcxx . . $ std/::stringcxx . . $ std/string#::nposcxx . . $ std/__cxx11/basic_string#npos.) continue; |
| 47 | |
| 48 | stdcxx . . $ std/::stringcxx . . $ std/string# name = linecxx . . $ std/__cxx11/basic_string#basic_string(3bc0f77cdfbaa67f)..substrcxx . . $ std/__cxx11/basic_string#substr(149c655009c9bbd2).(0, equals_pos); |
| 49 | stdcxx . . $ std/::stringcxx . . $ std/string# value = linecxx . . $ std/__cxx11/basic_string#basic_string(3bc0f77cdfbaa67f)..substrcxx . . $ std/__cxx11/basic_string#substr(149c655009c9bbd2).(equals_pos + 1); |
| 50 | |
| 51 | if (name ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "icon_filename") |
| 52 | { |
| 53 | config.icon_filenamecxx . . $ JoystickConfig#icon_filename. =cxx . . $ std/__cxx11/basic_string#`operator=`(c0b47be1fddc0e75). value; |
| 54 | config.icon_filename_is_goodcxx . . $ JoystickConfig#icon_filename_is_good. = file_is_goodcxx . . $ file_is_good(7c222eea44a24d43).( |
| 55 | Maincxx . . $ Main#::currentcxx . . $ Main#current(d972e08ee8a5bfee).()->get_data_directorycxx . . $ Main#get_data_directory(219584694fe2b248).() +cxx . . $ std/operator+(900ec9581c410ad9). "icons/" +cxx . . $ std/operator+(db5dc2ea912c6495). value); |
| 56 | } |
| 57 | |
| 58 | if (name ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "js_type") |
| 59 | { |
| 60 | config.js_typecxx . . $ JoystickConfig#js_type. =cxx . . $ std/__cxx11/basic_string#`operator=`(c0b47be1fddc0e75). value; |
| 61 | } |
| 62 | |
| 63 | if (name ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "usb_id") |
| 64 | { |
| 65 | if (m_verbosecxx . . $ m_verbose.) |
| 66 | stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " registering usb_id: " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). value <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 67 | config.usb_idscxx . . $ JoystickConfig#usb_ids..push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(value); |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | config.valuescxx . . $ JoystickConfig#values.[cxx . . $ std/unordered_map#`operator[]`(4f10b6009f0ee734).name] =cxx . . $ std/__cxx11/basic_string#`operator=`(c0b47be1fddc0e75). value; |
| 72 | |
| 73 | size_t underscore_pos = name.findcxx . . $ std/__cxx11/basic_string#find(625b4b37461b5dfd).('_'); |
| 74 | if (name.rfindcxx . . $ std/__cxx11/basic_string#rfind(91d5177563f5ce1b).("axis_", 0) == 0 && underscore_pos != stdcxx . . $ std/::stringcxx . . $ std/string#::nposcxx . . $ std/__cxx11/basic_string#npos.) |
| 75 | { |
| 76 | int idx = stdcxx . . $ std/::stoicxx . . $ std/__cxx11/stoi(ffee248579a0a688).(name.substrcxx . . $ std/__cxx11/basic_string#substr(149c655009c9bbd2).(underscore_pos + 1)); |
| 77 | if (idx < 0) |
| 78 | continue; |
| 79 | if (static_cast<int>(config.axescxx . . $ JoystickConfig#axes..sizecxx . . $ std/vector#size(fe4d2d262d4164a8).()) <= idx) |
| 80 | config.axescxx . . $ JoystickConfig#axes..resizecxx . . $ std/vector#resize(2c44cd906ae4e829).(idx + 1); |
| 81 | config.axescxx . . $ JoystickConfig#axes.[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).idx] =cxx . . $ std/__cxx11/basic_string#`operator=`(c0b47be1fddc0e75). value; |
| 82 | } |
| 83 | else if (name.rfindcxx . . $ std/__cxx11/basic_string#rfind(91d5177563f5ce1b).("button_", 0) == 0 && underscore_pos != stdcxx . . $ std/::stringcxx . . $ std/string#::nposcxx . . $ std/__cxx11/basic_string#npos.) |
| 84 | { |
| 85 | int idx = stdcxx . . $ std/::stoicxx . . $ std/__cxx11/stoi(ffee248579a0a688).(name.substrcxx . . $ std/__cxx11/basic_string#substr(149c655009c9bbd2).(underscore_pos + 1)); |
| 86 | if (idx < 0) |
| 87 | continue; |
| 88 | if (static_cast<int>(config.buttonscxx . . $ JoystickConfig#buttons..sizecxx . . $ std/vector#size(fe4d2d262d4164a8).()) <= idx) |
| 89 | config.buttonscxx . . $ JoystickConfig#buttons..resizecxx . . $ std/vector#resize(2c44cd906ae4e829).(idx + 1); |
| 90 | config.buttonscxx . . $ JoystickConfig#buttons.[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).idx] =cxx . . $ std/__cxx11/basic_string#`operator=`(c0b47be1fddc0e75). value; |
| 91 | if (static_cast<int>(value.sizecxx . . $ std/__cxx11/basic_string#size(48278e53df7b52a5).()) > config.button_maxlencxx . . $ JoystickConfig#button_maxlen.) |
| 92 | config.button_maxlencxx . . $ JoystickConfig#button_maxlen. = static_cast<int>(value.sizecxx . . $ std/__cxx11/basic_string#size(48278e53df7b52a5).()); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return config; |
| 98 | } |
| 99 | |
| 100 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<JoystickConfigcxx . . $ JoystickConfig#> load_all_configscxx . . $ load_all_configs(dd0b087177d19225).(const stdcxx . . $ std/::stringcxx . . $ std/string#& directory) { |
| 101 | if (m_verbosecxx . . $ m_verbose.) |
| 102 | stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "loading config files in: " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). directory <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 103 | |
| 104 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<JoystickConfigcxx . . $ JoystickConfig#> conf_filescxx . . $ std/vector#vector(49f6e7a06ebc5aa8).; |
| 105 | DIR* dir = opendir(directory.c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).()); |
| 106 | if (!dir) return conf_filescxx . . $ std/vector#vector(b1ea180184b47f03).; |
| 107 | |
| 108 | struct dirent* entry; |
| 109 | while ((entry = readdir(dir)) != nullptr) { |
| 110 | stdcxx . . $ std/::stringcxx . . $ std/string# namecxx . . $ std/__cxx11/basic_string#basic_string(3bc0f77cdfbaa67f). = entrycxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).->d_name; |
| 111 | if (name.sizecxx . . $ std/__cxx11/basic_string#size(48278e53df7b52a5).() >= 7 && name.substrcxx . . $ std/__cxx11/basic_string#substr(149c655009c9bbd2).(name.sizecxx . . $ std/__cxx11/basic_string#size(48278e53df7b52a5).() - 7) ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). ".config") { |
| 112 | if (m_verbosecxx . . $ m_verbose.) |
| 113 | stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "loading config: " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). directory +cxx . . $ std/operator+(40fc1db9226233c6). "/" +cxx . . $ std/operator+(db5dc2ea912c6495). name <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 114 | conf_files.push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(load_configcxx . . $ load_config(846110c2b27fd439).(directory +cxx . . $ std/operator+(40fc1db9226233c6). "/" +cxx . . $ std/operator+(db5dc2ea912c6495). name)); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | closedir(dir); |
| 119 | return conf_filescxx . . $ std/vector#vector(b1ea180184b47f03).; |
| 120 | } |