| 96 | static Map<OS, Map<String, ControllerMapping>> controllerMappings = new HashMap<>(); |
| 97 | |
| 98 | static void parseGameControllerDB(String mappings) { |
| 99 | for (OS os : OS.values()) { |
| 100 | controllerMappings.put(os, new HashMap<>()); |
| 101 | } |
| 102 | // File format: |
| 103 | // Any line starting with a # or empty is a comment |
| 104 | // Format is GUID, name, mappings |
| 105 | // Mappings are a comma-separated list of buttons, axes, and hats with a : delimiter |
| 106 | // Buttons are B<index>, axes are A<index>, hats are H<index> |
| 107 | |
| 108 | // Read into a map of GUID to mappings |
| 109 | String[] lines = mappings.split("\n"); |
| 110 | |
| 111 | for (String line : lines) { |
| 112 | line = line.trim(); |
| 113 | if (line.isEmpty() || line.startsWith("#")) { |
| 114 | continue; |
| 115 | } |
| 116 | String[] parts = line.split(","); |
| 117 | if (parts.length < 3) { |
| 118 | continue; |
| 119 | } |
| 120 | String guid = parts[0].trim(); |
| 121 | String name = parts[1].trim(); |
| 122 | ControllerMapping controller = new ControllerMapping(); |
| 123 | controller.guid = guid; |
| 124 | controller.name = name; |
| 125 | OS os = OS.Unknown; |
| 126 | // Split the mapping into parts |
| 127 | for (int i = 2; i < parts.length; i++) { |
| 128 | String[] mappingParts = parts[i].split(":"); |
| 129 | if (mappingParts.length < 2) { |
| 130 | continue; |
| 131 | } |
| 132 | String target = mappingParts[0].trim(); |
| 133 | String source = mappingParts[1].trim(); |
| 134 | boolean inverted = source.endsWith("~"); |
| 135 | if (inverted) { |
| 136 | source = source.substring(0, source.length() - 1); |
| 137 | } |
| 138 | boolean isAxis = source.charAt(0) == 'a'; |
| 139 | boolean isButton = source.charAt(0) == 'b'; |
| 140 | boolean isHat = source.charAt(0) == 'h'; |
| 141 | boolean isNAN = !isAxis && !isButton && !isHat; |
| 142 | int index = isNAN ? -1 : Integer.parseInt(source.substring(isHat ? 3 : 1)); |
| 143 | if (isAxis) { |
| 144 | switch (target) { |
| 145 | case "leftx" -> { |
| 146 | controller.xaxis = index; |
| 147 | controller.xinvert = inverted; |
| 148 | } |
| 149 | case "lefty" -> { |
| 150 | controller.yaxis = index; |
| 151 | controller.yinvert = inverted; |
| 152 | } |
| 153 | } |
| 154 | } else if (isButton) { |
| 155 | switch (target) { |