Skip to content

Commit

Permalink
D-pad now represented as buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisd1100 committed May 23, 2023
1 parent 50295a4 commit f9a7c36
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 178 deletions.
30 changes: 10 additions & 20 deletions src/hid/nx.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ static void nx_full_state(struct hid_dev *device, const uint8_t *d, MTY_Controll
c->type = MTY_CTYPE_SWITCH;
c->vid = mty_hid_device_get_vid(device);
c->pid = mty_hid_device_get_pid(device);
c->numAxes = 7;
c->numButtons = 14;
c->numAxes = 6;
c->numButtons = 18;
c->id = mty_hid_device_get_id(device);

c->buttons[MTY_CBUTTON_X] = d[3] & 0x01;
Expand All @@ -179,6 +179,10 @@ static void nx_full_state(struct hid_dev *device, const uint8_t *d, MTY_Controll
c->buttons[MTY_CBUTTON_LEFT_THUMB] = d[4] & 0x08;
c->buttons[MTY_CBUTTON_RIGHT_THUMB] = d[4] & 0x04;
c->buttons[MTY_CBUTTON_GUIDE] = d[4] & 0x10;
c->buttons[MTY_CBUTTON_DPAD_UP] = d[5] & 0x2;
c->buttons[MTY_CBUTTON_DPAD_RIGHT] = d[5] & 0x4;
c->buttons[MTY_CBUTTON_DPAD_DOWN] = d[5] & 0x1;
c->buttons[MTY_CBUTTON_DPAD_LEFT] = d[5] & 0x8;
c->buttons[MTY_CBUTTON_TOUCHPAD] = d[4] & 0x20; // The "Capture" button

int16_t lx = (d[6] | ((d[7] & 0x0F) << 8)) - ctx->lx.c;
Expand Down Expand Up @@ -224,17 +228,6 @@ static void nx_full_state(struct hid_dev *device, const uint8_t *d, MTY_Controll
c->axes[MTY_CAXIS_TRIGGER_R].value = c->buttons[MTY_CBUTTON_RIGHT_TRIGGER] ? UINT8_MAX : 0;
c->axes[MTY_CAXIS_TRIGGER_R].min = 0;
c->axes[MTY_CAXIS_TRIGGER_R].max = UINT8_MAX;

bool up = d[5] & 0x02;
bool down = d[5] & 0x01;
bool left = d[5] & 0x08;
bool right = d[5] & 0x04;

c->axes[MTY_CAXIS_DPAD].value = (up && right) ? 1 : (right && down) ? 3 :
(down && left) ? 5 : (left && up) ? 7 : up ? 0 : right ? 2 : down ? 4 : left ? 6 : 8;
c->axes[MTY_CAXIS_DPAD].usage = 0x39;
c->axes[MTY_CAXIS_DPAD].min = 0;
c->axes[MTY_CAXIS_DPAD].max = 7;
}

static void nx_simple_state(struct hid_dev *device, const uint8_t *d, MTY_ControllerEvent *c)
Expand All @@ -244,8 +237,8 @@ static void nx_simple_state(struct hid_dev *device, const uint8_t *d, MTY_Contro
c->type = MTY_CTYPE_SWITCH;
c->vid = mty_hid_device_get_vid(device);
c->pid = mty_hid_device_get_pid(device);
c->numAxes = 7;
c->numButtons = 14;
c->numAxes = 6;
c->numButtons = 18;
c->id = mty_hid_device_get_id(device);

c->buttons[MTY_CBUTTON_X] = d[1] & 0x04;
Expand All @@ -263,6 +256,8 @@ static void nx_simple_state(struct hid_dev *device, const uint8_t *d, MTY_Contro
c->buttons[MTY_CBUTTON_GUIDE] = d[2] & 0x10;
c->buttons[MTY_CBUTTON_TOUCHPAD] = d[2] & 0x20;

mty_hid_axis_to_dpad(d[3], c);

int16_t lx = (d[4] | (d[5] << 8)) - ctx->slx.c;
int16_t ly = -((d[6] | (d[7] << 8)) - ctx->sly.c);
int16_t rx = (d[8] | (d[9] << 8)) - ctx->srx.c;
Expand Down Expand Up @@ -306,11 +301,6 @@ static void nx_simple_state(struct hid_dev *device, const uint8_t *d, MTY_Contro
c->axes[MTY_CAXIS_TRIGGER_R].value = c->buttons[MTY_CBUTTON_RIGHT_TRIGGER] ? UINT8_MAX : 0;
c->axes[MTY_CAXIS_TRIGGER_R].min = 0;
c->axes[MTY_CAXIS_TRIGGER_R].max = UINT8_MAX;

c->axes[MTY_CAXIS_DPAD].value = d[3];
c->axes[MTY_CAXIS_DPAD].usage = 0x39;
c->axes[MTY_CAXIS_DPAD].min = 0;
c->axes[MTY_CAXIS_DPAD].max = 7;
}


Expand Down
11 changes: 4 additions & 7 deletions src/hid/ps4.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ static bool ps4_state(struct hid_dev *device, const void *data, size_t dsize, MT
c->type = MTY_CTYPE_PS4;
c->vid = mty_hid_device_get_vid(device);
c->pid = mty_hid_device_get_pid(device);
c->numAxes = 7;
c->numButtons = 14;
c->numAxes = 6;
c->numButtons = 18;
c->id = mty_hid_device_get_id(device);

c->buttons[MTY_CBUTTON_X] = d8[4] & 0x10;
Expand All @@ -102,6 +102,8 @@ static bool ps4_state(struct hid_dev *device, const void *data, size_t dsize, MT
c->buttons[MTY_CBUTTON_GUIDE] = d8[6] & 0x01;
c->buttons[MTY_CBUTTON_TOUCHPAD] = d8[6] & 0x02;

mty_hid_axis_to_dpad(d8[4] & 0x0F, c);

c->axes[MTY_CAXIS_THUMB_LX].value = d8[0];
c->axes[MTY_CAXIS_THUMB_LX].usage = 0x30;
c->axes[MTY_CAXIS_THUMB_LX].min = 0;
Expand Down Expand Up @@ -136,10 +138,5 @@ static bool ps4_state(struct hid_dev *device, const void *data, size_t dsize, MT
c->axes[MTY_CAXIS_TRIGGER_R].min = 0;
c->axes[MTY_CAXIS_TRIGGER_R].max = UINT8_MAX;

c->axes[MTY_CAXIS_DPAD].value = d8[4] & 0x0F;
c->axes[MTY_CAXIS_DPAD].usage = 0x39;
c->axes[MTY_CAXIS_DPAD].min = 0;
c->axes[MTY_CAXIS_DPAD].max = 7;

return true;
}
9 changes: 3 additions & 6 deletions src/hid/ps5.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ static bool ps5_state(struct hid_dev *device, const void *data, size_t dsize, MT
c->type = MTY_CTYPE_PS5;
c->vid = mty_hid_device_get_vid(device);
c->pid = mty_hid_device_get_pid(device);
c->numAxes = 7;
c->numButtons = 15;
c->numAxes = 6;
c->numButtons = 18;
c->id = mty_hid_device_get_id(device);

c->buttons[MTY_CBUTTON_X] = b[5] & 0x10;
Expand All @@ -109,10 +109,7 @@ static bool ps5_state(struct hid_dev *device, const void *data, size_t dsize, MT
c->buttons[MTY_CBUTTON_GUIDE] = b[7] & 0x01;
c->buttons[MTY_CBUTTON_TOUCHPAD] = b[7] & 0x02;

c->axes[MTY_CAXIS_DPAD].value = b[5] & 0x0F;
c->axes[MTY_CAXIS_DPAD].usage = 0x39;
c->axes[MTY_CAXIS_DPAD].min = 0;
c->axes[MTY_CAXIS_DPAD].max = 7;
mty_hid_axis_to_dpad(b[5] & 0x0F, c);

c->axes[MTY_CAXIS_THUMB_LX].value = a[1];
c->axes[MTY_CAXIS_THUMB_LX].usage = 0x30;
Expand Down
11 changes: 4 additions & 7 deletions src/hid/stadia.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ static bool stadia_state(struct hid_dev *device, const void *data, size_t dsize,
c->vid = mty_hid_device_get_vid(device);
c->pid = mty_hid_device_get_pid(device);

c->numAxes = 7;
c->numButtons = 15;
c->numAxes = 6;
c->numButtons = 19;
c->id = mty_hid_device_get_id(device);

c->buttons[MTY_CBUTTON_X] = d8[3] & 0x10;
Expand All @@ -35,6 +35,8 @@ static bool stadia_state(struct hid_dev *device, const void *data, size_t dsize,
c->buttons[MTY_CBUTTON_TOUCHPAD] = d8[2] & 0x02;
c->buttons[MTY_CBUTTON_CAPTURE] = d8[2] & 0x01;

mty_hid_axis_to_dpad(d8[1] & 0x0F, c);

c->axes[MTY_CAXIS_THUMB_LX].value = d8[4];
c->axes[MTY_CAXIS_THUMB_LX].usage = 0x30;
c->axes[MTY_CAXIS_THUMB_LX].min = 1;
Expand Down Expand Up @@ -69,10 +71,5 @@ static bool stadia_state(struct hid_dev *device, const void *data, size_t dsize,
c->axes[MTY_CAXIS_TRIGGER_R].min = 0;
c->axes[MTY_CAXIS_TRIGGER_R].max = UINT8_MAX;

c->axes[MTY_CAXIS_DPAD].value = d8[1] & 0x0F;
c->axes[MTY_CAXIS_DPAD].usage = 0x39;
c->axes[MTY_CAXIS_DPAD].min = 0;
c->axes[MTY_CAXIS_DPAD].max = 7;

return true;
}
43 changes: 37 additions & 6 deletions src/hid/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,36 @@ void mty_hid_u_to_u8(MTY_Axis *a)
a->max = UINT8_MAX;
}

void mty_hid_axis_to_dpad(int16_t v, MTY_ControllerEvent *c)
{
c->buttons[MTY_CBUTTON_DPAD_UP] = v == 7 || v == 0 || v == 1;
c->buttons[MTY_CBUTTON_DPAD_RIGHT] = v == 1 || v == 2 || v == 3;
c->buttons[MTY_CBUTTON_DPAD_DOWN] = v == 3 || v == 4 || v == 5;
c->buttons[MTY_CBUTTON_DPAD_LEFT] = v == 5 || v == 6 || v == 7;
}


// Default mapping

static void hid_convert_dpad(MTY_ControllerEvent *c, uint8_t index)
{
// Make room for additional D-pad buttons and move existing to end
if (c->numButtons > MTY_CBUTTON_DPAD_UP) {
if (c->numButtons > MTY_CBUTTON_MAX - 4)
c->numButtons = MTY_CBUTTON_MAX - 4;

memcpy(&c->buttons[MTY_CBUTTON_TOUCHPAD], &c->buttons[MTY_CBUTTON_DPAD_UP],
(c->numButtons - MTY_CBUTTON_DPAD_UP) * sizeof(c->buttons[0]));

c->numButtons += 4;

} else if (c->numButtons < MTY_CBUTTON_TOUCHPAD) {
c->numButtons = MTY_CBUTTON_TOUCHPAD;
}

mty_hid_axis_to_dpad(c->axes[index].value, c);
}

static bool hid_swap_value(MTY_Axis *axes, MTY_CAxis a, MTY_CAxis b)
{
if (a != b && axes[a].usage != axes[b].usage) {
Expand Down Expand Up @@ -110,10 +137,12 @@ static void hid_move_value(MTY_ControllerEvent *c, MTY_Axis *a, uint16_t usage,
void mty_hid_map_axes(MTY_ControllerEvent *c)
{
// Make sure there is enough room for the standard CVALUEs
if (c->numAxes < MTY_CAXIS_DPAD + 1)
c->numAxes = MTY_CAXIS_DPAD + 1;
if (c->numAxes < 6)
c->numAxes = 6;

// Swap positions
uint8_t dpad = c->numAxes;

for (uint8_t x = 0; x < c->numAxes; x++) {
retry:

Expand Down Expand Up @@ -142,13 +171,16 @@ void mty_hid_map_axes(MTY_ControllerEvent *c)
if (hid_swap_value(c->axes, x, MTY_CAXIS_TRIGGER_R))
goto retry;
break;
case 0x39: // Hat -> DPAD
if (hid_swap_value(c->axes, x, MTY_CAXIS_DPAD))
goto retry;
case 0x39: // Hat
dpad = x;
break;
}
}

// Convert Hat to D-pad
if (dpad < c->numAxes);
hid_convert_dpad(c, dpad);

// Move axes that are not in the right positions to the end
for (uint8_t x = 0; x < c->numAxes; x++) {
MTY_Axis *a = &c->axes[x];
Expand All @@ -160,7 +192,6 @@ void mty_hid_map_axes(MTY_ControllerEvent *c)
case MTY_CAXIS_THUMB_RY: hid_move_value(c, a, 0x35, 0, INT16_MIN, INT16_MAX); break;
case MTY_CAXIS_TRIGGER_L: hid_move_value(c, a, 0x33, 0, 0, UINT8_MAX); break;
case MTY_CAXIS_TRIGGER_R: hid_move_value(c, a, 0x34, 0, 0, UINT8_MAX); break;
case MTY_CAXIS_DPAD: hid_move_value(c, a, 0x39, 8, 0, 7); break;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/hid/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
void mty_hid_u_to_s16(MTY_Axis *v, bool invert);
void mty_hid_s_to_s16(MTY_Axis *v);
void mty_hid_u_to_u8(MTY_Axis *v);
void mty_hid_axis_to_dpad(int16_t v, MTY_ControllerEvent *c);
void mty_hid_map_axes(MTY_ControllerEvent *c);
bool mty_hid_dedupe(MTY_Hash *h, MTY_ControllerEvent *c);
11 changes: 4 additions & 7 deletions src/hid/xbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ static bool xbox_state(struct hid_dev *device, const void *data, size_t dsize, M
c->type = MTY_CTYPE_XBOX;
c->vid = mty_hid_device_get_vid(device);
c->pid = mty_hid_device_get_pid(device);
c->numAxes = 7;
c->numButtons = 14;
c->numAxes = 6;
c->numButtons = ctx->series_x ? 18 : 17;
c->id = mty_hid_device_get_id(device);

if (ctx->proto == XBOX_PROTO_UNKNOWN && d8[15] & 0x10)
Expand All @@ -106,6 +106,8 @@ static bool xbox_state(struct hid_dev *device, const void *data, size_t dsize, M
c->buttons[MTY_CBUTTON_GUIDE] = ctx->guide;
c->buttons[MTY_CBUTTON_TOUCHPAD] = ctx->series_x ? d8[16] & 0x01 : 0;

mty_hid_axis_to_dpad(d8[13] > 0 ? d8[13] - 1 : 8, c);

c->axes[MTY_CAXIS_THUMB_LX].value = *((int16_t *) (d8 + 1)) - 0x8000;
c->axes[MTY_CAXIS_THUMB_LX].usage = 0x30;
c->axes[MTY_CAXIS_THUMB_LX].min = INT16_MIN;
Expand Down Expand Up @@ -141,11 +143,6 @@ static bool xbox_state(struct hid_dev *device, const void *data, size_t dsize, M
c->buttons[MTY_CBUTTON_LEFT_TRIGGER] = c->axes[MTY_CAXIS_TRIGGER_L].value > 0;
c->buttons[MTY_CBUTTON_RIGHT_TRIGGER] = c->axes[MTY_CAXIS_TRIGGER_R].value > 0;

c->axes[MTY_CAXIS_DPAD].value = d8[13] > 0 ? d8[13] - 1 : 8;
c->axes[MTY_CAXIS_DPAD].usage = 0x39;
c->axes[MTY_CAXIS_DPAD].min = 0;
c->axes[MTY_CAXIS_DPAD].max = 7;

r = true;

} else if (d8[0] == 0x02) {
Expand Down
19 changes: 6 additions & 13 deletions src/hid/xboxw.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ static bool xboxw_state(struct hid_dev *device, const void *data, size_t size, M

c->vid = mty_hid_device_get_vid(device);
c->pid = mty_hid_device_get_pid(device);
c->numAxes = 7;
c->numButtons = 13;
c->numAxes = 6;
c->numButtons = 17;
c->type = MTY_CTYPE_XBOXW;
c->id = mty_hid_device_get_id(device);

Expand All @@ -26,6 +26,10 @@ static bool xboxw_state(struct hid_dev *device, const void *data, size_t size, M
c->buttons[MTY_CBUTTON_LEFT_THUMB] = d[2] & 0x40;
c->buttons[MTY_CBUTTON_RIGHT_THUMB] = d[2] & 0x80;
c->buttons[MTY_CBUTTON_GUIDE] = d[3] & 0x04;
c->buttons[MTY_CBUTTON_DPAD_UP] = d[2] & 0x1;
c->buttons[MTY_CBUTTON_DPAD_RIGHT] = d[2] & 0x8;
c->buttons[MTY_CBUTTON_DPAD_DOWN] = d[2] & 0x2;
c->buttons[MTY_CBUTTON_DPAD_LEFT] = d[2] & 0x4;

c->axes[MTY_CAXIS_THUMB_LX].value = *((int16_t *) (d + 6));
c->axes[MTY_CAXIS_THUMB_LX].usage = 0x30;
Expand Down Expand Up @@ -62,16 +66,5 @@ static bool xboxw_state(struct hid_dev *device, const void *data, size_t size, M
c->buttons[MTY_CBUTTON_LEFT_TRIGGER] = c->axes[MTY_CAXIS_TRIGGER_L].value > 0;
c->buttons[MTY_CBUTTON_RIGHT_TRIGGER] = c->axes[MTY_CAXIS_TRIGGER_R].value > 0;

bool up = d[2] & 0x01;
bool down = d[2] & 0x02;
bool left = d[2] & 0x04;
bool right = d[2] & 0x08;

c->axes[MTY_CAXIS_DPAD].value = (up && right) ? 1 : (right && down) ? 3 :
(down && left) ? 5 : (left && up) ? 7 : up ? 0 : right ? 2 : down ? 4 : left ? 6 : 8;
c->axes[MTY_CAXIS_DPAD].usage = 0x39;
c->axes[MTY_CAXIS_DPAD].min = 0;
c->axes[MTY_CAXIS_DPAD].max = 7;

return true;
}
26 changes: 7 additions & 19 deletions src/matoya.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,6 @@ extern "C" {
#define MTY_WINDOW_MAX 8 ///< Maximum number of windows that can be created.
#define MTY_SCREEN_MAX 32 ///< Maximum size of a screen identifier.

#define MTY_DPAD(c) \
((c)->axes[MTY_CAXIS_DPAD].value)

#define MTY_DPAD_UP(c) \
(MTY_DPAD(c) == 7 || MTY_DPAD(c) == 0 || MTY_DPAD(c) == 1)

#define MTY_DPAD_RIGHT(c) \
(MTY_DPAD(c) == 1 || MTY_DPAD(c) == 2 || MTY_DPAD(c) == 3)

#define MTY_DPAD_DOWN(c) \
(MTY_DPAD(c) == 3 || MTY_DPAD(c) == 4 || MTY_DPAD(c) == 5)

#define MTY_DPAD_LEFT(c) \
(MTY_DPAD(c) == 5 || MTY_DPAD(c) == 6 || MTY_DPAD(c) == 7)

typedef struct MTY_App MTY_App;
typedef int8_t MTY_Window;

Expand Down Expand Up @@ -405,14 +390,18 @@ typedef enum {
MTY_CBUTTON_LEFT_THUMB = 10, ///< Left Thumb Stick
MTY_CBUTTON_RIGHT_THUMB = 11, ///< Right Thumb Stick
MTY_CBUTTON_GUIDE = 12, ///< Guide Button
MTY_CBUTTON_TOUCHPAD = 13, ///< Touchpad Button
MTY_CBUTTON_CAPTURE = 14, ///< Capture Button (Stadia)
MTY_CBUTTON_DPAD_UP = 13, ///< D-pad Up
MTY_CBUTTON_DPAD_RIGHT = 14, ///< D-pad Right
MTY_CBUTTON_DPAD_DOWN = 15, ///< D-pad Down
MTY_CBUTTON_DPAD_LEFT = 16, ///< D-pad Left
MTY_CBUTTON_TOUCHPAD = 17, ///< Touchpad Button
MTY_CBUTTON_CAPTURE = 18, ///< Capture Button (Stadia)
MTY_CBUTTON_MAX = 64, ///< Maximum number of possible buttons.
MTY_CBUTTON_MAKE_32 = INT32_MAX,
} MTY_CButton;

/// @brief Standardized controller axes.
/// @details libmatoya makes a best effort to remap axes (sticks, triggers, and DPAD)
/// @details libmatoya makes a best effort to remap axes (sticks, triggers)
/// of known controllers to these values.
typedef enum {
MTY_CAXIS_THUMB_LX = 0, ///< Left Thumb Stick X-axis.
Expand All @@ -421,7 +410,6 @@ typedef enum {
MTY_CAXIS_THUMB_RY = 3, ///< Right Thumb Stick Y-axis.
MTY_CAXIS_TRIGGER_L = 4, ///< Left Trigger
MTY_CAXIS_TRIGGER_R = 5, ///< Right Trigger
MTY_CAXIS_DPAD = 6, ///< DPAD
MTY_CAXIS_MAX = 16, ///< Maximum number of possible axes.
MTY_CAXIS_MAKE_32 = INT32_MAX,
} MTY_CAxis;
Expand Down
Loading

0 comments on commit f9a7c36

Please sign in to comment.