2d61bc0924a1c14d7cdc397ee7c1ba29f1d8ded4
1 /*
2 * (c) 2006 by Kalle Wallin <kaw@linux.se>
3 * Copyright (C) 2008 Max Kellermann <max@duempel.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.
9 *
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 */
20 #include <sys/stat.h>
21 #include "i18n.h"
22 #include "options.h"
23 #include "mpdclient.h"
24 #include "command.h"
25 #include "screen.h"
26 #include "screen_utils.h"
27 #include "strfsong.h"
28 #include "lyrics.h"
29 #include "charset.h"
31 #define _GNU_SOURCE
32 #include <stdlib.h>
33 #include <string.h>
34 #include <glib.h>
35 #include <unistd.h>
36 #include <stdio.h>
38 static list_window_t *lw = NULL;
40 static const struct mpd_song *next_song;
42 static struct {
43 struct mpd_song *song;
45 char *artist, *title;
47 struct lyrics_loader *loader;
49 GPtrArray *lines;
50 } current;
52 static void
53 screen_lyrics_abort(void)
54 {
55 if (current.loader != NULL) {
56 lyrics_free(current.loader);
57 current.loader = NULL;
58 }
60 if (current.artist != NULL) {
61 g_free(current.artist);
62 current.artist = NULL;
63 }
65 if (current.title != NULL) {
66 g_free(current.title);
67 current.artist = NULL;
68 }
70 if (current.song != NULL) {
71 mpd_freeSong(current.song);
72 current.song = NULL;
73 }
74 }
76 static void
77 screen_lyrics_clear(void)
78 {
79 guint i;
81 list_window_reset(lw);
83 for (i = 0; i < current.lines->len; ++i)
84 g_free(g_ptr_array_index(current.lines, i));
86 g_ptr_array_set_size(current.lines, 0);
87 }
89 static void
90 lyrics_paint(void);
92 /**
93 * Repaint and update the screen.
94 */
95 static void
96 lyrics_repaint(void)
97 {
98 lyrics_paint();
99 wrefresh(lw->w);
100 }
102 /**
103 * Repaint and update the screen, if it is currently active.
104 */
105 static void
106 lyrics_repaint_if_active(void)
107 {
108 if (screen_is_visible(&screen_lyrics)) {
109 lyrics_repaint();
111 /* XXX repaint the screen title */
112 }
113 }
115 static void
116 screen_lyrics_set(const GString *str)
117 {
118 const char *p, *eol, *next;
120 screen_lyrics_clear();
122 p = str->str;
123 while ((eol = strchr(p, '\n')) != NULL) {
124 char *line;
126 next = eol + 1;
128 /* strip whitespace at end */
130 while (eol > p && (unsigned char)eol[-1] <= 0x20)
131 --eol;
133 /* create copy and append it to current.lines*/
135 line = g_malloc(eol - p + 1);
136 memcpy(line, p, eol - p);
137 line[eol - p] = 0;
139 g_ptr_array_add(current.lines, line);
141 /* reset control characters */
143 for (eol = line + (eol - p); line < eol; ++line)
144 if ((unsigned char)*line < 0x20)
145 *line = ' ';
147 p = next;
148 }
150 if (*p != 0)
151 g_ptr_array_add(current.lines, g_strdup(p));
153 /* paint new data */
155 lyrics_repaint_if_active();
156 }
158 static void
159 screen_lyrics_callback(const GString *result, G_GNUC_UNUSED void *data)
160 {
161 assert(current.loader != NULL);
163 if (result != NULL)
164 screen_lyrics_set(result);
165 else
166 screen_status_message (_("No lyrics"));
168 lyrics_free(current.loader);
169 current.loader = NULL;
170 }
172 static void
173 screen_lyrics_load(const struct mpd_song *song)
174 {
175 char buffer[MAX_SONGNAME_LENGTH];
177 assert(song != NULL);
179 screen_lyrics_abort();
180 screen_lyrics_clear();
182 current.song = mpd_songDup(song);
184 strfsong(buffer, sizeof(buffer), "%artist%", song);
185 current.artist = g_strdup(buffer);
187 strfsong(buffer, sizeof(buffer), "%title%", song);
188 current.title = g_strdup(buffer);
190 current.loader = lyrics_load(current.artist, current.title,
191 screen_lyrics_callback, NULL);
192 }
194 static FILE *create_lyr_file(const char *artist, const char *title)
195 {
196 char path[1024];
198 snprintf(path, 1024, "%s/.lyrics",
199 getenv("HOME"));
200 mkdir(path, S_IRWXU);
202 snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
203 getenv("HOME"), artist, title);
205 return fopen(path, "w");
206 }
208 static int store_lyr_hd(void)
209 {
210 FILE *lyr_file;
211 unsigned i;
213 lyr_file = create_lyr_file(current.artist, current.title);
214 if (lyr_file == NULL)
215 return -1;
217 for (i = 0; i < current.lines->len; ++i)
218 fprintf(lyr_file, "%s\n",
219 (const char*)g_ptr_array_index(current.lines, i));
221 fclose(lyr_file);
222 return 0;
223 }
225 static const char *
226 list_callback(unsigned idx, G_GNUC_UNUSED int *highlight,
227 G_GNUC_UNUSED void *data)
228 {
229 static char buffer[256];
230 char *value;
232 if (idx >= current.lines->len)
233 return NULL;
235 value = utf8_to_locale(g_ptr_array_index(current.lines, idx));
236 g_strlcpy(buffer, value, sizeof(buffer));
237 free(value);
239 return buffer;
240 }
243 static void
244 lyrics_screen_init(WINDOW *w, int cols, int rows)
245 {
246 current.lines = g_ptr_array_new();
247 lw = list_window_init(w, cols, rows);
248 lw->flags = LW_HIDE_CURSOR;
249 }
251 static void
252 lyrics_resize(int cols, int rows)
253 {
254 lw->cols = cols;
255 lw->rows = rows;
256 }
258 static void
259 lyrics_exit(void)
260 {
261 list_window_free(lw);
263 screen_lyrics_abort();
264 screen_lyrics_clear();
266 g_ptr_array_free(current.lines, TRUE);
267 current.lines = NULL;
268 }
270 static void
271 lyrics_open(mpdclient_t *c)
272 {
273 if (next_song == NULL)
274 next_song = c->song;
276 if (next_song != NULL &&
277 (current.song == NULL ||
278 strcmp(next_song->file, current.song->file) != 0))
279 screen_lyrics_load(next_song);
281 next_song = NULL;
282 }
285 static const char *
286 lyrics_title(char *str, size_t size)
287 {
288 if (current.loader != NULL) {
289 snprintf(str, size, "%s (%s)",
290 _("Lyrics"), _("loading..."));
291 return str;
292 } else if (current.artist != NULL && current.title != NULL &&
293 current.lines->len > 0) {
294 snprintf(str, size, "%s: %s - %s",
295 _("Lyrics"),
296 current.artist, current.title);
297 return str;
298 } else
299 return _("Lyrics");
300 }
302 static void
303 lyrics_paint(void)
304 {
305 list_window_paint(lw, list_callback, NULL);
306 }
308 static bool
309 lyrics_cmd(mpdclient_t *c, command_t cmd)
310 {
311 if (list_window_scroll_cmd(lw, current.lines->len, cmd)) {
312 lyrics_repaint();
313 return true;
314 }
316 switch(cmd) {
317 case CMD_INTERRUPT:
318 if (current.loader != NULL) {
319 screen_lyrics_abort();
320 screen_lyrics_clear();
321 }
322 return true;
323 case CMD_ADD:
324 if (current.loader == NULL && current.artist != NULL &&
325 current.title != NULL && store_lyr_hd() == 0)
326 screen_status_message (_("Lyrics saved!"));
327 return true;
328 case CMD_LYRICS_UPDATE:
329 if (c->song != NULL) {
330 screen_lyrics_load(c->song);
331 lyrics_repaint();
332 }
333 return true;
335 #ifdef ENABLE_SONG_SCREEN
336 case CMD_VIEW:
337 if (current.song != NULL) {
338 screen_song_switch(c, current.song);
339 return true;
340 }
342 break;
343 #endif
345 case CMD_LOCATE:
346 if (current.song != NULL) {
347 screen_file_goto_song(c, current.song);
348 return true;
349 }
351 return false;
353 default:
354 break;
355 }
357 lw->selected = lw->start+lw->rows;
358 if (screen_find(lw, current.lines->len,
359 cmd, list_callback, NULL)) {
360 /* center the row */
361 list_window_center(lw, current.lines->len, lw->selected);
362 lyrics_repaint();
363 return true;
364 }
366 return false;
367 }
369 const struct screen_functions screen_lyrics = {
370 .init = lyrics_screen_init,
371 .exit = lyrics_exit,
372 .open = lyrics_open,
373 .close = NULL,
374 .resize = lyrics_resize,
375 .paint = lyrics_paint,
376 .cmd = lyrics_cmd,
377 .get_title = lyrics_title,
378 };
380 void
381 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song)
382 {
383 assert(song != NULL);
385 next_song = song;
386 screen_switch(&screen_lyrics, c);
387 }