Code

include ncursesw/ncurses.h if available
[ncmpc.git] / src / screen_lyrics.c
1 /*
2  * (c) 2006 by Kalle Wallin <kaw@linux.se>
3  * Copyright (C) 2008 Max Kellermann <max@duempel.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  */
20 #include <sys/stat.h>
21 #include "i18n.h"
22 #include "options.h"
23 #include "mpdclient.h"
24 #include "command.h"
25 #include "screen.h"
26 #include "screen_utils.h"
27 #include "strfsong.h"
28 #include "lyrics.h"
29 #include "charset.h"
30 #include "gcc.h"
32 #define _GNU_SOURCE
33 #include <stdlib.h>
34 #include <string.h>
35 #include <glib.h>
36 #include <unistd.h>
37 #include <stdio.h>
39 static list_window_t *lw = NULL;
41 static const struct mpd_song *next_song;
43 static struct {
44         const struct mpd_song *song;
46         char *artist, *title;
48         struct lyrics_loader *loader;
50         GPtrArray *lines;
51 } current;
53 static void
54 screen_lyrics_abort(void)
55 {
56         if (current.loader != NULL) {
57                 lyrics_free(current.loader);
58                 current.loader = NULL;
59         }
61         if (current.artist != NULL) {
62                 g_free(current.artist);
63                 current.artist = NULL;
64         }
66         if (current.title != NULL) {
67                 g_free(current.title);
68                 current.artist = NULL;
69         }
71         current.song = NULL;
72 }
74 static void
75 screen_lyrics_clear(void)
76 {
77         guint i;
79         for (i = 0; i < current.lines->len; ++i)
80                 g_free(g_ptr_array_index(current.lines, i));
82         g_ptr_array_set_size(current.lines, 0);
83 }
85 static void
86 lyrics_paint(void);
88 /**
89  * Repaint and update the screen.
90  */
91 static void
92 lyrics_repaint(void)
93 {
94         lyrics_paint();
95         wrefresh(lw->w);
96 }
98 /**
99  * Repaint and update the screen, if it is currently active.
100  */
101 static void
102 lyrics_repaint_if_active(void)
104         if (screen_is_visible(&screen_lyrics)) {
105                 lyrics_repaint();
107                 /* XXX repaint the screen title */
108         }
111 static void
112 screen_lyrics_set(const GString *str)
114         const char *p, *eol, *next;
116         screen_lyrics_clear();
118         p = str->str;
119         while ((eol = strchr(p, '\n')) != NULL) {
120                 char *line;
122                 next = eol + 1;
124                 /* strip whitespace at end */
126                 while (eol > p && (unsigned char)eol[-1] <= 0x20)
127                         --eol;
129                 /* create copy and append it to current.lines*/
131                 line = g_malloc(eol - p + 1);
132                 memcpy(line, p, eol - p);
133                 line[eol - p] = 0;
135                 g_ptr_array_add(current.lines, line);
137                 /* reset control characters */
139                 for (eol = line + (eol - p); line < eol; ++line)
140                         if ((unsigned char)*line < 0x20)
141                                 *line = ' ';
143                 p = next;
144         }
146         if (*p != 0)
147                 g_ptr_array_add(current.lines, g_strdup(p));
149         /* paint new data */
151         lyrics_repaint_if_active();
154 static void
155 screen_lyrics_callback(const GString *result, mpd_unused void *data)
157         assert(current.loader != NULL);
159         if (result != NULL)
160                 screen_lyrics_set(result);
161         else
162                 screen_status_message (_("No lyrics"));
164         lyrics_free(current.loader);
165         current.loader = NULL;
168 static void
169 screen_lyrics_load(const struct mpd_song *song)
171         char buffer[MAX_SONGNAME_LENGTH];
173         assert(song != NULL);
175         screen_lyrics_abort();
176         screen_lyrics_clear();
178         current.song = song;
180         strfsong(buffer, sizeof(buffer), "%artist%", song);
181         current.artist = g_strdup(buffer);
183         strfsong(buffer, sizeof(buffer), "%title%", song);
184         current.title = g_strdup(buffer);
186         current.loader = lyrics_load(current.artist, current.title,
187                                      screen_lyrics_callback, NULL);
190 static FILE *create_lyr_file(const char *artist, const char *title)
192         char path[1024];
194         snprintf(path, 1024, "%s/.lyrics",
195                  getenv("HOME"));
196         mkdir(path, S_IRWXU);
198         snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
199                  getenv("HOME"), artist, title);
201         return fopen(path, "w");
204 static int store_lyr_hd(void)
206         FILE *lyr_file;
207         unsigned i;
209         lyr_file = create_lyr_file(current.artist, current.title);
210         if (lyr_file == NULL)
211                 return -1;
213         for (i = 0; i < current.lines->len; ++i)
214                 fprintf(lyr_file, "%s\n",
215                         (const char*)g_ptr_array_index(current.lines, i));
217         fclose(lyr_file);
218         return 0;
221 static const char *
222 list_callback(unsigned idx, mpd_unused int *highlight, mpd_unused void *data)
224         static char buffer[256];
225         char *value;
227         if (idx >= current.lines->len)
228                 return NULL;
230         value = utf8_to_locale(g_ptr_array_index(current.lines, idx));
231         g_strlcpy(buffer, value, sizeof(buffer));
232         free(value);
234         return buffer;
238 static void
239 lyrics_screen_init(WINDOW *w, int cols, int rows)
241         current.lines = g_ptr_array_new();
242         lw = list_window_init(w, cols, rows);
243         lw->flags = LW_HIDE_CURSOR;
246 static void
247 lyrics_resize(int cols, int rows)
249         lw->cols = cols;
250         lw->rows = rows;
253 static void
254 lyrics_exit(void)
256         list_window_free(lw);
258         screen_lyrics_abort();
259         screen_lyrics_clear();
261         g_ptr_array_free(current.lines, TRUE);
262         current.lines = NULL;
265 static void
266 lyrics_open(mpdclient_t *c)
268         if (next_song == NULL)
269                 next_song = c->song;
271         if (next_song != NULL && next_song != current.song)
272                 screen_lyrics_load(next_song);
274         next_song = NULL;
278 static const char *
279 lyrics_title(char *str, size_t size)
281         if (current.loader != NULL)
282                 return "Lyrics (loading)";
283         else if (current.artist != NULL && current.title != NULL &&
284                  current.lines->len > 0) {
285                 snprintf(str, size, "Lyrics: %s - %s",
286                          current.artist, current.title);
287                 return str;
288         } else
289                 return "Lyrics";
292 static void
293 lyrics_paint(void)
295         list_window_paint(lw, list_callback, NULL);
298 static int
299 lyrics_cmd(mpdclient_t *c, command_t cmd)
301         if (list_window_scroll_cmd(lw, current.lines->len, cmd)) {
302                 lyrics_repaint();
303                 return 1;
304         }
306         switch(cmd) {
307         case CMD_INTERRUPT:
308                 if (current.loader != NULL) {
309                         screen_lyrics_abort();
310                         screen_lyrics_clear();
311                 }
312                 return 1;
313         case CMD_ADD:
314                 if (current.loader == NULL && current.artist != NULL &&
315                     current.title != NULL && store_lyr_hd() == 0)
316                         screen_status_message (_("Lyrics saved!"));
317                 return 1;
318         case CMD_LYRICS_UPDATE:
319                 if (c->song != NULL) {
320                         screen_lyrics_load(c->song);
321                         lyrics_repaint();
322                 }
323                 return 1;
324         default:
325                 break;
326         }
328         lw->selected = lw->start+lw->rows;
329         if (screen_find(lw, current.lines->len,
330                         cmd, list_callback, NULL)) {
331                 /* center the row */
332                 list_window_center(lw, current.lines->len, lw->selected);
333                 lyrics_repaint();
334                 return 1;
335         }
337         return 0;
340 const struct screen_functions screen_lyrics = {
341         .init = lyrics_screen_init,
342         .exit = lyrics_exit,
343         .open = lyrics_open,
344         .close = NULL,
345         .resize = lyrics_resize,
346         .paint = lyrics_paint,
347         .cmd = lyrics_cmd,
348         .get_title = lyrics_title,
349 };
351 void
352 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song)
354         assert(song != NULL);
356         next_song = song;
357         screen_switch(&screen_lyrics, c);