on c42e54e help sign in
unity8/qml/Components/PhysicalKeysMapper.qml
113 lines 4.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
34*/
36Item {
37 id: root
39 signal powerKeyLongPressed;
40 signal volumeDownTriggered;
41 signal volumeUpTriggered;
42 signal screenshotTriggered;
44 property int powerKeyLongPressTime: 2000
46 QtObject {
47 id: d
49 property bool volumeDownKeyPressed: false
50 property bool volumeUpKeyPressed: false
51 property bool ignoreVolumeEvents: false
52 }
54 Timer {
55 id: powerKeyLongPressTimer
56 interval: root.powerKeyLongPressTime
57 onTriggered: root.powerKeyLongPressed();
58 }
60 function onKeyPressed(event) {
61 if ((event.key == Qt.Key_PowerDown || event.key == Qt.Key_PowerOff)
62 && !event.isAutoRepeat) {
64 // FIXME: We only consider power key presses if the screen is
65 // on because of bugs 1410830/1409003. The theory is that when
66 // those bugs are encountered, there is a >2s delay between the
67 // power press event and the power release event, which causes
68 // the shutdown dialog to appear on resume. So to avoid that
69 // symptom while we investigate the root cause, we simply won't
70 // initiate any dialogs when the screen is off.
71 if (Powerd.status === Powerd.On) {
72 powerKeyLongPressTimer.restart();
73 }
74 event.accepted = true;
75 } else if ((event.key == Qt.Key_MediaTogglePlayPause || event.key == Qt.Key_MediaPlay)
76 && !event.isAutoRepeat) {
77 event.accepted = callManager.handleMediaKey(false);
78 } else if (event.key == Qt.Key_VolumeDown) {
79 if (event.isAutoRepeat && !d.ignoreVolumeEvents) root.volumeDownTriggered();
80 else if (!event.isAutoRepeat) {
81 if (d.volumeUpKeyPressed) {
82 if (Powerd.status === Powerd.On) root.screenshotTriggered();
83 d.ignoreVolumeEvents = true;
84 }
85 d.volumeDownKeyPressed = true;
86 }
87 } else if (event.key == Qt.Key_VolumeUp) {
88 if (event.isAutoRepeat && !d.ignoreVolumeEvents) root.volumeUpTriggered();
89 else if (!event.isAutoRepeat) {
90 if (d.volumeDownKeyPressed) {
91 if (Powerd.status === Powerd.On) root.screenshotTriggered();
92 d.ignoreVolumeEvents = true;
93 }
94 d.volumeUpKeyPressed = true;
95 }
96 }
97 }
99 function onKeyReleased(event) {
100 if (event.key == Qt.Key_PowerDown || event.key == Qt.Key_PowerOff) {
101 powerKeyLongPressTimer.stop();
102 event.accepted = true;
103 } else if (event.key == Qt.Key_VolumeDown) {
104 if (!d.ignoreVolumeEvents) root.volumeDownTriggered();
105 d.volumeDownKeyPressed = false;
106 if (!d.volumeUpKeyPressed) d.ignoreVolumeEvents = false;
107 } else if (event.key == Qt.Key_VolumeUp) {
108 if (!d.ignoreVolumeEvents) root.volumeUpTriggered();
109 d.volumeUpKeyPressed = false;
110 if (!d.volumeDownKeyPressed) d.ignoreVolumeEvents = false;
111 }
112 }