Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make keys 1-4 in the emulator trigger the touchpad #107

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions emulator/src/extensions/touch/ext_touch.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ static void calculateTouch(int32_t x, int32_t y, int32_t* angle, int32_t* radius
static bool updateTouch(int32_t x, int32_t y, bool clicked);

static bool touchInit(emuArgs_t* emuArgs);
static int32_t touchKey(uint32_t key, bool down);
static bool touchMouseMove(int32_t x, int32_t y, mouseButton_t buttonMask);
static bool touchMouseButton(int32_t x, int32_t y, mouseButton_t button, bool down);
static void touchRender(uint32_t winW, uint32_t winH, const emuPane_t* pane, uint8_t numPanes);
Expand All @@ -87,10 +88,11 @@ typedef struct
int32_t lastHoverX;
int32_t lastHoverY;

uint32_t keyState;

int32_t lastTouchPhi;
int32_t lastTouchRadius;
int32_t lastTouchIntensity;
buttonBit_t lastTouchButtons;

uint32_t paneX;
uint32_t paneY;
Expand All @@ -107,7 +109,7 @@ emuExtension_t touchEmuCallback = {
.fnInitCb = touchInit,
.fnPreFrameCb = NULL,
.fnPostFrameCb = NULL,
.fnKeyCb = NULL,
.fnKeyCb = touchKey,
.fnMouseMoveCb = touchMouseMove,
.fnMouseButtonCb = touchMouseButton,
.fnRenderCb = touchRender,
Expand Down Expand Up @@ -254,6 +256,47 @@ static bool touchInit(emuArgs_t* emuArgs)
}
}

static int32_t touchKey(uint32_t key, bool down)
{
const int32_t phiMap[] = {90, 0, 180, 270};

if (key < '1' || key > '4')
{
// Do not consume event, we only want 1 to 4
return 0;
}

int keyNum = (key - '1');
// Handle the key states separately so rolling over them works more or less how one might expect
if (down && !(emuTouch.keyState & (1 << keyNum)))
{
emuTouch.keyState |= (1 << keyNum);
}
else if (!down && (emuTouch.keyState & (1 << keyNum)))
{
emuTouch.keyState &= ~(1 << keyNum);
}

if (emuTouch.keyState)
{
for (int i = 0; i < 4; i++)
{
if (emuTouch.keyState & (1 << i))
{
emulatorSetTouchJoystick(phiMap[i], 1024, emuTouch.lastTouchIntensity);
break;
}
}
}
else
{
emulatorSetTouchJoystick(0, 0, 0);
}

// Consume event
return -1;
}

static bool touchMouseMove(int32_t x, int32_t y, mouseButton_t buttonMask)
{
return updateTouch(x, y, (buttonMask & EMU_MOUSE_LEFT) == EMU_MOUSE_LEFT);
Expand Down