mirror of
https://github.com/Genymobile/scrcpy.git
synced 2025-12-17 05:24:19 +01:00
Move precondition checks for input events
Always call the appropriate method responsible for handling the input event, which can then decide to do nothing.
This commit is contained in:
@@ -313,6 +313,10 @@ apply_orientation_transform(struct sc_input_manager *im,
|
|||||||
static void
|
static void
|
||||||
sc_input_manager_process_text_input(struct sc_input_manager *im,
|
sc_input_manager_process_text_input(struct sc_input_manager *im,
|
||||||
const SDL_TextInputEvent *event) {
|
const SDL_TextInputEvent *event) {
|
||||||
|
if (!im->kp || im->screen->paused) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!im->kp->ops->process_text) {
|
if (!im->kp->ops->process_text) {
|
||||||
// The key processor does not support text input
|
// The key processor does not support text input
|
||||||
return;
|
return;
|
||||||
@@ -370,6 +374,9 @@ inverse_point(struct sc_point point, struct sc_size size,
|
|||||||
static void
|
static void
|
||||||
sc_input_manager_process_key(struct sc_input_manager *im,
|
sc_input_manager_process_key(struct sc_input_manager *im,
|
||||||
const SDL_KeyboardEvent *event) {
|
const SDL_KeyboardEvent *event) {
|
||||||
|
// some key events do not interact with the device, so process the event
|
||||||
|
// even if control is disabled
|
||||||
|
|
||||||
// controller is NULL if --no-control is requested
|
// controller is NULL if --no-control is requested
|
||||||
bool control = im->controller;
|
bool control = im->controller;
|
||||||
bool paused = im->screen->paused;
|
bool paused = im->screen->paused;
|
||||||
@@ -633,6 +640,10 @@ sc_input_manager_get_position(struct sc_input_manager *im, int32_t x,
|
|||||||
static void
|
static void
|
||||||
sc_input_manager_process_mouse_motion(struct sc_input_manager *im,
|
sc_input_manager_process_mouse_motion(struct sc_input_manager *im,
|
||||||
const SDL_MouseMotionEvent *event) {
|
const SDL_MouseMotionEvent *event) {
|
||||||
|
if (!im->mp || im->screen->paused) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (event->which == SDL_TOUCH_MOUSEID) {
|
if (event->which == SDL_TOUCH_MOUSEID) {
|
||||||
// simulated from touch events, so it's a duplicate
|
// simulated from touch events, so it's a duplicate
|
||||||
return;
|
return;
|
||||||
@@ -668,6 +679,10 @@ sc_input_manager_process_mouse_motion(struct sc_input_manager *im,
|
|||||||
static void
|
static void
|
||||||
sc_input_manager_process_touch(struct sc_input_manager *im,
|
sc_input_manager_process_touch(struct sc_input_manager *im,
|
||||||
const SDL_TouchFingerEvent *event) {
|
const SDL_TouchFingerEvent *event) {
|
||||||
|
if (!im->mp || im->screen->paused) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!im->mp->ops->process_touch) {
|
if (!im->mp->ops->process_touch) {
|
||||||
// The mouse processor does not support touch events
|
// The mouse processor does not support touch events
|
||||||
return;
|
return;
|
||||||
@@ -716,6 +731,9 @@ sc_input_manager_get_binding(const struct sc_mouse_binding_set *bindings,
|
|||||||
static void
|
static void
|
||||||
sc_input_manager_process_mouse_button(struct sc_input_manager *im,
|
sc_input_manager_process_mouse_button(struct sc_input_manager *im,
|
||||||
const SDL_MouseButtonEvent *event) {
|
const SDL_MouseButtonEvent *event) {
|
||||||
|
// some mouse events do not interact with the device, so process the event
|
||||||
|
// even if control is disabled
|
||||||
|
|
||||||
if (event->which == SDL_TOUCH_MOUSEID) {
|
if (event->which == SDL_TOUCH_MOUSEID) {
|
||||||
// simulated from touch events, so it's a duplicate
|
// simulated from touch events, so it's a duplicate
|
||||||
return;
|
return;
|
||||||
@@ -883,6 +901,10 @@ sc_input_manager_process_mouse_button(struct sc_input_manager *im,
|
|||||||
static void
|
static void
|
||||||
sc_input_manager_process_mouse_wheel(struct sc_input_manager *im,
|
sc_input_manager_process_mouse_wheel(struct sc_input_manager *im,
|
||||||
const SDL_MouseWheelEvent *event) {
|
const SDL_MouseWheelEvent *event) {
|
||||||
|
if (!im->kp || im->screen->paused) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!im->mp->ops->process_mouse_scroll) {
|
if (!im->mp->ops->process_mouse_scroll) {
|
||||||
// The mouse processor does not support scroll events
|
// The mouse processor does not support scroll events
|
||||||
return;
|
return;
|
||||||
@@ -907,6 +929,12 @@ sc_input_manager_process_mouse_wheel(struct sc_input_manager *im,
|
|||||||
static void
|
static void
|
||||||
sc_input_manager_process_gamepad_device(struct sc_input_manager *im,
|
sc_input_manager_process_gamepad_device(struct sc_input_manager *im,
|
||||||
const SDL_GamepadDeviceEvent *event) {
|
const SDL_GamepadDeviceEvent *event) {
|
||||||
|
// Handle device added or removed even if paused
|
||||||
|
|
||||||
|
if (!im->gp) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (event->type == SDL_EVENT_GAMEPAD_ADDED) {
|
if (event->type == SDL_EVENT_GAMEPAD_ADDED) {
|
||||||
SDL_Gamepad *gc = SDL_OpenGamepad(event->which);
|
SDL_Gamepad *gc = SDL_OpenGamepad(event->which);
|
||||||
if (!gc) {
|
if (!gc) {
|
||||||
@@ -948,6 +976,10 @@ sc_input_manager_process_gamepad_device(struct sc_input_manager *im,
|
|||||||
static void
|
static void
|
||||||
sc_input_manager_process_gamepad_axis(struct sc_input_manager *im,
|
sc_input_manager_process_gamepad_axis(struct sc_input_manager *im,
|
||||||
const SDL_GamepadAxisEvent *event) {
|
const SDL_GamepadAxisEvent *event) {
|
||||||
|
if (!im->gp || im->screen->paused) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
enum sc_gamepad_axis axis = sc_gamepad_axis_from_sdl(event->axis);
|
enum sc_gamepad_axis axis = sc_gamepad_axis_from_sdl(event->axis);
|
||||||
if (axis == SC_GAMEPAD_AXIS_UNKNOWN) {
|
if (axis == SC_GAMEPAD_AXIS_UNKNOWN) {
|
||||||
return;
|
return;
|
||||||
@@ -964,6 +996,10 @@ sc_input_manager_process_gamepad_axis(struct sc_input_manager *im,
|
|||||||
static void
|
static void
|
||||||
sc_input_manager_process_gamepad_button(struct sc_input_manager *im,
|
sc_input_manager_process_gamepad_button(struct sc_input_manager *im,
|
||||||
const SDL_GamepadButtonEvent *event) {
|
const SDL_GamepadButtonEvent *event) {
|
||||||
|
if (!im->gp || im->screen->paused) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
enum sc_gamepad_button button = sc_gamepad_button_from_sdl(event->button);
|
enum sc_gamepad_button button = sc_gamepad_button_from_sdl(event->button);
|
||||||
if (button == SC_GAMEPAD_BUTTON_UNKNOWN) {
|
if (button == SC_GAMEPAD_BUTTON_UNKNOWN) {
|
||||||
return;
|
return;
|
||||||
@@ -986,6 +1022,10 @@ is_apk(const char *file) {
|
|||||||
static void
|
static void
|
||||||
sc_input_manager_process_file(struct sc_input_manager *im,
|
sc_input_manager_process_file(struct sc_input_manager *im,
|
||||||
const SDL_DropEvent *event) {
|
const SDL_DropEvent *event) {
|
||||||
|
if (!im->controller) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
assert(event->type == SDL_EVENT_DROP_FILE);
|
assert(event->type == SDL_EVENT_DROP_FILE);
|
||||||
char *file = strdup(event->data);
|
char *file = strdup(event->data);
|
||||||
if (!file) {
|
if (!file) {
|
||||||
@@ -1008,72 +1048,41 @@ sc_input_manager_process_file(struct sc_input_manager *im,
|
|||||||
void
|
void
|
||||||
sc_input_manager_handle_event(struct sc_input_manager *im,
|
sc_input_manager_handle_event(struct sc_input_manager *im,
|
||||||
const SDL_Event *event) {
|
const SDL_Event *event) {
|
||||||
bool control = im->controller;
|
|
||||||
bool paused = im->screen->paused;
|
|
||||||
switch (event->type) {
|
switch (event->type) {
|
||||||
case SDL_EVENT_TEXT_INPUT:
|
case SDL_EVENT_TEXT_INPUT:
|
||||||
if (!im->kp || paused) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
sc_input_manager_process_text_input(im, &event->text);
|
sc_input_manager_process_text_input(im, &event->text);
|
||||||
break;
|
break;
|
||||||
case SDL_EVENT_KEY_DOWN:
|
case SDL_EVENT_KEY_DOWN:
|
||||||
case SDL_EVENT_KEY_UP:
|
case SDL_EVENT_KEY_UP:
|
||||||
// some key events do not interact with the device, so process the
|
|
||||||
// event even if control is disabled
|
|
||||||
sc_input_manager_process_key(im, &event->key);
|
sc_input_manager_process_key(im, &event->key);
|
||||||
break;
|
break;
|
||||||
case SDL_EVENT_MOUSE_MOTION:
|
case SDL_EVENT_MOUSE_MOTION:
|
||||||
if (!im->mp || paused) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
sc_input_manager_process_mouse_motion(im, &event->motion);
|
sc_input_manager_process_mouse_motion(im, &event->motion);
|
||||||
break;
|
break;
|
||||||
case SDL_EVENT_MOUSE_WHEEL:
|
case SDL_EVENT_MOUSE_WHEEL:
|
||||||
if (!im->mp || paused) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
sc_input_manager_process_mouse_wheel(im, &event->wheel);
|
sc_input_manager_process_mouse_wheel(im, &event->wheel);
|
||||||
break;
|
break;
|
||||||
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
||||||
case SDL_EVENT_MOUSE_BUTTON_UP:
|
case SDL_EVENT_MOUSE_BUTTON_UP:
|
||||||
// some mouse events do not interact with the device, so process
|
|
||||||
// the event even if control is disabled
|
|
||||||
sc_input_manager_process_mouse_button(im, &event->button);
|
sc_input_manager_process_mouse_button(im, &event->button);
|
||||||
break;
|
break;
|
||||||
case SDL_EVENT_FINGER_MOTION:
|
case SDL_EVENT_FINGER_MOTION:
|
||||||
case SDL_EVENT_FINGER_DOWN:
|
case SDL_EVENT_FINGER_DOWN:
|
||||||
case SDL_EVENT_FINGER_UP:
|
case SDL_EVENT_FINGER_UP:
|
||||||
if (!im->mp || paused) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
sc_input_manager_process_touch(im, &event->tfinger);
|
sc_input_manager_process_touch(im, &event->tfinger);
|
||||||
break;
|
break;
|
||||||
case SDL_EVENT_GAMEPAD_ADDED:
|
case SDL_EVENT_GAMEPAD_ADDED:
|
||||||
case SDL_EVENT_GAMEPAD_REMOVED:
|
case SDL_EVENT_GAMEPAD_REMOVED:
|
||||||
// Handle device added or removed even if paused
|
|
||||||
if (!im->gp) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
sc_input_manager_process_gamepad_device(im, &event->gdevice);
|
sc_input_manager_process_gamepad_device(im, &event->gdevice);
|
||||||
break;
|
break;
|
||||||
case SDL_EVENT_GAMEPAD_AXIS_MOTION:
|
case SDL_EVENT_GAMEPAD_AXIS_MOTION:
|
||||||
if (!im->gp || paused) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
sc_input_manager_process_gamepad_axis(im, &event->gaxis);
|
sc_input_manager_process_gamepad_axis(im, &event->gaxis);
|
||||||
break;
|
break;
|
||||||
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
|
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
|
||||||
case SDL_EVENT_GAMEPAD_BUTTON_UP:
|
case SDL_EVENT_GAMEPAD_BUTTON_UP:
|
||||||
if (!im->gp || paused) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
sc_input_manager_process_gamepad_button(im, &event->gbutton);
|
sc_input_manager_process_gamepad_button(im, &event->gbutton);
|
||||||
break;
|
break;
|
||||||
case SDL_EVENT_DROP_FILE: {
|
case SDL_EVENT_DROP_FILE: {
|
||||||
if (!control) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
sc_input_manager_process_file(im, &event->drop);
|
sc_input_manager_process_file(im, &event->drop);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user