all repos — mgba @ 3ca5e52de17d3acc9858cc22cf7ad6921dff8a67

mGBA Game Boy Advance Emulator

Handle key, quit events
Jeffrey Pfau jeffrey@endrift.com
Sun, 21 Apr 2013 00:35:21 -0700
commit

3ca5e52de17d3acc9858cc22cf7ad6921dff8a67

parent

71986b0477a960833c87113d7dda36f02132e93b

6 files changed, 81 insertions(+), 2 deletions(-)

jump to
M src/gba/gba-io.csrc/gba/gba-io.c

@@ -150,6 +150,12 @@ case REG_TM3CNT_LO:

GBATimerUpdateRegister(gba, 3); break; + case REG_KEYINPUT: + if (gba->keySource) { + return 0x3FF ^ *gba->keySource; + } + break; + case REG_DMA0CNT_LO: case REG_DMA1CNT_LO: case REG_DMA2CNT_LO:
M src/gba/gba-thread.csrc/gba/gba-thread.c

@@ -26,6 +26,7 @@ if (threadContext->fd >= 0) {

GBALoadROM(&gba, threadContext->fd); } GBAAttachDebugger(&gba, &debugger); + gba.keySource = &threadContext->activeKeys; threadContext->started = 1; pthread_mutex_lock(&threadContext->mutex);

@@ -51,6 +52,7 @@ pthread_mutex_init(&threadContext->mutex, 0);

pthread_cond_init(&threadContext->cond, 0); pthread_mutex_lock(&threadContext->mutex); + threadContext->activeKeys = 0; threadContext->started = 0; pthread_create(&threadContext->thread, 0, _GBAThreadRun, threadContext); pthread_cond_wait(&threadContext->cond, &threadContext->mutex);
M src/gba/gba-thread.hsrc/gba/gba-thread.h

@@ -12,6 +12,7 @@

// Input struct GBAVideoRenderer* renderer; int fd; + int activeKeys; // Threading state pthread_mutex_t mutex;
M src/gba/gba.csrc/gba/gba.c

@@ -44,6 +44,7 @@

memset(gba->timers, 0, sizeof(gba->timers)); gba->springIRQ = 0; + gba->keySource = 0; ARMReset(&gba->cpu); }
M src/gba/gba.hsrc/gba/gba.h

@@ -33,6 +33,19 @@ GBA_LOG_STUB,

GBA_LOG_WARN }; +enum GBAKey { + GBA_KEY_A = 0, + GBA_KEY_B = 1, + GBA_KEY_SELECT = 2, + GBA_KEY_START = 3, + GBA_KEY_RIGHT = 4, + GBA_KEY_LEFT = 5, + GBA_KEY_UP = 6, + GBA_KEY_DOWN = 7, + GBA_KEY_R = 8, + GBA_KEY_L = 9 +}; + struct GBABoard { struct ARMBoard d; struct GBA* p;

@@ -60,6 +73,7 @@ unsigned enable : 1;

} timers[4]; int springIRQ; + int* keySource; enum GBAError errno; const char* errstr;
M src/main.csrc/main.c

@@ -1,5 +1,6 @@

#include "debugger.h" #include "gba-thread.h" +#include "gba.h" #include "renderers/video-software.h" #include <sdl.h>

@@ -19,6 +20,7 @@

static int _GBASDLInit(struct GLSoftwareRenderer* renderer); static void _GBASDLDeinit(struct GLSoftwareRenderer* renderer); static void _GBASDLRunloop(struct GBAThread* context, struct GLSoftwareRenderer* renderer); +static void _GBASDLHandleKeypress(struct GBAThread* context, const struct SDL_KeyboardEvent* event); static const GLint _glVertices[] = { 0, 0,

@@ -112,8 +114,17 @@ SDL_GL_SwapBuffers();

pthread_mutex_lock(&renderer->d.mutex); pthread_cond_broadcast(&renderer->d.cond); pthread_mutex_unlock(&renderer->d.mutex); - while(SDL_PollEvent(&event)) { - + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + // FIXME: this isn't thread-safe + context->debugger->state = DEBUGGER_EXITING; + break; + case SDL_KEYDOWN: + case SDL_KEYUP: + _GBASDLHandleKeypress(context, &event.key); + break; + } } } }

@@ -123,3 +134,47 @@ free(renderer->d.outputBuffer);

SDL_Quit(); } + +static void _GBASDLHandleKeypress(struct GBAThread* context, const struct SDL_KeyboardEvent* event) { + enum GBAKey key = 0; + switch (event->keysym.sym) { + case SDLK_z: + key = GBA_KEY_A; + break; + case SDLK_x: + key = GBA_KEY_B; + break; + case SDLK_a: + key = GBA_KEY_L; + break; + case SDLK_s: + key = GBA_KEY_R; + break; + case SDLK_RETURN: + key = GBA_KEY_START; + break; + case SDLK_BACKSPACE: + key = GBA_KEY_SELECT; + break; + case SDLK_UP: + key = GBA_KEY_UP; + break; + case SDLK_DOWN: + key = GBA_KEY_DOWN; + break; + case SDLK_LEFT: + key = GBA_KEY_LEFT; + break; + case SDLK_RIGHT: + key = GBA_KEY_RIGHT; + break; + default: + return; + } + + if (event->type == SDL_KEYDOWN) { + context->activeKeys |= 1 << key; + } else { + context->activeKeys &= ~(1 << key); + } +}