Unity 8
HomeKeyWatcher.h
1 /*
2  * Copyright (C) 2015 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef UNITY_HOMEKEYWATCHER_H
18 #define UNITY_HOMEKEYWATCHER_H
19 
20 #include <QQuickItem>
21 #include <QPointer>
22 
23 #include "Timer.h"
24 #include "ElapsedTimer.h"
25 
26 /*
27  Signals when the home key seems to be have been intentionally tapped.
28 
29  It only says the home key has been activated if it has been tapped in isolation,
30  that is, without being accompanied by touches on the screen. Home key taps that
31  happen along with (or immediately after, or immediately before) touches on the
32  screen are considered to have happened unintentionally and are thus ignored.
33 
34  Rationale being that it's easy to accidentally hit the home key while performing
35  a swipe from a screen edge, for instance. That's particularly the case when the
36  home key is a capacitive key.
37  */
38 class HomeKeyWatcher : public QQuickItem
39 {
40  Q_OBJECT
41 public:
42 
43  HomeKeyWatcher(QQuickItem *parent = 0);
44 
45  // for testing
46  HomeKeyWatcher(UnityUtil::AbstractTimer *timer,
47  UnityUtil::AbstractElapsedTimer *elapsedTimer,
48  QQuickItem *parent = 0);
49 
50  virtual ~HomeKeyWatcher();
51 
52  bool eventFilter(QObject *watched, QEvent *event) override;
53 
54  void update(QEvent *event);
55 
56  const qint64 msecsWithoutTouches = 150;
57 
58 Q_SIGNALS:
59  // Emitted when the home key has been intentionally tapped
60  void activated();
61 
62 private Q_SLOTS:
63  void setupFilterOnWindow(QQuickWindow *window);
64  void emitActivatedIfNoTouchesAround();
65 
66 private:
67  QPointer<QQuickWindow> m_filteredWindow;
68  bool m_windowBeingTouched;
69  bool m_homeKeyPressed;
70  UnityUtil::AbstractElapsedTimer *m_windowLastTouchedTimer;
71  UnityUtil::AbstractTimer *m_activationTimer;
72 };
73 
74 #endif // UNITY_HOMEKEYWATCHER_H