Code

screen_utils: automatically append "[y/n]"
[ncmpc.git] / src / screen_lyrics.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 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 "screen_utils.h"
32 #include "ncu.h"
34 #include <assert.h>
35 #include <errno.h>
36 #include <sys/stat.h>
37 #include <sys/wait.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <glib.h>
41 #include <unistd.h>
42 #include <stdio.h>
44 static struct screen_text text;
46 static struct mpd_song *next_song;
47 static bool follow = false;
48 /** Set if the cursor position shall be kept during the next lyrics update. */
49 static bool reloading = false;
51 static struct {
52         struct mpd_song *song;
54         char *artist, *title, *plugin_name;
56         struct plugin_cycle *loader;
58         guint loader_timeout;
59 } current;
61 static void
62 screen_lyrics_abort(void)
63 {
64         if (current.loader != NULL) {
65                 plugin_stop(current.loader);
66                 current.loader = NULL;
67         }
69         if (current.loader_timeout != 0) {
70                 g_source_remove(current.loader_timeout);
71                 current.loader_timeout = 0;
72         }
74         if (current.plugin_name != NULL) {
75                 g_free(current.plugin_name);
76                 current.plugin_name = NULL;
77         }
79         if (current.artist != NULL) {
80                 g_free(current.artist);
81                 current.artist = NULL;
82         }
84         if (current.title != NULL) {
85                 g_free(current.title);
86                 current.title = NULL;
87         }
89         if (current.song != NULL) {
90                 mpd_song_free(current.song);
91                 current.song = NULL;
92         }
93 }
95 /**
96  * Repaint and update the screen, if it is currently active.
97  */
98 static void
99 lyrics_repaint_if_active(void)
101         if (screen_is_visible(&screen_lyrics)) {
102                 screen_text_repaint(&text);
104                 /* XXX repaint the screen title */
105         }
108 static void
109 path_lyr_file(char *path, size_t size,
110                 const char *artist, const char *title)
112         snprintf(path, size, "%s/.lyrics/%s - %s.txt",
113                         getenv("HOME"), artist, title);
116 static bool
117 exists_lyr_file(const char *artist, const char *title)
119         char path[1024];
120         path_lyr_file(path, 1024, artist, title);
122         struct stat result;
123         return (stat(path, &result) == 0);
126 static FILE *
127 create_lyr_file(const char *artist, const char *title)
129         char path[1024];
130         snprintf(path, 1024, "%s/.lyrics",
131                  getenv("HOME"));
132         mkdir(path, S_IRWXU);
134         path_lyr_file(path, 1024, artist, title);
136         return fopen(path, "w");
139 static int
140 store_lyr_hd(void)
142         FILE *lyr_file = create_lyr_file(current.artist, current.title);
143         if (lyr_file == NULL)
144                 return -1;
146         for (unsigned i = 0; i < text.lines->len; ++i)
147                 fprintf(lyr_file, "%s\n",
148                         (const char*)g_ptr_array_index(text.lines, i));
150         fclose(lyr_file);
151         return 0;
154 static int
155 delete_lyr_hd(void)
157         if (!exists_lyr_file(current.artist, current.title))
158                 return -1;
160         char path[1024];
161         path_lyr_file(path, 1024, current.artist, current.title);
162         if (unlink(path) != 0)
163                 return -2;
165         return 0;
168 static void
169 screen_lyrics_set(const GString *str)
171         if (reloading) {
172                 unsigned saved_start = text.lw->start;
174                 screen_text_set(&text, str->str);
176                 /* restore the cursor and ensure that it's still valid */
177                 text.lw->start = saved_start;
178                 list_window_fetch_cursor(text.lw);
179         } else {
180                 screen_text_set(&text, str->str);
181         }
183         reloading = false;
185         /* paint new data */
187         lyrics_repaint_if_active();
190 static void
191 screen_lyrics_callback(const GString *result, const bool success,
192                        const char *plugin_name, gcc_unused void *data)
194         assert(current.loader != NULL);
196         current.plugin_name = g_strdup(plugin_name);
198         /* Display result, which may be lyrics or error messages */
199         if (result != NULL)
200                 screen_lyrics_set(result);
202         if (success == true) {
203                 if (options.lyrics_autosave &&
204                     !exists_lyr_file(current.artist, current.title))
205                         store_lyr_hd();
206         } else {
207                 /* translators: no lyrics were found for the song */
208                 screen_status_message (_("No lyrics"));
209         }
211         if (current.loader_timeout != 0) {
212                 g_source_remove(current.loader_timeout);
213                 current.loader_timeout = 0;
214         }
216         plugin_stop(current.loader);
217         current.loader = NULL;
220 static gboolean
221 screen_lyrics_timeout_callback(gpointer gcc_unused data)
223         plugin_stop(current.loader);
224         current.loader = NULL;
226         screen_status_printf(_("Lyrics timeout occurred after %d seconds"),
227                              options.lyrics_timeout);
229         current.loader_timeout = 0;
230         return FALSE;
233 static void
234 screen_lyrics_load(const struct mpd_song *song)
236         assert(song != NULL);
238         screen_lyrics_abort();
239         screen_text_clear(&text);
241         const char *artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
242         const char *title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
244         current.song = mpd_song_dup(song);
245         current.artist = g_strdup(artist);
246         current.title = g_strdup(title);
248         current.loader = lyrics_load(current.artist, current.title,
249                                      screen_lyrics_callback, NULL);
251         if (options.lyrics_timeout != 0) {
252                 current.loader_timeout =
253                         g_timeout_add_seconds(options.lyrics_timeout,
254                                               screen_lyrics_timeout_callback,
255                                               NULL);
256         }
259 static void
260 screen_lyrics_reload(void)
262         if (current.loader == NULL && current.artist != NULL &&
263             current.title != NULL) {
264                 reloading = true;
265                 current.loader = lyrics_load(current.artist, current.title,
266                                              screen_lyrics_callback, NULL);
267                 screen_text_repaint(&text);
268         }
271 static void
272 lyrics_screen_init(WINDOW *w, unsigned cols, unsigned rows)
274         screen_text_init(&text, w, cols, rows);
277 static void
278 lyrics_resize(unsigned cols, unsigned rows)
280         screen_text_resize(&text, cols, rows);
283 static void
284 lyrics_exit(void)
286         screen_lyrics_abort();
288         screen_text_deinit(&text);
291 static void
292 lyrics_open(struct mpdclient *c)
294         const struct mpd_song *next_song_c =
295                 next_song != NULL ? next_song : c->song;
297         if (next_song_c != NULL &&
298             (current.song == NULL ||
299              strcmp(mpd_song_get_uri(next_song_c),
300                     mpd_song_get_uri(current.song)) != 0))
301                 screen_lyrics_load(next_song_c);
303         if (next_song != NULL) {
304                 mpd_song_free(next_song);
305                 next_song = NULL;
306         }
309 static void
310 lyrics_update(struct mpdclient *c)
312         if (!follow)
313                 return;
315         if (c->song != NULL &&
316             (current.song == NULL ||
317              strcmp(mpd_song_get_uri(c->song),
318                     mpd_song_get_uri(current.song)) != 0))
319                 screen_lyrics_load(c->song);
322 static const char *
323 lyrics_title(char *str, size_t size)
325         if (current.loader != NULL) {
326                 snprintf(str, size, "%s (%s)",
327                          _("Lyrics"),
328                          /* translators: this message is displayed
329                             while data is retrieved */
330                          _("loading..."));
331                 return str;
332         } else if (current.artist != NULL && current.title != NULL &&
333                    !screen_text_is_empty(&text)) {
334                 int n;
335                 n = snprintf(str, size, "%s: %s - %s",
336                              _("Lyrics"),
337                              current.artist, current.title);
339                 if (options.lyrics_show_plugin && current.plugin_name != NULL &&
340                     (unsigned int) n < size - 1)
341                         snprintf(str + n, size - n, " (%s)",
342                                  current.plugin_name);
344                 return str;
345         } else
346                 return _("Lyrics");
349 static void
350 lyrics_paint(void)
352         screen_text_paint(&text);
355 /* save current lyrics to a file and run editor on it */
356 static void
357 lyrics_edit(void)
359         char *editor = options.text_editor;
360         if (editor == NULL) {
361                 screen_status_message(_("Editor not configured"));
362                 return;
363         }
365         if (options.text_editor_ask) {
366                 const char *prompt =
367                         _("Do you really want to start an editor and edit these lyrics?");
368                 bool really = screen_get_yesno(prompt, false);
369                 if (!really) {
370                         screen_status_message(_("Aborted"));
371                         return;
372                 }
373         }
375         if (store_lyr_hd() < 0)
376                 return;
378         ncu_deinit();
380         /* TODO: fork/exec/wait won't work on Windows, but building a command
381            string for system() is too tricky */
382         int status;
383         pid_t pid = fork();
384         if (pid == -1) {
385                 screen_status_printf(("%s (%s)"), _("Can't start editor"), g_strerror(errno));
386                 ncu_init();
387                 return;
388         } else if (pid == 0) {
389                 char path[1024];
390                 path_lyr_file(path, sizeof(path), current.artist, current.title);
391                 execlp(editor, editor, path, NULL);
392                 /* exec failed, do what system does */
393                 _exit(127);
394         } else {
395                 int ret;
396                 do {
397                         ret = waitpid(pid, &status, 0);
398                 } while (ret == -1 && errno == EINTR);
399         }
401         ncu_init();
403         /* TODO: hardly portable */
404         if (WIFEXITED(status)) {
405                 if (WEXITSTATUS(status) == 0)
406                         /* update to get the changes */
407                         screen_lyrics_reload();
408                 else if (WEXITSTATUS(status) == 127)
409                         screen_status_message(_("Can't start editor"));
410                 else
411                         screen_status_printf(_("Editor exited unexpectedly (%d)"),
412                                              WEXITSTATUS(status));
413         } else if (WIFSIGNALED(status)) {
414                 screen_status_printf(_("Editor exited unexpectedly (signal %d)"),
415                                      WTERMSIG(status));
416         }
419 static bool
420 lyrics_cmd(struct mpdclient *c, command_t cmd)
422         if (screen_text_cmd(&text, c, cmd))
423                 return true;
425         switch(cmd) {
426         case CMD_INTERRUPT:
427                 if (current.loader != NULL) {
428                         screen_lyrics_abort();
429                         screen_text_clear(&text);
430                 }
431                 return true;
432         case CMD_SAVE_PLAYLIST:
433                 if (current.loader == NULL && current.artist != NULL &&
434                     current.title != NULL && store_lyr_hd() == 0)
435                         /* lyrics for the song were saved on hard disk */
436                         screen_status_message (_("Lyrics saved"));
437                 return true;
438         case CMD_DELETE:
439                 if (current.loader == NULL && current.artist != NULL &&
440                     current.title != NULL) {
441                         switch (delete_lyr_hd()) {
442                         case 0:
443                                 screen_status_message (_("Lyrics deleted"));
444                                 break;
445                         case -1:
446                                 screen_status_message (_("No saved lyrics"));
447                                 break;
448                         }
449                 }
450                 return true;
451         case CMD_LYRICS_UPDATE:
452                 if (c->song != NULL) {
453                         screen_lyrics_load(c->song);
454                         screen_text_paint(&text);
455                 }
456                 return true;
457         case CMD_EDIT:
458                 lyrics_edit();
459                 return true;
460         case CMD_SELECT:
461                 screen_lyrics_reload();
462                 return true;
464 #ifdef ENABLE_SONG_SCREEN
465         case CMD_SCREEN_SONG:
466                 if (current.song != NULL) {
467                         screen_song_switch(c, current.song);
468                         return true;
469                 }
471                 break;
472 #endif
473         case CMD_SCREEN_SWAP:
474                 screen_swap(c, current.song);
475                 return true;
477         case CMD_LOCATE:
478                 if (current.song != NULL) {
479                         screen_file_goto_song(c, current.song);
480                         return true;
481                 }
483                 return false;
485         default:
486                 break;
487         }
489         return false;
492 const struct screen_functions screen_lyrics = {
493         .init = lyrics_screen_init,
494         .exit = lyrics_exit,
495         .open = lyrics_open,
496         .update = lyrics_update,
497         .close = NULL,
498         .resize = lyrics_resize,
499         .paint = lyrics_paint,
500         .cmd = lyrics_cmd,
501         .get_title = lyrics_title,
502 };
504 void
505 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
507         assert(song != NULL);
509         follow = f;
510         next_song = mpd_song_dup(song);
511         screen_switch(&screen_lyrics, c);