45 if (SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) < 0) {
46 throw SDLException(SDL_GetError());
53 for (int32_t i = 0; i < SDL_NumJoysticks(); ++i) {
57 SDL_JoystickEventState(SDL_ENABLE);
58 SDL_GameControllerEventState(SDL_ENABLE);
66 SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
72 if ((*it)->getDeviceIndex() == deviceIndex) {
78 if (!(*it)->isConnected() && (*it)->getGuid() == guidStr) {
123 if (!(*it)->isController()) {
124 (*it)->openController();
136 std::string stringMappings;
137 std::map<std::string, uint8_t>::iterator it =
m_gamepadGuids.begin();
145 SDL_JoystickGUID realGuid = SDL_JoystickGetGUIDFromString(guid.c_str());
146 char* mapping = SDL_GameControllerMappingForGUID(realGuid);
148 throw SDLException(SDL_GetError());
149 return std::string();
152 std::string stringMapping(mapping);
155 if (stringMapping.find_last_of(
',') != stringMapping.length() - 1) {
156 stringMapping +=
",";
158 std::size_t platPos = stringMapping.find(
"platform:");
159 if (platPos == std::string::npos) {
160 stringMapping +=
"platform:" + std::string(SDL_GetPlatform()) +
",\n";
162 return stringMapping;
166 int32_t result = SDL_GameControllerAddMapping(mapping.c_str());
170 if (!(*it)->isController()) {
171 (*it)->openController();
175 }
else if (result == -1) {
176 throw SDLException(SDL_GetError());
192 if (*it == listener) {
201 bool dispatch =
true;
205 if (event.type == SDL_JOYAXISMOTION) {
208 joyevt.
setAxis(event.jaxis.axis);
210 }
else if (event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP) {
214 }
else if (event.type == SDL_JOYHATMOTION) {
217 joyevt.
setHat(event.jhat.hat);
219 }
else if (event.type == SDL_JOYDEVICEADDED) {
228 }
else if (event.type == SDL_JOYDEVICEREMOVED) {
237 dispatch = dispatch && (!joy->
isController() || (
event.type == SDL_JOYDEVICEREMOVED ||
event.type == SDL_JOYDEVICEADDED));
243 if (event.type == SDL_JOYDEVICEREMOVED) {
249 bool dispatch =
true;
254 if (event.type == SDL_CONTROLLERAXISMOTION) {
257 joyevt.
setAxis(event.caxis.axis);
259 }
else if (event.type == SDL_CONTROLLERBUTTONDOWN || event.type == SDL_CONTROLLERBUTTONUP) {
275 std::deque<IJoystickListener*>::iterator i = listeners.begin();
276 for (; i != listeners.end(); ++i) {
277 if (!(*i)->isActive())
continue;
280 (*i)->axisMotion(evt);
283 (*i)->hatMotion(evt);
286 (*i)->buttonPressed(evt);
289 (*i)->buttonReleased(evt);
292 (*i)->deviceAdded(evt);
295 (*i)->deviceRemoved(evt);
312 SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(deviceIndex);
313 SDL_JoystickGetGUIDString(guid, tmp,
sizeof(tmp));
314 std::string guidString(tmp);
319 float range =
static_cast<float>(value) / 32768.0f;
323 if (range < -0.99f) {
325 }
else if (range > 0.99f) {
335 std::pair<std::map<std::string, uint8_t>::iterator,
bool> ret;
337 if (ret.second ==
false) {
void addJoystickListener(IJoystickListener *listener)
Adds a listener to the back of the listener deque Listener will be notified via the corresponding eve...
JoystickManager()
Constructor.
void save(const std::string data, const std::string &filename)
Saves mapping to file.
virtual ~JoystickManager()
Destructor.
void removeJoystick(Joystick *joystick)
Removes the given joystick, can be reused.
void processJoystickEvent(SDL_Event event)
Creates and process joystick events.
ControllerMappingLoader m_mappingLoader
Loader for gamecontroller mapping.
uint8_t getJoystickCount() const
Return the number of joysticks / gamecontrollers.
virtual bool isActive()
Indicates if the listener is active.
float convertRange(int16_t value)
Converts the int16 in -1.0 to 1.0 range.
void removeControllerGuid(Joystick *joystick)
Removes / decal controller GUID.
virtual bool isConsumed() const
Checks whether event is consumed.
bool isController() const
Indicates if this a controller.
static Logger _log(LM_AUDIO)
std::map< std::string, uint8_t > m_gamepadGuids
Each sort of gamepad have a GUID from SDL. Indicates the number of gamepads with given GUID are conne...
void setStringMapping(const std::string &mapping)
Sets controller mapping from string and adds or updates the related controllers.
void setHat(int8_t hat)
Sets the hat index.
std::vector< Joystick * > m_joysticks
All "known" Joysticks. Useful if a user reconnect a Joystick.
void setAxis(int8_t axis)
Sets the axis index number or the Joystick::ControllerAxis.
void setDeviceIndex(int32_t deviceIndex)
Sets the device index of the joystick.
Listener of joystick events.
std::vector< Joystick * > m_activeJoysticks
All active / connected Joysticks.
Represents a Joystick and if available the Gamecontroller.
void addControllerGuid(Joystick *joystick)
Adds GUID from controller.
std::map< int32_t, uint32_t > m_joystickIndices
Map to hold the relation between Joystick InstanceId and JoystickId.
void setType(JoystickEventType type)
Sets the event type.
void dispatchJoystickEvent(JoystickEvent &evt)
Dispatches joystick / controller events.
void setInstanceId(int32_t id)
Sets the instance id of the joystick.
const std::string & getGuid()
Return the GUID of the joystick / gamecontroller class as string.
ControllerMappingSaver m_mappingSaver
Saver for gamecontroller mapping.
int32_t getJoystickId() const
Sets the instance id of the joystick.
void load(const std::string &filename)
Loads mapping from file.
void setController(bool value)
Sets to true if the event is for a controller, otherwise false.
void saveMapping(const std::string guid, const std::string &file)
Saves controller mapping for given GUID in the specified file.
void processControllerEvent(SDL_Event event)
Creates and process gamecontroller events.
std::string getGuidString(int32_t deviceIndex)
Return GUID for given device index as string.
Joystick * getJoystick(int32_t instanceId)
Return the joystick with the given instance id.
void loadMapping(const std::string &file)
Loads controller mappings from given file and if possible, it opens the related controllers.
void addJoystickListenerFront(IJoystickListener *listener)
Adds a listener to the front of the listener deque Listener will be notified via the corresponding ev...
Class for Joystick events.
std::string getStringMapping(const std::string &guid)
Return the controller mapping for given GUID as string.
void removeJoystickListener(IJoystickListener *listener)
Removes an added listener from the controller.
Joystick * addJoystick(int32_t deviceIndex)
Adds a joystick with the given device index.
void setButton(int8_t button)
Sets the button index or Joystick::ControllerButton.
void saveMappings(const std::string &file)
Saves all controller mappings that were used during the season.
EventSourceType
Types for different event sources.
JoystickEventType getType() const
Return the event type.
void setHatValue(int8_t value)
Sets the hat value.
void setAxisValue(float value)
Sets the axis value.
int32_t getInstanceId() const
Return the instance id of the joystick.
int32_t getInstanceId() const
Return the instance id of the joystick.
void open()
Opens / activates the joystick and sets values.
virtual void setSource(IEventSource *source)
Sets the source of the event.
std::deque< IJoystickListener * > m_joystickListeners
The Joystick listeners.
virtual void setActive(bool active)
Changes the listener status.
EventSourceType getEventSourceType()
Gets the source type of this event.