2012-08-13 17:50:28 +02:00
|
|
|
/*
|
|
|
|
* This software is licensed under the terms of the MIT-License
|
|
|
|
* See COPYING for further information.
|
|
|
|
* ---
|
|
|
|
* Copyright (C) 2011, Lukas Weber <laochailan@web.de>
|
|
|
|
* Copyright (C) 2012, Alexeyew Andrew <http://akari.thebadasschoobs.org/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "events.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "global.h"
|
|
|
|
#include "video.h"
|
2012-08-15 02:41:21 +02:00
|
|
|
#include "gamepad.h"
|
2012-08-13 17:50:28 +02:00
|
|
|
|
|
|
|
void handle_events(EventHandler handler, EventFlags flags, void *arg) {
|
|
|
|
SDL_Event event;
|
2017-02-04 03:56:40 +01:00
|
|
|
const Uint8 *keys = SDL_GetKeyboardState(NULL);
|
2012-08-13 17:50:28 +02:00
|
|
|
|
|
|
|
int kbd = flags & EF_Keyboard;
|
|
|
|
int text = flags & EF_Text;
|
|
|
|
int menu = flags & EF_Menu;
|
|
|
|
int game = flags & EF_Game;
|
2017-02-04 03:56:40 +01:00
|
|
|
|
|
|
|
// TODO: rewrite text input handling to use SDL2's IME-aware input API
|
|
|
|
// https://wiki.libsdl.org/Tutorials/TextInput
|
|
|
|
|
2012-08-13 17:50:28 +02:00
|
|
|
while(SDL_PollEvent(&event)) {
|
|
|
|
int sym = event.key.keysym.sym;
|
2017-02-04 03:56:40 +01:00
|
|
|
int uni = sym; // event.key.keysym.unicode;
|
2012-08-13 17:50:28 +02:00
|
|
|
|
|
|
|
switch(event.type) {
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
if(sym == tconfig.intval[KEY_SCREENSHOT]) {
|
|
|
|
take_screenshot();
|
|
|
|
break;
|
|
|
|
}
|
2017-02-04 03:56:40 +01:00
|
|
|
|
|
|
|
if((sym == SDLK_RETURN && (keys[SDL_SCANCODE_LALT] || keys[SDL_SCANCODE_RALT])) || sym == tconfig.intval[KEY_FULLSCREEN]) {
|
2012-08-13 17:50:28 +02:00
|
|
|
video_toggle_fullscreen();
|
|
|
|
break;
|
|
|
|
}
|
2017-02-04 03:56:40 +01:00
|
|
|
|
2012-08-13 17:50:28 +02:00
|
|
|
if(kbd)
|
|
|
|
handler(E_KeyDown, sym, arg);
|
|
|
|
|
|
|
|
if(menu) {
|
|
|
|
if(sym == tconfig.intval[KEY_DOWN] || sym == SDLK_DOWN) {
|
|
|
|
handler(E_CursorDown, 0, arg);
|
|
|
|
} else if(sym == tconfig.intval[KEY_UP] || sym == SDLK_UP) {
|
|
|
|
handler(E_CursorUp, 0, arg);
|
|
|
|
} else if(sym == tconfig.intval[KEY_RIGHT] || sym == SDLK_RIGHT) {
|
|
|
|
handler(E_CursorRight, 0, arg);
|
|
|
|
} else if(sym == tconfig.intval[KEY_LEFT] || sym == SDLK_LEFT) {
|
|
|
|
handler(E_CursorLeft, 0, arg);
|
|
|
|
} else if(sym == tconfig.intval[KEY_SHOT] || sym == SDLK_RETURN) {
|
|
|
|
handler(E_MenuAccept, 0, arg);
|
2017-01-28 16:39:25 +01:00
|
|
|
} else if(sym == SDLK_ESCAPE || sym == tconfig.intval[KEY_BOMB]) {
|
2012-08-13 17:50:28 +02:00
|
|
|
handler(E_MenuAbort, 0, arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(game) {
|
|
|
|
if(sym == SDLK_ESCAPE)
|
|
|
|
handler(E_Pause, 0, arg);
|
|
|
|
else {
|
|
|
|
int key = config_sym2key(sym);
|
|
|
|
if(key >= 0)
|
|
|
|
handler(E_PlrKeyDown, key, arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(text) {
|
|
|
|
if(sym == SDLK_ESCAPE)
|
|
|
|
handler(E_CancelText, 0, arg);
|
|
|
|
else if(sym == SDLK_RETURN)
|
|
|
|
handler(E_SubmitText, 0, arg);
|
|
|
|
else if(sym == SDLK_BACKSPACE)
|
|
|
|
handler(E_CharErased, 0, arg);
|
|
|
|
else if(uni && sym != SDLK_TAB) {
|
|
|
|
handler(E_CharTyped, uni, arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_KEYUP:
|
|
|
|
if(kbd)
|
|
|
|
handler(E_KeyUp, sym, arg);
|
|
|
|
|
|
|
|
if(game) {
|
|
|
|
int key = config_sym2key(sym);
|
|
|
|
if(key >= 0)
|
|
|
|
handler(E_PlrKeyUp, key, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_QUIT:
|
|
|
|
exit(0);
|
|
|
|
break;
|
2012-08-15 02:41:21 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
gamepad_event(&event, handler, flags, arg);
|
|
|
|
break;
|
2012-08-13 17:50:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|