106d439b7f4a44aa2159ff1b865377c07d82c78e
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
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;
81 XML_Parser parser, contentp;
82 static int lyrics_text_rows = -1;
83 static list_window_t *lw = NULL;
84 short int lock;
85 /* result is a bitset in which the succes when searching 4 lyrics is logged
86 countend by position - backwards
87 0: lyrics in database
88 1: proper access to the lyrics provider
89 2: lyrics found
90 3: exact match
91 4: lyrics downloaded
92 5: lyrics saved
93 wasting 3 bits doesn't mean being a fat memory hog like kde.... does it?
94 */
95 static void lyrics_paint(screen_t *screen, mpdclient_t *c);
97 int get_text_line(formed_text *text, int num, char *dest, int len)
98 {
99 memset(dest, '\0', len*sizeof(char));
100 if(num >= text->lines->len-1) return -1;
101 int linelen;
102 if(num == 0)
103 {
104 linelen = g_array_index(text->lines, int, num);
105 memcpy(dest, text->text->str, linelen*sizeof(char));
106 }
107 else if(num == 1)
108 { //dont ask me why, but this is needed....
109 linelen = g_array_index(text->lines, int, num)
110 - g_array_index(text->lines, int, num-1);
111 memcpy(dest, &text->text->str[g_array_index(text->lines, int, num-1)],
112 linelen*sizeof(char));
113 }
114 else
115 {
116 linelen = g_array_index(text->lines, int, num+1)
117 - g_array_index(text->lines, int, num);
118 memcpy(dest, &text->text->str[g_array_index(text->lines, int, num)],
119 linelen*sizeof(char));
120 }
121 dest[linelen] = '\n';
122 dest[linelen+1] = '\0';
124 return 0;
125 }
127 void add_text_line(formed_text *dest, const char *src, int len)
128 {
129 // need this because g_array_append_val doesnt work with literals
130 // and expat sends "\n" as an extra line everytime
131 if(len == 0)
132 {
133 dest->val = strlen(src);
134 if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int,
135 dest->lines->len-1);
136 g_string_append(dest->text, src);
137 g_array_append_val(dest->lines, dest->val);
138 return;
139 }
140 if(len > 1 || dest->val == 0)
141 {
142 dest->val = len;
143 if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int,
144 dest->lines->len-1);
145 }
146 else if (len == 1 && dest->val != 0) dest->val = 0;
148 if(dest->val > 0)
149 {
150 g_string_append_len(dest->text, src, len);
151 g_array_append_val(dest->lines, dest->val);
152 }
153 }
155 void formed_text_init(formed_text *text)
156 {
157 if(text->text != NULL) g_string_free(text->text, TRUE);
158 text->text = g_string_new("");
160 if(text->lines != NULL) g_array_free(text->lines, TRUE);
161 text->lines = g_array_new(FALSE, TRUE, 4);
163 text->val = 0;
164 }
166 static void check_content(void *data, const char *name, const char **atts)
167 {
168 if(strstr(name, "text") != NULL)
169 {
171 lyrics.result |= 16;
172 }
173 }
176 static void check_search_response(void *data, const char *name,
177 const char **atts)
178 {
179 if(strstr(name, "response") != NULL)
180 {
181 lyrics.result |=2;
182 return;
183 }
185 if(lyrics.result & 4)
186 {
187 if(strstr(name, "lyrics.result") != NULL)
188 {
189 if(strstr(atts[2], "hid") != NULL)
190 {
191 lyrics.hid = atts[3];
192 }
194 if(strstr(atts[2], "exactMatch") != NULL)
195 {
196 lyrics.result |= 8;
197 }
198 }
199 }
201 }
203 static void end_tag(void *data, const char *name)
204 {
205 //hmmmmmm
206 }
208 static void check_search_success(void *userData, const XML_Char *s, int len)
209 {
210 if(lyrics.result & 2) //lets first check whether we're right
211 { //we don't really want to search in the wrong string
212 if(strstr((char*) s, "SUCCESS"))
213 {
214 lyrics.result |=4;
215 }
216 }
217 }
219 static void
220 fetch_text(void *userData, const XML_Char *s, int len)
221 {
222 if(lyrics.result & 16)
223 {
224 add_text_line(&lyrics.content, s, len);
225 }
226 }
228 int
229 check_dl_progress(void *clientp, double dltotal, double dlnow,
230 double ultotal, double ulnow)
231 {
232 if(g_timer_elapsed(lyrics.dltime, NULL) >= options.lyrics_timeout || lock == 4)
233 {
234 formed_text_init(&lyrics.content);
235 return -1;
236 }
238 return 0;
239 }
242 int
243 check_lyr_http(char *artist, char *title, char *url)
244 {
245 char url_avail[256];
247 //this replacess the whitespaces with '+'
248 g_strdelimit(artist, " ", '+');
249 g_strdelimit(title, " ", '+');
251 //we insert the artist and the title into the url
252 snprintf(url_avail, 512, LEOSLYRICS_SEARCH_URL, artist, title);
254 //download that xml!
255 easy_download_struct lyr_avail = {NULL, 0,-1};
257 g_timer_start(lyrics.dltime);
258 if(!easy_download(url_avail, &lyr_avail, check_dl_progress)) return -1;
259 g_timer_stop(lyrics.dltime);
261 //we gotta parse that stuff with expat
262 parser = XML_ParserCreate(NULL);
263 XML_SetUserData(parser, NULL);
265 XML_SetElementHandler(parser, check_search_response, end_tag);
266 XML_SetCharacterDataHandler(parser, check_search_success);
267 XML_Parse(parser, lyr_avail.data, strlen(lyr_avail.data), 0);
268 XML_ParserFree(parser);
270 if(!(lyrics.result & 4)) return -1; //check whether lyrics found
271 snprintf(url, 512, LEOSLYRICS_CONTENT_URL, lyrics.hid);
273 return 0;
274 }
275 int get_lyr_http(char *artist, char *title)
276 {
277 char url_hid[256];
278 if(lyrics.dltime == NULL) lyrics.dltime = g_timer_new();
280 if(check_lyr_http(artist, title, url_hid) != 0) return -1;
282 easy_download_struct lyr_content = {NULL, 0,-1};
283 g_timer_continue(lyrics.dltime);
284 if(!(easy_download(url_hid, &lyr_content, check_dl_progress))) return -1;
285 g_timer_stop(lyrics.dltime);
287 contentp = XML_ParserCreate(NULL);
288 XML_SetUserData(contentp, NULL);
289 XML_SetElementHandler(contentp, check_content, end_tag);
290 XML_SetCharacterDataHandler(contentp, fetch_text);
291 XML_Parse(contentp, lyr_content.data, strlen(lyr_content.data), 0);
292 XML_ParserFree(contentp);
294 return 0;
296 }
297 FILE *create_lyr_file(char *artist, char *title)
298 {
299 char path[1024];
301 snprintf(path, 1024, "%s/.lyrics",
302 getenv("HOME"));
303 if(g_access(path, W_OK) != 0) if(mkdir(path, S_IRWXU) != 0) return NULL;
305 snprintf(path, 1024, "%s/.lyrics/%s",
306 getenv("HOME"), artist);
308 if(g_access(path, W_OK) != 0) if(mkdir(path, S_IRWXU) != 0) return NULL;
310 snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
311 getenv("HOME"), artist, title);
313 return fopen(path, "w");
314 }
316 char *check_lyr_hd(char *artist, char *title, int how)
317 { //checking whether for lyrics file existence and proper access
318 static char path[1024];
319 snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
320 getenv("HOME"), artist, title);
322 if(g_access(path, how) != 0) return NULL;
324 return path;
325 }
328 int get_lyr_hd(char *artist, char *title)
329 {
330 char *path = check_lyr_hd(artist, title, R_OK);
331 if(path == NULL) return -1;
333 FILE *lyr_file;
334 lyr_file = fopen(path, "r");
335 if(lyr_file == NULL) return -1;
337 char *buf = NULL;
338 char **line = &buf;
339 size_t n = 0;
341 while(1)
342 {
343 n = getline(line, &n, lyr_file);
344 if( n < 1 || *line == NULL || feof(lyr_file) != 0 ) return 0;
345 add_text_line(&lyrics.content, *line, n);
346 free(*line);
347 *line = NULL; n = 0;
348 }
350 return 0;
351 }
353 int store_lyr_hd()
354 {
355 char artist[512];
356 char title[512];
357 static char path[1024];
358 FILE *lyr_file;
360 get_text_line(&lyrics.content, 0, artist, 512);
361 get_text_line(&lyrics.content, 1, title, 512);
362 artist[strlen(artist)-1] = '\0';
363 title[strlen(title)-1] = '\0';
365 snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
366 getenv("HOME"), artist, title);
367 lyr_file = create_lyr_file(artist, title);
368 if(lyr_file == NULL) return -1;
369 int i;
370 char line_buf[1024];
372 for(i = 3; i <= lyrics.content.text->len; i++)
373 {
374 if(get_text_line(&lyrics.content, i, line_buf, 1024) == -1);
375 fputs(line_buf, lyr_file);
376 }
377 fclose(lyr_file);
378 return 0;
379 }
382 void check_repaint()
383 {
384 if(screen_get_id("lyrics") == get_cur_mode_id())lyrics_paint(NULL, NULL);
385 }
388 gpointer get_lyr(void *c)
389 {
390 mpd_Status *status = ((retrieval_spec*)c)->client->status;
391 mpd_Song *cur = ((retrieval_spec*)c)->client->song;
392 //mpdclient_update((mpdclient_t*)c);
394 if(!(IS_PAUSED(status->state)||IS_PLAYING(status->state)))
395 {
396 formed_text_init(&lyrics.content);
397 return NULL;
398 }
401 char artist[MAX_SONGNAME_LENGTH];
402 char title[MAX_SONGNAME_LENGTH];
403 lock = 2;
404 lyrics.result = 0;
406 formed_text_init(&lyrics.content);
408 strfsong(artist, MAX_SONGNAME_LENGTH, "%artist%", cur);
409 strfsong(title, MAX_SONGNAME_LENGTH, "%title%", cur);
411 //write header..
412 formed_text_init(&lyrics.content);
413 add_text_line(&lyrics.content, artist, 0);
414 add_text_line(&lyrics.content, title, 0);
415 add_text_line(&lyrics.content, "", 0);
416 add_text_line(&lyrics.content, "", 0);
418 if (((retrieval_spec*)c)->way == 1)
419 {
420 if(get_lyr_http(artist, title) != 0) return NULL;
421 }
422 else{
423 if(get_lyr_hd(artist, title) != 0)
424 {
425 if(get_lyr_http(artist, title) != 0) return NULL;
426 }
427 else lyrics.result |= 1;
428 }
430 lw->start = 0;
431 check_repaint();
432 lock = 1;
433 return &lyrics.content;
434 }
436 static char *
437 list_callback(int index, int *highlight, void *data)
438 {
439 static char buf[512];
441 //i think i'ts fine to write it into the 1st line...
442 if((index == lyrics.content.lines->len && lyrics.content.lines->len > 4)||
443 ((lyrics.content.lines->len == 0
444 ||lyrics.content.lines->len == 4) && index == 0))
445 {
446 *highlight=3;
447 return CREDITS;
448 }
450 if(index < 2 && lyrics.content.lines->len > 4) *highlight=3;
451 else if(index >= lyrics.content.lines->len ||
452 ( index < 4 && index != 0 && lyrics.content.lines->len < 5))
453 {
454 return "";
455 }
457 get_text_line(&lyrics.content, index, buf, 512);
458 return buf;
459 }
462 static void
463 lyrics_init(WINDOW *w, int cols, int rows)
464 {
465 lw = list_window_init(w, cols, rows);
466 lw->flags = LW_HIDE_CURSOR;
467 //lyrics.content.lines = g_array_new(FALSE, TRUE, 4);
468 formed_text_init(&lyrics.content);
469 if (!g_thread_supported()) g_thread_init(NULL);
471 }
473 static void
474 lyrics_resize(int cols, int rows)
475 {
476 lw->cols = cols;
477 lw->rows = rows;
478 }
480 static void
481 lyrics_exit(void)
482 {
483 list_window_free(lw);
484 }
487 static char *
488 lyrics_title(char *str, size_t size)
489 {
490 if(lyrics.content.lines->len == 4)
491 {
492 if(lock == 1)
493 {
494 if(!(lyrics.result & 1))
495 {
496 if(!(lyrics.result & 2)) return _("Lyrics [No connection]");
497 if(!(lyrics.result & 4)) return _("Lyrics [Not found]");
498 }
499 }
500 if(lock == 2) return _("Lyrics [retrieving]");
501 }
502 /*if(lyrics.content.lines->len > 2)
503 {
504 static char buf[512];
505 char artist[512];
506 char title[512];
507 get_text_line(&lyrics.content, 0, artist, 512);
508 get_text_line(&lyrics.content, 1, artist, 512);
509 snprintf(buf, 512, "Lyrics %s - %s", artist, title);
510 return buf;
511 }*/
512 return "Lyrics";
513 }
515 static void
516 lyrics_paint(screen_t *screen, mpdclient_t *c)
517 {
518 lw->clear = 1;
519 list_window_paint(lw, list_callback, NULL);
520 wrefresh(lw->w);
521 }
523 static void
524 lyrics_update(screen_t *screen, mpdclient_t *c)
525 {
526 if( lw->repaint )
527 {
528 list_window_paint(lw, list_callback, NULL);
529 wrefresh(lw->w);
530 lw->repaint = 0;
531 }
532 }
535 static int
536 lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
537 {
538 lw->repaint=1;
539 static retrieval_spec spec;
540 switch(cmd)
541 {
542 case CMD_LIST_NEXT:
543 if( lw->start+lw->rows < lyrics.content.lines->len+1 )
544 lw->start++;
545 return 1;
546 case CMD_LIST_PREVIOUS:
547 if( lw->start >0 )
548 lw->start--;
549 return 1;
550 case CMD_LIST_FIRST:
551 lw->start = 0;
552 return 1;
553 case CMD_LIST_LAST:
554 lw->start = lyrics_text_rows-lw->rows;
555 if( lw->start<0 )
556 lw->start = 0;
557 return 1;
558 case CMD_LIST_NEXT_PAGE:
559 lw->start = lw->start + lw->rows-1;
560 if( lw->start+lw->rows >= lyrics.content.lines->len+1 )
561 lw->start = lyrics.content.lines->len-lw->rows+1;
562 if( lw->start<0 )
563 lw->start = 0;
564 return 1;
565 case CMD_LIST_PREVIOUS_PAGE:
566 lw->start = lw->start - lw->rows;
567 if( lw->start<0 )
568 lw->start = 0;
569 return 1;
570 case CMD_SELECT:
571 spec.client = c;
572 spec.way = 0;
573 g_thread_create(get_lyr, &spec, FALSE, NULL);
574 return 1;
575 case CMD_INTERRUPT:
576 if(lock > 1) lock = 4;
577 return 1;
578 case CMD_ADD:
579 if(lock > 0 && lock != 4)
580 {
581 if(store_lyr_hd() == 0) screen_status_message (_("Lyrics saved!"));
582 }
583 return 1;
584 case CMD_LYRICS_UPDATE:
585 spec.client = c;
586 spec.way = 1;
587 g_thread_create(get_lyr, &spec, FALSE, NULL);
589 default:
590 break;
591 }
593 lw->selected = lw->start+lw->rows;
594 if( screen_find(screen, c,
595 lw, lyrics_text_rows,
596 cmd, list_callback, NULL) )
597 {
598 /* center the row */
599 lw->start = lw->selected-(lw->rows/2);
600 if( lw->start+lw->rows > lyrics_text_rows )
601 lw->start = lyrics_text_rows-lw->rows;
602 if( lw->start<0 )
603 lw->start=0;
604 return 1;
605 }
607 return 0;
608 }
610 static list_window_t *
611 lyrics_lw(void)
612 {
613 return lw;
614 }
616 screen_functions_t *
617 get_screen_lyrics(void)
618 {
619 static screen_functions_t functions;
621 memset(&functions, 0, sizeof(screen_functions_t));
622 functions.init = lyrics_init;
623 functions.exit = lyrics_exit;
624 functions.open = NULL;
625 functions.close = NULL;
626 functions.resize = lyrics_resize;
627 functions.paint = lyrics_paint;
628 functions.update = lyrics_update;
629 functions.cmd = lyrics_cmd;
630 functions.get_lw = lyrics_lw;
631 functions.get_title = lyrics_title;
633 return &functions;
634 }
635 #endif /* ENABLE_LYRICS_SCREEN */