Skip to content

Commit

Permalink
Merge pull request #107 from AEFeinstein/digital-touchpad-emu
Browse files Browse the repository at this point in the history
Make keys 1-4 in the emulator trigger the touchpad
  • Loading branch information
AEFeinstein authored Oct 31, 2023
2 parents c23e563 + 4a1aaa4 commit fdce162
Showing 1 changed file with 45 additions and 2 deletions.
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

0 comments on commit fdce162

Please sign in to comment.