Skip to content

Commit

Permalink
moved relevant stuff from playlist to streamer code
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksiy-Yakovenko committed Jan 9, 2010
1 parent 0a3d9fb commit 46aff62
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 244 deletions.
6 changes: 3 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,11 @@ player_mainloop (void) {
break;
case M_NEXTSONG:
p_stop ();
pl_nextsong (1);
streamer_move_nextsong (1);
break;
case M_PREVSONG:
p_stop ();
pl_prevsong ();
streamer_move_prevsong ();
break;
case M_PAUSESONG:
if (p_get_state () == OUTPUT_STATE_PAUSED) {
Expand All @@ -343,7 +343,7 @@ player_mainloop (void) {
break;
case M_PLAYRANDOM:
p_stop ();
pl_randomsong ();
streamer_move_randomsong ();
break;
case M_PLAYLISTREFRESH:
plug_trigger_event_playlistchanged ();
Expand Down
212 changes: 5 additions & 207 deletions playlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ playItem_t *playlist_head[PL_MAX_ITERATORS];
playItem_t *playlist_tail[PL_MAX_ITERATORS];
int playlist_current_row[PL_MAX_ITERATORS];

playItem_t *playlist_current_ptr;
static int pl_count[2];
static float pl_totaltime = 0;

Expand Down Expand Up @@ -726,9 +725,6 @@ pl_remove (playItem_t *it) {
if (!it)
return -1;
streamer_song_removed_notify (it);
if (playlist_current_ptr == it) {
playlist_current_ptr = NULL;
}
pl_playqueue_remove (it);

// remove from both lists list
Expand Down Expand Up @@ -911,204 +907,6 @@ pl_item_free (playItem_t *it) {
}
}

int
pl_prevsong (void) {
pl_playqueue_clear ();
if (!playlist_head[PL_MAIN]) {
streamer_set_nextsong (-2, 1);
return 0;
}
int pl_order = conf_get_int ("playback.order", 0);
int pl_loop_mode = conf_get_int ("playback.loop", 0);
if (pl_order == PLAYBACK_ORDER_SHUFFLE) { // shuffle
if (!playlist_current_ptr) {
return pl_nextsong (1);
}
else {
playlist_current_ptr->played = 0;
// find already played song with maximum shuffle rating below prev song
int rating = playlist_current_ptr->shufflerating;
playItem_t *pmax = NULL; // played maximum
playItem_t *amax = NULL; // absolute maximum
for (playItem_t *i = playlist_head[PL_MAIN]; i; i = i->next[PL_MAIN]) {
if (i != playlist_current_ptr && i->played && (!amax || i->shufflerating > amax->shufflerating)) {
amax = i;
}
if (i == playlist_current_ptr || i->shufflerating > rating || !i->played) {
continue;
}
if (!pmax || i->shufflerating > pmax->shufflerating) {
pmax = i;
}
}
playItem_t *it = pmax;
if (!it) {
// that means 1st in playlist, take amax
if (pl_loop_mode == PLAYBACK_MODE_LOOP_ALL) {
if (!amax) {
pl_reshuffle (NULL, &amax);
}
it = amax;
}
}

if (!it) {
return -1;
}
int r = pl_get_idx_of (it);
streamer_set_nextsong (r, 1);
return 0;
}
}
else if (pl_order == PLAYBACK_ORDER_LINEAR) { // linear
playItem_t *it = NULL;
if (playlist_current_ptr) {
it = playlist_current_ptr->prev[PL_MAIN];
}
if (!it) {
if (pl_loop_mode == PLAYBACK_MODE_LOOP_ALL) {
it = playlist_tail[PL_MAIN];
}
}
if (!it) {
return -1;
}
int r = pl_get_idx_of (it);
streamer_set_nextsong (r, 1);
return 0;
}
else if (pl_order == PLAYBACK_ORDER_RANDOM) { // random
pl_randomsong ();
}
return -1;
}

int
pl_nextsong (int reason) {
if (playqueue_count > 0) {
playItem_t *it = playqueue[0];
pl_playqueue_pop ();
int r = pl_get_idx_of (it);
streamer_set_nextsong (r, 1);
return 0;
}

playItem_t *curr = streamer_get_streaming_track ();
if (!playlist_head[PL_MAIN]) {
streamer_set_nextsong (-2, 1);
return 0;
}
int pl_order = conf_get_int ("playback.order", 0);
int pl_loop_mode = conf_get_int ("playback.loop", 0);
if (pl_order == PLAYBACK_ORDER_SHUFFLE) { // shuffle
if (!curr) {
// find minimal notplayed
playItem_t *pmin = NULL; // notplayed minimum
for (playItem_t *i = playlist_head[PL_MAIN]; i; i = i->next[PL_MAIN]) {
if (i->played) {
continue;
}
if (!pmin || i->shufflerating < pmin->shufflerating) {
pmin = i;
}
}
playItem_t *it = pmin;
if (!it) {
// all songs played, reshuffle and try again
if (pl_loop_mode == PLAYBACK_MODE_LOOP_ALL) { // loop
pl_reshuffle (&it, NULL);
}
}
if (!it) {
return -1;
}
int r = pl_get_idx_of (it);
streamer_set_nextsong (r, 1);
return 0;
}
else {
trace ("pl_next_song: reason=%d, loop=%d\n", reason, pl_loop_mode);
if (reason == 0 && pl_loop_mode == PLAYBACK_MODE_LOOP_SINGLE) { // song finished, loop mode is "loop 1 track"
int r = pl_get_idx_of (curr);
streamer_set_nextsong (r, 1);
return 0;
}
// find minimal notplayed above current
int rating = curr->shufflerating;
playItem_t *pmin = NULL; // notplayed minimum
for (playItem_t *i = playlist_head[PL_MAIN]; i; i = i->next[PL_MAIN]) {
if (i->played || i->shufflerating < rating) {
continue;
}
if (!pmin || i->shufflerating < pmin->shufflerating) {
pmin = i;
}
}
playItem_t *it = pmin;
if (!it) {
trace ("all songs played! reshuffle\n");
// all songs played, reshuffle and try again
if (pl_loop_mode == PLAYBACK_MODE_LOOP_ALL) { // loop
pl_reshuffle (&it, NULL);
}
}
if (!it) {
return -1;
}
int r = pl_get_idx_of (it);
streamer_set_nextsong (r, 1);
return 0;
}
}
else if (pl_order == PLAYBACK_ORDER_LINEAR) { // linear
playItem_t *it = NULL;
if (curr) {
if (reason == 0 && pl_loop_mode == PLAYBACK_MODE_LOOP_SINGLE) { // loop same track
int r = pl_get_idx_of (curr);
streamer_set_nextsong (r, 1);
return 0;
}
it = curr->next[PL_MAIN];
}
if (!it) {
trace ("pl_nextsong: was last track\n");
if (pl_loop_mode == PLAYBACK_MODE_LOOP_ALL) {
it = playlist_head[PL_MAIN];
}
else {
streamer_set_nextsong (-2, 1);
return 0;
}
}
if (!it) {
return -1;
}
int r = pl_get_idx_of (it);
streamer_set_nextsong (r, 1);
return 0;
}
else if (pl_order == PLAYBACK_ORDER_RANDOM) { // random
if (reason == 0 && pl_loop_mode == PLAYBACK_MODE_LOOP_SINGLE && curr) {
int r = pl_get_idx_of (curr);
streamer_set_nextsong (r, 1);
return 0;
}
return pl_randomsong ();
}
return -1;
}

int
pl_randomsong (void) {
int cnt = pl_getcount (PL_MAIN);
if (!cnt) {
return -1;
}
int r = rand () / (float)RAND_MAX * cnt;
streamer_set_nextsong (r, 1);
return 0;
}

void
pl_add_meta (playItem_t *it, const char *key, const char *value) {
// check if it's already set
Expand Down Expand Up @@ -1894,11 +1692,6 @@ pl_get_totaltime (void) {
return pl_totaltime;
}

playItem_t *
pl_getcurrent (void) {
return playlist_current_ptr;
}

void
pl_set_selected (playItem_t *it, int sel) {
it->selected = sel;
Expand Down Expand Up @@ -2107,3 +1900,8 @@ pl_playqueue_getnext (void) {
}
return NULL;
}

int
pl_playqueue_getcount (void) {
return playqueue_count;
}
28 changes: 10 additions & 18 deletions playlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@ typedef struct playItem_s {
unsigned in_playlist : 1; // 1 if item is in playlist
} playItem_t;

typedef struct {
playItem_t *playlist_head[PL_MAX_ITERATORS]; // head of linked list
playItem_t *playlist_tail[PL_MAX_ITERATORS]; // tail of linked list
int playlist_current_row[PL_MAX_ITERATORS]; // current row (cursor)
playItem_t *playlist_current_ptr; // pointer to a real current playlist item (or NULL)
} playlist_t;

extern playItem_t *playlist_head[PL_MAX_ITERATORS]; // head of linked list
extern playItem_t *playlist_tail[PL_MAX_ITERATORS]; // tail of linked list
extern int playlist_current_row[PL_MAX_ITERATORS]; // current row (cursor)
extern playItem_t *playlist_current_ptr; // pointer to a real current playlist item (or NULL)

int
pl_add_dir (const char *dirname, int (*cb)(playItem_t *it, void *data), void *user_data);
Expand Down Expand Up @@ -109,20 +115,6 @@ pl_insert_cue_from_buffer (playItem_t *after, playItem_t *origin, const uint8_t
playItem_t *
pl_insert_cue (playItem_t *after, playItem_t *origin, int numsamples, int samplerate);

//int
//pl_set_current (playItem_t *it);

// returns -1 if theres no next song, or playlist finished
// reason 0 means "song finished", 1 means "user clicked next"
int
pl_nextsong (int reason);

int
pl_prevsong (void);

int
pl_randomsong (void);

void
pl_add_meta (playItem_t *it, const char *key, const char *value);

Expand Down Expand Up @@ -172,9 +164,6 @@ pl_reset_cursor (void);
float
pl_get_totaltime (void);

playItem_t *
pl_getcurrent (void);

void
pl_set_selected (playItem_t *it, int sel);

Expand Down Expand Up @@ -230,4 +219,7 @@ pl_playqueue_test (playItem_t *it);
playItem_t *
pl_playqueue_getnext (void);

int
pl_playqueue_getcount (void);

#endif // __PLAYLIST_H
2 changes: 1 addition & 1 deletion plugins.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ static DB_functions_t deadbeef_api = {
.pl_sort = pl_sort,
.pl_get_totaltime = pl_get_totaltime,
.pl_getcount = pl_getcount,
.pl_getcurrent = (DB_playItem_t *(*)(void))pl_getcurrent,
.pl_getcurrent = (DB_playItem_t *(*)(void))streamer_get_current,
.pl_delete_selected = pl_delete_selected,
.pl_set_cursor = pl_set_cursor,
.pl_get_cursor = pl_get_cursor,
Expand Down
Loading

0 comments on commit 46aff62

Please sign in to comment.