Code

fix unused parameter warnings
[ncmpc.git] / src / screen_lyrics.c
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 #include "config.h"
23 #ifndef DISABLE_LYRICS_SCREEN
24 #include <sys/stat.h>
25 #include "ncmpc.h"
26 #include "options.h"
27 #include "mpdclient.h"
28 #include "command.h"
29 #include "screen.h"
30 #include "screen_utils.h"
31 #include "easy_download.h"
32 #include "strfsong.h"
33 #include "src_lyrics.h"
34 #include "gcc.h"
36 #define _GNU_SOURCE
37 #include <stdlib.h>
38 #include <string.h>
39 #include <glib.h>
40 #include <ncurses.h>
41 #include <expat.h>
42 #include <unistd.h>
43 #include <glib/gstdio.h>
44 #include <stdio.h>
46 int src_selection;
48 static void lyrics_paint(screen_t *screen, mpdclient_t *c);
50 static FILE *create_lyr_file(char *artist, char *title)
51 {
52         char path[1024];
54         snprintf(path, 1024, "%s/.lyrics",
55                  getenv("HOME"));
56         if(g_access(path, W_OK) != 0) if(mkdir(path, S_IRWXU) != 0) return NULL;
58         snprintf(path, 1024, "%s/.lyrics/%s",
59                  getenv("HOME"), artist);
60         if(g_access(path, W_OK) != 0) if(mkdir(path, S_IRWXU) != 0) return NULL;
62         snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
63                  getenv("HOME"), artist, title);
65         return fopen(path, "w");
66 }
69 static int store_lyr_hd(void)
70 {
71         char artist[512];
72         char title[512];
73         static char path[1024];
74         FILE *lyr_file;
75         unsigned i;
76         char line_buf[1024];
78         get_text_line(&lyr_text, 0, artist, 512);
79         get_text_line(&lyr_text, 1, title, 512);
80         artist[strlen(artist)-1] = '\0';
81         title[strlen(title)-1] = '\0';
83         snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
84                  getenv("HOME"), artist, title);
85         lyr_file = create_lyr_file(artist, title);
86         if (lyr_file == NULL)
87                 return -1;
89         for (i = 3; i <= lyr_text.text->len; i++) {
90                 if(get_text_line(&lyr_text, i, line_buf, 1024) == -1);
91                 fputs(line_buf, lyr_file);
92         }
94         fclose(lyr_file);
95         return 0;
96 }
99 static void check_repaint(void)
101         if(screen_get_id("lyrics") == get_cur_mode_id())lyrics_paint(NULL, NULL);
105 static gpointer get_lyr(void *c)
107         mpd_Status *status = ((retrieval_spec*)c)->client->status;
108         mpd_Song *cur = ((retrieval_spec*)c)->client->song;
109         char artist[MAX_SONGNAME_LENGTH];
110         char title[MAX_SONGNAME_LENGTH];
112         //mpdclient_update((mpdclient_t*)c);
114         if(!(IS_PAUSED(status->state)||IS_PLAYING(status->state))) {
115                 formed_text_init(&lyr_text);
116                 return NULL;
117         }
120         lock=2;
121         result = 0;
123         formed_text_init(&lyr_text);
125         strfsong(artist, MAX_SONGNAME_LENGTH, "%artist%", cur);
126         strfsong(title, MAX_SONGNAME_LENGTH, "%title%", cur);
128         //write header..
129         formed_text_init(&lyr_text);
130         add_text_line(&lyr_text, artist, 0);
131         add_text_line(&lyr_text, title, 0);
132         add_text_line(&lyr_text, "", 0);
133         add_text_line(&lyr_text, "", 0);
135         if (((retrieval_spec*)c)->way != -1) /*till it'S of use*/ {
136                 if(get_lyr_by_src (src_selection, artist, title) != 0) {
137                         lock=0;
138                         return NULL;
139                 }
140         }
141         /*else{
142           if(get_lyr_hd(artist, title) != 0)
143           {
144           if(get_lyr_hd(artist, title) != 0) return NULL;
145           }
146           else result |= 1;
147           }*/
148         //return NULL;
149         lw->start = 0;
150         check_repaint();
151         lock = 1;
152         return &lyr_text;
155 static const char *
156 list_callback(unsigned idx, int *highlight, mpd_unused void *data)
158         static char buf[512];
160         //i think i'ts fine to write it into the 1st line...
161         if ((idx == lyr_text.lines->len && lyr_text.lines->len > 4) ||
162             ((lyr_text.lines->len == 0 || lyr_text.lines->len == 4) &&
163              idx == 0)) {
164                 src_lyr* selected = g_array_index(src_lyr_stack, src_lyr*, src_selection);
165                 *highlight=3;
166                 if (selected != NULL)
167                         return selected->description;
168                 return "";
169         }
171         if (idx < 2 && lyr_text.lines->len > 4)
172                 *highlight=3;
173         else if(idx >= lyr_text.lines->len ||
174                 (idx < 4 && idx != 0 && lyr_text.lines->len < 5)) {
175                 return "";
176         }
178         get_text_line(&lyr_text, idx, buf, 512);
179         return buf;
183 static void
184 lyrics_init(WINDOW *w, int cols, int rows)
186         lw = list_window_init(w, cols, rows);
187         lw->flags = LW_HIDE_CURSOR;
188         //lyr_text.lines = g_array_new(FALSE, TRUE, 4);
189         formed_text_init(&lyr_text);
190         if (!g_thread_supported())
191                 g_thread_init(NULL);
194 static void
195 lyrics_resize(int cols, int rows)
197         lw->cols = cols;
198         lw->rows = rows;
201 static void
202 lyrics_exit(void)
204         list_window_free(lw);
208 static const char *
209 lyrics_title(mpd_unused char *str, mpd_unused size_t size)
211         static GString *msg;
212         if (msg == NULL)
213                 msg = g_string_new ("");
214         else g_string_erase (msg, 0, -1);
216         g_string_append (msg, "Lyrics  [");
218         if (src_selection > (int)src_lyr_stack->len - 1)
219                 g_string_append (msg, "No plugin available");
220         else {
221                 src_lyr* selected =  g_array_index (src_lyr_stack, src_lyr*, src_selection);
222                 if (selected != NULL)
223                         g_string_append (msg, selected->name);
224                 else
225                         g_string_append (msg, "NONE");
226         }
228         if(lyr_text.lines->len == 4) {
229                 if(lock == 1) {
230                         if(!(result & 1)) {
231                                 g_string_append (msg, " - ");
232                                 if(!(result & 2)) g_string_append (msg, _("No access"));
233                                 else if(!(result & 4)||!(result & 16)) g_string_append (msg, _("Not found"));
234                         }
235                 }
236                 if(lock == 2) {
237                         g_string_append (msg, " - ");
238                         g_string_append (msg, _("retrieving"));
239                 }
240         }
242         g_string_append_c (msg, ']');
244         return msg->str;
247 static void
248 lyrics_paint(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
250         lw->clear = 1;
251         list_window_paint(lw, list_callback, NULL);
252         wrefresh(lw->w);
256 static void
257 lyrics_update(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
259         if( lw->repaint ) {
260                 list_window_paint(lw, list_callback, NULL);
261                 wrefresh(lw->w);
262                 lw->repaint = 0;
263         }
267 static int
268 lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
270         static retrieval_spec spec;
272         lw->repaint=1;
273         switch(cmd) {
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                 if ((unsigned)lyrics_text_rows > lw->rows)
287                         lw->start = lyrics_text_rows - lw->rows;
288                 else
289                         lw->start = 0;
290                 return 1;
291         case CMD_LIST_NEXT_PAGE:
292                 lw->start = lw->start + lw->rows - 1;
293                 if (lw->start + lw->rows >= (unsigned)lyrics_text_rows + 1) {
294                         if ((unsigned)lyrics_text_rows + 1 > lw->rows)
295                                 lw->start = lyrics_text_rows + 1 - lw->rows;
296                         else
297                                 lw->start = 0;
298                 }
299                 return 1;
300         case CMD_LIST_PREVIOUS_PAGE:
301                 if (lw->start > lw->rows)
302                         lw->start -= lw->rows;
303                 else
304                         lw->start = 0;
305                 return 1;
306         case CMD_SELECT:
307                 spec.client = c;
308                 spec.way = 0;
309                 g_thread_create(get_lyr, &spec, FALSE, NULL);
310                 return 1;
311         case CMD_INTERRUPT:
312                 if(lock > 1) lock = 4;
313                 return 1;
314         case CMD_ADD:
315                 if(lock > 0 && lock != 4) {
316                         if(store_lyr_hd() == 0)
317                                 screen_status_message (_("Lyrics saved!"));
318                 }
319                 return 1;
320         case CMD_LYRICS_UPDATE:
321                 spec.client = c;
322                 spec.way = 1;
323                 g_thread_create(get_lyr, &spec, FALSE, NULL);
324                 return 1;
325         case CMD_SEARCH_MODE:
326                 //while (0==0) fprintf (stderr, "%i", src_lyr_stack->len);
327                 if (src_selection == (int)src_lyr_stack->len - 1)
328                         src_selection = -1;
329                 src_selection++;
330                 return 1;
331         default:
332                 break;
333         }
335         lw->selected = lw->start+lw->rows;
336         if (screen_find(screen,
337                         lw,  lyrics_text_rows,
338                         cmd, list_callback, NULL)) {
339                 /* center the row */
340                 lw->start = lw->selected - (lw->rows / 2);
341                 if (lw->start + lw->rows > (unsigned)lyrics_text_rows) {
342                         if (lw->rows < (unsigned)lyrics_text_rows)
343                                 lw->start = lyrics_text_rows - lw->rows;
344                         else
345                                 lw->start = 0;
346                 }
347                 return 1;
348         }
350         return 0;
353 static list_window_t *
354 lyrics_lw(void)
356   return lw;
359 screen_functions_t *
360 get_screen_lyrics(void)
362   static screen_functions_t functions;
364   memset(&functions, 0, sizeof(screen_functions_t));
365   functions.init   = lyrics_init;
366   functions.exit   = lyrics_exit;
367   functions.open   = NULL;
368   functions.close  = NULL;
369   functions.resize = lyrics_resize;
370   functions.paint  = lyrics_paint;
371   functions.update = lyrics_update;
372   functions.cmd    = lyrics_cmd;
373   functions.get_lw = lyrics_lw;
374   functions.get_title = lyrics_title;
376   return &functions;
378 #endif /* ENABLE_LYRICS_SCREEN */