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 "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)
100 {
101 if (screen_is_visible(&screen_lyrics)) {
102 screen_text_repaint(&text);
104 /* XXX repaint the screen title */
105 }
106 }
108 static void
109 path_lyr_file(char *path, size_t size,
110 const char *artist, const char *title)
111 {
112 snprintf(path, size, "%s/.lyrics/%s - %s.txt",
113 getenv("HOME"), artist, title);
114 }
116 static bool
117 exists_lyr_file(const char *artist, const char *title)
118 {
119 char path[1024];
120 struct stat result;
122 path_lyr_file(path, 1024, artist, title);
124 return (stat(path, &result) == 0);
125 }
127 static FILE *
128 create_lyr_file(const char *artist, const char *title)
129 {
130 char path[1024];
132 snprintf(path, 1024, "%s/.lyrics",
133 getenv("HOME"));
134 mkdir(path, S_IRWXU);
136 path_lyr_file(path, 1024, artist, title);
138 return fopen(path, "w");
139 }
141 static int
142 store_lyr_hd(void)
143 {
144 FILE *lyr_file;
145 unsigned i;
147 lyr_file = create_lyr_file(current.artist, current.title);
148 if (lyr_file == NULL)
149 return -1;
151 for (i = 0; i < text.lines->len; ++i)
152 fprintf(lyr_file, "%s\n",
153 (const char*)g_ptr_array_index(text.lines, i));
155 fclose(lyr_file);
156 return 0;
157 }
159 static int
160 delete_lyr_hd(void)
161 {
162 char path[1024];
164 if (!exists_lyr_file(current.artist, current.title))
165 return -1;
167 path_lyr_file(path, 1024, current.artist, current.title);
168 if (unlink(path) != 0)
169 return -2;
171 return 0;
172 }
174 static void
175 screen_lyrics_set(const GString *str)
176 {
177 if (reloading) {
178 unsigned saved_start = text.lw->start;
180 screen_text_set(&text, str->str);
182 /* restore the cursor and ensure that it's still valid */
183 text.lw->start = saved_start;
184 list_window_fetch_cursor(text.lw);
185 } else {
186 screen_text_set(&text, str->str);
187 }
189 reloading = false;
191 /* paint new data */
193 lyrics_repaint_if_active();
194 }
196 static void
197 screen_lyrics_callback(const GString *result, const bool success,
198 const char *plugin_name, G_GNUC_UNUSED void *data)
199 {
200 assert(current.loader != NULL);
202 current.plugin_name = g_strdup(plugin_name);
204 /* Display result, which may be lyrics or error messages */
205 if (result != NULL)
206 screen_lyrics_set(result);
208 if (success == true) {
209 if (options.lyrics_autosave &&
210 !exists_lyr_file(current.artist, current.title))
211 store_lyr_hd();
212 } else {
213 /* translators: no lyrics were found for the song */
214 screen_status_message (_("No lyrics"));
215 }
217 if (current.loader_timeout != 0) {
218 g_source_remove(current.loader_timeout);
219 current.loader_timeout = 0;
220 }
222 plugin_stop(current.loader);
223 current.loader = NULL;
224 }
226 static gboolean
227 screen_lyrics_timeout_callback(gpointer G_GNUC_UNUSED data)
228 {
229 plugin_stop(current.loader);
230 current.loader = NULL;
232 screen_status_printf(_("Lyrics timeout occurred after %d seconds"),
233 options.lyrics_timeout);
235 current.loader_timeout = 0;
236 return FALSE;
237 }
239 static void
240 screen_lyrics_load(const struct mpd_song *song)
241 {
242 const char *artist, *title;
244 assert(song != NULL);
246 screen_lyrics_abort();
247 screen_text_clear(&text);
249 artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
250 title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
252 current.song = mpd_song_dup(song);
253 current.artist = g_strdup(artist);
254 current.title = g_strdup(title);
256 current.loader = lyrics_load(current.artist, current.title,
257 screen_lyrics_callback, NULL);
259 if (options.lyrics_timeout != 0) {
260 current.loader_timeout =
261 g_timeout_add_seconds(options.lyrics_timeout,
262 screen_lyrics_timeout_callback,
263 NULL);
264 }
265 }
267 static void
268 screen_lyrics_reload(void)
269 {
270 if (current.loader == NULL && current.artist != NULL &&
271 current.title != NULL) {
272 reloading = true;
273 current.loader = lyrics_load(current.artist, current.title,
274 screen_lyrics_callback, NULL);
275 screen_text_repaint(&text);
276 }
277 }
279 static void
280 lyrics_screen_init(WINDOW *w, int cols, int rows)
281 {
282 screen_text_init(&text, w, cols, rows);
283 }
285 static void
286 lyrics_resize(int cols, int rows)
287 {
288 screen_text_resize(&text, cols, rows);
289 }
291 static void
292 lyrics_exit(void)
293 {
294 screen_lyrics_abort();
296 screen_text_deinit(&text);
297 }
299 static void
300 lyrics_open(struct mpdclient *c)
301 {
302 const struct mpd_song *next_song_c =
303 next_song != NULL ? next_song : c->song;
305 if (next_song_c != NULL &&
306 (current.song == NULL ||
307 strcmp(mpd_song_get_uri(next_song_c),
308 mpd_song_get_uri(current.song)) != 0))
309 screen_lyrics_load(next_song_c);
311 if (next_song != NULL) {
312 mpd_song_free(next_song);
313 next_song = NULL;
314 }
315 }
317 static void
318 lyrics_update(struct mpdclient *c)
319 {
320 if (!follow)
321 return;
323 if (c->song != NULL &&
324 (current.song == NULL ||
325 strcmp(mpd_song_get_uri(c->song),
326 mpd_song_get_uri(current.song)) != 0))
327 screen_lyrics_load(c->song);
328 }
330 static const char *
331 lyrics_title(char *str, size_t size)
332 {
333 if (current.loader != NULL) {
334 snprintf(str, size, "%s (%s)",
335 _("Lyrics"),
336 /* translators: this message is displayed
337 while data is retrieved */
338 _("loading..."));
339 return str;
340 } else if (current.artist != NULL && current.title != NULL &&
341 !screen_text_is_empty(&text)) {
342 int n;
343 n = snprintf(str, size, "%s: %s - %s",
344 _("Lyrics"),
345 current.artist, current.title);
347 if (options.lyrics_show_plugin && current.plugin_name != NULL &&
348 (unsigned int) n < size - 1)
349 snprintf(str + n, size - n, " (%s)",
350 current.plugin_name);
352 return str;
353 } else
354 return _("Lyrics");
355 }
357 static void
358 lyrics_paint(void)
359 {
360 screen_text_paint(&text);
361 }
363 /* save current lyrics to a file and run editor on it */
364 static void
365 lyrics_edit(void)
366 {
367 char *editor = options.text_editor;
368 int status;
370 if (editor == NULL) {
371 screen_status_message(_("Editor not configured"));
372 return;
373 }
375 if (options.text_editor_ask) {
376 char *buf = g_strdup_printf(
377 _("Do you really want to start an editor and edit these lyrics [%s/%s]? "),
378 YES, NO);
379 bool really = screen_get_yesno(buf, false);
380 g_free(buf);
381 if (!really) {
382 screen_status_message(_("Aborted"));
383 return;
384 }
385 }
387 if (store_lyr_hd() < 0)
388 return;
390 ncu_deinit();
392 /* TODO: fork/exec/wait won't work on Windows, but building a command
393 string for system() is too tricky */
394 pid_t pid = fork();
395 if (pid == -1) {
396 screen_status_printf(("%s (%s)"), _("Can't start editor"), g_strerror(errno));
397 ncu_init();
398 return;
399 } else if (pid == 0) {
400 char path[1024];
401 path_lyr_file(path, sizeof(path), current.artist, current.title);
402 execlp(editor, editor, path, NULL);
403 /* exec failed, do what system does */
404 _exit(127);
405 } else {
406 int ret;
407 do {
408 ret = waitpid(pid, &status, 0);
409 } while (ret == -1 && errno == EINTR);
410 }
412 ncu_init();
414 /* TODO: hardly portable */
415 if (WIFEXITED(status)) {
416 if (WEXITSTATUS(status) == 0)
417 /* update to get the changes */
418 screen_lyrics_reload();
419 else if (WEXITSTATUS(status) == 127)
420 screen_status_message(_("Can't start editor"));
421 else
422 screen_status_printf(_("Editor exited unexpectedly (%d)"),
423 WEXITSTATUS(status));
424 } else if (WIFSIGNALED(status)) {
425 screen_status_printf(_("Editor exited unexpectedly (signal %d)"),
426 WTERMSIG(status));
427 }
428 }
430 static bool
431 lyrics_cmd(struct mpdclient *c, command_t cmd)
432 {
433 if (screen_text_cmd(&text, c, cmd))
434 return true;
436 switch(cmd) {
437 case CMD_INTERRUPT:
438 if (current.loader != NULL) {
439 screen_lyrics_abort();
440 screen_text_clear(&text);
441 }
442 return true;
443 case CMD_SAVE_PLAYLIST:
444 if (current.loader == NULL && current.artist != NULL &&
445 current.title != NULL && store_lyr_hd() == 0)
446 /* lyrics for the song were saved on hard disk */
447 screen_status_message (_("Lyrics saved"));
448 return true;
449 case CMD_DELETE:
450 if (current.loader == NULL && current.artist != NULL &&
451 current.title != NULL) {
452 switch (delete_lyr_hd()) {
453 case 0:
454 screen_status_message (_("Lyrics deleted"));
455 break;
456 case -1:
457 screen_status_message (_("No saved lyrics"));
458 break;
459 }
460 }
461 return true;
462 case CMD_LYRICS_UPDATE:
463 if (c->song != NULL) {
464 screen_lyrics_load(c->song);
465 screen_text_repaint(&text);
466 }
467 return true;
468 case CMD_EDIT:
469 lyrics_edit();
470 return true;
471 case CMD_SELECT:
472 screen_lyrics_reload();
473 return true;
475 #ifdef ENABLE_SONG_SCREEN
476 case CMD_SCREEN_SONG:
477 if (current.song != NULL) {
478 screen_song_switch(c, current.song);
479 return true;
480 }
482 break;
483 #endif
484 case CMD_SCREEN_SWAP:
485 screen_swap(c, current.song);
486 return true;
488 case CMD_LOCATE:
489 if (current.song != NULL) {
490 screen_file_goto_song(c, current.song);
491 return true;
492 }
494 return false;
496 default:
497 break;
498 }
500 return false;
501 }
503 const struct screen_functions screen_lyrics = {
504 .init = lyrics_screen_init,
505 .exit = lyrics_exit,
506 .open = lyrics_open,
507 .update = lyrics_update,
508 .close = NULL,
509 .resize = lyrics_resize,
510 .paint = lyrics_paint,
511 .cmd = lyrics_cmd,
512 .get_title = lyrics_title,
513 };
515 void
516 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
517 {
518 assert(song != NULL);
520 follow = f;
521 next_song = mpd_song_dup(song);
522 screen_switch(&screen_lyrics, c);
523 }