Skip to content

Commit 9fe4790

Browse files
committed
- add more switch buttons, add descriptions to if
- add 3ds buttons and use them - define console_input
1 parent e95e147 commit 9fe4790

File tree

3 files changed

+168
-25
lines changed

3 files changed

+168
-25
lines changed

src/platform/capabilities.hpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,23 @@ namespace utils {
7070
{static_cast<u8>(CrossPlatformAction::OPEN_SETTINGS), { JOYCON_Y }},
7171
{ static_cast<u8>(CrossPlatformAction::TAB), {}}, // no tab support
7272
};
73-
//TODO: support 3ds
73+
#elif defined(__3DS__)
74+
{
75+
{ static_cast<u8>(CrossPlatformAction::OK),{ JOYCON_A } },
76+
{ static_cast<u8>(CrossPlatformAction::PAUSE), { JOYCON_SELECT }},
77+
{ static_cast<u8>(CrossPlatformAction::UNPAUSE), { JOYCON_SELECT }},
78+
{ static_cast<u8>(CrossPlatformAction::EXIT), { JOYCON_START }},
79+
{ static_cast<u8>(CrossPlatformAction::DOWN),
80+
{ JOYCON_DPAD_DOWN, JOYCON_CSTICK_DOWN, JOYCON_CPAD_DOWN } },
81+
{ static_cast<u8>(CrossPlatformAction::UP), { JOYCON_DPAD_UP, JOYCON_CSTICK_UP, JOYCON_CPAD_UP }},
82+
{ static_cast<u8>(CrossPlatformAction::LEFT),
83+
{ JOYCON_DPAD_LEFT, JOYCON_CSTICK_LEFT, JOYCON_CPAD_LEFT } },
84+
{ static_cast<u8>(CrossPlatformAction::RIGHT),
85+
{ JOYCON_DPAD_RIGHT, JOYCON_CSTICK_RIGHT, JOYCON_CPAD_RIGHT } },
86+
{ static_cast<u8>(CrossPlatformAction::CLOSE), { JOYCON_X }},
87+
{static_cast<u8>(CrossPlatformAction::OPEN_SETTINGS), { JOYCON_Y }},
88+
{ static_cast<u8>(CrossPlatformAction::TAB), {}}, // no tab support
89+
};
7490
#else
7591
{
7692
{ static_cast<u8>(CrossPlatformAction::OK), { SDLK_RETURN, SDLK_SPACE }},

src/platform/console_buttons.hpp

Lines changed: 103 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,125 @@
1414

1515
namespace {
1616
// this is rounding down since >> 1 throws away the least significant bit, but if its a power of two it is spot on
17-
constexpr unsigned BITL_REVERSE(unsigned x) {
17+
constexpr unsigned long BITL_REVERSE(unsigned long x) {
1818
return x == 1 ? 0 : 1 + BITL_REVERSE(x >> 1);
1919
}
2020
}; // namespace
2121

2222
//using C enum (not enum class) on purpose
2323
enum JOYCON {
2424

25-
JOYCON_A = BITL_REVERSE(HidNpadButton_A),
26-
JOYCON_B = BITL_REVERSE(HidNpadButton_B),
27-
JOYCON_X = BITL_REVERSE(HidNpadButton_X),
28-
JOYCON_Y = BITL_REVERSE(HidNpadButton_Y),
29-
30-
JOYCON_PLUS = BITL_REVERSE(HidNpadButton_Plus),
31-
JOYCON_MINUS = BITL_REVERSE(HidNpadButton_Minus),
32-
JOYCON_CROSS_LEFT = BITL_REVERSE(HidNpadButton_Left),
33-
JOYCON_CROSS_UP = BITL_REVERSE(HidNpadButton_Up),
34-
JOYCON_CROSS_RIGHT = BITL_REVERSE(HidNpadButton_Right),
35-
JOYCON_CROSS_DOWN = BITL_REVERSE(HidNpadButton_Down),
36-
JOYCON_LDPAD_LEFT = BITL_REVERSE(HidNpadButton_StickLLeft),
37-
JOYCON_LDPAD_UP = BITL_REVERSE(HidNpadButton_StickLUp),
38-
JOYCON_LDPAD_RIGHT = BITL_REVERSE(HidNpadButton_StickLRight),
39-
JOYCON_LDPAD_DOWN = BITL_REVERSE(HidNpadButton_StickLDown),
40-
JOYCON_RDPAD_LEFT = BITL_REVERSE(HidNpadButton_StickRLeft),
41-
JOYCON_RDPAD_UP = BITL_REVERSE(HidNpadButton_StickRUp),
42-
JOYCON_RDPAD_RIGHT = BITL_REVERSE(HidNpadButton_StickRRight),
43-
JOYCON_RDPAD_DOWN = BITL_REVERSE(HidNpadButton_StickRDown)
25+
// see /opt/devkitpro/libnx/include/switch/services/hid.h
26+
27+
JOYCON_A = BITL_REVERSE(HidNpadButton_A), ///< A button / Right face button
28+
JOYCON_B = BITL_REVERSE(HidNpadButton_B), ///< B button / Down face button
29+
JOYCON_X = BITL_REVERSE(HidNpadButton_X), ///< X button / Up face button
30+
JOYCON_Y = BITL_REVERSE(HidNpadButton_Y), ///< Y button / Left face button
31+
32+
//NOTE: stick buttons are not key events, but can be moved, these need separate handling
33+
JOYCON_STICK_L = BITL(HidNpadButton_StickL), ///< Left Stick button
34+
JOYCON_STICK_R = BITL(HidNpadButton_StickR), ///< Right Stick button
35+
36+
JOYCON_L = BITL_REVERSE(HidNpadButton_L), ///< L button
37+
JOYCON_R = BITL(HidNpadButton_R), ///< R button
38+
JOYCON_ZL = BITL(HidNpadButton_ZL), ///< ZL button
39+
JOYCON_ZR = BITL(HidNpadButton_ZR), ///< ZR button
40+
41+
42+
JOYCON_PLUS = BITL_REVERSE(HidNpadButton_Plus), ///< Plus button
43+
JOYCON_MINUS = BITL_REVERSE(HidNpadButton_Minus), ///< Minus button
44+
45+
// DPAD alias CROSS
46+
JOYCON_DPAD_LEFT = BITL_REVERSE(HidNpadButton_Left), ///< D-Pad Left button
47+
JOYCON_DPAD_UP = BITL_REVERSE(HidNpadButton_Up), ///< D-Pad Up button
48+
JOYCON_DPAD_RIGHT = BITL_REVERSE(HidNpadButton_Right), ///< D-Pad Right button
49+
JOYCON_DPAD_DOWN = BITL_REVERSE(HidNpadButton_Down), ///< D-Pad Down button
50+
51+
JOYCON_LDPAD_LEFT = BITL_REVERSE(HidNpadButton_StickLLeft), ///< Left Stick pseudo-button when moved Left
52+
JOYCON_LDPAD_UP = BITL_REVERSE(HidNpadButton_StickLUp), ///< Left Stick pseudo-button when moved Up
53+
JOYCON_LDPAD_RIGHT = BITL_REVERSE(HidNpadButton_StickLRight), ///< Left Stick pseudo-button when moved Right
54+
JOYCON_LDPAD_DOWN = BITL_REVERSE(HidNpadButton_StickLDown), ///< Left Stick pseudo-button when moved Down
55+
56+
JOYCON_RDPAD_LEFT = BITL_REVERSE(HidNpadButton_StickRLeft), ///< Right Stick pseudo-button when moved Left
57+
JOYCON_RDPAD_UP = BITL_REVERSE(HidNpadButton_StickRUp), ///< Right Stick pseudo-button when moved Up
58+
JOYCON_RDPAD_RIGHT = BITL_REVERSE(HidNpadButton_StickRRight), ///< Right Stick pseudo-button when moved Right
59+
JOYCON_RDPAD_DOWN = BITL_REVERSE(HidNpadButton_StickRDown), ///< Right Stick pseudo-button when moved Left
60+
61+
62+
JOYCON_JOYCONL_SL = BITL_REVERSE(HidNpadButton_LeftSL), ///< SL button on Left Joy-Con
63+
JOYCON_JOYCONL_SR = BITL_REVERSE(HidNpadButton_LeftSR), ///< SR button on Left Joy-Con
64+
65+
JOYCON_JOYCONR_SL = BITL_REVERSE(HidNpadButton_RightSL), ///< SL button on Right Joy-Con
66+
JOYCON_JOYCONR_SL = BITL_REVERSE(HidNpadButton_RightSR), ///< SR button on Right Joy-Con
67+
68+
JOYCON_VERIFICATION = BITL_REVERSE(HidNpadButton_Verification), ///< Verification
4469

4570
};
4671

4772
// some static asserts to check if BITL_REVERSE works as expected
4873
static_assert(BITL(JOYCON_B) == HidNpadButton_B);
4974
static_assert(BITL(JOYCON_PLUS) == HidNpadButton_Plus);
5075

76+
#elif defined(__3DS__)
77+
78+
79+
#include <3ds.h>
80+
81+
// some 3ds buttons, from libctru, but since SDL doesn't handle inputs as flags, like libctru, the have to be reversed and reversing 1 << x = log_2(x), this is done constexpr
82+
83+
84+
namespace {
85+
// this is rounding down since >> 1 throws away the least significant bit, but if its a power of two it is spot on
86+
constexpr unsigned int BIT_REVERSE(unsigned int x) {
87+
return x == 1 ? 0 : 1 + BIT_REVERSE(x >> 1);
88+
}
89+
}; // namespace
90+
91+
//using C enum (not enum class) on purpose
92+
enum JOYCON {
93+
94+
// see /opt/devkitpro/libctru/include/3ds/services/hid.h
95+
96+
JOYCON_A = BIT_REVERSE(KEY_A), ///< A
97+
JOYCON_B = BIT_REVERSE(KEY_B), ///< B
98+
99+
JOYCON_SELECT = BIT_REVERSE(KEY_SELECT), ///< Select
100+
JOYCON_START = BIT_REVERSE(KEY_START), ///< Start
101+
102+
JOYCON_DPAD_RIGHT = BIT_REVERSE(KEY_DRIGHT), ///< D-Pad Right
103+
JOYCON_DPAD_LEFT = BIT_REVERSE(KEY_DLEFT), ///< D-Pad Left
104+
JOYCON_DPAD_UP = BIT_REVERSE(KEY_DUP), ///< D-Pad Up
105+
JOYCON_DPAD_DOWN = BIT_REVERSE(KEY_DDOWN), ///< D-Pad Down
106+
107+
JOYCON_R = BIT_REVERSE(KEY_R), ///< R
108+
JOYCON_L = BIT_REVERSE(KEY_L), ///< L
109+
JOYCON_X = BIT_REVERSE(KEY_X), ///< X
110+
JOYCON_Y = BIT_REVERSE(KEY_Y), ///< Y
111+
112+
JOYCON_ZL = BIT_REVERSE(KEY_ZL), ///< ZL (New 3DS only)
113+
JOYCON_ZR = BIT_REVERSE(KEY_ZR), ///< ZR (New 3DS only)
114+
115+
// has to be handled separately, but MIGHT be used in some form by SDL2
116+
JOYCON_TOUCH = BIT_REVERSE(KEY_TOUCH), ///< Touch (Not actually provided by HID)
117+
118+
JOYCON_CSTICK_RIGHT = BIT_REVERSE(KEY_CSTICK_RIGHT), ///< C-Stick Right (New 3DS only)
119+
JOYCON_CSTICK_LEFT = BIT_REVERSE(KEY_CSTICK_LEFT), ///< C-Stick Left (New 3DS only)
120+
JOYCON_CSTICK_UP = BIT_REVERSE(KEY_CSTICK_UP), ///< C-Stick Up (New 3DS only)
121+
JOYCON_CSTICK_DOWN = BIT_REVERSE(KEY_CSTICK_DOWN), ///< C-Stick Down (New 3DS only)
122+
123+
JOYCON_CPAD_RIGHT = BIT_REVERSE(KEY_CPAD_RIGHT), ///< Circle Pad Right
124+
JOYCON_CPAD_LEFT = BIT_REVERSE(KEY_CPAD_LEFT), ///< Circle Pad Left
125+
JOYCON_CPAD_UP = BIT_REVERSE(KEY_CPAD_UP), ///< Circle Pad Up
126+
JOYCON_CPAD_DOWN = BIT_REVERSE(KEY_CPAD_DOWN), ///< Circle Pad Down
127+
128+
};
129+
130+
// some static asserts to check if BIT_REVERSE works as expected
131+
static_assert(BIT(JOYCON_B) == KEY_B);
132+
static_assert(BIT(JOYCON_SELECT) == KEY_SELECT);
133+
51134

52135
#endif
53136

54-
//TODO: implement for 3ds
55137

56138
#endif

src/platform/console_input.cpp

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,57 @@ helper::optional<InputEvent> JoystickInput::sdl_event_to_input_event(const SDL_E
7474
}
7575
#elif defined(__3DS__)
7676

77-
//TODO: implement for 3ds
77+
7878
helper::optional<InputEvent> JoystickInput::sdl_event_to_input_event(const SDL_Event& event) const {
79-
UNUSED(event);
79+
if (event.type == SDL_JOYBUTTONDOWN) {
80+
const auto button = event.jbutton.button;
81+
if (button == JOYCON_Y) {
82+
return InputEvent::RotateLeftPressed;
83+
}
84+
if (button == JOYCON_X) {
85+
return InputEvent::RotateRightPressed;
86+
}
87+
if (button == JOYCON_DPAD_DOWN or button == JOYCON_CSTICK_DOWN) {
88+
return InputEvent::MoveDownPressed;
89+
}
90+
if (button == JOYCON_DPAD_LEFT or button == JOYCON_CSTICK_LEFT) {
91+
return InputEvent::MoveLeftPressed;
92+
}
93+
if (button == JOYCON_DPAD_RIGHT or button == JOYCON_CSTICK_RIGHT) {
94+
return InputEvent::MoveRightPressed;
95+
}
96+
if (button == JOYCON_A) {
97+
return InputEvent::DropPressed;
98+
}
99+
if (button == JOYCON_B) {
100+
return InputEvent::HoldPressed;
101+
}
102+
} else if (event.type == SDL_JOYBUTTONUP) {
103+
const auto button = event.jbutton.button;
104+
if (button == JOYCON_Y) {
105+
return InputEvent::RotateLeftReleased;
106+
}
107+
if (button == JOYCON_X) {
108+
return InputEvent::RotateRightReleased;
109+
}
110+
if (button == JOYCON_DPAD_DOWN or button == JOYCON_CSTICK_DOWN) {
111+
return InputEvent::MoveDownReleased;
112+
}
113+
if (button == JOYCON_DPAD_LEFT or button == JOYCON_CSTICK_LEFT) {
114+
return InputEvent::MoveLeftReleased;
115+
}
116+
if (button == JOYCON_DPAD_RIGHT or button == JOYCON_CSTICK_RIGHT) {
117+
return InputEvent::MoveRightReleased;
118+
}
119+
if (button == JOYCON_A) {
120+
return InputEvent::DropReleased;
121+
}
122+
if (button == JOYCON_B) {
123+
return InputEvent::HoldReleased;
124+
}
125+
}
80126
return helper::nullopt;
81127
}
82-
83128
#endif
84129

85130

0 commit comments

Comments
 (0)