PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/platformsupport/input/evdevmouse/qevdevmousehandler.cpp

https://bitbucket.org/cvp2ri/qt5-tlsauth
C++ | 239 lines | 164 code | 24 blank | 51 comment | 75 complexity | 612402e3db370c953025a888668f3196 MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  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 Digia. For licensing terms and
  14. ** conditions see http://qt.digia.com/licensing. For further information
  15. ** use the contact form at http://qt.digia.com/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 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 2.1 requirements
  23. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  24. **
  25. ** In addition, as a special exception, Digia gives you certain additional
  26. ** rights. These rights are described in the Digia Qt LGPL Exception
  27. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  28. **
  29. ** GNU General Public License Usage
  30. ** Alternatively, this file may be used under the terms of the GNU
  31. ** General Public License version 3.0 as published by the Free Software
  32. ** Foundation and appearing in the file LICENSE.GPL included in the
  33. ** packaging of this file. Please review the following information to
  34. ** ensure the GNU General Public License version 3.0 requirements will be
  35. ** met: http://www.gnu.org/copyleft/gpl.html.
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "qevdevmousehandler_p.h"
  42. #include <QSocketNotifier>
  43. #include <QStringList>
  44. #include <QPoint>
  45. #include <QGuiApplication>
  46. #include <QScreen>
  47. #include <qpa/qwindowsysteminterface.h>
  48. #include <qplatformdefs.h>
  49. #include <private/qcore_unix_p.h> // overrides QT_OPEN
  50. #include <errno.h>
  51. #include <linux/kd.h>
  52. #include <linux/input.h>
  53. #include <qdebug.h>
  54. //#define QT_QPA_MOUSE_HANDLER_DEBUG
  55. QT_BEGIN_NAMESPACE
  56. QEvdevMouseHandler *QEvdevMouseHandler::create(const QString &device, const QString &specification)
  57. {
  58. #ifdef QT_QPA_MOUSE_HANDLER_DEBUG
  59. qWarning() << "Try to create mouse handler for" << device << specification;
  60. #endif
  61. bool compression = true;
  62. int jitterLimit = 0;
  63. QStringList args = specification.split(QLatin1Char(':'));
  64. foreach (const QString &arg, args) {
  65. if (arg == QLatin1String("nocompress"))
  66. compression = false;
  67. else if (arg.startsWith(QLatin1String("dejitter=")))
  68. jitterLimit = arg.mid(9).toInt();
  69. }
  70. int fd;
  71. fd = qt_safe_open(device.toLocal8Bit().constData(), O_RDONLY | O_NDELAY, 0);
  72. if (fd >= 0) {
  73. return new QEvdevMouseHandler(device, fd, compression, jitterLimit);
  74. } else {
  75. qWarning("Cannot open mouse input device '%s': %s", qPrintable(device), strerror(errno));
  76. return 0;
  77. }
  78. }
  79. QEvdevMouseHandler::QEvdevMouseHandler(const QString &device, int fd, bool compression, int jitterLimit)
  80. : m_device(device), m_fd(fd), m_notify(0), m_x(0), m_y(0), m_prevx(0), m_prevy(0),
  81. m_compression(compression), m_buttons(0), m_prevInvalid(true)
  82. {
  83. setObjectName(QLatin1String("Evdev Mouse Handler"));
  84. m_jitterLimitSquared = jitterLimit * jitterLimit;
  85. // socket notifier for events on the mouse device
  86. QSocketNotifier *notifier;
  87. notifier = new QSocketNotifier(m_fd, QSocketNotifier::Read, this);
  88. connect(notifier, SIGNAL(activated(int)), this, SLOT(readMouseData()));
  89. }
  90. QEvdevMouseHandler::~QEvdevMouseHandler()
  91. {
  92. if (m_fd >= 0)
  93. qt_safe_close(m_fd);
  94. }
  95. void QEvdevMouseHandler::sendMouseEvent()
  96. {
  97. int x = m_x - m_prevx;
  98. int y = m_y - m_prevy;
  99. if (m_prevInvalid) {
  100. x = y = 0;
  101. m_prevInvalid = false;
  102. }
  103. emit handleMouseEvent(x, y, m_buttons);
  104. m_prevx = m_x;
  105. m_prevy = m_y;
  106. }
  107. void QEvdevMouseHandler::readMouseData()
  108. {
  109. struct ::input_event buffer[32];
  110. int n = 0;
  111. bool posChanged = false, btnChanged = false;
  112. bool pendingMouseEvent = false;
  113. int eventCompressCount = 0;
  114. forever {
  115. int result = QT_READ(m_fd, reinterpret_cast<char *>(buffer) + n, sizeof(buffer) - n);
  116. if (result == 0) {
  117. qWarning("Got EOF from the input device.");
  118. return;
  119. } else if (result < 0) {
  120. if (errno != EINTR && errno != EAGAIN) {
  121. qWarning("Could not read from input device: %s", strerror(errno));
  122. return;
  123. }
  124. } else {
  125. n += result;
  126. if (n % sizeof(buffer[0]) == 0)
  127. break;
  128. }
  129. }
  130. n /= sizeof(buffer[0]);
  131. for (int i = 0; i < n; ++i) {
  132. struct ::input_event *data = &buffer[i];
  133. //qDebug() << ">>" << hex << data->type << data->code << dec << data->value;
  134. if (data->type == EV_ABS) {
  135. // Touchpads: store the absolute position for now, will calculate a relative one later.
  136. if (data->code == ABS_X && m_x != data->value) {
  137. m_x = data->value;
  138. posChanged = true;
  139. } else if (data->code == ABS_Y && m_y != data->value) {
  140. m_y = data->value;
  141. posChanged = true;
  142. }
  143. } else if (data->type == EV_REL) {
  144. if (data->code == REL_X) {
  145. m_x += data->value;
  146. posChanged = true;
  147. } else if (data->code == REL_Y) {
  148. m_y += data->value;
  149. posChanged = true;
  150. } else if (data->code == ABS_WHEEL) { // vertical scroll
  151. // data->value: 1 == up, -1 == down
  152. const int delta = 120 * data->value;
  153. emit handleWheelEvent(delta, Qt::Vertical);
  154. } else if (data->code == ABS_THROTTLE) { // horizontal scroll
  155. // data->value: 1 == right, -1 == left
  156. const int delta = 120 * -data->value;
  157. emit handleWheelEvent(delta, Qt::Horizontal);
  158. }
  159. } else if (data->type == EV_KEY && data->code == BTN_TOUCH) {
  160. // We care about touchpads only, not touchscreens -> don't map to button press.
  161. // Need to invalidate prevx/y however to get proper relative pos.
  162. m_prevInvalid = true;
  163. } else if (data->type == EV_KEY && data->code >= BTN_LEFT && data->code <= BTN_JOYSTICK) {
  164. Qt::MouseButton button = Qt::NoButton;
  165. // BTN_LEFT == 0x110 in kernel's input.h
  166. // The range of possible mouse buttons ends just before BTN_JOYSTICK, value 0x120.
  167. switch (data->code) {
  168. case 0x110: button = Qt::LeftButton; break; // BTN_LEFT
  169. case 0x111: button = Qt::RightButton; break;
  170. case 0x112: button = Qt::MiddleButton; break;
  171. case 0x113: button = Qt::ExtraButton1; break; // AKA Qt::BackButton
  172. case 0x114: button = Qt::ExtraButton2; break; // AKA Qt::ForwardButton
  173. case 0x115: button = Qt::ExtraButton3; break; // AKA Qt::TaskButton
  174. case 0x116: button = Qt::ExtraButton4; break;
  175. case 0x117: button = Qt::ExtraButton5; break;
  176. case 0x118: button = Qt::ExtraButton6; break;
  177. case 0x119: button = Qt::ExtraButton7; break;
  178. case 0x11a: button = Qt::ExtraButton8; break;
  179. case 0x11b: button = Qt::ExtraButton9; break;
  180. case 0x11c: button = Qt::ExtraButton10; break;
  181. case 0x11d: button = Qt::ExtraButton11; break;
  182. case 0x11e: button = Qt::ExtraButton12; break;
  183. case 0x11f: button = Qt::ExtraButton13; break;
  184. }
  185. if (data->value)
  186. m_buttons |= button;
  187. else
  188. m_buttons &= ~button;
  189. btnChanged = true;
  190. } else if (data->type == EV_SYN && data->code == SYN_REPORT) {
  191. if (btnChanged) {
  192. btnChanged = posChanged = false;
  193. sendMouseEvent();
  194. pendingMouseEvent = false;
  195. } else if (posChanged) {
  196. posChanged = false;
  197. if (m_compression) {
  198. pendingMouseEvent = true;
  199. eventCompressCount++;
  200. } else {
  201. sendMouseEvent();
  202. }
  203. }
  204. } else if (data->type == EV_MSC && data->code == MSC_SCAN) {
  205. // kernel encountered an unmapped key - just ignore it
  206. continue;
  207. }
  208. }
  209. if (m_compression && pendingMouseEvent) {
  210. int distanceSquared = (m_x - m_prevx)*(m_x - m_prevx) + (m_y - m_prevy)*(m_y - m_prevy);
  211. if (distanceSquared > m_jitterLimitSquared)
  212. sendMouseEvent();
  213. }
  214. }
  215. QT_END_NAMESPACE