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
This commit is contained in:
Albert Vaca 2018-04-02 20:20:24 +02:00
parent a35ac8706c
commit f1f19eb01b
15 changed files with 559 additions and 470 deletions

View file

@ -10,14 +10,12 @@ add_subdirectory(notifications)
add_subdirectory(battery)
add_subdirectory(findmyphone)
add_subdirectory(remotekeyboard)
if(WIN32)
add_subdirectory(mousepad_windows)
else()
add_subdirectory(mousepad)
if(NOT WIN32)
add_subdirectory(runcommand)
add_subdirectory(sendnotifications)
add_subdirectory(pausemusic)
add_subdirectory(mpriscontrol)
add_subdirectory(mousepad)
add_subdirectory(screensaver-inhibit)
add_subdirectory(sftp)
endif()

View file

@ -1,32 +1,41 @@
find_package(LibFakeKey)
set_package_properties(LibFakeKey PROPERTIES DESCRIPTION "fake key events"
URL "https://www.yoctoproject.org/tools-resources/projects/matchbox"
TYPE OPTIONAL
PURPOSE "Needed for the remote mousepad plugin"
)
if(NOT WIN32)
find_package(LibFakeKey)
set_package_properties(LibFakeKey PROPERTIES DESCRIPTION "fake key events"
URL "https://www.yoctoproject.org/tools-resources/projects/matchbox"
TYPE OPTIONAL
PURPOSE "Needed for the remote mousepad plugin"
)
if (LibFakeKey_FOUND)
find_package(XTest REQUIRED)
find_package(X11 REQUIRED)
find_package(Qt5 ${QT_MIN_VERSION} REQUIRED COMPONENTS X11Extras)
include_directories(${XTEST_INCLUDE_DIRS} ${X11_INCLUDE_DIR} ${LibFakeKey_INCLUDE_DIRS})
endif()
find_package(KF5 ${KF5_MIN_VERSION} COMPONENTS Wayland)
if (LibFakeKey_FOUND)
find_package(XTest REQUIRED)
find_package(X11 REQUIRED)
find_package(Qt5 ${QT_MIN_VERSION} REQUIRED COMPONENTS X11Extras)
endif()
find_package(KF5 ${KF5_MIN_VERSION} COMPONENTS Wayland)
set(HAVE_WINDOWS ${WIN32})
set(HAVE_X11 ${LibFakeKey_FOUND})
set(HAVE_WAYLAND ${KF5Wayland_FOUND})
configure_file(config-mousepad.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-mousepad.h )
kdeconnect_add_plugin(kdeconnect_mousepad JSON kdeconnect_mousepad.json SOURCES mousepadplugin.cpp)
if (HAVE_X11)
include_directories(${XTEST_INCLUDE_DIRS} ${X11_INCLUDE_DIR} ${LibFakeKey_INCLUDE_DIRS})
endif()
kdeconnect_add_plugin(kdeconnect_mousepad JSON kdeconnect_mousepad.json SOURCES mousepadplugin.cpp abstractremoteinput.cpp)
target_link_libraries(kdeconnect_mousepad kdeconnectcore Qt5::Gui KF5::I18n)
if (HAVE_WINDOWS)
target_sources(kdeconnect_mousepad PUBLIC windowsremoteinput.cpp)
endif()
if(HAVE_WAYLAND)
target_sources(kdeconnect_mousepad PUBLIC waylandremoteinput.cpp)
target_link_libraries(kdeconnect_mousepad KF5::WaylandClient)
endif()
if(HAVE_X11)
target_sources(kdeconnect_mousepad PUBLIC x11remoteinput.cpp)
target_link_libraries(kdeconnect_mousepad Qt5::X11Extras ${X11_LIBRARIES} ${XTEST_LIBRARIES} ${LibFakeKey_LIBRARIES})
endif()

View file

