1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QT_NO_QWS_MOUSE_QVFB
43 
44 #include <stdlib.h>
45 #include <sys/types.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <errno.h>
49 #include <stdio.h>
50 
51 #include <qvfbhdr.h>
52 #include <qmousevfb_qws.h>
53 #include <qwindowsystem_qws.h>
54 #include <qsocketnotifier.h>
55 #include <qapplication.h>
56 #include <qtimer.h>
57 #include <private/qcore_unix_p.h> // overrides QT_OPEN
58 
59 QT_BEGIN_NAMESPACE
60 
QVFbMouseHandler(const QString & driver,const QString & device)61 QVFbMouseHandler::QVFbMouseHandler(const QString &driver, const QString &device)
62     : QObject(), QWSMouseHandler(driver, device)
63 {
64     QString mouseDev = device;
65     if (device.isEmpty())
66         mouseDev = QLatin1String("/dev/vmouse");
67 
68     mouseFD = QT_OPEN(mouseDev.toLatin1().constData(), O_RDWR | O_NDELAY);
69     if (mouseFD == -1) {
70         perror("QVFbMouseHandler::QVFbMouseHandler");
71         qWarning("QVFbMouseHander: Unable to open device %s",
72                  qPrintable(mouseDev));
73         return;
74     }
75 
76     // Clear pending input
77     char buf[2];
78     while (QT_READ(mouseFD, buf, 1) > 0) { }
79 
80     mouseIdx = 0;
81 
82     mouseNotifier = new QSocketNotifier(mouseFD, QSocketNotifier::Read, this);
83     connect(mouseNotifier, SIGNAL(activated(int)),this, SLOT(readMouseData()));
84 }
85 
~QVFbMouseHandler()86 QVFbMouseHandler::~QVFbMouseHandler()
87 {
88     if (mouseFD >= 0)
89         QT_CLOSE(mouseFD);
90 }
91 
resume()92 void QVFbMouseHandler::resume()
93 {
94     mouseNotifier->setEnabled(true);
95 }
96 
suspend()97 void QVFbMouseHandler::suspend()
98 {
99     mouseNotifier->setEnabled(false);
100 }
101 
readMouseData()102 void QVFbMouseHandler::readMouseData()
103 {
104     int n;
105     do {
106         n = QT_READ(mouseFD, mouseBuf+mouseIdx, mouseBufSize-mouseIdx);
107         if (n > 0)
108             mouseIdx += n;
109     } while (n > 0);
110 
111     int idx = 0;
112     static const int packetsize = sizeof(QPoint) + 2*sizeof(int);
113     while (mouseIdx-idx >= packetsize) {
114         uchar *mb = mouseBuf+idx;
115         QPoint mousePos = *reinterpret_cast<QPoint *>(mb);
116         mb += sizeof(QPoint);
117         int bstate = *reinterpret_cast<int *>(mb);
118         mb += sizeof(int);
119         int wheel = *reinterpret_cast<int *>(mb);
120 //        limitToScreen(mousePos);
121         mouseChanged(mousePos, bstate, wheel);
122         idx += packetsize;
123     }
124 
125     int surplus = mouseIdx - idx;
126     for (int i = 0; i < surplus; i++)
127         mouseBuf[i] = mouseBuf[idx+i];
128     mouseIdx = surplus;
129 }
130 
131 QT_END_NAMESPACE
132 
133 #endif // QT_NO_QWS_MOUSE_QVFB
134