From 000ced9ba8ec544667ff0e526d7bc8ea0d84c1ec Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Thu, 15 Feb 2018 12:07:47 +0100 Subject: [PATCH] Refactor screencontrol to inputmanager The "screen control" handled user input, which happened to be only used to control the screen. The controller and screen were passed to every function. Instead, group them in a struct input_manager. The purpose is to add a new shortcut to enable/disable FPS counter. This feature is not related to "screen control", and will require access to the "frames" instance. --- app/meson.build | 2 +- app/src/{screencontrol.c => inputmanager.c} | 63 ++++++++++----------- app/src/inputmanager.h | 24 ++++++++ app/src/scrcpy.c | 17 ++++-- app/src/screencontrol.h | 25 -------- 5 files changed, 65 insertions(+), 66 deletions(-) rename app/src/{screencontrol.c => inputmanager.c} (69%) create mode 100644 app/src/inputmanager.h delete mode 100644 app/src/screencontrol.h diff --git a/app/meson.build b/app/meson.build index e81130bd..83375ee2 100644 --- a/app/meson.build +++ b/app/meson.build @@ -8,11 +8,11 @@ src = [ 'src/device.c', 'src/fpscounter.c', 'src/frames.c', + 'src/inputmanager.c', 'src/lockutil.c', 'src/netutil.c', 'src/scrcpy.c', 'src/screen.c', - 'src/screencontrol.c', 'src/server.c', 'src/strutil.c', 'src/tinyxpm.c', diff --git a/app/src/screencontrol.c b/app/src/inputmanager.c similarity index 69% rename from app/src/screencontrol.c rename to app/src/inputmanager.c index aca62729..2255d066 100644 --- a/app/src/screencontrol.c +++ b/app/src/inputmanager.c @@ -1,4 +1,4 @@ -#include "screencontrol.h" +#include "inputmanager.h" #include "convert.h" #include "log.h" @@ -78,16 +78,15 @@ static void turn_screen_on(struct controller *controller) { } } -void screencontrol_handle_text_input(struct controller *controller, - struct screen *screen, - const SDL_TextInputEvent *event) { +void input_manager_process_text_input(struct input_manager *input_manager, + const SDL_TextInputEvent *event) { if (is_ctrl_down()) { switch (event->text[0]) { case '+': - action_volume_up(controller); + action_volume_up(input_manager->controller); break; case '-': - action_volume_down(controller); + action_volume_down(input_manager->controller); break; } return; @@ -97,14 +96,13 @@ void screencontrol_handle_text_input(struct controller *controller, control_event.type = CONTROL_EVENT_TYPE_TEXT; strncpy(control_event.text_event.text, event->text, TEXT_MAX_LENGTH); control_event.text_event.text[TEXT_MAX_LENGTH] = '\0'; - if (!controller_push_event(controller, &control_event)) { + if (!controller_push_event(input_manager->controller, &control_event)) { LOGW("Cannot send text event"); } } -void screencontrol_handle_key(struct controller *controller, - struct screen *screen, - const SDL_KeyboardEvent *event) { +void input_manager_process_key(struct input_manager *input_manager, + const SDL_KeyboardEvent *event) { SDL_Keycode keycode = event->keysym.sym; SDL_bool ctrl = event->keysym.mod & (KMOD_LCTRL | KMOD_RCTRL); SDL_bool shift = event->keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT); @@ -124,26 +122,26 @@ void screencontrol_handle_key(struct controller *controller, switch (keycode) { case SDLK_h: - action_home(controller); + action_home(input_manager->controller); return; case SDLK_b: // fall-through case SDLK_BACKSPACE: - action_back(controller); + action_back(input_manager->controller); return; case SDLK_m: - action_app_switch(controller); + action_app_switch(input_manager->controller); return; case SDLK_p: - action_power(controller); + action_power(input_manager->controller); return; case SDLK_f: - screen_switch_fullscreen(screen); + screen_switch_fullscreen(input_manager->screen); return; case SDLK_x: - screen_resize_to_fit(screen); + screen_resize_to_fit(input_manager->screen); return; case SDLK_g: - screen_resize_to_pixel_perfect(screen); + screen_resize_to_pixel_perfect(input_manager->screen); return; } @@ -152,52 +150,49 @@ void screencontrol_handle_key(struct controller *controller, struct control_event control_event; if (input_key_from_sdl_to_android(event, &control_event)) { - if (!controller_push_event(controller, &control_event)) { + if (!controller_push_event(input_manager->controller, &control_event)) { LOGW("Cannot send control event"); } } } -void screencontrol_handle_mouse_motion(struct controller *controller, - struct screen *screen, - const SDL_MouseMotionEvent *event) { +void input_manager_process_mouse_motion(struct input_manager *input_manager, + const SDL_MouseMotionEvent *event) { if (!event->state) { // do not send motion events when no button is pressed return; } struct control_event control_event; - if (mouse_motion_from_sdl_to_android(event, screen->frame_size, &control_event)) { - if (!controller_push_event(controller, &control_event)) { + if (mouse_motion_from_sdl_to_android(event, input_manager->screen->frame_size, &control_event)) { + if (!controller_push_event(input_manager->controller, &control_event)) { LOGW("Cannot send mouse motion event"); } } } -void screencontrol_handle_mouse_button(struct controller *controller, - struct screen *screen, - const SDL_MouseButtonEvent *event) { +void input_manager_process_mouse_button(struct input_manager *input_manager, + const SDL_MouseButtonEvent *event) { if (event->button == SDL_BUTTON_RIGHT && event->type == SDL_MOUSEBUTTONDOWN) { - turn_screen_on(controller); + turn_screen_on(input_manager->controller); return; }; struct control_event control_event; - if (mouse_button_from_sdl_to_android(event, screen->frame_size, &control_event)) { - if (!controller_push_event(controller, &control_event)) { + if (mouse_button_from_sdl_to_android(event, input_manager->screen->frame_size, &control_event)) { + if (!controller_push_event(input_manager->controller, &control_event)) { LOGW("Cannot send mouse button event"); } } } -void screencontrol_handle_mouse_wheel(struct controller *controller, - struct screen *screen, - const SDL_MouseWheelEvent *event) { +void input_manager_process_mouse_wheel(struct input_manager *input_manager, + const SDL_MouseWheelEvent *event) { struct position position = { - .screen_size = screen->frame_size, + .screen_size = input_manager->screen->frame_size, .point = get_mouse_point(), }; struct control_event control_event; if (mouse_wheel_from_sdl_to_android(event, position, &control_event)) { - if (!controller_push_event(controller, &control_event)) { + if (!controller_push_event(input_manager->controller, &control_event)) { LOGW("Cannot send wheel button event"); } } diff --git a/app/src/inputmanager.h b/app/src/inputmanager.h new file mode 100644 index 00000000..0e49c68a --- /dev/null +++ b/app/src/inputmanager.h @@ -0,0 +1,24 @@ +#ifndef INPUTMANAGER_H +#define INPUTMANAGER_H + +#include "common.h" +#include "controller.h" +#include "screen.h" + +struct input_manager { + struct controller *controller; + struct screen *screen; +}; + +void input_manager_process_text_input(struct input_manager *input_manager, + const SDL_TextInputEvent *event); +void input_manager_process_key(struct input_manager *input_manager, + const SDL_KeyboardEvent *event); +void input_manager_process_mouse_motion(struct input_manager *input_manager, + const SDL_MouseMotionEvent *event); +void input_manager_process_mouse_button(struct input_manager *input_manager, + const SDL_MouseButtonEvent *event); +void input_manager_process_mouse_wheel(struct input_manager *input_manager, + const SDL_MouseWheelEvent *event); + +#endif diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index d55ae897..119181d5 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -16,11 +16,11 @@ #include "events.h" #include "frames.h" #include "fpscounter.h" +#include "inputmanager.h" #include "log.h" #include "lockutil.h" #include "netutil.h" #include "screen.h" -#include "screencontrol.h" #include "server.h" #include "tinyxpm.h" @@ -30,6 +30,11 @@ static struct frames frames; static struct decoder decoder; static struct controller controller; +static struct input_manager input_manager = { + .controller = &controller, + .screen = &screen, +}; + static void event_loop(void) { SDL_Event event; while (SDL_WaitEvent(&event)) { @@ -60,23 +65,23 @@ static void event_loop(void) { } break; case SDL_TEXTINPUT: { - screencontrol_handle_text_input(&controller, &screen, &event.text); + input_manager_process_text_input(&input_manager, &event.text); break; } case SDL_KEYDOWN: case SDL_KEYUP: - screencontrol_handle_key(&controller, &screen, &event.key); + input_manager_process_key(&input_manager, &event.key); break; case SDL_MOUSEMOTION: - screencontrol_handle_mouse_motion(&controller, &screen, &event.motion); + input_manager_process_mouse_motion(&input_manager, &event.motion); break; case SDL_MOUSEWHEEL: { - screencontrol_handle_mouse_wheel(&controller, &screen, &event.wheel); + input_manager_process_mouse_wheel(&input_manager, &event.wheel); break; } case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: { - screencontrol_handle_mouse_button(&controller, &screen, &event.button); + input_manager_process_mouse_button(&input_manager, &event.button); break; } } diff --git a/app/src/screencontrol.h b/app/src/screencontrol.h deleted file mode 100644 index e422db21..00000000 --- a/app/src/screencontrol.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef SCREENCONTROL_H -#define SCREENCONTROL_H - -#include "common.h" -#include "controller.h" -#include "screen.h" - -void screencontrol_handle_text_input(struct controller *controller, - struct screen *screen, - const SDL_TextInputEvent *event); -void screencontrol_handle_key(struct controller *controller, - struct screen *screen, - const SDL_KeyboardEvent *event); -void screencontrol_handle_mouse_motion(struct controller *controller, - struct screen *screen, - const SDL_MouseMotionEvent *event); -void screencontrol_handle_mouse_button(struct controller *controller, - struct screen *screen, - const SDL_MouseButtonEvent *event); -void screencontrol_handle_mouse_wheel(struct controller *controller, - struct screen *screen, - const SDL_MouseWheelEvent *event); - - -#endif