MPD
queue.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003-2010 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
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.
9  *
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.
14  *
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  */
19 
20 #ifndef QUEUE_H
21 #define QUEUE_H
22 
23 #include <glib.h>
24 
25 #include <assert.h>
26 #include <stdbool.h>
27 #include <stdint.h>
28 
29 enum {
35 };
36 
41 struct queue_item {
42  struct song *song;
43 
45  unsigned id;
46 
48  uint32_t version;
49 };
50 
61 struct queue {
63  unsigned max_length;
64 
66  unsigned length;
67 
69  uint32_t version;
70 
72  struct queue_item *items;
73 
75  unsigned *order;
76 
79 
82  bool repeat;
83 
85  bool single;
86 
88  bool consume;
89 
91  bool random;
92 
94  GRand *rand;
95 };
96 
97 static inline unsigned
98 queue_length(const struct queue *queue)
99 {
100  assert(queue->length <= queue->max_length);
101 
102  return queue->length;
103 }
104 
108 static inline bool
109 queue_is_empty(const struct queue *queue)
110 {
111  return queue->length == 0;
112 }
113 
117 static inline bool
118 queue_is_full(const struct queue *queue)
119 {
120  assert(queue->length <= queue->max_length);
121 
122  return queue->length >= queue->max_length;
123 }
124 
128 static inline bool
129 queue_valid_position(const struct queue *queue, unsigned position)
130 {
131  return position < queue->length;
132 }
133 
137 static inline bool
138 queue_valid_order(const struct queue *queue, unsigned order)
139 {
140  return order < queue->length;
141 }
142 
143 static inline int
144 queue_id_to_position(const struct queue *queue, unsigned id)
145 {
146  if (id >= queue->max_length * QUEUE_HASH_MULT)
147  return -1;
148 
149  assert(queue->id_to_position[id] >= -1);
150  assert(queue->id_to_position[id] < (int)queue->length);
151 
152  return queue->id_to_position[id];
153 }
154 
155 static inline int
156 queue_position_to_id(const struct queue *queue, unsigned position)
157 {
158  assert(position < queue->length);
159 
160  return queue->items[position].id;
161 }
162 
163 static inline unsigned
164 queue_order_to_position(const struct queue *queue, unsigned order)
165 {
166  assert(order < queue->length);
167 
168  return queue->order[order];
169 }
170 
171 static inline unsigned
172 queue_position_to_order(const struct queue *queue, unsigned position)
173 {
174  assert(position < queue->length);
175 
176  for (unsigned i = 0;; ++i) {
177  assert(i < queue->length);
178 
179  if (queue->order[i] == position)
180  return i;
181  }
182 }
183 
187 static inline struct song *
188 queue_get(const struct queue *queue, unsigned position)
189 {
190  assert(position < queue->length);
191 
192  return queue->items[position].song;
193 }
194 
198 static inline struct song *
199 queue_get_order(const struct queue *queue, unsigned order)
200 {
201  return queue_get(queue, queue_order_to_position(queue, order));
202 }
203 
208 static inline bool
209 queue_song_newer(const struct queue *queue, unsigned position,
210  uint32_t version)
211 {
212  assert(position < queue->length);
213 
214  return version > queue->version ||
215  queue->items[position].version >= version ||
216  queue->items[position].version == 0;
217 }
218 
222 void
223 queue_init(struct queue *queue, unsigned max_length);
224 
229 void
230 queue_finish(struct queue *queue);
231 
238 int
239 queue_next_order(const struct queue *queue, unsigned order);
240 
245 void
247 
252 void
253 queue_modify(struct queue *queue, unsigned order);
254 
258 void
259 queue_modify_all(struct queue *queue);
260 
268 unsigned
269 queue_append(struct queue *queue, struct song *song);
270 
274 void
275 queue_swap(struct queue *queue, unsigned position1, unsigned position2);
276 
280 static inline void
281 queue_swap_order(struct queue *queue, unsigned order1, unsigned order2)
282 {
283  unsigned tmp = queue->order[order1];
284  queue->order[order1] = queue->order[order2];
285  queue->order[order2] = tmp;
286 }
287 
291 void
292 queue_move(struct queue *queue, unsigned from, unsigned to);
293 
297 void
298 queue_move_range(struct queue *queue, unsigned start, unsigned end, unsigned to);
299 
303 void
304 queue_delete(struct queue *queue, unsigned position);
305 
309 void
310 queue_clear(struct queue *queue);
311 
315 static inline void
317 {
318  for (unsigned i = 0; i < queue->length; ++i)
319  queue->order[i] = i;
320 }
321 
326 void
328 
334 void
335 queue_shuffle_order_last(struct queue *queue, unsigned start, unsigned end);
336 
341 void
342 queue_shuffle_range(struct queue *queue, unsigned start, unsigned end);
343 
344 #endif