app.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Radio UI: everything that is the same in the window and on the real panel.
  3. * The frontends supply a way to show pixels and feed in key presses.
  4. */
  5. #ifndef RADIO_APP_H
  6. #define RADIO_APP_H
  7. #include <cairo.h>
  8. #include <glib.h>
  9. #include <stdbool.h>
  10. #include <stdint.h>
  11. enum radio_key {
  12. RK_ESC, RK_NEXT, RK_STOP, RK_MENU, RK_LIST, RK_UP, RK_PREV,
  13. RK_RIGHT, RK_DOWN, RK_PAUSE, RK_OK, RK_LEFT, RK_PLAY,
  14. RK_COUNT
  15. };
  16. enum play_state { PLAY_UNKNOWN, PLAY_PLAYING, PLAY_PAUSED, PLAY_STOPPED };
  17. #define LOG_ROWS 8
  18. #define LOG_COLS 44
  19. #define TITLE_MAX 512
  20. struct radio_state {
  21. bool connected;
  22. char server[128];
  23. enum play_state state;
  24. char title[TITLE_MAX];
  25. int song_pos; /* -1: none */
  26. unsigned int queue_len;
  27. unsigned int elapsed_ms;
  28. gint64 elapsed_stamp_us; /* monotonic time elapsed_ms was read */
  29. unsigned int total_ms; /* 0: unknown, e.g. a stream */
  30. int volume; /* -1: unknown */
  31. bool repeat, random;
  32. char log[LOG_ROWS][LOG_COLS * 4 + 1]; /* UTF-8, wrapped */
  33. };
  34. /* Current position, advanced locally between MPD updates while playing */
  35. unsigned int radio_state_elapsed_ms(const struct radio_state *s);
  36. /* ---- rendering ---------------------------------------------------------- */
  37. void ui_render(cairo_t *cr, const struct radio_state *s, int width, int height);
  38. /* ---- app ---------------------------------------------------------------- */
  39. struct app_frontend {
  40. void *ctx;
  41. /* pixels is the whole frame; (x, y, w, h) bounds what changed */
  42. void (*present)(void *ctx, const uint32_t *pixels, int stride,
  43. int x, int y, int w, int h);
  44. void (*backlight)(void *ctx, bool on);
  45. };
  46. struct app_options {
  47. const char *host;
  48. unsigned int port;
  49. int width, height;
  50. bool kmsg; /* show this machine's kernel log */
  51. };
  52. struct app;
  53. struct app *app_new(const struct app_options *opt, const struct app_frontend *fe);
  54. void app_free(struct app *app);
  55. void app_key(struct app *app, enum radio_key key);
  56. void app_log(struct app *app, const char *text);
  57. /* ---- MPD client --------------------------------------------------------- */
  58. struct mpdc;
  59. enum mpdc_command {
  60. MPDC_PLAY, MPDC_TOGGLE, MPDC_STOP, MPDC_NEXT, MPDC_PREV,
  61. MPDC_VOLUME_UP, MPDC_VOLUME_DOWN, MPDC_REFRESH,
  62. };
  63. /* Called on the GLib main loop whenever *state may have changed */
  64. typedef void (*mpdc_changed_fn)(void *user);
  65. typedef void (*mpdc_log_fn)(void *user, const char *text);
  66. struct mpdc *mpdc_new(const char *host, unsigned int port, struct radio_state *state,
  67. mpdc_changed_fn changed, mpdc_log_fn log, void *user);
  68. void mpdc_free(struct mpdc *m);
  69. void mpdc_command(struct mpdc *m, enum mpdc_command cmd);
  70. /* ---- kernel log --------------------------------------------------------- */
  71. struct log_kmsg;
  72. struct log_kmsg *log_kmsg_new(mpdc_log_fn log, void *user);
  73. void log_kmsg_free(struct log_kmsg *k);
  74. #endif /* RADIO_APP_H */