Skip to content

Commit

Permalink
Support MOD keys in HID mode
Browse files Browse the repository at this point in the history
  • Loading branch information
AlynxZhou committed Sep 10, 2021
1 parent b97ea03 commit 18bda29
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 36 deletions.
6 changes: 3 additions & 3 deletions app/src/hid_keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extern const unsigned char REPORT_DESC[REPORT_DESC_SIZE];
#define HID_MEDIA_REPORT_ID 0x02
/**
* Media keys handle as mask so we define them here.
* FIXME: Some functions already exists on keyboard, maybe media key is useless?
* Currently not used because desktop environment catches them before window.
*/
#define HID_MEDIA_KEY_UNDEFINED 0x00
#define HID_MEDIA_KEY_NEXT (1 << 0)
Expand All @@ -44,8 +44,8 @@ extern const unsigned char REPORT_DESC[REPORT_DESC_SIZE];
#define HID_MEDIA_KEY_EJECT (1 << 3)
#define HID_MEDIA_KEY_PLAY_PAUSE (1 << 4)
#define HID_MEDIA_KEY_MUTE (1 << 5)
#define HID_MEDIA_VOLUME_UP (1 << 6)
#define HID_MEDIA_VOLUME_DOWN (1 << 7)
#define HID_MEDIA_KEY_VOLUME_UP (1 << 6)
#define HID_MEDIA_KEY_VOLUME_DOWN (1 << 7)

