ソースを参照

Ignore autorepeat except on up/down/prev/next

A held List key repeated 300 ms later and closed the playlist it had just
opened. Key sources now tell repeats from presses (evdev and the key
expander by value 2, the X windows by a press without a release), and the
app acts on repeats only for up/down and prev/next.
k4be 2 週間 前
親
コミット
b33aa82953
6 ファイル変更、50 行追加、25 行削除
  1. 6 2
      radio/app.py
  2. 27 10
      radio/gtkkeys.py
  3. 2 2
      radio/keypad.py
  4. 2 2
      radio/platform.py
  5. 1 8
      radio/sim.py
  6. 12 1
      tests/test_ui.py

+ 6 - 2
radio/app.py

@@ -14,6 +14,9 @@ from . import state as st
 
 BACKLIGHT_TIMEOUT_S = 30
 LIST_TIMEOUT_S = 30         # the playlist closes after this long without a key
+# keys that act again while held (at the lockout rate); a held List would
+# otherwise close the playlist it just opened
+REPEATING_KEYS = ("up", "down", "prev", "next")
 KEY_LOCKOUT_MS = 300        # as the firmware: 300 ms after a press
 LOG_REDRAW_MS = 1000        # scrolling is the costliest redraw
 
@@ -129,8 +132,9 @@ class App(object):
         self.backlight_id = GLib.timeout_add_seconds(BACKLIGHT_TIMEOUT_S,
                                                      self._backlight_expired)
 
-    def key(self, name):
-        if name not in st.KEYS:
+    def key(self, name, repeat=False):
+        """repeat: an autorepeat of a held key, not a new press"""
+        if name not in st.KEYS or (repeat and name not in REPEATING_KEYS):
             return
         now = st.monotonic_ms()
         last = self.key_time_ms.get(name)

+ 27 - 10
radio/gtkkeys.py

@@ -45,6 +45,30 @@ def key_for_event(event):
     return _BY_KEYVAL.get(event.keyval)
 
 
+class HeldKeys(object):
+    """Tells X autorepeat from presses: GDK sends repeats as presses with no
+    release in between"""
+
+    def __init__(self, widget, on_key):
+        self.held = set()
+        self.on_key = on_key
+        widget.connect("key-press-event", self._press)
+        widget.connect("key-release-event", self._release)
+
+    def _press(self, _w, event):
+        key = key_for_event(event)
+        if key is None:
+            return False
+        repeat = event.keyval in self.held
+        self.held.add(event.keyval)
+        self.on_key(key, repeat)
+        return True
+
+    def _release(self, _w, event):
+        self.held.discard(event.keyval)
+        return False
+
+
 def key_grid(on_key):
     """Grid with a button per key; returns (grid, {key: button})"""
     grid = Gtk.Grid()
@@ -70,7 +94,7 @@ class KeyWindow(object):
         self.window = Gtk.Window(title="Radio panel keys")
         self.window.set_resizable(False)
         self.window.connect("destroy", lambda _w: on_close())
-        self.window.connect("key-press-event", self._key_press)
+        self.keys = HeldKeys(self.window, self._pressed)
 
         box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
         box.set_border_width(8)
@@ -86,13 +110,6 @@ class KeyWindow(object):
         box.pack_start(self.last, False, False, 0)
         self.window.show_all()
 
-    def _pressed(self, key):
+    def _pressed(self, key, repeat=False):
         self.last.set_text("last key: " + key)
-        self.on_key(key)
-
-    def _key_press(self, _w, event):
-        key = key_for_event(event)
-        if key is None:
-            return False
-        self._pressed(key)
-        return True
+        self.on_key(key, repeat)

+ 2 - 2
radio/keypad.py

@@ -213,7 +213,7 @@ class KeyPoller(object):
 
 
 def start(fd, addr, on_key, poll_ms=POLL_MS):
