@@ -0,0 +1,142 @@
9+UInput::UInput(QObject *parent) :
12+ m_devName = QStringLiteral("unity8-simulated-mouse");
13+ m_uinput.setFileName("/dev/uinput");
15+ memset(&m_uinput_mouse_dev, 0, sizeof(m_uinput_mouse_dev));
16+ m_uinput_mouse_dev.id.bustype = BUS_USB;
17+ m_uinput_mouse_dev.id.version = 1;
18+ strncpy(m_uinput_mouse_dev.name, m_devName.toLocal8Bit().data(), m_devName.length());
24+ if (m_mouseCreated) {
29+void UInput::createMouse()
31+ if (m_mouseCreated) {
32+ qDebug() << "Already have a virtual device. Not creating another one.";
36+ if (!m_uinput.isOpen() && !m_uinput.open(QFile::WriteOnly)) {
40+ ioctl(m_uinput.handle(), UI_SET_EVBIT, EV_REL);
41+ ioctl(m_uinput.handle(), UI_SET_RELBIT, REL_X);
42+ ioctl(m_uinput.handle(), UI_SET_RELBIT, REL_Y);
44+ ioctl(m_uinput.handle(), UI_SET_EVBIT, EV_KEY);
45+ ioctl(m_uinput.handle(), UI_SET_KEYBIT, BTN_MOUSE);
46+ ioctl(m_uinput.handle(), UI_SET_KEYBIT, BTN_LEFT);
47+ ioctl(m_uinput.handle(), UI_SET_KEYBIT, BTN_MIDDLE);
48+ ioctl(m_uinput.handle(), UI_SET_KEYBIT, BTN_RIGHT);
49+ ioctl(m_uinput.handle(), UI_SET_KEYBIT, BTN_FORWARD);
50+ ioctl(m_uinput.handle(), UI_SET_KEYBIT, BTN_BACK);
52+ ioctl(m_uinput.handle(), UI_SET_EVBIT, EV_SYN);
54+ int len = write(m_uinput.handle(), &m_uinput_mouse_dev, sizeof(m_uinput_mouse_dev));
56+ qWarning() << "Failed to write to uinput. Cannot create virtual uinput mouse.";
60+ int err = ioctl(m_uinput.handle(), UI_DEV_CREATE);
62+ qWarning() << "Cannot create virtual uinput device. Create ioctl failed:" << err;
65+ m_mouseCreated = true;
66+ qDebug() << "Virtual uinput mouse device created.";
69+void UInput::removeMouse()
71+ if (!m_mouseCreated) {
75+ if (!m_uinput.isOpen() && !m_uinput.open(QFile::WriteOnly)) {
76+ qDebug() << "cannot open uinput... ";
80+ int err = ioctl(m_uinput.handle(), UI_DEV_DESTROY);
82+ qWarning() << "Failed to destroy virtual uinput device. Destroy ioctl failed:" << err;
84+ qDebug() << "Virtual uinput mouse device removed.";
87+ m_mouseCreated = false;
90+void UInput::moveMouse(int dx, int dy)
92+ struct input_event event;
93+ memset(&event, 0, sizeof(event));
94+ gettimeofday(&event.time, 0);
98+ write(m_uinput.handle(), &event, sizeof(event));
102+ write(m_uinput.handle(), &event, sizeof(event));
104+ event.type = EV_SYN;
105+ event.code = SYN_REPORT;
107+ write(m_uinput.handle(), &event, sizeof(event));
110+void UInput::pressMouse(Qt::MouseButton button)
112+ injectMouse(button, 1);
115+void UInput::releaseMouse(Qt::MouseButton button)
117+ injectMouse(button, 0);
120+void UInput::injectMouse(Qt::MouseButton button, int down)
122+ struct input_event event;
123+ memset(&event, 0, sizeof(event));
124+ gettimeofday(&event.time, 0);
125+ event.type = EV_KEY;
127+ case Qt::LeftButton:
128+ event.code = BTN_LEFT;
130+ case Qt::RightButton:
131+ event.code = BTN_RIGHT;
133+ case Qt::MiddleButton:
134+ event.code = BTN_MIDDLE;
137+ qWarning() << "Unsupported mouse button.";
141+ write(m_uinput.handle(), &event, sizeof(event));