Code

options: configurable timeout for MPD connections
[ncmpc.git] / src / screen_lyrics.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2010 The Music Player Daemon Project
3  * Project homepage: http://musicpd.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  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
20 #include "screen_lyrics.h"
21 #include "screen_interface.h"
22 #include "screen_status.h"
23 #include "screen_file.h"
24 #include "screen_song.h"
25 #include "i18n.h"
26 #include "options.h"
27 #include "mpdclient.h"
28 #include "screen.h"
29 #include "lyrics.h"
30 #include "screen_text.h"
31 #include "ncu.h"
33 #include <assert.h>
34 #include <errno.h>
35 #include <sys/stat.h>
36 #include <sys/wait.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <glib.h>
40 #include <unistd.h>
41 #include <stdio.h>
43 static struct screen_text text;
45 static struct mpd_song *next_song;
46 static bool follow = false;
48 static struct {
49         struct mpd_song *song;
51         char *artist, *title, *plugin_name;
53         struct plugin_cycle *loader;
55         guint loader_timeout;
56 } current;
58 static void
59 screen_lyrics_abort(void)
60 {
61         if (current.loader != NULL) {
62                 plugin_stop(current.loader);
63                 current.loader = NULL;
64         }
66         if (current.loader_timeout != 0) {
67                 g_source_remove(current.loader_timeout);
68                 current.loader_timeout = 0;
69         }
71         if (current.plugin_name != NULL) {
72                 g_free(current.plugin_name);
73                 current.plugin_name = NULL;
74         }
76         if (current.artist != NULL) {
77                 g_free(current.artist);
78                 current.artist = NULL;
79         }
81         if (current.title != NULL) {
82                 g_free(current.title);
83                 current.title = NULL;
84         }
86         if (current.song != NULL) {
87                 mpd_song_free(current.song);
88                 current.song = NULL;
89         }
90 }
92 /**
93  * Repaint and update the screen, if it is currently active.
94  */
95 static void
96 lyrics_repaint_if_active(void)
97 {
98         if (screen_is_visible(&screen_lyrics)) {
99                 screen_text_repaint(&text);
101                 /* XXX repaint the screen title */
102         }
105 static void
106 path_lyr_file(char *path, size_t size,
107                 const char *artist, const char *title)
109         snprintf(path, size, "%s/.lyrics/%s - %s.txt",
110                         getenv("HOME"), artist, title);
113 static bool
114 exists_lyr_file(const char *artist, const char *title)
116         char path[1024];
117         struct stat result;
119         path_lyr_file(path, 1024, artist, title);
121         return (stat(path, &result) == 0);
124 static FILE *
125 create_lyr_file(const char *artist, const char *title)
127         char path[1024];
129         snprintf(path, 1024, "%s/.lyrics",
130                  getenv("HOME"));
131         mkdir(path, S_IRWXU);
133         path_lyr_file(path, 1024, artist, title);
135         return fopen(path, "w");
138 static int
139 store_lyr_hd(void)
141         FILE *lyr_file;
142         unsigned i;
144         lyr_file = create_lyr_file(current.artist, current.title);
145         if (lyr_file == NULL)
146                 return -1;
148         for (i = 0; i < text.lines->len; ++i)
149                 fprintf(lyr_file, "%s\n",
150                         (const char*)g_ptr_array_index(text.lines, i));
152         fclose(lyr_file);
153         return 0;
156 static int
157 delete_lyr_hd(void)
159         char path[1024];
161         if (!exists_lyr_file(current.artist, current.title))
162                 return -1;
164         path_lyr_file(path, 1024, current.artist, current.title);
165         if (unlink(path) != 0)
166                 return -2;
168         return 0;
171 static void
172 screen_lyrics_set(const GString *str)
174         screen_text_set(&text, str);
176         /* paint new data */
178         lyrics_repaint_if_active();
181 static void
182 screen_lyrics_callback(const GString *result, const bool success,
183                        const char *plugin_name, G_GNUC_UNUSED void *data)
185         assert(current.loader != NULL);
187         current.plugin_name = g_strdup(plugin_name);
189         /* Display result, which may be lyrics or error messages */
190         if (result != NULL)
191                 screen_lyrics_set(result);
193         if (success == true) {
194                 if (options.lyrics_autosave &&
195                     !exists_lyr_file(current.artist, current.title))
196                         store_lyr_hd();
197         } else {
198                 /* translators: no lyrics were found for the song */
199                 screen_status_message (_("No lyrics"));
200         }
202         if (current.loader_timeout != 0) {
203                 g_source_remove(current.loader_timeout);
204                 current.loader_timeout = 0;
205         }
207         plugin_stop(current.loader);
208         current.loader = NULL;
211 static gboolean
212 screen_lyrics_timeout_callback(gpointer G_GNUC_UNUSED data)
214         plugin_stop(current.loader);
215         current.loader = NULL;
217         screen_status_printf(_("Lyrics timeout occurred after %d seconds"),
218                              options.lyrics_timeout);
220         current.loader_timeout = 0;
221         return FALSE;
224 static void
225 screen_lyrics_load(const struct mpd_song *song)
227         const char *artist, *title;
229         assert(song != NULL);
231         screen_lyrics_abort();
232         screen_text_clear(&text);
234         artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
235         title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
237         current.song = mpd_song_dup(song);
238         current.artist = g_strdup(artist);
239         current.title = g_strdup(title);
241         current.loader = lyrics_load(current.artist, current.title,
242                                      screen_lyrics_callback, NULL);
244         if (options.lyrics_timeout != 0) {
245                 current.loader_timeout =
246                         g_timeout_add_seconds(options.lyrics_timeout,
247                                               screen_lyrics_timeout_callback,
248                                               NULL);
249         }
252 static void
253 screen_lyrics_reload(void)
255         if (current.loader == NULL && current.artist != NULL &&
256             current.title != NULL) {
257                 current.loader = lyrics_load(current.artist, current.title,
258                                              screen_lyrics_callback, NULL);
259                 screen_text_repaint(&text);
260         }
263 static void
264 lyrics_screen_init(WINDOW *w, int cols, int rows)
266         screen_text_init(&text, w, cols, rows);
269 static void
270 lyrics_resize(int cols, int rows)
272         screen_text_resize(&text, cols, rows);
275 static void
276 lyrics_exit(void)
278         screen_lyrics_abort();
280         screen_text_deinit(&text);
283 static void
284 lyrics_open(struct mpdclient *c)
286         const struct mpd_song *next_song_c =
287                 next_song != NULL ? next_song : c->song;
289         if (next_song_c != NULL &&
290             (current.song == NULL ||
291              strcmp(mpd_song_get_uri(next_song_c),
292                     mpd_song_get_uri(current.song)) != 0))
293                 screen_lyrics_load(next_song_c);
295         if (next_song != NULL) {
296                 mpd_song_free(next_song);
297                 next_song = NULL;
298         }
301 static void
302 lyrics_update(struct mpdclient *c)
304         if (!follow)
305                 return;
307         if (c->song != NULL &&
308             (current.song == NULL ||
309              strcmp(mpd_song_get_uri(c->song),
310                     mpd_song_get_uri(current.song)) != 0))
311                 screen_lyrics_load(c->song);
314 static const char *
315 lyrics_title(char *str, size_t size)
317         if (current.loader != NULL) {
318                 snprintf(str, size, "%s (%s)",
319                          _("Lyrics"),
320                          /* translators: this message is displayed
321                             while data is retrieved */
322                          _("loading..."));
323                 return str;
324         } else if (current.artist != NULL && current.title != NULL &&
325                    !screen_text_is_empty(&text)) {
326                 int n;
327                 n = snprintf(str, size, "%s: %s - %s",
328                              _("Lyrics"),
329                              current.artist, current.title);
331                 if (options.lyrics_show_plugin && current.plugin_name != NULL &&
332                     (unsigned int) n < size - 1)
333                         snprintf(str + n, size - n, " (%s)",
334                                  current.plugin_name);
336                 return str;
337         } else
338                 return _("Lyrics");
341 static void
342 lyrics_paint(void)
344         screen_text_paint(&text);
347 /* save current lyrics to a file and run editor on it */
348 static void
349 lyrics_edit(void)
351         char *editor = options.text_editor;
352         int status;
354         if (editor == NULL) {
355                 screen_status_message(_("Editor not configured"));
356                 return;
357         }
359         if (store_lyr_hd() < 0)
360                 return;
362         ncu_deinit();
364         /* TODO: fork/exec/wait won't work on Windows, but building a command
365            string for system() is too tricky */
366         pid_t pid = fork();
367         if (pid == -1) {
368                 screen_status_printf(("%s (%s)"), _("Can't start editor"), g_strerror(errno));
369         } else if (pid == 0) {
370                 char path[1024];
371                 path_lyr_file(path, sizeof(path), current.artist, current.title);
372                 execlp(editor, editor, path, NULL);
373                 /* exec failed, do what system does */
374                 _exit(127);
375         } else {
376                 int ret;
377                 do {
378                         ret = waitpid(pid, &status, 0);
379                 } while (ret == -1 && errno == EINTR);
380         }
382         ncu_init();
384         /* TODO: hardly portable */
385         if (WIFEXITED(status)) {
386                 if (WEXITSTATUS(status) == 0)
387                         /* update to get the changes */
388                         screen_lyrics_reload();
389                 else if (WEXITSTATUS(status) == 127)
390                         screen_status_message(_("Can't start editor"));
391                 else
392                         screen_status_printf(_("Editor exited unexpectedly (%d)"),
393                                              WEXITSTATUS(status));
394         } else if (WIFSIGNALED(status)) {
395                 screen_status_printf(_("Editor exited unexpectedly (signal %d)"),
396                                      WTERMSIG(status));
397         }
400 static bool
401 lyrics_cmd(struct mpdclient *c, command_t cmd)
403         if (screen_text_cmd(&text, c, cmd))
404                 return true;
406         switch(cmd) {
407         case CMD_INTERRUPT:
408                 if (current.loader != NULL) {
409                         screen_lyrics_abort();
410                         screen_text_clear(&text);
411                 }
412                 return true;
413         case CMD_SAVE_PLAYLIST:
414                 if (current.loader == NULL && current.artist != NULL &&
415                     current.title != NULL && store_lyr_hd() == 0)
416                         /* lyrics for the song were saved on hard disk */
417                         screen_status_message (_("Lyrics saved"));
418                 return true;
419         case CMD_DELETE:
420                 if (current.loader == NULL && current.artist != NULL &&
421                     current.title != NULL) {
422                         switch (delete_lyr_hd()) {
423                         case 0:
424                                 screen_status_message (_("Lyrics deleted"));
425                                 break;
426                         case -1:
427                                 screen_status_message (_("No saved lyrics"));
428                                 break;
429                         }
430                 }
431                 return true;
432         case CMD_LYRICS_UPDATE:
433                 if (c->song != NULL) {
434                         screen_lyrics_load(c->song);
435                         screen_text_repaint(&text);
436                 }
437                 return true;
438         case CMD_LYRICS_EDIT:
439                 lyrics_edit();
440                 return true;
441         case CMD_SELECT:
442                 screen_lyrics_reload();
443                 return true;
445 #ifdef ENABLE_SONG_SCREEN
446         case CMD_SCREEN_SONG:
447                 if (current.song != NULL) {
448                         screen_song_switch(c, current.song);
449                         return true;
450                 }
452                 break;
453 #endif
454         case CMD_SCREEN_SWAP:
455                 screen_swap(c, current.song);
456                 return true;
458         case CMD_LOCATE:
459                 if (current.song != NULL) {
460                         screen_file_goto_song(c, current.song);
461                         return true;
462                 }
464                 return false;
466         default:
467                 break;
468         }
470         return false;
473 const struct screen_functions screen_lyrics = {
474         .init = lyrics_screen_init,
475         .exit = lyrics_exit,
476         .open = lyrics_open,
477         .update = lyrics_update,
478         .close = NULL,
479         .resize = lyrics_resize,
480         .paint = lyrics_paint,
481         .cmd = lyrics_cmd,
482         .get_title = lyrics_title,
483 };
485 void
486 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
488         assert(song != NULL);
490         follow = f;
491         next_song = mpd_song_dup(song);
492         screen_switch(&screen_lyrics, c);