"""Radio UI core: everything that is the same in the window and on the panel. A frontend supplies present(data, stride, x, y, w, h), which gets the whole XRGB8888 frame and the rectangle that changed, and optionally backlight(on). """ import errno import os import cairo from gi.repository import GLib from . import mpd, render 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", "left", "right") KEY_LOCKOUT_MS = 300 # as the firmware: 300 ms after a press LOG_REDRAW_MS = 1000 # scrolling is the costliest redraw _KEY_COMMANDS = { "prev": mpd.PREV, "play": mpd.PLAY, "pause": mpd.TOGGLE, "stop": mpd.STOP, "next": mpd.NEXT, "esc": mpd.REFRESH, "up": mpd.VOLUME_UP, "down": mpd.VOLUME_DOWN, "left": mpd.SEEK_BACK, "right": mpd.SEEK_FORWARD, # menu, list, ok: not assigned yet } class App(object): def __init__(self, host, port, width, height, present, backlight=None, kmsg=False, grayscale=True): """grayscale: the panel shows more than black and white (bpp > 1)""" self.width, self.height = width, height self.grayscale = grayscale self.present = present self.backlight = backlight self.state = st.RadioState(host) self.surface = cairo.ImageSurface(cairo.FORMAT_RGB24, width, height) self.stride = self.surface.get_stride() self.prev = None self.render_id = 0 self.log_timer_id = 0 self.backlight_id = 0 self.list_timer_id = 0 self.key_time_ms = {} self.mpd = mpd.MpdClient(host, port, self.state, self._schedule_render, self.log) self.kmsg = KmsgFollower(self.log) if kmsg else None self.tick_id = GLib.timeout_add_seconds(1, self._tick) self._backlight_wake() self._schedule_render() def close(self): for attr in ("render_id", "log_timer_id", "backlight_id", "tick_id", "list_timer_id"): if getattr(self, attr): GLib.source_remove(getattr(self, attr)) setattr(self, attr, 0) if self.kmsg: self.kmsg.close() self.mpd.close() # ---- rendering ------------------------------------------------------------ def _render_now(self): self.render_id = 0 self._settle_selection() cr = cairo.Context(self.surface) render.render(cr, self.state, self.width, self.height, self.grayscale) del cr self.surface.flush() data = self.surface.get_data() frame = bytes(data) stride, y0, y1 = self.stride, None, 0 for y in range(self.height): a = y * stride if self.prev is not None and frame[a:a + stride] == self.prev[a:a + stride]: continue if y0 is None: y0 = y y1 = y + 1 self.prev = frame # whole rows: the panel core finds the changed bytes itself if y0 is not None: self.present(data, stride, 0, y0, self.width, y1 - y0) return False def _schedule_render(self): if not self.render_id: self.render_id = GLib.idle_add(self._render_now) def _tick(self): if self.state.state == st.PLAY_PLAYING and not self.state.list_open: self._schedule_render() return True # ---- log ------------------------------------------------------------------ def log(self, text): self.state.log_append(text) if not self.log_timer_id: self.log_timer_id = GLib.timeout_add(LOG_REDRAW_MS, self._log_timer) def _log_timer(self): self.log_timer_id = 0 self._schedule_render() return False # ---- keys and backlight ----------------------------------------------------- def _backlight_expired(self): self.backlight_id = 0 if self.backlight: self.backlight(False) return False def _backlight_wake(self): if self.backlight: self.backlight(True) if self.backlight_id: GLib.source_remove(self.backlight_id) self.backlight_id = GLib.timeout_add_seconds(BACKLIGHT_TIMEOUT_S, self._backlight_expired) 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) if last is not None and now - last < KEY_LOCKOUT_MS: return self.key_time_ms[name] = now self._backlight_wake() if self.state.list_open: if self._list_key(name): return elif name == "list": 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): s = self.state s.list_open = on s.list_sel = -1 self.mpd.follow_playlist(on) if self.list_timer_id: GLib.source_remove(self.list_timer_id) self.list_timer_id = 0 if on: self.list_timer_id = GLib.timeout_add_seconds(LIST_TIMEOUT_S, self._list_expired) self._schedule_render() def _list_expired(self): self.list_timer_id = 0 self._list_show(False) return False def _list_key(self, name): """A key on the playlist screen; False passes it on to the player""" s = self.state if name in ("menu", "esc", "list"): self._list_show(False) return True if self.list_timer_id: GLib.source_remove(self.list_timer_id) self.list_timer_id = GLib.timeout_add_seconds(LIST_TIMEOUT_S, self._list_expired) if name in ("up", "down"): if s.playlist and s.list_sel >= 0: step = -1 if name == "up" else 1 s.list_sel = min(max(s.list_sel + step, 0), len(s.playlist) - 1) self._schedule_render() return True if name == "ok": if s.playlist and s.list_sel >= 0: pos = s.list_sel self._list_show(False) self.mpd.command(mpd.PLAY_POS, pos) return True return False def _settle_selection(self): """Selection starts on the current song once the list is in""" s = self.state if not s.list_open or not s.playlist: return if s.list_sel < 0: s.list_sel = s.song_pos if 0 <= s.song_pos < len(s.playlist) else 0 s.list_sel = min(s.list_sel, len(s.playlist) - 1) class KmsgFollower(object): """Kernel log pane source: follows /dev/kmsg from the moment it is opened""" def __init__(self, log): self.log = log self.watch_id = 0 try: self.fd = os.open("/dev/kmsg", os.O_RDONLY | os.O_NONBLOCK | os.O_CLOEXEC) except OSError: self.fd = -1 log("Nie można otworzyć /dev/kmsg") return os.lseek(self.fd, 0, os.SEEK_END) self.watch_id = GLib.io_add_watch(self.fd, GLib.PRIORITY_DEFAULT, GLib.IO_IN, self._on_readable) def _on_readable(self, fd, cond): while True: try: rec = os.read(self.fd, 4096) except OSError as e: if e.errno == errno.EPIPE: continue # records were overwritten; carry on if e.errno == errno.EAGAIN: return True break if not rec: break # "prio,seq,usec,flags;message\n" then optional " KEY=value" lines head, sep, text = rec.decode("utf-8", "replace").partition(";") if sep: self.log(text.split("\n", 1)[0]) self.watch_id = 0 return False def close(self): if self.watch_id: GLib.source_remove(self.watch_id) if self.fd >= 0: os.close(self.fd)