@ -0,0 +1,28 @@
/**
* 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 "abstractremoteinput.h"
AbstractRemoteInput::AbstractRemoteInput(QObject* parent)
: QObject(parent)
{
}

View file

@ -1,5 +1,5 @@
/**
* Copyright 2014 Ahmed I. Khalil <albertvaka@gmail.com>
* 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
@ -18,25 +18,21 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MOUSEPADPLUGIN_H
#define MOUSEPADPLUGIN_H
#ifndef ABSTRACTREMOTEINPUT_H
#define ABSTRACTREMOTEINPUT_H
#include <QObject>
#include <QVariant>
#include <core/kdeconnectplugin.h>
#include <core/networkpacket.h>
class MousepadPlugin
: public KdeConnectPlugin
class AbstractRemoteInput
: public QObject
{
Q_OBJECT
public:
explicit MousepadPlugin(QObject* parent, const QVariantList &args);
~MousepadPlugin() override;
explicit AbstractRemoteInput(QObject* parent = nullptr);
bool receivePacket(const NetworkPacket& np) override;
void connected() override { }
virtual bool handlePacket(const NetworkPacket& np) = 0;
};
#endif

View file

@ -1,2 +1,3 @@
#cmakedefine01 HAVE_WAYLAND
#cmakedefine01 HAVE_X11
#cmakedefine01 HAVE_WINDOWS

View file

@ -1,6 +1,7 @@
/**
* Copyright 2014 Ahmed I. Khalil <ahmedibrahimkhali@gmail.com>
* Copyright 2018 Albert Vaca Cintora <albertvaka@gmail.com>
* Copyright 2015 Martin Gräßlin <mgraesslin@kde.org>
* Copyright 2014 Ahmed I. Khalil <ahmedibrahimkhali@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
@ -22,316 +23,54 @@
#include "mousepadplugin.h"
#include <KPluginFactory>
#include <KLocalizedString>
#include <QDebug>
#include <QGuiApplication>
#if HAVE_X11
#include <QX11Info>
#include <X11/extensions/XTest.h>
#include <X11/keysym.h>
#include <fakekey/fakekey.h>
#if HAVE_WINDOWS
#include "windowsremoteinput.h"
#else
#include <QX11Info>
#include <QGuiApplication>
#if HAVE_X11
#include "x11remoteinput.h"
#endif
#if HAVE_WAYLAND
#include "waylandremoteinput.h"
#endif
#endif
#if HAVE_WAYLAND
#include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/fakeinput.h>
#include <KWayland/Client/registry.h>
#endif
K_PLUGIN_FACTORY_WITH_JSON( KdeConnectPluginFactory, "kdeconnect_mousepad.json", registerPlugin< MousepadPlugin >(); )
enum MouseButtons {
LeftMouseButton = 1,
MiddleMouseButton = 2,
RightMouseButton = 3,
MouseWheelUp = 4,
MouseWheelDown = 5
};
#if HAVE_X11
//Translation table to keep in sync within all the implementations
int SpecialKeysMap[] = {
0, // Invalid
XK_BackSpace, // 1
XK_Tab, // 2
XK_Linefeed, // 3
XK_Left, // 4
XK_Up, // 5
XK_Right, // 6
XK_Down, // 7
XK_Page_Up, // 8
XK_Page_Down, // 9
XK_Home, // 10
XK_End, // 11
XK_Return, // 12
XK_Delete, // 13
XK_Escape, // 14
XK_Sys_Req, // 15
XK_Scroll_Lock, // 16
0, // 17
0, // 18
0, // 19
0, // 20
XK_F1, // 21
XK_F2, // 22
XK_F3, // 23
XK_F4, // 24
XK_F5, // 25
XK_F6, // 26
XK_F7, // 27
XK_F8, // 28
XK_F9, // 29
XK_F10, // 30
XK_F11, // 31
XK_F12, // 32
};
#endif
template <typename T, size_t N>
size_t arraySize(T(&arr)[N]) { (void)arr; return N; }
MousepadPlugin::MousepadPlugin(QObject* parent, const QVariantList& args)
: KdeConnectPlugin(parent, args)
#if HAVE_X11
, m_fakekey(nullptr)
, m_x11(QX11Info::isPlatformX11())
#else
, m_x11(false)
#endif
#if HAVE_WAYLAND
, m_waylandInput(nullptr)
, m_waylandAuthenticationRequested(false)
#endif
{
#if HAVE_WAYLAND
setupWaylandIntegration();
#if HAVE_WINDOWS
m_impl = new WindowsRemoteInput(this);
#else
if (QX11Info::isPlatformX11()) {
#if HAVE_X11
m_impl = new X11RemoteInput(this);
#else
qDebug() << "KDE Connect was built without X11 support";
#endif
} else {
Q_ASSERT(QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive));
#if HAVE_WAYLAND
m_impl = new WaylandRemoteInput(this);
#else
qDebug() << "KDE Connect was built without Wayland support";
#endif
}
#endif
}
MousepadPlugin::~MousepadPlugin()
{
#if HAVE_X11
if (m_fakekey) {
free(m_fakekey);
m_fakekey = nullptr;
}
#endif
delete m_impl;
}
bool MousepadPlugin::receivePacket(const NetworkPacket& np)
{
#if HAVE_X11
if (m_x11) {
return handlePacketX11(np);
}
#endif
#if HAVE_WAYLAND
if (m_waylandInput) {
if (!m_waylandAuthenticationRequested) {
m_waylandInput->authenticate(i18n("KDE Connect"), i18n("Use your phone as a touchpad and keyboard"));
m_waylandAuthenticationRequested = true;
}
handPacketWayland(np);
}
#endif
return false;
return m_impl->handlePacket(np);
}
#if HAVE_X11
bool isLeftHanded(Display * display)
{
unsigned char map[20];
int num_buttons = XGetPointerMapping(display, map, 20);
if( num_buttons == 1 ) {
return false;
} else if( num_buttons == 2 ) {
return ( (int)map[0] == 2 && (int)map[1] == 1 );
} else {
return ( (int)map[0] == 3 && (int)map[2] == 1 );
}
}
#endif
#if HAVE_X11
bool MousepadPlugin::handlePacketX11(const NetworkPacket& np)
{
//qDebug() << np.serialize();
//TODO: Split mouse/keyboard in two different plugins to avoid this big if statement
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) {
Display* display = QX11Info::display();
if(!display) {
return false;
}
bool leftHanded = isLeftHanded(display);
int mainMouseButton = leftHanded? RightMouseButton : LeftMouseButton;
int secondaryMouseButton = leftHanded? LeftMouseButton : RightMouseButton;
if (isSingleClick) {
XTestFakeButtonEvent(display, mainMouseButton, True, 0);
XTestFakeButtonEvent(display, mainMouseButton, False, 0);
} else if (isDoubleClick) {
XTestFakeButtonEvent(display, mainMouseButton, True, 0);
XTestFakeButtonEvent(display, mainMouseButton, False, 0);
XTestFakeButtonEvent(display, mainMouseButton, True, 0);
XTestFakeButtonEvent(display, mainMouseButton, False, 0);
} else if (isMiddleClick) {
XTestFakeButtonEvent(display, MiddleMouseButton, True, 0);
XTestFakeButtonEvent(display, MiddleMouseButton, False, 0);
} else if (isRightClick) {
XTestFakeButtonEvent(display, secondaryMouseButton, True, 0);
XTestFakeButtonEvent(display, secondaryMouseButton, False, 0);
} else if (isSingleHold){
//For drag'n drop
XTestFakeButtonEvent(display, mainMouseButton, True, 0);
} else if (isSingleRelease){
//For drag'n drop. NEVER USED (release is done by tapping, which actually triggers a isSingleClick). Kept here for future-proofnes.
XTestFakeButtonEvent(display, mainMouseButton, False, 0);
} else if (isScroll) {
if (dy < 0) {
XTestFakeButtonEvent(display, MouseWheelDown, True, 0);
XTestFakeButtonEvent(display, MouseWheelDown, False, 0);
} else if (dy > 0) {
XTestFakeButtonEvent(display, MouseWheelUp, True, 0);
XTestFakeButtonEvent(display, MouseWheelUp, False, 0);
}
} 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);
}
XFlush(display);
} else { //Is a mouse move event
QPoint point = QCursor::pos();
QCursor::setPos(point.x() + (int)dx, point.y() + (int)dy);
}
return true;
}
#endif
#if HAVE_WAYLAND
void MousepadPlugin::setupWaylandIntegration()
{
if (!QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) {
// not wayland
return;
}
using namespace KWayland::Client;
ConnectionThread* connection = ConnectionThread::fromApplication(this);
if (!connection) {
// failed to get the Connection from Qt
return;
}
Registry* registry = new Registry(this);
registry->create(connection);
connect(registry, &Registry::fakeInputAnnounced, this,
[this, registry] (quint32 name, quint32 version) {
m_waylandInput = registry->createFakeInput(name, version, this);
}
);
registry->setup();
}
bool MousepadPlugin::handPacketWayland(const NetworkPacket& np)
{
const float dx = np.get<float>(QStringLiteral("dx"), 0);
const float dy = np.get<float>(QStringLiteral("dy"), 0);
const bool isSingleClick = np.get<bool>(QStringLiteral("singleclick"), false);
const bool isDoubleClick = np.get<bool>(QStringLiteral("doubleclick"), false);
const bool isMiddleClick = np.get<bool>(QStringLiteral("middleclick"), false);
const bool isRightClick = np.get<bool>(QStringLiteral("rightclick"), false);
const bool isSingleHold = np.get<bool>(QStringLiteral("singlehold"), false);
const bool isSingleRelease = np.get<bool>(QStringLiteral("singlerelease"), false);
const bool isScroll = np.get<bool>(QStringLiteral("scroll"), false);
const QString key = np.get<QString>(QStringLiteral("key"), QLatin1String(""));
const int specialKey = np.get<int>(QStringLiteral("specialKey"), 0);
if (isSingleClick || isDoubleClick || isMiddleClick || isRightClick || isSingleHold || isScroll || !key.isEmpty() || specialKey) {
if (isSingleClick) {
m_waylandInput->requestPointerButtonClick(Qt::LeftButton);
} else if (isDoubleClick) {
m_waylandInput->requestPointerButtonClick(Qt::LeftButton);
m_waylandInput->requestPointerButtonClick(Qt::LeftButton);
} else if (isMiddleClick) {
m_waylandInput->requestPointerButtonClick(Qt::MiddleButton);
} else if (isRightClick) {
m_waylandInput->requestPointerButtonClick(Qt::RightButton);
} else if (isSingleHold){
//For drag'n drop
m_waylandInput->requestPointerButtonPress(Qt::LeftButton);
} else if (isSingleRelease){
//For drag'n drop. NEVER USED (release is done by tapping, which actually triggers a isSingleClick). Kept here for future-proofnes.
m_waylandInput->requestPointerButtonRelease(Qt::LeftButton);
} else if (isScroll) {
m_waylandInput->requestPointerAxis(Qt::Vertical, dy);
} else if (!key.isEmpty() || specialKey) {
// TODO: implement key support
}
} else { //Is a mouse move event
m_waylandInput->requestPointerMove(QSizeF(dx, dy));
}
return true;
}
#endif
#include "mousepadplugin.moc"

View file

@ -1,5 +1,7 @@
/**
* Copyright 2014 Ahmed I. Khalil <albertvaka@gmail.com>
* Copyright 2018 Albert Vaca Cintora <albertvaka@gmail.com>
* Copyright 2015 Martin Gräßlin <mgraesslin@kde.org>
* Copyright 2014 Ahmed I. Khalil <ahmedibrahimkhali@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
@ -21,21 +23,10 @@
#ifndef MOUSEPADPLUGIN_H
#define MOUSEPADPLUGIN_H
#include <QtGui/QCursor>
#include <core/kdeconnectplugin.h>
#include <config-mousepad.h>
struct FakeKey;
#if HAVE_WAYLAND
namespace KWayland
{
namespace Client
{
class FakeInput;
}
}
#endif
#include "abstractremoteinput.h"
class MousepadPlugin
: public KdeConnectPlugin
@ -50,22 +41,8 @@ public:
void connected() override { }
private:
#if HAVE_X11
bool handlePacketX11(const NetworkPacket& np);
#endif
#if HAVE_WAYLAND
void setupWaylandIntegration();
bool handPacketWayland(const NetworkPacket& np);
#endif
AbstractRemoteInput* m_impl;
#if HAVE_X11
FakeKey* m_fakekey;
#endif
const bool m_x11;
#if HAVE_WAYLAND
KWayland::Client::FakeInput* m_waylandInput;
bool m_waylandAuthenticationRequested;
#endif
};
#endif

View file

@ -0,0 +1,103 @@
/**
* Copyright 2015 Martin Gräßlin <mgraesslin@kde.org>
*
* 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 "waylandremoteinput.h"
#include <QSizeF>
#include <QDebug>
#include <KLocalizedString>
#include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/fakeinput.h>
#include <KWayland/Client/registry.h>
WaylandRemoteInput::WaylandRemoteInput(QObject* parent)
: AbstractRemoteInput(parent)
, m_waylandInput(nullptr)
, m_waylandAuthenticationRequested(false)
{
using namespace KWayland::Client;
ConnectionThread* connection = ConnectionThread::fromApplication(this);
if (!connection) {
qDebug() << "failed to get the Connection from Qt, Wayland remote input will not work";
return;
}
Registry* registry = new Registry(this);
registry->create(connection);
connect(registry, &Registry::fakeInputAnnounced, this,
[this, registry] (quint32 name, quint32 version) {
m_waylandInput = registry->createFakeInput(name, version, this);
}
);
registry->setup();
}
bool WaylandRemoteInput::handlePacket(const NetworkPacket& np)
{
if (!m_waylandInput) {
return false;
}
if (!m_waylandAuthenticationRequested) {
m_waylandInput->authenticate(i18n("KDE Connect"), i18n("Use your phone as a touchpad and keyboard"));
m_waylandAuthenticationRequested = true;
}
const float dx = np.get<float>(QStringLiteral("dx"), 0);
const float dy = np.get<float>(QStringLiteral("dy"), 0);
const bool isSingleClick = np.get<bool>(QStringLiteral("singleclick"), false);
const bool isDoubleClick = np.get<bool>(QStringLiteral("doubleclick"), false);
const bool isMiddleClick = np.get<bool>(QStringLiteral("middleclick"), false);
const bool isRightClick = np.get<bool>(QStringLiteral("rightclick"), false);
const bool isSingleHold = np.get<bool>(QStringLiteral("singlehold"), false);
const bool isSingleRelease = np.get<bool>(QStringLiteral("singlerelease"), false);
const bool isScroll = np.get<bool>(QStringLiteral("scroll"), false);
const QString key = np.get<QString>(QStringLiteral("key"), QLatin1String(""));
const int specialKey = np.get<int>(QStringLiteral("specialKey"), 0);
if (isSingleClick || isDoubleClick || isMiddleClick || isRightClick || isSingleHold || isScroll || !key.isEmpty() || specialKey) {
if (isSingleClick) {
m_waylandInput->requestPointerButtonClick(Qt::LeftButton);
} else if (isDoubleClick) {
m_waylandInput->requestPointerButtonClick(Qt::LeftButton);
m_waylandInput->requestPointerButtonClick(Qt::LeftButton);
} else if (isMiddleClick) {
m_waylandInput->requestPointerButtonClick(Qt::MiddleButton);
} else if (isRightClick) {
m_waylandInput->requestPointerButtonClick(Qt::RightButton);
} else if (isSingleHold){
//For drag'n drop
m_waylandInput->requestPointerButtonPress(Qt::LeftButton);
} else if (isSingleRelease){
//For drag'n drop. NEVER USED (release is done by tapping, which actually triggers a isSingleClick). Kept here for future-proofnes.
m_waylandInput->requestPointerButtonRelease(Qt::LeftButton);
} else if (isScroll) {
m_waylandInput->requestPointerAxis(Qt::Vertical, dy);
} else if (!key.isEmpty() || specialKey) {
// TODO: implement key support
}
} else { //Is a mouse move event
m_waylandInput->requestPointerMove(QSizeF(dx, dy));
}
return true;
}

View file

@ -0,0 +1,51 @@
/**
* 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/>.
*/
#ifndef WAYLANDREMOTEINPUT_H
#define WAYLANDREMOTEINPUT_H
#include "abstractremoteinput.h"
namespace KWayland
{
namespace Client
{
class FakeInput;
}
}
class WaylandRemoteInput
: public AbstractRemoteInput
{
Q_OBJECT
public:
explicit WaylandRemoteInput(QObject* parent);
bool handlePacket(const NetworkPacket& np) override;
private:
void setupWaylandIntegration();
KWayland::Client::FakeInput* m_waylandInput;
bool m_waylandAuthenticationRequested;
};
#endif

