Accept disabled keyboard or mouse

The input manager assumed that if a controller was present, then both a
key processor and a mouse processor were present.

Remove this assumption, to support disabling keyboard and mouse
separately. This prepares the introduction of new command line options
--keyboard and --mouse.

PR #4473 <https://github.com/Genymobile/scrcpy/pull/4473>
uhid.38
Romain Vimont 4 months ago
parent c0a1aee8ce
commit 35add3daee

@ -52,8 +52,11 @@ is_shortcut_mod(struct sc_input_manager *im, uint16_t sdl_mod) {
void void
sc_input_manager_init(struct sc_input_manager *im, sc_input_manager_init(struct sc_input_manager *im,
const struct sc_input_manager_params *params) { const struct sc_input_manager_params *params) {
assert(!params->controller || (params->kp && params->kp->ops)); // A key/mouse processor may not be present if there is no controller
assert(!params->controller || (params->mp && params->mp->ops)); assert((!params->kp && !params->mp) || params->controller);
// A processor must have ops initialized
assert(!params->kp || params->kp->ops);
assert(!params->mp || params->mp->ops);
im->controller = params->controller; im->controller = params->controller;
im->fp = params->fp; im->fp = params->fp;
@ -89,7 +92,7 @@ sc_input_manager_init(struct sc_input_manager *im,
static void static void
send_keycode(struct sc_input_manager *im, enum android_keycode keycode, send_keycode(struct sc_input_manager *im, enum android_keycode keycode,
enum sc_action action, const char *name) { enum sc_action action, const char *name) {
assert(im->controller); assert(im->controller && im->kp);
// send DOWN event // send DOWN event
struct sc_control_msg msg; struct sc_control_msg msg;
@ -146,7 +149,7 @@ action_menu(struct sc_input_manager *im, enum sc_action action) {
static void static void
press_back_or_turn_screen_on(struct sc_input_manager *im, press_back_or_turn_screen_on(struct sc_input_manager *im,
enum sc_action action) { enum sc_action action) {
assert(im->controller); assert(im->controller && im->kp);
struct sc_control_msg msg; struct sc_control_msg msg;
msg.type = SC_CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON; msg.type = SC_CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON;
@ -197,7 +200,7 @@ collapse_panels(struct sc_input_manager *im) {
static bool static bool
get_device_clipboard(struct sc_input_manager *im, enum sc_copy_key copy_key) { get_device_clipboard(struct sc_input_manager *im, enum sc_copy_key copy_key) {
assert(im->controller); assert(im->controller && im->kp);
struct sc_control_msg msg; struct sc_control_msg msg;
msg.type = SC_CONTROL_MSG_TYPE_GET_CLIPBOARD; msg.type = SC_CONTROL_MSG_TYPE_GET_CLIPBOARD;
@ -214,7 +217,7 @@ get_device_clipboard(struct sc_input_manager *im, enum sc_copy_key copy_key) {
static bool static bool
set_device_clipboard(struct sc_input_manager *im, bool paste, set_device_clipboard(struct sc_input_manager *im, bool paste,
uint64_t sequence) { uint64_t sequence) {
assert(im->controller); assert(im->controller && im->kp);
char *text = SDL_GetClipboardText(); char *text = SDL_GetClipboardText();
if (!text) { if (!text) {
@ -274,7 +277,7 @@ switch_fps_counter_state(struct sc_input_manager *im) {
static void static void
clipboard_paste(struct sc_input_manager *im) { clipboard_paste(struct sc_input_manager *im) {
assert(im->controller); assert(im->controller && im->kp);
char *text = SDL_GetClipboardText(); char *text = SDL_GetClipboardText();
if (!text) { if (!text) {
@ -412,28 +415,28 @@ sc_input_manager_process_key(struct sc_input_manager *im,
enum sc_action action = down ? SC_ACTION_DOWN : SC_ACTION_UP; enum sc_action action = down ? SC_ACTION_DOWN : SC_ACTION_UP;
switch (keycode) { switch (keycode) {
case SDLK_h: case SDLK_h:
if (control && !shift && !repeat) { if (im->kp && !shift && !repeat) {
action_home(im, action); action_home(im, action);
} }
return; return;
case SDLK_b: // fall-through case SDLK_b: // fall-through
case SDLK_BACKSPACE: case SDLK_BACKSPACE:
if (control && !shift && !repeat) { if (im->kp && !shift && !repeat) {
action_back(im, action); action_back(im, action);
} }
return; return;
case SDLK_s: case SDLK_s:
if (control && !shift && !repeat) { if (im->kp && !shift && !repeat) {
action_app_switch(im, action); action_app_switch(im, action);
} }
return; return;
case SDLK_m: case SDLK_m:
if (control && !shift && !repeat) { if (im->kp && !shift && !repeat) {
action_menu(im, action); action_menu(im, action);
} }
return; return;
case SDLK_p: case SDLK_p:
if (control && !shift && !repeat) { if (im->kp && !shift && !repeat) {
action_power(im, action); action_power(im, action);
} }
return; return;
@ -451,7 +454,7 @@ sc_input_manager_process_key(struct sc_input_manager *im,
apply_orientation_transform(im, apply_orientation_transform(im,
SC_ORIENTATION_FLIP_180); SC_ORIENTATION_FLIP_180);
} }
} else if (control) { } else if (im->kp) {
// forward repeated events // forward repeated events
action_volume_down(im, action); action_volume_down(im, action);
} }
@ -462,7 +465,7 @@ sc_input_manager_process_key(struct sc_input_manager *im,
apply_orientation_transform(im, apply_orientation_transform(im,
SC_ORIENTATION_FLIP_180); SC_ORIENTATION_FLIP_180);
} }
} else if (control) { } else if (im->kp) {
// forward repeated events // forward repeated events
action_volume_up(im, action); action_volume_up(im, action);
} }
@ -490,17 +493,17 @@ sc_input_manager_process_key(struct sc_input_manager *im,
} }
return; return;
case SDLK_c: case SDLK_c:
if (control && !shift && !repeat && down) { if (im->kp && !shift && !repeat && down) {
get_device_clipboard(im, SC_COPY_KEY_COPY); get_device_clipboard(im, SC_COPY_KEY_COPY);
} }
return; return;
case SDLK_x: case SDLK_x:
if (control && !shift && !repeat && down) { if (im->kp && !shift && !repeat && down) {
get_device_clipboard(im, SC_COPY_KEY_CUT); get_device_clipboard(im, SC_COPY_KEY_CUT);
} }
return; return;
case SDLK_v: case SDLK_v:
if (control && !repeat && down) { if (im->kp && !repeat && down) {
if (shift || im->legacy_paste) { if (shift || im->legacy_paste) {
// inject the text as input events // inject the text as input events
clipboard_paste(im); clipboard_paste(im);
@ -552,7 +555,7 @@ sc_input_manager_process_key(struct sc_input_manager *im,
return; return;
} }
if (!control) { if (!im->kp) {
return; return;
} }
@ -685,7 +688,7 @@ sc_input_manager_process_mouse_button(struct sc_input_manager *im,
if (control) { if (control) {
enum sc_action action = down ? SC_ACTION_DOWN : SC_ACTION_UP; enum sc_action action = down ? SC_ACTION_DOWN : SC_ACTION_UP;
if (event->button == SDL_BUTTON_X1) { if (im->kp && event->button == SDL_BUTTON_X1) {
action_app_switch(im, action); action_app_switch(im, action);
return; return;
} }
@ -697,11 +700,11 @@ sc_input_manager_process_mouse_button(struct sc_input_manager *im,
} }
return; return;
} }
if (event->button == SDL_BUTTON_RIGHT) { if (im->kp && event->button == SDL_BUTTON_RIGHT) {
press_back_or_turn_screen_on(im, action); press_back_or_turn_screen_on(im, action);
return; return;
} }
if (event->button == SDL_BUTTON_MIDDLE) { if (im->kp && event->button == SDL_BUTTON_MIDDLE) {
action_home(im, action); action_home(im, action);
return; return;
} }
@ -725,7 +728,7 @@ sc_input_manager_process_mouse_button(struct sc_input_manager *im,
// otherwise, send the click event to the device // otherwise, send the click event to the device
} }
if (!control) { if (!im->mp) {
return; return;
} }
@ -865,7 +868,7 @@ sc_input_manager_handle_event(struct sc_input_manager *im,
bool control = im->controller; bool control = im->controller;
switch (event->type) { switch (event->type) {
case SDL_TEXTINPUT: case SDL_TEXTINPUT:
if (!control) { if (!im->kp) {
break; break;
} }
sc_input_manager_process_text_input(im, &event->text); sc_input_manager_process_text_input(im, &event->text);
@ -877,13 +880,13 @@ sc_input_manager_handle_event(struct sc_input_manager *im,
sc_input_manager_process_key(im, &event->key); sc_input_manager_process_key(im, &event->key);
break; break;
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
if (!control) { if (!im->mp) {
break; break;
} }
sc_input_manager_process_mouse_motion(im, &event->motion); sc_input_manager_process_mouse_motion(im, &event->motion);
break; break;
case SDL_MOUSEWHEEL: case SDL_MOUSEWHEEL:
if (!control) { if (!im->mp) {
break; break;
} }
sc_input_manager_process_mouse_wheel(im, &event->wheel); sc_input_manager_process_mouse_wheel(im, &event->wheel);
@ -897,7 +900,7 @@ sc_input_manager_handle_event(struct sc_input_manager *im,
case SDL_FINGERMOTION: case SDL_FINGERMOTION:
case SDL_FINGERDOWN: case SDL_FINGERDOWN:
case SDL_FINGERUP: case SDL_FINGERUP:
if (!control) { if (!im->mp) {
break; break;
} }
sc_input_manager_process_touch(im, &event->tfinger); sc_input_manager_process_touch(im, &event->tfinger);

Loading…
Cancel
Save