ui_render.c 5.3 KB

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