View file

@ -1,6 +1,5 @@
/**
* Copyright 2014 Ahmed I. Khalil <ahmedibrahimkhali@gmail.com>
* Copyright 2015 Martin Gräßlin <mgraesslin@kde.org>
* 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
@ -19,27 +18,19 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mousepadplugin_windows.h"
#include <KPluginFactory>
#include <KLocalizedString>
#include <QDebug>
#include <QGuiApplication>
#include "windowsremoteinput.h"
#include <QCursor>
#include <Windows.h>
K_PLUGIN_FACTORY_WITH_JSON( KdeConnectPluginFactory, "kdeconnect_mousepad.json", registerPlugin< MousepadPlugin >(); )
MousepadPlugin::MousepadPlugin(QObject* parent, const QVariantList& args)
: KdeConnectPlugin(parent, args)
WindowsRemoteInput::WindowsRemoteInput(QObject* parent)
: AbstractRemoteInput(parent)
{
}
MousepadPlugin::~MousepadPlugin()
{
}
bool MousepadPlugin::receivePacket(const NetworkPacket& np)
bool WindowsRemoteInput::handlePacket(const NetworkPacket& np)
{
float dx = np.get<float>(QStringLiteral("dx"), 0);
float dy = np.get<float>(QStringLiteral("dy"), 0);
@ -93,6 +84,7 @@ bool MousepadPlugin::receivePacket(const NetworkPacket& np)
input.mi.dwFlags = MOUSEEVENTF_WHEEL;
input.mi.mouseData = dy;
::SendInput(1,&input,sizeof(INPUT));
//TODO: Keyboard input support
/*
} else if (!key.isEmpty() || specialKey) {
@ -146,5 +138,3 @@ bool MousepadPlugin::receivePacket(const NetworkPacket& np)
}
return true;
}
#include "mousepadplugin_windows.moc"

View file

@ -0,0 +1,37 @@
/**
* 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/>.
*/
#ifndef WINDOWSREMOTEINPUT_H
#define WINDOWSREMOTEINPUT_H
#include "abstractremoteinput.h"
class WindowsRemoteInput
: public AbstractRemoteInput
{
Q_OBJECT
public:
explicit WindowsRemoteInput(QObject* parent);
bool handlePacket(const NetworkPacket& np) override;
};
#endif

