2 * Copyright 2006 Timo Hirvonen
7 #include "track_info.h"
8 #include "search_mode.h"
17 void simple_track_init(struct simple_track
*track
, struct track_info
*ti
)
23 struct simple_track
*simple_track_new(struct track_info
*ti
)
25 struct simple_track
*t
= xnew(struct simple_track
, 1);
28 simple_track_init(t
, ti
);
32 GENERIC_ITER_PREV(simple_track_get_prev
, struct simple_track
, node
)
33 GENERIC_ITER_NEXT(simple_track_get_next
, struct simple_track
, node
)
35 int simple_track_search_get_current(void *data
, struct iter
*iter
)
37 return window_get_sel(data
, iter
);
40 int simple_track_search_matches(void *data
, struct iter
*iter
, const char *text
)
42 unsigned int flags
= TI_MATCH_TITLE
;
43 struct simple_track
*track
= iter_to_simple_track(iter
);
45 if (!search_restricted
)
46 flags
|= TI_MATCH_ARTIST
| TI_MATCH_ALBUM
;
48 if (!track_info_matches(track
->info
, text
, flags
))
51 window_set_sel(data
, iter
);
55 struct shuffle_track
*shuffle_list_get_next(struct list_head
*head
, struct shuffle_track
*cur
,
56 int (*filter
)(const struct simple_track
*))
58 struct list_head
*item
;
61 return to_shuffle_track(head
->next
);
63 item
= cur
->node
.next
;
65 while (item
!= head
) {
66 struct shuffle_track
*track
= to_shuffle_track(item
);
68 if (filter((struct simple_track
*)track
))
81 struct shuffle_track
*shuffle_list_get_prev(struct list_head
*head
, struct shuffle_track
*cur
,
82 int (*filter
)(const struct simple_track
*))
84 struct list_head
*item
;
87 return to_shuffle_track(head
->next
);
89 item
= cur
->node
.prev
;
91 while (item
!= head
) {
92 struct shuffle_track
*track
= to_shuffle_track(item
);
94 if (filter((struct simple_track
*)track
))
107 struct simple_track
*simple_list_get_next(struct list_head
*head
, struct simple_track
*cur
,
108 int (*filter
)(const struct simple_track
*))
110 struct list_head
*item
;
113 return to_simple_track(head
->next
);
115 item
= cur
->node
.next
;
117 while (item
!= head
) {
118 struct simple_track
*track
= to_simple_track(item
);
130 struct simple_track
*simple_list_get_prev(struct list_head
*head
, struct simple_track
*cur
,
131 int (*filter
)(const struct simple_track
*))
133 struct list_head
*item
;
136 return to_simple_track(head
->next
);
138 item
= cur
->node
.prev
;
140 while (item
!= head
) {
141 struct simple_track
*track
= to_simple_track(item
);
153 void sorted_list_add_track(struct list_head
*head
, struct simple_track
*track
, const char * const *keys
)
155 struct list_head
*item
;
157 /* It is _much_ faster to iterate in reverse order because playlist
158 * file is usually sorted.
161 while (item
!= head
) {
162 const struct simple_track
*a
= to_simple_track(&track
->node
);
163 const struct simple_track
*b
= to_simple_track(item
);
165 if (track_info_cmp(a
->info
, b
->info
, keys
) >= 0)
170 list_add(&track
->node
, item
);
173 void list_add_rand(struct list_head
*head
, struct list_head
*node
, int nr
)
175 struct list_head
*item
;
178 pos
= rand() % (nr
+ 1);
186 list_add(node
, item
);
193 /* add before item */
194 list_add_tail(node
, item
);
198 void reshuffle(struct list_head
*head
)
200 struct list_head
*item
, *last
;
203 if (list_empty(head
))
211 struct list_head
*next
= item
->next
;
213 list_add_rand(head
, item
, i
++);
220 int simple_list_for_each_marked(struct list_head
*head
,
221 int (*cb
)(void *data
, struct track_info
*ti
), void *data
, int reverse
)
223 struct simple_track
*t
;
227 list_for_each_entry_reverse(t
, head
, node
) {
229 rc
= cb(data
, t
->info
);
235 list_for_each_entry(t
, head
, node
) {
237 rc
= cb(data
, t
->info
);