wavpack: Pass correct sample count to format_samples()
[cmus.git] / pl.c
blobda460cf2022227f789b5c8c92a679cdd444e44d6
1 /*
2 * Copyright 2006 Timo Hirvonen
3 */
5 #include "pl.h"
6 #include "editable.h"
7 #include "options.h"
8 #include "xmalloc.h"
10 struct editable pl_editable;
11 struct simple_track *pl_cur_track = NULL;
13 static LIST_HEAD(pl_shuffle_head);
15 static void pl_free_track(struct list_head *item)
17 struct simple_track *track = to_simple_track(item);
19 if (track == pl_cur_track)
20 pl_cur_track = NULL;
22 list_del(&((struct shuffle_track *)track)->node);
23 track_info_unref(track->info);
24 free(track);
27 void pl_init(void)
29 editable_init(&pl_editable, pl_free_track);
32 static int dummy_filter(const struct simple_track *track)
34 return 1;
37 static struct track_info *set_track(struct simple_track *track)
39 struct track_info *ti = NULL;
41 if (track) {
42 pl_cur_track = track;
43 ti = track->info;
44 track_info_ref(ti);
45 pl_editable.win->changed = 1;
47 return ti;
50 struct track_info *pl_set_next(void)
52 struct simple_track *track;
54 if (list_empty(&pl_editable.head))
55 return NULL;
57 if (shuffle) {
58 track = (struct simple_track *)shuffle_list_get_next(&pl_shuffle_head,
59 (struct shuffle_track *)pl_cur_track, dummy_filter);
60 } else {
61 track = simple_list_get_next(&pl_editable.head, pl_cur_track, dummy_filter);
63 return set_track(track);
66 struct track_info *pl_set_prev(void)
68 struct simple_track *track;
70 if (list_empty(&pl_editable.head))
71 return NULL;
73 if (shuffle) {
74 track = (struct simple_track *)shuffle_list_get_prev(&pl_shuffle_head,
75 (struct shuffle_track *)pl_cur_track, dummy_filter);
76 } else {
77 track = simple_list_get_prev(&pl_editable.head, pl_cur_track, dummy_filter);
79 return set_track(track);
82 struct track_info *pl_set_selected(void)
84 struct iter sel;
86 if (list_empty(&pl_editable.head))
87 return NULL;
89 window_get_sel(pl_editable.win, &sel);
90 return set_track(iter_to_simple_track(&sel));
93 void pl_sel_current(void)
95 if (pl_cur_track) {
96 struct iter iter;
98 editable_track_to_iter(&pl_editable, pl_cur_track, &iter);
99 window_set_sel(pl_editable.win, &iter);
103 void pl_add_track(struct track_info *ti)
105 struct shuffle_track *track = xnew(struct shuffle_track, 1);
107 track_info_ref(ti);
108 simple_track_init((struct simple_track *)track, ti);
109 shuffle_list_add_track(&pl_shuffle_head, &track->node, pl_editable.nr_tracks);
110 editable_add(&pl_editable, (struct simple_track *)track);
113 void pl_reshuffle(void)
115 reshuffle(&pl_shuffle_head);
118 int pl_for_each(int (*cb)(void *data, struct track_info *ti), void *data)
120 struct simple_track *track;
121 int rc = 0;
123 list_for_each_entry(track, &pl_editable.head, node) {
124 rc = cb(data, track->info);
125 if (rc)
126 break;
128 return rc;