ui_render.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include <math.h>
  2. #include <pango/pangocairo.h>
  3. #include <stdio.h>
  4. #include "app.h"
  5. /*
  6. * Absolute sizes: these are panel pixels, not points. Text is drawn without
  7. * antialiasing, so these are fonts whose hinting holds up in plain black and
  8. * white at 9-11 px. Verdana and Andale Mono are Microsoft core fonts, with
  9. * Liberation as the fallback.
  10. */
  11. #define FONT_TITLE "DejaVu Sans 11px"
  12. #define FONT_STATUS "DejaVu Sans Mono 10px"
  13. #define FONT_SMALL "Verdana, Liberation Sans 9px"
  14. #define FONT_LOG "Andale Mono, Liberation Mono 9px"
  15. #define TITLE_Y 0
  16. #define TITLE_LINES 3
  17. #define STATUS_Y 40
  18. #define ICON_SIZE 12
  19. #define BAR_Y 54
  20. #define OPTIONS_Y 60
  21. #define LINK_Y 71
  22. #define LOG_Y 85
  23. #define LOG_LINE_H 10 /* 9 px text: room for Polish accents */
  24. /*
  25. * Gray edges look smeared on the panel at 2 bpp, so glyphs are rendered in
  26. * plain black and white; full hinting snaps the outlines to the pixel grid.
  27. */
  28. #ifndef UI_TEXT_ANTIALIAS
  29. #define UI_TEXT_ANTIALIAS CAIRO_ANTIALIAS_NONE
  30. #endif
  31. static PangoLayout *new_layout(cairo_t *cr, const char *font)
  32. {
  33. PangoLayout *l = pango_cairo_create_layout(cr);
  34. PangoFontDescription *d = pango_font_description_from_string(font);
  35. cairo_font_options_t *fo = cairo_font_options_create();
  36. cairo_font_options_set_antialias(fo, UI_TEXT_ANTIALIAS);
  37. cairo_font_options_set_hint_style(fo, CAIRO_HINT_STYLE_FULL);
  38. cairo_font_options_set_hint_metrics(fo, CAIRO_HINT_METRICS_ON);
  39. pango_cairo_context_set_font_options(pango_layout_get_context(l), fo);
  40. cairo_font_options_destroy(fo);
  41. pango_layout_set_font_description(l, d);
  42. pango_font_description_free(d);
  43. return l;
  44. }
  45. static void draw_text(cairo_t *cr, const char *font, int x, int y, int w,
  46. PangoAlignment align, const char *text)
  47. {
  48. PangoLayout *l = new_layout(cr, font);
  49. pango_layout_set_width(l, w * PANGO_SCALE);
  50. pango_layout_set_alignment(l, align);
  51. pango_layout_set_ellipsize(l, PANGO_ELLIPSIZE_END);
  52. pango_layout_set_text(l, text, -1);
  53. cairo_move_to(cr, x, y);
  54. pango_cairo_show_layout(cr, l);
  55. g_object_unref(l);
  56. }
  57. static void draw_title(cairo_t *cr, int w, const char *text)
  58. {
  59. PangoLayout *l = new_layout(cr, FONT_TITLE);
  60. pango_layout_set_width(l, w * PANGO_SCALE);
  61. pango_layout_set_wrap(l, PANGO_WRAP_WORD_CHAR);
  62. pango_layout_set_ellipsize(l, PANGO_ELLIPSIZE_END);
  63. pango_layout_set_height(l, -TITLE_LINES);
  64. pango_layout_set_text(l, text, -1);
  65. cairo_move_to(cr, 1, TITLE_Y);
  66. pango_cairo_show_layout(cr, l);
  67. g_object_unref(l);
  68. }
  69. static void draw_icon(cairo_t *cr, enum play_state state, int x, int y)
  70. {
  71. int s = ICON_SIZE;
  72. switch (state) {
  73. case PLAY_PLAYING:
  74. cairo_move_to(cr, x + 2, y);
  75. cairo_line_to(cr, x + s, y + s / 2.0);
  76. cairo_line_to(cr, x + 2, y + s);
  77. cairo_close_path(cr);
  78. cairo_fill(cr);
  79. break;
  80. case PLAY_PAUSED:
  81. cairo_rectangle(cr, x + 2, y, 3, s);
  82. cairo_rectangle(cr, x + 7, y, 3, s);
  83. cairo_fill(cr);
  84. break;
  85. case PLAY_STOPPED:
  86. cairo_rectangle(cr, x + 1, y + 1, s - 2, s - 2);
  87. cairo_fill(cr);
  88. break;
  89. default:
  90. break;
  91. }
  92. }
  93. static void format_time(char *buf, size_t size, unsigned int ms)
  94. {
  95. unsigned int t = ms / 1000;
  96. if (t >= 3600)
  97. snprintf(buf, size, "%u:%02u:%02u", t / 3600, t / 60 % 60, t % 60);
  98. else
  99. snprintf(buf, size, "%u:%02u", t / 60, t % 60);
  100. }
  101. static void draw_status(cairo_t *cr, const struct radio_state *s, int w)
  102. {
  103. char buf[64], a[16], b[16];
  104. unsigned int elapsed;
  105. draw_icon(cr, s->state, 2, STATUS_Y);
  106. if (s->state == PLAY_STOPPED || s->song_pos < 0)
  107. return;
  108. snprintf(buf, sizeof(buf), "#%d/%u", s->song_pos + 1, s->queue_len);
  109. draw_text(cr, FONT_STATUS, 20, STATUS_Y, 90, PANGO_ALIGN_LEFT, buf);
  110. elapsed = radio_state_elapsed_ms(s);
  111. format_time(a, sizeof(a), elapsed);
  112. if (s->total_ms) {
  113. format_time(b, sizeof(b), s->total_ms);
  114. snprintf(buf, sizeof(buf), "%s/%s", a, b);
  115. } else {
  116. g_strlcpy(buf, a, sizeof(buf));
  117. }
  118. draw_text(cr, FONT_STATUS, 110, STATUS_Y, w - 112, PANGO_ALIGN_RIGHT, buf);
  119. if (s->total_ms) {
  120. int bar_w = w - 22;
  121. int fill = (int)((guint64)elapsed * bar_w / s->total_ms);
  122. cairo_set_line_width(cr, 1);
  123. cairo_rectangle(cr, 20.5, BAR_Y + 0.5, bar_w - 1, 3);
  124. cairo_stroke(cr);
  125. cairo_rectangle(cr, 20, BAR_Y, fill, 4);
  126. cairo_fill(cr);
  127. }
  128. }
  129. static void draw_options(cairo_t *cr, const struct radio_state *s, int w)
  130. {
  131. char buf[64];
  132. if (!s->connected)
  133. return;
  134. snprintf(buf, sizeof(buf), "Włączone:%s%s",
  135. s->repeat ? " repeat" : "", s->random ? " random" : "");
  136. draw_text(cr, FONT_SMALL, 1, OPTIONS_Y, 170, PANGO_ALIGN_LEFT, buf);
  137. if (s->volume >= 0)
  138. snprintf(buf, sizeof(buf), "Głośność %d%%", s->volume);
  139. else
  140. g_strlcpy(buf, "Głośność –", sizeof(buf));
  141. draw_text(cr, FONT_SMALL, 150, OPTIONS_Y, w - 151, PANGO_ALIGN_RIGHT, buf);
  142. }
  143. static void draw_link(cairo_t *cr, const struct radio_state *s, int w)
  144. {
  145. char buf[192];
  146. if (s->connected)
  147. return;
  148. cairo_rectangle(cr, 0, LINK_Y, w, 12);
  149. cairo_fill(cr);
  150. cairo_set_source_rgb(cr, 1, 1, 1);
  151. snprintf(buf, sizeof(buf), "Brak połączenia z %s", s->server);
  152. draw_text(cr, FONT_SMALL, 2, LINK_Y, w - 4, PANGO_ALIGN_LEFT, buf);
  153. cairo_set_source_rgb(cr, 0, 0, 0);
  154. }
  155. static void draw_log(cairo_t *cr, const struct radio_state *s, int w, int h)
  156. {
  157. int i;
  158. cairo_set_line_width(cr, 1);
  159. cairo_move_to(cr, 0, LOG_Y - 1.5);
  160. cairo_line_to(cr, w, LOG_Y - 1.5);
  161. cairo_stroke(cr);
  162. for (i = 0; i < LOG_ROWS; i++) {
  163. int y = LOG_Y + i * LOG_LINE_H;
  164. if (y + LOG_LINE_H > h)
  165. break;
  166. if (s->log[i][0])
  167. draw_text(cr, FONT_LOG, 1, y - 1, w - 1, PANGO_ALIGN_LEFT, s->log[i]);
  168. }
  169. }
  170. void ui_render(cairo_t *cr, const struct radio_state *s, int width, int height)
  171. {
  172. cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
  173. cairo_set_source_rgb(cr, 1, 1, 1);
  174. cairo_paint(cr);
  175. cairo_set_source_rgb(cr, 0, 0, 0);
  176. if (s->title[0])
  177. draw_title(cr, width - 2, s->title);
  178. draw_status(cr, s, width);
  179. draw_options(cr, s, width);
  180. draw_link(cr, s, width);
  181. draw_log(cr, s, width, height);
  182. }