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 <sys/stat.h>
34 #include "ncmpc.h"
35 #include "options.h"
36 #include "mpdclient.h"
37 #include "command.h"
38 #include "screen.h"
39 #include "screen_utils.h"
40 #include "easy_download.h"
41 #include "lyrics_screen.h"
42 #include "strfsong.h"
45 #define LEOSLYRICS_SEARCH_URL "http://api.leoslyrics.com/api_search.php?auth=ncmpc&artist=%s&songtitle=%s"
47 #define LEOSLYRICS_CONTENT_URL "http://api.leoslyrics.com/api_lyrics.php?auth=ncmpc&hid=%s"
49 #define CREDITS "Lyrics provided by www.LeosLyrics.com"
50 typedef struct _formed_text
51 {
52 GString *text;
53 GArray *lines;
54 int val;
55 } formed_text;
57 typedef struct _retrieval_spec
58 {
59 mpdclient_t *client;
60 int way;
61 } retrieval_spec;
65 XML_Parser parser, contentp;
66 static int lyrics_text_rows = -1;
67 static list_window_t *lw = NULL;
68 guint8 result;
69 char *hid;
70 GTimer *dltime;
71 short int lock;
72 formed_text lyr_text;
73 /* result is a bitset in which the succes when searching 4 lyrics is logged
74 countend by position - backwards
75 0: lyrics in database
76 1: proper access to the lyrics provider
77 2: lyrics found
78 3: exact match
79 4: lyrics downloaded
80 5: lyrics saved
81 wasting 3 bits doesn't mean being a fat memory hog like kde.... does it?
82 */
83 static void lyrics_paint(screen_t *screen, mpdclient_t *c);
85 int get_text_line(formed_text *text, int num, char *dest, int len)
86 {
87 memset(dest, '\0', len*sizeof(char));
88 if(num >= text->lines->len-1) return -1;
89 int linelen;
90 if(num == 0)
91 {
92 linelen = g_array_index(text->lines, int, num);
93 memcpy(dest, text->text->str, linelen*sizeof(char));
94 }
95 else if(num == 1)
96 { //dont ask me why, but this is needed....
97 linelen = g_array_index(text->lines, int, num)
98 - g_array_index(text->lines, int, num-1);
99 memcpy(dest, &text->text->str[g_array_index(text->lines, int, num-1)],
100 linelen*sizeof(char));
101 }
102 else
103 {
104 linelen = g_array_index(text->lines, int, num+1)
105 - g_array_index(text->lines, int, num);
106 memcpy(dest, &text->text->str[g_array_index(text->lines, int, num)],
107 linelen*sizeof(char));
108 }
109 dest[linelen] = '\n';
110 dest[linelen+1] = '\0';
112 return 0;
113 }
115 void add_text_line(formed_text *dest, const char *src, int len)
116 {
117 // need this because g_array_append_val doesnt work with literals
118 // and expat sends "\n" as an extra line everytime
119 if(len == 0)
120 {
121 dest->val = strlen(src);
122 if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int,
123 dest->lines->len-1);
124 g_string_append(dest->text, src);
125 g_array_append_val(dest->lines, dest->val);
126 return;
127 }
128 if(len > 1 || dest->val == 0)
129 {
130 dest->val = len;
131 if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int,
132 dest->lines->len-1);
133 }
134 else if (len == 1 && dest->val != 0) dest->val = 0;
136 if(dest->val > 0)
137 {
138 g_string_append_len(dest->text, src, len);
139 g_array_append_val(dest->lines, dest->val);
140 }
141 }
143 void formed_text_init(formed_text *text)
144 {
145 if(text->text != NULL) g_string_free(text->text, TRUE);
146 text->text = g_string_new("");
148 if(text->lines != NULL) g_array_free(text->lines, TRUE);
149 text->lines = g_array_new(FALSE, TRUE, 4);
151 text->val = 0;
152 }
154 static void check_content(void *data, const char *name, const char **atts)
155 {
156 if(strstr(name, "text") != NULL)
157 {
159 result |= 16;
160 }
161 }
164 static void check_search_response(void *data, const char *name,
165 const char **atts)
166 {
167 if(strstr(name, "response") != NULL)
168 {
169 result |=2;
170 return;
171 }
173 if(result & 4)
174 {
175 if(strstr(name, "result") != NULL)
176 {
177 if(strstr(atts[2], "hid") != NULL)
178 {
179 hid = atts[3];
180 }
182 if(strstr(atts[2], "exactMatch") != NULL)
183 {
184 result |= 8;
185 }
186 }
187 }
189 }
191 static void end_tag(void *data, const char *name)
192 {
193 //hmmmmmm
194 }
196 static void check_search_success(void *userData, const XML_Char *s, int len)
197 {
198 if(result & 2) //lets first check whether we're right
199 { //we don't really want to search in the wrong string
200 if(strstr((char*) s, "SUCCESS"))
201 {
202 result |=4;
203 }
204 }
205 }
207 static void fetch_text(void *userData, const XML_Char *s, int len)
208 {
209 if(result & 16)
210 {
211 add_text_line(&lyr_text, s, len);
212 }
213 }
215 int check_dl_progress(void *clientp, double dltotal, double dlnow,
216 double ultotal, double ulnow)
217 {
218 if(g_timer_elapsed(dltime, NULL) >= options.lyrics_timeout || lock == 4)
219 {
220 while (1)fprintf(stderr,"hihffo!");
221 formed_text_init(&lyr_text);
222 return -1;
223 }
225 return 0;
226 }
229 int check_lyr_http(char *artist, char *title, char *url)
230 {
231 char url_avail[256];
233 //this replacess the whitespaces with '+'
234 g_strdelimit(artist, " ", '+');
235 g_strdelimit(title, " ", '+');
237 //we insert the artist and the title into the url
238 snprintf(url_avail, 512, LEOSLYRICS_SEARCH_URL, artist, title);
240 //download that xml!
241 easy_download_struct lyr_avail = {NULL, 0,-1};
243 g_timer_start(dltime);
244 if(!easy_download(url_avail, &lyr_avail, check_dl_progress)) return -1;
245 g_timer_stop(dltime);
247 //we gotta parse that stuff with expat
248 parser = XML_ParserCreate(NULL);
249 XML_SetUserData(parser, NULL);
251 XML_SetElementHandler(parser, check_search_response, end_tag);
252 XML_SetCharacterDataHandler(parser, check_search_success);
253 XML_Parse(parser, lyr_avail.data, strlen(lyr_avail.data), 0);
254 XML_ParserFree(parser);
256 if(!(result & 4)) return -1; //check whether lyrics found
257 snprintf(url, 512, LEOSLYRICS_CONTENT_URL, hid);
259 return 0;
260 }
261 int get_lyr_http(char *artist, char *title)
262 {
263 char url_hid[256];
264 if(dltime == NULL) dltime = g_timer_new();
266 if(check_lyr_http(artist, title, url_hid) != 0) return -1;
268 easy_download_struct lyr_content = {NULL, 0,-1};
269 g_timer_continue(dltime);
270 if(!(easy_download(url_hid, &lyr_content, check_dl_progress))) return -1;
271 g_timer_stop(dltime);
273 contentp = XML_ParserCreate(NULL);
274 XML_SetUserData(contentp, NULL);
275 XML_SetElementHandler(contentp, check_content, end_tag);
276 XML_SetCharacterDataHandler(contentp, fetch_text);
277 XML_Parse(contentp, lyr_content.data, strlen(lyr_content.data), 0);
278 XML_ParserFree(contentp);
280 return 0;
282 }
283 FILE *create_lyr_file(char *artist, char *title)
284 {
285 char path[1024];
287 snprintf(path, 1024, "%s/.lyrics",
288 getenv("HOME"));
289 if(g_access(path, W_OK) != 0) if(mkdir(path, S_IRWXU) != 0) return NULL;
291 snprintf(path, 1024, "%s/.lyrics/%s",
292 getenv("HOME"), artist);
293 if(g_access(path, W_OK) != 0) if(mkdir(path, S_IRWXU) != 0) return NULL;
295 snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
296 getenv("HOME"), artist, title);
298 FILE *file = fopen(path, "w");
299 }
301 char *check_lyr_hd(char *artist, char *title, int how)
302 { //checking whether for lyrics file existence and proper access
303 static char path[1024];
304 snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
305 getenv("HOME"), artist, title);
307 if(g_access(path, how) != 0) return NULL;
309 return path;
310 }
313 int get_lyr_hd(char *artist, char *title)
314 {
315 char *path = check_lyr_hd(artist, title, R_OK);
316 if(path == NULL) return -1;
318 FILE *lyr_file;
319 lyr_file = fopen(path, "r");
320 if(lyr_file == NULL) return -1;
322 char *buf = NULL;
323 char **line = &buf;
324 size_t n = 0;
326 while(1)
327 {
328 n = getline(line, &n, lyr_file);
329 if( n < 1 || *line == NULL || feof(lyr_file) != 0 ) return 0;
330 add_text_line(&lyr_text, *line, n+1);
331 free(*line);
332 *line = NULL; n = 0;
333 }
335 return 0;
336 }
338 int store_lyr_hd()
339 {
340 char artist[512];
341 char title[512];
342 static char path[1024];
343 FILE *lyr_file;
345 get_text_line(&lyr_text, 0, artist, 512);
346 get_text_line(&lyr_text, 1, title, 512);
347 artist[strlen(artist)-1] = '\0';
348 title[strlen(title)-1] = '\0';
350 snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
351 getenv("HOME"), artist, title);
352 lyr_file = create_lyr_file(artist, title);
353 if(lyr_file == NULL) return -1;
354 int i;
355 char line_buf[1024];
357 for(i = 4; i <= lyr_text.text->len; i++)
358 {
359 if(get_text_line(&lyr_text, i, line_buf, 1024) == -1);
360 fputs(line_buf, lyr_file);
361 }
362 fclose(lyr_file);
363 return 0;
364 }
367 void check_repaint()
368 {
369 if(screen_get_id("lyrics") == get_cur_mode_id())lyrics_paint(NULL, NULL);
370 }
373 gpointer get_lyr(void *c)
374 {
375 mpd_Status *status = ((retrieval_spec*)c)->client->status;
376 mpd_Song *cur = ((retrieval_spec*)c)->client->song;
377 //mpdclient_update((mpdclient_t*)c);
379 if(!(IS_PAUSED(status->state)||IS_PLAYING(status->state)))
380 {
381 formed_text_init(&lyr_text);
382 return NULL;
383 }
386 char artist[MAX_SONGNAME_LENGTH];
387 char title[MAX_SONGNAME_LENGTH];
388 lock = 2;
389 result = 0;
391 formed_text_init(&lyr_text);
393 strfsong(artist, MAX_SONGNAME_LENGTH, "%artist%", cur);
394 strfsong(title, MAX_SONGNAME_LENGTH, "%title%", cur);
396 //write header..
397 formed_text_init(&lyr_text);
398 add_text_line(&lyr_text, artist, 0);
399 add_text_line(&lyr_text, title, 0);
400 add_text_line(&lyr_text, "", 0);
401 add_text_line(&lyr_text, "", 0);
403 if (((retrieval_spec*)c)->way == 1)
404 {
405 if(get_lyr_http(artist, title) != 0) return NULL;
406 }
407 else{
408 if(get_lyr_hd(artist, title) != 0)
409 {
410 if(get_lyr_http(artist, title) != 0) return NULL;
411 }
412 else result |= 1;
413 }
415 lw->start = 0;
416 check_repaint();
417 lock = 1;
418 return &lyr_text;
419 }
421 static char *
422 list_callback(int index, int *highlight, void *data)
423 {
424 static char buf[512];
426 //i think i'ts fine to write it into the 1st line...
427 if((index == lyr_text.lines->len && lyr_text.lines->len > 4)||
428 ((lyr_text.lines->len == 0
429 ||lyr_text.lines->len == 4) && index == 0))
430 {
431 *highlight=3;
432 return CREDITS;
433 }
435 if(index < 2 && lyr_text.lines->len > 4) *highlight=3;
436 else if(index >= lyr_text.lines->len ||
437 ( index < 4 && index != 0 && lyr_text.lines->len < 5))
438 {
439 return "";
440 }
442 get_text_line(&lyr_text, index, buf, 512);
443 return buf;
444 }
447 static void
448 lyrics_init(WINDOW *w, int cols, int rows)
449 {
450 lw = list_window_init(w, cols, rows);
451 lw->flags = LW_HIDE_CURSOR;
452 //lyr_text.lines = g_array_new(FALSE, TRUE, 4);
453 formed_text_init(&lyr_text);
454 if (!g_thread_supported()) g_thread_init(NULL);
456 }
458 static void
459 lyrics_resize(int cols, int rows)
460 {
461 lw->cols = cols;
462 lw->rows = rows;
463 }
465 static void
466 lyrics_exit(void)
467 {
468 list_window_free(lw);
469 }
472 static char *
473 lyrics_title(char *str, size_t size)
474 {
475 if(lyr_text.lines->len == 4)
476 {
477 if(lock == 1)
478 {
479 if(!(result & 1))
480 {
481 if(!(result & 2)) return _("Lyrics [No connection]");
482 if(!(result & 4)) return _("Lyrics [Not found]");
483 }
484 }
485 if(lock == 2) return _("Lyrics [retrieving]");
486 }
487 /*if(lyr_text.lines->len > 2)
488 {
489 static char buf[512];
490 char artist[512];
491 char title[512];
492 get_text_line(&lyr_text, 0, artist, 512);
493 get_text_line(&lyr_text, 1, artist, 512);
494 snprintf(buf, 512, "Lyrics %s - %s", artist, title);
495 return buf;
496 }*/
497 return "Lyrics";
498 }
500 static void
501 lyrics_paint(screen_t *screen, mpdclient_t *c)
502 {
503 lw->clear = 1;
504 list_window_paint(lw, list_callback, NULL);
505 wrefresh(lw->w);
506 }
508 static void
509 lyrics_update(screen_t *screen, mpdclient_t *c)
510 {
511 if( lw->repaint )
512 {
513 list_window_paint(lw, list_callback, NULL);
514 wrefresh(lw->w);
515 lw->repaint = 0;
516 }
517 }
520 static int
521 lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
522 {
523 lw->repaint=1;
524 static retrieval_spec spec;
525 char *msg;
526 switch(cmd)
527 {
528 case CMD_LIST_NEXT:
529 if( lw->start+lw->rows < lyr_text.lines->len+1 )
530 lw->start++;
531 return 1;
532 case CMD_LIST_PREVIOUS:
533 if( lw->start >0 )
534 lw->start--;
535 return 1;
536 case CMD_LIST_FIRST:
537 lw->start = 0;
538 return 1;
539 case CMD_LIST_LAST:
540 lw->start = lyrics_text_rows-lw->rows;
541 if( lw->start<0 )
542 lw->start = 0;
543 return 1;
544 case CMD_LIST_NEXT_PAGE:
545 lw->start = lw->start + lw->rows-1;
546 if( lw->start+lw->rows >= lyr_text.lines->len+1 )
547 lw->start = lyr_text.lines->len-lw->rows+1;
548 if( lw->start<0 )
549 lw->start = 0;
550 return 1;
551 case CMD_LIST_PREVIOUS_PAGE:
552 lw->start = lw->start - lw->rows;
553 if( lw->start<0 )
554 lw->start = 0;
555 return 1;
556 case CMD_SELECT:
557 spec.client = c;
558 spec.way = 0;
559 g_thread_create(get_lyr, &spec, FALSE, NULL);
560 return 1;
561 case CMD_INTERRUPT:
562 if(lock > 1) lock = 4;
563 return 1;
564 case CMD_ADD:
565 if(lock > 0 && lock != 4)
566 {
567 if(store_lyr_hd() == 0) screen_status_message (_("Lyrics saved!"));
568 }
569 return 1;
570 case CMD_LYRICS_UPDATE:
571 spec.client = c;
572 spec.way = 1;
573 g_thread_create(get_lyr, &spec, FALSE, NULL);
574 default:
575 break;
576 }
578 lw->selected = lw->start+lw->rows;
579 if( screen_find(screen, c,
580 lw, lyrics_text_rows,
581 cmd, list_callback, NULL) )
582 {
583 /* center the row */
584 lw->start = lw->selected-(lw->rows/2);
585 if( lw->start+lw->rows > lyrics_text_rows )
586 lw->start = lyrics_text_rows-lw->rows;
587 if( lw->start<0 )
588 lw->start=0;
589 return 1;
590 }
592 return 0;
593 }
595 static list_window_t *
596 lyrics_lw(void)
597 {
598 return lw;
599 }
601 screen_functions_t *
602 get_screen_lyrics(void)
603 {
604 static screen_functions_t functions;
606 memset(&functions, 0, sizeof(screen_functions_t));
607 functions.init = lyrics_init;
608 functions.exit = lyrics_exit;
609 functions.open = NULL;
610 functions.close = NULL;
611 functions.resize = lyrics_resize;
612 functions.paint = lyrics_paint;
613 functions.update = lyrics_update;
614 functions.cmd = lyrics_cmd;
615 functions.get_lw = lyrics_lw;
616 functions.get_title = lyrics_title;
618 return &functions;
619 }
620 #endif /* ENABLE_LYRICS_SCREEN */