1#include "InputEvents.hpp" 
    2#include "RootDisplay.hpp" 
    6CST_Keycode key_buttons[] = { SDLK_a, SDLK_b, SDLK_x, SDLK_y, SDLK_UP, SDLK_DOWN, SDLK_LEFT, SDLK_RIGHT, SDLK_RETURN, SDLK_l, SDLK_r, SDLK_z, SDLK_BACKSPACE, SDLK_UP, SDLK_DOWN, SDLK_q };
 
    8SDL_GameControllerButton pad_buttons[] = { SDL_A, SDL_B, SDL_X, SDL_Y, SDL_UP, SDL_DOWN, SDL_LEFT, SDL_RIGHT, SDL_PLUS, SDL_L, SDL_R, SDL_ZL, SDL_MINUS, SDL_UP_STICK, SDL_DOWN_STICK, SDL_LEFT_STICK, SDL_RIGHT_STICK, SDL_ZR,  };
 
   10#if defined(__WIIU__) && defined(USE_KEYBOARD) 
   11#include "../libs/wiiu_kbd/keybdwrapper.h" 
   15unsigned int nintendo_buttons[] = { A_BUTTON, B_BUTTON, X_BUTTON, Y_BUTTON, UP_BUTTON, DOWN_BUTTON, LEFT_BUTTON, RIGHT_BUTTON, START_BUTTON, L_BUTTON, R_BUTTON, ZL_BUTTON, SELECT_BUTTON, UP_BUTTON, DOWN_BUTTON, LEFT_BUTTON, RIGHT_BUTTON, ZR_BUTTON };
 
   18std::string nintendoButtonNames[] = { 
"a", 
"b", 
"x", 
"y", 
"up", 
"down", 
"left", 
"right", 
"plus", 
"l", 
"r", 
"zl", 
"minus", 
"up", 
"down", 
"left", 
"right", 
"zr" };
 
   21std::string keyButtonNames[] = { 
"a", 
"b", 
"x", 
"y", 
"up", 
"down", 
"left", 
"right", 
"return", 
"l", 
"r", 
"z", 
"backspace", 
"up", 
"down", 
"left", 
"right", 
"q" };
 
   25unsigned int wii_buttons[] = { A_BUTTON, B_BUTTON, L_BUTTON, R_BUTTON, UP_BUTTON, DOWN_BUTTON, LEFT_BUTTON, RIGHT_BUTTON, START_BUTTON, 0, 0, 0, SELECT_BUTTON, 0, 0, 0, 0, 0 };
 
   27std::string wiiButtonNames[] = { 
"a", 
"b", 
"1", 
"2", 
"up", 
"down", 
"left", 
"right", 
"plus", 
"", 
"", 
"", 
"minus", 
"", 
"", 
"", 
"", 
"" };
 
   30unsigned int nunchuk_buttons[] = { A_BUTTON, B_BUTTON, L_BUTTON, R_BUTTON, UP_BUTTON, DOWN_BUTTON, LEFT_BUTTON, RIGHT_BUTTON, START_BUTTON, 0, 0, X_BUTTON, SELECT_BUTTON, UP_BUTTON, DOWN_BUTTON, LEFT_BUTTON, RIGHT_BUTTON, Y_BUTTON };
 
   32std::string nunchukButtonNames[] = { 
"a", 
"b", 
"1", 
"2", 
"up", 
"down", 
"left", 
"right", 
"plus", 
"", 
"", 
"z", 
"minus", 
"up", 
"down", 
"left", 
"right", 
"c" };
 
   35bool InputEvents::bypassKeyEvents = 
false;
 
   37auto defaultKeyName = 
