on xenial help sign in
Count the key events rather than wall time when considering showing the shutdown dialog
commitcde772bfe74ddccbbca5903a74f45a6b03fb79c4 authorMichael Terry <michael.terry@canonical.com> dateWed 21 Oct 2015, 16:30 (11 years ago) parentcab9943
2 files changed+24−26
qml/Components/PhysicalKeysMapper.qml+17−19 tests/qmltests/Components/tst_PhysicalKeysMapper.qml+7−7
qml/Components/PhysicalKeysMapper.qml+17−19
@@ -42,7 +42,7 @@ Item {
4242
4343 readonly property bool altTabPressed: d.altTabPressed
4444
45− property int powerKeyLongPressTime: 2000
45+ property int powerKeyLongPressCount: 31 // about 2s
4646
4747 // For testing. If running windowed (e.g. tryShell), Alt+Tab is taken by the
4848 // running desktop, set this to true to use Ctrl+Tab instead.
@@ -57,27 +57,26 @@ Item {
5757
5858 property bool altPressed: false
5959 property bool altTabPressed: false
60− }
6160
62− Timer {
63− id: powerKeyLongPressTimer
64− interval: root.powerKeyLongPressTime
65− onTriggered: root.powerKeyLongPressed();
61+ property int powerButtonCount
6662 }
6763
6864 function onKeyPressed(event) {
69− if ((event.key == Qt.Key_PowerDown || event.key == Qt.Key_PowerOff)
70− && (!event.isAutoRepeat || !powerKeyLongPressTimer.running)) {
71−
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();
65+ if (event.key == Qt.Key_PowerDown || event.key == Qt.Key_PowerOff) {
66+ if (event.isAutoRepeat) {
67+ d.powerButtonCount++;
68+
69+ // We count repeats rather than watch the time because if the
70+ // system is loaded, we may misinterpret a long wall-clock
71+ // delay as a long press, when in fact we just didn't get
72+ // around to noticing the key-release event in time. But if we
73+ // count events instead, we will be in sync with the event
74+ // queue.
75+ if (d.powerButtonCount === powerKeyLongPressCount) {
76+ root.powerKeyLongPressed();
77+ }
78+ } else {
79+ d.powerButtonCount = 0;
8180 }
8281 } else if ((event.key == Qt.Key_MediaTogglePlayPause || event.key == Qt.Key_MediaPlay) && !event.isAutoRepeat) {
8382 event.accepted = callManager.handleMediaKey(false);
@@ -115,7 +114,6 @@ Item {
115114
116115 function onKeyReleased(event) {
117116 if (event.key == Qt.Key_PowerDown || event.key == Qt.Key_PowerOff) {
118− powerKeyLongPressTimer.stop();
119117 event.accepted = true;
120118 } else if (event.key == Qt.Key_VolumeDown) {
121119 if (!d.ignoreVolumeEvents) root.volumeDownTriggered();
tests/qmltests/Components/tst_PhysicalKeysMapper.qml+7−7
@@ -65,7 +65,7 @@ TestCase {
6565
6666 function test_powerKeyLongPressed(data) {
6767 Powerd.setStatus(data.status, Powerd.Unknown);
68− physicalKeysMapper.powerKeyLongPressTime = 500;
68+ physicalKeysMapper.powerKeyLongPressCount = 5;
6969
7070 physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: false });
7171
@@ -73,19 +73,19 @@ TestCase {
7373 // and the rest of keypresses are auto repeat
7474 Powerd.setStatus(Powerd.On, Powerd.Unknown);
7575
76− for (var i = 0; i < 3; ++i) {
77− wait(10);
76+ for (var i = 0; i < 4; ++i) {
7877 physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true});
7978 }
8079
8180 // powerKeyLongPressed should not have been emitted yet.
8281 compare(powerSpy.count, 0);
8382
84− for (var i = 0; i < 10; ++i) {
85− wait(50);
86− physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true});
87− }
83+ // Fifth repeat event will trigger emission
84+ physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true});
85+ compare(powerSpy.count, 1);
8886
87+ // Make sure we don't double-emit
88+ physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true});
8989 compare(powerSpy.count, 1);
9090 }
9191