Skip to content

Commit

Permalink
API: expose device definition instead of hardcoding it
Browse files Browse the repository at this point in the history
  • Loading branch information
ABeltramo committed Mar 15, 2024
1 parent db46f9c commit 84d2f0e
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 72 deletions.
51 changes: 43 additions & 8 deletions include/inputtino/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,24 @@ class VirtualDevice {
virtual ~VirtualDevice() = default;
};

struct DeviceDefinition {
std::string name;
uint16_t vendor_id;
uint16_t product_id;
uint16_t version;

std::string device_phys = "00:11:22:33:44:55";
std::string device_uniq = "00:11:22:33:44:55";
};

/**
* A virtual mouse device
*/
class Mouse : public VirtualDevice {
public:
static Result<Mouse> create();
static Result<Mouse>
create(const DeviceDefinition &device = {
.name = "Wolf mouse virtual device", .vendor_id = 0xAB00, .product_id = 0xAB01, .version = 0xAB00});

Mouse(Mouse &&j) noexcept : _state(nullptr) {
std::swap(j._state, _state);
Expand Down Expand Up @@ -101,7 +113,9 @@ class Mouse : public VirtualDevice {
*/
class Trackpad : public VirtualDevice {
public:
static Result<Trackpad> create();
static Result<Trackpad>
create(const DeviceDefinition &device = {
.name = "Wolf (virtual) touchpad", .vendor_id = 0xAB00, .product_id = 0xAB02, .version = 0xAB00});
Trackpad(Trackpad &&j) noexcept : _state(nullptr) {
std::swap(j._state, _state);
}
Expand Down Expand Up @@ -136,7 +150,9 @@ class Trackpad : public VirtualDevice {
class TouchScreen : public VirtualDevice {

public:
static Result<TouchScreen> create();
static Result<TouchScreen>
create(const DeviceDefinition &device = {
.name = "Wolf (virtual) touchscreen", .vendor_id = 0xAB00, .product_id = 0xAB03, .version = 0xAB00});
TouchScreen(TouchScreen &&j) noexcept : _state(nullptr) {
std::swap(j._state, _state);
}
Expand Down Expand Up @@ -170,7 +186,9 @@ class TouchScreen : public VirtualDevice {
*/
class PenTablet : public VirtualDevice {
public:
static Result<PenTablet> create();
static Result<PenTablet>
create(const DeviceDefinition &device = {
.name = "Wolf (virtual) pen tablet", .vendor_id = 0xAB00, .product_id = 0xAB04, .version = 0xAB00});
PenTablet(PenTablet &&j) : _state(nullptr) {
std::swap(j._state, _state);
}
Expand Down Expand Up @@ -224,7 +242,11 @@ class PenTablet : public VirtualDevice {
*/
class Keyboard : public VirtualDevice {
public:
static Result<Keyboard> create(std::chrono::milliseconds timeout_repress_key = 50ms);
static Result<Keyboard> create(const DeviceDefinition &device = {.name = "Wolf (virtual) keyboard",
.vendor_id = 0xAB00,
.product_id = 0xAB05,
.version = 0xAB00},
std::chrono::milliseconds timeout_repress_key = 50ms);
Keyboard(Keyboard &&j) noexcept : _state(nullptr) {
std::swap(j._state, _state);
}
Expand Down Expand Up @@ -300,7 +322,13 @@ class Joypad : public VirtualDevice {

class XboxOneJoypad : public Joypad {
public:
static Result<XboxOneJoypad> create();
static Result<XboxOneJoypad>
create(const DeviceDefinition &device = {
.name = "Wolf X-Box One (virtual) pad",
// https://github.com/torvalds/linux/blob/master/drivers/input/joystick/xpad.c#L147
.vendor_id = 0x045E,
.product_id = 0x02EA,
.version = 0x0408});
XboxOneJoypad(XboxOneJoypad &&j) noexcept : _state(nullptr) {
std::swap(j._state, _state);
}
Expand All @@ -323,7 +351,12 @@ class XboxOneJoypad : public Joypad {

class SwitchJoypad : public Joypad {
public:
static Result<SwitchJoypad> create();
static Result<SwitchJoypad> create(const DeviceDefinition &device = {
.name = "Wolf Nintendo (virtual) pad",
// https://github.com/torvalds/linux/blob/master/drivers/hid/hid-ids.h#L981
.vendor_id = 0x057e,
.product_id = 0x2009,
.version = 0x8111});
SwitchJoypad(SwitchJoypad &&j) : _state(nullptr) {
std::swap(j._state, _state);
}
Expand All @@ -346,7 +379,9 @@ class SwitchJoypad : public Joypad {

class PS5Joypad : public Joypad {
public:
static Result<PS5Joypad> create();
static Result<PS5Joypad>
create(const DeviceDefinition &device = {
.name = "Wolf DualSense (virtual) pad", .vendor_id = 0x054C, .product_id = 0x0CE6, .version = 0x8111});
PS5Joypad(PS5Joypad &&j) noexcept : _state(nullptr) {
std::swap(j._state, _state);
}
Expand Down
1 change: 1 addition & 0 deletions inputtino
14 changes: 7 additions & 7 deletions src/uhid/joypad_ps5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ PS5Joypad::~PS5Joypad() {
}
}

Result<PS5Joypad> PS5Joypad::create() {
Result<PS5Joypad> PS5Joypad::create(const DeviceDefinition &device) {
auto def = uhid::DeviceDefinition{
.name = "Wolf PS5 Joypad",
.phys = "00:11:22:33:44:55",
.uniq = "00:11:22:33:44:55",
.name = device.name,
.phys = device.device_phys,
.uniq = device.device_uniq,
.bus = BUS_USB,
.vendor = 0x054C,
.product = 0x0CE6,
.version = 0x8111,
.vendor = static_cast<uint32_t>(device.vendor_id),
.product = static_cast<uint32_t>(device.product_id),
.version = static_cast<uint32_t>(device.version),
.country = 0,
.report_description = {&uhid::ps5_rdesc[0], &uhid::ps5_rdesc[0] + sizeof(uhid::ps5_rdesc)}};

Expand Down
16 changes: 7 additions & 9 deletions src/uinput/joypad_nintendo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ std::vector<std::string> SwitchJoypad::get_nodes() const {
return nodes;
}

Result<libevdev_uinput_ptr> create_nintendo_controller() {
Result<libevdev_uinput_ptr> create_nintendo_controller(const DeviceDefinition &device) {
libevdev *dev = libevdev_new();
libevdev_uinput *uidev;

// Nintendo switch pro controller
// https://github.com/torvalds/linux/blob/master/drivers/hid/hid-ids.h#L981
libevdev_set_name(dev, "Wolf Nintendo (virtual) pad");
libevdev_set_id_vendor(dev, 0x057e);
libevdev_set_id_product(dev, 0x2009);
libevdev_set_id_version(dev, 0x8111);
libevdev_set_name(dev, device.name.c_str());
libevdev_set_id_vendor(dev, device.vendor_id);
libevdev_set_id_product(dev, device.product_id);
libevdev_set_id_version(dev, device.version);
libevdev_set_id_bustype(dev, BUS_USB);

libevdev_enable_event_type(dev, EV_KEY);
Expand Down Expand Up @@ -94,8 +92,8 @@ SwitchJoypad::~SwitchJoypad() {
}
}

Result<SwitchJoypad> SwitchJoypad::create() {
auto joy_el = create_nintendo_controller();
Result<SwitchJoypad> SwitchJoypad::create(const DeviceDefinition &device) {
auto joy_el = create_nintendo_controller(device);
if (!joy_el) {
return Error(joy_el.getErrorMessage());
}
Expand Down
16 changes: 7 additions & 9 deletions src/uinput/joypad_xbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ std::vector<std::string> XboxOneJoypad::get_nodes() const {
return nodes;
}

Result<libevdev_uinput_ptr> create_xbox_controller() {
Result<libevdev_uinput_ptr> create_xbox_controller(const DeviceDefinition &device) {
libevdev *dev = libevdev_new();
libevdev_uinput *uidev;

// Xbox one controller
// https://github.com/torvalds/linux/blob/master/drivers/input/joystick/xpad.c#L147
libevdev_set_name(dev, "Wolf X-Box One (virtual) pad");
libevdev_set_id_vendor(dev, 0x045E);
libevdev_set_id_product(dev, 0x02EA);
libevdev_set_id_version(dev, 0x0408);
libevdev_set_name(dev, device.name.c_str());
libevdev_set_id_vendor(dev, device.vendor_id);
libevdev_set_id_product(dev, device.product_id);
libevdev_set_id_version(dev, device.version);
libevdev_set_id_bustype(dev, BUS_USB);

libevdev_enable_event_type(dev, EV_KEY);
Expand Down Expand Up @@ -89,8 +87,8 @@ XboxOneJoypad::~XboxOneJoypad() {
}
}

Result<XboxOneJoypad> XboxOneJoypad::create() {
auto joy_el = create_xbox_controller();
Result<XboxOneJoypad> XboxOneJoypad::create(const DeviceDefinition &device) {
auto joy_el = create_xbox_controller(device);
if (!joy_el) {
return Error(joy_el.getErrorMessage());
}
Expand Down
15 changes: 7 additions & 8 deletions src/uinput/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ std::vector<std::string> Keyboard::get_nodes() const {
return nodes;
}

Result<libevdev_uinput_ptr> create_keyboard() {
Result<libevdev_uinput_ptr> create_keyboard(const DeviceDefinition &device) {
auto dev = libevdev_new();
libevdev_uinput *uidev;

libevdev_set_uniq(dev, "Wolf Keyboard");
libevdev_set_name(dev, "Wolf keyboard virtual device");
libevdev_set_id_vendor(dev, 0xAB00);
libevdev_set_id_product(dev, 0xAB03);
libevdev_set_id_version(dev, 0xAB00);
libevdev_set_name(dev, device.name.c_str());
libevdev_set_id_vendor(dev, device.vendor_id);
libevdev_set_id_product(dev, device.product_id);
libevdev_set_id_version(dev, device.version);
libevdev_set_id_bustype(dev, BUS_USB);

libevdev_enable_event_type(dev, EV_KEY);
Expand Down Expand Up @@ -71,8 +70,8 @@ Keyboard::~Keyboard() {
}
}

Result<Keyboard> Keyboard::create(std::chrono::milliseconds timeout_repress_key) {
auto kb_el = create_keyboard();
Result<Keyboard> Keyboard::create(const DeviceDefinition &device, std::chrono::milliseconds timeout_repress_key) {
auto kb_el = create_keyboard(device);
if (kb_el) {
Keyboard kb;
kb._state->kb = std::move(*kb_el);
Expand Down
14 changes: 7 additions & 7 deletions src/uinput/mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ std::vector<std::string> Mouse::get_nodes() const {
constexpr int ABS_MAX_WIDTH = 19200;
constexpr int ABS_MAX_HEIGHT = 12000;

static Result<libevdev_uinput_ptr> create_mouse() {
static Result<libevdev_uinput_ptr> create_mouse(const DeviceDefinition &device) {
libevdev *dev = libevdev_new();
libevdev_uinput *uidev;

libevdev_set_name(dev, "Wolf mouse virtual device");
libevdev_set_id_vendor(dev, 0xAB00);
libevdev_set_id_product(dev, 0xAB01);
libevdev_set_id_version(dev, 0xAB00);
libevdev_set_name(dev, device.name.c_str());
libevdev_set_id_vendor(dev, device.vendor_id);
libevdev_set_id_product(dev, device.product_id);
libevdev_set_id_version(dev, device.version);
libevdev_set_id_bustype(dev, BUS_USB);

libevdev_enable_event_type(dev, EV_KEY);
Expand Down Expand Up @@ -104,10 +104,10 @@ Mouse::~Mouse() {
}
}

Result<Mouse> Mouse::create() {
Result<Mouse> Mouse::create(const DeviceDefinition &device) {
auto mouse = Mouse();

auto mouse_rel_or_error = create_mouse();
auto mouse_rel_or_error = create_mouse(device);
if (mouse_rel_or_error) {
mouse._state->mouse_rel = std::move(*mouse_rel_or_error);
} else {
Expand Down
15 changes: 7 additions & 8 deletions src/uinput/pentablet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ static constexpr int PRESSURE_MAX = 253;
static constexpr int DISTANCE_MAX = 1024;
static constexpr int RESOLUTION = 28;

Result<libevdev_uinput_ptr> create_tablet() {
Result<libevdev_uinput_ptr> create_tablet(const DeviceDefinition &device) {
libevdev *dev = libevdev_new();
libevdev_uinput *uidev;

libevdev_set_name(dev, "Wolf (virtual) pen tablet");
libevdev_set_id_version(dev, 0xAB00);

libevdev_set_id_product(dev, 0xAB01);
libevdev_set_id_version(dev, 0xAB00);
libevdev_set_name(dev, device.name.c_str());
libevdev_set_id_vendor(dev, device.vendor_id);
libevdev_set_id_product(dev, device.product_id);
libevdev_set_id_version(dev, device.version);
libevdev_set_id_bustype(dev, BUS_USB);

libevdev_enable_event_type(dev, EV_KEY);
Expand Down Expand Up @@ -84,8 +83,8 @@ PenTablet::~PenTablet() {
}
}

Result<PenTablet> PenTablet::create() {
auto tablet = create_tablet();
Result<PenTablet> PenTablet::create(const DeviceDefinition &device) {
auto tablet = create_tablet(device);
if (tablet) {
PenTablet pt;
pt._state->pen_tablet = std::move(*tablet);
Expand Down
15 changes: 7 additions & 8 deletions src/uinput/touchscreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ static constexpr int TOUCH_MAX_Y = 10800;
static constexpr int NUM_FINGERS = 16;
static constexpr int PRESSURE_MAX = 253;

Result<libevdev_uinput_ptr> create_touch_screen() {
Result<libevdev_uinput_ptr> create_touch_screen(const DeviceDefinition &device) {
libevdev *dev = libevdev_new();
libevdev_uinput *uidev;

libevdev_set_name(dev, "Wolf (virtual) touch screen");
libevdev_set_id_version(dev, 0xAB00);

libevdev_set_id_product(dev, 0xAB01);
libevdev_set_id_version(dev, 0xAB00);
libevdev_set_name(dev, device.name.c_str());
libevdev_set_id_vendor(dev, device.vendor_id);
libevdev_set_id_product(dev, device.product_id);
libevdev_set_id_version(dev, device.version);
libevdev_set_id_bustype(dev, BUS_USB);

libevdev_enable_event_type(dev, EV_KEY);
Expand Down Expand Up @@ -80,8 +79,8 @@ TouchScreen::~TouchScreen() {
}
}

Result<TouchScreen> TouchScreen::create() {
auto touch_screen = create_touch_screen();
Result<TouchScreen> TouchScreen::create(const DeviceDefinition &device) {
auto touch_screen = create_touch_screen(device);
if (touch_screen) {
TouchScreen ts;
ts._state->touch_screen = std::move(*touch_screen);
Expand Down
15 changes: 7 additions & 8 deletions src/uinput/trackpad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ static constexpr int TOUCH_MAX_Y = 10800;
static constexpr int NUM_FINGERS = 16; // Apple's touchpads support 16 touches
static constexpr int PRESSURE_MAX = 253;

Result<libevdev_uinput_ptr> create_trackpad() {
Result<libevdev_uinput_ptr> create_trackpad(const DeviceDefinition &device) {
libevdev *dev = libevdev_new();
libevdev_uinput *uidev;

libevdev_set_name(dev, "Wolf (virtual) touchpad");
libevdev_set_id_version(dev, 0xAB00);

libevdev_set_id_product(dev, 0xAB01);
libevdev_set_id_version(dev, 0xAB00);
libevdev_set_name(dev, device.name.c_str());
libevdev_set_id_vendor(dev, device.vendor_id);
libevdev_set_id_product(dev, device.product_id);
libevdev_set_id_version(dev, device.version);
libevdev_set_id_bustype(dev, BUS_USB);

libevdev_enable_event_type(dev, EV_KEY);
Expand Down Expand Up @@ -86,8 +85,8 @@ Trackpad::~Trackpad() {
}
}

Result<Trackpad> Trackpad::create() {
auto trackpad_el = create_trackpad();
Result<Trackpad> Trackpad::create(const DeviceDefinition &device) {
auto trackpad_el = create_trackpad(device);
if (trackpad_el) {
Trackpad trackpad;
trackpad._state->trackpad = std::move(*trackpad_el);
Expand Down

0 comments on commit 84d2f0e

Please sign in to comment.