"""What the screen shows, shared by the MPD client, the log and the renderer.""" import time LOG_ROWS = 7 LOG_COLS = 44 PLAY_UNKNOWN, PLAY_PLAYING, PLAY_PAUSED, PLAY_STOPPED = range(4) # Key identifiers, as the C version's enum radio_key; the names are what # --press and the help text use. KEYS = ("esc", "next", "stop", "menu", "list", "up", "prev", "right", "down", "pause", "ok", "left", "play") def monotonic_ms(): return int(time.monotonic() * 1000) class QueueEntry(object): """One song of the MPD queue, as the playlist screen shows it""" __slots__ = ("artist", "title", "fallback", "duration_s") def __init__(self, artist, title, fallback, duration_s): self.artist = artist # both set, or the line shows the fallback self.title = title self.fallback = fallback # the main screen's title text self.duration_s = duration_s # -1: unknown, e.g. a stream class RadioState(object): def __init__(self, server=""): self.connected = False self.server = server self.state = PLAY_UNKNOWN self.title = "" self.song_pos = -1 # -1: none self.queue_len = 0 self.elapsed_ms = 0 self.elapsed_stamp_ms = 0 # monotonic time elapsed_ms was read self.total_ms = 0 # 0: unknown, e.g. a stream self.volume = -1 # -1: unknown self.repeat = False self.random = False self.log = [""] * LOG_ROWS # wrapped rows, oldest first self.playlist = None # list of QueueEntry; None: not fetched self.list_open = False # the playlist screen is shown self.list_sel = -1 # selected queue position; -1: not yet set def current_elapsed_ms(self): """Position, advanced locally between MPD updates while playing""" ms = self.elapsed_ms # with the link down the player's position is unknown, so hold it if not self.connected or self.state != PLAY_PLAYING or not self.elapsed_stamp_ms: return ms ms += monotonic_ms() - self.elapsed_stamp_ms return min(ms, self.total_ms) if self.total_ms else ms def log_append(self, text): """Wrap text into rows of LOG_COLS characters and scroll them in""" text = text.replace("\n", " ").replace("\t", " ") while True: row, text = text[:LOG_COLS], text[LOG_COLS:] self.log = self.log[1:] + [row] if not text: break