cc4d53801fce2c202aee86ea27954386815039ee
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "i18n.h"
21 #include "screen.h"
22 #include "screen_utils.h"
23 #include "charset.h"
24 #include "utils.h"
26 #include <glib/gprintf.h>
27 #include <string.h>
29 static list_window_t *lw;
31 static const struct mpd_song *next_song;
33 static struct {
34 struct mpd_song *selected_song;
35 struct mpd_song *played_song;
36 GPtrArray *lines;
37 } current;
39 static void
40 screen_song_clear(void)
41 {
42 for (guint i = 0; i < current.lines->len; ++i)
43 g_free(g_ptr_array_index(current.lines, i));
45 g_ptr_array_set_size(current.lines, 0);
47 if (current.selected_song != NULL) {
48 mpd_freeSong(current.selected_song);
49 current.selected_song = NULL;
50 }
51 if (current.played_song != NULL) {
52 mpd_freeSong(current.played_song);
53 current.played_song = NULL;
54 }
55 }
57 static void
58 screen_song_paint(void);
60 /**
61 * Repaint and update the screen.
62 */
63 static void
64 screen_song_repaint(void)
65 {
66 screen_song_paint();
67 wrefresh(lw->w);
68 }
70 static const char *
71 screen_song_list_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
72 G_GNUC_UNUSED void *data)
73 {
74 static char buffer[256];
75 char *value;
77 if (idx >= current.lines->len)
78 return NULL;
80 value = utf8_to_locale(g_ptr_array_index(current.lines, idx));
81 g_strlcpy(buffer, value, sizeof(buffer));
82 g_free(value);
84 return buffer;
85 }
88 static void
89 screen_song_init(WINDOW *w, int cols, int rows)
90 {
91 /* We will need at least 10 lines, so this saves 10 reallocations :) */
92 current.lines = g_ptr_array_sized_new(10);
93 lw = list_window_init(w, cols, rows);
94 lw->hide_cursor = true;
95 }
97 static void
98 screen_song_exit(void)
99 {
100 list_window_free(lw);
102 screen_song_clear();
104 g_ptr_array_free(current.lines, TRUE);
105 current.lines = NULL;
106 }
108 static void
109 screen_song_resize(int cols, int rows)
110 {
111 lw->cols = cols;
112 lw->rows = rows;
113 }
115 static const char *
116 screen_song_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
117 {
118 return _("Song viewer");
119 }
121 static void
122 screen_song_paint(void)
123 {
124 list_window_paint(lw, screen_song_list_callback, NULL);
125 }
127 /* Appends a line with a fixed width for the label column
128 * Handles NULL strings gracefully */
129 static void
130 screen_song_append(const char *label, const char *value, unsigned label_col)
131 {
132 int value_col, linebreaks, entry_size, label_size;
133 int i, k;
134 gchar *entry, *entry_iter;
135 const gchar *value_iter;
137 assert(label != NULL);
138 assert(g_utf8_validate(label, -1, NULL));
140 if (value != NULL) {
141 assert(g_utf8_validate(value, -1, NULL));
142 /* +2 for ': ' */
143 label_col += 2;
144 value_col = lw->cols - label_col;
145 /* calculate the number of required linebreaks */
146 linebreaks = (utf8_width(value) - 1) / value_col + 1;
147 value_iter = value;
148 label_size = strlen(label) + label_col - utf8_width(label);
149 entry_size = label_size + strlen(value) + 2;
151 for (i = 0; i < linebreaks; ++i)
152 {
153 entry = g_malloc(entry_size);
154 if (i == 0) {
155 entry_iter = entry + g_sprintf(entry, "%s: ", label);
156 /* fill the label column with whitespaces */
157 for ( ; entry_iter < entry + label_size; ++entry_iter)
158 *entry_iter = ' ';
159 }
160 else {
161 entry_iter = entry;
162 /* fill the label column with whitespaces */
163 for ( ; entry_iter < entry + label_col; ++entry_iter)
164 *entry_iter = ' ';
165 }
166 /* skip whitespaces */
167 while (g_ascii_isspace(*value_iter)) ++value_iter;
168 k = 0;
169 while (value_iter && k < value_col)
170 {
171 g_utf8_strncpy(entry_iter, value_iter, 1);
172 value_iter = g_utf8_find_next_char(value_iter, NULL);
173 entry_iter = g_utf8_find_next_char(entry_iter, NULL);
174 ++k;
175 }
176 *entry_iter = '\0';
177 g_ptr_array_add(current.lines, entry);
178 }
179 }
180 }
182 static void
183 screen_song_add_song(const struct mpd_song *song, const mpdclient_t *c)
184 {
185 unsigned i, max_label_width;
186 enum label {
187 ARTIST, TITLE, ALBUM, COMPOSER, NAME, DISC, TRACK,
188 DATE, GENRE, COMMENT, PATH, BITRATE
189 };
190 const char *labels[] = { [ARTIST] = _("Artist"),
191 [TITLE] = _("Title"),
192 [ALBUM] = _("Album"),
193 [COMPOSER] = _("Composer"),
194 [NAME] = _("Name"),
195 [DISC] = _("Disc"),
196 [TRACK] = _("Track"),
197 [DATE] = _("Date"),
198 [GENRE] = _("Genre"),
199 [COMMENT] = _("Comment"),
200 [PATH] = _("Path"),
201 [BITRATE] = _("Bitrate"),
202 };
203 /* Determine the width of the longest label */
204 max_label_width = utf8_width(labels[0]);
205 for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
206 if (utf8_width(labels[i]) > max_label_width)
207 max_label_width = utf8_width(labels[i]);
208 }
210 assert(song != NULL);
212 screen_song_append(labels[ARTIST], song->artist, max_label_width);
213 screen_song_append(labels[TITLE], song->title, max_label_width);
214 screen_song_append(labels[ALBUM], song->album, max_label_width);
215 screen_song_append(labels[COMPOSER], song->composer, max_label_width);
216 screen_song_append(labels[NAME], song->name, max_label_width);
217 screen_song_append(labels[DISC], song->disc, max_label_width);
218 screen_song_append(labels[TRACK], song->track, max_label_width);
219 screen_song_append(labels[DATE], song->date, max_label_width);
220 screen_song_append(labels[GENRE], song->genre, max_label_width);
221 screen_song_append(labels[COMMENT], song->comment, max_label_width);
222 screen_song_append(labels[PATH], song->file, max_label_width);
223 if (c->status != NULL && c->song != NULL &&
224 g_strcmp0(c->song->file, song->file) == 0 &&
225 (c->status->state == MPD_STATUS_STATE_PLAY ||
226 c->status->state == MPD_STATUS_STATE_PAUSE) ) {
227 char buf[16];
228 g_snprintf(buf, sizeof(buf), _("%d kbps"), c->status->bitRate);
229 screen_song_append(labels[BITRATE], buf, max_label_width);
230 }
231 }
233 static void
234 screen_song_add_stats(const mpdclient_t *c)
235 {
236 unsigned i, max_label_width;
237 char buf[64];
238 char *duration;
239 GDate *date;
240 enum label {
241 ARTISTS, ALBUMS, SONGS, UPTIME,
242 DBUPTIME, PLAYTIME, DBPLAYTIME
243 };
244 const char *labels[] = { [ARTISTS] = _("Number of artists"),
245 [ALBUMS] = _("Number of albums"),
246 [SONGS] = _("Number of songs"),
247 [UPTIME] = _("Uptime"),
248 [DBUPTIME] =_("DB last updated"),
249 [PLAYTIME] = _("Playtime"),
250 [DBPLAYTIME] = _("DB playtime")
251 };
252 mpd_Stats *mpd_stats = NULL;
253 if (c->connection != NULL) {
254 mpd_sendStatsCommand(c->connection);
255 mpd_stats = mpd_getStats(c->connection);
256 }
258 if (mpd_stats != NULL) {
259 /* Determine the width of the longest label */
260 max_label_width = utf8_width(labels[0]);
261 for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
262 if (utf8_width(labels[i]) > max_label_width)
263 max_label_width = utf8_width(labels[i]);
264 }
266 g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
267 g_snprintf(buf, sizeof(buf), "%d", mpd_stats->numberOfArtists);
268 screen_song_append(labels[ARTISTS], buf, max_label_width);
269 g_snprintf(buf, sizeof(buf), "%d", mpd_stats->numberOfAlbums);
270 screen_song_append(labels[ALBUMS], buf, max_label_width);
271 g_snprintf(buf, sizeof(buf), "%d", mpd_stats->numberOfSongs);
272 screen_song_append(labels[SONGS], buf, max_label_width);
273 duration = time_seconds_to_durationstr(mpd_stats->dbPlayTime);
274 screen_song_append(labels[DBPLAYTIME], duration, max_label_width);
275 g_free(duration);
276 duration = time_seconds_to_durationstr(mpd_stats->playTime);
277 screen_song_append(labels[PLAYTIME], duration, max_label_width);
278 g_free(duration);
279 duration = time_seconds_to_durationstr(mpd_stats->uptime);
280 screen_song_append(labels[UPTIME], duration, max_label_width);
281 g_free(duration);
282 date = g_date_new();
283 g_date_set_time_t(date, mpd_stats->dbUpdateTime);
284 g_date_strftime(buf, sizeof(buf), "%x", date);
285 screen_song_append(labels[DBUPTIME], buf, max_label_width);
286 g_date_free(date);
287 }
288 }
290 static void
291 screen_song_update(mpdclient_t *c)
292 {
293 /* Clear all lines */
294 for (guint i = 0; i < current.lines->len; ++i)
295 g_free(g_ptr_array_index(current.lines, i));
296 g_ptr_array_set_size(current.lines, 0);
298 /* If a song was selected before the song screen was opened */
299 if (next_song != NULL) {
300 assert(current.selected_song == NULL);
301 current.selected_song = mpd_songDup(next_song);
302 next_song = NULL;
303 }
305 if (current.selected_song != NULL &&
306 (c->song == NULL ||
307 g_strcmp0(current.selected_song->file, c->song->file) != 0 ||
308 (c->status->state != MPD_STATUS_STATE_PLAY &&
309 c->status->state != MPD_STATUS_STATE_PAUSE)) ) {
310 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
311 screen_song_add_song(current.selected_song, c);
312 g_ptr_array_add(current.lines, g_strdup("\0"));
313 }
315 if (c->song != NULL &&
316 (c->status->state == MPD_STATUS_STATE_PLAY ||
317 c->status->state == MPD_STATUS_STATE_PAUSE) ) {
318 if (current.played_song != NULL) {
319 mpd_freeSong(current.played_song);
320 }
321 current.played_song = mpd_songDup(c->song);
322 g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
323 screen_song_add_song(current.played_song, c);
324 g_ptr_array_add(current.lines, g_strdup("\0"));
325 }
327 /* Add some statistics about mpd */
328 if (c->connection != NULL)
329 screen_song_add_stats(c);
331 screen_song_repaint();
332 }
334 static bool
335 screen_song_cmd(mpdclient_t *c, command_t cmd)
336 {
337 if (list_window_scroll_cmd(lw, current.lines->len, cmd)) {
338 screen_song_repaint();
339 return true;
340 }
342 switch(cmd) {
343 case CMD_LOCATE:
344 if (current.selected_song != NULL) {
345 screen_file_goto_song(c, current.selected_song);
346 return true;
347 }
348 if (current.played_song != NULL) {
349 screen_file_goto_song(c, current.played_song);
350 return true;
351 }
353 return false;
355 #ifdef ENABLE_LYRICS_SCREEN
356 case CMD_SCREEN_LYRICS:
357 if (current.selected_song != NULL) {
358 screen_lyrics_switch(c, current.selected_song);
359 return true;
360 }
361 if (current.played_song != NULL) {
362 screen_lyrics_switch(c, current.played_song);
363 return true;
364 }
366 return false;
367 #endif
369 default:
370 break;
371 }
373 if (screen_find(lw, current.lines->len,
374 cmd, screen_song_list_callback, NULL)) {
375 /* center the row */
376 list_window_center(lw, current.lines->len, lw->selected);
377 screen_song_repaint();
378 return true;
379 }
381 return false;
382 }
384 const struct screen_functions screen_song = {
385 .init = screen_song_init,
386 .exit = screen_song_exit,
387 .open = screen_song_update,
388 .close = screen_song_clear,
389 .resize = screen_song_resize,
390 .paint = screen_song_paint,
391 .update = screen_song_update,
392 .cmd = screen_song_cmd,
393 .get_title = screen_song_title,
394 };
396 void
397 screen_song_switch(mpdclient_t *c, const struct mpd_song *song)
398 {
399 assert(song != NULL);
400 assert(current.selected_song == NULL);
401 assert(current.played_song == NULL);
403 next_song = song;
404 screen_switch(&screen_song, c);
405 }