Debian Codegraph @

src/joystick_config_files.cpp

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2009 Ingo Ruhnke <[email protected]>
2 // SPDX-FileCopyrightText: 2025 Raphael Rosch <[email protected]>
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #include "joystick_config_files.hpp"
6 #include "main.hpp"
7
8 std::vector<JoystickConfig> joystick_configs;
9
10 bool file_is_good(const std::string& filename) {
11 std::ifstream file(filename);
12 return file.good();
13 }
14
15 JoystickConfig get_config_for_usb_id(const std::string& usb_id) {
16 JoystickConfig ret;
17 for (const auto& config : joystick_configs) {
18 for (const auto& cur_usb_id : config.usb_ids) {
19 //std::cout << " USB ID: " << cur_usb_id << "\n";
20 if (cur_usb_id == usb_id) {
21 if (m_verbose)
22 std::cout << " FOUND usb_id: " << usb_id << "\n";
23 return config;
24 }
25 }
26 }
27 return ret;
28 }
29
30 JoystickConfig load_config(const std::string& filename) {
31 JoystickConfig config;
32 std::ifstream file(filename);
33 std::string line;
34
35 while (std::getline(file, line)) {
36 // Remove comments
37 size_t comment_pos = line.find('#');
38 if (comment_pos != std::string::npos) {
39 line = line.substr(0, comment_pos);
40 }
41
42 if (line.empty()) continue;
43
44 // Parse name=value
45 size_t equals_pos = line.find('=');
46 if (equals_pos == std::string::npos) continue;
47
48 std::string name = line.substr(0, equals_pos);
49 std::string value = line.substr(equals_pos + 1);
50
51 if (name == "icon_filename")
52 {
53 config.icon_filename = value;
54 config.icon_filename_is_good = file_is_good(
55 Main::current()->get_data_directory() + "icons/" + value);
56 }
57
58 if (name == "js_type")
59 {
60 config.js_type = value;
61 }
62
63 if (name == "usb_id")
64 {
65 if (m_verbose)
66 std::cout << " registering usb_id: " << value << std::endl;
67 config.usb_ids.push_back(value);
68 }
69 else
70 {
71 config.values[name] = value;
72
73 size_t underscore_pos = name.find('_');
74 if (name.rfind("axis_", 0) == 0 && underscore_pos != std::string::npos)
75 {
76 int idx = std::stoi(name.substr(underscore_pos + 1));
77 if (idx < 0)
78 continue;
79 if (static_cast<int>(config.axes.size()) <= idx)
80 config.axes.resize(idx + 1);
81 config.axes[idx] = value;
82 }
83 else if (name.rfind("button_", 0) == 0 && underscore_pos != std::string::npos)
84 {
85 int idx = std::stoi(name.substr(underscore_pos + 1));
86 if (idx < 0)
87 continue;
88 if (static_cast<int>(config.buttons.size()) <= idx)
89 config.buttons.resize(idx + 1);
90 config.buttons[idx] = value;
91 if (static_cast<int>(value.size()) > config.button_maxlen)
92 config.button_maxlen = static_cast<int>(value.size());
93 }
94 }
95 }
96
97 return config;
98 }
99
100 std::vector<JoystickConfig> load_all_configs(const std::string& directory) {
101 if (m_verbose)
102 std::cout << "loading config files in: " << directory << std::endl;
103
104 std::vector<JoystickConfig> conf_files;
105 DIR* dir = opendir(directory.c_str());
106 if (!dir) return conf_files;
107
108 struct dirent* entry;
109 while ((entry = readdir(dir)) != nullptr) {
110 std::string name = entry->d_name;
111 if (name.size() >= 7 && name.substr(name.size() - 7) == ".config") {
112 if (m_verbose)
113 std::cout << "loading config: " << directory + "/" + name << std::endl;
114 conf_files.push_back(load_config(directory + "/" + name));
115 }
116 }
117
118 closedir(dir);
119 return conf_files;
120 }