add mouse interface (#96)

This commit is contained in:
tomaszduda23 2022-07-11 01:48:47 +09:00 committed by GitHub
parent 50b9bb5950
commit 1f33d92f61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 73 deletions

1
hid/.gitignore vendored
View File

@ -1,2 +1,3 @@
/.pio/
/.current
/.vscode/

View File

@ -28,7 +28,19 @@
namespace DRIVERS {
class Mouse : public Driver {
struct Mouse : public Driver {
using Driver::Driver;
virtual void begin() {}
virtual void clear() {}
virtual void sendButtons(
bool left_select, bool left_state,
bool right_select, bool right_state,
bool middle_select, bool middle_state,
bool up_select, bool up_state,
bool down_select, bool down_state) {}
virtual void sendMove(int x, int y) {}
virtual void sendRelative(int x, int y) {}
virtual void sendWheel(int delta_y) {}
virtual bool isOffline() { return false; }
};
}

View File

@ -49,10 +49,8 @@
// -----------------------------------------------------------------------------
static UsbMouseAbsolute *_usb_mouse_abs = NULL;
static UsbMouseRelative *_usb_mouse_rel = NULL;
static DRIVERS::Keyboard *_kbd = nullptr;
static DRIVERS::Mouse * _mouse = nullptr;
#ifdef HID_DYNAMIC
static bool _reset_required = false;
@ -131,34 +129,24 @@ static void _initOutputs() {
switch (mouse) {
# ifdef HID_WITH_USB
case PROTO::OUTPUTS1::MOUSE::USB_ABS:
_usb_mouse_abs = new UsbMouseAbsolute(DRIVERS::USB_MOUSE_ABSOLUTE);
_mouse = new UsbMouseAbsolute(DRIVERS::USB_MOUSE_ABSOLUTE);
break;
case PROTO::OUTPUTS1::MOUSE::USB_WIN98:
_usb_mouse_abs = new UsbMouseAbsolute(DRIVERS::USB_MOUSE_ABSOLUTE_WIN98);
_mouse = new UsbMouseAbsolute(DRIVERS::USB_MOUSE_ABSOLUTE_WIN98);
break;
case PROTO::OUTPUTS1::MOUSE::USB_REL:
_usb_mouse_rel = new UsbMouseRelative();
_mouse = new UsbMouseRelative();
break;
# endif
default:
_mouse = new DRIVERS::Mouse(DRIVERS::DUMMY);
break;
}
USBDevice.attach();
_kbd->begin();
switch (mouse) {
# ifdef HID_WITH_USB
case PROTO::OUTPUTS1::MOUSE::USB_ABS:
# ifdef HID_WITH_USB_WIN98
case PROTO::OUTPUTS1::MOUSE::USB_WIN98:
# endif
_usb_mouse_abs->begin();
break;
case PROTO::OUTPUTS1::MOUSE::USB_REL:
_usb_mouse_rel->begin();
break;
# endif
}
_mouse->begin();
}
@ -185,11 +173,7 @@ static void _cmdSetConnected(const uint8_t *data) { // 1 byte
static void _cmdClearHid(const uint8_t *_) { // 0 bytes
_kbd->clear();
if (_usb_mouse_abs) {
_usb_mouse_abs->clear();
} else if (_usb_mouse_rel) {
_usb_mouse_rel->clear();
}
_mouse->clear();
}
static void _cmdKeyEvent(const uint8_t *data) { // 2 bytes
@ -200,46 +184,31 @@ static void _cmdMouseButtonEvent(const uint8_t *data) { // 2 bytes
# define MOUSE_PAIR(_state, _button) \
_state & PROTO::CMD::MOUSE::_button::SELECT, \
_state & PROTO::CMD::MOUSE::_button::STATE
# define SEND_BUTTONS(_hid) \
_hid->sendButtons( \
MOUSE_PAIR(data[0], LEFT), \
MOUSE_PAIR(data[0], RIGHT), \
MOUSE_PAIR(data[0], MIDDLE), \
MOUSE_PAIR(data[1], EXTRA_UP), \
MOUSE_PAIR(data[1], EXTRA_DOWN) \
_mouse->sendButtons(
MOUSE_PAIR(data[0], LEFT),
MOUSE_PAIR(data[0], RIGHT),
MOUSE_PAIR(data[0], MIDDLE),
MOUSE_PAIR(data[1], EXTRA_UP),
MOUSE_PAIR(data[1], EXTRA_DOWN)
);
if (_usb_mouse_abs) {
SEND_BUTTONS(_usb_mouse_abs);
} else if (_usb_mouse_rel) {
SEND_BUTTONS(_usb_mouse_rel);
}
# undef SEND_BUTTONS
# undef MOUSE_PAIR
}
static void _cmdMouseMoveEvent(const uint8_t *data) { // 4 bytes
// See /kvmd/apps/otg/hid/keyboard.py for details
if (_usb_mouse_abs) {
_usb_mouse_abs->sendMove(
_mouse->sendMove(
PROTO::merge8_int(data[0], data[1]),
PROTO::merge8_int(data[2], data[3])
);
}
}
static void _cmdMouseRelativeEvent(const uint8_t *data) { // 2 bytes
if (_usb_mouse_rel) {
_usb_mouse_rel->sendRelative(data[0], data[1]);
}
_mouse->sendRelative(data[0], data[1]);
}
static void _cmdMouseWheelEvent(const uint8_t *data) { // 2 bytes
// Y only, X is not supported
if (_usb_mouse_abs) {
_usb_mouse_abs->sendWheel(data[1]);
} else if (_usb_mouse_rel) {
_usb_mouse_rel->sendWheel(data[1]);
}
_mouse->sendWheel(data[1]);
}
static uint8_t _handleRequest(const uint8_t *data) { // 8 bytes
@ -299,16 +268,20 @@ static void _sendResponse(uint8_t code) {
break;
}
}
if (_usb_mouse_abs) {
response[1] |= (_usb_mouse_abs->isOffline() ? PROTO::PONG::MOUSE_OFFLINE : 0);
if (_usb_mouse_abs->getType() == DRIVERS::USB_MOUSE_ABSOLUTE_WIN98) {
response[2] |= PROTO::OUTPUTS1::MOUSE::USB_WIN98;
} else {
response[2] |= PROTO::OUTPUTS1::MOUSE::USB_ABS;
if(_mouse->getType() != DRIVERS::DUMMY) {
response[1] |= (_mouse->isOffline() ? PROTO::PONG::MOUSE_OFFLINE : 0);
switch (_mouse->getType())
{
case DRIVERS::USB_MOUSE_ABSOLUTE_WIN98:
response[2] |= PROTO::OUTPUTS1::MOUSE::USB_WIN98;
break;
case DRIVERS::USB_MOUSE_ABSOLUTE:
response[2] |= PROTO::OUTPUTS1::MOUSE::USB_ABS;
break;
case DRIVERS::USB_MOUSE_RELATIVE:
response[2] |= PROTO::OUTPUTS1::MOUSE::USB_REL;
break;
}
} else if (_usb_mouse_rel) {
response[1] |= (_usb_mouse_rel->isOffline() ? PROTO::PONG::MOUSE_OFFLINE : 0);
response[2] |= PROTO::OUTPUTS1::MOUSE::USB_REL;
} // TODO: ps2
# ifdef AUM
response[3] |= PROTO::OUTPUTS2::CONNECTABLE;

View File

@ -45,7 +45,7 @@ using namespace DRIVERS;
# define CHECK_AUM_USB
# endif
# define CLS_IS_OFFLINE(_hid) \
bool isOffline() { \
bool isOffline() override { \
CHECK_AUM_USB; \
uint8_t ep = _hid.getPluggedEndpoint(); \
uint8_t intr_state = SREG; \
@ -62,14 +62,13 @@ using namespace DRIVERS;
#else
# define CLS_IS_OFFLINE(_hid) \
bool isOffline() { \
bool isOffline() override { \
return false; \
}
# define CHECK_HID_EP
#endif
class UsbKeyboard : public DRIVERS::Keyboard {
public:
UsbKeyboard() : DRIVERS::Keyboard(DRIVERS::USB_KEYBOARD) {}
@ -142,7 +141,7 @@ class UsbKeyboard : public DRIVERS::Keyboard {
bool middle_select, bool middle_state, \
bool up_select, bool up_state, \
bool down_select, bool down_state \
) { \
) override { \
if (left_select) _sendButton(MOUSE_LEFT, left_state); \
if (right_select) _sendButton(MOUSE_RIGHT, right_state); \
if (middle_select) _sendButton(MOUSE_MIDDLE, middle_state); \
@ -154,23 +153,23 @@ class UsbMouseAbsolute : public DRIVERS::Mouse {
public:
UsbMouseAbsolute(DRIVERS::type _type) : Mouse(_type) {}
void begin() {
void begin() override {
_mouse.begin();
_mouse.setWin98FixEnabled(getType() == DRIVERS::USB_MOUSE_ABSOLUTE_WIN98);
}
void clear() {
void clear() override {
_mouse.releaseAll();
}
CLS_SEND_BUTTONS
void sendMove(int x, int y) {
void sendMove(int x, int y) override {
CHECK_HID_EP;
_mouse.moveTo(x, y);
}
void sendWheel(int delta_y) {
void sendWheel(int delta_y) override {
// delta_x is not supported by hid-project now
CHECK_HID_EP;
_mouse.move(0, 0, delta_y);
@ -188,26 +187,26 @@ class UsbMouseAbsolute : public DRIVERS::Mouse {
}
};
class UsbMouseRelative {
class UsbMouseRelative : public DRIVERS::Mouse{
public:
UsbMouseRelative() {}
UsbMouseRelative() : DRIVERS::Mouse(DRIVERS::USB_MOUSE_RELATIVE) {}
void begin() {
void begin() override {
_mouse.begin();
}
void clear() {
void clear() override {
_mouse.releaseAll();
}
CLS_SEND_BUTTONS
void sendRelative(int x, int y) {
void sendRelative(int x, int y) override {
CHECK_HID_EP;
_mouse.move(x, y, 0);
}
void sendWheel(int delta_y) {
void sendWheel(int delta_y) override {
// delta_x is not supported by hid-project now
CHECK_HID_EP;
_mouse.move(0, 0, delta_y);