Skip to content

Commit

Permalink
show message when controller is connected/disconnected (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras authored Oct 7, 2024
1 parent 9a1db91 commit 635147e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ void Application::processEvents()

case SDL_CONTROLLERDEVICEADDED:
case SDL_CONTROLLERDEVICEREMOVED:
_input.processEvent(&event, &_keybinds);
_input.processEvent(&event, &_keybinds, isGameActive() ? &_video : nullptr);
break;

case SDL_CONTROLLERBUTTONUP:
Expand Down
20 changes: 15 additions & 5 deletions src/components/Input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,16 +282,16 @@ void Input::axisEvent(int port, Axis axis, int16_t value)
_info[port][_devices[port]]._axis[raxis] = value;
}

void Input::processEvent(const SDL_Event* event, KeyBinds* keyBinds)
void Input::processEvent(const SDL_Event* event, KeyBinds* keyBinds, libretro::VideoComponent* video)
{
switch (event->type)
{
case SDL_CONTROLLERDEVICEADDED:
addController(event, keyBinds);
addController(event, keyBinds, video);
break;

case SDL_CONTROLLERDEVICEREMOVED:
removeController(event);
removeController(event, video);
break;
}
}
Expand Down Expand Up @@ -677,7 +677,7 @@ KeyBinds::Binding Input::captureButtonPress()
return desc;
}

void Input::addController(const SDL_Event* event, KeyBinds* keyBinds)
void Input::addController(const SDL_Event* event, KeyBinds* keyBinds, libretro::VideoComponent* video)
{
SDL_JoystickID id = addController(event->cdevice.which);
if (id >= 0)
Expand All @@ -698,11 +698,16 @@ void Input::addController(const SDL_Event* event, KeyBinds* keyBinds)
}

pad->second._navigationPort = keyBinds->getNavigationPort(id);

if (pad->second._navigationPort != -1 && video) {
std::string message = pad->second._controllerName + std::string(" connected");
video->showMessage(message.c_str(), 200);
}
}
}
}

void Input::removeController(const SDL_Event* event)
void Input::removeController(const SDL_Event* event, libretro::VideoComponent* video)
{
auto it = _pads.find(event->cdevice.which);

Expand All @@ -711,6 +716,11 @@ void Input::removeController(const SDL_Event* event)
Pad* pad = &it->second;
_logger->info(TAG "Controller %s (%s) removed", pad->_controllerName, pad->_joystickName);

if (video && pad->_navigationPort != -1) {
std::string message = pad->_controllerName + std::string(" disconnected");
video->showMessage(message.c_str(), 200);
}

// cleanup
if (pad->_haptic)
SDL_HapticClose(pad->_haptic);
Expand Down
6 changes: 3 additions & 3 deletions src/components/Input.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Input: public libretro::InputComponent
void autoAssign();
void buttonEvent(int port, Button button, bool pressed);
void axisEvent(int port, Axis axis, int16_t value);
void processEvent(const SDL_Event* event, KeyBinds* keyBinds);
void processEvent(const SDL_Event* event, KeyBinds* keyBinds, libretro::VideoComponent* video);
void mouseButtonEvent(MouseButton button, bool pressed);
void mouseMoveEvent(int relative_x, int relative_y, int absolute_x, int absolute_y);
void keyboardEvent(enum retro_key key, bool pressed);
Expand Down Expand Up @@ -153,8 +153,8 @@ class Input: public libretro::InputComponent
};

SDL_JoystickID addController(int which);
void addController(const SDL_Event* event, KeyBinds* keyBinds);
void removeController(const SDL_Event* event);
void addController(const SDL_Event* event, KeyBinds* keyBinds, libretro::VideoComponent* video);
void removeController(const SDL_Event* event, libretro::VideoComponent* video);

static const char* s_getType(int index, void* udata);
static const char* s_getPad(int index, void* udata);
Expand Down

0 comments on commit 635147e

Please sign in to comment.