985be3898fcd290f4baa38f9d78e6233cfe0058a
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"
35 #include "song_paint.h"
37 #include <mpd/client.h>
39 #include <string.h>
41 #define BUFSIZE 1024
43 #ifndef NCMPC_MINI
44 #define HIGHLIGHT (0x01)
45 #endif
47 static const char playlist_format[] = "*%s*";
49 #ifndef NCMPC_MINI
51 /* sync highlight flags with playlist */
52 void
53 screen_browser_sync_highlights(struct filelist *fl,
54 const struct mpdclient_playlist *playlist)
55 {
56 guint i;
58 for (i = 0; i < filelist_length(fl); ++i) {
59 struct filelist_entry *entry = filelist_get(fl, i);
60 const struct mpd_entity *entity = entry->entity;
62 if (entity != NULL && mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
63 const struct mpd_song *song =
64 mpd_entity_get_song(entity);
66 if (playlist_get_index_from_same_song(playlist,
67 song) >= 0)
68 entry->flags |= HIGHLIGHT;
69 else
70 entry->flags &= ~HIGHLIGHT;
71 }
72 }
73 }
75 #endif
77 /* list_window callback */
78 static const char *
79 browser_lw_callback(unsigned idx, void *data)
80 {
81 const struct filelist *fl = (const struct filelist *) data;
82 static char buf[BUFSIZE];
83 const struct filelist_entry *entry;
84 const struct mpd_entity *entity;
86 assert(fl != NULL);
87 assert(idx < filelist_length(fl));
89 entry = filelist_get(fl, idx);
90 assert(entry != NULL);
92 entity = entry->entity;
94 if( entity == NULL )
95 return "..";
97 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY) {
98 const struct mpd_directory *dir =
99 mpd_entity_get_directory(entity);
100 char *directory = utf8_to_locale(g_basename(mpd_directory_get_path(dir)));
101 g_strlcpy(buf, directory, sizeof(buf));
102 g_free(directory);
103 return buf;
104 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
105 const struct mpd_song *song = mpd_entity_get_song(entity);
107 strfsong(buf, BUFSIZE, options.list_format, song);
108 return buf;
109 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST) {
110 const struct mpd_playlist *playlist =
111 mpd_entity_get_playlist(entity);
112 char *filename = utf8_to_locale(g_basename(mpd_playlist_get_path(playlist)));
114 g_snprintf(buf, BUFSIZE, playlist_format, filename);
115 g_free(filename);
116 return buf;
117 }
119 return "Error: Unknown entry!";
120 }
122 static bool
123 load_playlist(struct mpdclient *c, const struct mpd_playlist *playlist)
124 {
125 struct mpd_connection *connection = mpdclient_get_connection(c);
127 if (connection == NULL)
128 return false;
130 if (mpd_run_load(connection, mpd_playlist_get_path(playlist))) {
131 char *filename = utf8_to_locale(mpd_playlist_get_path(playlist));
132 screen_status_printf(_("Loading playlist %s..."),
133 g_basename(filename));
134 g_free(filename);
136 c->events |= MPD_IDLE_QUEUE;
137 } else
138 mpdclient_handle_error(c);
140 return true;
141 }
143 static bool
144 enqueue_and_play(struct mpdclient *c, struct filelist_entry *entry)
145 {
146 struct mpd_connection *connection = mpdclient_get_connection(c);
147 const struct mpd_song *song = mpd_entity_get_song(entry->entity);
148 int id;
150 #ifndef NCMPC_MINI
151 if (!(entry->flags & HIGHLIGHT))
152 id = -1;
153 else
154 #endif
155 id = playlist_get_id_from_same_song(&c->playlist, song);
157 if (id < 0) {
158 char buf[BUFSIZE];
160 id = mpd_run_add_id(connection, mpd_song_get_uri(song));
161 if (id < 0) {
162 mpdclient_handle_error(c);
163 return false;
164 }
166 #ifndef NCMPC_MINI
167 entry->flags |= HIGHLIGHT;
168 #endif
169 strfsong(buf, BUFSIZE, options.list_format, song);
170 screen_status_printf(_("Adding \'%s\' to playlist"), buf);
171 }
173 if (!mpd_run_play_id(connection, id)) {
174 mpdclient_handle_error(c);
175 return false;
176 }
178 return true;
179 }
181 struct filelist_entry *
182 browser_get_selected_entry(const struct screen_browser *browser)
183 {
184 struct list_window_range range;
186 list_window_get_range(browser->lw, &range);
188 if (browser->filelist == NULL ||
189 range.end <= range.start ||
190 range.end > range.start + 1 ||
191 range.start >= filelist_length(browser->filelist))
192 return NULL;
194 return filelist_get(browser->filelist, range.start);
195 }
197 static const struct mpd_entity *
198 browser_get_selected_entity(const struct screen_browser *browser)
199 {
200 const struct filelist_entry *entry = browser_get_selected_entry(browser);
202 return entry != NULL
203 ? entry->entity
204 : NULL;
205 }
207 static const struct mpd_song *
208 browser_get_selected_song(const struct screen_browser *browser)
209 {
210 const struct mpd_entity *entity = browser_get_selected_entity(browser);
212 return entity != NULL &&
213 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG
214 ? mpd_entity_get_song(entity)
215 : NULL;
216 }
218 static struct filelist_entry *
219 browser_get_index(const struct screen_browser *browser, unsigned i)
220 {
221 if (browser->filelist == NULL ||
222 i >= filelist_length(browser->filelist))
223 return NULL;
225 return filelist_get(browser->filelist, i);
226 }
228 static bool
229 browser_handle_enter(struct screen_browser *browser, struct mpdclient *c)
230 {
231 struct filelist_entry *entry = browser_get_selected_entry(browser);
232 struct mpd_entity *entity;
234 if (entry == NULL)
235 return false;
237 entity = entry->entity;
238 if (entity == NULL)
239 return false;
241 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST)
242 return load_playlist(c, mpd_entity_get_playlist(entity));
243 else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
244 return enqueue_and_play(c, entry);
245 return false;
246 }
248 static bool
249 browser_select_entry(struct mpdclient *c, struct filelist_entry *entry,
250 G_GNUC_UNUSED gboolean toggle)
251 {
252 assert(entry != NULL);
253 assert(entry->entity != NULL);
255 if (mpd_entity_get_type(entry->entity) == MPD_ENTITY_TYPE_PLAYLIST)
256 return load_playlist(c, mpd_entity_get_playlist(entry->entity));
258 if (mpd_entity_get_type(entry->entity) == MPD_ENTITY_TYPE_DIRECTORY) {
259 const struct mpd_directory *dir =
260 mpd_entity_get_directory(entry->entity);
262 if (mpdclient_cmd_add_path(c, mpd_directory_get_path(dir))) {
263 char *tmp = utf8_to_locale(mpd_directory_get_path(dir));
265 screen_status_printf(_("Adding \'%s\' to playlist"), tmp);
266 g_free(tmp);
267 }
269 return true;
270 }
272 if (mpd_entity_get_type(entry->entity) != MPD_ENTITY_TYPE_SONG)
273 return false;
275 #ifndef NCMPC_MINI
276 if (!toggle || (entry->flags & HIGHLIGHT) == 0)
277 #endif
278 {
279 const struct mpd_song *song =
280 mpd_entity_get_song(entry->entity);
282 #ifndef NCMPC_MINI
283 entry->flags |= HIGHLIGHT;
284 #endif
286 if (mpdclient_cmd_add(c, song)) {
287 char buf[BUFSIZE];
289 strfsong(buf, BUFSIZE, options.list_format, song);
290 screen_status_printf(_("Adding \'%s\' to playlist"), buf);
291 }
292 #ifndef NCMPC_MINI
293 } else {
294 /* remove song from playlist */
295 const struct mpd_song *song =
296 mpd_entity_get_song(entry->entity);
297 int idx;
299 entry->flags &= ~HIGHLIGHT;
301 while ((idx = playlist_get_index_from_same_song(&c->playlist,
302 song)) >= 0)
303 mpdclient_cmd_delete(c, idx);
304 #endif
305 }
307 return true;
308 }
310 static bool
311 browser_handle_select(struct screen_browser *browser, struct mpdclient *c)
312 {
313 struct list_window_range range;
314 struct filelist_entry *entry;
315 bool success = false;
317 list_window_get_range(browser->lw, &range);
318 for (unsigned i = range.start; i < range.end; ++i) {
319 entry = browser_get_index(browser, i);
321 if (entry != NULL && entry->entity != NULL)
322 success = browser_select_entry(c, entry, TRUE);
323 }
325 return range.end == range.start + 1 && success;
326 }
328 static bool
329 browser_handle_add(struct screen_browser *browser, struct mpdclient *c)
330 {
331 struct list_window_range range;
332 struct filelist_entry *entry;
333 bool success;
335 list_window_get_range(browser->lw, &range);
336 for (unsigned i = range.start; i < range.end; ++i) {
337 entry = browser_get_index(browser, i);
339 if (entry != NULL && entry->entity != NULL)
340 success = browser_select_entry(c, entry, FALSE);
341 }
343 return range.end == range.start + 1 && success;
344 }
346 static void
347 browser_handle_select_all(struct screen_browser *browser, struct mpdclient *c)
348 {
349 guint i;
351 if (browser->filelist == NULL)
352 return;
354 for (i = 0; i < filelist_length(browser->filelist); ++i) {
355 struct filelist_entry *entry = filelist_get(browser->filelist, i);
357 if (entry != NULL && entry->entity != NULL)
358 browser_select_entry(c, entry, FALSE);
359 }
360 }
362 #ifdef HAVE_GETMOUSE
363 static int
364 browser_handle_mouse_event(struct screen_browser *browser, struct mpdclient *c)
365 {
366 int row;
367 unsigned prev_selected = browser->lw->selected;
368 unsigned long bstate;
370 if (screen_get_mouse_event(c, &bstate, &row) ||
371 list_window_mouse(browser->lw, bstate, row))
372 return 1;
374 list_window_set_cursor(browser->lw, browser->lw->start + row);
376 if( bstate & BUTTON1_CLICKED ) {
377 if (prev_selected == browser->lw->selected)
378 browser_handle_enter(browser, c);
379 } else if (bstate & BUTTON3_CLICKED) {
380 if (prev_selected == browser->lw->selected)
381 browser_handle_select(browser, c);
382 }
384 return 1;
385 }
386 #endif
388 static void
389 screen_browser_paint_callback(WINDOW *w, unsigned i, unsigned y,
390 unsigned width, bool selected, void *data);
392 bool
393 browser_cmd(struct screen_browser *browser,
394 struct mpdclient *c, command_t cmd)
395 {
396 const struct mpd_song *song;
398 if (browser->filelist == NULL)
399 return false;
401 if (list_window_cmd(browser->lw, cmd))
402 return true;
404 switch (cmd) {
405 case CMD_LIST_FIND:
406 case CMD_LIST_RFIND:
407 case CMD_LIST_FIND_NEXT:
408 case CMD_LIST_RFIND_NEXT:
409 screen_find(browser->lw, cmd, browser_lw_callback,
410 browser->filelist);
411 return true;
412 case CMD_LIST_JUMP:
413 screen_jump(browser->lw, browser_lw_callback,
414 screen_browser_paint_callback, browser->filelist);
415 return true;
417 #ifdef HAVE_GETMOUSE
418 case CMD_MOUSE_EVENT:
419 browser_handle_mouse_event(browser, c);
420 return true;
421 #endif
423 #ifdef ENABLE_SONG_SCREEN
424 case CMD_SCREEN_SONG:
425 song = browser_get_selected_song(browser);
426 if (song == NULL)
427 return false;
429 screen_song_switch(c, song);
430 return true;
431 #endif
433 #ifdef ENABLE_LYRICS_SCREEN
434 case CMD_SCREEN_LYRICS:
435 song = browser_get_selected_song(browser);
436 if (song == NULL)
437 return false;
439 screen_lyrics_switch(c, song, false);
440 return true;
441 #endif
442 case CMD_SCREEN_SWAP:
443 screen_swap(c, browser_get_selected_song(browser));
444 return true;
446 default:
447 break;
448 }
450 if (!mpdclient_is_connected(c))
451 return false;
453 switch (cmd) {
454 case CMD_PLAY:
455 browser_handle_enter(browser, c);
456 return true;
458 case CMD_SELECT:
459 if (browser_handle_select(browser, c))
460 list_window_cmd(browser->lw, CMD_LIST_NEXT);
461 return true;
463 case CMD_ADD:
464 if (browser_handle_add(browser, c))
465 list_window_cmd(browser->lw, CMD_LIST_NEXT);
466 return true;
468 case CMD_SELECT_ALL:
469 browser_handle_select_all(browser, c);
470 return true;
472 case CMD_LOCATE:
473 song = browser_get_selected_song(browser);
474 if (song == NULL)
475 return false;
477 screen_file_goto_song(c, song);
478 return true;
480 default:
481 break;
482 }
484 return false;
485 }
487 static void
488 screen_browser_paint_directory(WINDOW *w, unsigned width,
489 bool selected, const char *name)
490 {
491 row_color(w, COLOR_DIRECTORY, selected);
493 waddch(w, '[');
494 waddstr(w, name);
495 waddch(w, ']');
497 /* erase the unused space after the text */
498 row_clear_to_eol(w, width, selected);
499 }
501 static void
502 screen_browser_paint_playlist(WINDOW *w, unsigned width,
503 bool selected, const char *name)
504 {
505 row_paint_text(w, width, COLOR_PLAYLIST, selected, name);
506 }
508 static void
509 screen_browser_paint_callback(WINDOW *w, unsigned i,
510 unsigned y, unsigned width,
511 bool selected, void *data)
512 {
513 const struct filelist *fl = (const struct filelist *) data;
514 const struct filelist_entry *entry;
515 const struct mpd_entity *entity;
516 bool highlight;
517 const struct mpd_directory *directory;
518 const struct mpd_playlist *playlist;
519 char *p;
521 assert(fl != NULL);
522 assert(i < filelist_length(fl));
524 entry = filelist_get(fl, i);
525 assert(entry != NULL);
527 entity = entry->entity;
528 if (entity == NULL) {
529 screen_browser_paint_directory(w, width, selected, "..");
530 return;
531 }
533 #ifndef NCMPC_MINI
534 highlight = (entry->flags & HIGHLIGHT) != 0;
535 #else
536 highlight = false;
537 #endif
539 if (highlight)
540 colors_use(w, COLOR_LIST_BOLD);
541 else
542 colors_use(w, COLOR_LIST);
544 switch (mpd_entity_get_type(entity)) {
545 case MPD_ENTITY_TYPE_DIRECTORY:
546 directory = mpd_entity_get_directory(entity);
547 p = utf8_to_locale(g_basename(mpd_directory_get_path(directory)));
548 screen_browser_paint_directory(w, width, selected, p);
549 g_free(p);
550 break;
552 case MPD_ENTITY_TYPE_SONG:
553 paint_song_row(w, y, width, selected, highlight,
554 mpd_entity_get_song(entity), NULL);
555 break;
557 case MPD_ENTITY_TYPE_PLAYLIST:
558 playlist = mpd_entity_get_playlist(entity);
559 p = utf8_to_locale(g_basename(mpd_playlist_get_path(playlist)));
560 screen_browser_paint_playlist(w, width, selected, p);
561 g_free(p);
562 break;
564 default:
565 row_paint_text(w, width, highlight ? COLOR_LIST_BOLD : COLOR_LIST,
566 selected, "<unknown>");
567 }
569 if (selected)
570 wattroff(w, A_REVERSE);
571 }
573 void
574 screen_browser_paint(const struct screen_browser *browser)
575 {
576 list_window_paint2(browser->lw, screen_browser_paint_callback,
577 browser->filelist);
578 }