on e63da72 help sign in
unity8/qml/Components/PhysicalKeysMapper.qml
136 lines 5.1 kB history raw
1/*
2 * Copyright (C) 2014-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 */
17import QtQuick 2.0
18import Powerd 0.1
20/*!
21 \brief A mapper for the physical keys on the device
23 A mapper to handle events triggered by pressing physical keys on a device.
24 Keys included are
25 * Volume Decrease
26 * Volume Increase
27 * Power
29 This allows for handling the following events
30 * Power dialog
31 * Volume Decreases/Increases
32 * Screenshots
33*/
35Item {
36 id: root
38 signal powerKeyLongPressed;
39 signal volumeDownTriggered;
40 signal volumeUpTriggered;
41 signal screenshotTriggered;
43 readonly property bool altTabPressed: d.altTabPressed
45 property int powerKeyLongPressTime: 2000
47 // For testing. If running windowed (e.g. tryShell), Alt+Tab is taken by the
48 // running desktop, set this to true to use Ctrl+Tab instead.
49 property bool controlInsteadOfAlt: false
51 QtObject {
52 id: d
54 property bool volumeDownKeyPressed: false
55 property bool volumeUpKeyPressed: false
56 property bool ignoreVolumeEvents: false
58 property bool altPressed: false
59 property bool altTabPressed: false
60 }
62 Timer {
63 id: powerKeyLongPressTimer
64 interval: root.powerKeyLongPressTime
65 onTriggered: root.powerKeyLongPressed();
66 }
68 function onKeyPressed(event) {
69 if ((event.key == Qt.Key_PowerDown || event.key == Qt.Key_PowerOff)
70 && (!event.isAutoRepeat || !powerKeyLongPressTimer.running)) {
72 // FIXME: We only consider power key presses if the screen is
73 // on because of bugs 1410830/1409003. The theory is that when
74 // those bugs are encountered, there is a >2s delay between the
75 // power press event and the power release event, which causes
76 // the shutdown dialog to appear on resume. So to avoid that
77 // symptom while we investigate the root cause, we simply won't
78 // initiate any dialogs when the screen is off.
79 if (Powerd.status === Powerd.On) {
80 powerKeyLongPressTimer.restart();
81 }
82 } else if ((event.key == Qt.Key_MediaTogglePlayPause || event.key == Qt.Key_MediaPlay) && !event.isAutoRepeat) {
83 event.accepted = callManager.handleMediaKey(false);
84 } else if (event.key == Qt.Key_VolumeDown) {
85 if (event.isAutoRepeat && !d.ignoreVolumeEvents) root.volumeDownTriggered();
86 else if (!event.isAutoRepeat) {
87 if (d.volumeUpKeyPressed) {
88 if (Powerd.status === Powerd.On) {
89 root.screenshotTriggered();
90 }
91 d.ignoreVolumeEvents = true;
92 }
93 d.volumeDownKeyPressed = true;
94 }
95 } else if (event.key == Qt.Key_VolumeUp) {
96 if (event.isAutoRepeat && !d.ignoreVolumeEvents) root.volumeUpTriggered();
97 else if (!event.isAutoRepeat) {
98 if (d.volumeDownKeyPressed) {
99 if (Powerd.status === Powerd.On) {
100 root.screenshotTriggered();
101 }
102 d.ignoreVolumeEvents = true;
103 }
104 d.volumeUpKeyPressed = true;
105 }
106 } else if (event.key == Qt.Key_Alt || (root.controlInsteadOfAlt && event.key == Qt.Key_Control)) {
107 d.altPressed = true;
108 } else if (event.key == Qt.Key_Tab) {
109 if (d.altPressed && !d.altTabPressed) {
110 d.altTabPressed = true;
111 event.accepted = true;
112 }
113 }
114 }
116 function onKeyReleased(event) {
117 if (event.key == Qt.Key_PowerDown || event.key == Qt.Key_PowerOff) {
118 powerKeyLongPressTimer.stop();
119 event.accepted = true;
120 } else if (event.key == Qt.Key_VolumeDown) {
121 if (!d.ignoreVolumeEvents) root.volumeDownTriggered();
122 d.volumeDownKeyPressed = false;
123 if (!d.volumeUpKeyPressed) d.ignoreVolumeEvents = false;
124 } else if (event.key == Qt.Key_VolumeUp) {
125 if (!d.ignoreVolumeEvents) root.volumeUpTriggered();
126 d.volumeUpKeyPressed = false;
127 if (!d.volumeDownKeyPressed) d.ignoreVolumeEvents = false;
128 } else if (event.key == Qt.Key_Alt || (root.controlInsteadOfAlt && event.key == Qt.Key_Control)) {
129 d.altPressed = false;
130 if (d.altTabPressed) {
131 d.altTabPressed = false;
132 event.accepted = true;
133 }
134 }
135 }