Skip to content

Commit

Permalink
Make -K -M and -G use AOA in OTG mode
Browse files Browse the repository at this point in the history
For convenience, short options were added to select UHID input modes:
 - -K for --keyboard=uhid
 - -M for --mouse=uhid
 - -G for --gamepad=uhid

In OTG mode, UHID is not available, so the short options should select
AOA instead.
  • Loading branch information
rom1v committed Sep 7, 2024
1 parent b0b0590 commit ccc0306
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
8 changes: 4 additions & 4 deletions app/scrcpy.1
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ Start in fullscreen.
Do not attempt to use "adb reverse" to connect to the device.

.TP
.B \-K
Same as \fB\-\-gamepad=uhid\fR.
.B \-G
Same as \fB\-\-gamepad=uhid\fR, or \fB\-\-keyboard=aoa\fR if \fB\-\-otg\fR is set.

.TP
.BI "\-\-gamepad " mode
Expand All @@ -196,7 +196,7 @@ Print this help.

.TP
.B \-K
Same as \fB\-\-keyboard=uhid\fR.
Same as \fB\-\-keyboard=uhid\fR, or \fB\-\-keyboard=aoa\fR if \fB\-\-otg\fR is set.

.TP
.BI "\-\-keyboard " mode
Expand Down Expand Up @@ -261,7 +261,7 @@ Default is 0 (unlimited).

.TP
.B \-M
Same as \fB\-\-mouse=uhid\fR.
Same as \fB\-\-mouse=uhid\fR, or \fB\-\-mouse=aoa\fR if \fB\-\-otg\fR is set.

.TP
.BI "\-\-max\-fps " value
Expand Down
25 changes: 19 additions & 6 deletions app/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ static const struct sc_option options[] = {
},
{
.shortopt = 'G',
.text = "Same as --gamepad=uhid.",
.text = "Same as --gamepad=uhid, or --gamepad=aoa if --otg is set.",
},
{
.longopt_id = OPT_GAMEPAD,
Expand All @@ -397,7 +397,7 @@ static const struct sc_option options[] = {
},
{
.shortopt = 'K',
.text = "Same as --keyboard=uhid.",
.text = "Same as --keyboard=uhid, or --keyboard=aoa if --otg is set.",
},
{
.longopt_id = OPT_KEYBOARD,
Expand Down Expand Up @@ -493,7 +493,7 @@ static const struct sc_option options[] = {
},
{
.shortopt = 'M',
.text = "Same as --mouse=uhid.",
.text = "Same as --mouse=uhid, or --mouse=aoa if --otg is set.",
},
{
.longopt_id = OPT_MAX_FPS,
Expand Down Expand Up @@ -2264,7 +2264,7 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
args->help = true;
break;
case 'K':
opts->keyboard_input_mode = SC_KEYBOARD_INPUT_MODE_UHID;
opts->keyboard_input_mode = SC_KEYBOARD_INPUT_MODE_UHID_OR_AOA;
break;
case OPT_KEYBOARD:
if (!parse_keyboard(optarg, &opts->keyboard_input_mode)) {
Expand All @@ -2286,7 +2286,7 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
}
break;
case 'M':
opts->mouse_input_mode = SC_MOUSE_INPUT_MODE_UHID;
opts->mouse_input_mode = SC_MOUSE_INPUT_MODE_UHID_OR_AOA;
break;
case OPT_MOUSE:
if (!parse_mouse(optarg, &opts->mouse_input_mode)) {
Expand Down Expand Up @@ -2671,7 +2671,7 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
opts->audio_dup = true;
break;
case 'G':
opts->gamepad_input_mode = SC_GAMEPAD_INPUT_MODE_UHID;
opts->gamepad_input_mode = SC_GAMEPAD_INPUT_MODE_UHID_OR_AOA;
break;
case OPT_GAMEPAD:
if (!parse_gamepad(optarg, &opts->gamepad_input_mode)) {
Expand Down Expand Up @@ -2795,7 +2795,12 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
if (opts->keyboard_input_mode == SC_KEYBOARD_INPUT_MODE_AUTO) {
opts->keyboard_input_mode = otg ? SC_KEYBOARD_INPUT_MODE_AOA
: SC_KEYBOARD_INPUT_MODE_SDK;
} else if (opts->keyboard_input_mode
== SC_KEYBOARD_INPUT_MODE_UHID_OR_AOA) {
opts->keyboard_input_mode = otg ? SC_KEYBOARD_INPUT_MODE_AOA
: SC_KEYBOARD_INPUT_MODE_UHID;
}

if (opts->mouse_input_mode == SC_MOUSE_INPUT_MODE_AUTO) {
if (otg) {
opts->mouse_input_mode = SC_MOUSE_INPUT_MODE_AOA;
Expand All @@ -2805,16 +2810,24 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
} else {
opts->mouse_input_mode = SC_MOUSE_INPUT_MODE_SDK;
}
} else if (opts->mouse_input_mode == SC_MOUSE_INPUT_MODE_UHID_OR_AOA) {
opts->mouse_input_mode = otg ? SC_MOUSE_INPUT_MODE_AOA
: SC_MOUSE_INPUT_MODE_SDK;
} else if (opts->mouse_input_mode == SC_MOUSE_INPUT_MODE_SDK
&& !opts->video_playback) {
LOGE("SDK mouse mode requires video playback. Try --mouse=uhid.");
return false;
}

if (opts->gamepad_input_mode == SC_GAMEPAD_INPUT_MODE_AUTO) {
// UHID does not work on all devices (with old Android
// versions), so it cannot be enabled by default
opts->gamepad_input_mode = otg ? SC_GAMEPAD_INPUT_MODE_AOA
: SC_GAMEPAD_INPUT_MODE_DISABLED;
} else if (opts->gamepad_input_mode
== SC_GAMEPAD_INPUT_MODE_UHID_OR_AOA) {
opts->gamepad_input_mode = otg ? SC_GAMEPAD_INPUT_MODE_AOA
: SC_GAMEPAD_INPUT_MODE_UHID;
}
}

Expand Down
3 changes: 3 additions & 0 deletions app/src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ enum sc_lock_video_orientation {

enum sc_keyboard_input_mode {
SC_KEYBOARD_INPUT_MODE_AUTO,
SC_KEYBOARD_INPUT_MODE_UHID_OR_AOA, // normal vs otg mode
SC_KEYBOARD_INPUT_MODE_DISABLED,
SC_KEYBOARD_INPUT_MODE_SDK,
SC_KEYBOARD_INPUT_MODE_UHID,
Expand All @@ -150,6 +151,7 @@ enum sc_keyboard_input_mode {

enum sc_mouse_input_mode {
SC_MOUSE_INPUT_MODE_AUTO,
SC_MOUSE_INPUT_MODE_UHID_OR_AOA, // normal vs otg mode
SC_MOUSE_INPUT_MODE_DISABLED,
SC_MOUSE_INPUT_MODE_SDK,
SC_MOUSE_INPUT_MODE_UHID,
Expand All @@ -158,6 +160,7 @@ enum sc_mouse_input_mode {

enum sc_gamepad_input_mode {
SC_GAMEPAD_INPUT_MODE_AUTO,
SC_GAMEPAD_INPUT_MODE_UHID_OR_AOA, // normal vs otg mode
SC_GAMEPAD_INPUT_MODE_DISABLED,
SC_GAMEPAD_INPUT_MODE_UHID,
SC_GAMEPAD_INPUT_MODE_AOA,
Expand Down

0 comments on commit ccc0306

Please sign in to comment.