From 2e72b1b0ff27f472b09e678d2d0f43c1a5176169 Mon Sep 17 00:00:00 2001 From: CTurt Date: Sat, 21 Mar 2015 12:28:25 +0000 Subject: [PATCH] Support joypad key mapping --- PC/3DSController.ini | 2 +- PC/include/joystick.h | 10 ++++-- PC/include/keys.h | 20 ++++++++++-- PC/include/settings.h | 4 ++- PC/source/joystick.c | 26 +++------------ PC/source/main.c | 22 ++++++++++--- PC/source/settings.c | 74 +++++++++++++++++++++++++------------------ 7 files changed, 95 insertions(+), 63 deletions(-) diff --git a/PC/3DSController.ini b/PC/3DSController.ini index 4c8c3d6..4059437 100644 --- a/PC/3DSController.ini +++ b/PC/3DSController.ini @@ -4,7 +4,7 @@ Circle Pad and Touch can be either JOYSTICK or MOUSE, Mouse Speed controls how fast the Circle Pad or Touch Screen moves the mouse. If set to 0 and using the Touch Screen, it will set to the absolute position, rather than moving relatively to last position, -Buttons can either be a letter for example B, or a special key, like SPACE, CLICK, RIGHT CLICK, ENTER, BACKSPACE, SHIFT, TAB, LEFT, RIGHT, UP, DOWN, PAGE UP, PAGE DOWN, or WINDOWS, +Buttons can be a letter for a keyboard key (like Q, W, E, R, T, or Y), a special keyboard key (like SPACE, CLICK, RIGHT CLICK, ENTER, BACKSPACE, SHIFT, TAB, LEFT, RIGHT, UP, DOWN, PAGE UP, PAGE DOWN, or WINDOWS), or a joypad button (JOY1, JOY2, JOY3, to JOY8), Alternatively, you can disable a key by binding it to NONE, diff --git a/PC/include/joystick.h b/PC/include/joystick.h index 2efba2b..9f4bf31 100644 --- a/PC/include/joystick.h +++ b/PC/include/joystick.h @@ -9,10 +9,16 @@ #include "public.h" #include "vjoyinterface.h" +#define joyX iReport.wAxisX +#define joyY iReport.wAxisY +#define joyButtons iReport.lButtons + #define JOY_MIDDLE (128 * 128) extern int ContPovNumber; extern UINT iInterface; -extern BOOL ContinuousPOV; +//extern BOOL ContinuousPOV; -BOOL updateJoystick(int x, int y); +extern JOYSTICK_POSITION iReport; + +BOOL updateJoystick(void); diff --git a/PC/include/keys.h b/PC/include/keys.h index 293b3b5..40baacc 100644 --- a/PC/include/keys.h +++ b/PC/include/keys.h @@ -7,8 +7,16 @@ #define newpress(key) ((currentKeys & key) && !(lastKeys & key)) #define release(key) (!(currentKeys & key) && (lastKeys & key)) -#define handleKey(DSKey, PCKey) if(newpress(DSKey)) simulateKeyNewpress(PCKey);\ -if(release(DSKey)) simulateKeyRelease(PCKey) +#define handleKey(DSKey, PCKey) do {\ + if(PCKey.useKeyboard) {\ + if(newpress(DSKey)) simulateKeyNewpress(PCKey.virtualKey);\ + if(release(DSKey)) simulateKeyRelease(PCKey.virtualKey);\ + }\ + else {\ + if(currentKeys & DSKey) joyButtons |= PCKey.joypadButton;\ + else joyButtons &= ~PCKey.joypadButton;\ + }\ +} while(0) #define BIT(n) (1 << (n)) @@ -44,6 +52,14 @@ typedef enum { KEY_RIGHT = KEY_DRIGHT | KEY_CPAD_RIGHT, } KEYPAD_BITS; +struct keyMapping { + unsigned char useKeyboard; // 0 joypad button, 1 keyboard key + union { + unsigned char virtualKey; + unsigned char joypadButton; + }; +}; + struct circlePad { short x; short y; diff --git a/PC/include/settings.h b/PC/include/settings.h index c25114f..f909109 100644 --- a/PC/include/settings.h +++ b/PC/include/settings.h @@ -2,6 +2,8 @@ #include +#include "keys.h" + enum analogue { mouse, joystick, @@ -13,7 +15,7 @@ struct settings { enum analogue circlePad; enum analogue touch; int mouseSpeed; - int A, B, X, Y, L, R, Left, Right, Up, Down, Start, Select, Tap; + struct keyMapping A, B, X, Y, L, R, Left, Right, Up, Down, Start, Select, Tap; }; extern struct settings settings; diff --git a/PC/source/joystick.c b/PC/source/joystick.c index c24c40a..735cec2 100644 --- a/PC/source/joystick.c +++ b/PC/source/joystick.c @@ -5,33 +5,15 @@ int ContPovNumber; UINT iInterface = 1; -BOOL ContinuousPOV = FALSE; +//BOOL ContinuousPOV = FALSE; -BOOL updateJoystick(int x, int y) { +JOYSTICK_POSITION iReport; + +BOOL updateJoystick(void) { BYTE id = (BYTE)iInterface; - JOYSTICK_POSITION iReport; iReport.bDevice = id; - iReport.wAxisX = x; - iReport.wAxisY = y; - iReport.wAxisZ = JOY_MIDDLE; - iReport.wAxisXRot = JOY_MIDDLE; - iReport.wAxisYRot = JOY_MIDDLE; - iReport.wAxisZRot = JOY_MIDDLE; - - iReport.lButtons = 0; - - if(ContPovNumber) { - iReport.bHats = -1; // Neutral state - iReport.bHatsEx1 = -1; // Neutral state - iReport.bHatsEx2 = -1; // Neutral state - iReport.bHatsEx3 = -1; // Neutral state - } - else { - iReport.bHats = -1; // Neutral state - }; - if(!UpdateVJD(iInterface, (PVOID)&iReport)) { /*printf("vJoy device %d failed - try to enable device\n", iInterface); printf("PRESS ENTER TO CONTINUE\n"); diff --git a/PC/source/main.c b/PC/source/main.c index d81e2cc..6a2b055 100644 --- a/PC/source/main.c +++ b/PC/source/main.c @@ -23,11 +23,19 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow) double widthMultiplier = screenWidth / 320.0; double heightMultiplier = screenHeight / 240.0; - screenshot(SCREENSHOT_NAMEL, TRUE, 0, 0, 18); + //screenshot(SCREENSHOT_NAMEL, TRUE, 0, 0, 18); bool vJoy = true; UINT iInterface = 1; + iReport.wAxisZ = JOY_MIDDLE; + iReport.wAxisXRot = JOY_MIDDLE; + iReport.wAxisYRot = JOY_MIDDLE; + iReport.wAxisZRot = JOY_MIDDLE; + iReport.wSlider = JOY_MIDDLE; + iReport.lButtons = 0; + iReport.bHats = -1; + if(vJoy && !vJoyEnabled()) { printf("vJoy failed (1)! Buttons will still work, but joy stick won't work.\n"); vJoy = false; @@ -42,8 +50,8 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow) ContPovNumber = GetVJDContPovNumber(iInterface); //int DiscPovNumber = GetVJDDiscPovNumber(iInterface); - if(vJoy && !updateJoystick(128 * 128, 128 * 128)) { - printf("vJoy failed (3)! Buttons will still work, but joy stick won't work.\n"); + if(vJoy && !updateJoystick()) { + printf("vJoy failed (3)! Buttons will still work, but joystick won't work.\n"); vJoy = false; } @@ -148,7 +156,8 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow) } } else if(settings.touch == joystick) { - if(vJoy) updateJoystick((currentTouch.x) * 128, (currentTouch.y) * 128); + joyX = (currentTouch.x) * 128; + joyY = (currentTouch.y) * 128; } else { handleKey(KEY_TOUCH, settings.Tap); @@ -164,12 +173,15 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow) SetCursorPos(p.x + (circlePad.x * settings.mouseSpeed) / 32, p.y - (circlePad.y * settings.mouseSpeed) / 32); } else if(settings.circlePad == joystick) { - if(vJoy) updateJoystick((circlePad.x + 128) * 128, (128 - circlePad.y) * 128); + joyX = (circlePad.x + 128) * 128; + joyY = (128 - circlePad.y) * 128; } break; } + if(vJoy) updateJoystick(); + //sendScreenshot(); } diff --git a/PC/source/settings.c b/PC/source/settings.c index 32193b1..604d195 100644 --- a/PC/source/settings.c +++ b/PC/source/settings.c @@ -2,6 +2,7 @@ #include #include +#include "keys.h" #include "wireless.h" #include "settings.h" @@ -14,19 +15,19 @@ struct settings defaultSettings = { circlePad: joystick, touch: mouse, mouseSpeed: 4, - A: 'A', - B: 'B', - X: 'X', - Y: 'Y', - L: 'L', - R: 'R', - Left: VK_LEFT, - Right: VK_RIGHT, - Up: VK_UP, - Down: VK_DOWN, - Start: VK_RETURN, - Select: VK_BACK, - Tap: 'T', + A: { 1, {'A'} }, + B: { 1, {'B'} }, + X: { 1, {'X'} }, + Y: { 1, {'Y'} }, + L: { 1, {'L'} }, + R: { 1, {'R'} }, + Left: { 1, {VK_LEFT} }, + Right: { 1, {VK_RIGHT} }, + Up: { 1, {VK_UP} }, + Down: { 1, {VK_DOWN} }, + Start: { 1, {VK_RETURN} }, + Select: { 1, {VK_BACK} }, + Tap: { 1, {'T'} }, }; static bool getSetting(char *name, char *src, char *dest) { @@ -48,24 +49,37 @@ static bool getSetting(char *name, char *src, char *dest) { return false; } -static int getButton(char *string) { - if(strcmp(string, "SPACE") == 0) return VK_SPACE; - else if(strcmp(string, "CLICK") == 0) return VK_LBUTTON; - else if(strcmp(string, "RIGHT CLICK") == 0) return VK_RBUTTON; - else if(strcmp(string, "ENTER") == 0) return VK_RETURN; - else if(strcmp(string, "BACKSPACE") == 0) return VK_BACK; - else if(strcmp(string, "SHIFT") == 0) return VK_SHIFT; - else if(strcmp(string, "TAB") == 0) return VK_TAB; - else if(strcmp(string, "LEFT") == 0) return VK_LEFT; - else if(strcmp(string, "RIGHT") == 0) return VK_RIGHT; - else if(strcmp(string, "UP") == 0) return VK_UP; - else if(strcmp(string, "DOWN") == 0) return VK_DOWN; - else if(strcmp(string, "PAGE UP") == 0) return VK_PRIOR; - else if(strcmp(string, "PAGE DOWN") == 0) return VK_NEXT; - else if(strcmp(string, "WINDOWS") == 0) return VK_LWIN; - else if(strcmp(string, "NONE") == 0) return 0; +static struct keyMapping getButton(char *string) { + struct keyMapping k = { 1, {0} }; - return (int)string[0]; + if(strcmp(string, "SPACE") == 0) k.virtualKey = VK_SPACE; + else if(strcmp(string, "CLICK") == 0) k.virtualKey = VK_LBUTTON; + else if(strcmp(string, "RIGHT CLICK") == 0) k.virtualKey = VK_RBUTTON; + else if(strcmp(string, "ENTER") == 0) k.virtualKey = VK_RETURN; + else if(strcmp(string, "BACKSPACE") == 0) k.virtualKey = VK_BACK; + else if(strcmp(string, "SHIFT") == 0) k.virtualKey = VK_SHIFT; + else if(strcmp(string, "TAB") == 0) k.virtualKey = VK_TAB; + else if(strcmp(string, "LEFT") == 0) k.virtualKey = VK_LEFT; + else if(strcmp(string, "RIGHT") == 0) k.virtualKey = VK_RIGHT; + else if(strcmp(string, "UP") == 0) k.virtualKey = VK_UP; + else if(strcmp(string, "DOWN") == 0) k.virtualKey = VK_DOWN; + else if(strcmp(string, "PAGE UP") == 0) k.virtualKey = VK_PRIOR; + else if(strcmp(string, "PAGE DOWN") == 0) k.virtualKey = VK_NEXT; + else if(strcmp(string, "WINDOWS") == 0) k.virtualKey = VK_LWIN; + else if(strcmp(string, "NONE") == 0) k.virtualKey = 0; + + else if(strcmp(string, "JOY1") == 0) { k.useKeyboard = 0; k.joypadButton = 1 << 0; } + else if(strcmp(string, "JOY2") == 0) { k.useKeyboard = 0; k.joypadButton = 1 << 1; } + else if(strcmp(string, "JOY3") == 0) { k.useKeyboard = 0; k.joypadButton = 1 << 2; } + else if(strcmp(string, "JOY4") == 0) { k.useKeyboard = 0; k.joypadButton = 1 << 3; } + else if(strcmp(string, "JOY5") == 0) { k.useKeyboard = 0; k.joypadButton = 1 << 4; } + else if(strcmp(string, "JOY6") == 0) { k.useKeyboard = 0; k.joypadButton = 1 << 5; } + else if(strcmp(string, "JOY7") == 0) { k.useKeyboard = 0; k.joypadButton = 1 << 6; } + else if(strcmp(string, "JOY8") == 0) { k.useKeyboard = 0; k.joypadButton = 1 << 7; } + + else k.virtualKey = (int)string[0]; + + return k; } bool readSettings(void) {