Skip to content

Commit

Permalink
Input event and device enums (hrydgard#17514)
Browse files Browse the repository at this point in the history
* Switch deviceID from int to enum InputDeviceID, globally

* Switch axisId to enum InputAxis

* Change int keycodes to InputKeyCode where it makes sense.

* SDL input buildfix

* SDL keycode buildfix

* Switch on enum warning fixes

* Qt keycode buildfix

* iOS keycode buildfix

* UWP keycode buildfix

* More iOS buildfix

* More iOS buildfix

* Update DinputDevice.cpp
  • Loading branch information
hrydgard authored May 26, 2023
1 parent 6f380a7 commit 2675d6e
Show file tree
Hide file tree
Showing 32 changed files with 129 additions and 133 deletions.
16 changes: 8 additions & 8 deletions Common/Input/InputState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ std::vector<InputMapping> confirmKeys;
std::vector<InputMapping> cancelKeys;
std::vector<InputMapping> tabLeftKeys;
std::vector<InputMapping> tabRightKeys;
static std::unordered_map<int, int> uiFlipAnalogY;
static std::unordered_map<InputDeviceID, int> uiFlipAnalogY;

static void AppendKeys(std::vector<InputMapping> &keys, const std::vector<InputMapping> &newKeys) {
for (auto iter = newKeys.begin(); iter != newKeys.end(); ++iter) {
Expand Down Expand Up @@ -69,11 +69,11 @@ void SetTabLeftRightKeys(const std::vector<InputMapping> &tabLeft, const std::ve
tabRightKeys = tabRight;
}

void SetAnalogFlipY(std::unordered_map<int, int> flipYByDeviceId) {
void SetAnalogFlipY(std::unordered_map<InputDeviceID, int> flipYByDeviceId) {
uiFlipAnalogY = flipYByDeviceId;
}

int GetAnalogYDirection(int deviceId) {
int GetAnalogYDirection(InputDeviceID deviceId) {
auto configured = uiFlipAnalogY.find(deviceId);
if (configured != uiFlipAnalogY.end())
return configured->second;
Expand All @@ -84,8 +84,8 @@ int GetAnalogYDirection(int deviceId) {
InputMapping InputMapping::FromConfigString(const std::string &str) {
std::vector<std::string> parts;
SplitString(str, '-', parts);
int deviceId = atoi(parts[0].c_str());
int keyCode = atoi(parts[1].c_str());
InputDeviceID deviceId = (InputDeviceID)(atoi(parts[0].c_str()));
InputKeyCode keyCode = (InputKeyCode)atoi(parts[1].c_str());

InputMapping mapping;
mapping.deviceId = deviceId;
Expand All @@ -94,15 +94,15 @@ InputMapping InputMapping::FromConfigString(const std::string &str) {
}

std::string InputMapping::ToConfigString() const {
return StringFromFormat("%d-%d", deviceId, keyCode);
return StringFromFormat("%d-%d", (int)deviceId, keyCode);
}

void InputMapping::FormatDebug(char *buffer, size_t bufSize) const {
if (IsAxis()) {
int direction;
int axis = Axis(&direction);
snprintf(buffer, bufSize, "Device: %d Axis: %d (%d)", deviceId, axis, direction);
snprintf(buffer, bufSize, "Device: %d Axis: %d (%d)", (int)deviceId, axis, direction);
} else {
snprintf(buffer, bufSize, "Device: %d Key: %d", deviceId, keyCode);
snprintf(buffer, bufSize, "Device: %d Key: %d", (int)deviceId, keyCode);
}
}
64 changes: 20 additions & 44 deletions Common/Input/InputState.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

// Default device IDs

enum {
enum InputDeviceID {
DEVICE_ID_ANY = -1, // Represents any device ID
DEVICE_ID_DEFAULT = 0, // Old Android
DEVICE_ID_KEYBOARD = 1, // PC keyboard, android keyboards
Expand All @@ -38,43 +38,15 @@ enum {
DEVICE_ID_TOUCH = 42,
};

inline InputDeviceID operator +(InputDeviceID deviceID, int addend) {
return (InputDeviceID)((int)deviceID + addend);
}

//number of contiguous generic joypad IDs
const int MAX_NUM_PADS = 10;

const char *GetDeviceName(int deviceId);

enum {
PAD_BUTTON_A = 1,
PAD_BUTTON_B = 2,
PAD_BUTTON_X = 4,
PAD_BUTTON_Y = 8,
PAD_BUTTON_LBUMPER = 16,
PAD_BUTTON_RBUMPER = 32,
PAD_BUTTON_START = 64,
PAD_BUTTON_SELECT = 128,
PAD_BUTTON_UP = 256,
PAD_BUTTON_DOWN = 512,
PAD_BUTTON_LEFT = 1024,
PAD_BUTTON_RIGHT = 2048,

PAD_BUTTON_MENU = 4096,
PAD_BUTTON_BACK = 8192,

// For Qt
PAD_BUTTON_JOY_UP = 1<<14,
PAD_BUTTON_JOY_DOWN = 1<<15,
PAD_BUTTON_JOY_LEFT = 1<<16,
PAD_BUTTON_JOY_RIGHT = 1<<17,

PAD_BUTTON_LEFT_THUMB = 1 << 18, // Click left thumb stick on X360
PAD_BUTTON_RIGHT_THUMB = 1 << 19, // Click right thumb stick on X360

PAD_BUTTON_LEFT_TRIGGER = 1 << 21, // Click left thumb stick on X360
PAD_BUTTON_RIGHT_TRIGGER = 1 << 22, // Click left thumb stick on X360

PAD_BUTTON_FASTFORWARD = 1 << 20, // Click Tab to unthrottle
};

#ifndef MAX_KEYQUEUESIZE
#define MAX_KEYQUEUESIZE 20
#endif
Expand All @@ -98,18 +70,18 @@ class InputMapping {
return AXIS_BIND_NKCODE_START + axisId * 2 + (direction < 0 ? 1 : 0);
}
public:
InputMapping() : deviceId(0), keyCode(0) {}
InputMapping() : deviceId(DEVICE_ID_DEFAULT), keyCode(0) {}
// From a key mapping
InputMapping(int _deviceId, int key) : deviceId(_deviceId), keyCode(key) {}
InputMapping(InputDeviceID _deviceId, int key) : deviceId(_deviceId), keyCode(key) {}
// From an axis
InputMapping(int _deviceId, int axis, int direction) : deviceId(_deviceId), keyCode(TranslateKeyCodeFromAxis(axis, direction)) {
InputMapping(InputDeviceID _deviceId, int axis, int direction) : deviceId(_deviceId), keyCode(TranslateKeyCodeFromAxis(axis, direction)) {
_dbg_assert_(direction != 0);
}

static InputMapping FromConfigString(const std::string &str);
std::string ToConfigString() const;

int deviceId;
InputDeviceID deviceId;
int keyCode; // Can also represent an axis with direction, if encoded properly.

bool IsAxis() const {
Expand Down Expand Up @@ -195,15 +167,19 @@ enum {

struct KeyInput {
KeyInput() {}
KeyInput(int devId, int code, int fl) : deviceId(devId), keyCode(code), flags(fl) {}
int deviceId;
int keyCode; // Android keycodes are the canonical keycodes, everyone else map to them.
KeyInput(InputDeviceID devId, InputKeyCode code, int fl) : deviceId(devId), keyCode(code), flags(fl) {}
KeyInput(InputDeviceID devId, int unicode) : deviceId(devId), unicodeChar(unicode), flags(KEY_CHAR) {}
InputDeviceID deviceId;
union {
InputKeyCode keyCode; // Android keycodes are the canonical keycodes, everyone else map to them.
int unicodeChar; // for KEY_CHAR
};
int flags;
};

struct AxisInput {
int deviceId;
int axisId; // Android axis Ids are the canonical ones.
InputDeviceID deviceId;
InputAxis axisId;
float value;
};

Expand All @@ -219,5 +195,5 @@ void SetConfirmCancelKeys(const std::vector<InputMapping> &confirm, const std::v
void SetTabLeftRightKeys(const std::vector<InputMapping> &tabLeft, const std::vector<InputMapping> &tabRight);

// 0 means unknown (attempt autodetect), -1 means flip, 1 means original direction.
void SetAnalogFlipY(std::unordered_map<int, int> flipYByDeviceId);
int GetAnalogYDirection(int deviceId);
void SetAnalogFlipY(std::unordered_map<InputDeviceID, int> flipYByDeviceId);
int GetAnalogYDirection(InputDeviceID deviceId);
8 changes: 5 additions & 3 deletions Common/Input/KeyCodes.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

typedef enum _keycode_t {
// These mostly match Android keycodes.
enum InputKeyCode {
NKCODE_BUTTON_CROSS = 23, // trackpad or X button(Xperia Play) is pressed
NKCODE_BUTTON_CROSS_PS3 = 96, // PS3 X button is pressed
NKCODE_BUTTON_CIRCLE = 1004, // Special custom keycode generated from 'O' button by our java code. Or 'O' button if Alt is pressed (TODO)
Expand Down Expand Up @@ -259,9 +260,10 @@ typedef enum _keycode_t {
NKCODE_EXT_ROTATION_RIGHT = 1114,

NKCODE_MAX
} keycode_t;
};

enum AndroidJoystickAxis {
// These mostly match Android axis IDs.
enum InputAxis {
// Field descriptor #15 I
JOYSTICK_AXIS_X = 0,
JOYSTICK_AXIS_Y = 1,
Expand Down
8 changes: 5 additions & 3 deletions Common/UI/Root.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ static int frameCount;
// completely broken input where the original keypresses have deviceId = 10 and the repeats
// have deviceId = 0.
struct HeldKey {
int key;
int deviceId;
InputKeyCode key;
InputDeviceID deviceId;
double triggerTime;

// Ignores startTime
Expand Down Expand Up @@ -250,6 +250,8 @@ bool KeyEvent(const KeyInput &key, ViewGroup *root) {
case NKCODE_VOLUME_MUTE:
retval = false;
break;
default:
break;
}

return retval;
Expand Down Expand Up @@ -318,7 +320,7 @@ bool AxisEvent(const AxisInput &axis, ViewGroup *root) {

// Cannot use the remapper since this is for the menu, so we provide our own
// axis->button emulation here.
auto GenerateKeyFromAxis = [&](DirState old, DirState cur, keycode_t neg_key, keycode_t pos_key) {
auto GenerateKeyFromAxis = [&](DirState old, DirState cur, InputKeyCode neg_key, InputKeyCode pos_key) {
if (old == cur)
return;
if (old == DirState::POS) {
Expand Down
4 changes: 4 additions & 0 deletions Common/UI/ScrollView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ bool ScrollView::Key(const KeyInput &input) {
case DEVICE_ID_XR_CONTROLLER_RIGHT:
scrollSpeed = 50;
break;
default:
break;
}

if (input.flags & KEY_DOWN) {
Expand All @@ -127,6 +129,8 @@ bool ScrollView::Key(const KeyInput &input) {
case NKCODE_EXT_MOUSEWHEEL_DOWN:
ScrollRelative(scrollSpeed);
break;
default:
break;
}
}
return ViewGroup::Key(input);
Expand Down
12 changes: 9 additions & 3 deletions Common/UI/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ bool Clickable::Key(const KeyInput &key) {
down_ = false;
ret = true;
}
} else if (IsEscapeKey(key)) {
} else if (down_ && IsEscapeKey(key)) {
down_ = false;
}
}
Expand Down Expand Up @@ -1190,6 +1190,8 @@ bool TextEdit::Key(const KeyInput &input) {
case NKCODE_BACK:
case NKCODE_ESCAPE:
return false;
default:
break;
}

if (ctrlDown_) {
Expand Down Expand Up @@ -1227,6 +1229,8 @@ bool TextEdit::Key(const KeyInput &input) {
case NKCODE_Z:
text_ = undo_;
break;
default:
break;
}
}

Expand All @@ -1244,6 +1248,8 @@ bool TextEdit::Key(const KeyInput &input) {
case NKCODE_CTRL_RIGHT:
ctrlDown_ = false;
break;
default:
break;
}
}

Expand Down Expand Up @@ -1369,7 +1375,7 @@ bool Slider::Key(const KeyInput &input) {
}
}

bool Slider::ApplyKey(int keyCode) {
bool Slider::ApplyKey(InputKeyCode keyCode) {
switch (keyCode) {
case NKCODE_DPAD_LEFT:
case NKCODE_MINUS:
Expand Down Expand Up @@ -1497,7 +1503,7 @@ bool SliderFloat::Key(const KeyInput &input) {
}
}

bool SliderFloat::ApplyKey(int keyCode) {
bool SliderFloat::ApplyKey(InputKeyCode keyCode) {
switch (keyCode) {
case NKCODE_DPAD_LEFT:
case NKCODE_MINUS:
Expand Down
9 changes: 5 additions & 4 deletions Common/UI/View.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "Common/Math/lin/matrix4x4.h"
#include "Common/Math/math_util.h"
#include "Common/Math/geom2d.h"
#include "Common/Input/KeyCodes.h"

#include "Common/Common.h"

Expand Down Expand Up @@ -619,7 +620,7 @@ class Slider : public Clickable {
Event OnChange;

private:
bool ApplyKey(int keyCode);
bool ApplyKey(InputKeyCode keyCode);

int *value_;
bool showPercent_;
Expand All @@ -629,7 +630,7 @@ class Slider : public Clickable {
float paddingRight_;
int step_;
int repeat_ = 0;
int repeatCode_ = 0;
InputKeyCode repeatCode_ = NKCODE_UNKNOWN;
};

class SliderFloat : public Clickable {
Expand All @@ -649,15 +650,15 @@ class SliderFloat : public Clickable {
Event OnChange;

private:
bool ApplyKey(int keyCode);
bool ApplyKey(InputKeyCode keyCode);

float *value_;
float minValue_;
float maxValue_;
float paddingLeft_;
float paddingRight_;
int repeat_;
int repeatCode_ = 0;
InputKeyCode repeatCode_ = NKCODE_UNKNOWN;
};

// Basic button that modifies a bitfield based on the pressed status. Supports multitouch.
Expand Down
12 changes: 6 additions & 6 deletions Common/VR/PPSSPPVR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ enum VRMirroring {

static VRAppMode appMode = VR_MENU_MODE;
static std::map<int, std::map<int, float> > pspAxis;
static std::map<int, bool> pspKeys;
static std::map<int, bool> pspKeys; // key can be virtual, so not using the enum.

static int vr3DGeometryCount = 0;
static long vrCompat[VR_COMPAT_MAX];
Expand All @@ -69,11 +69,11 @@ VR button mapping

struct ButtonMapping {
ovrButton ovr;
int keycode;
InputKeyCode keycode;
bool pressed;
int repeat;

ButtonMapping(int keycode, ovrButton ovr) {
ButtonMapping(InputKeyCode keycode, ovrButton ovr) {
this->keycode = keycode;
this->ovr = ovr;
pressed = false;
Expand Down Expand Up @@ -106,7 +106,7 @@ static std::vector<ButtonMapping> rightControllerMapping = {
ButtonMapping(NKCODE_ENTER, ovrButton_Trigger),
};

static const int controllerIds[] = {DEVICE_ID_XR_CONTROLLER_LEFT, DEVICE_ID_XR_CONTROLLER_RIGHT};
static const InputDeviceID controllerIds[] = {DEVICE_ID_XR_CONTROLLER_LEFT, DEVICE_ID_XR_CONTROLLER_RIGHT};
static std::vector<ButtonMapping> controllerMapping[2] = {
leftControllerMapping,
rightControllerMapping
Expand Down Expand Up @@ -223,7 +223,7 @@ void SetVRAppMode(VRAppMode mode) {

void UpdateVRInput(bool haptics, float dp_xscale, float dp_yscale) {
//axis
if (pspKeys[VIRTKEY_VR_CAMERA_ADJUST]) {
if (pspKeys[(int)VIRTKEY_VR_CAMERA_ADJUST]) {
AxisInput axis = {};
for (int j = 0; j < 2; j++) {
XrVector2f joystick = IN_VRGetJoystickState(j);
Expand Down Expand Up @@ -515,7 +515,7 @@ bool UpdateVRKeys(const KeyInput &key) {
pspKeys[VIRTKEY_VR_CAMERA_ADJUST] = false;
for (auto& pspKey : pspKeys) {
if (pspKey.second) {
keyUp.keyCode = pspKey.first;
keyUp.keyCode = (InputKeyCode)pspKey.first;
NativeKey(keyUp);
}
}
Expand Down
Loading

0 comments on commit 2675d6e

Please sign in to comment.