Merge pull request #1370 from reicast/baka/sdl

SDL: Fix keyboard usage
This commit is contained in:
Christoph 2018-09-04 22:25:03 +02:00 committed by GitHub
commit 00ecf2f1ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 70 additions and 64 deletions

View File

@ -56,7 +56,7 @@ void input_sdl_init()
{ {
if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0)
{ {
if(SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0)
{ {
die("error initializing SDL Joystick subsystem"); die("error initializing SDL Joystick subsystem");
} }
@ -68,10 +68,10 @@ void input_sdl_init()
{ {
JoySDL = SDL_JoystickOpen(0); JoySDL = SDL_JoystickOpen(0);
} }
printf("Joystick opened\n"); printf("Joystick opened\n");
if(JoySDL) if (JoySDL)
{ {
int AxisCount,ButtonCount; int AxisCount,ButtonCount;
const char* Name; const char* Name;
@ -114,13 +114,13 @@ void input_sdl_init()
JSensitivity[128+i] = j; JSensitivity[128+i] = j;
} }
#endif #endif
SDL_SetRelativeMouseMode(SDL_TRUE); SDL_SetRelativeMouseMode(SDL_TRUE);
} }
void input_sdl_handle(u32 port) void input_sdl_handle(u32 port)
{ {
static int keys[13]; #define SET_FLAG(field, mask, expr) field =((expr) ? (field & ~mask) : (field | mask))
static int mouse_use = 0; static int mouse_use = 0;
SDL_Event event; SDL_Event event;
int k, value; int k, value;
@ -131,30 +131,59 @@ void input_sdl_handle(u32 port)
switch (event.type) switch (event.type)
{ {
case SDL_QUIT: case SDL_QUIT:
die("death by SDL request"); dc_stop();
break; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
case SDL_KEYUP: case SDL_KEYUP:
k = event.key.keysym.sym; k = event.key.keysym.sym;
value = (event.type == SDL_KEYDOWN) ? 1 : 0; value = (event.type == SDL_KEYDOWN) ? 1 : 0;
//printf("type %i key %i \n", event.type, k); printf("type %i key %i \n", event.type, k);
switch (k) { switch (k) {
//TODO: Better keymaps for non-pandora platforms //TODO: Better keymaps for non-pandora platforms
case SDLK_SPACE: keys[0] = value; break; case SDLK_SPACE:
case SDLK_UP: keys[1] = value; break; SET_FLAG(kcode[port], DC_BTN_C, value);
case SDLK_DOWN: keys[2] = value; break; break;
case SDLK_LEFT: keys[3] = value; break; case SDLK_UP:
case SDLK_RIGHT: keys[4] = value; break; SET_FLAG(kcode[port], DC_DPAD_UP, value);
case SDLK_PAGEUP: keys[5] = value; break; break;
case SDLK_PAGEDOWN: keys[6] = value; break; case SDLK_DOWN:
case SDLK_END: keys[7] = value; break; SET_FLAG(kcode[port], DC_DPAD_DOWN, value);
case SDLK_HOME: keys[8] = value; break; break;
case SDLK_LEFT:
SET_FLAG(kcode[port], DC_DPAD_LEFT, value);
break;
case SDLK_RIGHT:
SET_FLAG(kcode[port], DC_DPAD_RIGHT, value);
break;
case SDLK_PAGEUP:
SET_FLAG(kcode[port], DC_BTN_Y, value);
break;
case SDLK_PAGEDOWN:
SET_FLAG(kcode[port], DC_BTN_A, value);
break;
case SDLK_END:
SET_FLAG(kcode[port], DC_BTN_B, value);
break;
case SDLK_HOME:
SET_FLAG(kcode[port], DC_BTN_X, value);
break;
case SDLK_MENU: case SDLK_MENU:
case SDLK_ESCAPE: keys[9] = value; break; case SDLK_ESCAPE:
case SDLK_RSHIFT: keys[11] = value; break; dc_stop();
case SDLK_RCTRL: keys[10] = value; break; break;
case SDLK_LALT: keys[12] = value; break; case SDLK_RSHIFT:
case SDLK_k: KillTex = true; break; lt[port] = (value ? 255 : 0);
break;
case SDLK_RCTRL:
rt[port] = (value ? 255 : 0);;
break;
case SDLK_LALT:
SET_FLAG(kcode[port], DC_BTN_START, value);
break;
case SDLK_k:
SET_FLAG(kcode[port], DC_BTN_A, value);
break;
#if defined(TARGET_PANDORA) #if defined(TARGET_PANDORA)
case SDLK_n: case SDLK_n:
if (value) if (value)
@ -163,7 +192,7 @@ void input_sdl_handle(u32 port)
snprintf(OSD_Info, 128, "Right Nub mode: %s\n", num_mode[mouse_use]); snprintf(OSD_Info, 128, "Right Nub mode: %s\n", num_mode[mouse_use]);
OSD_Delay=300; OSD_Delay=300;
} }
break; break;
case SDLK_s: case SDLK_s:
if (value) if (value)
{ {
@ -179,7 +208,7 @@ void input_sdl_handle(u32 port)
snprintf(OSD_Info, 128, "FrameSkipping %s\n", (FrameSkipping) ? "On" : "Off"); snprintf(OSD_Info, 128, "FrameSkipping %s\n", (FrameSkipping) ? "On" : "Off");
OSD_Delay = 300; OSD_Delay = 300;
} }
break; break;
case SDLK_c: case SDLK_c:
if (value) if (value)
{ {
@ -196,16 +225,16 @@ void input_sdl_handle(u32 port)
{ {
u32 mt = sdl_map_btn[k] >> 16; u32 mt = sdl_map_btn[k] >> 16;
u32 mo = sdl_map_btn[k] & 0xFFFF; u32 mo = sdl_map_btn[k] & 0xFFFF;
// printf("BUTTON %d,%d\n",JE.number,JE.value); // printf("BUTTON %d,%d\n",JE.number,JE.value);
if (mt == 0) if (mt == 0)
{ {
// printf("Mapped to %d\n",mo); // printf("Mapped to %d\n",mo);
if (value) if (value)
kcode[port] &= ~mo; kcode[port] &= ~mo;
else else
kcode[port] |= mo; kcode[port] |= mo;
} }
else if (mt == 1) else if (mt == 1)
{ {
@ -219,7 +248,7 @@ void input_sdl_handle(u32 port)
rt[port] = value ? 255 : 0; rt[port] = value ? 255 : 0;
} }
} }
} }
break; break;
case SDL_JOYAXISMOTION: case SDL_JOYAXISMOTION:
@ -228,13 +257,13 @@ void input_sdl_handle(u32 port)
{ {
u32 mt = sdl_map_axis[k] >> 16; u32 mt = sdl_map_axis[k] >> 16;
u32 mo = sdl_map_axis[k] & 0xFFFF; u32 mo = sdl_map_axis[k] & 0xFFFF;
//printf("AXIS %d,%d\n",JE.number,JE.value); //printf("AXIS %d,%d\n",JE.number,JE.value);
s8 v=(s8)(value/256); //-127 ... + 127 range s8 v=(s8)(value/256); //-127 ... + 127 range
#ifdef TARGET_PANDORA #ifdef TARGET_PANDORA
v = JSensitivity[128+v]; v = JSensitivity[128+v];
#endif #endif
if (mt == 0) if (mt == 0)
{ {
kcode[port] |= mo; kcode[port] |= mo;
@ -247,15 +276,15 @@ void input_sdl_handle(u32 port)
{ {
kcode[port] &= ~(mo*2); kcode[port] &= ~(mo*2);
} }
// printf("Mapped to %d %d %d\n",mo,kcode[port]&mo,kcode[port]&(mo*2)); // printf("Mapped to %d %d %d\n",mo,kcode[port]&mo,kcode[port]&(mo*2));
} }
else if (mt == 1) else if (mt == 1)
{ {
if (v >= 0) v++; //up to 255 if (v >= 0) v++; //up to 255
// printf("AXIS %d,%d Mapped to %d %d %d\n",JE.number,JE.value,mo,v,v+127); // printf("AXIS %d,%d Mapped to %d %d %d\n",JE.number,JE.value,mo,v,v+127);
if (mo == 0) if (mo == 0)
{ {
lt[port] = v + 127; lt[port] = v + 127;
@ -282,7 +311,7 @@ void input_sdl_handle(u32 port)
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
xx = event.motion.xrel; xx = event.motion.xrel;
yy = event.motion.yrel; yy = event.motion.yrel;
// some caping and dead zone... // some caping and dead zone...
if (abs(xx) < 4) if (abs(xx) < 4)
{ {
@ -292,10 +321,10 @@ void input_sdl_handle(u32 port)
{ {
yy = 0; yy = 0;
} }
xx = xx * 255 / 20; xx = xx * 255 / 20;
yy = yy * 255 / 20; yy = yy * 255 / 20;
if (xx > 255) if (xx > 255)
{ {
xx = 255; xx = 255;
@ -304,7 +333,7 @@ void input_sdl_handle(u32 port)
{ {
xx = -255; xx = -255;
} }
if (yy > 255) if (yy > 255)
{ {
yy = 255; yy = 255;
@ -313,7 +342,7 @@ void input_sdl_handle(u32 port)
{ {
yy = -255; yy = -255;
} }
//if (abs(xx)>0 || abs(yy)>0) printf("mouse %i, %i\n", xx, yy); //if (abs(xx)>0 || abs(yy)>0) printf("mouse %i, %i\n", xx, yy);
switch (mouse_use) switch (mouse_use)
{ {
@ -361,29 +390,6 @@ void input_sdl_handle(u32 port)
break; break;
} }
} }
if (keys[0]) { kcode[port] &= ~DC_BTN_C; }
if (keys[6]) { kcode[port] &= ~DC_BTN_A; }
if (keys[7]) { kcode[port] &= ~DC_BTN_B; }
if (keys[5]) { kcode[port] &= ~DC_BTN_Y; }
if (keys[8]) { kcode[port] &= ~DC_BTN_X; }
if (keys[1]) { kcode[port] &= ~DC_DPAD_UP; }
if (keys[2]) { kcode[port] &= ~DC_DPAD_DOWN; }
if (keys[3]) { kcode[port] &= ~DC_DPAD_LEFT; }
if (keys[4]) { kcode[port] &= ~DC_DPAD_RIGHT; }
if (keys[12]){ kcode[port] &= ~DC_BTN_START; }
if (keys[9])
{
dc_stop();
}
if (keys[10])
{
rt[port] = 255;
}
if (keys[11])
{
lt[port] = 255;
}
} }
void sdl_window_set_text(const char* text) void sdl_window_set_text(const char* text)