1 /*
2 * $Id: screen_lyrics.c 3355 2006-09-1 17:44:04Z tradiaz $
3 *
4 * (c) 2006 by Kalle Wallin <kaw@linux.se>
5 * Tue Aug 1 23:17:38 2006
6 * lyrics enhancement written by Andreas Obergrusberger <tradiaz@yahoo.de>
7 * using www.leoslyrics.com XML API
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
24 #include <stdlib.h>
25 #include <string.h>
26 #include <glib.h>
27 #include <ncurses.h>
28 #include <expat.h>
29 #include <unistd.h>
31 #include "config.h"
32 #ifndef DISABLE_LYRICS_SCREEN
33 #include "ncmpc.h"
34 #include "options.h"
35 #include "mpdclient.h"
36 #include "command.h"
37 #include "screen.h"
38 #include "screen_utils.h"
39 #include "easy_download.h"
40 #include "strfsong.h"
42 #define LEOSLYRICS_SEARCH_URL "http://api.leoslyrics.com/api_search.php?auth=QuodLibet&artist=%s&songtitle=%s"
44 #define LEOSLYRICS_CONTENT_URL "http://api.leoslyrics.com/api_lyrics.php?auth=QuodLibet&hid=%s"
46 #define CREDITS "Lyrics provided by www.LeosLyrics.com"
47 typedef struct _formed_text
48 {
49 GString *text;
50 GArray *lines;
51 int val;
52 } formed_text;
55 XML_Parser parser, contentp;
56 static int lyrics_text_rows = -1;
57 static list_window_t *lw = NULL;
58 guint8 result;
59 char *hid;
60 GTimer *dltime;
61 short int lock;
62 formed_text lyr_text;
63 /* result is a bitset in which the succes when searching 4 lyrics is logged
64 countend by position - backwards
65 0: lyrics in database
66 1: proper access to the lyrics provider
67 2: lyrics found
68 3: exact match
69 4: lyrics downloaded
70 5: lyrics saved
71 wasting 3 bits doesn't mean being a fat memory hog like kde.... does it?
72 */
73 static void lyrics_paint(screen_t *screen, mpdclient_t *c);
75 void get_text_line(formed_text *text, int num, char *dest, int len)
76 {
77 memset(dest, '\0', len*sizeof(char));
78 if(num >= text->lines->len-1) return;
79 int linelen;
80 if(num == 0)
81 {
82 linelen = g_array_index(text->lines, int, num);
83 memcpy(dest, text->text->str, linelen*sizeof(char));
84 }
85 else if(num == 1)
86 { //dont ask me why, but this is needed....
87 linelen = g_array_index(text->lines, int, num)
88 - g_array_index(text->lines, int, num-1);
89 memcpy(dest, &text->text->str[g_array_index(text->lines, int, num-1)],
90 linelen*sizeof(char));
91 }
92 else
93 {
94 linelen = g_array_index(text->lines, int, num+1)
95 - g_array_index(text->lines, int, num);
96 memcpy(dest, &text->text->str[g_array_index(text->lines, int, num)],
97 linelen*sizeof(char));
98 }
99 dest[linelen] = '\n';
100 dest[linelen+1] = '\0';
101 }
103 void add_text_line(formed_text *dest, const char *src, int len)
104 {
105 // need this because g_array_append_val doesnt work with literals
106 // and expat sends "\n" as an extra line everytime
107 if(len == 0)
108 {
109 dest->val = strlen(src);
110 if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int,
111 dest->lines->len-1);
112 g_string_append(dest->text, src);
113 g_array_append_val(dest->lines, dest->val);
114 return;
115 }
116 if(len > 1 || dest->val == 0)
117 {
118 dest->val = len;
119 if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int,
120 dest->lines->len-1);
121 }
122 else if (len == 1 && dest->val != 0) dest->val = 0;
124 if(dest->val > 0)
125 {
126 g_string_append_len(dest->text, src, len);
127 g_array_append_val(dest->lines, dest->val);
128 }
129 }
131 void formed_text_init(formed_text *text)
132 {
133 if(text->text != NULL) g_string_free(text->text, TRUE);
134 text->text = g_string_new("");
136 if(text->lines != NULL) g_array_free(text->lines, TRUE);
137 text->lines = g_array_new(FALSE, TRUE, 4);
139 text->val = 0;
140 }
141 /*
142 char *check_lyr_hd(char *artist, char *title, int how)
143 { //checking whether for lyrics file existence and proper access
144 static char path[1024];
145 snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
146 getenv("HOME"), artist, title);
148 if(g_access(path, how) != 0) return NULL;
149 return path;
150 }
152 int get_lyr_hd(char *artist, char *title)
153 {
154 char *path = check_lyr_hd(artist, title, R_OK);
155 if(path == NULL) return -1;
157 FILE *lyr_file;
158 lyr_file = fopen(path, "r");
159 if(lyr_file == NULL) return -1;
161 char *buf = NULL;
162 char **line = &buf;
163 size_t n = 0;
165 while(1)
166 {
167 n = getline(line, &n, lyr_file);
168 if( n < 1 || *line == NULL || feof(lyr_file) != 0 ) return 0;
169 add_text_line(&lyr_text, *line, n+1);
170 free(*line);
171 *line = NULL; n = 0;
172 }
174 return 0;
175 }
177 int store_lyr_hd()
178 {
179 char artist[512];
180 char title[512];
181 static char path[1024];
182 FILE *lyr_file;
184 get_text_line(&lyr_text, 0, artist, 512);
185 get_text_line(&lyr_text, 1, title, 512);
186 artist[strlen(artist)-2] = '\0';
187 title[strlen(title)-2] = '\0';
189 snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
190 getenv("HOME"), artist, title);
192 if (check_lyr_hd(artist, title, F_OK) == 0) lyr_file = fopen(path, "w");
193 else lyr_file = fopen(path, "xw");
194 if(lyr_file == NULL) return -1;
196 int i;
197 char line_buf[1024];
199 for(i = 2; i <= lyr_text.text->len; i++)
200 {
201 get_text_line(&lyr_text, i, line_buf, 0);
202 fputs(line_buf, lyr_file);
203 }
204 fclose(lyr_file);
205 return 0;
206 }
208 */
209 void check_repaint()
210 {
211 if(screen_get_id("lyrics") == get_cur_mode_id())lyrics_paint(NULL, NULL);
212 }
214 int check_dl_progress(void *clientp, double dltotal, double dlnow,
215 double ultotal, double ulnow)
216 {
217 if(g_timer_elapsed(dltime, NULL) >= options.lyrics_timeout || lock == 4)
218 {
219 formed_text_init(&lyr_text);
220 return -1;
221 }
223 return 0;
224 }
228 static void check_content(void *data, const char *name, const char **atts)
229 {
230 if(strstr(name, "text") != NULL)
231 {
233 result |= 16;
234 }
235 }
238 static void check_search_response(void *data, const char *name,
239 const char **atts)
240 {
241 if(strstr(name, "response") != NULL)
242 {
243 result |=2;
244 return;
245 }
247 if(result & 4)
248 {
249 if(strstr(name, "result") != NULL)
250 {
251 if(strstr(atts[2], "hid") != NULL)
252 {
253 hid = atts[3];
254 }
256 if(strstr(atts[2], "exactMatch") != NULL)
257 {
258 result |= 8;
259 }
260 }
261 }
263 }
265 static void end_tag(void *data, const char *name)
266 {
267 //hmmmmmm
268 }
270 static void check_search_success(void *userData, const XML_Char *s, int len)
271 {
272 if(result & 2) //lets first check whether we're right
273 { //we don't really want to search in the wrong string
274 if(strstr((char*) s, "SUCCESS"))
275 {
276 result |=4;
277 }
278 }
279 }
281 static void fetch_text(void *userData, const XML_Char *s, int len)
282 {
283 if(result & 16)
284 {
285 add_text_line(&lyr_text, s, len);
286 }
287 }
289 gpointer get_lyr(void *c)
290 {
291 mpd_Status *status = ((mpdclient_t*)c)->status;
292 mpd_Song *cur = ((mpdclient_t*)c)->song;
293 mpdclient_update((mpdclient_t*)c);
295 if(!(IS_PAUSED(status->state)||IS_PLAYING(status->state)))
296 {
297 formed_text_init(&lyr_text);
298 return NULL;
299 }
301 char url_avail[256];
302 char url_hid[256];
303 char artist[MAX_SONGNAME_LENGTH];
304 char title[MAX_SONGNAME_LENGTH];
305 lock = 2;
306 result = 0;
308 if(dltime == NULL) dltime = g_timer_new();
310 strfsong(artist, MAX_SONGNAME_LENGTH, "%artist%", cur);
311 strfsong(title, MAX_SONGNAME_LENGTH, "%title%", cur);
313 formed_text_init(&lyr_text);
314 add_text_line(&lyr_text, artist, 0);
315 add_text_line(&lyr_text, title, 0);
317 //if(get_lyr_hd(artist, title) == 0) return &lyr_text;
319 //this replacess the whitespaces with '+'
320 g_strdelimit(artist, " ", '+');
321 g_strdelimit(title, " ", '+');
323 //we insert the artist and the title into the url
324 snprintf(url_avail, 512, LEOSLYRICS_SEARCH_URL, artist, title);
326 //download that xml!
327 easy_download_struct lyr_avail = {NULL, 0,-1};
329 g_timer_start(dltime);
330 if(!easy_download(url_avail, &lyr_avail, check_dl_progress)) return NULL;
331 g_timer_stop(dltime);
333 //we gotta parse that stuff with expat
334 parser = XML_ParserCreate(NULL);
335 XML_SetUserData(parser, NULL);
337 int state = 0;
339 XML_SetElementHandler(parser, check_search_response, end_tag);
340 XML_SetCharacterDataHandler(parser, check_search_success);
341 XML_Parse(parser, lyr_avail.data, strlen(lyr_avail.data), state);
342 XML_ParserFree(parser);
344 if(!(result & 4)) return NULL; //check whether lyrics found
345 easy_download_struct lyr_content = {NULL, 0,-1};
346 snprintf(url_hid, 512, LEOSLYRICS_CONTENT_URL, hid);
348 g_timer_continue(dltime);
349 if(!(easy_download(url_hid, &lyr_content, check_dl_progress))) return NULL;
350 g_timer_stop(dltime);
352 contentp = XML_ParserCreate(NULL);
353 XML_SetUserData(contentp, NULL);
354 XML_SetElementHandler(contentp, check_content, end_tag);
355 XML_SetCharacterDataHandler(contentp, fetch_text);
356 XML_Parse(contentp, lyr_content.data, strlen(lyr_content.data), state);
357 XML_ParserFree(contentp);
359 lw->start = 0;
360 check_repaint();
362 lock = 1;
363 return &lyr_text;
364 }
366 static char *
367 list_callback(int index, int *highlight, void *data)
368 {
369 static char buf[512];
370 int linelen;
372 //i think i'ts fine to write it into the 1st line...
373 if((index == lyr_text.lines->len && lyr_text.lines->len != 2)||
374 ((lyr_text.lines->len == 0
375 ||lyr_text.lines->len == 2) && index == 0))
376 {
377 *highlight=3;
378 return CREDITS;
379 }
381 if(index < 2 && lyr_text.lines->len > 2) *highlight=3;
382 else if(index >= lyr_text.lines->len || index == 2)
383 {
384 return "";
385 }
386 if(index >1) index--;
387 get_text_line(&lyr_text, index, buf, 512);
388 return buf;
389 }
392 static void
393 lyrics_init(WINDOW *w, int cols, int rows)
394 {
395 lw = list_window_init(w, cols, rows);
396 lw->flags = LW_HIDE_CURSOR;
397 //lyr_text.lines = g_array_new(FALSE, TRUE, 4);
398 formed_text_init(&lyr_text);
399 if (!g_thread_supported()) g_thread_init(NULL);
401 }
403 static void
404 lyrics_resize(int cols, int rows)
405 {
406 lw->cols = cols;
407 lw->rows = rows;
408 }
410 static void
411 lyrics_exit(void)
412 {
413 list_window_free(lw);
414 }
417 static char *
418 lyrics_title(char *str, size_t size)
419 {
420 if(lyr_text.lines->len == 2){
421 if(lock == 1 && !(result & 2)) return _("Lyrics [No connection]");
422 if(lock == 1 && !(result & 4)) return _("Lyrics [Not found]");
423 if(lock == 2) return _("Lyrics [retrieving]");
424 }
425 /*if(lyr_text.lines->len > 2)
426 {
427 static char buf[512];
428 char artist[512];
429 char title[512];
430 get_text_line(&lyr_text, 0, artist, 512);
431 get_text_line(&lyr_text, 1, artist, 512);
432 snprintf(buf, 512, "Lyrics %s - %s", artist, title);
433 return buf;
434 }*/
435 return "Lyrics";
436 }
438 static void
439 lyrics_paint(screen_t *screen, mpdclient_t *c)
440 {
441 lw->clear = 1;
442 list_window_paint(lw, list_callback, NULL);
443 wrefresh(lw->w);
444 }
446 static void
447 lyrics_update(screen_t *screen, mpdclient_t *c)
448 {
449 if( lw->repaint )
450 {
451 list_window_paint(lw, list_callback, NULL);
452 wrefresh(lw->w);
453 lw->repaint = 0;
454 }
455 }
458 static int
459 lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
460 {
461 lw->repaint=1;
462 switch(cmd)
463 {
464 case CMD_LIST_NEXT:
465 if( lw->start+lw->rows < lyr_text.lines->len+1 )
466 lw->start++;
467 return 1;
468 case CMD_LIST_PREVIOUS:
469 if( lw->start >0 )
470 lw->start--;
471 return 1;
472 case CMD_LIST_FIRST:
473 lw->start = 0;
474 return 1;
475 case CMD_LIST_LAST:
476 lw->start = lyrics_text_rows-lw->rows;
477 if( lw->start<0 )
478 lw->start = 0;
479 return 1;
480 case CMD_LIST_NEXT_PAGE:
481 lw->start = lw->start + lw->rows-1;
482 if( lw->start+lw->rows >= lyr_text.lines->len+1 )
483 lw->start = lyr_text.lines->len-lw->rows+1;
484 if( lw->start<0 )
485 lw->start = 0;
486 return 1;
487 case CMD_LIST_PREVIOUS_PAGE:
488 lw->start = lw->start - lw->rows;
489 if( lw->start<0 )
490 lw->start = 0;
491 return 1;
492 case CMD_SELECT:
493 g_thread_create(get_lyr, c, FALSE, NULL);
494 return 1;
495 case CMD_INTERRUPT:
496 if(lock != 0) lock = 4;
497 return 1;
498 default:
499 break;
500 }
502 lw->selected = lw->start+lw->rows;
503 if( screen_find(screen, c,
504 lw, lyrics_text_rows,
505 cmd, list_callback, NULL) )
506 {
507 /* center the row */
508 lw->start = lw->selected-(lw->rows/2);
509 if( lw->start+lw->rows > lyrics_text_rows )
510 lw->start = lyrics_text_rows-lw->rows;
511 if( lw->start<0 )
512 lw->start=0;
513 return 1;
514 }
516 return 0;
517 }
519 static list_window_t *
520 lyrics_lw(void)
521 {
522 return lw;
523 }
525 screen_functions_t *
526 get_screen_lyrics(void)
527 {
528 static screen_functions_t functions;
530 memset(&functions, 0, sizeof(screen_functions_t));
531 functions.init = lyrics_init;
532 functions.exit = lyrics_exit;
533 functions.open = NULL;
534 functions.close = NULL;
535 functions.resize = lyrics_resize;
536 functions.paint = lyrics_paint;
537 functions.update = lyrics_update;
538 functions.cmd = lyrics_cmd;
539 functions.get_lw = lyrics_lw;
540 functions.get_title = lyrics_title;
542 return &functions;
543 }
544 #endif /* ENABLE_LYRICS_SCREEN */