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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
22 #define _GNU_SOURCE
23 #include <stdlib.h>
24 #include <string.h>
25 #include <glib.h>
26 #include <ncurses.h>
27 #include <expat.h>
28 #include <unistd.h>
29 #include <glib/gstdio.h>
30 #include <stdio.h>
32 #include "config.h"
33 #ifndef DISABLE_LYRICS_SCREEN
34 #include <sys/stat.h>
35 #include "ncmpc.h"
36 #include "options.h"
37 #include "mpdclient.h"
38 #include "command.h"
39 #include "screen.h"
40 #include "screen_utils.h"
41 #include "easy_download.h"
42 #include "strfsong.h"
43 #include "src_lyrics.h"
45 int src_selection;
47 static void lyrics_paint(screen_t *screen, mpdclient_t *c);
49 FILE *create_lyr_file(char *artist, char *title)
50 {
51 char path[1024];
53 snprintf(path, 1024, "%s/.lyrics",
54 getenv("HOME"));
55 if(g_access(path, W_OK) != 0) if(mkdir(path, S_IRWXU) != 0) return NULL;
57 snprintf(path, 1024, "%s/.lyrics/%s",
58 getenv("HOME"), artist);
59 if(g_access(path, W_OK) != 0) if(mkdir(path, S_IRWXU) != 0) return NULL;
61 snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
62 getenv("HOME"), artist, title);
64 return fopen(path, "w");
65 }
68 int store_lyr_hd()
69 {
70 char artist[512];
71 char title[512];
72 static char path[1024];
73 FILE *lyr_file;
75 get_text_line(&lyr_text, 0, artist, 512);
76 get_text_line(&lyr_text, 1, title, 512);
77 artist[strlen(artist)-1] = '\0';
78 title[strlen(title)-1] = '\0';
80 snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
81 getenv("HOME"), artist, title);
82 lyr_file = create_lyr_file(artist, title);
83 if(lyr_file == NULL) return -1;
84 int i;
85 char line_buf[1024];
87 for(i = 3; i <= lyr_text.text->len; i++)
88 {
89 if(get_text_line(&lyr_text, i, line_buf, 1024) == -1);
90 fputs(line_buf, lyr_file);
91 }
92 fclose(lyr_file);
93 return 0;
94 }
97 void check_repaint()
98 {
99 if(screen_get_id("lyrics") == get_cur_mode_id())lyrics_paint(NULL, NULL);
100 }
103 gpointer get_lyr(void *c)
104 {
105 mpd_Status *status = ((retrieval_spec*)c)->client->status;
106 mpd_Song *cur = ((retrieval_spec*)c)->client->song;
107 //mpdclient_update((mpdclient_t*)c);
109 if(!(IS_PAUSED(status->state)||IS_PLAYING(status->state)))
110 {
111 formed_text_init(&lyr_text);
112 return NULL;
113 }
116 char artist[MAX_SONGNAME_LENGTH];
117 char title[MAX_SONGNAME_LENGTH];
118 lock = 2;
119 result = 0;
121 formed_text_init(&lyr_text);
123 strfsong(artist, MAX_SONGNAME_LENGTH, "%artist%", cur);
124 strfsong(title, MAX_SONGNAME_LENGTH, "%title%", cur);
126 //write header..
127 formed_text_init(&lyr_text);
128 add_text_line(&lyr_text, artist, 0);
129 add_text_line(&lyr_text, title, 0);
130 add_text_line(&lyr_text, "", 0);
131 add_text_line(&lyr_text, "", 0);
133 if (((retrieval_spec*)c)->way != -1) /*till it'S of use*/
134 {
135 if(get_lyr_by_src (src_selection, artist, title) != 0) return NULL;
136 }
137 /*else{
138 if(get_lyr_hd(artist, title) != 0)
139 {
140 if(get_lyr_hd(artist, title) != 0) return NULL;
141 }
142 else result |= 1;
143 }*/
145 lw->start = 0;
146 check_repaint();
147 lock = 1;
148 return &lyr_text;
149 }
151 static char *
152 list_callback(int index, int *highlight, void *data)
153 {
154 static char buf[512];
156 //i think i'ts fine to write it into the 1st line...
157 if((index == lyr_text.lines->len && lyr_text.lines->len > 4)||
158 ((lyr_text.lines->len == 0
159 ||lyr_text.lines->len == 4) && index == 0))
160 {
161 *highlight=3;
162 src_lyr* selected = g_array_index (src_lyr_stack, src_lyr*, src_selection);
163 if (selected != NULL) return selected->description;
164 return "";
165 }
167 if(index < 2 && lyr_text.lines->len > 4) *highlight=3;
168 else if(index >= lyr_text.lines->len ||
169 ( index < 4 && index != 0 && lyr_text.lines->len < 5))
170 {
171 return "";
172 }
174 get_text_line(&lyr_text, index, buf, 512);
175 return buf;
176 }
179 static void
180 lyrics_init(WINDOW *w, int cols, int rows)
181 {
182 lw = list_window_init(w, cols, rows);
183 lw->flags = LW_HIDE_CURSOR;
184 //lyr_text.lines = g_array_new(FALSE, TRUE, 4);
185 formed_text_init(&lyr_text);
186 if (!g_thread_supported()) g_thread_init(NULL);
188 }
190 static void
191 lyrics_resize(int cols, int rows)
192 {
193 lw->cols = cols;
194 lw->rows = rows;
195 }
197 static void
198 lyrics_exit(void)
199 {
200 list_window_free(lw);
201 }
204 static char *
205 lyrics_title(char *str, size_t size)
206 {
207 static GString *msg;
208 if (msg == NULL)
209 msg = g_string_new ("");
210 else g_string_erase (msg, 0, -1);
212 g_string_append (msg, "Lyrics [");
214 if (src_selection > src_lyr_stack->len-1)
215 g_string_append (msg, "No plugin available");
216 else
217 {
218 src_lyr* selected = g_array_index (src_lyr_stack, src_lyr*, src_selection);
219 if (selected != NULL)
220 g_string_append (msg, selected->name);
221 else g_string_append (msg, "NONE");
222 }
223 if(lyr_text.lines->len == 4)
224 {
225 if(lock == 1)
226 {
227 if(!(result & 1))
228 {
229 g_string_append (msg, " - ");
230 if(!(result & 2)) g_string_append (msg, _("No access"));
231 else if(!(result & 4)||!(result & 16)) g_string_append (msg, _("Not found"));
232 }
233 }
234 if(lock == 2)
235 {
236 g_string_append (msg, " - ");
237 g_string_append (msg, _("retrieving"));
238 }
239 }
241 g_string_append_c (msg, ']');
243 return msg->str;
244 }
246 static void
247 lyrics_paint(screen_t *screen, mpdclient_t *c)
248 {
249 lw->clear = 1;
250 list_window_paint(lw, list_callback, NULL);
251 wrefresh(lw->w);
252 }
255 static void
256 lyrics_update(screen_t *screen, mpdclient_t *c)
257 {
258 if( lw->repaint )
259 {
260 list_window_paint(lw, list_callback, NULL);
261 wrefresh(lw->w);
262 lw->repaint = 0;
263 }
264 }
267 static int
268 lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
269 {
270 lw->repaint=1;
271 static retrieval_spec spec;
272 switch(cmd)
273 {
274 case CMD_LIST_NEXT:
275 if( lw->start+lw->rows < lyr_text.lines->len+1 )
276 lw->start++;
277 return 1;
278 case CMD_LIST_PREVIOUS:
279 if( lw->start >0 )
280 lw->start--;
281 return 1;
282 case CMD_LIST_FIRST:
283 lw->start = 0;
284 return 1;
285 case CMD_LIST_LAST:
286 lw->start = lyrics_text_rows-lw->rows;
287 if( lw->start<0 )
288 lw->start = 0;
289 return 1;
290 case CMD_LIST_NEXT_PAGE:
291 lw->start = lw->start + lw->rows-1;
292 if( lw->start+lw->rows >= lyr_text.lines->len+1 )
293 lw->start = lyr_text.lines->len-lw->rows+1;
294 if( lw->start<0 )
295 lw->start = 0;
296 return 1;
297 case CMD_LIST_PREVIOUS_PAGE:
298 lw->start = lw->start - lw->rows;
299 if( lw->start<0 )
300 lw->start = 0;
301 return 1;
302 case CMD_SELECT:
303 spec.client = c;
304 spec.way = 0;
305 g_thread_create(get_lyr, &spec, FALSE, NULL);
306 return 1;
307 case CMD_INTERRUPT:
308 if(lock > 1) lock = 4;
309 return 1;
310 case CMD_ADD:
311 if(lock > 0 && lock != 4)
312 {
313 if(store_lyr_hd() == 0) screen_status_message (_("Lyrics saved!"));
314 }
315 return 1;
316 case CMD_LYRICS_UPDATE:
317 spec.client = c;
318 spec.way = 1;
319 g_thread_create(get_lyr, &spec, FALSE, NULL);
320 return 1;
321 case CMD_SEARCH_MODE:
322 //while (0==0) fprintf (stderr, "%i", src_lyr_stack->len);
323 if (src_selection == src_lyr_stack->len-1) src_selection = -1;
324 src_selection++;
325 return 1;
326 default:
327 break;
328 }
330 lw->selected = lw->start+lw->rows;
331 if( screen_find(screen, c,
332 lw, lyrics_text_rows,
333 cmd, list_callback, NULL) )
334 {
335 /* center the row */
336 lw->start = lw->selected-(lw->rows/2);
337 if( lw->start+lw->rows > lyrics_text_rows )
338 lw->start = lyrics_text_rows-lw->rows;
339 if( lw->start<0 )
340 lw->start=0;
341 return 1;
342 }
344 return 0;
345 }
347 static list_window_t *
348 lyrics_lw(void)
349 {
350 return lw;
351 }
353 screen_functions_t *
354 get_screen_lyrics(void)
355 {
356 static screen_functions_t functions;
358 memset(&functions, 0, sizeof(screen_functions_t));
359 functions.init = lyrics_init;
360 functions.exit = lyrics_exit;
361 functions.open = NULL;
362 functions.close = NULL;
363 functions.resize = lyrics_resize;
364 functions.paint = lyrics_paint;
365 functions.update = lyrics_update;
366 functions.cmd = lyrics_cmd;
367 functions.get_lw = lyrics_lw;
368 functions.get_title = lyrics_title;
370 return &functions;
371 }
372 #endif /* ENABLE_LYRICS_SCREEN */