MiniBrowserApplication.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
  3. * Copyright (C) 2010 University of Szeged
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  20. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  24. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "config.h"
  29. #include "MiniBrowserApplication.h"
  30. #include "BrowserWindow.h"
  31. #if HAVE(QTTESTSUPPORT)
  32. #include "QtTestSupport.h"
  33. #endif
  34. #include "private/qquickwebview_p.h"
  35. #include "utils.h"
  36. #include <QRegExp>
  37. #include <QEvent>
  38. #include <QMouseEvent>
  39. #include <QTouchEvent>
  40. static inline QRectF touchRectForPosition(QPointF centerPoint)
  41. {
  42. QRectF touchRect(0, 0, 40, 40);
  43. touchRect.moveCenter(centerPoint);
  44. return touchRect;
  45. }
  46. static inline bool isTouchEvent(const QEvent* event)
  47. {
  48. switch (event->type()) {
  49. case QEvent::TouchBegin:
  50. case QEvent::TouchUpdate:
  51. case QEvent::TouchEnd:
  52. return true;
  53. default:
  54. return false;
  55. }
  56. }
  57. static inline bool isMouseEvent(const QEvent* event)
  58. {
  59. switch (event->type()) {
  60. case QEvent::MouseButtonPress:
  61. case QEvent::MouseMove:
  62. case QEvent::MouseButtonRelease:
  63. case QEvent::MouseButtonDblClick:
  64. return true;
  65. default:
  66. return false;
  67. }
  68. }
  69. MiniBrowserApplication::MiniBrowserApplication(int& argc, char** argv)
  70. : QGuiApplication(argc, argv)
  71. , m_realTouchEventReceived(false)
  72. , m_pendingFakeTouchEventCount(0)
  73. , m_isRobotized(false)
  74. , m_robotTimeoutSeconds(0)
  75. , m_robotExtraTimeSeconds(0)
  76. , m_windowOptions(this)
  77. , m_holdingControl(false)
  78. {
  79. setOrganizationName("Nokia");
  80. setApplicationName("QtMiniBrowser");
  81. setApplicationVersion("0.1");
  82. handleUserOptions();
  83. }
  84. bool MiniBrowserApplication::notify(QObject* target, QEvent* event)
  85. {
  86. // We try to be smart, if we received real touch event, we are probably on a device
  87. // with touch screen, and we should not have touch mocking.
  88. if (!event->spontaneous() || m_realTouchEventReceived || !m_windowOptions.touchMockingEnabled())
  89. return QGuiApplication::notify(target, event);
  90. if (isTouchEvent(event)) {
  91. if (m_pendingFakeTouchEventCount)
  92. --m_pendingFakeTouchEventCount;
  93. else
  94. m_realTouchEventReceived = true;
  95. return QGuiApplication::notify(target, event);
  96. }
  97. BrowserWindow* browserWindow = qobject_cast<BrowserWindow*>(target);
  98. if (!browserWindow)
  99. return QGuiApplication::notify(target, event);
  100. m_holdingControl = QGuiApplication::keyboardModifiers().testFlag(Qt::ControlModifier);
  101. // In QML events are propagated through parents. But since the WebView
  102. // may consume key events, a shortcut might never reach the top QQuickItem.
  103. // Therefore we are checking here for shortcuts.
  104. if (event->type() == QEvent::KeyPress) {
  105. QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
  106. if ((keyEvent->key() == Qt::Key_R && keyEvent->modifiers() == Qt::ControlModifier) || keyEvent->key() == Qt::Key_F5) {
  107. browserWindow->reload();
  108. return true;
  109. }
  110. if ((keyEvent->key() == Qt::Key_L && keyEvent->modifiers() == Qt::ControlModifier) || keyEvent->key() == Qt::Key_F6) {
  111. browserWindow->focusAddressBar();
  112. return true;
  113. }
  114. if ((keyEvent->key() == Qt::Key_F && keyEvent->modifiers() == Qt::ControlModifier) || keyEvent->key() == Qt::Key_F3) {
  115. browserWindow->toggleFind();
  116. return true;
  117. }
  118. }
  119. if (event->type() == QEvent::KeyRelease && static_cast<QKeyEvent*>(event)->key() == Qt::Key_Control) {
  120. foreach (int id, m_heldTouchPoints)
  121. if (m_touchPoints.contains(id) && !QGuiApplication::mouseButtons().testFlag(Qt::MouseButton(id))) {
  122. m_touchPoints[id].setState(Qt::TouchPointReleased);
  123. m_heldTouchPoints.remove(id);
  124. } else
  125. m_touchPoints[id].setState(Qt::TouchPointStationary);
  126. sendTouchEvent(browserWindow, m_heldTouchPoints.isEmpty() ? QEvent::TouchEnd : QEvent::TouchUpdate, static_cast<QKeyEvent*>(event)->timestamp());
  127. }
  128. if (isMouseEvent(event)) {
  129. const QMouseEvent* const mouseEvent = static_cast<QMouseEvent*>(event);
  130. QTouchEvent::TouchPoint touchPoint;
  131. touchPoint.setPressure(1);
  132. QEvent::Type touchType = QEvent::None;
  133. switch (mouseEvent->type()) {
  134. case QEvent::MouseButtonPress:
  135. touchPoint.setId(mouseEvent->button());
  136. if (m_touchPoints.contains(touchPoint.id())) {
  137. touchPoint.setState(Qt::TouchPointMoved);
  138. touchType = QEvent::TouchUpdate;
  139. } else {
  140. touchPoint.setState(Qt::TouchPointPressed);
  141. // Check if more buttons are held down than just the event triggering one.
  142. if (mouseEvent->buttons() > mouseEvent->button())
  143. touchType = QEvent::TouchUpdate;
  144. else
  145. touchType = QEvent::TouchBegin;
  146. }
  147. break;
  148. case QEvent::MouseMove:
  149. if (!mouseEvent->buttons()) {
  150. // We have to swallow the event instead of propagating it,
  151. // since we avoid sending the mouse release events and if the
  152. // Flickable is the mouse grabber it would receive the event
  153. // and would move the content.
  154. event->accept();
  155. return true;
  156. }
  157. touchType = QEvent::TouchUpdate;
  158. touchPoint.setId(mouseEvent->buttons());
  159. touchPoint.setState(Qt::TouchPointMoved);
  160. break;
  161. case QEvent::MouseButtonRelease:
  162. // Check if any buttons are still held down after this event.
  163. if (mouseEvent->buttons())
  164. touchType = QEvent::TouchUpdate;
  165. else
  166. touchType = QEvent::TouchEnd;
  167. touchPoint.setId(mouseEvent->button());
  168. touchPoint.setState(Qt::TouchPointReleased);
  169. break;
  170. case QEvent::MouseButtonDblClick:
  171. // Eat double-clicks, their accompanying press event is all we need.
  172. event->accept();
  173. return true;
  174. default:
  175. Q_ASSERT_X(false, "multi-touch mocking", "unhandled event type");
  176. }
  177. // A move can have resulted in multiple buttons, so we need check them individually.
  178. if (touchPoint.id() & Qt::LeftButton)
  179. updateTouchPoint(mouseEvent, touchPoint, Qt::LeftButton);
  180. if (touchPoint.id() & Qt::MidButton)
  181. updateTouchPoint(mouseEvent, touchPoint, Qt::MidButton);
  182. if (touchPoint.id() & Qt::RightButton)
  183. updateTouchPoint(mouseEvent, touchPoint, Qt::RightButton);
  184. if (m_holdingControl && touchPoint.state() == Qt::TouchPointReleased) {
  185. // We avoid sending the release event because the Flickable is
  186. // listening to mouse events and would start a bounce-back
  187. // animation if it received a mouse release.
  188. event->accept();
  189. return true;
  190. }
  191. // Update states for all other touch-points
  192. for (QHash<int, QTouchEvent::TouchPoint>::iterator it = m_touchPoints.begin(), end = m_touchPoints.end(); it != end; ++it) {
  193. if (!(it.value().id() & touchPoint.id()))
  194. it.value().setState(Qt::TouchPointStationary);
  195. }
  196. Q_ASSERT(touchType != QEvent::None);
  197. if (!sendTouchEvent(browserWindow, touchType, mouseEvent->timestamp()))
  198. return QGuiApplication::notify(target, event);
  199. event->accept();
  200. return true;
  201. }
  202. return QGuiApplication::notify(target, event);
  203. }
  204. void MiniBrowserApplication::updateTouchPoint(const QMouseEvent* mouseEvent, QTouchEvent::TouchPoint touchPoint, Qt::MouseButton mouseButton)
  205. {
  206. // Ignore inserting additional touch points if Ctrl isn't held because it produces
  207. // inconsistent touch events and results in assers in the gesture recognizers.
  208. if (!m_holdingControl && m_touchPoints.size() && !m_touchPoints.contains(mouseButton))
  209. return;
  210. if (m_holdingControl && touchPoint.state() == Qt::TouchPointReleased) {
  211. m_heldTouchPoints.insert(mouseButton);
  212. return;
  213. }
  214. // Gesture recognition uses the screen position for the initial threshold
  215. // but since the canvas translates touch events we actually need to pass
  216. // the screen position as the scene position to deliver the appropriate
  217. // coordinates to the target.
  218. touchPoint.setRect(touchRectForPosition(mouseEvent->localPos()));
  219. touchPoint.setSceneRect(touchRectForPosition(mouseEvent->screenPos()));
  220. if (touchPoint.state() == Qt::TouchPointPressed)
  221. touchPoint.setStartScenePos(mouseEvent->screenPos());
  222. else {
  223. const QTouchEvent::TouchPoint& oldTouchPoint = m_touchPoints[mouseButton];
  224. touchPoint.setStartScenePos(oldTouchPoint.startScenePos());
  225. touchPoint.setLastPos(oldTouchPoint.pos());
  226. touchPoint.setLastScenePos(oldTouchPoint.scenePos());
  227. }
  228. // Update current touch-point.
  229. touchPoint.setId(mouseButton);
  230. m_touchPoints.insert(mouseButton, touchPoint);
  231. }
  232. bool MiniBrowserApplication::sendTouchEvent(BrowserWindow* browserWindow, QEvent::Type type, ulong timestamp)
  233. {
  234. static QTouchDevice* device = 0;
  235. if (!device) {
  236. device = new QTouchDevice;
  237. device->setType(QTouchDevice::TouchScreen);
  238. QWindowSystemInterface::registerTouchDevice(device);
  239. }
  240. m_pendingFakeTouchEventCount++;
  241. const QList<QTouchEvent::TouchPoint>& currentTouchPoints = m_touchPoints.values();
  242. Qt::TouchPointStates touchPointStates = 0;
  243. foreach (const QTouchEvent::TouchPoint& touchPoint, currentTouchPoints)
  244. touchPointStates |= touchPoint.state();
  245. QTouchEvent event(type, device, Qt::NoModifier, touchPointStates, currentTouchPoints);
  246. event.setTimestamp(timestamp);
  247. event.setAccepted(false);
  248. QGuiApplication::notify(browserWindow, &event);
  249. if (QQuickWebViewExperimental::flickableViewportEnabled())
  250. browserWindow->updateVisualMockTouchPoints(m_holdingControl ? currentTouchPoints : QList<QTouchEvent::TouchPoint>());
  251. // Get rid of touch-points that are no longer valid
  252. foreach (const QTouchEvent::TouchPoint& touchPoint, currentTouchPoints) {
  253. if (touchPoint.state() == Qt::TouchPointReleased)
  254. m_touchPoints.remove(touchPoint.id());
  255. }
  256. return event.isAccepted();
  257. }
  258. static void printHelp(const QString& programName)
  259. {
  260. qDebug() << "Usage:" << programName.toLatin1().data()
  261. << "[--desktop]"
  262. << "[-r list]"
  263. << "[--robot-timeout seconds]"
  264. << "[--robot-extra-time seconds]"
  265. << "[--window-size (width)x(height)]"
  266. << "[--maximize]"
  267. << "[-f] Full screen mode."
  268. << "[--user-agent string]"
  269. << "[-v]"
  270. << "URL";
  271. }
  272. void MiniBrowserApplication::handleUserOptions()
  273. {
  274. QStringList args = arguments();
  275. QFileInfo program(args.takeAt(0));
  276. QString programName("MiniBrowser");
  277. if (program.exists())
  278. programName = program.baseName();
  279. if (takeOptionFlag(&args, "--help")) {
  280. printHelp(programName);
  281. appQuit(0);
  282. }
  283. const bool useDesktopBehavior = takeOptionFlag(&args, "--desktop");
  284. if (useDesktopBehavior)
  285. windowOptions()->setTouchMockingEnabled(false);
  286. QQuickWebViewExperimental::setFlickableViewportEnabled(!useDesktopBehavior);
  287. if (!useDesktopBehavior)
  288. qputenv("QT_WEBKIT_USE_MOBILE_THEME", QByteArray("1"));
  289. m_windowOptions.setPrintLoadedUrls(takeOptionFlag(&args, "-v"));
  290. m_windowOptions.setStartMaximized(takeOptionFlag(&args, "--maximize"));
  291. m_windowOptions.setStartFullScreen(takeOptionFlag(&args, "-f"));
  292. if (args.contains("--user-agent"))
  293. m_windowOptions.setUserAgent(takeOptionValue(&args, "--user-agent"));
  294. if (args.contains("--window-size")) {
  295. QString value = takeOptionValue(&args, "--window-size");
  296. QStringList list = value.split(QRegExp("\\D+"), QString::SkipEmptyParts);
  297. if (list.length() == 2)
  298. m_windowOptions.setRequestedWindowSize(QSize(list.at(0).toInt(), list.at(1).toInt()));
  299. }
  300. #if HAVE(QTTESTSUPPORT)
  301. if (takeOptionFlag(&args, QStringLiteral("--use-test-fonts")))
  302. WebKit::QtTestSupport::initializeTestFonts();
  303. #endif
  304. if (args.contains("-r")) {
  305. QString listFile = takeOptionValue(&args, "-r");
  306. if (listFile.isEmpty())
  307. appQuit(1, "-r needs a list file to start in robotized mode");
  308. if (!QFile::exists(listFile))
  309. appQuit(1, "The list file supplied to -r does not exist.");
  310. m_isRobotized = true;
  311. m_urls = QStringList(listFile);
  312. // toInt() returns 0 if it fails parsing.
  313. m_robotTimeoutSeconds = takeOptionValue(&args, "--robot-timeout").toInt();
  314. m_robotExtraTimeSeconds = takeOptionValue(&args, "--robot-extra-time").toInt();
  315. } else {
  316. int urlArg;
  317. while ((urlArg = args.indexOf(QRegExp("^[^-].*"))) != -1)
  318. m_urls += args.takeAt(urlArg);
  319. }
  320. if (!args.isEmpty()) {
  321. printHelp(programName);
  322. appQuit(1, QString("Unknown argument(s): %1").arg(args.join(",")));
  323. }
  324. }