|
| 1 | +#ifndef CEMU_NSYSHID_BACKEND_H |
| 2 | +#define CEMU_NSYSHID_BACKEND_H |
| 3 | + |
| 4 | +#include <list> |
| 5 | +#include <memory> |
| 6 | +#include <mutex> |
| 7 | + |
| 8 | +#include "Common/precompiled.h" |
| 9 | + |
| 10 | +namespace nsyshid |
| 11 | +{ |
| 12 | + typedef struct |
| 13 | + { |
| 14 | + /* +0x00 */ uint32be handle; |
| 15 | + /* +0x04 */ uint32 ukn04; |
| 16 | + /* +0x08 */ uint16 vendorId; // little-endian ? |
| 17 | + /* +0x0A */ uint16 productId; // little-endian ? |
| 18 | + /* +0x0C */ uint8 ifIndex; |
| 19 | + /* +0x0D */ uint8 subClass; |
| 20 | + /* +0x0E */ uint8 protocol; |
| 21 | + /* +0x0F */ uint8 paddingGuessed0F; |
| 22 | + /* +0x10 */ uint16be maxPacketSizeRX; |
| 23 | + /* +0x12 */ uint16be maxPacketSizeTX; |
| 24 | + } HID_t; |
| 25 | + |
| 26 | + static_assert(offsetof(HID_t, vendorId) == 0x8, ""); |
| 27 | + static_assert(offsetof(HID_t, productId) == 0xA, ""); |
| 28 | + static_assert(offsetof(HID_t, ifIndex) == 0xC, ""); |
| 29 | + static_assert(offsetof(HID_t, protocol) == 0xE, ""); |
| 30 | + |
| 31 | + class Device { |
| 32 | + public: |
| 33 | + Device() = delete; |
| 34 | + |
| 35 | + Device(uint16 vendorId, |
| 36 | + uint16 productId, |
| 37 | + uint8 interfaceIndex, |
| 38 | + uint8 interfaceSubClass, |
| 39 | + uint8 protocol); |
| 40 | + |
| 41 | + Device(const Device& device) = delete; |
| 42 | + |
| 43 | + Device& operator=(const Device& device) = delete; |
| 44 | + |
| 45 | + virtual ~Device() = default; |
| 46 | + |
| 47 | + HID_t* m_hid; // this info is passed to applications and must remain intact |
| 48 | + |
| 49 | + uint16 m_vendorId; |
| 50 | + uint16 m_productId; |
| 51 | + uint8 m_interfaceIndex; |
| 52 | + uint8 m_interfaceSubClass; |
| 53 | + uint8 m_protocol; |
| 54 | + uint16 m_maxPacketSizeRX; |
| 55 | + uint16 m_maxPacketSizeTX; |
| 56 | + |
| 57 | + virtual void AssignHID(HID_t* hid); |
| 58 | + |
| 59 | + virtual bool Open() = 0; |
| 60 | + |
| 61 | + virtual void Close() = 0; |
| 62 | + |
| 63 | + virtual bool IsOpened() = 0; |
| 64 | + |
| 65 | + enum class ReadResult |
| 66 | + { |
| 67 | + Success, |
| 68 | + Error, |
| 69 | + ErrorTimeout, |
| 70 | + }; |
| 71 | + |
| 72 | + virtual ReadResult Read(uint8* data, sint32 length, sint32& bytesRead) = 0; |
| 73 | + |
| 74 | + enum class WriteResult |
| 75 | + { |
| 76 | + Success, |
| 77 | + Error, |
| 78 | + ErrorTimeout, |
| 79 | + }; |
| 80 | + |
| 81 | + virtual WriteResult Write(uint8* data, sint32 length, sint32& bytesWritten) = 0; |
| 82 | + |
| 83 | + virtual bool GetDescriptor(uint8 descType, |
| 84 | + uint8 descIndex, |
| 85 | + uint8 lang, |
| 86 | + uint8* output, |
| 87 | + uint32 outputMaxLength) = 0; |
| 88 | + |
| 89 | + virtual bool SetProtocol(uint32 ifIndef, uint32 protocol) = 0; |
| 90 | + |
| 91 | + virtual bool SetReport(uint8* reportData, sint32 length, uint8* originalData, sint32 originalLength) = 0; |
| 92 | + }; |
| 93 | + |
| 94 | + class Backend { |
| 95 | + public: |
| 96 | + Backend(); |
| 97 | + |
| 98 | + Backend(const Backend& backend) = delete; |
| 99 | + |
| 100 | + Backend& operator=(const Backend& backend) = delete; |
| 101 | + |
| 102 | + virtual ~Backend() = default; |
| 103 | + |
| 104 | + void DetachAllDevices(); |
| 105 | + |
| 106 | + // called from nsyshid when this backend is attached - do not call this yourself! |
| 107 | + void OnAttach(); |
| 108 | + |
| 109 | + // called from nsyshid when this backend is detached - do not call this yourself! |
| 110 | + void OnDetach(); |
| 111 | + |
| 112 | + bool IsBackendAttached(); |
| 113 | + |
| 114 | + virtual bool IsInitialisedOk() = 0; |
| 115 | + |
| 116 | + protected: |
| 117 | + // try to attach a device - only works if this backend is attached |
| 118 | + bool AttachDevice(const std::shared_ptr<Device>& device); |
| 119 | + |
| 120 | + void DetachDevice(const std::shared_ptr<Device>& device); |
| 121 | + |
| 122 | + std::shared_ptr<Device> FindDevice(std::function<bool(const std::shared_ptr<Device>&)> isWantedDevice); |
| 123 | + |
| 124 | + bool IsDeviceWhitelisted(uint16 vendorId, uint16 productId); |
| 125 | + |
| 126 | + // called from OnAttach() - attach devices that your backend can see here |
| 127 | + virtual void AttachVisibleDevices() = 0; |
| 128 | + |
| 129 | + private: |
| 130 | + std::list<std::shared_ptr<Device>> m_devices; |
| 131 | + std::recursive_mutex m_devicesMutex; |
| 132 | + bool m_isAttached; |
| 133 | + }; |
| 134 | + |
| 135 | + namespace backend |
| 136 | + { |
| 137 | + void AttachDefaultBackends(); |
| 138 | + } |
| 139 | +} // namespace nsyshid |
| 140 | + |
| 141 | +#endif // CEMU_NSYSHID_BACKEND_H |
0 commit comments