a19fa84f40c7bcfa7e7f927e78150505d7e1be20
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"
34 #include <mpd/client.h>
36 #include <string.h>
38 #define BUFSIZE 1024
40 #ifndef NCMPC_MINI
41 #define HIGHLIGHT (0x01)
42 #endif
44 static const char playlist_format[] = "*%s*";
46 #ifndef NCMPC_MINI
48 /* sync highlight flags with playlist */
49 void
50 screen_browser_sync_highlights(struct filelist *fl,
51 const struct mpdclient_playlist *playlist)
52 {
53 guint i;
55 for (i = 0; i < filelist_length(fl); ++i) {
56 struct filelist_entry *entry = filelist_get(fl, i);
57 const struct mpd_entity *entity = entry->entity;
59 if (entity != NULL && mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
60 const struct mpd_song *song =
61 mpd_entity_get_song(entity);
63 if (playlist_get_index_from_same_song(playlist,
64 song) >= 0)
65 entry->flags |= HIGHLIGHT;
66 else
67 entry->flags &= ~HIGHLIGHT;
68 }
69 }
70 }
72 #endif
74 /* list_window callback */
75 const char *
76 browser_lw_callback(unsigned idx, bool *highlight, G_GNUC_UNUSED char **second_column, void *data)
77 {
78 const struct filelist *fl = (const struct filelist *) data;
79 static char buf[BUFSIZE];
80 const struct filelist_entry *entry;
81 const struct mpd_entity *entity;
83 assert(fl != NULL);
84 assert(idx < filelist_length(fl));
86 entry = filelist_get(fl, idx);
87 assert(entry != NULL);
89 entity = entry->entity;
90 #ifndef NCMPC_MINI
91 *highlight = (entry->flags & HIGHLIGHT) != 0;
92 #else
93 *highlight = false;
94 #endif
96 if( entity == NULL )
97 return "[..]";
99 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY) {
100 const struct mpd_directory *dir =
101 mpd_entity_get_directory(entity);
102 char *directory = utf8_to_locale(g_basename(mpd_directory_get_path(dir)));
104 g_snprintf(buf, BUFSIZE, "[%s]", directory);
105 g_free(directory);
106 return buf;
107 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
108 const struct mpd_song *song = mpd_entity_get_song(entity);
110 strfsong(buf, BUFSIZE, options.list_format, song);
111 return buf;
112 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST) {
113 const struct mpd_playlist *playlist =
114 mpd_entity_get_playlist(entity);
115 char *filename = utf8_to_locale(g_basename(mpd_playlist_get_path(playlist)));
117 g_snprintf(buf, BUFSIZE, playlist_format, filename);
118 g_free(filename);
119 return buf;
120 }
122 return "Error: Unknown entry!";
123 }
125 static bool
126 load_playlist(struct mpdclient *c, const struct mpd_playlist *playlist)
127 {
128 struct mpd_connection *connection = mpdclient_get_connection(c);
129 char *filename = utf8_to_locale(mpd_playlist_get_path(playlist));
131 if (mpd_run_load(connection, mpd_playlist_get_path(playlist))) {
132 screen_status_printf(_("Loading playlist %s..."),
133 g_basename(filename));
134 c->events |= MPD_IDLE_QUEUE;
135 } else
136 mpdclient_handle_error(c);
138 g_free(filename);
139 return true;
140 }
142 static bool
143 enqueue_and_play(struct mpdclient *c, struct filelist_entry *entry)
144 {
145 struct mpd_connection *connection = mpdclient_get_connection(c);
146 const struct mpd_song *song = mpd_entity_get_song(entry->entity);
147 int id;
149 #ifndef NCMPC_MINI
150 if (!(entry->flags & HIGHLIGHT))
151 id = -1;
152 else
153 #endif
154 id = playlist_get_id_from_same_song(&c->playlist, song);
156 if (id < 0) {
157 char buf[BUFSIZE];
159 id = mpd_run_add_id(connection, mpd_song_get_uri(song));
160 if (id < 0) {
161 mpdclient_handle_error(c);
162 return false;
163 }
165 #ifndef NCMPC_MINI
166 entry->flags |= HIGHLIGHT;
167 #endif
168 strfsong(buf, BUFSIZE, options.list_format, song);
169 screen_status_printf(_("Adding \'%s\' to playlist"), buf);
170 }
172 if (!mpd_run_play_id(connection, id)) {
173 mpdclient_handle_error(c);
174 return false;
175 }
177 return true;
178 }
180 struct filelist_entry *
181 browser_get_selected_entry(const struct screen_browser *browser)
182 {
183 if (browser->filelist == NULL ||
184 browser->lw->selected_start < browser->lw->selected_end ||
185 browser->lw->selected >= filelist_length(browser->filelist))
186 return NULL;
188 return filelist_get(browser->filelist, browser->lw->selected);
189 }
191 static const struct mpd_entity *
192 browser_get_selected_entity(const struct screen_browser *browser)
193 {
194 const struct filelist_entry *entry = browser_get_selected_entry(browser);
196 return entry != NULL
197 ? entry->entity
198 : NULL;
199 }
201 static const struct mpd_song *
202 browser_get_selected_song(const struct screen_browser *browser)
203 {
204 const struct mpd_entity *entity = browser_get_selected_entity(browser);
206 return entity != NULL &&
207 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG
208 ? mpd_entity_get_song(entity)
209 : NULL;
210 }
212 static struct filelist_entry *
213 browser_get_index(const struct screen_browser *browser, unsigned i)
214 {
215 if (browser->filelist == NULL ||
216 i >= filelist_length(browser->filelist))
217 return NULL;
219 return filelist_get(browser->filelist, i);
220 }
222 static bool
223 browser_handle_enter(struct screen_browser *browser, struct mpdclient *c)
224 {
225 struct filelist_entry *entry = browser_get_selected_entry(browser);
226 struct mpd_entity *entity;
228 if (entry == NULL)
229 return false;
231 entity = entry->entity;
232 if (entity == NULL)
233 return false;
235 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST)
236 return load_playlist(c, mpd_entity_get_playlist(entity));
237 else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
238 return enqueue_and_play(c, entry);
239 return false;
240 }
242 static bool
243 browser_select_entry(struct mpdclient *c, struct filelist_entry *entry,
244 G_GNUC_UNUSED gboolean toggle)
245 {
246 assert(entry != NULL);
247 assert(entry->entity != NULL);
249 if (mpd_entity_get_type(entry->entity) == MPD_ENTITY_TYPE_PLAYLIST)
250 return load_playlist(c, mpd_entity_get_playlist(entry->entity));
252 if (mpd_entity_get_type(entry->entity) == MPD_ENTITY_TYPE_DIRECTORY) {
253 const struct mpd_directory *dir =
254 mpd_entity_get_directory(entry->entity);
256 if (mpdclient_cmd_add_path(c, mpd_directory_get_path(dir))) {
257 char *tmp = utf8_to_locale(mpd_directory_get_path(dir));
259 screen_status_printf(_("Adding \'%s\' to playlist"), tmp);
260 g_free(tmp);
261 }
263 return true;
264 }
266 if (mpd_entity_get_type(entry->entity) != MPD_ENTITY_TYPE_SONG)
267 return false;
269 #ifndef NCMPC_MINI
270 if (!toggle || (entry->flags & HIGHLIGHT) == 0)
271 #endif
272 {
273 const struct mpd_song *song =
274 mpd_entity_get_song(entry->entity);
276 #ifndef NCMPC_MINI
277 entry->flags |= HIGHLIGHT;
278 #endif
280 if (mpdclient_cmd_add(c, song)) {
281 char buf[BUFSIZE];
283 strfsong(buf, BUFSIZE, options.list_format, song);
284 screen_status_printf(_("Adding \'%s\' to playlist"), buf);
285 }
286 #ifndef NCMPC_MINI
287 } else {
288 /* remove song from playlist */
289 const struct mpd_song *song =
290 mpd_entity_get_song(entry->entity);
291 int idx;
293 entry->flags &= ~HIGHLIGHT;
295 while ((idx = playlist_get_index_from_same_song(&c->playlist,
296 song)) >= 0)
297 mpdclient_cmd_delete(c, idx);
298 #endif
299 }
301 return true;
302 }
304 static bool
305 browser_handle_select(struct screen_browser *browser, struct mpdclient *c)
306 {
307 struct filelist_entry *entry;
309 if (browser->lw->range_selection) {
310 for (unsigned i = browser->lw->selected_start;
311 i <= browser->lw->selected_end; i++) {
312 entry = browser_get_index(browser, i);
314 if (entry != NULL && entry->entity != NULL)
315 browser_select_entry(c, entry, TRUE);
316 }
317 return false;
318 } else {
319 entry = browser_get_selected_entry(browser);
321 if (entry == NULL || entry->entity == NULL)
322 return false;
324 return browser_select_entry(c, entry, TRUE);
325 }
326 }
328 static bool
329 browser_handle_add(struct screen_browser *browser, struct mpdclient *c)
330 {
331 struct filelist_entry *entry;
333 if (browser->lw->range_selection) {
334 for (unsigned i = browser->lw->selected_start;
335 i <= browser->lw->selected_end; i++) {
336 entry = browser_get_index(browser, i);
338 if (entry != NULL && entry->entity != NULL)
339 browser_select_entry(c, entry, FALSE);
340 }
341 return false;
342 } else {
343 entry = browser_get_selected_entry(browser);
345 if (entry == NULL || entry->entity == NULL)
346 return false;
348 return browser_select_entry(c, entry, FALSE);
349 }
350 }
352 static void
353 browser_handle_select_all(struct screen_browser *browser, struct mpdclient *c)
354 {
355 guint i;
357 if (browser->filelist == NULL)
358 return;
360 for (i = 0; i < filelist_length(browser->filelist); ++i) {
361 struct filelist_entry *entry = filelist_get(browser->filelist, i);
363 if (entry != NULL && entry->entity != NULL)
364 browser_select_entry(c, entry, FALSE);
365 }
366 }
368 #ifdef HAVE_GETMOUSE
369 static int
370 browser_handle_mouse_event(struct screen_browser *browser, struct mpdclient *c)
371 {
372 int row;
373 unsigned prev_selected = browser->lw->selected;
374 unsigned long bstate;
376 if (screen_get_mouse_event(c, &bstate, &row) ||
377 list_window_mouse(browser->lw, bstate, row))
378 return 1;
380 list_window_set_cursor(browser->lw, browser->lw->start + row);
382 if( bstate & BUTTON1_CLICKED ) {
383 if (prev_selected == browser->lw->selected)
384 browser_handle_enter(browser, c);
385 } else if (bstate & BUTTON3_CLICKED) {
386 if (prev_selected == browser->lw->selected)
387 browser_handle_select(browser, c);
388 }
390 return 1;
391 }
392 #endif
394 bool
395 browser_cmd(struct screen_browser *browser,
396 struct mpdclient *c, command_t cmd)
397 {
398 const struct mpd_song *song;
400 if (browser->filelist == NULL)
401 return false;
403 if (list_window_cmd(browser->lw, cmd))
404 return true;
406 switch (cmd) {
407 case CMD_LIST_FIND:
408 case CMD_LIST_RFIND:
409 case CMD_LIST_FIND_NEXT:
410 case CMD_LIST_RFIND_NEXT:
411 screen_find(browser->lw, cmd, browser_lw_callback,
412 browser->filelist);
413 return true;
414 case CMD_LIST_JUMP:
415 screen_jump(browser->lw, browser_lw_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 }