View file

@ -0,0 +1,215 @@
/**
* Copyright 2018 Albert Vaca Cintora <albertvaka@gmail.com>
* Copyright 2014 Ahmed I. Khalil <ahmedibrahimkhali@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 "x11remoteinput.h"
#include <QX11Info>
#include <QCursor>
#include <QDebug>
#include <X11/extensions/XTest.h>
#include <X11/keysym.h>
#include <fakekey/fakekey.h>
enum MouseButtons {
LeftMouseButton = 1,
MiddleMouseButton = 2,
RightMouseButton = 3,
MouseWheelUp = 4,
MouseWheelDown = 5
};
//Translation table to keep in sync within all the implementations
int SpecialKeysMap[] = {
0, // Invalid
XK_BackSpace, // 1
XK_Tab, // 2
XK_Linefeed, // 3
XK_Left, // 4
XK_Up, // 5
XK_Right, // 6
XK_Down, // 7
XK_Page_Up, // 8
XK_Page_Down, // 9
XK_Home, // 10
XK_End, // 11
XK_Return, // 12
XK_Delete, // 13
XK_Escape, // 14
XK_Sys_Req, // 15
XK_Scroll_Lock, // 16
0, // 17
0, // 18
0, // 19
0, // 20
XK_F1, // 21
XK_F2, // 22
XK_F3, // 23
XK_F4, // 24
XK_F5, // 25
XK_F6, // 26
XK_F7, // 27
XK_F8, // 28
XK_F9, // 29
XK_F10, // 30
XK_F11, // 31
XK_F12, // 32
};
template <typename T, size_t N>
size_t arraySize(T(&arr)[N]) { (void)arr; return N; }
X11RemoteInput::X11RemoteInput(QObject* parent)
: AbstractRemoteInput(parent)
, m_fakekey(nullptr)
{
}
X11RemoteInput::~X11RemoteInput()
{
if (m_fakekey) {
free(m_fakekey);
m_fakekey = nullptr;
}
}
bool isLeftHanded(Display * display)
{
unsigned char map[20];
int num_buttons = XGetPointerMapping(display, map, 20);
if( num_buttons == 1 ) {
return false;
} else if( num_buttons == 2 ) {
return ( (int)map[0] == 2 && (int)map[1] == 1 );
} else {
return ( (int)map[0] == 3 && (int)map[2] == 1 );
}
}
bool X11RemoteInput::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) {
Display* display = QX11Info::display();
if(!display) {
return false;
}
bool leftHanded = isLeftHanded(display);
int mainMouseButton = leftHanded? RightMouseButton : LeftMouseButton;
int secondaryMouseButton = leftHanded? LeftMouseButton : RightMouseButton;
if (isSingleClick) {
XTestFakeButtonEvent(display, mainMouseButton, True, 0);
XTestFakeButtonEvent(display, mainMouseButton, False, 0);
} else if (isDoubleClick) {
XTestFakeButtonEvent(display, mainMouseButton, True, 0);
XTestFakeButtonEvent(display, mainMouseButton, False, 0);
XTestFakeButtonEvent(display, mainMouseButton, True, 0);
XTestFakeButtonEvent(display, mainMouseButton, False, 0);
} else if (isMiddleClick) {
XTestFakeButtonEvent(display, MiddleMouseButton, True, 0);
XTestFakeButtonEvent(display, MiddleMouseButton, False, 0);
} else if (isRightClick) {
XTestFakeButtonEvent(display, secondaryMouseButton, True, 0);
XTestFakeButtonEvent(display, secondaryMouseButton, False, 0);
} else if (isSingleHold){
//For drag'n drop
XTestFakeButtonEvent(display, mainMouseButton, True, 0);
} else if (isSingleRelease){
//For drag'n drop. NEVER USED (release is done by tapping, which actually triggers a isSingleClick). Kept here for future-proofnes.
XTestFakeButtonEvent(display, mainMouseButton, False, 0);
} else if (isScroll) {
if (dy < 0) {
XTestFakeButtonEvent(display, MouseWheelDown, True, 0);
XTestFakeButtonEvent(display, MouseWheelDown, False, 0);
} else if (dy > 0) {
XTestFakeButtonEvent(display, MouseWheelUp, True, 0);
XTestFakeButtonEvent(display, MouseWheelUp, False, 0);
}
} 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);
}
XFlush(display);
} else { //Is a mouse move event
QPoint point = QCursor::pos();
QCursor::setPos(point.x() + (int)dx, point.y() + (int)dy);
}
return true;
}

View file

@ -0,0 +1,43 @@
/**
* 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/>.
*/
#ifndef X11REMOTEINPUT_H
#define X11REMOTEINPUT_H
#include "abstractremoteinput.h"
struct FakeKey;
class X11RemoteInput
: public AbstractRemoteInput
{
Q_OBJECT
public:
explicit X11RemoteInput(QObject* parent);
~X11RemoteInput() override;
bool handlePacket(const NetworkPacket& np) override;
private:
FakeKey* m_fakekey;
};
#endif

View file

@ -1,2 +0,0 @@
kdeconnect_add_plugin(kdeconnect_mousepad JSON kdeconnect_mousepad.json SOURCES mousepadplugin_windows.cpp)
target_link_libraries(kdeconnect_mousepad kdeconnectcore Qt5::Gui KF5::I18n)

View file

@ -1,96 +0,0 @@
{
"KPlugin": {
"Authors": [
{
"Email": "ahmedibrahimkhali@gmail.com",
"Name": "Ahmed I. Khalil",
"Name[ar]": "أحمد إبراهيم خليل",
"Name[sr@ijekavian]": "Ахмед И. Калил",
"Name[sr@ijekavianlatin]": "Ahmed I. Kalil",
"Name[sr@latin]": "Ahmed I. Kalil",
"Name[sr]": "Ахмед И. Калил",
"Name[x-test]": "xxAhmed I. Khalilxx"
}
],
"Description": "Use your phone as a touchpad and keyboard",
"Description[ar]": "استخدم الهاتف كفأرة ولوحة مفاتيح",
"Description[ca@valencia]": "Empra el telèfon com un ratolí tàctil i teclat",
"Description[ca]": "Empra el telèfon com un ratolí tàctil i teclat",
"Description[cs]": "Používejte svůj telefon jako touchpad a klávesnici",
"Description[da]": "Brug din telefon som touchpad og tastatur",
"Description[de]": "Verwendet Ihr Handy als Touchpad und Tastatur",
"Description[el]": "Χρήση του τηλεφώνου σας ως οθόνη αφής και πληκτρολογίου",
"Description[es]": "Usar teléfono como panel táctil y teclado",
"Description[et]": "Telefoni kasutamine puutepadja ja klaviatuurina",
"Description[eu]": "Erabili zure telefonoa touchpad eta teklatu gisa",
"Description[fi]": "Käytä puhelintasi kosketuslevynä ja näppäimistönä",
"Description[fr]": "Utilisez votre téléphone comme un pavé tactile et un clavier",
"Description[gl]": "Usar o teléfono móbil como área táctil e teclado.",
"Description[hu]": "A telefon használata érintőtáblaként és billentyűzetként",
"Description[it]": "Usa il telefono come touchpad e tastiera",
"Description[ko]": "휴대폰을 터치패드와 키보드로 사용",
"Description[nl]": "Uw telefoon gebruiken als een touchpad en toetsenbord",
"Description[nn]": "Bruk telefonen som styreplate og tastatur",
"Description[pl]": "Używaj swojego telefonu jako gładzika i klawiatury",
"Description[pt]": "Use o seu telefone como um rato e teclado por toque",
"Description[pt_BR]": "Use seu telefone como um touchpad e teclado",
"Description[ru]": "Использование телефона в качестве сенсорной панели и клавиатуры",
"Description[sk]": "Použite váš telefón ako touchpad a klávesnicu",
"Description[sr@ijekavian]": "Користите телефон као додирник и тастатуру",
"Description[sr@ijekavianlatin]": "Koristite telefon kao dodirnik i tastaturu",
"Description[sr@latin]": "Koristite telefon kao dodirnik i tastaturu",
"Description[sr]": "Користите телефон као додирник и тастатуру",
"Description[sv]": "Använd telefonen som mus och tangentbord",
"Description[tr]": "Telefonunuzu dokunmatik yüzey ve klayve olarak kullanın",
"Description[uk]": "Скористайтеся телефоном як замінником сенсорної панелі і клавіатури",
"Description[x-test]": "xxUse your phone as a touchpad and keyboardxx",
"Description[zh_CN]": "用您的手机作为触摸板和键盘",
"Description[zh_TW]": "讓您的智慧型手機當作觸碰板與鍵盤",
"EnabledByDefault": true,
"Icon": "edit-select",
"Id": "kdeconnect_mousepad",
"License": "GPL",
"Name": "Virtual input",
"Name[ar]": "دخل وهميّ",
"Name[ca@valencia]": "Entrada virtual",
"Name[ca]": "Entrada virtual",
"Name[cs]": "Virtuální vstup",
"Name[da]": "Virtuelt input",
"Name[de]": "Virtuelle Eingabe",
"Name[el]": "Εικονικά στοιχεία εισόδου",
"Name[es]": "Entrada virtual",
"Name[et]": "Virtuaalsisestus",
"Name[eu]": "Sarrera birtuala",
"Name[fi]": "Virtuaalinen syöttö",
"Name[fr]": "Entrée virtuelle",
"Name[gl]": "Entrada virtual",
"Name[hu]": "Virtuális bevitel",
"Name[it]": "Inserimento virtuale",
"Name[ko]": "가상 입력",
"Name[nl]": "Virtuele invoer",
"Name[nn]": "Virtuelt tastatur",
"Name[pl]": "Wirtualna obsługa",
"Name[pt]": "Entrada virtual",
"Name[pt_BR]": "Entrada virtual",
"Name[ru]": "Виртуальный ввод",
"Name[sk]": "Virtuálny vstup",
"Name[sr@ijekavian]": "Виртуелни унос",
"Name[sr@ijekavianlatin]": "Virtuelni unos",
"Name[sr@latin]": "Virtuelni unos",
"Name[sr]": "Виртуелни унос",
"Name[sv]": "Virtuell inmatning",
"Name[tr]": "Sanal içe aktarma",
"Name[uk]": "Віртуальне введення даних",
"Name[x-test]": "xxVirtual inputxx",
"Name[zh_CN]": "虚拟输入",
"Name[zh_TW]": "虛擬輸入",
"ServiceTypes": [
"KdeConnect/Plugin"
],
"Version": "0.1"
},
"X-KdeConnect-OutgoingPacketType": [],
"X-KdeConnect-SupportedPacketType": [
"kdeconnect.mousepad.request"
]
}