Skip to content

Commit

Permalink
Update for libnx hid-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fincs committed Dec 2, 2020
1 parent fcbc56a commit 20c1f00
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 45 deletions.
51 changes: 28 additions & 23 deletions nx_main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
uint8_t* g_framebuf;
u32 g_framebuf_width;

PadState g_pad;
PadRepeater g_pad_repeater;

bool menuUpdateErrorScreen(void);

#ifdef PERF_LOG
Expand All @@ -38,6 +41,12 @@ int main(int argc, char **argv)
u64 start_tick=0;
#endif

padConfigureInput(8, HidNpadStyleSet_NpadStandard);
padInitializeAny(&g_pad);
padRepeaterInitialize(&g_pad_repeater, 20, 10);
hidSetNpadHandheldActivationMode(HidNpadHandheldActivationMode_Single);
touchInit();

memset(errormsg, 0, sizeof(errormsg));

appletLockExit();
Expand Down Expand Up @@ -129,10 +138,11 @@ int main(int argc, char **argv)

while (appletMainLoop())
{


//Scan all the inputs. This should be done once for each frame
hidScanInput();
// Scan the gamepad. This should be done once for each frame
padUpdate(&g_pad);
padRepeaterUpdate(&g_pad_repeater, padGetButtons(&g_pad) & (
HidNpadButton_AnyLeft | HidNpadButton_AnyUp | HidNpadButton_AnyRight | HidNpadButton_AnyDown
));

if (!error_screen) {
if (!uiUpdate()) break;
Expand Down Expand Up @@ -186,14 +196,9 @@ int main(int argc, char **argv)
}

u64 menuGetKeysDown(void) {
u64 down = 0;

for (u32 controller=0; controller<8; controller++) {
if (hidIsControllerConnected(controller)) down |= hidKeysDown(controller);
}
if (hidIsControllerConnected(CONTROLLER_HANDHELD)) down |= hidKeysDown(CONTROLLER_HANDHELD);

return down;
u64 keys = padGetButtonsDown(&g_pad);
keys |= padRepeaterGetButtons(&g_pad_repeater);
return keys;
}

//This is implemented here due to the hid code.
Expand All @@ -206,37 +211,37 @@ bool menuUpdate(void) {

handleTouch(menu);

if (down & KEY_Y)
if (down & HidNpadButton_Y)
{
launchMenuNetloaderTask();
}
else if (down & KEY_X)
else if (down & HidNpadButton_X)
{
menuHandleXButton();
}
else if (down & KEY_A)
else if (down & HidNpadButton_A)
{
menuHandleAButton();
}
else if (down & KEY_B)
else if (down & HidNpadButton_B)
{
launchMenuBackTask();
}
else if(down & KEY_MINUS){
else if(down & HidNpadButton_Minus){
themeMenuStartup();
}
else if (down & KEY_PLUS)
else if (down & HidNpadButton_Plus)
{
exitflag = 1;
}
else if (menu->nEntries > 0)
{
int move = 0;

if (down & KEY_LEFT) move--;
if (down & KEY_RIGHT) move++;
if (down & KEY_DOWN) move-=entries_count;
if (down & KEY_UP) move+=entries_count;
if (down & HidNpadButton_AnyLeft) move--;
if (down & HidNpadButton_AnyRight) move++;
if (down & HidNpadButton_AnyDown) move-=entries_count;
if (down & HidNpadButton_AnyUp) move+=entries_count;

int newEntry = menu->curEntry + move;
if (newEntry < 0) newEntry = 0;
Expand All @@ -251,7 +256,7 @@ bool menuUpdateErrorScreen(void) {
bool exitflag = 0;
u64 down = menuGetKeysDown();

if (down & KEY_PLUS)
if (down & HidNpadButton_Plus)
{
exitflag = 1;
}
Expand Down
37 changes: 17 additions & 20 deletions nx_main/nx_touch.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@

struct touchInfo_s touchInfo;

void touchInit() {
void touchInit(void) {
touchInfo.gestureInProgress = false;
touchInfo.isTap = true;
touchInfo.initMenuXPos = 0;
touchInfo.initMenuIndex = 0;
touchInfo.lastSlideSpeed = 0;
hidInitializeTouchScreen();
}

void handleTappingOnApp(menu_s* menu, int px) {
Expand Down Expand Up @@ -56,38 +57,34 @@ static inline bool checkInsideTextLayoutObject(ThemeLayoutId id, int x, int y) {

void handleTouch(menu_s* menu) {
ThemeLayoutObject *layoutobj = NULL;
touchPosition currentTouch;
u32 touches = hidTouchCount();
HidTouchScreenState touch = {0};
hidGetTouchScreenStates(&touch, 1);

layoutobj = &themeCurrent.layoutObjects[ThemeLayoutId_MenuListTiles];
int entries_count = layoutobj->posEnd[0];
layoutobj = &themeCurrent.layoutObjects[ThemeLayoutId_MenuList];

// On touch start.
if (touches == 1 && !touchInfo.gestureInProgress) {
hidTouchRead(&currentTouch, 0);

if (touch.count == 1 && !touchInfo.gestureInProgress) {
touchInfo.gestureInProgress = true;
touchInfo.firstTouch = currentTouch;
touchInfo.prevTouch = currentTouch;
touchInfo.firstTouch = touch.touches[0];
touchInfo.prevTouch = touch.touches[0];
touchInfo.isTap = true;
touchInfo.initMenuXPos = menu->xPos;
touchInfo.initMenuIndex = menu->curEntry;
touchInfo.lastSlideSpeed = 0;
menu->slideSpeed = 0;
}
// On touch moving.
else if (touches >= 1 && touchInfo.gestureInProgress) {
hidTouchRead(&currentTouch, 0);

touchInfo.lastSlideSpeed = ((int)(currentTouch.px - touchInfo.prevTouch.px));
else if (touch.count >= 1 && touchInfo.gestureInProgress) {
touchInfo.lastSlideSpeed = ((int)(touch.touches[0].x - touchInfo.prevTouch.x));

touchInfo.prevTouch = currentTouch;
touchInfo.prevTouch = touch.touches[0];

if (touchInfo.isTap && (abs(touchInfo.firstTouch.px - currentTouch.px) > TAP_MOVEMENT_GAP || abs(touchInfo.firstTouch.py - currentTouch.py) > TAP_MOVEMENT_GAP)) {
if (touchInfo.isTap && (abs(touchInfo.firstTouch.x - touch.touches[0].x) > TAP_MOVEMENT_GAP || abs(touchInfo.firstTouch.y - touch.touches[0].y) > TAP_MOVEMENT_GAP)) {
touchInfo.isTap = false;
}
if (!menuIsMsgBoxOpen() && touchInfo.firstTouch.py > layoutobj->posStart[1] && touchInfo.firstTouch.py < layoutobj->posStart[1]+layoutobj->size[1] && !touchInfo.isTap && menu->nEntries > entries_count) {
if (!menuIsMsgBoxOpen() && touchInfo.firstTouch.y > layoutobj->posStart[1] && touchInfo.firstTouch.y < layoutobj->posStart[1]+layoutobj->size[1] && !touchInfo.isTap && menu->nEntries > entries_count) {

if (!touchInfo.isTap) {
menu->slideSpeed = touchInfo.lastSlideSpeed;
Expand All @@ -96,10 +93,10 @@ void handleTouch(menu_s* menu) {
}
// On touch end.
else if (touchInfo.gestureInProgress) {
int x1 = touchInfo.firstTouch.px;
int y1 = touchInfo.firstTouch.py;
int x2 = touchInfo.prevTouch.px;
int y2 = touchInfo.prevTouch.py;
int x1 = touchInfo.firstTouch.x;
int y1 = touchInfo.firstTouch.y;
int x2 = touchInfo.prevTouch.x;
int y2 = touchInfo.prevTouch.y;

if (!touchInfo.isTap) {
menu->slideSpeed = touchInfo.lastSlideSpeed;
Expand All @@ -122,7 +119,7 @@ void handleTouch(menu_s* menu) {
} else if (touchInfo.isTap && !netloader_active) {
// App Icons
if (y1 > layoutobj->posStart[1] && y1 < layoutobj->posStart[1]+layoutobj->size[1]) {
handleTappingOnApp(menu, touchInfo.prevTouch.px);
handleTappingOnApp(menu, touchInfo.prevTouch.x);
}
// Bottom Buttons
else {
Expand Down
5 changes: 3 additions & 2 deletions nx_main/nx_touch.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

struct touchInfo_s {
bool gestureInProgress;
touchPosition firstTouch;
touchPosition prevTouch;
HidTouchState firstTouch;
HidTouchState prevTouch;
bool isTap;
int initMenuXPos;
int initMenuIndex;
int lastSlideSpeed;
};

void touchInit(void);
void handleTouch(menu_s* menu);

0 comments on commit 20c1f00

Please sign in to comment.