Code

Major cleanup of the mpd client code (mpc->mpdclient)
[ncmpc.git] / src / screen_play.c
1 /* 
2  * $Id$
3  *
4  * (c) 2004 by Kalle Wallin <kaw@linux.se>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
21 #include <stdlib.h>
22 #include <string.h>
23 #include <glib.h>
24 #include <ncurses.h>
26 #include "config.h"
27 #include "ncmpc.h"
28 #include "options.h"
29 #include "support.h"
30 #include "mpdclient.h"
31 #include "strfsong.h"
32 #include "command.h"
33 #include "screen.h"
34 #include "screen_utils.h"
36 #define MAX_SONG_LENGTH 512
38 static list_window_t *lw = NULL;
40 static void 
41 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
42 {
43   D("screen_play.c> playlist_callback() [%d]\n", event);
44   switch(event)
45     {
46     case PLAYLIST_EVENT_DELETE:
47       break;
48     case PLAYLIST_EVENT_MOVE:
49       lw->selected = *((int *) data);
50       break;
51     default:
52       break;
53     }
54   /* make shure the playlist is repainted */
55   lw->clear = 1;
56   lw->repaint = 1;
57   list_window_check_selected(lw, c->playlist.length);
58 }
60 static char *
61 list_callback(int index, int *highlight, void *data)
62 {
63   static char songname[MAX_SONG_LENGTH];
64   mpdclient_t *c = (mpdclient_t *) data;
65   mpd_Song *song;
67   *highlight = 0;
68   if( (song=playlist_get_song(c, index)) == NULL )
69     {
70       return NULL;
71     }
73   if( c->song && song->id==c->song->id && !IS_STOPPED(c->status->state) )
74     {
75       *highlight = 1;
76     }
77   strfsong(songname, MAX_SONG_LENGTH, LIST_FORMAT, song);
78   return songname;
79 }
81 static int
82 center_playing_item(screen_t *screen, mpdclient_t *c)
83 {
84   int length = c->playlist.length;
85   int offset = lw->selected-lw->start;
86   int index;
87   
88   if( !lw || !c->song || length<lw->rows || IS_STOPPED(c->status->state) )
89     return 0;
91   /* try to center the song that are playing */
92   index = playlist_get_index(c, c->song);
93   D("Autocenter song id:%d pos:%d index:%d\n", c->song->id,c->song->pos,index);
94   lw->start = index-(lw->rows/2);
95   if( lw->start+lw->rows > length )
96     lw->start = length-lw->rows;
97   if( lw->start<0 )
98     lw->start=0;
100   /* make sure the cursor is in the window */
101   lw->selected = lw->start+offset;
102   list_window_check_selected(lw, length);
104   lw->clear = 1;
105   lw->repaint = 1;
107   return 0;
110 static int
111 handle_save_playlist(screen_t *screen, mpdclient_t *c)
113   char *filename;
115   filename=screen_getstr(screen->status_window.w, _("Save playlist as: "));
116   filename=trim(filename);
117   if( filename==NULL || filename[0]=='\0' )
118     return -1;
119   /* send save command to mpd */
120   if( mpdclient_cmd_save_playlist(c, filename) )
121     {
122       beep();
123       return -1;
124     }
125   /* success */
126   screen_status_printf(_("Saved %s"), filename);
127   g_free(filename);
128   return 0;
131 static void
132 play_init(WINDOW *w, int cols, int rows)
134   lw = list_window_init(w, cols, rows);
137 static void
138 play_open(screen_t *screen, mpdclient_t *c)
140   static gboolean install_cb = TRUE;
142   if( install_cb )
143     {
144       mpdclient_install_playlist_callback(c, playlist_changed_callback);
145       install_cb = FALSE;
146     }
149 static void
150 play_resize(int cols, int rows)
152   lw->cols = cols;
153   lw->rows = rows;
157 static void
158 play_exit(void)
160   list_window_free(lw);
163 static char *
164 play_title(char *str, size_t size)
166   if( strcmp(options.host, "localhost") == 0 )
167     return _("Playlist");
168   
169   snprintf(str, size, _("Playlist on %s"), options.host);
171   return str;
174 static void
175 play_paint(screen_t *screen, mpdclient_t *c)
176
177   lw->clear = 1;
179   list_window_paint(lw, list_callback, (void *) c);
180   wnoutrefresh(lw->w);
183 static void
184 play_update(screen_t *screen, mpdclient_t *c)
186   if( options.auto_center )
187     {
188       static int prev_song_id = 0;
189       
190       if( c->song && prev_song_id != c->song->id )      
191         {
192           center_playing_item(screen, c);
193           prev_song_id = c->song->id;
194         }
195     }
197   if( c->playlist.updated )
198     {
199       if( lw->selected >= c->playlist.length )
200         lw->selected = c->playlist.length-1;
201       if( lw->start    >= c->playlist.length )
202         list_window_reset(lw);
204       play_paint(screen, c);
205       c->playlist.updated = FALSE;
206     }
207   else if( lw->repaint || 1)
208     {
209       list_window_paint(lw, list_callback, (void *) c);
210       wnoutrefresh(lw->w);
211       lw->repaint = 0;
212     }
215 static int
216 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
218   switch(cmd)
219     {
220     case CMD_PLAY:
221       mpdclient_cmd_play(c, lw->selected);
222       break;
223     case CMD_DELETE:
224       mpdclient_cmd_delete(c, lw->selected);
225       return 1;
226     case CMD_SAVE_PLAYLIST:
227       handle_save_playlist(screen, c);
228       return 1;
229     case CMD_SCREEN_UPDATE:
230       center_playing_item(screen, c);
231       return 1;
232     case CMD_LIST_MOVE_UP:
233       mpdclient_cmd_move(c, lw->selected, lw->selected-1);
234       break;
235     case CMD_LIST_MOVE_DOWN:
236       mpdclient_cmd_move(c, lw->selected, lw->selected+1);
237       break;
238     case CMD_LIST_FIND:
239     case CMD_LIST_RFIND:
240     case CMD_LIST_FIND_NEXT:
241     case CMD_LIST_RFIND_NEXT:
242       return screen_find(screen, c, 
243                          lw, c->playlist.length,
244                          cmd, list_callback);
245     default:
246       break;
247     }
248   return list_window_cmd(lw, c->playlist.length, cmd) ;
253 static list_window_t *
254 play_lw(void)
256   return lw;
260 screen_functions_t *
261 get_screen_playlist(void)
263   static screen_functions_t functions;
265   memset(&functions, 0, sizeof(screen_functions_t));
266   functions.init   = play_init;
267   functions.exit   = play_exit;
268   functions.open   = play_open;
269   functions.close  = NULL;
270   functions.resize = play_resize;
271   functions.paint  = play_paint;
272   functions.update = play_update;
273   functions.cmd    = play_cmd;
274   functions.get_lw = play_lw;
275   functions.get_title = play_title;
277   return &functions;