| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #include <math.h>
- #include <pango/pangocairo.h>
- #include <stdio.h>
- #include "app.h"
- /* Absolute sizes: these are panel pixels, not points */
- #define FONT_TITLE "DejaVu Sans 11px"
- #define FONT_STATUS "DejaVu Sans Mono 10px"
- #define FONT_SMALL "DejaVu Sans 9px"
- #define FONT_LOG "DejaVu Sans Mono 9px"
- #define TITLE_Y 0
- #define TITLE_LINES 3
- #define STATUS_Y 40
- #define ICON_SIZE 12
- #define BAR_Y 54
- #define OPTIONS_Y 60
- #define LINK_Y 71
- #define LOG_Y 85
- #define LOG_LINE_H 9
- static PangoLayout *new_layout(cairo_t *cr, const char *font)
- {
- PangoLayout *l = pango_cairo_create_layout(cr);
- PangoFontDescription *d = pango_font_description_from_string(font);
- cairo_font_options_t *fo = cairo_font_options_create();
- /* crisp glyphs that survive reduction to four gray levels */
- cairo_font_options_set_antialias(fo, CAIRO_ANTIALIAS_GRAY);
- cairo_font_options_set_hint_style(fo, CAIRO_HINT_STYLE_FULL);
- cairo_font_options_set_hint_metrics(fo, CAIRO_HINT_METRICS_ON);
- pango_cairo_context_set_font_options(pango_layout_get_context(l), fo);
- cairo_font_options_destroy(fo);
- pango_layout_set_font_description(l, d);
- pango_font_description_free(d);
- return l;
- }
- static void draw_text(cairo_t *cr, const char *font, int x, int y, int w,
- PangoAlignment align, const char *text)
- {
- PangoLayout *l = new_layout(cr, font);
- pango_layout_set_width(l, w * PANGO_SCALE);
- pango_layout_set_alignment(l, align);
- pango_layout_set_ellipsize(l, PANGO_ELLIPSIZE_END);
- pango_layout_set_text(l, text, -1);
- cairo_move_to(cr, x, y);
- pango_cairo_show_layout(cr, l);
- g_object_unref(l);
- }
- static void draw_title(cairo_t *cr, int w, const char *text)
- {
- PangoLayout *l = new_layout(cr, FONT_TITLE);
- pango_layout_set_width(l, w * PANGO_SCALE);
- pango_layout_set_wrap(l, PANGO_WRAP_WORD_CHAR);
- pango_layout_set_ellipsize(l, PANGO_ELLIPSIZE_END);
- pango_layout_set_height(l, -TITLE_LINES);
- pango_layout_set_text(l, text, -1);
- cairo_move_to(cr, 1, TITLE_Y);
- pango_cairo_show_layout(cr, l);
- g_object_unref(l);
- }
- static void draw_icon(cairo_t *cr, enum play_state state, int x, int y)
- {
- int s = ICON_SIZE;
- switch (state) {
- case PLAY_PLAYING:
- cairo_move_to(cr, x + 2, y);
- cairo_line_to(cr, x + s, y + s / 2.0);
- cairo_line_to(cr, x + 2, y + s);
- cairo_close_path(cr);
- cairo_fill(cr);
- break;
- case PLAY_PAUSED:
- cairo_rectangle(cr, x + 2, y, 3, s);
- cairo_rectangle(cr, x + 7, y, 3, s);
- cairo_fill(cr);
- break;
- case PLAY_STOPPED:
- cairo_rectangle(cr, x + 1, y + 1, s - 2, s - 2);
- cairo_fill(cr);
- break;
- default:
- break;
- }
- }
- static void format_time(char *buf, size_t size, unsigned int ms)
- {
- unsigned int t = ms / 1000;
- if (t >= 3600)
- snprintf(buf, size, "%u:%02u:%02u", t / 3600, t / 60 % 60, t % 60);
- else
- snprintf(buf, size, "%u:%02u", t / 60, t % 60);
- }
- static void draw_status(cairo_t *cr, const struct radio_state *s, int w)
- {
- char buf[64], a[16], b[16];
- unsigned int elapsed;
- draw_icon(cr, s->state, 2, STATUS_Y);
- if (s->state == PLAY_STOPPED || s->song_pos < 0)
- return;
- snprintf(buf, sizeof(buf), "#%d/%u", s->song_pos + 1, s->queue_len);
- draw_text(cr, FONT_STATUS, 20, STATUS_Y, 90, PANGO_ALIGN_LEFT, buf);
- elapsed = radio_state_elapsed_ms(s);
- format_time(a, sizeof(a), elapsed);
- if (s->total_ms) {
- format_time(b, sizeof(b), s->total_ms);
- snprintf(buf, sizeof(buf), "%s/%s", a, b);
- } else {
- g_strlcpy(buf, a, sizeof(buf));
- }
- draw_text(cr, FONT_STATUS, 110, STATUS_Y, w - 112, PANGO_ALIGN_RIGHT, buf);
- if (s->total_ms) {
- int bar_w = w - 22;
- int fill = (int)((guint64)elapsed * bar_w / s->total_ms);
- cairo_set_line_width(cr, 1);
- cairo_rectangle(cr, 20.5, BAR_Y + 0.5, bar_w - 1, 3);
- cairo_stroke(cr);
- cairo_rectangle(cr, 20, BAR_Y, fill, 4);
- cairo_fill(cr);
- }
- }
- static void draw_options(cairo_t *cr, const struct radio_state *s, int w)
- {
- char buf[64];
- if (!s->connected)
- return;
- snprintf(buf, sizeof(buf), "Włączone:%s%s",
- s->repeat ? " repeat" : "", s->random ? " random" : "");
- draw_text(cr, FONT_SMALL, 1, OPTIONS_Y, 170, PANGO_ALIGN_LEFT, buf);
- if (s->volume >= 0)
- snprintf(buf, sizeof(buf), "Głośność %d%%", s->volume);
- else
- g_strlcpy(buf, "Głośność –", sizeof(buf));
- draw_text(cr, FONT_SMALL, 150, OPTIONS_Y, w - 151, PANGO_ALIGN_RIGHT, buf);
- }
- static void draw_link(cairo_t *cr, const struct radio_state *s, int w)
- {
- char buf[192];
- if (s->connected)
- return;
- cairo_rectangle(cr, 0, LINK_Y, w, 12);
- cairo_fill(cr);
- cairo_set_source_rgb(cr, 1, 1, 1);
- snprintf(buf, sizeof(buf), "Brak połączenia z %s", s->server);
- draw_text(cr, FONT_SMALL, 2, LINK_Y, w - 4, PANGO_ALIGN_LEFT, buf);
- cairo_set_source_rgb(cr, 0, 0, 0);
- }
- static void draw_log(cairo_t *cr, const struct radio_state *s, int w, int h)
- {
- int i;
- cairo_set_line_width(cr, 1);
- cairo_move_to(cr, 0, LOG_Y - 1.5);
- cairo_line_to(cr, w, LOG_Y - 1.5);
- cairo_stroke(cr);
- for (i = 0; i < LOG_ROWS; i++) {
- int y = LOG_Y + i * LOG_LINE_H;
- if (y + LOG_LINE_H > h)
- break;
- if (s->log[i][0])
- draw_text(cr, FONT_LOG, 1, y - 1, w - 1, PANGO_ALIGN_LEFT, s->log[i]);
- }
- }
- void ui_render(cairo_t *cr, const struct radio_state *s, int width, int height)
- {
- cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
- cairo_set_source_rgb(cr, 1, 1, 1);
- cairo_paint(cr);
- cairo_set_source_rgb(cr, 0, 0, 0);
- if (s->title[0])
- draw_title(cr, width - 2, s->title);
- draw_status(cr, s, width);
- draw_options(cr, s, width);
- draw_link(cr, s, width);
- draw_log(cr, s, width, height);
- }
|