1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
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.
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_browser.h"
21 #include "i18n.h"
22 #include "options.h"
23 #include "charset.h"
24 #include "strfsong.h"
25 #include "screen_utils.h"
27 #include <string.h>
29 #define BUFSIZE 1024
31 #ifndef NCMPC_MINI
32 #define HIGHLIGHT (0x01)
33 #endif
35 static const char playlist_format[] = "*%s*";
37 #ifndef NCMPC_MINI
39 /* clear the highlight flag for all items in the filelist */
40 static void
41 clear_highlights(mpdclient_filelist_t *fl)
42 {
43 guint i;
45 for (i = 0; i < filelist_length(fl); ++i) {
46 struct filelist_entry *entry = filelist_get(fl, i);
48 entry->flags &= ~HIGHLIGHT;
49 }
50 }
52 /* change the highlight flag for a song */
53 static void
54 set_highlight(mpdclient_filelist_t *fl, mpd_Song *song, int highlight)
55 {
56 int i = filelist_find_song(fl, song);
57 struct filelist_entry *entry;
59 if (i < 0)
60 return;
62 entry = filelist_get(fl, i);
63 if (highlight)
64 entry->flags |= HIGHLIGHT;
65 else
66 entry->flags &= ~HIGHLIGHT;
67 }
69 /* sync highlight flags with playlist */
70 void
71 sync_highlights(mpdclient_t *c, mpdclient_filelist_t *fl)
72 {
73 guint i;
75 for (i = 0; i < filelist_length(fl); ++i) {
76 struct filelist_entry *entry = filelist_get(fl, i);
77 mpd_InfoEntity *entity = entry->entity;
79 if ( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
80 mpd_Song *song = entity->info.song;
82 if( playlist_get_index_from_file(c, song->file) >= 0 )
83 entry->flags |= HIGHLIGHT;
84 else
85 entry->flags &= ~HIGHLIGHT;
86 }
87 }
88 }
90 /* the playlist has been updated -> fix highlights */
91 void
92 browser_playlist_changed(struct screen_browser *browser, mpdclient_t *c,
93 int event, gpointer data)
94 {
95 if (browser->filelist == NULL)
96 return;
98 switch(event) {
99 case PLAYLIST_EVENT_CLEAR:
100 clear_highlights(browser->filelist);
101 break;
102 case PLAYLIST_EVENT_ADD:
103 set_highlight(browser->filelist, (mpd_Song *) data, 1);
104 break;
105 case PLAYLIST_EVENT_DELETE:
106 set_highlight(browser->filelist, (mpd_Song *) data, 0);
107 break;
108 case PLAYLIST_EVENT_MOVE:
109 break;
110 default:
111 sync_highlights(c, browser->filelist);
112 break;
113 }
114 }
116 #endif
118 /* list_window callback */
119 const char *
120 browser_lw_callback(unsigned idx, bool *highlight, void *data)
121 {
122 static char buf[BUFSIZE];
123 mpdclient_filelist_t *fl = (mpdclient_filelist_t *) data;
124 filelist_entry_t *entry;
125 mpd_InfoEntity *entity;
127 if (idx >= filelist_length(fl))
128 return NULL;
130 entry = filelist_get(fl, idx);
131 assert(entry != NULL);
133 entity = entry->entity;
134 #ifndef NCMPC_MINI
135 *highlight = (entry->flags & HIGHLIGHT) != 0;
136 #else
137 *highlight = false;
138 #endif
140 if( entity == NULL )
141 return "[..]";
143 if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) {
144 mpd_Directory *dir = entity->info.directory;
145 char *directory = utf8_to_locale(g_basename(dir->path));
147 g_snprintf(buf, BUFSIZE, "[%s]", directory);
148 g_free(directory);
149 return buf;
150 } else if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
151 mpd_Song *song = entity->info.song;
153 strfsong(buf, BUFSIZE, options.list_format, song);
154 return buf;
155 } else if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
156 mpd_PlaylistFile *plf = entity->info.playlistFile;
157 char *filename = utf8_to_locale(g_basename(plf->path));
159 g_snprintf(buf, BUFSIZE, playlist_format, filename);
160 g_free(filename);
161 return buf;
162 }
164 return "Error: Unknown entry!";
165 }
167 /* chdir */
168 bool
169 browser_change_directory(struct screen_browser *browser, mpdclient_t *c,
170 filelist_entry_t *entry, const char *new_path)
171 {
172 mpd_InfoEntity *entity = NULL;
173 gchar *path = NULL;
174 char *old_path;
175 int idx;
177 if( entry!=NULL )
178 entity = entry->entity;
179 else if( new_path==NULL )
180 return false;
182 if( entity==NULL ) {
183 if( entry || 0==strcmp(new_path, "..") ) {
184 /* return to parent */
185 char *parent = g_path_get_dirname(browser->filelist->path);
186 if( strcmp(parent, ".") == 0 )
187 parent[0] = '\0';
188 path = g_strdup(parent);
189 g_free(parent);
190 } else {
191 /* entry==NULL, then new_path ("" is root) */
192 path = g_strdup(new_path);
193 }
194 } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY) {
195 /* enter sub */
196 mpd_Directory *dir = entity->info.directory;
197 path = g_strdup(dir->path);
198 } else
199 return false;
201 if (browser->filelist != NULL) {
202 old_path = g_strdup(browser->filelist->path);
203 filelist_free(browser->filelist);
204 } else
205 old_path = NULL;
207 browser->filelist = mpdclient_filelist_get(c, path);
208 #ifndef NCMPC_MINI
209 sync_highlights(c, browser->filelist);
210 #endif
212 idx = old_path != NULL
213 ? filelist_find_directory(browser->filelist, old_path)
214 : -1;
215 g_free(old_path);
217 list_window_reset(browser->lw);
218 if (idx >= 0) {
219 list_window_set_selected(browser->lw, idx);
220 list_window_center(browser->lw,
221 filelist_length(browser->filelist), idx);
222 }
224 g_free(path);
225 return true;
226 }
228 static bool
229 load_playlist(mpdclient_t *c, filelist_entry_t *entry)
230 {
231 mpd_InfoEntity *entity = entry->entity;
232 mpd_PlaylistFile *plf = entity->info.playlistFile;
233 char *filename = utf8_to_locale(plf->path);
235 if (mpdclient_cmd_load_playlist(c, plf->path) == 0)
236 screen_status_printf(_("Loading playlist %s..."),
237 g_basename(filename));
238 g_free(filename);
239 return true;
240 }
242 static bool
243 enqueue_and_play(mpdclient_t *c, filelist_entry_t *entry)
244 {
245 int idx;
246 mpd_InfoEntity *entity = entry->entity;
247 mpd_Song *song = entity->info.song;
249 #ifndef NCMPC_MINI
250 if (!(entry->flags & HIGHLIGHT)) {
251 #endif
252 if (mpdclient_cmd_add(c, song) == 0) {
253 char buf[BUFSIZE];
255 #ifndef NCMPC_MINI
256 entry->flags |= HIGHLIGHT;
257 #endif
258 strfsong(buf, BUFSIZE, options.list_format, song);
259 screen_status_printf(_("Adding \'%s\' to playlist"), buf);
260 mpdclient_update(c); /* get song id */
261 } else
262 return false;
263 #ifndef NCMPC_MINI
264 }
265 #endif
267 idx = playlist_get_index_from_file(c, song->file);
268 mpdclient_cmd_play(c, idx);
269 return true;
270 }
272 static struct filelist_entry *
273 browser_get_selected(const struct screen_browser *browser)
274 {
275 if (browser->filelist == NULL ||
276 browser->lw->selected >= filelist_length(browser->filelist))
277 return NULL;
279 return filelist_get(browser->filelist, browser->lw->selected);
280 }
282 static bool
283 browser_handle_enter(struct screen_browser *browser, mpdclient_t *c)
284 {
285 struct filelist_entry *entry = browser_get_selected(browser);
286 mpd_InfoEntity *entity;
288 if( entry==NULL )
289 return false;
291 entity = entry->entity;
292 if (entity == NULL || entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY)
293 return browser_change_directory(browser, c, entry, NULL);
294 else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
295 return load_playlist(c, entry);
296 else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
297 return enqueue_and_play(c, entry);
298 return false;
299 }
301 static bool
302 browser_select_entry(mpdclient_t *c, filelist_entry_t *entry,
303 G_GNUC_UNUSED gboolean toggle)
304 {
305 assert(entry != NULL);
306 assert(entry->entity != NULL);
308 if (entry->entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
309 return load_playlist(c, entry);
311 if (entry->entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
312 mpd_Directory *dir = entry->entity->info.directory;
314 if (mpdclient_cmd_add_path(c, dir->path) == 0) {
315 char *tmp = utf8_to_locale(dir->path);
317 screen_status_printf(_("Adding \'%s\' to playlist"), tmp);
318 g_free(tmp);
319 }
321 return true;
322 }
324 if (entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
325 return false;
327 assert(entry->entity->info.song != NULL);
329 #ifndef NCMPC_MINI
330 if (!toggle || (entry->flags & HIGHLIGHT) == 0)
331 #endif
332 {
333 mpd_Song *song = entry->entity->info.song;
335 #ifndef NCMPC_MINI
336 entry->flags |= HIGHLIGHT;
337 #endif
339 if (mpdclient_cmd_add(c, song) == 0) {
340 char buf[BUFSIZE];
342 strfsong(buf, BUFSIZE, options.list_format, song);
343 screen_status_printf(_("Adding \'%s\' to playlist"), buf);
344 }
345 #ifndef NCMPC_MINI
346 } else {
347 /* remove song from playlist */
348 mpd_Song *song = entry->entity->info.song;
349 int idx;
351 entry->flags &= ~HIGHLIGHT;
353 while ((idx = playlist_get_index_from_file(c, song->file)) >=0)
354 mpdclient_cmd_delete(c, idx);
355 #endif
356 }
358 return true;
359 }
361 static bool
362 browser_handle_select(struct screen_browser *browser, mpdclient_t *c)
363 {
364 struct filelist_entry *entry = browser_get_selected(browser);
366 if (entry == NULL || entry->entity == NULL)
367 return false;
369 return browser_select_entry(c, entry, TRUE);
370 }
372 static bool
373 browser_handle_add(struct screen_browser *browser, mpdclient_t *c)
374 {
375 struct filelist_entry *entry = browser_get_selected(browser);
377 if (entry == NULL || entry->entity == NULL)
378 return false;
380 return browser_select_entry(c, entry, FALSE);
381 }
383 static void
384 browser_handle_select_all(struct screen_browser *browser, mpdclient_t *c)
385 {
386 guint i;
388 if (browser->filelist == NULL)
389 return;
391 for (i = 0; i < filelist_length(browser->filelist); ++i) {
392 struct filelist_entry *entry = filelist_get(browser->filelist, i);
394 if (entry != NULL && entry->entity != NULL)
395 browser_select_entry(c, entry, FALSE);
396 }
397 }
399 #ifdef HAVE_GETMOUSE
400 static int
401 browser_handle_mouse_event(struct screen_browser *browser, mpdclient_t *c)
402 {
403 int row;
404 unsigned prev_selected = browser->lw->selected;
405 unsigned long bstate;
406 int length;
408 if (browser->filelist)
409 length = filelist_length(browser->filelist);
410 else
411 length = 0;
413 if (screen_get_mouse_event(c, &bstate, &row) ||
414 list_window_mouse(browser->lw, length, bstate, row))
415 return 1;
417 browser->lw->selected = browser->lw->start + row;
418 list_window_check_selected(browser->lw, length);
420 if( bstate & BUTTON1_CLICKED ) {
421 if (prev_selected == browser->lw->selected)
422 browser_handle_enter(browser, c);
423 } else if (bstate & BUTTON3_CLICKED) {
424 if (prev_selected == browser->lw->selected)
425 browser_handle_select(browser, c);
426 }
428 return 1;
429 }
430 #endif
432 bool
433 browser_cmd(struct screen_browser *browser,
434 struct mpdclient *c, command_t cmd)
435 {
436 struct filelist_entry *entry;
438 switch (cmd) {
439 case CMD_PLAY:
440 browser_handle_enter(browser, c);
441 return true;
443 case CMD_SELECT:
444 if (browser_handle_select(browser, c))
445 /* continue and select next item... */
446 cmd = CMD_LIST_NEXT;
448 /* call list_window_cmd to go to the next item */
449 break;
451 case CMD_ADD:
452 if (browser_handle_add(browser, c))
453 /* continue and select next item... */
454 cmd = CMD_LIST_NEXT;
456 /* call list_window_cmd to go to the next item */
457 break;
459 case CMD_SELECT_ALL:
460 browser_handle_select_all(browser, c);
461 return true;
463 case CMD_LIST_FIND:
464 case CMD_LIST_RFIND:
465 case CMD_LIST_FIND_NEXT:
466 case CMD_LIST_RFIND_NEXT:
467 screen_find(browser->lw, filelist_length(browser->filelist),
468 cmd, browser_lw_callback,
469 browser->filelist);
470 return true;
471 case CMD_LIST_JUMP:
472 screen_jump(browser->lw, browser_lw_callback, browser->filelist);
473 return true;
475 #ifdef HAVE_GETMOUSE
476 case CMD_MOUSE_EVENT:
477 browser_handle_mouse_event(browser, c);
478 return true;
479 #endif
481 #ifdef ENABLE_SONG_SCREEN
482 case CMD_SCREEN_SONG:
483 entry = browser_get_selected(browser);
484 if (entry == NULL || entry->entity == NULL ||
485 entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
486 return false;
488 screen_song_switch(c, entry->entity->info.song);
489 return true;
490 #endif
492 case CMD_LOCATE:
493 entry = browser_get_selected(browser);
494 if (entry == NULL || entry->entity == NULL ||
495 entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
496 return false;
498 screen_file_goto_song(c, entry->entity->info.song);
499 return true;
501 #ifdef ENABLE_LYRICS_SCREEN
502 case CMD_SCREEN_LYRICS:
503 entry = browser_get_selected(browser);
504 if (entry == NULL)
505 return false;
507 if (entry->entity == NULL ||
508 entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
509 return true;
511 screen_lyrics_switch(c, entry->entity->info.song);
512 return true;
513 #endif
515 default:
516 break;
517 }
519 if (list_window_cmd(browser->lw, filelist_length(browser->filelist),
520 cmd))
521 return true;
523 return false;
524 }