Code

Rename variable sun as it is predefined (to 1) on solaris
[ncmpc.git] / src / screen_play.c
1 /*
2  * (c) 2004 by Kalle Wallin <kaw@linux.se>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  */
19 #include "config.h"
20 #include "i18n.h"
21 #include "charset.h"
22 #include "options.h"
23 #include "mpdclient.h"
24 #include "utils.h"
25 #include "strfsong.h"
26 #include "wreadln.h"
27 #include "command.h"
28 #include "colors.h"
29 #include "screen.h"
30 #include "screen_utils.h"
31 #include "screen_play.h"
33 #ifndef NCMPC_MINI
34 #include "hscroll.h"
35 #endif
37 #include <ctype.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <time.h>
41 #include <glib.h>
43 #define MAX_SONG_LENGTH 512
45 #ifndef NCMPC_MINI
46 typedef struct
47 {
48         GList **list;
49         GList **dir_list;
50         mpdclient_t *c;
51 } completion_callback_data_t;
52 #endif
54 static struct mpdclient_playlist *playlist;
55 static int current_song_id = -1;
56 static list_window_t *lw = NULL;
57 static guint timer_hide_cursor_id;
59 static void
60 play_paint(void);
62 static void
63 playlist_repaint(void)
64 {
65         play_paint();
66         wrefresh(lw->w);
67 }
69 static void
70 playlist_repaint_if_active(void)
71 {
72         if (screen_is_visible(&screen_playlist))
73                 playlist_repaint();
74 }
76 static void
77 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
78 {
79         switch (event) {
80         case PLAYLIST_EVENT_DELETE:
81                 break;
82         case PLAYLIST_EVENT_MOVE:
83                 lw->selected = *((int *) data);
84                 if (lw->selected < lw->start)
85                         lw->start--;
86                 break;
87         default:
88                 break;
89         }
91         list_window_check_selected(lw, c->playlist.list->len);
92         playlist_repaint_if_active();
93 }
95 static const char *
96 list_callback(unsigned idx, bool *highlight, G_GNUC_UNUSED void *data)
97 {
98         static char songname[MAX_SONG_LENGTH];
99 #ifndef NCMPC_MINI
100         static scroll_state_t st;
101 #endif
102         mpd_Song *song;
104         if (playlist == NULL || idx >= playlist_length(playlist))
105                 return NULL;
107         song = playlist_get(playlist, idx);
108         if (song->id == current_song_id)
109                 *highlight = true;
111         strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
113 #ifndef NCMPC_MINI
114         if (options.scroll && (unsigned)song->pos == lw->selected &&
115             utf8_width(songname) > (unsigned)COLS) {
116                 static unsigned current_song;
117                 char *tmp;
119                 if (current_song != lw->selected) {
120                         st.offset = 0;
121                         current_song = lw->selected;
122                 }
124                 tmp = strscroll(songname, options.scroll_sep,
125                                 MAX_SONG_LENGTH, &st);
126                 g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
127                 g_free(tmp);
128         } else if ((unsigned)song->pos == lw->selected)
129                 st.offset = 0;
130 #endif
132         return songname;
135 static void
136 center_playing_item(mpdclient_t *c)
138         unsigned length = c->playlist.list->len;
139         unsigned offset = lw->selected - lw->start;
140         int idx;
142         if (!c->song || length < lw->rows ||
143             c->status == NULL || IS_STOPPED(c->status->state))
144                 return;
146         /* try to center the song that are playing */
147         idx = playlist_get_index(c, c->song);
148         if (idx < 0)
149                 return;
151         list_window_center(lw, length, idx);
153         /* make sure the cursor is in the window */
154         lw->selected = lw->start+offset;
155         list_window_check_selected(lw, length);
158 #ifndef NCMPC_MINI
159 static void
160 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
161                        void *data)
163         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
164         GList **list = tmp->list;
165         mpdclient_t *c = tmp->c;
167         if( *list == NULL ) {
168                 /* create completion list */
169                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
170                 g_completion_add_items(gcmp, *list);
171         }
174 static void
175 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
176                         G_GNUC_UNUSED gchar *line, GList *items,
177                         G_GNUC_UNUSED void *data)
179         if (g_list_length(items) >= 1)
180                 screen_display_completion_list(items);
182 #endif
184 #ifndef NCMPC_MINI
185 /**
186  * Wrapper for strncmp().  We are not allowed to pass &strncmp to
187  * g_completion_set_compare(), because strncmp() takes size_t where
188  * g_completion_set_compare passes a gsize value.
189  */
190 static gint
191 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
193         return strncmp(s1, s2, n);
195 #endif
197 int
198 playlist_save(mpdclient_t *c, char *name, char *defaultname)
200         gchar *filename, *filename_utf8;
201         gint error;
202 #ifndef NCMPC_MINI
203         GCompletion *gcmp;
204         GList *list = NULL;
205         completion_callback_data_t data;
206 #endif
208 #ifdef NCMPC_MINI
209         (void)defaultname;
210 #endif
212 #ifndef NCMPC_MINI
213         if (name == NULL) {
214                 /* initialize completion support */
215                 gcmp = g_completion_new(NULL);
216                 g_completion_set_compare(gcmp, completion_strncmp);
217                 data.list = &list;
218                 data.dir_list = NULL;
219                 data.c = c;
220                 wrln_completion_callback_data = &data;
221                 wrln_pre_completion_callback = save_pre_completion_cb;
222                 wrln_post_completion_callback = save_post_completion_cb;
225                 /* query the user for a filename */
226                 filename = screen_readln(screen.status_window.w,
227                                          _("Save playlist as: "),
228                                          defaultname,
229                                          NULL,
230                                          gcmp);
232                 /* destroy completion support */
233                 wrln_completion_callback_data = NULL;
234                 wrln_pre_completion_callback = NULL;
235                 wrln_post_completion_callback = NULL;
236                 g_completion_free(gcmp);
237                 list = string_list_free(list);
238                 if( filename )
239                         filename=g_strstrip(filename);
240         } else
241 #endif
242                         filename=g_strdup(name);
244         if (filename == NULL)
245                 return -1;
247         /* send save command to mpd */
249         filename_utf8 = locale_to_utf8(filename);
250         error = mpdclient_cmd_save_playlist(c, filename_utf8);
251         g_free(filename_utf8);
253         if (error) {
254                 gint code = GET_ACK_ERROR_CODE(error);
256                 if (code == MPD_ACK_ERROR_EXIST) {
257                         char *buf;
258                         int key;
260                         buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
261                                               filename, YES, NO);
262                         key = tolower(screen_getch(screen.status_window.w,
263                                                    buf));
264                         g_free(buf);
266                         if (key == YES[0]) {
267                                 filename_utf8 = locale_to_utf8(filename);
268                                 error = mpdclient_cmd_delete_playlist(c, filename_utf8);
269                                 g_free(filename_utf8);
271                                 if (error) {
272                                         g_free(filename);
273                                         return -1;
274                                 }
276                                 error = playlist_save(c, filename, NULL);
277                                 g_free(filename);
278                                 return error;
279                         }
281                         screen_status_printf(_("Aborted"));
282                 }
284                 g_free(filename);
285                 return -1;
286         }
288         /* success */
289         screen_status_printf(_("Saved %s"), filename);
290         g_free(filename);
291         return 0;
294 #ifndef NCMPC_MINI
295 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
296                     GList **list, mpdclient_t *c)
298         g_completion_remove_items(gcmp, *list);
299         *list = string_list_remove(*list, dir);
300         *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
301         g_completion_add_items(gcmp, *list);
302         *dir_list = g_list_append(*dir_list, g_strdup(dir));
305 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
307         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
308         GList **dir_list = tmp->dir_list;
309         GList **list = tmp->list;
310         mpdclient_t *c = tmp->c;
312         if (*list == NULL) {
313                 /* create initial list */
314                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
315                 g_completion_add_items(gcmp, *list);
316         } else if (line && line[0] && line[strlen(line)-1]=='/' &&
317                    string_list_find(*dir_list, line) == NULL) {
318                 /* add directory content to list */
319                 add_dir(gcmp, line, dir_list, list, c);
320         }
323 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
324                                    GList *items, void *data)
326         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
327         GList **dir_list = tmp->dir_list;
328         GList **list = tmp->list;
329         mpdclient_t *c = tmp->c;
331         if (g_list_length(items) >= 1)
332                 screen_display_completion_list(items);
334         if (line && line[0] && line[strlen(line) - 1] == '/' &&
335             string_list_find(*dir_list, line) == NULL) {
336                 /* add directory content to list */
337                 add_dir(gcmp, line, dir_list, list, c);
338         }
340 #endif
342 static int
343 handle_add_to_playlist(mpdclient_t *c)
345         gchar *path;
346 #ifndef NCMPC_MINI
347         GCompletion *gcmp;
348         GList *list = NULL;
349         GList *dir_list = NULL;
350         completion_callback_data_t data;
352         /* initialize completion support */
353         gcmp = g_completion_new(NULL);
354         g_completion_set_compare(gcmp, completion_strncmp);
355         data.list = &list;
356         data.dir_list = &dir_list;
357         data.c = c;
358         wrln_completion_callback_data = &data;
359         wrln_pre_completion_callback = add_pre_completion_cb;
360         wrln_post_completion_callback = add_post_completion_cb;
361 #endif
363         /* get path */
364         path = screen_readln(screen.status_window.w,
365                              _("Add: "),
366                              NULL,
367                              NULL,
368 #ifdef NCMPC_MINI
369                              NULL
370 #else
371                              gcmp
372 #endif
373                              );
375         /* destroy completion data */
376 #ifndef NCMPC_MINI
377         wrln_completion_callback_data = NULL;
378         wrln_pre_completion_callback = NULL;
379         wrln_post_completion_callback = NULL;
380         g_completion_free(gcmp);
381         string_list_free(list);
382         string_list_free(dir_list);
383 #endif
385         /* add the path to the playlist */
386         if (path != NULL) {
387                 char *path_utf8 = locale_to_utf8(path);
388                 mpdclient_cmd_add_path(c, path_utf8);
389                 g_free(path_utf8);
390         }
392         g_free(path);
393         return 0;
396 static void
397 play_init(WINDOW *w, int cols, int rows)
399         lw = list_window_init(w, cols, rows);
402 static gboolean
403 timer_hide_cursor(gpointer data)
405         struct mpdclient *c = data;
407         assert(options.hide_cursor > 0);
408         assert(timer_hide_cursor_id != 0);
410         timer_hide_cursor_id = 0;
412         /* hide the cursor when mpd is playing and the user is inactive */
414         if (c->status != NULL && c->status->state == MPD_STATUS_STATE_PLAY) {
415                 lw->hide_cursor = true;
416                 playlist_repaint();
417         } else
418                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
419                                                      timer_hide_cursor, c);
421         return FALSE;
424 static void
425 play_open(mpdclient_t *c)
427         static gboolean install_cb = TRUE;
429         playlist = &c->playlist;
431         assert(timer_hide_cursor_id == 0);
432         if (options.hide_cursor > 0) {
433                 lw->hide_cursor = false;
434                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
435                                                      timer_hide_cursor, c);
436         }
438         if (install_cb) {
439                 mpdclient_install_playlist_callback(c, playlist_changed_callback);
440                 install_cb = FALSE;
441         }
444 static void
445 play_close(void)
447         if (timer_hide_cursor_id != 0) {
448                 g_source_remove(timer_hide_cursor_id);
449                 timer_hide_cursor_id = 0;
450         }
453 static void
454 play_resize(int cols, int rows)
456         lw->cols = cols;
457         lw->rows = rows;
461 static void
462 play_exit(void)
464         list_window_free(lw);
467 static const char *
468 play_title(char *str, size_t size)
470         if( strcmp(options.host, "localhost") == 0 )
471                 return _("Playlist");
473         g_snprintf(str, size, _("Playlist on %s"), options.host);
474         return str;
477 static void
478 play_paint(void)
480         list_window_paint(lw, list_callback, NULL);
483 static void
484 play_update(mpdclient_t *c)
486         static int prev_song_id = -1;
488         current_song_id = c->song != NULL && c->status != NULL &&
489                 !IS_STOPPED(c->status->state) ? c->song->id : -1;
491         if (current_song_id != prev_song_id) {
492                 prev_song_id = current_song_id;
494                 /* center the cursor */
495                 if (options.auto_center && current_song_id != -1)
496                         center_playing_item(c);
498                 playlist_repaint();
499 #ifndef NCMPC_MINI
500         } else if (options.scroll) {
501                 /* always repaint if horizontal scrolling is
502                    enabled */
503                 playlist_repaint();
504 #endif
505         }
508 #ifdef HAVE_GETMOUSE
509 static bool
510 handle_mouse_event(struct mpdclient *c)
512         int row;
513         unsigned selected;
514         unsigned long bstate;
516         if (screen_get_mouse_event(c, &bstate, &row) ||
517             list_window_mouse(lw, playlist_length(playlist), bstate, row)) {
518                 playlist_repaint();
519                 return true;
520         }
522         if (bstate & BUTTON1_DOUBLE_CLICKED) {
523                 /* stop */
524                 screen_cmd(c, CMD_STOP);
525                 return true;
526         }
528         selected = lw->start + row;
530         if (bstate & BUTTON1_CLICKED) {
531                 /* play */
532                 if (lw->start + row < playlist_length(playlist))
533                         mpdclient_cmd_play(c, lw->start + row);
534         } else if (bstate & BUTTON3_CLICKED) {
535                 /* delete */
536                 if (selected == lw->selected)
537                         mpdclient_cmd_delete(c, lw->selected);
538         }
540         lw->selected = selected;
541         list_window_check_selected(lw, playlist_length(playlist));
542         playlist_repaint();
544         return true;
546 #endif
548 static bool
549 play_cmd(mpdclient_t *c, command_t cmd)
551         lw->hide_cursor = false;
553         if (options.hide_cursor > 0) {
554                 if (timer_hide_cursor_id != 0)
555                         g_source_remove(timer_hide_cursor_id);
556                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
557                                                      timer_hide_cursor, c);
558         }
560         if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
561                 playlist_repaint();
562                 return true;
563         }
565         switch(cmd) {
566         case CMD_PLAY:
567                 mpdclient_cmd_play(c, lw->selected);
568                 return true;
569         case CMD_DELETE:
570                 mpdclient_cmd_delete(c, lw->selected);
571                 return true;
572         case CMD_SAVE_PLAYLIST:
573                 playlist_save(c, NULL, NULL);
574                 return true;
575         case CMD_ADD:
576                 handle_add_to_playlist(c);
577                 return true;
578         case CMD_SCREEN_UPDATE:
579                 center_playing_item(c);
580                 playlist_repaint();
581                 return false;
583         case CMD_LIST_MOVE_UP:
584                 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
585                 return true;
586         case CMD_LIST_MOVE_DOWN:
587                 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
588                 return true;
589         case CMD_LIST_FIND:
590         case CMD_LIST_RFIND:
591         case CMD_LIST_FIND_NEXT:
592         case CMD_LIST_RFIND_NEXT:
593                 screen_find(lw, playlist_length(&c->playlist),
594                             cmd, list_callback, NULL);
595                 playlist_repaint();
596                 return true;
598 #ifdef HAVE_GETMOUSE
599         case CMD_MOUSE_EVENT:
600                 return handle_mouse_event(c);
601 #endif
603 #ifdef ENABLE_SONG_SCREEN
604         case CMD_VIEW:
605                 if (lw->selected < playlist_length(&c->playlist)) {
606                         screen_song_switch(c, playlist_get(&c->playlist, lw->selected));
607                         return true;
608                 }
610                 break;
611 #endif
613         case CMD_LOCATE:
614                 if (lw->selected < playlist_length(&c->playlist)) {
615                         screen_file_goto_song(c, playlist_get(&c->playlist, lw->selected));
616                         return true;
617                 }
619                 break;
621 #ifdef ENABLE_LYRICS_SCREEN
622         case CMD_SCREEN_LYRICS:
623                 if (lw->selected < playlist_length(&c->playlist)) {
624                         screen_lyrics_switch(c, playlist_get(&c->playlist, lw->selected));
625                         return true;
626                 }
628                 break;
629 #endif
631         default:
632                 break;
633         }
635         return false;
638 const struct screen_functions screen_playlist = {
639         .init = play_init,
640         .exit = play_exit,
641         .open = play_open,
642         .close = play_close,
643         .resize = play_resize,
644         .paint = play_paint,
645         .update = play_update,
646         .cmd = play_cmd,
647         .get_title = play_title,
648 };