app_core.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "app.h"
  4. #define BACKLIGHT_TIMEOUT_S 30
  5. #define KEY_LOCKOUT_US 300000 /* as the firmware: 300 ms after a press */
  6. #define LOG_REDRAW_MS 1000 /* scrolling is the costliest redraw */
  7. struct app {
  8. struct app_options opt;
  9. struct app_frontend fe;
  10. struct radio_state state;
  11. struct mpdc *mpd;
  12. struct log_kmsg *kmsg;
  13. cairo_surface_t *surface;
  14. uint32_t *prev;
  15. bool prev_valid;
  16. guint render_id;
  17. guint log_timer_id;
  18. guint tick_id;
  19. guint backlight_id;
  20. gint64 key_time_us[RK_COUNT];
  21. };
  22. unsigned int radio_state_elapsed_ms(const struct radio_state *s)
  23. {
  24. gint64 extra;
  25. unsigned int ms = s->elapsed_ms;
  26. /* with the link down the player's position is unknown, so hold it */
  27. if (!s->connected || s->state != PLAY_PLAYING || !s->elapsed_stamp_us)
  28. return ms;
  29. extra = (g_get_monotonic_time() - s->elapsed_stamp_us) / 1000;
  30. ms += (unsigned int)extra;
  31. return (s->total_ms && ms > s->total_ms) ? s->total_ms : ms;
  32. }
  33. /* Render, and hand the frontend the bounding box of what actually changed */
  34. static gboolean render_now(gpointer data)
  35. {
  36. struct app *app = data;
  37. int w = app->opt.width, h = app->opt.height;
  38. int stride, x, y, x0 = w, y0 = h, x1 = 0, y1 = 0;
  39. const uint32_t *pixels;
  40. cairo_t *cr;
  41. app->render_id = 0;
  42. cr = cairo_create(app->surface);
  43. ui_render(cr, &app->state, w, h);
  44. cairo_destroy(cr);
  45. cairo_surface_flush(app->surface);
  46. pixels = (const uint32_t *)cairo_image_surface_get_data(app->surface);
  47. stride = cairo_image_surface_get_stride(app->surface);
  48. for (y = 0; y < h; y++) {
  49. const uint32_t *a = (const uint32_t *)((const char *)pixels + y * stride);
  50. const uint32_t *b = app->prev + y * w;
  51. if (app->prev_valid && !memcmp(a, b, (size_t)w * 4))
  52. continue;
  53. for (x = 0; x < w; x++) {
  54. if (app->prev_valid && a[x] == b[x])
  55. continue;
  56. if (x < x0)
  57. x0 = x;
  58. if (x >= x1)
  59. x1 = x + 1;
  60. }
  61. if (y < y0)
  62. y0 = y;
  63. y1 = y + 1;
  64. memcpy(app->prev + y * w, a, (size_t)w * 4);
  65. }
  66. app->prev_valid = true;
  67. if (x1 > x0)
  68. app->fe.present(app->fe.ctx, pixels, stride, x0, y0, x1 - x0, y1 - y0);
  69. return G_SOURCE_REMOVE;
  70. }
  71. static void schedule_render(struct app *app)
  72. {
  73. if (!app->render_id)
  74. app->render_id = g_idle_add(render_now, app);
  75. }
  76. static void on_mpd_changed(void *user)
  77. {
  78. schedule_render(user);
  79. }
  80. static gboolean log_timer(gpointer data)
  81. {
  82. struct app *app = data;
  83. app->log_timer_id = 0;
  84. schedule_render(app);
  85. return G_SOURCE_REMOVE;
  86. }
  87. /* Append one wrapped row, scrolling the pane; batched into one redraw */
  88. static void log_push_row(struct app *app, const char *start, const char *end)
  89. {
  90. struct radio_state *s = &app->state;
  91. size_t len = (size_t)(end - start);
  92. memmove(s->log[0], s->log[1], sizeof(s->log[0]) * (LOG_ROWS - 1));
  93. if (len > sizeof(s->log[0]) - 1)
  94. len = sizeof(s->log[0]) - 1;
  95. memcpy(s->log[LOG_ROWS - 1], start, len);
  96. s->log[LOG_ROWS - 1][len] = '\0';
  97. }
  98. void app_log(struct app *app, const char *text)
  99. {
  100. char *clean = g_utf8_make_valid(text, -1);
  101. const char *p = clean;
  102. do {
  103. const char *q = p;
  104. int n = 0;
  105. while (*q && n < LOG_COLS) {
  106. q = g_utf8_next_char(q);
  107. n++;
  108. }
  109. log_push_row(app, p, q);
  110. p = q;
  111. } while (*p);
  112. g_free(clean);
  113. if (!app->log_timer_id)
  114. app->log_timer_id = g_timeout_add(LOG_REDRAW_MS, log_timer, app);
  115. }
  116. static void on_log(void *user, const char *text)
  117. {
  118. app_log(user, text);
  119. }
  120. static gboolean tick(gpointer data)
  121. {
  122. struct app *app = data;
  123. if (app->state.state == PLAY_PLAYING)
  124. schedule_render(app);
  125. return G_SOURCE_CONTINUE;
  126. }
  127. static gboolean backlight_expired(gpointer data)
  128. {
  129. struct app *app = data;
  130. app->backlight_id = 0;
  131. if (app->fe.backlight)
  132. app->fe.backlight(app->fe.ctx, false);
  133. return G_SOURCE_REMOVE;
  134. }
  135. static void backlight_wake(struct app *app)
  136. {
  137. if (app->fe.backlight)
  138. app->fe.backlight(app->fe.ctx, true);
  139. if (app->backlight_id)
  140. g_source_remove(app->backlight_id);
  141. app->backlight_id = g_timeout_add_seconds(BACKLIGHT_TIMEOUT_S,
  142. backlight_expired, app);
  143. }
  144. void app_key(struct app *app, enum radio_key key)
  145. {
  146. gint64 now = g_get_monotonic_time();
  147. if ((unsigned int)key >= RK_COUNT)
  148. return;
  149. if (now - app->key_time_us[key] < KEY_LOCKOUT_US)
  150. return;
  151. app->key_time_us[key] = now;
  152. backlight_wake(app);
  153. switch (key) {
  154. case RK_PREV: mpdc_command(app->mpd, MPDC_PREV); break;
  155. case RK_PLAY: mpdc_command(app->mpd, MPDC_PLAY); break;
  156. case RK_PAUSE: mpdc_command(app->mpd, MPDC_TOGGLE); break;
  157. case RK_STOP: mpdc_command(app->mpd, MPDC_STOP); break;
  158. case RK_NEXT: mpdc_command(app->mpd, MPDC_NEXT); break;
  159. case RK_ESC: mpdc_command(app->mpd, MPDC_REFRESH); break;
  160. case RK_UP: mpdc_command(app->mpd, MPDC_VOLUME_UP); break;
  161. case RK_DOWN: mpdc_command(app->mpd, MPDC_VOLUME_DOWN); break;
  162. default: break; /* MENU, LIST, OK, LEFT, RIGHT: not assigned yet */
  163. }
  164. }
  165. struct app *app_new(const struct app_options *opt, const struct app_frontend *fe)
  166. {
  167. struct app *app = g_new0(struct app, 1);
  168. app->opt = *opt;
  169. app->fe = *fe;
  170. app->state.song_pos = -1;
  171. app->state.volume = -1;
  172. g_strlcpy(app->state.server, opt->host, sizeof(app->state.server));
  173. app->surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
  174. opt->width, opt->height);
  175. app->prev = g_new0(uint32_t, (gsize)opt->width * opt->height);
  176. app->mpd = mpdc_new(opt->host, opt->port, &app->state,
  177. on_mpd_changed, on_log, app);
  178. if (opt->kmsg)
  179. app->kmsg = log_kmsg_new(on_log, app);
  180. app->tick_id = g_timeout_add_seconds(1, tick, app);
  181. backlight_wake(app);
  182. schedule_render(app);
  183. return app;
  184. }
  185. void app_free(struct app *app)
  186. {
  187. if (app->render_id)
  188. g_source_remove(app->render_id);
  189. if (app->log_timer_id)
  190. g_source_remove(app->log_timer_id);
  191. if (app->backlight_id)
  192. g_source_remove(app->backlight_id);
  193. g_source_remove(app->tick_id);
  194. if (app->kmsg)
  195. log_kmsg_free(app->kmsg);
  196. mpdc_free(app->mpd);
  197. cairo_surface_destroy(app->surface);
  198. g_free(app->prev);
  199. g_free(app);
  200. }