-    """Feed presses and repeats to on_key(name), like evdev. Returns the
+    """Feed presses and repeats to on_key(name, repeat), like evdev. Returns the
     poller, or None if the expander does not answer. Keys held at start, or
     too many at once, are ignored: these presses send player commands."""
     exp = Expander(fd, addr)
@@ -232,7 +232,7 @@ def start(fd, addr, on_key, poll_ms=POLL_MS):
 
     def on_event(key, value):
         if value:
-            on_key(key)
+            on_key(key, value == 2)
 
     scanner = KeyScanner(max_keys=MAX_KEYS, ignore_initial=True)
     scanner.feed(raw, st.monotonic_ms())    # what is held now

+ 2 - 2
radio/platform.py

@@ -71,7 +71,7 @@ def have_keys_device():
 
 
 def start_evdev_keys(on_key, path=None):
-    """Feed presses from an evdev device to on_key(name). path None looks for
+    """Feed presses from an evdev device to on_key(name, repeat). path None looks for
     the device named DEFAULT_KEYS_NAME. False if there is none."""
     if path:
         try:
@@ -98,7 +98,7 @@ def start_evdev_keys(on_key, path=None):
                 _, _, etype, code, value = _EVENT.unpack_from(data, off)
                 # 1 press, 2 autorepeat: the app applies the lockout
                 if etype == EV_KEY and value and code in EVDEV_KEYS:
-                    on_key(EVDEV_KEYS[code])
+                    on_key(EVDEV_KEYS[code], value == 2)
 
     GLib.io_add_watch(fd, GLib.PRIORITY_DEFAULT, GLib.IO_IN | GLib.IO_HUP | GLib.IO_ERR,
                       on_input)

+ 1 - 8
radio/sim.py

@@ -55,7 +55,7 @@ class SimWindow(object):
         self.window = Gtk.Window(title="Radio panel simulator")
         self.window.set_resizable(False)
         self.window.connect("destroy", lambda _w: Gtk.main_quit())
-        self.window.connect("key-press-event", self._key_press)
+        self.keys = gtkkeys.HeldKeys(self.window, lambda k, r: self.app.key(k, r))
 
         box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
         box.set_border_width(8)
@@ -127,13 +127,6 @@ class SimWindow(object):
         cr.paint()
         return True
 
-    def _key_press(self, _w, event):
-        key = gtkkeys.key_for_event(event)
-        if key is None:
-            return False
-        self.app.key(key)
-        return True
-
     # ---- scripted runs, for automated checks ----------------------------------
 
     def _press_next(self):

+ 12 - 1
tests/test_ui.py

@@ -387,6 +387,17 @@ class TestPlaylistScreen(unittest.TestCase):
         self.assertFalse(self.app.list_timer_id)
         self.assertNotIn("refresh", self.server.commands())
 
+    def test_held_list_key_stays_open(self):
+        self.press("list")
+        self.app.key_time_ms.clear()
+        self.app.key("list", repeat=True)       # still held: autorepeat
+        self.assertTrue(self.state.list_open)
+        self.assertTrue(run_loop_until(lambda: self.state.playlist is not None))
+        self.rendered()
+        self.app.key_time_ms.clear()
+        self.app.key("down", repeat=True)       # held arrows do move
+        self.assertEqual(self.state.list_sel, 3)
+
     def test_selection_clamped(self):
         self.press("list")
         self.assertTrue(run_loop_until(lambda: self.state.playlist is not None))
@@ -545,7 +556,7 @@ class TestKeypad(unittest.TestCase):
         fake = FakeExpander()
         got = []
         with mock.patch.object(keypad.fcntl, "ioctl", fake.ioctl):
-            poller = keypad.start(3, 0x76, got.append, poll_ms=5)
+            poller = keypad.start(3, 0x76, lambda k, repeat: got.append(k), poll_ms=5)
             try:
                 fake.inputs = 0xffff                # all high: a fault
                 run_loop_until(lambda: False, 0.1)