| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- #include <stdlib.h>
- #include <string.h>
- #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);
- }
|