Code

screen_song: Added some statistics about mpd to the song screen.
[ncmpc.git] / src / screen_song.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  
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;
108 static void
109 screen_song_resize(int cols, int rows)
111         lw->cols = cols;
112         lw->rows = rows;
115 static const char *
116 screen_song_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
118         return _("Song viewer");
121 static void
122 screen_song_paint(void)
124         list_window_paint(lw, screen_song_list_callback, NULL);
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)
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         }
182 static void
183 screen_song_add_song(const struct mpd_song *song, const mpdclient_t *c)
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 && g_strcmp0(c->song->file, song->file) == 0) {
224                 char buf[16];
225                 g_snprintf(buf, sizeof(buf), _("%d kbps"), c->status->bitRate);
226                 screen_song_append(labels[BITRATE], buf, max_label_width);
227         }
230 static void
231 screen_song_add_stats(const mpdclient_t *c)
233         unsigned i, max_label_width;
234         char buf[64];
235         char *duration;
236         GDate *date;
237         enum label {
238                 ARTISTS, ALBUMS, SONGS, UPTIME,
239                 DBUPTIME, PLAYTIME, DBPLAYTIME
240         };
241         const char *labels[] = { [ARTISTS] = _("Number of artists"),
242                 [ALBUMS] = _("Number of albums"),
243                 [SONGS] = _("Number of songs"),
244                 [UPTIME] = _("Uptime"),
245                 [DBUPTIME] =_("DB last updated"),
246                 [PLAYTIME] = _("Playtime"),
247                 [DBPLAYTIME] = _("DB playtime")
248         };
249         mpd_Stats *mpd_stats = NULL;
250         if (c->connection != NULL) {
251                 mpd_sendStatsCommand(c->connection);
252                 mpd_stats = mpd_getStats(c->connection);
253         }
255         if (mpd_stats != NULL) {
256                 /* Determine the width of the longest label */
257                 max_label_width = utf8_width(labels[0]);
258                 for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
259                         if (utf8_width(labels[i]) > max_label_width)
260                                 max_label_width = utf8_width(labels[i]);
261                 }
263                 g_ptr_array_add(current.lines, g_strdup(_("MPD Statistics")) );
264                 g_snprintf(buf, sizeof(buf), "%d", mpd_stats->numberOfArtists);
265                 screen_song_append(labels[ARTISTS], buf, max_label_width);
266                 g_snprintf(buf, sizeof(buf), "%d", mpd_stats->numberOfAlbums);
267                 screen_song_append(labels[ALBUMS], buf, max_label_width);
268                 g_snprintf(buf, sizeof(buf), "%d", mpd_stats->numberOfSongs);
269                 screen_song_append(labels[SONGS], buf, max_label_width);
270                 duration = time_seconds_to_durationstr(mpd_stats->dbPlayTime);
271                 screen_song_append(labels[DBPLAYTIME], duration, max_label_width);
272                 g_free(duration);
273                 duration = time_seconds_to_durationstr(mpd_stats->playTime);
274                 screen_song_append(labels[PLAYTIME], duration, max_label_width);
275                 g_free(duration);
276                 duration = time_seconds_to_durationstr(mpd_stats->uptime);
277                 screen_song_append(labels[UPTIME], duration, max_label_width);
278                 g_free(duration);
279                 date = g_date_new();
280                 g_date_set_time_t(date, mpd_stats->dbUpdateTime);
281                 g_date_strftime(buf, sizeof(buf), "%x", date);
282                 screen_song_append(labels[DBUPTIME], buf, max_label_width);
283                 g_date_free(date);
284         }
287 static void
288 screen_song_update(mpdclient_t *c)
290         /* if any song changed */
291 /*      if ((c->song != NULL &&
292                                 (current.played_song == NULL ||
293                                  g_strcmp0(c->song->file, current.played_song->file) != 0) ) ||
294                         next_song != NULL)
295         {*/
296         for (guint i = 0; i < current.lines->len; ++i)
297                 g_free(g_ptr_array_index(current.lines, i));
298         g_ptr_array_set_size(current.lines, 0);
300         /* if a song was selected before the song screen was opened */
301         if (next_song != NULL) {
302                 assert(current.selected_song == NULL);
303                 current.selected_song = mpd_songDup(next_song);
304                 next_song = NULL;
305         }
307         if (current.selected_song != NULL &&
308                         (c->song == NULL ||
309                          g_strcmp0(current.selected_song->file, c->song->file) != 0 ||
310                         (c->status->state != MPD_STATUS_STATE_PLAY &&
311                          c->status->state != MPD_STATUS_STATE_PAUSE)) ) {
312                 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
313                 screen_song_add_song(current.selected_song, c);
314                 g_ptr_array_add(current.lines, g_strdup("\0"));
315         }
317         if (c->song != NULL && (c->status->state == MPD_STATUS_STATE_PLAY || 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         //}
335 static bool
336 screen_song_cmd(mpdclient_t *c, command_t cmd)
338         if (list_window_scroll_cmd(lw, current.lines->len, cmd)) {
339                 screen_song_repaint();
340                 return true;
341         }
343         switch(cmd) {
344         case CMD_LOCATE:
345                 if (current.selected_song != NULL) {
346                         screen_file_goto_song(c, current.selected_song);
347                         return true;
348                 }
349                 if (current.played_song != NULL) {
350                         screen_file_goto_song(c, current.played_song);
351                         return true;
352                 }
354                 return false;
356 #ifdef ENABLE_LYRICS_SCREEN
357         case CMD_SCREEN_LYRICS:
358                 if (current.selected_song != NULL) {
359                         screen_lyrics_switch(c, current.selected_song);
360                         return true;
361                 }
362                 if (current.played_song != NULL) {
363                         screen_lyrics_switch(c, current.played_song);
364                         return true;
365                 }
367                 return false;
368 #endif
370         default:
371                 break;
372         }
374         if (screen_find(lw, current.lines->len,
375                         cmd, screen_song_list_callback, NULL)) {
376                 /* center the row */
377                 list_window_center(lw, current.lines->len, lw->selected);
378                 screen_song_repaint();
379                 return true;
380         }
382         return false;
385 const struct screen_functions screen_song = {
386         .init = screen_song_init,
387         .exit = screen_song_exit,
388         .open = screen_song_update,
389         .close = screen_song_clear,
390         .resize = screen_song_resize,
391         .paint = screen_song_paint,
392         .update = screen_song_update,
393         .cmd = screen_song_cmd,
394         .get_title = screen_song_title,
395 };
397 void
398 screen_song_switch(mpdclient_t *c, const struct mpd_song *song)
400         assert(song != NULL);
401         assert(current.selected_song == NULL);
402         assert(current.played_song == NULL);
404         next_song = song;
405         screen_switch(&screen_song, c);