Skip to content
This repository has been archived by the owner on Oct 13, 2019. It is now read-only.

Commit

Permalink
Support joypad key mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
CTurt committed Mar 21, 2015
1 parent 313e21e commit 1b8a3eb
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 64 deletions.
2 changes: 1 addition & 1 deletion PC/3DSController.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions PC/include/joystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

extern JOYSTICK_POSITION iReport;

BOOL updateJoystick(int x, int y);
BOOL updateJoystick(void);
20 changes: 18 additions & 2 deletions PC/include/keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion PC/include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <stdbool.h>

#include "keys.h"

enum analogue {
mouse,
joystick,
Expand All @@ -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;
Expand Down
26 changes: 4 additions & 22 deletions PC/source/joystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
22 changes: 17 additions & 5 deletions PC/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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();
}

Expand Down
76 changes: 45 additions & 31 deletions PC/source/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <string.h>
#include <windows.h>

#include "keys.h"
#include "wireless.h"

#include "settings.h"
Expand All @@ -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) {
Expand All @@ -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;

return (int)string[0];
static struct keyMapping getButton(char *string) {
struct keyMapping k = { 1, {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) {
Expand Down

0 comments on commit 1b8a3eb

Please sign in to comment.