"""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 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, # menu, list, ok, left, right: not assigned yet } class App(object): def __init__(self, host, port, width, height, present, backlight=None, kmsg=False): self.width, self.height = width, height 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.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"): 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 cr = cairo.Context(self.surface) render.render(cr, self.state, self.width, self.height) 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: 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): if name not in st.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() cmd = _KEY_COMMANDS.get(name) if cmd is not None: self.mpd.command(cmd) 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)