File: mousepadplugin.cpp

package info (click to toggle)
kdeconnect 0.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,800 kB
  • ctags: 729
  • sloc: cpp: 5,636; xml: 67; sh: 14; makefile: 12
file content (92 lines) | stat: -rw-r--r-- 3,860 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
 * 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 "mousepadplugin.h"

#include <core/networkpackage.h>
#include <X11/extensions/XTest.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/

MousepadPlugin::MousepadPlugin(QObject* parent, const QVariantList& args)
    : KdeConnectPlugin(parent, args), m_display(0)
{

}

MousepadPlugin::~MousepadPlugin()
{
    if (m_display) {
        XCloseDisplay(m_display);
        m_display = 0;
    }
}

bool MousepadPlugin::receivePackage(const NetworkPackage& np)
{
    float dx = np.get<float>("dx", 0);
    float dy = np.get<float>("dy", 0);

    bool isSingleClick = np.get<bool>("singleclick", false);
    bool isDoubleClick = np.get<bool>("doubleclick", false);
    bool isMiddleClick = np.get<bool>("middleclick", false);
    bool isRightClick = np.get<bool>("rightclick", false);
    bool isScroll = np.get<bool>("scroll", false);

    if (isSingleClick || isDoubleClick || isMiddleClick || isRightClick || isScroll) {
        if(!m_display) {
            m_display = XOpenDisplay(NULL);
        }

        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);
                }
            }
            XFlush(m_display);
        }
    } else {
        QPoint point = QCursor::pos();
        QCursor::setPos(point.x() + (int)dx, point.y() + (int)dy);
    }
    return true;
}