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/ /.pio/
/.current /.current
/.vscode/

View File

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

View File

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