d941967ea595f9244bee5e2e0e0050998d131531
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 //write header..
314 formed_text_init(&lyr_text);
315 add_text_line(&lyr_text, artist, 0);
316 add_text_line(&lyr_text, title, 0);
317 add_text_line(&lyr_text, "", 0);
318 add_text_line(&lyr_text, "", 0);
320 //if(get_lyr_hd(artist, title) == 0) return &lyr_text;
322 //this replacess the whitespaces with '+'
323 g_strdelimit(artist, " ", '+');
324 g_strdelimit(title, " ", '+');
326 //we insert the artist and the title into the url
327 snprintf(url_avail, 512, LEOSLYRICS_SEARCH_URL, artist, title);
329 //download that xml!
330 easy_download_struct lyr_avail = {NULL, 0,-1};
332 g_timer_start(dltime);
333 if(!easy_download(url_avail, &lyr_avail, check_dl_progress)) return NULL;
334 g_timer_stop(dltime);
336 //we gotta parse that stuff with expat
337 parser = XML_ParserCreate(NULL);
338 XML_SetUserData(parser, NULL);
340 int state = 0;
342 XML_SetElementHandler(parser, check_search_response, end_tag);
343 XML_SetCharacterDataHandler(parser, check_search_success);
344 XML_Parse(parser, lyr_avail.data, strlen(lyr_avail.data), state);
345 XML_ParserFree(parser);
347 if(!(result & 4)) return NULL; //check whether lyrics found
348 easy_download_struct lyr_content = {NULL, 0,-1};
349 snprintf(url_hid, 512, LEOSLYRICS_CONTENT_URL, hid);
351 g_timer_continue(dltime);
352 if(!(easy_download(url_hid, &lyr_content, check_dl_progress))) return NULL;
353 g_timer_stop(dltime);
355 contentp = XML_ParserCreate(NULL);
356 XML_SetUserData(contentp, NULL);
357 XML_SetElementHandler(contentp, check_content, end_tag);
358 XML_SetCharacterDataHandler(contentp, fetch_text);
359 XML_Parse(contentp, lyr_content.data, strlen(lyr_content.data), state);
360 XML_ParserFree(contentp);
362 lw->start = 0;
363 check_repaint();
365 lock = 1;
366 return &lyr_text;
367 }
369 static char *
370 list_callback(int index, int *highlight, void *data)
371 {
372 static char buf[512];
373 int linelen;
375 //i think i'ts fine to write it into the 1st line...
376 if((index == lyr_text.lines->len && lyr_text.lines->len != 3)||
377 ((lyr_text.lines->len == 0
378 ||lyr_text.lines->len == 4) && index == 0))
379 {
380 *highlight=3;
381 return CREDITS;
382 }
384 if(index < 2 && lyr_text.lines->len > 4) *highlight=3;
385 else if(index >= lyr_text.lines->len)
386 {
387 return "";
388 }
389 //if(index >1) index--;
390 get_text_line(&lyr_text, index, buf, 512);
391 return buf;
392 }
395 static void
396 lyrics_init(WINDOW *w, int cols, int rows)
397 {
398 lw = list_window_init(w, cols, rows);
399 lw->flags = LW_HIDE_CURSOR;
400 //lyr_text.lines = g_array_new(FALSE, TRUE, 4);
401 formed_text_init(&lyr_text);
402 if (!g_thread_supported()) g_thread_init(NULL);
404 }
406 static void
407 lyrics_resize(int cols, int rows)
408 {
409 lw->cols = cols;
410 lw->rows = rows;
411 }
413 static void
414 lyrics_exit(void)
415 {
416 list_window_free(lw);
417 }
420 static char *
421 lyrics_title(char *str, size_t size)
422 {
423 if(lyr_text.lines->len == 4){
424 if(lock == 1 && !(result & 2)) return _("Lyrics [No connection]");
425 if(lock == 1 && !(result & 4)) return _("Lyrics [Not found]");
426 if(lock == 2) return _("Lyrics [retrieving]");
427 }
428 /*if(lyr_text.lines->len > 2)
429 {
430 static char buf[512];
431 char artist[512];
432 char title[512];
433 get_text_line(&lyr_text, 0, artist, 512);
434 get_text_line(&lyr_text, 1, artist, 512);
435 snprintf(buf, 512, "Lyrics %s - %s", artist, title);
436 return buf;
437 }*/
438 return "Lyrics";
439 }
441 static void
442 lyrics_paint(screen_t *screen, mpdclient_t *c)
443 {
444 lw->clear = 1;
445 list_window_paint(lw, list_callback, NULL);
446 wrefresh(lw->w);
447 }
449 static void
450 lyrics_update(screen_t *screen, mpdclient_t *c)
451 {
452 if( lw->repaint )
453 {
454 list_window_paint(lw, list_callback, NULL);
455 wrefresh(lw->w);
456 lw->repaint = 0;
457 }
458 }
461 static int
462 lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
463 {
464 lw->repaint=1;
465 switch(cmd)
466 {
467 case CMD_LIST_NEXT:
468 if( lw->start+lw->rows < lyr_text.lines->len+1 )
469 lw->start++;
470 return 1;
471 case CMD_LIST_PREVIOUS:
472 if( lw->start >0 )
473 lw->start--;
474 return 1;
475 case CMD_LIST_FIRST:
476 lw->start = 0;
477 return 1;
478 case CMD_LIST_LAST:
479 lw->start = lyrics_text_rows-lw->rows;
480 if( lw->start<0 )
481 lw->start = 0;
482 return 1;
483 case CMD_LIST_NEXT_PAGE:
484 lw->start = lw->start + lw->rows-1;
485 if( lw->start+lw->rows >= lyr_text.lines->len+1 )
486 lw->start = lyr_text.lines->len-lw->rows+1;
487 if( lw->start<0 )
488 lw->start = 0;
489 return 1;
490 case CMD_LIST_PREVIOUS_PAGE:
491 lw->start = lw->start - lw->rows;
492 if( lw->start<0 )
493 lw->start = 0;
494 return 1;
495 case CMD_SELECT:
496 g_thread_create(get_lyr, c, FALSE, NULL);
497 return 1;
498 case CMD_INTERRUPT:
499 if(lock > 1) lock = 4;
500 return 1;
501 default:
502 break;
503 }
505 lw->selected = lw->start+lw->rows;
506 if( screen_find(screen, c,
507 lw, lyrics_text_rows,
508 cmd, list_callback, NULL) )
509 {
510 /* center the row */
511 lw->start = lw->selected-(lw->rows/2);
512 if( lw->start+lw->rows > lyrics_text_rows )
513 lw->start = lyrics_text_rows-lw->rows;
514 if( lw->start<0 )
515 lw->start=0;
516 return 1;
517 }
519 return 0;
520 }
522 static list_window_t *
523 lyrics_lw(void)
524 {
525 return lw;
526 }
528 screen_functions_t *
529 get_screen_lyrics(void)
530 {
531 static screen_functions_t functions;
533 memset(&functions, 0, sizeof(screen_functions_t));
534 functions.init = lyrics_init;
535 functions.exit = lyrics_exit;
536 functions.open = NULL;
537 functions.close = NULL;
538 functions.resize = lyrics_resize;
539 functions.paint = lyrics_paint;
540 functions.update = lyrics_update;
541 functions.cmd = lyrics_cmd;
542 functions.get_lw = lyrics_lw;
543 functions.get_title = lyrics_title;
545 return &functions;
546 }
547 #endif /* ENABLE_LYRICS_SCREEN */