Code

Documentation update.
[ncmpc.git] / 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 <stdlib.h>
20 #include <string.h>
21 #include <glib.h>
22 #include <ncurses.h>
24 #include "config.h"
25 #include "options.h"
26 #include "support.h"
27 #include "libmpdclient.h"
28 #include "mpc.h"
29 #include "command.h"
30 #include "screen.h"
31 #include "screen_utils.h"
32 #include "screen_file.h"
33 #include "screen_play.h"
35 #define BUFSIZE 256
37 #define ENABLE_FANCY_PLAYLIST_MANAGMENT
39 static list_window_t *lw = NULL;
41 static char *
42 list_callback(int index, int *highlight, void *data)
43 {
44   mpd_client_t *c = (mpd_client_t *) data;
45   mpd_Song *song;
47   *highlight = 0;
48   if( (song=mpc_playlist_get_song(c, index)) == NULL )
49     {
50       return NULL;
51     }
53   if( index==c->song_id && !IS_STOPPED(c->status->state) )
54     {
55       *highlight = 1;
56     }
58   return mpc_get_song_name(song);
59 }
61 static int
62 center_playing_item(screen_t *screen, mpd_client_t *c)
63 {
64   int length = c->playlist_length;
65   int offset = lw->selected-lw->start;
66   
67   if( !lw || length<lw->rows || IS_STOPPED(c->status->state) )
68     return 0;
70   /* try to center the song that are playing */
71   lw->start = c->song_id-(lw->rows/2);
72   if( lw->start+lw->rows > length )
73     lw->start = length-lw->rows;
74   if( lw->start<0 )
75     lw->start=0;
77   /* make sure the cursor is in the window */
78   lw->selected = lw->start+offset;
79   list_window_check_selected(lw, length);
81   lw->clear = 1;
82   lw->repaint = 1;
84   return 0;
85 }
87 static int
88 handle_save_playlist(screen_t *screen, mpd_client_t *c)
89 {
90   char *filename, *filename_utf8;
92   filename=screen_getstr(screen->status_window.w, "Save playlist as: ");
93   filename=trim(filename);
94   if( filename==NULL || filename[0]=='\0' )
95     return -1;
96   /* convert filename to utf-8 */
97   filename_utf8 = locale_to_utf8(filename);
98   /* send save command to mpd */
99   mpd_sendSaveCommand(c->connection, filename_utf8);
100   mpd_finishCommand(c->connection);
101   g_free(filename_utf8);
102   /* handle errors */
103   if( mpc_error(c))
104     {
105       if(  mpc_error_str(c) )
106         {
107           char *str = utf8_to_locale(mpc_error_str(c));
108           screen_status_message(str);
109           g_free(str);
110         }
111       else
112         screen_status_printf("Error: Unable to save playlist as %s", filename);
113       mpd_clearError(c->connection);
114       beep();
115       return -1;
116     }
117   /* success */
118   screen_status_printf("Saved %s", filename);
119   g_free(filename);
120   /* update the file list if it has been initalized */
121   if( c->filelist )
122     {
123       list_window_t *file_lw = get_filelist_window();
125       mpc_update_filelist(c);
126       list_window_check_selected(file_lw, c->filelist_length);
127     }
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_resize(int cols, int rows)
140   lw->cols = cols;
141   lw->rows = rows;
145 static void
146 play_exit(void)
148   list_window_free(lw);
151 static char *
152 play_title(void)
154   return (TOP_HEADER_PREFIX "Playlist");
157 static void
158 play_paint(screen_t *screen, mpd_client_t *c)
159
160   lw->clear = 1;
162   list_window_paint(lw, list_callback, (void *) c);
163   wnoutrefresh(lw->w);
166 static void
167 play_update(screen_t *screen, mpd_client_t *c)
169   if( options.auto_center )
170     {
171       static int prev_song_id = 0;
172       
173       if( prev_song_id != c->song_id )  
174         {
175           center_playing_item(screen, c);
176           prev_song_id = c->song_id;
177         }
178     }
180   if( c->playlist_updated )
181     {
182       if( lw->selected >= c->playlist_length )
183         lw->selected = c->playlist_length-1;
184       if( lw->start    >= c->playlist_length )
185         list_window_reset(lw);
187       play_paint(screen, c);
188       c->playlist_updated = 0;
189     }
190   else if( lw->repaint || 1)
191     {
192       list_window_paint(lw, list_callback, (void *) c);
193       wnoutrefresh(lw->w);
194       lw->repaint = 0;
195     }
198 static int
199 play_cmd(screen_t *screen, mpd_client_t *c, command_t cmd)
201   switch(cmd)
202     {
203     case CMD_DELETE:
204       playlist_delete_song(c, lw->selected);
205       return 1;
206     case CMD_SAVE_PLAYLIST:
207       handle_save_playlist(screen, c);
208       return 1;
209     case CMD_SCREEN_UPDATE:
210       center_playing_item(screen, c);
211       return 1;
212     case CMD_LIST_FIND:
213     case CMD_LIST_RFIND:
214     case CMD_LIST_FIND_NEXT:
215     case CMD_LIST_RFIND_NEXT:
216       return screen_find(screen, c, 
217                          lw, c->playlist_length,
218                          cmd, list_callback);
219     default:
220       break;
221     }
222   return list_window_cmd(lw, c->playlist_length, cmd) ;
227 static list_window_t *
228 play_lw(void)
230   return lw;
233 int 
234 play_get_selected(void)
236   return lw->selected;
239 int
240 playlist_add_song(mpd_client_t *c, mpd_Song *song)
242   if( !song || !song->file )
243     return -1;
245   /* send the add command to mpd */
246   mpd_sendAddCommand(c->connection, song->file);
247   mpd_finishCommand(c->connection);
248   if( mpc_error(c) )
249     return -1;
251 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT
252   /* add the song to playlist */
253   c->playlist = g_list_append(c->playlist, (gpointer) mpd_songDup(song));
254   c->playlist_length++;
256   /* increment the playlist id, so we dont retrives a new playlist */
257   c->playlist_id++;
259   /* make shure the playlist is repainted */
260   lw->clear = 1;
261   lw->repaint = 1;
262 #endif
264   /* set selected highlight in the browse screen */
265   file_set_highlight(c, song, 1);
267   return 0;
270 int
271 playlist_delete_song(mpd_client_t *c, int index)
273   mpd_Song *song = mpc_playlist_get_song(c, index);
275   if( !song )
276     return -1;
278   /* send the delete command to mpd */
279   mpd_sendDeleteCommand(c->connection, index);
280   mpd_finishCommand(c->connection); 
281   if( mpc_error(c) )
282     return -1;
284   /* print a status message */
285   screen_status_printf("Removed \'%s\' from playlist!",
286                        mpc_get_song_name(song));
287   /* clear selected highlight in the browse screen */
288   file_set_highlight(c, song, 0);
290 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT 
291   /* increment the playlist id, so we dont retrives a new playlist */
292   c->playlist_id++;
294   /* remove references to the song */
295   if( c->song == song )
296     {
297       c->song = NULL;
298       c->song_id = -1;
299     }
300   
301   /* remove the song from the playlist */
302   c->playlist = g_list_remove(c->playlist, (gpointer) song);
303   c->playlist_length = g_list_length(c->playlist);
304   mpd_freeSong(song);
306   /* make shure the playlist is repainted */
307   lw->clear = 1;
308   lw->repaint = 1;
309   list_window_check_selected(lw, c->playlist_length);
310 #endif
312   return 0;
316 screen_functions_t *
317 get_screen_playlist(void)
319   static screen_functions_t functions;
321   memset(&functions, 0, sizeof(screen_functions_t));
322   functions.init   = play_init;
323   functions.exit   = play_exit;
324   functions.open   = NULL;
325   functions.close  = NULL;
326   functions.resize = play_resize;
327   functions.paint  = play_paint;
328   functions.update = play_update;
329   functions.cmd    = play_cmd;
330   functions.get_lw = play_lw;
331   functions.get_title = play_title;
333   return &functions;