Skip to content

Commit

Permalink
Rename default keyboard implementation to "sdk"
Browse files Browse the repository at this point in the history
Rename {keyboard,mouse}_inject to {keyboard,mouse}_sdk.

All implementations "inject" key events and mouse events, what differs
is the mechanism. For these implementations, the Android SDK API is used
to inject events.

Note that the input mode enum variants were already renamed
(SC_KEYBOARD_INPUT_MODE_SDK and SC_MOUSE_INPUT_MODE_SDK).

PR #4473 <#4473>
  • Loading branch information
rom1v committed Feb 29, 2024
1 parent d952764 commit 2e7f6a6
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 63 deletions.
4 changes: 2 additions & 2 deletions app/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ src = [
'src/fps_counter.c',
'src/frame_buffer.c',
'src/input_manager.c',
'src/keyboard_inject.c',
'src/mouse_inject.c',
'src/keyboard_sdk.c',
'src/mouse_sdk.c',
'src/opengl.c',
'src/options.c',
'src/packet_merger.c',
Expand Down
46 changes: 23 additions & 23 deletions app/src/keyboard_inject.c → app/src/keyboard_sdk.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "keyboard_inject.h"
#include "keyboard_sdk.h"

#include <assert.h>

Expand All @@ -9,8 +9,8 @@
#include "util/intmap.h"
#include "util/log.h"

/** Downcast key processor to sc_keyboard_inject */
#define DOWNCAST(KP) container_of(KP, struct sc_keyboard_inject, key_processor)
/** Downcast key processor to sc_keyboard_sdk */
#define DOWNCAST(KP) container_of(KP, struct sc_keyboard_sdk, key_processor)

static enum android_keyevent_action
convert_keycode_action(enum sc_action action) {
Expand Down Expand Up @@ -271,20 +271,20 @@ sc_key_processor_process_key(struct sc_key_processor *kp,
// is set before injecting Ctrl+v.
(void) ack_to_wait;

struct sc_keyboard_inject *ki = DOWNCAST(kp);
struct sc_keyboard_sdk *kb = DOWNCAST(kp);

if (event->repeat) {
if (!ki->forward_key_repeat) {
if (!kb->forward_key_repeat) {
return;
}
++ki->repeat;
++kb->repeat;
} else {
ki->repeat = 0;
kb->repeat = 0;
}

struct sc_control_msg msg;
if (convert_input_key(event, &msg, ki->key_inject_mode, ki->repeat)) {
if (!sc_controller_push_msg(ki->controller, &msg)) {
if (convert_input_key(event, &msg, kb->key_inject_mode, kb->repeat)) {
if (!sc_controller_push_msg(kb->controller, &msg)) {
LOGW("Could not request 'inject keycode'");
}
}
Expand All @@ -293,14 +293,14 @@ sc_key_processor_process_key(struct sc_key_processor *kp,
static void
sc_key_processor_process_text(struct sc_key_processor *kp,
const struct sc_text_event *event) {
struct sc_keyboard_inject *ki = DOWNCAST(kp);
struct sc_keyboard_sdk *kb = DOWNCAST(kp);

if (ki->key_inject_mode == SC_KEY_INJECT_MODE_RAW) {
if (kb->key_inject_mode == SC_KEY_INJECT_MODE_RAW) {
// Never inject text events
return;
}

if (ki->key_inject_mode == SC_KEY_INJECT_MODE_MIXED) {
if (kb->key_inject_mode == SC_KEY_INJECT_MODE_MIXED) {
char c = event->text[0];
if (isalpha(c) || c == ' ') {
assert(event->text[1] == '\0');
Expand All @@ -316,29 +316,29 @@ sc_key_processor_process_text(struct sc_key_processor *kp,
LOGW("Could not strdup input text");
return;
}
if (!sc_controller_push_msg(ki->controller, &msg)) {
if (!sc_controller_push_msg(kb->controller, &msg)) {
free(msg.inject_text.text);
LOGW("Could not request 'inject text'");
}
}

void
sc_keyboard_inject_init(struct sc_keyboard_inject *ki,
struct sc_controller *controller,
enum sc_key_inject_mode key_inject_mode,
bool forward_key_repeat) {
ki->controller = controller;
ki->key_inject_mode = key_inject_mode;
ki->forward_key_repeat = forward_key_repeat;
sc_keyboard_sdk_init(struct sc_keyboard_sdk *kb,
struct sc_controller *controller,
enum sc_key_inject_mode key_inject_mode,
bool forward_key_repeat) {
kb->controller = controller;
kb->key_inject_mode = key_inject_mode;
kb->forward_key_repeat = forward_key_repeat;

ki->repeat = 0;
kb->repeat = 0;

static const struct sc_key_processor_ops ops = {
.process_key = sc_key_processor_process_key,
.process_text = sc_key_processor_process_text,
};

// Key injection and clipboard synchronization are serialized
ki->key_processor.async_paste = false;
ki->key_processor.ops = &ops;
kb->key_processor.async_paste = false;
kb->key_processor.ops = &ops;
}
14 changes: 7 additions & 7 deletions app/src/keyboard_inject.h → app/src/keyboard_sdk.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef SC_KEYBOARD_INJECT_H
#define SC_KEYBOARD_INJECT_H
#ifndef SC_KEYBOARD_SDK_H
#define SC_KEYBOARD_SDK_H

#include "common.h"

Expand All @@ -9,7 +9,7 @@
#include "options.h"
#include "trait/key_processor.h"

struct sc_keyboard_inject {
struct sc_keyboard_sdk {
struct sc_key_processor key_processor; // key processor trait

struct sc_controller *controller;
Expand All @@ -23,9 +23,9 @@ struct sc_keyboard_inject {
};

void
sc_keyboard_inject_init(struct sc_keyboard_inject *ki,
struct sc_controller *controller,
enum sc_key_inject_mode key_inject_mode,
bool forward_key_repeat);
sc_keyboard_sdk_init(struct sc_keyboard_sdk *kb,
struct sc_controller *controller,
enum sc_key_inject_mode key_inject_mode,
bool forward_key_repeat);

#endif
31 changes: 15 additions & 16 deletions app/src/mouse_inject.c → app/src/mouse_sdk.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "mouse_inject.h"
#include "mouse_sdk.h"

#include <assert.h>

Expand All @@ -9,8 +9,8 @@
#include "util/intmap.h"
#include "util/log.h"

/** Downcast mouse processor to sc_mouse_inject */
#define DOWNCAST(MP) container_of(MP, struct sc_mouse_inject, mouse_processor)
/** Downcast mouse processor to sc_mouse_sdk */
#define DOWNCAST(MP) container_of(MP, struct sc_mouse_sdk, mouse_processor)

static enum android_motionevent_buttons
convert_mouse_buttons(uint32_t state) {
Expand Down Expand Up @@ -63,7 +63,7 @@ sc_mouse_processor_process_mouse_motion(struct sc_mouse_processor *mp,
return;
}

struct sc_mouse_inject *mi = DOWNCAST(mp);
struct sc_mouse_sdk *m = DOWNCAST(mp);

struct sc_control_msg msg = {
.type = SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
Expand All @@ -76,15 +76,15 @@ sc_mouse_processor_process_mouse_motion(struct sc_mouse_processor *mp,
},
};

if (!sc_controller_push_msg(mi->controller, &msg)) {
if (!sc_controller_push_msg(m->controller, &msg)) {
LOGW("Could not request 'inject mouse motion event'");
}
}

static void
sc_mouse_processor_process_mouse_click(struct sc_mouse_processor *mp,
const struct sc_mouse_click_event *event) {
struct sc_mouse_inject *mi = DOWNCAST(mp);
struct sc_mouse_sdk *m = DOWNCAST(mp);

struct sc_control_msg msg = {
.type = SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
Expand All @@ -98,15 +98,15 @@ sc_mouse_processor_process_mouse_click(struct sc_mouse_processor *mp,
},
};

if (!sc_controller_push_msg(mi->controller, &msg)) {
if (!sc_controller_push_msg(m->controller, &msg)) {
LOGW("Could not request 'inject mouse click event'");
}
}

static void
sc_mouse_processor_process_mouse_scroll(struct sc_mouse_processor *mp,
const struct sc_mouse_scroll_event *event) {
struct sc_mouse_inject *mi = DOWNCAST(mp);
struct sc_mouse_sdk *m = DOWNCAST(mp);

struct sc_control_msg msg = {
.type = SC_CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT,
Expand All @@ -118,15 +118,15 @@ sc_mouse_processor_process_mouse_scroll(struct sc_mouse_processor *mp,
},
};

if (!sc_controller_push_msg(mi->controller, &msg)) {
if (!sc_controller_push_msg(m->controller, &msg)) {
LOGW("Could not request 'inject mouse scroll event'");
}
}

static void
sc_mouse_processor_process_touch(struct sc_mouse_processor *mp,
const struct sc_touch_event *event) {
struct sc_mouse_inject *mi = DOWNCAST(mp);
struct sc_mouse_sdk *m = DOWNCAST(mp);

struct sc_control_msg msg = {
.type = SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
Expand All @@ -139,15 +139,14 @@ sc_mouse_processor_process_touch(struct sc_mouse_processor *mp,
},
};

if (!sc_controller_push_msg(mi->controller, &msg)) {
if (!sc_controller_push_msg(m->controller, &msg)) {
LOGW("Could not request 'inject touch event'");
}
}

void
sc_mouse_inject_init(struct sc_mouse_inject *mi,
struct sc_controller *controller) {
mi->controller = controller;
sc_mouse_sdk_init(struct sc_mouse_sdk *m, struct sc_controller *controller) {
m->controller = controller;

static const struct sc_mouse_processor_ops ops = {
.process_mouse_motion = sc_mouse_processor_process_mouse_motion,
Expand All @@ -156,7 +155,7 @@ sc_mouse_inject_init(struct sc_mouse_inject *mi,
.process_touch = sc_mouse_processor_process_touch,
};

mi->mouse_processor.ops = &ops;
m->mouse_processor.ops = &ops;

mi->mouse_processor.relative_mode = false;
m->mouse_processor.relative_mode = false;
}
9 changes: 4 additions & 5 deletions app/src/mouse_inject.h → app/src/mouse_sdk.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef SC_MOUSE_INJECT_H
#define SC_MOUSE_INJECT_H
#ifndef SC_MOUSE_SDK_H
#define SC_MOUSE_SDK_H

#include "common.h"

Expand All @@ -9,14 +9,13 @@
#include "screen.h"
#include "trait/mouse_processor.h"

struct sc_mouse_inject {
struct sc_mouse_sdk {
struct sc_mouse_processor mouse_processor; // mouse processor trait

struct sc_controller *controller;
};

void
sc_mouse_inject_init(struct sc_mouse_inject *mi,
struct sc_controller *controller);
sc_mouse_sdk_init(struct sc_mouse_sdk *m, struct sc_controller *controller);

#endif
20 changes: 10 additions & 10 deletions app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#include "demuxer.h"
#include "events.h"
#include "file_pusher.h"
#include "keyboard_inject.h"
#include "mouse_inject.h"
#include "keyboard_sdk.h"
#include "mouse_sdk.h"
#include "recorder.h"
#include "screen.h"
#include "server.h"
Expand Down Expand Up @@ -63,13 +63,13 @@ struct scrcpy {
struct sc_acksync acksync;
#endif
union {
struct sc_keyboard_inject keyboard_inject;
struct sc_keyboard_sdk keyboard_sdk;
#ifdef HAVE_USB
struct sc_keyboard_aoa keyboard_aoa;
#endif
};
union {
struct sc_mouse_inject mouse_inject;
struct sc_mouse_sdk mouse_sdk;
#ifdef HAVE_USB
struct sc_mouse_aoa mouse_aoa;
#endif
Expand Down Expand Up @@ -651,16 +651,16 @@ scrcpy(struct scrcpy_options *options) {

// keyboard_input_mode may have been reset if HID mode failed
if (options->keyboard_input_mode == SC_KEYBOARD_INPUT_MODE_SDK) {
sc_keyboard_inject_init(&s->keyboard_inject, &s->controller,
options->key_inject_mode,
options->forward_key_repeat);
kp = &s->keyboard_inject.key_processor;
sc_keyboard_sdk_init(&s->keyboard_sdk, &s->controller,
options->key_inject_mode,
options->forward_key_repeat);
kp = &s->keyboard_sdk.key_processor;
}

// mouse_input_mode may have been reset if HID mode failed
if (options->mouse_input_mode == SC_MOUSE_INPUT_MODE_SDK) {
sc_mouse_inject_init(&s->mouse_inject, &s->controller);
mp = &s->mouse_inject.mouse_processor;
sc_mouse_sdk_init(&s->mouse_sdk, &s->controller);
mp = &s->mouse_sdk.mouse_processor;
}

if (!sc_controller_init(&s->controller, s->server.control_socket,
Expand Down

0 comments on commit 2e7f6a6

Please sign in to comment.