"WiiU Gamepad";
 
   38std::string InputEvents::lastGamepadKey = defaultKeyName;
 
   39unsigned int* currentButtons = nintendo_buttons;
 
   40std::string* currentButtonNames = nintendoButtonNames;
 
   43std::map<std::string, GamepadInfo> gamepadMap = {
 
   45    { 
"Keyboard", 
GamepadInfo(nintendo_buttons, keyButtonNames, 
"keyboard", 
"key") },
 
   47    { 
"WiiU Gamepad", 
GamepadInfo(nintendo_buttons, nintendoButtonNames, 
"wiiu_button", 
"gamepad") },
 
   48    { 
"WiiU Pro Controller", { nintendo_buttons, nintendoButtonNames, 
"wiiu_button", 
"pro" } },
 
   49    { 
"Wii Remote", { wii_buttons, wiiButtonNames, 
"wii_button", 
"remote" } },
 
   50    { 
"Wii Remote and Nunchuk", { nunchuk_buttons, nunchukButtonNames, 
"wii_button", 
"nunchuk" } },
 
   51    { 
"Wii Classic Controller", { nintendo_buttons, nintendoButtonNames, 
"wii_button", 
"classic"} },
 
   53    { 
"Switch Controller", { nintendo_buttons, nintendoButtonNames, 
"switch_button", 
"pro" } },
 
   67InputEvents::InputEvents()
 
   69#if defined(__WIIU__) && defined(USE_KEYBOARD) 
   71    KBWrapper* kbdwrapper = 
new KBWrapper(
true, 
true);
 
   78    if (!SDL_PollEvent(&event))
 
   82    this->type = 
event.type;
 
   88    std::string curControllerName= lastGamepadKey;
 
   91    if (this->type == SDL_KEYDOWN || this->type == SDL_KEYUP) {
 
   93        lastGamepadKey = 
"Keyboard";
 
   94    } 
else if (this->type == SDL_JOYBUTTONDOWN || this->type == SDL_JOYBUTTONUP) {
 
   95        SDL_Joystick* joystickId = SDL_JoystickFromInstanceID(event.jbutton.which);
 
   96        if (joystickId != NULL) {
 
   97            std::string controllerName = SDL_JoystickName(joystickId);
 
   98            lastGamepadKey = defaultKeyName; 
 
   99            if (!controllerName.empty() && gamepadMap.find(controllerName) != gamepadMap.end()){
 
  100                lastGamepadKey = controllerName;
 
  104    if (curControllerName != lastGamepadKey) {
 
  105        printf(
"Switched to controller profile: %s\n", lastGamepadKey.c_str());
 
  106        GamepadInfo& gamepadInfo = gamepadMap[lastGamepadKey];
 
  107        if (gamepadInfo.buttons != 
nullptr) {
 
  108            currentButtons = gamepadInfo.buttons;
 
  114    this->isScrolling = 
false;
 
  117    this->allowTouch = 
false;
 
  118    if (event.type == SDL_MOUSEWHEEL) {
 
  119        this->wheelScroll = 
event.wheel.y;
 
  120        this->isScrolling = 
true;
 
  124    if (this->type == SDL_QUIT)
 
  128    else if (event.key.repeat == 0 && (this->type == SDL_KEYDOWN || this->type == SDL_KEYUP))
 
  130        this->keyCode = 
event.key.keysym.sym;
 
  131        this->mod = 
event.key.keysym.mod;
 
  133    else if (this->type == SDL_JOYBUTTONDOWN || this->type == SDL_JOYBUTTONUP)
 
  135        this->keyCode = 
event.jbutton.button;
 
  137    else if (this->type == SDL_MOUSEMOTION || this->type == SDL_MOUSEBUTTONUP || this->type == SDL_MOUSEBUTTONDOWN)
 
  139        bool isMotion = this->type == SDL_MOUSEMOTION;
 
  141        this->yPos = isMotion ? 
event.motion.y : 
event.button.y;
 
  142        this->xPos = isMotion ? 
event.motion.x : 
event.button.x;
 
  144    else if (allowTouch && (this->type == SDL_FINGERMOTION || this->type == SDL_FINGERUP || this->type == SDL_FINGERDOWN))
 
  146        this->yPos = 
event.tfinger.y * SCREEN_HEIGHT;
 
  147        this->xPos = 
event.tfinger.x * SCREEN_WIDTH;
 
  150    if (this->type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED) {
 
  151        printf(
"Window resized to %dx%d\n", event.window.data1, event.window.data2);
 
  152        RootDisplay::mainDisplay->setScreenResolution(
 
  153            event.window.data1 * RootDisplay::dpiScale,
 
  154            event.window.data2 * RootDisplay::dpiScale
 
  158        if (RootDisplay::mainDisplay->windowResizeCallback)
 
  159            RootDisplay::mainDisplay->windowResizeCallback();
 
  165    this->xPos = (int)(this->xPos * RootDisplay::dpiScale);
 
  166    this->yPos = (int)(this->yPos * RootDisplay::dpiScale);
 
  173bool InputEvents::update()
 
  183void InputEvents::toggleHeldButtons()
 
  185    int directionCode = directionForKeycode();
 
  187    if (directionCode >= 0)
 
  192            if (!held_directions[directionCode])
 
  195                held_directions[directionCode] = 
true;
 
  196                held_type = this->type;
 
  207            held_directions[directionCode] = 
false;
 
  213bool InputEvents::processDirectionalButtons()
 
  220    if (curFrame > 0 && curFrame % rapidFireRate == 0)
 
  222        for (
int x = 0; x < 4; x++)
 
  224            if (!held_directions[x])
 
  228            this->type = held_type;
 
  229            bool isGamepad = (this->type == SDL_JOYBUTTONDOWN || this->type == SDL_JOYBUTTONUP);
 
  230            this->keyCode = isGamepad ? pad_buttons[4 + x] : key_buttons[4 + x]; 
 
  240int InputEvents::directionForKeycode()
 
  243    if (this->type == SDL_KEYDOWN && this->keyCode == SDLK_RETURN)
 
  247    switch (this->keyCode)
 
  261    case SDL_RIGHT_STICK:
 
  274    if ((this->type == SDL_KEYDOWN || this->type == SDL_KEYUP) && !InputEvents::bypassKeyEvents)
 
  276        for (
int x = 0; x < TOTAL_BUTTONS; x++)
 
  277            if (key_buttons[x] == keyCode && (buttons & currentButtons[x]))
 
  282    else if (this->type == SDL_JOYBUTTONDOWN || this->type == SDL_JOYBUTTONUP)
 
  284        for (
int x = 0; x < TOTAL_BUTTONS; x++)
 
  285            if (pad_buttons[x] == keyCode && (buttons & currentButtons[x]))
 
  292bool InputEvents::pressed(
int buttons)
 
  294    return isKeyDown() && 
held(buttons);
 
  297bool InputEvents::released(
int buttons)
 
  299    return isKeyUp() && 
held(buttons);
 
  304    return (this->xPos >= x && this->xPos <= x + width && this->yPos >= y && this->yPos <= y + height);
 
  307bool InputEvents::isTouchDown()
 
  309    return this->type == SDL_MOUSEBUTTONDOWN || (allowTouch && this->type == SDL_FINGERDOWN);
 
  312bool InputEvents::isTouchDrag()
 
  314    return this->type == SDL_MOUSEMOTION || (allowTouch && this->type == SDL_FINGERMOTION);
 
  317bool InputEvents::isTouchUp()
 
  319    return this->type == SDL_MOUSEBUTTONUP || (allowTouch && this->type == SDL_FINGERUP);
 
  322bool InputEvents::isTouch()
 
  324    return isTouchDown() || isTouchDrag() || isTouchUp();
 
  327bool InputEvents::isScroll()
 
  329    return this->isScrolling;
 
  332bool InputEvents::isKeyDown()
 
  334    return this->type == SDL_KEYDOWN || this->type == SDL_JOYBUTTONDOWN;
 
  337bool InputEvents::isKeyUp()
 
  339    return this->type == SDL_KEYUP || this->type == SDL_JOYBUTTONUP;
 
  347    case SDL_JOYDEVICEADDED:
 
  348        j = SDL_JoystickOpen(event->jdevice.which);
 
  350            printf(
"Added joystick device: %s, with ID %d\n", SDL_JoystickName(j), SDL_JoystickInstanceID(j));
 
  352    case SDL_JOYDEVICEREMOVED:
 
  353        j = SDL_JoystickFromInstanceID(event->jdevice.which);
 
  354        if (j && SDL_JoystickGetAttached(j))
 
  356            printf(
"Removed joystick device: %s\n", SDL_JoystickName(j));
 
  357            SDL_JoystickClose(j);
 
  367    return gamepadMap[lastGamepadKey];
 
bool needsRedraw
whether or not this element needs the screen redrawn next time it's processed