void PollForJoystickInput(int cpad) { int joyid = conf->get_joyid(cpad); if (!JoystickIdWithinBounds(joyid)) return; SDL_JoystickUpdate(); for (int i = 0; i < MAX_KEYS; i++) { JoystickInfo* pjoy = s_vjoysticks[joyid]; switch (type_of_joykey(cpad, i)) { case PAD_JOYBUTTONS: { int value = SDL_JoystickGetButton((pjoy)->GetJoy(), key_to_button(cpad, i)); if (value) key_status->press(cpad, i); else key_status->release(cpad, i); break; } case PAD_HAT: { int value = SDL_JoystickGetHat((pjoy)->GetJoy(), key_to_axis(cpad, i)); // key_to_hat_dir and SDL_JoystickGetHat are a 4 bits bitmap, one for each directions. Only 1 bit can be high for // key_to_hat_dir. SDL_JoystickGetHat handles diagonal too (2 bits) so you must check the intersection // '&' not only equality '=='. -- Gregory if (key_to_hat_dir(cpad, i) & value) key_status->press(cpad, i); else key_status->release(cpad, i); break; } case PAD_AXIS: { int value = pjoy->GetAxisFromKey(cpad, i); bool sign = key_to_axis_sign(cpad, i); bool full_axis = key_to_axis_type(cpad, i); if (IsAnalogKey(i)) { if (abs(value) > pjoy->GetDeadzone()) key_status->press(cpad, i, value); else key_status->release(cpad, i); } else { if (full_axis) { value += 0x8000; if (value > pjoy->GetDeadzone()) key_status->press(cpad, i, min(value/256 , 0xFF)); else key_status->release(cpad, i); } else { if (sign && (-value > pjoy->GetDeadzone())) key_status->press(cpad, i, min(-value /128, 0xFF)); else if (!sign && (value > pjoy->GetDeadzone())) key_status->press(cpad, i, min(value /128, 0xFF)); else key_status->release(cpad, i); } } } default: break; } } }
string KeyName(int pad, int key, int keysym) { string tmp; tmp.resize(28); if (keysym) { if (keysym < 10) { // mouse switch (keysym) { case 1: sprintf(&tmp[0], "Mouse Left"); break; case 2: sprintf(&tmp[0], "Mouse Middle"); break; case 3: sprintf(&tmp[0], "Mouse Right"); break; default: // Use only number for extra button sprintf(&tmp[0], "Mouse %d", keysym); } } else { // keyboard const char* pstr = PlatformKeysymToString(keysym); if (pstr != NULL) tmp = pstr; } } else { // joystick KeyType k = type_of_joykey(pad, key); switch (k) { case PAD_JOYBUTTONS: { int button = key_to_button(pad, key); sprintf(&tmp[0], "JBut %d", button); break; } case PAD_AXIS: { if (key_to_axis_type(pad,key)) sprintf(&tmp[0], "JAxis %d Full", key_to_axis(pad, key)); else sprintf(&tmp[0], "JAxis %d Half%s", key_to_axis(pad, key), key_to_axis_sign(pad, key) ? "-" : "+"); break; } case PAD_HAT: { int axis = key_to_axis(pad, key); switch(key_to_hat_dir(pad, key)) { case SDL_HAT_UP: sprintf(&tmp[0], "JPOVU-%d", axis); break; case SDL_HAT_RIGHT: sprintf(&tmp[0], "JPOVR-%d", axis); break; case SDL_HAT_DOWN: sprintf(&tmp[0], "JPOVD-%d", axis); break; case SDL_HAT_LEFT: sprintf(&tmp[0], "JPOVL-%d", axis); break; } break; } default: break; } } return tmp; }