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 #define _GNU_SOURCE
25 #include <stdlib.h>
26 #include <string.h>
27 #include <glib.h>
28 #include <ncurses.h>
29 #include <expat.h>
30 #include <unistd.h>
31 #include <glib/gstdio.h>
32 #include <stdio.h>
34 #include "config.h"
35 #ifndef DISABLE_LYRICS_SCREEN
36 #include <sys/stat.h>
37 #include "ncmpc.h"
38 #include "options.h"
39 #include "mpdclient.h"
40 #include "command.h"
41 #include "screen.h"
42 #include "screen_utils.h"
43 #include "easy_download.h"
44 #include "strfsong.h"
47 #define LEOSLYRICS_SEARCH_URL "http://api.leoslyrics.com/api_search.php?auth=ncmpc&artist=%s&songtitle=%s"
49 #define LEOSLYRICS_CONTENT_URL "http://api.leoslyrics.com/api_lyrics.php?auth=ncmpc&hid=%s"
51 #define CREDITS "Lyrics provided by www.LeosLyrics.com"
53 typedef struct _formed_text
54 {
55 GString *text;
56 GArray *lines;
57 int val;
58 } formed_text;
60 typedef struct _retrieval_spec
61 {
62 mpdclient_t *client;
63 int way;
64 } retrieval_spec;
66 typedef struct _lyrics_t
67 {
68 int header_len;
70 guint8 result;
71 char *hid;
72 GTimer *dltime;
74 XML_Parser parser;
75 XML_Parser contentp;
77 formed_text content;
79 } lyrics_t;
81 lyrics_t lyrics;
84 XML_Parser parser, contentp;
85 static int lyrics_text_rows = -1;
86 static list_window_t *lw = NULL;
87 short int lock;
88 /* result is a bitset in which the succes when searching 4 lyrics is logged
89 countend by position - backwards
90 0: lyrics in database
91 1: proper access to the lyrics provider
92 2: lyrics found
93 3: exact match
94 4: lyrics downloaded
95 5: lyrics saved
96 wasting 3 bits doesn't mean being a fat memory hog like kde.... does it?
97 */
98 static void lyrics_paint(screen_t *screen, mpdclient_t *c);
100 int get_text_line(formed_text *text, int num, char *dest, int len)
101 {
102 memset(dest, '\0', len*sizeof(char));
103 if(num >= text->lines->len-1) return -1;
104 int linelen;
105 if(num == 0)
106 {
107 linelen = g_array_index(text->lines, int, num);
108 memcpy(dest, text->text->str, linelen*sizeof(char));
109 }
110 else if(num == 1)
111 { //dont ask me why, but this is needed....
112 linelen = g_array_index(text->lines, int, num)
113 - g_array_index(text->lines, int, num-1);
114 memcpy(dest, &text->text->str[g_array_index(text->lines, int, num-1)],
115 linelen*sizeof(char));
116 }
117 else
118 {
119 linelen = g_array_index(text->lines, int, num+1)
120 - g_array_index(text->lines, int, num);
121 memcpy(dest, &text->text->str[g_array_index(text->lines, int, num)],
122 linelen*sizeof(char));
123 }
124 dest[linelen] = '\n';
125 dest[linelen+1] = '\0';
127 return 0;
128 }
130 void add_text_line(formed_text *dest, const char *src, int len)
131 {
132 // need this because g_array_append_val doesnt work with literals
133 // and expat sends "\n" as an extra line everytime
134 if(len == 0)
135 {
136 dest->val = strlen(src);
137 if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int,
138 dest->lines->len-1);
139 g_string_append(dest->text, src);
140 g_array_append_val(dest->lines, dest->val);
141 return;
142 }
143 if(len > 1 || dest->val == 0)
144 {
145 dest->val = len;
146 if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int,
147 dest->lines->len-1);
148 }
149 else if (len == 1 && dest->val != 0) dest->val = 0;
151 if(dest->val > 0)
152 {
153 g_string_append_len(dest->text, src, len);
154 g_array_append_val(dest->lines, dest->val);
155 }
156 }
158 void formed_text_init(formed_text *text)
159 {
160 if(text->text != NULL) g_string_free(text->text, TRUE);
161 text->text = g_string_new("");
163 if(text->lines != NULL) g_array_free(text->lines, TRUE);
164 text->lines = g_array_new(FALSE, TRUE, 4);
166 text->val = 0;
167 }
169 static void check_content(void *data, const char *name, const char **atts)
170 {
171 if(strstr(name, "text") != NULL)
172 {
174 lyrics.result |= 16;
175 }
176 }
179 static void check_search_response(void *data, const char *name,
180 const char **atts)
181 {
182 if(strstr(name, "response") != NULL)
183 {
184 lyrics.result |=2;
185 return;
186 }
188 if(lyrics.result & 4)
189 {
190 if(strstr(name, "lyrics.result") != NULL)
191 {
192 if(strstr(atts[2], "hid") != NULL)
193 {
194 lyrics.hid = atts[3];
195 }
197 if(strstr(atts[2], "exactMatch") != NULL)
198 {
199 lyrics.result |= 8;
200 }
201 }
202 }
204 }
206 static void end_tag(void *data, const char *name)
207 {
208 //hmmmmmm
209 }
211 static void check_search_success(void *userData, const XML_Char *s, int len)
212 {
213 if(lyrics.result & 2) //lets first check whether we're right
214 { //we don't really want to search in the wrong string
215 if(strstr((char*) s, "SUCCESS"))
216 {
217 lyrics.result |=4;
218 }
219 }
220 }
222 static void
223 fetch_text(void *userData, const XML_Char *s, int len)
224 {
225 if(lyrics.result & 16)
226 {
227 add_text_line(&lyrics.content, s, len);
228 }
229 }
231 int
232 check_dl_progress(void *clientp, double dltotal, double dlnow,
233 double ultotal, double ulnow)
234 {
235 if(g_timer_elapsed(lyrics.dltime, NULL) >= options.lyrics_timeout || lock == 4)
236 {
237 formed_text_init(&lyrics.content);
238 return -1;
239 }
241 return 0;
242 }
245 int
246 check_lyr_http(char *artist, char *title, char *url)
247 {
248 char url_avail[256];
250 //this replacess the whitespaces with '+'
251 g_strdelimit(artist, " ", '+');
252 g_strdelimit(title, " ", '+');
254 //we insert the artist and the title into the url
255 snprintf(url_avail, 512, LEOSLYRICS_SEARCH_URL, artist, title);
257 //download that xml!
258 easy_download_struct lyr_avail = {NULL, 0,-1};
260 g_timer_start(lyrics.dltime);
261 if(!easy_download(url_avail, &lyr_avail, check_dl_progress)) return -1;
262 g_timer_stop(lyrics.dltime);
264 //we gotta parse that stuff with expat
265 parser = XML_ParserCreate(NULL);
266 XML_SetUserData(parser, NULL);
268 XML_SetElementHandler(parser, check_search_response, end_tag);
269 XML_SetCharacterDataHandler(parser, check_search_success);
270 XML_Parse(parser, lyr_avail.data, strlen(lyr_avail.data), 0);
271 XML_ParserFree(parser);
273 if(!(lyrics.result & 4)) return -1; //check whether lyrics found
274 snprintf(url, 512, LEOSLYRICS_CONTENT_URL, lyrics.hid);
276 return 0;
277 }
278 int get_lyr_http(char *artist, char *title)
279 {
280 char url_hid[256];
281 if(lyrics.dltime == NULL) lyrics.dltime = g_timer_new();
283 if(check_lyr_http(artist, title, url_hid) != 0) return -1;
285 easy_download_struct lyr_content = {NULL, 0,-1};
286 g_timer_continue(lyrics.dltime);
287 if(!(easy_download(url_hid, &lyr_content, check_dl_progress))) return -1;
288 g_timer_stop(lyrics.dltime);
290 contentp = XML_ParserCreate(NULL);
291 XML_SetUserData(contentp, NULL);
292 XML_SetElementHandler(contentp, check_content, end_tag);
293 XML_SetCharacterDataHandler(contentp, fetch_text);
294 XML_Parse(contentp, lyr_content.data, strlen(lyr_content.data), 0);
295 XML_ParserFree(contentp);
297 return 0;
299 }
300 FILE *create_lyr_file(char *artist, char *title)
301 {
302 char path[1024];
304 snprintf(path, 1024, "%s/.lyrics",
305 getenv("HOME"));
306 if(g_access(path, W_OK) != 0) if(mkdir(path, S_IRWXU) != 0) return NULL;
308 snprintf(path, 1024, "%s/.lyrics/%s",
309 getenv("HOME"), artist);
311 if(g_access(path, W_OK) != 0) if(mkdir(path, S_IRWXU) != 0) return NULL;
313 snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
314 getenv("HOME"), artist, title);
316 return fopen(path, "w");
317 }
319 char *check_lyr_hd(char *artist, char *title, int how)
320 { //checking whether for lyrics file existence and proper access
321 static char path[1024];
322 snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
323 getenv("HOME"), artist, title);
325 if(g_access(path, how) != 0) return NULL;
327 return path;
328 }
331 int get_lyr_hd(char *artist, char *title)
332 {
333 char *path = check_lyr_hd(artist, title, R_OK);
334 if(path == NULL) return -1;
336 FILE *lyr_file;
337 lyr_file = fopen(path, "r");
338 if(lyr_file == NULL) return -1;
340 char *buf = NULL;
341 char **line = &buf;
342 size_t n = 0;
344 while(1)
345 {
346 n = getline(line, &n, lyr_file);
347 if( n < 1 || *line == NULL || feof(lyr_file) != 0 ) return 0;
348 add_text_line(&lyrics.content, *line, n);
349 free(*line);
350 *line = NULL; n = 0;
351 }
353 return 0;
354 }
356 int store_lyr_hd()
357 {
358 char artist[512];
359 char title[512];
360 static char path[1024];
361 FILE *lyr_file;
363 get_text_line(&lyrics.content, 0, artist, 512);
364 get_text_line(&lyrics.content, 1, title, 512);
365 artist[strlen(artist)-1] = '\0';
366 title[strlen(title)-1] = '\0';
368 snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
369 getenv("HOME"), artist, title);
370 lyr_file = create_lyr_file(artist, title);
371 if(lyr_file == NULL) return -1;
372 int i;
373 char line_buf[1024];
375 for(i = 3; i <= lyrics.content.text->len; i++)
376 {
377 if(get_text_line(&lyrics.content, i, line_buf, 1024) == -1);
378 fputs(line_buf, lyr_file);
379 }
380 fclose(lyr_file);
381 return 0;
382 }
385 void check_repaint()
386 {
387 if(screen_get_id("lyrics") == get_cur_mode_id())lyrics_paint(NULL, NULL);
388 }
391 gpointer get_lyr(void *c)
392 {
393 mpd_Status *status = ((retrieval_spec*)c)->client->status;
394 mpd_Song *cur = ((retrieval_spec*)c)->client->song;
395 //mpdclient_update((mpdclient_t*)c);
397 if(!(IS_PAUSED(status->state)||IS_PLAYING(status->state)))
398 {
399 formed_text_init(&lyrics.content);
400 return NULL;
401 }
404 char artist[MAX_SONGNAME_LENGTH];
405 char title[MAX_SONGNAME_LENGTH];
406 lock = 2;
407 lyrics.result = 0;
409 formed_text_init(&lyrics.content);
411 strfsong(artist, MAX_SONGNAME_LENGTH, "%artist%", cur);
412 strfsong(title, MAX_SONGNAME_LENGTH, "%title%", cur);
414 //write header..
415 formed_text_init(&lyrics.content);
416 add_text_line(&lyrics.content, artist, 0);
417 add_text_line(&lyrics.content, title, 0);
418 add_text_line(&lyrics.content, "", 0);
419 add_text_line(&lyrics.content, "", 0);
421 if (((retrieval_spec*)c)->way == 1)
422 {
423 if(get_lyr_http(artist, title) != 0) return NULL;
424 }
425 else{
426 if(get_lyr_hd(artist, title) != 0)
427 {
428 if(get_lyr_http(artist, title) != 0) return NULL;
429 }
430 else lyrics.result |= 1;
431 }
433 lw->start = 0;
434 check_repaint();
435 lock = 1;
436 return &lyrics.content;
437 }
439 static char *
440 list_callback(int index, int *highlight, void *data)
441 {
442 static char buf[512];
444 //i think i'ts fine to write it into the 1st line...
445 if((index == lyrics.content.lines->len && lyrics.content.lines->len > 4)||
446 ((lyrics.content.lines->len == 0
447 ||lyrics.content.lines->len == 4) && index == 0))
448 {
449 *highlight=3;
450 return CREDITS;
451 }
453 if(index < 2 && lyrics.content.lines->len > 4) *highlight=3;
454 else if(index >= lyrics.content.lines->len ||
455 ( index < 4 && index != 0 && lyrics.content.lines->len < 5))
456 {
457 return "";
458 }
460 get_text_line(&lyrics.content, index, buf, 512);
461 return buf;
462 }
465 static void
466 lyrics_init(WINDOW *w, int cols, int rows)
467 {
468 lw = list_window_init(w, cols, rows);
469 lw->flags = LW_HIDE_CURSOR;
470 //lyrics.content.lines = g_array_new(FALSE, TRUE, 4);
471 formed_text_init(&lyrics.content);
472 if (!g_thread_supported()) g_thread_init(NULL);
474 }
476 static void
477 lyrics_resize(int cols, int rows)
478 {
479 lw->cols = cols;
480 lw->rows = rows;
481 }
483 static void
484 lyrics_exit(void)
485 {
486 list_window_free(lw);
487 }
490 static char *
491 lyrics_title(char *str, size_t size)
492 {
493 if(lyrics.content.lines->len == 4)
494 {
495 if(lock == 1)
496 {
497 if(!(lyrics.result & 1))
498 {
499 if(!(lyrics.result & 2)) return _("Lyrics [No connection]");
500 if(!(lyrics.result & 4)) return _("Lyrics [Not found]");
501 }
502 }
503 if(lock == 2) return _("Lyrics [retrieving]");
504 }
505 /*if(lyrics.content.lines->len > 2)
506 {
507 static char buf[512];
508 char artist[512];
509 char title[512];
510 get_text_line(&lyrics.content, 0, artist, 512);
511 get_text_line(&lyrics.content, 1, artist, 512);
512 snprintf(buf, 512, "Lyrics %s - %s", artist, title);
513 return buf;
514 }*/
515 return "Lyrics";
516 }
518 static void
519 lyrics_paint(screen_t *screen, mpdclient_t *c)
520 {
521 lw->clear = 1;
522 list_window_paint(lw, list_callback, NULL);
523 wrefresh(lw->w);
524 }
526 static void
527 lyrics_update(screen_t *screen, mpdclient_t *c)
528 {
529 if( lw->repaint )
530 {
531 list_window_paint(lw, list_callback, NULL);
532 wrefresh(lw->w);
533 lw->repaint = 0;
534 }
535 }
538 static int
539 lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
540 {
541 lw->repaint=1;
542 static retrieval_spec spec;
543 switch(cmd)
544 {
545 case CMD_LIST_NEXT:
546 if( lw->start+lw->rows < lyrics.content.lines->len+1 )
547 lw->start++;
548 return 1;
549 case CMD_LIST_PREVIOUS:
550 if( lw->start >0 )
551 lw->start--;
552 return 1;
553 case CMD_LIST_FIRST:
554 lw->start = 0;
555 return 1;
556 case CMD_LIST_LAST:
557 lw->start = lyrics_text_rows-lw->rows;
558 if( lw->start<0 )
559 lw->start = 0;
560 return 1;
561 case CMD_LIST_NEXT_PAGE:
562 lw->start = lw->start + lw->rows-1;
563 if( lw->start+lw->rows >= lyrics.content.lines->len+1 )
564 lw->start = lyrics.content.lines->len-lw->rows+1;
565 if( lw->start<0 )
566 lw->start = 0;
567 return 1;
568 case CMD_LIST_PREVIOUS_PAGE:
569 lw->start = lw->start - lw->rows;
570 if( lw->start<0 )
571 lw->start = 0;
572 return 1;
573 case CMD_SELECT:
574 spec.client = c;
575 spec.way = 0;
576 g_thread_create(get_lyr, &spec, FALSE, NULL);
577 return 1;
578 case CMD_INTERRUPT:
579 if(lock > 1) lock = 4;
580 return 1;
581 case CMD_ADD:
582 if(lock > 0 && lock != 4)
583 {
584 if(store_lyr_hd() == 0) screen_status_message (_("Lyrics saved!"));
585 }
586 return 1;
587 case CMD_LYRICS_UPDATE:
588 spec.client = c;
589 spec.way = 1;
590 g_thread_create(get_lyr, &spec, FALSE, NULL);
592 default:
593 break;
594 }
596 lw->selected = lw->start+lw->rows;
597 if( screen_find(screen, c,
598 lw, lyrics_text_rows,
599 cmd, list_callback, NULL) )
600 {
601 /* center the row */
602 lw->start = lw->selected-(lw->rows/2);
603 if( lw->start+lw->rows > lyrics_text_rows )
604 lw->start = lyrics_text_rows-lw->rows;
605 if( lw->start<0 )
606 lw->start=0;
607 return 1;
608 }
610 return 0;
611 }
613 static list_window_t *
614 lyrics_lw(void)
615 {
616 return lw;
617 }
619 screen_functions_t *
620 get_screen_lyrics(void)
621 {
622 static screen_functions_t functions;
624 memset(&functions, 0, sizeof(screen_functions_t));
625 functions.init = lyrics_init;
626 functions.exit = lyrics_exit;
627 functions.open = NULL;
628 functions.close = NULL;
629 functions.resize = lyrics_resize;
630 functions.paint = lyrics_paint;
631 functions.update = lyrics_update;
632 functions.cmd = lyrics_cmd;
633 functions.get_lw = lyrics_lw;
634 functions.get_title = lyrics_title;
636 return &functions;
637 }
638 #endif /* ENABLE_LYRICS_SCREEN */