2012-08-13 17:50:28 +02:00
|
|
|
/*
|
|
|
|
* This software is licensed under the terms of the MIT-License
|
2017-02-11 04:52:08 +01:00
|
|
|
* See COPYING for further information.
|
2012-08-13 17:50:28 +02:00
|
|
|
* ---
|
|
|
|
* 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-11 04:52:08 +01:00
|
|
|
|
2017-02-24 22:58:27 +01:00
|
|
|
bool kbd = flags & EF_Keyboard;
|
|
|
|
bool text = flags & EF_Text;
|
|
|
|
bool menu = flags & EF_Menu;
|
|
|
|
bool game = flags & EF_Game;
|
2017-02-04 03:56:40 +01:00
|
|
|
|
2017-02-04 05:51:00 +01:00
|
|
|
// TODO: rewrite text input handling to properly support multibyte characters and IMEs
|
|
|
|
|
|
|
|
if(text) {
|
|
|
|
if(!SDL_IsTextInputActive()) {
|
|
|
|
SDL_StartTextInput();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(SDL_IsTextInputActive()) {
|
|
|
|
SDL_StopTextInput();
|
|
|
|
}
|
|
|
|
}
|
2017-02-04 03:56:40 +01:00
|
|
|
|
2012-08-13 17:50:28 +02:00
|
|
|
while(SDL_PollEvent(&event)) {
|
2017-02-07 05:26:04 +01:00
|
|
|
SDL_Scancode scan = event.key.keysym.scancode;
|
|
|
|
SDL_Keymod mod = event.key.keysym.mod;
|
2017-02-24 22:58:27 +01:00
|
|
|
bool repeat = event.key.repeat;
|
2017-02-11 04:52:08 +01:00
|
|
|
|
2012-08-13 17:50:28 +02:00
|
|
|
switch(event.type) {
|
|
|
|
case SDL_KEYDOWN:
|
2017-02-15 12:41:53 +01:00
|
|
|
if(text) {
|
|
|
|
if(scan == SDL_SCANCODE_ESCAPE)
|
|
|
|
handler(E_CancelText, 0, arg);
|
|
|
|
else if(scan == SDL_SCANCODE_RETURN)
|
|
|
|
handler(E_SubmitText, 0, arg);
|
|
|
|
else if(scan == SDL_SCANCODE_BACKSPACE)
|
|
|
|
handler(E_CharErased, 0, arg);
|
2017-02-24 22:58:27 +01:00
|
|
|
} else if(!repeat) {
|
2017-02-17 17:03:49 +01:00
|
|
|
if(scan == config_get_int(CONFIG_KEY_SCREENSHOT)) {
|
2017-02-15 12:41:53 +01:00
|
|
|
take_screenshot();
|
|
|
|
break;
|
|
|
|
}
|
2017-02-04 03:56:40 +01:00
|
|
|
|
2017-02-17 17:03:49 +01:00
|
|
|
if((scan == SDL_SCANCODE_RETURN && (mod & KMOD_ALT)) || scan == config_get_int(CONFIG_KEY_FULLSCREEN)) {
|
2017-02-17 19:23:22 +01:00
|
|
|
config_set_int(CONFIG_FULLSCREEN, !config_get_int(CONFIG_FULLSCREEN));
|
2017-02-15 12:41:53 +01:00
|
|
|
break;
|
|
|
|
}
|
2012-08-13 17:50:28 +02:00
|
|
|
}
|
2017-02-04 03:56:40 +01:00
|
|
|
|
2017-02-07 05:26:04 +01:00
|
|
|
if(kbd) {
|
|
|
|
handler(E_KeyDown, scan, arg);
|
|
|
|
}
|
2017-02-11 04:52:08 +01:00
|
|
|
|
2017-02-24 22:58:27 +01:00
|
|
|
if(menu && (!repeat || transition.state == TRANS_IDLE)) {
|
2017-02-17 17:03:49 +01:00
|
|
|
if(scan == config_get_int(CONFIG_KEY_DOWN) || scan == SDL_SCANCODE_DOWN) {
|
2012-08-13 17:50:28 +02:00
|
|
|
handler(E_CursorDown, 0, arg);
|
2017-02-17 17:03:49 +01:00
|
|
|
} else if(scan == config_get_int(CONFIG_KEY_UP) || scan == SDL_SCANCODE_UP) {
|
2012-08-13 17:50:28 +02:00
|
|
|
handler(E_CursorUp, 0, arg);
|
2017-02-17 17:03:49 +01:00
|
|
|
} else if(scan == config_get_int(CONFIG_KEY_RIGHT) || scan == SDL_SCANCODE_RIGHT) {
|
2012-08-13 17:50:28 +02:00
|
|
|
handler(E_CursorRight, 0, arg);
|
2017-02-17 17:03:49 +01:00
|
|
|
} else if(scan == config_get_int(CONFIG_KEY_LEFT) || scan == SDL_SCANCODE_LEFT) {
|
2012-08-13 17:50:28 +02:00
|
|
|
handler(E_CursorLeft, 0, arg);
|
2017-02-17 17:03:49 +01:00
|
|
|
} else if(scan == config_get_int(CONFIG_KEY_SHOT) || scan == SDL_SCANCODE_RETURN) {
|
2012-08-13 17:50:28 +02:00
|
|
|
handler(E_MenuAccept, 0, arg);
|
2017-02-17 17:03:49 +01:00
|
|
|
} else if(scan == config_get_int(CONFIG_KEY_BOMB) || scan == SDL_SCANCODE_ESCAPE) {
|
2012-08-13 17:50:28 +02:00
|
|
|
handler(E_MenuAbort, 0, arg);
|
|
|
|
}
|
|
|
|
}
|
2017-02-11 04:52:08 +01:00
|
|
|
|
2017-02-24 22:58:27 +01:00
|
|
|
if(game && !repeat) {
|
2017-02-17 17:03:49 +01:00
|
|
|
if(scan == config_get_int(CONFIG_KEY_PAUSE) || scan == SDL_SCANCODE_ESCAPE) {
|
2012-08-13 17:50:28 +02:00
|
|
|
handler(E_Pause, 0, arg);
|
2017-02-07 05:26:04 +01:00
|
|
|
} else {
|
2017-02-17 17:03:49 +01:00
|
|
|
int key = config_key_from_scancode(scan);
|
2012-08-13 17:50:28 +02:00
|
|
|
if(key >= 0)
|
|
|
|
handler(E_PlrKeyDown, key, arg);
|
|
|
|
}
|
|
|
|
}
|
2017-02-11 04:52:08 +01:00
|
|
|
|
2012-08-13 17:50:28 +02:00
|
|
|
break;
|
2017-02-11 04:52:08 +01:00
|
|
|
|
2012-08-13 17:50:28 +02:00
|
|
|
case SDL_KEYUP:
|
2017-02-07 05:26:04 +01:00
|
|
|
if(kbd) {
|
|
|
|
handler(E_KeyUp, scan, arg);
|
|
|
|
}
|
2017-02-11 04:52:08 +01:00
|
|
|
|
2017-02-24 22:58:27 +01:00
|
|
|
if(game && !repeat) {
|
2017-02-17 17:03:49 +01:00
|
|
|
int key = config_key_from_scancode(scan);
|
2012-08-13 17:50:28 +02:00
|
|
|
if(key >= 0)
|
|
|
|
handler(E_PlrKeyUp, key, arg);
|
|
|
|
}
|
2017-02-11 04:52:08 +01:00
|
|
|
|
2012-08-13 17:50:28 +02:00
|
|
|
break;
|
2017-02-11 04:52:08 +01:00
|
|
|
|
2017-02-04 05:51:00 +01:00
|
|
|
case SDL_TEXTINPUT: {
|
|
|
|
char *c;
|
|
|
|
|
|
|
|
for(c = event.text.text; *c; ++c) {
|
|
|
|
handler(E_CharTyped, *c, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-02-04 18:17:08 +01:00
|
|
|
case SDL_WINDOWEVENT:
|
|
|
|
switch(event.window.event) {
|
|
|
|
case SDL_WINDOWEVENT_RESIZED:
|
|
|
|
video_resize(event.window.data1, event.window.data2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_WINDOWEVENT_FOCUS_LOST:
|
|
|
|
if(game) {
|
|
|
|
handler(E_Pause, 0, arg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-08-13 17:50:28 +02:00
|
|
|
case SDL_QUIT:
|
|
|
|
exit(0);
|
|
|
|
break;
|
2017-02-11 04:52:08 +01:00
|
|
|
|
2012-08-15 02:41:21 +02:00
|
|
|
default:
|
|
|
|
gamepad_event(&event, handler, flags, arg);
|
|
|
|
break;
|
2012-08-13 17:50:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|