void hid_keyboard_init(void);
void hid_keyboard_update_state(const SDL_KeyboardEvent *event);
Expand Down
62 changes: 29 additions & 33 deletions app/src/input_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,29 @@ convert_input_key(const SDL_KeyboardEvent *from, struct control_msg *to,
static void
input_manager_process_key_inject(struct input_manager *im,
const SDL_KeyboardEvent *event) {
struct controller *controller = im->controller;
struct control_msg msg;
if (convert_input_key(event, &msg, im->prefer_text, im->repeat)) {
if (!controller_push_msg(controller, &msg)) {
LOGW("Could not request 'inject keycode'");
}
}
}

static void
input_manager_process_key_hid(struct input_manager *im,
const SDL_KeyboardEvent *event) {
hid_keyboard_update_state(event);
const unsigned char *hid_event = hid_keyboard_get_hid_event();
if (aoa_send_hid_event(im->usb_handle, hid_event,
HID_KEYBOARD_KEY_LENGTH) < 0) {
LOGW("Could not send HID event");
}
}

static void
input_manager_process_key(struct input_manager *im,
const SDL_KeyboardEvent *event) {
// control: indicates the state of the command-line option --no-control
bool control = im->control;

Expand Down Expand Up @@ -562,15 +585,19 @@ input_manager_process_key_inject(struct input_manager *im,
}

if (event->repeat) {
if (!im->forward_key_repeat) {
// In USB HID protocol, key repeat is handle by host (Android in this case),
// so just ignore key repeat here.
if (!im->forward_key_repeat || im->use_hid_over_aoa) {
return;
}
++im->repeat;
} else {
im->repeat = 0;
}

if (ctrl && !shift && keycode == SDLK_v && down && !repeat) {
// Ctrl+v works as paste in HID mode, I'd prefer not to modify it.

This comment has been minimized.

Copy link
@rom1v

rom1v Sep 10, 2021

It also works in InputManager injection mode.

What is important here is to synchronize the computer clipboard to the device clipboard before injecting Ctrl+v (set_device_clipboard() just below).

This comment has been minimized.

Copy link
@AlynxZhou

AlynxZhou Sep 10, 2021

Author Owner

If user select aaaa on Android, and then press Ctrl+X, aaaa is now in Android's clipboard. And if user press Ctrl+V, it's expected to bring aaaa back. However if I am not wrong, those codes will use computer's clipboard content to replace aaaa? I think this is not corrent. (But I did not try it, I'll try now.)

This comment has been minimized.

Copy link
@rom1v

rom1v Sep 10, 2021

If user select aaaa on Android, and then press Ctrl+X, aaaa is now in Android's clipboard.

Yes, and in the computer clipboard too.

And if user press Ctrl+V, it's expected to bring aaaa back.

That's what it does, since aaaa is in the computer clipboard.

This comment has been minimized.

Copy link
@AlynxZhou

AlynxZhou Sep 10, 2021

Author Owner

If user select aaaa on Android, and then press Ctrl+X, aaaa is now in Android's clipboard.

Yes, and in the computer clipboard too.

MOD is alt, why Ctrl+C copies to computer board, too? I think only MOD+C can do this.

And if user press Ctrl+V, it's expected to bring aaaa back.

That's what it does, since aaaa is in the computer clipboard.

This comment has been minimized.

Copy link
@rom1v

This comment has been minimized.

Copy link
@AlynxZhou

AlynxZhou Sep 10, 2021

Author Owner

OK, so Ctrl+C is handled on android side, so both HID and inject support it.

Then when I copied some text from computer, and press Ctrl+V on scrcpy, what is expected? I've removed my limitation on this if, but I cannot paste from computer to android, wired...

This comment has been minimized.

Copy link
@rom1v

rom1v Sep 10, 2021

Then when I copied some text from computer, and press Ctrl+V on scrcpy, what is expected?

What is expected is to synchronize the computer clipboard to the device clipboard, then inject Ctrl+v (so it pastes to Android).

Some devices do not behave correctly, that's why an option --legacy-paste had been added later: d5f059c

This comment has been minimized.

Copy link
@AlynxZhou

AlynxZhou Sep 10, 2021

Author Owner

Yea, I read the code, but now it seems not work on HID mode, for example, I have aaaa on Android clipboard, and copy bbbb on my computer, then press Ctrl+V or MOD+V, aaaa is out. I am wired...

This comment has been minimized.

Copy link
@AlynxZhou

AlynxZhou Sep 10, 2021

Author Owner

OK, after some test, now problem is:

  1. If I am in inject mode, copy aaaa on computer, go to android, press Ctrl+v or MOD+v, aaaa goes to my Gboard's clipboard but not in text field.
  2. If I am in HID mode, copy aaaa on computer, go to android, press Ctrl+v or MOD+v, [server] INFO: Device clipboard set is printed, but I cannot see my clipboard and text not in text field.

This comment has been minimized.

Copy link
@rom1v

rom1v Sep 10, 2021

  1. on master?

This comment has been minimized.

Copy link
@AlynxZhou

AlynxZhou Sep 10, 2021

Author Owner
  1. on dev, but I checked the difference, seems all about translation?

This comment has been minimized.

Copy link
@rom1v

rom1v Sep 10, 2021

  1. For me, it works as expected both on dev or master.

This comment has been minimized.

Copy link
@AlynxZhou

AlynxZhou Sep 10, 2021

Author Owner
1. For me, it works as expected both on `dev` or `master`.

So maybe my phone's question? Could you please tell me what's your phone's vendor and your input method?

I want to know whether my code for HID mode works as expect, but I cannot test it because of those problems...if you have time maybe you can checkout my latest commit on dev (which removed !im->use_hid_over_aoa) and to test if it works fine for your phone.

if (!im->use_hid_over_aoa && ctrl && !shift && keycode == SDLK_v && down &&
!repeat) {
if (im->legacy_paste) {
// inject the text as input events
clipboard_paste(controller);
Expand All @@ -581,37 +608,6 @@ input_manager_process_key_inject(struct input_manager *im,
set_device_clipboard(controller, false);
}

struct control_msg msg;
if (convert_input_key(event, &msg, im->prefer_text, im->repeat)) {
if (!controller_push_msg(controller, &msg)) {
LOGW("Could not request 'inject keycode'");
}
}
}

static void
input_manager_process_key_hid(struct input_manager *im,
const SDL_KeyboardEvent *event) {
// Mirror-only mode.
if (!im->control) {
return;
}
// In USB HID protocol, key repeat is handle by host (Android in this case),
// so just ignore key repeat here.
if (event->repeat) {
return;
}
hid_keyboard_update_state(event);
const unsigned char *hid_event = hid_keyboard_get_hid_event();
if (aoa_send_hid_event(im->usb_handle, hid_event,
HID_KEYBOARD_KEY_LENGTH) < 0) {
LOGW("Could not send HID event");
}
}

static void
input_manager_process_key(struct input_manager *im,
const SDL_KeyboardEvent *event) {
if (im->use_hid_over_aoa) {
input_manager_process_key_hid(im, event);
} else {
Expand Down

0 comments on commit 18bda29

Please sign in to comment.