Test and refactor process_notification

Related #2273
This commit is contained in:
MattHag 2024-10-13 12:06:35 +02:00 committed by Peter F. Patel-Schneider
parent badb76953d
commit 06fd32b501
2 changed files with 72 additions and 42 deletions

View File

@ -1495,51 +1495,52 @@ def evaluate_rules(feature, notification: HIDPPNotification, device):
rules.evaluate(feature, notification, device, True) rules.evaluate(feature, notification, device, True)
# process a notification def process_notification(device, notification: HIDPPNotification, feature) -> None:
def process_notification(device, notification: HIDPPNotification, feature): """Processes HID++ notifications."""
global keys_down, g_keys_down, m_keys_down, mr_key_down, key_down, key_up, thumb_wheel_displacement global keys_down, g_keys_down, m_keys_down, mr_key_down, key_down, key_up, thumb_wheel_displacement
key_down, key_up = None, None key_down, key_up = None, None
# need to keep track of keys that are down to find a new key down # need to keep track of keys that are down to find a new key down
if feature == FEATURE.REPROG_CONTROLS_V4 and notification.address == 0x00: if notification.address == 0x00:
new_keys_down = struct.unpack("!4H", notification.data[:8]) if feature == FEATURE.REPROG_CONTROLS_V4:
for key in new_keys_down: new_keys_down = struct.unpack("!4H", notification.data[:8])
if key and key not in keys_down: for key in new_keys_down:
key_down = key if key and key not in keys_down:
for key in keys_down: key_down = key
if key and key not in new_keys_down: for key in keys_down:
key_up = key if key and key not in new_keys_down:
keys_down = new_keys_down key_up = key
# and also G keys down keys_down = new_keys_down
elif feature == FEATURE.GKEY and notification.address == 0x00: # and also G keys down
new_g_keys_down = struct.unpack("<I", notification.data[:4])[0] elif feature == FEATURE.GKEY:
for i in range(32): new_g_keys_down = struct.unpack("<I", notification.data[:4])[0]
if new_g_keys_down & (0x01 << i) and not g_keys_down & (0x01 << i): for i in range(32):
key_down = CONTROL["G" + str(i + 1)] if new_g_keys_down & (0x01 << i) and not g_keys_down & (0x01 << i):
if g_keys_down & (0x01 << i) and not new_g_keys_down & (0x01 << i): key_down = CONTROL["G" + str(i + 1)]
key_up = CONTROL["G" + str(i + 1)] if g_keys_down & (0x01 << i) and not new_g_keys_down & (0x01 << i):
g_keys_down = new_g_keys_down key_up = CONTROL["G" + str(i + 1)]
# and also M keys down g_keys_down = new_g_keys_down
elif feature == FEATURE.MKEYS and notification.address == 0x00: # and also M keys down
new_m_keys_down = struct.unpack("!1B", notification.data[:1])[0] elif feature == FEATURE.MKEYS:
for i in range(1, 9): new_m_keys_down = struct.unpack("!1B", notification.data[:1])[0]
if new_m_keys_down & (0x01 << (i - 1)) and not m_keys_down & (0x01 << (i - 1)): for i in range(1, 9):
key_down = CONTROL["M" + str(i)] if new_m_keys_down & (0x01 << (i - 1)) and not m_keys_down & (0x01 << (i - 1)):
if m_keys_down & (0x01 << (i - 1)) and not new_m_keys_down & (0x01 << (i - 1)): key_down = CONTROL["M" + str(i)]
key_up = CONTROL["M" + str(i)] if m_keys_down & (0x01 << (i - 1)) and not new_m_keys_down & (0x01 << (i - 1)):
m_keys_down = new_m_keys_down key_up = CONTROL["M" + str(i)]
# and also MR key m_keys_down = new_m_keys_down
elif feature == FEATURE.MR and notification.address == 0x00: # and also MR key
new_mr_key_down = struct.unpack("!1B", notification.data[:1])[0] elif feature == FEATURE.MR:
if not mr_key_down and new_mr_key_down: new_mr_key_down = struct.unpack("!1B", notification.data[:1])[0]
key_down = CONTROL["MR"] if not mr_key_down and new_mr_key_down:
if mr_key_down and not new_mr_key_down: key_down = CONTROL["MR"]
key_up = CONTROL["MR"] if mr_key_down and not new_mr_key_down:
mr_key_down = new_mr_key_down key_up = CONTROL["MR"]
# keep track of thumb wheel movment mr_key_down = new_mr_key_down
elif feature == FEATURE.THUMB_WHEEL and notification.address == 0x00: # keep track of thumb wheel movement
if notification.data[4] <= 0x01: # when wheel starts, zero out last movement elif feature == FEATURE.THUMB_WHEEL:
thumb_wheel_displacement = 0 if notification.data[4] <= 0x01: # when wheel starts, zero out last movement
thumb_wheel_displacement += signed(notification.data[0:2]) thumb_wheel_displacement = 0
thumb_wheel_displacement += signed(notification.data[0:2])
GLib.idle_add(evaluate_rules, feature, notification, device) GLib.idle_add(evaluate_rules, feature, notification, device)

View File

@ -6,6 +6,8 @@ from unittest.mock import mock_open
import pytest import pytest
from logitech_receiver import diversion from logitech_receiver import diversion
from logitech_receiver.base import HIDPPNotification
from logitech_receiver.hidpp20_constants import FEATURE
@pytest.fixture @pytest.fixture
@ -88,3 +90,30 @@ def test_key_is_down():
result = diversion.key_is_down(key=diversion.CONTROL.G2) result = diversion.key_is_down(key=diversion.CONTROL.G2)
assert result is False assert result is False
@pytest.mark.parametrize(
"feature, data",
[
(
FEATURE.REPROG_CONTROLS_V4,
[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08],
),
(FEATURE.GKEY, [0x01, 0x02, 0x03, 0x04]),
(FEATURE.MKEYS, [0x01, 0x02, 0x03, 0x04]),
(FEATURE.MR, [0x01, 0x02, 0x03, 0x04]),
(FEATURE.THUMB_WHEEL, [0x01, 0x02, 0x03, 0x04, 0x05]),
(FEATURE.DEVICE_UNIT_ID, [0x01, 0x02, 0x03, 0x04, 0x05]),
],
)
def test_process_notification(feature, data):
device_mock = mock.Mock()
notification = HIDPPNotification(
report_id=0x01,
devnumber=1,
sub_id=0x13,
address=0x00,
data=bytes(data),
)
diversion.process_notification(device_mock, notification, feature)