| 1 | /* |
| 2 | * Copyright (C) 2016-2017 Sam Lantinga |
| 3 | * Copyright (C) 2014-2021 Garrett Brown |
| 4 | * Copyright (C) 2014-2021 Team Kodi |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 7 | * See LICENSE.md for more information. |
| 8 | */ |
| 9 | |
| 10 | #include "JoystickInterfaceSDL.h" |
| 11 | #include "JoystickSDL.h" |
| 12 | #include "api/JoystickTypes.h" |
| 13 | #include "log/Log.h" |
| 14 | |
| 15 | #include <SDL2/SDL.h> |
| 16 | |
| 17 | using namespace JOYSTICK; |
| 18 | |
| 19 | EJoystickInterface CJoystickInterfaceSDL::Type(void) const |
| 20 | { |
| 21 | return EJoystickInterface::SDL; |
| 22 | } |
| 23 | |
| 24 | bool CJoystickInterfaceSDL::Initialize(void) |
| 25 | { |
| 26 | return (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC) == 0); |
| 27 | } |
| 28 | |
| 29 | void CJoystickInterfaceSDL::Deinitialize(void) |
| 30 | { |
| 31 | SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC); |
| 32 | } |
| 33 | |
| 34 | bool CJoystickInterfaceSDL::ScanForJoysticks(JoystickVector& joysticks) |
| 35 | { |
| 36 | const int count = SDL_NumJoysticks(); |
| 37 | |
| 38 | if (m_bInitialScan) |
| 39 | dsyslog("SDL: Initial scan: %d joystick%s", count, count == 1 ? "" : "s"); |
| 40 | |
| 41 | for (int i = 0; i < count; i++) |
| 42 | { |
| 43 | if (!SDL_IsGameController(i)) |
| 44 | { |
| 45 | if (m_bInitialScan) |
| 46 | dsyslog("SDL: Joystick %d is not a game controller", i); |
| 47 | continue; |
| 48 | } |
| 49 | |
| 50 | joysticks.push_back(JoystickPtr(new CJoystickSDL(i))); |
| 51 | } |
| 52 | |
| 53 | m_bInitialScan = false; |
| 54 | |
| 55 | return true; |
| 56 | } |