Code

screen_queue: use paint_song_row()
[ncmpc.git] / src / screen_queue.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 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_queue.h"
21 #include "screen_interface.h"
22 #include "screen_file.h"
23 #include "screen_message.h"
24 #include "screen_find.h"
25 #include "config.h"
26 #include "i18n.h"
27 #include "charset.h"
28 #include "options.h"
29 #include "mpdclient.h"
30 #include "utils.h"
31 #include "strfsong.h"
32 #include "wreadln.h"
33 #include "song_paint.h"
34 #include "screen.h"
35 #include "screen_utils.h"
36 #include "screen_song.h"
37 #include "screen_lyrics.h"
39 #ifndef NCMPC_MINI
40 #include "hscroll.h"
41 #endif
43 #include <mpd/client.h>
45 #include <ctype.h>
46 #include <string.h>
47 #include <glib.h>
49 #define MAX_SONG_LENGTH 512
51 #ifndef NCMPC_MINI
52 typedef struct
53 {
54         GList **list;
55         GList **dir_list;
56         struct mpdclient *c;
57 } completion_callback_data_t;
59 static struct hscroll hscroll;
60 static guint scroll_source_id;
61 #endif
63 static struct mpdclient_playlist *playlist;
64 static int current_song_id = -1;
65 static int selected_song_id = -1;
66 static struct list_window *lw;
67 static guint timer_hide_cursor_id;
69 static void
70 screen_queue_paint(void);
72 static void
73 screen_queue_repaint(void)
74 {
75         screen_queue_paint();
76         wrefresh(lw->w);
77 }
79 static const struct mpd_song *
80 screen_queue_selected_song(void)
81 {
82         return !lw->range_selection &&
83                 lw->selected < playlist_length(playlist)
84                 ? playlist_get(playlist, lw->selected)
85                 : NULL;
86 }
88 static void
89 screen_queue_save_selection(void)
90 {
91         selected_song_id = screen_queue_selected_song() != NULL
92                 ? (int)mpd_song_get_id(screen_queue_selected_song())
93                 : -1;
94 }
96 static void
97 screen_queue_restore_selection(void)
98 {
99         const struct mpd_song *song;
100         int pos;
102         list_window_set_length(lw, playlist_length(playlist));
104         if (selected_song_id < 0)
105                 /* there was no selection */
106                 return;
108         song = screen_queue_selected_song();
109         if (song != NULL &&
110             mpd_song_get_id(song) == (unsigned)selected_song_id)
111                 /* selection is still valid */
112                 return;
114         pos = playlist_get_index_from_id(playlist, selected_song_id);
115         if (pos >= 0)
116                 list_window_set_cursor(lw, pos);
118         screen_queue_save_selection();
121 #ifndef NCMPC_MINI
122 static gboolean
123 scroll_timer_callback(G_GNUC_UNUSED gpointer data)
125         scroll_source_id = 0;
127         hscroll_step(&hscroll);
128         screen_queue_repaint();
129         return false;
131 #endif
133 static const char *
134 screen_queue_lw_callback(unsigned idx, bool *highlight, char **second_column,
135                          G_GNUC_UNUSED void *data)
137         static char songname[MAX_SONG_LENGTH];
138         struct mpd_song *song;
140         assert(playlist != NULL);
141         assert(idx < playlist_length(playlist));
143         song = playlist_get(playlist, idx);
144         if ((int)mpd_song_get_id(song) == current_song_id)
145                 *highlight = true;
147         strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
149 #ifndef NCMPC_MINI
150         if (second_column != NULL && mpd_song_get_duration(song) > 0) {
151                 char duration[32];
152                 format_duration_short(duration, sizeof(duration),
153                                       mpd_song_get_duration(song));
154                 *second_column = g_strdup(duration);
155         }
157         if (idx == lw->selected)
158         {
159                 int second_column_len = 0;
160                 if (second_column != NULL && *second_column != NULL)
161                         second_column_len = strlen(*second_column);
162                 if (options.scroll && utf8_width(songname) > (unsigned)(COLS - second_column_len - 1) )
163                 {
164                         static unsigned current_song;
165                         char *tmp;
167                         if (current_song != lw->selected) {
168                                 hscroll_reset(&hscroll);
169                                 current_song = lw->selected;
170                         }
172                         tmp = strscroll(&hscroll, songname, options.scroll_sep,
173                                         MAX_SONG_LENGTH);
174                         g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
175                         g_free(tmp);
177                         if (scroll_source_id == 0)
178                                 scroll_source_id =
179                                         g_timeout_add(1000,
180                                                       scroll_timer_callback,
181                                                       NULL);
182                 } else {
183                         hscroll_reset(&hscroll);
185                         if (scroll_source_id != 0) {
186                                 g_source_remove(scroll_source_id);
187                                 scroll_source_id = 0;
188                         }
189                 }
190         }
191 #else
192         (void)second_column;
193 #endif
195         return songname;
198 static void
199 center_playing_item(struct mpdclient *c, bool center_cursor)
201         const struct mpd_song *song;
202         unsigned length = c->playlist.list->len;
203         int idx;
205         song = mpdclient_get_current_song(c);
206         if (song == NULL)
207                 return;
209         /* try to center the song that are playing */
210         idx = playlist_get_index(&c->playlist, c->song);
211         if (idx < 0)
212                 return;
214         if (length < lw->rows)
215         {
216                 if (center_cursor)
217                         list_window_set_cursor(lw, idx);
218                 return;
219         }
221         list_window_center(lw, idx);
223         if (center_cursor) {
224                 list_window_set_cursor(lw, idx);
225                 return;
226         }
228         /* make sure the cursor is in the window */
229         list_window_fetch_cursor(lw);
232 #ifndef NCMPC_MINI
233 static void
234 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
235                        void *data)
237         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
238         GList **list = tmp->list;
239         struct mpdclient *c = tmp->c;
241         if( *list == NULL ) {
242                 /* create completion list */
243                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
244                 g_completion_add_items(gcmp, *list);
245         }
248 static void
249 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
250                         G_GNUC_UNUSED gchar *line, GList *items,
251                         G_GNUC_UNUSED void *data)
253         if (g_list_length(items) >= 1)
254                 screen_display_completion_list(items);
256 #endif
258 #ifndef NCMPC_MINI
259 /**
260  * Wrapper for strncmp().  We are not allowed to pass &strncmp to
261  * g_completion_set_compare(), because strncmp() takes size_t where
262  * g_completion_set_compare passes a gsize value.
263  */
264 static gint
265 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
267         return strncmp(s1, s2, n);
269 #endif
271 int
272 playlist_save(struct mpdclient *c, char *name, char *defaultname)
274         struct mpd_connection *connection;
275         gchar *filename, *filename_utf8;
276 #ifndef NCMPC_MINI
277         GCompletion *gcmp;
278         GList *list = NULL;
279         completion_callback_data_t data;
280 #endif
282 #ifdef NCMPC_MINI
283         (void)defaultname;
284 #endif
286 #ifndef NCMPC_MINI
287         if (name == NULL) {
288                 /* initialize completion support */
289                 gcmp = g_completion_new(NULL);
290                 g_completion_set_compare(gcmp, completion_strncmp);
291                 data.list = &list;
292                 data.dir_list = NULL;
293                 data.c = c;
294                 wrln_completion_callback_data = &data;
295                 wrln_pre_completion_callback = save_pre_completion_cb;
296                 wrln_post_completion_callback = save_post_completion_cb;
299                 /* query the user for a filename */
300                 filename = screen_readln(_("Save playlist as"),
301                                          defaultname,
302                                          NULL,
303                                          gcmp);
305                 /* destroy completion support */
306                 wrln_completion_callback_data = NULL;
307                 wrln_pre_completion_callback = NULL;
308                 wrln_post_completion_callback = NULL;
309                 g_completion_free(gcmp);
310                 list = string_list_free(list);
311                 if( filename )
312                         filename=g_strstrip(filename);
313         } else
314 #endif
315                         filename=g_strdup(name);
317         if (filename == NULL)
318                 return -1;
320         /* send save command to mpd */
322         filename_utf8 = locale_to_utf8(filename);
324         connection = mpdclient_get_connection(c);
325         if (!mpd_run_save(connection, filename_utf8)) {
326                 if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
327                     mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST &&
328                     mpd_connection_clear_error(connection)) {
329                         char *buf;
330                         int key;
332                         buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
333                                               filename, YES, NO);
334                         key = tolower(screen_getch(buf));
335                         g_free(buf);
337                         if (key != YES[0]) {
338                                 g_free(filename_utf8);
339                                 g_free(filename);
340                                 screen_status_printf(_("Aborted"));
341                                 return -1;
342                         }
344                         if (!mpd_run_rm(connection, filename_utf8) ||
345                             !mpd_run_save(connection, filename_utf8)) {
346                                 mpdclient_handle_error(c);
347                                 g_free(filename_utf8);
348                                 g_free(filename);
349                                 return -1;
350                         }
351                 } else {
352                         mpdclient_handle_error(c);
353                         g_free(filename_utf8);
354                         g_free(filename);
355                         return -1;
356                 }
357         }
359         c->events |= MPD_IDLE_STORED_PLAYLIST;
361         g_free(filename_utf8);
363         /* success */
364         screen_status_printf(_("Saved %s"), filename);
365         g_free(filename);
366         return 0;
369 #ifndef NCMPC_MINI
370 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
371                     GList **list, struct mpdclient *c)
373         g_completion_remove_items(gcmp, *list);
374         *list = string_list_remove(*list, dir);
375         *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
376         g_completion_add_items(gcmp, *list);
377         *dir_list = g_list_append(*dir_list, g_strdup(dir));
380 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
382         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
383         GList **dir_list = tmp->dir_list;
384         GList **list = tmp->list;
385         struct mpdclient *c = tmp->c;
387         if (*list == NULL) {
388                 /* create initial list */
389                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
390                 g_completion_add_items(gcmp, *list);
391         } else if (line && line[0] && line[strlen(line)-1]=='/' &&
392                    string_list_find(*dir_list, line) == NULL) {
393                 /* add directory content to list */
394                 add_dir(gcmp, line, dir_list, list, c);
395         }
398 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
399                                    GList *items, void *data)
401         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
402         GList **dir_list = tmp->dir_list;
403         GList **list = tmp->list;
404         struct mpdclient *c = tmp->c;
406         if (g_list_length(items) >= 1)
407                 screen_display_completion_list(items);
409         if (line && line[0] && line[strlen(line) - 1] == '/' &&
410             string_list_find(*dir_list, line) == NULL) {
411                 /* add directory content to list */
412                 add_dir(gcmp, line, dir_list, list, c);
413         }
415 #endif
417 static int
418 handle_add_to_playlist(struct mpdclient *c)
420         gchar *path;
421 #ifndef NCMPC_MINI
422         GCompletion *gcmp;
423         GList *list = NULL;
424         GList *dir_list = NULL;
425         completion_callback_data_t data;
427         /* initialize completion support */
428         gcmp = g_completion_new(NULL);
429         g_completion_set_compare(gcmp, completion_strncmp);
430         data.list = &list;
431         data.dir_list = &dir_list;
432         data.c = c;
433         wrln_completion_callback_data = &data;
434         wrln_pre_completion_callback = add_pre_completion_cb;
435         wrln_post_completion_callback = add_post_completion_cb;
436 #endif
438         /* get path */
439         path = screen_readln(_("Add"),
440                              NULL,
441                              NULL,
442 #ifdef NCMPC_MINI
443                              NULL
444 #else
445                              gcmp
446 #endif
447                              );
449         /* destroy completion data */
450 #ifndef NCMPC_MINI
451         wrln_completion_callback_data = NULL;
452         wrln_pre_completion_callback = NULL;
453         wrln_post_completion_callback = NULL;
454         g_completion_free(gcmp);
455         string_list_free(list);
456         string_list_free(dir_list);
457 #endif
459         /* add the path to the playlist */
460         if (path != NULL) {
461                 char *path_utf8 = locale_to_utf8(path);
462                 mpdclient_cmd_add_path(c, path_utf8);
463                 g_free(path_utf8);
464         }
466         g_free(path);
467         return 0;
470 static void
471 screen_queue_init(WINDOW *w, int cols, int rows)
473         lw = list_window_init(w, cols, rows);
476 static gboolean
477 timer_hide_cursor(gpointer data)
479         struct mpdclient *c = data;
481         assert(options.hide_cursor > 0);
482         assert(timer_hide_cursor_id != 0);
484         timer_hide_cursor_id = 0;
486         /* hide the cursor when mpd is playing and the user is inactive */
488         if (c->status != NULL &&
489             mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
490                 lw->hide_cursor = true;
491                 screen_queue_repaint();
492         } else
493                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
494                                                      timer_hide_cursor, c);
496         return FALSE;
499 static void
500 screen_queue_open(struct mpdclient *c)
502         playlist = &c->playlist;
504         assert(timer_hide_cursor_id == 0);
505         if (options.hide_cursor > 0) {
506                 lw->hide_cursor = false;
507                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
508                                                      timer_hide_cursor, c);
509         }
511         screen_queue_restore_selection();
514 static void
515 screen_queue_close(void)
517         if (timer_hide_cursor_id != 0) {
518                 g_source_remove(timer_hide_cursor_id);
519                 timer_hide_cursor_id = 0;
520         }
523 static void
524 screen_queue_resize(int cols, int rows)
526         list_window_resize(lw, cols, rows);
530 static void
531 screen_queue_exit(void)
533         list_window_free(lw);
536 static const char *
537 screen_queue_title(char *str, size_t size)
539         if (options.host == NULL)
540                 return _("Playlist");
542         g_snprintf(str, size, _("Playlist on %s"), options.host);
543         return str;
546 static void
547 screen_queue_paint_callback(WINDOW *w, unsigned i,
548                             unsigned y, unsigned width,
549                             bool selected, G_GNUC_UNUSED void *data)
551         const struct mpd_song *song;
553         assert(playlist != NULL);
554         assert(i < playlist_length(playlist));
556         song = playlist_get(playlist, i);
558         paint_song_row(w, y, width, selected,
559                        (int)mpd_song_get_id(song) == current_song_id,
560                        song);
563 static void
564 screen_queue_paint(void)
566         list_window_paint2(lw, screen_queue_paint_callback, NULL);
569 G_GNUC_PURE
570 static int
571 get_current_song_id(const struct mpd_status *status)
573         return status != NULL &&
574                 (mpd_status_get_state(status) == MPD_STATE_PLAY ||
575                  mpd_status_get_state(status) == MPD_STATE_PAUSE)
576                 ? (int)mpd_status_get_song_id(status)
577                 : -1;
580 static void
581 screen_queue_update(struct mpdclient *c)
583         if (c->events & MPD_IDLE_PLAYLIST)
584                 screen_queue_restore_selection();
586         if ((c->events & MPD_IDLE_PLAYER) != 0 &&
587             get_current_song_id(c->status) != current_song_id) {
588                 current_song_id = get_current_song_id(c->status);
590                 /* center the cursor */
591                 if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
592                         center_playing_item(c, false);
594                 screen_queue_repaint();
595         } else if (c->events & MPD_IDLE_PLAYLIST) {
596                 /* the playlist has changed, we must paint the new
597                    version */
598                 screen_queue_repaint();
599         }
602 #ifdef HAVE_GETMOUSE
603 static bool
604 handle_mouse_event(struct mpdclient *c)
606         int row;
607         unsigned selected;
608         unsigned long bstate;
610         if (screen_get_mouse_event(c, &bstate, &row) ||
611             list_window_mouse(lw, bstate, row)) {
612                 screen_queue_repaint();
613                 return true;
614         }
616         if (bstate & BUTTON1_DOUBLE_CLICKED) {
617                 /* stop */
618                 screen_cmd(c, CMD_STOP);
619                 return true;
620         }
622         selected = lw->start + row;
624         if (bstate & BUTTON1_CLICKED) {
625                 /* play */
626                 if (lw->start + row < playlist_length(playlist))
627                         mpdclient_cmd_play(c, lw->start + row);
628         } else if (bstate & BUTTON3_CLICKED) {
629                 /* delete */
630                 if (selected == lw->selected)
631                         mpdclient_cmd_delete(c, lw->selected);
632         }
634         list_window_set_cursor(lw, selected);
635         screen_queue_save_selection();
636         screen_queue_repaint();
638         return true;
640 #endif
642 static bool
643 screen_queue_cmd(struct mpdclient *c, command_t cmd)
645         struct mpd_connection *connection;
646         static command_t cached_cmd = CMD_NONE;
647         command_t prev_cmd = cached_cmd;
648         struct list_window_range range;
650         cached_cmd = cmd;
652         lw->hide_cursor = false;
654         if (options.hide_cursor > 0) {
655                 if (timer_hide_cursor_id != 0)
656                         g_source_remove(timer_hide_cursor_id);
657                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
658                                                      timer_hide_cursor, c);
659         }
661         if (list_window_cmd(lw, cmd)) {
662                 screen_queue_save_selection();
663                 screen_queue_repaint();
664                 return true;
665         }
667         switch(cmd) {
668         case CMD_SCREEN_UPDATE:
669                 center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
670                 screen_queue_repaint();
671                 return false;
672         case CMD_SELECT_PLAYING:
673                 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
674                                                               c->song));
675                 screen_queue_save_selection();
676                 screen_queue_repaint();
677                 return true;
679         case CMD_LIST_FIND:
680         case CMD_LIST_RFIND:
681         case CMD_LIST_FIND_NEXT:
682         case CMD_LIST_RFIND_NEXT:
683                 screen_find(lw, cmd, screen_queue_lw_callback, NULL);
684                 screen_queue_save_selection();
685                 screen_queue_repaint();
686                 return true;
687         case CMD_LIST_JUMP:
688                 screen_jump(lw, screen_queue_lw_callback, NULL, NULL);
689                 screen_queue_save_selection();
690                 screen_queue_repaint();
691                 return true;
693 #ifdef HAVE_GETMOUSE
694         case CMD_MOUSE_EVENT:
695                 return handle_mouse_event(c);
696 #endif
698 #ifdef ENABLE_SONG_SCREEN
699         case CMD_SCREEN_SONG:
700                 if (screen_queue_selected_song() != NULL) {
701                         screen_song_switch(c, screen_queue_selected_song());
702                         return true;
703                 }
705                 break;
706 #endif
708 #ifdef ENABLE_LYRICS_SCREEN
709         case CMD_SCREEN_LYRICS:
710                 if (lw->selected < playlist_length(&c->playlist)) {
711                         struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
712                         bool follow = false;
714                         if (c->song && selected &&
715                             !strcmp(mpd_song_get_uri(selected),
716                                     mpd_song_get_uri(c->song)))
717                                 follow = true;
719                         screen_lyrics_switch(c, selected, follow);
720                         return true;
721                 }
723                 break;
724 #endif
725         case CMD_SCREEN_SWAP:
726                 screen_swap(c, playlist_get(&c->playlist, lw->selected));
727                 return true;
729         default:
730                 break;
731         }
733         if (!mpdclient_is_connected(c))
734                 return false;
736         switch(cmd) {
737         case CMD_PLAY:
738                 mpdclient_cmd_play(c, lw->selected);
739                 return true;
741         case CMD_DELETE:
742                 list_window_get_range(lw, &range);
743                 mpdclient_cmd_delete_range(c, range.start, range.end);
745                 list_window_set_cursor(lw, range.start);
746                 return true;
748         case CMD_SAVE_PLAYLIST:
749                 playlist_save(c, NULL, NULL);
750                 return true;
752         case CMD_ADD:
753                 handle_add_to_playlist(c);
754                 return true;
756         case CMD_SHUFFLE:
757                 list_window_get_range(lw, &range);
758                 if (range.end < range.start + 1)
759                         /* No range selection, shuffle all list. */
760                         break;
762                 connection = mpdclient_get_connection(c);
763                 if (mpd_run_shuffle_range(connection, range.start, range.end))
764                         screen_status_message(_("Shuffled playlist"));
765                 else
766                         mpdclient_handle_error(c);
767                 return true;
769         case CMD_LIST_MOVE_UP:
770                 list_window_get_range(lw, &range);
771                 if (range.start == 0 || range.end <= range.start)
772                         return false;
774                 for (unsigned i = range.start; i < range.end; ++i)
775                         mpdclient_cmd_move(c, i, i - 1);
777                 lw->selected--;
778                 lw->range_base--;
780                 screen_queue_save_selection();
781                 return true;
783         case CMD_LIST_MOVE_DOWN:
784                 list_window_get_range(lw, &range);
785                 if (range.end >= playlist_length(&c->playlist))
786                         return false;
788                 for (int i = range.end - 1; i >= (int)range.start; --i)
789                         mpdclient_cmd_move(c, i, i + 1);
791                 lw->selected++;
792                 lw->range_base++;
794                 screen_queue_save_selection();
795                 return true;
797         case CMD_LOCATE:
798                 if (screen_queue_selected_song() != NULL) {
799                         screen_file_goto_song(c, screen_queue_selected_song());
800                         return true;
801                 }
803                 break;
805         default:
806                 break;
807         }
809         return false;
812 const struct screen_functions screen_queue = {
813         .init = screen_queue_init,
814         .exit = screen_queue_exit,
815         .open = screen_queue_open,
816         .close = screen_queue_close,
817         .resize = screen_queue_resize,
818         .paint = screen_queue_paint,
819         .update = screen_queue_update,
820         .cmd = screen_queue_cmd,
821         .get_title = screen_queue_title,
822 };