Created
May 14, 2017 22:18
-
-
Save TuxSH/48952bdc6aa7e00287b68460e62c5a59 to your computer and use it in GitHub Desktop.
shitty qt inputredirection client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <QWidget> | |
| #include <QApplication> | |
| #include <QDebug> | |
| #include <QGamepadManager> | |
| #include <QGamepad> | |
| #include <QtEndian> | |
| #include <QUdpSocket> | |
| #include <QTimer> | |
| typedef uint32_t u32; | |
| typedef uint16_t u16; | |
| typedef uint8_t u8; | |
| double lx = 0.0, ly = 0.0; | |
| double rx = 0.0, ry = 0.0; | |
| QGamepadManager::GamepadButtons buttons = 0; | |
| struct GamepadMonitor : public QObject { | |
| GamepadMonitor(QObject *parent = Q_NULLPTR) | |
| : QObject(parent) | |
| { | |
| qDebug() << "QGamePadManager Demo\n"; | |
| auto gamepads = QGamepadManager::instance()->connectedGamepads(); | |
| qDebug() << "Number of gamepads:" << gamepads.size(); | |
| for (auto i : gamepads) { | |
| QGamepad *gamepad = new QGamepad(i); | |
| qDebug() << "Gamepad:" << i; | |
| qDebug() << " device id: " << gamepad->deviceId(); | |
| qDebug() << " name: " << gamepad->name(); | |
| qDebug() << " is connected?" << gamepad->isConnected(); | |
| } | |
| qDebug() << "\nMonitoring for events...\n"; | |
| connect(QGamepadManager::instance(), &QGamepadManager::connectedGamepadsChanged, this, | |
| []() { qDebug() << "connected gamepads changed:"; }); | |
| connect(QGamepadManager::instance(), &QGamepadManager::gamepadConnected, this, | |
| [](int deviceId) { qDebug() << "gamepad connected:" << deviceId; }); | |
| connect(QGamepadManager::instance(), &QGamepadManager::gamepadDisconnected, this, | |
| [](int deviceId) { qDebug() << "gamepad disconnected:" << deviceId; }); | |
| connect(QGamepadManager::instance(), &QGamepadManager::gamepadButtonPressEvent, this, | |
| [](int deviceId, QGamepadManager::GamepadButton button, double value) | |
| { | |
| qDebug() << "button press event:" << deviceId << button << value; | |
| buttons |= QGamepadManager::GamepadButtons(1 << button); | |
| }); | |
| connect(QGamepadManager::instance(), &QGamepadManager::gamepadButtonReleaseEvent, this, | |
| [](int deviceId, QGamepadManager::GamepadButton button) | |
| { | |
| qDebug() << "button release event:" << deviceId << button; | |
| buttons &= QGamepadManager::GamepadButtons(~(1 << button)); | |
| }); | |
| connect(QGamepadManager::instance(), &QGamepadManager::gamepadAxisEvent, this, | |
| [](int deviceId, QGamepadManager::GamepadAxis axis, double value) | |
| { | |
| qDebug() << "axis event:" << deviceId << axis << value; | |
| switch(axis) | |
| { | |
| case QGamepadManager::AxisLeftX: | |
| lx = value; | |
| break; | |
| case QGamepadManager::AxisLeftY: | |
| ly = -value; // for some reason qt inverts this | |
| break; | |
| default: break; | |
| } | |
| }); | |
| connect(QGamepadManager::instance(), &QGamepadManager::buttonConfigured, this, | |
| [](int deviceId, QGamepadManager::GamepadButton button) { qDebug() << "button configured:" << deviceId << button; }); | |
| connect(QGamepadManager::instance(), &QGamepadManager::axisConfigured, this, | |
| [](int deviceId, QGamepadManager::GamepadAxis axis) { qDebug() << "axis configured:" << deviceId << axis; }); | |
| connect(QGamepadManager::instance(), &QGamepadManager::configurationCanceled, this, | |
| [](int deviceId) { qDebug() << "configuration canceled:" << deviceId; }); | |
| } | |
| }; | |
| struct FrameTimer : public QTimer { | |
| void sendFrame() const | |
| { | |
| static const QGamepadManager::GamepadButton hidButtons[] = { | |
| QGamepadManager::ButtonA, | |
| QGamepadManager::ButtonB, | |
| QGamepadManager::ButtonSelect, | |
| QGamepadManager::ButtonStart, | |
| QGamepadManager::ButtonRight, | |
| QGamepadManager::ButtonLeft, | |
| QGamepadManager::ButtonUp, | |
| QGamepadManager::ButtonDown, | |
| QGamepadManager::ButtonR1, | |
| QGamepadManager::ButtonL1, | |
| QGamepadManager::ButtonX, | |
| QGamepadManager::ButtonY, | |
| }; | |
| u32 hidPad = 0xfff; | |
| for(u32 i = 0; i < 12; i++) | |
| { | |
| if(buttons & (1 << hidButtons[i])) | |
| hidPad &= ~(1 << i); | |
| } | |
| u32 touchState = 0x2000000; | |
| u32 circleState = 0x7ff7ff; | |
| u16 x = (u16)(lx * 0x5d0 + 0x800); | |
| u16 y = (u16)(ly * 0x5d0 + 0x800); | |
| if(lx != 0.0 || ly != 0.0) | |
| circleState = ((u32) y << 12) | (u32)x; | |
| QByteArray ba(12, 0); | |
| qToLittleEndian(hidPad, (uchar *)ba.data()); | |
| qToLittleEndian(touchState, (uchar *)ba.data() + 4); | |
| qToLittleEndian(circleState, (uchar *)ba.data() + 8); | |
| QUdpSocket().writeDatagram(ba, QHostAddress("192.168.1.12"), 4950); | |
| } | |
| FrameTimer(QObject *parent = Q_NULLPTR) : QTimer(parent) | |
| { | |
| connect(this, &timeout, this, sendFrame); | |
| } | |
| }; | |
| int main(int argc, char *argv[]) | |
| { | |
| QApplication a(argc, argv); | |
| QWidget w; | |
| GamepadMonitor m(&w); | |
| FrameTimer t(&w); | |
| t.start(10); | |
| w.show(); | |
| return a.exec(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment