Commit ea5d9c8e authored by Albert Vaca Cintora's avatar Albert Vaca Cintora

MousePad: Added support for any UTF character and more special keys.

I've also removed modifiers like Shift and Ctrl by now.

LibFakeKey is now a dependency of this plugin, as it allows us to send UTF
characters as X11 keys in a simple and convenient way. Internally it uses a
convention in Xlib, that allows to pass UTF characters as keycodes just by
adding 0x01000000 to the keycode number.

CCMAIL: saiarcot895@gmail.com
parent 05fdbe0e
......@@ -9,7 +9,12 @@ kde4_add_plugin(kdeconnect_mousepad ${kdeconnect_mousepad_SRCS})
include_directories(${XTEST_INCLUDE_DIRS} ${X11_INCLUDE_DIR})
target_link_libraries(kdeconnect_mousepad kdeconnectcore ${QT_QTGUI_LIBRARY} ${X11_LIBRARIES} ${XTEST_LIBRARIES})
target_link_libraries(kdeconnect_mousepad kdeconnectcore
${QT_QTGUI_LIBRARY}
${X11_LIBRARIES}
${XTEST_LIBRARIES}
fakekey
)
install(TARGETS kdeconnect_mousepad DESTINATION ${PLUGIN_INSTALL_DIR} )
install(FILES kdeconnect_mousepad.desktop DESTINATION ${SERVICES_INSTALL_DIR} )
......@@ -23,14 +23,43 @@
#include <core/networkpackage.h>
#include <X11/extensions/XTest.h>
#include <X11/keysym.h>
#include <fakekey/fakekey.h>
K_PLUGIN_FACTORY( KdeConnectPluginFactory, registerPlugin< MousepadPlugin >(); )
K_EXPORT_PLUGIN( KdeConnectPluginFactory("kdeconnect_mousepad", "kdeconnect-plugins") )
// Source: http://bharathisubramanian.wordpress.com/2010/04/01/x11-fake-mouse-events-generation-using-xtest/
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
};
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), m_display(0)
: KdeConnectPlugin(parent, args), m_display(0), m_fakekey(0)
{
}
......@@ -41,37 +70,16 @@ MousepadPlugin::~MousepadPlugin()
XCloseDisplay(m_display);
m_display = 0;
}
}
XKeyEvent MousepadPlugin::createKeyEvent(Display *display, Window &win, Window &winRoot, bool press, KeyCode keycode, int modifiers)
{
// http://www.doctort.org/adam/nerd-notes/x11-fake-keypress-event.html
XKeyEvent event;
event.display = display;
event.window = win;
event.root = winRoot;
event.subwindow = None;
event.time = CurrentTime;
event.x = 1;
event.y = 1;
event.x_root = 1;
event.y_root = 1;
event.same_screen = true;
event.keycode = keycode;
event.state = modifiers;
if (press) {
event.type = KeyPress;
} else {
event.type = KeyRelease;
}
return event;
if (m_fakekey) {
free(m_fakekey);
m_fakekey = 0;
}
}
bool MousepadPlugin::receivePackage(const NetworkPackage& np)
{
//TODO: Split in several package types to avoid big if statements: mouse move, mouse click and keyboard events
float dx = np.get<float>("dx", 0);
float dy = np.get<float>("dy", 0);
......@@ -81,82 +89,73 @@ bool MousepadPlugin::receivePackage(const NetworkPackage& np)
bool isRightClick = np.get<bool>("rightclick", false);
bool isScroll = np.get<bool>("scroll", false);
QString key = np.get<QString>("key", "");
int modifiers = np.get<int>("modifiers", 0);
int specialKey = np.get<int>("specialKey", 0);
if (isSingleClick || isDoubleClick || isMiddleClick || isRightClick || isScroll || !key.isEmpty() || specialKey) {
if (isSingleClick || isDoubleClick || isMiddleClick || isRightClick || isScroll || !key.isEmpty() || modifiers) {
if(!m_display) {
m_display = XOpenDisplay(NULL);
if(!m_display) {
kDebug(debugArea()) << "Failed to open X11 display";
return false;
}
}
if(m_display) {
if (isSingleClick) {
XTestFakeButtonEvent(m_display, LeftMouseButton, true, CurrentTime);
XTestFakeButtonEvent(m_display, LeftMouseButton, false, CurrentTime);
} else if (isDoubleClick) {
XTestFakeButtonEvent(m_display, LeftMouseButton, true, CurrentTime);
XTestFakeButtonEvent(m_display, LeftMouseButton, false, CurrentTime);
XTestFakeButtonEvent(m_display, LeftMouseButton, true, CurrentTime);
XTestFakeButtonEvent(m_display, LeftMouseButton, false, CurrentTime);
} else if (isMiddleClick) {
XTestFakeButtonEvent(m_display, MiddleMouseButton, true, CurrentTime);
XTestFakeButtonEvent(m_display, MiddleMouseButton, false, CurrentTime);
} else if (isRightClick) {
XTestFakeButtonEvent(m_display, RightMouseButton, true, CurrentTime);
XTestFakeButtonEvent(m_display, RightMouseButton, false, CurrentTime);
} else if( isScroll ) {
if (dy < 0) {
XTestFakeButtonEvent(m_display, MouseWheelDown, true, CurrentTime);
XTestFakeButtonEvent(m_display, MouseWheelDown, false, CurrentTime);
} else if (dy > 0) {
XTestFakeButtonEvent(m_display, MouseWheelUp, true, CurrentTime);
XTestFakeButtonEvent(m_display, MouseWheelUp, false, CurrentTime);
}
} else if (!key.isEmpty() || modifiers) {
Window winRoot = XDefaultRootWindow(m_display);
Window winFocus;
int revert;
XGetInputFocus(m_display, &winFocus, &revert);
KeyCode keycode = 0;
int xModifier = 0;
if (!key.isEmpty()) {
if (key == QLatin1String(".")) {
keycode = XKeysymToKeycode(m_display, XK_period);
} else if (key == QLatin1String(",")) {
keycode = XKeysymToKeycode(m_display, XK_comma);
} else {
QByteArray keyByteArray = key.toLocal8Bit();
KeySym keysym = XStringToKeysym(keyByteArray.constData());
keycode = XKeysymToKeycode(m_display, keysym);
}
} else {
// It's a special key
if ((modifiers & FunctionalKeys) == Backspace) {
keycode = XKeysymToKeycode(m_display, XK_BackSpace);
} else if ((modifiers & FunctionalKeys) == Enter) {
keycode = XKeysymToKeycode(m_display, XK_Linefeed);
} else if ((modifiers & FunctionalKeys) == Tab) {
keycode = XKeysymToKeycode(m_display, XK_Tab);
}
}
if (isSingleClick) {
XTestFakeButtonEvent(m_display, LeftMouseButton, True, 0);
XTestFakeButtonEvent(m_display, LeftMouseButton, False, 0);
} else if (isDoubleClick) {
XTestFakeButtonEvent(m_display, LeftMouseButton, True, 0);
XTestFakeButtonEvent(m_display, LeftMouseButton, False, 0);
XTestFakeButtonEvent(m_display, LeftMouseButton, True, 0);
XTestFakeButtonEvent(m_display, LeftMouseButton, False, 0);
} else if (isMiddleClick) {
XTestFakeButtonEvent(m_display, MiddleMouseButton, True, 0);
XTestFakeButtonEvent(m_display, MiddleMouseButton, False, 0);
} else if (isRightClick) {
XTestFakeButtonEvent(m_display, RightMouseButton, True, 0);
XTestFakeButtonEvent(m_display, RightMouseButton, False, 0);
} else if( isScroll ) {
if (dy < 0) {
XTestFakeButtonEvent(m_display, MouseWheelDown, True, 0);
XTestFakeButtonEvent(m_display, MouseWheelDown, False, 0);
} else if (dy > 0) {
XTestFakeButtonEvent(m_display, MouseWheelUp, True, 0);
XTestFakeButtonEvent(m_display, MouseWheelUp, False, 0);
}
} else if (!key.isEmpty() || specialKey) {
if (modifiers & Shift) {
xModifier |= ShiftMask;
}
if (modifiers & Control) {
xModifier |= ControlMask;
if (specialKey)
{
if (specialKey > (int)arraySize(SpecialKeysMap)) {
kDebug(debugArea()) << "Unsupported special key identifier";
return false;
}
XKeyEvent event = createKeyEvent(m_display, winFocus, winRoot, true, keycode, xModifier);
XSendEvent(event.display, event.window, true, KeyPressMask, (XEvent*)&event);
int keycode = XKeysymToKeycode(m_display, SpecialKeysMap[specialKey]);
XTestFakeKeyEvent (m_display, keycode, True, 0);
XTestFakeKeyEvent (m_display, keycode, False, 0);
} else {
if (!m_fakekey) {
m_fakekey = fakekey_init(m_display);
if (!m_fakekey) {
kDebug(debugArea()) << "Failed to initialize libfakekey";
return false;
}
}
event = createKeyEvent(m_display, winFocus, winRoot, false, keycode, xModifier);
XSendEvent(event.display, event.window, true, KeyPressMask, (XEvent*)&event);
//We use fakekey here instead of XTest (above) because it can handle utf characters instead of keycodes.
fakekey_press(m_fakekey, (const unsigned char*)key.toUtf8().constData(), -1, 0);
fakekey_release(m_fakekey);
}
XFlush(m_display);
}
} else {
XFlush(m_display);
} else { //Is a mouse move event
QPoint point = QCursor::pos();
QCursor::setPos(point.x() + (int)dx, point.y() + (int)dy);
}
......
......@@ -29,30 +29,13 @@
#define PACKAGE_TYPE_MOUSEPAD QLatin1String("kdeconnect.mousepad")
struct FakeKey;
class MousepadPlugin
: public KdeConnectPlugin
{
Q_OBJECT
enum MouseButtons {
LeftMouseButton = 1,
MiddleMouseButton = 2,
RightMouseButton = 3,
MouseWheelUp = 4,
MouseWheelDown = 5
};
enum SpecialKeys {
Backspace = 1,
Enter = 1 << 1,
Tab = 1 << 1 | 1,
// Placeholder for other keys
FunctionalKeys = 1 << 2 | 1 << 1 | 1,
Shift = 1 << 3,
Control = 1 << 4
};
public:
explicit MousepadPlugin(QObject *parent, const QVariantList &args);
virtual ~MousepadPlugin();
......@@ -62,9 +45,9 @@ public Q_SLOTS:
virtual void connected() { }
private:
XKeyEvent createKeyEvent(Display *display, Window &win, Window &winRoot, bool press, KeyCode keycode, int modifiers);
Display *m_display;
FakeKey* m_fakekey;
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment