on xenial help sign in
PhysicalKeysMapper: handle first power button long press correctly
bdbbb20Ratchanan Srirattanamet
qml/Components/PhysicalKeysMapper.qml+4−4
@@ -65,7 +65,7 @@ Item {
65 property bool superPressed: false
65 property bool superPressed: false
66 property bool superTabPressed: false
66 property bool superTabPressed: false
67
67
68− property var powerButtonPressStart: 0
68+ property var powerButtonPressStart: -1
69 }
69 }
70
70
71 InputEventGenerator {
71 InputEventGenerator {
@@ -75,9 +75,9 @@ Item {
75 function onKeyPressed(event, currentEventTimestamp) {
75 function onKeyPressed(event, currentEventTimestamp) {
76 if (event.key == Qt.Key_PowerDown || event.key == Qt.Key_PowerOff) {
76 if (event.key == Qt.Key_PowerDown || event.key == Qt.Key_PowerOff) {
77 if (event.isAutoRepeat) {
77 if (event.isAutoRepeat) {
78− if (d.powerButtonPressStart > 0
78+ if (d.powerButtonPressStart != -1
79 && currentEventTimestamp - d.powerButtonPressStart >= powerKeyLongPressTime) {
79 && currentEventTimestamp - d.powerButtonPressStart >= powerKeyLongPressTime) {
80− d.powerButtonPressStart = 0;
80+ d.powerButtonPressStart = -1;
81 root.powerKeyLongPressed();
81 root.powerKeyLongPressed();
82 }
82 }
83 } else {
83 } else {
@@ -132,7 +132,7 @@ Item {
132
132
133 function onKeyReleased(event, currentEventTimestamp) {
133 function onKeyReleased(event, currentEventTimestamp) {
134 if (event.key == Qt.Key_PowerDown || event.key == Qt.Key_PowerOff) {
134 if (event.key == Qt.Key_PowerDown || event.key == Qt.Key_PowerOff) {
135− d.powerButtonPressStart = 0;
135+ d.powerButtonPressStart = -1;
136 event.accepted = true;
136 event.accepted = true;
137 } else if (event.key == Qt.Key_VolumeDown) {
137 } else if (event.key == Qt.Key_VolumeDown) {
138 if (!d.ignoreVolumeEvents) root.volumeDownTriggered();
138 if (!d.ignoreVolumeEvents) root.volumeDownTriggered();
tests/qmltests/Components/tst_PhysicalKeysMapper.qml+6−5
@@ -95,26 +95,27 @@ Item {
95 Powerd.setStatus(data.status, Powerd.Unknown);
95 Powerd.setStatus(data.status, Powerd.Unknown);
96 physicalKeysMapper.powerKeyLongPressTime = 500;
96 physicalKeysMapper.powerKeyLongPressTime = 500;
97
97
98+ // The event's timestamp can start at 0, so make sure we handle this correctly.
98− physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: false }, 100);
99+ physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: false }, 0);
99
100
100 // After the first key press the screen is always on
101 // After the first key press the screen is always on
101 // and the rest of keypresses are auto repeat
102 // and the rest of keypresses are auto repeat
102 Powerd.setStatus(Powerd.On, Powerd.Unknown);
103 Powerd.setStatus(Powerd.On, Powerd.Unknown);
103
104
105+ physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 200);
104 physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 300);
106 physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 300);
105 physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 400);
107 physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 400);
106− physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 500);
108+ physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 499);
107− physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 599);
108
109
109 // powerKeyLongPressed should not have been emitted yet.
110 // powerKeyLongPressed should not have been emitted yet.
110 compare(powerSpy.count, 0);
111 compare(powerSpy.count, 0);
111
112
112− physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 600);
113+ physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 500);
113
114
114 compare(powerSpy.count, 1);
115 compare(powerSpy.count, 1);
115
116
116 // Confirm we only emit once
117 // Confirm we only emit once
117− physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 601);
118+ physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 501);
118 compare(powerSpy.count, 1);
119 compare(powerSpy.count, 1);
119 }
120 }
120
121