Skip to content

Commit

Permalink
playqueue API improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksiy-Yakovenko committed Jan 7, 2015
1 parent 7c274df commit 7d604f8
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 179 deletions.
3 changes: 2 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ deadbeef_SOURCES =\
handler.c handler.h\
strdupa.h\
escape.c escape.h\
tf.c tf.h
tf.c tf.h\
playqueue.c playqueue.h

# ConvertUTF/ConvertUTF.c ConvertUTF/ConvertUTF.h

Expand Down
37 changes: 29 additions & 8 deletions deadbeef.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,12 @@ enum {
DB_EV_TOGGLE_PAUSE = 12,
DB_EV_ACTIVATED = 13, // will be fired every time player is activated
DB_EV_PAUSED = 14, // player was paused or unpaused
DB_EV_PLAYLISTCHANGED = 15, // playlist contents were changed

DB_EV_PLAYLISTCHANGED = 15, // playlist contents were changed (e.g. metadata in any track)
// DB_EV_PLAYLISTCHANGED NOTE: it's usually sent on LARGE changes,
// when multiple tracks are affected, while for single tracks
// the DB_EV_TRACKINFOCHANGED is preferred

DB_EV_VOLUMECHANGED = 16, // volume was changed
DB_EV_OUTPUTCHANGED = 17, // sound output plugin changed
DB_EV_PLAYLISTSWITCHED = 18, // playlist switch occured
Expand All @@ -378,6 +383,7 @@ enum {

#if (DDB_API_LEVEL >= 8)
DB_EV_FOCUS_SELECTION, // tell playlist viewer to focus on selection
DB_EV_PLAYQUEUE_CHANGED, // sent on any change to playqueue
#endif

// -----------------
Expand All @@ -387,7 +393,11 @@ enum {
DB_EV_SONGCHANGED = 1000, // current song changed from one to another, ctx=ddb_event_trackchange_t
DB_EV_SONGSTARTED = 1001, // song started playing, ctx=ddb_event_track_t
DB_EV_SONGFINISHED = 1002, // song finished playing, ctx=ddb_event_track_t
DB_EV_TRACKINFOCHANGED = 1004, // trackinfo was changed (included medatata and playback status), ctx=ddb_event_track_t

DB_EV_TRACKINFOCHANGED = 1004, // trackinfo was changed (included medatata, playback status, playqueue state, etc), ctx=ddb_event_track_t
// DB_EV_TRACKINFOCHANGED NOTE: when multiple tracks change, DB_EV_PLAYLISTCHANGED may be sent instead,
// for speed reasons, so always handle both events.

DB_EV_SEEKED = 1005, // seek happened, ctx=ddb_event_playpos_t

// since 1.5
Expand Down Expand Up @@ -832,12 +842,12 @@ typedef struct {
void (*pl_set_item_replaygain) (DB_playItem_t *it, int idx, float value);
float (*pl_get_item_replaygain) (DB_playItem_t *it, int idx);

// playqueue support
int (*pl_playqueue_push) (DB_playItem_t *it);
void (*pl_playqueue_clear) (void);
void (*pl_playqueue_pop) (void);
void (*pl_playqueue_remove) (DB_playItem_t *it);
int (*pl_playqueue_test) (DB_playItem_t *it);
// playqueue support (obsolete since API 1.8)
int (*pl_playqueue_push) (DB_playItem_t *it) DEPRECATED_18;
void (*pl_playqueue_clear) (void) DEPRECATED_18;
void (*pl_playqueue_pop) (void) DEPRECATED_18;
void (*pl_playqueue_remove) (DB_playItem_t *it) DEPRECATED_18;
int (*pl_playqueue_test) (DB_playItem_t *it) DEPRECATED_18;

// volume control
void (*volume_set_db) (float dB);
Expand Down Expand Up @@ -1092,6 +1102,17 @@ typedef struct {
// outlen: the size of out buffer
// returns -1 on fail, output size on success
int (*tf_eval) (ddb_tf_context_t *ctx, char *code, int codelen, char *out, int outlen);

// new playqueue APIs
int (*playqueue_push) (DB_playItem_t *it);
void (*playqueue_pop) (void);
void (*playqueue_remove) (DB_playItem_t *it);
void (*playqueue_clear) (void);
int (*playqueue_test) (DB_playItem_t *it);
int (*playqueue_get_count) (void);
DB_playItem_t *(*playqueue_get_item) (int n);
int (*playqueue_remove_nth) (int n);
void (*playqueue_insert_at) (int n, DB_playItem_t *it);
#endif
} DB_functions_t;

Expand Down
4 changes: 2 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ player_mainloop (void) {
{
save_resume_state ();

pl_playqueue_clear ();
playqueue_clear ();

// stop streaming and playback before unloading plugins
DB_output_t *output = plug_get_output ();
Expand All @@ -587,7 +587,7 @@ player_mainloop (void) {
streamer_play_current_track ();
break;
case DB_EV_PLAY_NUM:
pl_playqueue_clear ();
playqueue_clear ();
streamer_set_nextsong (p1, 4);
break;
case DB_EV_STOP:
Expand Down
137 changes: 13 additions & 124 deletions playlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#include "escape.h"
#include "strdupa.h"
#include "tf.h"
#include "playqueue.h"

// disable custom title function, until we have new title formatting (0.7)
#define DISABLE_CUSTOM_TITLE
Expand Down Expand Up @@ -98,10 +99,6 @@

#define min(x,y) ((x)<(y)?(x):(y))

#define PLAYQUEUE_SIZE 100
static playItem_t *playqueue[100];
static int playqueue_count = 0;

static int playlists_count = 0;
static playlist_t *playlists_head = NULL;
static playlist_t *playlist = NULL; // current playlist
Expand Down Expand Up @@ -179,7 +176,7 @@ void
pl_free (void) {
trace ("pl_free\n");
LOCK;
pl_playqueue_clear ();
playqueue_clear ();
plt_loading = 1;
while (playlists_head) {

Expand Down Expand Up @@ -1619,7 +1616,7 @@ plt_remove_item (playlist_t *playlist, playItem_t *it) {

if (!no_remove_notify) {
streamer_song_removed_notify (it);
pl_playqueue_remove (it);
playqueue_remove (it);
}

// remove from both lists
Expand Down Expand Up @@ -2808,7 +2805,7 @@ pl_format_item_queue (playItem_t *it, char *s, int size) {
e++;
}
}
int n = e - val;
int n = (int)(e - val);
if (n > size-1) {
n = size-1;
}
Expand All @@ -2827,19 +2824,23 @@ pl_format_item_queue (playItem_t *it, char *s, int size) {
}
}

if (!playqueue_count) {
int pq_cnt = playqueue_getcount ();

if (!pq_cnt) {
UNLOCK;
return 0;
}

int qinitsize = size;
int init = 1;
int len;
for (int i = 0; i < playqueue_count; i++) {
for (int i = 0; i < pq_cnt; i++) {
if (size <= 0) {
break;
}
if (playqueue[i] == it) {

playItem_t *pqitem = playqueue_get_item (i);
if (pqitem == it) {
if (init) {
init = 0;
s[0] = '(';
Expand All @@ -2853,6 +2854,7 @@ pl_format_item_queue (playItem_t *it, char *s, int size) {
s += len;
size -= len;
}
pl_item_unref (pqitem);
}
if (size != qinitsize && size > 0) {
len = snprintf (s, size, ")");
Expand Down Expand Up @@ -3860,7 +3862,7 @@ plt_search_process (playlist_t *playlist, const char *text) {
UNLOCK;
}

static void
void
send_trackinfochanged (playItem_t *track) {
ddb_event_track_t *ev = (ddb_event_track_t *)messagepump_event_alloc (DB_EV_TRACKINFOCHANGED);
ev->track = DB_PLAYITEM (track);
Expand All @@ -3870,119 +3872,6 @@ send_trackinfochanged (playItem_t *track) {
messagepump_push_event ((ddb_event_t*)ev, 0, 0);
}

int
pl_playqueue_push (playItem_t *it) {
if (playqueue_count == PLAYQUEUE_SIZE) {
trace ("playqueue is full\n");
return -1;
}
LOCK;
pl_item_ref (it);
playqueue[playqueue_count++] = it;
for (int i = 0; i < playqueue_count; i++) {
send_trackinfochanged (playqueue[i]);
}
UNLOCK;
return 0;
}

void
pl_playqueue_clear (void) {
LOCK;
int cnt = playqueue_count;
playqueue_count = 0;
int i;
for (i = 0; i < cnt; i++) {
send_trackinfochanged (playqueue[i]);
}
for (i = 0; i < cnt; i++) {
pl_item_unref (playqueue[i]);
playqueue[i] = NULL;
}
UNLOCK;
}

void
pl_playqueue_pop (void) {
if (!playqueue_count) {
return;
}
LOCK;
if (playqueue_count == 1) {
playqueue_count = 0;
send_trackinfochanged (playqueue[0]);
pl_item_unref (playqueue[0]);
UNLOCK;
return;
}
playItem_t *it = playqueue[0];
memmove (&playqueue[0], &playqueue[1], (playqueue_count-1) * sizeof (playItem_t*));
playqueue_count--;
send_trackinfochanged (it);
for (int i = 0; i < playqueue_count; i++) {
send_trackinfochanged (playqueue[i]);
}
pl_item_unref (it);
UNLOCK;
}

void
pl_playqueue_remove (playItem_t *it) {
LOCK;
for (;;) {
int i;
for (i = 0; i < playqueue_count; i++) {
if (playqueue[i] == it) {
if (i < playqueue_count-1) {
memmove (&playqueue[i], &playqueue[i+1], (playqueue_count-i) * sizeof (playItem_t*));
}
send_trackinfochanged (it);
pl_item_unref (it);
playqueue_count--;
break;
}
}
if (i == playqueue_count) {
break;
}
}
for (int i = 0; i < playqueue_count; i++) {
send_trackinfochanged (playqueue[i]);
}
UNLOCK;
}

int
pl_playqueue_test (playItem_t *it) {
LOCK;
for (int i = 0; i < playqueue_count; i++) {
if (playqueue[i] == it) {
UNLOCK;
return i;
}
}
UNLOCK;
return -1;
}

playItem_t *
pl_playqueue_getnext (void) {
LOCK;
if (playqueue_count > 0) {
playItem_t *val = playqueue[0];
pl_item_ref (val);
UNLOCK;
return val;
}
UNLOCK;
return NULL;
}

int
pl_playqueue_getcount (void) {
return playqueue_count;
}

void
pl_items_copy_junk (playItem_t *from, playItem_t *first, playItem_t *last) {
LOCK;
Expand Down
25 changes: 3 additions & 22 deletions playlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,28 +420,6 @@ plt_sort (playlist_t *plt, int iter, int id, const char *format, int order);
void
plt_sort_random (playlist_t *plt, int iter);

// playqueue support functions
int
pl_playqueue_push (playItem_t *it);

void
pl_playqueue_clear (void);

void
pl_playqueue_pop (void);

void
pl_playqueue_remove (playItem_t *it);

int
pl_playqueue_test (playItem_t *it);

playItem_t *
pl_playqueue_getnext (void);

int
pl_playqueue_getcount (void);

void
pl_items_copy_junk (struct playItem_s *from, struct playItem_s *first, struct playItem_s *last);

Expand Down Expand Up @@ -538,4 +516,7 @@ pl_format_duration (playItem_t *it, const char *ret, char *dur, int size);
int
pl_format_item_queue (playItem_t *it, char *s, int size);

void
send_trackinfochanged (playItem_t *track);

#endif // __PLAYLIST_H
Loading

0 comments on commit 7d604f8

Please sign in to comment.