Extract event processing out of event_loop()

To avoid too many levels of nested blocks, move the event handling logic
in a separate function.
This commit is contained in:
Romain Vimont 2019-03-02 21:49:39 +01:00
parent aeda583a2c
commit 33ccb1368f

View file

@ -68,20 +68,21 @@ is_apk(const char *file) {
return ext && !strcmp(ext, ".apk");
}
static SDL_bool
event_loop(void) {
#ifdef CONTINUOUS_RESIZING_WORKAROUND
SDL_AddEventWatch(event_watcher, NULL);
#endif
SDL_Event event;
while (SDL_WaitEvent(&event)) {
switch (event.type) {
enum event_result {
EVENT_RESULT_CONTINUE,
EVENT_RESULT_STOPPED_BY_USER,
EVENT_RESULT_STOPPED_BY_EOS,
};
static enum event_result
handle_event(SDL_Event *event) {
switch (event->type) {
case EVENT_STREAM_STOPPED:
LOGD("Video stream stopped");
return SDL_FALSE;
return EVENT_RESULT_STOPPED_BY_EOS;
case SDL_QUIT:
LOGD("User requested to quit");
return SDL_TRUE;
return EVENT_RESULT_STOPPED_BY_USER;
case EVENT_NEW_FRAME:
if (!screen.has_frame) {
screen.has_frame = SDL_TRUE;
@ -93,7 +94,7 @@ event_loop(void) {
}
break;
case SDL_WINDOWEVENT:
switch (event.window.event) {
switch (event->window.event) {
case SDL_WINDOWEVENT_EXPOSED:
case SDL_WINDOWEVENT_SIZE_CHANGED:
screen_render(&screen);
@ -101,36 +102,52 @@ event_loop(void) {
}
break;
case SDL_TEXTINPUT:
input_manager_process_text_input(&input_manager, &event.text);
input_manager_process_text_input(&input_manager, &event->text);
break;
case SDL_KEYDOWN:
case SDL_KEYUP:
input_manager_process_key(&input_manager, &event.key);
input_manager_process_key(&input_manager, &event->key);
break;
case SDL_MOUSEMOTION:
input_manager_process_mouse_motion(&input_manager,
&event.motion);
input_manager_process_mouse_motion(&input_manager, &event->motion);
break;
case SDL_MOUSEWHEEL:
input_manager_process_mouse_wheel(&input_manager,
&event.wheel);
input_manager_process_mouse_wheel(&input_manager, &event->wheel);
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
input_manager_process_mouse_button(&input_manager,
&event.button);
input_manager_process_mouse_button(&input_manager, &event->button);
break;
case SDL_DROPFILE: {
file_handler_action_t action;
if (is_apk(event.drop.file)) {
if (is_apk(event->drop.file)) {
action = ACTION_INSTALL_APK;
} else {
action = ACTION_PUSH_FILE;
}
file_handler_request(&file_handler, action, event.drop.file);
file_handler_request(&file_handler, action, event->drop.file);
break;
}
}
return EVENT_RESULT_CONTINUE;
}
static SDL_bool
event_loop(void) {
#ifdef CONTINUOUS_RESIZING_WORKAROUND
SDL_AddEventWatch(event_watcher, NULL);
#endif
SDL_Event event;
while (SDL_WaitEvent(&event)) {
enum event_result result = handle_event(&event);
switch (result) {
case EVENT_RESULT_STOPPED_BY_USER:
return SDL_TRUE;
case EVENT_RESULT_STOPPED_BY_EOS:
return SDL_FALSE;
case EVENT_RESULT_CONTINUE:
break;
}
}
return SDL_FALSE;
}