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_same_song(c, song) >= 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, G_GNUC_UNUSED char **second_column, 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_same_song(c, song);
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_start < browser->lw->selected_end ||
277 browser->lw->selected >= filelist_length(browser->filelist))
278 return NULL;
280 return filelist_get(browser->filelist, browser->lw->selected);
281 }
283 static struct filelist_entry *
284 browser_get_index(const struct screen_browser *browser, unsigned i)
285 {
286 if (browser->filelist == NULL ||
287 i >= filelist_length(browser->filelist))
288 return NULL;
290 return filelist_get(browser->filelist, i);
291 }
293 static bool
294 browser_handle_enter(struct screen_browser *browser, mpdclient_t *c)
295 {
296 struct filelist_entry *entry = browser_get_selected(browser);
297 mpd_InfoEntity *entity;
299 if (entry == NULL)
300 return false;
302 entity = entry->entity;
303 if (entity == NULL || entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY)
304 return browser_change_directory(browser, c, entry, NULL);
305 else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
306 return load_playlist(c, entry);
307 else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
308 return enqueue_and_play(c, entry);
309 return false;
310 }
312 static bool
313 browser_select_entry(mpdclient_t *c, filelist_entry_t *entry,
314 G_GNUC_UNUSED gboolean toggle)
315 {
316 assert(entry != NULL);
317 assert(entry->entity != NULL);
319 if (entry->entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
320 return load_playlist(c, entry);
322 if (entry->entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
323 mpd_Directory *dir = entry->entity->info.directory;
325 if (mpdclient_cmd_add_path(c, dir->path) == 0) {
326 char *tmp = utf8_to_locale(dir->path);
328 screen_status_printf(_("Adding \'%s\' to playlist"), tmp);
329 g_free(tmp);
330 }
332 return true;
333 }
335 if (entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
336 return false;
338 assert(entry->entity->info.song != NULL);
340 #ifndef NCMPC_MINI
341 if (!toggle || (entry->flags & HIGHLIGHT) == 0)
342 #endif
343 {
344 mpd_Song *song = entry->entity->info.song;
346 #ifndef NCMPC_MINI
347 entry->flags |= HIGHLIGHT;
348 #endif
350 if (mpdclient_cmd_add(c, song) == 0) {
351 char buf[BUFSIZE];
353 strfsong(buf, BUFSIZE, options.list_format, song);
354 screen_status_printf(_("Adding \'%s\' to playlist"), buf);
355 }
356 #ifndef NCMPC_MINI
357 } else {
358 /* remove song from playlist */
359 mpd_Song *song = entry->entity->info.song;
360 int idx;
362 entry->flags &= ~HIGHLIGHT;
364 while ((idx = playlist_get_index_from_same_song(c, song)) >= 0)
365 mpdclient_cmd_delete(c, idx);
366 #endif
367 }
369 return true;
370 }
372 static bool
373 browser_handle_select(struct screen_browser *browser, mpdclient_t *c)
374 {
375 struct filelist_entry *entry;
377 if (browser->lw->range_selection) {
378 for (unsigned i = browser->lw->selected_start;
379 i <= browser->lw->selected_end; i++) {
380 entry = browser_get_index(browser, i);
382 if (entry != NULL && entry->entity != NULL)
383 browser_select_entry(c, entry, TRUE);
384 }
385 return false;
386 } else {
387 entry = browser_get_selected(browser);
389 if (entry == NULL || entry->entity == NULL)
390 return false;
392 return browser_select_entry(c, entry, TRUE);
393 }
394 }
396 static bool
397 browser_handle_add(struct screen_browser *browser, mpdclient_t *c)
398 {
399 struct filelist_entry *entry;
401 if (browser->lw->range_selection) {
402 for (unsigned i = browser->lw->selected_start;
403 i <= browser->lw->selected_end; i++) {
404 entry = browser_get_index(browser, i);
406 if (entry != NULL && entry->entity != NULL)
407 browser_select_entry(c, entry, FALSE);
408 }
409 return false;
410 } else {
411 entry = browser_get_selected(browser);
413 if (entry == NULL || entry->entity == NULL)
414 return false;
416 return browser_select_entry(c, entry, FALSE);
417 }
418 }
420 static void
421 browser_handle_select_all(struct screen_browser *browser, mpdclient_t *c)
422 {
423 guint i;
425 if (browser->filelist == NULL)
426 return;
428 for (i = 0; i < filelist_length(browser->filelist); ++i) {
429 struct filelist_entry *entry = filelist_get(browser->filelist, i);
431 if (entry != NULL && entry->entity != NULL)
432 browser_select_entry(c, entry, FALSE);
433 }
434 }
436 #ifdef HAVE_GETMOUSE
437 static int
438 browser_handle_mouse_event(struct screen_browser *browser, mpdclient_t *c)
439 {
440 int row;
441 unsigned prev_selected = browser->lw->selected;
442 unsigned long bstate;
443 int length;
445 if (browser->filelist)
446 length = filelist_length(browser->filelist);
447 else
448 length = 0;
450 if (screen_get_mouse_event(c, &bstate, &row) ||
451 list_window_mouse(browser->lw, length, bstate, row))
452 return 1;
454 browser->lw->selected = browser->lw->start + row;
455 list_window_check_selected(browser->lw, length);
457 if( bstate & BUTTON1_CLICKED ) {
458 if (prev_selected == browser->lw->selected)
459 browser_handle_enter(browser, c);
460 } else if (bstate & BUTTON3_CLICKED) {
461 if (prev_selected == browser->lw->selected)
462 browser_handle_select(browser, c);
463 }
465 return 1;
466 }
467 #endif
469 bool
470 browser_cmd(struct screen_browser *browser,
471 struct mpdclient *c, command_t cmd)
472 {
473 struct filelist_entry *entry;
475 switch (cmd) {
476 case CMD_PLAY:
477 browser_handle_enter(browser, c);
478 return true;
480 case CMD_SELECT:
481 if (browser_handle_select(browser, c))
482 /* continue and select next item... */
483 cmd = CMD_LIST_NEXT;
485 /* call list_window_cmd to go to the next item */
486 break;
488 case CMD_ADD:
489 if (browser_handle_add(browser, c))
490 /* continue and select next item... */
491 cmd = CMD_LIST_NEXT;
493 /* call list_window_cmd to go to the next item */
494 break;
496 case CMD_SELECT_ALL:
497 browser_handle_select_all(browser, c);
498 return true;
500 case CMD_LIST_FIND:
501 case CMD_LIST_RFIND:
502 case CMD_LIST_FIND_NEXT:
503 case CMD_LIST_RFIND_NEXT:
504 screen_find(browser->lw, filelist_length(browser->filelist),
505 cmd, browser_lw_callback,
506 browser->filelist);
507 return true;
508 case CMD_LIST_JUMP:
509 screen_jump(browser->lw, browser_lw_callback, browser->filelist);
510 return true;
512 #ifdef HAVE_GETMOUSE
513 case CMD_MOUSE_EVENT:
514 browser_handle_mouse_event(browser, c);
515 return true;
516 #endif
518 #ifdef ENABLE_SONG_SCREEN
519 case CMD_SCREEN_SONG:
520 entry = browser_get_selected(browser);
521 if (entry == NULL || entry->entity == NULL ||
522 entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
523 return false;
525 screen_song_switch(c, entry->entity->info.song);
526 return true;
527 #endif
529 case CMD_LOCATE:
530 entry = browser_get_selected(browser);
531 if (entry == NULL || entry->entity == NULL ||
532 entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
533 return false;
535 screen_file_goto_song(c, entry->entity->info.song);
536 return true;
538 #ifdef ENABLE_LYRICS_SCREEN
539 case CMD_SCREEN_LYRICS:
540 entry = browser_get_selected(browser);
541 if (entry == NULL)
542 return false;
544 if (entry->entity == NULL ||
545 entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
546 return true;
548 screen_lyrics_switch(c, entry->entity->info.song, false);
549 return true;
550 #endif
551 case CMD_SCREEN_SWAP:
552 entry = browser_get_selected(browser);
553 if (entry->entity != NULL &&
554 entry->entity->type == MPD_INFO_ENTITY_TYPE_SONG)
555 screen_swap(c, entry->entity->info.song);
556 else
557 screen_swap(c, NULL);
558 return true;
560 default:
561 break;
562 }
564 if (list_window_cmd(browser->lw, filelist_length(browser->filelist),
565 cmd))
566 return true;
568 return false;
569 }