| 50 | Controller::~Controller() { } |
| 51 | |
| 52 | bool Controller::initInRender() { |
| 53 | SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0"); |
| 54 | auto time = SharedApplication.getCurrentTime(); |
| 55 | if (SharedContent.exist("gamecontrollerdb.txt"_slice)) { |
| 56 | int64_t size = 0; |
| 57 | uint8_t* buffer = SharedContent.loadUnsafe("gamecontrollerdb.txt"_slice, size); |
| 58 | OwnArray<uint8_t> data(buffer); |
| 59 | if (size > 0) { |
| 60 | const char* platform = SDL_GetPlatform(); |
| 61 | auto str = std::string{r_cast<const char*>(data.get()), s_cast<size_t>(size)}; |
| 62 | char *line, *line_end, *tmp, *comma, line_platform[64]; |
| 63 | size_t db_size = str.size(), platform_len; |
| 64 | char* buf = &str[0]; |
| 65 | line = buf; |
| 66 | auto platformStr = "platform:"sv; |
| 67 | while (line < buf + db_size) { |
| 68 | line_end = SDL_strchr(line, '\n'); |
| 69 | if (line_end != nullptr) { |
| 70 | *line_end = '\0'; |
| 71 | } else { |
| 72 | line_end = buf + db_size; |
| 73 | } |
| 74 | tmp = SDL_strstr(line, platformStr.data()); |
| 75 | if (tmp != nullptr) { |
| 76 | tmp += platformStr.size(); |
| 77 | comma = SDL_strchr(tmp, ','); |
| 78 | if (comma != nullptr) { |
| 79 | platform_len = comma - tmp + 1; |
| 80 | if (platform_len + 1 < SDL_arraysize(line_platform)) { |
| 81 | SDL_strlcpy(line_platform, tmp, platform_len); |
| 82 | if (SDL_strncasecmp(line_platform, platform, platform_len) == 0) { |
| 83 | if (SDL_GameControllerAddMapping(line) < 0) { |
| 84 | Error("failed to load controller mapping: {}", line); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | line = line_end + 1; |
| 91 | } |
| 92 | auto deltaTime = SharedApplication.getCurrentTime() - time; |
| 93 | SharedApplication.invokeInLogic([deltaTime]() { |
| 94 | Event::send(Profiler::EventName, "Loader"s, "gamecontrollerdb.txt"s, 0, deltaTime); |
| 95 | }); |
| 96 | } |
| 97 | } |
| 98 | for (int i = 0; i < SDL_NumJoysticks(); ++i) { |
| 99 | addControllerInRender(i); |
| 100 | } |
| 101 | #if DORA_DEV_VIRTUAL_CONTROLLER |
| 102 | if (isDevVirtualControllerEnabled()) { |
| 103 | #if SDL_VERSION_ATLEAST(2, 24, 0) |
| 104 | SDL_VirtualJoystickDesc desc; |
| 105 | SDL_zero(desc); |
| 106 | desc.version = SDL_VIRTUAL_JOYSTICK_DESC_VERSION; |
| 107 | desc.type = SDL_JOYSTICK_TYPE_GAMECONTROLLER; |
| 108 | desc.naxes = SDL_CONTROLLER_AXIS_MAX; |
| 109 | desc.nbuttons = SDL_CONTROLLER_BUTTON_MAX; |
no test coverage detected