931b114fb86eb789d697ef4a5b7e827ac5f3361a
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 "config.h"
21 #ifndef DISABLE_LYRICS_SCREEN
22 #include <sys/stat.h>
23 #include "ncmpc.h"
24 #include "options.h"
25 #include "mpdclient.h"
26 #include "command.h"
27 #include "screen.h"
28 #include "screen_utils.h"
29 #include "strfsong.h"
30 #include "lyrics.h"
31 #include "gcc.h"
33 #define _GNU_SOURCE
34 #include <stdlib.h>
35 #include <string.h>
36 #include <glib.h>
37 #include <ncurses.h>
38 #include <unistd.h>
39 #include <stdio.h>
41 static list_window_t *lw = NULL;
43 static struct {
44 const struct mpd_song *song;
46 char *artist, *title;
48 struct lyrics_loader *loader;
50 GPtrArray *lines;
51 } current;
53 static void
54 screen_lyrics_abort(void)
55 {
56 if (current.loader != NULL) {
57 lyrics_free(current.loader);
58 current.loader = NULL;
59 }
61 if (current.artist != NULL) {
62 g_free(current.artist);
63 current.artist = NULL;
64 }
66 if (current.title != NULL) {
67 g_free(current.title);
68 current.artist = NULL;
69 }
71 current.song = NULL;
72 }
74 static void
75 screen_lyrics_clear(void)
76 {
77 guint i;
79 for (i = 0; i < current.lines->len; ++i)
80 g_free(g_ptr_array_index(current.lines, i));
82 g_ptr_array_set_size(current.lines, 0);
83 }
85 static const char *
86 list_callback(unsigned idx, int *highlight, void *data);
88 static void
89 screen_lyrics_set(const GString *str)
90 {
91 const char *p, *eol, *next;
93 screen_lyrics_clear();
95 p = str->str;
96 while ((eol = strchr(p, '\n')) != NULL) {
97 char *line;
99 next = eol + 1;
101 /* strip whitespace at end */
103 while (eol > p && (unsigned char)eol[-1] <= 0x20)
104 --eol;
106 /* create copy and append it to current.lines*/
108 line = g_malloc(eol - p + 1);
109 memcpy(line, p, eol - p);
110 line[eol - p] = 0;
112 g_ptr_array_add(current.lines, line);
114 /* reset control characters */
116 for (eol = line + (eol - p); line < eol; ++line)
117 if ((unsigned char)*line < 0x20)
118 *line = ' ';
120 p = next;
121 }
123 if (*p != 0)
124 g_ptr_array_add(current.lines, g_strdup(p));
126 /* paint new data */
128 if (get_cur_mode_id() == 104) { /* XXX don't use the literal number */
129 list_window_paint(lw, list_callback, NULL);
130 wrefresh(lw->w);
132 /* XXX repaint the screen title */
133 }
134 }
136 static void
137 screen_lyrics_callback(const GString *result, mpd_unused void *data)
138 {
139 assert(current.loader != NULL);
141 if (result != NULL)
142 screen_lyrics_set(result);
143 else
144 screen_status_message (_("No lyrics"));
146 lyrics_free(current.loader);
147 current.loader = NULL;
148 }
150 static void
151 screen_lyrics_load(struct mpd_song *song)
152 {
153 char buffer[MAX_SONGNAME_LENGTH];
155 assert(song != NULL);
157 screen_lyrics_abort();
158 screen_lyrics_clear();
160 current.song = song;
162 strfsong(buffer, sizeof(buffer), "%artist%", song);
163 current.artist = g_strdup(buffer);
165 strfsong(buffer, sizeof(buffer), "%title%", song);
166 current.title = g_strdup(buffer);
168 current.loader = lyrics_load(current.artist, current.title,
169 screen_lyrics_callback, NULL);
170 }
172 static FILE *create_lyr_file(const char *artist, const char *title)
173 {
174 char path[1024];
176 snprintf(path, 1024, "%s/.lyrics",
177 getenv("HOME"));
178 mkdir(path, S_IRWXU);
180 snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
181 getenv("HOME"), artist, title);
183 return fopen(path, "w");
184 }
186 static int store_lyr_hd(void)
187 {
188 FILE *lyr_file;
189 unsigned i;
191 lyr_file = create_lyr_file(current.artist, current.title);
192 if (lyr_file == NULL)
193 return -1;
195 for (i = 0; i < current.lines->len; ++i)
196 fprintf(lyr_file, "%s\n",
197 (const char*)g_ptr_array_index(current.lines, i));
199 fclose(lyr_file);
200 return 0;
201 }
203 static const char *
204 list_callback(unsigned idx, mpd_unused int *highlight, mpd_unused void *data)
205 {
206 if (idx >= current.lines->len)
207 return NULL;
209 return g_ptr_array_index(current.lines, idx);
210 }
213 static void
214 lyrics_screen_init(WINDOW *w, int cols, int rows)
215 {
216 current.lines = g_ptr_array_new();
217 lw = list_window_init(w, cols, rows);
218 lw->flags = LW_HIDE_CURSOR;
219 }
221 static void
222 lyrics_resize(int cols, int rows)
223 {
224 lw->cols = cols;
225 lw->rows = rows;
226 }
228 static void
229 lyrics_exit(void)
230 {
231 list_window_free(lw);
233 screen_lyrics_abort();
234 screen_lyrics_clear();
236 g_ptr_array_free(current.lines, TRUE);
237 current.lines = NULL;
238 }
240 static void
241 lyrics_open(mpd_unused screen_t *screen, mpdclient_t *c)
242 {
243 if (c->song != NULL && c->song != current.song)
244 screen_lyrics_load(c->song);
245 }
248 static const char *
249 lyrics_title(char *str, size_t size)
250 {
251 if (current.loader != NULL)
252 return "Lyrics (loading)";
253 else if (current.artist != NULL && current.title != NULL &&
254 current.lines->len > 0) {
255 snprintf(str, size, "Lyrics: %s - %s",
256 current.artist, current.title);
257 return str;
258 } else
259 return "Lyrics";
260 }
262 static void
263 lyrics_paint(mpd_unused mpdclient_t *c)
264 {
265 list_window_paint(lw, list_callback, NULL);
266 }
269 static void
270 lyrics_update(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
271 {
272 if( lw->repaint ) {
273 list_window_paint(lw, list_callback, NULL);
274 lw->repaint = 0;
275 }
276 }
279 static int
280 lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
281 {
282 if (list_window_scroll_cmd(lw, current.lines->len, cmd))
283 return 1;
285 switch(cmd) {
286 case CMD_INTERRUPT:
287 if (current.loader != NULL) {
288 screen_lyrics_abort();
289 screen_lyrics_clear();
290 }
291 return 1;
292 case CMD_ADD:
293 if (current.loader == NULL && current.artist != NULL &&
294 current.title != NULL && store_lyr_hd() == 0)
295 screen_status_message (_("Lyrics saved!"));
296 return 1;
297 case CMD_LYRICS_UPDATE:
298 if (c->song != NULL) {
299 screen_lyrics_load(c->song);
300 lyrics_paint(NULL);
301 wrefresh(lw->w);
302 }
303 return 1;
304 default:
305 break;
306 }
308 lw->selected = lw->start+lw->rows;
309 if (screen_find(screen,
310 lw, current.lines->len,
311 cmd, list_callback, NULL)) {
312 /* center the row */
313 list_window_center(lw, current.lines->len, lw->selected);
314 return 1;
315 }
317 return 0;
318 }
320 const struct screen_functions screen_lyrics = {
321 .init = lyrics_screen_init,
322 .exit = lyrics_exit,
323 .open = lyrics_open,
324 .close = NULL,
325 .resize = lyrics_resize,
326 .paint = lyrics_paint,
327 .update = lyrics_update,
328 .cmd = lyrics_cmd,
329 .get_title = lyrics_title,
330 };
332 #endif /* ENABLE_LYRICS_SCREEN */