Skip to content

Commit

Permalink
Hotkey settings(not working yet), smarter menu selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Gruetzig committed Aug 8, 2023
1 parent 442b070 commit f056d43
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 73 deletions.
26 changes: 26 additions & 0 deletions con3troller/source/hotkeys.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <3ds.h>

u32 exithotkey;

void setDefaultHotkeys();
u32 listenAndSetHotkey();

void setDefaultHotkeys() {
exithotkey = (KEY_START);
}

u32 listenAndSetHotkey() {
u32 kDown;
u32 keydowns = 0;
hidScanInput();
kDown = hidKeysHeld();
while (kDown) {
hidScanInput();
kDown = hidKeysHeld();
keydowns = kDown;
char buf[10];
snprintf(buf, 10, "%ld", keydowns);
stringLog(buf);
}
return keydowns;
}
206 changes: 146 additions & 60 deletions con3troller/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "log.h"
#include "strdraw.h"
#include "bth.h"
#include "hotkeys.h"
#include "utils.h"

#define PORT 7078

Expand All @@ -21,6 +23,8 @@ typedef struct controls {
enum mainstates {
STATE_INIT,
STATE_INITIAL,
STATE_BUTTONPRESSED_X,
STATE_BUTTONPRESSED_B,
STATE_LOAD_IP_FROM_CONFIG,
STATE_SELECT_IP,
STATE_SAVE_IP_TO_CONFIG,
Expand All @@ -37,18 +41,30 @@ enum settingsstates {
SETTINGS_MENU
};

enum settingshotkeysstates {
HOTKEYS_MENU,
HOTKEYS_SET_EXIT
};

int main() {
//vars
int state = STATE_INIT;
int settingsstate = SETTINGS_MENU;
char ip[20] = "";
char currentIP[50] = "";
char actionText[200];
char errText[60];
char logbuf[100];
//general variables
int
state = STATE_INIT,
settingsstate = SETTINGS_MENU,
hotkeystate = HOTKEYS_MENU,
result;

char
ip[20] = "",
currentIP[50] = "",
actionText[200],
errText[60],
logbuf[100],
pbuttons[1000],
pbuttons2[900];
DrawContext ctx;
int result;
bool showLogs = false;
bool
showLogs = false;
//inits
gfxInitDefault();
cfguInit();
Expand Down Expand Up @@ -89,15 +105,18 @@ int main() {
}
initContext(&ctx);
initColors(&ctx);
setDefaultHotkeys();
sprintf(errText, "All good");
Button buttonQuit, buttonHotkeys, buttonReturn, buttonGeneral, buttonSettingsReturn, buttonGeneralHID, buttonGeneralTouch;
Button buttonQuit, buttonHotkeys, buttonReturn, buttonGeneral, buttonSettingsReturn, buttonGeneralHID, buttonGeneralTouch, buttonHotkeyCExit, buttonsHotkeyCReset;
newButton(&buttonGeneral, 0.0f, 0.0f, 0.0f, SCREEN_WIDTH_BOTTOM, 60.0f, "General", 2.0f, ctx.clrWhite, ctx.clrBgDark, 0.5f);
newButton(&buttonHotkeys, 0.0f, 60.0f, 0.0f, SCREEN_WIDTH_BOTTOM, 60.0f, "Hotkeys", 2.0f, ctx.clrWhite, ctx.clrBgDark, 0.5f);
newButton(&buttonReturn, 0.0f, 120.0f, 0.0f, SCREEN_WIDTH_BOTTOM, 60.0f, "Return to main menu", 2.0f, ctx.clrWhite, ctx.clrBgDark, 0.5f);
newButton(&buttonQuit, 0.0f, 180.0f, 0.0f, SCREEN_WIDTH_BOTTOM, 60.0f, "Quit", 2.0f, ctx.clrWhite, ctx.clrBgDark, 0.5f);
newButton(&buttonSettingsReturn, 0.0f, 180.0f, 0.0f, SCREEN_WIDTH_BOTTOM, 60.0f, "Return to settings menu", 2.0f, ctx.clrWhite, ctx.clrBgDark, 0.5f);
newButton(&buttonGeneralHID, 0.0f, 0.0f, 0.0f, SCREEN_WIDTH_BOTTOM, 30.0f, "Enable HID: ", 2.0f, ctx.clrWhite, ctx.clrBgDark, 0.5f);
newButton(&buttonGeneralTouch, 0.0f, 30.0f, 0.0f, SCREEN_WIDTH_BOTTOM, 30.0f, "Enable Touch: ", 2.0f, ctx.clrWhite, ctx.clrBgDark, 0.5f);
newButton(&buttonHotkeyCExit, 0.0f, 30.0f, 0.0f, SCREEN_WIDTH_BOTTOM, 30.0f, "Exit combo: ", 2.0f, ctx.clrWhite, ctx.clrBgDark, 0.5f);
newButton(&buttonsHotkeyCReset, 0.0f, 150.0f, 0.0f, SCREEN_WIDTH_BOTTOM, 30.0f, "Reset to default", 2.0f, ctx.clrWhite, ctx.clrBgDark, 0.5f);
while (aptMainLoop()) {
u32 _kDown, kDown, _kUp, kUp, _kHeld;
kDown = 0, kUp = 0;
Expand Down Expand Up @@ -130,51 +149,65 @@ int main() {
} else {
state = STATE_INITIAL;
}
break;
case STATE_INITIAL:
sprintf(currentIP, "IP: %s", ip);
if (_kDown & KEY_START) goto deinit;
if (_kHeld & KEY_START) goto deinit;
if (_kDown & KEY_X) {
while(_kHeld & KEY_X) { // X combos / Log
hidScanInput();
u32 _kHeld = hidKeysHeld();

if (_kHeld & KEY_DUP) { // X + Dpad Up: toggleLog
if (showLogs) {
showLogs = false;
} else {
showLogs = true;
}
break;
}
if (_kHeld & KEY_DDOWN) { // X + Dpad Down: clearLog
clearLog();
break;
}
}
state = STATE_BUTTONPRESSED_X;
}
if (_kDown & KEY_Y) {
state = STATE_SETTINGS_MENU;
}
if (_kDown & KEY_B) {
while(_kHeld & KEY_B) { // B combos / IP
hidScanInput();
u32 _kHeld = hidKeysHeld();

if (_kHeld & KEY_DUP) { // B + Dpad Up: load from config
state = STATE_LOAD_IP_FROM_CONFIG;
break;
}
if (_kHeld & KEY_DDOWN) { // X + Dpad Down: open swkbd
state = STATE_SELECT_IP;
break;
}
if (_kHeld & KEY_DLEFT) {
state = STATE_SAVE_IP_TO_CONFIG;
break;
state = STATE_BUTTONPRESSED_B;
}
if (_kDown & KEY_A) state = STATE_PRE_CONNECTION_SETUP;
break;
case STATE_BUTTONPRESSED_B:
while(_kHeld & KEY_B) { // B combos / IP
hidScanInput();
u32 _kHeld = hidKeysHeld();
if (_kHeld & KEY_DUP) { // B + Dpad Up: load from config
state = STATE_LOAD_IP_FROM_CONFIG;
break;
}
if (_kHeld & KEY_DDOWN) { // B + Dpad Down: open swkbd
state = STATE_SELECT_IP;
break;
}
if (_kHeld & KEY_DLEFT) { // B + Dpad Left: save to config
state = STATE_SAVE_IP_TO_CONFIG;
break;
}
if (!_kHeld) {
state = STATE_INITIAL;
break;
}
}
break;
case STATE_BUTTONPRESSED_X:
while(_kHeld & KEY_X) { // X combos / Log
hidScanInput();
u32 _kHeld = hidKeysHeld();

if (_kHeld & KEY_DUP) { // X + Dpad Up: toggleLog
if (showLogs) {
showLogs = false;
} else {
showLogs = true;
}
break;
}
if (_kHeld & KEY_DDOWN) { // X + Dpad Down: clearLog
clearLog();
break;
}
if (!_kHeld) {
break;
}
}
if (_kDown & KEY_A) state = STATE_PRE_CONNECTION_SETUP;
state = STATE_INITIAL;
break;
case STATE_LOAD_IP_FROM_CONFIG:
//parse ip
Expand Down Expand Up @@ -282,7 +315,7 @@ int main() {
}
sendData(&controls, sizeof(Controls), 0);
}
if (_kDown & KEY_START) {
if (_kDown & exithotkey) {
disconnectfromServer();
state = STATE_INITIAL;
}
Expand Down Expand Up @@ -315,26 +348,46 @@ int main() {
}
break;
case SETTINGS_HOTKEYS:
if (isButtonPressed(&buttonSettingsReturn)) {
settingsstate = SETTINGS_MENU;
switch(hotkeystate) {
case HOTKEYS_MENU:
if (isButtonPressed(&buttonsHotkeyCReset)) {
setDefaultHotkeys();
}
if (isButtonPressed(&buttonSettingsReturn)) {
settingsstate = SETTINGS_MENU;
}
if (isButtonPressed(&buttonHotkeyCExit)) {
hotkeystate = HOTKEYS_SET_EXIT;
}
break;
case HOTKEYS_SET_EXIT:
isButtonPressed(&buttonHotkeyCExit);
u32 hotkey = listenAndSetHotkey();
getComboString(hotkey, pbuttons);
sprintf(pbuttons, "Exit combo: %s", pbuttons2);
changeButtonString(&buttonHotkeyCExit, pbuttons);
hotkeystate = HOTKEYS_MENU;
break;
}
break;
break;
}
break;



}
if (state == STATE_SETTINGS_MENU && settingsstate == SETTINGS_GENERAL) {
if (allowHID()) {
changeButtonString(&buttonGeneralHID, "Enable HID: ON");
} else {
changeButtonString(&buttonGeneralHID, "Enable HID: OFF");
}
if (allowTouch()) {
changeButtonString(&buttonGeneralTouch, "Enable Touch: ON");
} else {
changeButtonString(&buttonGeneralTouch, "Enable Touch: OFF");
if (state == STATE_SETTINGS_MENU) {
if (settingsstate == SETTINGS_GENERAL) {
if (allowHID()) {
changeButtonString(&buttonGeneralHID, "Enable HID: ON");
} else {
changeButtonString(&buttonGeneralHID, "Enable HID: OFF");
}
if (allowTouch()) {
changeButtonString(&buttonGeneralTouch, "Enable Touch: ON");
} else {
changeButtonString(&buttonGeneralTouch, "Enable Touch: OFF");
}
}
}

Expand Down Expand Up @@ -380,7 +433,37 @@ int main() {
drawStringBoxXCentered(SCREEN_HEIGHT-(90.0f*0.5f)-5, 0, 0.5f, ctx.clrWhite, ctx.clrBgBright, currentIP, 1.0f);
switch(state) {
case STATE_INITIAL:
drawStringBoxXCentered(SCREEN_HEIGHT-(30*0.5f)-1, 0, 0.5f, ctx.clrWhite, ctx.clrBgBright, "START: Exit A: Begin X: Toggle logs Y: Change IP B: Load IP", 1.0f);
if (_kDown & KEY_X || _kHeld & KEY_X) {
drawStringBoxXCentered(SCREEN_HEIGHT-(30*0.5f)-1, 0, 0.5f, ctx.clrWhite, ctx.clrBgBright, "Down: Clear Log Up: Hide/show Log", 1.0f);
}
else if (_kHeld & KEY_B || _kHeld & KEY_B) {
drawStringBoxXCentered(SCREEN_HEIGHT-(30*0.5f)-3, 0, 0.5f, ctx.clrWhite, ctx.clrBgBright, "Down: Select IP Up: Load IP from configuration Left: Save IP to configuration", 1.0f);
}
else {
drawStringBoxXCentered(SCREEN_HEIGHT-(30*0.5f)-1, 0, 0.5f, ctx.clrWhite, ctx.clrBgBright, "START: Exit A: Begin Y: Settings X: Configure IP Y: Settings", 1.0f);
}
break;
case STATE_BUTTONPRESSED_B:
if (_kDown & KEY_X || _kHeld & KEY_X) {
drawStringBoxXCentered(SCREEN_HEIGHT-(30*0.5f)-1, 0, 0.5f, ctx.clrWhite, ctx.clrBgBright, "Down: Clear Log Up: Hide/show Log", 1.0f);
}
else if (_kHeld & KEY_B || _kHeld & KEY_B) {
drawStringBoxXCentered(SCREEN_HEIGHT-(30*0.5f)-3, 0, 0.5f, ctx.clrWhite, ctx.clrBgBright, "Down: Select IP Up: Load IP from configuration Left: Save IP to configuration", 1.0f);
}
else {
drawStringBoxXCentered(SCREEN_HEIGHT-(30*0.5f)-1, 0, 0.5f, ctx.clrWhite, ctx.clrBgBright, "START: Exit A: Begin Y: Settings X: Configure IP Y: Settings", 1.0f);
}
break;
case STATE_BUTTONPRESSED_X:
if (_kDown & KEY_X || _kHeld & KEY_X) {
drawStringBoxXCentered(SCREEN_HEIGHT-(30*0.5f)-1, 0, 0.5f, ctx.clrWhite, ctx.clrBgBright, "Down: Clear Log Up: Hide/show Log", 1.0f);
}
else if (_kHeld & KEY_B || _kHeld & KEY_B) {
drawStringBoxXCentered(SCREEN_HEIGHT-(30*0.5f)-3, 0, 0.5f, ctx.clrWhite, ctx.clrBgBright, "Down: Select IP Up: Load IP from configuration Left: Save IP to configuration", 1.0f);
}
else {
drawStringBoxXCentered(SCREEN_HEIGHT-(30*0.5f)-1, 0, 0.5f, ctx.clrWhite, ctx.clrBgBright, "START: Exit A: Begin Y: Settings X: Configure IP Y: Settings", 1.0f);
}
break;
case STATE_RUNNING:
drawStringBoxXCentered(SCREEN_HEIGHT-(30*0.5f)-1, 0, 0.5f, ctx.clrWhite, ctx.clrBgBright, "START: Stop", 1.0f);
Expand All @@ -403,6 +486,9 @@ int main() {
case SETTINGS_HOTKEYS:
C2D_SceneBegin(ctx.bottom);
drawButton(&buttonSettingsReturn);
drawButton(&buttonsHotkeyCReset);
drawButton(&buttonHotkeyCExit);
break;
}

}
Expand Down
3 changes: 3 additions & 0 deletions con3troller/source/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ size_t getComboString(u32 bbt, char* out) {
if (bbt & KEY_CSTICK_RIGHT) {
strcat(out, "CStick Right+");
}
if (bbt & KEY_TOUCH) {
strcat(out, "Touchscreen+");
}
size_t outlen = strlen(out);
out[outlen] = '\0';
return outlen;
Expand Down
1 change: 0 additions & 1 deletion ip.txt

This file was deleted.

16 changes: 4 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,13 @@ Buttons are mappable to other keyboard letters via kconfig.json. Special buttons
```python -3 -m pip install pynput```
2. Place `kconfig.json` next to `server.py`
3. Execute the `server.py` script in the command line with `python server.py` (python may also only work as `python3` or `py -3`)
4. n the 3DS SD card, create a folder named `con3troller`
5. Inside, place a file named `ip.txt`
6. In this file, you place your PCs LOCAL IP address, e. g. ```192.168.178.61```, just that, no newline
+ How do I get my local IP... - Google it
7. Place `con3troller.3dsx` into `/3ds/` on the 3DS SD card
8. Launch `con3troller` in the Homebrew Launcher

Profit

You don't have to do all that every time, basically just ensure IP, launch python script, launch 3DS application, profit.

4. Place `con3troller.3dsx` into `/3ds/` on the 3DS SD card
5. Launch `con3troller` in the Homebrew Launcher

## 3DS side usage

Press START to exit the app
Exit with the START button, begin with A button. Select the IP by using B and the D-Pad, open the settings(WIP) with the Y button.
IP is saved in sdmc:/con3troller/ip.txt

## Compiling

Expand Down

0 comments on commit f056d43

Please sign in to comment.