Code

screen_song: check c->status!=NULL
[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 &&
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         }
233 static void
234 screen_song_add_stats(const mpdclient_t *c)
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         }
290 static void
291 screen_song_update(mpdclient_t *c)
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 == NULL ||
309                         (c->status->state != MPD_STATUS_STATE_PLAY &&
310                          c->status->state != MPD_STATUS_STATE_PAUSE)) ) {
311                 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
312                 screen_song_add_song(current.selected_song, c);
313                 g_ptr_array_add(current.lines, g_strdup("\0"));
314         }
316         if (c->song != NULL && c->status != NULL &&
317                         (c->status->state == MPD_STATUS_STATE_PLAY ||
318                          c->status->state == MPD_STATUS_STATE_PAUSE) ) {
319                 if (current.played_song != NULL) {
320                         mpd_freeSong(current.played_song);
321                 }
322                 current.played_song = mpd_songDup(c->song);
323                 g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
324                 screen_song_add_song(current.played_song, c);
325                 g_ptr_array_add(current.lines, g_strdup("\0"));
326         }
328         /* Add some statistics about mpd */
329         if (c->connection != NULL)
330                 screen_song_add_stats(c);
332         screen_song_repaint();
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                 }
366                 return false;
368 #endif
370         case CMD_SCREEN_SWAP:
371                 if (current.selected_song != NULL)
372                         screen_swap(c, current.selected_song);
373                 else
374                 // No need to check if this is null - we'd pass null anyway
375                         screen_swap(c, current.played_song);
376                 return true;
378         default:
379                 break;
380         }
382         if (screen_find(lw, current.lines->len,
383                         cmd, screen_song_list_callback, NULL)) {
384                 /* center the row */
385                 list_window_center(lw, current.lines->len, lw->selected);
386                 screen_song_repaint();
387                 return true;
388         }
390         return false;
393 const struct screen_functions screen_song = {
394         .init = screen_song_init,
395         .exit = screen_song_exit,
396         .open = screen_song_update,
397         .close = screen_song_clear,
398         .resize = screen_song_resize,
399         .paint = screen_song_paint,
400         .update = screen_song_update,
401         .cmd = screen_song_cmd,
402         .get_title = screen_song_title,
403 };
405 void
406 screen_song_switch(mpdclient_t *c, const struct mpd_song *song)
408         assert(song != NULL);
409         assert(current.selected_song == NULL);
410         assert(current.played_song == NULL);
412         next_song = song;
413         screen_switch(&screen_song, c);