Code

screen_interface: make cols/rows unsigned
[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                 char *buf = g_strdup_printf(
367                     _("Do you really want to start an editor and edit these lyrics [%s/%s]? "),
368                                             YES, NO);
369                 bool really = screen_get_yesno(buf, false);
370                 g_free(buf);
371                 if (!really) {
372                         screen_status_message(_("Aborted"));
373                         return;
374                 }
375         }
377         if (store_lyr_hd() < 0)
378                 return;
380         ncu_deinit();
382         /* TODO: fork/exec/wait won't work on Windows, but building a command
383            string for system() is too tricky */
384         int status;
385         pid_t pid = fork();
386         if (pid == -1) {
387                 screen_status_printf(("%s (%s)"), _("Can't start editor"), g_strerror(errno));
388                 ncu_init();
389                 return;
390         } else if (pid == 0) {
391                 char path[1024];
392                 path_lyr_file(path, sizeof(path), current.artist, current.title);
393                 execlp(editor, editor, path, NULL);
394                 /* exec failed, do what system does */
395                 _exit(127);
396         } else {
397                 int ret;
398                 do {
399                         ret = waitpid(pid, &status, 0);
400                 } while (ret == -1 && errno == EINTR);
401         }
403         ncu_init();
405         /* TODO: hardly portable */
406         if (WIFEXITED(status)) {
407                 if (WEXITSTATUS(status) == 0)
408                         /* update to get the changes */
409                         screen_lyrics_reload();
410                 else if (WEXITSTATUS(status) == 127)
411                         screen_status_message(_("Can't start editor"));
412                 else
413                         screen_status_printf(_("Editor exited unexpectedly (%d)"),
414                                              WEXITSTATUS(status));
415         } else if (WIFSIGNALED(status)) {
416                 screen_status_printf(_("Editor exited unexpectedly (signal %d)"),
417                                      WTERMSIG(status));
418         }
421 static bool
422 lyrics_cmd(struct mpdclient *c, command_t cmd)
424         if (screen_text_cmd(&text, c, cmd))
425                 return true;
427         switch(cmd) {
428         case CMD_INTERRUPT:
429                 if (current.loader != NULL) {
430                         screen_lyrics_abort();
431                         screen_text_clear(&text);
432                 }
433                 return true;
434         case CMD_SAVE_PLAYLIST:
435                 if (current.loader == NULL && current.artist != NULL &&
436                     current.title != NULL && store_lyr_hd() == 0)
437                         /* lyrics for the song were saved on hard disk */
438                         screen_status_message (_("Lyrics saved"));
439                 return true;
440         case CMD_DELETE:
441                 if (current.loader == NULL && current.artist != NULL &&
442                     current.title != NULL) {
443                         switch (delete_lyr_hd()) {
444                         case 0:
445                                 screen_status_message (_("Lyrics deleted"));
446                                 break;
447                         case -1:
448                                 screen_status_message (_("No saved lyrics"));
449                                 break;
450                         }
451                 }
452                 return true;
453         case CMD_LYRICS_UPDATE:
454                 if (c->song != NULL) {
455                         screen_lyrics_load(c->song);
456                         screen_text_paint(&text);
457                 }
458                 return true;
459         case CMD_EDIT:
460                 lyrics_edit();
461                 return true;
462         case CMD_SELECT:
463                 screen_lyrics_reload();
464                 return true;
466 #ifdef ENABLE_SONG_SCREEN
467         case CMD_SCREEN_SONG:
468                 if (current.song != NULL) {
469                         screen_song_switch(c, current.song);
470                         return true;
471                 }
473                 break;
474 #endif
475         case CMD_SCREEN_SWAP:
476                 screen_swap(c, current.song);
477                 return true;
479         case CMD_LOCATE:
480                 if (current.song != NULL) {
481                         screen_file_goto_song(c, current.song);
482                         return true;
483                 }
485                 return false;
487         default:
488                 break;
489         }
491         return false;
494 const struct screen_functions screen_lyrics = {
495         .init = lyrics_screen_init,
496         .exit = lyrics_exit,
497         .open = lyrics_open,
498         .update = lyrics_update,
499         .close = NULL,
500         .resize = lyrics_resize,
501         .paint = lyrics_paint,
502         .cmd = lyrics_cmd,
503         .get_title = lyrics_title,
504 };
506 void
507 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
509         assert(song != NULL);
511         follow = f;
512         next_song = mpd_song_dup(song);
513         screen_switch(&screen_lyrics, c);