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.
This commit is contained in:
Romain Vimont 2018-02-15 12:07:47 +01:00
parent fb0e467585
commit 000ced9ba8
5 changed files with 65 additions and 66 deletions

View file

@ -8,11 +8,11 @@ src = [
'src/device.c', 'src/device.c',
'src/fpscounter.c', 'src/fpscounter.c',
'src/frames.c', 'src/frames.c',
'src/inputmanager.c',
'src/lockutil.c', 'src/lockutil.c',
'src/netutil.c', 'src/netutil.c',
'src/scrcpy.c', 'src/scrcpy.c',
'src/screen.c', 'src/screen.c',
'src/screencontrol.c',
'src/server.c', 'src/server.c',
'src/strutil.c', 'src/strutil.c',
'src/tinyxpm.c', 'src/tinyxpm.c',

View file

@ -1,4 +1,4 @@
#include "screencontrol.h" #include "inputmanager.h"
#include "convert.h" #include "convert.h"
#include "log.h" #include "log.h"
@ -78,16 +78,15 @@ static void turn_screen_on(struct controller *controller) {
} }
} }
void screencontrol_handle_text_input(struct controller *controller, void input_manager_process_text_input(struct input_manager *input_manager,
struct screen *screen,
const SDL_TextInputEvent *event) { const SDL_TextInputEvent *event) {
if (is_ctrl_down()) { if (is_ctrl_down()) {
switch (event->text[0]) { switch (event->text[0]) {
case '+': case '+':
action_volume_up(controller); action_volume_up(input_manager->controller);
break; break;
case '-': case '-':
action_volume_down(controller); action_volume_down(input_manager->controller);
break; break;
} }
return; return;
@ -97,13 +96,12 @@ void screencontrol_handle_text_input(struct controller *controller,
control_event.type = CONTROL_EVENT_TYPE_TEXT; control_event.type = CONTROL_EVENT_TYPE_TEXT;
strncpy(control_event.text_event.text, event->text, TEXT_MAX_LENGTH); strncpy(control_event.text_event.text, event->text, TEXT_MAX_LENGTH);
control_event.text_event.text[TEXT_MAX_LENGTH] = '\0'; 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"); LOGW("Cannot send text event");
} }
} }
void screencontrol_handle_key(struct controller *controller, void input_manager_process_key(struct input_manager *input_manager,
struct screen *screen,
const SDL_KeyboardEvent *event) { const SDL_KeyboardEvent *event) {
SDL_Keycode keycode = event->keysym.sym; SDL_Keycode keycode = event->keysym.sym;
SDL_bool ctrl = event->keysym.mod & (KMOD_LCTRL | KMOD_RCTRL); SDL_bool ctrl = event->keysym.mod & (KMOD_LCTRL | KMOD_RCTRL);
@ -124,26 +122,26 @@ void screencontrol_handle_key(struct controller *controller,
switch (keycode) { switch (keycode) {
case SDLK_h: case SDLK_h:
action_home(controller); action_home(input_manager->controller);
return; return;
case SDLK_b: // fall-through case SDLK_b: // fall-through
case SDLK_BACKSPACE: case SDLK_BACKSPACE:
action_back(controller); action_back(input_manager->controller);
return; return;
case SDLK_m: case SDLK_m:
action_app_switch(controller); action_app_switch(input_manager->controller);
return; return;
case SDLK_p: case SDLK_p:
action_power(controller); action_power(input_manager->controller);
return; return;
case SDLK_f: case SDLK_f:
screen_switch_fullscreen(screen); screen_switch_fullscreen(input_manager->screen);
return; return;
case SDLK_x: case SDLK_x:
screen_resize_to_fit(screen); screen_resize_to_fit(input_manager->screen);
return; return;
case SDLK_g: case SDLK_g:
screen_resize_to_pixel_perfect(screen); screen_resize_to_pixel_perfect(input_manager->screen);
return; return;
} }
@ -152,52 +150,49 @@ void screencontrol_handle_key(struct controller *controller,
struct control_event control_event; struct control_event control_event;
if (input_key_from_sdl_to_android(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"); LOGW("Cannot send control event");
} }
} }
} }
void screencontrol_handle_mouse_motion(struct controller *controller, void input_manager_process_mouse_motion(struct input_manager *input_manager,
struct screen *screen,
const SDL_MouseMotionEvent *event) { const SDL_MouseMotionEvent *event) {
if (!event->state) { if (!event->state) {
// do not send motion events when no button is pressed // do not send motion events when no button is pressed
return; return;
} }
struct control_event control_event; struct control_event control_event;
if (mouse_motion_from_sdl_to_android(event, screen->frame_size, &control_event)) { if (mouse_motion_from_sdl_to_android(event, input_manager->screen->frame_size, &control_event)) {
if (!controller_push_event(controller, &control_event)) { if (!controller_push_event(input_manager->controller, &control_event)) {
LOGW("Cannot send mouse motion event"); LOGW("Cannot send mouse motion event");
} }
} }
} }
void screencontrol_handle_mouse_button(struct controller *controller, void input_manager_process_mouse_button(struct input_manager *input_manager,
struct screen *screen,
const SDL_MouseButtonEvent *event) { const SDL_MouseButtonEvent *event) {
if (event->button == SDL_BUTTON_RIGHT && event->type == SDL_MOUSEBUTTONDOWN) { if (event->button == SDL_BUTTON_RIGHT && event->type == SDL_MOUSEBUTTONDOWN) {
turn_screen_on(controller); turn_screen_on(input_manager->controller);
return; return;
}; };
struct control_event control_event; struct control_event control_event;
if (mouse_button_from_sdl_to_android(event, screen->frame_size, &control_event)) { if (mouse_button_from_sdl_to_android(event, input_manager->screen->frame_size, &control_event)) {
if (!controller_push_event(controller, &control_event)) { if (!controller_push_event(input_manager->controller, &control_event)) {
LOGW("Cannot send mouse button event"); LOGW("Cannot send mouse button event");
} }
} }
} }
void screencontrol_handle_mouse_wheel(struct controller *controller, void input_manager_process_mouse_wheel(struct input_manager *input_manager,
struct screen *screen,
const SDL_MouseWheelEvent *event) { const SDL_MouseWheelEvent *event) {
struct position position = { struct position position = {
.screen_size = screen->frame_size, .screen_size = input_manager->screen->frame_size,
.point = get_mouse_point(), .point = get_mouse_point(),
}; };
struct control_event control_event; struct control_event control_event;
if (mouse_wheel_from_sdl_to_android(event, position, &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"); LOGW("Cannot send wheel button event");
} }
} }

24
app/src/inputmanager.h Normal file
View file

@ -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

View file

@ -16,11 +16,11 @@
#include "events.h" #include "events.h"
#include "frames.h" #include "frames.h"
#include "fpscounter.h" #include "fpscounter.h"
#include "inputmanager.h"
#include "log.h" #include "log.h"
#include "lockutil.h" #include "lockutil.h"
#include "netutil.h" #include "netutil.h"
#include "screen.h" #include "screen.h"
#include "screencontrol.h"
#include "server.h" #include "server.h"
#include "tinyxpm.h" #include "tinyxpm.h"
@ -30,6 +30,11 @@ static struct frames frames;
static struct decoder decoder; static struct decoder decoder;
static struct controller controller; static struct controller controller;
static struct input_manager input_manager = {
.controller = &controller,
.screen = &screen,
};
static void event_loop(void) { static void event_loop(void) {
SDL_Event event; SDL_Event event;
while (SDL_WaitEvent(&event)) { while (SDL_WaitEvent(&event)) {
@ -60,23 +65,23 @@ static void event_loop(void) {
} }
break; break;
case SDL_TEXTINPUT: { case SDL_TEXTINPUT: {
screencontrol_handle_text_input(&controller, &screen, &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:
screencontrol_handle_key(&controller, &screen, &event.key); input_manager_process_key(&input_manager, &event.key);
break; break;
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
screencontrol_handle_mouse_motion(&controller, &screen, &event.motion); input_manager_process_mouse_motion(&input_manager, &event.motion);
break; break;
case SDL_MOUSEWHEEL: { case SDL_MOUSEWHEEL: {
screencontrol_handle_mouse_wheel(&controller, &screen, &event.wheel); input_manager_process_mouse_wheel(&input_manager, &event.wheel);
break; break;
} }
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP: { case SDL_MOUSEBUTTONUP: {
screencontrol_handle_mouse_button(&controller, &screen, &event.button); input_manager_process_mouse_button(&input_manager, &event.button);
break; break;
} }
} }

View file

@ -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