#include <controller.h>

#define VB_PWR 0x0001
#define VB_SGN 0x0002
#define VB_A   0x0004
#define VB_B   0x0008
#define VB_RT  0x0010
#define VB_LT  0x0020
#define VB_RU  0x0040
#define VB_RR  0x0080
#define VB_LR  0x0100
#define VB_LL  0x0200
#define VB_LD  0x0400
#define VB_LU  0x0800
#define VB_STA 0x1000
#define VB_SEL 0x2000
#define VB_RL  0x4000
#define VB_RD  0x8000

static uint16_t symToMask(SDL_KeyCode sym) {
    switch (sym) {
    default: return 0;
    case SDLK_a:
        return VB_SEL;
    case SDLK_s:
        return VB_STA;
    case SDLK_d:
        return VB_B;
    case SDLK_f:
        return VB_A;
    case SDLK_e:
        return VB_LT;
    case SDLK_r:
        return VB_RT;
    case SDLK_i:
        return VB_RU;
    case SDLK_j:
        return VB_RL;
    case SDLK_k:
        return VB_RD;
    case SDLK_l:
        return VB_RR;
    case SDLK_UP:
        return VB_LU;
    case SDLK_LEFT:
        return VB_LL;
    case SDLK_DOWN:
        return VB_LD;
    case SDLK_RIGHT:
        return VB_LR;
    }
}

void ctrlInit(ControllerState *ctrl) {
    ctrl->keys = VB_SGN;
}
void ctrlKeyDown(ControllerState *ctrl, SDL_Keycode sym) {
    ctrl->keys |= symToMask(sym);
}
void ctrlKeyUp(ControllerState *ctrl, SDL_Keycode sym) {
    ctrl->keys &= ~symToMask(sym);
}
uint16_t ctrlKeys(ControllerState *ctrl) {
    return ctrl->keys;
}