Эх сурвалжийг харах

Seek 10 s back/forward with left/right within the current song

k4be 4 өдөр өмнө
parent
commit
0b155a9daa
5 өөрчлөгдсөн 50 нэмэгдсэн , 6 устгасан
  1. 2 1
      README.md
  2. 15 2
      radio/app.py
  3. 5 1
      radio/mpd.py
  4. 2 0
      tests/fake_mpd.py
  5. 26 2
      tests/test_ui.py

+ 2 - 1
README.md

@@ -77,7 +77,8 @@ or through the DRM driver:
     ./radio-ui drm --host radio.lo
 
 On the main screen the keys are prev/play/pause/stop/next, up/down for the
-volume and Esc to refresh. List opens the playlist: the whole MPD queue, the
+volume, left/right to seek 10 s back/forward within the current song (not
+past its end, not in a stream) and Esc to refresh. List opens the playlist: the whole MPD queue, the
 current song centered and selected. Up/down move the selection, OK plays it,
 and Menu, Esc, List or 30 s without a key go back. With more than 1 bpp the
 selection is a gray bar, otherwise inverted. The transport keys keep working

+ 15 - 2
radio/app.py

@@ -16,7 +16,7 @@ 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")
+REPEATING_KEYS = ("up", "down", "prev", "next", "left", "right")
 KEY_LOCKOUT_MS = 300        # as the firmware: 300 ms after a press
 LOG_REDRAW_MS = 1000        # scrolling is the costliest redraw
 
@@ -29,7 +29,9 @@ _KEY_COMMANDS = {
     "esc": mpd.REFRESH,
     "up": mpd.VOLUME_UP,
     "down": mpd.VOLUME_DOWN,
-    # menu, list, ok, left, right: not assigned yet
+    "left": mpd.SEEK_BACK,
+    "right": mpd.SEEK_FORWARD,
+    # menu, list, ok: not assigned yet
 }
 
 
@@ -149,9 +151,20 @@ class App(object):
             self._list_show(True)
             return
         cmd = _KEY_COMMANDS.get(name)
+        if cmd in (mpd.SEEK_BACK, mpd.SEEK_FORWARD) and not self._can_seek(cmd):
+            return
         if cmd is not None:
             self.mpd.command(cmd)
 
+    def _can_seek(self, cmd):
+        """Seeks stay within the current song; streams have no length to seek in"""
+        s = self.state
+        if s.state not in (st.PLAY_PLAYING, st.PLAY_PAUSED) or not s.total_ms:
+            return False
+        if cmd == mpd.SEEK_FORWARD:
+            return s.current_elapsed_ms() + mpd.SEEK_STEP_S * 1000 < s.total_ms
+        return True
+
     # ---- playlist screen --------------------------------------------------------
 
     def _list_show(self, on):

+ 5 - 1
radio/mpd.py

@@ -20,7 +20,9 @@ RESPONSE_TIMEOUT_S = 30
 RECONNECT_S = 3
 IDLE_SUBSYSTEMS = "playlist player mixer options"
 
-PLAY, TOGGLE, STOP, NEXT, PREV, VOLUME_UP, VOLUME_DOWN, REFRESH, PLAY_POS, PLAYLIST = range(10)
+(PLAY, TOGGLE, STOP, NEXT, PREV, VOLUME_UP, VOLUME_DOWN, REFRESH, PLAY_POS, PLAYLIST,
+ SEEK_BACK, SEEK_FORWARD) = range(12)
+SEEK_STEP_S = 10
 
 _COMMANDS = {
     PLAY: "play",
@@ -30,6 +32,8 @@ _COMMANDS = {
     PREV: "previous",
     VOLUME_UP: "volume +1",
     VOLUME_DOWN: "volume -1",
+    SEEK_BACK: "seekcur -%d" % SEEK_STEP_S,
+    SEEK_FORWARD: "seekcur +%d" % SEEK_STEP_S,
 }
 
 

+ 2 - 0
tests/fake_mpd.py

@@ -123,6 +123,8 @@ class Handler(socketserver.StreamRequestHandler):
             elif cmd == "previous":
                 player.song -= 1
                 self.send("OK\n")
+            elif cmd == "seekcur":
+                self.send("OK\n")
             elif cmd == "close":
                 return
             else:

+ 26 - 2
tests/test_ui.py

@@ -149,9 +149,10 @@ class TestMpdClient(unittest.TestCase):
 
     def test_commands(self):
         for cmd in (mpd.NEXT, mpd.VOLUME_UP, mpd.VOLUME_DOWN, mpd.TOGGLE, mpd.PREV,
-                    mpd.STOP, mpd.PLAY, mpd.REFRESH):
+                    mpd.STOP, mpd.PLAY, mpd.REFRESH, mpd.SEEK_BACK, mpd.SEEK_FORWARD):
             self.client.command(cmd)
-        expected = ["next", "volume +1", "volume -1", "pause", "previous", "stop", "play"]
+        expected = ["next", "volume +1", "volume -1", "pause", "previous", "stop", "play",
+                    "seekcur -10", "seekcur +10"]
 
         def sent():
             return [c for c in self.server.commands()
@@ -244,6 +245,29 @@ class TestPanel(unittest.TestCase):
             app.close()
         self.assertEqual(sent, [mpd.NEXT])
 
+    def test_seek_keys(self):
+        sent = []
+        app = App("127.0.0.1", free_port(), 240, 160, lambda *a: None)
+        app.mpd.command = sent.append
+        s = app.state
+
+        def press(name):
+            app.key_time_ms.clear()     # not the lockout under test
+            app.key(name)
+        try:
+            press("left")               # nothing playing
+            s.state, s.total_ms, s.elapsed_ms = st.PLAY_PAUSED, 0, 5000
+            press("right")              # a stream: no length
+            s.total_ms = 60000
+            press("left")
+            press("right")
+            s.elapsed_ms = 51000
+            press("right")              # past the end
+            press("left")
+        finally:
+            app.close()
+        self.assertEqual(sent, [mpd.SEEK_BACK, mpd.SEEK_FORWARD, mpd.SEEK_BACK])
+
     def test_patterns(self):
         for name in ("geometry", "gray"):
             fb = make_pattern(name, 240, 160)