kdeconnect-kde/plugins/mousepad/windowsremoteinput.cpp
Albert Vaca f1f19eb01b Split platform-specific code from mousepad plugin
Summary:
Windows no longer needs a separate plugin, and X11 and Wayland are now in
separate files instead of having lots of ifdefs.

Test Plan: Tested on X11, Wayland and Windows.

Reviewers: #kde_connect, apol, nicolasfella

Reviewed By: #kde_connect, apol, nicolasfella

Subscribers: apol, nicolasfella

Tags: #kde_connect

Differential Revision: https://phabricator.kde.org/D11692
2018-04-04 22:38:16 +02:00

140 lines
5.6 KiB
C++

/**
* Copyright 2018 Albert Vaca Cintora <albertvaka@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "windowsremoteinput.h"
#include <QCursor>
#include <Windows.h>
WindowsRemoteInput::WindowsRemoteInput(QObject* parent)
: AbstractRemoteInput(parent)
{
}
bool WindowsRemoteInput::handlePacket(const NetworkPacket& np)
{
float dx = np.get<float>(QStringLiteral("dx"), 0);
float dy = np.get<float>(QStringLiteral("dy"), 0);
bool isSingleClick = np.get<bool>(QStringLiteral("singleclick"), false);
bool isDoubleClick = np.get<bool>(QStringLiteral("doubleclick"), false);
bool isMiddleClick = np.get<bool>(QStringLiteral("middleclick"), false);
bool isRightClick = np.get<bool>(QStringLiteral("rightclick"), false);
bool isSingleHold = np.get<bool>(QStringLiteral("singlehold"), false);
bool isSingleRelease = np.get<bool>(QStringLiteral("singlerelease"), false);
bool isScroll = np.get<bool>(QStringLiteral("scroll"), false);
QString key = np.get<QString>(QStringLiteral("key"), QLatin1String(""));
int specialKey = np.get<int>(QStringLiteral("specialKey"), 0);
if (isSingleClick || isDoubleClick || isMiddleClick || isRightClick || isSingleHold || isScroll || !key.isEmpty() || specialKey) {
INPUT input={0};
input.type = INPUT_MOUSE;
if (isSingleClick) {
input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
::SendInput(1,&input,sizeof(INPUT));
input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
::SendInput(1,&input,sizeof(INPUT));
} else if (isDoubleClick) {
input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
::SendInput(1,&input,sizeof(INPUT));
input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
::SendInput(1,&input,sizeof(INPUT));
input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
::SendInput(1,&input,sizeof(INPUT));
input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
::SendInput(1,&input,sizeof(INPUT));
} else if (isMiddleClick) {
input.mi.dwFlags = MOUSEEVENTF_MIDDLEDOWN;
::SendInput(1,&input,sizeof(INPUT));
input.mi.dwFlags = MOUSEEVENTF_MIDDLEUP;
::SendInput(1,&input,sizeof(INPUT));
} else if (isRightClick) {
input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
::SendInput(1,&input,sizeof(INPUT));
input.mi.dwFlags = MOUSEEVENTF_RIGHTUP;
::SendInput(1,&input,sizeof(INPUT));
} else if (isSingleHold){
input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
::SendInput(1,&input,sizeof(INPUT));
} else if (isSingleRelease){
input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
::SendInput(1,&input,sizeof(INPUT));
} else if (isScroll) {
input.mi.dwFlags = MOUSEEVENTF_WHEEL;
input.mi.mouseData = dy;
::SendInput(1,&input,sizeof(INPUT));
//TODO: Keyboard input support
/*
} else if (!key.isEmpty() || specialKey) {
bool ctrl = np.get<bool>(QStringLiteral("ctrl"), false);
bool alt = np.get<bool>(QStringLiteral("alt"), false);
bool shift = np.get<bool>(QStringLiteral("shift"), false);
if (ctrl) XTestFakeKeyEvent (display, XKeysymToKeycode(display, XK_Control_L), True, 0);
if (alt) XTestFakeKeyEvent (display, XKeysymToKeycode(display, XK_Alt_L), True, 0);
if (shift) XTestFakeKeyEvent (display, XKeysymToKeycode(display, XK_Shift_L), True, 0);
if (specialKey)
{
if (specialKey >= (int)arraySize(SpecialKeysMap)) {
qWarning() << "Unsupported special key identifier";
return false;
}
int keycode = XKeysymToKeycode(display, SpecialKeysMap[specialKey]);
XTestFakeKeyEvent (display, keycode, True, 0);
XTestFakeKeyEvent (display, keycode, False, 0);
} else {
if (!m_fakekey) {
m_fakekey = fakekey_init(display);
if (!m_fakekey) {
qWarning() << "Failed to initialize libfakekey";
return false;
}
}
//We use fakekey here instead of XTest (above) because it can handle utf characters instead of keycodes.
for (int i=0;i<key.length();i++) {
QByteArray utf8 = QString(key.at(i)).toUtf8();
fakekey_press(m_fakekey, (const uchar*)utf8.constData(), utf8.size(), 0);
fakekey_release(m_fakekey);
}
}
if (ctrl) XTestFakeKeyEvent (display, XKeysymToKeycode(display, XK_Control_L), False, 0);
if (alt) XTestFakeKeyEvent (display, XKeysymToKeycode(display, XK_Alt_L), False, 0);
if (shift) XTestFakeKeyEvent (display, XKeysymToKeycode(display, XK_Shift_L), False, 0);
*/
}
} else { //Is a mouse move event
QPoint point = QCursor::pos();
QCursor::setPos(point.x() + (int)dx, point.y() + (int)dy);
}
return true;
}