Merge remote-tracking branch 'origin/hid_refactor' into 11_support

This commit is contained in:
Michael Scire 2020-12-02 02:24:18 -08:00
commit e4677d3a9d
4 changed files with 117 additions and 85 deletions

View File

@ -34,7 +34,7 @@ namespace ams::cfg {
constexpr ProgramOverrideKey DefaultAppletPhotoViewerOverrideKey = { constexpr ProgramOverrideKey DefaultAppletPhotoViewerOverrideKey = {
.override_key = { .override_key = {
.key_combination = KEY_R, .key_combination = HidNpadButton_R,
.override_by_default = true, .override_by_default = true,
}, },
.program_id = ncm::SystemAppletId::PhotoViewer, .program_id = ncm::SystemAppletId::PhotoViewer,
@ -58,12 +58,12 @@ namespace ams::cfg {
/* Override globals. */ /* Override globals. */
OverrideKey g_default_override_key = { OverrideKey g_default_override_key = {
.key_combination = KEY_L, .key_combination = HidNpadButton_L,
.override_by_default = true, .override_by_default = true,
}; };
OverrideKey g_default_cheat_enable_key = { OverrideKey g_default_cheat_enable_key = {
.key_combination = KEY_L, .key_combination = HidNpadButton_L,
.override_by_default = true, .override_by_default = true,
}; };
@ -89,7 +89,7 @@ namespace ams::cfg {
impl::OverrideStatusFlag_AddressSpace64Bit, impl::OverrideStatusFlag_AddressSpace64Bit,
}, },
.override_any_app_key = { .override_any_app_key = {
.key_combination = KEY_R, .key_combination = HidNpadButton_R,
.override_by_default = false, .override_by_default = false,
}, },
.override_any_app_as_flag = impl::OverrideStatusFlag_AddressSpace64Bit, .override_any_app_as_flag = impl::OverrideStatusFlag_AddressSpace64Bit,
@ -110,41 +110,41 @@ namespace ams::cfg {
/* Parse key combination. */ /* Parse key combination. */
if (strcasecmp(value, "A") == 0) { if (strcasecmp(value, "A") == 0) {
cfg.key_combination = KEY_A; cfg.key_combination = HidNpadButton_A;
} else if (strcasecmp(value, "B") == 0) { } else if (strcasecmp(value, "B") == 0) {
cfg.key_combination = KEY_B; cfg.key_combination = HidNpadButton_B;
} else if (strcasecmp(value, "X") == 0) { } else if (strcasecmp(value, "X") == 0) {
cfg.key_combination = KEY_X; cfg.key_combination = HidNpadButton_X;
} else if (strcasecmp(value, "Y") == 0) { } else if (strcasecmp(value, "Y") == 0) {
cfg.key_combination = KEY_Y; cfg.key_combination = HidNpadButton_Y;
} else if (strcasecmp(value, "LS") == 0) { } else if (strcasecmp(value, "LS") == 0) {
cfg.key_combination = KEY_LSTICK; cfg.key_combination = HidNpadButton_StickL;
} else if (strcasecmp(value, "RS") == 0) { } else if (strcasecmp(value, "RS") == 0) {
cfg.key_combination = KEY_RSTICK; cfg.key_combination = HidNpadButton_StickR;
} else if (strcasecmp(value, "L") == 0) { } else if (strcasecmp(value, "L") == 0) {
cfg.key_combination = KEY_L; cfg.key_combination = HidNpadButton_L;
} else if (strcasecmp(value, "R") == 0) { } else if (strcasecmp(value, "R") == 0) {
cfg.key_combination = KEY_R; cfg.key_combination = HidNpadButton_R;
} else if (strcasecmp(value, "ZL") == 0) { } else if (strcasecmp(value, "ZL") == 0) {
cfg.key_combination = KEY_ZL; cfg.key_combination = HidNpadButton_ZL;
} else if (strcasecmp(value, "ZR") == 0) { } else if (strcasecmp(value, "ZR") == 0) {
cfg.key_combination = KEY_ZR; cfg.key_combination = HidNpadButton_ZR;
} else if (strcasecmp(value, "PLUS") == 0) { } else if (strcasecmp(value, "PLUS") == 0) {
cfg.key_combination = KEY_PLUS; cfg.key_combination = HidNpadButton_Plus;
} else if (strcasecmp(value, "MINUS") == 0) { } else if (strcasecmp(value, "MINUS") == 0) {
cfg.key_combination = KEY_MINUS; cfg.key_combination = HidNpadButton_Minus;
} else if (strcasecmp(value, "DLEFT") == 0) { } else if (strcasecmp(value, "DLEFT") == 0) {
cfg.key_combination = KEY_DLEFT; cfg.key_combination = HidNpadButton_Left;
} else if (strcasecmp(value, "DUP") == 0) { } else if (strcasecmp(value, "DUP") == 0) {
cfg.key_combination = KEY_DUP; cfg.key_combination = HidNpadButton_Up;
} else if (strcasecmp(value, "DRIGHT") == 0) { } else if (strcasecmp(value, "DRIGHT") == 0) {
cfg.key_combination = KEY_DRIGHT; cfg.key_combination = HidNpadButton_Right;
} else if (strcasecmp(value, "DDOWN") == 0) { } else if (strcasecmp(value, "DDOWN") == 0) {
cfg.key_combination = KEY_DDOWN; cfg.key_combination = HidNpadButton_Down;
} else if (strcasecmp(value, "SL") == 0) { } else if (strcasecmp(value, "SL") == 0) {
cfg.key_combination = KEY_SL; cfg.key_combination = HidNpadButton_AnySL;
} else if (strcasecmp(value, "SR") == 0) { } else if (strcasecmp(value, "SR") == 0) {
cfg.key_combination = KEY_SR; cfg.key_combination = HidNpadButton_AnySR;
} }
return cfg; return cfg;

View File

@ -23,12 +23,30 @@ namespace ams::hid {
os::Mutex g_hid_lock(false); os::Mutex g_hid_lock(false);
bool g_initialized_hid = false; bool g_initialized_hid = false;
/* Set of supported NpadIds (we want to read from any connected controllers). */
constexpr const HidNpadIdType NpadIdTypes[] = {
HidNpadIdType_No1,
HidNpadIdType_No2,
HidNpadIdType_No3,
HidNpadIdType_No4,
HidNpadIdType_No5,
HidNpadIdType_No6,
HidNpadIdType_No7,
HidNpadIdType_No8,
HidNpadIdType_Handheld,
};
constexpr const size_t NumNpadIdTypes = util::size(NpadIdTypes);
/* Helper. */ /* Helper. */
void InitializeHid() { void InitializeHid() {
R_ABORT_UNLESS(smInitialize()); R_ABORT_UNLESS(smInitialize());
ON_SCOPE_EXIT { smExit(); }; ON_SCOPE_EXIT { smExit(); };
{ {
R_ABORT_UNLESS(hidInitialize()); R_ABORT_UNLESS(hidInitialize());
hidInitializeNpad();
R_ABORT_UNLESS(hidSetSupportedNpadIdType(NpadIdTypes, NumNpadIdTypes));
R_ABORT_UNLESS(hidSetSupportedNpadStyleSet(HidNpadStyleSet_NpadStandard | HidNpadStyleTag_NpadSystemExt));
} }
} }
@ -46,6 +64,17 @@ namespace ams::hid {
return ResultSuccess(); return ResultSuccess();
} }
u64 ReadHidNpad(HidNpadIdType id) {
HidNpadSystemExtState state;
size_t count = hidGetNpadStatesSystemExt(id, std::addressof(state), 1);
if (count != 0 && (state.attributes & HidNpadAttribute_IsConnected)) {
return state.buttons;
}
return 0;
}
} }
Result GetKeysHeld(u64 *out) { Result GetKeysHeld(u64 *out) {
@ -53,11 +82,10 @@ namespace ams::hid {
R_TRY(EnsureHidInitialized()); R_TRY(EnsureHidInitialized());
hidScanInput(); *out = ReadHidNpad(HidNpadIdType_Handheld);
*out = 0;
for (size_t controller = 0; controller < 10; controller++) { for (size_t controller = 0; controller < 8; controller++) {
*out |= hidKeysHeld(static_cast<HidControllerID>(controller)); *out |= ReadHidNpad(static_cast<HidNpadIdType>(controller));
} }
return ResultSuccess(); return ResultSuccess();

View File

@ -43,7 +43,6 @@ namespace dbk {
static constexpr float VerticalGap = 10.0f; static constexpr float VerticalGap = 10.0f;
u32 g_screen_width; u32 g_screen_width;
u32 g_screen_height; u32 g_screen_height;
@ -51,8 +50,10 @@ namespace dbk {
bool g_initialized = false; bool g_initialized = false;
bool g_exit_requested = false; bool g_exit_requested = false;
PadState g_pad;
u32 g_prev_touch_count = -1; u32 g_prev_touch_count = -1;
touchPosition g_start_touch_position; HidTouchScreenState g_start_touch;
bool g_started_touching = false; bool g_started_touching = false;
bool g_tapping = false; bool g_tapping = false;
bool g_touches_moving = false; bool g_touches_moving = false;
@ -67,15 +68,14 @@ namespace dbk {
constexpr u32 MaxTapMovement = 20; constexpr u32 MaxTapMovement = 20;
void UpdateInput() { void UpdateInput() {
/* Update the previous touch count. */
g_prev_touch_count = hidTouchCount();
/* Scan for input and update touch state. */ /* Scan for input and update touch state. */
hidScanInput(); padUpdate(&g_pad);
const u32 touch_count = hidTouchCount(); HidTouchScreenState current_touch;
hidGetTouchScreenStates(&current_touch, 1);
const u32 touch_count = current_touch.count;
if (g_prev_touch_count == 0 && touch_count > 0) { if (g_prev_touch_count == 0 && touch_count > 0) {
hidTouchRead(&g_start_touch_position, 0); hidGetTouchScreenStates(&g_start_touch, 1);
g_started_touching = true; g_started_touching = true;
g_tapping = true; g_tapping = true;
} else { } else {
@ -91,10 +91,7 @@ namespace dbk {
/* Check if currently moving. */ /* Check if currently moving. */
if (g_prev_touch_count > 0 && touch_count > 0) { if (g_prev_touch_count > 0 && touch_count > 0) {
touchPosition current_touch_position; if ((abs(current_touch.touches[0].x - g_start_touch.touches[0].x) > MaxTapMovement || abs(current_touch.touches[0].y - g_start_touch.touches[0].y) > MaxTapMovement)) {
hidTouchRead(&current_touch_position, 0);
if ((abs(current_touch_position.px - g_start_touch_position.px) > MaxTapMovement || abs(current_touch_position.py - g_start_touch_position.py) > MaxTapMovement)) {
g_touches_moving = true; g_touches_moving = true;
g_tapping = false; g_tapping = false;
} else { } else {
@ -103,6 +100,9 @@ namespace dbk {
} else { } else {
g_touches_moving = false; g_touches_moving = false;
} }
/* Update the previous touch count. */
g_prev_touch_count = current_touch.count;
} }
void ChangeMenu(std::shared_ptr<Menu> menu) { void ChangeMenu(std::shared_ptr<Menu> menu) {
@ -241,14 +241,13 @@ namespace dbk {
} }
Button *Menu::GetTouchedButton() { Button *Menu::GetTouchedButton() {
touchPosition touch; HidTouchScreenState current_touch;
const u32 touch_count = hidTouchCount(); hidGetTouchScreenStates(&current_touch, 1);
const u32 touch_count = current_touch.count;
for (u32 i = 0; i < touch_count && g_started_touching; i++) { for (u32 i = 0; i < touch_count && g_started_touching; i++) {
hidTouchRead(&touch, i);
for (auto &button : m_buttons) { for (auto &button : m_buttons) {
if (button && button->enabled && button->IsPositionInBounds(touch.px, touch.py)) { if (button && button->enabled && button->IsPositionInBounds(current_touch.touches[i].x, current_touch.touches[i].y)) {
return &(*button); return &(*button);
} }
} }
@ -264,9 +263,9 @@ namespace dbk {
return nullptr; return nullptr;
} }
const u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO); const u64 k_down = padGetButtonsDown(&g_pad);
if (k_down & KEY_A || this->GetTouchedButton() == selected_button) { if (k_down & HidNpadButton_A || this->GetTouchedButton() == selected_button) {
return selected_button; return selected_button;
} }
@ -274,16 +273,16 @@ namespace dbk {
} }
void Menu::UpdateButtons() { void Menu::UpdateButtons() {
const u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO); const u64 k_down = padGetButtonsDown(&g_pad);
Direction direction = Direction::Invalid; Direction direction = Direction::Invalid;
if (k_down & KEY_DOWN) { if (k_down & HidNpadButton_AnyDown) {
direction = Direction::Down; direction = Direction::Down;
} else if (k_down & KEY_UP) { } else if (k_down & HidNpadButton_AnyUp) {
direction = Direction::Up; direction = Direction::Up;
} else if (k_down & KEY_LEFT) { } else if (k_down & HidNpadButton_AnyLeft) {
direction = Direction::Left; direction = Direction::Left;
} else if (k_down & KEY_RIGHT) { } else if (k_down & HidNpadButton_AnyRight) {
direction = Direction::Right; direction = Direction::Right;
} }
@ -373,10 +372,10 @@ namespace dbk {
} }
void ErrorMenu::Update(u64 ns) { void ErrorMenu::Update(u64 ns) {
u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO); u64 k_down = padGetButtonsDown(&g_pad);
/* Go back if B is pressed. */ /* Go back if B is pressed. */
if (k_down & KEY_B) { if (k_down & HidNpadButton_B) {
g_exit_requested = true; g_exit_requested = true;
return; return;
} }
@ -411,10 +410,10 @@ namespace dbk {
} }
void WarningMenu::Update(u64 ns) { void WarningMenu::Update(u64 ns) {
u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO); u64 k_down = padGetButtonsDown(&g_pad);
/* Go back if B is pressed. */ /* Go back if B is pressed. */
if (k_down & KEY_B) { if (k_down & HidNpadButton_B) {
ReturnToPreviousMenu(); ReturnToPreviousMenu();
return; return;
} }
@ -449,9 +448,9 @@ namespace dbk {
} }
void MainMenu::Update(u64 ns) { void MainMenu::Update(u64 ns) {
u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO); u64 k_down = padGetButtonsDown(&g_pad);
if (k_down & KEY_B) { if (k_down & HidNpadButton_B) {
g_exit_requested = true; g_exit_requested = true;
} }
@ -574,16 +573,16 @@ namespace dbk {
const float x = g_screen_width / 2.0f - WindowWidth / 2.0f; const float x = g_screen_width / 2.0f - WindowWidth / 2.0f;
const float y = g_screen_height / 2.0f - WindowHeight / 2.0f; const float y = g_screen_height / 2.0f - WindowHeight / 2.0f;
touchPosition current_pos; HidTouchScreenState current_touch;
hidTouchRead(&current_pos, 0); hidGetTouchScreenStates(&current_touch, 1);
/* Check if the tap is within the x bounds. */ /* Check if the tap is within the x bounds. */
if (current_pos.px >= x + TextBackgroundOffset + FileRowHorizontalInset && current_pos.px <= WindowWidth - (TextBackgroundOffset + FileRowHorizontalInset) * 2.0f) { if (current_touch.touches[0].x >= x + TextBackgroundOffset + FileRowHorizontalInset && current_touch.touches[0].x <= WindowWidth - (TextBackgroundOffset + FileRowHorizontalInset) * 2.0f) {
const float y_min = y + TitleGap + FileRowGap + i * (FileRowHeight + FileRowGap) - m_scroll_offset; const float y_min = y + TitleGap + FileRowGap + i * (FileRowHeight + FileRowGap) - m_scroll_offset;
const float y_max = y_min + FileRowHeight; const float y_max = y_min + FileRowHeight;
/* Check if the tap is within the y bounds. */ /* Check if the tap is within the y bounds. */
if (current_pos.py >= y_min && current_pos.py <= y_max) { if (current_touch.touches[0].y >= y_min && current_touch.touches[0].y <= y_max) {
return true; return true;
} }
} }
@ -604,10 +603,10 @@ namespace dbk {
/* Scroll based on touch movement. */ /* Scroll based on touch movement. */
if (g_touches_moving) { if (g_touches_moving) {
touchPosition current_pos; HidTouchScreenState current_touch;
hidTouchRead(&current_pos, 0); hidGetTouchScreenStates(&current_touch, 1);
const int dist_y = current_pos.py - g_start_touch_position.py; const int dist_y = current_touch.touches[0].y - g_start_touch.touches[0].y;
float new_scroll_offset = m_touch_start_scroll_offset - static_cast<float>(dist_y); float new_scroll_offset = m_touch_start_scroll_offset - static_cast<float>(dist_y);
float max_scroll = (FileRowHeight + FileRowGap) * static_cast<float>(m_file_entries.size()) - FileListHeight; float max_scroll = (FileRowHeight + FileRowGap) * static_cast<float>(m_file_entries.size()) - FileListHeight;
@ -688,16 +687,16 @@ namespace dbk {
} }
void FileMenu::Update(u64 ns) { void FileMenu::Update(u64 ns) {
u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO); u64 k_down = padGetButtonsDown(&g_pad);
/* Go back if B is pressed. */ /* Go back if B is pressed. */
if (k_down & KEY_B) { if (k_down & HidNpadButton_B) {
ReturnToPreviousMenu(); ReturnToPreviousMenu();
return; return;
} }
/* Finalize selection on pressing A. */ /* Finalize selection on pressing A. */
if (k_down & KEY_A) { if (k_down & HidNpadButton_A) {
this->FinalizeSelection(); this->FinalizeSelection();
} }
@ -706,14 +705,14 @@ namespace dbk {
const u32 prev_index = m_current_index; const u32 prev_index = m_current_index;
if (k_down & KEY_DOWN) { if (k_down & HidNpadButton_AnyDown) {
/* Scroll down. */ /* Scroll down. */
if (m_current_index >= (m_file_entries.size() - 1)) { if (m_current_index >= (m_file_entries.size() - 1)) {
m_current_index = 0; m_current_index = 0;
} else { } else {
m_current_index++; m_current_index++;
} }
} else if (k_down & KEY_UP) { } else if (k_down & HidNpadButton_AnyUp) {
/* Scroll up. */ /* Scroll up. */
if (m_current_index == 0) { if (m_current_index == 0) {
m_current_index = m_file_entries.size() - 1; m_current_index = m_file_entries.size() - 1;
@ -859,10 +858,10 @@ namespace dbk {
this->ValidateUpdate(); this->ValidateUpdate();
} }
u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO); u64 k_down = padGetButtonsDown(&g_pad);
/* Go back if B is pressed. */ /* Go back if B is pressed. */
if (k_down & KEY_B) { if (k_down & HidNpadButton_B) {
ReturnToPreviousMenu(); ReturnToPreviousMenu();
return; return;
} }
@ -923,10 +922,10 @@ namespace dbk {
} }
void ChooseResetMenu::Update(u64 ns) { void ChooseResetMenu::Update(u64 ns) {
u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO); u64 k_down = padGetButtonsDown(&g_pad);
/* Go back if B is pressed. */ /* Go back if B is pressed. */
if (k_down & KEY_B) { if (k_down & HidNpadButton_B) {
ReturnToPreviousMenu(); ReturnToPreviousMenu();
return; return;
} }
@ -986,10 +985,10 @@ namespace dbk {
} }
void ChooseExfatMenu::Update(u64 ns) { void ChooseExfatMenu::Update(u64 ns) {
u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO); u64 k_down = padGetButtonsDown(&g_pad);
/* Go back if B is pressed. */ /* Go back if B is pressed. */
if (k_down & KEY_B) { if (k_down & HidNpadButton_B) {
ReturnToPreviousMenu(); ReturnToPreviousMenu();
return; return;
} }
@ -1195,6 +1194,13 @@ namespace dbk {
void InitializeMenu(u32 screen_width, u32 screen_height) { void InitializeMenu(u32 screen_width, u32 screen_height) {
Result rc = 0; Result rc = 0;
/* Configure and initialize the gamepad. */
padConfigureInput(1, HidNpadStyleSet_NpadStandard);
padInitializeDefault(&g_pad);
/* Initialize the touch screen. */
hidInitializeTouchScreen();
/* Set the screen width and height. */ /* Set the screen width and height. */
g_screen_width = screen_width; g_screen_width = screen_width;
g_screen_height = screen_height; g_screen_height = screen_height;

View File

@ -54,6 +54,11 @@ int main(int argc, char **argv)
{ {
consoleInit(NULL); consoleInit(NULL);
padConfigureInput(8, HidNpadStyleSet_NpadStandard);
PadState pad;
padInitializeAny(&pad);
bool can_reboot = true; bool can_reboot = true;
Result rc = splInitialize(); Result rc = splInitialize();
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
@ -76,20 +81,13 @@ int main(int argc, char **argv)
// Main loop // Main loop
while(appletMainLoop()) while(appletMainLoop())
{ {
//Scan all the inputs. This should be done once for each frame padUpdate(&pad);
hidScanInput(); u64 kDown = padGetButtonsDown(&pad);
u64 kDown = 0; if (can_reboot && (kDown & HidNpadButton_Minus)) {
for (int controller = 0; controller < 10; controller++) {
// hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)
kDown |= hidKeysDown((HidControllerID) controller);
}
if (can_reboot && kDown & KEY_MINUS) {
reboot_to_payload(); reboot_to_payload();
} }
if (kDown & KEY_L) { break; } // break in order to return to hbmenu if (kDown & HidNpadButton_L) { break; } // break in order to return to hbmenu
consoleUpdate(NULL); consoleUpdate(NULL);
} }