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"
25 #include <glib/gprintf.h>
26 #include <string.h>
28 static list_window_t *lw;
30 static struct {
31 struct mpd_song *song;
32 GPtrArray *lines;
33 } current;
35 static void
36 screen_song_clear(void)
37 {
38 for (guint i = 0; i < current.lines->len; ++i)
39 g_free(g_ptr_array_index(current.lines, i));
41 g_ptr_array_set_size(current.lines, 0);
43 if (current.song != NULL) {
44 mpd_freeSong(current.song);
45 current.song = NULL;
46 }
47 }
49 static void
50 screen_song_paint(void);
52 /**
53 * Repaint and update the screen.
54 */
55 static void
56 screen_song_repaint(void)
57 {
58 screen_song_paint();
59 wrefresh(lw->w);
60 }
62 static const char *
63 screen_song_list_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
64 G_GNUC_UNUSED void *data)
65 {
66 static char buffer[256];
67 char *value;
69 if (idx >= current.lines->len)
70 return NULL;
72 value = utf8_to_locale(g_ptr_array_index(current.lines, idx));
73 g_strlcpy(buffer, value, sizeof(buffer));
74 g_free(value);
76 return buffer;
77 }
80 static void
81 screen_song_init(WINDOW *w, int cols, int rows)
82 {
83 current.lines = g_ptr_array_new();
84 lw = list_window_init(w, cols, rows);
85 lw->hide_cursor = true;
86 }
88 static void
89 screen_song_exit(void)
90 {
91 list_window_free(lw);
93 screen_song_clear();
95 g_ptr_array_free(current.lines, TRUE);
96 current.lines = NULL;
97 }
99 static void
100 screen_song_resize(int cols, int rows)
101 {
102 lw->cols = cols;
103 lw->rows = rows;
104 }
106 static const char *
107 screen_song_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
108 {
109 return _("Song viewer");
110 }
112 static void
113 screen_song_paint(void)
114 {
115 list_window_paint(lw, screen_song_list_callback, NULL);
116 }
118 static bool
119 screen_song_cmd(mpdclient_t *c, command_t cmd)
120 {
121 if (list_window_scroll_cmd(lw, current.lines->len, cmd)) {
122 screen_song_repaint();
123 return true;
124 }
126 switch(cmd) {
127 case CMD_LOCATE:
128 if (current.song != NULL) {
129 screen_file_goto_song(c, current.song);
130 return true;
131 }
133 return false;
135 #ifdef ENABLE_LYRICS_SCREEN
136 case CMD_SCREEN_LYRICS:
137 if (current.song != NULL) {
138 screen_lyrics_switch(c, current.song);
139 return true;
140 }
142 return false;
143 #endif
145 default:
146 break;
147 }
149 if (screen_find(lw, current.lines->len,
150 cmd, screen_song_list_callback, NULL)) {
151 /* center the row */
152 list_window_center(lw, current.lines->len, lw->selected);
153 screen_song_repaint();
154 return true;
155 }
157 return false;
158 }
160 const struct screen_functions screen_song = {
161 .init = screen_song_init,
162 .exit = screen_song_exit,
163 .resize = screen_song_resize,
164 .paint = screen_song_paint,
165 .cmd = screen_song_cmd,
166 .get_title = screen_song_title,
167 };
169 static void
170 screen_song_append(const char *label, const char *value, int label_col)
171 {
172 int value_col, linebreaks, entry_size, label_size;
173 int i, k;
174 gchar *entry, *entry_iter;
175 const gchar *value_iter;
177 assert(label != NULL);
178 assert(g_utf8_validate(label, -1, NULL));
180 if (value != NULL) {
181 assert(g_utf8_validate(value, -1, NULL));
182 /* +2 for ': ' */
183 label_col += 2;
184 value_col = lw->cols - label_col;
185 /* calculate the number of required linebreaks */
186 linebreaks = (utf8_width(value) - 1) / value_col + 1;
187 value_iter = value;
188 label_size = strlen(label) + label_col - utf8_width(label);
189 entry_size = label_size + strlen(value) + 2;
191 for (i = 0; i < linebreaks; ++i)
192 {
193 entry = g_malloc(entry_size);
194 if (i == 0) {
195 entry_iter = entry + g_sprintf(entry, "%s: ", label);
196 /* fill the label column with whitespaces */
197 for ( ; entry_iter < entry + label_size; ++entry_iter)
198 *entry_iter = ' ';
199 }
200 else {
201 entry_iter = entry;
202 /* fill the label column with whitespaces */
203 for ( ; entry_iter < entry + label_col; ++entry_iter)
204 *entry_iter = ' ';
205 }
206 /* skip whitespaces */
207 while (g_ascii_isspace(*value_iter)) ++value_iter;
208 k = 0;
209 while (value_iter && k < value_col)
210 {
211 g_utf8_strncpy(entry_iter, value_iter, 1);
212 value_iter = g_utf8_find_next_char(value_iter, NULL);
213 entry_iter = g_utf8_find_next_char(entry_iter, NULL);
214 ++k;
215 }
216 *entry_iter = '\0';
217 g_ptr_array_add(current.lines, entry);
218 }
219 }
220 }
222 void
223 screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
224 {
225 unsigned int i, max_label_width;
226 enum label {
227 ARTIST, TITLE, ALBUM, COMPOSER, NAME, DISC, TRACK,
228 DATE, GENRE, COMMENT, PATH
229 };
230 const char *labels[] = { [ARTIST] = _("Artist"),
231 [TITLE] = _("Title"),
232 [ALBUM] = _("Album"),
233 [COMPOSER] = _("Composer"),
234 [NAME] = _("Name"),
235 [DISC] = _("Disc"),
236 [TRACK] = _("Track"),
237 [DATE] = _("Date"),
238 [GENRE] = _("Genre"),
239 [COMMENT] = _("Comment"),
240 [PATH] = _("Path"),
241 };
243 assert(song != NULL);
244 assert(song->file != NULL);
246 screen_song_clear();
248 current.song = mpd_songDup(song);
250 /* Determine the width of the longest label */
251 max_label_width = utf8_width(labels[0]);
252 for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
253 if (utf8_width(labels[i]) > max_label_width)
254 max_label_width = utf8_width(labels[i]);
255 }
257 screen_song_append(labels[ARTIST], song->artist, max_label_width);
258 screen_song_append(labels[TITLE], song->title, max_label_width);
259 screen_song_append(labels[ALBUM], song->album, max_label_width);
260 screen_song_append(labels[COMPOSER], song->composer, max_label_width);
261 screen_song_append(labels[NAME], song->name, max_label_width);
262 screen_song_append(labels[DISC], song->disc, max_label_width);
263 screen_song_append(labels[TRACK], song->track, max_label_width);
264 screen_song_append(labels[DATE], song->date, max_label_width);
265 screen_song_append(labels[GENRE], song->genre, max_label_width);
266 screen_song_append(labels[COMMENT], song->comment, max_label_width);
267 screen_song_append(labels[PATH], song->file, max_label_width);
269 screen_switch(&screen_song, c);
270 }