Code

screen_lyrics.c: replaced mpd_unused by G_GNUC_UNUSED
[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"
31 #define _GNU_SOURCE
32 #include <stdlib.h>
33 #include <string.h>
34 #include <glib.h>
35 #include <unistd.h>
36 #include <stdio.h>
38 static list_window_t *lw = NULL;
40 static const struct mpd_song *next_song;
42 static struct {
43         struct mpd_song *song;
45         char *artist, *title;
47         struct lyrics_loader *loader;
49         GPtrArray *lines;
50 } current;
52 static void
53 screen_lyrics_abort(void)
54 {
55         if (current.loader != NULL) {
56                 lyrics_free(current.loader);
57                 current.loader = NULL;
58         }
60         if (current.artist != NULL) {
61                 g_free(current.artist);
62                 current.artist = NULL;
63         }
65         if (current.title != NULL) {
66                 g_free(current.title);
67                 current.artist = NULL;
68         }
70         if (current.song != NULL) {
71                 mpd_freeSong(current.song);
72                 current.song = NULL;
73         }
74 }
76 static void
77 screen_lyrics_clear(void)
78 {
79         guint i;
81         for (i = 0; i < current.lines->len; ++i)
82                 g_free(g_ptr_array_index(current.lines, i));
84         g_ptr_array_set_size(current.lines, 0);
85 }
87 static void
88 lyrics_paint(void);
90 /**
91  * Repaint and update the screen.
92  */
93 static void
94 lyrics_repaint(void)
95 {
96         lyrics_paint();
97         wrefresh(lw->w);
98 }
100 /**
101  * Repaint and update the screen, if it is currently active.
102  */
103 static void
104 lyrics_repaint_if_active(void)
106         if (screen_is_visible(&screen_lyrics)) {
107                 lyrics_repaint();
109                 /* XXX repaint the screen title */
110         }
113 static void
114 screen_lyrics_set(const GString *str)
116         const char *p, *eol, *next;
118         screen_lyrics_clear();
120         p = str->str;
121         while ((eol = strchr(p, '\n')) != NULL) {
122                 char *line;
124                 next = eol + 1;
126                 /* strip whitespace at end */
128                 while (eol > p && (unsigned char)eol[-1] <= 0x20)
129                         --eol;
131                 /* create copy and append it to current.lines*/
133                 line = g_malloc(eol - p + 1);
134                 memcpy(line, p, eol - p);
135                 line[eol - p] = 0;
137                 g_ptr_array_add(current.lines, line);
139                 /* reset control characters */
141                 for (eol = line + (eol - p); line < eol; ++line)
142                         if ((unsigned char)*line < 0x20)
143                                 *line = ' ';
145                 p = next;
146         }
148         if (*p != 0)
149                 g_ptr_array_add(current.lines, g_strdup(p));
151         /* paint new data */
153         lyrics_repaint_if_active();
156 static void
157 screen_lyrics_callback(const GString *result, G_GNUC_UNUSED void *data)
159         assert(current.loader != NULL);
161         if (result != NULL)
162                 screen_lyrics_set(result);
163         else
164                 screen_status_message (_("No lyrics"));
166         lyrics_free(current.loader);
167         current.loader = NULL;
170 static void
171 screen_lyrics_load(const struct mpd_song *song)
173         char buffer[MAX_SONGNAME_LENGTH];
175         assert(song != NULL);
177         screen_lyrics_abort();
178         screen_lyrics_clear();
180         current.song = mpd_songDup(song);
182         strfsong(buffer, sizeof(buffer), "%artist%", song);
183         current.artist = g_strdup(buffer);
185         strfsong(buffer, sizeof(buffer), "%title%", song);
186         current.title = g_strdup(buffer);
188         current.loader = lyrics_load(current.artist, current.title,
189                                      screen_lyrics_callback, NULL);
192 static FILE *create_lyr_file(const char *artist, const char *title)
194         char path[1024];
196         snprintf(path, 1024, "%s/.lyrics",
197                  getenv("HOME"));
198         mkdir(path, S_IRWXU);
200         snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
201                  getenv("HOME"), artist, title);
203         return fopen(path, "w");
206 static int store_lyr_hd(void)
208         FILE *lyr_file;
209         unsigned i;
211         lyr_file = create_lyr_file(current.artist, current.title);
212         if (lyr_file == NULL)
213                 return -1;
215         for (i = 0; i < current.lines->len; ++i)
216                 fprintf(lyr_file, "%s\n",
217                         (const char*)g_ptr_array_index(current.lines, i));
219         fclose(lyr_file);
220         return 0;
223 static const char *
224 list_callback(unsigned idx, G_GNUC_UNUSED int *highlight,
225               G_GNUC_UNUSED void *data)
227         static char buffer[256];
228         char *value;
230         if (idx >= current.lines->len)
231                 return NULL;
233         value = utf8_to_locale(g_ptr_array_index(current.lines, idx));
234         g_strlcpy(buffer, value, sizeof(buffer));
235         free(value);
237         return buffer;
241 static void
242 lyrics_screen_init(WINDOW *w, int cols, int rows)
244         current.lines = g_ptr_array_new();
245         lw = list_window_init(w, cols, rows);
246         lw->flags = LW_HIDE_CURSOR;
249 static void
250 lyrics_resize(int cols, int rows)
252         lw->cols = cols;
253         lw->rows = rows;
256 static void
257 lyrics_exit(void)
259         list_window_free(lw);
261         screen_lyrics_abort();
262         screen_lyrics_clear();
264         g_ptr_array_free(current.lines, TRUE);
265         current.lines = NULL;
268 static void
269 lyrics_open(mpdclient_t *c)
271         if (next_song == NULL)
272                 next_song = c->song;
274         if (next_song != NULL &&
275             (current.song == NULL ||
276              strcmp(next_song->file, current.song->file) != 0))
277                 screen_lyrics_load(next_song);
279         next_song = NULL;
283 static const char *
284 lyrics_title(char *str, size_t size)
286         if (current.loader != NULL) {
287                 snprintf(str, size, "%s (%s)",
288                          _("Lyrics"), _("loading..."));
289                 return str;
290         } else if (current.artist != NULL && current.title != NULL &&
291                    current.lines->len > 0) {
292                 snprintf(str, size, "%s: %s - %s",
293                          _("Lyrics"),
294                          current.artist, current.title);
295                 return str;
296         } else
297                 return _("Lyrics");
300 static void
301 lyrics_paint(void)
303         list_window_paint(lw, list_callback, NULL);
306 static bool
307 lyrics_cmd(mpdclient_t *c, command_t cmd)
309         if (list_window_scroll_cmd(lw, current.lines->len, cmd)) {
310                 lyrics_repaint();
311                 return true;
312         }
314         switch(cmd) {
315         case CMD_INTERRUPT:
316                 if (current.loader != NULL) {
317                         screen_lyrics_abort();
318                         screen_lyrics_clear();
319                 }
320                 return true;
321         case CMD_ADD:
322                 if (current.loader == NULL && current.artist != NULL &&
323                     current.title != NULL && store_lyr_hd() == 0)
324                         screen_status_message (_("Lyrics saved!"));
325                 return true;
326         case CMD_LYRICS_UPDATE:
327                 if (c->song != NULL) {
328                         screen_lyrics_load(c->song);
329                         lyrics_repaint();
330                 }
331                 return true;
333 #ifdef ENABLE_SONG_SCREEN
334         case CMD_VIEW:
335                 if (current.song != NULL) {
336                         screen_song_switch(c, current.song);
337                         return true;
338                 }
340                 break;
341 #endif
343         case CMD_LOCATE:
344                 if (current.song != NULL) {
345                         screen_file_goto_song(c, current.song);
346                         return true;
347                 }
349                 return false;
351         default:
352                 break;
353         }
355         lw->selected = lw->start+lw->rows;
356         if (screen_find(lw, current.lines->len,
357                         cmd, list_callback, NULL)) {
358                 /* center the row */
359                 list_window_center(lw, current.lines->len, lw->selected);
360                 lyrics_repaint();
361                 return true;
362         }
364         return false;
367 const struct screen_functions screen_lyrics = {
368         .init = lyrics_screen_init,
369         .exit = lyrics_exit,
370         .open = lyrics_open,
371         .close = NULL,
372         .resize = lyrics_resize,
373         .paint = lyrics_paint,
374         .cmd = lyrics_cmd,
375         .get_title = lyrics_title,
376 };
378 void
379 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song)
381         assert(song != NULL);
383         next_song = song;
384         screen_switch(&screen_lyrics, c);