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