Skip to content

Commit

Permalink
SDL: Fix potential race condition when pressing keys (fixes #872)
Browse files Browse the repository at this point in the history
  • Loading branch information
endrift committed Sep 17, 2017
1 parent 936eb1d commit e6f81b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -24,6 +24,7 @@ Bugfixes:
- GB Audio: Fix NRx2 writes while active (fixes mgba.io/i/866)
- GBA BIOS: Use core's VRAM variable instead of renderer's
- GBA Savedata: Fix 512 byte EEPROM saving as 8kB (fixes mgba.io/i/877)
- SDL: Fix potential race condition when pressing keys (fixes mgba.io/i/872)
Misc:
- Qt: Don't rebuild library view if style hasn't changed
- SDL: Fix 2.0.5 build on macOS under some circumstances
Expand Down
32 changes: 20 additions & 12 deletions src/platform/sdl/sdl-events.c
Expand Up @@ -411,11 +411,13 @@ static void _mSDLHandleKeypress(struct mCoreThread* context, struct mSDLPlayer*
key = mInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.sym);
}
if (key != -1) {
mCoreThreadInterrupt(context);
if (event->type == SDL_KEYDOWN) {
context->core->addKeys(context->core, 1 << key);
} else {
context->core->clearKeys(context->core, 1 << key);
}
mCoreThreadContinue(context);
return;
}
if (event->keysym.sym == SDLK_TAB) {
Expand Down Expand Up @@ -516,42 +518,48 @@ static void _mSDLHandleKeypress(struct mCoreThread* context, struct mSDLPlayer*
}
}

static void _mSDLHandleJoyButton(struct mCore* core, struct mSDLPlayer* sdlContext, const struct SDL_JoyButtonEvent* event) {
static void _mSDLHandleJoyButton(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const struct SDL_JoyButtonEvent* event) {
int key = 0;
key = mInputMapKey(sdlContext->bindings, SDL_BINDING_BUTTON, event->button);
if (key == -1) {
return;
}

mCoreThreadInterrupt(context);
if (event->type == SDL_JOYBUTTONDOWN) {
core->addKeys(core, 1 << key);
context->core->addKeys(context->core, 1 << key);
} else {
core->clearKeys(core, 1 << key);
context->core->clearKeys(context->core, 1 << key);
}
mCoreThreadContinue(context);
}

static void _mSDLHandleJoyHat(struct mCore* core, struct mSDLPlayer* sdlContext, const struct SDL_JoyHatEvent* event) {
static void _mSDLHandleJoyHat(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const struct SDL_JoyHatEvent* event) {
int allKeys = mInputMapHat(sdlContext->bindings, SDL_BINDING_BUTTON, event->hat, -1);
if (allKeys == 0) {
return;
}

int keys = mInputMapHat(sdlContext->bindings, SDL_BINDING_BUTTON, event->hat, event->value);

core->clearKeys(core, allKeys ^ keys);
core->addKeys(core, keys);
mCoreThreadInterrupt(context);
context->core->clearKeys(context->core, allKeys ^ keys);
context->core->addKeys(context->core, keys);
mCoreThreadContinue(context);
}

static void _mSDLHandleJoyAxis(struct mCore* core, struct mSDLPlayer* sdlContext, const struct SDL_JoyAxisEvent* event) {
static void _mSDLHandleJoyAxis(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const struct SDL_JoyAxisEvent* event) {
int clearKeys = ~mInputClearAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, -1);
int newKeys = 0;
int key = mInputMapAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, event->value);
if (key != -1) {
newKeys |= 1 << key;
}
clearKeys &= ~newKeys;
core->clearKeys(core, clearKeys);
core->addKeys(core, newKeys);
mCoreThreadInterrupt(context);
context->core->clearKeys(context->core, clearKeys);
context->core->addKeys(context->core, newKeys);
mCoreThreadContinue(context);

}

Expand Down Expand Up @@ -581,13 +589,13 @@ void mSDLHandleEvent(struct mCoreThread* context, struct mSDLPlayer* sdlContext,
break;
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
_mSDLHandleJoyButton(context->core, sdlContext, &event->jbutton);
_mSDLHandleJoyButton(context, sdlContext, &event->jbutton);
break;
case SDL_JOYHATMOTION:
_mSDLHandleJoyHat(context->core, sdlContext, &event->jhat);
_mSDLHandleJoyHat(context, sdlContext, &event->jhat);
break;
case SDL_JOYAXISMOTION:
_mSDLHandleJoyAxis(context->core, sdlContext, &event->jaxis);
_mSDLHandleJoyAxis(context, sdlContext, &event->jaxis);
break;
}
}
Expand Down

0 comments on commit e6f81b0

Please sign in to comment.