#include #include #include "app.h" #define BACKLIGHT_TIMEOUT_S 30 #define KEY_LOCKOUT_US 300000 /* as the firmware: 300 ms after a press */ #define LOG_REDRAW_MS 1000 /* scrolling is the costliest redraw */ struct app { struct app_options opt; struct app_frontend fe; struct radio_state state; struct mpdc *mpd; struct log_kmsg *kmsg; cairo_surface_t *surface; uint32_t *prev; bool prev_valid; guint render_id; guint log_timer_id; guint tick_id; guint backlight_id; gint64 key_time_us[RK_COUNT]; }; unsigned int radio_state_elapsed_ms(const struct radio_state *s) { gint64 extra; unsigned int ms = s->elapsed_ms; /* with the link down the player's position is unknown, so hold it */ if (!s->connected || s->state != PLAY_PLAYING || !s->elapsed_stamp_us) return ms; extra = (g_get_monotonic_time() - s->elapsed_stamp_us) / 1000; ms += (unsigned int)extra; return (s->total_ms && ms > s->total_ms) ? s->total_ms : ms; } /* Render, and hand the frontend the bounding box of what actually changed */ static gboolean render_now(gpointer data) { struct app *app = data; int w = app->opt.width, h = app->opt.height; int stride, x, y, x0 = w, y0 = h, x1 = 0, y1 = 0; const uint32_t *pixels; cairo_t *cr; app->render_id = 0; cr = cairo_create(app->surface); ui_render(cr, &app->state, w, h); cairo_destroy(cr); cairo_surface_flush(app->surface); pixels = (const uint32_t *)cairo_image_surface_get_data(app->surface); stride = cairo_image_surface_get_stride(app->surface); for (y = 0; y < h; y++) { const uint32_t *a = (const uint32_t *)((const char *)pixels + y * stride); const uint32_t *b = app->prev + y * w; if (app->prev_valid && !memcmp(a, b, (size_t)w * 4)) continue; for (x = 0; x < w; x++) { if (app->prev_valid && a[x] == b[x]) continue; if (x < x0) x0 = x; if (x >= x1) x1 = x + 1; } if (y < y0) y0 = y; y1 = y + 1; memcpy(app->prev + y * w, a, (size_t)w * 4); } app->prev_valid = true; if (x1 > x0) app->fe.present(app->fe.ctx, pixels, stride, x0, y0, x1 - x0, y1 - y0); return G_SOURCE_REMOVE; } static void schedule_render(struct app *app) { if (!app->render_id) app->render_id = g_idle_add(render_now, app); } static void on_mpd_changed(void *user) { schedule_render(user); } static gboolean log_timer(gpointer data) { struct app *app = data; app->log_timer_id = 0; schedule_render(app); return G_SOURCE_REMOVE; } /* Append one wrapped row, scrolling the pane; batched into one redraw */ static void log_push_row(struct app *app, const char *start, const char *end) { struct radio_state *s = &app->state; size_t len = (size_t)(end - start); memmove(s->log[0], s->log[1], sizeof(s->log[0]) * (LOG_ROWS - 1)); if (len > sizeof(s->log[0]) - 1) len = sizeof(s->log[0]) - 1; memcpy(s->log[LOG_ROWS - 1], start, len); s->log[LOG_ROWS - 1][len] = '\0'; } void app_log(struct app *app, const char *text) { char *clean = g_utf8_make_valid(text, -1); const char *p = clean; do { const char *q = p; int n = 0; while (*q && n < LOG_COLS) { q = g_utf8_next_char(q); n++; } log_push_row(app, p, q); p = q; } while (*p); g_free(clean); if (!app->log_timer_id) app->log_timer_id = g_timeout_add(LOG_REDRAW_MS, log_timer, app); } static void on_log(void *user, const char *text) { app_log(user, text); } static gboolean tick(gpointer data) { struct app *app = data; if (app->state.state == PLAY_PLAYING) schedule_render(app); return G_SOURCE_CONTINUE; } static gboolean backlight_expired(gpointer data) { struct app *app = data; app->backlight_id = 0; if (app->fe.backlight) app->fe.backlight(app->fe.ctx, false); return G_SOURCE_REMOVE; } static void backlight_wake(struct app *app) { if (app->fe.backlight) app->fe.backlight(app->fe.ctx, true); if (app->backlight_id) g_source_remove(app->backlight_id); app->backlight_id = g_timeout_add_seconds(BACKLIGHT_TIMEOUT_S, backlight_expired, app); } void app_key(struct app *app, enum radio_key key) { gint64 now = g_get_monotonic_time(); if ((unsigned int)key >= RK_COUNT) return; if (now - app->key_time_us[key] < KEY_LOCKOUT_US) return; app->key_time_us[key] = now; backlight_wake(app); switch (key) { case RK_PREV: mpdc_command(app->mpd, MPDC_PREV); break; case RK_PLAY: mpdc_command(app->mpd, MPDC_PLAY); break; case RK_PAUSE: mpdc_command(app->mpd, MPDC_TOGGLE); break; case RK_STOP: mpdc_command(app->mpd, MPDC_STOP); break; case RK_NEXT: mpdc_command(app->mpd, MPDC_NEXT); break; case RK_ESC: mpdc_command(app->mpd, MPDC_REFRESH); break; case RK_UP: mpdc_command(app->mpd, MPDC_VOLUME_UP); break; case RK_DOWN: mpdc_command(app->mpd, MPDC_VOLUME_DOWN); break; default: break; /* MENU, LIST, OK, LEFT, RIGHT: not assigned yet */ } } struct app *app_new(const struct app_options *opt, const struct app_frontend *fe) { struct app *app = g_new0(struct app, 1); app->opt = *opt; app->fe = *fe; app->state.song_pos = -1; app->state.volume = -1; g_strlcpy(app->state.server, opt->host, sizeof(app->state.server)); app->surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, opt->width, opt->height); app->prev = g_new0(uint32_t, (gsize)opt->width * opt->height); app->mpd = mpdc_new(opt->host, opt->port, &app->state, on_mpd_changed, on_log, app); if (opt->kmsg) app->kmsg = log_kmsg_new(on_log, app); app->tick_id = g_timeout_add_seconds(1, tick, app); backlight_wake(app); schedule_render(app); return app; } void app_free(struct app *app) { if (app->render_id) g_source_remove(app->render_id); if (app->log_timer_id) g_source_remove(app->log_timer_id); if (app->backlight_id) g_source_remove(app->backlight_id); g_source_remove(app->tick_id); if (app->kmsg) log_kmsg_free(app->kmsg); mpdc_free(app->mpd); cairo_surface_destroy(app->surface); g_free(app->prev); g_free(app); }