16a2f5e7045c48233741e19aada820c76b1cdf02
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 "screen_file.h"
22 #include "screen_song.h"
23 #include "screen_lyrics.h"
24 #include "screen_message.h"
25 #include "screen_find.h"
26 #include "screen.h"
27 #include "i18n.h"
28 #include "options.h"
29 #include "charset.h"
30 #include "strfsong.h"
31 #include "mpdclient.h"
32 #include "filelist.h"
33 #include "colors.h"
34 #include "paint.h"
36 #include <mpd/client.h>
38 #include <string.h>
40 #define BUFSIZE 1024
42 #ifndef NCMPC_MINI
43 #define HIGHLIGHT (0x01)
44 #endif
46 static const char playlist_format[] = "*%s*";
48 #ifndef NCMPC_MINI
50 /* sync highlight flags with playlist */
51 void
52 screen_browser_sync_highlights(struct filelist *fl,
53 const struct mpdclient_playlist *playlist)
54 {
55 guint i;
57 for (i = 0; i < filelist_length(fl); ++i) {
58 struct filelist_entry *entry = filelist_get(fl, i);
59 const struct mpd_entity *entity = entry->entity;
61 if (entity != NULL && mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
62 const struct mpd_song *song =
63 mpd_entity_get_song(entity);
65 if (playlist_get_index_from_same_song(playlist,
66 song) >= 0)
67 entry->flags |= HIGHLIGHT;
68 else
69 entry->flags &= ~HIGHLIGHT;
70 }
71 }
72 }
74 #endif
76 /* list_window callback */
77 static const char *
78 browser_lw_callback(unsigned idx, bool *highlight, G_GNUC_UNUSED char **second_column, void *data)
79 {
80 const struct filelist *fl = (const struct filelist *) data;
81 static char buf[BUFSIZE];
82 const struct filelist_entry *entry;
83 const struct mpd_entity *entity;
85 assert(fl != NULL);
86 assert(idx < filelist_length(fl));
88 entry = filelist_get(fl, idx);
89 assert(entry != NULL);
91 entity = entry->entity;
92 #ifndef NCMPC_MINI
93 *highlight = (entry->flags & HIGHLIGHT) != 0;
94 #else
95 *highlight = false;
96 #endif
98 if( entity == NULL )
99 return "[..]";
101 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY) {
102 const struct mpd_directory *dir =
103 mpd_entity_get_directory(entity);
104 char *directory = utf8_to_locale(g_basename(mpd_directory_get_path(dir)));
106 g_snprintf(buf, BUFSIZE, "[%s]", directory);
107 g_free(directory);
108 return buf;
109 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
110 const struct mpd_song *song = mpd_entity_get_song(entity);
112 strfsong(buf, BUFSIZE, options.list_format, song);
113 return buf;
114 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST) {
115 const struct mpd_playlist *playlist =
116 mpd_entity_get_playlist(entity);
117 char *filename = utf8_to_locale(g_basename(mpd_playlist_get_path(playlist)));
119 g_snprintf(buf, BUFSIZE, playlist_format, filename);
120 g_free(filename);
121 return buf;
122 }
124 return "Error: Unknown entry!";
125 }
127 static bool
128 load_playlist(struct mpdclient *c, const struct mpd_playlist *playlist)
129 {
130 struct mpd_connection *connection = mpdclient_get_connection(c);
131 char *filename = utf8_to_locale(mpd_playlist_get_path(playlist));
133 if (mpd_run_load(connection, mpd_playlist_get_path(playlist))) {
134 screen_status_printf(_("Loading playlist %s..."),
135 g_basename(filename));
136 c->events |= MPD_IDLE_QUEUE;
137 } else
138 mpdclient_handle_error(c);
140 g_free(filename);
141 return true;
142 }
144 static bool
145 enqueue_and_play(struct mpdclient *c, struct filelist_entry *entry)
146 {
147 struct mpd_connection *connection = mpdclient_get_connection(c);
148 const struct mpd_song *song = mpd_entity_get_song(entry->entity);
149 int id;
151 #ifndef NCMPC_MINI
152 if (!(entry->flags & HIGHLIGHT))
153 id = -1;
154 else
155 #endif
156 id = playlist_get_id_from_same_song(&c->playlist, song);
158 if (id < 0) {
159 char buf[BUFSIZE];
161 id = mpd_run_add_id(connection, mpd_song_get_uri(song));
162 if (id < 0) {
163 mpdclient_handle_error(c);
164 return false;
165 }
167 #ifndef NCMPC_MINI
168 entry->flags |= HIGHLIGHT;
169 #endif
170 strfsong(buf, BUFSIZE, options.list_format, song);
171 screen_status_printf(_("Adding \'%s\' to playlist"), buf);
172 }
174 if (!mpd_run_play_id(connection, id)) {
175 mpdclient_handle_error(c);
176 return false;
177 }
179 return true;
180 }
182 struct filelist_entry *
183 browser_get_selected_entry(const struct screen_browser *browser)
184 {
185 struct list_window_range range;
187 list_window_get_range(browser->lw, &range);
189 if (browser->filelist == NULL ||
190 range.end <= range.start ||
191 range.end > range.start + 1 ||
192 range.start >= filelist_length(browser->filelist))
193 return NULL;
195 return filelist_get(browser->filelist, range.start);
196 }
198 static const struct mpd_entity *
199 browser_get_selected_entity(const struct screen_browser *browser)
200 {
201 const struct filelist_entry *entry = browser_get_selected_entry(browser);
203 return entry != NULL
204 ? entry->entity
205 : NULL;
206 }
208 static const struct mpd_song *
209 browser_get_selected_song(const struct screen_browser *browser)
210 {
211 const struct mpd_entity *entity = browser_get_selected_entity(browser);
213 return entity != NULL &&
214 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG
215 ? mpd_entity_get_song(entity)
216 : NULL;
217 }
219 static struct filelist_entry *
220 browser_get_index(const struct screen_browser *browser, unsigned i)
221 {
222 if (browser->filelist == NULL ||
223 i >= filelist_length(browser->filelist))
224 return NULL;
226 return filelist_get(browser->filelist, i);
227 }
229 static bool
230 browser_handle_enter(struct screen_browser *browser, struct mpdclient *c)
231 {
232 struct filelist_entry *entry = browser_get_selected_entry(browser);
233 struct mpd_entity *entity;
235 if (entry == NULL)
236 return false;
238 entity = entry->entity;
239 if (entity == NULL)
240 return false;
242 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST)
243 return load_playlist(c, mpd_entity_get_playlist(entity));
244 else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
245 return enqueue_and_play(c, entry);
246 return false;
247 }
249 static bool
250 browser_select_entry(struct mpdclient *c, struct filelist_entry *entry,
251 G_GNUC_UNUSED gboolean toggle)
252 {
253 assert(entry != NULL);
254 assert(entry->entity != NULL);
256 if (mpd_entity_get_type(entry->entity) == MPD_ENTITY_TYPE_PLAYLIST)
257 return load_playlist(c, mpd_entity_get_playlist(entry->entity));
259 if (mpd_entity_get_type(entry->entity) == MPD_ENTITY_TYPE_DIRECTORY) {
260 const struct mpd_directory *dir =
261 mpd_entity_get_directory(entry->entity);
263 if (mpdclient_cmd_add_path(c, mpd_directory_get_path(dir))) {
264 char *tmp = utf8_to_locale(mpd_directory_get_path(dir));
266 screen_status_printf(_("Adding \'%s\' to playlist"), tmp);
267 g_free(tmp);
268 }
270 return true;
271 }
273 if (mpd_entity_get_type(entry->entity) != MPD_ENTITY_TYPE_SONG)
274 return false;
276 #ifndef NCMPC_MINI
277 if (!toggle || (entry->flags & HIGHLIGHT) == 0)
278 #endif
279 {
280 const struct mpd_song *song =
281 mpd_entity_get_song(entry->entity);
283 #ifndef NCMPC_MINI
284 entry->flags |= HIGHLIGHT;
285 #endif
287 if (mpdclient_cmd_add(c, song)) {
288 char buf[BUFSIZE];
290 strfsong(buf, BUFSIZE, options.list_format, song);
291 screen_status_printf(_("Adding \'%s\' to playlist"), buf);
292 }
293 #ifndef NCMPC_MINI
294 } else {
295 /* remove song from playlist */
296 const struct mpd_song *song =
297 mpd_entity_get_song(entry->entity);
298 int idx;
300 entry->flags &= ~HIGHLIGHT;
302 while ((idx = playlist_get_index_from_same_song(&c->playlist,
303 song)) >= 0)
304 mpdclient_cmd_delete(c, idx);
305 #endif
306 }
308 return true;
309 }
311 static bool
312 browser_handle_select(struct screen_browser *browser, struct mpdclient *c)
313 {
314 struct list_window_range range;
315 struct filelist_entry *entry;
316 bool success;
318 list_window_get_range(browser->lw, &range);
319 for (unsigned i = range.start; i < range.end; ++i) {
320 entry = browser_get_index(browser, i);
322 if (entry != NULL && entry->entity != NULL)
323 success = browser_select_entry(c, entry, TRUE);
324 }
326 return range.end == range.start + 1 && success;
327 }
329 static bool
330 browser_handle_add(struct screen_browser *browser, struct mpdclient *c)
331 {
332 struct list_window_range range;
333 struct filelist_entry *entry;
334 bool success;
336 list_window_get_range(browser->lw, &range);
337 for (unsigned i = range.start; i < range.end; ++i) {
338 entry = browser_get_index(browser, i);
340 if (entry != NULL && entry->entity != NULL)
341 success = browser_select_entry(c, entry, FALSE);
342 }
344 return range.end == range.start + 1 && success;
345 }
347 static void
348 browser_handle_select_all(struct screen_browser *browser, struct mpdclient *c)
349 {
350 guint i;
352 if (browser->filelist == NULL)
353 return;
355 for (i = 0; i < filelist_length(browser->filelist); ++i) {
356 struct filelist_entry *entry = filelist_get(browser->filelist, i);
358 if (entry != NULL && entry->entity != NULL)
359 browser_select_entry(c, entry, FALSE);
360 }
361 }
363 #ifdef HAVE_GETMOUSE
364 static int
365 browser_handle_mouse_event(struct screen_browser *browser, struct mpdclient *c)
366 {
367 int row;
368 unsigned prev_selected = browser->lw->selected;
369 unsigned long bstate;
371 if (screen_get_mouse_event(c, &bstate, &row) ||
372 list_window_mouse(browser->lw, bstate, row))
373 return 1;
375 list_window_set_cursor(browser->lw, browser->lw->start + row);
377 if( bstate & BUTTON1_CLICKED ) {
378 if (prev_selected == browser->lw->selected)
379 browser_handle_enter(browser, c);
380 } else if (bstate & BUTTON3_CLICKED) {
381 if (prev_selected == browser->lw->selected)
382 browser_handle_select(browser, c);
383 }
385 return 1;
386 }
387 #endif
389 static void
390 screen_browser_paint_callback(WINDOW *w, unsigned i, unsigned y,
391 unsigned width, bool selected, void *data);
393 bool
394 browser_cmd(struct screen_browser *browser,
395 struct mpdclient *c, command_t cmd)
396 {
397 const struct mpd_song *song;
399 if (browser->filelist == NULL)
400 return false;
402 if (list_window_cmd(browser->lw, cmd))
403 return true;
405 switch (cmd) {
406 case CMD_LIST_FIND:
407 case CMD_LIST_RFIND:
408 case CMD_LIST_FIND_NEXT:
409 case CMD_LIST_RFIND_NEXT:
410 screen_find(browser->lw, cmd, browser_lw_callback,
411 browser->filelist);
412 return true;
413 case CMD_LIST_JUMP:
414 screen_jump(browser->lw, browser_lw_callback,
415 screen_browser_paint_callback, browser->filelist);
416 return true;
418 #ifdef HAVE_GETMOUSE
419 case CMD_MOUSE_EVENT:
420 browser_handle_mouse_event(browser, c);
421 return true;
422 #endif
424 #ifdef ENABLE_SONG_SCREEN
425 case CMD_SCREEN_SONG:
426 song = browser_get_selected_song(browser);
427 if (song == NULL)
428 return false;
430 screen_song_switch(c, song);
431 return true;
432 #endif
434 #ifdef ENABLE_LYRICS_SCREEN
435 case CMD_SCREEN_LYRICS:
436 song = browser_get_selected_song(browser);
437 if (song == NULL)
438 return false;
440 screen_lyrics_switch(c, song, false);
441 return true;
442 #endif
443 case CMD_SCREEN_SWAP:
444 screen_swap(c, browser_get_selected_song(browser));
445 return true;
447 default:
448 break;
449 }
451 if (!mpdclient_is_connected(c))
452 return false;
454 switch (cmd) {
455 case CMD_PLAY:
456 browser_handle_enter(browser, c);
457 return true;
459 case CMD_SELECT:
460 if (browser_handle_select(browser, c))
461 list_window_cmd(browser->lw, CMD_LIST_NEXT);
462 return true;
464 case CMD_ADD:
465 if (browser_handle_add(browser, c))
466 list_window_cmd(browser->lw, CMD_LIST_NEXT);
467 return true;
469 case CMD_SELECT_ALL:
470 browser_handle_select_all(browser, c);
471 return true;
473 case CMD_LOCATE:
474 song = browser_get_selected_song(browser);
475 if (song == NULL)
476 return false;
478 screen_file_goto_song(c, song);
479 return true;
481 default:
482 break;
483 }
485 return false;
486 }
488 static void
489 screen_browser_paint_directory(WINDOW *w, unsigned width,
490 bool selected, const char *name)
491 {
492 row_color(w, COLOR_DIRECTORY, selected);
494 waddch(w, '[');
495 waddstr(w, name);
496 waddch(w, ']');
498 /* erase the unused space after the text */
499 row_clear_to_eol(w, width, selected);
500 }
502 static void
503 screen_browser_paint_song(WINDOW *w, unsigned width, bool selected,
504 bool highlight, const struct mpd_song *song)
505 {
506 char buffer[width * 4];
508 strfsong(buffer, sizeof(buffer), options.list_format, song);
509 row_paint_text(w, width, highlight ? COLOR_LIST_BOLD : COLOR_LIST,
510 selected, buffer);
511 }
513 static void
514 screen_browser_paint_playlist(WINDOW *w, unsigned width,
515 bool selected, const char *name)
516 {
517 row_paint_text(w, width, COLOR_PLAYLIST, selected, name);
518 }
520 static void
521 screen_browser_paint_callback(WINDOW *w, unsigned i,
522 G_GNUC_UNUSED unsigned y, unsigned width,
523 bool selected, void *data)
524 {
525 const struct filelist *fl = (const struct filelist *) data;
526 const struct filelist_entry *entry;
527 const struct mpd_entity *entity;
528 bool highlight;
529 const struct mpd_directory *directory;
530 const struct mpd_playlist *playlist;
531 char *p;
533 assert(fl != NULL);
534 assert(i < filelist_length(fl));
536 entry = filelist_get(fl, i);
537 assert(entry != NULL);
539 entity = entry->entity;
540 if (entity == NULL) {
541 screen_browser_paint_directory(w, width, selected, "..");
542 return;
543 }
545 #ifndef NCMPC_MINI
546 highlight = (entry->flags & HIGHLIGHT) != 0;
547 #else
548 highlight = false;
549 #endif
551 if (highlight)
552 colors_use(w, COLOR_LIST_BOLD);
553 else
554 colors_use(w, COLOR_LIST);
556 switch (mpd_entity_get_type(entity)) {
557 case MPD_ENTITY_TYPE_DIRECTORY:
558 directory = mpd_entity_get_directory(entity);
559 p = utf8_to_locale(g_basename(mpd_directory_get_path(directory)));
560 screen_browser_paint_directory(w, width, selected, p);
561 g_free(p);
562 break;
564 case MPD_ENTITY_TYPE_SONG:
565 screen_browser_paint_song(w, width, selected, highlight,
566 mpd_entity_get_song(entity));
567 break;
569 case MPD_ENTITY_TYPE_PLAYLIST:
570 playlist = mpd_entity_get_playlist(entity);
571 p = utf8_to_locale(g_basename(mpd_playlist_get_path(playlist)));
572 screen_browser_paint_playlist(w, width, selected, p);
573 g_free(p);
574 break;
576 default:
577 waddstr(w, "<unknown>");
578 }
580 whline(w, ' ', width);
582 if (selected)
583 wattroff(w, A_REVERSE);
584 }
586 void
587 screen_browser_paint(const struct screen_browser *browser)
588 {
589 list_window_paint2(browser->lw, screen_browser_paint_callback,
590 browser->filelist);
591 }