VLC  4.0.0-dev
randomizer.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * randomizer.h
3  *****************************************************************************
4  * Copyright (C) 2018 VLC authors and VideoLAN
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20 
21 #ifndef RANDOMIZER_H
22 #define RANDOMIZER_H
23 
24 #include <vlc_common.h>
25 #include <vlc_vector.h>
26 
28 
29 /**
30  * \defgroup playlist_randomizer Playlist randomizer helper
31  * \ingroup playlist
32  * @{ */
33 
34 /**
35  * Playlist helper to manage random playback.
36  *
37  * See randomizer.c for implementation details.
38  */
39 struct randomizer {
41  unsigned short xsubi[3]; /* random state */
42  bool loop;
43  size_t head;
44  size_t next;
45  size_t history;
46 };
47 
48 /**
49  * Initialize an empty randomizer.
50  */
51 void
53 
54 /**
55  * Destroy a randomizer.
56  */
57 void
59 
60 /**
61  * Enable or disable "loop" mode.
62  *
63  * This affects the behavior of prev/next.
64  */
65 void
66 randomizer_SetLoop(struct randomizer *randomizer, bool loop);
67 
68 /**
69  * Return the number of items in the randomizer.
70  */
71 bool
73 
74 /**
75  * Start a new random cycle.
76  *
77  * The "history" is lost, and "next" can be called _n_ times if the randomizer
78  * contains _n_ items (when loop is disabled).
79  */
80 void
82 
83 /**
84  * Indicate whether there is a previous item.
85  */
86 bool
88 
89 /**
90  * Indicate whether there is a next item.
91  */
92 bool
94 
95 /**
96  * Peek the previous item (without changing the current one).
97  */
100 
101 /**
102  * Peek the next item (without changing the current one).
103  */
106 
107 /**
108  * Go back to the previous item.
109  */
112 
113 /**
114  * Go back to the next item.
115  */
118 
119 /**
120  * Force the selection of a specific item.
121  *
122  * This function should be called when the user requested to play a specific
123  * item in the playlist.
124  */
125 void
127  const vlc_playlist_item_t *item);
128 
129 /**
130  * Add items to the randomizer.
131  *
132  * This function should be called when items are added to the playlist.
133  */
134 bool
136  size_t count);
137 
138 /**
139  * Remove items from the randomizer.
140  *
141  * This function should be called when items are removed from the playlist.
142  */
143 void
145  vlc_playlist_item_t *const items[], size_t count);
146 
147 /**
148  * Clear the randomizer.
149  */
150 void
152 
153 /** @} */
154 
155 #endif
VLC_VECTOR
#define VLC_VECTOR(type)
Vector struct body.
Definition: vlc_vector.h:65
randomizer_Prev
vlc_playlist_item_t * randomizer_Prev(struct randomizer *r)
Go back to the previous item.
Definition: randomizer.c:404
count
size_t count
Definition: core.c:401
randomizer::loop
bool loop
Definition: randomizer.h:42
randomizer::size
size_t size
Definition: randomizer.h:40
randomizer_RemoveAt
static void randomizer_RemoveAt(struct randomizer *r, size_t index)
Definition: randomizer.c:477
vlc_rand_bytes
void vlc_rand_bytes(void *buf, size_t len)
Definition: rand.c:28
vlc_common.h
main
int main(void)
Definition: test.c:310
randomizer_Destroy
void randomizer_Destroy(struct randomizer *r)
Destroy a randomizer.
Definition: randomizer.c:275
randomizer_Count
bool randomizer_Count(struct randomizer *r)
Return the number of items in the randomizer.
Definition: randomizer.c:295
nrand48
long nrand48(unsigned short subi[3])
randomizer.h
randomizer::data
vlc_playlist_item_t ** data
Definition: randomizer.h:40
randomizer_AutoReshuffle
static void randomizer_AutoReshuffle(struct randomizer *r)
Definition: randomizer.c:341
randomizer_Init
void randomizer_Init(struct randomizer *r)
Initialize an empty randomizer.
Definition: randomizer.c:260
vlc_vector_init
#define vlc_vector_init(pv)
Initialize an empty vector.
Definition: vlc_vector.h:80
vlc_playlist_item
Definition: item.h:29
randomizer_RemoveOne
static void randomizer_RemoveOne(struct randomizer *r, const vlc_playlist_item_t *item)
Definition: randomizer.c:520
randomizer::next
size_t next
Definition: randomizer.h:44
randomizer::history
size_t history
Definition: randomizer.h:45
vlc_rand.h
randomizer_Select
void randomizer_Select(struct randomizer *r, const vlc_playlist_item_t *item)
Force the selection of a specific item.
Definition: randomizer.c:469
vlc_vector_insert_all
#define vlc_vector_insert_all(pv, index, items, count)
Insert count items at the given index.
Definition: vlc_vector.h:411
randomizer_Add
bool randomizer_Add(struct randomizer *r, vlc_playlist_item_t *items[], size_t count)
Add items to the randomizer.
Definition: randomizer.c:424
randomizer_DetermineOne
static void randomizer_DetermineOne(struct randomizer *r)
Definition: randomizer.c:332
randomizer
Playlist helper to manage random playback.
Definition: randomizer.h:39
randomizer_Remove
void randomizer_Remove(struct randomizer *r, vlc_playlist_item_t *const items[], size_t count)
Remove items from the randomizer.
Definition: randomizer.c:528
randomizer_HasNext
bool randomizer_HasNext(struct randomizer *r)
Indicate whether there is a next item.
Definition: randomizer.c:372
randomizer_Clear
void randomizer_Clear(struct randomizer *r)
Clear the randomizer.
Definition: randomizer.c:538
randomizer_Reshuffle
void randomizer_Reshuffle(struct randomizer *r)
Start a new random cycle.
Definition: randomizer.c:301
vlc_vector.h
vlc_vector_clear
#define vlc_vector_clear(pv)
Clear a vector.
Definition: vlc_vector.h:101
NOT_SAME_BEFORE
#define NOT_SAME_BEFORE
Definition: randomizer.c:257
vlc_vector_index_of
#define vlc_vector_index_of(pv, item, pidx)
Return the index of an item.
Definition: vlc_vector.h:615
vlc_vector_autoshrink
#define vlc_vector_autoshrink(pv)
Resize the vector down automatically.
Definition: vlc_vector.h:293
randomizer::items
struct randomizer::@113 items
randomizer_SetLoop
void randomizer_SetLoop(struct randomizer *r, bool loop)
Enable or disable "loop" mode.
Definition: randomizer.c:281
vlc_vector_destroy
#define vlc_vector_destroy(pv)
Destroy a vector.
Definition: vlc_vector.h:93
randomizer_IndexOf
static ssize_t randomizer_IndexOf(struct randomizer *r, const vlc_playlist_item_t *item)
Definition: randomizer.c:287
randomizer_SelectIndex
static void randomizer_SelectIndex(struct randomizer *r, size_t index)
Definition: randomizer.c:436
randomizer::head
size_t head
Definition: randomizer.h:43
randomizer_Next
vlc_playlist_item_t * randomizer_Next(struct randomizer *r)
Go back to the next item.
Definition: randomizer.c:413
randomizer_PeekPrev
vlc_playlist_item_t * randomizer_PeekPrev(struct randomizer *r)
Peek the previous item (without changing the current one).
Definition: randomizer.c:378
randomizer_PeekNext
vlc_playlist_item_t * randomizer_PeekNext(struct randomizer *r)
Peek the next item (without changing the current one).
Definition: randomizer.c:386
swap_items
static void swap_items(struct randomizer *r, int i, int j)
Definition: randomizer.c:310
randomizer_DetermineOne_
static void randomizer_DetermineOne_(struct randomizer *r, size_t avoid_last_n)
Definition: randomizer.c:318
randomizer::xsubi
unsigned short xsubi[3]
Definition: randomizer.h:41
randomizer_HasPrev
bool randomizer_HasPrev(struct randomizer *r)
Indicate whether there is a previous item.
Definition: randomizer.c:356