Skip to content

Commit

Permalink
Add UHID gamepad rumble WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
rom1v committed Sep 9, 2024
1 parent 890ee4d commit b587014
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
6 changes: 4 additions & 2 deletions app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ scrcpy(struct scrcpy_options *options) {
#endif

struct sc_keyboard_uhid *uhid_keyboard = NULL;
struct sc_gamepad_uhid *uhid_gamepad = NULL;

if (options->keyboard_input_mode == SC_KEYBOARD_INPUT_MODE_SDK) {
sc_keyboard_sdk_init(&s->keyboard_sdk, &s->controller,
Expand Down Expand Up @@ -755,11 +756,12 @@ scrcpy(struct scrcpy_options *options) {
if (options->gamepad_input_mode == SC_GAMEPAD_INPUT_MODE_UHID) {
sc_gamepad_uhid_init(&s->gamepad_uhid, &s->controller);
gp = &s->gamepad_uhid.gamepad_processor;
uhid_gamepad = &s->gamepad_uhid;
}

struct sc_uhid_devices *uhid_devices = NULL;
if (uhid_keyboard) {
sc_uhid_devices_init(&s->uhid_devices, uhid_keyboard);
if (uhid_keyboard || uhid_gamepad) {
sc_uhid_devices_init(&s->uhid_devices, uhid_keyboard, uhid_gamepad);
uhid_devices = &s->uhid_devices;
}

Expand Down
17 changes: 17 additions & 0 deletions app/src/uhid/gamepad_uhid.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "hid/hid_gamepad.h"
#include "input_events.h"
#include "util/log.h"
#include "util/str.h"

/** Downcast gamepad processor to sc_gamepad_uhid */
#define DOWNCAST(GP) container_of(GP, struct sc_gamepad_uhid, gamepad_processor)
Expand Down Expand Up @@ -106,6 +107,22 @@ sc_gamepad_processor_process_gamepad_button(struct sc_gamepad_processor *gp,

}

void
sc_gamepad_uhid_process_hid_output(struct sc_gamepad_uhid *gamepad,
uint16_t hid_id, const uint8_t *data,
size_t size) {
(void) gamepad;
char *hex = sc_str_to_hex_string(data, size);
if (hex) {
LOGI("==== HID output [%" PRIu16 "] %s", hid_id, hex);
free(hex);
} else {
LOGI("==== HID output [%" PRIu16 "]", hid_id);
}

// TODO
}

void
sc_gamepad_uhid_init(struct sc_gamepad_uhid *gamepad,
struct sc_controller *controller) {
Expand Down
5 changes: 5 additions & 0 deletions app/src/uhid/gamepad_uhid.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ void
sc_gamepad_uhid_init(struct sc_gamepad_uhid *mouse,
struct sc_controller *controller);

void
sc_gamepad_uhid_process_hid_output(struct sc_gamepad_uhid *gamepad,
uint16_t hid_id, const uint8_t *data,
size_t size);

#endif
12 changes: 11 additions & 1 deletion app/src/uhid/uhid_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
#include <inttypes.h>

#include "uhid/keyboard_uhid.h"
#include "uhid/gamepad_uhid.h"
#include "util/log.h"

void
sc_uhid_devices_init(struct sc_uhid_devices *devices,
struct sc_keyboard_uhid *keyboard) {
struct sc_keyboard_uhid *keyboard,
struct sc_gamepad_uhid *gamepad) {
devices->keyboard = keyboard;
devices->gamepad = gamepad;
}

void
Expand All @@ -21,6 +24,13 @@ sc_uhid_devices_process_hid_output(struct sc_uhid_devices *devices, uint16_t id,
} else {
LOGW("Unexpected keyboard HID output without UHID keyboard");
}
} else if (id >= SC_HID_ID_GAMEPAD_FIRST && id <= SC_HID_ID_GAMEPAD_LAST) {
if (devices->gamepad) {
sc_gamepad_uhid_process_hid_output(devices->gamepad, id, data,
size);
} else {
LOGW("Unexpected gamepad HID output without UHID gamepad");
}
} else {
LOGW("HID output ignored for id %" PRIu16, id);
}
Expand Down
4 changes: 3 additions & 1 deletion app/src/uhid/uhid_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

struct sc_uhid_devices {
struct sc_keyboard_uhid *keyboard;
struct sc_gamepad_uhid *gamepad;
};

void
sc_uhid_devices_init(struct sc_uhid_devices *devices,
struct sc_keyboard_uhid *keyboard);
struct sc_keyboard_uhid *keyboard,
struct sc_gamepad_uhid *gamepad);

void
sc_uhid_devices_process_hid_output(struct sc_uhid_devices *devices, uint16_t id,
Expand Down

0 comments on commit b587014

Please sign in to comment.