Skip to content

Commit

Permalink
Merge pull request #35 from berichan/master
Browse files Browse the repository at this point in the history
Screen on/off, single-command sequential button presses, sys info requests, token cancels + 12.0.0 support
  • Loading branch information
berichan authored Apr 18, 2021
2 parents bf8f471 + 3cd95d0 commit a962bb0
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 52 deletions.
3 changes: 2 additions & 1 deletion sys-botbase/source/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"files.associations": {
"switch.h": "c"
"switch.h": "c",
"cstring": "c"
}
}
96 changes: 90 additions & 6 deletions sys-botbase/source/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,25 +189,29 @@ void readMem(u8* out, u64 offset, u64 size)
printf("svcReadDebugProcessMemory: %d\n", rc);
}

void click(HidControllerKeys btn)
void click(HidNpadButton btn)
{
initController();
press(btn);
svcSleepThread(buttonClickSleepTime * 1e+6L);
release(btn);
}
void press(HidControllerKeys btn)
void press(HidNpadButton btn)
{
initController();
controllerState.buttons |= btn;
hiddbgSetHdlsState(controllerHandle, &controllerState);
Result rc = hiddbgSetHdlsState(controllerHandle, &controllerState);
if (R_FAILED(rc) && debugResultCodes)
printf("hiddbgSetHdlsState: %d\n", rc);
}

void release(HidControllerKeys btn)
void release(HidNpadButton btn)
{
initController();
controllerState.buttons &= ~btn;
hiddbgSetHdlsState(controllerHandle, &controllerState);
Result rc = hiddbgSetHdlsState(controllerHandle, &controllerState);
if (R_FAILED(rc) && debugResultCodes)
printf("hiddbgSetHdlsState: %d\n", rc);
}

void setStickState(int side, int dxVal, int dyVal)
Expand Down Expand Up @@ -261,7 +265,7 @@ u64 followMainPointer(s64* jumps, size_t count)
return offset;
}

void touch(HidTouchState* state, u64 sequentialCount, u64 holdTime, bool hold)
void touch(HidTouchState* state, u64 sequentialCount, u64 holdTime, bool hold, u8* token)
{
initController();
state->delta_time = holdTime; // only the first touch needs this for whatever reason
Expand All @@ -274,6 +278,9 @@ void touch(HidTouchState* state, u64 sequentialCount, u64 holdTime, bool hold)
hiddbgSetTouchScreenAutoPilotState(NULL, 0);
svcSleepThread(pollRate * 1e+6L);
}

if ((*token) == 1)
break;
}

if(hold) // send finger release event
Expand Down Expand Up @@ -314,3 +321,80 @@ void key(HiddbgKeyboardAutoPilotState* states, u64 sequentialCount)

hiddbgUnsetKeyboardAutoPilotState();
}

void clickSequence(char* seq, u8* token)
{
const char delim = ','; // used for chars and sticks
const char startWait = 'W';
const char startPress = '+';
const char startRelease = '-';
const char startLStick = '%';
const char startRStick = '&';
char* command = strtok(seq, &delim);
HidNpadButton currKey = {0};
u64 currentWait = 0;

initController();
while (command != NULL)
{
if ((*token) == 1)
break;

if (!strncmp(command, &startLStick, 1))
{
// l stick
s64 x = parseStringToSignedLong(&command[1]);
if(x > JOYSTICK_MAX) x = JOYSTICK_MAX;
if(x < JOYSTICK_MIN) x = JOYSTICK_MIN;
s64 y = 0;
command = strtok(NULL, &delim);
if (command != NULL)
y = parseStringToSignedLong(command);
if(y > JOYSTICK_MAX) y = JOYSTICK_MAX;
if(y < JOYSTICK_MIN) y = JOYSTICK_MIN;
setStickState(JOYSTICK_LEFT, (s32)x, (s32)y);
}
else if (!strncmp(command, &startRStick, 1))
{
// r stick
s64 x = parseStringToSignedLong(&command[1]);
if(x > JOYSTICK_MAX) x = JOYSTICK_MAX;
if(x < JOYSTICK_MIN) x = JOYSTICK_MIN;
s64 y = 0;
command = strtok(NULL, &delim);
if (command != NULL)
y = parseStringToSignedLong(command);
if(y > JOYSTICK_MAX) y = JOYSTICK_MAX;
if(y < JOYSTICK_MIN) y = JOYSTICK_MIN;
setStickState(JOYSTICK_RIGHT, (s32)x, (s32)y);
}
else if (!strncmp(command, &startPress, 1))
{
// press
currKey = parseStringToButton(&command[1]);
press(currKey);
}
else if (!strncmp(command, &startRelease, 1))
{
// release
currKey = parseStringToButton(&command[1]);
press(currKey);
}
else if (!strncmp(command, &startWait, 1))
{
// wait
currentWait = parseStringToInt(&command[1]);
svcSleepThread(currentWait * 1e+6l);
}
else
{
// click
currKey = parseStringToButton(command);
press(currKey);
svcSleepThread(buttonClickSleepTime * 1e+6L);
release(currKey);
}

command = strtok(NULL, &delim);
}
}
14 changes: 9 additions & 5 deletions sys-botbase/source/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ typedef struct {
u8 state;
} KeyData;

#define JOYSTICK_LEFT 0
#define JOYSTICK_RIGHT 1

void attach();
void detach();
u64 getMainNsoBase(u64 pid);
Expand All @@ -44,11 +47,12 @@ void poke(u64 offset, u64 size, u8* val);
void writeMem(u64 offset, u64 size, u8* val);
void peek(u64 offset, u64 size);
void readMem(u8* out, u64 offset, u64 size);
void click(HidControllerKeys btn);
void press(HidControllerKeys btn);
void release(HidControllerKeys btn);
void click(HidNpadButton btn);
void press(HidNpadButton btn);
void release(HidNpadButton btn);
void setStickState(int side, int dxVal, int dyVal);
void reverseArray(u8* arr, int start, int end);
u64 followMainPointer(s64* jumps, size_t count);
void touch(HidTouchState* state, u64 sequentialCount, u64 holdTime, bool hold);
void key(HiddbgKeyboardAutoPilotState* states, u64 sequentialCount);
void touch(HidTouchState* state, u64 sequentialCount, u64 holdTime, bool hold, u8* token);
void key(HiddbgKeyboardAutoPilotState* states, u64 sequentialCount);
void clickSequence(char* seq, u8* token);
Loading

0 comments on commit a962bb0

Please sign in to comment.