app.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. """Radio UI core: everything that is the same in the window and on the panel.
  2. A frontend supplies present(data, stride, x, y, w, h), which gets the whole
  3. XRGB8888 frame and the rectangle that changed, and optionally backlight(on).
  4. """
  5. import errno
  6. import os
  7. import cairo
  8. from gi.repository import GLib
  9. from . import mpd, render
  10. from . import state as st
  11. BACKLIGHT_TIMEOUT_S = 30
  12. KEY_LOCKOUT_MS = 300 # as the firmware: 300 ms after a press
  13. LOG_REDRAW_MS = 1000 # scrolling is the costliest redraw
  14. _KEY_COMMANDS = {
  15. "prev": mpd.PREV,
  16. "play": mpd.PLAY,
  17. "pause": mpd.TOGGLE,
  18. "stop": mpd.STOP,
  19. "next": mpd.NEXT,
  20. "esc": mpd.REFRESH,
  21. "up": mpd.VOLUME_UP,
  22. "down": mpd.VOLUME_DOWN,
  23. # menu, list, ok, left, right: not assigned yet
  24. }
  25. class App(object):
  26. def __init__(self, host, port, width, height, present, backlight=None, kmsg=False):
  27. self.width, self.height = width, height
  28. self.present = present
  29. self.backlight = backlight
  30. self.state = st.RadioState(host)
  31. self.surface = cairo.ImageSurface(cairo.FORMAT_RGB24, width, height)
  32. self.stride = self.surface.get_stride()
  33. self.prev = None
  34. self.render_id = 0
  35. self.log_timer_id = 0
  36. self.backlight_id = 0
  37. self.key_time_ms = {}
  38. self.mpd = mpd.MpdClient(host, port, self.state, self._schedule_render, self.log)
  39. self.kmsg = KmsgFollower(self.log) if kmsg else None
  40. self.tick_id = GLib.timeout_add_seconds(1, self._tick)
  41. self._backlight_wake()
  42. self._schedule_render()
  43. def close(self):
  44. for attr in ("render_id", "log_timer_id", "backlight_id", "tick_id"):
  45. if getattr(self, attr):
  46. GLib.source_remove(getattr(self, attr))
  47. setattr(self, attr, 0)
  48. if self.kmsg:
  49. self.kmsg.close()
  50. self.mpd.close()
  51. # ---- rendering ------------------------------------------------------------
  52. def _render_now(self):
  53. self.render_id = 0
  54. cr = cairo.Context(self.surface)
  55. render.render(cr, self.state, self.width, self.height)
  56. del cr
  57. self.surface.flush()
  58. data = self.surface.get_data()
  59. frame = bytes(data)
  60. stride, y0, y1 = self.stride, None, 0
  61. for y in range(self.height):
  62. a = y * stride
  63. if self.prev is not None and frame[a:a + stride] == self.prev[a:a + stride]:
  64. continue
  65. if y0 is None:
  66. y0 = y
  67. y1 = y + 1
  68. self.prev = frame
  69. # whole rows: the panel core finds the changed bytes itself
  70. if y0 is not None:
  71. self.present(data, stride, 0, y0, self.width, y1 - y0)
  72. return False
  73. def _schedule_render(self):
  74. if not self.render_id:
  75. self.render_id = GLib.idle_add(self._render_now)
  76. def _tick(self):
  77. if self.state.state == st.PLAY_PLAYING:
  78. self._schedule_render()
  79. return True
  80. # ---- log ------------------------------------------------------------------
  81. def log(self, text):
  82. self.state.log_append(text)
  83. if not self.log_timer_id:
  84. self.log_timer_id = GLib.timeout_add(LOG_REDRAW_MS, self._log_timer)
  85. def _log_timer(self):
  86. self.log_timer_id = 0
  87. self._schedule_render()
  88. return False
  89. # ---- keys and backlight -----------------------------------------------------
  90. def _backlight_expired(self):
  91. self.backlight_id = 0
  92. if self.backlight:
  93. self.backlight(False)
  94. return False
  95. def _backlight_wake(self):
  96. if self.backlight:
  97. self.backlight(True)
  98. if self.backlight_id:
  99. GLib.source_remove(self.backlight_id)
  100. self.backlight_id = GLib.timeout_add_seconds(BACKLIGHT_TIMEOUT_S,
  101. self._backlight_expired)
  102. def key(self, name):
  103. if name not in st.KEYS:
  104. return
  105. now = st.monotonic_ms()
  106. last = self.key_time_ms.get(name)
  107. if last is not None and now - last < KEY_LOCKOUT_MS:
  108. return
  109. self.key_time_ms[name] = now
  110. self._backlight_wake()
  111. cmd = _KEY_COMMANDS.get(name)
  112. if cmd is not None:
  113. self.mpd.command(cmd)
  114. class KmsgFollower(object):
  115. """Kernel log pane source: follows /dev/kmsg from the moment it is opened"""
  116. def __init__(self, log):
  117. self.log = log
  118. self.watch_id = 0
  119. try:
  120. self.fd = os.open("/dev/kmsg", os.O_RDONLY | os.O_NONBLOCK | os.O_CLOEXEC)
  121. except OSError:
  122. self.fd = -1
  123. log("Nie można otworzyć /dev/kmsg")
  124. return
  125. os.lseek(self.fd, 0, os.SEEK_END)
  126. self.watch_id = GLib.io_add_watch(self.fd, GLib.PRIORITY_DEFAULT,
  127. GLib.IO_IN, self._on_readable)
  128. def _on_readable(self, fd, cond):
  129. while True:
  130. try:
  131. rec = os.read(self.fd, 4096)
  132. except OSError as e:
  133. if e.errno == errno.EPIPE:
  134. continue # records were overwritten; carry on
  135. if e.errno == errno.EAGAIN:
  136. return True
  137. break
  138. if not rec:
  139. break
  140. # "prio,seq,usec,flags;message\n" then optional " KEY=value" lines
  141. head, sep, text = rec.decode("utf-8", "replace").partition(";")
  142. if sep:
  143. self.log(text.split("\n", 1)[0])
  144. self.watch_id = 0
  145. return False
  146. def close(self):
  147. if self.watch_id:
  148. GLib.source_remove(self.watch_id)
  149. if self.fd >= 0:
  150. os.close(self.fd)