ui.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. """ui.py -- fullscreen touchscreen UI for the Steam Deck forwarder.
  2. Big tappable tiles, one per bonded puck. A tile is tappable when its puck is LIVE (the RF link is up).
  3. Tapping a live tile detaches the Deck controls and forwards to that puck; tapping anywhere while
  4. forwarding stops and returns control to the Deck. Sized for the Deck's 1280x800 touchscreen but scales.
  5. """
  6. import time
  7. import pygame
  8. BG = (16, 18, 24)
  9. FG = (230, 232, 238)
  10. DIM = (120, 126, 140)
  11. ACCENT = (90, 170, 255)
  12. LIVE = (70, 200, 120)
  13. WARN = (235, 180, 70)
  14. STOP = (235, 90, 90)
  15. TILE = (32, 36, 46)
  16. TILE_LIVE = (28, 54, 40)
  17. TILE_SEL = (40, 70, 110)
  18. def _font(sz, bold=False):
  19. f = pygame.font.SysFont("noto sans,dejavusans,sans", sz, bold=bold)
  20. return f
  21. def run(app):
  22. pygame.init()
  23. pygame.display.set_caption("OpenController · Steam Deck")
  24. screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
  25. W, H = screen.get_size()
  26. clock = pygame.time.Clock()
  27. f_title = _font(int(H * 0.05), bold=True)
  28. f_big = _font(int(H * 0.045), bold=True)
  29. f_med = _font(int(H * 0.03))
  30. f_small = _font(int(H * 0.022))
  31. tile_rects = [] # (rect, slot, tappable)
  32. def draw():
  33. nonlocal tile_rects
  34. screen.fill(BG)
  35. s = app.status
  36. # title bar
  37. screen.blit(f_title.render("⬡ OpenController", True, ACCENT), (40, 28))
  38. link = "● linked ch%d" % s["sess_ch"] if s["link_up"] else "○ searching…"
  39. col = LIVE if s["link_up"] else DIM
  40. t = f_med.render(link, True, col)
  41. screen.blit(t, (W - t.get_width() - 40, 38))
  42. # tiles
  43. tile_rects = []
  44. bonds = [(i, b) for i, b in enumerate(s["bonds"]) if b["used"]]
  45. top = int(H * 0.16)
  46. gap = int(H * 0.025)
  47. th = int((H * 0.42 - gap * max(0, len(bonds) - 1)) / max(1, len(bonds)))
  48. th = min(th, int(H * 0.20))
  49. if not bonds:
  50. msg = f_big.render("No paired pucks", True, DIM)
  51. screen.blit(msg, (W // 2 - msg.get_width() // 2, H // 2 - 40))
  52. sub = f_small.render("Pair on a computer with Steam or PairTUI (scpair.py), then plug the puck into your host.",
  53. True, DIM)
  54. screen.blit(sub, (W // 2 - sub.get_width() // 2, H // 2 + 20))
  55. for n, (slot, b) in enumerate(bonds):
  56. y = top + n * (th + gap)
  57. rect = pygame.Rect(40, y, W - 80, th)
  58. sel = app.forwarding and app.fwd_slot == slot
  59. tappable = b["alive"] and not app.forwarding
  60. color = TILE_SEL if sel else (TILE_LIVE if b["alive"] else TILE)
  61. pygame.draw.rect(screen, color, rect, border_radius=18)
  62. pygame.draw.rect(screen, (ACCENT if (tappable or sel) else DIM), rect, width=2, border_radius=18)
  63. # glyph + serial
  64. glyph = "●" if b["alive"] else "○"
  65. gcol = LIVE if b["alive"] else DIM
  66. screen.blit(f_big.render(glyph, True, gcol), (rect.x + 30, rect.y + th // 2 - 22))
  67. screen.blit(f_big.render(b["serial"] or "puck %d" % slot, True, FG),
  68. (rect.x + 90, rect.y + 20))
  69. state = ("FORWARDING — tap to stop" if sel
  70. else "available — tap to forward" if tappable
  71. else "live (forwarding elsewhere)" if b["alive"]
  72. else "paired · offline")
  73. screen.blit(f_small.render("slot %d · %s" % (slot, state), True, DIM),
  74. (rect.x + 90, rect.y + th - 38))
  75. tile_rects.append((rect, slot, tappable or sel))
  76. # firmware log panel (the in-app serial monitor) — fills the area below the tiles
  77. log_top = int(H * 0.60)
  78. log_bot = H - int(H * (0.12 if app.forwarding else 0.02))
  79. lh = f_small.get_height() + 2
  80. nlines = max(1, (log_bot - log_top - 24) // lh)
  81. screen.blit(f_small.render("firmware log", True, DIM), (40, log_top))
  82. lines = app.log[-nlines:]
  83. for i, ln in enumerate(lines):
  84. # colorize the lines that matter for connecting
  85. c = DIM
  86. if "SESSION live" in ln or "CONNECTED" in ln:
  87. c = LIVE
  88. elif "adopted" in ln or "E1 heard" in ln:
  89. c = ACCENT
  90. elif "lost" in ln or "no bond" in ln or "fail" in ln:
  91. c = WARN
  92. screen.blit(f_small.render(ln[:120], True, c), (40, log_top + 24 + i * lh))
  93. # forwarding banner (drawn last so it sits on top)
  94. if app.forwarding:
  95. bar = pygame.Rect(0, H - int(H * 0.1), W, int(H * 0.1))
  96. pygame.draw.rect(screen, STOP, bar)
  97. msg = f_big.render("FORWARDING — Deck controls detached · tap anywhere to stop", True, (255, 255, 255))
  98. screen.blit(msg, (W // 2 - msg.get_width() // 2, bar.y + bar.height // 2 - 22))
  99. pygame.display.flip()
  100. def handle_tap(pos):
  101. if app.forwarding:
  102. app.stop_forwarding()
  103. return
  104. for rect, slot, tappable in tile_rects:
  105. if rect.collidepoint(pos) and tappable:
  106. app.toggle(slot)
  107. return
  108. running = True
  109. while running:
  110. for ev in pygame.event.get():
  111. if ev.type == pygame.QUIT:
  112. running = False
  113. elif ev.type == pygame.KEYDOWN and ev.key in (pygame.K_ESCAPE, pygame.K_q):
  114. running = False
  115. elif ev.type == pygame.MOUSEBUTTONDOWN:
  116. handle_tap(ev.pos)
  117. elif ev.type == pygame.FINGERDOWN:
  118. handle_tap((int(ev.x * W), int(ev.y * H)))
  119. app.pump_serial()
  120. app.pump_input()
  121. app.auto_release_on_drop()
  122. draw()
  123. # high tick so forwarding input stays smooth; UI redraw is cheap
  124. clock.tick(120)
  125. pygame.quit()